From 8df25dfb2451125efb9fa6e4b385611ac4360753 Mon Sep 17 00:00:00 2001 From: Philipp Schmid <25935690+phil1995@users.noreply.github.com> Date: Fri, 15 Oct 2021 18:26:26 +0200 Subject: [PATCH] Use `autoreleasepool` inside the encryption / decryption loop Fixes #8 --- Sources/CryptomatorCryptoLib/Cryptor.swift | 28 ++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Sources/CryptomatorCryptoLib/Cryptor.swift b/Sources/CryptomatorCryptoLib/Cryptor.swift index 8d40c94..3dae122 100644 --- a/Sources/CryptomatorCryptoLib/Cryptor.swift +++ b/Sources/CryptomatorCryptoLib/Cryptor.swift @@ -224,13 +224,15 @@ public class Cryptor { // encrypt and write ciphertext content: var chunkNumber: UInt64 = 0 while cleartextStream.hasBytesAvailable { - guard let cleartextChunk = try cleartextStream.read(maxLength: cleartextChunkSize) else { - continue + try autoreleasepool { + guard let cleartextChunk = try cleartextStream.read(maxLength: cleartextChunkSize) else { + return + } + let ciphertextChunk = try encryptSingleChunk(cleartextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey) + ciphertextStream.write(ciphertextChunk, maxLength: ciphertextChunk.count) + progress.completedUnitCount += Int64(ciphertextChunk.count) + chunkNumber += 1 } - let ciphertextChunk = try encryptSingleChunk(cleartextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey) - ciphertextStream.write(ciphertextChunk, maxLength: ciphertextChunk.count) - progress.completedUnitCount += Int64(ciphertextChunk.count) - chunkNumber += 1 } } @@ -286,13 +288,15 @@ public class Cryptor { let ciphertextChunkSize = contentCryptor.nonceLen + cleartextChunkSize + contentCryptor.tagLen var chunkNumber: UInt64 = 0 while ciphertextStream.hasBytesAvailable { - guard let ciphertextChunk = try ciphertextStream.read(maxLength: ciphertextChunkSize) else { - continue + try autoreleasepool { + guard let ciphertextChunk = try ciphertextStream.read(maxLength: ciphertextChunkSize) else { + return + } + let cleartextChunk = try decryptSingleChunk(ciphertextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey) + cleartextStream.write(cleartextChunk, maxLength: cleartextChunk.count) + progress.completedUnitCount += Int64(cleartextChunk.count) + chunkNumber += 1 } - let cleartextChunk = try decryptSingleChunk(ciphertextChunk, chunkNumber: chunkNumber, headerNonce: header.nonce, fileKey: header.contentKey) - cleartextStream.write(cleartextChunk, maxLength: cleartextChunk.count) - progress.completedUnitCount += Int64(cleartextChunk.count) - chunkNumber += 1 } }