mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-11-16 09:20:25 +00:00
Compare commits
2 Commits
2.2.0
...
zlib-trans
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5a8d239fa | ||
|
|
ba0161ce02 |
74
.github/workflows/build_and_test.yml
vendored
74
.github/workflows/build_and_test.yml
vendored
@@ -1,48 +1,48 @@
|
|||||||
name: Build and Test
|
name: Build and Test
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: ["main"]
|
branches: [ "main" ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Setup .NET
|
- name: Setup .NET
|
||||||
uses: actions/setup-dotnet@v5
|
uses: actions/setup-dotnet@v5
|
||||||
with:
|
with:
|
||||||
dotnet-version: |
|
dotnet-version: |
|
||||||
8.0.x
|
8.0.x
|
||||||
9.0.x
|
9.0.x
|
||||||
10.0.x
|
10.0.x
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: dotnet test
|
run: dotnet test
|
||||||
|
|
||||||
- name: Run publish script
|
- name: Run publish script
|
||||||
run: ./publish-nix.sh -d
|
run: ./publish-nix.sh -d
|
||||||
|
|
||||||
- name: Update rolling tag
|
- name: Update rolling tag
|
||||||
run: |
|
run: |
|
||||||
git config user.name "github-actions[bot]"
|
git config user.name "github-actions[bot]"
|
||||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
git tag -f rolling
|
git tag -f rolling
|
||||||
git push origin :refs/tags/rolling || true
|
git push origin :refs/tags/rolling || true
|
||||||
git push origin rolling --force
|
git push origin rolling --force
|
||||||
|
|
||||||
- name: Upload to rolling
|
- name: Upload to rolling
|
||||||
uses: ncipollo/release-action@v1.20.0
|
uses: ncipollo/release-action@v1.20.0
|
||||||
with:
|
with:
|
||||||
allowUpdates: True
|
allowUpdates: True
|
||||||
artifacts: "*.nupkg,*.snupkg,*.zip"
|
artifacts: "*.nupkg,*.snupkg,*.zip"
|
||||||
body: "Last built commit: ${{ github.sha }}"
|
body: 'Last built commit: ${{ github.sha }}'
|
||||||
name: "Rolling Release"
|
name: 'Rolling Release'
|
||||||
prerelease: True
|
prerelease: True
|
||||||
replacesArtifacts: True
|
replacesArtifacts: True
|
||||||
tag: "rolling"
|
tag: "rolling"
|
||||||
updateOnlyUnreleased: True
|
updateOnlyUnreleased: True
|
||||||
|
|||||||
30
.github/workflows/check_pr.yml
vendored
30
.github/workflows/check_pr.yml
vendored
@@ -3,21 +3,21 @@ name: Build PR
|
|||||||
on: [pull_request]
|
on: [pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Setup .NET
|
- name: Setup .NET
|
||||||
uses: actions/setup-dotnet@v5
|
uses: actions/setup-dotnet@v5
|
||||||
with:
|
with:
|
||||||
dotnet-version: |
|
dotnet-version: |
|
||||||
8.0.x
|
8.0.x
|
||||||
9.0.x
|
9.0.x
|
||||||
10.0.x
|
10.0.x
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: dotnet build
|
run: dotnet build
|
||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: dotnet test
|
run: dotnet test
|
||||||
|
|||||||
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
// Use IntelliSense to find out which attributes exist for C# debugging
|
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||||
// Use hover for the description of the existing attributes
|
// Use hover for the description of the existing attributes
|
||||||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
|
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": ".NET Core Launch (ExtractionTool)",
|
"name": ".NET Core Launch (ExtractionTool)",
|
||||||
"type": "coreclr",
|
"type": "coreclr",
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
<Version>2.2.0</Version>
|
<Version>2.1.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<!-- Support All Frameworks -->
|
<!-- Support All Frameworks -->
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
<Version>2.2.0</Version>
|
<Version>2.1.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<!-- Support All Frameworks -->
|
<!-- Support All Frameworks -->
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ Options:
|
|||||||
| 7-zip archive | .NET Framework 4.6.2 and greater |
|
| 7-zip archive | .NET Framework 4.6.2 and greater |
|
||||||
| BFPK custom archive format | |
|
| BFPK custom archive format | |
|
||||||
| bzip2 archive | .NET Framework 4.6.2 and greater |
|
| 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 | |
|
| gzip archive | |
|
||||||
| Half-Life Game Cache File (GCF) | |
|
| Half-Life Game Cache File (GCF) | |
|
||||||
| Half-Life Level (BSP) | |
|
| Half-Life Level (BSP) | |
|
||||||
|
|||||||
@@ -12,7 +12,8 @@
|
|||||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
<Version>2.2.0</Version>
|
<Version>2.1.0</Version>
|
||||||
|
<WarningsNotAsErrors>NU5104</WarningsNotAsErrors>
|
||||||
|
|
||||||
<!-- Package Properties -->
|
<!-- Package Properties -->
|
||||||
<Authors>Matt Nadareski</Authors>
|
<Authors>Matt Nadareski</Authors>
|
||||||
@@ -43,7 +44,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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="NetLegacySupport.Numerics" Version="1.0.1" Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
<PackageReference Include="SabreTools.Hashing" Version="[1.6.0]" />
|
<PackageReference Include="SabreTools.Hashing" Version="[1.6.0]" />
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
#if NET20
|
||||||
using SabreTools.IO.Compression.Deflate;
|
using SabreTools.IO.Compression.Deflate;
|
||||||
|
#else
|
||||||
|
using Nanook.GrindCore;
|
||||||
|
using Nanook.GrindCore.ZLib;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace SabreTools.Serialization.Wrappers
|
namespace SabreTools.Serialization.Wrappers
|
||||||
{
|
{
|
||||||
@@ -93,7 +98,11 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
using var ms = new MemoryStream(data);
|
using var ms = new MemoryStream(data);
|
||||||
|
#if NET20
|
||||||
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
|
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
|
||||||
|
#else
|
||||||
|
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
|
||||||
|
#endif
|
||||||
zs.CopyTo(fs);
|
zs.CopyTo(fs);
|
||||||
fs.Flush();
|
fs.Flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,12 @@ using System.IO;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using SabreTools.Data.Models.InstallShieldCabinet;
|
using SabreTools.Data.Models.InstallShieldCabinet;
|
||||||
using SabreTools.Hashing;
|
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 SabreTools.IO.Extensions;
|
||||||
using static SabreTools.Data.Models.InstallShieldCabinet.Constants;
|
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="index">Cabinet part index to be opened</param>
|
||||||
/// <param name="suffix">Cabinet files suffix (e.g. `.cab`)</param>
|
/// <param name="suffix">Cabinet files suffix (e.g. `.cab`)</param>
|
||||||
/// <returns>A Stream representing the cabinet part, null on error</returns>
|
/// <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
|
// An invalid pattern means no cabinet files
|
||||||
if (string.IsNullOrEmpty(pattern))
|
if (string.IsNullOrEmpty(pattern))
|
||||||
@@ -268,7 +273,7 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Save the file at the given index to the filename specified
|
/// Save the file at the given index to the filename specified
|
||||||
/// </summary>
|
/// </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
|
// Get the file descriptor
|
||||||
if (!TryGetFileDescriptor(index, out var fileDescriptor) || fileDescriptor == null)
|
if (!TryGetFileDescriptor(index, out var fileDescriptor) || fileDescriptor == null)
|
||||||
@@ -276,7 +281,7 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
|
|
||||||
// If the file is split
|
// If the file is split
|
||||||
if (fileDescriptor.LinkFlags == LinkFlags.LINK_PREV)
|
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
|
// Get the reader at the index
|
||||||
var reader = Reader.Create(this, index, fileDescriptor);
|
var reader = Reader.Create(this, index, fileDescriptor);
|
||||||
@@ -285,21 +290,19 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
|
|
||||||
// Create the output file and hasher
|
// Create the output file and hasher
|
||||||
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
|
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);
|
long readBytesLeft = (long)GetReadableBytes(fileDescriptor);
|
||||||
ulong writeBytesLeft = GetWritableBytes(fileDescriptor);
|
long writeBytesLeft = (long)GetWritableBytes(fileDescriptor);
|
||||||
|
byte[] inputBuffer;
|
||||||
byte[] outputBuffer = new byte[BUFFER_SIZE];
|
byte[] outputBuffer = new byte[BUFFER_SIZE];
|
||||||
ulong totalWritten = 0;
|
long totalWritten = 0;
|
||||||
|
|
||||||
// Cache the expected values
|
|
||||||
ulong storedSize = readBytesLeft;
|
|
||||||
|
|
||||||
// Read while there are bytes remaining
|
// Read while there are bytes remaining
|
||||||
while (readBytesLeft > 0 && readBytesLeft <= storedSize)
|
while (readBytesLeft > 0 && writeBytesLeft > 0)
|
||||||
{
|
{
|
||||||
uint bytesToWrite = BUFFER_SIZE;
|
long bytesToWrite = BUFFER_SIZE;
|
||||||
int result;
|
bool result;
|
||||||
|
|
||||||
// Handle compressed files
|
// Handle compressed files
|
||||||
#if NET20 || NET35
|
#if NET20 || NET35
|
||||||
@@ -319,8 +322,8 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to read the specified number of bytes
|
// Attempt to read the specified number of bytes
|
||||||
uint bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
|
ushort bytesToRead = BitConverter.ToUInt16(lengthArr, 0);
|
||||||
byte[] inputBuffer = new byte[BUFFER_SIZE];
|
inputBuffer = new byte[BUFFER_SIZE + 1];
|
||||||
if (!reader.Read(inputBuffer, 0, bytesToRead))
|
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}");
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a null byte to make inflate happy
|
||||||
|
inputBuffer[bytesToRead] = 0;
|
||||||
|
int readBytes = bytesToRead + 1;
|
||||||
|
|
||||||
// Uncompress into a buffer
|
// Uncompress into a buffer
|
||||||
if (useOld)
|
result = Uncompress(outputBuffer, inputBuffer, readBytes);
|
||||||
result = UncompressOld(outputBuffer, ref bytesToWrite, inputBuffer, ref bytesToRead);
|
|
||||||
else
|
|
||||||
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 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();
|
reader.Dispose();
|
||||||
fs?.Close();
|
fs?.Close();
|
||||||
return false;
|
return false;
|
||||||
@@ -352,7 +356,7 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
// Handle uncompressed files
|
// Handle uncompressed files
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bytesToWrite = (uint)Math.Min(readBytesLeft, BUFFER_SIZE);
|
bytesToWrite = Math.Min(readBytesLeft, BUFFER_SIZE);
|
||||||
if (!reader.Read(outputBuffer, 0, (int)bytesToWrite))
|
if (!reader.Read(outputBuffer, 0, (int)bytesToWrite))
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Failed to write {bytesToWrite} bytes from input cabinet file {fileDescriptor.Volume}");
|
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
|
// Set remaining bytes
|
||||||
readBytesLeft -= bytesToWrite;
|
readBytesLeft -= (uint)bytesToWrite;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash and write the next block
|
// Hash and write the next block
|
||||||
bytesToWrite = (uint)Math.Min(bytesToWrite, writeBytesLeft);
|
bytesToWrite = Math.Min(bytesToWrite, writeBytesLeft);
|
||||||
md5.Process(outputBuffer, 0, (int)bytesToWrite);
|
md5.Process(outputBuffer, 0, (int)bytesToWrite);
|
||||||
fs?.Write(outputBuffer, 0, (int)bytesToWrite);
|
fs?.Write(outputBuffer, 0, (int)bytesToWrite);
|
||||||
|
|
||||||
@@ -375,7 +379,7 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate the number of bytes written
|
// 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}");
|
if (includeDebug) Console.WriteLine($"Expanded size of file {index} ({GetFileName(index)}) expected to be {fileDescriptor.ExpandedSize}, but was {totalWritten}");
|
||||||
|
|
||||||
// Finalize output values
|
// Finalize output values
|
||||||
@@ -452,72 +456,31 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Uncompress a source byte array to a destination
|
/// Uncompress a source byte array to a destination
|
||||||
/// </summary>
|
/// </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,
|
Type = CompressionType.Decompress,
|
||||||
avail_in = sourceLen,
|
Dictionary = new CompressionDictionaryOptions { WindowBits = -MAX_WBITS }
|
||||||
next_out = destPtr,
|
|
||||||
avail_out = destLen,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// make second parameter negative to disable checksum verification
|
using var zs = new ZLibStream(ms, options);
|
||||||
int err = ZLib.inflateInit2_(stream, -MAX_WBITS, ZLib.zlibVersion(), source.Length);
|
#endif
|
||||||
if (err != zlibConst.Z_OK)
|
zs.CopyTo(os);
|
||||||
return err;
|
os.Flush();
|
||||||
|
return true;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
catch
|
||||||
|
|
||||||
/// <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)
|
|
||||||
{
|
{
|
||||||
var stream = new ZLib.z_stream_s
|
return false;
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -720,7 +683,6 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Set the number of bytes left
|
// Set the number of bytes left
|
||||||
start += bytesToRead;
|
|
||||||
bytesLeft -= bytesToRead;
|
bytesLeft -= bytesToRead;
|
||||||
_volumeBytesLeft -= (uint)bytesToRead;
|
_volumeBytesLeft -= (uint)bytesToRead;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,16 +150,10 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
// Display warning in debug runs
|
// Display warning in debug runs
|
||||||
if (includeDebug) Console.WriteLine("WARNING: LZX and Quantum compression schemes are not supported so some files may be skipped!");
|
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
|
// Open the full set if possible
|
||||||
var cabinet = this;
|
var cabinet = this;
|
||||||
if (Filename != null)
|
if (Filename != null)
|
||||||
{
|
|
||||||
cabinet = OpenSet(Filename);
|
cabinet = OpenSet(Filename);
|
||||||
ignorePrev = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the archive is invalid
|
// If the archive is invalid
|
||||||
if (cabinet?.Folders == null || cabinet.Folders.Length == 0)
|
if (cabinet?.Folders == null || cabinet.Folders.Length == 0)
|
||||||
@@ -169,19 +163,10 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
{
|
{
|
||||||
// Loop through the folders
|
// Loop through the folders
|
||||||
bool allExtracted = true;
|
bool allExtracted = true;
|
||||||
while (true)
|
for (int f = 0; f < cabinet.Folders.Length; f++)
|
||||||
{
|
{
|
||||||
// Loop through the current folders
|
var folder = cabinet.Folders[f];
|
||||||
for (int f = 0; f < cabinet.Folders.Length; f++)
|
allExtracted &= cabinet.ExtractFolder(Filename, outputDirectory, folder, f, includeDebug);
|
||||||
{
|
|
||||||
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;
|
return allExtracted;
|
||||||
@@ -200,14 +185,12 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
/// <param name="outputDirectory">Path to the output directory</param>
|
/// <param name="outputDirectory">Path to the output directory</param>
|
||||||
/// <param name="folder">Folder containing the blocks to decompress</param>
|
/// <param name="folder">Folder containing the blocks to decompress</param>
|
||||||
/// <param name="folderIndex">Index of the folder in the cabinet</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>
|
/// <param name="includeDebug">True to include debug data, false otherwise</param>
|
||||||
/// <returns>True if all files extracted, false otherwise</returns>
|
/// <returns>True if all files extracted, false otherwise</returns>
|
||||||
private bool ExtractFolder(string? filename,
|
private bool ExtractFolder(string? filename,
|
||||||
string outputDirectory,
|
string outputDirectory,
|
||||||
CFFOLDER? folder,
|
CFFOLDER? folder,
|
||||||
int folderIndex,
|
int folderIndex,
|
||||||
bool ignorePrev,
|
|
||||||
bool includeDebug)
|
bool includeDebug)
|
||||||
{
|
{
|
||||||
// Decompress the blocks, if possible
|
// Decompress the blocks, if possible
|
||||||
@@ -217,7 +200,7 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
|
|
||||||
// Loop through the files
|
// Loop through the files
|
||||||
bool allExtracted = true;
|
bool allExtracted = true;
|
||||||
var files = GetFiles(folderIndex, ignorePrev);
|
var files = GetFiles(folderIndex);
|
||||||
for (int i = 0; i < files.Length; i++)
|
for (int i = 0; i < files.Length; i++)
|
||||||
{
|
{
|
||||||
var file = files[i];
|
var file = files[i];
|
||||||
|
|||||||
@@ -365,9 +365,8 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
/// Get all files for the current folder index
|
/// Get all files for the current folder index
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="folderIndex">Index of the folder in the cabinet</param>
|
/// <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>
|
/// <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
|
// Ignore invalid archives
|
||||||
if (Files == null)
|
if (Files == null)
|
||||||
@@ -379,15 +378,6 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
if (string.IsNullOrEmpty(f.Name))
|
if (string.IsNullOrEmpty(f.Name))
|
||||||
return false;
|
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);
|
int fileFolder = GetFolderIndex(f);
|
||||||
return fileFolder == folderIndex;
|
return fileFolder == folderIndex;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using SabreTools.IO.Compression.BZip2;
|
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;
|
using SabreTools.IO.Extensions;
|
||||||
|
|
||||||
|
|
||||||
namespace SabreTools.Serialization.Wrappers
|
namespace SabreTools.Serialization.Wrappers
|
||||||
{
|
{
|
||||||
public partial class PortableExecutable : IExtractable
|
public partial class PortableExecutable : IExtractable
|
||||||
@@ -789,35 +795,16 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Inflate the data into the buffer
|
// Inflate the data into the buffer
|
||||||
var zstream = new ZLib.z_stream_s();
|
using var ms = new MemoryStream(resource);
|
||||||
byte[] data = new byte[resource.Length * 4];
|
using var os = new MemoryStream();
|
||||||
unsafe
|
#if NET20
|
||||||
{
|
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
|
||||||
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;
|
|
||||||
#else
|
#else
|
||||||
data = new ReadOnlySpan<byte>(data, 0, (int)read).ToArray();
|
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
|
||||||
#endif
|
#endif
|
||||||
return data;
|
zs.CopyTo(os);
|
||||||
|
os.Flush();
|
||||||
|
return os.ToArray();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
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
|
namespace SabreTools.Serialization.Wrappers
|
||||||
{
|
{
|
||||||
@@ -94,24 +99,16 @@ namespace SabreTools.Serialization.Wrappers
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Inflate the data into the buffer
|
// Inflate the data into the buffer
|
||||||
var zstream = new ZLib.z_stream_s();
|
using var ms = new MemoryStream(compressedData);
|
||||||
data = new byte[outputFileSize];
|
using var os = new MemoryStream();
|
||||||
unsafe
|
#if NET20
|
||||||
{
|
using var zs = new ZlibStream(ms, CompressionMode.Decompress);
|
||||||
fixed (byte* payloadPtr = compressedData, dataPtr = data)
|
#else
|
||||||
{
|
using var zs = new ZLibStream(ms, CompressionOptions.DefaultDecompress());
|
||||||
zstream.next_in = payloadPtr;
|
#endif
|
||||||
zstream.avail_in = (uint)compressedData.Length;
|
zs.CopyTo(os);
|
||||||
zstream.total_in = (uint)compressedData.Length;
|
os.Flush();
|
||||||
zstream.next_out = dataPtr;
|
data = os.ToArray();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have an invalid output directory
|
// If we have an invalid output directory
|
||||||
|
|||||||
Reference in New Issue
Block a user