Skip to content

Commit

Permalink
Refrormatted using CSharpier
Browse files Browse the repository at this point in the history
  • Loading branch information
TwanVanDongen committed Jan 27, 2024
1 parent fe13d29 commit c057ffb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 35 deletions.
7 changes: 6 additions & 1 deletion src/SharpCompress/Common/Zip/ZipFilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ protected Stream CreateDecompressionStream(Stream stream, ZipCompressionMethod m
}
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:
{
Expand Down
37 changes: 18 additions & 19 deletions src/SharpCompress/Compressors/Shrink/BitStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using System.Threading.Tasks;


namespace SharpCompress.Compressors.Shrink
{
internal class BitStream
Expand All @@ -14,26 +13,26 @@ internal class BitStream
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)
Expand Down
59 changes: 49 additions & 10 deletions src/SharpCompress/Compressors/Shrink/HwUnshrink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ private static void UnshrinkPartialClear(CodeTabEntry[] codeTab, ref CodeQueue q
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))
Expand Down Expand Up @@ -130,8 +137,17 @@ private static void CopyFromPrevPos(byte[] dst, int prevPos, int dstPos, int len
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;

Expand Down Expand Up @@ -174,7 +190,7 @@ private static UnshrnkStatus OutputCode(int code, byte[] dst, int dstPos, int ds
// 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])
{
Expand Down Expand Up @@ -215,13 +231,24 @@ private static UnshrnkStatus OutputCode(int code, byte[] dst, int dstPos, int ds
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);
Expand Down Expand Up @@ -296,7 +323,17 @@ public static UnshrnkStatus Unshrink(byte[] src, int srcLen, out int srcUsed, by
}

// 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;
Expand All @@ -308,7 +345,7 @@ public static UnshrnkStatus Unshrink(byte[] src, int srcLen, out int srcUsed, by
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;
}
Expand Down Expand Up @@ -376,11 +413,13 @@ private static void CodeQueueInit(ref CodeQueue q)
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);
Expand Down
32 changes: 27 additions & 5 deletions src/SharpCompress/Compressors/Shrink/ShrinkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,9 +41,14 @@ public ShrinkStream(Stream stream, CompressionMode compressionMode, long compres

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)
Expand All @@ -50,7 +60,14 @@ public override int Read(byte[] buffer, int offset, int count)
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)
Expand All @@ -61,7 +78,12 @@ public override int Read(byte[] buffer, int offset, int count)
_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();
}

0 comments on commit c057ffb

Please sign in to comment.