Compare commits

...

12 Commits

Author SHA1 Message Date
Matt Nadareski
27a9b12bbf Bump version 2025-11-25 07:45:09 -05:00
Matt Nadareski
11319c3341 Update GC.SharpCompress 2025-11-25 07:44:29 -05:00
Matt Nadareski
4de061e0d1 Bump version 2025-11-24 08:59:19 -05:00
Matt Nadareski
258e894c30 Update GC.SharpCompress 2025-11-23 19:59:28 -05:00
Matt Nadareski
8658eaf4e0 Format GHA definitions 2025-11-17 08:39:16 -05:00
Matt Nadareski
6d341abc75 Fix comment about CFB 2025-11-17 08:33:00 -05:00
Matt Nadareski
4821d63b4a Off-by-one spacing 2025-11-15 09:10:34 -05:00
Matt Nadareski
0617bf3170 Process all folders in all MS-CAB files in a set (fixes #43) 2025-11-15 09:00:28 -05:00
Matt Nadareski
425d13a2ac Fix inconsistencies in IS-CAB 2025-11-14 23:21:04 -05:00
Matt Nadareski
8a1e90f537 Reduce unnecessary casts 2025-11-14 21:33:55 -05:00
Matt Nadareski
ddc00948ed Remove red herring zlib code 2025-11-14 21:08:55 -05:00
Matt Nadareski
c96ee81f06 Simplify fixed statements 2025-11-14 20:36:46 -05:00
12 changed files with 131 additions and 110 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
- 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
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- 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: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
8.0.x
9.0.x
10.0.x
- 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: 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

View File

@@ -3,21 +3,21 @@ name: Build PR
on: [pull_request]
jobs:
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
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Run tests
run: dotnet test
- 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

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.1.0</Version>
<Version>2.2.1</Version>
</PropertyGroup>
<!-- Support All Frameworks -->

View File

@@ -10,7 +10,7 @@
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>2.1.0</Version>
<Version>2.2.1</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. .NET Framework 4.0 and greater |
| Compound File Binary (CFB) | Only CFB common pieces extractable |
| gzip archive | |
| Half-Life Game Cache File (GCF) | |
| Half-Life Level (BSP) | |

View File

@@ -12,8 +12,7 @@
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>2.1.0</Version>
<WarningsNotAsErrors>NU5104</WarningsNotAsErrors>
<Version>2.2.1</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -44,7 +43,7 @@
</ItemGroup>
<ItemGroup>
<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="GrindCore.SharpCompress" Version="0.41.1" 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

@@ -176,7 +176,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 Stream? OpenFileForReading(string? pattern, int index, string suffix)
private static FileStream? OpenFileForReading(string? pattern, int index, string suffix)
{
// An invalid pattern means no cabinet files
if (string.IsNullOrEmpty(pattern))
@@ -287,16 +287,18 @@ namespace SabreTools.Serialization.Wrappers
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
var md5 = new HashWrapper(HashType.MD5);
long readBytesLeft = (long)GetReadableBytes(fileDescriptor);
long writeBytesLeft = (long)GetWritableBytes(fileDescriptor);
byte[] inputBuffer;
ulong readBytesLeft = GetReadableBytes(fileDescriptor);
ulong writeBytesLeft = GetWritableBytes(fileDescriptor);
byte[] outputBuffer = new byte[BUFFER_SIZE];
long totalWritten = 0;
ulong totalWritten = 0;
// Cache the expected values
ulong storedSize = readBytesLeft;
// Read while there are bytes remaining
while (readBytesLeft > 0 && writeBytesLeft > 0)
while (readBytesLeft > 0 && readBytesLeft <= storedSize)
{
long bytesToWrite = BUFFER_SIZE;
uint bytesToWrite = BUFFER_SIZE;
int result;
// Handle compressed files
@@ -317,8 +319,8 @@ namespace SabreTools.Serialization.Wrappers
}
// Attempt to read the specified number of bytes
ushort bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
inputBuffer = new byte[BUFFER_SIZE + 1];
uint bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
byte[] inputBuffer = new byte[BUFFER_SIZE];
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}");
@@ -327,20 +329,16 @@ namespace SabreTools.Serialization.Wrappers
return false;
}
// Add a null byte to make inflate happy
inputBuffer[bytesToRead] = 0;
ulong readBytes = (ulong)(bytesToRead + 1);
// Uncompress into a buffer
if (useOld)
result = UncompressOld(outputBuffer, ref bytesToWrite, inputBuffer, ref readBytes);
result = UncompressOld(outputBuffer, ref bytesToWrite, inputBuffer, ref bytesToRead);
else
result = Uncompress(outputBuffer, ref bytesToWrite, inputBuffer, ref readBytes);
result = Uncompress(outputBuffer, ref bytesToWrite, inputBuffer, ref bytesToRead);
// 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)
{
Console.Error.WriteLine($"Decompression failed with code {result.ToZlibConstName()}. bytes_to_read={bytesToRead}, volume={fileDescriptor.Volume}, read_bytes={readBytes}");
Console.Error.WriteLine($"Decompression failed with code {result.ToZlibConstName()}. bytes_to_read={bytesToRead}, volume={fileDescriptor.Volume}, read_bytes={bytesToRead}");
reader.Dispose();
fs?.Close();
return false;
@@ -354,7 +352,7 @@ namespace SabreTools.Serialization.Wrappers
// Handle uncompressed files
else
{
bytesToWrite = Math.Min(readBytesLeft, BUFFER_SIZE);
bytesToWrite = (uint)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}");
@@ -364,11 +362,11 @@ namespace SabreTools.Serialization.Wrappers
}
// Set remaining bytes
readBytesLeft -= (uint)bytesToWrite;
readBytesLeft -= bytesToWrite;
}
// Hash and write the next block
bytesToWrite = Math.Min(bytesToWrite, writeBytesLeft);
bytesToWrite = (uint)Math.Min(bytesToWrite, writeBytesLeft);
md5.Process(outputBuffer, 0, (int)bytesToWrite);
fs?.Write(outputBuffer, 0, (int)bytesToWrite);
@@ -377,7 +375,7 @@ namespace SabreTools.Serialization.Wrappers
}
// Validate the number of bytes written
if ((long)fileDescriptor.ExpandedSize != totalWritten)
if (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
@@ -454,17 +452,16 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Uncompress a source byte array to a destination
/// </summary>
private unsafe static int Uncompress(byte[] dest, ref long destLen, byte[] source, ref ulong sourceLen)
private unsafe static int Uncompress(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
{
fixed (byte* sourcePtr = source)
fixed (byte* destPtr = dest)
fixed (byte* sourcePtr = source, destPtr = dest)
{
var stream = new ZLib.z_stream_s
{
next_in = sourcePtr,
avail_in = (uint)sourceLen,
avail_in = sourceLen,
next_out = destPtr,
avail_out = (uint)destLen,
avail_out = destLen,
};
// make second parameter negative to disable checksum verification
@@ -472,7 +469,7 @@ namespace SabreTools.Serialization.Wrappers
if (err != zlibConst.Z_OK)
return err;
err = ZLib.inflate(stream, 1);
err = ZLib.inflate(stream, zlibConst.Z_FINISH);
if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
{
ZLib.inflateEnd(stream);
@@ -488,17 +485,16 @@ namespace SabreTools.Serialization.Wrappers
/// <summary>
/// Uncompress a source byte array to a destination (old version)
/// </summary>
private unsafe static int UncompressOld(byte[] dest, ref long destLen, byte[] source, ref ulong sourceLen)
private unsafe static int UncompressOld(byte[] dest, ref uint destLen, byte[] source, ref uint sourceLen)
{
fixed (byte* sourcePtr = source)
fixed (byte* destPtr = dest)
fixed (byte* sourcePtr = source, destPtr = dest)
{
var stream = new ZLib.z_stream_s
{
next_in = sourcePtr,
avail_in = (uint)sourceLen,
avail_in = sourceLen,
next_out = destPtr,
avail_out = (uint)destLen,
avail_out = destLen,
};
destLen = 0;
@@ -511,7 +507,7 @@ namespace SabreTools.Serialization.Wrappers
while (stream.avail_in > 1)
{
err = ZLib.inflate(stream, 1);
err = ZLib.inflate(stream, zlibConst.Z_BLOCK);
if (err != zlibConst.Z_OK)
{
ZLib.inflateEnd(stream);
@@ -724,6 +720,7 @@ namespace SabreTools.Serialization.Wrappers
return false;
// Set the number of bytes left
start += bytesToRead;
bytesLeft -= bytesToRead;
_volumeBytesLeft -= (uint)bytesToRead;
}
@@ -784,7 +781,7 @@ namespace SabreTools.Serialization.Wrappers
ulong volumeBytesLeftCompressed, volumeBytesLeftExpanded;
#if NET20 || NET35
if ((_fileDescriptor.Flags & FileFlags.FILE_SPLIT) != 0)
if ((_fileDescriptor.Flags & FileFlags.FILE_SPLIT) != 0)
#else
if (_fileDescriptor.Flags.HasFlag(FileFlags.FILE_SPLIT))
#endif

View File

@@ -150,10 +150,16 @@ 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)
@@ -163,10 +169,19 @@ namespace SabreTools.Serialization.Wrappers
{
// Loop through the folders
bool allExtracted = true;
for (int f = 0; f < cabinet.Folders.Length; f++)
while (true)
{
var folder = cabinet.Folders[f];
allExtracted &= cabinet.ExtractFolder(Filename, outputDirectory, folder, f, includeDebug);
// 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;
}
return allExtracted;
@@ -185,12 +200,14 @@ 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
@@ -200,7 +217,7 @@ namespace SabreTools.Serialization.Wrappers
// Loop through the files
bool allExtracted = true;
var files = GetFiles(folderIndex);
var files = GetFiles(folderIndex, ignorePrev);
for (int i = 0; i < files.Length; i++)
{
var file = files[i];

View File

@@ -365,8 +365,9 @@ 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)
private CFFILE[] GetFiles(int folderIndex, bool ignorePrev = false)
{
// Ignore invalid archives
if (Files == null)
@@ -378,6 +379,15 @@ 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

@@ -793,8 +793,7 @@ namespace SabreTools.Serialization.Wrappers
byte[] data = new byte[resource.Length * 4];
unsafe
{
fixed (byte* payloadPtr = resource)
fixed (byte* dataPtr = data)
fixed (byte* payloadPtr = resource, dataPtr = data)
{
zstream.next_in = payloadPtr;
zstream.avail_in = (uint)resource.Length;

View File

@@ -98,8 +98,7 @@ namespace SabreTools.Serialization.Wrappers
data = new byte[outputFileSize];
unsafe
{
fixed (byte* payloadPtr = compressedData)
fixed (byte* dataPtr = data)
fixed (byte* payloadPtr = compressedData, dataPtr = data)
{
zstream.next_in = payloadPtr;
zstream.avail_in = (uint)compressedData.Length;