Skip to content

Commit cedd246

Browse files
committed
Fix Quantum boundary issue
1 parent 0e7aebd commit cedd246

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

SabreTools.Compression/Quantum/Decompressor.cs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,42 +89,8 @@ public class Decompressor
8989
/// <param name="input">Byte array to decompress</param>
9090
/// <param name="windowBits">Number of bits in the sliding window</param>
9191
public Decompressor(byte[]? input, uint windowBits)
92-
{
93-
// If we have an invalid stream
94-
if (input == null || input.Length == 0)
95-
throw new ArgumentException(nameof(input));
96-
97-
// If we have an invalid value for the window bits
98-
if (windowBits < 10 || windowBits > 21)
99-
throw new ArgumentOutOfRangeException(nameof(windowBits));
100-
101-
// Create a memory stream to wrap
102-
var ms = new MemoryStream(input);
103-
104-
// Wrap the stream in a ReadOnlyBitStream
105-
_bitStream = new ReadOnlyBitStream(ms);
106-
107-
// Initialize literal models
108-
_model0 = CreateModel(0, 64);
109-
_model1 = CreateModel(64, 64);
110-
_model2 = CreateModel(128, 64);
111-
_model3 = CreateModel(192, 64);
112-
113-
// Initialize LZ models
114-
int maxBitLength = (int)(windowBits * 2);
115-
_model4 = CreateModel(0, maxBitLength > 24 ? 24 : maxBitLength);
116-
_model5 = CreateModel(0, maxBitLength > 36 ? 36 : maxBitLength);
117-
_model6 = CreateModel(0, maxBitLength);
118-
_model6len = CreateModel(0, 27);
119-
120-
// Initialze the selector model
121-
_selector = CreateModel(0, 7);
122-
123-
// Initialize coding state
124-
CS_H = 0;
125-
CS_L = 0;
126-
CS_C = 0;
127-
}
92+
: this(new MemoryStream(input ?? []), windowBits)
93+
{ }
12894

12995
/// <summary>
13096
/// Create a new Decompressor from a Stream
@@ -264,12 +230,12 @@ private Model CreateModel(ushort start, int length)
264230
var model = new Model
265231
{
266232
Entries = length,
267-
Symbols = new ModelSymbol[length],
233+
Symbols = new ModelSymbol[length + 1],
268234
TimeToReorder = 4,
269235
};
270236

271237
// Populate the symbol array
272-
for (int i = 0; i < length; i++)
238+
for (int i = 0; i <= length; i++)
273239
{
274240
model.Symbols[i] = new ModelSymbol
275241
{
@@ -415,7 +381,7 @@ private void UpdateModel(Model model, int lastUpdated)
415381
private ushort GetFrequency(ushort totalFrequency)
416382
{
417383
ulong range = (ulong)(((CS_H - CS_L) & 0xFFFF) + 1);
418-
ulong frequency = (ulong)((CS_C - CS_L + 1) * totalFrequency - 1) / range;
384+
ulong frequency = (ulong)(((CS_C - CS_L + 1) * totalFrequency) - 1) / range;
419385
return (ushort)(frequency & 0xFFFF);
420386
}
421387
}

0 commit comments

Comments
 (0)