From c057ffb153bedace076b102efc6586d60ba26bf2 Mon Sep 17 00:00:00 2001 From: Twan van Dongen Date: Sat, 27 Jan 2024 18:59:56 +0100 Subject: [PATCH] Refrormatted using CSharpier --- src/SharpCompress/Common/Zip/ZipFilePart.cs | 7 ++- .../Compressors/Shrink/BitStream.cs | 37 ++++++------ .../Compressors/Shrink/HwUnshrink.cs | 59 +++++++++++++++---- .../Compressors/Shrink/ShrinkStream.cs | 32 ++++++++-- 4 files changed, 100 insertions(+), 35 deletions(-) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index 0463c97f..f8a0e8a8 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -82,7 +82,12 @@ internal abstract class ZipFilePart : FilePart } case ZipCompressionMethod.Shrink: { - return new ShrinkStream(stream, CompressionMode.Decompress, Header.CompressedSize, Header.UncompressedSize); + return new ShrinkStream( + stream, + CompressionMode.Decompress, + Header.CompressedSize, + Header.UncompressedSize + ); } case ZipCompressionMethod.Deflate: { diff --git a/src/SharpCompress/Compressors/Shrink/BitStream.cs b/src/SharpCompress/Compressors/Shrink/BitStream.cs index 76b59041..ef01e2af 100644 --- a/src/SharpCompress/Compressors/Shrink/BitStream.cs +++ b/src/SharpCompress/Compressors/Shrink/BitStream.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; - namespace SharpCompress.Compressors.Shrink { internal class BitStream @@ -14,26 +13,26 @@ namespace SharpCompress.Compressors.Shrink private int _byteIdx; private int _bitIdx; private int _bitsLeft; - private ulong _bitBuffer; + private ulong _bitBuffer; private static uint[] _maskBits = new uint[17] { - 0U, - 1U, - 3U, - 7U, - 15U, - 31U, - 63U, - (uint) sbyte.MaxValue, - (uint) byte.MaxValue, - 511U, - 1023U, - 2047U, - 4095U, - 8191U, - 16383U, - (uint) short.MaxValue, - (uint) ushort.MaxValue + 0U, + 1U, + 3U, + 7U, + 15U, + 31U, + 63U, + (uint)sbyte.MaxValue, + (uint)byte.MaxValue, + 511U, + 1023U, + 2047U, + 4095U, + 8191U, + 16383U, + (uint)short.MaxValue, + (uint)ushort.MaxValue }; public BitStream(byte[] src, int srcLen) diff --git a/src/SharpCompress/Compressors/Shrink/HwUnshrink.cs b/src/SharpCompress/Compressors/Shrink/HwUnshrink.cs index ca3dcbe6..04f38c2b 100644 --- a/src/SharpCompress/Compressors/Shrink/HwUnshrink.cs +++ b/src/SharpCompress/Compressors/Shrink/HwUnshrink.cs @@ -69,9 +69,16 @@ namespace SharpCompress.Compressors.Shrink queue.nextIdx = 0; } - private static bool ReadCode(BitStream stream, ref int codeSize, CodeTabEntry[] codeTab, ref CodeQueue queue, out int nextCode) + private static bool ReadCode( + BitStream stream, + ref int codeSize, + CodeTabEntry[] codeTab, + ref CodeQueue queue, + out int nextCode + ) { - int code, controlCode; + int code, + controlCode; code = (int)stream.NextBits(codeSize); if (!stream.Advance(codeSize)) @@ -130,8 +137,17 @@ namespace SharpCompress.Compressors.Shrink Buffer.BlockCopy(dst, prevPos, dst, dstPos, len); } - private static UnshrnkStatus OutputCode(int code, byte[] dst, int dstPos, int dstCap, int prevCode, - CodeTabEntry[] codeTab, ref CodeQueue queue, out byte firstByte, out int len) + private static UnshrnkStatus OutputCode( + int code, + byte[] dst, + int dstPos, + int dstCap, + int prevCode, + CodeTabEntry[] codeTab, + ref CodeQueue queue, + out byte firstByte, + out int len + ) { int prefixCode; @@ -174,7 +190,7 @@ namespace SharpCompress.Compressors.Shrink // Output a string of unknown length. //assert(codeTab[code].len == UNKNOWN_LEN); prefixCode = codeTab[code].prefixCode; - // assert(prefixCode > CONTROL_CODE); + // assert(prefixCode > CONTROL_CODE); if (prefixCode == queue.codes[queue.nextIdx]) { @@ -215,13 +231,24 @@ namespace SharpCompress.Compressors.Shrink return UnshrnkStatus.Ok; } - public static UnshrnkStatus Unshrink(byte[] src, int srcLen, out int srcUsed, byte[] dst, int dstCap, out int dstUsed) + public static UnshrnkStatus Unshrink( + byte[] src, + int srcLen, + out int srcUsed, + byte[] dst, + int dstCap, + out int dstUsed + ) { CodeTabEntry[] codeTab = new CodeTabEntry[HASHTAB_SIZE]; CodeQueue queue = new CodeQueue(); var stream = new BitStream(src, srcLen); - int codeSize, dstPos, len; - int currCode, prevCode, newCode; + int codeSize, + dstPos, + len; + int currCode, + prevCode, + newCode; byte firstByte; CodeTabInit(codeTab); @@ -296,7 +323,17 @@ namespace SharpCompress.Compressors.Shrink } // Output the string represented by the current code. - UnshrnkStatus status = OutputCode(currCode, dst, dstPos, dstCap, prevCode, codeTab, ref queue, out firstByte, out len); + UnshrnkStatus status = OutputCode( + currCode, + dst, + dstPos, + dstCap, + prevCode, + codeTab, + ref queue, + out firstByte, + out len + ); if (status != UnshrnkStatus.Ok) { srcUsed = stream.BytesRead; @@ -308,7 +345,7 @@ namespace SharpCompress.Compressors.Shrink var c = currCode; for (int i = 0; i < len; i++) { - // assert(codeTab[c].len == len - i); + // assert(codeTab[c].len == len - i); //assert(codeTab[c].extByte == dst[dstPos + len - i - 1]); c = codeTab[c].prefixCode; } @@ -376,11 +413,13 @@ namespace SharpCompress.Compressors.Shrink q.codes[codeQueueSize] = INVALID_CODE; // End-of-queue marker. q.nextIdx = 0; } + private static ushort CodeQueueNext(ref CodeQueue q) { //assert(q.nextIdx < q.codes.Length); return q.codes[q.nextIdx]; } + private static ushort CodeQueueRemoveNext(ref CodeQueue q) { ushort code = CodeQueueNext(ref q); diff --git a/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs b/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs index 7926d093..52bba1ad 100644 --- a/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs +++ b/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs @@ -17,7 +17,12 @@ internal class ShrinkStream : Stream private byte[] _byteOut; private long _outBytesCount; - public ShrinkStream(Stream stream, CompressionMode compressionMode, long compressedSize, long uncompressedSize) + public ShrinkStream( + Stream stream, + CompressionMode compressionMode, + long compressedSize, + long uncompressedSize + ) { inStream = stream; _compressionMode = compressionMode; @@ -36,9 +41,14 @@ internal class ShrinkStream : Stream public override long Length => _uncompressedSize; - public override long Position { get => _outBytesCount; set => throw new NotImplementedException(); } + public override long Position + { + get => _outBytesCount; + set => throw new NotImplementedException(); + } public override void Flush() => throw new NotImplementedException(); + public override int Read(byte[] buffer, int offset, int count) { if (inStream.Position == (long)_compressedSize) @@ -50,7 +60,14 @@ internal class ShrinkStream : Stream int srcUsed = 0; int dstUsed = 0; - HwUnshrink.Unshrink(src, (int)_compressedSize, out srcUsed, _byteOut, (int)_uncompressedSize, out dstUsed); + HwUnshrink.Unshrink( + src, + (int)_compressedSize, + out srcUsed, + _byteOut, + (int)_uncompressedSize, + out dstUsed + ); _outBytesCount = _byteOut.Length; for (int index = 0; index < _outBytesCount; ++index) @@ -61,7 +78,12 @@ internal class ShrinkStream : Stream _outBytesCount = 0; return (int)tmp; } - public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); + + public override long Seek(long offset, SeekOrigin origin) => + throw new NotImplementedException(); + public override void SetLength(long value) => throw new NotImplementedException(); - public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotImplementedException(); }