diff --git a/Aaru.Images/AaruFormat/AaruFormat.cs b/Aaru.Images/AaruFormat/AaruFormat.cs index 3750bffb0..9011fdbd8 100644 --- a/Aaru.Images/AaruFormat/AaruFormat.cs +++ b/Aaru.Images/AaruFormat/AaruFormat.cs @@ -90,7 +90,8 @@ namespace Aaru.DiscImages Dictionary _blockHeaderCache; /// Provides checksum for deduplication of sectors. SHA256 _checksumProvider; - bool _compress; + bool _compress; + byte[] _compressedBuffer; /// Provides CRC64. Crc64Context _crc64; /// Header of the currently writing block. @@ -101,8 +102,6 @@ namespace Aaru.DiscImages uint _currentCacheSize; /// Cache of DDT entries. Dictionary _ddtEntryCache; - - //NonClosableStream _decompressedStream; bool _deduplicate; /// On-memory deduplication table indexed by checksum. Dictionary _deduplicationTable; diff --git a/Aaru.Images/AaruFormat/Write.cs b/Aaru.Images/AaruFormat/Write.cs index d43f2b345..f3b11caa5 100644 --- a/Aaru.Images/AaruFormat/Write.cs +++ b/Aaru.Images/AaruFormat/Write.cs @@ -1754,7 +1754,6 @@ namespace Aaru.DiscImages var cmpCrc64Context = new Crc64Context(); byte[] lzmaProperties = Array.Empty(); - byte[] compressedBuffer = new byte[_writingBuffer.Length]; int compressedLength = 0; switch(_currentBlockHeader.compression) @@ -1777,7 +1776,7 @@ namespace Aaru.DiscImages for(int r = 0; r < remaining * 4; r++) _writingBuffer[_writingBufferPosition + r] = 0; - compressedLength = FLAC.EncodeBuffer(_writingBuffer, compressedBuffer, flacBlockSize, true, + compressedLength = FLAC.EncodeBuffer(_writingBuffer, _compressedBuffer, flacBlockSize, true, false, "partial_tukey(0/1.0/1.0)", 12, 0, true, false, 0, 8, "Aaru"); @@ -1788,7 +1787,7 @@ namespace Aaru.DiscImages } case CompressionType.Lzma: { - compressedLength = LZMA.EncodeBuffer(_writingBuffer, compressedBuffer, out lzmaProperties, 9, + compressedLength = LZMA.EncodeBuffer(_writingBuffer, _compressedBuffer, out lzmaProperties, 9, _dictionarySize, 3, 0, 2, 273); cmpCrc64Context.Update(lzmaProperties); @@ -1807,7 +1806,7 @@ namespace Aaru.DiscImages } else { - cmpCrc64Context.Update(compressedBuffer, (uint)compressedLength); + cmpCrc64Context.Update(_compressedBuffer, (uint)compressedLength); _currentBlockHeader.cmpCrc64 = BitConverter.ToUInt64(cmpCrc64Context.Final(), 0); _currentBlockHeader.cmpLength = (uint)compressedLength; } @@ -1833,15 +1832,15 @@ namespace Aaru.DiscImages if(_currentBlockHeader.compression == CompressionType.None) _imageStream.Write(_writingBuffer, 0, _writingBufferPosition); else - _imageStream.Write(compressedBuffer, 0, compressedLength); + _imageStream.Write(_compressedBuffer, 0, compressedLength); - _writingBuffer = null; + _writingBufferPosition = 0; GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, false); _currentBlockOffset = 0; } // No block set - if(_writingBuffer == null) + if(_writingBufferPosition == 0) { _currentBlockHeader = new BlockHeader { @@ -1866,8 +1865,15 @@ namespace Aaru.DiscImages _currentBlockHeader.compression == CompressionType.Flac) _currentBlockHeader.compression = CompressionType.Lzma; - // TODO: Create at file creation - _writingBuffer = new byte[(1 << _shift) * data.Length * 2]; + int maxBufferSize = ((1 << _shift) * data.Length) + (MAX_FLAKE_BLOCK * 4); + + if(_writingBuffer == null || + _writingBuffer.Length < maxBufferSize) + { + _writingBuffer = new byte[maxBufferSize]; + _compressedBuffer = new byte[maxBufferSize * 2]; + } + _writingBufferPosition = 0; _crc64 = new Crc64Context(); } @@ -2472,7 +2478,6 @@ namespace Aaru.DiscImages var cmpCrc64Context = new Crc64Context(); byte[] lzmaProperties = Array.Empty(); - byte[] compressedBuffer = new byte[_writingBuffer.Length]; int compressedLength = 0; switch(_currentBlockHeader.compression) @@ -2495,7 +2500,7 @@ namespace Aaru.DiscImages for(int r = 0; r < remaining * 4; r++) _writingBuffer[_writingBufferPosition + r] = 0; - compressedLength = FLAC.EncodeBuffer(_writingBuffer, compressedBuffer, flacBlockSize, true, + compressedLength = FLAC.EncodeBuffer(_writingBuffer, _compressedBuffer, flacBlockSize, true, false, "partial_tukey(0/1.0/1.0)", 12, 0, true, false, 0, 8, "Aaru"); @@ -2506,7 +2511,7 @@ namespace Aaru.DiscImages } case CompressionType.Lzma: { - compressedLength = LZMA.EncodeBuffer(_writingBuffer, compressedBuffer, out lzmaProperties, 9, + compressedLength = LZMA.EncodeBuffer(_writingBuffer, _compressedBuffer, out lzmaProperties, 9, _dictionarySize, 3, 0, 2, 273); cmpCrc64Context.Update(lzmaProperties); @@ -2525,7 +2530,7 @@ namespace Aaru.DiscImages } else { - cmpCrc64Context.Update(compressedBuffer, (uint)compressedLength); + cmpCrc64Context.Update(_compressedBuffer, (uint)compressedLength); _currentBlockHeader.cmpCrc64 = BitConverter.ToUInt64(cmpCrc64Context.Final(), 0); _currentBlockHeader.cmpLength = (uint)compressedLength; } @@ -2551,7 +2556,7 @@ namespace Aaru.DiscImages if(_currentBlockHeader.compression == CompressionType.None) _imageStream.Write(_writingBuffer, 0, _writingBufferPosition); else - _imageStream.Write(compressedBuffer, 0, compressedLength); + _imageStream.Write(_compressedBuffer, 0, compressedLength); _writingBuffer = null; }