Skip to content

Commit 9efd2ff

Browse files
committed
Slightly more Quantum work
1 parent cedd246 commit 9efd2ff

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

SabreTools.Compression/Quantum/Decompressor.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,25 @@ public class Decompressor
8383

8484
#endregion
8585

86-
/// <summary>
87-
/// Create a new Decompressor from a byte array
88-
/// </summary>
89-
/// <param name="input">Byte array to decompress</param>
90-
/// <param name="windowBits">Number of bits in the sliding window</param>
91-
public Decompressor(byte[]? input, uint windowBits)
92-
: this(new MemoryStream(input ?? []), windowBits)
93-
{ }
86+
#region Constructors
9487

9588
/// <summary>
9689
/// Create a new Decompressor from a Stream
9790
/// </summary>
98-
/// <param name="input">Stream to decompress</param>
91+
/// <param name="source">Stream to decompress</param>
9992
/// <param name="windowBits">Number of bits in the sliding window</param>
100-
public Decompressor(Stream? input, uint windowBits)
93+
private Decompressor(Stream source, uint windowBits)
10194
{
102-
// If we have an invalid stream
103-
if (input == null || !input.CanRead || !input.CanSeek)
104-
throw new ArgumentException(nameof(input));
105-
106-
// If we have an invalid value for the window bits
95+
// Validate the inputs
96+
if (source.Length == 0)
97+
throw new ArgumentOutOfRangeException(nameof(source));
98+
if (!source.CanRead)
99+
throw new InvalidOperationException(nameof(source));
107100
if (windowBits < 10 || windowBits > 21)
108101
throw new ArgumentOutOfRangeException(nameof(windowBits));
109102

110103
// Wrap the stream in a ReadOnlyBitStream
111-
_bitStream = new ReadOnlyBitStream(input);
104+
_bitStream = new ReadOnlyBitStream(source);
112105

113106
// Initialize literal models
114107
_model0 = CreateModel(0, 64);
@@ -132,6 +125,20 @@ public Decompressor(Stream? input, uint windowBits)
132125
CS_C = 0;
133126
}
134127

128+
/// <summary>
129+
/// Create a Quantum decompressor
130+
/// </summary>
131+
public static Decompressor Create(byte[] source, uint windowBits)
132+
=> Create(new MemoryStream(source), windowBits);
133+
134+
/// <summary>
135+
/// Create a Quantum decompressor
136+
/// </summary>
137+
public static Decompressor Create(Stream source, uint windowBits)
138+
=> new(source, windowBits);
139+
140+
#endregion
141+
135142
/// <summary>
136143
/// Process the stream and return the decompressed output
137144
/// </summary>
@@ -218,7 +225,7 @@ public byte[] Process()
218225
}
219226
}
220227

221-
return bytes.ToArray();
228+
return [.. bytes];
222229
}
223230

224231
/// <summary>

Test/Program.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4-
using SabreTools.Compression.MSZIP;
4+
using SabreTools.Compression.Quantum;
55
using SabreTools.IO.Extensions;
66
using SabreTools.Models.MicrosoftCabinet;
77
using static SabreTools.Models.MicrosoftCabinet.Constants;
@@ -13,11 +13,12 @@ public static class Program
1313
public static void Main(string[] args)
1414
{
1515
// No implementation, used for experimentation
16+
READMSZIPTEST();
1617
}
1718

1819
private static void READMSZIPTEST()
1920
{
20-
using var fs = File.OpenRead("INFILE.cab");
21+
using var fs = File.OpenRead("/mnt/b/BurnOutSharp Testing Files/FileType/Microsoft CAB/Quantum/WORDWEB_10.CAB");
2122
var cab = Deserialize(fs);
2223
if (cab == null || cab.Folders == null || cab.Files == null)
2324
return;
@@ -28,15 +29,18 @@ private static void READMSZIPTEST()
2829
if (folder?.DataBlocks == null || folder.DataBlocks.Length == 0)
2930
continue;
3031

31-
var decomp = Decompressor.Create();
32+
uint windowBits = (uint)(((ushort)folder.CompressionType >> 8) & 0x1f);
3233

3334
var ms = new MemoryStream();
3435
foreach (var db in folder.DataBlocks)
3536
{
3637
if (db?.CompressedData == null)
3738
continue;
3839

39-
decomp.CopyTo(db.CompressedData, ms);
40+
var decomp = Decompressor.Create(db.CompressedData, windowBits);
41+
byte[] data = decomp.Process();
42+
ms.Write(data);
43+
ms.Flush();
4044
}
4145

4246
if (cab?.Files == null || cab.Files.Length == 0)
@@ -50,7 +54,7 @@ private static void READMSZIPTEST()
5054
byte[] fileData = new byte[file.FileSize];
5155
Array.Copy(ms.ToArray(), file.FolderStartOffset, fileData, 0, file.FileSize);
5256

53-
using var of = File.OpenWrite(Path.Combine("OUTDIR", file.Name));
57+
using var of = File.OpenWrite(Path.Combine("/mnt/b/BurnOutSharp Testing Files/FileType/Microsoft CAB/Quantum/WORDWEB_10/", file.Name));
5458
of.Write(fileData);
5559
of.Flush();
5660
}
@@ -220,7 +224,7 @@ private static CFFOLDER ParseFolder(Stream data, CFHEADER header)
220224

221225
folder.CabStartOffset = data.ReadUInt32();
222226
folder.DataCount = data.ReadUInt16();
223-
folder.CompressionType = (CompressionType)data.ReadUInt16() & CompressionType.MASK_TYPE;
227+
folder.CompressionType = (CompressionType)data.ReadUInt16();
224228

225229
if (header.FolderReservedSize > 0)
226230
folder.ReservedData = data.ReadBytes(header.FolderReservedSize);

0 commit comments

Comments
 (0)