Skip to content

Commit 8df25df

Browse files
committed
Use autoreleasepool inside the encryption / decryption loop
Fixes #8
1 parent 1055df4 commit 8df25df

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

Sources/CryptomatorCryptoLib/Cryptor.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,15 @@ public class Cryptor {
224224
// encrypt and write ciphertext content:
225225
var chunkNumber: UInt64 = 0
226226
while cleartextStream.hasBytesAvailable {
227-
guard let cleartextChunk = try cleartextStream.read(maxLength: cleartextChunkSize) else {
228-
continue
227+
try autoreleasepool {
228+
guard let cleartextChunk = try cleartextStream.read(maxLength: cleartextChunkSize) else {
229+
return
230+
}
231+
let ciphertextChunk = try encryptSingleChunk(cleartextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
232+
ciphertextStream.write(ciphertextChunk, maxLength: ciphertextChunk.count)
233+
progress.completedUnitCount += Int64(ciphertextChunk.count)
234+
chunkNumber += 1
229235
}
230-
let ciphertextChunk = try encryptSingleChunk(cleartextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
231-
ciphertextStream.write(ciphertextChunk, maxLength: ciphertextChunk.count)
232-
progress.completedUnitCount += Int64(ciphertextChunk.count)
233-
chunkNumber += 1
234236
}
235237
}
236238

@@ -286,13 +288,15 @@ public class Cryptor {
286288
let ciphertextChunkSize = contentCryptor.nonceLen + cleartextChunkSize + contentCryptor.tagLen
287289
var chunkNumber: UInt64 = 0
288290
while ciphertextStream.hasBytesAvailable {
289-
guard let ciphertextChunk = try ciphertextStream.read(maxLength: ciphertextChunkSize) else {
290-
continue
291+
try autoreleasepool {
292+
guard let ciphertextChunk = try ciphertextStream.read(maxLength: ciphertextChunkSize) else {
293+
return
294+
}
295+
let cleartextChunk = try decryptSingleChunk(ciphertextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
296+
cleartextStream.write(cleartextChunk, maxLength: cleartextChunk.count)
297+
progress.completedUnitCount += Int64(cleartextChunk.count)
298+
chunkNumber += 1
291299
}
292-
let cleartextChunk = try decryptSingleChunk(ciphertextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey)
293-
cleartextStream.write(cleartextChunk, maxLength: cleartextChunk.count)
294-
progress.completedUnitCount += Int64(cleartextChunk.count)
295-
chunkNumber += 1
296300
}
297301
}
298302

0 commit comments

Comments
 (0)