Compare commits

..

2 Commits

Author SHA1 Message Date
Matt Nadareski
f5a8d239fa Set the window bits for IS-CAB zlib 2025-11-14 20:13:42 -05:00
Matt Nadareski
ba0161ce02 Attempt to replace old zlib implementations 2025-11-14 19:56:19 -05:00
13 changed files with 162 additions and 232 deletions

View File

@@ -1,48 +1,48 @@
name: Build and Test
on:
push:
branches: ["main"]
push:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
- name: Run tests
run: dotnet test
- name: Run publish script
run: ./publish-nix.sh -d
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
- name: Update rolling tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -f rolling
git push origin :refs/tags/rolling || true
git push origin rolling --force
- name: Run tests
run: dotnet test
- name: Run publish script
run: ./publish-nix.sh -d
- name: Update rolling tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -f rolling
git push origin :refs/tags/rolling || true
git push origin rolling --force
- name: Upload to rolling
uses: ncipollo/release-action@v1.20.0
with:
allowUpdates: True
artifacts: "*.nupkg,*.snupkg,*.zip"
body: "Last built commit: ${{ github.sha }}"
name: "Rolling Release"
prerelease: True
replacesArtifacts: True
tag: "rolling"
updateOnlyUnreleased: True
- name: Upload to rolling
uses: ncipollo/release-action@v1.20.0
with:
allowUpdates: True
artifacts: "*.nupkg,*.snupkg,*.zip"
body: 'Last built commit: ${{ github.sha }}'
name: 'Rolling Release'
prerelease: True
replacesArtifacts: True
tag: "rolling"
updateOnlyUnreleased: True

View File

@@ -3,21 +3,21 @@ name: Build PR
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
- name: Build
run: dotnet build
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
- name: Build
run: dotnet build
- name: Run tests
run: dotnet test
- name: Run tests
run: dotnet test

10
.vscode/launch.json vendored
View File

@@ -1,9 +1,9 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (ExtractionTool)",
"type": "coreclr",

View File

@@ -10,7 +10,7 @@
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>2.2.0</Version>
<Version>2.1.0</Version>
</PropertyGroup>
<!-- Support All Frameworks -->

View File

@@ -10,7 +10,7 @@
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>2.2.0</Version>
<Version>2.1.0</Version>
</PropertyGroup>
<!-- Support All Frameworks -->

View File

@@ -55,7 +55,7 @@ Options:
| 7-zip archive | .NET Framework 4.6.2 and greater |
| BFPK custom archive format | |
| bzip2 archive | .NET Framework 4.6.2 and greater |
| Compound File Binary (CFB) | Only CFB common pieces extractable |
| Compound File Binary (CFB) | Only CFB common pieces extractable. .NET Framework 4.0 and greater |
| gzip archive | |
| Half-Life Game Cache File (GCF) | |
| Half-Life Level (BSP) | |

View File

@@ -12,7 +12,8 @@
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>2.2.0</Version>
<Version>2.1.0</Version>
<WarningsNotAsErrors>NU5104</WarningsNotAsErrors>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -43,7 +44,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="GrindCore.SharpCompress" Version="0.41.0" Condition="!$(TargetFramework.StartsWith(`net2`)) AND !$(TargetFramework.StartsWith(`net3`)) AND !$(TargetFramework.StartsWith(`net40`)) AND !$(TargetFramework.StartsWith(`net452`))" />
<PackageReference Include="GrindCore" Version="0.5.3" Condition="!$(TargetFramework.StartsWith(`net2`))" />
<PackageReference Include="GrindCore.SharpCompress" Version="0.40.4-alpha" Condition="!$(TargetFramework.StartsWith(`net2`)) AND !$(TargetFramework.StartsWith(`net3`)) AND !$(TargetFramework.StartsWith(`net40`)) AND !$(TargetFramework.StartsWith(`net452`))" />
<PackageReference Include="NetLegacySupport.Numerics" Version="1.0.1" Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="SabreTools.Hashing" Version="[1.6.0]" />

View File

@@ -1,6 +1,11 @@
using System;
using System.IO;
#if NET20
using SabreTools.IO.Compression.Deflate;
#else
using Nanook.GrindCore;
using Nanook.GrindCore.ZLib;
#endif
namespace SabreTools.Serialization.Wrappers
{
@@ -93,7 +98,11 @@ namespace SabreTools.Serialization.Wrappers
else
{
using var ms = new MemoryStream(data);
#if NET20
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
#else
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
#endif
zs.CopyTo(fs);
fs.Flush();
}

View File

@@ -3,7 +3,12 @@ using System.IO;
using System.Text.RegularExpressions;
using SabreTools.Data.Models.InstallShieldCabinet;
using SabreTools.Hashing;
using SabreTools.IO.Compression.zlib;
#if NET20
using SabreTools.IO.Compression.Deflate;
#else
using Nanook.GrindCore;
using Nanook.GrindCore.ZLib;
#endif
using SabreTools.IO.Extensions;
using static SabreTools.Data.Models.InstallShieldCabinet.Constants;
@@ -176,7 +181,7 @@ namespace SabreTools.Serialization.Wrappers
/// <param name="index">Cabinet part index to be opened</param>
/// <param name="suffix">Cabinet files suffix (e.g. `.cab`)</param>
/// <returns>A Stream representing the cabinet part, null on error</returns>
private static FileStream? OpenFileForReading(string? pattern, int index, string suffix)
private static Stream? OpenFileForReading(string? pattern, int index, string suffix)
{
// An invalid pattern means no cabinet files
if (string.IsNullOrEmpty(pattern))
@@ -268,7 +273,7 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Save the file at the given index to the filename specified
/// </summary>
public bool FileSave(int index, string filename, bool includeDebug, bool useOld = false)
public bool FileSave(int index, string filename, bool includeDebug)
{
// Get the file descriptor
if (!TryGetFileDescriptor(index, out var fileDescriptor) || fileDescriptor == null)
@@ -276,7 +281,7 @@ namespace SabreTools.Serialization.Wrappers
// If the file is split
if (fileDescriptor.LinkFlags == LinkFlags.LINK_PREV)
return FileSave((int)fileDescriptor.LinkPrevious, filename, includeDebug, useOld);
return FileSave((int)fileDescriptor.LinkPrevious, filename, includeDebug);
// Get the reader at the index
var reader = Reader.Create(this, index, fileDescriptor);
@@ -285,21 +290,19 @@ namespace SabreTools.Serialization.Wrappers
// Create the output file and hasher
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
var md5 = new HashWrapper(HashType.MD5);
var md5 = new HashWrapper(Hashing.HashType.MD5);
ulong readBytesLeft = GetReadableBytes(fileDescriptor);
ulong writeBytesLeft = GetWritableBytes(fileDescriptor);
long readBytesLeft = (long)GetReadableBytes(fileDescriptor);
long writeBytesLeft = (long)GetWritableBytes(fileDescriptor);
byte[] inputBuffer;
byte[] outputBuffer = new byte[BUFFER_SIZE];
ulong totalWritten = 0;
// Cache the expected values
ulong storedSize = readBytesLeft;
long totalWritten = 0;
// Read while there are bytes remaining
while (readBytesLeft > 0 && readBytesLeft <= storedSize)
while (readBytesLeft > 0 && writeBytesLeft > 0)
{
uint bytesToWrite = BUFFER_SIZE;
int result;
long bytesToWrite = BUFFER_SIZE;
bool result;
// Handle compressed files
#if NET20 || NET35
@@ -319,8 +322,8 @@ namespace SabreTools.Serialization.Wrappers
}
// Attempt to read the specified number of bytes
uint bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
byte[] inputBuffer = new byte[BUFFER_SIZE];
ushort bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
inputBuffer = new byte[BUFFER_SIZE + 1];
if (!reader.Read(inputBuffer, 0, bytesToRead))
{
Console.Error.WriteLine($"Failed to read {lengthArr.Length} bytes of file {index} ({GetFileName(index)}) from input cabinet file {fileDescriptor.Volume}");
@@ -329,16 +332,17 @@ namespace SabreTools.Serialization.Wrappers
return false;
}
// Add a null byte to make inflate happy
inputBuffer[bytesToRead] = 0;
int readBytes = bytesToRead + 1;
// Uncompress into a buffer
if (useOld)
result = UncompressOld(outputBuffer, ref bytesToWrite, inputBuffer, ref bytesToRead);
else
result = Uncompress(outputBuffer, ref bytesToWrite, inputBuffer, ref bytesToRead);
result = Uncompress(outputBuffer, inputBuffer, readBytes);
// If we didn't get a positive result that's not a data error (false positives)
if (result != zlibConst.Z_OK && result != zlibConst.Z_DATA_ERROR)
if (!result)
{
Console.Error.WriteLine($"Decompression failed with code {result.ToZlibConstName()}. bytes_to_read={bytesToRead}, volume={fileDescriptor.Volume}, read_bytes={bytesToRead}");
Console.Error.WriteLine($"Decompression failed. bytes_to_read={bytesToRead}, volume={fileDescriptor.Volume}, read_bytes={readBytes}");
reader.Dispose();
fs?.Close();
return false;
@@ -352,7 +356,7 @@ namespace SabreTools.Serialization.Wrappers
// Handle uncompressed files
else
{
bytesToWrite = (uint)Math.Min(readBytesLeft, BUFFER_SIZE);
bytesToWrite = Math.Min(readBytesLeft, BUFFER_SIZE);
if (!reader.Read(outputBuffer, 0, (int)bytesToWrite))
{
Console.Error.WriteLine($"Failed to write {bytesToWrite} bytes from input cabinet file {fileDescriptor.Volume}");
@@ -362,11 +366,11 @@ namespace SabreTools.Serialization.Wrappers
}
// Set remaining bytes
readBytesLeft -= bytesToWrite;
readBytesLeft -= (uint)bytesToWrite;
}
// Hash and write the next block
bytesToWrite = (uint)Math.Min(bytesToWrite, writeBytesLeft);
bytesToWrite = Math.Min(bytesToWrite, writeBytesLeft);
md5.Process(outputBuffer, 0, (int)bytesToWrite);
fs?.Write(outputBuffer, 0, (int)bytesToWrite);
@@ -375,7 +379,7 @@ namespace SabreTools.Serialization.Wrappers
}
// Validate the number of bytes written
if (fileDescriptor.ExpandedSize != totalWritten)
if ((long)fileDescriptor.ExpandedSize != totalWritten)
if (includeDebug) Console.WriteLine($"Expanded size of file {index} ({GetFileName(index)}) expected to be {fileDescriptor.ExpandedSize}, but was {totalWritten}");
// Finalize output values
@@ -452,72 +456,31 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Uncompress a source byte array to a destination
/// </summary>
private unsafe static int Uncompress(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
private static bool Uncompress(byte[] dest, byte[] source, int sourceLength)
{
fixed (byte* sourcePtr = source, destPtr = dest)
try
{
var stream = new ZLib.z_stream_s
// Inflate the data into the buffer
using var ms = new MemoryStream(source, 0, sourceLength);
using var os = new MemoryStream(dest);
#if NET20
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
#else
var options = new CompressionOptions
{
next_in = sourcePtr,
avail_in = sourceLen,
next_out = destPtr,
avail_out = destLen,
Type = CompressionType.Decompress,
Dictionary = new CompressionDictionaryOptions { WindowBits = -MAX_WBITS }
};
// make second parameter negative to disable checksum verification
int err = ZLib.inflateInit2_(stream, -MAX_WBITS, ZLib.zlibVersion(), source.Length);
if (err != zlibConst.Z_OK)
return err;
err = ZLib.inflate(stream, zlibConst.Z_FINISH);
if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
{
ZLib.inflateEnd(stream);
return err;
}
destLen = stream.total_out;
sourceLen = stream.total_in;
return ZLib.inflateEnd(stream);
using var zs = new ZLibStream(ms, options);
#endif
zs.CopyTo(os);
os.Flush();
return true;
}
}
/// <summary>
/// Uncompress a source byte array to a destination (old version)
/// </summary>
private unsafe static int UncompressOld(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
{
fixed (byte* sourcePtr = source, destPtr = dest)
catch
{
var stream = new ZLib.z_stream_s
{
next_in = sourcePtr,
avail_in = sourceLen,
next_out = destPtr,
avail_out = destLen,
};
destLen = 0;
sourceLen = 0;
// make second parameter negative to disable checksum verification
int err = ZLib.inflateInit2_(stream, -MAX_WBITS, ZLib.zlibVersion(), source.Length);
if (err != zlibConst.Z_OK)
return err;
while (stream.avail_in > 1)
{
err = ZLib.inflate(stream, zlibConst.Z_BLOCK);
if (err != zlibConst.Z_OK)
{
ZLib.inflateEnd(stream);
return err;
}
}
destLen = stream.total_out;
sourceLen = stream.total_in;
return ZLib.inflateEnd(stream);
return false;
}
}
@@ -720,7 +683,6 @@ namespace SabreTools.Serialization.Wrappers
return false;
// Set the number of bytes left
start += bytesToRead;
bytesLeft -= bytesToRead;
_volumeBytesLeft -= (uint)bytesToRead;
}

View File

@@ -150,16 +150,10 @@ namespace SabreTools.Serialization.Wrappers
// Display warning in debug runs
if (includeDebug) Console.WriteLine("WARNING: LZX and Quantum compression schemes are not supported so some files may be skipped!");
// Do not ignore previous links by default
bool ignorePrev = false;
// Open the full set if possible
var cabinet = this;
if (Filename != null)
{
cabinet = OpenSet(Filename);
ignorePrev = true;
}
// If the archive is invalid
if (cabinet?.Folders == null || cabinet.Folders.Length == 0)
@@ -169,19 +163,10 @@ namespace SabreTools.Serialization.Wrappers
{
// Loop through the folders
bool allExtracted = true;
while (true)
for (int f = 0; f < cabinet.Folders.Length; f++)
{
// Loop through the current folders
for (int f = 0; f < cabinet.Folders.Length; f++)
{
var folder = cabinet.Folders[f];
allExtracted &= cabinet.ExtractFolder(Filename, outputDirectory, folder, f, ignorePrev, includeDebug);
}
// Move to the next cabinet, if possible
cabinet = cabinet.Next;
if (cabinet?.Folders == null || cabinet.Folders.Length == 0)
break;
var folder = cabinet.Folders[f];
allExtracted &= cabinet.ExtractFolder(Filename, outputDirectory, folder, f, includeDebug);
}
return allExtracted;
@@ -200,14 +185,12 @@ namespace SabreTools.Serialization.Wrappers
/// <param name="outputDirectory">Path to the output directory</param>
/// <param name="folder">Folder containing the blocks to decompress</param>
/// <param name="folderIndex">Index of the folder in the cabinet</param>
/// <param name="ignorePrev">True to ignore previous links, false otherwise</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>True if all files extracted, false otherwise</returns>
private bool ExtractFolder(string? filename,
string outputDirectory,
CFFOLDER? folder,
int folderIndex,
bool ignorePrev,
bool includeDebug)
{
// Decompress the blocks, if possible
@@ -217,7 +200,7 @@ namespace SabreTools.Serialization.Wrappers
// Loop through the files
bool allExtracted = true;
var files = GetFiles(folderIndex, ignorePrev);
var files = GetFiles(folderIndex);
for (int i = 0; i < files.Length; i++)
{
var file = files[i];

View File

@@ -365,9 +365,8 @@ namespace SabreTools.Serialization.Wrappers
/// Get all files for the current folder index
/// </summary>
/// <param name="folderIndex">Index of the folder in the cabinet</param>
/// <param name="ignorePrev">True to ignore previous links, false otherwise</param>
/// <returns>Array of all files for the folder</returns>
private CFFILE[] GetFiles(int folderIndex, bool ignorePrev = false)
private CFFILE[] GetFiles(int folderIndex)
{
// Ignore invalid archives
if (Files == null)
@@ -379,15 +378,6 @@ namespace SabreTools.Serialization.Wrappers
if (string.IsNullOrEmpty(f.Name))
return false;
// Ignore links to previous cabinets, if required
if (ignorePrev)
{
if (f.FolderIndex == FolderIndex.CONTINUED_FROM_PREV)
return false;
else if (f.FolderIndex == FolderIndex.CONTINUED_PREV_AND_NEXT)
return false;
}
int fileFolder = GetFolderIndex(f);
return fileFolder == folderIndex;
});

View File

@@ -1,9 +1,15 @@
using System;
using System.IO;
using SabreTools.IO.Compression.BZip2;
using SabreTools.IO.Compression.zlib;
#if NET20
using SabreTools.IO.Compression.Deflate;
#else
using Nanook.GrindCore;
using Nanook.GrindCore.ZLib;
#endif
using SabreTools.IO.Extensions;
namespace SabreTools.Serialization.Wrappers
{
public partial class PortableExecutable : IExtractable
@@ -789,35 +795,16 @@ namespace SabreTools.Serialization.Wrappers
try
{
// Inflate the data into the buffer
var zstream = new ZLib.z_stream_s();
byte[] data = new byte[resource.Length * 4];
unsafe
{
fixed (byte* payloadPtr = resource, dataPtr = data)
{
zstream.next_in = payloadPtr;
zstream.avail_in = (uint)resource.Length;
zstream.total_in = (uint)resource.Length;
zstream.next_out = dataPtr;
zstream.avail_out = (uint)data.Length;
zstream.total_out = 0;
ZLib.inflateInit_(zstream, ZLib.zlibVersion(), resource.Length);
int zret = ZLib.inflate(zstream, 1);
ZLib.inflateEnd(zstream);
}
}
// Trim the buffer to the proper size
uint read = zstream.total_out;
#if NETFRAMEWORK
var temp = new byte[read];
Array.Copy(data, temp, read);
data = temp;
using var ms = new MemoryStream(resource);
using var os = new MemoryStream();
#if NET20
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
#else
data = new ReadOnlySpan<byte>(data, 0, (int)read).ToArray();
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
#endif
return data;
zs.CopyTo(os);
os.Flush();
return os.ToArray();
}
catch
{

View File

@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Compression.zlib;
#if NET20
using SabreTools.IO.Compression.Deflate;
#else
using Nanook.GrindCore;
using Nanook.GrindCore.ZLib;
#endif
namespace SabreTools.Serialization.Wrappers
{
@@ -94,24 +99,16 @@ namespace SabreTools.Serialization.Wrappers
else
{
// Inflate the data into the buffer
var zstream = new ZLib.z_stream_s();
data = new byte[outputFileSize];
unsafe
{
fixed (byte* payloadPtr = compressedData, dataPtr = data)
{
zstream.next_in = payloadPtr;
zstream.avail_in = (uint)compressedData.Length;
zstream.total_in = (uint)compressedData.Length;
zstream.next_out = dataPtr;
zstream.avail_out = (uint)data.Length;
zstream.total_out = 0;
ZLib.inflateInit_(zstream, ZLib.zlibVersion(), compressedData.Length);
int zret = ZLib.inflate(zstream, 1);
ZLib.inflateEnd(zstream);
}
}
using var ms = new MemoryStream(compressedData);
using var os = new MemoryStream();
#if NET20
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
#else
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
#endif
zs.CopyTo(os);
os.Flush();
data = os.ToArray();
}
// If we have an invalid output directory