Skip to content

Commit 2a23f30

Browse files
authored
Merge 6ceb473 into cfccc32
2 parents cfccc32 + 6ceb473 commit 2a23f30

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

Confuser.Core/Services/CompressionService.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ public byte[] Compress(byte[] data, Action<double> progressFunc = null) {
9696
var encoder = new Encoder();
9797
encoder.SetCoderProperties(propIDs, properties);
9898
encoder.WriteCoderProperties(x);
99-
Int64 fileSize;
100-
fileSize = data.Length;
101-
for (int i = 0; i < 8; i++)
102-
x.WriteByte((Byte)(fileSize >> (8 * i)));
99+
100+
var length = BitConverter.GetBytes(data.Length);
101+
if (!BitConverter.IsLittleEndian)
102+
Array.Reverse(length);
103+
104+
// Store 4 byte length value (little-endian)
105+
x.Write(length, 0, sizeof(int));
103106

104107
ICodeProgress progress = null;
105108
if (progressFunc != null)
@@ -156,4 +159,4 @@ public interface ICompressionService {
156159
/// <returns>The compressed data.</returns>
157160
byte[] Compress(byte[] data, Action<double> progressFunc = null);
158161
}
159-
}
162+
}

Confuser.Runtime/Lzma.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,22 @@ public static byte[] Decompress(byte[] data) {
3232
var s = new MemoryStream(data);
3333
var decoder = new LzmaDecoder();
3434
var prop = new byte[5];
35-
s.Read(prop, 0, 5);
35+
var readCnt = 0;
36+
while (readCnt < 5) {
37+
readCnt += s.Read(prop, readCnt, 5 - readCnt);
38+
}
3639
decoder.SetDecoderProperties(prop);
37-
long outSize = 0;
38-
for (int i = 0; i < 8; i++) {
39-
int v = s.ReadByte();
40-
outSize |= ((long)(byte)v) << (8 * i);
40+
41+
readCnt = 0;
42+
while (readCnt < sizeof(int)) {
43+
readCnt += s.Read(prop, readCnt, sizeof(int) - readCnt);
4144
}
42-
var b = new byte[(int)outSize];
45+
if (!BitConverter.IsLittleEndian)
46+
Array.Reverse(prop, 0, sizeof(int));
47+
var outSize = BitConverter.ToInt32(prop, 0);
48+
var b = new byte[outSize];
4349
var z = new MemoryStream(b, true);
44-
long compressedSize = s.Length - 13;
50+
long compressedSize = s.Length - 5 - sizeof(int);
4551
decoder.Code(s, z, compressedSize, outSize);
4652
return b;
4753
}
@@ -594,4 +600,4 @@ public bool IsCharState() {
594600
}
595601
}
596602
}
597-
}
603+
}

0 commit comments

Comments
 (0)