mirror of
https://github.com/SabreTools/SabreTools.Compression.git
synced 2026-02-04 13:45:35 +00:00
Compare commits
125 Commits
libmspack_
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7be09eae1a | ||
|
|
6561a96ae5 | ||
|
|
000a050075 | ||
|
|
bbbbcb8e53 | ||
|
|
9ffbae1419 | ||
|
|
68fc881c36 | ||
|
|
d8cf671a31 | ||
|
|
2c3082d2cf | ||
|
|
6a59105f3a | ||
|
|
d7fe18612c | ||
|
|
c3044bdc5a | ||
|
|
8fc071d4d8 | ||
|
|
97a83b42ee | ||
|
|
b2fe81dac8 | ||
|
|
cdaf7a03d6 | ||
|
|
9efd2ff02b | ||
|
|
cedd246c9c | ||
|
|
0e7aebde54 | ||
|
|
24afbb3003 | ||
|
|
d13c4965d5 | ||
|
|
b6d9dcd77a | ||
|
|
0d942158a9 | ||
|
|
9bf7839d69 | ||
|
|
04d5efdca5 | ||
|
|
27c44ab7f8 | ||
|
|
871d6353cd | ||
|
|
80375b6b48 | ||
|
|
f7b800acd9 | ||
|
|
5a8da26e48 | ||
|
|
66891c4ab3 | ||
|
|
c363d8fdf9 | ||
|
|
daf27b9175 | ||
|
|
daaf157bc5 | ||
|
|
163b2483f9 | ||
|
|
8ed85d52ed | ||
|
|
042fc18aa7 | ||
|
|
16d8d9839c | ||
|
|
dfcbce8874 | ||
|
|
fa5084d067 | ||
|
|
d9e1748ec1 | ||
|
|
84a3e515c0 | ||
|
|
e217f4109c | ||
|
|
882f2c5335 | ||
|
|
3f6c7dc0d6 | ||
|
|
bf413cbb85 | ||
|
|
7a403cf368 | ||
|
|
aec4611d14 | ||
|
|
7052584cea | ||
|
|
efe6c545b9 | ||
|
|
612a8b3c83 | ||
|
|
bc06cb5bdb | ||
|
|
ae223a4589 | ||
|
|
018fd01922 | ||
|
|
910b01b072 | ||
|
|
d239d9f09b | ||
|
|
0cf3e3e816 | ||
|
|
fe319b71f1 | ||
|
|
cd5bf99f21 | ||
|
|
f79b5353f7 | ||
|
|
5e184b03c5 | ||
|
|
a1417d2f8a | ||
|
|
7580d49830 | ||
|
|
c04c7d438d | ||
|
|
a0f602ed6f | ||
|
|
4bc7f53c9f | ||
|
|
049a8cf499 | ||
|
|
9a8875f1e0 | ||
|
|
a916cb9954 | ||
|
|
a6ef762a73 | ||
|
|
7aac1e0bed | ||
|
|
bc569964d8 | ||
|
|
a3ac98a9f4 | ||
|
|
57c0f9b747 | ||
|
|
38f3ea1c98 | ||
|
|
ab36802840 | ||
|
|
422bda1830 | ||
|
|
1c989985d9 | ||
|
|
62c6e79ad3 | ||
|
|
6bbf521828 | ||
|
|
7a4e2f0ee0 | ||
|
|
1d1a6f5976 | ||
|
|
065b68124b | ||
|
|
07b50e8c46 | ||
|
|
8eb82384d6 | ||
|
|
dd6cc0e2f3 | ||
|
|
58502e0362 | ||
|
|
f3bf1082d3 | ||
|
|
7958b24a36 | ||
|
|
3010a0523c | ||
|
|
d7670ae685 | ||
|
|
2d09d9696a | ||
|
|
d75883a6cf | ||
|
|
8e5cf3ee2e | ||
|
|
e739fd6fd5 | ||
|
|
6b238df5dc | ||
|
|
3e3a0e122b | ||
|
|
cb6e157cb4 | ||
|
|
12466d7083 | ||
|
|
47cb06cf34 | ||
|
|
c152cba81d | ||
|
|
4684a6612c | ||
|
|
a58da1d8db | ||
|
|
8f098a6669 | ||
|
|
8c5482a59a | ||
|
|
b1f1863e9a | ||
|
|
8ab555d6fc | ||
|
|
32b2f6c443 | ||
|
|
44f1544725 | ||
|
|
471cbc5707 | ||
|
|
5b785fb28f | ||
|
|
38dd2a5caf | ||
|
|
5e21a09fd1 | ||
|
|
8174af616f | ||
|
|
297fffe8d7 | ||
|
|
bd9258d9fa | ||
|
|
b7a081824c | ||
|
|
9617e5c583 | ||
|
|
ec40e759a9 | ||
|
|
15bf2001b5 | ||
|
|
81eab984fb | ||
|
|
47691d2034 | ||
|
|
dde90a852d | ||
|
|
2ce175af39 | ||
|
|
3353264090 | ||
|
|
5477afaf1e |
40
.github/workflows/build_and_test.yml
vendored
Normal file
40
.github/workflows/build_and_test.yml
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
name: Build and Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: |
|
||||
6.0.x
|
||||
8.0.x
|
||||
9.0.x
|
||||
|
||||
- name: Run tests
|
||||
run: dotnet test
|
||||
|
||||
- name: Run publish script
|
||||
run: ./publish-nix.sh
|
||||
|
||||
- name: Upload to rolling
|
||||
uses: ncipollo/release-action@v1.14.0
|
||||
with:
|
||||
allowUpdates: True
|
||||
artifacts: "*.nupkg,*.snupkg"
|
||||
body: 'Last built commit: ${{ github.sha }}'
|
||||
name: 'Rolling Release'
|
||||
prerelease: True
|
||||
replacesArtifacts: True
|
||||
tag: "rolling"
|
||||
updateOnlyUnreleased: True
|
||||
23
.github/workflows/check_pr.yml
vendored
Normal file
23
.github/workflows/check_pr.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
name: Build PR
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: |
|
||||
6.0.x
|
||||
8.0.x
|
||||
9.0.x
|
||||
|
||||
- name: Build
|
||||
run: dotnet build
|
||||
|
||||
- name: Run tests
|
||||
run: dotnet test
|
||||
@@ -1,581 +0,0 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SabreTools.IO;
|
||||
using SabreTools.Models.Compression.LZ;
|
||||
using static SabreTools.Models.Compression.LZ.Constants;
|
||||
|
||||
namespace SabreTools.Compression.LZ
|
||||
{
|
||||
/// <see href="https://github.com/wine-mirror/wine/blob/master/dlls/kernel32/lzexpand.c"/>
|
||||
public class Decompressor
|
||||
{
|
||||
#region Static Methods
|
||||
|
||||
/// <summary>
|
||||
/// Decompress LZ-compressed data
|
||||
/// </summary>
|
||||
/// <param name="compressed">Byte array representing the compressed data</param>
|
||||
/// <returns>Decompressed data as a byte array, null on error</returns>
|
||||
#if NET48
|
||||
public static byte[] Decompress(byte[] compressed)
|
||||
#else
|
||||
public static byte[]? Decompress(byte[]? compressed)
|
||||
#endif
|
||||
{
|
||||
// If we have and invalid input
|
||||
if (compressed == null || compressed.Length == 0)
|
||||
return null;
|
||||
|
||||
// Create a memory stream for the input and decompress that
|
||||
var compressedStream = new MemoryStream(compressed);
|
||||
return Decompress(compressedStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompress LZ-compressed data
|
||||
/// </summary>
|
||||
/// <param name="compressed">Stream representing the compressed data</param>
|
||||
/// <returns>Decompressed data as a byte array, null on error</returns>
|
||||
#if NET48
|
||||
public static byte[] Decompress(Stream compressed)
|
||||
#else
|
||||
public static byte[]? Decompress(Stream? compressed)
|
||||
#endif
|
||||
{
|
||||
// If we have and invalid input
|
||||
if (compressed == null || compressed.Length == 0)
|
||||
return null;
|
||||
|
||||
// Create a new LZ for decompression
|
||||
var lz = new Decompressor();
|
||||
|
||||
// Open the input data
|
||||
var sourceState = lz.Open(compressed, out _);
|
||||
if (sourceState?.Window == null)
|
||||
return null;
|
||||
|
||||
// Create the output data and open it
|
||||
var decompressedStream = new MemoryStream();
|
||||
var destState = lz.Open(decompressedStream, out _);
|
||||
if (destState == null)
|
||||
return null;
|
||||
|
||||
// Decompress the data by copying
|
||||
long read = lz.CopyTo(sourceState, destState, out LZERROR error);
|
||||
|
||||
// Copy the data to the buffer
|
||||
var decompressed = new byte[0];
|
||||
if (read == 0 || (error != LZERROR.LZERROR_OK && error != LZERROR.LZERROR_NOT_LZ))
|
||||
{
|
||||
decompressed = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
int dataEnd = (int)decompressedStream.Position;
|
||||
decompressedStream.Seek(0, SeekOrigin.Begin);
|
||||
decompressed = decompressedStream.ReadBytes(dataEnd);
|
||||
}
|
||||
|
||||
// Close the streams
|
||||
lz.Close(sourceState);
|
||||
lz.Close(destState);
|
||||
|
||||
return decompressed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reconstructs the full filename of the compressed file
|
||||
/// </summary>
|
||||
#if NET48
|
||||
public static string GetExpandedName(string input, out LZERROR error)
|
||||
#else
|
||||
public static string? GetExpandedName(string input, out LZERROR error)
|
||||
#endif
|
||||
{
|
||||
// Try to open the file as a compressed stream
|
||||
var fileStream = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var state = new Decompressor().Open(fileStream, out error);
|
||||
if (state?.Window == null)
|
||||
return null;
|
||||
|
||||
// Get the extension for modification
|
||||
string inputExtension = Path.GetExtension(input).TrimStart('.');
|
||||
|
||||
// If we have no extension
|
||||
if (string.IsNullOrWhiteSpace(inputExtension))
|
||||
return Path.GetFileNameWithoutExtension(input);
|
||||
|
||||
// If we have an extension of length 1
|
||||
if (inputExtension.Length == 1)
|
||||
{
|
||||
if (inputExtension == "_")
|
||||
return $"{Path.GetFileNameWithoutExtension(input)}.{char.ToLower(state.LastChar)}";
|
||||
else
|
||||
return Path.GetFileNameWithoutExtension(input);
|
||||
}
|
||||
|
||||
// If we have an extension that doesn't end in an underscore
|
||||
if (!inputExtension.EndsWith("_"))
|
||||
return Path.GetFileNameWithoutExtension(input);
|
||||
|
||||
// Build the new filename
|
||||
bool isLowerCase = char.IsUpper(input[0]);
|
||||
char replacementChar = isLowerCase ? char.ToLower(state.LastChar) : char.ToUpper(state.LastChar);
|
||||
string outputExtension = inputExtension.Substring(0, inputExtension.Length - 1) + replacementChar;
|
||||
return $"{Path.GetFileNameWithoutExtension(input)}.{outputExtension}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State Management
|
||||
|
||||
/// <summary>
|
||||
/// Opens a stream and creates a state from it
|
||||
/// </summary>
|
||||
/// <param name="stream">Source stream to create a state from</stream>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>An initialized State, null on error</returns>
|
||||
/// <remarks>Uncompressed streams are represented by a State with no buffer</remarks>
|
||||
#if NET48
|
||||
public State Open(Stream stream, out LZERROR error)
|
||||
#else
|
||||
public State? Open(Stream stream, out LZERROR error)
|
||||
#endif
|
||||
{
|
||||
var lzs = Init(stream, out error);
|
||||
if (error == LZERROR.LZERROR_OK || error == LZERROR.LZERROR_NOT_LZ)
|
||||
return lzs;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes a state by invalidating the source
|
||||
/// </summary>
|
||||
/// <param name="stream">State object to close</stream>
|
||||
public void Close(State state)
|
||||
{
|
||||
try
|
||||
{
|
||||
state?.Source?.Close();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes internal decompression buffers
|
||||
/// </summary>
|
||||
/// <param name="source">Input stream to create a state from</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>An initialized State, null on error</returns>
|
||||
/// <remarks>Uncompressed streams are represented by a State with no buffer</remarks>
|
||||
#if NET48
|
||||
public State Init(Stream source, out LZERROR error)
|
||||
#else
|
||||
public State? Init(Stream? source, out LZERROR error)
|
||||
#endif
|
||||
{
|
||||
// If we have an invalid source
|
||||
if (source == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Attempt to read the header
|
||||
var fileHeader = ParseFileHeader(source, out error);
|
||||
|
||||
// If we had a valid but uncompressed stream
|
||||
if (error == LZERROR.LZERROR_NOT_LZ)
|
||||
{
|
||||
source.Seek(0, SeekOrigin.Begin);
|
||||
return new State { Source = source };
|
||||
}
|
||||
|
||||
// If we had any error
|
||||
else if (fileHeader == null || error != LZERROR.LZERROR_OK)
|
||||
{
|
||||
source.Seek(0, SeekOrigin.Begin);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Initialize the table with all spaces
|
||||
byte[] table = Enumerable.Repeat((byte)' ', LZ_TABLE_SIZE).ToArray();
|
||||
|
||||
// Build the state
|
||||
var state = new State
|
||||
{
|
||||
Source = source,
|
||||
LastChar = fileHeader.LastChar,
|
||||
RealLength = fileHeader.RealLength,
|
||||
|
||||
Window = new byte[GETLEN],
|
||||
WindowLength = 0,
|
||||
WindowCurrent = 0,
|
||||
|
||||
Table = table,
|
||||
CurrentTableEntry = 0xff0,
|
||||
};
|
||||
|
||||
// Return the state
|
||||
return state;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Stream Functionality
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to read the specified number of bytes from the State
|
||||
/// </summary>
|
||||
/// <param name="state">Source State to read from</param>
|
||||
/// <param name="buffer">Byte buffer to read into</param>
|
||||
/// <param name="offset">Offset within the buffer to read</param>
|
||||
/// <param name="count">Number of bytes to read</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>The number of bytes read, if possible</returns>
|
||||
/// <remarks>
|
||||
/// If the source data is compressed, this will decompress the data.
|
||||
/// If the source data is uncompressed, it is copied directly
|
||||
/// </remarks>
|
||||
public int Read(State state, byte[] buffer, int offset, int count, out LZERROR error)
|
||||
{
|
||||
// If the source is invalid
|
||||
if (state.Source == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If we have an uncompressed input
|
||||
if (state.Window == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_NOT_LZ;
|
||||
return state.Source.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
// If seeking has occurred, we need to perform the seek
|
||||
if (state.RealCurrent != state.RealWanted)
|
||||
{
|
||||
// If the requested position is before the current, we need to reset
|
||||
if (state.RealCurrent > state.RealWanted)
|
||||
{
|
||||
// Reset the decompressor state
|
||||
state.Source.Seek(LZ_HEADER_LEN, SeekOrigin.Begin);
|
||||
FlushWindow(state);
|
||||
state.RealCurrent = 0;
|
||||
state.ByteType = 0;
|
||||
state.StringLength = 0;
|
||||
state.Table = Enumerable.Repeat((byte)' ', LZ_TABLE_SIZE).ToArray();
|
||||
state.CurrentTableEntry = 0xFF0;
|
||||
}
|
||||
|
||||
// While we are not at the right offset
|
||||
while (state.RealCurrent < state.RealWanted)
|
||||
{
|
||||
_ = DecompressByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int bytesRemaining = count;
|
||||
while (bytesRemaining > 0)
|
||||
{
|
||||
byte b = DecompressByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return count - bytesRemaining;
|
||||
|
||||
state.RealWanted++;
|
||||
buffer[offset++] = b;
|
||||
bytesRemaining--;
|
||||
}
|
||||
|
||||
error = LZERROR.LZERROR_OK;
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform a seek on the source data
|
||||
/// </summary>
|
||||
/// <param name="state">State to seek within</param>
|
||||
/// <param name="offset">Data offset to seek to</state>
|
||||
/// <param name="seekOrigin">SeekOrigin representing how to seek</state>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>The position that was seeked to, -1 on error</returns>
|
||||
public long Seek(State state, long offset, SeekOrigin seekOrigin, out LZERROR error)
|
||||
{
|
||||
// If the source is invalid
|
||||
if (state.Source == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If we have an invalid state
|
||||
if (state == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If we have an uncompressed input
|
||||
if (state.Window == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_NOT_LZ;
|
||||
return state.Source.Seek(offset, seekOrigin);
|
||||
}
|
||||
|
||||
// Otherwise, generate the new offset
|
||||
long newWanted = state.RealWanted;
|
||||
switch (seekOrigin)
|
||||
{
|
||||
case SeekOrigin.Current:
|
||||
newWanted += offset;
|
||||
break;
|
||||
case SeekOrigin.End:
|
||||
newWanted = state.RealLength - offset;
|
||||
break;
|
||||
default:
|
||||
newWanted = offset;
|
||||
break;
|
||||
}
|
||||
|
||||
// If we have an invalid new offset
|
||||
if (newWanted < 0 && newWanted > state.RealLength)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return -1;
|
||||
}
|
||||
|
||||
error = LZERROR.LZERROR_OK;
|
||||
state.RealWanted = (uint)newWanted;
|
||||
return newWanted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all data from the source to the destination
|
||||
/// </summary>
|
||||
/// <param name="state">Source State to read from</param>
|
||||
/// <param name="dest">Destination state to write to</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>The number of bytes written, -1 on error</returns>
|
||||
/// <remarks>
|
||||
/// If the source data is compressed, this will decompress the data.
|
||||
/// If the source data is uncompressed, it is copied directly
|
||||
/// </remarks>
|
||||
public long CopyTo(State state, State dest, out LZERROR error)
|
||||
{
|
||||
error = LZERROR.LZERROR_OK;
|
||||
|
||||
// If the sources are invalid
|
||||
if (state.Source == null || dest.Source == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If we have an uncompressed input
|
||||
if (state.Window == null)
|
||||
{
|
||||
state.Source.CopyTo(dest.Source);
|
||||
return state.Source.Length;
|
||||
}
|
||||
|
||||
// Loop until we have read everything
|
||||
long length = 0;
|
||||
while (true)
|
||||
{
|
||||
// Read at most 1000 bytes
|
||||
byte[] buf = new byte[1000];
|
||||
int read = Read(state, buf, 0, buf.Length, out error);
|
||||
|
||||
// If we had an error
|
||||
if (read == 0)
|
||||
{
|
||||
if (error == LZERROR.LZERROR_NOT_LZ)
|
||||
{
|
||||
error = LZERROR.LZERROR_OK;
|
||||
break;
|
||||
}
|
||||
else if (error != LZERROR.LZERROR_OK)
|
||||
{
|
||||
error = LZERROR.LZERROR_READ;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, append the length read and write the data
|
||||
length += read;
|
||||
dest.Source.Write(buf, 0, read);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompress a single byte of data from the source State
|
||||
/// </summary>
|
||||
/// <param name="state">Source State to read from</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>The read byte, if possible</returns>
|
||||
private byte DecompressByte(State state, out LZERROR error)
|
||||
{
|
||||
// If the table is invalid
|
||||
if (state.Table == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
byte b;
|
||||
|
||||
if (state.StringLength != 0)
|
||||
{
|
||||
b = state.Table[state.StringPosition];
|
||||
state.StringPosition = (state.StringPosition + 1) & 0xFFF;
|
||||
state.StringLength--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((state.ByteType & 0x100) == 0)
|
||||
{
|
||||
b = ReadByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return 0;
|
||||
|
||||
state.ByteType = (ushort)(b | 0xFF00);
|
||||
}
|
||||
if ((state.ByteType & 1) != 0)
|
||||
{
|
||||
b = ReadByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte b1 = ReadByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return 0;
|
||||
|
||||
byte b2 = ReadByte(state, out error);
|
||||
if (error != LZERROR.LZERROR_OK)
|
||||
return 0;
|
||||
|
||||
// Format:
|
||||
// b1 b2
|
||||
// AB CD
|
||||
// where CAB is the stringoffset in the table
|
||||
// and D+3 is the len of the string
|
||||
state.StringPosition = (uint)(b1 | ((b2 & 0xf0) << 4));
|
||||
state.StringLength = (byte)((b2 & 0xf) + 2);
|
||||
|
||||
// 3, but we use a byte already below...
|
||||
b = state.Table[state.StringPosition];
|
||||
state.StringPosition = (state.StringPosition + 1) & 0xFFF;
|
||||
}
|
||||
|
||||
state.ByteType >>= 1;
|
||||
}
|
||||
|
||||
// Store b in table
|
||||
state.Table[state.CurrentTableEntry++] = b;
|
||||
state.CurrentTableEntry &= 0xFFF;
|
||||
state.RealCurrent++;
|
||||
|
||||
error = LZERROR.LZERROR_OK;
|
||||
return b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads one compressed byte, including buffering
|
||||
/// </summary>
|
||||
/// <param name="state">State to read using</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>Byte value that was read, if possible</returns>
|
||||
private byte ReadByte(State state, out LZERROR error)
|
||||
{
|
||||
// If the source or window is invalid
|
||||
if (state.Source == null || state.Window == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADVALUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If we have enough data in the buffer
|
||||
if (state.WindowCurrent < state.WindowLength)
|
||||
{
|
||||
error = LZERROR.LZERROR_OK;
|
||||
return state.Window[state.WindowCurrent++];
|
||||
}
|
||||
|
||||
// Otherwise, read from the source
|
||||
int ret = state.Source.Read(state.Window, 0, GETLEN);
|
||||
if (ret == 0)
|
||||
{
|
||||
error = LZERROR.LZERROR_NOT_LZ;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reset the window state
|
||||
state.WindowLength = (uint)ret;
|
||||
state.WindowCurrent = 1;
|
||||
error = LZERROR.LZERROR_OK;
|
||||
return state.Window[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the current window position to the length
|
||||
/// </summary>
|
||||
/// <param name="state">State to flush</param>
|
||||
private void FlushWindow(State state)
|
||||
{
|
||||
state.WindowCurrent = state.WindowLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a Stream into a file header
|
||||
/// </summary>
|
||||
/// <param name="data">Stream to parse</param>
|
||||
/// <param name="error">Output representing the last error</param>
|
||||
/// <returns>Filled file header on success, null on error</returns>
|
||||
#if NET48
|
||||
private FileHeaader ParseFileHeader(Stream data, out LZERROR error)
|
||||
#else
|
||||
private FileHeaader? ParseFileHeader(Stream data, out LZERROR error)
|
||||
#endif
|
||||
{
|
||||
error = LZERROR.LZERROR_OK;
|
||||
var fileHeader = new FileHeaader();
|
||||
|
||||
var magic = data.ReadBytes(LZ_MAGIC_LEN);
|
||||
if (magic == null)
|
||||
{
|
||||
error = LZERROR.LZERROR_BADINHANDLE;
|
||||
return null;
|
||||
}
|
||||
|
||||
fileHeader.Magic = Encoding.ASCII.GetString(magic);
|
||||
if (fileHeader.Magic != MagicString)
|
||||
{
|
||||
error = LZERROR.LZERROR_NOT_LZ;
|
||||
return null;
|
||||
}
|
||||
|
||||
fileHeader.CompressionType = data.ReadByteValue();
|
||||
if (fileHeader.CompressionType != (byte)'A')
|
||||
{
|
||||
error = LZERROR.LZERROR_UNKNOWNALG;
|
||||
return null;
|
||||
}
|
||||
|
||||
fileHeader.LastChar = (char)data.ReadByteValue();
|
||||
fileHeader.RealLength = data.ReadUInt32();
|
||||
|
||||
return fileHeader;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
32
README.MD
32
README.MD
@@ -1,14 +1,34 @@
|
||||
# SabreTools.Compression
|
||||
|
||||
[](https://github.com/SabreTools/SabreTools.Compression/actions/workflows/build_and_test.yml)
|
||||
|
||||
This library comprises of various compression implementations that are used across multiple projects. Most of the implementations are be ports of existing C and C++ code.
|
||||
|
||||
Find the link to the Nuget package [here](https://www.nuget.org/packages/SabreTools.Compression).
|
||||
|
||||
## Releases
|
||||
|
||||
For the most recent stable build, download the latest release here: [Releases Page](https://github.com/SabreTools/SabreTools.Compression/releases)
|
||||
|
||||
For the latest WIP build here: [Rolling Release](https://github.com/SabreTools/SabreTools.Compression/releases/rolling)
|
||||
|
||||
## Supported Compressions
|
||||
|
||||
| Compression Name | Decompress | Compress |
|
||||
| --- | --- | --- |
|
||||
| LZ | Yes | No |
|
||||
| LZX | Incomplete | No |
|
||||
| MSZIP | Incomplete | No |
|
||||
| Quantum | Incomplete | No |
|
||||
| Compression Name | Decompress | Compress | Notes |
|
||||
| --- | --- | --- | --- |
|
||||
| Blast | Yes | No | |
|
||||
| BZip2 | Yes | Yes | Sourced from DotNetZip |
|
||||
| Deflate | Yes | Yes | Sourced from DotNetZip |
|
||||
| LZ | Yes | No | KWAJ, QBasic 4.5, and SZDD variants; KWAJ incomplete |
|
||||
| LZX | No | No | |
|
||||
| MSZIP | Yes | No | |
|
||||
| Quantum | Yes* | No | Partial implementation based on standalone archives |
|
||||
|
||||
**Note:** If something is marked with a `*` it means that it need testing.
|
||||
|
||||
## External Libraries
|
||||
|
||||
| Library Name | Use |
|
||||
| --- | ---|
|
||||
| [DotNetZip](https://github.com/DinoChiesa/DotNetZip) | BZip2 and DEFLATE implementations; minor edits have been made |
|
||||
| [ZLibPort](https://github.com/Nanook/zlib-C-To-CSharp-Port) | Adds zlib code for internal and external use; minor edits have been made |
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<!-- Assembly Properties -->
|
||||
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
|
||||
<Version>0.1.0</Version>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
<Description>Clean compression implementations</Description>
|
||||
<Copyright>Copyright (c) Matt Nadareski 2022-2023</Copyright>
|
||||
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<RepositoryUrl>https://github.com/SabreTools/SabreTools.Printing</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>compression decompression lz</PackageTags>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)'!='net48'">
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="README.md" Pack="true" PackagePath=""/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SabreTools.IO" Version="1.1.1" />
|
||||
<PackageReference Include="SabreTools.Models" Version="1.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Compression", "SabreTools.Compression.csproj", "{B26E863F-8509-48BB-BABA-4FF83DB28D2A}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Compression", "SabreTools.Compression\SabreTools.Compression.csproj", "{B26E863F-8509-48BB-BABA-4FF83DB28D2A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{FDF6EF41-1B5A-4F3B-A7DD-DEB810E55A30}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -18,5 +20,9 @@ Global
|
||||
{B26E863F-8509-48BB-BABA-4FF83DB28D2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B26E863F-8509-48BB-BABA-4FF83DB28D2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B26E863F-8509-48BB-BABA-4FF83DB28D2A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FDF6EF41-1B5A-4F3B-A7DD-DEB810E55A30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FDF6EF41-1B5A-4F3B-A7DD-DEB810E55A30}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FDF6EF41-1B5A-4F3B-A7DD-DEB810E55A30}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FDF6EF41-1B5A-4F3B-A7DD-DEB810E55A30}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
114
SabreTools.Compression/BZip2/BZip2.cs
Normal file
114
SabreTools.Compression/BZip2/BZip2.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
// BZip2InputStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-July-31 11:57:32>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the BZip2InputStream class, which is a decompressing
|
||||
// stream that handles BZIP2. This code is derived from Apache commons source code.
|
||||
// The license below applies to the original Apache code.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This package is based on the work done by Keiron Liddle, Aftex Software
|
||||
* <keiron@aftexsw.com> to whom the Ant project is very grateful for his
|
||||
* great code.
|
||||
*/
|
||||
|
||||
// compile: msbuild
|
||||
// not: csc.exe /t:library /debug+ /out:SabreTools.Compression.BZip2.dll BZip2InputStream.cs BCRC32.cs Rand.cs
|
||||
|
||||
namespace SabreTools.Compression.BZip2
|
||||
{
|
||||
// /**
|
||||
// * Checks if the signature matches what is expected for a bzip2 file.
|
||||
// *
|
||||
// * @param signature
|
||||
// * the bytes to check
|
||||
// * @param length
|
||||
// * the number of bytes to check
|
||||
// * @return true, if this stream is a bzip2 compressed stream, false otherwise
|
||||
// *
|
||||
// * @since Apache Commons Compress 1.1
|
||||
// */
|
||||
// public static boolean MatchesSig(byte[] signature)
|
||||
// {
|
||||
// if ((signature.Length < 3) ||
|
||||
// (signature[0] != 'B') ||
|
||||
// (signature[1] != 'Z') ||
|
||||
// (signature[2] != 'h'))
|
||||
// return false;
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
|
||||
internal static class BZip2
|
||||
{
|
||||
internal static T[][] InitRectangularArray<T>(int d1, int d2)
|
||||
{
|
||||
var x = new T[d1][];
|
||||
for (int i = 0; i < d1; i++)
|
||||
{
|
||||
x[i] = new T[d2];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static readonly int BlockSizeMultiple = 100000;
|
||||
public static readonly int MinBlockSize = 1;
|
||||
public static readonly int MaxBlockSize = 9;
|
||||
public static readonly int MaxAlphaSize = 258;
|
||||
public static readonly int MaxCodeLength = 23;
|
||||
public static readonly char RUNA = (char)0;
|
||||
public static readonly char RUNB = (char)1;
|
||||
public static readonly int NGroups = 6;
|
||||
public static readonly int G_SIZE = 50;
|
||||
public static readonly int N_ITERS = 4;
|
||||
public static readonly int MaxSelectors = (2 + (900000 / G_SIZE));
|
||||
public static readonly int NUM_OVERSHOOT_BYTES = 20;
|
||||
/*
|
||||
* <p> If you are ever unlucky/improbable enough to get a stack
|
||||
* overflow whilst sorting, increase the following constant and
|
||||
* try again. In practice I have never seen the stack go above 27
|
||||
* elems, so the following limit seems very generous. </p>
|
||||
*/
|
||||
internal static readonly int QSORT_STACK_SIZE = 1000;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
1942
SabreTools.Compression/BZip2/BZip2Compressor.cs
Normal file
1942
SabreTools.Compression/BZip2/BZip2Compressor.cs
Normal file
File diff suppressed because it is too large
Load Diff
1386
SabreTools.Compression/BZip2/BZip2InputStream.cs
Normal file
1386
SabreTools.Compression/BZip2/BZip2InputStream.cs
Normal file
File diff suppressed because it is too large
Load Diff
530
SabreTools.Compression/BZip2/BZip2OutputStream.cs
Normal file
530
SabreTools.Compression/BZip2/BZip2OutputStream.cs
Normal file
@@ -0,0 +1,530 @@
|
||||
//#define Trace
|
||||
|
||||
// BZip2OutputStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-02 16:44:11>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the BZip2OutputStream class, which is a
|
||||
// compressing stream that handles BZIP2. This code may have been
|
||||
// derived in part from Apache commons source code. The license below
|
||||
// applies to the original Apache code.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
// flymake: csc.exe /t:module BZip2InputStream.cs BZip2Compressor.cs Rand.cs BCRC32.cs @@FILE@@
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
|
||||
// Design Notes:
|
||||
//
|
||||
// This class follows the classic Decorator pattern: it is a Stream that
|
||||
// wraps itself around a Stream, and in doing so provides bzip2
|
||||
// compression as callers Write into it.
|
||||
//
|
||||
// BZip2 is a straightforward data format: there are 4 magic bytes at
|
||||
// the top of the file, followed by 1 or more compressed blocks. There
|
||||
// is a small "magic byte" trailer after all compressed blocks. This
|
||||
// class emits the magic bytes for the header and trailer, and relies on
|
||||
// a BZip2Compressor to generate each of the compressed data blocks.
|
||||
//
|
||||
// BZip2 does byte-shredding - it uses partial fractions of bytes to
|
||||
// represent independent pieces of information. This class relies on the
|
||||
// BitWriter to adapt the bit-oriented BZip2 output to the byte-oriented
|
||||
// model of the .NET Stream class.
|
||||
//
|
||||
// ----
|
||||
//
|
||||
// Regarding the Apache code base: Most of the code in this particular
|
||||
// class is related to stream operations, and is my own code. It largely
|
||||
// does not rely on any code obtained from Apache commons. If you
|
||||
// compare this code with the Apache commons BZip2OutputStream, you will
|
||||
// see very little code that is common, except for the
|
||||
// nearly-boilerplate structure that is common to all subtypes of
|
||||
// System.IO.Stream. There may be some small remnants of code in this
|
||||
// module derived from the Apache stuff, which is why I left the license
|
||||
// in here. Most of the Apache commons compressor magic has been ported
|
||||
// into the BZip2Compressor class.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.BZip2
|
||||
{
|
||||
/// <summary>
|
||||
/// A write-only decorator stream that compresses data as it is
|
||||
/// written using the BZip2 algorithm.
|
||||
/// </summary>
|
||||
public class BZip2OutputStream : System.IO.Stream
|
||||
{
|
||||
int totalBytesWrittenIn;
|
||||
bool leaveOpen;
|
||||
BZip2Compressor compressor;
|
||||
uint combinedCRC;
|
||||
Stream output;
|
||||
BitWriter bw;
|
||||
int blockSize100k; // 0...9
|
||||
|
||||
private TraceBits desiredTrace = TraceBits.Crc | TraceBits.Write;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new <c>BZip2OutputStream</c>, that sends its
|
||||
/// compressed output to the given output stream.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name='output'>
|
||||
/// The destination stream, to which compressed output will be sent.
|
||||
/// </param>
|
||||
///
|
||||
/// <example>
|
||||
///
|
||||
/// This example reads a file, then compresses it with bzip2 file,
|
||||
/// and writes the compressed data into a newly created file.
|
||||
///
|
||||
/// <code>
|
||||
/// var fname = "logfile.log";
|
||||
/// using (var fs = File.OpenRead(fname))
|
||||
/// {
|
||||
/// var outFname = fname + ".bz2";
|
||||
/// using (var output = File.Create(outFname))
|
||||
/// {
|
||||
/// using (var compressor = new SabreTools.Compression.BZip2.BZip2OutputStream(output))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[2048];
|
||||
/// int n;
|
||||
/// while ((n = fs.Read(buffer, 0, buffer.Length)) > 0)
|
||||
/// {
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public BZip2OutputStream(Stream output)
|
||||
: this(output, BZip2.MaxBlockSize, false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new <c>BZip2OutputStream</c> with specified blocksize.
|
||||
/// </summary>
|
||||
/// <param name = "output">the destination stream.</param>
|
||||
/// <param name = "blockSize">
|
||||
/// The blockSize in units of 100000 bytes.
|
||||
/// The valid range is 1..9.
|
||||
/// </param>
|
||||
public BZip2OutputStream(Stream output, int blockSize)
|
||||
: this(output, blockSize, false)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new <c>BZip2OutputStream</c>.
|
||||
/// </summary>
|
||||
/// <param name = "output">the destination stream.</param>
|
||||
/// <param name = "leaveOpen">
|
||||
/// whether to leave the captive stream open upon closing this stream.
|
||||
/// </param>
|
||||
public BZip2OutputStream(Stream output, bool leaveOpen)
|
||||
: this(output, BZip2.MaxBlockSize, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new <c>BZip2OutputStream</c> with specified blocksize,
|
||||
/// and explicitly specifies whether to leave the wrapped stream open.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name = "output">the destination stream.</param>
|
||||
/// <param name = "blockSize">
|
||||
/// The blockSize in units of 100000 bytes.
|
||||
/// The valid range is 1..9.
|
||||
/// </param>
|
||||
/// <param name = "leaveOpen">
|
||||
/// whether to leave the captive stream open upon closing this stream.
|
||||
/// </param>
|
||||
public BZip2OutputStream(Stream output, int blockSize, bool leaveOpen)
|
||||
{
|
||||
if (blockSize < BZip2.MinBlockSize ||
|
||||
blockSize > BZip2.MaxBlockSize)
|
||||
{
|
||||
var msg = String.Format("blockSize={0} is out of range; must be between {1} and {2}",
|
||||
blockSize,
|
||||
BZip2.MinBlockSize, BZip2.MaxBlockSize);
|
||||
throw new ArgumentException(msg, "blockSize");
|
||||
}
|
||||
|
||||
this.output = output;
|
||||
if (!this.output.CanWrite)
|
||||
throw new ArgumentException("The stream is not writable.", "output");
|
||||
|
||||
this.bw = new BitWriter(this.output);
|
||||
this.blockSize100k = blockSize;
|
||||
this.compressor = new BZip2Compressor(this.bw, blockSize);
|
||||
this.leaveOpen = leaveOpen;
|
||||
this.combinedCRC = 0;
|
||||
EmitHeader();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Close the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This may or may not close the underlying stream. Check the
|
||||
/// constructors that accept a bool value.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public override void Close()
|
||||
{
|
||||
if (output != null)
|
||||
{
|
||||
Stream o = this.output;
|
||||
Finish();
|
||||
if (!leaveOpen)
|
||||
o.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Flush the stream.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
if (this.output != null)
|
||||
{
|
||||
this.bw.Flush();
|
||||
this.output.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private void EmitHeader()
|
||||
{
|
||||
var magic = new byte[] {
|
||||
(byte) 'B',
|
||||
(byte) 'Z',
|
||||
(byte) 'h',
|
||||
(byte) ('0' + this.blockSize100k)
|
||||
};
|
||||
|
||||
// not necessary to shred the initial magic bytes
|
||||
this.output.Write(magic, 0, magic.Length);
|
||||
}
|
||||
|
||||
private void EmitTrailer()
|
||||
{
|
||||
// A magic 48-bit number, 0x177245385090, to indicate the end
|
||||
// of the last block. (sqrt(pi), if you want to know)
|
||||
|
||||
TraceOutput(TraceBits.Write, "total written out: {0} (0x{0:X})",
|
||||
this.bw.TotalBytesWrittenOut);
|
||||
|
||||
// must shred
|
||||
this.bw.WriteByte(0x17);
|
||||
this.bw.WriteByte(0x72);
|
||||
this.bw.WriteByte(0x45);
|
||||
this.bw.WriteByte(0x38);
|
||||
this.bw.WriteByte(0x50);
|
||||
this.bw.WriteByte(0x90);
|
||||
|
||||
this.bw.WriteInt(this.combinedCRC);
|
||||
|
||||
this.bw.FinishAndPad();
|
||||
|
||||
TraceOutput(TraceBits.Write, "final total: {0} (0x{0:X})",
|
||||
this.bw.TotalBytesWrittenOut);
|
||||
}
|
||||
|
||||
void Finish()
|
||||
{
|
||||
// Console.WriteLine("BZip2:Finish");
|
||||
|
||||
try
|
||||
{
|
||||
var totalBefore = this.bw.TotalBytesWrittenOut;
|
||||
this.compressor.CompressAndWrite();
|
||||
TraceOutput(TraceBits.Write, "out block length (bytes): {0} (0x{0:X})",
|
||||
this.bw.TotalBytesWrittenOut - totalBefore);
|
||||
|
||||
TraceOutput(TraceBits.Crc, " combined CRC (before): {0:X8}",
|
||||
this.combinedCRC);
|
||||
this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >> 31);
|
||||
this.combinedCRC ^= (uint)compressor.Crc32;
|
||||
TraceOutput(TraceBits.Crc, " block CRC : {0:X8}",
|
||||
this.compressor.Crc32);
|
||||
TraceOutput(TraceBits.Crc, " combined CRC (final) : {0:X8}",
|
||||
this.combinedCRC);
|
||||
|
||||
EmitTrailer();
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.output = null;
|
||||
this.compressor = null;
|
||||
this.bw = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The blocksize parameter specified at construction time.
|
||||
/// </summary>
|
||||
public int BlockSize
|
||||
{
|
||||
get { return this.blockSize100k; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write data to the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// Use the <c>BZip2OutputStream</c> to compress data while writing:
|
||||
/// create a <c>BZip2OutputStream</c> with a writable output stream.
|
||||
/// Then call <c>Write()</c> on that <c>BZip2OutputStream</c>, providing
|
||||
/// uncompressed data as input. The data sent to the output stream will
|
||||
/// be the compressed form of the input data.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A <c>BZip2OutputStream</c> can be used only for <c>Write()</c> not for <c>Read()</c>.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="buffer">The buffer holding data to write to the stream.</param>
|
||||
/// <param name="offset">the offset within that data array to find the first byte to write.</param>
|
||||
/// <param name="count">the number of bytes to write.</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (offset < 0)
|
||||
throw new IndexOutOfRangeException(String.Format("offset ({0}) must be > 0", offset));
|
||||
if (count < 0)
|
||||
throw new IndexOutOfRangeException(String.Format("count ({0}) must be > 0", count));
|
||||
if (offset + count > buffer.Length)
|
||||
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
|
||||
offset, count, buffer.Length));
|
||||
if (this.output == null)
|
||||
throw new IOException("the stream is not open");
|
||||
|
||||
if (count == 0) return; // nothing to do
|
||||
|
||||
int bytesWritten = 0;
|
||||
int bytesRemaining = count;
|
||||
|
||||
do
|
||||
{
|
||||
int n = compressor.Fill(buffer, offset, bytesRemaining);
|
||||
if (n != bytesRemaining)
|
||||
{
|
||||
// The compressor data block is full. Compress and
|
||||
// write out the compressed data, then reset the
|
||||
// compressor and continue.
|
||||
|
||||
var totalBefore = this.bw.TotalBytesWrittenOut;
|
||||
this.compressor.CompressAndWrite();
|
||||
TraceOutput(TraceBits.Write, "out block length (bytes): {0} (0x{0:X})",
|
||||
this.bw.TotalBytesWrittenOut - totalBefore);
|
||||
|
||||
// and now any remaining bits
|
||||
TraceOutput(TraceBits.Write,
|
||||
" remaining: {0} 0x{1:X}",
|
||||
this.bw.NumRemainingBits,
|
||||
this.bw.RemainingBits);
|
||||
|
||||
TraceOutput(TraceBits.Crc, " combined CRC (before): {0:X8}",
|
||||
this.combinedCRC);
|
||||
this.combinedCRC = (this.combinedCRC << 1) | (this.combinedCRC >> 31);
|
||||
this.combinedCRC ^= (uint)compressor.Crc32;
|
||||
TraceOutput(TraceBits.Crc, " block CRC : {0:X8}",
|
||||
compressor.Crc32);
|
||||
TraceOutput(TraceBits.Crc, " combined CRC (after) : {0:X8}",
|
||||
this.combinedCRC);
|
||||
offset += n;
|
||||
}
|
||||
bytesRemaining -= n;
|
||||
bytesWritten += n;
|
||||
} while (bytesRemaining > 0);
|
||||
|
||||
totalBytesWrittenIn += bytesWritten;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value is always false.
|
||||
/// </remarks>
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports Seek operations.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Always returns false.
|
||||
/// </remarks>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be written.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value should always be true, unless and until the
|
||||
/// object is disposed and closed.
|
||||
/// </remarks>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.output == null) throw new ObjectDisposedException("BZip2Stream");
|
||||
return this.output.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading this property always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The position of the stream pointer.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Setting this property always throws a <see
|
||||
/// cref="NotImplementedException"/>. Reading will return the
|
||||
/// total number of uncompressed bytes written through.
|
||||
/// </remarks>
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.totalBytesWrittenIn;
|
||||
}
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="offset">this is irrelevant, since it will always throw!</param>
|
||||
/// <param name="origin">this is irrelevant, since it will always throw!</param>
|
||||
/// <returns>irrelevant!</returns>
|
||||
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">this is irrelevant, since it will always throw!</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
/// <param name='buffer'>this parameter is never used</param>
|
||||
/// <param name='offset'>this parameter is never used</param>
|
||||
/// <param name='count'>this parameter is never used</param>
|
||||
/// <returns>never returns anything; always throws</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
// used only when Trace is defined
|
||||
[Flags]
|
||||
enum TraceBits : uint
|
||||
{
|
||||
None = 0,
|
||||
Crc = 1,
|
||||
Write = 2,
|
||||
All = 0xffffffff,
|
||||
}
|
||||
|
||||
|
||||
[System.Diagnostics.ConditionalAttribute("Trace")]
|
||||
private void TraceOutput(TraceBits bits, string format, params object[] varParams)
|
||||
{
|
||||
if ((bits & this.desiredTrace) != 0)
|
||||
{
|
||||
//lock(outputLock)
|
||||
{
|
||||
int tid = System.Threading.Thread.CurrentThread.GetHashCode();
|
||||
#if !SILVERLIGHT && !NETCF
|
||||
Console.ForegroundColor = (ConsoleColor)(tid % 8 + 10);
|
||||
#endif
|
||||
Console.Write("{0:000} PBOS ", tid);
|
||||
Console.WriteLine(format, varParams);
|
||||
#if !SILVERLIGHT && !NETCF
|
||||
Console.ResetColor();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
246
SabreTools.Compression/BZip2/BitWriter.cs
Normal file
246
SabreTools.Compression/BZip2/BitWriter.cs
Normal file
@@ -0,0 +1,246 @@
|
||||
// BitWriter.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-July-25 18:57:31>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the BitWriter class, which writes bits at a time
|
||||
// to an output stream. It's used by the BZip2Compressor class, and by
|
||||
// the BZip2OutputStream class and its parallel variant,
|
||||
// ParallelBZip2OutputStream.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
//
|
||||
// Design notes:
|
||||
//
|
||||
// BZip2 employs byte-shredding in its data format - rather than
|
||||
// aligning all data items in a compressed .bz2 file on byte barriers,
|
||||
// the BZip2 format uses portions of bytes to represent independent
|
||||
// pieces of information. This "shredding" starts with the first
|
||||
// "randomised" bit - just 12 bytes or so into a bz2 file or stream. But
|
||||
// the approach is used extensively in bzip2 files - sometimes 5 bits
|
||||
// are used, sometimes 24 or 3 bits, sometimes just 1 bit, and so on.
|
||||
// It's not possible to send this information directly to a stream in
|
||||
// this form; Streams in .NET accept byte-oriented input. Therefore,
|
||||
// when actually writing a bz2 file, the output data must be organized
|
||||
// into a byte-aligned format before being written to the output stream.
|
||||
//
|
||||
// This BitWriter class provides the byte-shredding necessary for BZip2
|
||||
// output. Think of this class as an Adapter that enables Bit-oriented
|
||||
// output to a standard byte-oriented .NET stream. This class writes
|
||||
// data out to the captive output stream only after the data bits have
|
||||
// been accumulated and aligned. For example, suppose that during
|
||||
// operation, the BZip2 compressor emits 5 bits, then 24 bits, then 32
|
||||
// bits. When the first 5 bits are sent to the BitWriter, nothing is
|
||||
// written to the output stream; instead these 5 bits are simply stored
|
||||
// in the internal accumulator. When the next 24 bits are written, the
|
||||
// first 3 bits are gathered with the accumulated bits. The resulting
|
||||
// 5+3 constitutes an entire byte; the BitWriter then actually writes
|
||||
// that byte to the output stream. This leaves 21 bits. BitWriter writes
|
||||
// 2 more whole bytes (16 more bits), in 8-bit chunks, leaving 5 in the
|
||||
// accumulator. BitWriter then follows the same procedure with the 32
|
||||
// new bits. And so on.
|
||||
//
|
||||
// A quick tour of the implementation:
|
||||
//
|
||||
// The accumulator is a uint - so it can accumulate at most 4 bytes of
|
||||
// information. In practice because of the design of this class, it
|
||||
// never accumulates more than 3 bytes.
|
||||
//
|
||||
// The Flush() method emits all whole bytes available. After calling
|
||||
// Flush(), there may be between 0-7 bits yet to be emitted into the
|
||||
// output stream.
|
||||
//
|
||||
// FinishAndPad() emits all data, including the last partial byte and
|
||||
// any necessary padding. In effect, it establishes a byte-alignment
|
||||
// barrier. To support bzip2, FinishAndPad() should be called only once
|
||||
// for a bz2 file, after the last bit of data has been written through
|
||||
// this adapter. Other binary file formats may use byte-alignment at
|
||||
// various points within the file, and FinishAndPad() would support that
|
||||
// scenario.
|
||||
//
|
||||
// The internal fn Reset() is used to reset the state of the adapter;
|
||||
// this class is used by BZip2Compressor, instances of which get re-used
|
||||
// by multiple distinct threads, for different blocks of data.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SabreTools.Compression.BZip2
|
||||
{
|
||||
internal class BitWriter
|
||||
{
|
||||
uint accumulator;
|
||||
int nAccumulatedBits;
|
||||
Stream output;
|
||||
int totalBytesWrittenOut;
|
||||
|
||||
public BitWriter(Stream s)
|
||||
{
|
||||
this.output = s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delivers the remaining bits, left-aligned, in a byte.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This is valid only if NumRemainingBits is less than 8;
|
||||
/// in other words it is valid only after a call to Flush().
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public byte RemainingBits
|
||||
{
|
||||
get
|
||||
{
|
||||
return (byte)(this.accumulator >> (32 - this.nAccumulatedBits) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
public int NumRemainingBits
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.nAccumulatedBits;
|
||||
}
|
||||
}
|
||||
|
||||
public int TotalBytesWrittenOut
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.totalBytesWrittenOut;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the BitWriter.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This is useful when the BitWriter writes into a MemoryStream, and
|
||||
/// is used by a BZip2Compressor, which itself is re-used for multiple
|
||||
/// distinct data blocks.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void Reset()
|
||||
{
|
||||
this.accumulator = 0;
|
||||
this.nAccumulatedBits = 0;
|
||||
this.totalBytesWrittenOut = 0;
|
||||
this.output.Seek(0, SeekOrigin.Begin);
|
||||
this.output.SetLength(0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write some number of bits from the given value, into the output.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The nbits value should be a max of 25, for safety. For performance
|
||||
/// reasons, this method does not check!
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void WriteBits(int nbits, uint value)
|
||||
{
|
||||
int nAccumulated = this.nAccumulatedBits;
|
||||
uint u = this.accumulator;
|
||||
|
||||
while (nAccumulated >= 8)
|
||||
{
|
||||
this.output.WriteByte((byte)(u >> 24 & 0xff));
|
||||
this.totalBytesWrittenOut++;
|
||||
u <<= 8;
|
||||
nAccumulated -= 8;
|
||||
}
|
||||
|
||||
this.accumulator = u | (value << (32 - nAccumulated - nbits));
|
||||
this.nAccumulatedBits = nAccumulated + nbits;
|
||||
|
||||
// Console.WriteLine("WriteBits({0}, 0x{1:X2}) => {2:X8} n({3})",
|
||||
// nbits, value, accumulator, nAccumulatedBits);
|
||||
// Console.ReadLine();
|
||||
|
||||
// At this point the accumulator may contain up to 31 bits waiting for
|
||||
// output.
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write a full 8-bit byte into the output.
|
||||
/// </summary>
|
||||
public void WriteByte(byte b)
|
||||
{
|
||||
WriteBits(8, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write four 8-bit bytes into the output.
|
||||
/// </summary>
|
||||
public void WriteInt(uint u)
|
||||
{
|
||||
WriteBits(8, (u >> 24) & 0xff);
|
||||
WriteBits(8, (u >> 16) & 0xff);
|
||||
WriteBits(8, (u >> 8) & 0xff);
|
||||
WriteBits(8, u & 0xff);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write all available byte-aligned bytes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method writes no new output, but flushes any accumulated
|
||||
/// bits. At completion, the accumulator may contain up to 7
|
||||
/// bits.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This is necessary when re-assembling output from N independent
|
||||
/// compressors, one for each of N blocks. The output of any
|
||||
/// particular compressor will in general have some fragment of a byte
|
||||
/// remaining. This fragment needs to be accumulated into the
|
||||
/// parent BZip2OutputStream.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void Flush()
|
||||
{
|
||||
WriteBits(0, 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes all available bytes, and emits padding for the final byte as
|
||||
/// necessary. This must be the last method invoked on an instance of
|
||||
/// BitWriter.
|
||||
/// </summary>
|
||||
public void FinishAndPad()
|
||||
{
|
||||
Flush();
|
||||
|
||||
if (this.NumRemainingBits > 0)
|
||||
{
|
||||
byte b = (byte)((this.accumulator >> 24) & 0xff);
|
||||
this.output.WriteByte(b);
|
||||
this.totalBytesWrittenOut++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
815
SabreTools.Compression/BZip2/CRC32.cs
Normal file
815
SabreTools.Compression/BZip2/CRC32.cs
Normal file
@@ -0,0 +1,815 @@
|
||||
// CRC32.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-02 18:25:54>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the CRC32 class, which can do the CRC32 algorithm, using
|
||||
// arbitrary starting polynomials, and bit reversal. The bit reversal is what
|
||||
// distinguishes this CRC-32 used in BZip2 from the CRC-32 that is used in PKZIP
|
||||
// files, or GZIP files. This class does both.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using Interop = System.Runtime.InteropServices;
|
||||
|
||||
#nullable disable
|
||||
#pragma warning disable CS0618
|
||||
namespace SabreTools.Compression.BZip2
|
||||
{
|
||||
/// <summary>
|
||||
/// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
|
||||
/// can set the polynomial and enable or disable bit
|
||||
/// reversal. This can be used for GZIP, BZip2, or ZIP.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type is used internally by DotNetZip; it is generally not used
|
||||
/// directly by applications wishing to create, read, or manipulate zip
|
||||
/// archive files.
|
||||
/// </remarks>
|
||||
|
||||
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000C")]
|
||||
[Interop.ComVisible(true)]
|
||||
#if !NETCF
|
||||
[Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
|
||||
#endif
|
||||
public class CRC32
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates the total number of bytes applied to the CRC.
|
||||
/// </summary>
|
||||
public Int64 TotalBytesRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return _TotalBytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the current CRC for all blocks slurped in.
|
||||
/// </summary>
|
||||
public Int32 Crc32Result
|
||||
{
|
||||
get
|
||||
{
|
||||
return unchecked((Int32)(~_register));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the CRC32 for the specified stream.
|
||||
/// </summary>
|
||||
/// <param name="input">The stream over which to calculate the CRC32</param>
|
||||
/// <returns>the CRC32 calculation</returns>
|
||||
public Int32 GetCrc32(System.IO.Stream input)
|
||||
{
|
||||
return GetCrc32AndCopy(input, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the CRC32 for the specified stream, and writes the input into the
|
||||
/// output stream.
|
||||
/// </summary>
|
||||
/// <param name="input">The stream over which to calculate the CRC32</param>
|
||||
/// <param name="output">The stream into which to deflate the input</param>
|
||||
/// <returns>the CRC32 calculation</returns>
|
||||
public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
|
||||
{
|
||||
if (input == null)
|
||||
throw new Exception("The input stream must not be null.");
|
||||
|
||||
unchecked
|
||||
{
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int readSize = BUFFER_SIZE;
|
||||
|
||||
_TotalBytesRead = 0;
|
||||
int count = input.Read(buffer, 0, readSize);
|
||||
if (output != null) output.Write(buffer, 0, count);
|
||||
_TotalBytesRead += count;
|
||||
while (count > 0)
|
||||
{
|
||||
SlurpBlock(buffer, 0, count);
|
||||
count = input.Read(buffer, 0, readSize);
|
||||
if (output != null) output.Write(buffer, 0, count);
|
||||
_TotalBytesRead += count;
|
||||
}
|
||||
|
||||
return (Int32)(~_register);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get the CRC32 for the given (word,byte) combo. This is a
|
||||
/// computation defined by PKzip for PKZIP 2.0 (weak) encryption.
|
||||
/// </summary>
|
||||
/// <param name="W">The word to start with.</param>
|
||||
/// <param name="B">The byte to combine it with.</param>
|
||||
/// <returns>The CRC-ized result.</returns>
|
||||
public Int32 ComputeCrc32(Int32 W, byte B)
|
||||
{
|
||||
return _InternalComputeCrc32((UInt32)W, B);
|
||||
}
|
||||
|
||||
internal Int32 _InternalComputeCrc32(UInt32 W, byte B)
|
||||
{
|
||||
return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Update the value for the running CRC32 using the given block of bytes.
|
||||
/// This is useful when using the CRC32() class in a Stream.
|
||||
/// </summary>
|
||||
/// <param name="block">block of bytes to slurp</param>
|
||||
/// <param name="offset">starting point in the block</param>
|
||||
/// <param name="count">how many bytes within the block to slurp</param>
|
||||
public void SlurpBlock(byte[] block, int offset, int count)
|
||||
{
|
||||
if (block == null)
|
||||
throw new Exception("The data buffer must not be null.");
|
||||
|
||||
// bzip algorithm
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int x = offset + i;
|
||||
byte b = block[x];
|
||||
if (this.reverseBits)
|
||||
{
|
||||
UInt32 temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[temp];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[temp];
|
||||
}
|
||||
}
|
||||
_TotalBytesRead += count;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Process one byte in the CRC.
|
||||
/// </summary>
|
||||
/// <param name = "b">the byte to include into the CRC . </param>
|
||||
public void UpdateCRC(byte b)
|
||||
{
|
||||
if (this.reverseBits)
|
||||
{
|
||||
UInt32 temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[temp];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[temp];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process a run of N identical bytes into the CRC.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method serves as an optimization for updating the CRC when a
|
||||
/// run of identical bytes is found. Rather than passing in a buffer of
|
||||
/// length n, containing all identical bytes b, this method accepts the
|
||||
/// byte value and the length of the (virtual) buffer - the length of
|
||||
/// the run.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name = "b">the byte to include into the CRC. </param>
|
||||
/// <param name = "n">the number of times that byte should be repeated. </param>
|
||||
public void UpdateCRC(byte b, int n)
|
||||
{
|
||||
while (n-- > 0)
|
||||
{
|
||||
if (this.reverseBits)
|
||||
{
|
||||
uint temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[(temp >= 0)
|
||||
? temp
|
||||
: (temp + 256)];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[(temp >= 0)
|
||||
? temp
|
||||
: (temp + 256)];
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static uint ReverseBits(uint data)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint ret = data;
|
||||
ret = (ret & 0x55555555) << 1 | (ret >> 1) & 0x55555555;
|
||||
ret = (ret & 0x33333333) << 2 | (ret >> 2) & 0x33333333;
|
||||
ret = (ret & 0x0F0F0F0F) << 4 | (ret >> 4) & 0x0F0F0F0F;
|
||||
ret = (ret << 24) | ((ret & 0xFF00) << 8) | ((ret >> 8) & 0xFF00) | (ret >> 24);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte ReverseBits(byte data)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint u = (uint)data * 0x00020202;
|
||||
uint m = 0x01044010;
|
||||
uint s = u & m;
|
||||
uint t = (u << 2) & (m << 1);
|
||||
return (byte)((0x01001001 * (s + t)) >> 24);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void GenerateLookupTable()
|
||||
{
|
||||
crc32Table = new UInt32[256];
|
||||
unchecked
|
||||
{
|
||||
UInt32 dwCrc;
|
||||
byte i = 0;
|
||||
do
|
||||
{
|
||||
dwCrc = i;
|
||||
for (byte j = 8; j > 0; j--)
|
||||
{
|
||||
if ((dwCrc & 1) == 1)
|
||||
{
|
||||
dwCrc = (dwCrc >> 1) ^ dwPolynomial;
|
||||
}
|
||||
else
|
||||
{
|
||||
dwCrc >>= 1;
|
||||
}
|
||||
}
|
||||
if (reverseBits)
|
||||
{
|
||||
crc32Table[ReverseBits(i)] = ReverseBits(dwCrc);
|
||||
}
|
||||
else
|
||||
{
|
||||
crc32Table[i] = dwCrc;
|
||||
}
|
||||
i++;
|
||||
} while (i!=0);
|
||||
}
|
||||
|
||||
#if VERBOSE
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("private static readonly UInt32[] crc32Table = {");
|
||||
for (int i = 0; i < crc32Table.Length; i+=4)
|
||||
{
|
||||
Console.Write(" ");
|
||||
for (int j=0; j < 4; j++)
|
||||
{
|
||||
Console.Write(" 0x{0:X8}U,", crc32Table[i+j]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.WriteLine("};");
|
||||
Console.WriteLine();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
private uint gf2_matrix_times(uint[] matrix, uint vec)
|
||||
{
|
||||
uint sum = 0;
|
||||
int i=0;
|
||||
while (vec != 0)
|
||||
{
|
||||
if ((vec & 0x01)== 0x01)
|
||||
sum ^= matrix[i];
|
||||
vec >>= 1;
|
||||
i++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private void gf2_matrix_square(uint[] square, uint[] mat)
|
||||
{
|
||||
for (int i = 0; i < 32; i++)
|
||||
square[i] = gf2_matrix_times(mat, mat[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Combines the given CRC32 value with the current running total.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is useful when using a divide-and-conquer approach to
|
||||
/// calculating a CRC. Multiple threads can each calculate a
|
||||
/// CRC32 on a segment of the data, and then combine the
|
||||
/// individual CRC32 values at the end.
|
||||
/// </remarks>
|
||||
/// <param name="crc">the crc value to be combined with this one</param>
|
||||
/// <param name="length">the length of data the CRC value was calculated on</param>
|
||||
public void Combine(int crc, int length)
|
||||
{
|
||||
uint[] even = new uint[32]; // even-power-of-two zeros operator
|
||||
uint[] odd = new uint[32]; // odd-power-of-two zeros operator
|
||||
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
uint crc1= ~_register;
|
||||
uint crc2= (uint) crc;
|
||||
|
||||
// put operator for one zero bit in odd
|
||||
odd[0] = this.dwPolynomial; // the CRC-32 polynomial
|
||||
uint row = 1;
|
||||
for (int i = 1; i < 32; i++)
|
||||
{
|
||||
odd[i] = row;
|
||||
row <<= 1;
|
||||
}
|
||||
|
||||
// put operator for two zero bits in even
|
||||
gf2_matrix_square(even, odd);
|
||||
|
||||
// put operator for four zero bits in odd
|
||||
gf2_matrix_square(odd, even);
|
||||
|
||||
uint len2 = (uint) length;
|
||||
|
||||
// apply len2 zeros to crc1 (first square will put the operator for one
|
||||
// zero byte, eight zero bits, in even)
|
||||
do {
|
||||
// apply zeros operator for this bit of len2
|
||||
gf2_matrix_square(even, odd);
|
||||
|
||||
if ((len2 & 1)== 1)
|
||||
crc1 = gf2_matrix_times(even, crc1);
|
||||
len2 >>= 1;
|
||||
|
||||
if (len2 == 0)
|
||||
break;
|
||||
|
||||
// another iteration of the loop with odd and even swapped
|
||||
gf2_matrix_square(odd, even);
|
||||
if ((len2 & 1)==1)
|
||||
crc1 = gf2_matrix_times(odd, crc1);
|
||||
len2 >>= 1;
|
||||
|
||||
|
||||
} while (len2 != 0);
|
||||
|
||||
crc1 ^= crc2;
|
||||
|
||||
_register= ~crc1;
|
||||
|
||||
//return (int) crc1;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class using the default settings: no
|
||||
/// bit reversal, and a polynomial of 0xEDB88320.
|
||||
/// </summary>
|
||||
public CRC32() : this(false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class, specifying whether to reverse
|
||||
/// data bits or not.
|
||||
/// </summary>
|
||||
/// <param name='reverseBits'>
|
||||
/// specify true if the instance should reverse data bits.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
||||
/// want a CRC32 with compatibility with BZip2, you should pass true
|
||||
/// here. In the CRC-32 used by GZIP and PKZIP, the bits are not
|
||||
/// reversed; Therefore if you want a CRC32 with compatibility with
|
||||
/// those, you should pass false.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public CRC32(bool reverseBits) :
|
||||
this( unchecked((int)0xEDB88320), reverseBits)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class, specifying the polynomial and
|
||||
/// whether to reverse data bits or not.
|
||||
/// </summary>
|
||||
/// <param name='polynomial'>
|
||||
/// The polynomial to use for the CRC, expressed in the reversed (LSB)
|
||||
/// format: the highest ordered bit in the polynomial value is the
|
||||
/// coefficient of the 0th power; the second-highest order bit is the
|
||||
/// coefficient of the 1 power, and so on. Expressed this way, the
|
||||
/// polynomial for the CRC-32C used in IEEE 802.3, is 0xEDB88320.
|
||||
/// </param>
|
||||
/// <param name='reverseBits'>
|
||||
/// specify true if the instance should reverse data bits.
|
||||
/// </param>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
||||
/// want a CRC32 with compatibility with BZip2, you should pass true
|
||||
/// here for the <c>reverseBits</c> parameter. In the CRC-32 used by
|
||||
/// GZIP and PKZIP, the bits are not reversed; Therefore if you want a
|
||||
/// CRC32 with compatibility with those, you should pass false for the
|
||||
/// <c>reverseBits</c> parameter.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public CRC32(int polynomial, bool reverseBits)
|
||||
{
|
||||
this.reverseBits = reverseBits;
|
||||
this.dwPolynomial = (uint) polynomial;
|
||||
this.GenerateLookupTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the CRC-32 class - clear the CRC "remainder register."
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Use this when employing a single instance of this class to compute
|
||||
/// multiple, distinct CRCs on multiple, distinct data blocks.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void Reset()
|
||||
{
|
||||
_register = 0xFFFFFFFFU;
|
||||
}
|
||||
|
||||
// private member vars
|
||||
private UInt32 dwPolynomial;
|
||||
private Int64 _TotalBytesRead;
|
||||
private bool reverseBits;
|
||||
private UInt32[] crc32Table;
|
||||
private const int BUFFER_SIZE = 8192;
|
||||
private UInt32 _register = 0xFFFFFFFFU;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A Stream that calculates a CRC32 (a checksum) on all bytes read,
|
||||
/// or on all bytes written.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This class can be used to verify the CRC of a ZipEntry when
|
||||
/// reading from a stream, or to calculate a CRC when writing to a
|
||||
/// stream. The stream should be used to either read, or write, but
|
||||
/// not both. If you intermix reads and writes, the results are not
|
||||
/// defined.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This class is intended primarily for use internally by the
|
||||
/// DotNetZip library.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class CrcCalculatorStream : System.IO.Stream, System.IDisposable
|
||||
{
|
||||
private static readonly Int64 UnsetLengthLimit = -99;
|
||||
|
||||
internal System.IO.Stream _innerStream;
|
||||
private CRC32 _Crc32;
|
||||
private Int64 _lengthLimit = -99;
|
||||
private bool _leaveOpen;
|
||||
|
||||
/// <summary>
|
||||
/// The default constructor.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Instances returned from this constructor will leave the underlying
|
||||
/// stream open upon Close(). The stream uses the default CRC32
|
||||
/// algorithm, which implies a polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream)
|
||||
: this(true, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor allows the caller to specify how to handle the
|
||||
/// underlying stream at close.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, bool leaveOpen)
|
||||
: this(leaveOpen, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Instances returned from this constructor will leave the underlying
|
||||
/// stream open upon Close().
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length)
|
||||
: this(true, length, stream, null)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read, as well as whether to keep the underlying stream open upon
|
||||
/// Close().
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen)
|
||||
: this(leaveOpen, length, stream, null)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read, as well as whether to keep the underlying stream open upon
|
||||
/// Close(), and the CRC32 instance to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the specified CRC32 instance, which allows the
|
||||
/// application to specify how the CRC gets calculated.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
/// <param name="crc32">the CRC32 instance to use to calculate the CRC32</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen,
|
||||
CRC32 crc32)
|
||||
: this(leaveOpen, length, stream, crc32)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
|
||||
// This ctor is private - no validation is done here. This is to allow the use
|
||||
// of a (specific) negative value for the _lengthLimit, to indicate that there
|
||||
// is no length set. So we validate the length limit in those ctors that use an
|
||||
// explicit param, otherwise we don't validate, because it could be our special
|
||||
// value.
|
||||
private CrcCalculatorStream
|
||||
(bool leaveOpen, Int64 length, System.IO.Stream stream, CRC32 crc32)
|
||||
: base()
|
||||
{
|
||||
_innerStream = stream;
|
||||
_Crc32 = crc32 ?? new CRC32();
|
||||
_lengthLimit = length;
|
||||
_leaveOpen = leaveOpen;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of bytes run through the CRC32 calculator.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// This is either the total number of bytes read, or the total number of
|
||||
/// bytes written, depending on the direction of this stream.
|
||||
/// </remarks>
|
||||
public Int64 TotalBytesSlurped
|
||||
{
|
||||
get { return _Crc32.TotalBytesRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides the current CRC for all blocks slurped in.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The running total of the CRC is kept as data is written or read
|
||||
/// through the stream. read this property after all reads or writes to
|
||||
/// get an accurate CRC for the entire stream.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public Int32 Crc
|
||||
{
|
||||
get { return _Crc32.Crc32Result; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the underlying stream will be left open when the
|
||||
/// <c>CrcCalculatorStream</c> is Closed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Set this at any point before calling <see cref="Close()"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public bool LeaveOpen
|
||||
{
|
||||
get { return _leaveOpen; }
|
||||
set { _leaveOpen = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read from the stream
|
||||
/// </summary>
|
||||
/// <param name="buffer">the buffer to read</param>
|
||||
/// <param name="offset">the offset at which to start</param>
|
||||
/// <param name="count">the number of bytes to read</param>
|
||||
/// <returns>the number of bytes actually read</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int bytesToRead = count;
|
||||
|
||||
// Need to limit the # of bytes returned, if the stream is intended to have
|
||||
// a definite length. This is especially useful when returning a stream for
|
||||
// the uncompressed data directly to the application. The app won't
|
||||
// necessarily read only the UncompressedSize number of bytes. For example
|
||||
// wrapping the stream returned from OpenReader() into a StreadReader() and
|
||||
// calling ReadToEnd() on it, We can "over-read" the zip data and get a
|
||||
// corrupt string. The length limits that, prevents that problem.
|
||||
|
||||
if (_lengthLimit != CrcCalculatorStream.UnsetLengthLimit)
|
||||
{
|
||||
if (_Crc32.TotalBytesRead >= _lengthLimit) return 0; // EOF
|
||||
Int64 bytesRemaining = _lengthLimit - _Crc32.TotalBytesRead;
|
||||
if (bytesRemaining < count) bytesToRead = (int)bytesRemaining;
|
||||
}
|
||||
int n = _innerStream.Read(buffer, offset, bytesToRead);
|
||||
if (n > 0) _Crc32.SlurpBlock(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write to the stream.
|
||||
/// </summary>
|
||||
/// <param name="buffer">the buffer from which to write</param>
|
||||
/// <param name="offset">the offset at which to start writing</param>
|
||||
/// <param name="count">the number of bytes to write</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (count > 0) _Crc32.SlurpBlock(buffer, offset, count);
|
||||
_innerStream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports reading.
|
||||
/// </summary>
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return _innerStream.CanRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports seeking.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Always returns false.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports writing.
|
||||
/// </summary>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return _innerStream.CanWrite; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the stream.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
_innerStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the length of the underlying stream.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lengthLimit == CrcCalculatorStream.UnsetLengthLimit)
|
||||
return _innerStream.Length;
|
||||
else return _lengthLimit;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The getter for this property returns the total bytes read.
|
||||
/// If you use the setter, it will throw
|
||||
/// <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
public override long Position
|
||||
{
|
||||
get { return _Crc32.TotalBytesRead; }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeking is not supported on this stream. This method always throws
|
||||
/// <see cref="NotSupportedException"/>
|
||||
/// </summary>
|
||||
/// <param name="offset">N/A</param>
|
||||
/// <param name="origin">N/A</param>
|
||||
/// <returns>N/A</returns>
|
||||
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method always throws
|
||||
/// <see cref="NotSupportedException"/>
|
||||
/// </summary>
|
||||
/// <param name="value">N/A</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the stream.
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
if (!_leaveOpen)
|
||||
_innerStream.Close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
99
SabreTools.Compression/BZip2/Rand.cs
Normal file
99
SabreTools.Compression/BZip2/Rand.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
// Rand.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-July-31 15:09:16>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines a helper class for the BZip2 classes. This code
|
||||
// is derived from the original BZip2 source code.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace SabreTools.Compression.BZip2
|
||||
{
|
||||
internal static class Rand
|
||||
{
|
||||
private static int[] RNUMS =
|
||||
{
|
||||
619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
|
||||
985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
|
||||
733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
|
||||
419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
|
||||
878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
|
||||
862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
|
||||
150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
|
||||
170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
|
||||
73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
|
||||
909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
|
||||
641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
|
||||
161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
|
||||
382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
|
||||
98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
|
||||
227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
|
||||
469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
|
||||
184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
|
||||
715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
|
||||
951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
|
||||
652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
|
||||
645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
|
||||
609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
|
||||
653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
|
||||
411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
|
||||
170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
|
||||
857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
|
||||
669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
|
||||
944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
|
||||
344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
|
||||
897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
|
||||
433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
|
||||
686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
|
||||
946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
|
||||
978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
|
||||
680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
|
||||
707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
|
||||
297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
|
||||
134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
|
||||
343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
|
||||
140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
|
||||
170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
|
||||
369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
|
||||
804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
|
||||
896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
|
||||
661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
|
||||
768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
|
||||
61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
|
||||
372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
|
||||
780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
|
||||
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
|
||||
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
|
||||
936, 638
|
||||
};
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the "random" number at a specific index.
|
||||
/// </summary>
|
||||
/// <param name='i'>the index</param>
|
||||
/// <returns>the random number</returns>
|
||||
internal static int Rnums(int i)
|
||||
{
|
||||
return RNUMS[i];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
15
SabreTools.Compression/Blast/Constants.cs
Normal file
15
SabreTools.Compression/Blast/Constants.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace SabreTools.Compression.Blast
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Maximum code length
|
||||
/// </summary>
|
||||
public const int MAXBITS = 13;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum window size
|
||||
/// </summary>
|
||||
public const int MAXWIN = 4096;
|
||||
}
|
||||
}
|
||||
287
SabreTools.Compression/Blast/Decompressor.cs
Normal file
287
SabreTools.Compression/Blast/Decompressor.cs
Normal file
@@ -0,0 +1,287 @@
|
||||
/* blast.c
|
||||
* Copyright (C) 2003, 2012, 2013 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in blast.h
|
||||
* version 1.3, 24 Aug 2013
|
||||
*
|
||||
* blast.c decompresses data compressed by the PKWare Compression Library.
|
||||
* This function provides functionality similar to the explode() function of
|
||||
* the PKWare library, hence the name "blast".
|
||||
*
|
||||
* This decompressor is based on the excellent format description provided by
|
||||
* Ben Rudiak-Gould in comp.compression on August 13, 2001. Interestingly, the
|
||||
* example Ben provided in the post is incorrect. The distance 110001 should
|
||||
* instead be 111000. When corrected, the example byte stream becomes:
|
||||
*
|
||||
* 00 04 82 24 25 8f 80 7f
|
||||
*
|
||||
* which decompresses to "AIAIAIAIAIAIA" (without the quotes).
|
||||
*/
|
||||
|
||||
/*
|
||||
* Change history:
|
||||
*
|
||||
* 1.0 12 Feb 2003 - First version
|
||||
* 1.1 16 Feb 2003 - Fixed distance check for > 4 GB uncompressed data
|
||||
* 1.2 24 Oct 2012 - Add note about using binary mode in stdio
|
||||
* - Fix comparisons of differently signed integers
|
||||
* 1.3 24 Aug 2013 - Return unused input from blast()
|
||||
* - Fix test code to correctly report unused input
|
||||
* - Enable the provision of initial input to blast()
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using static SabreTools.Compression.Blast.Constants;
|
||||
|
||||
namespace SabreTools.Compression.Blast
|
||||
{
|
||||
/// <summary>
|
||||
/// blast() decompresses the PKWare Data Compression Library (DCL) compressed
|
||||
/// format. It provides the same functionality as the explode() function in
|
||||
/// that library. (Note: PKWare overused the "implode" verb, and the format
|
||||
/// used by their library implode() function is completely different and
|
||||
/// incompatible with the implode compression method supported by PKZIP.)
|
||||
///
|
||||
/// The binary mode for stdio functions should be used to assure that the
|
||||
/// compressed data is not corrupted when read or written. For example:
|
||||
/// fopen(..., "rb") and fopen(..., "wb").
|
||||
/// </summary>
|
||||
public unsafe class Decompressor
|
||||
{
|
||||
#region Huffman Encoding
|
||||
|
||||
/// <summary>
|
||||
/// Literal code
|
||||
/// </summary>
|
||||
private readonly Huffman litcode = new(MAXBITS + 1, 256);
|
||||
|
||||
/// <summary>
|
||||
/// Length code
|
||||
/// </summary>
|
||||
private readonly Huffman lencode = new(MAXBITS + 1, 16);
|
||||
|
||||
/// <summary>
|
||||
/// Distance code
|
||||
/// </summary>
|
||||
private readonly Huffman distcode = new(MAXBITS + 1, 64);
|
||||
|
||||
/// <summary>
|
||||
/// Base for length codes
|
||||
/// </summary>
|
||||
private static readonly short[] baseLength =
|
||||
[
|
||||
3, 2, 4, 5, 6, 7, 8, 9, 10, 12, 16, 24, 40, 72, 136, 264
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Extra bits for length codes
|
||||
/// </summary>
|
||||
private static readonly byte[] extra =
|
||||
[
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8
|
||||
];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a Blast decompressor
|
||||
/// </summary>
|
||||
private Decompressor()
|
||||
{
|
||||
// Repeated code lengths of literal codes
|
||||
byte[] litlen =
|
||||
[
|
||||
11, 124, 8, 7, 28, 7, 188, 13, 76, 4, 10, 8, 12, 10, 12, 10, 8, 23, 8,
|
||||
9, 7, 6, 7, 8, 7, 6, 55, 8, 23, 24, 12, 11, 7, 9, 11, 12, 6, 7, 22, 5,
|
||||
7, 24, 6, 11, 9, 6, 7, 22, 7, 11, 38, 7, 9, 8, 25, 11, 8, 11, 9, 12,
|
||||
8, 12, 5, 38, 5, 38, 5, 11, 7, 5, 6, 21, 6, 10, 53, 8, 7, 24, 10, 27,
|
||||
44, 253, 253, 253, 252, 252, 252, 13, 12, 45, 12, 45, 12, 61, 12, 45,
|
||||
44, 173
|
||||
];
|
||||
litcode.Initialize(litlen);
|
||||
|
||||
// Repeated code lengths of length codes 0..15
|
||||
byte[] lenlen =
|
||||
[
|
||||
2, 35, 36, 53, 38, 23
|
||||
];
|
||||
lencode.Initialize(lenlen);
|
||||
|
||||
// Repeated code lengths of distance codes 0..63
|
||||
byte[] distlen =
|
||||
[
|
||||
2, 20, 53, 230, 247, 151, 248
|
||||
];
|
||||
distcode.Initialize(distlen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Blast decompressor
|
||||
/// </summary>
|
||||
public static Decompressor Create() => new();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Decompress source data to an output stream
|
||||
/// </summary>
|
||||
public bool CopyTo(byte[] source, Stream dest)
|
||||
=> CopyTo(new MemoryStream(source), dest);
|
||||
|
||||
/// <summary>
|
||||
/// Decompress source data to an output stream
|
||||
/// </summary>
|
||||
public bool CopyTo(Stream source, Stream dest)
|
||||
{
|
||||
// Ignore unwritable streams
|
||||
if (!dest.CanWrite)
|
||||
return false;
|
||||
|
||||
// Input/output state
|
||||
var state = new State(source, dest);
|
||||
|
||||
// Attempt to decompress using the above state
|
||||
int err;
|
||||
try
|
||||
{
|
||||
err = Decompress(state);
|
||||
}
|
||||
catch (IndexOutOfRangeException)
|
||||
{
|
||||
// This was originally a jump, which is bad form for C#
|
||||
err = 2;
|
||||
}
|
||||
|
||||
// Write any leftover output and update the error code if needed
|
||||
if (err != 1 && state.Next != 0 && !state.ProcessOutput() && err == 0)
|
||||
err = 1;
|
||||
|
||||
return err == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decode PKWare Compression Library stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// First byte is 0 if literals are uncoded or 1 if they are coded. Second
|
||||
/// byte is 4, 5, or 6 for the number of extra bits in the distance code.
|
||||
/// This is the base-2 logarithm of the dictionary size minus six.
|
||||
///
|
||||
/// Compressed data is a combination of literals and length/distance pairs
|
||||
/// terminated by an end code. Literals are either Huffman coded or
|
||||
/// uncoded bytes. A length/distance pair is a coded length followed by a
|
||||
/// coded distance to represent a string that occurs earlier in the
|
||||
/// uncompressed data that occurs again at the current location.
|
||||
///
|
||||
/// A bit preceding a literal or length/distance pair indicates which comes
|
||||
/// next, 0 for literals, 1 for length/distance.
|
||||
///
|
||||
/// If literals are uncoded, then the next eight bits are the literal, in the
|
||||
/// normal bit order in the stream, i.e. no bit-reversal is needed. Similarly,
|
||||
/// no bit reversal is needed for either the length extra bits or the distance
|
||||
/// extra bits.
|
||||
///
|
||||
/// Literal bytes are simply written to the output. A length/distance pair is
|
||||
/// an instruction to copy previously uncompressed bytes to the output. The
|
||||
/// copy is from distance bytes back in the output stream, copying for length
|
||||
/// bytes.
|
||||
///
|
||||
/// Distances pointing before the beginning of the output data are not
|
||||
/// permitted.
|
||||
///
|
||||
/// Overlapped copies, where the length is greater than the distance, are
|
||||
/// allowed and common. For example, a distance of one and a length of 518
|
||||
/// simply copies the last byte 518 times. A distance of four and a length of
|
||||
/// twelve copies the last four bytes three times. A simple forward copy
|
||||
/// ignoring whether the length is greater than the distance or not implements
|
||||
/// this correctly.
|
||||
/// </remarks>
|
||||
private int Decompress(State state)
|
||||
{
|
||||
int symbol; // decoded symbol, extra bits for distance
|
||||
int len; // length for copy
|
||||
uint dist; // distance for copy
|
||||
int copy; // copy counter
|
||||
int from, to; // copy pointers
|
||||
|
||||
// Read header
|
||||
int lit = state.ReadBits(8); // true if literals are coded
|
||||
if (lit > 1)
|
||||
return -1;
|
||||
|
||||
int dict = state.ReadBits(8); // log2(dictionary size) - 6
|
||||
if (dict < 4 || dict > 6)
|
||||
return -2;
|
||||
|
||||
// Decode literals and length/distance pairs
|
||||
while (true)
|
||||
{
|
||||
if (state.ReadBits(1) != 0)
|
||||
{
|
||||
// Get length
|
||||
symbol = lencode.Decode(state);
|
||||
len = baseLength[symbol] + state.ReadBits(extra[symbol]);
|
||||
if (len == 519)
|
||||
break; // end code
|
||||
|
||||
// Get distance
|
||||
symbol = len == 2 ? 2 : dict;
|
||||
dist = (uint)(distcode.Decode(state) << symbol);
|
||||
dist += (uint)state.ReadBits(symbol);
|
||||
dist++;
|
||||
if (state.First && dist > state.Next)
|
||||
return -3; //distance too far back
|
||||
|
||||
// Copy length bytes from distance bytes back
|
||||
do
|
||||
{
|
||||
to = (int)state.Next;
|
||||
from = (int)(to - dist);
|
||||
copy = MAXWIN;
|
||||
if (state.Next < dist)
|
||||
{
|
||||
from += copy;
|
||||
copy = (int)dist;
|
||||
}
|
||||
|
||||
copy -= (int)state.Next;
|
||||
if (copy > len)
|
||||
copy = len;
|
||||
|
||||
len -= copy;
|
||||
state.Next += (uint)copy;
|
||||
state.CopyOutputBytes(to, from, copy);
|
||||
|
||||
if (state.Next == MAXWIN)
|
||||
{
|
||||
if (!state.ProcessOutput())
|
||||
return 1;
|
||||
|
||||
state.Next = 0;
|
||||
state.First = false;
|
||||
}
|
||||
}
|
||||
while (len != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get literal and write it
|
||||
symbol = lit != 0 ? litcode.Decode(state) : state.ReadBits(8);
|
||||
state.WriteToOutput((byte)symbol);
|
||||
if (state.Next == MAXWIN)
|
||||
{
|
||||
if (!state.ProcessOutput())
|
||||
return 1;
|
||||
|
||||
state.Next = 0;
|
||||
state.First = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
198
SabreTools.Compression/Blast/Huffman.cs
Normal file
198
SabreTools.Compression/Blast/Huffman.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using static SabreTools.Compression.Blast.Constants;
|
||||
|
||||
namespace SabreTools.Compression.Blast
|
||||
{
|
||||
/// <summary>
|
||||
/// Huffman code decoding tables. count[1..MAXBITS] is the number of symbols of
|
||||
/// each length, which for a canonical code are stepped through in order.
|
||||
/// symbol[] are the symbol values in canonical order, where the number of
|
||||
/// entries is the sum of the counts in count[]. The decoding process can be
|
||||
/// seen in the function decode() below.
|
||||
/// </summary>
|
||||
public class Huffman
|
||||
{
|
||||
/// <summary>
|
||||
/// Number of symbols of each length
|
||||
/// </summary>
|
||||
public short[] Count { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Pointer to number of symbols of each length
|
||||
/// </summary>
|
||||
public int CountPtr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Canonically ordered symbols
|
||||
/// </summary>
|
||||
public short[] Symbol { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="countLength">Length of the Count array</param>
|
||||
/// <param name="symbolLength">Length of the Symbol array</param>
|
||||
public Huffman(int countLength, int symbolLength)
|
||||
{
|
||||
Count = new short[countLength];
|
||||
Symbol = new short[symbolLength];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Given a list of repeated code lengths rep[0..n-1], where each byte is a
|
||||
/// count (high four bits + 1) and a code length (low four bits), generate the
|
||||
/// list of code lengths. This compaction reduces the size of the object code.
|
||||
/// Then given the list of code lengths length[0..n-1] representing a canonical
|
||||
/// Huffman code for n symbols, construct the tables required to decode those
|
||||
/// codes. Those tables are the number of codes of each length, and the symbols
|
||||
/// sorted by length, retaining their original order within each length. The
|
||||
/// return value is zero for a complete code set, negative for an over-
|
||||
/// subscribed code set, and positive for an incomplete code set. The tables
|
||||
/// can be used if the return value is zero or positive, but they cannot be used
|
||||
/// if the return value is negative. If the return value is zero, it is not
|
||||
/// possible for decode() using that table to return an error--any stream of
|
||||
/// enough bits will resolve to a symbol. If the return value is positive, then
|
||||
/// it is possible for decode() using that table to return an error for received
|
||||
/// codes past the end of the incomplete lengths.
|
||||
/// </summary>
|
||||
/// <param name="rep">Repeated code length array</param>
|
||||
public int Initialize(byte[] rep)
|
||||
{
|
||||
int n = rep.Length; // Length of the bit length array
|
||||
short symbol = 0; // Current symbol when stepping through length[]
|
||||
short len; // Current length when stepping through h.Count[]
|
||||
int left; // Number of possible codes left of current length
|
||||
short[] offs = new short[MAXBITS + 1]; // offsets in symbol table for each length
|
||||
short[] length = new short[256]; // Code lengths
|
||||
|
||||
// Convert compact repeat counts into symbol bit length list
|
||||
int repPtr = 0;
|
||||
do
|
||||
{
|
||||
len = rep[repPtr++];
|
||||
left = (len >> 4) + 1;
|
||||
len &= 15;
|
||||
do
|
||||
{
|
||||
length[symbol++] = len;
|
||||
}
|
||||
while (--left != 0);
|
||||
}
|
||||
while (--n != 0);
|
||||
|
||||
n = symbol;
|
||||
|
||||
// Count number of codes of each length
|
||||
for (len = 0; len <= MAXBITS; len++)
|
||||
{
|
||||
Count[len] = 0;
|
||||
}
|
||||
|
||||
// Assumes lengths are within bounds
|
||||
for (symbol = 0; symbol < n; symbol++)
|
||||
{
|
||||
(Count[length[symbol]])++;
|
||||
}
|
||||
|
||||
// No codes! Complete, but decode() will fail
|
||||
if (Count[0] == n)
|
||||
return 0;
|
||||
|
||||
// Check for an over-subscribed or incomplete set of lengths
|
||||
left = 1; // One possible code of zero length
|
||||
for (len = 1; len <= MAXBITS; len++)
|
||||
{
|
||||
left <<= 1; // One more bit, double codes left
|
||||
left -= Count[len]; // Deduct count from possible codes
|
||||
if (left < 0)
|
||||
return left; // over-subscribed--return negative
|
||||
}
|
||||
|
||||
// Generate offsets into symbol table for each length for sorting
|
||||
offs[1] = 0;
|
||||
for (len = 1; len < MAXBITS; len++)
|
||||
{
|
||||
offs[len + 1] = (short)(offs[len] + Count[len]);
|
||||
}
|
||||
|
||||
// Put symbols in table sorted by length, by symbol order within each length
|
||||
for (symbol = 0; symbol < n; symbol++)
|
||||
{
|
||||
if (length[symbol] != 0)
|
||||
Symbol[offs[length[symbol]]++] = symbol;
|
||||
}
|
||||
|
||||
// Return zero for complete set, positive for incomplete set
|
||||
return left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decode a code from the stream s using huffman table h. Return the symbol or
|
||||
/// a negative value if there is an error. If all of the lengths are zero, i.e.
|
||||
/// an empty code, or if the code is incomplete and an invalid code is received,
|
||||
/// then -9 is returned after reading MAXBITS bits.
|
||||
/// </summary>
|
||||
/// <param name="state">Current input/output state to process</param>
|
||||
/// <remarks>
|
||||
/// The codes as stored in the compressed data are bit-reversed relative to
|
||||
/// a simple integer ordering of codes of the same lengths. Hence below the
|
||||
/// bits are pulled from the compressed data one at a time and used to
|
||||
/// build the code value reversed from what is in the stream in order to
|
||||
/// permit simple integer comparisons for decoding.
|
||||
///
|
||||
/// The first code for the shortest length is all ones. Subsequent codes of
|
||||
/// the same length are simply integer decrements of the previous code. When
|
||||
/// moving up a length, a one bit is appended to the code. For a complete
|
||||
/// code, the last code of the longest length will be all zeros. To support
|
||||
/// this ordering, the bits pulled during decoding are inverted to apply the
|
||||
/// more "natural" ordering starting with all zeros and incrementing.
|
||||
/// </remarks>
|
||||
public int Decode(State state)
|
||||
{
|
||||
int len = 1; // Current number of bits in code
|
||||
int code = 0; // len bits being decoded
|
||||
int first = 0; // First code of length len
|
||||
int count; // Number of codes of length len
|
||||
int index = 0; // Index of first code of length len in symbol table
|
||||
int bitbuf = state.BitBuf; // Bits from stream
|
||||
int left = state.BitCnt; // Bits left in next or left to process
|
||||
int nextPtr = CountPtr + 1; // Next number of codes
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (left-- != 0)
|
||||
{
|
||||
// Invert code
|
||||
code |= (bitbuf & 1) ^ 1;
|
||||
bitbuf >>= 1;
|
||||
count = Count[nextPtr++];
|
||||
|
||||
// If length len, return symbol
|
||||
if (code < first + count)
|
||||
{
|
||||
state.BitBuf = bitbuf;
|
||||
state.BitCnt = (state.BitCnt - len) & 7;
|
||||
return Symbol[index + (code - first)];
|
||||
}
|
||||
|
||||
// Else update for next length
|
||||
index += count;
|
||||
first += count;
|
||||
first <<= 1;
|
||||
code <<= 1;
|
||||
len++;
|
||||
}
|
||||
|
||||
left = (MAXBITS + 1) - len;
|
||||
if (left == 0)
|
||||
break;
|
||||
|
||||
bitbuf = state.ReadNextByte();
|
||||
if (left > 8)
|
||||
left = 8;
|
||||
}
|
||||
|
||||
// Ran out of codes
|
||||
return -9;
|
||||
}
|
||||
};
|
||||
}
|
||||
184
SabreTools.Compression/Blast/State.cs
Normal file
184
SabreTools.Compression/Blast/State.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using static SabreTools.Compression.Blast.Constants;
|
||||
|
||||
namespace SabreTools.Compression.Blast
|
||||
{
|
||||
/// <summary>
|
||||
/// Input and output state
|
||||
/// </summary>
|
||||
public class State
|
||||
{
|
||||
#region Input State
|
||||
|
||||
/// <summary>
|
||||
/// Opaque information passed to InputFunction()
|
||||
/// </summary>
|
||||
private readonly Stream _source;
|
||||
|
||||
/// <summary>
|
||||
/// Next input location
|
||||
/// </summary>
|
||||
private readonly byte[] _input = new byte[MAXWIN];
|
||||
|
||||
/// <summary>
|
||||
/// Pointer to the next input location
|
||||
/// </summary>
|
||||
private int _inputPtr;
|
||||
|
||||
/// <summary>
|
||||
/// Available input at in
|
||||
/// </summary>
|
||||
private uint _available;
|
||||
|
||||
/// <summary>
|
||||
/// Bit buffer
|
||||
/// </summary>
|
||||
public int BitBuf { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of bits in bit buffer
|
||||
/// </summary>
|
||||
public int BitCnt { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Output State
|
||||
|
||||
/// <summary>
|
||||
/// Opaque information passed to OutputFunction()
|
||||
/// </summary>
|
||||
private readonly Stream _dest;
|
||||
|
||||
/// <summary>
|
||||
/// Index of next write location in out[]
|
||||
/// </summary>
|
||||
public uint Next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True to check distances (for first 4K)
|
||||
/// </summary>
|
||||
public bool First { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Output buffer and sliding window
|
||||
/// </summary>
|
||||
private readonly byte[] _output = new byte[MAXWIN];
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public State(Stream source, Stream dest)
|
||||
{
|
||||
_source = source;
|
||||
_inputPtr = 0;
|
||||
_available = 0;
|
||||
BitBuf = 0;
|
||||
BitCnt = 0;
|
||||
|
||||
_dest = dest;
|
||||
Next = 0;
|
||||
First = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy bytes in the output buffer between locations
|
||||
/// </summary>
|
||||
public void CopyOutputBytes(int to, int from, int len)
|
||||
{
|
||||
do
|
||||
{
|
||||
_output[to++] = _output[from++];
|
||||
}
|
||||
while (--len > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return need bits from the input stream. This always leaves less than
|
||||
/// eight bits in the buffer. bits() works properly for need == 0.
|
||||
/// </summary>
|
||||
/// <param name="need">Number of bits to read</param>
|
||||
/// <remarks>
|
||||
/// Bits are stored in bytes from the least significant bit to the most
|
||||
/// significant bit. Therefore bits are dropped from the bottom of the bit
|
||||
/// buffer, using shift right, and new bytes are appended to the top of the
|
||||
/// bit buffer, using shift left.
|
||||
/// </remarks>
|
||||
public int ReadBits(int need)
|
||||
{
|
||||
// Load at least need bits into val
|
||||
int val = BitBuf;
|
||||
while (BitCnt < need)
|
||||
{
|
||||
// Load eight bits
|
||||
EnsureAvailable();
|
||||
val |= _input[_inputPtr++] << BitCnt;
|
||||
_available--;
|
||||
BitCnt += 8;
|
||||
}
|
||||
|
||||
// Drop need bits and update buffer, always zero to seven bits left
|
||||
BitBuf = val >> need;
|
||||
BitCnt -= need;
|
||||
|
||||
// Return need bits, zeroing the bits above that
|
||||
return val & ((1 << need) - 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process output for the current state
|
||||
/// </summary>
|
||||
/// <returns>True if the output could be added, false otherwise</returns>
|
||||
public bool ProcessOutput()
|
||||
{
|
||||
try
|
||||
{
|
||||
_dest.Write(_output, 0, (int)Next);
|
||||
_dest.Flush();
|
||||
|
||||
Next = 0;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the next byte from the input buffer
|
||||
/// </summary>
|
||||
public byte ReadNextByte()
|
||||
{
|
||||
EnsureAvailable();
|
||||
return _input[_inputPtr++];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a byte value to the output buffer
|
||||
/// </summary>
|
||||
public void WriteToOutput(byte value)
|
||||
=> _output[Next++] = value;
|
||||
|
||||
/// <summary>
|
||||
/// Ensure there are bytes available, if possible
|
||||
/// </summary>
|
||||
/// <exception cref="IndexOutOfRangeException"></exception>
|
||||
private void EnsureAvailable()
|
||||
{
|
||||
// If there are bytes
|
||||
if (_inputPtr < _available)
|
||||
return;
|
||||
|
||||
// Read the next block
|
||||
_available = (uint)_source.Read(_input, 0, MAXWIN);
|
||||
if (_available == 0)
|
||||
throw new IndexOutOfRangeException();
|
||||
|
||||
// Reset the pointer
|
||||
_inputPtr = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
180
SabreTools.Compression/Deflate/Adler.cs
Normal file
180
SabreTools.Compression/Deflate/Adler.cs
Normal file
@@ -0,0 +1,180 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// Computes an Adler-32 checksum.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
|
||||
/// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
|
||||
/// is a required part of the "ZLIB" standard. Applications will almost never need to
|
||||
/// use this class directly.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <exclude/>
|
||||
public sealed class Adler
|
||||
{
|
||||
// largest prime smaller than 65536
|
||||
private static readonly uint BASE = 65521;
|
||||
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
|
||||
private static readonly int NMAX = 5552;
|
||||
|
||||
|
||||
#pragma warning disable 3001
|
||||
#pragma warning disable 3002
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the Adler32 checksum.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This is used within ZLIB. You probably don't need to use this directly.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// To compute an Adler32 checksum on a byte array:
|
||||
/// <code>
|
||||
/// var adler = Adler.Adler32(0, null, 0, 0);
|
||||
/// adler = Adler.Adler32(adler, buffer, index, length);
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static uint Adler32(uint adler, byte[] buf, int index, int len)
|
||||
{
|
||||
if (buf == null)
|
||||
return 1;
|
||||
|
||||
uint s1 = (uint)(adler & 0xffff);
|
||||
uint s2 = (uint)((adler >> 16) & 0xffff);
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
int k = len < NMAX ? len : NMAX;
|
||||
len -= k;
|
||||
while (k >= 16)
|
||||
{
|
||||
//s1 += (buf[index++] & 0xff); s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
s1 += buf[index++]; s2 += s1;
|
||||
k -= 16;
|
||||
}
|
||||
if (k != 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
s1 += buf[index++];
|
||||
s2 += s1;
|
||||
}
|
||||
while (--k != 0);
|
||||
}
|
||||
s1 %= BASE;
|
||||
s2 %= BASE;
|
||||
}
|
||||
return (uint)((s2 << 16) | s1);
|
||||
}
|
||||
#pragma warning restore 3001
|
||||
#pragma warning restore 3002
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
78
SabreTools.Compression/Deflate/BlockState.cs
Normal file
78
SabreTools.Compression/Deflate/BlockState.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
// Deflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-August-03 19:52:15>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines logic for handling the Deflate or compression.
|
||||
//
|
||||
// This code is based on multiple sources:
|
||||
// - the original zlib v1.2.3 source, which is Copyright (C) 1995-2005 Jean-loup Gailly.
|
||||
// - the original jzlib, which is Copyright (c) 2000-2003 ymnk, JCraft,Inc.
|
||||
//
|
||||
// However, this code is significantly different from both.
|
||||
// The object model is not the same, and many of the behaviors are different.
|
||||
//
|
||||
// In keeping with the license for these other works, the copyrights for
|
||||
// jzlib and zlib are here.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal enum BlockState
|
||||
{
|
||||
NeedMore = 0, // block not completed, need more input or more output
|
||||
BlockDone, // block flush performed
|
||||
FinishStarted, // finish started, need only more output at next deflate
|
||||
FinishDone // finish done, accept no more input or output
|
||||
}
|
||||
}
|
||||
815
SabreTools.Compression/Deflate/CRC32.cs
Normal file
815
SabreTools.Compression/Deflate/CRC32.cs
Normal file
@@ -0,0 +1,815 @@
|
||||
// CRC32.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2011 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-02 18:25:54>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the CRC32 class, which can do the CRC32 algorithm, using
|
||||
// arbitrary starting polynomials, and bit reversal. The bit reversal is what
|
||||
// distinguishes this CRC-32 used in BZip2 from the CRC-32 that is used in PKZIP
|
||||
// files, or GZIP files. This class does both.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using Interop = System.Runtime.InteropServices;
|
||||
|
||||
#nullable disable
|
||||
#pragma warning disable CS0618
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
|
||||
/// can set the polynomial and enable or disable bit
|
||||
/// reversal. This can be used for GZIP, BZip2, or ZIP.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This type is used internally by DotNetZip; it is generally not used
|
||||
/// directly by applications wishing to create, read, or manipulate zip
|
||||
/// archive files.
|
||||
/// </remarks>
|
||||
|
||||
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000C")]
|
||||
[Interop.ComVisible(true)]
|
||||
#if !NETCF
|
||||
[Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
|
||||
#endif
|
||||
public class CRC32
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates the total number of bytes applied to the CRC.
|
||||
/// </summary>
|
||||
public Int64 TotalBytesRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return _TotalBytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates the current CRC for all blocks slurped in.
|
||||
/// </summary>
|
||||
public Int32 Crc32Result
|
||||
{
|
||||
get
|
||||
{
|
||||
return unchecked((Int32)(~_register));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the CRC32 for the specified stream.
|
||||
/// </summary>
|
||||
/// <param name="input">The stream over which to calculate the CRC32</param>
|
||||
/// <returns>the CRC32 calculation</returns>
|
||||
public Int32 GetCrc32(System.IO.Stream input)
|
||||
{
|
||||
return GetCrc32AndCopy(input, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the CRC32 for the specified stream, and writes the input into the
|
||||
/// output stream.
|
||||
/// </summary>
|
||||
/// <param name="input">The stream over which to calculate the CRC32</param>
|
||||
/// <param name="output">The stream into which to deflate the input</param>
|
||||
/// <returns>the CRC32 calculation</returns>
|
||||
public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
|
||||
{
|
||||
if (input == null)
|
||||
throw new Exception("The input stream must not be null.");
|
||||
|
||||
unchecked
|
||||
{
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int readSize = BUFFER_SIZE;
|
||||
|
||||
_TotalBytesRead = 0;
|
||||
int count = input.Read(buffer, 0, readSize);
|
||||
if (output != null) output.Write(buffer, 0, count);
|
||||
_TotalBytesRead += count;
|
||||
while (count > 0)
|
||||
{
|
||||
SlurpBlock(buffer, 0, count);
|
||||
count = input.Read(buffer, 0, readSize);
|
||||
if (output != null) output.Write(buffer, 0, count);
|
||||
_TotalBytesRead += count;
|
||||
}
|
||||
|
||||
return (Int32)(~_register);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get the CRC32 for the given (word,byte) combo. This is a
|
||||
/// computation defined by PKzip for PKZIP 2.0 (weak) encryption.
|
||||
/// </summary>
|
||||
/// <param name="W">The word to start with.</param>
|
||||
/// <param name="B">The byte to combine it with.</param>
|
||||
/// <returns>The CRC-ized result.</returns>
|
||||
public Int32 ComputeCrc32(Int32 W, byte B)
|
||||
{
|
||||
return _InternalComputeCrc32((UInt32)W, B);
|
||||
}
|
||||
|
||||
internal Int32 _InternalComputeCrc32(UInt32 W, byte B)
|
||||
{
|
||||
return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Update the value for the running CRC32 using the given block of bytes.
|
||||
/// This is useful when using the CRC32() class in a Stream.
|
||||
/// </summary>
|
||||
/// <param name="block">block of bytes to slurp</param>
|
||||
/// <param name="offset">starting point in the block</param>
|
||||
/// <param name="count">how many bytes within the block to slurp</param>
|
||||
public void SlurpBlock(byte[] block, int offset, int count)
|
||||
{
|
||||
if (block == null)
|
||||
throw new Exception("The data buffer must not be null.");
|
||||
|
||||
// bzip algorithm
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int x = offset + i;
|
||||
byte b = block[x];
|
||||
if (this.reverseBits)
|
||||
{
|
||||
UInt32 temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[temp];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[temp];
|
||||
}
|
||||
}
|
||||
_TotalBytesRead += count;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Process one byte in the CRC.
|
||||
/// </summary>
|
||||
/// <param name = "b">the byte to include into the CRC . </param>
|
||||
public void UpdateCRC(byte b)
|
||||
{
|
||||
if (this.reverseBits)
|
||||
{
|
||||
UInt32 temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[temp];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[temp];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process a run of N identical bytes into the CRC.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method serves as an optimization for updating the CRC when a
|
||||
/// run of identical bytes is found. Rather than passing in a buffer of
|
||||
/// length n, containing all identical bytes b, this method accepts the
|
||||
/// byte value and the length of the (virtual) buffer - the length of
|
||||
/// the run.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name = "b">the byte to include into the CRC. </param>
|
||||
/// <param name = "n">the number of times that byte should be repeated. </param>
|
||||
public void UpdateCRC(byte b, int n)
|
||||
{
|
||||
while (n-- > 0)
|
||||
{
|
||||
if (this.reverseBits)
|
||||
{
|
||||
uint temp = (_register >> 24) ^ b;
|
||||
_register = (_register << 8) ^ crc32Table[(temp >= 0)
|
||||
? temp
|
||||
: (temp + 256)];
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 temp = (_register & 0x000000FF) ^ b;
|
||||
_register = (_register >> 8) ^ crc32Table[(temp >= 0)
|
||||
? temp
|
||||
: (temp + 256)];
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static uint ReverseBits(uint data)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint ret = data;
|
||||
ret = (ret & 0x55555555) << 1 | (ret >> 1) & 0x55555555;
|
||||
ret = (ret & 0x33333333) << 2 | (ret >> 2) & 0x33333333;
|
||||
ret = (ret & 0x0F0F0F0F) << 4 | (ret >> 4) & 0x0F0F0F0F;
|
||||
ret = (ret << 24) | ((ret & 0xFF00) << 8) | ((ret >> 8) & 0xFF00) | (ret >> 24);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte ReverseBits(byte data)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint u = (uint)data * 0x00020202;
|
||||
uint m = 0x01044010;
|
||||
uint s = u & m;
|
||||
uint t = (u << 2) & (m << 1);
|
||||
return (byte)((0x01001001 * (s + t)) >> 24);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void GenerateLookupTable()
|
||||
{
|
||||
crc32Table = new UInt32[256];
|
||||
unchecked
|
||||
{
|
||||
UInt32 dwCrc;
|
||||
byte i = 0;
|
||||
do
|
||||
{
|
||||
dwCrc = i;
|
||||
for (byte j = 8; j > 0; j--)
|
||||
{
|
||||
if ((dwCrc & 1) == 1)
|
||||
{
|
||||
dwCrc = (dwCrc >> 1) ^ dwPolynomial;
|
||||
}
|
||||
else
|
||||
{
|
||||
dwCrc >>= 1;
|
||||
}
|
||||
}
|
||||
if (reverseBits)
|
||||
{
|
||||
crc32Table[ReverseBits(i)] = ReverseBits(dwCrc);
|
||||
}
|
||||
else
|
||||
{
|
||||
crc32Table[i] = dwCrc;
|
||||
}
|
||||
i++;
|
||||
} while (i!=0);
|
||||
}
|
||||
|
||||
#if VERBOSE
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("private static readonly UInt32[] crc32Table = {");
|
||||
for (int i = 0; i < crc32Table.Length; i+=4)
|
||||
{
|
||||
Console.Write(" ");
|
||||
for (int j=0; j < 4; j++)
|
||||
{
|
||||
Console.Write(" 0x{0:X8}U,", crc32Table[i+j]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.WriteLine("};");
|
||||
Console.WriteLine();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
private uint gf2_matrix_times(uint[] matrix, uint vec)
|
||||
{
|
||||
uint sum = 0;
|
||||
int i=0;
|
||||
while (vec != 0)
|
||||
{
|
||||
if ((vec & 0x01)== 0x01)
|
||||
sum ^= matrix[i];
|
||||
vec >>= 1;
|
||||
i++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private void gf2_matrix_square(uint[] square, uint[] mat)
|
||||
{
|
||||
for (int i = 0; i < 32; i++)
|
||||
square[i] = gf2_matrix_times(mat, mat[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Combines the given CRC32 value with the current running total.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is useful when using a divide-and-conquer approach to
|
||||
/// calculating a CRC. Multiple threads can each calculate a
|
||||
/// CRC32 on a segment of the data, and then combine the
|
||||
/// individual CRC32 values at the end.
|
||||
/// </remarks>
|
||||
/// <param name="crc">the crc value to be combined with this one</param>
|
||||
/// <param name="length">the length of data the CRC value was calculated on</param>
|
||||
public void Combine(int crc, int length)
|
||||
{
|
||||
uint[] even = new uint[32]; // even-power-of-two zeros operator
|
||||
uint[] odd = new uint[32]; // odd-power-of-two zeros operator
|
||||
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
uint crc1= ~_register;
|
||||
uint crc2= (uint) crc;
|
||||
|
||||
// put operator for one zero bit in odd
|
||||
odd[0] = this.dwPolynomial; // the CRC-32 polynomial
|
||||
uint row = 1;
|
||||
for (int i = 1; i < 32; i++)
|
||||
{
|
||||
odd[i] = row;
|
||||
row <<= 1;
|
||||
}
|
||||
|
||||
// put operator for two zero bits in even
|
||||
gf2_matrix_square(even, odd);
|
||||
|
||||
// put operator for four zero bits in odd
|
||||
gf2_matrix_square(odd, even);
|
||||
|
||||
uint len2 = (uint) length;
|
||||
|
||||
// apply len2 zeros to crc1 (first square will put the operator for one
|
||||
// zero byte, eight zero bits, in even)
|
||||
do {
|
||||
// apply zeros operator for this bit of len2
|
||||
gf2_matrix_square(even, odd);
|
||||
|
||||
if ((len2 & 1)== 1)
|
||||
crc1 = gf2_matrix_times(even, crc1);
|
||||
len2 >>= 1;
|
||||
|
||||
if (len2 == 0)
|
||||
break;
|
||||
|
||||
// another iteration of the loop with odd and even swapped
|
||||
gf2_matrix_square(odd, even);
|
||||
if ((len2 & 1)==1)
|
||||
crc1 = gf2_matrix_times(odd, crc1);
|
||||
len2 >>= 1;
|
||||
|
||||
|
||||
} while (len2 != 0);
|
||||
|
||||
crc1 ^= crc2;
|
||||
|
||||
_register= ~crc1;
|
||||
|
||||
//return (int) crc1;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class using the default settings: no
|
||||
/// bit reversal, and a polynomial of 0xEDB88320.
|
||||
/// </summary>
|
||||
public CRC32() : this(false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class, specifying whether to reverse
|
||||
/// data bits or not.
|
||||
/// </summary>
|
||||
/// <param name='reverseBits'>
|
||||
/// specify true if the instance should reverse data bits.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
||||
/// want a CRC32 with compatibility with BZip2, you should pass true
|
||||
/// here. In the CRC-32 used by GZIP and PKZIP, the bits are not
|
||||
/// reversed; Therefore if you want a CRC32 with compatibility with
|
||||
/// those, you should pass false.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public CRC32(bool reverseBits) :
|
||||
this( unchecked((int)0xEDB88320), reverseBits)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create an instance of the CRC32 class, specifying the polynomial and
|
||||
/// whether to reverse data bits or not.
|
||||
/// </summary>
|
||||
/// <param name='polynomial'>
|
||||
/// The polynomial to use for the CRC, expressed in the reversed (LSB)
|
||||
/// format: the highest ordered bit in the polynomial value is the
|
||||
/// coefficient of the 0th power; the second-highest order bit is the
|
||||
/// coefficient of the 1 power, and so on. Expressed this way, the
|
||||
/// polynomial for the CRC-32C used in IEEE 802.3, is 0xEDB88320.
|
||||
/// </param>
|
||||
/// <param name='reverseBits'>
|
||||
/// specify true if the instance should reverse data bits.
|
||||
/// </param>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
||||
/// want a CRC32 with compatibility with BZip2, you should pass true
|
||||
/// here for the <c>reverseBits</c> parameter. In the CRC-32 used by
|
||||
/// GZIP and PKZIP, the bits are not reversed; Therefore if you want a
|
||||
/// CRC32 with compatibility with those, you should pass false for the
|
||||
/// <c>reverseBits</c> parameter.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public CRC32(int polynomial, bool reverseBits)
|
||||
{
|
||||
this.reverseBits = reverseBits;
|
||||
this.dwPolynomial = (uint) polynomial;
|
||||
this.GenerateLookupTable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the CRC-32 class - clear the CRC "remainder register."
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Use this when employing a single instance of this class to compute
|
||||
/// multiple, distinct CRCs on multiple, distinct data blocks.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public void Reset()
|
||||
{
|
||||
_register = 0xFFFFFFFFU;
|
||||
}
|
||||
|
||||
// private member vars
|
||||
private UInt32 dwPolynomial;
|
||||
private Int64 _TotalBytesRead;
|
||||
private bool reverseBits;
|
||||
private UInt32[] crc32Table;
|
||||
private const int BUFFER_SIZE = 8192;
|
||||
private UInt32 _register = 0xFFFFFFFFU;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A Stream that calculates a CRC32 (a checksum) on all bytes read,
|
||||
/// or on all bytes written.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This class can be used to verify the CRC of a ZipEntry when
|
||||
/// reading from a stream, or to calculate a CRC when writing to a
|
||||
/// stream. The stream should be used to either read, or write, but
|
||||
/// not both. If you intermix reads and writes, the results are not
|
||||
/// defined.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This class is intended primarily for use internally by the
|
||||
/// DotNetZip library.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class CrcCalculatorStream : System.IO.Stream, System.IDisposable
|
||||
{
|
||||
private static readonly Int64 UnsetLengthLimit = -99;
|
||||
|
||||
internal System.IO.Stream _innerStream;
|
||||
private CRC32 _Crc32;
|
||||
private Int64 _lengthLimit = -99;
|
||||
private bool _leaveOpen;
|
||||
|
||||
/// <summary>
|
||||
/// The default constructor.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Instances returned from this constructor will leave the underlying
|
||||
/// stream open upon Close(). The stream uses the default CRC32
|
||||
/// algorithm, which implies a polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream)
|
||||
: this(true, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor allows the caller to specify how to handle the
|
||||
/// underlying stream at close.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, bool leaveOpen)
|
||||
: this(leaveOpen, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Instances returned from this constructor will leave the underlying
|
||||
/// stream open upon Close().
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length)
|
||||
: this(true, length, stream, null)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read, as well as whether to keep the underlying stream open upon
|
||||
/// Close().
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the default CRC32 algorithm, which implies a
|
||||
/// polynomial of 0xEDB88320.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen)
|
||||
: this(leaveOpen, length, stream, null)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A constructor allowing the specification of the length of the stream
|
||||
/// to read, as well as whether to keep the underlying stream open upon
|
||||
/// Close(), and the CRC32 instance to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The stream uses the specified CRC32 instance, which allows the
|
||||
/// application to specify how the CRC gets calculated.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">The underlying stream</param>
|
||||
/// <param name="length">The length of the stream to slurp</param>
|
||||
/// <param name="leaveOpen">true to leave the underlying stream
|
||||
/// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
||||
/// <param name="crc32">the CRC32 instance to use to calculate the CRC32</param>
|
||||
public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen,
|
||||
CRC32 crc32)
|
||||
: this(leaveOpen, length, stream, crc32)
|
||||
{
|
||||
if (length < 0)
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
|
||||
|
||||
// This ctor is private - no validation is done here. This is to allow the use
|
||||
// of a (specific) negative value for the _lengthLimit, to indicate that there
|
||||
// is no length set. So we validate the length limit in those ctors that use an
|
||||
// explicit param, otherwise we don't validate, because it could be our special
|
||||
// value.
|
||||
private CrcCalculatorStream
|
||||
(bool leaveOpen, Int64 length, System.IO.Stream stream, CRC32 crc32)
|
||||
: base()
|
||||
{
|
||||
_innerStream = stream;
|
||||
_Crc32 = crc32 ?? new CRC32();
|
||||
_lengthLimit = length;
|
||||
_leaveOpen = leaveOpen;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total number of bytes run through the CRC32 calculator.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// This is either the total number of bytes read, or the total number of
|
||||
/// bytes written, depending on the direction of this stream.
|
||||
/// </remarks>
|
||||
public Int64 TotalBytesSlurped
|
||||
{
|
||||
get { return _Crc32.TotalBytesRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides the current CRC for all blocks slurped in.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The running total of the CRC is kept as data is written or read
|
||||
/// through the stream. read this property after all reads or writes to
|
||||
/// get an accurate CRC for the entire stream.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public Int32 Crc
|
||||
{
|
||||
get { return _Crc32.Crc32Result; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the underlying stream will be left open when the
|
||||
/// <c>CrcCalculatorStream</c> is Closed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Set this at any point before calling <see cref="Close()"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public bool LeaveOpen
|
||||
{
|
||||
get { return _leaveOpen; }
|
||||
set { _leaveOpen = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read from the stream
|
||||
/// </summary>
|
||||
/// <param name="buffer">the buffer to read</param>
|
||||
/// <param name="offset">the offset at which to start</param>
|
||||
/// <param name="count">the number of bytes to read</param>
|
||||
/// <returns>the number of bytes actually read</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int bytesToRead = count;
|
||||
|
||||
// Need to limit the # of bytes returned, if the stream is intended to have
|
||||
// a definite length. This is especially useful when returning a stream for
|
||||
// the uncompressed data directly to the application. The app won't
|
||||
// necessarily read only the UncompressedSize number of bytes. For example
|
||||
// wrapping the stream returned from OpenReader() into a StreadReader() and
|
||||
// calling ReadToEnd() on it, We can "over-read" the zip data and get a
|
||||
// corrupt string. The length limits that, prevents that problem.
|
||||
|
||||
if (_lengthLimit != CrcCalculatorStream.UnsetLengthLimit)
|
||||
{
|
||||
if (_Crc32.TotalBytesRead >= _lengthLimit) return 0; // EOF
|
||||
Int64 bytesRemaining = _lengthLimit - _Crc32.TotalBytesRead;
|
||||
if (bytesRemaining < count) bytesToRead = (int)bytesRemaining;
|
||||
}
|
||||
int n = _innerStream.Read(buffer, offset, bytesToRead);
|
||||
if (n > 0) _Crc32.SlurpBlock(buffer, offset, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write to the stream.
|
||||
/// </summary>
|
||||
/// <param name="buffer">the buffer from which to write</param>
|
||||
/// <param name="offset">the offset at which to start writing</param>
|
||||
/// <param name="count">the number of bytes to write</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (count > 0) _Crc32.SlurpBlock(buffer, offset, count);
|
||||
_innerStream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports reading.
|
||||
/// </summary>
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return _innerStream.CanRead; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports seeking.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Always returns false.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports writing.
|
||||
/// </summary>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return _innerStream.CanWrite; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the stream.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
_innerStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the length of the underlying stream.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_lengthLimit == CrcCalculatorStream.UnsetLengthLimit)
|
||||
return _innerStream.Length;
|
||||
else return _lengthLimit;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The getter for this property returns the total bytes read.
|
||||
/// If you use the setter, it will throw
|
||||
/// <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
public override long Position
|
||||
{
|
||||
get { return _Crc32.TotalBytesRead; }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeking is not supported on this stream. This method always throws
|
||||
/// <see cref="NotSupportedException"/>
|
||||
/// </summary>
|
||||
/// <param name="offset">N/A</param>
|
||||
/// <param name="origin">N/A</param>
|
||||
/// <returns>N/A</returns>
|
||||
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method always throws
|
||||
/// <see cref="NotSupportedException"/>
|
||||
/// </summary>
|
||||
/// <param name="value">N/A</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the stream.
|
||||
/// </summary>
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
if (!_leaveOpen)
|
||||
_innerStream.Close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
168
SabreTools.Compression/Deflate/CompressionLevel.cs
Normal file
168
SabreTools.Compression/Deflate/CompressionLevel.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
|
||||
/// </summary>
|
||||
public enum CompressionLevel
|
||||
{
|
||||
/// <summary>
|
||||
/// None means that the data will be simply stored, with no change at all.
|
||||
/// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None
|
||||
/// cannot be opened with the default zip reader. Use a different CompressionLevel.
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// Same as None.
|
||||
/// </summary>
|
||||
Level0 = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The fastest but least effective compression.
|
||||
/// </summary>
|
||||
BestSpeed = 1,
|
||||
|
||||
/// <summary>
|
||||
/// A synonym for BestSpeed.
|
||||
/// </summary>
|
||||
Level1 = 1,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 1.
|
||||
/// </summary>
|
||||
Level2 = 2,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 2.
|
||||
/// </summary>
|
||||
Level3 = 3,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower, but better, than level 3.
|
||||
/// </summary>
|
||||
Level4 = 4,
|
||||
|
||||
/// <summary>
|
||||
/// A little slower than level 4, but with better compression.
|
||||
/// </summary>
|
||||
Level5 = 5,
|
||||
|
||||
/// <summary>
|
||||
/// The default compression level, with a good balance of speed and compression efficiency.
|
||||
/// </summary>
|
||||
Default = 6,
|
||||
/// <summary>
|
||||
/// A synonym for Default.
|
||||
/// </summary>
|
||||
Level6 = 6,
|
||||
|
||||
/// <summary>
|
||||
/// Pretty good compression!
|
||||
/// </summary>
|
||||
Level7 = 7,
|
||||
|
||||
/// <summary>
|
||||
/// Better compression than Level7!
|
||||
/// </summary>
|
||||
Level8 = 8,
|
||||
|
||||
/// <summary>
|
||||
/// The "best" compression, where best means greatest reduction in size of the input data stream.
|
||||
/// This is also the slowest compression.
|
||||
/// </summary>
|
||||
BestCompression = 9,
|
||||
|
||||
/// <summary>
|
||||
/// A synonym for BestCompression.
|
||||
/// </summary>
|
||||
Level9 = 9,
|
||||
}
|
||||
|
||||
}
|
||||
106
SabreTools.Compression/Deflate/CompressionMode.cs
Normal file
106
SabreTools.Compression/Deflate/CompressionMode.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// An enum to specify the direction of transcoding - whether to compress or decompress.
|
||||
/// </summary>
|
||||
public enum CompressionMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to specify that the stream should compress the data.
|
||||
/// </summary>
|
||||
Compress = 0,
|
||||
/// <summary>
|
||||
/// Used to specify that the stream should decompress the data.
|
||||
/// </summary>
|
||||
Decompress = 1,
|
||||
}
|
||||
|
||||
}
|
||||
119
SabreTools.Compression/Deflate/CompressionStrategy.cs
Normal file
119
SabreTools.Compression/Deflate/CompressionStrategy.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes options for how the compression algorithm is executed. Different strategies
|
||||
/// work better on different sorts of data. The strategy parameter can affect the compression
|
||||
/// ratio and the speed of compression but not the correctness of the compresssion.
|
||||
/// </summary>
|
||||
public enum CompressionStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// The default strategy is probably the best for normal data.
|
||||
/// </summary>
|
||||
Default = 0,
|
||||
|
||||
/// <summary>
|
||||
/// The <c>Filtered</c> strategy is intended to be used most effectively with data produced by a
|
||||
/// filter or predictor. By this definition, filtered data consists mostly of small
|
||||
/// values with a somewhat random distribution. In this case, the compression algorithm
|
||||
/// is tuned to compress them better. The effect of <c>Filtered</c> is to force more Huffman
|
||||
/// coding and less string matching; it is a half-step between <c>Default</c> and <c>HuffmanOnly</c>.
|
||||
/// </summary>
|
||||
Filtered = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Using <c>HuffmanOnly</c> will force the compressor to do Huffman encoding only, with no
|
||||
/// string matching.
|
||||
/// </summary>
|
||||
HuffmanOnly = 2,
|
||||
}
|
||||
|
||||
}
|
||||
77
SabreTools.Compression/Deflate/DeflateFlavor.cs
Normal file
77
SabreTools.Compression/Deflate/DeflateFlavor.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
// Deflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-August-03 19:52:15>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines logic for handling the Deflate or compression.
|
||||
//
|
||||
// This code is based on multiple sources:
|
||||
// - the original zlib v1.2.3 source, which is Copyright (C) 1995-2005 Jean-loup Gailly.
|
||||
// - the original jzlib, which is Copyright (c) 2000-2003 ymnk, JCraft,Inc.
|
||||
//
|
||||
// However, this code is significantly different from both.
|
||||
// The object model is not the same, and many of the behaviors are different.
|
||||
//
|
||||
// In keeping with the license for these other works, the copyrights for
|
||||
// jzlib and zlib are here.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal enum DeflateFlavor
|
||||
{
|
||||
Store,
|
||||
Fast,
|
||||
Slow
|
||||
}
|
||||
}
|
||||
1871
SabreTools.Compression/Deflate/DeflateManager.cs
Normal file
1871
SabreTools.Compression/Deflate/DeflateManager.cs
Normal file
File diff suppressed because it is too large
Load Diff
751
SabreTools.Compression/Deflate/DeflateStream.cs
Normal file
751
SabreTools.Compression/Deflate/DeflateStream.cs
Normal file
@@ -0,0 +1,751 @@
|
||||
// DeflateStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2010 Dino Chiesa.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-July-31 14:48:11>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the DeflateStream class, which can be used as a replacement for
|
||||
// the System.IO.Compression.DeflateStream class in the .NET BCL.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// A class for compressing and decompressing streams using the Deflate algorithm.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// The DeflateStream is a <see
|
||||
/// href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</see> on a <see
|
||||
/// cref="System.IO.Stream"/>. It adds DEFLATE compression or decompression to any
|
||||
/// stream.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// Using this stream, applications can compress or decompress data via stream
|
||||
/// <c>Read</c> and <c>Write</c> operations. Either compresssion or decompression
|
||||
/// can occur through either reading or writing. The compression format used is
|
||||
/// DEFLATE, which is documented in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1951.txt">IETF RFC 1951</see>, "DEFLATE
|
||||
/// Compressed Data Format Specification version 1.3.".
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This class is similar to <see cref="ZlibStream"/>, except that
|
||||
/// <c>ZlibStream</c> adds the <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
|
||||
/// 1950 - ZLIB</see> framing bytes to a compressed stream when compressing, or
|
||||
/// expects the RFC1950 framing bytes when decompressing. The <c>DeflateStream</c>
|
||||
/// does not.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <seealso cref="ZlibStream" />
|
||||
/// <seealso cref="GZipStream" />
|
||||
public class DeflateStream : System.IO.Stream
|
||||
{
|
||||
internal ZlibBaseStream _baseStream;
|
||||
internal System.IO.Stream _innerStream;
|
||||
bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Create a DeflateStream using the specified CompressionMode.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// When mode is <c>CompressionMode.Compress</c>, the DeflateStream will use
|
||||
/// the default compression level. The "captive" stream will be closed when
|
||||
/// the DeflateStream is closed.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
/// This example uses a DeflateStream to compress data from a file, and writes
|
||||
/// the compressed data to another file.
|
||||
/// <code>
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
|
||||
/// {
|
||||
/// using (Stream compressor = new DeflateStream(raw, CompressionMode.Compress))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n;
|
||||
/// while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
|
||||
/// {
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
///
|
||||
/// <code lang="VB">
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using raw As FileStream = File.Create(fileToCompress & ".deflated")
|
||||
/// Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="stream">The stream which will be read or written.</param>
|
||||
/// <param name="mode">Indicates whether the DeflateStream will compress or decompress.</param>
|
||||
public DeflateStream(System.IO.Stream stream, CompressionMode mode)
|
||||
: this(stream, mode, CompressionLevel.Default, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Decompress</c>, the level parameter is
|
||||
/// ignored. The "captive" stream will be closed when the DeflateStream is
|
||||
/// closed.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
///
|
||||
/// This example uses a DeflateStream to compress data from a file, and writes
|
||||
/// the compressed data to another file.
|
||||
///
|
||||
/// <code>
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (var raw = System.IO.File.Create(fileToCompress + ".deflated"))
|
||||
/// {
|
||||
/// using (Stream compressor = new DeflateStream(raw,
|
||||
/// CompressionMode.Compress,
|
||||
/// CompressionLevel.BestCompression))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n= -1;
|
||||
/// while (n != 0)
|
||||
/// {
|
||||
/// if (n > 0)
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// n= input.Read(buffer, 0, buffer.Length);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
///
|
||||
/// <code lang="VB">
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using raw As FileStream = File.Create(fileToCompress & ".deflated")
|
||||
/// Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="stream">The stream to be read or written while deflating or inflating.</param>
|
||||
/// <param name="mode">Indicates whether the <c>DeflateStream</c> will compress or decompress.</param>
|
||||
/// <param name="level">A tuning knob to trade speed for effectiveness.</param>
|
||||
public DeflateStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
|
||||
: this(stream, mode, level, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>DeflateStream</c> using the specified
|
||||
/// <c>CompressionMode</c>, and explicitly specify whether the
|
||||
/// stream should be left open after Deflation or Inflation.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// This constructor allows the application to request that the captive stream
|
||||
/// remain open after the deflation or inflation occurs. By default, after
|
||||
/// <c>Close()</c> is called on the stream, the captive stream is also
|
||||
/// closed. In some cases this is not desired, for example if the stream is a
|
||||
/// memory stream that will be re-read after compression. Specify true for
|
||||
/// the <paramref name="leaveOpen"/> parameter to leave the stream open.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// The <c>DeflateStream</c> will use the default compression level.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// See the other overloads of this constructor for example code.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="stream">
|
||||
/// The stream which will be read or written. This is called the
|
||||
/// "captive" stream in other places in this documentation.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="mode">
|
||||
/// Indicates whether the <c>DeflateStream</c> will compress or decompress.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="leaveOpen">true if the application would like the stream to
|
||||
/// remain open after inflation/deflation.</param>
|
||||
public DeflateStream(System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
|
||||
: this(stream, mode, CompressionLevel.Default, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>DeflateStream</c> using the specified <c>CompressionMode</c>
|
||||
/// and the specified <c>CompressionLevel</c>, and explicitly specify whether
|
||||
/// the stream should be left open after Deflation or Inflation.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Decompress</c>, the level parameter is ignored.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This constructor allows the application to request that the captive stream
|
||||
/// remain open after the deflation or inflation occurs. By default, after
|
||||
/// <c>Close()</c> is called on the stream, the captive stream is also
|
||||
/// closed. In some cases this is not desired, for example if the stream is a
|
||||
/// <see cref="System.IO.MemoryStream"/> that will be re-read after
|
||||
/// compression. Specify true for the <paramref name="leaveOpen"/> parameter
|
||||
/// to leave the stream open.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
///
|
||||
/// This example shows how to use a <c>DeflateStream</c> to compress data from
|
||||
/// a file, and store the compressed data into another file.
|
||||
///
|
||||
/// <code>
|
||||
/// using (var output = System.IO.File.Create(fileToCompress + ".deflated"))
|
||||
/// {
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (Stream compressor = new DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n= -1;
|
||||
/// while (n != 0)
|
||||
/// {
|
||||
/// if (n > 0)
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// n= input.Read(buffer, 0, buffer.Length);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// // can write additional data to the output stream here
|
||||
/// }
|
||||
/// </code>
|
||||
///
|
||||
/// <code lang="VB">
|
||||
/// Using output As FileStream = File.Create(fileToCompress & ".deflated")
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using compressor As Stream = New DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// ' can write additional data to the output stream here.
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="stream">The stream which will be read or written.</param>
|
||||
/// <param name="mode">Indicates whether the DeflateStream will compress or decompress.</param>
|
||||
/// <param name="leaveOpen">true if the application would like the stream to remain open after inflation/deflation.</param>
|
||||
/// <param name="level">A tuning knob to trade speed for effectiveness.</param>
|
||||
public DeflateStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
|
||||
{
|
||||
_innerStream = stream;
|
||||
_baseStream = new ZlibBaseStream(stream, mode, level, ZlibStreamFlavor.DEFLATE, leaveOpen);
|
||||
}
|
||||
|
||||
#region Zlib properties
|
||||
|
||||
/// <summary>
|
||||
/// This property sets the flush behavior on the stream.
|
||||
/// </summary>
|
||||
/// <remarks> See the ZLIB documentation for the meaning of the flush behavior.
|
||||
/// </remarks>
|
||||
virtual public FlushType FlushMode
|
||||
{
|
||||
get { return (this._baseStream._flushMode); }
|
||||
set
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
this._baseStream._flushMode = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The size of the working buffer for the compression codec.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The working buffer is used for all stream operations. The default size is
|
||||
/// 1024 bytes. The minimum size is 128 bytes. You may get better performance
|
||||
/// with a larger buffer. Then again, you might not. You would have to test
|
||||
/// it.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// Set this before the first call to <c>Read()</c> or <c>Write()</c> on the
|
||||
/// stream. If you try to set it afterwards, it will throw.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public int BufferSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseStream._bufferSize;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
if (this._baseStream._workingBuffer != null)
|
||||
throw new ZlibException("The working buffer is already set.");
|
||||
if (value < ZlibConstants.WorkingBufferSizeMin)
|
||||
throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
|
||||
this._baseStream._bufferSize = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The ZLIB strategy to be used during compression.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// By tweaking this parameter, you may be able to optimize the compression for
|
||||
/// data with particular characteristics.
|
||||
/// </remarks>
|
||||
public CompressionStrategy Strategy
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseStream.Strategy;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
this._baseStream.Strategy = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Returns the total number of bytes input so far.</summary>
|
||||
virtual public long TotalIn
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseStream._z.TotalBytesIn;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Returns the total number of bytes output so far.</summary>
|
||||
virtual public long TotalOut
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseStream._z.TotalBytesOut;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region System.IO.Stream methods
|
||||
/// <summary>
|
||||
/// Dispose the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This may or may not result in a <c>Close()</c> call on the captive
|
||||
/// stream. See the constructors that have a <c>leaveOpen</c> parameter
|
||||
/// for more information.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Application code won't call this code directly. This method may be
|
||||
/// invoked in two distinct scenarios. If disposing == true, the method
|
||||
/// has been called directly or indirectly by a user's code, for example
|
||||
/// via the public Dispose() method. In this case, both managed and
|
||||
/// unmanaged resources can be referenced and disposed. If disposing ==
|
||||
/// false, the method has been called by the runtime from inside the
|
||||
/// object finalizer and this method should not reference other objects;
|
||||
/// in that case only unmanaged resources must be referenced or
|
||||
/// disposed.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="disposing">
|
||||
/// true if the Dispose method was invoked by user code.
|
||||
/// </param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing && (this._baseStream != null))
|
||||
this._baseStream.Close();
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value depends on whether the captive stream supports reading.
|
||||
/// </remarks>
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
return _baseStream._stream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports Seek operations.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Always returns false.
|
||||
/// </remarks>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be written.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value depends on whether the captive stream supports writing.
|
||||
/// </remarks>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
return _baseStream._stream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the stream.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
_baseStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading this property always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The position of the stream pointer.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Setting this property always throws a <see
|
||||
/// cref="NotImplementedException"/>. Reading will return the total bytes
|
||||
/// written out, if used in writing, or the total bytes read in, if used in
|
||||
/// reading. The count may refer to compressed bytes or uncompressed bytes,
|
||||
/// depending on how you've used the stream.
|
||||
/// </remarks>
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._baseStream._streamMode == SabreTools.Compression.Deflate.ZlibBaseStream.StreamMode.Writer)
|
||||
return this._baseStream._z.TotalBytesOut;
|
||||
if (this._baseStream._streamMode == SabreTools.Compression.Deflate.ZlibBaseStream.StreamMode.Reader)
|
||||
return this._baseStream._z.TotalBytesIn;
|
||||
return 0;
|
||||
}
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// If you wish to use the <c>DeflateStream</c> to compress data while
|
||||
/// reading, you can create a <c>DeflateStream</c> with
|
||||
/// <c>CompressionMode.Compress</c>, providing an uncompressed data stream.
|
||||
/// Then call Read() on that <c>DeflateStream</c>, and the data read will be
|
||||
/// compressed as you read. If you wish to use the <c>DeflateStream</c> to
|
||||
/// decompress data while reading, you can create a <c>DeflateStream</c> with
|
||||
/// <c>CompressionMode.Decompress</c>, providing a readable compressed data
|
||||
/// stream. Then call Read() on that <c>DeflateStream</c>, and the data read
|
||||
/// will be decompressed as you read.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A <c>DeflateStream</c> can be used for <c>Read()</c> or <c>Write()</c>, but not both.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="buffer">The buffer into which the read data should be placed.</param>
|
||||
/// <param name="offset">the offset within that data array to put the first byte read.</param>
|
||||
/// <param name="count">the number of bytes to read.</param>
|
||||
/// <returns>the number of bytes actually read</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
return _baseStream.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="offset">this is irrelevant, since it will always throw!</param>
|
||||
/// <param name="origin">this is irrelevant, since it will always throw!</param>
|
||||
/// <returns>irrelevant!</returns>
|
||||
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotImplementedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">this is irrelevant, since it will always throw!</param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write data to the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// If you wish to use the <c>DeflateStream</c> to compress data while
|
||||
/// writing, you can create a <c>DeflateStream</c> with
|
||||
/// <c>CompressionMode.Compress</c>, and a writable output stream. Then call
|
||||
/// <c>Write()</c> on that <c>DeflateStream</c>, providing uncompressed data
|
||||
/// as input. The data sent to the output stream will be the compressed form
|
||||
/// of the data written. If you wish to use the <c>DeflateStream</c> to
|
||||
/// decompress data while writing, you can create a <c>DeflateStream</c> with
|
||||
/// <c>CompressionMode.Decompress</c>, and a writable output stream. Then
|
||||
/// call <c>Write()</c> on that stream, providing previously compressed
|
||||
/// data. The data sent to the output stream will be the decompressed form of
|
||||
/// the data written.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A <c>DeflateStream</c> can be used for <c>Read()</c> or <c>Write()</c>,
|
||||
/// but not both.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="buffer">The buffer holding data to write to the stream.</param>
|
||||
/// <param name="offset">the offset within that data array to find the first byte to write.</param>
|
||||
/// <param name="count">the number of bytes to write.</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("DeflateStream");
|
||||
_baseStream.Write(buffer, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the dictionary to be used for either Inflation or Deflation.
|
||||
/// </summary>
|
||||
/// <param name="dictionary">The dictionary bytes to use.</param>
|
||||
/// <param name="check">Determines if dictionary checks are run</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDictionary(byte[] dictionary, bool check = true)
|
||||
{
|
||||
return _baseStream.SetDictionary(dictionary, check);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compress a string into a byte array using DEFLATE (RFC 1951).
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Uncompress it with <see cref="DeflateStream.UncompressString(byte[])"/>.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
|
||||
/// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="GZipStream.CompressString(string)">GZipStream.CompressString(string)</seealso>
|
||||
/// <seealso cref="ZlibStream.CompressString(string)">ZlibStream.CompressString(string)</seealso>
|
||||
///
|
||||
/// <param name="s">
|
||||
/// A string to compress. The string will first be encoded
|
||||
/// using UTF8, then compressed.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The string in compressed form</returns>
|
||||
public static byte[] CompressString(String s)
|
||||
{
|
||||
using (var ms = new System.IO.MemoryStream())
|
||||
{
|
||||
System.IO.Stream compressor =
|
||||
new DeflateStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
|
||||
ZlibBaseStream.CompressString(s, compressor);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compress a byte array into a new byte array using DEFLATE.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Uncompress it with <see cref="DeflateStream.UncompressBuffer(byte[])"/>.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <seealso cref="DeflateStream.CompressString(string)">DeflateStream.CompressString(string)</seealso>
|
||||
/// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="GZipStream.CompressBuffer(byte[])">GZipStream.CompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="ZlibStream.CompressBuffer(byte[])">ZlibStream.CompressBuffer(byte[])</seealso>
|
||||
///
|
||||
/// <param name="b">
|
||||
/// A buffer to compress.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The data in compressed form</returns>
|
||||
public static byte[] CompressBuffer(byte[] b)
|
||||
{
|
||||
using (var ms = new System.IO.MemoryStream())
|
||||
{
|
||||
System.IO.Stream compressor =
|
||||
new DeflateStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
|
||||
|
||||
ZlibBaseStream.CompressBuffer(b, compressor);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uncompress a DEFLATE'd byte array into a single string.
|
||||
/// </summary>
|
||||
///
|
||||
/// <seealso cref="DeflateStream.CompressString(String)">DeflateStream.CompressString(String)</seealso>
|
||||
/// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="GZipStream.UncompressString(byte[])">GZipStream.UncompressString(byte[])</seealso>
|
||||
/// <seealso cref="ZlibStream.UncompressString(byte[])">ZlibStream.UncompressString(byte[])</seealso>
|
||||
///
|
||||
/// <param name="compressed">
|
||||
/// A buffer containing DEFLATE-compressed data.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The uncompressed string</returns>
|
||||
public static String UncompressString(byte[] compressed)
|
||||
{
|
||||
using (var input = new System.IO.MemoryStream(compressed))
|
||||
{
|
||||
System.IO.Stream decompressor =
|
||||
new DeflateStream(input, CompressionMode.Decompress);
|
||||
|
||||
return ZlibBaseStream.UncompressString(compressed, decompressor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uncompress a DEFLATE'd byte array into a byte array.
|
||||
/// </summary>
|
||||
///
|
||||
/// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso>
|
||||
/// <seealso cref="GZipStream.UncompressBuffer(byte[])">GZipStream.UncompressBuffer(byte[])</seealso>
|
||||
/// <seealso cref="ZlibStream.UncompressBuffer(byte[])">ZlibStream.UncompressBuffer(byte[])</seealso>
|
||||
///
|
||||
/// <param name="compressed">
|
||||
/// A buffer containing data that has been compressed with DEFLATE.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The data in uncompressed form</returns>
|
||||
public static byte[] UncompressBuffer(byte[] compressed)
|
||||
{
|
||||
using (var input = new System.IO.MemoryStream(compressed))
|
||||
{
|
||||
System.IO.Stream decompressor =
|
||||
new DeflateStream(input, CompressionMode.Decompress);
|
||||
|
||||
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
133
SabreTools.Compression/Deflate/FlushType.cs
Normal file
133
SabreTools.Compression/Deflate/FlushType.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes how to flush the current deflate operation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The different FlushType values are useful when using a Deflate in a streaming application.
|
||||
/// </remarks>
|
||||
public enum FlushType
|
||||
{
|
||||
/// <summary>No flush at all.</summary>
|
||||
None = 0,
|
||||
|
||||
/// <summary>Closes the current block, but doesn't flush it to
|
||||
/// the output. Used internally only in hypothetical
|
||||
/// scenarios. This was supposed to be removed by Zlib, but it is
|
||||
/// still in use in some edge cases.
|
||||
/// </summary>
|
||||
Partial,
|
||||
|
||||
/// <summary>
|
||||
/// Use this during compression to specify that all pending output should be
|
||||
/// flushed to the output buffer and the output should be aligned on a byte
|
||||
/// boundary. You might use this in a streaming communication scenario, so that
|
||||
/// the decompressor can get all input data available so far. When using this
|
||||
/// with a ZlibCodec, <c>AvailableBytesIn</c> will be zero after the call if
|
||||
/// enough output space has been provided before the call. Flushing will
|
||||
/// degrade compression and so it should be used only when necessary.
|
||||
/// </summary>
|
||||
Sync,
|
||||
|
||||
/// <summary>
|
||||
/// Use this during compression to specify that all output should be flushed, as
|
||||
/// with <c>FlushType.Sync</c>, but also, the compression state should be reset
|
||||
/// so that decompression can restart from this point if previous compressed
|
||||
/// data has been damaged or if random access is desired. Using
|
||||
/// <c>FlushType.Full</c> too often can significantly degrade the compression.
|
||||
/// </summary>
|
||||
Full,
|
||||
|
||||
/// <summary>Signals the end of the compression/decompression stream.</summary>
|
||||
Finish,
|
||||
}
|
||||
|
||||
}
|
||||
1033
SabreTools.Compression/Deflate/GZipStream.cs
Normal file
1033
SabreTools.Compression/Deflate/GZipStream.cs
Normal file
File diff suppressed because it is too large
Load Diff
435
SabreTools.Compression/Deflate/InfTree.cs
Normal file
435
SabreTools.Compression/Deflate/InfTree.cs
Normal file
@@ -0,0 +1,435 @@
|
||||
// Inftree.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-October-28 12:43:54>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes used in decompression. This code is derived
|
||||
// from the jzlib implementation of zlib. In keeping with the license for jzlib,
|
||||
// the copyright to that code is below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
sealed class InfTree
|
||||
{
|
||||
|
||||
private const int MANY = 1440;
|
||||
|
||||
private const int Z_OK = 0;
|
||||
private const int Z_STREAM_END = 1;
|
||||
private const int Z_NEED_DICT = 2;
|
||||
private const int Z_ERRNO = -1;
|
||||
private const int Z_STREAM_ERROR = -2;
|
||||
private const int Z_DATA_ERROR = -3;
|
||||
private const int Z_MEM_ERROR = -4;
|
||||
private const int Z_BUF_ERROR = -5;
|
||||
private const int Z_VERSION_ERROR = -6;
|
||||
|
||||
internal const int fixed_bl = 9;
|
||||
internal const int fixed_bd = 5;
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186,
|
||||
0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8,
|
||||
14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255};
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] fixed_td = new int[] { 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 };
|
||||
|
||||
// Tables for deflate from PKZIP's appnote.txt.
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cplens = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
|
||||
|
||||
// see note #13 above about 258
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cplext = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 };
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cpdist = new int[] { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 };
|
||||
|
||||
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
|
||||
internal static readonly int[] cpdext = new int[] { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
|
||||
|
||||
// If BMAX needs to be larger than 16, then h and x[] should be uLong.
|
||||
internal const int BMAX = 15; // maximum bit length of any code
|
||||
|
||||
internal int[] hn = null; // hufts used in space
|
||||
internal int[] v = null; // work area for huft_build
|
||||
internal int[] c = null; // bit length count table
|
||||
internal int[] r = null; // table entry for structure assignment
|
||||
internal int[] u = null; // table stack
|
||||
internal int[] x = null; // bit offsets, then code stack
|
||||
|
||||
private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
|
||||
{
|
||||
// Given a list of code lengths and a maximum table size, make a set of
|
||||
// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
|
||||
// if the given code set is incomplete (the tables are still built in this
|
||||
// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
|
||||
// lengths), or Z_MEM_ERROR if not enough memory.
|
||||
|
||||
int a; // counter for codes of length k
|
||||
int f; // i repeats in table every f entries
|
||||
int g; // maximum code length
|
||||
int h; // table level
|
||||
int i; // counter, current code
|
||||
int j; // counter
|
||||
int k; // number of bits in current code
|
||||
int l; // bits per table (returned in m)
|
||||
int mask; // (1 << w) - 1, to avoid cc -O bug on HP
|
||||
int p; // pointer into c[], b[], or v[]
|
||||
int q; // points to current table
|
||||
int w; // bits before this table == (l * h)
|
||||
int xp; // pointer into x
|
||||
int y; // number of dummy codes added
|
||||
int z; // number of entries in current table
|
||||
|
||||
// Generate counts for each bit length
|
||||
|
||||
p = 0; i = n;
|
||||
do
|
||||
{
|
||||
c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
|
||||
}
|
||||
while (i != 0);
|
||||
|
||||
if (c[0] == n)
|
||||
{
|
||||
// null input--all zero length codes
|
||||
t[0] = -1;
|
||||
m[0] = 0;
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
// Find minimum and maximum length, bound *m by those
|
||||
l = m[0];
|
||||
for (j = 1; j <= BMAX; j++)
|
||||
if (c[j] != 0)
|
||||
break;
|
||||
k = j; // minimum code length
|
||||
if (l < j)
|
||||
{
|
||||
l = j;
|
||||
}
|
||||
for (i = BMAX; i != 0; i--)
|
||||
{
|
||||
if (c[i] != 0)
|
||||
break;
|
||||
}
|
||||
g = i; // maximum code length
|
||||
if (l > i)
|
||||
{
|
||||
l = i;
|
||||
}
|
||||
m[0] = l;
|
||||
|
||||
// Adjust last length count to fill out codes, if needed
|
||||
for (y = 1 << j; j < i; j++, y <<= 1)
|
||||
{
|
||||
if ((y -= c[j]) < 0)
|
||||
{
|
||||
return Z_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
if ((y -= c[i]) < 0)
|
||||
{
|
||||
return Z_DATA_ERROR;
|
||||
}
|
||||
c[i] += y;
|
||||
|
||||
// Generate starting offsets into the value table for each length
|
||||
x[1] = j = 0;
|
||||
p = 1; xp = 2;
|
||||
while (--i != 0)
|
||||
{
|
||||
// note that i == g from above
|
||||
x[xp] = (j += c[p]);
|
||||
xp++;
|
||||
p++;
|
||||
}
|
||||
|
||||
// Make a table of values in order of bit lengths
|
||||
i = 0; p = 0;
|
||||
do
|
||||
{
|
||||
if ((j = b[bindex + p]) != 0)
|
||||
{
|
||||
v[x[j]++] = i;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
while (++i < n);
|
||||
n = x[g]; // set n to length of v
|
||||
|
||||
// Generate the Huffman codes and for each, make the table entries
|
||||
x[0] = i = 0; // first Huffman code is zero
|
||||
p = 0; // grab values in bit order
|
||||
h = -1; // no tables yet--level -1
|
||||
w = -l; // bits decoded == (l * h)
|
||||
u[0] = 0; // just to keep compilers happy
|
||||
q = 0; // ditto
|
||||
z = 0; // ditto
|
||||
|
||||
// go through the bit lengths (k already is bits in shortest code)
|
||||
for (; k <= g; k++)
|
||||
{
|
||||
a = c[k];
|
||||
while (a-- != 0)
|
||||
{
|
||||
// here i is the Huffman code of length k bits for value *p
|
||||
// make tables up to required level
|
||||
while (k > w + l)
|
||||
{
|
||||
h++;
|
||||
w += l; // previous table always l bits
|
||||
// compute minimum size table less than or equal to l bits
|
||||
z = g - w;
|
||||
z = (z > l) ? l : z; // table size upper limit
|
||||
if ((f = 1 << (j = k - w)) > a + 1)
|
||||
{
|
||||
// try a k-w bit table
|
||||
// too few codes for k-w bit table
|
||||
f -= (a + 1); // deduct codes from patterns left
|
||||
xp = k;
|
||||
if (j < z)
|
||||
{
|
||||
while (++j < z)
|
||||
{
|
||||
// try smaller tables up to z bits
|
||||
if ((f <<= 1) <= c[++xp])
|
||||
break; // enough codes to use up j bits
|
||||
f -= c[xp]; // else deduct codes from patterns
|
||||
}
|
||||
}
|
||||
}
|
||||
z = 1 << j; // table entries for j-bit table
|
||||
|
||||
// allocate new table
|
||||
if (hn[0] + z > MANY)
|
||||
{
|
||||
// (note: doesn't matter for fixed)
|
||||
return Z_DATA_ERROR; // overflow of MANY
|
||||
}
|
||||
u[h] = q = hn[0]; // DEBUG
|
||||
hn[0] += z;
|
||||
|
||||
// connect to last table, if there is one
|
||||
if (h != 0)
|
||||
{
|
||||
x[h] = i; // save pattern for backing up
|
||||
r[0] = (sbyte)j; // bits in this table
|
||||
r[1] = (sbyte)l; // bits to dump before this table
|
||||
j = SharedUtils.URShift(i, (w - l));
|
||||
r[2] = (int)(q - u[h - 1] - j); // offset to this table
|
||||
Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
|
||||
}
|
||||
else
|
||||
{
|
||||
t[0] = q; // first table is returned result
|
||||
}
|
||||
}
|
||||
|
||||
// set up table entry in r
|
||||
r[1] = (sbyte)(k - w);
|
||||
if (p >= n)
|
||||
{
|
||||
r[0] = 128 + 64; // out of values--invalid code
|
||||
}
|
||||
else if (v[p] < s)
|
||||
{
|
||||
r[0] = (sbyte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
|
||||
r[2] = v[p++]; // simple code is just the value
|
||||
}
|
||||
else
|
||||
{
|
||||
r[0] = (sbyte)(e[v[p] - s] + 16 + 64); // non-simple--look up in lists
|
||||
r[2] = d[v[p++] - s];
|
||||
}
|
||||
|
||||
// fill code-like entries with r
|
||||
f = 1 << (k - w);
|
||||
for (j = SharedUtils.URShift(i, w); j < z; j += f)
|
||||
{
|
||||
Array.Copy(r, 0, hp, (q + j) * 3, 3);
|
||||
}
|
||||
|
||||
// backwards increment the k-bit code i
|
||||
for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
|
||||
{
|
||||
i ^= j;
|
||||
}
|
||||
i ^= j;
|
||||
|
||||
// backup over finished tables
|
||||
mask = (1 << w) - 1; // needed on HP, cc -O bug
|
||||
while ((i & mask) != x[h])
|
||||
{
|
||||
h--; // don't need to update q
|
||||
w -= l;
|
||||
mask = (1 << w) - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Return Z_BUF_ERROR if we were given an incomplete table
|
||||
return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
|
||||
}
|
||||
|
||||
internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
|
||||
{
|
||||
int result;
|
||||
initWorkArea(19);
|
||||
hn[0] = 0;
|
||||
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
|
||||
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed dynamic bit lengths tree";
|
||||
}
|
||||
else if (result == Z_BUF_ERROR || bb[0] == 0)
|
||||
{
|
||||
z.Message = "incomplete dynamic bit lengths tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
|
||||
{
|
||||
int result;
|
||||
|
||||
// build literal/length tree
|
||||
initWorkArea(288);
|
||||
hn[0] = 0;
|
||||
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
|
||||
if (result != Z_OK || bl[0] == 0)
|
||||
{
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed literal/length tree";
|
||||
}
|
||||
else if (result != Z_MEM_ERROR)
|
||||
{
|
||||
z.Message = "incomplete literal/length tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// build distance tree
|
||||
initWorkArea(288);
|
||||
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
|
||||
|
||||
if (result != Z_OK || (bd[0] == 0 && nl > 257))
|
||||
{
|
||||
if (result == Z_DATA_ERROR)
|
||||
{
|
||||
z.Message = "oversubscribed distance tree";
|
||||
}
|
||||
else if (result == Z_BUF_ERROR)
|
||||
{
|
||||
z.Message = "incomplete distance tree";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
else if (result != Z_MEM_ERROR)
|
||||
{
|
||||
z.Message = "empty distance tree with lengths";
|
||||
result = Z_DATA_ERROR;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
|
||||
{
|
||||
bl[0] = fixed_bl;
|
||||
bd[0] = fixed_bd;
|
||||
tl[0] = fixed_tl;
|
||||
td[0] = fixed_td;
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
private void initWorkArea(int vsize)
|
||||
{
|
||||
if (hn == null)
|
||||
{
|
||||
hn = new int[1];
|
||||
v = new int[vsize];
|
||||
c = new int[BMAX + 1];
|
||||
r = new int[3];
|
||||
u = new int[BMAX];
|
||||
x = new int[BMAX + 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (v.Length < vsize)
|
||||
{
|
||||
v = new int[vsize];
|
||||
}
|
||||
Array.Clear(v, 0, vsize);
|
||||
Array.Clear(c, 0, BMAX + 1);
|
||||
r[0] = 0; r[1] = 0; r[2] = 0;
|
||||
// for(int i=0; i<BMAX; i++){u[i]=0;}
|
||||
//Array.Copy(c, 0, u, 0, BMAX);
|
||||
Array.Clear(u, 0, BMAX);
|
||||
// for(int i=0; i<BMAX+1; i++){x[i]=0;}
|
||||
//Array.Copy(c, 0, x, 0, BMAX + 1);
|
||||
Array.Clear(x, 0, BMAX + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
737
SabreTools.Compression/Deflate/InflateBlocks.cs
Normal file
737
SabreTools.Compression/Deflate/InflateBlocks.cs
Normal file
@@ -0,0 +1,737 @@
|
||||
// Inflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2010-January-08 18:32:12>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for decompression. This code is derived
|
||||
// from the jzlib implementation of zlib, but significantly modified.
|
||||
// The object model is not the same, and many of the behaviors are
|
||||
// different. Nonetheless, in keeping with the license for jzlib, I am
|
||||
// reproducing the copyright to that code here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
sealed class InflateBlocks
|
||||
{
|
||||
private const int MANY = 1440;
|
||||
|
||||
// Table for deflate from PKZIP's appnote.txt.
|
||||
internal static readonly int[] border = new int[]
|
||||
{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
|
||||
|
||||
private enum InflateBlockMode
|
||||
{
|
||||
TYPE = 0, // get type bits (3, including end bit)
|
||||
LENS = 1, // get lengths for stored
|
||||
STORED = 2, // processing stored block
|
||||
TABLE = 3, // get table lengths
|
||||
BTREE = 4, // get bit lengths tree for a dynamic block
|
||||
DTREE = 5, // get length, distance trees for a dynamic block
|
||||
CODES = 6, // processing fixed or dynamic block
|
||||
DRY = 7, // output remaining window bytes
|
||||
DONE = 8, // finished last block, done
|
||||
BAD = 9, // ot a data error--stuck here
|
||||
}
|
||||
|
||||
private InflateBlockMode mode; // current inflate_block mode
|
||||
|
||||
internal int left; // if STORED, bytes left to copy
|
||||
|
||||
internal int table; // table lengths (14 bits)
|
||||
internal int index; // index into blens (or border)
|
||||
internal int[] blens; // bit lengths of codes
|
||||
internal int[] bb = new int[1]; // bit length tree depth
|
||||
internal int[] tb = new int[1]; // bit length decoding tree
|
||||
|
||||
internal InflateCodes codes = new InflateCodes(); // if CODES, current state
|
||||
|
||||
internal int last; // true if this block is the last block
|
||||
|
||||
internal ZlibCodec _codec; // pointer back to this zlib stream
|
||||
|
||||
// mode independent information
|
||||
internal int bitk; // bits in bit buffer
|
||||
internal int bitb; // bit buffer
|
||||
internal int[] hufts; // single malloc for tree space
|
||||
internal byte[] window; // sliding window
|
||||
internal int end; // one byte after sliding window
|
||||
internal int readAt; // window read pointer
|
||||
internal int writeAt; // window write pointer
|
||||
internal System.Object checkfn; // check function
|
||||
internal uint check; // check on output
|
||||
|
||||
internal InfTree inftree = new InfTree();
|
||||
|
||||
internal InflateBlocks(ZlibCodec codec, System.Object checkfn, int w)
|
||||
{
|
||||
_codec = codec;
|
||||
hufts = new int[MANY * 3];
|
||||
window = new byte[w];
|
||||
end = w;
|
||||
this.checkfn = checkfn;
|
||||
mode = InflateBlockMode.TYPE;
|
||||
Reset();
|
||||
}
|
||||
|
||||
internal uint Reset()
|
||||
{
|
||||
uint oldCheck = check;
|
||||
mode = InflateBlockMode.TYPE;
|
||||
bitk = 0;
|
||||
bitb = 0;
|
||||
readAt = writeAt = 0;
|
||||
|
||||
if (checkfn != null)
|
||||
_codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
|
||||
return oldCheck;
|
||||
}
|
||||
|
||||
|
||||
internal int Process(int r)
|
||||
{
|
||||
int t; // temporary storage
|
||||
int b; // bit buffer
|
||||
int k; // bits in bit buffer
|
||||
int p; // input data pointer
|
||||
int n; // bytes available there
|
||||
int q; // output window write pointer
|
||||
int m; // bytes to end of window or read pointer
|
||||
|
||||
// copy input/output information to locals (UPDATE macro restores)
|
||||
|
||||
p = _codec.NextIn;
|
||||
n = _codec.AvailableBytesIn;
|
||||
b = bitb;
|
||||
k = bitk;
|
||||
|
||||
q = writeAt;
|
||||
m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
|
||||
|
||||
// process input based on current state
|
||||
while (true)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case InflateBlockMode.TYPE:
|
||||
|
||||
while (k < (3))
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
t = (int)(b & 7);
|
||||
last = t & 1;
|
||||
|
||||
switch ((uint)t >> 1)
|
||||
{
|
||||
case 0: // stored
|
||||
b >>= 3; k -= (3);
|
||||
t = k & 7; // go to byte boundary
|
||||
b >>= t; k -= t;
|
||||
mode = InflateBlockMode.LENS; // get length of stored block
|
||||
break;
|
||||
|
||||
case 1: // fixed
|
||||
int[] bl = new int[1];
|
||||
int[] bd = new int[1];
|
||||
int[][] tl = new int[1][];
|
||||
int[][] td = new int[1][];
|
||||
InfTree.inflate_trees_fixed(bl, bd, tl, td, _codec);
|
||||
codes.Init(bl[0], bd[0], tl[0], 0, td[0], 0);
|
||||
b >>= 3; k -= 3;
|
||||
mode = InflateBlockMode.CODES;
|
||||
break;
|
||||
|
||||
case 2: // dynamic
|
||||
b >>= 3; k -= 3;
|
||||
mode = InflateBlockMode.TABLE;
|
||||
break;
|
||||
|
||||
case 3: // illegal
|
||||
b >>= 3; k -= 3;
|
||||
mode = InflateBlockMode.BAD;
|
||||
_codec.Message = "invalid block type";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
break;
|
||||
|
||||
case InflateBlockMode.LENS:
|
||||
|
||||
while (k < (32))
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
;
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
|
||||
{
|
||||
mode = InflateBlockMode.BAD;
|
||||
_codec.Message = "invalid stored block lengths";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
left = (b & 0xffff);
|
||||
b = k = 0; // dump bits
|
||||
mode = left != 0 ? InflateBlockMode.STORED : (last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE);
|
||||
break;
|
||||
|
||||
case InflateBlockMode.STORED:
|
||||
if (n == 0)
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
if (m == 0)
|
||||
{
|
||||
if (q == end && readAt != 0)
|
||||
{
|
||||
q = 0; m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
}
|
||||
if (m == 0)
|
||||
{
|
||||
writeAt = q;
|
||||
r = Flush(r);
|
||||
q = writeAt; m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
if (q == end && readAt != 0)
|
||||
{
|
||||
q = 0; m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
}
|
||||
if (m == 0)
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
r = ZlibConstants.Z_OK;
|
||||
|
||||
t = left;
|
||||
if (t > n)
|
||||
t = n;
|
||||
if (t > m)
|
||||
t = m;
|
||||
Array.Copy(_codec.InputBuffer, p, window, q, t);
|
||||
p += t; n -= t;
|
||||
q += t; m -= t;
|
||||
if ((left -= t) != 0)
|
||||
break;
|
||||
mode = last != 0 ? InflateBlockMode.DRY : InflateBlockMode.TYPE;
|
||||
break;
|
||||
|
||||
case InflateBlockMode.TABLE:
|
||||
|
||||
while (k < (14))
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
table = t = (b & 0x3fff);
|
||||
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
|
||||
{
|
||||
mode = InflateBlockMode.BAD;
|
||||
_codec.Message = "too many length or distance symbols";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
|
||||
if (blens == null || blens.Length < t)
|
||||
{
|
||||
blens = new int[t];
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Clear(blens, 0, t);
|
||||
// for (int i = 0; i < t; i++)
|
||||
// {
|
||||
// blens[i] = 0;
|
||||
// }
|
||||
}
|
||||
|
||||
b >>= 14;
|
||||
k -= 14;
|
||||
|
||||
|
||||
index = 0;
|
||||
mode = InflateBlockMode.BTREE;
|
||||
goto case InflateBlockMode.BTREE;
|
||||
|
||||
case InflateBlockMode.BTREE:
|
||||
while (index < 4 + (table >> 10))
|
||||
{
|
||||
while (k < (3))
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
blens[border[index++]] = b & 7;
|
||||
|
||||
b >>= 3; k -= 3;
|
||||
}
|
||||
|
||||
while (index < 19)
|
||||
{
|
||||
blens[border[index++]] = 0;
|
||||
}
|
||||
|
||||
bb[0] = 7;
|
||||
t = inftree.inflate_trees_bits(blens, bb, tb, hufts, _codec);
|
||||
if (t != ZlibConstants.Z_OK)
|
||||
{
|
||||
r = t;
|
||||
if (r == ZlibConstants.Z_DATA_ERROR)
|
||||
{
|
||||
blens = null;
|
||||
mode = InflateBlockMode.BAD;
|
||||
}
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
index = 0;
|
||||
mode = InflateBlockMode.DTREE;
|
||||
goto case InflateBlockMode.DTREE;
|
||||
|
||||
case InflateBlockMode.DTREE:
|
||||
while (true)
|
||||
{
|
||||
t = table;
|
||||
if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
int i, j, c;
|
||||
|
||||
t = bb[0];
|
||||
|
||||
while (k < t)
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
t = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 1];
|
||||
c = hufts[(tb[0] + (b & InternalInflateConstants.InflateMask[t])) * 3 + 2];
|
||||
|
||||
if (c < 16)
|
||||
{
|
||||
b >>= t; k -= t;
|
||||
blens[index++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
// c == 16..18
|
||||
i = c == 18 ? 7 : c - 14;
|
||||
j = c == 18 ? 11 : 3;
|
||||
|
||||
while (k < (t + i))
|
||||
{
|
||||
if (n != 0)
|
||||
{
|
||||
r = ZlibConstants.Z_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
n--;
|
||||
b |= (_codec.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
b >>= t; k -= t;
|
||||
|
||||
j += (b & InternalInflateConstants.InflateMask[i]);
|
||||
|
||||
b >>= i; k -= i;
|
||||
|
||||
i = index;
|
||||
t = table;
|
||||
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || (c == 16 && i < 1))
|
||||
{
|
||||
blens = null;
|
||||
mode = InflateBlockMode.BAD;
|
||||
_codec.Message = "invalid bit length repeat";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
c = (c == 16) ? blens[i - 1] : 0;
|
||||
do
|
||||
{
|
||||
blens[i++] = c;
|
||||
}
|
||||
while (--j != 0);
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
tb[0] = -1;
|
||||
{
|
||||
int[] bl = new int[] { 9 }; // must be <= 9 for lookahead assumptions
|
||||
int[] bd = new int[] { 6 }; // must be <= 9 for lookahead assumptions
|
||||
int[] tl = new int[1];
|
||||
int[] td = new int[1];
|
||||
|
||||
t = table;
|
||||
t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), blens, bl, bd, tl, td, hufts, _codec);
|
||||
|
||||
if (t != ZlibConstants.Z_OK)
|
||||
{
|
||||
if (t == ZlibConstants.Z_DATA_ERROR)
|
||||
{
|
||||
blens = null;
|
||||
mode = InflateBlockMode.BAD;
|
||||
}
|
||||
r = t;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
codes.Init(bl[0], bd[0], hufts, tl[0], hufts, td[0]);
|
||||
}
|
||||
mode = InflateBlockMode.CODES;
|
||||
goto case InflateBlockMode.CODES;
|
||||
|
||||
case InflateBlockMode.CODES:
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
|
||||
r = codes.Process(this, r);
|
||||
if (r != ZlibConstants.Z_STREAM_END)
|
||||
{
|
||||
return Flush(r);
|
||||
}
|
||||
|
||||
r = ZlibConstants.Z_OK;
|
||||
p = _codec.NextIn;
|
||||
n = _codec.AvailableBytesIn;
|
||||
b = bitb;
|
||||
k = bitk;
|
||||
q = writeAt;
|
||||
m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
|
||||
if (last == 0)
|
||||
{
|
||||
mode = InflateBlockMode.TYPE;
|
||||
break;
|
||||
}
|
||||
mode = InflateBlockMode.DRY;
|
||||
goto case InflateBlockMode.DRY;
|
||||
|
||||
case InflateBlockMode.DRY:
|
||||
writeAt = q;
|
||||
r = Flush(r);
|
||||
q = writeAt; m = (int)(q < readAt ? readAt - q - 1 : end - q);
|
||||
if (readAt != writeAt)
|
||||
{
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
mode = InflateBlockMode.DONE;
|
||||
goto case InflateBlockMode.DONE;
|
||||
|
||||
case InflateBlockMode.DONE:
|
||||
r = ZlibConstants.Z_STREAM_END;
|
||||
bitb = b;
|
||||
bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
|
||||
case InflateBlockMode.BAD:
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
|
||||
|
||||
default:
|
||||
r = ZlibConstants.Z_STREAM_ERROR;
|
||||
|
||||
bitb = b; bitk = k;
|
||||
_codec.AvailableBytesIn = n;
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
writeAt = q;
|
||||
return Flush(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void Free()
|
||||
{
|
||||
Reset();
|
||||
window = null;
|
||||
hufts = null;
|
||||
}
|
||||
|
||||
internal void SetDictionary(byte[] d, int start, int n)
|
||||
{
|
||||
Array.Copy(d, start, window, 0, n);
|
||||
readAt = writeAt = n;
|
||||
}
|
||||
|
||||
// Returns true if inflate is currently at the end of a block generated
|
||||
// by Z_SYNC_FLUSH or Z_FULL_FLUSH.
|
||||
internal int SyncPoint()
|
||||
{
|
||||
return mode == InflateBlockMode.LENS ? 1 : 0;
|
||||
}
|
||||
|
||||
// copy as much as possible from the sliding window to the output area
|
||||
internal int Flush(int r)
|
||||
{
|
||||
int nBytes;
|
||||
|
||||
for (int pass = 0; pass < 2; pass++)
|
||||
{
|
||||
if (pass == 0)
|
||||
{
|
||||
// compute number of bytes to copy as far as end of window
|
||||
nBytes = (int)((readAt <= writeAt ? writeAt : end) - readAt);
|
||||
}
|
||||
else
|
||||
{
|
||||
// compute bytes to copy
|
||||
nBytes = writeAt - readAt;
|
||||
}
|
||||
|
||||
// workitem 8870
|
||||
if (nBytes == 0)
|
||||
{
|
||||
if (r == ZlibConstants.Z_BUF_ERROR)
|
||||
r = ZlibConstants.Z_OK;
|
||||
return r;
|
||||
}
|
||||
|
||||
if (nBytes > _codec.AvailableBytesOut)
|
||||
nBytes = _codec.AvailableBytesOut;
|
||||
|
||||
if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
|
||||
r = ZlibConstants.Z_OK;
|
||||
|
||||
// update counters
|
||||
_codec.AvailableBytesOut -= nBytes;
|
||||
_codec.TotalBytesOut += nBytes;
|
||||
|
||||
// update check information
|
||||
if (checkfn != null)
|
||||
_codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
|
||||
|
||||
// copy as far as end of window
|
||||
Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
|
||||
_codec.NextOut += nBytes;
|
||||
readAt += nBytes;
|
||||
|
||||
// see if more to copy at beginning of window
|
||||
if (readAt == end && pass == 0)
|
||||
{
|
||||
// wrap pointers
|
||||
readAt = 0;
|
||||
if (writeAt == end)
|
||||
writeAt = 0;
|
||||
}
|
||||
else pass++;
|
||||
}
|
||||
|
||||
// done
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
723
SabreTools.Compression/Deflate/InflateCodes.cs
Normal file
723
SabreTools.Compression/Deflate/InflateCodes.cs
Normal file
@@ -0,0 +1,723 @@
|
||||
// Inflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2010-January-08 18:32:12>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for decompression. This code is derived
|
||||
// from the jzlib implementation of zlib, but significantly modified.
|
||||
// The object model is not the same, and many of the behaviors are
|
||||
// different. Nonetheless, in keeping with the license for jzlib, I am
|
||||
// reproducing the copyright to that code here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
sealed class InflateCodes
|
||||
{
|
||||
// waiting for "i:"=input,
|
||||
// "o:"=output,
|
||||
// "x:"=nothing
|
||||
private const int START = 0; // x: set up for LEN
|
||||
private const int LEN = 1; // i: get length/literal/eob next
|
||||
private const int LENEXT = 2; // i: getting length extra (have base)
|
||||
private const int DIST = 3; // i: get distance next
|
||||
private const int DISTEXT = 4; // i: getting distance extra
|
||||
private const int COPY = 5; // o: copying bytes in window, waiting for space
|
||||
private const int LIT = 6; // o: got literal, waiting for output space
|
||||
private const int WASH = 7; // o: got eob, possibly still output waiting
|
||||
private const int END = 8; // x: got eob and all data flushed
|
||||
private const int BADCODE = 9; // x: got error
|
||||
|
||||
internal int mode; // current inflate_codes mode
|
||||
|
||||
// mode dependent information
|
||||
internal int len;
|
||||
|
||||
internal int[] tree; // pointer into tree
|
||||
internal int tree_index = 0;
|
||||
internal int need; // bits needed
|
||||
|
||||
internal int lit;
|
||||
|
||||
// if EXT or COPY, where and how much
|
||||
internal int bitsToGet; // bits to get for extra
|
||||
internal int dist; // distance back to copy from
|
||||
|
||||
internal byte lbits; // ltree bits decoded per branch
|
||||
internal byte dbits; // dtree bits decoder per branch
|
||||
internal int[] ltree; // literal/length/eob tree
|
||||
internal int ltree_index; // literal/length/eob tree
|
||||
internal int[] dtree; // distance tree
|
||||
internal int dtree_index; // distance tree
|
||||
|
||||
internal InflateCodes()
|
||||
{
|
||||
}
|
||||
|
||||
internal void Init(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index)
|
||||
{
|
||||
mode = START;
|
||||
lbits = (byte)bl;
|
||||
dbits = (byte)bd;
|
||||
ltree = tl;
|
||||
ltree_index = tl_index;
|
||||
dtree = td;
|
||||
dtree_index = td_index;
|
||||
tree = null;
|
||||
}
|
||||
|
||||
internal int Process(InflateBlocks blocks, int r)
|
||||
{
|
||||
int j; // temporary storage
|
||||
int tindex; // temporary pointer
|
||||
int e; // extra bits or operation
|
||||
int b = 0; // bit buffer
|
||||
int k = 0; // bits in bit buffer
|
||||
int p = 0; // input data pointer
|
||||
int n; // bytes available there
|
||||
int q; // output window write pointer
|
||||
int m; // bytes to end of window or read pointer
|
||||
int f; // pointer to copy strings from
|
||||
|
||||
ZlibCodec z = blocks._codec;
|
||||
|
||||
// copy input/output information to locals (UPDATE macro restores)
|
||||
p = z.NextIn;
|
||||
n = z.AvailableBytesIn;
|
||||
b = blocks.bitb;
|
||||
k = blocks.bitk;
|
||||
q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
|
||||
// process input and output based on current state
|
||||
while (true)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
// waiting for "i:"=input, "o:"=output, "x:"=nothing
|
||||
case START: // x: set up for LEN
|
||||
if (m >= 258 && n >= 10)
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n;
|
||||
z.TotalBytesIn += p - z.NextIn;
|
||||
z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
r = InflateFast(lbits, dbits, ltree, ltree_index, dtree, dtree_index, blocks, z);
|
||||
|
||||
p = z.NextIn;
|
||||
n = z.AvailableBytesIn;
|
||||
b = blocks.bitb;
|
||||
k = blocks.bitk;
|
||||
q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
|
||||
if (r != ZlibConstants.Z_OK)
|
||||
{
|
||||
mode = (r == ZlibConstants.Z_STREAM_END) ? WASH : BADCODE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
need = lbits;
|
||||
tree = ltree;
|
||||
tree_index = ltree_index;
|
||||
|
||||
mode = LEN;
|
||||
goto case LEN;
|
||||
|
||||
case LEN: // i: get length/literal/eob next
|
||||
j = need;
|
||||
|
||||
while (k < j)
|
||||
{
|
||||
if (n != 0)
|
||||
r = ZlibConstants.Z_OK;
|
||||
else
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n;
|
||||
z.TotalBytesIn += p - z.NextIn;
|
||||
z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
n--;
|
||||
b |= (z.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
|
||||
|
||||
b >>= (tree[tindex + 1]);
|
||||
k -= (tree[tindex + 1]);
|
||||
|
||||
e = tree[tindex];
|
||||
|
||||
if (e == 0)
|
||||
{
|
||||
// literal
|
||||
lit = tree[tindex + 2];
|
||||
mode = LIT;
|
||||
break;
|
||||
}
|
||||
if ((e & 16) != 0)
|
||||
{
|
||||
// length
|
||||
bitsToGet = e & 15;
|
||||
len = tree[tindex + 2];
|
||||
mode = LENEXT;
|
||||
break;
|
||||
}
|
||||
if ((e & 64) == 0)
|
||||
{
|
||||
// next table
|
||||
need = e;
|
||||
tree_index = tindex / 3 + tree[tindex + 2];
|
||||
break;
|
||||
}
|
||||
if ((e & 32) != 0)
|
||||
{
|
||||
// end of block
|
||||
mode = WASH;
|
||||
break;
|
||||
}
|
||||
mode = BADCODE; // invalid code
|
||||
z.Message = "invalid literal/length code";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n;
|
||||
z.TotalBytesIn += p - z.NextIn;
|
||||
z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
|
||||
|
||||
case LENEXT: // i: getting length extra (have base)
|
||||
j = bitsToGet;
|
||||
|
||||
while (k < j)
|
||||
{
|
||||
if (n != 0)
|
||||
r = ZlibConstants.Z_OK;
|
||||
else
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
n--; b |= (z.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
len += (b & InternalInflateConstants.InflateMask[j]);
|
||||
|
||||
b >>= j;
|
||||
k -= j;
|
||||
|
||||
need = dbits;
|
||||
tree = dtree;
|
||||
tree_index = dtree_index;
|
||||
mode = DIST;
|
||||
goto case DIST;
|
||||
|
||||
case DIST: // i: get distance next
|
||||
j = need;
|
||||
|
||||
while (k < j)
|
||||
{
|
||||
if (n != 0)
|
||||
r = ZlibConstants.Z_OK;
|
||||
else
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
n--; b |= (z.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
tindex = (tree_index + (b & InternalInflateConstants.InflateMask[j])) * 3;
|
||||
|
||||
b >>= tree[tindex + 1];
|
||||
k -= tree[tindex + 1];
|
||||
|
||||
e = (tree[tindex]);
|
||||
if ((e & 0x10) != 0)
|
||||
{
|
||||
// distance
|
||||
bitsToGet = e & 15;
|
||||
dist = tree[tindex + 2];
|
||||
mode = DISTEXT;
|
||||
break;
|
||||
}
|
||||
if ((e & 64) == 0)
|
||||
{
|
||||
// next table
|
||||
need = e;
|
||||
tree_index = tindex / 3 + tree[tindex + 2];
|
||||
break;
|
||||
}
|
||||
mode = BADCODE; // invalid code
|
||||
z.Message = "invalid distance code";
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
|
||||
|
||||
case DISTEXT: // i: getting distance extra
|
||||
j = bitsToGet;
|
||||
|
||||
while (k < j)
|
||||
{
|
||||
if (n != 0)
|
||||
r = ZlibConstants.Z_OK;
|
||||
else
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
n--; b |= (z.InputBuffer[p++] & 0xff) << k;
|
||||
k += 8;
|
||||
}
|
||||
|
||||
dist += (b & InternalInflateConstants.InflateMask[j]);
|
||||
|
||||
b >>= j;
|
||||
k -= j;
|
||||
|
||||
mode = COPY;
|
||||
goto case COPY;
|
||||
|
||||
case COPY: // o: copying bytes in window, waiting for space
|
||||
f = q - dist;
|
||||
while (f < 0)
|
||||
{
|
||||
// modulo window size-"while" instead
|
||||
f += blocks.end; // of "if" handles invalid distances
|
||||
}
|
||||
while (len != 0)
|
||||
{
|
||||
if (m == 0)
|
||||
{
|
||||
if (q == blocks.end && blocks.readAt != 0)
|
||||
{
|
||||
q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
}
|
||||
if (m == 0)
|
||||
{
|
||||
blocks.writeAt = q; r = blocks.Flush(r);
|
||||
q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
|
||||
if (q == blocks.end && blocks.readAt != 0)
|
||||
{
|
||||
q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
}
|
||||
|
||||
if (m == 0)
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n;
|
||||
z.TotalBytesIn += p - z.NextIn;
|
||||
z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
blocks.window[q++] = blocks.window[f++]; m--;
|
||||
|
||||
if (f == blocks.end)
|
||||
f = 0;
|
||||
len--;
|
||||
}
|
||||
mode = START;
|
||||
break;
|
||||
|
||||
case LIT: // o: got literal, waiting for output space
|
||||
if (m == 0)
|
||||
{
|
||||
if (q == blocks.end && blocks.readAt != 0)
|
||||
{
|
||||
q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
}
|
||||
if (m == 0)
|
||||
{
|
||||
blocks.writeAt = q; r = blocks.Flush(r);
|
||||
q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
|
||||
if (q == blocks.end && blocks.readAt != 0)
|
||||
{
|
||||
q = 0; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
}
|
||||
if (m == 0)
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
r = ZlibConstants.Z_OK;
|
||||
|
||||
blocks.window[q++] = (byte)lit; m--;
|
||||
|
||||
mode = START;
|
||||
break;
|
||||
|
||||
case WASH: // o: got eob, possibly more output
|
||||
if (k > 7)
|
||||
{
|
||||
// return unused byte, if any
|
||||
k -= 8;
|
||||
n++;
|
||||
p--; // can always return one
|
||||
}
|
||||
|
||||
blocks.writeAt = q; r = blocks.Flush(r);
|
||||
q = blocks.writeAt; m = q < blocks.readAt ? blocks.readAt - q - 1 : blocks.end - q;
|
||||
|
||||
if (blocks.readAt != blocks.writeAt)
|
||||
{
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
mode = END;
|
||||
goto case END;
|
||||
|
||||
case END:
|
||||
r = ZlibConstants.Z_STREAM_END;
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
|
||||
case BADCODE: // x: got error
|
||||
|
||||
r = ZlibConstants.Z_DATA_ERROR;
|
||||
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
|
||||
default:
|
||||
r = ZlibConstants.Z_STREAM_ERROR;
|
||||
|
||||
blocks.bitb = b; blocks.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
blocks.writeAt = q;
|
||||
return blocks.Flush(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Called with number of bytes left to write in window at least 258
|
||||
// (the maximum string length) and number of input bytes available
|
||||
// at least ten. The ten bytes are six bytes for the longest length/
|
||||
// distance pair plus four bytes for overloading the bit buffer.
|
||||
|
||||
internal int InflateFast(int bl, int bd, int[] tl, int tl_index, int[] td, int td_index, InflateBlocks s, ZlibCodec z)
|
||||
{
|
||||
int t; // temporary pointer
|
||||
int[] tp; // temporary pointer
|
||||
int tp_index; // temporary pointer
|
||||
int e; // extra bits or operation
|
||||
int b; // bit buffer
|
||||
int k; // bits in bit buffer
|
||||
int p; // input data pointer
|
||||
int n; // bytes available there
|
||||
int q; // output window write pointer
|
||||
int m; // bytes to end of window or read pointer
|
||||
int ml; // mask for literal/length tree
|
||||
int md; // mask for distance tree
|
||||
int c; // bytes to copy
|
||||
int d; // distance back to copy from
|
||||
int r; // copy source pointer
|
||||
|
||||
int tp_index_t_3; // (tp_index+t)*3
|
||||
|
||||
// load input, output, bit values
|
||||
p = z.NextIn; n = z.AvailableBytesIn; b = s.bitb; k = s.bitk;
|
||||
q = s.writeAt; m = q < s.readAt ? s.readAt - q - 1 : s.end - q;
|
||||
|
||||
// initialize masks
|
||||
ml = InternalInflateConstants.InflateMask[bl];
|
||||
md = InternalInflateConstants.InflateMask[bd];
|
||||
|
||||
// do until not enough input or output space for fast loop
|
||||
do
|
||||
{
|
||||
// assume called with m >= 258 && n >= 10
|
||||
// get literal/length code
|
||||
while (k < (20))
|
||||
{
|
||||
// max bits for literal/length code
|
||||
n--;
|
||||
b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
|
||||
}
|
||||
|
||||
t = b & ml;
|
||||
tp = tl;
|
||||
tp_index = tl_index;
|
||||
tp_index_t_3 = (tp_index + t) * 3;
|
||||
if ((e = tp[tp_index_t_3]) == 0)
|
||||
{
|
||||
b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
|
||||
|
||||
s.window[q++] = (byte)tp[tp_index_t_3 + 2];
|
||||
m--;
|
||||
continue;
|
||||
}
|
||||
do
|
||||
{
|
||||
|
||||
b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
|
||||
|
||||
if ((e & 16) != 0)
|
||||
{
|
||||
e &= 15;
|
||||
c = tp[tp_index_t_3 + 2] + ((int)b & InternalInflateConstants.InflateMask[e]);
|
||||
|
||||
b >>= e; k -= e;
|
||||
|
||||
// decode distance base of block to copy
|
||||
while (k < 15)
|
||||
{
|
||||
// max bits for distance code
|
||||
n--;
|
||||
b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
|
||||
}
|
||||
|
||||
t = b & md;
|
||||
tp = td;
|
||||
tp_index = td_index;
|
||||
tp_index_t_3 = (tp_index + t) * 3;
|
||||
e = tp[tp_index_t_3];
|
||||
|
||||
do
|
||||
{
|
||||
|
||||
b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
|
||||
|
||||
if ((e & 16) != 0)
|
||||
{
|
||||
// get extra bits to add to distance base
|
||||
e &= 15;
|
||||
while (k < e)
|
||||
{
|
||||
// get extra bits (up to 13)
|
||||
n--;
|
||||
b |= (z.InputBuffer[p++] & 0xff) << k; k += 8;
|
||||
}
|
||||
|
||||
d = tp[tp_index_t_3 + 2] + (b & InternalInflateConstants.InflateMask[e]);
|
||||
|
||||
b >>= e; k -= e;
|
||||
|
||||
// do the copy
|
||||
m -= c;
|
||||
if (q >= d)
|
||||
{
|
||||
// offset before dest
|
||||
// just copy
|
||||
r = q - d;
|
||||
if (q - r > 0 && 2 > (q - r))
|
||||
{
|
||||
s.window[q++] = s.window[r++]; // minimum count is three,
|
||||
s.window[q++] = s.window[r++]; // so unroll loop a little
|
||||
c -= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(s.window, r, s.window, q, 2);
|
||||
q += 2; r += 2; c -= 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// else offset after destination
|
||||
r = q - d;
|
||||
do
|
||||
{
|
||||
r += s.end; // force pointer in window
|
||||
}
|
||||
while (r < 0); // covers invalid distances
|
||||
e = s.end - r;
|
||||
if (c > e)
|
||||
{
|
||||
// if source crosses,
|
||||
c -= e; // wrapped copy
|
||||
if (q - r > 0 && e > (q - r))
|
||||
{
|
||||
do
|
||||
{
|
||||
s.window[q++] = s.window[r++];
|
||||
}
|
||||
while (--e != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(s.window, r, s.window, q, e);
|
||||
q += e; r += e; e = 0;
|
||||
}
|
||||
r = 0; // copy rest from start of window
|
||||
}
|
||||
}
|
||||
|
||||
// copy all or what's left
|
||||
if (q - r > 0 && c > (q - r))
|
||||
{
|
||||
do
|
||||
{
|
||||
s.window[q++] = s.window[r++];
|
||||
}
|
||||
while (--c != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(s.window, r, s.window, q, c);
|
||||
q += c; r += c; c = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if ((e & 64) == 0)
|
||||
{
|
||||
t += tp[tp_index_t_3 + 2];
|
||||
t += (b & InternalInflateConstants.InflateMask[e]);
|
||||
tp_index_t_3 = (tp_index + t) * 3;
|
||||
e = tp[tp_index_t_3];
|
||||
}
|
||||
else
|
||||
{
|
||||
z.Message = "invalid distance code";
|
||||
|
||||
c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
|
||||
|
||||
s.bitb = b; s.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
s.writeAt = q;
|
||||
|
||||
return ZlibConstants.Z_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
break;
|
||||
}
|
||||
|
||||
if ((e & 64) == 0)
|
||||
{
|
||||
t += tp[tp_index_t_3 + 2];
|
||||
t += (b & InternalInflateConstants.InflateMask[e]);
|
||||
tp_index_t_3 = (tp_index + t) * 3;
|
||||
if ((e = tp[tp_index_t_3]) == 0)
|
||||
{
|
||||
b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]);
|
||||
s.window[q++] = (byte)tp[tp_index_t_3 + 2];
|
||||
m--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if ((e & 32) != 0)
|
||||
{
|
||||
c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
|
||||
|
||||
s.bitb = b; s.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
s.writeAt = q;
|
||||
|
||||
return ZlibConstants.Z_STREAM_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
z.Message = "invalid literal/length code";
|
||||
|
||||
c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
|
||||
|
||||
s.bitb = b; s.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
s.writeAt = q;
|
||||
|
||||
return ZlibConstants.Z_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
while (true);
|
||||
}
|
||||
while (m >= 258 && n >= 10);
|
||||
|
||||
// not enough input or output--restore pointers and return
|
||||
c = z.AvailableBytesIn - n; c = (k >> 3) < c ? k >> 3 : c; n += c; p -= c; k -= (c << 3);
|
||||
|
||||
s.bitb = b; s.bitk = k;
|
||||
z.AvailableBytesIn = n; z.TotalBytesIn += p - z.NextIn; z.NextIn = p;
|
||||
s.writeAt = q;
|
||||
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
465
SabreTools.Compression/Deflate/InflateManager.cs
Normal file
465
SabreTools.Compression/Deflate/InflateManager.cs
Normal file
@@ -0,0 +1,465 @@
|
||||
// Inflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2010-January-08 18:32:12>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for decompression. This code is derived
|
||||
// from the jzlib implementation of zlib, but significantly modified.
|
||||
// The object model is not the same, and many of the behaviors are
|
||||
// different. Nonetheless, in keeping with the license for jzlib, I am
|
||||
// reproducing the copyright to that code here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal sealed class InflateManager
|
||||
{
|
||||
// preset dictionary flag in zlib header
|
||||
private const int PRESET_DICT = 0x20;
|
||||
|
||||
private const int Z_DEFLATED = 8;
|
||||
|
||||
private enum InflateManagerMode
|
||||
{
|
||||
METHOD = 0, // waiting for method byte
|
||||
FLAG = 1, // waiting for flag byte
|
||||
DICT4 = 2, // four dictionary check bytes to go
|
||||
DICT3 = 3, // three dictionary check bytes to go
|
||||
DICT2 = 4, // two dictionary check bytes to go
|
||||
DICT1 = 5, // one dictionary check byte to go
|
||||
DICT0 = 6, // waiting for inflateSetDictionary
|
||||
BLOCKS = 7, // decompressing blocks
|
||||
CHECK4 = 8, // four check bytes to go
|
||||
CHECK3 = 9, // three check bytes to go
|
||||
CHECK2 = 10, // two check bytes to go
|
||||
CHECK1 = 11, // one check byte to go
|
||||
DONE = 12, // finished check, done
|
||||
BAD = 13, // got an error--stay here
|
||||
}
|
||||
|
||||
private InflateManagerMode mode; // current inflate mode
|
||||
internal ZlibCodec _codec; // pointer back to this zlib stream
|
||||
|
||||
// mode dependent information
|
||||
internal int method; // if FLAGS, method byte
|
||||
|
||||
// if CHECK, check values to compare
|
||||
internal uint computedCheck; // computed check value
|
||||
internal uint expectedCheck; // stream check value
|
||||
|
||||
// if BAD, inflateSync's marker bytes count
|
||||
internal int marker;
|
||||
|
||||
// mode independent information
|
||||
//internal int nowrap; // flag for no wrapper
|
||||
private bool _handleRfc1950HeaderBytes = true;
|
||||
internal bool HandleRfc1950HeaderBytes
|
||||
{
|
||||
get { return _handleRfc1950HeaderBytes; }
|
||||
set { _handleRfc1950HeaderBytes = value; }
|
||||
}
|
||||
internal int wbits; // log2(window size) (8..15, defaults to 15)
|
||||
|
||||
internal InflateBlocks blocks; // current inflate_blocks state
|
||||
|
||||
public InflateManager() { }
|
||||
|
||||
public InflateManager(bool expectRfc1950HeaderBytes)
|
||||
{
|
||||
_handleRfc1950HeaderBytes = expectRfc1950HeaderBytes;
|
||||
}
|
||||
|
||||
internal int Reset()
|
||||
{
|
||||
_codec.TotalBytesIn = _codec.TotalBytesOut = 0;
|
||||
_codec.Message = null;
|
||||
mode = HandleRfc1950HeaderBytes ? InflateManagerMode.METHOD : InflateManagerMode.BLOCKS;
|
||||
blocks.Reset();
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
|
||||
internal int End()
|
||||
{
|
||||
if (blocks != null)
|
||||
blocks.Free();
|
||||
blocks = null;
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
|
||||
internal int Initialize(ZlibCodec codec, int w)
|
||||
{
|
||||
_codec = codec;
|
||||
_codec.Message = null;
|
||||
blocks = null;
|
||||
|
||||
// handle undocumented nowrap option (no zlib header or check)
|
||||
//nowrap = 0;
|
||||
//if (w < 0)
|
||||
//{
|
||||
// w = - w;
|
||||
// nowrap = 1;
|
||||
//}
|
||||
|
||||
// set window size
|
||||
if (w < 8 || w > 15)
|
||||
{
|
||||
End();
|
||||
throw new ZlibException("Bad window size.");
|
||||
|
||||
//return ZlibConstants.Z_STREAM_ERROR;
|
||||
}
|
||||
wbits = w;
|
||||
|
||||
blocks = new InflateBlocks(codec,
|
||||
HandleRfc1950HeaderBytes ? this : null,
|
||||
1 << w);
|
||||
|
||||
// reset state
|
||||
Reset();
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
|
||||
|
||||
internal int Inflate(FlushType flush)
|
||||
{
|
||||
int b;
|
||||
|
||||
if (_codec.InputBuffer == null)
|
||||
throw new ZlibException("InputBuffer is null. ");
|
||||
|
||||
// int f = (flush == FlushType.Finish)
|
||||
// ? ZlibConstants.Z_BUF_ERROR
|
||||
// : ZlibConstants.Z_OK;
|
||||
|
||||
// workitem 8870
|
||||
int f = ZlibConstants.Z_OK;
|
||||
int r = ZlibConstants.Z_BUF_ERROR;
|
||||
|
||||
while (true)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case InflateManagerMode.METHOD:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
if (((method = _codec.InputBuffer[_codec.NextIn++]) & 0xf) != Z_DEFLATED)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
_codec.Message = String.Format("unknown compression method (0x{0:X2})", method);
|
||||
marker = 5; // can't try inflateSync
|
||||
break;
|
||||
}
|
||||
if ((method >> 4) + 8 > wbits)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
_codec.Message = String.Format("invalid window size ({0})", (method >> 4) + 8);
|
||||
marker = 5; // can't try inflateSync
|
||||
break;
|
||||
}
|
||||
mode = InflateManagerMode.FLAG;
|
||||
break;
|
||||
|
||||
|
||||
case InflateManagerMode.FLAG:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
b = (_codec.InputBuffer[_codec.NextIn++]) & 0xff;
|
||||
|
||||
if ((((method << 8) + b) % 31) != 0)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
_codec.Message = "incorrect header check";
|
||||
marker = 5; // can't try inflateSync
|
||||
break;
|
||||
}
|
||||
|
||||
mode = ((b & PRESET_DICT) == 0)
|
||||
? InflateManagerMode.BLOCKS
|
||||
: InflateManagerMode.DICT4;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.DICT4:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
|
||||
mode = InflateManagerMode.DICT3;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.DICT3:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
|
||||
mode = InflateManagerMode.DICT2;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.DICT2:
|
||||
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
|
||||
mode = InflateManagerMode.DICT1;
|
||||
break;
|
||||
|
||||
|
||||
case InflateManagerMode.DICT1:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
|
||||
_codec._Adler32 = expectedCheck;
|
||||
mode = InflateManagerMode.DICT0;
|
||||
return ZlibConstants.Z_NEED_DICT;
|
||||
|
||||
|
||||
case InflateManagerMode.DICT0:
|
||||
mode = InflateManagerMode.BAD;
|
||||
_codec.Message = "need dictionary";
|
||||
marker = 0; // can try inflateSync
|
||||
return ZlibConstants.Z_STREAM_ERROR;
|
||||
|
||||
|
||||
case InflateManagerMode.BLOCKS:
|
||||
r = blocks.Process(r);
|
||||
if (r == ZlibConstants.Z_DATA_ERROR)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
marker = 0; // can try inflateSync
|
||||
break;
|
||||
}
|
||||
|
||||
if (r == ZlibConstants.Z_OK) r = f;
|
||||
|
||||
if (r != ZlibConstants.Z_STREAM_END)
|
||||
return r;
|
||||
|
||||
r = f;
|
||||
computedCheck = blocks.Reset();
|
||||
if (!HandleRfc1950HeaderBytes)
|
||||
{
|
||||
mode = InflateManagerMode.DONE;
|
||||
return ZlibConstants.Z_STREAM_END;
|
||||
}
|
||||
mode = InflateManagerMode.CHECK4;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.CHECK4:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
|
||||
mode = InflateManagerMode.CHECK3;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.CHECK3:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
|
||||
mode = InflateManagerMode.CHECK2;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.CHECK2:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--;
|
||||
_codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
|
||||
mode = InflateManagerMode.CHECK1;
|
||||
break;
|
||||
|
||||
case InflateManagerMode.CHECK1:
|
||||
if (_codec.AvailableBytesIn == 0) return r;
|
||||
r = f;
|
||||
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
|
||||
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
|
||||
if (computedCheck != expectedCheck)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
_codec.Message = "incorrect data check";
|
||||
marker = 5; // can't try inflateSync
|
||||
break;
|
||||
}
|
||||
mode = InflateManagerMode.DONE;
|
||||
return ZlibConstants.Z_STREAM_END;
|
||||
|
||||
case InflateManagerMode.DONE:
|
||||
return ZlibConstants.Z_STREAM_END;
|
||||
|
||||
case InflateManagerMode.BAD:
|
||||
throw new ZlibException(String.Format("Bad state ({0})", _codec.Message));
|
||||
|
||||
default:
|
||||
throw new ZlibException("Stream error.");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal int SetDictionary(byte[] dictionary, bool check = true)
|
||||
{
|
||||
int index = 0;
|
||||
int length = dictionary.Length;
|
||||
|
||||
if (check)
|
||||
{
|
||||
if (mode != InflateManagerMode.DICT0)
|
||||
throw new ZlibException("Stream error.");
|
||||
|
||||
if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
|
||||
{
|
||||
return ZlibConstants.Z_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
_codec._Adler32 = Adler.Adler32(0, null, 0, 0);
|
||||
|
||||
if (length >= (1 << wbits))
|
||||
{
|
||||
length = (1 << wbits) - 1;
|
||||
index = dictionary.Length - length;
|
||||
}
|
||||
blocks.SetDictionary(dictionary, index, length);
|
||||
mode = InflateManagerMode.BLOCKS;
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
|
||||
|
||||
private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
|
||||
|
||||
internal int Sync()
|
||||
{
|
||||
int n; // number of bytes to look at
|
||||
int p; // pointer to bytes
|
||||
int m; // number of marker bytes found in a row
|
||||
long r, w; // temporaries to save total_in and total_out
|
||||
|
||||
// set up
|
||||
if (mode != InflateManagerMode.BAD)
|
||||
{
|
||||
mode = InflateManagerMode.BAD;
|
||||
marker = 0;
|
||||
}
|
||||
if ((n = _codec.AvailableBytesIn) == 0)
|
||||
return ZlibConstants.Z_BUF_ERROR;
|
||||
p = _codec.NextIn;
|
||||
m = marker;
|
||||
|
||||
// search
|
||||
while (n != 0 && m < 4)
|
||||
{
|
||||
if (_codec.InputBuffer[p] == mark[m])
|
||||
{
|
||||
m++;
|
||||
}
|
||||
else if (_codec.InputBuffer[p] != 0)
|
||||
{
|
||||
m = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m = 4 - m;
|
||||
}
|
||||
p++; n--;
|
||||
}
|
||||
|
||||
// restore
|
||||
_codec.TotalBytesIn += p - _codec.NextIn;
|
||||
_codec.NextIn = p;
|
||||
_codec.AvailableBytesIn = n;
|
||||
marker = m;
|
||||
|
||||
// return no joy or set up to restart on a new block
|
||||
if (m != 4)
|
||||
{
|
||||
return ZlibConstants.Z_DATA_ERROR;
|
||||
}
|
||||
r = _codec.TotalBytesIn;
|
||||
w = _codec.TotalBytesOut;
|
||||
Reset();
|
||||
_codec.TotalBytesIn = r;
|
||||
_codec.TotalBytesOut = w;
|
||||
mode = InflateManagerMode.BLOCKS;
|
||||
return ZlibConstants.Z_OK;
|
||||
}
|
||||
|
||||
|
||||
// Returns true if inflate is currently at the end of a block generated
|
||||
// by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
|
||||
// implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
|
||||
// but removes the length bytes of the resulting empty stored block. When
|
||||
// decompressing, PPP checks that at the end of input packet, inflate is
|
||||
// waiting for these length bytes.
|
||||
internal int SyncPoint(ZlibCodec z)
|
||||
{
|
||||
return blocks.SyncPoint();
|
||||
}
|
||||
}
|
||||
}
|
||||
114
SabreTools.Compression/Deflate/InternalConstants.cs
Normal file
114
SabreTools.Compression/Deflate/InternalConstants.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal static class InternalConstants
|
||||
{
|
||||
internal static readonly int MAX_BITS = 15;
|
||||
internal static readonly int BL_CODES = 19;
|
||||
internal static readonly int D_CODES = 30;
|
||||
internal static readonly int LITERALS = 256;
|
||||
internal static readonly int LENGTH_CODES = 29;
|
||||
internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
|
||||
|
||||
// Bit length codes must not exceed MAX_BL_BITS bits
|
||||
internal static readonly int MAX_BL_BITS = 7;
|
||||
|
||||
// repeat previous bit length 3-6 times (2 bits of repeat count)
|
||||
internal static readonly int REP_3_6 = 16;
|
||||
|
||||
// repeat a zero length 3-10 times (3 bits of repeat count)
|
||||
internal static readonly int REPZ_3_10 = 17;
|
||||
|
||||
// repeat a zero length 11-138 times (7 bits of repeat count)
|
||||
internal static readonly int REPZ_11_138 = 18;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
75
SabreTools.Compression/Deflate/InternalInflateConstants.cs
Normal file
75
SabreTools.Compression/Deflate/InternalInflateConstants.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
// Inflate.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2010-January-08 18:32:12>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for decompression. This code is derived
|
||||
// from the jzlib implementation of zlib, but significantly modified.
|
||||
// The object model is not the same, and many of the behaviors are
|
||||
// different. Nonetheless, in keeping with the license for jzlib, I am
|
||||
// reproducing the copyright to that code here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal static class InternalInflateConstants
|
||||
{
|
||||
// And'ing with mask[n] masks the lower n bits
|
||||
internal static readonly int[] InflateMask = new int[] {
|
||||
0x00000000, 0x00000001, 0x00000003, 0x00000007,
|
||||
0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
|
||||
0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
|
||||
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
|
||||
}
|
||||
}
|
||||
32
SabreTools.Compression/Deflate/LICENSE.jzlib.txt
Normal file
32
SabreTools.Compression/Deflate/LICENSE.jzlib.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
The ZLIB library, available as SabreTools.Compression.Deflate.dll or as part of DotNetZip,
|
||||
is a ported-then-modified version of jzlib. The following applies to jzlib:
|
||||
|
||||
JZlib 0.0.* were released under the GNU LGPL license. Later, we have switched
|
||||
over to a BSD-style license.
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. The names of the authors may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
31
SabreTools.Compression/Deflate/License.zlib.txt
Normal file
31
SabreTools.Compression/Deflate/License.zlib.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
The ZLIB library, available as SabreTools.Compression.Deflate.dll or as part of DotNetZip,
|
||||
is a ported-then-modified version of jzlib, which itself is based on
|
||||
zlib-1.1.3, the well-known C-language compression library.
|
||||
|
||||
The following notice applies to zlib:
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
|
||||
Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
|
||||
The ZLIB software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
Jean-loup Gailly jloup@gzip.org
|
||||
Mark Adler madler@alumni.caltech.edu
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
162
SabreTools.Compression/Deflate/SharedUtils.cs
Normal file
162
SabreTools.Compression/Deflate/SharedUtils.cs
Normal file
@@ -0,0 +1,162 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal class SharedUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs an unsigned bitwise right shift with the specified number
|
||||
/// </summary>
|
||||
/// <param name="number">Number to operate on</param>
|
||||
/// <param name="bits">Ammount of bits to shift</param>
|
||||
/// <returns>The resulting number from the shift operation</returns>
|
||||
public static int URShift(int number, int bits)
|
||||
{
|
||||
return (int)((uint)number >> bits);
|
||||
}
|
||||
|
||||
#if NOT
|
||||
/// <summary>
|
||||
/// Performs an unsigned bitwise right shift with the specified number
|
||||
/// </summary>
|
||||
/// <param name="number">Number to operate on</param>
|
||||
/// <param name="bits">Ammount of bits to shift</param>
|
||||
/// <returns>The resulting number from the shift operation</returns>
|
||||
public static long URShift(long number, int bits)
|
||||
{
|
||||
return (long) ((UInt64)number >> bits);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Reads a number of characters from the current source TextReader and writes
|
||||
/// the data to the target array at the specified index.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="sourceTextReader">The source TextReader to read from</param>
|
||||
/// <param name="target">Contains the array of characteres read from the source TextReader.</param>
|
||||
/// <param name="start">The starting index of the target array.</param>
|
||||
/// <param name="count">The maximum number of characters to read from the source TextReader.</param>
|
||||
///
|
||||
/// <returns>
|
||||
/// The number of characters read. The number will be less than or equal to
|
||||
/// count depending on the data available in the source TextReader. Returns -1
|
||||
/// if the end of the stream is reached.
|
||||
/// </returns>
|
||||
public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
|
||||
{
|
||||
// Returns 0 bytes if not enough space in target
|
||||
if (target.Length == 0) return 0;
|
||||
|
||||
char[] charArray = new char[target.Length];
|
||||
int bytesRead = sourceTextReader.Read(charArray, start, count);
|
||||
|
||||
// Returns -1 if EOF
|
||||
if (bytesRead == 0) return -1;
|
||||
|
||||
for (int index = start; index < start + bytesRead; index++)
|
||||
target[index] = (byte)charArray[index];
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
|
||||
internal static byte[] ToByteArray(System.String sourceString)
|
||||
{
|
||||
return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
|
||||
}
|
||||
|
||||
|
||||
internal static char[] ToCharArray(byte[] byteArray)
|
||||
{
|
||||
return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
164
SabreTools.Compression/Deflate/StaticTree.cs
Normal file
164
SabreTools.Compression/Deflate/StaticTree.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal sealed class StaticTree
|
||||
{
|
||||
internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
|
||||
12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8,
|
||||
28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8,
|
||||
2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8,
|
||||
18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8,
|
||||
10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
|
||||
26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8,
|
||||
6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
|
||||
22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8,
|
||||
14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8,
|
||||
30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
|
||||
1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8,
|
||||
17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8,
|
||||
9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8,
|
||||
25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8,
|
||||
5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
|
||||
21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8,
|
||||
13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8,
|
||||
29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8,
|
||||
19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
|
||||
51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
|
||||
11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9,
|
||||
43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9,
|
||||
27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9,
|
||||
59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9,
|
||||
7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
|
||||
39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9,
|
||||
23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9,
|
||||
55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9,
|
||||
15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9,
|
||||
47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
|
||||
31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9,
|
||||
63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9,
|
||||
0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7,
|
||||
8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7,
|
||||
4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
|
||||
3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8
|
||||
};
|
||||
|
||||
internal static readonly short[] distTreeCodes = new short[] {
|
||||
0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5,
|
||||
2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5,
|
||||
1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5,
|
||||
3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
|
||||
|
||||
internal static readonly StaticTree Literals;
|
||||
internal static readonly StaticTree Distances;
|
||||
internal static readonly StaticTree BitLengths;
|
||||
|
||||
internal short[]? treeCodes; // static tree or null
|
||||
internal int[]? extraBits; // extra bits for each code or null
|
||||
internal int extraBase; // base index for extra_bits
|
||||
internal int elems; // max number of elements in the tree
|
||||
internal int maxLength; // max bit length for the codes
|
||||
|
||||
private StaticTree(short[]? treeCodes, int[]? extraBits, int extraBase, int elems, int maxLength)
|
||||
{
|
||||
this.treeCodes = treeCodes;
|
||||
this.extraBits = extraBits;
|
||||
this.extraBase = extraBase;
|
||||
this.elems = elems;
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
static StaticTree()
|
||||
{
|
||||
Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
|
||||
Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS);
|
||||
BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
421
SabreTools.Compression/Deflate/Tree.cs
Normal file
421
SabreTools.Compression/Deflate/Tree.cs
Normal file
@@ -0,0 +1,421 @@
|
||||
// Tree.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-October-28 13:29:50>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for zlib compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib. In keeping with the license for jzlib, the copyright to that
|
||||
// code is below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
#nullable disable
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
sealed class Tree
|
||||
{
|
||||
private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1);
|
||||
|
||||
// extra bits for each length code
|
||||
internal static readonly int[] ExtraLengthBits = new int[]
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
||||
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
|
||||
};
|
||||
|
||||
// extra bits for each distance code
|
||||
internal static readonly int[] ExtraDistanceBits = new int[]
|
||||
{
|
||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
||||
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
|
||||
};
|
||||
|
||||
// extra bits for each bit length code
|
||||
internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
|
||||
|
||||
internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
||||
|
||||
|
||||
// The lengths of the bit length codes are sent in order of decreasing
|
||||
// probability, to avoid transmitting the lengths for unused bit
|
||||
// length codes.
|
||||
|
||||
internal const int Buf_size = 8 * 2;
|
||||
|
||||
// see definition of array dist_code below
|
||||
//internal const int DIST_CODE_LEN = 512;
|
||||
|
||||
private static readonly sbyte[] _dist_code = new sbyte[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
|
||||
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
|
||||
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
|
||||
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
||||
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
||||
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
||||
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||
0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
|
||||
22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
|
||||
};
|
||||
|
||||
internal static readonly sbyte[] LengthCode = new sbyte[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
||||
12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
|
||||
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17,
|
||||
18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
|
||||
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
|
||||
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
|
||||
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
|
||||
};
|
||||
|
||||
|
||||
internal static readonly int[] LengthBase = new int[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
|
||||
32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
|
||||
};
|
||||
|
||||
|
||||
internal static readonly int[] DistanceBase = new int[]
|
||||
{
|
||||
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
|
||||
256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
|
||||
};
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Map from a distance to a distance code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// No side effects. _dist_code[256] and _dist_code[257] are never used.
|
||||
/// </remarks>
|
||||
internal static int DistanceCode(int dist)
|
||||
{
|
||||
return (dist < 256)
|
||||
? _dist_code[dist]
|
||||
: _dist_code[256 + SharedUtils.URShift(dist, 7)];
|
||||
}
|
||||
|
||||
internal short[] dyn_tree; // the dynamic tree
|
||||
internal int max_code; // largest code with non zero frequency
|
||||
internal StaticTree staticTree; // the corresponding static tree
|
||||
|
||||
// Compute the optimal bit lengths for a tree and update the total bit length
|
||||
// for the current block.
|
||||
// IN assertion: the fields freq and dad are set, heap[heap_max] and
|
||||
// above are the tree nodes sorted by increasing frequency.
|
||||
// OUT assertions: the field len is set to the optimal bit length, the
|
||||
// array bl_count contains the frequencies for each bit length.
|
||||
// The length opt_len is updated; static_len is also updated if stree is
|
||||
// not null.
|
||||
internal void gen_bitlen(DeflateManager s)
|
||||
{
|
||||
short[] tree = dyn_tree;
|
||||
short[] stree = staticTree.treeCodes;
|
||||
int[] extra = staticTree.extraBits;
|
||||
int base_Renamed = staticTree.extraBase;
|
||||
int max_length = staticTree.maxLength;
|
||||
int h; // heap index
|
||||
int n, m; // iterate over the tree elements
|
||||
int bits; // bit length
|
||||
int xbits; // extra bits
|
||||
short f; // frequency
|
||||
int overflow = 0; // number of elements with bit length too large
|
||||
|
||||
for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
|
||||
s.bl_count[bits] = 0;
|
||||
|
||||
// In a first pass, compute the optimal bit lengths (which may
|
||||
// overflow in the case of the bit length tree).
|
||||
tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
|
||||
|
||||
for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
|
||||
{
|
||||
n = s.heap[h];
|
||||
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
|
||||
if (bits > max_length)
|
||||
{
|
||||
bits = max_length; overflow++;
|
||||
}
|
||||
tree[n * 2 + 1] = (short) bits;
|
||||
// We overwrite tree[n*2+1] which is no longer needed
|
||||
|
||||
if (n > max_code)
|
||||
continue; // not a leaf node
|
||||
|
||||
s.bl_count[bits]++;
|
||||
xbits = 0;
|
||||
if (n >= base_Renamed)
|
||||
xbits = extra[n - base_Renamed];
|
||||
f = tree[n * 2];
|
||||
s.opt_len += f * (bits + xbits);
|
||||
if (stree != null)
|
||||
s.static_len += f * (stree[n * 2 + 1] + xbits);
|
||||
}
|
||||
if (overflow == 0)
|
||||
return ;
|
||||
|
||||
// This happens for example on obj2 and pic of the Calgary corpus
|
||||
// Find the first bit length which could increase:
|
||||
do
|
||||
{
|
||||
bits = max_length - 1;
|
||||
while (s.bl_count[bits] == 0)
|
||||
bits--;
|
||||
s.bl_count[bits]--; // move one leaf down the tree
|
||||
s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
|
||||
s.bl_count[max_length]--;
|
||||
// The brother of the overflow item also moves one step up,
|
||||
// but this does not affect bl_count[max_length]
|
||||
overflow -= 2;
|
||||
}
|
||||
while (overflow > 0);
|
||||
|
||||
for (bits = max_length; bits != 0; bits--)
|
||||
{
|
||||
n = s.bl_count[bits];
|
||||
while (n != 0)
|
||||
{
|
||||
m = s.heap[--h];
|
||||
if (m > max_code)
|
||||
continue;
|
||||
if (tree[m * 2 + 1] != bits)
|
||||
{
|
||||
s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
|
||||
tree[m * 2 + 1] = (short) bits;
|
||||
}
|
||||
n--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Construct one Huffman tree and assigns the code bit strings and lengths.
|
||||
// Update the total bit length for the current block.
|
||||
// IN assertion: the field freq is set for all tree elements.
|
||||
// OUT assertions: the fields len and code are set to the optimal bit length
|
||||
// and corresponding code. The length opt_len is updated; static_len is
|
||||
// also updated if stree is not null. The field max_code is set.
|
||||
internal void build_tree(DeflateManager s)
|
||||
{
|
||||
short[] tree = dyn_tree;
|
||||
short[] stree = staticTree.treeCodes;
|
||||
int elems = staticTree.elems;
|
||||
int n, m; // iterate over heap elements
|
||||
int max_code = -1; // largest code with non zero frequency
|
||||
int node; // new node being created
|
||||
|
||||
// Construct the initial heap, with least frequent element in
|
||||
// heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
|
||||
// heap[0] is not used.
|
||||
s.heap_len = 0;
|
||||
s.heap_max = HEAP_SIZE;
|
||||
|
||||
for (n = 0; n < elems; n++)
|
||||
{
|
||||
if (tree[n * 2] != 0)
|
||||
{
|
||||
s.heap[++s.heap_len] = max_code = n;
|
||||
s.depth[n] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tree[n * 2 + 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// The pkzip format requires that at least one distance code exists,
|
||||
// and that at least one bit should be sent even if there is only one
|
||||
// possible code. So to avoid special checks later on we force at least
|
||||
// two codes of non zero frequency.
|
||||
while (s.heap_len < 2)
|
||||
{
|
||||
node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0);
|
||||
tree[node * 2] = 1;
|
||||
s.depth[node] = 0;
|
||||
s.opt_len--;
|
||||
if (stree != null)
|
||||
s.static_len -= stree[node * 2 + 1];
|
||||
// node is 0 or 1 so it does not have extra bits
|
||||
}
|
||||
this.max_code = max_code;
|
||||
|
||||
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
|
||||
// establish sub-heaps of increasing lengths:
|
||||
|
||||
for (n = s.heap_len / 2; n >= 1; n--)
|
||||
s.pqdownheap(tree, n);
|
||||
|
||||
// Construct the Huffman tree by repeatedly combining the least two
|
||||
// frequent nodes.
|
||||
|
||||
node = elems; // next internal node of the tree
|
||||
do
|
||||
{
|
||||
// n = node of least frequency
|
||||
n = s.heap[1];
|
||||
s.heap[1] = s.heap[s.heap_len--];
|
||||
s.pqdownheap(tree, 1);
|
||||
m = s.heap[1]; // m = node of next least frequency
|
||||
|
||||
s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
|
||||
s.heap[--s.heap_max] = m;
|
||||
|
||||
// Create a new node father of n and m
|
||||
tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2]));
|
||||
s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
|
||||
tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
|
||||
|
||||
// and insert the new node in the heap
|
||||
s.heap[1] = node++;
|
||||
s.pqdownheap(tree, 1);
|
||||
}
|
||||
while (s.heap_len >= 2);
|
||||
|
||||
s.heap[--s.heap_max] = s.heap[1];
|
||||
|
||||
// At this point, the fields freq and dad are set. We can now
|
||||
// generate the bit lengths.
|
||||
|
||||
gen_bitlen(s);
|
||||
|
||||
// The field len is now set, we can generate the bit codes
|
||||
gen_codes(tree, max_code, s.bl_count);
|
||||
}
|
||||
|
||||
// Generate the codes for a given tree and bit counts (which need not be
|
||||
// optimal).
|
||||
// IN assertion: the array bl_count contains the bit length statistics for
|
||||
// the given tree and the field len is set for all tree elements.
|
||||
// OUT assertion: the field code is set for all tree elements of non
|
||||
// zero code length.
|
||||
internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
|
||||
{
|
||||
short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length
|
||||
short code = 0; // running code value
|
||||
int bits; // bit index
|
||||
int n; // code index
|
||||
|
||||
// The distribution counts are first used to generate the code values
|
||||
// without bit reversal.
|
||||
for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
|
||||
unchecked {
|
||||
next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1);
|
||||
}
|
||||
|
||||
// Check that the bit counts in bl_count are consistent. The last code
|
||||
// must be all ones.
|
||||
//Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
|
||||
// "inconsistent bit counts");
|
||||
//Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
|
||||
|
||||
for (n = 0; n <= max_code; n++)
|
||||
{
|
||||
int len = tree[n * 2 + 1];
|
||||
if (len == 0)
|
||||
continue;
|
||||
// Now reverse the bits
|
||||
tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len)));
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the first len bits of a code, using straightforward code (a faster
|
||||
// method would use a table)
|
||||
// IN assertion: 1 <= len <= 15
|
||||
internal static int bi_reverse(int code, int len)
|
||||
{
|
||||
int res = 0;
|
||||
do
|
||||
{
|
||||
res |= code & 1;
|
||||
code >>= 1; //SharedUtils.URShift(code, 1);
|
||||
res <<= 1;
|
||||
}
|
||||
while (--len > 0);
|
||||
return res >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
61
SabreTools.Compression/Deflate/WorkItem.cs
Normal file
61
SabreTools.Compression/Deflate/WorkItem.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
//#define Trace
|
||||
|
||||
// ParallelDeflateOutputStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// A DeflateStream that does compression only, it uses a
|
||||
// divide-and-conquer approach with multiple threads to exploit multiple
|
||||
// CPUs for the DEFLATE computation.
|
||||
//
|
||||
// last saved: <2011-July-31 14:49:40>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 by Dino Chiesa
|
||||
// All rights reserved!
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
#nullable disable
|
||||
#pragma warning disable CS0649
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal class WorkItem
|
||||
{
|
||||
public byte[] buffer;
|
||||
public byte[] compressed;
|
||||
public int crc;
|
||||
public int index;
|
||||
public int ordinal;
|
||||
public int inputBytesAvailable;
|
||||
public int compressedBytesAvailable;
|
||||
public ZlibCodec compressor;
|
||||
|
||||
public WorkItem(int size,
|
||||
SabreTools.Compression.Deflate.CompressionLevel compressLevel,
|
||||
CompressionStrategy strategy,
|
||||
int ix)
|
||||
{
|
||||
this.buffer = new byte[size];
|
||||
// alloc 5 bytes overhead for every block (margin of safety= 2)
|
||||
int n = size + ((size / 32768) + 1) * 5 * 2;
|
||||
this.compressed = new byte[n];
|
||||
this.compressor = new ZlibCodec();
|
||||
this.compressor.InitializeDeflate(compressLevel, false);
|
||||
this.compressor.OutputBuffer = this.compressed;
|
||||
this.compressor.InputBuffer = this.buffer;
|
||||
this.index = ix;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
637
SabreTools.Compression/Deflate/ZlibBaseStream.cs
Normal file
637
SabreTools.Compression/Deflate/ZlibBaseStream.cs
Normal file
@@ -0,0 +1,637 @@
|
||||
// ZlibBaseStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-August-06 21:22:38>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the ZlibBaseStream class, which is an intnernal
|
||||
// base class for DeflateStream, ZlibStream and GZipStream.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
#nullable disable
|
||||
#pragma warning disable CA2022
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal class ZlibBaseStream : System.IO.Stream
|
||||
{
|
||||
protected internal ZlibCodec _z = null; // deferred init... new ZlibCodec();
|
||||
|
||||
protected internal StreamMode _streamMode = StreamMode.Undefined;
|
||||
protected internal FlushType _flushMode;
|
||||
protected internal ZlibStreamFlavor _flavor;
|
||||
protected internal CompressionMode _compressionMode;
|
||||
protected internal CompressionLevel _level;
|
||||
protected internal bool _leaveOpen;
|
||||
protected internal byte[] _workingBuffer;
|
||||
protected internal int _bufferSize = ZlibConstants.WorkingBufferSizeDefault;
|
||||
protected internal byte[] _buf1 = new byte[1];
|
||||
|
||||
protected internal System.IO.Stream _stream;
|
||||
protected internal CompressionStrategy Strategy = CompressionStrategy.Default;
|
||||
|
||||
// workitem 7159
|
||||
CRC32 crc;
|
||||
protected internal string _GzipFileName;
|
||||
protected internal string _GzipComment;
|
||||
protected internal DateTime _GzipMtime;
|
||||
protected internal int _gzipHeaderByteCount;
|
||||
|
||||
internal int Crc32 { get { if (crc == null) return 0; return crc.Crc32Result; } }
|
||||
|
||||
public ZlibBaseStream(System.IO.Stream stream,
|
||||
CompressionMode compressionMode,
|
||||
CompressionLevel level,
|
||||
ZlibStreamFlavor flavor,
|
||||
bool leaveOpen)
|
||||
: base()
|
||||
{
|
||||
this._flushMode = FlushType.None;
|
||||
//this._workingBuffer = new byte[WORKING_BUFFER_SIZE_DEFAULT];
|
||||
this._stream = stream;
|
||||
this._leaveOpen = leaveOpen;
|
||||
this._compressionMode = compressionMode;
|
||||
this._flavor = flavor;
|
||||
this._level = level;
|
||||
// workitem 7159
|
||||
if (flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
this.crc = new CRC32();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected internal bool _wantCompress
|
||||
{
|
||||
get
|
||||
{
|
||||
return (this._compressionMode == CompressionMode.Compress);
|
||||
}
|
||||
}
|
||||
|
||||
private ZlibCodec z
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_z == null)
|
||||
{
|
||||
bool wantRfc1950Header = (this._flavor == ZlibStreamFlavor.ZLIB);
|
||||
_z = new ZlibCodec();
|
||||
if (this._compressionMode == CompressionMode.Decompress)
|
||||
{
|
||||
_z.InitializeInflate(wantRfc1950Header);
|
||||
}
|
||||
else
|
||||
{
|
||||
_z.Strategy = Strategy;
|
||||
_z.InitializeDeflate(this._level, wantRfc1950Header);
|
||||
}
|
||||
}
|
||||
return _z;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private byte[] workingBuffer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_workingBuffer == null)
|
||||
_workingBuffer = new byte[_bufferSize];
|
||||
return _workingBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void Write(System.Byte[] buffer, int offset, int count)
|
||||
{
|
||||
// workitem 7159
|
||||
// calculate the CRC on the unccompressed data (before writing)
|
||||
if (crc != null)
|
||||
crc.SlurpBlock(buffer, offset, count);
|
||||
|
||||
if (_streamMode == StreamMode.Undefined)
|
||||
_streamMode = StreamMode.Writer;
|
||||
else if (_streamMode != StreamMode.Writer)
|
||||
throw new ZlibException("Cannot Write after Reading.");
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
// first reference of z property will initialize the private var _z
|
||||
z.InputBuffer = buffer;
|
||||
_z.NextIn = offset;
|
||||
_z.AvailableBytesIn = count;
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
_z.OutputBuffer = workingBuffer;
|
||||
_z.NextOut = 0;
|
||||
_z.AvailableBytesOut = _workingBuffer.Length;
|
||||
int rc = (_wantCompress)
|
||||
? _z.Deflate(_flushMode)
|
||||
: _z.Inflate(_flushMode);
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
|
||||
|
||||
//if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
|
||||
_stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
|
||||
|
||||
done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
|
||||
|
||||
// If GZIP and de-compress, we're done when 8 bytes remain.
|
||||
if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
|
||||
done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
|
||||
|
||||
}
|
||||
while (!done);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void finish()
|
||||
{
|
||||
if (_z == null) return;
|
||||
|
||||
if (_streamMode == StreamMode.Writer)
|
||||
{
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
_z.OutputBuffer = workingBuffer;
|
||||
_z.NextOut = 0;
|
||||
_z.AvailableBytesOut = _workingBuffer.Length;
|
||||
int rc = (_wantCompress)
|
||||
? _z.Deflate(FlushType.Finish)
|
||||
: _z.Inflate(FlushType.Finish);
|
||||
|
||||
if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
{
|
||||
string verb = (_wantCompress ? "de" : "in") + "flating";
|
||||
if (_z.Message == null)
|
||||
throw new ZlibException(String.Format("{0}: (rc = {1})", verb, rc));
|
||||
else
|
||||
throw new ZlibException(verb + ": " + _z.Message);
|
||||
}
|
||||
|
||||
if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
|
||||
{
|
||||
_stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
|
||||
}
|
||||
|
||||
done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
|
||||
// If GZIP and de-compress, we're done when 8 bytes remain.
|
||||
if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
|
||||
done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
|
||||
|
||||
}
|
||||
while (!done);
|
||||
|
||||
Flush();
|
||||
|
||||
// workitem 7159
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
if (_wantCompress)
|
||||
{
|
||||
// Emit the GZIP trailer: CRC32 and size mod 2^32
|
||||
int c1 = crc.Crc32Result;
|
||||
_stream.Write(BitConverter.GetBytes(c1), 0, 4);
|
||||
int c2 = (Int32)(crc.TotalBytesRead & 0x00000000FFFFFFFF);
|
||||
_stream.Write(BitConverter.GetBytes(c2), 0, 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ZlibException("Writing with decompression is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
// workitem 7159
|
||||
else if (_streamMode == StreamMode.Reader)
|
||||
{
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
if (!_wantCompress)
|
||||
{
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (_z.TotalBytesOut == 0L)
|
||||
return;
|
||||
|
||||
// Read and potentially verify the GZIP trailer:
|
||||
// CRC32 and size mod 2^32
|
||||
byte[] trailer = new byte[8];
|
||||
|
||||
// workitems 8679 & 12554
|
||||
if (_z.AvailableBytesIn < 8)
|
||||
{
|
||||
// Make sure we have read to the end of the stream
|
||||
Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, _z.AvailableBytesIn);
|
||||
int bytesNeeded = 8 - _z.AvailableBytesIn;
|
||||
int bytesRead = _stream.Read(trailer,
|
||||
_z.AvailableBytesIn,
|
||||
bytesNeeded);
|
||||
if (bytesNeeded != bytesRead)
|
||||
{
|
||||
throw new ZlibException(String.Format("Missing or incomplete GZIP trailer. Expected 8 bytes, got {0}.",
|
||||
_z.AvailableBytesIn + bytesRead));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(_z.InputBuffer, _z.NextIn, trailer, 0, trailer.Length);
|
||||
}
|
||||
|
||||
Int32 crc32_expected = BitConverter.ToInt32(trailer, 0);
|
||||
Int32 crc32_actual = crc.Crc32Result;
|
||||
Int32 isize_expected = BitConverter.ToInt32(trailer, 4);
|
||||
Int32 isize_actual = (Int32)(_z.TotalBytesOut & 0x00000000FFFFFFFF);
|
||||
|
||||
if (crc32_actual != crc32_expected)
|
||||
throw new ZlibException(String.Format("Bad CRC32 in GZIP trailer. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected));
|
||||
|
||||
if (isize_actual != isize_expected)
|
||||
throw new ZlibException(String.Format("Bad size in GZIP trailer. (actual({0})!=expected({1}))", isize_actual, isize_expected));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ZlibException("Reading with compression is not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void end()
|
||||
{
|
||||
if (z == null)
|
||||
return;
|
||||
if (_wantCompress)
|
||||
{
|
||||
_z.EndDeflate();
|
||||
}
|
||||
else
|
||||
{
|
||||
_z.EndInflate();
|
||||
}
|
||||
_z = null;
|
||||
}
|
||||
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_stream == null) return;
|
||||
try
|
||||
{
|
||||
finish();
|
||||
}
|
||||
finally
|
||||
{
|
||||
end();
|
||||
if (!_leaveOpen) _stream.Close();
|
||||
_stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
_stream.Flush();
|
||||
}
|
||||
|
||||
public override System.Int64 Seek(System.Int64 offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//_outStream.Seek(offset, origin);
|
||||
}
|
||||
public override void SetLength(System.Int64 value)
|
||||
{
|
||||
_stream.SetLength(value);
|
||||
}
|
||||
|
||||
|
||||
#if NOT
|
||||
public int Read()
|
||||
{
|
||||
if (Read(_buf1, 0, 1) == 0)
|
||||
return 0;
|
||||
// calculate CRC after reading
|
||||
if (crc!=null)
|
||||
crc.SlurpBlock(_buf1,0,1);
|
||||
return (_buf1[0] & 0xFF);
|
||||
}
|
||||
#endif
|
||||
|
||||
private bool nomoreinput = false;
|
||||
|
||||
|
||||
|
||||
private string ReadZeroTerminatedString()
|
||||
{
|
||||
var list = new System.Collections.Generic.List<byte>();
|
||||
bool done = false;
|
||||
do
|
||||
{
|
||||
// workitem 7740
|
||||
int n = _stream.Read(_buf1, 0, 1);
|
||||
if (n != 1)
|
||||
throw new ZlibException("Unexpected EOF reading GZIP header.");
|
||||
else
|
||||
{
|
||||
if (_buf1[0] == 0)
|
||||
done = true;
|
||||
else
|
||||
list.Add(_buf1[0]);
|
||||
}
|
||||
} while (!done);
|
||||
byte[] a = list.ToArray();
|
||||
return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
|
||||
}
|
||||
|
||||
|
||||
private int _ReadAndValidateGzipHeader()
|
||||
{
|
||||
int totalBytesRead = 0;
|
||||
// read the header on the first read
|
||||
byte[] header = new byte[10];
|
||||
int n = _stream.Read(header, 0, header.Length);
|
||||
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
if (n != 10)
|
||||
throw new ZlibException("Not a valid GZIP stream.");
|
||||
|
||||
if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
|
||||
throw new ZlibException("Bad GZIP header.");
|
||||
|
||||
Int32 timet = BitConverter.ToInt32(header, 4);
|
||||
_GzipMtime = GZipStream._unixEpoch.AddSeconds(timet);
|
||||
totalBytesRead += n;
|
||||
if ((header[3] & 0x04) == 0x04)
|
||||
{
|
||||
// read and discard extra field
|
||||
n = _stream.Read(header, 0, 2); // 2-byte length field
|
||||
totalBytesRead += n;
|
||||
|
||||
Int16 extraLength = (Int16)(header[0] + header[1] * 256);
|
||||
byte[] extra = new byte[extraLength];
|
||||
n = _stream.Read(extra, 0, extra.Length);
|
||||
if (n != extraLength)
|
||||
throw new ZlibException("Unexpected end-of-file reading GZIP header.");
|
||||
totalBytesRead += n;
|
||||
}
|
||||
if ((header[3] & 0x08) == 0x08)
|
||||
_GzipFileName = ReadZeroTerminatedString();
|
||||
if ((header[3] & 0x10) == 0x010)
|
||||
_GzipComment = ReadZeroTerminatedString();
|
||||
if ((header[3] & 0x02) == 0x02)
|
||||
Read(_buf1, 0, 1); // CRC16, ignore
|
||||
|
||||
return totalBytesRead;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override System.Int32 Read(System.Byte[] buffer, System.Int32 offset, System.Int32 count)
|
||||
{
|
||||
// According to MS documentation, any implementation of the IO.Stream.Read function must:
|
||||
// (a) throw an exception if offset & count reference an invalid part of the buffer,
|
||||
// or if count < 0, or if buffer is null
|
||||
// (b) return 0 only upon EOF, or if count = 0
|
||||
// (c) if not EOF, then return at least 1 byte, up to <count> bytes
|
||||
|
||||
if (_streamMode == StreamMode.Undefined)
|
||||
{
|
||||
if (!this._stream.CanRead) throw new ZlibException("The stream is not readable.");
|
||||
// for the first read, set up some controls.
|
||||
_streamMode = StreamMode.Reader;
|
||||
// (The first reference to _z goes through the private accessor which
|
||||
// may initialize it.)
|
||||
z.AvailableBytesIn = 0;
|
||||
if (_flavor == ZlibStreamFlavor.GZIP)
|
||||
{
|
||||
_gzipHeaderByteCount = _ReadAndValidateGzipHeader();
|
||||
// workitem 8501: handle edge case (decompress empty stream)
|
||||
if (_gzipHeaderByteCount == 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (_streamMode != StreamMode.Reader)
|
||||
throw new ZlibException("Cannot Read after Writing.");
|
||||
|
||||
if (count == 0) return 0;
|
||||
if (nomoreinput && _wantCompress) return 0; // workitem 8557
|
||||
if (buffer == null) throw new ArgumentNullException("buffer");
|
||||
if (count < 0) throw new ArgumentOutOfRangeException("count");
|
||||
if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
|
||||
if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
int rc = 0;
|
||||
|
||||
// set up the output of the deflate/inflate codec:
|
||||
_z.OutputBuffer = buffer;
|
||||
_z.NextOut = offset;
|
||||
_z.AvailableBytesOut = count;
|
||||
|
||||
// This is necessary in case _workingBuffer has been resized. (new byte[])
|
||||
// (The first reference to _workingBuffer goes through the private accessor which
|
||||
// may initialize it.)
|
||||
_z.InputBuffer = workingBuffer;
|
||||
|
||||
do
|
||||
{
|
||||
// need data in _workingBuffer in order to deflate/inflate. Here, we check if we have any.
|
||||
if ((_z.AvailableBytesIn == 0) && (!nomoreinput))
|
||||
{
|
||||
// No data available, so try to Read data from the captive stream.
|
||||
_z.NextIn = 0;
|
||||
_z.AvailableBytesIn = _stream.Read(_workingBuffer, 0, _workingBuffer.Length);
|
||||
if (_z.AvailableBytesIn == 0)
|
||||
nomoreinput = true;
|
||||
|
||||
}
|
||||
// we have data in InputBuffer; now compress or decompress as appropriate
|
||||
rc = (_wantCompress)
|
||||
? _z.Deflate(_flushMode)
|
||||
: _z.Inflate(_flushMode);
|
||||
|
||||
if (nomoreinput && (rc == ZlibConstants.Z_BUF_ERROR))
|
||||
return 0;
|
||||
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException(String.Format("{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, _z.Message));
|
||||
|
||||
if ((nomoreinput || rc == ZlibConstants.Z_STREAM_END) && (_z.AvailableBytesOut == count))
|
||||
break; // nothing more to read
|
||||
}
|
||||
//while (_z.AvailableBytesOut == count && rc == ZlibConstants.Z_OK);
|
||||
while (_z.AvailableBytesOut > 0 && !nomoreinput && rc == ZlibConstants.Z_OK);
|
||||
|
||||
|
||||
// workitem 8557
|
||||
// is there more room in output?
|
||||
if (_z.AvailableBytesOut > 0)
|
||||
{
|
||||
if (rc == ZlibConstants.Z_OK && _z.AvailableBytesIn == 0)
|
||||
{
|
||||
// deferred
|
||||
}
|
||||
|
||||
// are we completely done reading?
|
||||
if (nomoreinput)
|
||||
{
|
||||
// and in compression?
|
||||
if (_wantCompress)
|
||||
{
|
||||
// no more input data available; therefore we flush to
|
||||
// try to complete the read
|
||||
rc = _z.Deflate(FlushType.Finish);
|
||||
|
||||
if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
throw new ZlibException(String.Format("Deflating: rc={0} msg={1}", rc, _z.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rc = (count - _z.AvailableBytesOut);
|
||||
|
||||
// calculate CRC after reading
|
||||
if (crc != null)
|
||||
crc.SlurpBlock(buffer, offset, rc);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override System.Boolean CanRead
|
||||
{
|
||||
get { return this._stream.CanRead; }
|
||||
}
|
||||
|
||||
public override System.Boolean CanSeek
|
||||
{
|
||||
get { return this._stream.CanSeek; }
|
||||
}
|
||||
|
||||
public override System.Boolean CanWrite
|
||||
{
|
||||
get { return this._stream.CanWrite; }
|
||||
}
|
||||
|
||||
public override System.Int64 Length
|
||||
{
|
||||
get { return _stream.Length; }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
internal enum StreamMode
|
||||
{
|
||||
Writer,
|
||||
Reader,
|
||||
Undefined,
|
||||
}
|
||||
|
||||
|
||||
public static void CompressString(String s, Stream compressor)
|
||||
{
|
||||
byte[] uncompressed = System.Text.Encoding.UTF8.GetBytes(s);
|
||||
using (compressor)
|
||||
{
|
||||
compressor.Write(uncompressed, 0, uncompressed.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CompressBuffer(byte[] b, Stream compressor)
|
||||
{
|
||||
// workitem 8460
|
||||
using (compressor)
|
||||
{
|
||||
compressor.Write(b, 0, b.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static String UncompressString(byte[] compressed, Stream decompressor)
|
||||
{
|
||||
// workitem 8460
|
||||
byte[] working = new byte[1024];
|
||||
var encoding = System.Text.Encoding.UTF8;
|
||||
using (var output = new MemoryStream())
|
||||
{
|
||||
using (decompressor)
|
||||
{
|
||||
int n;
|
||||
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
|
||||
{
|
||||
output.Write(working, 0, n);
|
||||
}
|
||||
}
|
||||
|
||||
// reset to allow read from start
|
||||
output.Seek(0, SeekOrigin.Begin);
|
||||
var sr = new StreamReader(output, encoding);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] UncompressBuffer(byte[] compressed, Stream decompressor)
|
||||
{
|
||||
// workitem 8460
|
||||
byte[] working = new byte[1024];
|
||||
using (var output = new MemoryStream())
|
||||
{
|
||||
using (decompressor)
|
||||
{
|
||||
int n;
|
||||
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
|
||||
{
|
||||
output.Write(working, 0, n);
|
||||
}
|
||||
}
|
||||
return output.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the dictionary to be used for either Inflation or Deflation.
|
||||
/// </summary>
|
||||
/// <param name="dictionary">The dictionary bytes to use.</param>
|
||||
/// <param name="check">Determines if dictionary checks are run</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDictionary(byte[] dictionary, bool check = true)
|
||||
{
|
||||
return z.SetDictionary(dictionary, check);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
720
SabreTools.Compression/Deflate/ZlibCodec.cs
Normal file
720
SabreTools.Compression/Deflate/ZlibCodec.cs
Normal file
@@ -0,0 +1,720 @@
|
||||
// ZlibCodec.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-November-03 15:40:51>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines a Codec for ZLIB compression and
|
||||
// decompression. This code extends code that was based the jzlib
|
||||
// implementation of zlib, but this code is completely novel. The codec
|
||||
// class is new, and encapsulates some behaviors that are new, and some
|
||||
// that were present in other classes in the jzlib code base. In
|
||||
// keeping with the license for jzlib, the copyright to the jzlib code
|
||||
// is included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
using Interop=System.Runtime.InteropServices;
|
||||
|
||||
#nullable disable
|
||||
#pragma warning disable CS0618
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// This class compresses and decompresses data according to the Deflate algorithm
|
||||
/// and optionally, the ZLIB format, as documented in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950 - ZLIB</see> and <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE</see>.
|
||||
/// </remarks>
|
||||
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000D")]
|
||||
[Interop.ComVisible(true)]
|
||||
#if !NETCF
|
||||
[Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
|
||||
#endif
|
||||
sealed public class ZlibCodec
|
||||
{
|
||||
/// <summary>
|
||||
/// The buffer from which data is taken.
|
||||
/// </summary>
|
||||
public byte[] InputBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// An index into the InputBuffer array, indicating where to start reading.
|
||||
/// </summary>
|
||||
public int NextIn;
|
||||
|
||||
/// <summary>
|
||||
/// The number of bytes available in the InputBuffer, starting at NextIn.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call.
|
||||
/// The class will update this number as calls to Inflate/Deflate are made.
|
||||
/// </remarks>
|
||||
public int AvailableBytesIn;
|
||||
|
||||
/// <summary>
|
||||
/// Total number of bytes read so far, through all calls to Inflate()/Deflate().
|
||||
/// </summary>
|
||||
public long TotalBytesIn;
|
||||
|
||||
/// <summary>
|
||||
/// Buffer to store output data.
|
||||
/// </summary>
|
||||
public byte[] OutputBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// An index into the OutputBuffer array, indicating where to start writing.
|
||||
/// </summary>
|
||||
public int NextOut;
|
||||
|
||||
/// <summary>
|
||||
/// The number of bytes available in the OutputBuffer, starting at NextOut.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call.
|
||||
/// The class will update this number as calls to Inflate/Deflate are made.
|
||||
/// </remarks>
|
||||
public int AvailableBytesOut;
|
||||
|
||||
/// <summary>
|
||||
/// Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
|
||||
/// </summary>
|
||||
public long TotalBytesOut;
|
||||
|
||||
/// <summary>
|
||||
/// used for diagnostics, when something goes wrong!
|
||||
/// </summary>
|
||||
public System.String Message;
|
||||
|
||||
internal DeflateManager dstate;
|
||||
internal InflateManager istate;
|
||||
|
||||
internal uint _Adler32;
|
||||
|
||||
/// <summary>
|
||||
/// The compression level to use in this codec. Useful only in compression mode.
|
||||
/// </summary>
|
||||
public CompressionLevel CompressLevel = CompressionLevel.Default;
|
||||
|
||||
/// <summary>
|
||||
/// The number of Window Bits to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This gauges the size of the sliding window, and hence the
|
||||
/// compression effectiveness as well as memory consumption. It's best to just leave this
|
||||
/// setting alone if you don't know what it is. The maximum value is 15 bits, which implies
|
||||
/// a 32k window.
|
||||
/// </remarks>
|
||||
public int WindowBits = ZlibConstants.WindowBitsDefault;
|
||||
|
||||
/// <summary>
|
||||
/// The compression strategy to use.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is only effective in compression. The theory offered by ZLIB is that different
|
||||
/// strategies could potentially produce significant differences in compression behavior
|
||||
/// for different data sets. Unfortunately I don't have any good recommendations for how
|
||||
/// to set it differently. When I tested changing the strategy I got minimally different
|
||||
/// compression performance. It's best to leave this property alone if you don't have a
|
||||
/// good feel for it. Or, you may want to produce a test harness that runs through the
|
||||
/// different strategy options and evaluates them on different file types. If you do that,
|
||||
/// let me know your results.
|
||||
/// </remarks>
|
||||
public CompressionStrategy Strategy = CompressionStrategy.Default;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
|
||||
/// </summary>
|
||||
public int Adler32 { get { return (int)_Adler32; } }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create a ZlibCodec.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If you use this default constructor, you will later have to explicitly call
|
||||
/// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress
|
||||
/// or decompress.
|
||||
/// </remarks>
|
||||
public ZlibCodec() { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a ZlibCodec that either compresses or decompresses.
|
||||
/// </summary>
|
||||
/// <param name="mode">
|
||||
/// Indicates whether the codec should compress (deflate) or decompress (inflate).
|
||||
/// </param>
|
||||
public ZlibCodec(CompressionMode mode)
|
||||
{
|
||||
if (mode == CompressionMode.Compress)
|
||||
{
|
||||
int rc = InitializeDeflate();
|
||||
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for deflate.");
|
||||
}
|
||||
else if (mode == CompressionMode.Decompress)
|
||||
{
|
||||
int rc = InitializeInflate();
|
||||
if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for inflate.");
|
||||
}
|
||||
else throw new ZlibException("Invalid ZlibStreamFlavor.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// It is not necessary to call this before using the ZlibCodec to inflate data;
|
||||
/// It is implicitly called when you call the constructor.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate()
|
||||
{
|
||||
return InitializeInflate(this.WindowBits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state with an explicit flag to
|
||||
/// govern the handling of RFC1950 header bytes.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// By default, the ZLIB header defined in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950</see> is expected. If
|
||||
/// you want to read a zlib stream you should specify true for
|
||||
/// expectRfc1950Header. If you have a deflate stream, you will want to specify
|
||||
/// false. It is only necessary to invoke this initializer explicitly if you
|
||||
/// want to specify false.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte
|
||||
/// pair when reading the stream of data to be inflated.</param>
|
||||
///
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate(bool expectRfc1950Header)
|
||||
{
|
||||
return InitializeInflate(this.WindowBits, expectRfc1950Header);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for inflation, with the specified number of window bits.
|
||||
/// </summary>
|
||||
/// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
|
||||
/// then you shouldn't be calling this initializer.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeInflate(int windowBits)
|
||||
{
|
||||
this.WindowBits = windowBits;
|
||||
return InitializeInflate(windowBits, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the inflation state with an explicit flag to govern the handling of
|
||||
/// RFC1950 header bytes.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// If you want to read a zlib stream you should specify true for
|
||||
/// expectRfc1950Header. In this case, the library will expect to find a ZLIB
|
||||
/// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
|
||||
/// 1950</see>, in the compressed stream. If you will be reading a DEFLATE or
|
||||
/// GZIP stream, which does not have such a header, you will want to specify
|
||||
/// false.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte pair when reading
|
||||
/// the stream of data to be inflated.</param>
|
||||
/// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
|
||||
/// then you shouldn't be calling this initializer.</param>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int InitializeInflate(int windowBits, bool expectRfc1950Header)
|
||||
{
|
||||
this.WindowBits = windowBits;
|
||||
if (dstate != null) throw new ZlibException("You may not call InitializeInflate() after calling InitializeDeflate().");
|
||||
istate = new InflateManager(expectRfc1950Header);
|
||||
return istate.Initialize(this, windowBits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
|
||||
/// AvailableBytesOut before calling this method.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// private void InflateBuffer()
|
||||
/// {
|
||||
/// int bufferSize = 1024;
|
||||
/// byte[] buffer = new byte[bufferSize];
|
||||
/// ZlibCodec decompressor = new ZlibCodec();
|
||||
///
|
||||
/// Console.WriteLine("\n============================================");
|
||||
/// Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length);
|
||||
/// MemoryStream ms = new MemoryStream(DecompressedBytes);
|
||||
///
|
||||
/// int rc = decompressor.InitializeInflate();
|
||||
///
|
||||
/// decompressor.InputBuffer = CompressedBytes;
|
||||
/// decompressor.NextIn = 0;
|
||||
/// decompressor.AvailableBytesIn = CompressedBytes.Length;
|
||||
///
|
||||
/// decompressor.OutputBuffer = buffer;
|
||||
///
|
||||
/// // pass 1: inflate
|
||||
/// do
|
||||
/// {
|
||||
/// decompressor.NextOut = 0;
|
||||
/// decompressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = decompressor.Inflate(FlushType.None);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
/// throw new Exception("inflating: " + decompressor.Message);
|
||||
///
|
||||
/// ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// // pass 2: finish and flush
|
||||
/// do
|
||||
/// {
|
||||
/// decompressor.NextOut = 0;
|
||||
/// decompressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = decompressor.Inflate(FlushType.Finish);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
/// throw new Exception("inflating: " + decompressor.Message);
|
||||
///
|
||||
/// if (buffer.Length - decompressor.AvailableBytesOut > 0)
|
||||
/// ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// decompressor.EndInflate();
|
||||
/// }
|
||||
///
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="flush">The flush to use when inflating.</param>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int Inflate(FlushType flush)
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
return istate.Inflate(flush);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Ends an inflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this after successively calling Inflate(). This will cause all buffers to be flushed.
|
||||
/// After calling this you cannot call Inflate() without a intervening call to one of the
|
||||
/// InitializeInflate() overloads.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int EndInflate()
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
int ret = istate.End();
|
||||
istate = null;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I don't know what this does!
|
||||
/// </summary>
|
||||
/// <returns>Z_OK if everything goes well.</returns>
|
||||
public int SyncInflate()
|
||||
{
|
||||
if (istate == null)
|
||||
throw new ZlibException("No Inflate State!");
|
||||
return istate.Sync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the MAX window bits and the default level of compression.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// int bufferSize = 40000;
|
||||
/// byte[] CompressedBytes = new byte[bufferSize];
|
||||
/// byte[] DecompressedBytes = new byte[bufferSize];
|
||||
///
|
||||
/// ZlibCodec compressor = new ZlibCodec();
|
||||
///
|
||||
/// compressor.InitializeDeflate(CompressionLevel.Default);
|
||||
///
|
||||
/// compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
|
||||
/// compressor.NextIn = 0;
|
||||
/// compressor.AvailableBytesIn = compressor.InputBuffer.Length;
|
||||
///
|
||||
/// compressor.OutputBuffer = CompressedBytes;
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = CompressedBytes.Length;
|
||||
///
|
||||
/// while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize)
|
||||
/// {
|
||||
/// compressor.Deflate(FlushType.None);
|
||||
/// }
|
||||
///
|
||||
/// while (true)
|
||||
/// {
|
||||
/// int rc= compressor.Deflate(FlushType.Finish);
|
||||
/// if (rc == ZlibConstants.Z_STREAM_END) break;
|
||||
/// }
|
||||
///
|
||||
/// compressor.EndDeflate();
|
||||
///
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <returns>Z_OK if all goes well. You generally don't need to check the return code.</returns>
|
||||
public int InitializeDeflate()
|
||||
{
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the maximum window bits (15) and the specified
|
||||
/// CompressionLevel. It will emit a ZLIB stream as it compresses.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
|
||||
/// and the explicit flag governing whether to emit an RFC1950 header byte pair.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the maximum window bits (15) and the specified CompressionLevel.
|
||||
/// If you want to generate a zlib stream, you should specify true for
|
||||
/// wantRfc1950Header. In this case, the library will emit a ZLIB
|
||||
/// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
|
||||
/// 1950</see>, in the compressed stream.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, bool wantRfc1950Header)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
return _InternalInitializeDeflate(wantRfc1950Header);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
|
||||
/// and the specified number of window bits.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The codec will use the specified number of window bits and the specified CompressionLevel.
|
||||
/// </remarks>
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, int bits)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
this.WindowBits = bits;
|
||||
return _InternalInitializeDeflate(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the ZlibCodec for deflation operation, using the specified
|
||||
/// CompressionLevel, the specified number of window bits, and the explicit flag
|
||||
/// governing whether to emit an RFC1950 header byte pair.
|
||||
/// </summary>
|
||||
///
|
||||
/// <param name="level">The compression level for the codec.</param>
|
||||
/// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
|
||||
/// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int InitializeDeflate(CompressionLevel level, int bits, bool wantRfc1950Header)
|
||||
{
|
||||
this.CompressLevel = level;
|
||||
this.WindowBits = bits;
|
||||
return _InternalInitializeDeflate(wantRfc1950Header);
|
||||
}
|
||||
|
||||
private int _InternalInitializeDeflate(bool wantRfc1950Header)
|
||||
{
|
||||
if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
|
||||
dstate = new DeflateManager();
|
||||
dstate.WantRfc1950HeaderBytes = wantRfc1950Header;
|
||||
|
||||
return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deflate one batch of data.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You must have set InputBuffer and OutputBuffer before calling this method.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// private void DeflateBuffer(CompressionLevel level)
|
||||
/// {
|
||||
/// int bufferSize = 1024;
|
||||
/// byte[] buffer = new byte[bufferSize];
|
||||
/// ZlibCodec compressor = new ZlibCodec();
|
||||
///
|
||||
/// Console.WriteLine("\n============================================");
|
||||
/// Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length);
|
||||
/// MemoryStream ms = new MemoryStream();
|
||||
///
|
||||
/// int rc = compressor.InitializeDeflate(level);
|
||||
///
|
||||
/// compressor.InputBuffer = UncompressedBytes;
|
||||
/// compressor.NextIn = 0;
|
||||
/// compressor.AvailableBytesIn = UncompressedBytes.Length;
|
||||
///
|
||||
/// compressor.OutputBuffer = buffer;
|
||||
///
|
||||
/// // pass 1: deflate
|
||||
/// do
|
||||
/// {
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = compressor.Deflate(FlushType.None);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
|
||||
/// throw new Exception("deflating: " + compressor.Message);
|
||||
///
|
||||
/// ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// // pass 2: finish and flush
|
||||
/// do
|
||||
/// {
|
||||
/// compressor.NextOut = 0;
|
||||
/// compressor.AvailableBytesOut = buffer.Length;
|
||||
/// rc = compressor.Deflate(FlushType.Finish);
|
||||
///
|
||||
/// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
|
||||
/// throw new Exception("deflating: " + compressor.Message);
|
||||
///
|
||||
/// if (buffer.Length - compressor.AvailableBytesOut > 0)
|
||||
/// ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
|
||||
/// }
|
||||
/// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
|
||||
///
|
||||
/// compressor.EndDeflate();
|
||||
///
|
||||
/// ms.Seek(0, SeekOrigin.Begin);
|
||||
/// CompressedBytes = new byte[compressor.TotalBytesOut];
|
||||
/// ms.Read(CompressedBytes, 0, CompressedBytes.Length);
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <param name="flush">whether to flush all data as you deflate. Generally you will want to
|
||||
/// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to
|
||||
/// flush everything.
|
||||
/// </param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int Deflate(FlushType flush)
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
return dstate.Deflate(flush);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// End a deflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this after making a series of one or more calls to Deflate(). All buffers are flushed.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int EndDeflate()
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
// TODO: dinoch Tue, 03 Nov 2009 15:39 (test this)
|
||||
//int ret = dstate.End();
|
||||
dstate = null;
|
||||
return ZlibConstants.Z_OK; //ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset a codec for another deflation session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this to reset the deflation state. For example if a thread is deflating
|
||||
/// non-consecutive blocks, you can call Reset() after the Deflate(Sync) of the first
|
||||
/// block and before the next Deflate(None) of the second block.
|
||||
/// </remarks>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public void ResetDeflate()
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
dstate.Reset();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set the CompressionStrategy and CompressionLevel for a deflation session.
|
||||
/// </summary>
|
||||
/// <param name="level">the level of compression to use.</param>
|
||||
/// <param name="strategy">the strategy to use for compression.</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDeflateParams(CompressionLevel level, CompressionStrategy strategy)
|
||||
{
|
||||
if (dstate == null)
|
||||
throw new ZlibException("No Deflate State!");
|
||||
return dstate.SetParams(level, strategy);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set the dictionary to be used for either Inflation or Deflation.
|
||||
/// </summary>
|
||||
/// <param name="dictionary">The dictionary bytes to use.</param>
|
||||
/// <param name="check">Determines if dictionary checks are run</param>
|
||||
/// <returns>Z_OK if all goes well.</returns>
|
||||
public int SetDictionary(byte[] dictionary, bool check = true)
|
||||
{
|
||||
if (istate != null)
|
||||
return istate.SetDictionary(dictionary, check);
|
||||
|
||||
if (dstate != null)
|
||||
return dstate.SetDictionary(dictionary, check);
|
||||
|
||||
throw new ZlibException("No Inflate or Deflate state!");
|
||||
}
|
||||
|
||||
// Flush as much pending output as possible. All deflate() output goes
|
||||
// through this function so some applications may wish to modify it
|
||||
// to avoid allocating a large strm->next_out buffer and copying into it.
|
||||
// (See also read_buf()).
|
||||
internal void flush_pending()
|
||||
{
|
||||
int len = dstate.pendingCount;
|
||||
|
||||
if (len > AvailableBytesOut)
|
||||
len = AvailableBytesOut;
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
if (dstate.pending.Length <= dstate.nextPending ||
|
||||
OutputBuffer.Length <= NextOut ||
|
||||
dstate.pending.Length < (dstate.nextPending + len) ||
|
||||
OutputBuffer.Length < (NextOut + len))
|
||||
{
|
||||
throw new ZlibException(String.Format("Invalid State. (pending.Length={0}, pendingCount={1})",
|
||||
dstate.pending.Length, dstate.pendingCount));
|
||||
}
|
||||
|
||||
Array.Copy(dstate.pending, dstate.nextPending, OutputBuffer, NextOut, len);
|
||||
|
||||
NextOut += len;
|
||||
dstate.nextPending += len;
|
||||
TotalBytesOut += len;
|
||||
AvailableBytesOut -= len;
|
||||
dstate.pendingCount -= len;
|
||||
if (dstate.pendingCount == 0)
|
||||
{
|
||||
dstate.nextPending = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Read a new buffer from the current input stream, update the adler32
|
||||
// and total number of bytes read. All deflate() input goes through
|
||||
// this function so some applications may wish to modify it to avoid
|
||||
// allocating a large strm->next_in buffer and copying from it.
|
||||
// (See also flush_pending()).
|
||||
internal int read_buf(byte[] buf, int start, int size)
|
||||
{
|
||||
int len = AvailableBytesIn;
|
||||
|
||||
if (len > size)
|
||||
len = size;
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
AvailableBytesIn -= len;
|
||||
|
||||
if (dstate.WantRfc1950HeaderBytes)
|
||||
{
|
||||
_Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
|
||||
}
|
||||
Array.Copy(InputBuffer, NextIn, buf, start, len);
|
||||
NextIn += len;
|
||||
TotalBytesIn += len;
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
128
SabreTools.Compression/Deflate/ZlibConstants.cs
Normal file
128
SabreTools.Compression/Deflate/ZlibConstants.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
// ZlibConstants.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2009-November-03 18:50:19>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines constants used by the zlib class library. This
|
||||
// code is derived from the jzlib implementation of zlib, but
|
||||
// significantly modified. In keeping with the license for jzlib, the
|
||||
// copyright to that code is included here.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// This program is based on zlib-1.1.3; credit to authors
|
||||
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
|
||||
// and contributors of zlib.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
using System;
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// A bunch of constants used in the Zlib interface.
|
||||
/// </summary>
|
||||
public static class ZlibConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum number of window bits for the Deflate algorithm.
|
||||
/// </summary>
|
||||
public const int WindowBitsMax = 15; // 32K LZ77 window
|
||||
|
||||
/// <summary>
|
||||
/// The default number of window bits for the Deflate algorithm.
|
||||
/// </summary>
|
||||
public const int WindowBitsDefault = WindowBitsMax;
|
||||
|
||||
/// <summary>
|
||||
/// indicates everything is A-OK
|
||||
/// </summary>
|
||||
public const int Z_OK = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the last operation reached the end of the stream.
|
||||
/// </summary>
|
||||
public const int Z_STREAM_END = 1;
|
||||
|
||||
/// <summary>
|
||||
/// The operation ended in need of a dictionary.
|
||||
/// </summary>
|
||||
public const int Z_NEED_DICT = 2;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the stream - not enough data, not open and readable, etc.
|
||||
/// </summary>
|
||||
public const int Z_STREAM_ERROR = -2;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the data - not enough data, bad data, etc.
|
||||
/// </summary>
|
||||
public const int Z_DATA_ERROR = -3;
|
||||
|
||||
/// <summary>
|
||||
/// There was an error with the working buffer.
|
||||
/// </summary>
|
||||
public const int Z_BUF_ERROR = -5;
|
||||
|
||||
/// <summary>
|
||||
/// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
|
||||
/// </summary>
|
||||
#if NETCF
|
||||
public const int WorkingBufferSizeDefault = 8192;
|
||||
#else
|
||||
public const int WorkingBufferSizeDefault = 16384;
|
||||
#endif
|
||||
/// <summary>
|
||||
/// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
|
||||
/// </summary>
|
||||
public const int WorkingBufferSizeMin = 1024;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
120
SabreTools.Compression/Deflate/ZlibException.cs
Normal file
120
SabreTools.Compression/Deflate/ZlibException.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
// Zlib.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Last Saved: <2011-August-03 19:52:28>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines classes for ZLIB compression and
|
||||
// decompression. This code is derived from the jzlib implementation of
|
||||
// zlib, but significantly modified. The object model is not the same,
|
||||
// and many of the behaviors are new or different. Nonetheless, in
|
||||
// keeping with the license for jzlib, the copyright to that code is
|
||||
// included below.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// The following notice applies to jzlib:
|
||||
//
|
||||
// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in
|
||||
// the documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of the authors may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
||||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
||||
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// jzlib is based on zlib-1.1.3.
|
||||
//
|
||||
// The following notice applies to zlib:
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
||||
//
|
||||
// The ZLIB software is provided 'as-is', without any express or implied
|
||||
// warranty. In no event will the authors be held liable for any damages
|
||||
// arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it
|
||||
// freely, subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented; you must not
|
||||
// claim that you wrote the original software. If you use this software
|
||||
// in a product, an acknowledgment in the product documentation would be
|
||||
// appreciated but is not required.
|
||||
// 2. Altered source versions must be plainly marked as such, and must not be
|
||||
// misrepresented as being the original software.
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
// Jean-loup Gailly jloup@gzip.org
|
||||
// Mark Adler madler@alumni.caltech.edu
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
using Interop = System.Runtime.InteropServices;
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
/// <summary>
|
||||
/// A general purpose exception class for exceptions in the Zlib library.
|
||||
/// </summary>
|
||||
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
|
||||
public class ZlibException : System.Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// The ZlibException class captures exception information generated
|
||||
/// by the Zlib library.
|
||||
/// </summary>
|
||||
public ZlibException()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This ctor collects a message attached to the exception.
|
||||
/// </summary>
|
||||
/// <param name="s">the message for the exception.</param>
|
||||
public ZlibException(System.String s)
|
||||
: base(s)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
725
SabreTools.Compression/Deflate/ZlibStream.cs
Normal file
725
SabreTools.Compression/Deflate/ZlibStream.cs
Normal file
@@ -0,0 +1,725 @@
|
||||
// ZlibStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-July-31 14:53:33>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the ZlibStream class, which is similar in idea to
|
||||
// the System.IO.Compression.DeflateStream and
|
||||
// System.IO.Compression.GZipStream classes in the .NET BCL.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Zlib stream for compression or decompression.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// The ZlibStream is a <see
|
||||
/// href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator</see> on a <see
|
||||
/// cref="System.IO.Stream"/>. It adds ZLIB compression or decompression to any
|
||||
/// stream.
|
||||
/// </para>
|
||||
///
|
||||
/// <para> Using this stream, applications can compress or decompress data via
|
||||
/// stream <c>Read()</c> and <c>Write()</c> operations. Either compresssion or
|
||||
/// decompression can occur through either reading or writing. The compression
|
||||
/// format used is ZLIB, which is documented in <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1950.txt">IETF RFC 1950</see>, "ZLIB Compressed
|
||||
/// Data Format Specification version 3.3". This implementation of ZLIB always uses
|
||||
/// DEFLATE as the compression method. (see <see
|
||||
/// href="http://www.ietf.org/rfc/rfc1951.txt">IETF RFC 1951</see>, "DEFLATE
|
||||
/// Compressed Data Format Specification version 1.3.") </para>
|
||||
///
|
||||
/// <para>
|
||||
/// The ZLIB format allows for varying compression methods, window sizes, and dictionaries.
|
||||
/// This implementation always uses the DEFLATE compression method, a preset dictionary,
|
||||
/// and 15 window bits by default.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This class is similar to <see cref="DeflateStream"/>, except that it adds the
|
||||
/// RFC1950 header and trailer bytes to a compressed stream when compressing, or expects
|
||||
/// the RFC1950 header and trailer bytes when decompressing. It is also similar to the
|
||||
/// <see cref="GZipStream"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <seealso cref="DeflateStream" />
|
||||
/// <seealso cref="GZipStream" />
|
||||
public class ZlibStream : System.IO.Stream
|
||||
{
|
||||
internal ZlibBaseStream _baseStream;
|
||||
bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>ZlibStream</c> using the specified <c>CompressionMode</c>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Compress</c>, the <c>ZlibStream</c>
|
||||
/// will use the default compression level. The "captive" stream will be
|
||||
/// closed when the <c>ZlibStream</c> is closed.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
/// This example uses a <c>ZlibStream</c> to compress a file, and writes the
|
||||
/// compressed data to another file.
|
||||
/// <code>
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (var raw = System.IO.File.Create(fileToCompress + ".zlib"))
|
||||
/// {
|
||||
/// using (Stream compressor = new ZlibStream(raw, CompressionMode.Compress))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n;
|
||||
/// while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
|
||||
/// {
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// <code lang="VB">
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using raw As FileStream = File.Create(fileToCompress & ".zlib")
|
||||
/// Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
///
|
||||
/// <param name="stream">The stream which will be read or written.</param>
|
||||
/// <param name="mode">Indicates whether the ZlibStream will compress or decompress.</param>
|
||||
public ZlibStream(System.IO.Stream stream, CompressionMode mode)
|
||||
: this(stream, mode, CompressionLevel.Default, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>ZlibStream</c> using the specified <c>CompressionMode</c> and
|
||||
/// the specified <c>CompressionLevel</c>.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Decompress</c>, the level parameter is ignored.
|
||||
/// The "captive" stream will be closed when the <c>ZlibStream</c> is closed.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
/// This example uses a <c>ZlibStream</c> to compress data from a file, and writes the
|
||||
/// compressed data to another file.
|
||||
///
|
||||
/// <code>
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (var raw = System.IO.File.Create(fileToCompress + ".zlib"))
|
||||
/// {
|
||||
/// using (Stream compressor = new ZlibStream(raw,
|
||||
/// CompressionMode.Compress,
|
||||
/// CompressionLevel.BestCompression))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n;
|
||||
/// while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
|
||||
/// {
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
///
|
||||
/// <code lang="VB">
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using raw As FileStream = File.Create(fileToCompress & ".zlib")
|
||||
/// Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
///
|
||||
/// <param name="stream">The stream to be read or written while deflating or inflating.</param>
|
||||
/// <param name="mode">Indicates whether the ZlibStream will compress or decompress.</param>
|
||||
/// <param name="level">A tuning knob to trade speed for effectiveness.</param>
|
||||
public ZlibStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level)
|
||||
: this(stream, mode, level, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>ZlibStream</c> using the specified <c>CompressionMode</c>, and
|
||||
/// explicitly specify whether the captive stream should be left open after
|
||||
/// Deflation or Inflation.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Compress</c>, the <c>ZlibStream</c> will use
|
||||
/// the default compression level.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// This constructor allows the application to request that the captive stream
|
||||
/// remain open after the deflation or inflation occurs. By default, after
|
||||
/// <c>Close()</c> is called on the stream, the captive stream is also
|
||||
/// closed. In some cases this is not desired, for example if the stream is a
|
||||
/// <see cref="System.IO.MemoryStream"/> that will be re-read after
|
||||
/// compression. Specify true for the <paramref name="leaveOpen"/> parameter to leave the stream
|
||||
/// open.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// See the other overloads of this constructor for example code.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="stream">The stream which will be read or written. This is called the
|
||||
/// "captive" stream in other places in this documentation.</param>
|
||||
/// <param name="mode">Indicates whether the ZlibStream will compress or decompress.</param>
|
||||
/// <param name="leaveOpen">true if the application would like the stream to remain
|
||||
/// open after inflation/deflation.</param>
|
||||
public ZlibStream(System.IO.Stream stream, CompressionMode mode, bool leaveOpen)
|
||||
: this(stream, mode, CompressionLevel.Default, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <c>ZlibStream</c> using the specified <c>CompressionMode</c>
|
||||
/// and the specified <c>CompressionLevel</c>, and explicitly specify
|
||||
/// whether the stream should be left open after Deflation or Inflation.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// This constructor allows the application to request that the captive
|
||||
/// stream remain open after the deflation or inflation occurs. By
|
||||
/// default, after <c>Close()</c> is called on the stream, the captive
|
||||
/// stream is also closed. In some cases this is not desired, for example
|
||||
/// if the stream is a <see cref="System.IO.MemoryStream"/> that will be
|
||||
/// re-read after compression. Specify true for the <paramref
|
||||
/// name="leaveOpen"/> parameter to leave the stream open.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// When mode is <c>CompressionMode.Decompress</c>, the level parameter is
|
||||
/// ignored.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <example>
|
||||
///
|
||||
/// This example shows how to use a ZlibStream to compress the data from a file,
|
||||
/// and store the result into another file. The filestream remains open to allow
|
||||
/// additional data to be written to it.
|
||||
///
|
||||
/// <code>
|
||||
/// using (var output = System.IO.File.Create(fileToCompress + ".zlib"))
|
||||
/// {
|
||||
/// using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress))
|
||||
/// {
|
||||
/// using (Stream compressor = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true))
|
||||
/// {
|
||||
/// byte[] buffer = new byte[WORKING_BUFFER_SIZE];
|
||||
/// int n;
|
||||
/// while ((n= input.Read(buffer, 0, buffer.Length)) != 0)
|
||||
/// {
|
||||
/// compressor.Write(buffer, 0, n);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// // can write additional data to the output stream here
|
||||
/// }
|
||||
/// </code>
|
||||
/// <code lang="VB">
|
||||
/// Using output As FileStream = File.Create(fileToCompress & ".zlib")
|
||||
/// Using input As Stream = File.OpenRead(fileToCompress)
|
||||
/// Using compressor As Stream = New ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True)
|
||||
/// Dim buffer As Byte() = New Byte(4096) {}
|
||||
/// Dim n As Integer = -1
|
||||
/// Do While (n <> 0)
|
||||
/// If (n > 0) Then
|
||||
/// compressor.Write(buffer, 0, n)
|
||||
/// End If
|
||||
/// n = input.Read(buffer, 0, buffer.Length)
|
||||
/// Loop
|
||||
/// End Using
|
||||
/// End Using
|
||||
/// ' can write additional data to the output stream here.
|
||||
/// End Using
|
||||
/// </code>
|
||||
/// </example>
|
||||
///
|
||||
/// <param name="stream">The stream which will be read or written.</param>
|
||||
///
|
||||
/// <param name="mode">Indicates whether the ZlibStream will compress or decompress.</param>
|
||||
///
|
||||
/// <param name="leaveOpen">
|
||||
/// true if the application would like the stream to remain open after
|
||||
/// inflation/deflation.
|
||||
/// </param>
|
||||
///
|
||||
/// <param name="level">
|
||||
/// A tuning knob to trade speed for effectiveness. This parameter is
|
||||
/// effective only when mode is <c>CompressionMode.Compress</c>.
|
||||
/// </param>
|
||||
public ZlibStream(System.IO.Stream stream, CompressionMode mode, CompressionLevel level, bool leaveOpen)
|
||||
{
|
||||
_baseStream = new ZlibBaseStream(stream, mode, level, ZlibStreamFlavor.ZLIB, leaveOpen);
|
||||
}
|
||||
|
||||
#region Zlib properties
|
||||
|
||||
/// <summary>
|
||||
/// This property sets the flush behavior on the stream.
|
||||
/// Sorry, though, not sure exactly how to describe all the various settings.
|
||||
/// </summary>
|
||||
virtual public FlushType FlushMode
|
||||
{
|
||||
get { return (this._baseStream._flushMode); }
|
||||
set
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
this._baseStream._flushMode = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The size of the working buffer for the compression codec.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The working buffer is used for all stream operations. The default size is
|
||||
/// 1024 bytes. The minimum size is 128 bytes. You may get better performance
|
||||
/// with a larger buffer. Then again, you might not. You would have to test
|
||||
/// it.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// Set this before the first call to <c>Read()</c> or <c>Write()</c> on the
|
||||
/// stream. If you try to set it afterwards, it will throw.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public int BufferSize
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseStream._bufferSize;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
if (this._baseStream._workingBuffer != null)
|
||||
throw new ZlibException("The working buffer is already set.");
|
||||
if (value < ZlibConstants.WorkingBufferSizeMin)
|
||||
throw new ZlibException(String.Format("Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin));
|
||||
this._baseStream._bufferSize = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Returns the total number of bytes input so far.</summary>
|
||||
virtual public long TotalIn
|
||||
{
|
||||
get { return this._baseStream._z.TotalBytesIn; }
|
||||
}
|
||||
|
||||
/// <summary> Returns the total number of bytes output so far.</summary>
|
||||
virtual public long TotalOut
|
||||
{
|
||||
get { return this._baseStream._z.TotalBytesOut; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region System.IO.Stream methods
|
||||
|
||||
/// <summary>
|
||||
/// Dispose the stream.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This may or may not result in a <c>Close()</c> call on the captive
|
||||
/// stream. See the constructors that have a <c>leaveOpen</c> parameter
|
||||
/// for more information.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This method may be invoked in two distinct scenarios. If disposing
|
||||
/// == true, the method has been called directly or indirectly by a
|
||||
/// user's code, for example via the public Dispose() method. In this
|
||||
/// case, both managed and unmanaged resources can be referenced and
|
||||
/// disposed. If disposing == false, the method has been called by the
|
||||
/// runtime from inside the object finalizer and this method should not
|
||||
/// reference other objects; in that case only unmanaged resources must
|
||||
/// be referenced or disposed.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="disposing">
|
||||
/// indicates whether the Dispose method was invoked by user code.
|
||||
/// </param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing && (this._baseStream != null))
|
||||
this._baseStream.Close();
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be read.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value depends on whether the captive stream supports reading.
|
||||
/// </remarks>
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
return _baseStream._stream.CanRead;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream supports Seek operations.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Always returns false.
|
||||
/// </remarks>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the stream can be written.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The return value depends on whether the captive stream supports writing.
|
||||
/// </remarks>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
return _baseStream._stream.CanWrite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flush the stream.
|
||||
/// </summary>
|
||||
public override void Flush()
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
_baseStream.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reading this property always throws a <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The position of the stream pointer.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Setting this property always throws a <see
|
||||
/// cref="NotSupportedException"/>. Reading will return the total bytes
|
||||
/// written out, if used in writing, or the total bytes read in, if used in
|
||||
/// reading. The count may refer to compressed bytes or uncompressed bytes,
|
||||
/// depending on how you've used the stream.
|
||||
/// </remarks>
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._baseStream._streamMode == SabreTools.Compression.Deflate.ZlibBaseStream.StreamMode.Writer)
|
||||
return this._baseStream._z.TotalBytesOut;
|
||||
if (this._baseStream._streamMode == SabreTools.Compression.Deflate.ZlibBaseStream.StreamMode.Reader)
|
||||
return this._baseStream._z.TotalBytesIn;
|
||||
return 0;
|
||||
}
|
||||
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the stream.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// If you wish to use the <c>ZlibStream</c> to compress data while reading,
|
||||
/// you can create a <c>ZlibStream</c> with <c>CompressionMode.Compress</c>,
|
||||
/// providing an uncompressed data stream. Then call <c>Read()</c> on that
|
||||
/// <c>ZlibStream</c>, and the data read will be compressed. If you wish to
|
||||
/// use the <c>ZlibStream</c> to decompress data while reading, you can create
|
||||
/// a <c>ZlibStream</c> with <c>CompressionMode.Decompress</c>, providing a
|
||||
/// readable compressed data stream. Then call <c>Read()</c> on that
|
||||
/// <c>ZlibStream</c>, and the data will be decompressed as it is read.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A <c>ZlibStream</c> can be used for <c>Read()</c> or <c>Write()</c>, but
|
||||
/// not both.
|
||||
/// </para>
|
||||
///
|
||||
/// </remarks>
|
||||
///
|
||||
/// <param name="buffer">
|
||||
/// The buffer into which the read data should be placed.</param>
|
||||
///
|
||||
/// <param name="offset">
|
||||
/// the offset within that data array to put the first byte read.</param>
|
||||
///
|
||||
/// <param name="count">the number of bytes to read.</param>
|
||||
///
|
||||
/// <returns>the number of bytes read</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
return _baseStream.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="offset">
|
||||
/// The offset to seek to....
|
||||
/// IF THIS METHOD ACTUALLY DID ANYTHING.
|
||||
/// </param>
|
||||
/// <param name="origin">
|
||||
/// The reference specifying how to apply the offset.... IF
|
||||
/// THIS METHOD ACTUALLY DID ANYTHING.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>nothing. This method always throws.</returns>
|
||||
public override long Seek(long offset, System.IO.SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calling this method always throws a <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">
|
||||
/// The new value for the stream length.... IF
|
||||
/// THIS METHOD ACTUALLY DID ANYTHING.
|
||||
/// </param>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write data to the stream.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
///
|
||||
/// <para>
|
||||
/// If you wish to use the <c>ZlibStream</c> to compress data while writing,
|
||||
/// you can create a <c>ZlibStream</c> with <c>CompressionMode.Compress</c>,
|
||||
/// and a writable output stream. Then call <c>Write()</c> on that
|
||||
/// <c>ZlibStream</c>, providing uncompressed data as input. The data sent to
|
||||
/// the output stream will be the compressed form of the data written. If you
|
||||
/// wish to use the <c>ZlibStream</c> to decompress data while writing, you
|
||||
/// can create a <c>ZlibStream</c> with <c>CompressionMode.Decompress</c>, and a
|
||||
/// writable output stream. Then call <c>Write()</c> on that stream,
|
||||
/// providing previously compressed data. The data sent to the output stream
|
||||
/// will be the decompressed form of the data written.
|
||||
/// </para>
|
||||
///
|
||||
/// <para>
|
||||
/// A <c>ZlibStream</c> can be used for <c>Read()</c> or <c>Write()</c>, but not both.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="buffer">The buffer holding data to write to the stream.</param>
|
||||
/// <param name="offset">the offset within that data array to find the first byte to write.</param>
|
||||
/// <param name="count">the number of bytes to write.</param>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (_disposed) throw new ObjectDisposedException("ZlibStream");
|
||||
_baseStream.Write(buffer, offset, count);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compress a string into a byte array using ZLIB.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Uncompress it with <see cref="ZlibStream.UncompressString(byte[])"/>.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <seealso cref="ZlibStream.UncompressString(byte[])"/>
|
||||
/// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
|
||||
/// <seealso cref="GZipStream.CompressString(string)"/>
|
||||
///
|
||||
/// <param name="s">
|
||||
/// A string to compress. The string will first be encoded
|
||||
/// using UTF8, then compressed.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The string in compressed form</returns>
|
||||
public static byte[] CompressString(String s)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
Stream compressor =
|
||||
new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
|
||||
ZlibBaseStream.CompressString(s, compressor);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compress a byte array into a new byte array using ZLIB.
|
||||
/// </summary>
|
||||
///
|
||||
/// <remarks>
|
||||
/// Uncompress it with <see cref="ZlibStream.UncompressBuffer(byte[])"/>.
|
||||
/// </remarks>
|
||||
///
|
||||
/// <seealso cref="ZlibStream.CompressString(string)"/>
|
||||
/// <seealso cref="ZlibStream.UncompressBuffer(byte[])"/>
|
||||
///
|
||||
/// <param name="b">
|
||||
/// A buffer to compress.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The data in compressed form</returns>
|
||||
public static byte[] CompressBuffer(byte[] b)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
Stream compressor =
|
||||
new ZlibStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression );
|
||||
|
||||
ZlibBaseStream.CompressBuffer(b, compressor);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uncompress a ZLIB-compressed byte array into a single string.
|
||||
/// </summary>
|
||||
///
|
||||
/// <seealso cref="ZlibStream.CompressString(String)"/>
|
||||
/// <seealso cref="ZlibStream.UncompressBuffer(byte[])"/>
|
||||
///
|
||||
/// <param name="compressed">
|
||||
/// A buffer containing ZLIB-compressed data.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The uncompressed string</returns>
|
||||
public static String UncompressString(byte[] compressed)
|
||||
{
|
||||
using (var input = new MemoryStream(compressed))
|
||||
{
|
||||
Stream decompressor =
|
||||
new ZlibStream(input, CompressionMode.Decompress);
|
||||
|
||||
return ZlibBaseStream.UncompressString(compressed, decompressor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uncompress a ZLIB-compressed byte array into a byte array.
|
||||
/// </summary>
|
||||
///
|
||||
/// <seealso cref="ZlibStream.CompressBuffer(byte[])"/>
|
||||
/// <seealso cref="ZlibStream.UncompressString(byte[])"/>
|
||||
///
|
||||
/// <param name="compressed">
|
||||
/// A buffer containing ZLIB-compressed data.
|
||||
/// </param>
|
||||
///
|
||||
/// <returns>The data in uncompressed form</returns>
|
||||
public static byte[] UncompressBuffer(byte[] compressed)
|
||||
{
|
||||
using (var input = new MemoryStream(compressed))
|
||||
{
|
||||
Stream decompressor =
|
||||
new ZlibStream( input, CompressionMode.Decompress );
|
||||
|
||||
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
35
SabreTools.Compression/Deflate/ZlibStreamFlavor.cs
Normal file
35
SabreTools.Compression/Deflate/ZlibStreamFlavor.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
// ZlibBaseStream.cs
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This code module is part of DotNetZip, a zipfile class library.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This code is licensed under the Microsoft Public License.
|
||||
// See the file License.txt for the license details.
|
||||
// More info on: http://dotnetzip.codeplex.com
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// last saved (in emacs):
|
||||
// Time-stamp: <2011-August-06 21:22:38>
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
//
|
||||
// This module defines the ZlibBaseStream class, which is an intnernal
|
||||
// base class for DeflateStream, ZlibStream and GZipStream.
|
||||
//
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
namespace SabreTools.Compression.Deflate
|
||||
{
|
||||
internal enum ZlibStreamFlavor
|
||||
{
|
||||
ZLIB = 1950,
|
||||
DEFLATE = 1951,
|
||||
GZIP = 1952
|
||||
}
|
||||
}
|
||||
9
SabreTools.Compression/ExtensionAttribute.cs
Normal file
9
SabreTools.Compression/ExtensionAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
#if NET20
|
||||
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
|
||||
internal sealed class ExtensionAttribute : Attribute {}
|
||||
}
|
||||
|
||||
#endif
|
||||
72
SabreTools.Compression/MSZIP/Decompressor.cs
Normal file
72
SabreTools.Compression/MSZIP/Decompressor.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.Models.Compression.MSZIP;
|
||||
|
||||
namespace SabreTools.Compression.MSZIP
|
||||
{
|
||||
/// <see href="https://msopenspecs.azureedge.net/files/MS-MCI/%5bMS-MCI%5d.pdf"/>
|
||||
public class Decompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Last uncompressed block data
|
||||
/// </summary>
|
||||
private byte[]? _history = null;
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a MS-ZIP decompressor
|
||||
/// </summary>
|
||||
private Decompressor() { }
|
||||
|
||||
/// <summary>
|
||||
/// Create a MS-ZIP decompressor
|
||||
/// </summary>
|
||||
public static Decompressor Create() => new();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Decompress source data to an output stream
|
||||
/// </summary>
|
||||
public bool CopyTo(byte[] source, Stream dest)
|
||||
=> CopyTo(new MemoryStream(source), dest);
|
||||
|
||||
/// <summary>
|
||||
/// Decompress source data to an output stream
|
||||
/// </summary>
|
||||
public bool CopyTo(Stream source, Stream dest)
|
||||
{
|
||||
// Ignore unwritable streams
|
||||
if (!dest.CanWrite)
|
||||
return false;
|
||||
|
||||
// Validate the header
|
||||
var header = new BlockHeader();
|
||||
header.Signature = source.ReadUInt16LittleEndian();
|
||||
if (header.Signature != 0x4B43)
|
||||
throw new InvalidDataException(nameof(source));
|
||||
|
||||
byte[] buffer = new byte[32 * 1024];
|
||||
var blockStream = new Deflate.DeflateStream(source, Deflate.CompressionMode.Decompress);
|
||||
if (_history != null)
|
||||
blockStream.SetDictionary(_history, check: false);
|
||||
|
||||
int read = blockStream.Read(buffer, 0, buffer.Length);
|
||||
if (read > 0)
|
||||
{
|
||||
// Write to output
|
||||
dest.Write(buffer, 0, read);
|
||||
|
||||
// Save the history for rollover
|
||||
_history = new byte[read];
|
||||
Array.Copy(buffer, _history, read);
|
||||
}
|
||||
|
||||
// Flush and return
|
||||
dest.Flush();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
50
SabreTools.Compression/OldDotNet.cs
Normal file
50
SabreTools.Compression/OldDotNet.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
#if NET20 || NET35
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SabreTools.Compression
|
||||
{
|
||||
/// <summary>
|
||||
/// Derived from the mscorlib code from .NET Framework 4.0
|
||||
/// </summary>
|
||||
internal static class OldDotNet
|
||||
{
|
||||
public static void CopyTo(this Stream source, Stream destination)
|
||||
{
|
||||
if (destination == null)
|
||||
{
|
||||
throw new ArgumentNullException("destination");
|
||||
}
|
||||
|
||||
if (!source.CanRead && !source.CanWrite)
|
||||
{
|
||||
throw new ObjectDisposedException(null);
|
||||
}
|
||||
|
||||
if (!destination.CanRead && !destination.CanWrite)
|
||||
{
|
||||
throw new ObjectDisposedException("destination");
|
||||
}
|
||||
|
||||
if (!source.CanRead)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
if (!destination.CanWrite)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
byte[] array = new byte[81920];
|
||||
int count;
|
||||
while ((count = source.Read(array, 0, array.Length)) != 0)
|
||||
{
|
||||
destination.Write(array, 0, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
395
SabreTools.Compression/Quantum/Decompressor.cs
Normal file
395
SabreTools.Compression/Quantum/Decompressor.cs
Normal file
@@ -0,0 +1,395 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SabreTools.IO.Streams;
|
||||
using SabreTools.Models.Compression.Quantum;
|
||||
using static SabreTools.Models.Compression.Quantum.Constants;
|
||||
|
||||
namespace SabreTools.Compression.Quantum
|
||||
{
|
||||
/// <see href="www.russotto.net/quantumcomp.html"/>
|
||||
public class Decompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal bitstream to use for decompression
|
||||
/// </summary>
|
||||
private readonly ReadOnlyBitStream _bitStream;
|
||||
|
||||
#region Models
|
||||
|
||||
/// <summary>
|
||||
/// Selector 0: literal, 64 entries, starting symbol 0
|
||||
/// </summary>
|
||||
private readonly Model _model0;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 1: literal, 64 entries, starting symbol 64
|
||||
/// </summary>
|
||||
private readonly Model _model1;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 2: literal, 64 entries, starting symbol 128
|
||||
/// </summary>
|
||||
private readonly Model _model2;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 3: literal, 64 entries, starting symbol 192
|
||||
/// </summary>
|
||||
private readonly Model _model3;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 4: LZ, 3 character matches
|
||||
/// </summary>
|
||||
private readonly Model _model4;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 5: LZ, 4 character matches
|
||||
/// </summary>
|
||||
private readonly Model _model5;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 6: LZ, 5+ character matches
|
||||
/// </summary>
|
||||
private readonly Model _model6;
|
||||
|
||||
/// <summary>
|
||||
/// Selector 6 length model
|
||||
/// </summary>
|
||||
private readonly Model _model6len;
|
||||
|
||||
/// <summary>
|
||||
/// Selector selector model
|
||||
/// </summary>
|
||||
private readonly Model _selector;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Coding State
|
||||
|
||||
/// <summary>
|
||||
/// Artihmetic coding state: high
|
||||
/// </summary>
|
||||
private ushort CS_H;
|
||||
|
||||
/// <summary>
|
||||
/// Artihmetic coding state: low
|
||||
/// </summary>
|
||||
private ushort CS_L;
|
||||
|
||||
/// <summary>
|
||||
/// Artihmetic coding state: current
|
||||
/// </summary>
|
||||
private ushort CS_C;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a new Decompressor from a Stream
|
||||
/// </summary>
|
||||
/// <param name="source">Stream to decompress</param>
|
||||
/// <param name="windowBits">Number of bits in the sliding window</param>
|
||||
private Decompressor(Stream source, uint windowBits)
|
||||
{
|
||||
// Validate the inputs
|
||||
if (source.Length == 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(source));
|
||||
if (!source.CanRead)
|
||||
throw new InvalidOperationException(nameof(source));
|
||||
if (windowBits < 10 || windowBits > 21)
|
||||
throw new ArgumentOutOfRangeException(nameof(windowBits));
|
||||
|
||||
// Wrap the stream in a ReadOnlyBitStream
|
||||
_bitStream = new ReadOnlyBitStream(source);
|
||||
|
||||
// Initialize literal models
|
||||
_model0 = CreateModel(0, 64);
|
||||
_model1 = CreateModel(64, 64);
|
||||
_model2 = CreateModel(128, 64);
|
||||
_model3 = CreateModel(192, 64);
|
||||
|
||||
// Initialize LZ models
|
||||
int maxBitLength = (int)(windowBits * 2);
|
||||
_model4 = CreateModel(0, maxBitLength > 24 ? 24 : maxBitLength);
|
||||
_model5 = CreateModel(0, maxBitLength > 36 ? 36 : maxBitLength);
|
||||
_model6 = CreateModel(0, maxBitLength);
|
||||
_model6len = CreateModel(0, 27);
|
||||
|
||||
// Initialze the selector model
|
||||
_selector = CreateModel(0, 7);
|
||||
|
||||
// Initialize coding state
|
||||
CS_H = 0;
|
||||
CS_L = 0;
|
||||
CS_C = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a Quantum decompressor
|
||||
/// </summary>
|
||||
public static Decompressor Create(byte[] source, uint windowBits)
|
||||
=> Create(new MemoryStream(source), windowBits);
|
||||
|
||||
/// <summary>
|
||||
/// Create a Quantum decompressor
|
||||
/// </summary>
|
||||
public static Decompressor Create(Stream source, uint windowBits)
|
||||
=> new(source, windowBits);
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Process the stream and return the decompressed output
|
||||
/// </summary>
|
||||
/// <returns>Byte array representing the decompressed data, null on error</returns>
|
||||
public byte[] Process()
|
||||
{
|
||||
// Initialize the coding state
|
||||
CS_H = 0xffff;
|
||||
CS_L = 0x0000;
|
||||
CS_C = (ushort)(_bitStream.ReadBitsBE(16) ?? 0);
|
||||
|
||||
// Loop until the end of the stream
|
||||
var bytes = new List<byte>();
|
||||
while (_bitStream.Position < _bitStream.Length)
|
||||
{
|
||||
// Determine the selector to use
|
||||
int selector = GetSymbol(_selector);
|
||||
|
||||
// Handle literal selectors
|
||||
if (selector < 4)
|
||||
{
|
||||
switch (selector)
|
||||
{
|
||||
case 0:
|
||||
bytes.Add((byte)GetSymbol(_model0));
|
||||
break;
|
||||
case 1:
|
||||
bytes.Add((byte)GetSymbol(_model1));
|
||||
break;
|
||||
case 2:
|
||||
bytes.Add((byte)GetSymbol(_model2));
|
||||
break;
|
||||
case 3:
|
||||
bytes.Add((byte)GetSymbol(_model3));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle LZ selectors
|
||||
else
|
||||
{
|
||||
int offset, length;
|
||||
switch (selector)
|
||||
{
|
||||
case 4:
|
||||
int model4sym = GetSymbol(_model4);
|
||||
int model4extra = (int)(_bitStream.ReadBitsBE(PositionExtraBits[model4sym]) ?? 0);
|
||||
offset = PositionSlot[model4sym] + model4extra + 1;
|
||||
length = 3;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
int model5sym = GetSymbol(_model5);
|
||||
int model5extra = (int)(_bitStream.ReadBitsBE(PositionExtraBits[model5sym]) ?? 0);
|
||||
offset = PositionSlot[model5sym] + model5extra + 1;
|
||||
length = 4;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
int lengthSym = GetSymbol(_model6len);
|
||||
int lengthExtra = (int)(_bitStream.ReadBitsBE(LengthExtraBits[lengthSym]) ?? 0);
|
||||
length = LengthSlot[lengthSym] + lengthExtra + 5;
|
||||
|
||||
int model6sym = GetSymbol(_model6);
|
||||
int model6extra = (int)(_bitStream.ReadBitsBE(PositionExtraBits[model6sym]) ?? 0);
|
||||
offset = PositionSlot[model6sym] + model6extra + 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
// Copy the previous data
|
||||
int copyIndex = bytes.Count - offset;
|
||||
while (length-- > 0)
|
||||
{
|
||||
bytes.Add(bytes[copyIndex++]);
|
||||
}
|
||||
|
||||
// TODO: Add MS-CAB specific padding
|
||||
// TODO: Add Cinematronics specific checksum
|
||||
}
|
||||
}
|
||||
|
||||
return [.. bytes];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create and initialize a model base on the start symbol and length
|
||||
/// </summary>
|
||||
private Model CreateModel(ushort start, int length)
|
||||
{
|
||||
// Create the model
|
||||
var model = new Model
|
||||
{
|
||||
Entries = length,
|
||||
Symbols = new ModelSymbol[length + 1],
|
||||
TimeToReorder = 4,
|
||||
};
|
||||
|
||||
// Populate the symbol array
|
||||
for (int i = 0; i <= length; i++)
|
||||
{
|
||||
model.Symbols[i] = new ModelSymbol
|
||||
{
|
||||
Symbol = (ushort)(start + i),
|
||||
CumulativeFrequency = (ushort)(length - 1),
|
||||
};
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next symbol from a model
|
||||
/// </summary>
|
||||
private int GetSymbol(Model model)
|
||||
{
|
||||
int freq = GetFrequency(model.Symbols![0]!.CumulativeFrequency);
|
||||
|
||||
int i;
|
||||
for (i = 1; i < model.Entries; i++)
|
||||
{
|
||||
if (model.Symbols[i]!.CumulativeFrequency <= freq)
|
||||
break;
|
||||
}
|
||||
|
||||
int sym = model.Symbols![i - 1]!.Symbol;
|
||||
|
||||
GetCode(model.Symbols![i - 1]!.CumulativeFrequency,
|
||||
model.Symbols![i]!.CumulativeFrequency,
|
||||
model.Symbols![0]!.CumulativeFrequency);
|
||||
|
||||
UpdateModel(model, i);
|
||||
|
||||
return sym;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next code based on the frequencies
|
||||
/// </summary>
|
||||
private void GetCode(int prevFrequency, int currentFrequency, int totalFrequency)
|
||||
{
|
||||
uint range = (ushort)((CS_H - CS_L) + 1);
|
||||
CS_H = (ushort)(CS_L + (prevFrequency * range) / totalFrequency - 1);
|
||||
CS_L = (ushort)(CS_L + (currentFrequency * range) / totalFrequency);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if ((CS_L & 0x8000) != (CS_H & 0x8000))
|
||||
{
|
||||
if ((CS_L & 0x4000) != 0 && (CS_H & 0x4000) == 0)
|
||||
{
|
||||
// Underflow case
|
||||
CS_C ^= 0x4000;
|
||||
CS_L &= 0x3FFF;
|
||||
CS_H |= 0x4000;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CS_L <<= 1;
|
||||
CS_H = (ushort)((CS_H << 1) | 1);
|
||||
CS_C = (ushort)((CS_C << 1) | _bitStream.ReadBit() ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the model after an encode or decode step
|
||||
/// </summary>
|
||||
private void UpdateModel(Model model, int lastUpdated)
|
||||
{
|
||||
// Update cumulative frequencies
|
||||
for (int i = 0; i < lastUpdated; i++)
|
||||
{
|
||||
var sym = model.Symbols![i]!;
|
||||
sym.CumulativeFrequency += 8;
|
||||
}
|
||||
|
||||
// Decrement reordering time, if needed
|
||||
if (model.Symbols![0]!.CumulativeFrequency > 3800)
|
||||
model.TimeToReorder--;
|
||||
|
||||
// If we haven't hit the reordering time
|
||||
if (model.TimeToReorder > 0)
|
||||
{
|
||||
// Update the cumulative frequencies
|
||||
for (int i = model.Entries - 1; i >= 0; i--)
|
||||
{
|
||||
// Divide with truncation by 2
|
||||
var sym = model.Symbols![i]!;
|
||||
sym.CumulativeFrequency >>= 1;
|
||||
|
||||
// If we are lower the next frequency
|
||||
if (i != 0 && sym.CumulativeFrequency <= model.Symbols![i + 1]!.CumulativeFrequency)
|
||||
sym.CumulativeFrequency = (ushort)(model.Symbols![i + 1]!.CumulativeFrequency + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// If we hit the reordering time
|
||||
else
|
||||
{
|
||||
// Calculate frequencies from cumulative frequencies
|
||||
for (int i = 0; i < model.Entries; i++)
|
||||
{
|
||||
if (i != model.Entries - 1)
|
||||
model.Symbols![i]!.CumulativeFrequency -= model.Symbols![i + 1]!.CumulativeFrequency;
|
||||
|
||||
model.Symbols![i]!.CumulativeFrequency++;
|
||||
model.Symbols![i]!.CumulativeFrequency >>= 1;
|
||||
}
|
||||
|
||||
// Sort frequencies in decreasing order
|
||||
for (int i = 0; i < model.Entries; i++)
|
||||
{
|
||||
for (int j = i + 1; j < model.Entries; j++)
|
||||
{
|
||||
if (model.Symbols![i]!.CumulativeFrequency < model.Symbols![j]!.CumulativeFrequency)
|
||||
{
|
||||
var temp = model.Symbols[i];
|
||||
model.Symbols[i] = model.Symbols[j];
|
||||
model.Symbols[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate cumulative frequencies from frequencies
|
||||
for (int i = model.Entries - 1; i >= 0; i--)
|
||||
{
|
||||
if (i != model.Entries - 1)
|
||||
model.Symbols![i]!.CumulativeFrequency += model.Symbols![i + 1]!.CumulativeFrequency;
|
||||
}
|
||||
|
||||
// Reset the time to reorder
|
||||
model.TimeToReorder = 50;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the frequency of a symbol based on its total frequency
|
||||
/// </summary>
|
||||
private ushort GetFrequency(ushort totalFrequency)
|
||||
{
|
||||
ulong range = (ulong)(((CS_H - CS_L) & 0xFFFF) + 1);
|
||||
ulong frequency = (ulong)(((CS_C - CS_L + 1) * totalFrequency) - 1) / range;
|
||||
return (ushort)(frequency & 0xFFFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
305
SabreTools.Compression/SZDD/Decompressor.cs
Normal file
305
SabreTools.Compression/SZDD/Decompressor.cs
Normal file
@@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SabreTools.Models.LZ;
|
||||
|
||||
namespace SabreTools.Compression.SZDD
|
||||
{
|
||||
/// <see href="https://www.cabextract.org.uk/libmspack/doc/szdd_kwaj_format.html"/>
|
||||
public class Decompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Window to deflate data into
|
||||
/// </summary>
|
||||
private readonly byte[] _window = new byte[4096];
|
||||
|
||||
/// <summary>
|
||||
/// Source stream for the decompressor
|
||||
/// </summary>
|
||||
private readonly BufferedStream _source;
|
||||
|
||||
/// <summary>
|
||||
/// SZDD format being decompressed
|
||||
/// </summary>
|
||||
private Format _format;
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Create a SZDD decompressor
|
||||
/// </summary>
|
||||
private Decompressor(Stream source)
|
||||
{
|
||||
// Validate the inputs
|
||||
if (source.Length == 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(source));
|
||||
if (!source.CanRead)
|
||||
throw new InvalidOperationException(nameof(source));
|
||||
|
||||
// Initialize the window with space characters
|
||||
_window = Array.ConvertAll(_window, b => (byte)0x20);
|
||||
_source = new BufferedStream(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a KWAJ decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateKWAJ(byte[] source, KWAJCompressionType compressionType)
|
||||
=> CreateKWAJ(new MemoryStream(source), compressionType);
|
||||
|
||||
/// <summary>
|
||||
/// Create a KWAJ decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateKWAJ(Stream source, KWAJCompressionType compressionType)
|
||||
{
|
||||
// Create the decompressor
|
||||
var decompressor = new Decompressor(source);
|
||||
|
||||
// Set the format and return
|
||||
decompressor._format = compressionType switch
|
||||
{
|
||||
KWAJCompressionType.NoCompression => Format.KWAJNoCompression,
|
||||
KWAJCompressionType.NoCompressionXor => Format.KWAJXor,
|
||||
KWAJCompressionType.QBasic => Format.KWAJQBasic,
|
||||
KWAJCompressionType.LZH => Format.KWAJLZH,
|
||||
KWAJCompressionType.MSZIP => Format.KWAJMSZIP,
|
||||
_ => throw new IndexOutOfRangeException(nameof(source)),
|
||||
};
|
||||
return decompressor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a QBasic 4.5 installer SZDD decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateQBasic(byte[] source)
|
||||
=> CreateQBasic(new MemoryStream(source));
|
||||
|
||||
/// <summary>
|
||||
/// Create a QBasic 4.5 installer SZDD decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateQBasic(Stream source)
|
||||
{
|
||||
// Create the decompressor
|
||||
var decompressor = new Decompressor(source);
|
||||
|
||||
// Set the format and return
|
||||
decompressor._format = Format.QBasic;
|
||||
return decompressor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a standard SZDD decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateSZDD(byte[] source)
|
||||
=> CreateSZDD(new MemoryStream(source));
|
||||
|
||||
/// <summary>
|
||||
/// Create a standard SZDD decompressor
|
||||
/// </summary>
|
||||
public static Decompressor CreateSZDD(Stream source)
|
||||
{
|
||||
// Create the decompressor
|
||||
var decompressor = new Decompressor(source);
|
||||
|
||||
// Set the format and return
|
||||
decompressor._format = Format.SZDD;
|
||||
return decompressor;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Decompress source data to an output stream
|
||||
/// </summary>
|
||||
public bool CopyTo(Stream dest)
|
||||
{
|
||||
// Ignore unwritable streams
|
||||
if (!dest.CanWrite)
|
||||
return false;
|
||||
|
||||
// Handle based on the format
|
||||
return _format switch
|
||||
{
|
||||
Format.SZDD => DecompressSZDD(dest, 4096 - 16),
|
||||
Format.QBasic => DecompressSZDD(dest, 4096 - 18),
|
||||
Format.KWAJNoCompression => CopyKWAJ(dest, xor: false),
|
||||
Format.KWAJXor => CopyKWAJ(dest, xor: true),
|
||||
Format.KWAJQBasic => DecompressSZDD(dest, 4096 - 18),
|
||||
Format.KWAJLZH => false,
|
||||
Format.KWAJMSZIP => false,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompress using SZDD
|
||||
/// </summary>
|
||||
private bool DecompressSZDD(Stream dest, int offset)
|
||||
{
|
||||
// Ignore unwritable streams
|
||||
if (!dest.CanWrite)
|
||||
return false;
|
||||
|
||||
// Loop and decompress
|
||||
while (true)
|
||||
{
|
||||
// Get the control byte
|
||||
byte? control = _source.ReadNextByte();
|
||||
if (control == null)
|
||||
break;
|
||||
|
||||
for (int cbit = 0x01; (cbit & 0xFF) != 0; cbit <<= 1)
|
||||
{
|
||||
// Literal value
|
||||
if ((control & cbit) != 0)
|
||||
{
|
||||
// Read the literal byte
|
||||
byte? literal = _source.ReadNextByte();
|
||||
if (literal == null)
|
||||
break;
|
||||
|
||||
// Store the data in the window and write
|
||||
_window[offset] = literal.Value;
|
||||
dest.WriteByte(_window[offset]);
|
||||
|
||||
// Set the next offset value
|
||||
offset++;
|
||||
offset &= 4095;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read the match position
|
||||
int? matchpos = _source.ReadNextByte();
|
||||
if (matchpos == null)
|
||||
break;
|
||||
|
||||
// Read the match length
|
||||
int? matchlen = _source.ReadNextByte();
|
||||
if (matchlen == null)
|
||||
break;
|
||||
|
||||
// Adjust the position and length
|
||||
matchpos |= (matchlen & 0xF0) << 4;
|
||||
matchlen = (matchlen & 0x0F) + 3;
|
||||
|
||||
// Loop over the match length
|
||||
while (matchlen-- > 0)
|
||||
{
|
||||
// Copy the window value and write
|
||||
_window[offset] = _window[matchpos.Value];
|
||||
dest.WriteByte(_window[offset]);
|
||||
|
||||
// Set the next offset value
|
||||
offset++; matchpos++;
|
||||
offset &= 4095; matchpos &= 4095;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flush and return
|
||||
dest.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy KWAJ data, optionally using XOR
|
||||
/// </summary>
|
||||
private bool CopyKWAJ(Stream dest, bool xor)
|
||||
{
|
||||
// Ignore unwritable streams
|
||||
if (!dest.CanWrite)
|
||||
return false;
|
||||
|
||||
// Loop and copy
|
||||
while (true)
|
||||
{
|
||||
// Read the next byte
|
||||
byte? next = _source.ReadNextByte();
|
||||
if (next == null)
|
||||
break;
|
||||
|
||||
// XOR with 0xFF if required
|
||||
if (xor)
|
||||
next = (byte)(next ^ 0xFF);
|
||||
|
||||
// Write the byte
|
||||
dest.WriteByte(next.Value);
|
||||
}
|
||||
|
||||
// Flush and return
|
||||
dest.Flush();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buffered stream that reads in blocks
|
||||
/// </summary>
|
||||
private class BufferedStream
|
||||
{
|
||||
/// <summary>
|
||||
/// Source stream for populating the buffer
|
||||
/// </summary>
|
||||
private readonly Stream _source;
|
||||
|
||||
/// <summary>
|
||||
/// Internal buffer to read
|
||||
/// </summary>
|
||||
private readonly byte[] _buffer = new byte[2048];
|
||||
|
||||
/// <summary>
|
||||
/// Current pointer into the buffer
|
||||
/// </summary>
|
||||
private int _bufferPtr = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the number of available bytes
|
||||
/// </summary>
|
||||
private int _available = -1;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new buffered stream
|
||||
/// </summary>
|
||||
public BufferedStream(Stream source)
|
||||
{
|
||||
_source = source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the next byte from the buffer, if possible
|
||||
/// </summary>
|
||||
public byte? ReadNextByte()
|
||||
{
|
||||
// Ensure the buffer first
|
||||
if (!EnsureBuffer())
|
||||
return null;
|
||||
|
||||
// Return the next available value
|
||||
return _buffer[_bufferPtr++];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure the buffer has data to read
|
||||
/// </summary>
|
||||
private bool EnsureBuffer()
|
||||
{
|
||||
// Force an update if in the initial state
|
||||
if (_available == -1)
|
||||
{
|
||||
_available = _source.Read(_buffer, 0, _buffer.Length);
|
||||
_bufferPtr = 0;
|
||||
return _available != 0;
|
||||
}
|
||||
|
||||
// If the pointer is out of range
|
||||
if (_bufferPtr >= _available)
|
||||
{
|
||||
_available = _source.Read(_buffer, 0, _buffer.Length);
|
||||
_bufferPtr = 0;
|
||||
return _available != 0;
|
||||
}
|
||||
|
||||
// Otherwise, assume data is available
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
SabreTools.Compression/SZDD/Enums.cs
Normal file
43
SabreTools.Compression/SZDD/Enums.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace SabreTools.Compression.SZDD
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the SZDD format being decompressed
|
||||
/// </summary>
|
||||
internal enum Format
|
||||
{
|
||||
/// <summary>
|
||||
/// Standard SZDD implementation
|
||||
/// </summary>
|
||||
SZDD,
|
||||
|
||||
/// <summary>
|
||||
/// QBasic 4.5 installer variant
|
||||
/// </summary>
|
||||
QBasic,
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ variant, no compression
|
||||
/// </summary>
|
||||
KWAJNoCompression,
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ variant, XORed with 0xFF
|
||||
/// </summary>
|
||||
KWAJXor,
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ variant, QBasic variant compression
|
||||
/// </summary>
|
||||
KWAJQBasic,
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ variant, LZ + Huffman compression
|
||||
/// </summary>
|
||||
KWAJLZH,
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ variant, MS-ZIP compression
|
||||
/// </summary>
|
||||
KWAJMSZIP,
|
||||
}
|
||||
}
|
||||
35
SabreTools.Compression/SabreTools.Compression.csproj
Normal file
35
SabreTools.Compression/SabreTools.Compression.csproj
Normal file
@@ -0,0 +1,35 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<!-- Assembly Properties -->
|
||||
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>0.6.3</Version>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
<Description>Clean compression implementations</Description>
|
||||
<Copyright>Copyright (c) Matt Nadareski 2022-2025</Copyright>
|
||||
<PackageProjectUrl>https://github.com/SabreTools/</PackageProjectUrl>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<RepositoryUrl>https://github.com/SabreTools/SabreTools.Compression</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>compression decompression lz mszip zlib blast</PackageTags>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../README.md" Pack="true" PackagePath="" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SabreTools.IO" Version="1.6.3" />
|
||||
<PackageReference Include="SabreTools.Models" Version="1.5.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
218
SabreTools.Compression/zlib/Hebron.Runtime/CRuntime.cs
Normal file
218
SabreTools.Compression/zlib/Hebron.Runtime/CRuntime.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public static unsafe class CRuntime
|
||||
{
|
||||
private static readonly string numbers = "0123456789";
|
||||
|
||||
public static void* malloc(ulong size)
|
||||
{
|
||||
return malloc((long)size);
|
||||
}
|
||||
|
||||
public static void* malloc(long size)
|
||||
{
|
||||
var ptr = Marshal.AllocHGlobal((int)size);
|
||||
|
||||
MemoryStats.Allocated();
|
||||
|
||||
return ptr.ToPointer();
|
||||
}
|
||||
|
||||
public static void free(void* a)
|
||||
{
|
||||
if (a == null)
|
||||
return;
|
||||
|
||||
var ptr = new IntPtr(a);
|
||||
Marshal.FreeHGlobal(ptr);
|
||||
MemoryStats.Freed();
|
||||
}
|
||||
|
||||
public static void memcpy(void* a, void* b, long size)
|
||||
{
|
||||
var ap = (byte*)a;
|
||||
var bp = (byte*)b;
|
||||
for (long i = 0; i < size; ++i)
|
||||
*ap++ = *bp++;
|
||||
}
|
||||
|
||||
public static void memcpy(void* a, void* b, ulong size)
|
||||
{
|
||||
memcpy(a, b, (long)size);
|
||||
}
|
||||
|
||||
public static void memmove(void* a, void* b, long size)
|
||||
{
|
||||
void* temp = null;
|
||||
|
||||
try
|
||||
{
|
||||
temp = malloc(size);
|
||||
memcpy(temp, b, size);
|
||||
memcpy(a, temp, size);
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
if (temp != null)
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
public static void memmove(void* a, void* b, ulong size)
|
||||
{
|
||||
memmove(a, b, (long)size);
|
||||
}
|
||||
|
||||
public static int memcmp(void* a, void* b, long size)
|
||||
{
|
||||
var result = 0;
|
||||
var ap = (byte*)a;
|
||||
var bp = (byte*)b;
|
||||
for (long i = 0; i < size; ++i)
|
||||
{
|
||||
if (*ap != *bp)
|
||||
result += 1;
|
||||
|
||||
ap++;
|
||||
bp++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int memcmp(void* a, void* b, ulong size)
|
||||
{
|
||||
return memcmp(a, b, (long)size);
|
||||
}
|
||||
|
||||
public static int memcmp(byte* a, byte[] b, ulong size)
|
||||
{
|
||||
fixed (void* bptr = b)
|
||||
{
|
||||
return memcmp(a, bptr, (long)size);
|
||||
}
|
||||
}
|
||||
|
||||
public static void memset(void* ptr, int value, long size)
|
||||
{
|
||||
var bptr = (byte*)ptr;
|
||||
var bval = (byte)value;
|
||||
for (long i = 0; i < size; ++i)
|
||||
*bptr++ = bval;
|
||||
}
|
||||
|
||||
public static void memset(void* ptr, int value, ulong size)
|
||||
{
|
||||
memset(ptr, value, (long)size);
|
||||
}
|
||||
|
||||
public static uint _lrotl(uint x, int y)
|
||||
{
|
||||
return (x << y) | (x >> (32 - y));
|
||||
}
|
||||
|
||||
public static void* realloc(void* a, long newSize)
|
||||
{
|
||||
if (a == null)
|
||||
return malloc(newSize);
|
||||
|
||||
var ptr = new IntPtr(a);
|
||||
var result = Marshal.ReAllocHGlobal(ptr, new IntPtr(newSize));
|
||||
|
||||
return result.ToPointer();
|
||||
}
|
||||
|
||||
public static void* realloc(void* a, ulong newSize)
|
||||
{
|
||||
return realloc(a, (long)newSize);
|
||||
}
|
||||
|
||||
public static int abs(int v)
|
||||
{
|
||||
return Math.Abs(v);
|
||||
}
|
||||
|
||||
public static double pow(double a, double b)
|
||||
{
|
||||
return Math.Pow(a, b);
|
||||
}
|
||||
|
||||
public static void SetArray<T>(T[] data, T value)
|
||||
{
|
||||
for (var i = 0; i < data.Length; ++i)
|
||||
data[i] = value;
|
||||
}
|
||||
|
||||
public static double ldexp(double number, int exponent)
|
||||
{
|
||||
return number * Math.Pow(2, exponent);
|
||||
}
|
||||
|
||||
public static int strcmp(sbyte* src, string token)
|
||||
{
|
||||
var result = 0;
|
||||
|
||||
for (var i = 0; i < token.Length; ++i)
|
||||
{
|
||||
if (src[i] != token[i])
|
||||
{
|
||||
++result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int strncmp(sbyte* src, string token, ulong size)
|
||||
{
|
||||
var result = 0;
|
||||
|
||||
for (var i = 0; i < Math.Min(token.Length, (int)size); ++i)
|
||||
{
|
||||
if (src[i] != token[i])
|
||||
{
|
||||
++result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long strtol(sbyte* start, sbyte** end, int radix)
|
||||
{
|
||||
// First step - determine length
|
||||
var length = 0;
|
||||
sbyte* ptr = start;
|
||||
while (numbers.IndexOf((char)*ptr) != -1)
|
||||
{
|
||||
++ptr;
|
||||
++length;
|
||||
}
|
||||
|
||||
long result = 0;
|
||||
|
||||
// Now build up the number
|
||||
ptr = start;
|
||||
while (length > 0)
|
||||
{
|
||||
long num = numbers.IndexOf((char)*ptr);
|
||||
long pow = (long)Math.Pow(10, length - 1);
|
||||
result += num * pow;
|
||||
|
||||
++ptr;
|
||||
--length;
|
||||
}
|
||||
|
||||
if (end != null)
|
||||
{
|
||||
*end = ptr;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
SabreTools.Compression/zlib/Hebron.Runtime/MemoryStats.cs
Normal file
28
SabreTools.Compression/zlib/Hebron.Runtime/MemoryStats.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public unsafe static class MemoryStats
|
||||
{
|
||||
private static int _allocations;
|
||||
|
||||
public static int Allocations
|
||||
{
|
||||
get
|
||||
{
|
||||
return _allocations;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Allocated()
|
||||
{
|
||||
Interlocked.Increment(ref _allocations);
|
||||
}
|
||||
|
||||
internal static void Freed()
|
||||
{
|
||||
Interlocked.Decrement(ref _allocations);
|
||||
}
|
||||
}
|
||||
}
|
||||
88
SabreTools.Compression/zlib/Hebron.Runtime/UnsafeArray1D.cs
Normal file
88
SabreTools.Compression/zlib/Hebron.Runtime/UnsafeArray1D.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public unsafe class UnsafeArray1D<T> where T : struct
|
||||
{
|
||||
private readonly T[] _data;
|
||||
private readonly GCHandle _pinHandle;
|
||||
public bool IsFreed { get; private set; }
|
||||
|
||||
internal GCHandle PinHandle => _pinHandle;
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => _data[index];
|
||||
set
|
||||
{
|
||||
_data[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public T this[uint index]
|
||||
{
|
||||
get => _data[index];
|
||||
set
|
||||
{
|
||||
_data[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public T[] Data => _data;
|
||||
|
||||
public UnsafeArray1D(int size)
|
||||
{
|
||||
if (size < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(size));
|
||||
}
|
||||
|
||||
_data = new T[size];
|
||||
_pinHandle = GCHandle.Alloc(_data, GCHandleType.Pinned);
|
||||
IsFreed = false;
|
||||
}
|
||||
|
||||
public UnsafeArray1D(T[] data, int sizeOf)
|
||||
{
|
||||
if (sizeOf <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(sizeOf));
|
||||
}
|
||||
|
||||
_data = data ?? throw new ArgumentNullException(nameof(data));
|
||||
_pinHandle = GCHandle.Alloc(_data, GCHandleType.Pinned);
|
||||
IsFreed = false;
|
||||
}
|
||||
|
||||
public void Free()
|
||||
{
|
||||
if (!IsFreed)
|
||||
{
|
||||
_pinHandle.Free();
|
||||
IsFreed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~UnsafeArray1D()
|
||||
{
|
||||
if (!IsFreed)
|
||||
_pinHandle.Free();
|
||||
}
|
||||
|
||||
public void* ToPointer()
|
||||
{
|
||||
return _pinHandle.AddrOfPinnedObject().ToPointer();
|
||||
}
|
||||
|
||||
public static implicit operator void*(UnsafeArray1D<T> array)
|
||||
{
|
||||
return array.ToPointer();
|
||||
}
|
||||
|
||||
public static void* operator +(UnsafeArray1D<T> array, int delta)
|
||||
{
|
||||
return array.ToPointer();
|
||||
}
|
||||
}
|
||||
}
|
||||
45
SabreTools.Compression/zlib/Hebron.Runtime/UnsafeArray2D.cs
Normal file
45
SabreTools.Compression/zlib/Hebron.Runtime/UnsafeArray2D.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public unsafe class UnsafeArray2D<T> where T : struct
|
||||
{
|
||||
private readonly UnsafeArray1D<T>[] _data;
|
||||
private long[] _pinAddresses;
|
||||
private readonly GCHandle _pinAddressesHandle;
|
||||
|
||||
public UnsafeArray1D<T> this[int index]
|
||||
{
|
||||
get => _data[index];
|
||||
set
|
||||
{
|
||||
_data[index] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public UnsafeArray2D(int size1, int size2)
|
||||
{
|
||||
_data = new UnsafeArray1D<T>[size1];
|
||||
_pinAddresses = new long[size1];
|
||||
for (var i = 0; i < size1; ++i)
|
||||
{
|
||||
_data[i] = new UnsafeArray1D<T>(size2);
|
||||
_pinAddresses[i] = _data[i].PinHandle.AddrOfPinnedObject().ToInt64();
|
||||
}
|
||||
|
||||
_pinAddressesHandle = GCHandle.Alloc(_pinAddresses, GCHandleType.Pinned);
|
||||
}
|
||||
|
||||
~UnsafeArray2D()
|
||||
{
|
||||
_pinAddressesHandle.Free();
|
||||
}
|
||||
|
||||
public void* ToPointer() => _pinAddressesHandle.AddrOfPinnedObject().ToPointer();
|
||||
|
||||
public static implicit operator void*(UnsafeArray2D<T> array)
|
||||
{
|
||||
return array.ToPointer();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
SabreTools.Compression/zlib/LICENSE
Normal file
9
SabreTools.Compression/zlib/LICENSE
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Nanook
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
23
SabreTools.Compression/zlib/README.md
Normal file
23
SabreTools.Compression/zlib/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
Project to port ZLib from C to C# (CSharp).
|
||||
|
||||
Src zlib 1.2.12 2022-Mar-28 - https://github.com/madler/zlib
|
||||
|
||||
See the Stages folder
|
||||
|
||||
1_zlib.c - Created by running 1_zlib.c_Concat.ps1 Builds with Clang (used by hebron to convert)
|
||||
- Only deflate, inflate, crc32 and adler32 code at the moment. GZip might be added if required.
|
||||
- The only edits to these files are to remove any #includes that have been combined
|
||||
- The file list includes a 000_ to insert any #defines etc and 100_ for a main for debugging etc
|
||||
- Notice crc32.c and trees.c had to be split to allow the single file to build
|
||||
|
||||
2_zlib.cs_Converted - The converted output that Hebron produced - https://github.com/HebronFramework/Hebron
|
||||
- This is a little app that uses Clang to read the C code as DOM and write with Roslyn
|
||||
- It does a fairly decent job and removes a lot of complication
|
||||
|
||||
3_zlib.cs_Working - The fixed up and amended C# that actually runs and matches the C code output
|
||||
- It's had minimal change so is not the prettiest C# code
|
||||
- It's Unsafe in places
|
||||
|
||||
Deflate and Inflate streams have been added.
|
||||
178
SabreTools.Compression/zlib/ZlibDeflateStream.cs
Normal file
178
SabreTools.Compression/zlib/ZlibDeflateStream.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public class ZlibDeflateStream : Stream
|
||||
{
|
||||
private readonly bool _leaveOpen;
|
||||
private ZLib.z_stream_s? _s;
|
||||
private long _p;
|
||||
private byte[]? _b;
|
||||
|
||||
public ZlibDeflateStream(int level, Stream baseStream) : this(level, false, 0, baseStream, false)
|
||||
{
|
||||
}
|
||||
|
||||
public ZlibDeflateStream(int level, Stream baseStream, bool leaveOpen) : this(level, false, 0, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
public ZlibDeflateStream(int level, bool headerless, Stream baseStream, bool leaveOpen) : this(level, headerless, 0, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
public ZlibDeflateStream(int level, int bufferSize, Stream baseStream, bool leaveOpen) : this(level, false, bufferSize, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
public ZlibDeflateStream(int level, bool headerless, int bufferSize, Stream baseStream, bool leaveOpen)
|
||||
{
|
||||
this.Level = level;
|
||||
this.Headerless = headerless;
|
||||
this.BaseStream = baseStream;
|
||||
_leaveOpen = leaveOpen;
|
||||
_s = null;
|
||||
_b = new byte[bufferSize == 0 ? 0x10000 : bufferSize];
|
||||
}
|
||||
|
||||
public override bool CanRead => false;
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public override long Length => _p;
|
||||
|
||||
public override long Position { get => _p; set => throw new NotImplementedException(); }
|
||||
|
||||
public int Level { get; }
|
||||
public bool Headerless { get; }
|
||||
public Stream BaseStream { get; }
|
||||
public string Version { get => ZLib.zlibVersion(); }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
unsafe public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (buffer == null) throw new ArgumentNullException();
|
||||
if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
|
||||
if ((offset + count) > buffer.Length) throw new ArgumentException();
|
||||
|
||||
int err = 0;
|
||||
int hdr = 0;
|
||||
|
||||
if (_s == null)
|
||||
{
|
||||
_s = new ZLib.z_stream_s();
|
||||
ZLib.deflateInit_(_s, this.Level, this.Version, 0); //0 = sizeof(z_stream_s) not used
|
||||
if (this.Headerless)
|
||||
hdr = 2;
|
||||
_s.total_in = 0u;
|
||||
_s.total_out = 0u;
|
||||
_s.avail_in = 0u;
|
||||
_s.avail_out = 0u;
|
||||
}
|
||||
|
||||
_s.avail_in = (uint)count;
|
||||
|
||||
fixed (byte* i = buffer, o = _b)
|
||||
{
|
||||
_s.next_in = i;
|
||||
_s.next_out = o + _s.total_out;
|
||||
|
||||
while (err >= 0 && _s.avail_in != 0) //process the buffer
|
||||
{
|
||||
if (_s.avail_out == 0) //get more data
|
||||
{
|
||||
if (_s.total_out != 0)
|
||||
{
|
||||
if (hdr != 0)
|
||||
{
|
||||
BaseStream.Write(_b!, hdr, (int)_s.total_out - hdr);
|
||||
_s.total_out -= (uint)hdr;
|
||||
hdr = 0;
|
||||
}
|
||||
else
|
||||
BaseStream.Write(_b!, 0, (int)_s.total_out);
|
||||
}
|
||||
_p += _s.total_out;
|
||||
_s.avail_out = (uint)_b!.Length;
|
||||
_s.next_out = o;
|
||||
_s.total_out = 0;
|
||||
}
|
||||
|
||||
if (_s.avail_in != 0 || _s.avail_out != 0)
|
||||
err = ZLib.deflate(_s, 2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allow blocks to be written to the base stream. Call when write is finished with.
|
||||
/// Used for creating block seekable files. The caller must manage blocks, indexes and lengths
|
||||
/// </summary>
|
||||
unsafe public long BlockFlush()
|
||||
{
|
||||
//finish previous stream
|
||||
if (_s != null)
|
||||
{
|
||||
int err = 0;
|
||||
fixed (byte* o = _b)
|
||||
{
|
||||
_s.next_in = null;
|
||||
_s.avail_in = 0;
|
||||
_s.next_out = o + _s.total_out; //point to correct location
|
||||
|
||||
int hdr = _p == 0 && Headerless ? 2 : 0;
|
||||
while (err == 0 && (_s.total_out != 0 || _s.state!.pending != 0))
|
||||
{
|
||||
this.BaseStream.Write(_b!, hdr, (int)_s.total_out - hdr);
|
||||
_s.avail_out = (uint)_b!.Length;
|
||||
_p += _s.total_out - hdr;
|
||||
hdr = 0;
|
||||
_s.next_out = o;
|
||||
_s.total_out = 0;
|
||||
if (_s.state!.pending != 0)
|
||||
err = ZLib.deflate(_s, 2);
|
||||
}
|
||||
|
||||
err = ZLib.deflate(_s, 4);
|
||||
|
||||
}
|
||||
ZLib.deflateEnd(_s);
|
||||
_s = null;
|
||||
}
|
||||
long ret = _p;
|
||||
_p = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsafe protected override void Dispose(bool disposing)
|
||||
{
|
||||
this.BlockFlush();
|
||||
_b = null;
|
||||
if (!_leaveOpen)
|
||||
this.BaseStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
170
SabreTools.Compression/zlib/ZlibInflateStream.cs
Normal file
170
SabreTools.Compression/zlib/ZlibInflateStream.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public class ZlibInflateStream : Stream
|
||||
{
|
||||
private readonly bool _leaveOpen;
|
||||
private ZLib.z_stream_s? _s;
|
||||
private long _p;
|
||||
private byte[]? _b;
|
||||
private bool _complete;
|
||||
|
||||
public ZlibInflateStream(Stream baseStream) : this(0, false, 0, baseStream, false)
|
||||
{
|
||||
}
|
||||
|
||||
public ZlibInflateStream(Stream baseStream, bool leaveOpen) : this(0, false, 0, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
public ZlibInflateStream(bool headerless, Stream baseStream, bool leaveOpen) : this(0, headerless, 0, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
public ZlibInflateStream(bool headerless, int bufferSize, Stream baseStream, bool leaveOpen) : this(0, headerless, bufferSize, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
public ZlibInflateStream(int bufferSize, Stream baseStream, bool leaveOpen) : this(0, false, bufferSize, baseStream, leaveOpen)
|
||||
{
|
||||
}
|
||||
|
||||
public ZlibInflateStream(long maxRead, bool headerless, int bufferSize, Stream baseStream, bool leaveOpen)
|
||||
{
|
||||
this.MaxRead = maxRead == 0 ? int.MaxValue : maxRead;
|
||||
this.Headerless = headerless;
|
||||
this.BaseStream = baseStream;
|
||||
_leaveOpen = leaveOpen;
|
||||
_s = null;
|
||||
_b = new byte[bufferSize == 0 ? 0x10000 : bufferSize];
|
||||
_complete = false;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public override long Length => _p;
|
||||
|
||||
public override long Position { get => _p; set => throw new NotImplementedException(); }
|
||||
public long MaxRead { get; private set; }
|
||||
public bool Headerless { get; }
|
||||
public Stream BaseStream { get; }
|
||||
public string Version { get => ZLib.zlibVersion(); }
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
unsafe public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
|
||||
if (buffer == null) throw new ArgumentNullException();
|
||||
if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
|
||||
if ((offset + count) > buffer.Length) throw new ArgumentException();
|
||||
if (_complete)
|
||||
return 0;
|
||||
|
||||
int err = 0;
|
||||
int hdr = 0;
|
||||
if (_s == null)
|
||||
{
|
||||
_s = new ZLib.z_stream_s();
|
||||
ZLib.inflateInit_(_s, this.Version, 0); //0 = sizeof(z_stream_s) not used
|
||||
if (this.Headerless)
|
||||
{
|
||||
_b![0] = 0x78;
|
||||
_b[1] = 0x9c; //da
|
||||
hdr = 2;
|
||||
}
|
||||
_s.total_in = 0u;
|
||||
_s.total_out = 0u;
|
||||
_s.avail_in = 0u;
|
||||
_s.avail_out = 0u;
|
||||
}
|
||||
|
||||
int read;
|
||||
_s.avail_out = (uint)count;
|
||||
|
||||
fixed (byte* i = _b, o = buffer)
|
||||
{
|
||||
_s.next_in = i + _s.total_in;
|
||||
_s.next_out = o;
|
||||
|
||||
while (err == 0 && (_s.avail_out != 0 && !_complete)) //process the buffer
|
||||
{
|
||||
if (_s.avail_in == 0) //get more data
|
||||
{
|
||||
_s.total_in = 0;
|
||||
read = (int)Math.Min(this.MaxRead - _p, (long)_b!.Length);
|
||||
if (hdr != 0) //test once to save on the extra calculations
|
||||
{
|
||||
_s.avail_in = (uint)(hdr + (read = BaseStream.Read(_b, hdr, Math.Min(read, _b.Length - hdr))));
|
||||
hdr = 0;
|
||||
}
|
||||
else
|
||||
_s.avail_in = (uint)(read = BaseStream.Read(_b, 0, read));
|
||||
_complete = read == 0;
|
||||
_p += (long)read;
|
||||
_s.next_in = i;
|
||||
}
|
||||
|
||||
if (_s.avail_in != 0 || (!_complete && _s.total_out != 0))
|
||||
err = ZLib.inflate(_s, 2);
|
||||
}
|
||||
}
|
||||
|
||||
uint ret = _s.total_out;
|
||||
_s.total_out = 0u;
|
||||
return (int)ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allow blocks to be read from the base stream without overreading. Call when write is finished with.
|
||||
/// Used for reading block seekable files. The caller must manage blocks, indexes and lengths. Seek the BaseStream
|
||||
/// </summary>
|
||||
public long BlockFlush(int maxRead)
|
||||
{
|
||||
this.MaxRead = maxRead;
|
||||
if (_s != null)
|
||||
{
|
||||
ZLib.deflateEnd(_s);
|
||||
_s = null;
|
||||
}
|
||||
_complete = false;
|
||||
long ret = _p;
|
||||
_p = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
BlockFlush(0);
|
||||
_complete = true;
|
||||
_b = null;
|
||||
if (!_leaveOpen)
|
||||
this.BaseStream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
7101
SabreTools.Compression/zlib/zlib.cs
Normal file
7101
SabreTools.Compression/zlib/zlib.cs
Normal file
File diff suppressed because one or more lines are too long
47
SabreTools.Compression/zlib/zlibConst.cs
Normal file
47
SabreTools.Compression/zlib/zlibConst.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace SabreTools.Compression.zlib
|
||||
{
|
||||
public static class zlibConst
|
||||
{
|
||||
public const int Z_NO_FLUSH = 0;
|
||||
public const int Z_PARTIAL_FLUSH = 1;
|
||||
public const int Z_SYNC_FLUSH = 2;
|
||||
public const int Z_FULL_FLUSH = 3;
|
||||
public const int Z_FINISH = 4;
|
||||
public const int Z_BLOCK = 5;
|
||||
public const int Z_TREES = 6;
|
||||
|
||||
public const int Z_OK = 0;
|
||||
public const int Z_STREAM_END = 1;
|
||||
public const int Z_NEED_DICT = 2;
|
||||
public const int Z_ERRNO = (-1);
|
||||
public const int Z_STREAM_ERROR = (-2);
|
||||
public const int Z_DATA_ERROR = (-3);
|
||||
public const int Z_MEM_ERROR = (-4);
|
||||
public const int Z_BUF_ERROR = (-5);
|
||||
public const int Z_VERSION_ERROR = (-6);
|
||||
|
||||
/// <summary>
|
||||
/// Get the zlib result name from an integer
|
||||
/// </summary>
|
||||
/// <param name="result">Integer to translate to the result name</param>
|
||||
/// <returns>Name of the result, the integer as a string otherwise</returns>
|
||||
public static string ToZlibConstName(this int result)
|
||||
{
|
||||
return result switch
|
||||
{
|
||||
Z_OK => "Z_OK",
|
||||
Z_STREAM_END => "Z_STREAM_END",
|
||||
Z_NEED_DICT => "Z_NEED_DICT",
|
||||
|
||||
Z_ERRNO => "Z_ERRNO",
|
||||
Z_STREAM_ERROR => "Z_STREAM_ERROR",
|
||||
Z_DATA_ERROR => "Z_DATA_ERROR",
|
||||
Z_MEM_ERROR => "Z_MEM_ERROR",
|
||||
Z_BUF_ERROR => "Z_BUF_ERROR",
|
||||
Z_VERSION_ERROR => "Z_VERSION_ERROR",
|
||||
|
||||
_ => result.ToString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Test/Program.cs
Normal file
10
Test/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Test
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
// No implementation, used for experimentation
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Test/Test.csproj
Normal file
32
Test/Test.csproj
Normal file
@@ -0,0 +1,32 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<OutputType>Exe</OutputType>
|
||||
<CheckEolTargetFramework>false</CheckEolTargetFramework>
|
||||
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Support All Frameworks -->
|
||||
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net4`))">
|
||||
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="$(TargetFramework.StartsWith(`netcoreapp`)) OR $(TargetFramework.StartsWith(`net5`))">
|
||||
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net6`)) OR $(TargetFramework.StartsWith(`net7`)) OR $(TargetFramework.StartsWith(`net8`)) OR $(TargetFramework.StartsWith(`net9`))">
|
||||
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith(`osx-arm`))">
|
||||
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SabreTools.Compression\SabreTools.Compression.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,16 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all compressor implementations
|
||||
/// </summary>
|
||||
public abstract class BaseCompressor : mspack_file
|
||||
{
|
||||
#if NET48
|
||||
public mspack_system system { get; set; }
|
||||
#else
|
||||
public mspack_system? system { get; set; }
|
||||
#endif
|
||||
|
||||
public MSPACK_ERR error { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all decompressor implementations
|
||||
/// </summary>
|
||||
public abstract class BaseDecompressor : mspack_file
|
||||
{
|
||||
#if NET48
|
||||
public mspack_system system { get; set; }
|
||||
#else
|
||||
public mspack_system? system { get; set; }
|
||||
#endif
|
||||
|
||||
public MSPACK_ERR error { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public unsafe abstract class BitStream
|
||||
{
|
||||
/// <summary>
|
||||
/// I/O routines
|
||||
/// </summary>
|
||||
public mspack_system sys { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input file handle
|
||||
/// </summary>
|
||||
public mspack_file input { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Output file handle
|
||||
/// </summary>
|
||||
public mspack_file output { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Decompression offset within window
|
||||
/// </summary>
|
||||
public uint window_posn { get; set; }
|
||||
|
||||
#region I/O buffering
|
||||
|
||||
public byte* inbuf { get; set; }
|
||||
|
||||
public byte* i_ptr { get; set; }
|
||||
|
||||
public byte* i_end { get; set; }
|
||||
|
||||
public byte* o_ptr { get; set; }
|
||||
|
||||
public byte* o_end { get; set; }
|
||||
|
||||
public int input_end { get; set; }
|
||||
|
||||
public uint bit_buffer { get; set; }
|
||||
|
||||
public uint bits_left { get; set; }
|
||||
|
||||
public uint inbuf_size { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
public MSPACK_ERR error { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,231 +0,0 @@
|
||||
using static SabreTools.Compression.libmspack.macros;
|
||||
using static SabreTools.Compression.libmspack.CAB.Constants;
|
||||
|
||||
namespace SabreTools.Compression.libmspack.CAB
|
||||
{
|
||||
public unsafe class CABSystem : mspack_default_system
|
||||
{
|
||||
/// <summary>
|
||||
/// cabd_sys_read is the internal reader function which the decompressors
|
||||
/// use. will read data blocks (and merge split blocks) from the cabinet
|
||||
/// and serve the read bytes to the decompressors
|
||||
/// </summary>
|
||||
public override int read(mspack_file file, void* buffer, int bytes)
|
||||
{
|
||||
Decompressor self = (Decompressor)file;
|
||||
byte* buf = (byte*)buffer;
|
||||
mspack_system sys = self.system;
|
||||
int avail, todo, outlen = 0, ignore_cksum, ignore_blocksize;
|
||||
|
||||
ignore_cksum = self.salvage != 0 || (self.fix_mszip != 0 && ((MSCAB_COMP)((int)self.d.comp_type & cffoldCOMPTYPE_MASK) == MSCAB_COMP.MSCAB_COMP_MSZIP)) == true ? 1 : 0;
|
||||
ignore_blocksize = self.salvage;
|
||||
|
||||
todo = bytes;
|
||||
while (todo > 0)
|
||||
{
|
||||
avail = (int)(self.d.i_end - self.d.i_ptr);
|
||||
|
||||
// If out of input data, read a new block
|
||||
if (avail != 0)
|
||||
{
|
||||
// Copy as many input bytes available as possible
|
||||
if (avail > todo) avail = todo;
|
||||
sys.copy(self.d.i_ptr, buf, avail);
|
||||
self.d.i_ptr += avail;
|
||||
buf += avail;
|
||||
todo -= avail;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Out of data, read a new block
|
||||
|
||||
// Check if we're out of input blocks, advance block counter
|
||||
if (self.d.block++ >= self.d.folder.num_blocks)
|
||||
{
|
||||
if (self.salvage == 0)
|
||||
{
|
||||
self.read_error = MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Console.Error.WriteLine("Ran out of CAB input blocks prematurely");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Read a block
|
||||
self.read_error = ReadBlock(sys, self.d, ref outlen, ignore_cksum, ignore_blocksize);
|
||||
if (self.read_error != MSPACK_ERR.MSPACK_ERR_OK) return -1;
|
||||
self.d.outlen += outlen;
|
||||
|
||||
// Special Quantum hack -- trailer byte to allow the decompressor
|
||||
// to realign itself. CAB Quantum blocks, unlike LZX blocks, can have
|
||||
// anything from 0 to 4 trailing null bytes.
|
||||
if ((MSCAB_COMP)((int)self.d.comp_type & cffoldCOMPTYPE_MASK) == MSCAB_COMP.MSCAB_COMP_QUANTUM)
|
||||
{
|
||||
*self.d.i_end++ = 0xFF;
|
||||
}
|
||||
|
||||
// Is this the last block?
|
||||
if (self.d.block >= self.d.folder.num_blocks)
|
||||
{
|
||||
if ((MSCAB_COMP)((int)self.d.comp_type & cffoldCOMPTYPE_MASK) == MSCAB_COMP.MSCAB_COMP_LZX)
|
||||
{
|
||||
// Special LZX hack -- on the last block, inform LZX of the
|
||||
// size of the output data stream.
|
||||
lzxd_set_output_length((lzxd_stream)self.d.state, self.d.outlen);
|
||||
}
|
||||
}
|
||||
} /* if (avail) */
|
||||
} /* while (todo > 0) */
|
||||
return bytes - todo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// cabd_sys_write is the internal writer function which the decompressors
|
||||
/// use. it either writes data to disk (self.d.outfh) with the real
|
||||
/// sys.write() function, or does nothing with the data when
|
||||
/// self.d.outfh == null. advances self.d.offset
|
||||
/// </summary>
|
||||
public override int write(mspack_file file, void* buffer, int bytes)
|
||||
{
|
||||
Decompressor self = (Decompressor)file;
|
||||
self.d.offset += (uint)bytes;
|
||||
if (self.d.outfh != null)
|
||||
{
|
||||
return self.system.write(self.d.outfh, buffer, bytes);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a whole data block from a cab file. the block may span more than
|
||||
/// one cab file, if it does then the fragments will be reassembled
|
||||
/// </summary>
|
||||
private static MSPACK_ERR ReadBlock(mspack_system sys, mscabd_decompress_state d, ref int @out, int ignore_cksum, int ignore_blocksize)
|
||||
{
|
||||
FixedArray<byte> hdr = new FixedArray<byte>(cfdata_SIZEOF);
|
||||
uint cksum;
|
||||
int len, full_len;
|
||||
|
||||
// Reset the input block pointer and end of block pointer
|
||||
d.i_ptr = d.i_end = d.input;
|
||||
|
||||
do
|
||||
{
|
||||
// Read the block header
|
||||
if (sys.read(d.infh, hdr, cfdata_SIZEOF) != cfdata_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// Skip any reserved block headers
|
||||
if (d.data.cab.block_resv != 0 &&
|
||||
sys.seek(d.infh, d.data.cab.block_resv, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_CUR) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
|
||||
// Blocks must not be over CAB_INPUTMAX in size
|
||||
len = EndGetI16(hdr, cfdata_CompressedSize);
|
||||
full_len = (int)(d.i_end - d.i_ptr + len); // Include cab-spanning blocks */
|
||||
if (full_len > CAB_INPUTMAX)
|
||||
{
|
||||
System.Console.Error.WriteLine($"Block size {full_len} > CAB_INPUTMAX");
|
||||
// In salvage mode, blocks can be 65535 bytes but no more than that
|
||||
if (ignore_blocksize == 0 || full_len > CAB_INPUTMAX_SALVAGE)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
}
|
||||
|
||||
// Blocks must not expand to more than CAB_BLOCKMAX
|
||||
if (EndGetI16(hdr, cfdata_UncompressedSize) > CAB_BLOCKMAX)
|
||||
{
|
||||
System.Console.Error.WriteLine("block size > CAB_BLOCKMAX");
|
||||
if (ignore_blocksize == 0) return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Read the block data
|
||||
if (sys.read(d.infh, d.i_end, len) != len)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// Perform checksum test on the block (if one is stored)
|
||||
if ((cksum = EndGetI32(hdr, cfdata_CheckSum)) != 0)
|
||||
{
|
||||
uint sum2 = Checksum(d.i_end, (uint)len, 0);
|
||||
if (Checksum(hdr, 4, 4, sum2) != cksum)
|
||||
{
|
||||
if (ignore_cksum == 0) return MSPACK_ERR.MSPACK_ERR_CHECKSUM;
|
||||
sys.message(d.infh, "WARNING; bad block checksum found");
|
||||
}
|
||||
}
|
||||
|
||||
// Advance end of block pointer to include newly read data
|
||||
d.i_end += len;
|
||||
|
||||
// Uncompressed size == 0 means this block was part of a split block
|
||||
// and it continues as the first block of the next cabinet in the set.
|
||||
// otherwise, this is the last part of the block, and no more block
|
||||
// reading needs to be done.
|
||||
|
||||
// EXIT POINT OF LOOP -- uncompressed size != 0
|
||||
if ((@out = EndGetI16(hdr, cfdata_UncompressedSize)) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
// Otherwise, advance to next cabinet
|
||||
|
||||
// Close current file handle
|
||||
sys.close(d.infh);
|
||||
d.infh = null;
|
||||
|
||||
// Aadvance to next member in the cabinet set
|
||||
if ((d.data = d.data.next) == null)
|
||||
{
|
||||
sys.message(d.infh, "WARNING; ran out of cabinets in set. Are any missing?");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Open next cab file
|
||||
d.incab = d.data.cab;
|
||||
if ((d.infh = sys.open(d.incab.filename, MSPACK_SYS_OPEN.MSPACK_SYS_OPEN_READ)) == null)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_OPEN;
|
||||
}
|
||||
|
||||
// Seek to start of data blocks
|
||||
if (sys.seek(d.infh, d.data.offset, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_START) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
// Not reached
|
||||
return MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
private static uint Checksum(FixedArray<byte> data, int ptr, uint bytes, uint cksum)
|
||||
{
|
||||
uint len, ul = 0;
|
||||
|
||||
for (len = bytes >> 2; len-- > 0; ptr += 4)
|
||||
{
|
||||
cksum ^= EndGetI32(data, ptr);
|
||||
}
|
||||
|
||||
switch (bytes & 3)
|
||||
{
|
||||
case 3: ul |= (uint)(data[ptr++] << 16); goto case 2;
|
||||
case 2: ul |= (uint)(data[ptr++] << 8); goto case 1;
|
||||
case 1: ul |= data[ptr]; break;
|
||||
}
|
||||
cksum ^= ul;
|
||||
|
||||
return cksum;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.CAB
|
||||
{
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
public class Compressor : BaseCompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new CAB compressor
|
||||
/// </summary>
|
||||
public Compressor()
|
||||
{
|
||||
this.system = new CABSystem();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.CAB
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
/* structure offsets */
|
||||
public const byte cfhead_Signature = 0x00;
|
||||
public const byte cfhead_CabinetSize = 0x08;
|
||||
public const byte cfhead_FileOffset = 0x10;
|
||||
public const byte cfhead_MinorVersion = 0x18;
|
||||
public const byte cfhead_MajorVersion = 0x19;
|
||||
public const byte cfhead_NumFolders = 0x1A;
|
||||
public const byte cfhead_NumFiles = 0x1C;
|
||||
public const byte cfhead_Flags = 0x1E;
|
||||
public const byte cfhead_SetID = 0x20;
|
||||
public const byte cfhead_CabinetIndex = 0x22;
|
||||
public const byte cfhead_SIZEOF = 0x24;
|
||||
public const byte cfheadext_HeaderReserved = 0x00;
|
||||
public const byte cfheadext_FolderReserved = 0x02;
|
||||
public const byte cfheadext_DataReserved = 0x03;
|
||||
public const byte cfheadext_SIZEOF = 0x04;
|
||||
public const byte cffold_DataOffset = 0x00;
|
||||
public const byte cffold_NumBlocks = 0x04;
|
||||
public const byte cffold_CompType = 0x06;
|
||||
public const byte cffold_SIZEOF = 0x08;
|
||||
public const byte cffile_UncompressedSize = 0x00;
|
||||
public const byte cffile_FolderOffset = 0x04;
|
||||
public const byte cffile_FolderIndex = 0x08;
|
||||
public const byte cffile_Date = 0x0A;
|
||||
public const byte cffile_Time = 0x0C;
|
||||
public const byte cffile_Attribs = 0x0E;
|
||||
public const byte cffile_SIZEOF = 0x10;
|
||||
public const byte cfdata_CheckSum = 0x00;
|
||||
public const byte cfdata_CompressedSize = 0x04;
|
||||
public const byte cfdata_UncompressedSize = 0x06;
|
||||
public const byte cfdata_SIZEOF = 0x08;
|
||||
|
||||
/* flags */
|
||||
public const ushort cffoldCOMPTYPE_MASK = 0x000f;
|
||||
public const ushort cffileCONTINUED_FROM_PREV = 0xFFFD;
|
||||
public const ushort cffileCONTINUED_TO_NEXT = 0xFFFE;
|
||||
public const ushort cffileCONTINUED_PREV_AND_NEXT = 0xFFFF;
|
||||
|
||||
/* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
|
||||
* blocks have zero growth. MSZIP guarantees that it won't grow above
|
||||
* uncompressed size by more than 12 bytes. LZX guarantees it won't grow
|
||||
* more than 6144 bytes. Quantum has no documentation, but the largest
|
||||
* block seen in the wild is 337 bytes above uncompressed size.
|
||||
*/
|
||||
public const int CAB_BLOCKMAX = 32768;
|
||||
public const int CAB_INPUTMAX = CAB_BLOCKMAX + 6144;
|
||||
|
||||
/* input buffer needs to be CAB_INPUTMAX + 1 byte to allow for max-sized block
|
||||
* plus 1 trailer byte added by cabd_sys_read_block() for Quantum alignment.
|
||||
*
|
||||
* When MSCABD_PARAM_SALVAGE is set, block size is not checked so can be
|
||||
* up to 65535 bytes, so max input buffer size needed is 65535 + 1
|
||||
*/
|
||||
public const int CAB_INPUTMAX_SALVAGE = 65535;
|
||||
public const int CAB_INPUTBUF = CAB_INPUTMAX_SALVAGE + 1;
|
||||
|
||||
/* There are no more than 65535 data blocks per folder, so a folder cannot
|
||||
* be more than 32768*65535 bytes in length. As files cannot span more than
|
||||
* one folder, this is also their max offset, length and offset+length limit.
|
||||
*/
|
||||
public const int CAB_FOLDERMAX = 65535;
|
||||
public const int CAB_LENGTHMAX = CAB_BLOCKMAX * CAB_FOLDERMAX;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,65 +0,0 @@
|
||||
using static SabreTools.Compression.libmspack.CAB.Constants;
|
||||
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public unsafe static class cab
|
||||
{
|
||||
#region decomp
|
||||
|
||||
/// <summary>
|
||||
/// cabd_free_decomp frees decompression state, according to which method
|
||||
/// was used.
|
||||
/// </summary>
|
||||
public static MSPACK_ERR cabd_init_decomp(CAB.Decompressor self, MSCAB_COMP ct)
|
||||
{
|
||||
mspack_file fh = self;
|
||||
|
||||
self.d.comp_type = ct;
|
||||
|
||||
switch ((MSCAB_COMP)((int)ct & cffoldCOMPTYPE_MASK))
|
||||
{
|
||||
case MSCAB_COMP.MSCAB_COMP_NONE:
|
||||
self.d = new None.DecompressState(self.d);
|
||||
self.d.state = new None.State(self.d.sys, fh, fh, self.buf_size);
|
||||
break;
|
||||
case MSCAB_COMP.MSCAB_COMP_MSZIP:
|
||||
self.d = new mscabd_mszipd_decompress_state();
|
||||
self.d.state = mszipd_init(self.d.sys, fh, fh, self.buf_size, self.fix_mszip);
|
||||
break;
|
||||
case MSCAB_COMP.MSCAB_COMP_QUANTUM:
|
||||
self.d = new mscabd_qtmd_decompress_state();
|
||||
self.d.state = qtmd_init(self.d.sys, fh, fh, ((int)ct >> 8) & 0x1f, self.buf_size);
|
||||
break;
|
||||
case MSCAB_COMP.MSCAB_COMP_LZX:
|
||||
self.d = new mscabd_lzxd_decompress_state();
|
||||
self.d.state = lzxd_init(self.d.sys, fh, fh, ((int)ct >> 8) & 0x1f, 0, self.buf_size, 0, 0);
|
||||
break;
|
||||
default:
|
||||
return self.error = MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
return self.error = (self.d.state != null) ? MSPACK_ERR.MSPACK_ERR_OK : MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// cabd_init_decomp initialises decompression state, according to which
|
||||
/// decompression method was used. relies on self.d.folder being the same
|
||||
/// as when initialised.
|
||||
/// </summary>
|
||||
public static void cabd_free_decomp(CAB.Decompressor self)
|
||||
{
|
||||
if (self == null || self.d == null || self.d.state == null) return;
|
||||
|
||||
switch ((MSCAB_COMP)((int)self.d.comp_type & cffoldCOMPTYPE_MASK))
|
||||
{
|
||||
case MSCAB_COMP.MSCAB_COMP_MSZIP: mszipd_free((mszipd_stream)self.d.state); break;
|
||||
case MSCAB_COMP.MSCAB_COMP_QUANTUM: qtmd_free((qtmd_stream)self.d.state); break;
|
||||
case MSCAB_COMP.MSCAB_COMP_LZX: lzxd_free((lzxd_stream)self.d.state); break;
|
||||
}
|
||||
|
||||
//self.d.decompress = null;
|
||||
self.d.state = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a single cabinet file.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
///
|
||||
/// If this cabinet is part of a merged cabinet set, the #files and #folders
|
||||
/// fields are common to all cabinets in the set, and will be identical.
|
||||
/// </summary>
|
||||
/// <see cref="mscab_decompressor::open()"/>
|
||||
/// <see cref="mscab_decompressor::close()"/>
|
||||
/// <see cref="mscab_decompressor::search()"/>
|
||||
public unsafe class mscabd_cabinet
|
||||
{
|
||||
/// <summary>
|
||||
/// The next cabinet in a chained list, if this cabinet was opened with
|
||||
/// mscab_decompressor::search(). May be null to mark the end of the
|
||||
/// list.
|
||||
/// </summary>
|
||||
public mscabd_cabinet next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the cabinet. More correctly, the filename of the
|
||||
/// physical file that the cabinet resides in. This is given by the
|
||||
/// library user and may be in any format.
|
||||
/// </summary>
|
||||
public string filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The file offset of cabinet within the physical file it resides in.
|
||||
/// </summary>
|
||||
public long base_offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The length of the cabinet file in bytes.
|
||||
/// </summary>
|
||||
public uint length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The previous cabinet in a cabinet set, or null.
|
||||
/// </summary>
|
||||
public mscabd_cabinet prevcab { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The next cabinet in a cabinet set, or null.
|
||||
/// </summary>
|
||||
public mscabd_cabinet nextcab { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the previous cabinet in a cabinet set, or null.
|
||||
/// </summary>
|
||||
public string prevname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the next cabinet in a cabinet set, or null.
|
||||
/// </summary>
|
||||
public string nextname { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the disk containing the previous cabinet in a cabinet
|
||||
/// set, or null.
|
||||
/// </summary>
|
||||
public string previnfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the disk containing the next cabinet in a cabinet set,
|
||||
/// or null.
|
||||
/// </summary>
|
||||
public string nextinfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of all files in the cabinet or cabinet set.
|
||||
/// </summary>
|
||||
public mscabd_file files { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of all folders in the cabinet or cabinet set.
|
||||
/// </summary>
|
||||
public mscabd_folder folders { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The set ID of the cabinet. All cabinets in the same set should have
|
||||
/// the same set ID.
|
||||
/// </summary>
|
||||
public ushort set_id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The index number of the cabinet within the set. Numbering should
|
||||
/// start from 0 for the first cabinet in the set, and increment by 1 for
|
||||
/// each following cabinet.
|
||||
/// </summary>
|
||||
public ushort set_index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of bytes reserved in the header area of the cabinet.
|
||||
///
|
||||
/// If this is non-zero and flags has MSCAB_HDR_RESV set, this data can
|
||||
/// be read by the calling application. It is of the given length,
|
||||
/// located at offset (base_offset + MSCAB_HDR_RESV_OFFSET) in the
|
||||
/// cabinet file.
|
||||
/// </summary>
|
||||
public ushort header_resv { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Header flags.
|
||||
/// </summary>
|
||||
/// <see cref="prevname"/>
|
||||
/// <see cref="previnfo"/>
|
||||
/// <see cref="nextname"/>
|
||||
/// <see cref="nextinfo"/>
|
||||
/// <see cref="header_resv"/>
|
||||
public MSCAB_HDR flags { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Offset to data blocks
|
||||
/// </summary>
|
||||
public long blocks_off { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reserved space in data blocks
|
||||
/// </summary>
|
||||
public int block_resv { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
using SabreTools.Compression.libmspack.CAB;
|
||||
using static SabreTools.Compression.libmspack.CAB.Constants;
|
||||
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public unsafe abstract class mscabd_decompress_state
|
||||
{
|
||||
/// <summary>
|
||||
/// Current folder we're extracting from
|
||||
/// </summary>
|
||||
public mscabd_folder folder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Current folder split we're in
|
||||
/// </summary>
|
||||
public mscabd_folder_data data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Uncompressed offset within folder
|
||||
/// </summary>
|
||||
public uint offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Which block are we decompressing?
|
||||
/// </summary>
|
||||
public uint block { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cumulative sum of block output sizes
|
||||
/// </summary>
|
||||
public long outlen { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Special I/O code for decompressor
|
||||
/// </summary>
|
||||
public CABSystem sys { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Type of compression used by folder
|
||||
/// </summary>
|
||||
public MSCAB_COMP comp_type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Decompressor state
|
||||
/// </summary>
|
||||
public object state { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet where input data comes from
|
||||
/// </summary>
|
||||
public mscabd_cabinet incab { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input file handle
|
||||
/// </summary>
|
||||
public mspack_file infh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Output file handle
|
||||
/// </summary>
|
||||
public mspack_file outfh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input data consumed
|
||||
/// </summary>
|
||||
public byte* i_ptr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input data end
|
||||
/// </summary>
|
||||
public byte* i_end { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// One input block of data
|
||||
/// </summary>
|
||||
public FixedArray<byte> input { get; set; } = new FixedArray<byte>(CAB_INPUTBUF);
|
||||
|
||||
/// <summary>
|
||||
/// Decompressor code
|
||||
/// </summary>
|
||||
public abstract MSPACK_ERR decompress(object data, long offset);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a single file in a cabinet or cabinet set.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public class mscabd_file
|
||||
{
|
||||
/// <summary>
|
||||
/// The next file in the cabinet or cabinet set, or null if this is the
|
||||
/// final file.
|
||||
/// </summary>
|
||||
public mscabd_file next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the file.
|
||||
///
|
||||
/// A null terminated string of up to 255 bytes in length, it may be in
|
||||
/// either ISO-8859-1 or UTF8 format, depending on the file attributes.
|
||||
/// </summary>
|
||||
public string filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The uncompressed length of the file, in bytes.
|
||||
/// </summary>
|
||||
public uint length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File attributes.
|
||||
/// </summary>
|
||||
public MSCAB_ATTRIB attribs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified time, hour field.
|
||||
/// </summary>
|
||||
public char time_h { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified time, minute field.
|
||||
/// </summary>
|
||||
public char time_m { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified time, second field.
|
||||
/// </summary>
|
||||
public char time_s { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified date, day field.
|
||||
/// </summary>
|
||||
public char date_d { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified date, month field.
|
||||
/// </summary>
|
||||
public char date_m { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// File's last modified date, year field.
|
||||
/// </summary>
|
||||
public int date_y;
|
||||
|
||||
/// <summary>
|
||||
/// A pointer to the folder that contains this file.
|
||||
/// </summary>
|
||||
public mscabd_folder folder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The uncompressed offset of this file in its folder.
|
||||
/// </summary>
|
||||
public uint offset { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a single folder in a cabinet or cabinet set.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
///
|
||||
/// A folder is a single compressed stream of data. When uncompressed, it
|
||||
/// holds the data of one or more files. A folder may be split across more
|
||||
/// than one cabinet.
|
||||
/// </summary>
|
||||
public class mscabd_folder
|
||||
{
|
||||
/// <summary>
|
||||
/// A pointer to the next folder in this cabinet or cabinet set, or null
|
||||
/// if this is the final folder.
|
||||
/// </summary>
|
||||
public mscabd_folder next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The compression format used by this folder.
|
||||
///
|
||||
/// The macro MSCABD_COMP_METHOD() should be used on this field to get
|
||||
/// the algorithm used. The macro MSCABD_COMP_LEVEL() should be used to get
|
||||
/// the "compression level".
|
||||
/// </summary>
|
||||
/// <see cref="MSCABD_COMP_METHOD()"/>
|
||||
/// <see cref="MSCABD_COMP_LEVEL()"/>
|
||||
public MSCAB_COMP comp_type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The total number of data blocks used by this folder. This includes
|
||||
/// data blocks present in other files, if this folder spans more than
|
||||
/// one cabinet.
|
||||
/// </summary>
|
||||
public uint num_blocks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Where are the data blocks?
|
||||
/// </summary>
|
||||
public mscabd_folder_data data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// First file needing backwards merge
|
||||
/// </summary>
|
||||
public mscabd_file merge_prev { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// First file needing forwards merge
|
||||
/// </summary>
|
||||
public mscabd_file merge_next { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// There is one of these for every cabinet a folder spans
|
||||
/// </summary>
|
||||
public class mscabd_folder_data
|
||||
{
|
||||
public mscabd_folder_data next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet file of this folder span
|
||||
/// </summary>
|
||||
public mscabd_cabinet cab { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet offset of first datablock
|
||||
/// </summary>
|
||||
public long offset { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.CHM
|
||||
{
|
||||
/// <summary>
|
||||
/// A compressor for .CHM (Microsoft HTMLHelp) files.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
/// <see cref="mspack.DestroyCHMCompressor(Compressor)"/>
|
||||
public class Compressor : BaseCompressor
|
||||
{
|
||||
public string temp_file { get; private set; }
|
||||
|
||||
public int use_temp_file { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new CHM compressor
|
||||
/// </summary>
|
||||
public Compressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a CHM help file.
|
||||
///
|
||||
/// The help file will contain up to two sections, an Uncompressed
|
||||
/// section and potentially an MSCompressed (LZX compressed)
|
||||
/// section.
|
||||
///
|
||||
/// While the contents listing of a CHM file is always in lexical order,
|
||||
/// the file list passed in will be taken as the correct order for files
|
||||
/// within the sections. It is in your interest to place similar files
|
||||
/// together for better compression.
|
||||
///
|
||||
/// There are two modes of generation, to use a temporary file or not to
|
||||
/// use one. See use_temporary_file() for the behaviour of generate() in
|
||||
/// these two different modes.
|
||||
/// </summary>
|
||||
/// <param name="file_list">
|
||||
/// An array of mschmc_file structures, terminated
|
||||
/// with an entry whose mschmc_file::section field is
|
||||
/// #MSCHMC_ENDLIST. The order of the list is
|
||||
/// preserved within each section. The length of any
|
||||
/// mschmc_file::chm_filename string cannot exceed
|
||||
/// roughly 4096 bytes. Each source file must be able
|
||||
/// to supply as many bytes as given in the
|
||||
/// mschmc_file::length field.
|
||||
/// </param>
|
||||
/// <param name="output_file">
|
||||
/// The file to write the generated CHM helpfile to.
|
||||
/// This is passed directly to mspack_system::open()
|
||||
/// </param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
/// <see cref="use_temporary_file(int in string)"/>
|
||||
/// <see cref="set_param(MSCHMC_PARAM, int)"/>
|
||||
public MSPACK_ERR generate(mschmc_file[] file_list, in string output_file) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Specifies whether a temporary file is used during CHM generation.
|
||||
///
|
||||
/// The CHM file format includes data about the compressed section (such
|
||||
/// as its overall size) that is stored in the output CHM file prior to
|
||||
/// the compressed section itself. This unavoidably requires that the
|
||||
/// compressed section has to be generated, before these details can be
|
||||
/// set. There are several ways this can be handled. Firstly, the
|
||||
/// compressed section could be generated entirely in memory before
|
||||
/// writing any of the output CHM file. This approach is not used in
|
||||
/// libmspack, as the compressed section can exceed the addressable
|
||||
/// memory space on most architectures.
|
||||
///
|
||||
/// libmspack has two options, either to write these unknowable sections
|
||||
/// with blank data, generate the compressed section, then re-open the
|
||||
/// output file for update once the compressed section has been
|
||||
/// completed, or to write the compressed section to a temporary file,
|
||||
/// then write the entire output file at once, performing a simple
|
||||
/// file-to-file copy for the compressed section.
|
||||
///
|
||||
/// The simple solution of buffering the entire compressed section in
|
||||
/// memory can still be used, if desired. As the temporary file's
|
||||
/// filename is passed directly to mspack_system::open(), it is possible
|
||||
/// for a custom mspack_system implementation to hold this file in memory,
|
||||
/// without writing to a disk.
|
||||
///
|
||||
/// If a temporary file is set, generate() performs the following
|
||||
/// sequence of events: the temporary file is opened for writing, the
|
||||
/// compression algorithm writes to the temporary file, the temporary
|
||||
/// file is closed. Then the output file is opened for writing and the
|
||||
/// temporary file is re-opened for reading. The output file is written
|
||||
/// and the temporary file is read from. Both files are then closed. The
|
||||
/// temporary file itself is not deleted. If that is desired, the
|
||||
/// temporary file should be deleted after the completion of generate(),
|
||||
/// if it exists.
|
||||
///
|
||||
/// If a temporary file is set not to be used, generate() performs the
|
||||
/// following sequence of events: the output file is opened for writing,
|
||||
/// then it is written and closed. The output file is then re-opened for
|
||||
/// update, the appropriate sections are seek()ed to and re-written, then
|
||||
/// the output file is closed.
|
||||
/// </summary>
|
||||
/// <param name="use_temp_file">
|
||||
/// Non-zero if the temporary file should be used,
|
||||
/// zero if the temporary file should not be used.
|
||||
/// </param>
|
||||
/// <param name="temp_file">
|
||||
/// A file to temporarily write compressed data to,
|
||||
/// before opening it for reading and copying the
|
||||
/// contents to the output file. This is passed
|
||||
/// directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
/// <see cref="generate(mschmc_file[], in string)"/>
|
||||
public MSPACK_ERR use_temporary_file(int use_temp_file, in string temp_file) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Sets a CHM compression engine parameter.
|
||||
///
|
||||
/// The following parameters are defined:
|
||||
///
|
||||
/// - #MSCHMC_PARAM_TIMESTAMP: Sets the "timestamp" of the CHM file
|
||||
/// generated. This is not a timestamp, see mschmd_header::timestamp
|
||||
/// for a description. If this timestamp is 0, generate() will use its
|
||||
/// own algorithm for making a unique ID, based on the lengths and
|
||||
/// names of files in the CHM itself. Defaults to 0, any value between
|
||||
/// 0 and (2^32)-1 is valid.
|
||||
/// - #MSCHMC_PARAM_LANGUAGE: Sets the "language" of the CHM file
|
||||
/// generated. This is not the language used in the CHM file, but the
|
||||
/// language setting of the user who ran the HTMLHelp compiler. It
|
||||
/// defaults to 0x0409. The valid range is between 0x0000 and 0x7F7F.
|
||||
/// - #MSCHMC_PARAM_LZXWINDOW: Sets the size of the LZX history window,
|
||||
/// which is also the interval at which the compressed data stream can be
|
||||
/// randomly accessed. The value is not a size in bytes, but a power of
|
||||
/// two. The default value is 16 (which makes the window 2^16 bytes, or
|
||||
/// 64 kilobytes), the valid range is from 15 (32 kilobytes) to 21 (2
|
||||
/// megabytes).
|
||||
/// - #MSCHMC_PARAM_DENSITY: Sets the "density" of quick reference
|
||||
/// entries stored at the end of directory listing chunk. Each chunk is
|
||||
/// 4096 bytes in size, and contains as many file entries as there is
|
||||
/// room for. At the other end of the chunk, a list of "quick reference"
|
||||
/// pointers is included. The offset of every 'N'th file entry is given a
|
||||
/// quick reference, where N = (2^density) + 1. The default density is
|
||||
/// 2. The smallest density is 0 (N=2), the maximum is 10 (N=1025). As
|
||||
/// each file entry requires at least 5 bytes, the maximum number of
|
||||
/// entries in a single chunk is roughly 800, so the maximum value 10
|
||||
/// can be used to indicate there are no quickrefs at all.
|
||||
/// - #MSCHMC_PARAM_INDEX: Sets whether or not to include quick lookup
|
||||
/// index chunk(s), in addition to normal directory listing chunks. A
|
||||
/// value of zero means no index chunks will be created, a non-zero value
|
||||
/// means index chunks will be created. The default is zero, "don't
|
||||
/// create an index".
|
||||
/// </summary>
|
||||
/// <param name="param">The parameter to set</param>
|
||||
/// <param name="value">The value to set the parameter to</param>
|
||||
/// <returns>
|
||||
/// MSPACK_ERR_OK if all is OK, or MSPACK_ERR_ARGS if there
|
||||
/// is a problem with either parameter or value.
|
||||
/// </returns>
|
||||
/// <see cref="generate(mschmc_file[], in string)"/>
|
||||
public MSPACK_ERR set_param(MSCHMC_PARAM param, int value) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the error code set by the most recently called method.
|
||||
/// </summary>
|
||||
/// <returns>The most recent error code</returns>
|
||||
/// <see cref="set_param(int, int)"/>
|
||||
/// <see cref="generate(mschmc_file[], in string)"/>
|
||||
public MSPACK_ERR last_error() => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.CHM
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const ushort chmhead_Signature = 0x0000;
|
||||
public const ushort chmhead_Version = 0x0004;
|
||||
public const ushort chmhead_HeaderLen = 0x0008;
|
||||
public const ushort chmhead_Unknown1 = 0x000C;
|
||||
public const ushort chmhead_Timestamp = 0x0010;
|
||||
public const ushort chmhead_LanguageID = 0x0014;
|
||||
public const ushort chmhead_GUID1 = 0x0018;
|
||||
public const ushort chmhead_GUID2 = 0x0028;
|
||||
public const ushort chmhead_SIZEOF = 0x0038;
|
||||
|
||||
public const ushort chmhst_OffsetHS0 = 0x0000;
|
||||
public const ushort chmhst_LengthHS0 = 0x0008;
|
||||
public const ushort chmhst_OffsetHS1 = 0x0010;
|
||||
public const ushort chmhst_LengthHS1 = 0x0018;
|
||||
public const ushort chmhst_SIZEOF = 0x0020;
|
||||
public const ushort chmhst3_OffsetCS0 = 0x0020;
|
||||
public const ushort chmhst3_SIZEOF = 0x0028;
|
||||
|
||||
public const ushort chmhs0_Unknown1 = 0x0000;
|
||||
public const ushort chmhs0_Unknown2 = 0x0004;
|
||||
public const ushort chmhs0_FileLen = 0x0008;
|
||||
public const ushort chmhs0_Unknown3 = 0x0010;
|
||||
public const ushort chmhs0_Unknown4 = 0x0014;
|
||||
public const ushort chmhs0_SIZEOF = 0x0018;
|
||||
|
||||
public const ushort chmhs1_Signature = 0x0000;
|
||||
public const ushort chmhs1_Version = 0x0004;
|
||||
public const ushort chmhs1_HeaderLen = 0x0008;
|
||||
public const ushort chmhs1_Unknown1 = 0x000C;
|
||||
public const ushort chmhs1_ChunkSize = 0x0010;
|
||||
public const ushort chmhs1_Density = 0x0014;
|
||||
public const ushort chmhs1_Depth = 0x0018;
|
||||
public const ushort chmhs1_IndexRoot = 0x001C;
|
||||
public const ushort chmhs1_FirstPMGL = 0x0020;
|
||||
public const ushort chmhs1_LastPMGL = 0x0024;
|
||||
public const ushort chmhs1_Unknown2 = 0x0028;
|
||||
public const ushort chmhs1_NumChunks = 0x002C;
|
||||
public const ushort chmhs1_LanguageID = 0x0030;
|
||||
public const ushort chmhs1_GUID = 0x0034;
|
||||
public const ushort chmhs1_Unknown3 = 0x0044;
|
||||
public const ushort chmhs1_Unknown4 = 0x0048;
|
||||
public const ushort chmhs1_Unknown5 = 0x004C;
|
||||
public const ushort chmhs1_Unknown6 = 0x0050;
|
||||
public const ushort chmhs1_SIZEOF = 0x0054;
|
||||
|
||||
public const ushort pmgl_Signature = 0x0000;
|
||||
public const ushort pmgl_QuickRefSize = 0x0004;
|
||||
public const ushort pmgl_Unknown1 = 0x0008;
|
||||
public const ushort pmgl_PrevChunk = 0x000C;
|
||||
public const ushort pmgl_NextChunk = 0x0010;
|
||||
public const ushort pmgl_Entries = 0x0014;
|
||||
public const ushort pmgl_headerSIZEOF = 0x0014;
|
||||
|
||||
public const ushort pmgi_Signature = 0x0000;
|
||||
public const ushort pmgi_QuickRefSize = 0x0004;
|
||||
public const ushort pmgi_Entries = 0x0008;
|
||||
public const ushort pmgi_headerSIZEOF = 0x000C;
|
||||
|
||||
public const ushort lzxcd_Length = 0x0000;
|
||||
public const ushort lzxcd_Signature = 0x0004;
|
||||
public const ushort lzxcd_Version = 0x0008;
|
||||
public const ushort lzxcd_ResetInterval = 0x000C;
|
||||
public const ushort lzxcd_WindowSize = 0x0010;
|
||||
public const ushort lzxcd_CacheSize = 0x0014;
|
||||
public const ushort lzxcd_Unknown1 = 0x0018;
|
||||
public const ushort lzxcd_SIZEOF = 0x001C;
|
||||
|
||||
public const ushort lzxrt_Unknown1 = 0x0000;
|
||||
public const ushort lzxrt_NumEntries = 0x0004;
|
||||
public const ushort lzxrt_EntrySize = 0x0008;
|
||||
public const ushort lzxrt_TableOffset = 0x000C;
|
||||
public const ushort lzxrt_UncompLen = 0x0010;
|
||||
public const ushort lzxrt_CompLen = 0x0018;
|
||||
public const ushort lzxrt_FrameLen = 0x0020;
|
||||
public const ushort lzxrt_Entries = 0x0028;
|
||||
public const ushort lzxrt_headerSIZEOF = 0x0028;
|
||||
}
|
||||
}
|
||||
@@ -1,922 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using static SabreTools.Compression.libmspack.CHM.Constants;
|
||||
using static SabreTools.Compression.libmspack.macros;
|
||||
using static SabreTools.Compression.libmspack.system;
|
||||
|
||||
namespace SabreTools.Compression.libmspack.CHM
|
||||
{
|
||||
/// <summary>
|
||||
/// A decompressor for .CHM (Microsoft HTMLHelp) files
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
/// <see cref="mspack.DestroyCHMDecomperssor(Decompressor)"/>
|
||||
public unsafe class Decompressor : BaseDecompressor
|
||||
{
|
||||
public mschmd_decompress_state d { get; private set; }
|
||||
|
||||
// Filenames of the system files used for decompression.
|
||||
// Content and ControlData are essential.
|
||||
// ResetTable is preferred, but SpanInfo can be used if not available
|
||||
private const string content_name = "::DataSpace/Storage/MSCompressed/Content";
|
||||
private const string control_name = "::DataSpace/Storage/MSCompressed/ControlData";
|
||||
private const string spaninfo_name = "::DataSpace/Storage/MSCompressed/SpanInfo";
|
||||
private const string rtable_name = "::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable";
|
||||
|
||||
// The GUIDs found in CHM header
|
||||
private static readonly byte[] guids = new byte[32]
|
||||
{
|
||||
// {7C01FD10-7BAA-11D0-9E0C-00A0-C922-E6EC}
|
||||
0x10, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11,
|
||||
0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC,
|
||||
|
||||
// {7C01FD11-7BAA-11D0-9E0C-00A0-C922-E6EC}
|
||||
0x11, 0xFD, 0x01, 0x7C, 0xAA, 0x7B, 0xD0, 0x11,
|
||||
0x9E, 0x0C, 0x00, 0xA0, 0xC9, 0x22, 0xE6, 0xEC
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new CHM decompressor
|
||||
/// </summary>
|
||||
public Decompressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
d = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys an existing CHM decompressor
|
||||
/// </summary>
|
||||
~Decompressor()
|
||||
{
|
||||
mspack_system sys = this.system;
|
||||
if (this.d != null)
|
||||
{
|
||||
if (this.d.infh != null) sys.close(this.d.infh);
|
||||
if (this.d.state != null) lzxd_free(this.d.state);
|
||||
//sys.free(this.d);
|
||||
}
|
||||
//sys.free(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a CHM helpfile and reads its contents.
|
||||
///
|
||||
/// If the file opened is a valid CHM helpfile, all headers will be read
|
||||
/// and a mschmd_header structure will be returned, with a full list of
|
||||
/// files.
|
||||
///
|
||||
/// In the case of an error occuring, null is returned and the error code
|
||||
/// is available from last_error().
|
||||
///
|
||||
/// The filename pointer should be considered "in use" until close() is
|
||||
/// called on the CHM helpfile.
|
||||
/// </summary>
|
||||
/// <param name="filename">
|
||||
/// The filename of the CHM helpfile. This is passed
|
||||
/// directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>A pointer to a mschmd_header structure, or null on failure</returns>
|
||||
/// <see cref="close(mschmd_header)"/>
|
||||
public mschmd_header open(in string filename)
|
||||
{
|
||||
return chmd_real_open(filename, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes a previously opened CHM helpfile.
|
||||
///
|
||||
/// This closes a CHM helpfile, frees the mschmd_header and all
|
||||
/// mschmd_file structures associated with it (if any). This works on
|
||||
/// both helpfiles opened with open() and helpfiles opened with
|
||||
/// fast_open().
|
||||
///
|
||||
/// The CHM header pointer is now invalid and cannot be used again. All
|
||||
/// mschmd_file pointers referencing that CHM are also now invalid, and
|
||||
/// cannot be used again.
|
||||
/// </summary>
|
||||
/// <param name="chm">The CHM helpfile to close</param>
|
||||
/// <see cref="open(in string)"/>
|
||||
/// <see cref="fast_open(in string)"/>
|
||||
public void close(mschmd_header chm)
|
||||
{
|
||||
mschmd_file fi, nfi;
|
||||
mspack_system sys;
|
||||
uint i;
|
||||
|
||||
sys = this.system;
|
||||
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
// Free files
|
||||
for (fi = chm.files; fi != null; fi = nfi)
|
||||
{
|
||||
nfi = fi.next;
|
||||
//sys.free(fi);
|
||||
}
|
||||
for (fi = chm.sysfiles; fi != null; fi = nfi)
|
||||
{
|
||||
nfi = fi.next;
|
||||
//sys.free(fi);
|
||||
}
|
||||
|
||||
// If this CHM was being decompressed, free decompression state
|
||||
if (this.d != null && (this.d.chm == chm))
|
||||
{
|
||||
if (this.d.infh != null) sys.close(this.d.infh);
|
||||
if (this.d.state != null) lzxd_free(this.d.state);
|
||||
//sys.free(this.d);
|
||||
this.d = null;
|
||||
}
|
||||
|
||||
// If this CHM had a chunk cache, free it and contents
|
||||
if (chm.chunk_cache != null)
|
||||
{
|
||||
for (i = 0; i < chm.num_chunks; i++) sys.free(chm.chunk_cache[i]);
|
||||
sys.free(chm.chunk_cache);
|
||||
}
|
||||
|
||||
//sys.free(chm);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the basic CHM file headers. If the "entire" parameter is
|
||||
/// non-zero, all file entries will also be read. fills out a pre-existing
|
||||
/// mschmd_header structure, allocates memory for files as necessary
|
||||
/// </summary>
|
||||
private MSPACK_ERR chmd_read_headers(mspack_system sys, mspack_file fh, mschmd_header chm, int entire)
|
||||
{
|
||||
uint errors, num_chunks;
|
||||
FixedArray<byte> buf = new FixedArray<byte>(0x54);
|
||||
FixedArray<byte> chunk = null;
|
||||
byte* name, p, end;
|
||||
mschmd_file fi, link = null;
|
||||
long offset_hs0, filelen;
|
||||
int num_entries;
|
||||
MSPACK_ERR err = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
// Initialise pointers
|
||||
chm.files = null;
|
||||
chm.sysfiles = null;
|
||||
chm.chunk_cache = null;
|
||||
chm.sec0.chm = chm;
|
||||
chm.sec0.id = 0;
|
||||
chm.sec1.chm = chm;
|
||||
chm.sec1.id = 1;
|
||||
chm.sec1.content = null;
|
||||
chm.sec1.control = null;
|
||||
chm.sec1.spaninfo = null;
|
||||
chm.sec1.rtable = null;
|
||||
|
||||
// Read the first header
|
||||
if (sys.read(fh, buf, chmhead_SIZEOF) != chmhead_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// Check ITSF signature
|
||||
if (EndGetI32(buf, chmhead_Signature) != 0x46535449)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SIGNATURE;
|
||||
}
|
||||
|
||||
// Check both header GUIDs
|
||||
if (!buf.ToArray().Skip(chmhead_GUID1).Take(guids.Length).SequenceEqual(guids))
|
||||
{
|
||||
Console.Error.WriteLine("Incorrect GUIDs");
|
||||
return MSPACK_ERR.MSPACK_ERR_SIGNATURE;
|
||||
}
|
||||
|
||||
chm.version = EndGetI32(buf, chmhead_Version);
|
||||
chm.timestamp = EndGetM32(buf, chmhead_Timestamp);
|
||||
chm.language = EndGetI32(buf, chmhead_LanguageID);
|
||||
if (chm.version > 3)
|
||||
{
|
||||
sys.message(fh, "WARNING; CHM version > 3");
|
||||
}
|
||||
|
||||
// Read the header section table
|
||||
if (sys.read(fh, buf, chmhst3_SIZEOF) != chmhst3_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// chmhst3_OffsetCS0 does not exist in version 1 or 2 CHM files.
|
||||
// The offset will be corrected later, once HS1 is read.
|
||||
if (read_off64(&offset_hs0, &buf[chmhst_OffsetHS0], sys, fh) ||
|
||||
read_off64(&chm.dir_offset, &buf[chmhst_OffsetHS1], sys, fh) ||
|
||||
read_off64(&chm.sec0.offset, &buf[chmhst3_OffsetCS0], sys, fh))
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Seek to header section 0
|
||||
if (sys.seek(fh, offset_hs0, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_START) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
|
||||
// Read header section 0
|
||||
if (sys.read(fh, buf, chmhs0_SIZEOF) != chmhs0_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
if (read_off64(&chm.length, &buf[chmhs0_FileLen], sys, fh))
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Compare declared CHM file size against actual size
|
||||
if (mspack_sys_filelen(sys, fh, &filelen) == 0)
|
||||
{
|
||||
if (chm.length > filelen)
|
||||
{
|
||||
sys.message(fh, $"WARNING; file possibly truncated by {chm.length - filelen} bytes");
|
||||
}
|
||||
else if (chm.length < filelen)
|
||||
{
|
||||
sys.message(fh, $"WARNING; possible {filelen - chm.length} extra bytes at end of file");
|
||||
}
|
||||
}
|
||||
|
||||
// Seek to header section 1
|
||||
if (sys.seek(fh, chm.dir_offset, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_START) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
|
||||
// Read header section 1
|
||||
if (sys.read(fh, buf, chmhs1_SIZEOF) != chmhs1_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
chm.dir_offset = sys.tell(fh);
|
||||
chm.chunk_size = EndGetI32(buf, chmhs1_ChunkSize);
|
||||
chm.density = EndGetI32(buf, chmhs1_Density);
|
||||
chm.depth = EndGetI32(buf, chmhs1_Depth);
|
||||
chm.index_root = EndGetI32(buf, chmhs1_IndexRoot);
|
||||
chm.num_chunks = EndGetI32(buf, chmhs1_NumChunks);
|
||||
chm.first_pmgl = EndGetI32(buf, chmhs1_FirstPMGL);
|
||||
chm.last_pmgl = EndGetI32(buf, chmhs1_LastPMGL);
|
||||
|
||||
if (chm.version < 3)
|
||||
{
|
||||
// Versions before 3 don't have chmhst3_OffsetCS0
|
||||
chm.sec0.offset = chm.dir_offset + (chm.chunk_size * chm.num_chunks);
|
||||
}
|
||||
|
||||
// Check if content offset or file size is wrong
|
||||
if (chm.sec0.offset > chm.length)
|
||||
{
|
||||
Console.Error.WriteLine("content section begins after file has ended");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Ensure there are chunks and that chunk size is
|
||||
// large enough for signature and num_entries
|
||||
if (chm.chunk_size < (pmgl_Entries + 2))
|
||||
{
|
||||
Console.Error.WriteLine("chunk size not large enough");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
if (chm.num_chunks == 0)
|
||||
{
|
||||
Console.Error.WriteLine("no chunks");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// The chunk_cache data structure is not great; large values for num_chunks
|
||||
// or num_chunks*chunk_size can exhaust all memory. Until a better chunk
|
||||
// cache is implemented, put arbitrary limits on num_chunks and chunk size.
|
||||
if (chm.num_chunks > 100000)
|
||||
{
|
||||
Console.Error.WriteLine("more than 100,000 chunks");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
if (chm.chunk_size > 8192)
|
||||
{
|
||||
Console.Error.WriteLine("chunk size over 8192 (get in touch if this is valid)");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
if ((long)chm.chunk_size * (long)chm.num_chunks > chm.length)
|
||||
{
|
||||
Console.Error.WriteLine("chunks larger than entire file");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Common sense checks on header section 1 fields
|
||||
if (chm.chunk_size != 4096)
|
||||
{
|
||||
sys.message(fh, "WARNING; chunk size is not 4096");
|
||||
}
|
||||
if (chm.first_pmgl != 0)
|
||||
{
|
||||
sys.message(fh, "WARNING; first PMGL chunk is not zero");
|
||||
}
|
||||
if (chm.first_pmgl > chm.last_pmgl)
|
||||
{
|
||||
Console.Error.WriteLine("first pmgl chunk is after last pmgl chunk");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
if (chm.index_root != 0xFFFFFFFF && chm.index_root >= chm.num_chunks)
|
||||
{
|
||||
Console.Error.WriteLine("index_root outside valid range");
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// If we are doing a quick read, stop here!
|
||||
if (entire == 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
// Seek to the first PMGL chunk, and reduce the number of chunks to read
|
||||
if (chm.first_pmgl != 0)
|
||||
{
|
||||
long pmgl_offset = (long)chm.first_pmgl * (long)chm.chunk_size;
|
||||
if (sys.seek(fh, pmgl_offset, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_CUR) != 0)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
}
|
||||
num_chunks = chm.last_pmgl - chm.first_pmgl + 1;
|
||||
|
||||
chunk = new FixedArray<byte>((int)chm.chunk_size);
|
||||
|
||||
// Read and process all chunks from FirstPMGL to LastPMGL
|
||||
errors = 0;
|
||||
while (num_chunks-- > 0)
|
||||
{
|
||||
// Read next chunk
|
||||
if (sys.read(fh, chunk, (int)chm.chunk_size) != (int)chm.chunk_size)
|
||||
{
|
||||
sys.free(chunk);
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// Process only directory (PMGL) chunks
|
||||
if (EndGetI32(chunk, pmgl_Signature) != 0x4C474D50) continue;
|
||||
|
||||
if (EndGetI32(chunk, pmgl_QuickRefSize) < 2)
|
||||
{
|
||||
sys.message(fh, "WARNING; PMGL quickref area is too small");
|
||||
}
|
||||
if (EndGetI32(chunk, pmgl_QuickRefSize) >
|
||||
(chm.chunk_size - pmgl_Entries))
|
||||
{
|
||||
sys.message(fh, "WARNING; PMGL quickref area is too large");
|
||||
}
|
||||
|
||||
p = (byte*)chunk.Pointer + pmgl_Entries;
|
||||
end = (byte*)chunk.Pointer + chm.chunk_size - 2;
|
||||
num_entries = EndGetI16(chunk, (int)(chm.chunk_size - 2));
|
||||
|
||||
while (num_entries-- > 0)
|
||||
{
|
||||
uint name_len, section;
|
||||
long offset, length;
|
||||
name_len = read_encint(&p, end, &err);
|
||||
if (err != MSPACK_ERR.MSPACK_ERR_OK || (name_len > (uint)(end - p))) goto encint_err;
|
||||
name = p; p += name_len;
|
||||
section = read_encint(&p, end, &err);
|
||||
offset = read_encint(&p, end, &err);
|
||||
length = read_encint(&p, end, &err);
|
||||
if (err != MSPACK_ERR.MSPACK_ERR_OK) goto encint_err;
|
||||
|
||||
// Ignore blank or one-char (e.g. "/") filenames we'd return as blank */
|
||||
if (name_len < 2 || name[0] == 0x00 || name[1] == 0x00) continue;
|
||||
|
||||
// Empty files and directory names are stored as a file entry at
|
||||
// offset 0 with length 0. We want to keep empty files, but not
|
||||
// directory names, which end with a "/"
|
||||
if ((offset == 0) && (length == 0))
|
||||
{
|
||||
if ((name_len > 0) && (name[name_len - 1] == '/')) continue;
|
||||
}
|
||||
|
||||
if (section > 1)
|
||||
{
|
||||
sys.message(fh, $"Invalid section number '{section}'.");
|
||||
continue;
|
||||
}
|
||||
|
||||
fi = new mschmd_file();
|
||||
fi.next = null;
|
||||
fi.section = (section == 0 ? (mschmd_section)chm.sec0 : (mschmd_section)chm.sec1);
|
||||
fi.offset = offset;
|
||||
fi.length = length;
|
||||
|
||||
char[] filenameArr = new char[name_len];
|
||||
Marshal.Copy((IntPtr)name, filenameArr, 0, (int)name_len);
|
||||
filenameArr[(int)name_len] = '\0';
|
||||
fi.filename = new string(filenameArr);
|
||||
|
||||
if (name[0] == ':' && name[1] == ':')
|
||||
{
|
||||
// System file
|
||||
if (name_len == 40 && fi.filename.StartsWith(content_name))
|
||||
{
|
||||
chm.sec1.content = fi;
|
||||
}
|
||||
else if (name_len == 44 && fi.filename.StartsWith(control_name))
|
||||
{
|
||||
chm.sec1.control = fi;
|
||||
}
|
||||
else if (name_len == 41 && fi.filename.StartsWith(spaninfo_name))
|
||||
{
|
||||
chm.sec1.spaninfo = fi;
|
||||
}
|
||||
else if (name_len == 105 && fi.filename.StartsWith(rtable_name))
|
||||
{
|
||||
chm.sec1.rtable = fi;
|
||||
}
|
||||
fi.next = chm.sysfiles;
|
||||
chm.sysfiles = fi;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Normal file
|
||||
if (link != null) link.next = fi; else chm.files = fi;
|
||||
link = fi;
|
||||
}
|
||||
}
|
||||
|
||||
// This is reached either when num_entries runs out, or if
|
||||
// an ENCINT is badly encoded
|
||||
encint_err:
|
||||
if (num_entries >= 0)
|
||||
{
|
||||
Console.Error.WriteLine("bad encint before all entries could be read");
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
sys.free(chunk);
|
||||
return (errors > 0) ? MSPACK_ERR.MSPACK_ERR_DATAFORMAT : MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts a file from a CHM helpfile.
|
||||
///
|
||||
/// This extracts a file from a CHM helpfile and writes it to the given
|
||||
/// filename. The filename of the file, mscabd_file::filename, is not
|
||||
/// used by extract(), but can be used by the caller as a guide for
|
||||
/// constructing an appropriate filename.
|
||||
///
|
||||
/// This method works both with files found in the mschmd_header::files
|
||||
/// and mschmd_header::sysfiles list and mschmd_file structures generated
|
||||
/// on the fly by fast_find().
|
||||
/// </summary>
|
||||
/// <param name="file">The file to be decompressed</param>
|
||||
/// <param name="filename">The filename of the file being written to</param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
public MSPACK_ERR extract(mschmd_file file, in string filename) => throw new NotImplementedException();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the error code set by the most recently called method.
|
||||
///
|
||||
/// This is useful for open() and fast_open(), which do not return an
|
||||
/// error code directly.
|
||||
/// </summary>
|
||||
/// <returns>The most recent error code</returns>
|
||||
/// <see cref="open(in string)"/>
|
||||
/// <see cref="extract(mschmd_file, in string)"/>
|
||||
public MSPACK_ERR last_error() => throw new NotImplementedException();
|
||||
|
||||
/// <summary>
|
||||
/// Opens a CHM helpfile quickly.
|
||||
///
|
||||
/// If the file opened is a valid CHM helpfile, only essential headers
|
||||
/// will be read. A mschmd_header structure will be still be returned, as
|
||||
/// with open(), but the mschmd_header::files field will be null. No
|
||||
/// files details will be automatically read. The fast_find() method
|
||||
/// must be used to obtain file details.
|
||||
///
|
||||
/// In the case of an error occuring, null is returned and the error code
|
||||
/// is available from last_error().
|
||||
///
|
||||
/// The filename pointer should be considered "in use" until close() is
|
||||
/// called on the CHM helpfile.
|
||||
/// </summary>
|
||||
/// <param name="filename">
|
||||
/// The filename of the CHM helpfile. This is passed
|
||||
/// directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>A pointer to a mschmd_header structure, or null on failure</returns>
|
||||
/// <see cref="open(in string)"/>
|
||||
/// <see cref="close(mschmd_header)"/>
|
||||
/// <see cref="fast_find(mschmd_header, in string, ref mschmd_file, int)"/>
|
||||
/// <see cref="extract(mschmd_file, in string)"/>
|
||||
public mschmd_header fast_open(in string filename)
|
||||
{
|
||||
return chmd_real_open(filename, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The real implementation of chmd_open() and chmd_fast_open(). It simply
|
||||
/// passes the "entire" parameter to chmd_read_headers(), which will then
|
||||
/// either read all headers, or a bare mininum.
|
||||
/// </summary>
|
||||
private mschmd_header chmd_real_open(in string filename, int entire)
|
||||
{
|
||||
mschmd_header chm = null;
|
||||
MSPACK_ERR error;
|
||||
|
||||
mspack_system sys = this.system;
|
||||
|
||||
mspack_file fh;
|
||||
if ((fh = sys.open(filename, MSPACK_SYS_OPEN.MSPACK_SYS_OPEN_READ)) != null)
|
||||
{
|
||||
chm = new mschmd_header();
|
||||
chm.filename = filename;
|
||||
error = chmd_read_headers(sys, fh, chm, entire);
|
||||
if (error != MSPACK_ERR.MSPACK_ERR_OK)
|
||||
{
|
||||
// If the error is DATAFORMAT, and there are some results, return
|
||||
// partial results with a warning, rather than nothing
|
||||
if (error == MSPACK_ERR.MSPACK_ERR_DATAFORMAT && (chm.files != null || chm.sysfiles != null))
|
||||
{
|
||||
sys.message(fh, "WARNING; contents are corrupt");
|
||||
error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
close(chm);
|
||||
chm = null;
|
||||
}
|
||||
}
|
||||
|
||||
this.error = error;
|
||||
sys.close(fh);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OPEN;
|
||||
}
|
||||
return chm;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds file details quickly.
|
||||
///
|
||||
/// Instead of reading all CHM helpfile headers and building a list of
|
||||
/// files, fast_open() and fast_find() are intended for finding file
|
||||
/// details only when they are needed. The CHM file format includes an
|
||||
/// on-disk file index to allow this.
|
||||
///
|
||||
/// Given a case-sensitive filename, fast_find() will search the on-disk
|
||||
/// index for that file.
|
||||
///
|
||||
/// If the file was found, the caller-provided mschmd_file structure will
|
||||
/// be filled out like so:
|
||||
/// - section: the correct value for the found file
|
||||
/// - offset: the correct value for the found file
|
||||
/// - length: the correct value for the found file
|
||||
/// - all other structure elements: null or 0
|
||||
///
|
||||
/// If the file was not found, MSPACK_ERR_OK will still be returned as the
|
||||
/// result, but the caller-provided structure will be filled out like so:
|
||||
/// - section: null
|
||||
/// - offset: 0
|
||||
/// - length: 0
|
||||
/// - all other structure elements: null or 0
|
||||
///
|
||||
/// This method is intended to be used in conjunction with CHM helpfiles
|
||||
/// opened with fast_open(), but it also works with helpfiles opened
|
||||
/// using the regular open().
|
||||
/// </summary>
|
||||
/// <param name="chm">The CHM helpfile to search for the file</param>
|
||||
/// <param name="filename">The filename of the file to search for</param>
|
||||
/// <param name="f_ptr">A pointer to a caller-provded mschmd_file structure</param>
|
||||
/// <param name="f_size"><tt>sizeof(mschmd_file)</tt></param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
/// <see cref="open(in string)"/>
|
||||
/// <see cref="close(mschmd_header)"/>
|
||||
/// <see cref="fast_find(mschmd_header, in string, ref mschmd_file, int)"/>
|
||||
/// <see cref="extract(mschmd_file, in string)"/>
|
||||
public MSPACK_ERR fast_find(mschmd_header chm, in string filename, ref mschmd_file f_ptr, int f_size)
|
||||
{
|
||||
mspack_system sys;
|
||||
mspack_file fh;
|
||||
|
||||
// p and end are initialised to prevent MSVC warning about "potentially"
|
||||
// uninitialised usage. This is provably untrue, but MS won't fix:
|
||||
// https://developercommunity.visualstudio.com/content/problem/363489/c4701-false-positive-warning.html
|
||||
FixedArray<byte> chunk;
|
||||
byte* p = null, end = null;
|
||||
MSPACK_ERR err = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
int result = -1;
|
||||
uint n, sec;
|
||||
|
||||
if (chm == null || f_ptr == null)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_ARGS;
|
||||
}
|
||||
|
||||
sys = this.system;
|
||||
|
||||
// Clear the results structure
|
||||
f_ptr = new mschmd_file();
|
||||
|
||||
if ((fh = sys.open(chm.filename, MSPACK_SYS_OPEN.MSPACK_SYS_OPEN_READ)) == null)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_OPEN;
|
||||
}
|
||||
|
||||
// Go through PMGI chunk hierarchy to reach PMGL chunk
|
||||
if (chm.index_root < chm.num_chunks)
|
||||
{
|
||||
n = chm.index_root;
|
||||
for (; ; )
|
||||
{
|
||||
if ((chunk = read_chunk(chm, fh, n)) == null)
|
||||
{
|
||||
sys.close(fh);
|
||||
return this.error;
|
||||
}
|
||||
|
||||
// Search PMGI/PMGL chunk. exit early if no entry found
|
||||
if ((result = search_chunk(chm, chunk, filename, &p, &end)) <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Found result. loop around for next chunk if this is PMGI
|
||||
if (chunk[3] == 0x4C) break;
|
||||
|
||||
n = read_encint(&p, end, &err);
|
||||
if (err != MSPACK_ERR.MSPACK_ERR_OK) goto encint_err;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// PMGL chunks only, search from first_pmgl to last_pmgl
|
||||
for (n = chm.first_pmgl; n <= chm.last_pmgl; n = EndGetI32(chunk, pmgl_NextChunk))
|
||||
{
|
||||
if ((chunk = read_chunk(chm, fh, n)) == null)
|
||||
{
|
||||
err = this.error;
|
||||
break;
|
||||
}
|
||||
|
||||
// Search PMGL chunk. exit if file found
|
||||
if ((result = search_chunk(chm, chunk, filename, &p, &end)) > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Stop simple infinite loops: can't visit the same chunk twice
|
||||
if (n == EndGetI32(chunk, pmgl_NextChunk))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we found a file, read it
|
||||
if (result > 0)
|
||||
{
|
||||
sec = read_encint(&p, end, &err);
|
||||
f_ptr.section = (sec == 0) ? (mschmd_section)chm.sec0 : (mschmd_section)chm.sec1;
|
||||
f_ptr.offset = read_encint(&p, end, &err);
|
||||
f_ptr.length = read_encint(&p, end, &err);
|
||||
if (err != MSPACK_ERR.MSPACK_ERR_OK) goto encint_err;
|
||||
}
|
||||
else if (result < 0)
|
||||
{
|
||||
err = MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
sys.close(fh);
|
||||
return this.error = err;
|
||||
|
||||
encint_err:
|
||||
Console.Error.WriteLine("Bad encint in PGMI/PGML chunk");
|
||||
sys.close(fh);
|
||||
return this.error = MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the given chunk into memory, storing it in a chunk cache
|
||||
/// so it doesn't need to be read from disk more than once
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private FixedArray<byte> read_chunk(mschmd_header chm, mspack_file fh, uint chunk_num)
|
||||
{
|
||||
mspack_system sys = this.system;
|
||||
FixedArray<byte> buf;
|
||||
|
||||
// Check arguments - most are already checked by chmd_fast_find
|
||||
if (chunk_num >= chm.num_chunks) return null;
|
||||
|
||||
// Ensure chunk cache is available
|
||||
if (chm.chunk_cache == null)
|
||||
{
|
||||
chm.chunk_cache = new FixedArray<byte>[chm.num_chunks];
|
||||
if (chm.chunk_cache == null)
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to answer out of chunk cache
|
||||
if (chm.chunk_cache[chunk_num] != null) return chm.chunk_cache[chunk_num];
|
||||
|
||||
// Need to read chunk - allocate memory for it
|
||||
buf = new FixedArray<byte>((int)chm.chunk_size);
|
||||
|
||||
// Seek to block and read it
|
||||
if (sys.seek(fh, chm.dir_offset + (chunk_num * chm.chunk_size), MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_START) != 0)
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
sys.free(buf);
|
||||
return null;
|
||||
}
|
||||
if (sys.read(fh, buf, (int)chm.chunk_size) != (int)chm.chunk_size)
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_READ;
|
||||
sys.free(buf);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check the signature. Is is PMGL or PMGI?
|
||||
if (!((buf[0] == 0x50) && (buf[1] == 0x4D) && (buf[2] == 0x47) && ((buf[3] == 0x4C) || (buf[3] == 0x49))))
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
sys.free(buf);
|
||||
return null;
|
||||
}
|
||||
|
||||
// All OK. Store chunk in cache and return it
|
||||
return chm.chunk_cache[chunk_num] = buf;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Searches a PMGI/PMGL chunk for a given filename entry. Returns -1 on
|
||||
/// data format error, 0 if entry definitely not found, 1 if entry
|
||||
/// found. In the latter case, *result and *result_end are set pointing
|
||||
/// to that entry's data (either the "next chunk" ENCINT for a PMGI or
|
||||
/// the section, offset and length ENCINTs for a PMGL).
|
||||
///
|
||||
/// In the case of PMGL chunks, the entry has definitely been
|
||||
/// found. In the case of PMGI chunks, the entry which points to the
|
||||
/// chunk that may eventually contain that entry has been found.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private int search_chunk(mschmd_header chm, in FixedArray<byte> chunk, in string filename, byte** result, byte** result_end)
|
||||
{
|
||||
byte* p;
|
||||
uint qr_size, num_entries, qr_entries, qr_density, name_len;
|
||||
uint L, R, M, entries_off, is_pmgl;
|
||||
int cmp;
|
||||
MSPACK_ERR err = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
uint fname_len = (uint)filename.Length;
|
||||
|
||||
// PMGL chunk or PMGI chunk? (note: read_chunk() has already
|
||||
// checked the rest of the characters in the chunk signature)
|
||||
if (chunk[3] == 0x4C)
|
||||
{
|
||||
is_pmgl = 1;
|
||||
entries_off = pmgl_Entries;
|
||||
}
|
||||
else
|
||||
{
|
||||
is_pmgl = 0;
|
||||
entries_off = pmgi_Entries;
|
||||
}
|
||||
|
||||
// Step 1: binary search first filename of each QR entry
|
||||
// - target filename == entry
|
||||
// found file
|
||||
// - target filename < all entries
|
||||
// file not found
|
||||
// - target filename > all entries
|
||||
// proceed to step 2 using final entry
|
||||
// - target filename between two searched entries
|
||||
// proceed to step 2
|
||||
qr_size = EndGetI32(chunk, pmgl_QuickRefSize);
|
||||
int start = (int)(chm.chunk_size - 2);
|
||||
int end = (int)(chm.chunk_size - qr_size);
|
||||
num_entries = EndGetI16(chunk, (int)(chm.chunk_size - 2));
|
||||
qr_density = (uint)(1 + (1 << (int)chm.density));
|
||||
qr_entries = (num_entries + qr_density - 1) / qr_density;
|
||||
|
||||
if (num_entries == 0)
|
||||
{
|
||||
Console.Error.WriteLine("Chunk has no entries");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (qr_size > chm.chunk_size)
|
||||
{
|
||||
Console.Error.WriteLine("Quickref size > chunk size");
|
||||
return -1;
|
||||
}
|
||||
|
||||
*result_end = &chunk[end];
|
||||
|
||||
if (((int)qr_entries * 2) > (start - end))
|
||||
{
|
||||
Console.Error.WriteLine("WARNING; more quickrefs than quickref space");
|
||||
qr_entries = 0; // But we can live with it
|
||||
}
|
||||
|
||||
if (qr_entries > 0)
|
||||
{
|
||||
L = 0;
|
||||
R = qr_entries - 1;
|
||||
do
|
||||
{
|
||||
// Pick new midpoint
|
||||
M = (L + R) >> 1;
|
||||
|
||||
// Compare filename with entry QR points to
|
||||
p = &chunk[entries_off + (M != 0 ? EndGetI16(chunk, start - (int)(M << 1)) : 0)];
|
||||
name_len = read_encint(&p, end, &err);
|
||||
if (err != MSPACK_ERR.MSPACK_ERR_OK || (name_len > (uint)(end - p))) goto encint_err;
|
||||
cmp = compare(filename, (char*)p, fname_len, name_len);
|
||||
|
||||
if (cmp == 0) break;
|
||||
else if (cmp < 0) { if (M) R = M - 1; else return 0; }
|
||||
else if (cmp > 0) L = M + 1;
|
||||
} while (L <= R);
|
||||
M = (L + R) >> 1;
|
||||
|
||||
if (cmp == 0)
|
||||
{
|
||||
/* exact match! */
|
||||
p += name_len;
|
||||
*result = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* otherwise, read the group of entries for QR entry M */
|
||||
p = &chunk[entries_off + (M ? EndGetI16(chunk, start - (M << 1)) : 0)];
|
||||
num_entries -= (M * qr_density);
|
||||
if (num_entries > qr_density) num_entries = qr_density;
|
||||
}
|
||||
else
|
||||
{
|
||||
p = &chunk[entries_off];
|
||||
}
|
||||
|
||||
/* Step 2: linear search through the set of entries reached in step 1.
|
||||
* - filename == any entry
|
||||
* found entry
|
||||
* - filename < all entries (PMGI) or any entry (PMGL)
|
||||
* entry not found, stop now
|
||||
* - filename > all entries
|
||||
* entry not found (PMGL) / maybe found (PMGI)
|
||||
* -
|
||||
*/
|
||||
*result = null;
|
||||
while (num_entries-- > 0)
|
||||
{
|
||||
name_len = read_encint(&p, end, &err);
|
||||
if (err || (name_len > (uint)(end - p))) goto encint_err;
|
||||
cmp = compare(filename, (char*)p, fname_len, name_len);
|
||||
p += name_len;
|
||||
|
||||
if (cmp == 0)
|
||||
{
|
||||
/* entry found */
|
||||
*result = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (cmp < 0)
|
||||
{
|
||||
/* entry not found (PMGL) / maybe found (PMGI) */
|
||||
break;
|
||||
}
|
||||
|
||||
/* read and ignore the rest of this entry */
|
||||
if (is_pmgl)
|
||||
{
|
||||
while (p < end && (*p++ & 0x80)) ; /* skip section ENCINT */
|
||||
while (p < end && (*p++ & 0x80)) ; /* skip offset ENCINT */
|
||||
while (p < end && (*p++ & 0x80)) ; /* skip length ENCINT */
|
||||
}
|
||||
else
|
||||
{
|
||||
*result = p; /* store potential final result */
|
||||
while (p < end && (*p++ & 0x80)) ; /* skip chunk number ENCINT */
|
||||
}
|
||||
}
|
||||
|
||||
/* PMGL? not found. PMGI? maybe found */
|
||||
return (is_pmgl) ? 0 : (*result ? 1 : 0);
|
||||
|
||||
encint_err:
|
||||
Console.Error.WriteLine("bad encint while searching");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a file to be placed in a CHM helpfile.
|
||||
///
|
||||
/// A contiguous array of these structures should be passed to
|
||||
/// mschm_compressor::generate(). The array list is terminated with an
|
||||
/// entry whose mschmc_file::section field is set to #MSCHMC_ENDLIST, the
|
||||
/// other fields in this entry are ignored.
|
||||
/// </summary>
|
||||
public class mschmc_file
|
||||
{
|
||||
/// <summary>
|
||||
/// One of <see cref="MSCHMC"/> values.
|
||||
/// </summary>
|
||||
public MSCHMC section { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the source file that will be added to the CHM. This
|
||||
/// is passed directly to mspack_system::open().
|
||||
/// </summary>
|
||||
public string filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The full path and filename of the file within the CHM helpfile, a
|
||||
/// UTF-1 encoded null-terminated string.
|
||||
/// </summary>
|
||||
public string chm_filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The length of the file, in bytes. This will be adhered to strictly
|
||||
/// and a read error will be issued if this many bytes cannot be read
|
||||
/// from the real file at CHM generation time.
|
||||
/// </summary>
|
||||
public long length { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public class mschmd_decompress_state
|
||||
{
|
||||
/// <summary>
|
||||
/// CHM file being decompressed
|
||||
/// </summary>
|
||||
public mschmd_header chm { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Uncompressed length of LZX stream
|
||||
/// </summary>
|
||||
public long length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Uncompressed offset within stream
|
||||
/// </summary>
|
||||
public long offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Offset in input file
|
||||
/// </summary>
|
||||
public long inoffset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// LZX decompressor state
|
||||
/// </summary>
|
||||
public lzxd_stream state { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Special I/O code for decompressor
|
||||
/// </summary>
|
||||
public mspack_system sys { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input file handle
|
||||
/// </summary>
|
||||
public mspack_file infh { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Output file handle
|
||||
/// </summary>
|
||||
public mspack_file outfh { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a file stored in a CHM helpfile.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public class mschmd_file
|
||||
{
|
||||
/// <summary>
|
||||
/// A pointer to the next file in the list, or null if this is the final
|
||||
/// file.
|
||||
/// </summary>
|
||||
public mschmd_file next { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A pointer to the section that this file is located in. Indirectly,
|
||||
/// it also points to the CHM helpfile the file is located in.
|
||||
/// </summary>
|
||||
public mschmd_section section { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The offset within the section data that this file is located at.
|
||||
/// </summary>
|
||||
public long offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The length of this file, in bytes
|
||||
/// </summary>
|
||||
public long length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of this file -- a null terminated string in UTF-8.
|
||||
/// </summary>
|
||||
public string filename { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a CHM helpfile.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public unsafe class mschmd_header
|
||||
{
|
||||
/// <summary>
|
||||
/// The version of the CHM file format used in this file.
|
||||
/// </summary>
|
||||
public uint version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The "timestamp" of the CHM helpfile.
|
||||
///
|
||||
/// It is the lower 32 bits of a 64-bit value representing the number of
|
||||
/// centiseconds since 1601-01-01 00:00:00 UTC, plus 42. It is not useful
|
||||
/// as a timestamp, but it is useful as a semi-unique ID.
|
||||
/// </summary>
|
||||
public uint timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The default Language and Country ID (LCID) of the user who ran the
|
||||
/// HTMLHelp Compiler. This is not the language of the CHM file itself.
|
||||
/// </summary>
|
||||
public uint language { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The filename of the CHM helpfile. This is given by the library user
|
||||
/// and may be in any format.
|
||||
/// </summary>
|
||||
public string filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The length of the CHM helpfile, in bytes.
|
||||
/// </summary>
|
||||
public long length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of all non-system files in the CHM helpfile.
|
||||
/// </summary>
|
||||
public mschmd_file files { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of all system files in the CHM helpfile.
|
||||
///
|
||||
/// System files are files which begin with "::". They are meta-files
|
||||
/// generated by the CHM creation process.
|
||||
/// </summary>
|
||||
public mschmd_file sysfiles { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The section 0 (uncompressed) data in this CHM helpfile.
|
||||
/// </summary>
|
||||
public mschmd_sec_uncompressed sec0 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The section 1 (MSCompressed) data in this CHM helpfile.
|
||||
/// </summary>
|
||||
public mschmd_sec_mscompressed sec1 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The file offset of the first PMGL/PMGI directory chunk.
|
||||
/// </summary>
|
||||
public long dir_offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of PMGL/PMGI directory chunks in this CHM helpfile.
|
||||
/// </summary>
|
||||
public uint num_chunks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The size of each PMGL/PMGI chunk, in bytes.
|
||||
/// </summary>
|
||||
public uint chunk_size { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The "density" of the quick-reference section in PMGL/PMGI chunks.
|
||||
/// </summary>
|
||||
public uint density { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The depth of the index tree.
|
||||
///
|
||||
/// - if 1, there are no PMGI chunks, only PMGL chunks.
|
||||
/// - if 2, there is 1 PMGI chunk. All chunk indices point to PMGL chunks.
|
||||
/// - if 3, the root PMGI chunk points to secondary PMGI chunks, which in
|
||||
/// turn point to PMGL chunks.
|
||||
/// - and so on...
|
||||
/// </summary>
|
||||
public uint depth { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of the root PMGI chunk.
|
||||
///
|
||||
/// If there is no index in the CHM helpfile, this will be 0xFFFFFFFF.
|
||||
/// </summary>
|
||||
public uint index_root { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of the first PMGL chunk. Usually zero.
|
||||
/// Available only in CHM decoder version 2 and above.
|
||||
/// </summary>
|
||||
public uint first_pmgl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The number of the last PMGL chunk. Usually num_chunks-1.
|
||||
/// Available only in CHM decoder version 2 and above.
|
||||
/// </summary>
|
||||
public uint last_pmgl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A cache of loaded chunks, filled in by mschm_decoder::fast_find().
|
||||
/// Available only in CHM decoder version 2 and above.
|
||||
/// </summary>
|
||||
public FixedArray<byte>[] chunk_cache { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents the LZX compressed section of a CHM helpfile.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public class mschmd_sec_mscompressed : mschmd_section
|
||||
{
|
||||
/// <summary>
|
||||
/// A pointer to the meta-file which represents all LZX compressed data.
|
||||
/// </summary>
|
||||
public mschmd_file content { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A pointer to the file which contains the LZX control data.
|
||||
/// </summary>
|
||||
public mschmd_file control { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A pointer to the file which contains the LZX reset table.
|
||||
/// </summary>
|
||||
public mschmd_file rtable { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A pointer to the file which contains the LZX span information.
|
||||
/// Available only in CHM decoder version 2 and above.
|
||||
/// </summary>
|
||||
public mschmd_file spaninfo { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents the uncompressed section of a CHM helpfile.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public class mschmd_sec_uncompressed : mschmd_section
|
||||
{
|
||||
/// <summary>
|
||||
/// The file offset of where this section begins in the CHM helpfile.
|
||||
/// </summary>
|
||||
public long offset { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents a section of a CHM helpfile.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
///
|
||||
/// Not used directly, but used as a generic base type for
|
||||
/// mschmd_sec_uncompressed and mschmd_sec_mscompressed.
|
||||
/// </summary>
|
||||
public class mschmd_section
|
||||
{
|
||||
/// <summary>
|
||||
/// A pointer to the CHM helpfile that contains this section.
|
||||
/// </summary>
|
||||
public mschmd_header chm { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The section ID. Either 0 for the uncompressed section
|
||||
/// mschmd_sec_uncompressed, or 1 for the LZX compressed section
|
||||
/// mschmd_sec_mscompressed. No other section IDs are known.
|
||||
/// </summary>
|
||||
public uint id { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Offset from start of cabinet to the reserved header data (if present).
|
||||
/// </summary>
|
||||
public const int MSCAB_HDR_RESV_OFFSET = 0x28;
|
||||
}
|
||||
}
|
||||
@@ -1,422 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
#region mspack.h
|
||||
|
||||
/// <summary>
|
||||
/// mspack_system::open() mode
|
||||
/// </summary>
|
||||
public enum MSPACK_SYS_OPEN : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Open existing file for reading
|
||||
/// </summary>
|
||||
MSPACK_SYS_OPEN_READ = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Open new file for writing
|
||||
/// </summary>
|
||||
MSPACK_SYS_OPEN_WRITE = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Open existing file for writing
|
||||
/// </summary>
|
||||
MSPACK_SYS_OPEN_UPDATE = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Open existing file for writing
|
||||
/// </summary>
|
||||
MSPACK_SYS_OPEN_APPEND = 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// mspack_system::seek() mode
|
||||
/// </summary>
|
||||
public enum MSPACK_SYS_SEEK : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Seek relative to start of file
|
||||
/// </summary>
|
||||
MSPACK_SYS_SEEK_START = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Seek relative to current offset
|
||||
/// </summary>
|
||||
MSPACK_SYS_SEEK_CUR = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Seek relative to end of file
|
||||
/// </summary>
|
||||
MSPACK_SYS_SEEK_END = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Error code
|
||||
/// </summary>
|
||||
public enum MSPACK_ERR : int
|
||||
{
|
||||
MSPACK_ERR_OK = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Bad arguments to method
|
||||
/// </summary>
|
||||
MSPACK_ERR_ARGS = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Error opening file
|
||||
/// </summary>
|
||||
MSPACK_ERR_OPEN = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Error reading file
|
||||
/// </summary>
|
||||
MSPACK_ERR_READ = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Error writing file
|
||||
/// </summary>
|
||||
MSPACK_ERR_WRITE = 4,
|
||||
|
||||
/// <summary>
|
||||
/// Seek error
|
||||
/// </summary>
|
||||
MSPACK_ERR_SEEK = 5,
|
||||
|
||||
/// <summary>
|
||||
/// Out of memory
|
||||
/// </summary>
|
||||
MSPACK_ERR_NOMEMORY = 6,
|
||||
|
||||
/// <summary>
|
||||
/// Bad "magic id" in file
|
||||
/// </summary>
|
||||
MSPACK_ERR_SIGNATURE = 7,
|
||||
|
||||
/// <summary>
|
||||
/// Bad or corrupt file format
|
||||
/// </summary>
|
||||
MSPACK_ERR_DATAFORMAT = 8,
|
||||
|
||||
/// <summary>
|
||||
/// Bad checksum or CRC
|
||||
/// </summary>
|
||||
MSPACK_ERR_CHECKSUM = 9,
|
||||
|
||||
/// <summary>
|
||||
/// Error during compression
|
||||
/// </summary>
|
||||
MSPACK_ERR_CRUNCH = 10,
|
||||
|
||||
/// <summary>
|
||||
/// Error during decompression
|
||||
/// </summary>
|
||||
MSPACK_ERR_DECRUNCH = 11,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet header flag
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum MSCAB_HDR : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Cabinet has a predecessor
|
||||
/// </summary>
|
||||
MSCAB_HDR_PREVCAB = 0x01,
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet has a successor
|
||||
/// </summary>
|
||||
MSCAB_HDR_NEXTCAB = 0x02,
|
||||
|
||||
/// <summary>
|
||||
/// Cabinet has reserved header space
|
||||
/// </summary>
|
||||
MSCAB_HDR_RESV = 0x04,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compression mode
|
||||
/// </summary>
|
||||
public enum MSCAB_COMP : int
|
||||
{
|
||||
/// <summary>
|
||||
/// No compression
|
||||
/// </summary>
|
||||
MSCAB_COMP_NONE = 0,
|
||||
|
||||
/// <summary>
|
||||
/// MSZIP (deflate) compression
|
||||
/// </summary>
|
||||
MSCAB_COMP_MSZIP = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Quantum compression
|
||||
/// </summary>
|
||||
MSCAB_COMP_QUANTUM = 2,
|
||||
|
||||
/// <summary>
|
||||
/// LZX compression
|
||||
/// </summary>
|
||||
MSCAB_COMP_LZX = 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// mscabd_file::attribs attribute
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum MSCAB_ATTRIB : int
|
||||
{
|
||||
/// <summary>
|
||||
/// File is read-only
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_RDONLY = 0x01,
|
||||
|
||||
/// <summary>
|
||||
/// File is hidden
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_HIDDEN = 0x02,
|
||||
|
||||
/// <summary>
|
||||
/// File is an operating system file
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_SYSTEM = 0x04,
|
||||
|
||||
/// <summary>
|
||||
/// File is "archived"
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_ARCH = 0x20,
|
||||
|
||||
/// <summary>
|
||||
/// File is an executable program
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_EXEC = 0x40,
|
||||
|
||||
/// <summary>
|
||||
/// Filename is UTF8, not ISO-8859-1
|
||||
/// </summary>
|
||||
MSCAB_ATTRIB_UTF_NAME = 0x80,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// mschmc_file::section value
|
||||
/// </summary>
|
||||
public enum MSCHMC : int
|
||||
{
|
||||
/// <summary>
|
||||
/// End of CHM file list
|
||||
/// </summary>
|
||||
MSCHMC_ENDLIST = 0,
|
||||
|
||||
/// <summary>
|
||||
/// This file is in the Uncompressed section
|
||||
/// </summary>
|
||||
MSCHMC_UNCOMP = 1,
|
||||
|
||||
/// <summary>
|
||||
/// This file is in the MSCompressed section
|
||||
/// </summary>
|
||||
MSCHMC_MSCOMP = 2,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// msszddd_header::format value
|
||||
/// </summary>
|
||||
public enum MSSZDD_FMT : int
|
||||
{
|
||||
/// <summary>
|
||||
/// A regular SZDD file
|
||||
/// </summary>
|
||||
MSSZDD_FMT_NORMAL = 0,
|
||||
|
||||
/// <summary>
|
||||
/// A special QBasic SZDD file
|
||||
/// </summary>
|
||||
MSSZDD_FMT_QBASIC = 1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WAJ compression type
|
||||
/// </summary>
|
||||
public enum MSKWAJ_COMP : int
|
||||
{
|
||||
/// <summary>
|
||||
/// No compression
|
||||
/// </summary>
|
||||
MSKWAJ_COMP_NONE = 0,
|
||||
|
||||
/// <summary>
|
||||
/// No compression, 0xFF XOR "encryption"
|
||||
/// </summary>
|
||||
MSKWAJ_COMP_XOR = 1,
|
||||
|
||||
/// <summary>
|
||||
/// LZSS (same method as SZDD)
|
||||
/// </summary>
|
||||
MSKWAJ_COMP_SZDD = 2,
|
||||
|
||||
/// <summary>
|
||||
/// LZ+Huffman compression
|
||||
/// </summary>
|
||||
MSKWAJ_COMP_LZH = 3,
|
||||
|
||||
/// <summary>
|
||||
/// MSZIP
|
||||
/// </summary>
|
||||
MSKWAJ_COMP_MSZIP = 4,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KWAJ optional header flag
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum MSKWAJ_HDR : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Decompressed file length is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASLENGTH = 0x01,
|
||||
|
||||
/// <summary>
|
||||
/// Unknown 2-byte structure is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASUNKNOWN1 = 0x02,
|
||||
|
||||
/// <summary>
|
||||
/// Unknown multi-sized structure is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASUNKNOWN2 = 0x04,
|
||||
|
||||
/// <summary>
|
||||
/// File name (no extension) is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASFILENAME = 0x08,
|
||||
|
||||
/// <summary>
|
||||
/// File extension is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASFILEEXT = 0x10,
|
||||
|
||||
/// <summary>
|
||||
/// Extra text is included
|
||||
/// </summary>
|
||||
MSKWAJ_HDR_HASEXTRATEXT = 0x20,
|
||||
}
|
||||
|
||||
#region Parameters
|
||||
|
||||
/// <summary>
|
||||
/// mscab_decompressor::set_param() parameter
|
||||
/// </summary>
|
||||
public enum MSCABD_PARAM : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Search buffer size
|
||||
/// </summary>
|
||||
MSCABD_PARAM_SEARCHBUF = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Repair MS-ZIP streams?
|
||||
/// </summary>
|
||||
MSCABD_PARAM_FIXMSZIP = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Size of decompression buffer
|
||||
/// </summary>
|
||||
MSCABD_PARAM_DECOMPBUF = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Salvage data from bad cabinets?
|
||||
/// If enabled, open() will skip file with bad folder indices or filenames
|
||||
/// rather than reject the whole cabinet, and extract() will limit rather than
|
||||
/// reject files with invalid offsets and lengths, and bad data block checksums
|
||||
/// will be ignored. Available only in CAB decoder version 2 and above.
|
||||
/// </summary>
|
||||
MSCABD_PARAM_SALVAGE = 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// mschm_compressor::set_param() parameter
|
||||
/// </summary>
|
||||
public enum MSCHMC_PARAM : int
|
||||
{
|
||||
/// <summary>
|
||||
/// "timestamp" header
|
||||
/// </summary>
|
||||
MSCHMC_PARAM_TIMESTAMP = 0,
|
||||
|
||||
/// <summary>
|
||||
/// "language" header
|
||||
/// </summary>
|
||||
MSCHMC_PARAM_LANGUAGE = 1,
|
||||
|
||||
/// <summary>
|
||||
/// LZX window size
|
||||
/// </summary>
|
||||
MSCHMC_PARAM_LZXWINDOW = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Intra-chunk quickref density
|
||||
/// </summary>
|
||||
MSCHMC_PARAM_DENSITY = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Whether to create indices
|
||||
/// </summary>
|
||||
MSCHMC_PARAM_INDEX = 4,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// msszdd_compressor::set_param() parameter
|
||||
/// </summary>
|
||||
public enum MSSZDDC_PARAM : int
|
||||
{
|
||||
/// <summary>
|
||||
/// The missing character
|
||||
/// </summary>
|
||||
MSSZDDC_PARAM_MISSINGCHAR = 0,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// mskwaj_compressor::set_param() parameter
|
||||
/// </summary>
|
||||
public enum MSKWAJC_PARAM : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Compression type
|
||||
/// </summary>
|
||||
MSKWAJC_PARAM_COMP_TYPE = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Include the length of the uncompressed file in the header?
|
||||
/// </summary>
|
||||
MSKWAJC_PARAM_INCLUDE_LENGTH = 1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// msoab_decompressor::set_param() parameter
|
||||
/// </summary>
|
||||
public enum MSOABD_PARAM : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Size of decompression buffer
|
||||
/// </summary>
|
||||
MSOABD_PARAM_DECOMPBUF = 0,
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region lzss.h
|
||||
|
||||
public enum LZSS_MODE : int
|
||||
{
|
||||
LZSS_MODE_EXPAND = 0,
|
||||
LZSS_MODE_MSHELP = 1,
|
||||
LZSS_MODE_QBASIC = 2,
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public unsafe class FixedArray<T> where T : struct
|
||||
{
|
||||
/// <summary>
|
||||
/// Direct access to the internal pointer
|
||||
/// </summary>
|
||||
public IntPtr Pointer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Size of the T object
|
||||
/// </summary>
|
||||
private int sizeofT { get { return Marshal.SizeOf(typeof(T)); } }
|
||||
|
||||
/// <summary>
|
||||
/// Length of the fixed array
|
||||
/// </summary>
|
||||
private int _length;
|
||||
|
||||
public T this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i < 0 || i >= _length)
|
||||
return default;
|
||||
|
||||
return (T)Marshal.PtrToStructure(Pointer + i * sizeofT, typeof(T));
|
||||
}
|
||||
set
|
||||
{
|
||||
if (i < 0 || i >= _length)
|
||||
return;
|
||||
|
||||
Marshal.StructureToPtr(value, Pointer + i * sizeofT, false);
|
||||
}
|
||||
}
|
||||
|
||||
public FixedArray(int length)
|
||||
{
|
||||
Pointer = Marshal.AllocHGlobal(sizeofT * length);
|
||||
_length = 0;
|
||||
}
|
||||
|
||||
~FixedArray()
|
||||
{
|
||||
Marshal.FreeHGlobal(Pointer);
|
||||
}
|
||||
|
||||
public static implicit operator T*(FixedArray<T> arr) => (T*)arr.Pointer;
|
||||
|
||||
public static implicit operator T[](FixedArray<T> arr) => arr.ToArray();
|
||||
|
||||
/// <inheritdoc cref="System.Linq.Enumerable.SequenceEqual{TSource}(System.Collections.Generic.IEnumerable{TSource}, System.Collections.Generic.IEnumerable{TSource})"/>
|
||||
public bool SequenceEqual(T[] arr)
|
||||
{
|
||||
if (arr.Length < _length)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < _length; i++)
|
||||
{
|
||||
if (!this[i].Equals(arr[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert the unmanaged data to an array
|
||||
/// </summary>
|
||||
/// <returns>Array created from the pointer data</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
T[] arr = new T[_length];
|
||||
for (int i = 0; i < _length; i++)
|
||||
{
|
||||
arr[i] = this[i];
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.HLP
|
||||
{
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
public class Compressor : BaseCompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new HLP compressor
|
||||
/// </summary>
|
||||
public Compressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.HLP
|
||||
{
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
public class Decompressor : BaseDecompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new HLP decompressor
|
||||
/// </summary>
|
||||
public Decompressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.KWAJ
|
||||
{
|
||||
/// <summary>
|
||||
/// A compressor for the KWAJ file format.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public unsafe class Compressor : BaseCompressor
|
||||
{
|
||||
public int[] param { get; private set; } = new int[2];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new KWAJ compressor
|
||||
/// </summary>
|
||||
public Compressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an input file and creates a compressed output file in the
|
||||
/// KWAJ compressed file format. The KWAJ compression format is quick
|
||||
/// but gives poor compression. It is possible for the compressed output
|
||||
/// file to be larger than the input file.
|
||||
/// </summary>
|
||||
/// <param name="input">
|
||||
/// The name of the file to compressed. This is passed
|
||||
/// passed directly to mspack_system::open()
|
||||
/// </param>
|
||||
/// <param name="output">
|
||||
/// The name of the file to write compressed data to.
|
||||
/// This is passed directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <param name="length">
|
||||
/// The length of the uncompressed file, or -1 to indicate
|
||||
/// that this should be determined automatically by using
|
||||
/// mspack_system::seek() on the input file.
|
||||
/// </param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
/// <see cref="set_param(int, int)" />
|
||||
public MSPACK_ERR compress(in string input, in string output, long length) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Sets an KWAJ compression engine parameter.
|
||||
///
|
||||
/// The following parameters are defined:
|
||||
///
|
||||
/// - #MSKWAJC_PARAM_COMP_TYPE: the compression method to use. Must
|
||||
/// be one of #MSKWAJC_COMP_NONE, #MSKWAJC_COMP_XOR, #MSKWAJ_COMP_SZDD
|
||||
/// or #MSKWAJ_COMP_LZH. The default is #MSKWAJ_COMP_LZH.
|
||||
///
|
||||
/// - #MSKWAJC_PARAM_INCLUDE_LENGTH: a boolean; should the compressed
|
||||
/// output file should include the uncompressed length of the input
|
||||
/// file in the header? This adds 4 bytes to the size of the output
|
||||
/// file. A value of zero says "no", non-zero says "yes". The default
|
||||
/// is "no".
|
||||
/// </summary>
|
||||
/// <param name="param">The parameter to set</param>
|
||||
/// <param name="value">The value to set the parameter to</param>
|
||||
/// <returns>
|
||||
/// MSPACK_ERR_OK if all is OK, or MSPACK_ERR_ARGS if there
|
||||
/// is a problem with either parameter or value.
|
||||
/// </returns>
|
||||
/// <see cref="generate()"/>
|
||||
public MSPACK_ERR set_param(MSKWAJC_PARAM param, int value) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the original filename of the file before compression,
|
||||
/// which will be stored in the header of the output file.
|
||||
///
|
||||
/// The filename should be a null-terminated string, it must be an
|
||||
/// MS-DOS "8.3" type filename (up to 8 bytes for the filename, then
|
||||
/// optionally a "." and up to 3 bytes for a filename extension).
|
||||
///
|
||||
/// If null is passed as the filename, no filename is included in the
|
||||
/// header. This is the default.
|
||||
/// </summary>
|
||||
/// <param name="filename">The original filename to use</param>
|
||||
/// <returns>
|
||||
/// MSPACK_ERR_OK if all is OK, or MSPACK_ERR_ARGS if the
|
||||
/// filename is too long
|
||||
/// </returns>
|
||||
public MSPACK_ERR set_filename(in string filename) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Sets arbitrary data that will be stored in the header of the
|
||||
/// output file, uncompressed. It can be up to roughly 64 kilobytes,
|
||||
/// as the overall size of the header must not exceed 65535 bytes.
|
||||
/// The data can contain null bytes if desired.
|
||||
///
|
||||
/// If null is passed as the data pointer, or zero is passed as the
|
||||
/// length, no extra data is included in the header. This is the
|
||||
/// default.
|
||||
/// </summary>
|
||||
/// <param name="data">A pointer to the data to be stored in the header</param>
|
||||
/// <param name="bytes">the length of the data in bytes</param>
|
||||
/// <returns>
|
||||
/// MSPACK_ERR_OK if all is OK, or MSPACK_ERR_ARGS extra data
|
||||
/// is too long
|
||||
/// </returns>
|
||||
public MSPACK_ERR set_extra_data(void* data, int bytes) => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the error code set by the most recently called method.
|
||||
/// </summary>
|
||||
/// <returns>The most recent error code</returns>
|
||||
/// <see cref="compress(in string, in string, long)"/>
|
||||
public MSPACK_ERR last_error() => MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.KWAJ
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public const byte kwajh_Signature1 = 0x00;
|
||||
public const byte kwajh_Signature2 = 0x04;
|
||||
public const byte kwajh_CompMethod = 0x08;
|
||||
public const byte kwajh_DataOffset = 0x0a;
|
||||
public const byte kwajh_Flags = 0x0c;
|
||||
public const byte kwajh_SIZEOF = 0x0e;
|
||||
|
||||
/// <summary>
|
||||
/// Input buffer size during decompression - not worth parameterising IMHO
|
||||
/// </summary>
|
||||
public const int KWAJ_INPUT_SIZE = 2048;
|
||||
|
||||
/// <summary>
|
||||
/// Huffman codes that are 9 bits or less are decoded immediately
|
||||
/// </summary>
|
||||
public const int KWAJ_TABLEBITS = 9;
|
||||
|
||||
// Number of codes in each huffman table
|
||||
public const int KWAJ_MATCHLEN1_SYMS = 16;
|
||||
public const int KWAJ_MATCHLEN2_SYMS = 16;
|
||||
public const int KWAJ_LITLEN_SYMS = 32;
|
||||
public const int KWAJ_OFFSET_SYMS = 64;
|
||||
public const int KWAJ_LITERAL_SYMS = 256;
|
||||
|
||||
// Define decoding table sizes
|
||||
public const int KWAJ_TABLESIZE = 1 << KWAJ_TABLEBITS;
|
||||
public const int KWAJ_MATCHLEN1_TBLSIZE = KWAJ_TABLESIZE + (KWAJ_MATCHLEN1_SYMS * 2);
|
||||
public const int KWAJ_MATCHLEN2_TBLSIZE = KWAJ_TABLESIZE + (KWAJ_MATCHLEN2_SYMS * 2);
|
||||
public const int KWAJ_LITLEN_TBLSIZE = KWAJ_TABLESIZE + (KWAJ_LITLEN_SYMS * 2);
|
||||
public const int KWAJ_OFFSET_TBLSIZE = KWAJ_TABLESIZE + (KWAJ_OFFSET_SYMS * 2);
|
||||
public const int KWAJ_LITERAL_TBLSIZE = KWAJ_TABLESIZE + (KWAJ_LITERAL_SYMS * 2);
|
||||
}
|
||||
}
|
||||
@@ -1,375 +0,0 @@
|
||||
using System;
|
||||
using static SabreTools.Compression.libmspack.KWAJ.Constants;
|
||||
using static SabreTools.Compression.libmspack.macros;
|
||||
|
||||
namespace SabreTools.Compression.libmspack.KWAJ
|
||||
{
|
||||
/// <summary>
|
||||
/// A decompressor for KWAJ compressed files.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public unsafe class Decompressor : BaseDecompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new KWAJ decompressor.
|
||||
/// </summary>
|
||||
public Decompressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys an existing KWAJ decompressor
|
||||
/// </summary>
|
||||
~Decompressor()
|
||||
{
|
||||
mspack_system sys = this.system;
|
||||
//sys.free(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a KWAJ file and reads the header.
|
||||
///
|
||||
/// If the file opened is a valid KWAJ file, all headers will be read and
|
||||
/// a mskwajd_header structure will be returned.
|
||||
///
|
||||
/// In the case of an error occuring, null is returned and the error code
|
||||
/// is available from last_error().
|
||||
///
|
||||
/// The filename pointer should be considered "in use" until close() is
|
||||
/// called on the KWAJ file.
|
||||
/// </summary>
|
||||
/// <param name="filename">
|
||||
/// The filename of the KWAJ compressed file. This is
|
||||
/// passed directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>A pointer to a mskwajd_header structure, or null on failure</returns>
|
||||
/// <see cref="Close(mskwajd_header)"/>
|
||||
public mskwajd_header Open(in string filename)
|
||||
{
|
||||
mspack_system sys = this.system;
|
||||
|
||||
mspack_file fh = sys.open(filename, MSPACK_SYS_OPEN.MSPACK_SYS_OPEN_READ);
|
||||
if (fh == null)
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OPEN;
|
||||
return null;
|
||||
}
|
||||
|
||||
mskwajd_header hdr = new mskwajd_header();
|
||||
hdr.fh = fh;
|
||||
|
||||
MSPACK_ERR err;
|
||||
if ((err = ReadHeaders(sys, fh, hdr)) != MSPACK_ERR.MSPACK_ERR_OK)
|
||||
{
|
||||
Close(hdr);
|
||||
this.error = err;
|
||||
return null;
|
||||
}
|
||||
|
||||
return hdr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes a previously opened KWAJ file.
|
||||
///
|
||||
/// This closes a KWAJ file and frees the mskwajd_header associated
|
||||
/// with it. The KWAJ header pointer is now invalid and cannot be
|
||||
/// used again.
|
||||
/// </summary>
|
||||
/// <param name="kwaj">The KWAJ file to close</param>
|
||||
/// <see cref="Open(in string)"/>
|
||||
public void Close(mskwajd_header kwaj)
|
||||
{
|
||||
if (this.system == null)
|
||||
return;
|
||||
|
||||
// Close the file handle associated
|
||||
this.system.close(kwaj.fh);
|
||||
|
||||
// Free the memory associated
|
||||
//this.system.free(hdr.filename);
|
||||
//this.system.free(hdr.extra);
|
||||
//this.system.free(hdr);
|
||||
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the headers of a KWAJ format file
|
||||
/// </summary>
|
||||
private MSPACK_ERR ReadHeaders(mspack_system sys, mspack_file fh, mskwajd_header hdr)
|
||||
{
|
||||
FixedArray<byte> buf = new FixedArray<byte>(16);
|
||||
int i;
|
||||
|
||||
// Read in the header
|
||||
if (sys.read(fh, buf, kwajh_SIZEOF) != kwajh_SIZEOF)
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// Check for "KWAJ" signature
|
||||
if ((BitConverter.ToUInt32(buf, kwajh_Signature1) != 0x4A41574B) ||
|
||||
(BitConverter.ToUInt32(buf, kwajh_Signature2) != 0xD127F088))
|
||||
{
|
||||
return MSPACK_ERR.MSPACK_ERR_SIGNATURE;
|
||||
}
|
||||
|
||||
// Basic header fields
|
||||
hdr.comp_type = (MSKWAJ_COMP)BitConverter.ToUInt16(buf, kwajh_CompMethod);
|
||||
hdr.data_offset = BitConverter.ToUInt16(buf, kwajh_DataOffset);
|
||||
hdr.headers = (MSKWAJ_HDR)BitConverter.ToUInt16(buf, kwajh_Flags);
|
||||
hdr.length = 0;
|
||||
hdr.filename = null;
|
||||
hdr.extra = null;
|
||||
hdr.extra_length = 0;
|
||||
|
||||
// Optional headers
|
||||
|
||||
// 4 bytes: length of unpacked file
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASLENGTH))
|
||||
{
|
||||
if (sys.read(fh, buf, 4) != 4)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
hdr.length = BitConverter.ToUInt32(buf, 0);
|
||||
}
|
||||
|
||||
// 2 bytes: unknown purpose
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASUNKNOWN1))
|
||||
{
|
||||
if (sys.read(fh, buf, 2) != 2)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
}
|
||||
|
||||
// 2 bytes: length of section, then [length] bytes: unknown purpose
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASUNKNOWN2))
|
||||
{
|
||||
if (sys.read(fh, buf, 2) != 2)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
i = BitConverter.ToUInt16(buf, 0);
|
||||
if (sys.seek(fh, i, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_CUR) != 0)
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
|
||||
// Filename and extension
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASFILENAME | MSKWAJ_HDR.MSKWAJ_HDR_HASFILEEXT))
|
||||
{
|
||||
int len;
|
||||
|
||||
// Allocate memory for maximum length filename
|
||||
char* fn = (char*)sys.alloc(13);
|
||||
if ((hdr.extra = fn) == null)
|
||||
return MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
|
||||
// Copy filename if present
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASFILENAME))
|
||||
{
|
||||
// Read and copy up to 9 bytes of a null terminated string
|
||||
if ((len = sys.read(fh, buf, 9)) < 2)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
if ((*fn++ = (char)buf[i]) == '\0')
|
||||
break;
|
||||
|
||||
// If string was 9 bytes with no null terminator, reject it
|
||||
if (i == 9 && buf[8] != '\0')
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
|
||||
// Seek to byte after string ended in file
|
||||
if (sys.seek(fh, i + 1 - len, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_CUR) != 0)
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
|
||||
fn--; // Remove the null terminator
|
||||
}
|
||||
|
||||
// Copy extension if present
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASFILEEXT))
|
||||
{
|
||||
*fn++ = '.';
|
||||
|
||||
// Read and copy up to 4 bytes of a null terminated string
|
||||
if ((len = sys.read(fh, buf, 4)) < 2)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
if ((*fn++ = (char)buf[i]) == '\0')
|
||||
break;
|
||||
|
||||
// If string was 4 bytes with no null terminator, reject it
|
||||
if (i == 4 && buf[3] != '\0')
|
||||
return MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
|
||||
// Seek to byte after string ended in file
|
||||
if (sys.seek(fh, i + 1 - len, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_CUR) != 0)
|
||||
return MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
|
||||
fn--; // Remove the null terminator
|
||||
}
|
||||
*fn = '\0';
|
||||
}
|
||||
|
||||
// 2 bytes: extra text length then [length] bytes of extra text data
|
||||
if (hdr.headers.HasFlag(MSKWAJ_HDR.MSKWAJ_HDR_HASEXTRATEXT))
|
||||
{
|
||||
if (sys.read(fh, buf, 2) != 2)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
i = EndGetI16(buf, 0);
|
||||
hdr.extra = (char*)sys.alloc(i + 1);
|
||||
if (hdr.extra == null)
|
||||
return MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
|
||||
if (sys.read(fh, hdr.extra, i) != i)
|
||||
return MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
hdr.extra[i] = '\0';
|
||||
hdr.extra_length = (ushort)i;
|
||||
}
|
||||
|
||||
return MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts the compressed data from a KWAJ file.
|
||||
///
|
||||
/// This decompresses the compressed KWAJ data stream and writes it to
|
||||
/// an output file.
|
||||
/// </summary>
|
||||
/// <param name="kwaj">The KWAJ file to extract data from</param>
|
||||
/// <param name="filename">
|
||||
/// The filename to write the decompressed data to. This
|
||||
/// is passed directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
public MSPACK_ERR Extract(mskwajd_header kwaj, in string filename)
|
||||
{
|
||||
if (kwaj == null)
|
||||
return this.error = MSPACK_ERR.MSPACK_ERR_ARGS;
|
||||
|
||||
mspack_system sys = this.system;
|
||||
mspack_file fh = kwaj.fh;
|
||||
|
||||
// Seek to the compressed data
|
||||
if (sys.seek(fh, kwaj.data_offset, MSPACK_SYS_SEEK.MSPACK_SYS_SEEK_START) != 0)
|
||||
{
|
||||
return this.error = MSPACK_ERR.MSPACK_ERR_SEEK;
|
||||
}
|
||||
|
||||
// Open file for output
|
||||
mspack_file outfh;
|
||||
if ((outfh = sys.open(filename, MSPACK_SYS_OPEN.MSPACK_SYS_OPEN_WRITE)) == null)
|
||||
{
|
||||
return this.error = MSPACK_ERR.MSPACK_ERR_OPEN;
|
||||
}
|
||||
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
|
||||
// Decompress based on format
|
||||
if (kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_NONE || kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_XOR)
|
||||
{
|
||||
// NONE is a straight copy. XOR is a copy xored with 0xFF
|
||||
byte* buf = (byte*)sys.alloc(KWAJ_INPUT_SIZE);
|
||||
if (buf != null)
|
||||
{
|
||||
int read, i;
|
||||
while ((read = sys.read(fh, buf, KWAJ_INPUT_SIZE)) > 0)
|
||||
{
|
||||
if (kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_XOR)
|
||||
{
|
||||
for (i = 0; i < read; i++)
|
||||
buf[i] ^= 0xFF;
|
||||
}
|
||||
|
||||
if (sys.write(outfh, buf, read) != read)
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_WRITE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (read < 0)
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_READ;
|
||||
|
||||
sys.free(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
}
|
||||
}
|
||||
else if (kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_SZDD)
|
||||
{
|
||||
this.error = lzss_decompress(sys, fh, outfh, KWAJ_INPUT_SIZE, LZSS_MODE.LZSS_MODE_QBASIC);
|
||||
}
|
||||
else if (kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_LZH)
|
||||
{
|
||||
kwajd_stream lzh = lzh_init(sys, fh, outfh);
|
||||
this.error = (lzh != null) ? lzh_decompress(lzh) : MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
lzh_free(lzh);
|
||||
}
|
||||
else if (kwaj.comp_type == MSKWAJ_COMP.MSKWAJ_COMP_MSZIP)
|
||||
{
|
||||
mszipd_stream zip = mszipd_init(sys, fh, outfh, KWAJ_INPUT_SIZE, 0);
|
||||
this.error = (zip != null) ? mszipd_decompress_kwaj(zip) : MSPACK_ERR.MSPACK_ERR_NOMEMORY;
|
||||
mszipd_free(zip);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_DATAFORMAT;
|
||||
}
|
||||
|
||||
// Close output file
|
||||
sys.close(outfh);
|
||||
|
||||
return this.error;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decompresses an KWAJ file to an output file in one step.
|
||||
///
|
||||
/// This opens an KWAJ file as input, reads the header, then decompresses
|
||||
/// the compressed data immediately to an output file, finally closing
|
||||
/// both the input and output file. It is more convenient to use than
|
||||
/// open() then extract() then close(), if you do not need to know the
|
||||
/// KWAJ output size or output filename.
|
||||
/// </summary>
|
||||
/// <param name="input">
|
||||
/// The filename of the input KWAJ file. This is passed
|
||||
/// directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <param name="output">
|
||||
/// The filename to write the decompressed data to. This
|
||||
/// is passed directly to mspack_system::open().
|
||||
/// </param>
|
||||
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
|
||||
public MSPACK_ERR Decompress(in string input, in string output)
|
||||
{
|
||||
mskwajd_header hdr;
|
||||
if ((hdr = Open(input)) == null)
|
||||
return this.error;
|
||||
|
||||
MSPACK_ERR error = Extract(hdr, output);
|
||||
Close(hdr);
|
||||
return this.error = error;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the error code set by the most recently called method.
|
||||
///
|
||||
/// This is useful for open() which does not return an
|
||||
/// error code directly.
|
||||
/// </summary>
|
||||
/// <returns>The most recent error code</returns>
|
||||
/// <see cref="Open(in string)"/>
|
||||
/// <see cref="search()"/>
|
||||
public MSPACK_ERR LastError()
|
||||
{
|
||||
return this.error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
using static SabreTools.Compression.libmspack.KWAJ.Constants;
|
||||
using static SabreTools.Compression.libmspack.lzss;
|
||||
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
public unsafe class kwajd_stream : readbits
|
||||
{
|
||||
#region Huffman code lengths
|
||||
|
||||
public byte[] MATCHLEN1_len { get; set; } = new byte[KWAJ_MATCHLEN1_SYMS];
|
||||
|
||||
public byte[] MATCHLEN2_len { get; set; } = new byte[KWAJ_MATCHLEN2_SYMS];
|
||||
|
||||
public byte[] LITLEN_len { get; set; } = new byte[KWAJ_LITLEN_SYMS];
|
||||
|
||||
public byte[] OFFSET_len { get; set; } = new byte[KWAJ_OFFSET_SYMS];
|
||||
|
||||
public byte[] LITERAL_len { get; set; } = new byte[KWAJ_LITERAL_SYMS];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Huffman decoding tables
|
||||
|
||||
public ushort[] MATCHLEN1_table { get; set; } = new ushort[KWAJ_MATCHLEN1_TBLSIZE];
|
||||
|
||||
public ushort[] MATCHLEN2_table { get; set; } = new ushort[KWAJ_MATCHLEN2_TBLSIZE];
|
||||
|
||||
public ushort[] LITLEN_table { get; set; } = new ushort[KWAJ_LITLEN_TBLSIZE];
|
||||
|
||||
public ushort[] OFFSET_table { get; set; } = new ushort[KWAJ_OFFSET_TBLSIZE];
|
||||
|
||||
public ushort[] LITERAL_table { get; set; } = new ushort[KWAJ_LITERAL_TBLSIZE];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Input buffer
|
||||
|
||||
public new byte[] inbuf { get; set; } = new byte[KWAJ_INPUT_SIZE];
|
||||
|
||||
#endregion
|
||||
|
||||
#region History window
|
||||
|
||||
public byte[] window { get; set; } = new byte[LZSS_WINDOW_SIZE];
|
||||
|
||||
#endregion
|
||||
|
||||
public override void READ_BYTES()
|
||||
{
|
||||
if (i_ptr >= i_end)
|
||||
{
|
||||
if ((err = lzh_read_input(lzh)))
|
||||
return err;
|
||||
i_ptr = lzh.i_ptr;
|
||||
i_end = lzh.i_end;
|
||||
}
|
||||
INJECT_BITS_MSB(*i_ptr++, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack
|
||||
{
|
||||
/// <summary>
|
||||
/// A structure which represents an KWAJ compressed file.
|
||||
///
|
||||
/// All fields are READ ONLY.
|
||||
/// </summary>
|
||||
public unsafe class mskwajd_header
|
||||
{
|
||||
/// <summary>
|
||||
/// The compression type
|
||||
/// </summary>
|
||||
public MSKWAJ_COMP comp_type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The offset in the file where the compressed data stream begins
|
||||
/// </summary>
|
||||
public long data_offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Flags indicating which optional headers were included.
|
||||
/// </summary>
|
||||
public MSKWAJ_HDR headers { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The amount of uncompressed data in the file, or 0 if not present.
|
||||
/// </summary>
|
||||
public long length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Output filename, or null if not present
|
||||
/// </summary>
|
||||
public char* filename { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Extra uncompressed data (usually text) in the header.
|
||||
/// This data can contain nulls so use extra_length to get the size.
|
||||
/// </summary>
|
||||
public char* extra { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Length of extra uncompressed data in the header
|
||||
/// </summary>
|
||||
public ushort extra_length { get; set; }
|
||||
|
||||
public mspack_file fh { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.LIT
|
||||
{
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
public class Compressor : BaseCompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new LIT compressor
|
||||
/// </summary>
|
||||
public Compressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace SabreTools.Compression.libmspack.LIT
|
||||
{
|
||||
/// <summary>
|
||||
/// TODO
|
||||
/// </summary>
|
||||
public class Decompressor : BaseDecompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new LIT decompressor
|
||||
/// </summary>
|
||||
public Decompressor()
|
||||
{
|
||||
this.system = new mspack_default_system();
|
||||
this.error = MSPACK_ERR.MSPACK_ERR_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user