36 Commits

Author SHA1 Message Date
Matt Nadareski
9ef17819a3 Split tests as well 2025-12-02 08:21:25 -05:00
Matt Nadareski
b4d87e53a0 Try out split libraries in the same repo 2025-12-02 08:15:48 -05:00
Matt Nadareski
ae87869bb9 Add GrindCore package 2025-11-30 19:39:15 -05:00
Matt Nadareski
46ad576668 Format GHA definitions 2025-11-17 08:40:06 -05:00
Matt Nadareski
37b09b07a6 Bump version 2025-11-13 20:02:30 -05:00
Matt Nadareski
448e43dd05 Add MergeWith dictionary extension 2025-11-13 10:35:02 -05:00
Matt Nadareski
e29b8ab4db Add support for .NET 10 2025-11-13 08:56:30 -05:00
Matt Nadareski
6075aa25a2 Name some type parameters 2025-11-11 09:54:18 -05:00
Matt Nadareski
c53fb33278 Add remaining encryption constants 2025-11-04 10:10:20 -05:00
Matt Nadareski
fb1fc5d85d Add IsNumericArray extension 2025-11-04 10:08:56 -05:00
Matt Nadareski
2763d3915b Add MPQ hashing methods 2025-11-02 22:54:10 -05:00
Matt Nadareski
16b238539b Bump version 2025-10-27 22:09:15 -04:00
Matt Nadareski
68f49eeb48 Add Bitwise Binary Operators tests 2025-10-27 16:47:36 -04:00
Matt Nadareski
c5ecd41a8f Add Shift Binary Operators tests 2025-10-27 16:43:05 -04:00
Matt Nadareski
9ab9dd4ff8 Add Bitwise Unary Operators tests 2025-10-27 16:38:45 -04:00
Matt Nadareski
867c8d11da Fix unary operator tests 2025-10-27 16:28:14 -04:00
Matt Nadareski
65dbb7a31a Add Arithmetic Binary Operators tests 2025-10-27 16:18:45 -04:00
Matt Nadareski
1eaf7954fe Add Arithmetic Unary Operators tests 2025-10-27 16:12:12 -04:00
Matt Nadareski
a602a07514 Fill out bitwise operators for both-endian 2025-10-27 16:06:01 -04:00
Matt Nadareski
466b0e90e7 Fill out arithmetic operators for both-endian 2025-10-27 15:52:48 -04:00
Matt Nadareski
abdf50c9e0 Add bitwise AND and OR operations to both-endian 2025-10-27 15:34:32 -04:00
Matt Nadareski
12341ba6aa Simplify Numerics namespace 2025-10-27 14:12:52 -04:00
Matt Nadareski
70b78f861c Revert "Add base type operator support to both-endian"
This reverts commit 5b306ce9e8.
2025-10-27 14:07:01 -04:00
Matt Nadareski
5b306ce9e8 Add base type operator support to both-endian 2025-10-27 14:02:52 -04:00
Matt Nadareski
2b6fc200e2 Add Latin1 and BigEndianUnicode extensions 2025-10-27 12:22:02 -04:00
Matt Nadareski
7c63f44c75 Add both-endian write extensions 2025-10-27 12:01:52 -04:00
Matt Nadareski
edd3e6eef2 Add both-endian try read extensions 2025-10-27 11:21:29 -04:00
Matt Nadareski
244b7411d4 Add both-endian peek read extensions 2025-10-27 10:43:54 -04:00
Matt Nadareski
fb60f1fed5 Add both-endian exact read extensions 2025-10-27 10:03:17 -04:00
Matt Nadareski
8f06bf5859 Add both-endian numeric types 2025-10-27 09:09:00 -04:00
Matt Nadareski
2c5d7ad56b Update rolling tag 2025-10-26 20:31:52 -04:00
Matt Nadareski
46996c10e5 Add Peek implementations for reading 2025-10-15 09:59:07 -04:00
Matt Nadareski
7491821679 Add origin-based SeekIfPossible 2025-10-15 09:37:41 -04:00
Matt Nadareski
8fe404e732 Remove some nonsensical endian methods 2025-10-14 20:45:02 -04:00
Matt Nadareski
793168fbe5 Add TryGet implementations for reading 2025-10-14 20:42:32 -04:00
Matt Nadareski
67b6118cc1 Add functionality from Transform tool 2025-10-14 13:58:40 -04:00
145 changed files with 16458 additions and 1115 deletions

View File

@@ -1,40 +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@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
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- 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
- 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
- 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"
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@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
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

View File

@@ -2,99 +2,7 @@
[![Build and Test](https://github.com/SabreTools/SabreTools.IO/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/SabreTools/SabreTools.IO/actions/workflows/build_and_test.yml)
This library compries of I/O functionality used by other SabreTools projects.
Find the link to the Nuget package [here](https://www.nuget.org/packages/SabreTools.IO).
## Namespaces
Below are a list of the included namespaces and their overall utility.
### `SabreTools.IO`
Generic helper classes that involve custom functionality and utility.
### `SabreTools.IO.Compression`
Various compression implementations that are used across multiple projects. Most of the implementations are be ports of existing C and C++ code.
#### Supported Compressions
| 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; not working |
**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 |
### `SabreTools.IO.Encryption`
Various encryption implementations that are used across multiple projects. Most of the implementations are be ports of existing C and C++ code.
#### Supported Encryption Schemes
| Encryption Scheme | Encrypt | Decrypt | Notes |
| --- | --- | --- | --- |
| AES/CTR | Yes | Yes | Subset of functionality exposed from [The Bouncy Castle Cryptography Library For .NET](https://github.com/bcgit/bc-csharp) |
| MoPaQ | No | Yes | Used to encrypt and decrypt MoPaQ tables for processing |
### `SabreTools.IO.Extensions`
Extensions for `BinaryReader`, `byte[]`, and `Stream` to help with reading and writing various data types. Some data types are locked behind .NET version support.
This namespace also contains other various extensions that help with common functionality and safe access.
### `SabreTools.IO.Interfaces`
Common interfaces used mainly internal to the library.
| Interface | Notes |
| --- | --- |
| `IMatch<T>` | Represents a matcher for a generic type |
| `IMatchSet<T, U>` | Represents a set of `IMatch<T>` types |
### `SabreTools.IO.Logging`
Logic for a logging system, including writing to console and textfile outputs. There are 4 possible log levels for logging statements to be invoked with. There is also a stopwatch implementation included for logging statements with automatic timespan tracking.
### `SabreTools.IO.Matching`
Classes designed to make matching contents and paths easier. These classes allow for both grouped and single matching as well as post-processing of matched information.
### `SabreTools.IO.Readers` and `SabreTools.IO.Writers`
Reading and writing support for the following file types:
- ClrMamePro-derived Metadata files
- Standard and non-standard INI files
- Separated-Value files (e.g. CSV, SSV, TSV)
For a generic INI implementation, see `SabreTools.IO.IniFile`.
### `SabreTools.IO.Streams`
Custom `Stream` implementations that are required for specialized use:
- `BufferedStream`: A format that is not a true stream implementation used for buffered, single-byte reads
- `ReadOnlyBitStream`: A readonly stream implementation allowing bitwise reading
- `ReadOnlyCompositeStream`: A readonly stream implementation that wraps multiple source streams in a set order
- `ViewStream`: A readonly stream implementation representing a view into source data
### `SabreTools.Text.Compare`
Classes focused on string comparison by natural sorting. For example, "5" would be sorted before "100".
This set of libraries enable special I/O-focused functionality used by other SabreTools projects. See the README files in each project directory for more details.
## Releases

View File

@@ -29,7 +29,7 @@ namespace SabreTools.IO.Test.Compression
{
string path = Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-compress.bin");
byte[] input = File.ReadAllBytes(path);
MemoryStream output = new MemoryStream();
var output = new MemoryStream();
var bzip = new BZip2OutputStream(output, leaveOpen: true);
bzip.Write(input, 0, input.Length);
@@ -38,4 +38,4 @@ namespace SabreTools.IO.Test.Compression
Assert.Equal(122, output.Length);
}
}
}
}

View File

@@ -24,4 +24,4 @@ namespace SabreTools.IO.Test.Compression
Assert.Equal("AIAIAIAIAIAIA", str);
}
}
}
}

View File

@@ -16,8 +16,8 @@ namespace SabreTools.IO.Test.Compression
// CFDATA blocks.
string path = Path.Combine(Environment.CurrentDirectory, "TestData", "test-archive.msz");
byte[] inputBytes = File.ReadAllBytes(path);
MemoryStream input = new MemoryStream(inputBytes);
MemoryStream output = new MemoryStream();
var input = new MemoryStream(inputBytes);
var output = new MemoryStream();
var decompressor = Decompressor.Create();
input.SeekIfPossible(0x0000);
@@ -28,4 +28,4 @@ namespace SabreTools.IO.Test.Compression
Assert.Equal(38470, output.Length);
}
}
}
}

View File

@@ -26,4 +26,4 @@ namespace SabreTools.IO.Test.Compression
Assert.Equal(38470, output.Length);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -33,17 +33,29 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bw.Write((byte)0x00);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteByteBothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(2)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteBytesTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bw.Write([0x00, 0x01, 0x02, 0x03]);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -53,7 +65,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bw.WriteBigEndian([0x03, 0x02, 0x01, 0x00]);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -63,17 +75,29 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bw.Write((sbyte)0x00);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteSByteBothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(2)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteCharTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bw.Write('\0');
ValidateBytes(expected, stream.GetBuffer());
}
@@ -93,7 +117,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bw.Write((short)0x0100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -103,18 +127,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = bw.WriteBigEndian((short)0x0001);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt16BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(4)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt16Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bw.Write((ushort)0x0100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -124,18 +160,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = bw.WriteBigEndian((ushort)0x0001);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt16BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(4)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteHalfTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bw.Write(BitConverter.Int16BitsToHalf(0x0100));
ValidateBytes(expected, stream.GetBuffer());
}
@@ -145,7 +193,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = bw.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -156,7 +204,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bw.WriteAsInt24(0x020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -166,7 +214,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = bw.WriteAsInt24BigEndian(0x000102);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -177,7 +225,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bw.WriteAsUInt24(0x020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -187,7 +235,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = bw.WriteAsUInt24BigEndian(0x000102);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -198,7 +246,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bw.Write(0x03020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -208,18 +256,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = bw.WriteBigEndian(0x00010203);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt32BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(8)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt32Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bw.Write((uint)0x03020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -229,18 +289,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = bw.WriteBigEndian((uint)0x00010203);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt32BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(8)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteSingleTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bw.Write(BitConverter.Int32BitsToSingle(0x03020100));
ValidateBytes(expected, stream.GetBuffer());
}
@@ -250,7 +322,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = bw.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -261,7 +333,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bw.WriteAsInt48(0x050403020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -271,7 +343,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = bw.WriteAsInt48BigEndian(0x000102030405);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -282,7 +354,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bw.WriteAsUInt48(0x050403020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -292,7 +364,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = bw.WriteAsUInt48BigEndian(0x000102030405);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -303,7 +375,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bw.Write(0x0706050403020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -313,18 +385,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = bw.WriteBigEndian(0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt64BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(16)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt64Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bw.Write((ulong)0x0706050403020100);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -334,18 +418,30 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = bw.WriteBigEndian((ulong)0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt64BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = [.. _bytes.Take(16)];
int offset = 0;
bw.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteDoubleTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bw.Write(BitConverter.Int64BitsToDouble(0x0706050403020100));
ValidateBytes(expected, stream.GetBuffer());
}
@@ -355,7 +451,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = bw.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -366,7 +462,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _decimalBytes.Take(16).ToArray();
byte[] expected = [.. _decimalBytes.Take(16)];
bw.Write(0.0123456789M);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -376,7 +472,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _decimalBytes.Take(16).Reverse().ToArray();
byte[] expected = [.. _decimalBytes.Take(16).Reverse()];
bool write = bw.WriteBigEndian(0.0123456789M);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -387,7 +483,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.Write(new Guid(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -398,8 +494,8 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
bool write = bw.WriteBigEndian(new Guid(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.WriteBigEndian(new Guid([.. Enumerable.Reverse(_bytes)]));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -409,7 +505,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.Write((Int128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -420,8 +516,8 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
bool write = bw.WriteBigEndian((Int128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -431,7 +527,7 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.Write((UInt128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -442,8 +538,8 @@ namespace SabreTools.IO.Test.Extensions
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
byte[] expected = _bytes.Take(16).ToArray();
bool write = bw.WriteBigEndian((UInt128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = bw.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -576,7 +672,7 @@ namespace SabreTools.IO.Test.Extensions
SecondValue = 0x07060504,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(12).ToArray();
byte[] expected = [.. bytesWithString.Take(12)];
bool write = bw.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -601,7 +697,7 @@ namespace SabreTools.IO.Test.Extensions
FourthValue = 0x0B0A,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(16).ToArray();
byte[] expected = [.. bytesWithString.Take(16)];
bool write = bw.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());

View File

@@ -36,6 +36,42 @@ namespace SabreTools.IO.Test.Extensions
#endregion
#region IsNumericArray
[Fact]
public void IsNumericArray_Empty_False()
{
byte[] arr = [];
bool actual = arr.IsNumericArray();
Assert.False(actual);
}
[Fact]
public void IsNumericArray_NonNumeric_False()
{
byte[] arr = Encoding.ASCII.GetBytes("ABCDEF");
bool actual = arr.IsNumericArray();
Assert.False(actual);
}
[Fact]
public void IsNumericArray_MixedNumeric_False()
{
byte[] arr = Encoding.ASCII.GetBytes("ABC123");
bool actual = arr.IsNumericArray();
Assert.False(actual);
}
[Fact]
public void IsNumericArray_Numeric_True()
{
byte[] arr = Encoding.ASCII.GetBytes("0123456789");
bool actual = arr.IsNumericArray();
Assert.True(actual);
}
#endregion
#region FindAllPositions
[Fact]

File diff suppressed because it is too large Load Diff

View File

@@ -32,18 +32,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = buffer.Write(ref offset, (byte)0x00);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteByteBothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(2)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadByteBothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteBytesTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.Write(ref offset, [0x00, 0x01, 0x02, 0x03]);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -54,7 +66,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.WriteBigEndian(ref offset, [0x03, 0x02, 0x01, 0x00]);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -65,18 +77,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = buffer.Write(ref offset, (sbyte)0x00);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteSByteBothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(2)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadSByteBothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteCharTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = buffer.Write(ref offset, '\0');
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -98,7 +122,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.Write(ref offset, (short)0x0100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -109,18 +133,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.WriteBigEndian(ref offset, (short)0x0001);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteInt16BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(4)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadInt16BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt16Test()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.Write(ref offset, (ushort)0x0100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -131,18 +167,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.WriteBigEndian(ref offset, (ushort)0x0001);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt16BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(4)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadUInt16BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteHalfTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.Write(ref offset, BitConverter.Int16BitsToHalf(0x0100));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -153,7 +201,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int16BitsToHalf(0x0001));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -164,7 +212,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = buffer.WriteAsInt24(ref offset, 0x020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -175,7 +223,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = buffer.WriteAsInt24BigEndian(ref offset, 0x000102);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -186,7 +234,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = buffer.WriteAsUInt24(ref offset, 0x020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -197,7 +245,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = buffer.WriteAsUInt24BigEndian(ref offset, 0x000102);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -208,7 +256,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.Write(ref offset, 0x03020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -219,18 +267,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.WriteBigEndian(ref offset, 0x00010203);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteInt32BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(8)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadInt32BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt32Test()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.Write(ref offset, (uint)0x03020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -241,18 +301,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.WriteBigEndian(ref offset, (uint)0x00010203);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt32BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(8)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadUInt32BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteSingleTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.Write(ref offset, BitConverter.Int32BitsToSingle(0x03020100));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -263,7 +335,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int32BitsToSingle(0x00010203));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -274,7 +346,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = buffer.WriteAsInt48(ref offset, 0x050403020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -285,7 +357,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = buffer.WriteAsInt48BigEndian(ref offset, 0x000102030405);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -296,7 +368,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = buffer.WriteAsUInt48(ref offset, 0x050403020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -307,7 +379,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = buffer.WriteAsUInt48BigEndian(ref offset, 0x000102030405);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -318,7 +390,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.Write(ref offset, 0x0706050403020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -329,18 +401,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.WriteBigEndian(ref offset, 0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteInt64BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(16)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadInt64BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt64Test()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.Write(ref offset, (ulong)0x0706050403020100);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -351,18 +435,30 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.WriteBigEndian(ref offset, (ulong)0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteUInt64BothEndianTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = [.. _bytes.Take(16)];
int readOffset = 0;
buffer.WriteBothEndian(ref offset, _bytes.ReadUInt64BothEndian(ref readOffset));
ValidateBytes(expected, buffer);
}
[Fact]
public void WriteDoubleTest()
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.Write(ref offset, BitConverter.Int64BitsToDouble(0x0706050403020100));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -373,7 +469,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = buffer.WriteBigEndian(ref offset, BitConverter.Int64BitsToDouble(0x0001020304050607));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -384,7 +480,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _decimalBytes.Take(16).ToArray();
byte[] expected = [.. _decimalBytes.Take(16)];
bool write = buffer.Write(ref offset, 0.0123456789M);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -395,7 +491,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _decimalBytes.Take(16).Reverse().ToArray();
byte[] expected = [.. _decimalBytes.Take(16).Reverse()];
bool write = buffer.WriteBigEndian(ref offset, 0.0123456789M);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -406,7 +502,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.Write(ref offset, new Guid(_bytes));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -417,8 +513,8 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
bool write = buffer.WriteBigEndian(ref offset, new Guid(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.WriteBigEndian(ref offset, new Guid([.. Enumerable.Reverse(_bytes)]));
Assert.True(write);
ValidateBytes(expected, buffer);
}
@@ -428,7 +524,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.Write(ref offset, (Int128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -439,8 +535,8 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
bool write = buffer.WriteBigEndian(ref offset, (Int128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.WriteBigEndian(ref offset, (Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, buffer);
}
@@ -450,7 +546,7 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.Write(ref offset, (UInt128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -461,8 +557,8 @@ namespace SabreTools.IO.Test.Extensions
{
byte[] buffer = new byte[16];
int offset = 0;
byte[] expected = _bytes.Take(16).ToArray();
bool write = buffer.WriteBigEndian(ref offset, (UInt128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = buffer.WriteBigEndian(ref offset, (UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, buffer);
}
@@ -595,7 +691,7 @@ namespace SabreTools.IO.Test.Extensions
SecondValue = 0x07060504,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(12).ToArray();
byte[] expected = [.. bytesWithString.Take(12)];
bool write = buffer.WriteType(ref offset, obj);
Assert.True(write);
ValidateBytes(expected, buffer);
@@ -620,7 +716,7 @@ namespace SabreTools.IO.Test.Extensions
FourthValue = 0x0B0A,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(16).ToArray();
byte[] expected = [.. bytesWithString.Take(16)];
bool write = buffer.WriteType(ref offset, obj);
Assert.True(write);
ValidateBytes(expected, buffer);

View File

@@ -0,0 +1,86 @@
using System.Collections.Generic;
using SabreTools.IO.Extensions;
using Xunit;
namespace SabreTools.IO.Test.Extensions
{
public class DictionaryExtensionsTests
{
#region MergeWith
[Fact]
public void MergeWith_EmptySource_EmptyOther_Empty()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
source.MergeWith(other);
Assert.Empty(source);
}
[Fact]
public void MergeWith_EmptySource_EmptyKeyOther_Empty()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
other.Add("key", []);
source.MergeWith(other);
Assert.Empty(source);
}
[Fact]
public void MergeWith_EmptySource_FilledOther_Filled()
{
Dictionary<string, List<string>> source = [];
Dictionary<string, List<string>> other = [];
other.Add("key", ["value"]);
source.MergeWith(other);
string key = Assert.Single(source.Keys);
Assert.Equal("key", key);
List<string> actual = source[key];
string value = Assert.Single(actual);
Assert.Equal("value", value);
}
[Fact]
public void MergeWith_FilledSource_EmptyOther_Filled()
{
Dictionary<string, List<string>> source = [];
source.Add("key", ["value"]);
Dictionary<string, List<string>> other = [];
source.MergeWith(other);
string key = Assert.Single(source.Keys);
Assert.Equal("key", key);
List<string> actual = source[key];
string value = Assert.Single(actual);
Assert.Equal("value", value);
}
[Fact]
public void MergeWith_FilledSource_FilledOther_Filled()
{
Dictionary<string, List<string>> source = [];
source.Add("key1", ["value1"]);
Dictionary<string, List<string>> other = [];
other.Add("key2", ["value2"]);
source.MergeWith(other);
Assert.Equal(2, source.Keys.Count);
Assert.Contains("key1", source.Keys);
List<string> actualKey1 = source["key1"];
string value1 = Assert.Single(actualKey1);
Assert.Equal("value1", value1);
Assert.Contains("key2", source.Keys);
List<string> actualKey2 = source["key2"];
string value2 = Assert.Single(actualKey2);
Assert.Equal("value2", value2);
}
#endregion
}
}

View File

@@ -80,7 +80,7 @@ namespace SabreTools.IO.Test.Extensions
#endregion
/// <summary>
/// Fake enumerable that uses <see cref="ErrorEnumerator"/>
/// Fake enumerable that uses <see cref="ErrorEnumerator"/>
/// </summary>
private class ErrorEnumerable : IEnumerable<string>
{

View File

@@ -313,6 +313,52 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(13, actual);
}
[Theory]
[InlineData(SeekOrigin.Begin)]
[InlineData(SeekOrigin.Current)]
[InlineData(SeekOrigin.End)]
public void SeekIfPossible_NonSeekable_OriginTest(SeekOrigin origin)
{
var stream = new NonSeekableStream();
long actual = stream.SeekIfPossible(0, origin);
Assert.Equal(8, actual);
}
[Theory]
[InlineData(SeekOrigin.Begin)]
[InlineData(SeekOrigin.Current)]
[InlineData(SeekOrigin.End)]
public void SeekIfPossible_NonPositionable_OriginTest(SeekOrigin origin)
{
var stream = new NonPositionableStream();
long actual = stream.SeekIfPossible(0, origin);
Assert.Equal(-1, actual);
}
[Theory]
[InlineData(SeekOrigin.Begin)]
[InlineData(SeekOrigin.Current)]
[InlineData(SeekOrigin.End)]
public void SeekIfPossible_HiddenNonSeekable_OriginTest(SeekOrigin origin)
{
var stream = new HiddenNonSeekableStream();
long actual = stream.SeekIfPossible(0, origin);
Assert.Equal(-1, actual);
}
[Theory]
[InlineData(SeekOrigin.Begin, 5, 5)]
[InlineData(SeekOrigin.Current, 5, 7)]
[InlineData(SeekOrigin.End, -5, 11)]
public void SeekIfPossible_Seekable_OriginTest(SeekOrigin origin, long offset, long expected)
{
var stream = new MemoryStream(new byte[16], 0, 16, false, true);
stream.Position = 2;
long actual = stream.SeekIfPossible(offset, origin);
Assert.Equal(expected, actual);
}
#endregion
#region SegmentValid

File diff suppressed because it is too large Load Diff

View File

@@ -32,17 +32,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteByteValueTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = stream.Write((byte)0x00);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteByteBothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(2)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadByteBothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteBytesTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = StreamWriterExtensions.Write(stream, [0x00, 0x01, 0x02, 0x03]);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -52,7 +63,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteBytesBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
stream.WriteBigEndian([0x03, 0x02, 0x01, 0x00]);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -61,17 +72,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteSByteTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = stream.Write((sbyte)0x00);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteSByteBothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(2)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadSByteBothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteCharTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(1).ToArray();
byte[] expected = [.. _bytes.Take(1)];
bool write = stream.Write('\0');
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -90,7 +112,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt16Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.Write((short)0x0100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -100,17 +122,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt16BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.WriteBigEndian((short)0x0001);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt16BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(4)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadInt16BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt16Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.Write((ushort)0x0100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -120,17 +153,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt16BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.WriteBigEndian((ushort)0x0001);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt16BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(4)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadUInt16BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteHalfTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.Write(BitConverter.Int16BitsToHalf(0x0100));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -140,7 +184,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteHalfBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(2).ToArray();
byte[] expected = [.. _bytes.Take(2)];
bool write = stream.WriteBigEndian(BitConverter.Int16BitsToHalf(0x0001));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -150,7 +194,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt24Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = stream.WriteAsInt24(0x020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -160,7 +204,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt24BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = stream.WriteAsInt24BigEndian(0x000102);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -170,7 +214,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt24Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = stream.WriteAsUInt24(0x020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -180,7 +224,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt24BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(3).ToArray();
byte[] expected = [.. _bytes.Take(3)];
bool write = stream.WriteAsUInt24BigEndian(0x000102);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -190,7 +234,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt32Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.Write(0x03020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -200,17 +244,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt32BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.WriteBigEndian(0x00010203);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt32BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(8)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadInt32BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt32Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.Write((uint)0x03020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -220,17 +275,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt32BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.WriteBigEndian((uint)0x00010203);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt32BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(8)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadUInt32BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteSingleTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.Write(BitConverter.Int32BitsToSingle(0x03020100));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -240,7 +306,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteSingleBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(4).ToArray();
byte[] expected = [.. _bytes.Take(4)];
bool write = stream.WriteBigEndian(BitConverter.Int32BitsToSingle(0x00010203));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -250,7 +316,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt48Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = stream.WriteAsInt48(0x050403020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -260,7 +326,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt48BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = stream.WriteAsInt48BigEndian(0x000102030405);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -270,7 +336,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt48Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = stream.WriteAsUInt48(0x050403020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -280,7 +346,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt48BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(6).ToArray();
byte[] expected = [.. _bytes.Take(6)];
bool write = stream.WriteAsUInt48BigEndian(0x000102030405);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -290,7 +356,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt64Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.Write(0x0706050403020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -300,17 +366,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt64BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.WriteBigEndian(0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteInt64BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(16)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadInt64BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt64Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.Write((ulong)0x0706050403020100);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -320,17 +397,28 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt64BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.WriteBigEndian((ulong)0x0001020304050607);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteUInt64BothEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = [.. _bytes.Take(16)];
int offset = 0;
stream.WriteBothEndian(_bytes.ReadUInt64BothEndian(ref offset));
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteDoubleTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.Write(BitConverter.Int64BitsToDouble(0x0706050403020100));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -340,7 +428,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteDoubleBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(8).ToArray();
byte[] expected = [.. _bytes.Take(8)];
bool write = stream.WriteBigEndian(BitConverter.Int64BitsToDouble(0x0001020304050607));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -350,7 +438,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteDecimalTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _decimalBytes.Take(16).ToArray();
byte[] expected = [.. _decimalBytes.Take(16)];
bool write = stream.Write(0.0123456789M);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -360,7 +448,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteDecimalBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _decimalBytes.Take(16).Reverse().ToArray();
byte[] expected = [.. _decimalBytes.Take(16).Reverse()];
bool write = stream.WriteBigEndian(0.0123456789M);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -370,7 +458,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteGuidTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.Write(new Guid(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -380,8 +468,8 @@ namespace SabreTools.IO.Test.Extensions
public void WriteGuidBigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
bool write = stream.WriteBigEndian(new Guid(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.WriteBigEndian(new Guid([.. Enumerable.Reverse(_bytes)]));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -390,7 +478,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt128Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.Write((Int128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -400,8 +488,8 @@ namespace SabreTools.IO.Test.Extensions
public void WriteInt128BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
bool write = stream.WriteBigEndian((Int128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.WriteBigEndian((Int128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -410,7 +498,7 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt128Test()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.Write((UInt128)new BigInteger(_bytes));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -420,8 +508,8 @@ namespace SabreTools.IO.Test.Extensions
public void WriteUInt128BigEndianTest()
{
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
byte[] expected = _bytes.Take(16).ToArray();
bool write = stream.WriteBigEndian((UInt128)new BigInteger(_bytes.Reverse().ToArray()));
byte[] expected = [.. _bytes.Take(16)];
bool write = stream.WriteBigEndian((UInt128)new BigInteger(Enumerable.Reverse(_bytes).ToArray()));
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
@@ -542,7 +630,7 @@ namespace SabreTools.IO.Test.Extensions
SecondValue = 0x07060504,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(12).ToArray();
byte[] expected = [.. bytesWithString.Take(12)];
bool write = stream.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
@@ -566,7 +654,7 @@ namespace SabreTools.IO.Test.Extensions
FourthValue = 0x0B0A,
FifthValue = "ABC",
};
byte[] expected = bytesWithString.Take(16).ToArray();
byte[] expected = [.. bytesWithString.Take(16)];
bool write = stream.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());

View File

@@ -73,4 +73,4 @@ namespace SabreTools.IO.Test.Extensions
#endregion
}
}
}

View File

@@ -30,7 +30,7 @@ namespace SabreTools.IO.Test.Extensions
public TestStructPoint[]? StructArray;
/// <summary>
/// Length of <see cref="LPByteArray"/>
/// Length of <see cref="LPByteArray"/>
/// </summary>
public ushort LPByteArrayLength;
@@ -39,7 +39,7 @@ namespace SabreTools.IO.Test.Extensions
/// </summary>
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]
public byte[]? LPByteArray;
// /// <summary>
// /// 4 entry nested byte array
// /// </summary>

View File

@@ -18,7 +18,7 @@ namespace SabreTools.IO.Test.Extensions
public uint FieldB;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal class TestStructInheritanceChild2 : TestStructInheritanceParent
{

View File

@@ -10,7 +10,7 @@ namespace SabreTools.IO.Test.Extensions
public int SecondValue;
public ushort ThirdValue;
public short FourthValue;
[MarshalAs(UnmanagedType.LPStr)]

View File

@@ -5,7 +5,7 @@ using Xunit;
namespace SabreTools.IO.Test.Matching
{
/// <remarks>
/// All other test cases are covered by <see cref="PathMatchTests"/>
/// All other test cases are covered by <see cref="PathMatchTests"/>
/// </remarks>
public class FilePathMatchTests
{

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
@@ -24,7 +24,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -37,4 +37,4 @@ namespace SabreTools.IO.Test.Streams
#endregion
}
}
}

View File

@@ -415,4 +415,4 @@ namespace SabreTools.IO.Test.Streams
#endregion
}
}
}

View File

@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Transform;
using Xunit;
namespace SabreTools.IO.Test.Transform
{
public class CombineTests
{
#region Concatenate
[Fact]
public void Concatenate_EmptyList_False()
{
List<string> paths = [];
string output = string.Empty;
bool actual = Combine.Concatenate(paths, output);
Assert.False(actual);
}
[Fact]
public void Concatenate_InvalidOutput_False()
{
List<string> paths = ["a"];
string output = string.Empty;
bool actual = Combine.Concatenate(paths, output);
Assert.False(actual);
}
[Fact]
public void Concatenate_FilledList_True()
{
List<string> paths = [
Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"),
Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-compress.bin"),
];
string output = Guid.NewGuid().ToString();
bool actual = Combine.Concatenate(paths, output);
Assert.True(actual);
string text = File.ReadAllText(output);
Assert.Equal("This doesn't match anythingThis is just a file that has a known set of hashes to make sure that everything with hashing is still working as anticipated.", text);
File.Delete(output);
}
#endregion
#region Interleave
[Fact]
public void Interleave_EvenNotExists_False()
{
string even = "NOT A REAL PATH";
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
Assert.False(actual);
}
[Fact]
public void Interleave_OddNotExists_False()
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = "NOT A REAL PATH";
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
Assert.False(actual);
}
[Fact]
public void Interleave_InvalidType_False()
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, (BlockSize)int.MaxValue);
Assert.False(actual);
}
[Theory]
[InlineData(BlockSize.Byte, "TThhiiss ddooeessnn''tt mmaattcchh aannyytthhiinngg")]
[InlineData(BlockSize.Word, "ThThisis d doeoesnsn't't m matatchch a anynyththiningg")]
[InlineData(BlockSize.Dword, "ThisThis doe doesn'tsn't mat match ach anythnythinging")]
[InlineData(BlockSize.Qword, "This doeThis doesn't matsn't match anythch anythinging")]
public void Interleave_SameLength_True(BlockSize type, string expected)
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, type);
Assert.True(actual);
string text = File.ReadAllText(output);
Assert.Equal(expected, text);
File.Delete(output);
}
[Fact]
public void Interleave_DifferentLength_True()
{
string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-compress.bin");
string output = Guid.NewGuid().ToString();
bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte);
Assert.True(actual);
string text = File.ReadAllText(output);
Assert.Equal("TThhiiss diose sjnu'stt maa tfcihl ea ntyhtahti nhgas a known set of hashes to make sure that everything with hashing is still working as anticipated.", text);
File.Delete(output);
}
#endregion
}
}

View File

@@ -0,0 +1,124 @@
using System;
using System.IO;
using SabreTools.IO.Transform;
using Xunit;
namespace SabreTools.IO.Test.Transform
{
public class SplitTests
{
#region BlockSplit
[Fact]
public void BlockSplit_EmptyFileName_False()
{
string input = string.Empty;
string outputDir = string.Empty;
bool actual = Split.BlockSplit(input, outputDir, BlockSize.Byte);
Assert.False(actual);
}
[Fact]
public void BlockSplit_InvalidFile_False()
{
string input = "INVALID";
string outputDir = string.Empty;
bool actual = Split.BlockSplit(input, outputDir, BlockSize.Byte);
Assert.False(actual);
}
[Fact]
public void BlockSplit_InvalidType_False()
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string outputDir = Guid.NewGuid().ToString();
bool actual = Split.BlockSplit(input, outputDir, (BlockSize)int.MaxValue);
Assert.False(actual);
}
[Theory]
[InlineData(BlockSize.Byte, "Ti os' ac ntig", "hsdentmthayhn")]
[InlineData(BlockSize.Word, "Th dsn mchnyin", "isoe'tat athg")]
[InlineData(BlockSize.Dword, "Thissn'tch aing", " doe matnyth")]
[InlineData(BlockSize.Qword, "This doech anyth", "sn't mating")]
public void BlockSplit_ValidFile_True(BlockSize type, string expectedEven, string expectedOdd)
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string outputDir = Guid.NewGuid().ToString();
bool actual = Split.BlockSplit(input, outputDir, type);
Assert.True(actual);
string baseFilename = Path.GetFileName(input);
string text = File.ReadAllText(Path.Combine(outputDir, $"{baseFilename}.even"));
Assert.Equal(expectedEven, text);
text = File.ReadAllText(Path.Combine(outputDir, $"{baseFilename}.odd"));
Assert.Equal(expectedOdd, text);
File.Delete($"{baseFilename}.even");
File.Delete($"{baseFilename}.odd");
}
#endregion
#region SizeSplit
[Fact]
public void SizeSplit_EmptyFileName_False()
{
string input = string.Empty;
string outputDir = string.Empty;
int size = 1;
bool actual = Split.SizeSplit(input, outputDir, size);
Assert.False(actual);
}
[Fact]
public void SizeSplit_InvalidFile_False()
{
string input = "INVALID";
string outputDir = string.Empty;
int size = 1;
bool actual = Split.SizeSplit(input, outputDir, size);
Assert.False(actual);
}
[Fact]
public void SizeSplit_InvalidSize_False()
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string outputDir = string.Empty;
int size = 0;
bool actual = Split.SizeSplit(input, outputDir, size);
Assert.False(actual);
}
[Fact]
public void SizeSplit_Valid_True()
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string outputDir = Guid.NewGuid().ToString();
int size = 16;
bool actual = Split.SizeSplit(input, outputDir, size);
Assert.True(actual);
Assert.Equal(2, Directory.GetFiles(outputDir).Length);
string baseFilename = Path.GetFileName(input);
string text = File.ReadAllText(Path.Combine(outputDir, $"{baseFilename}.0"));
Assert.Equal("This doesn't mat", text);
text = File.ReadAllText(Path.Combine(outputDir, $"{baseFilename}.1"));
Assert.Equal("ch anything", text);
File.Delete($"{baseFilename}.0");
File.Delete($"{baseFilename}.1");
}
#endregion
}
}

View File

@@ -0,0 +1,76 @@
using System;
using System.IO;
using SabreTools.IO.Extensions;
using SabreTools.IO.Transform;
using Xunit;
namespace SabreTools.IO.Test.Transform
{
public class SwapTests
{
#region Process
[Fact]
public void Process_EmptyFileName_False()
{
string input = string.Empty;
string output = string.Empty;
bool actual = Swap.Process(input, output, Operation.Byteswap);
Assert.False(actual);
}
[Fact]
public void Process_InvalidFile_False()
{
string input = "INVALID";
string output = string.Empty;
bool actual = Swap.Process(input, output, Operation.Byteswap);
Assert.False(actual);
}
[Fact]
public void Process_InvalidType_False()
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
bool actual = Swap.Process(input, output, (Operation)int.MaxValue);
Assert.False(actual);
}
[Fact]
public void Process_Valid_True()
{
string input = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt");
string output = Guid.NewGuid().ToString();
// Bitswap
bool actual = Swap.Process(input, output, Operation.Bitswap);
Assert.True(actual);
byte[] actualBytes = File.ReadAllBytes(output);
Assert.True(new byte[] { 0x2A, 0x16, 0x96, 0xCE, 0x04, 0x26, 0xF6, 0xA6, 0xCE, 0x76, 0xE4, 0x2E, 0x04, 0xB6, 0x86, 0x2E, 0xC6, 0x16, 0x04, 0x86, 0x76, 0x9E, 0x2E, 0x16, 0x96, 0x76, 0xE6 }.EqualsExactly(actualBytes));
// Byteswap
actual = Swap.Process(input, output, Operation.Byteswap);
Assert.True(actual);
actualBytes = File.ReadAllBytes(output);
Assert.True(new byte[] { 0x68, 0x54, 0x73, 0x69, 0x64, 0x20, 0x65, 0x6F, 0x6E, 0x73, 0x74, 0x27, 0x6D, 0x20, 0x74, 0x61, 0x68, 0x63, 0x61, 0x20, 0x79, 0x6E, 0x68, 0x74, 0x6E, 0x69, 0x67 }.EqualsExactly(actualBytes));
// Wordswap
actual = Swap.Process(input, output, Operation.Wordswap);
Assert.True(actual);
actualBytes = File.ReadAllBytes(output);
Assert.True(new byte[] { 0x69, 0x73, 0x54, 0x68, 0x6F, 0x65, 0x20, 0x64, 0x27, 0x74, 0x73, 0x6E, 0x61, 0x74, 0x20, 0x6D, 0x20, 0x61, 0x63, 0x68, 0x74, 0x68, 0x6E, 0x79, 0x69, 0x6E, 0x67 }.EqualsExactly(actualBytes));
// WordByteswap
actual = Swap.Process(input, output, Operation.WordByteswap);
Assert.True(actual);
actualBytes = File.ReadAllBytes(output);
Assert.True(new byte[] { 0x73, 0x69, 0x68, 0x54, 0x65, 0x6F, 0x64, 0x20, 0x74, 0x27, 0x6E, 0x73, 0x74, 0x61, 0x6D, 0x20, 0x61, 0x20, 0x68, 0x63, 0x68, 0x74, 0x79, 0x6E, 0x69, 0x6E, 0x67 }.EqualsExactly(actualBytes));
File.Delete(output);
}
#endregion
}
}

View File

@@ -7,22 +7,98 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.IO", "SabreTools
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.IO.Test", "SabreTools.IO.Test\SabreTools.IO.Test.csproj", "{A9767735-5042-48A1-849C-96035DB1DD53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Text", "SabreTools.Text\SabreTools.Text.csproj", "{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Numerics", "SabreTools.Numerics\SabreTools.Numerics.csproj", "{565EAC34-B533-4C5C-9B81-77AE4B33A027}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Numerics.Test", "SabreTools.Numerics.Test\SabreTools.Numerics.Test.csproj", "{35F068EF-A652-4435-8890-1E6239EEBEFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Text.Test", "SabreTools.Text.Test\SabreTools.Text.Test.csproj", "{71513020-B588-4A66-8122-E1639A39B8A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|x64.ActiveCfg = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|x64.Build.0 = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|x86.ActiveCfg = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Debug|x86.Build.0 = Debug|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|Any CPU.Build.0 = Release|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|x64.ActiveCfg = Release|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|x64.Build.0 = Release|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|x86.ActiveCfg = Release|Any CPU
{87CE4411-80D9-49FF-894C-761F1C20D9A5}.Release|x86.Build.0 = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|x64.ActiveCfg = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|x64.Build.0 = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|x86.ActiveCfg = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Debug|x86.Build.0 = Debug|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|Any CPU.Build.0 = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|x64.ActiveCfg = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|x64.Build.0 = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|x86.ActiveCfg = Release|Any CPU
{A9767735-5042-48A1-849C-96035DB1DD53}.Release|x86.Build.0 = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|x64.ActiveCfg = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|x64.Build.0 = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|x86.ActiveCfg = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Debug|x86.Build.0 = Debug|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|Any CPU.Build.0 = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|x64.ActiveCfg = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|x64.Build.0 = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|x86.ActiveCfg = Release|Any CPU
{EC8FD8AD-579F-4B8B-A65E-119E5294ADC1}.Release|x86.Build.0 = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|Any CPU.Build.0 = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|x64.ActiveCfg = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|x64.Build.0 = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|x86.ActiveCfg = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Debug|x86.Build.0 = Debug|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|Any CPU.ActiveCfg = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|Any CPU.Build.0 = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|x64.ActiveCfg = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|x64.Build.0 = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|x86.ActiveCfg = Release|Any CPU
{565EAC34-B533-4C5C-9B81-77AE4B33A027}.Release|x86.Build.0 = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|x64.ActiveCfg = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|x64.Build.0 = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|x86.ActiveCfg = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Debug|x86.Build.0 = Debug|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|Any CPU.Build.0 = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|x64.ActiveCfg = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|x64.Build.0 = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|x86.ActiveCfg = Release|Any CPU
{35F068EF-A652-4435-8890-1E6239EEBEFB}.Release|x86.Build.0 = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|x64.ActiveCfg = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|x64.Build.0 = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|x86.ActiveCfg = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Debug|x86.Build.0 = Debug|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|Any CPU.Build.0 = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|x64.ActiveCfg = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|x64.Build.0 = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|x86.ActiveCfg = Release|Any CPU
{71513020-B588-4A66-8122-E1639A39B8A8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -111,4 +111,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}

View File

@@ -93,14 +93,14 @@ namespace SabreTools.IO.Compression.BZip2
{
internal class BZip2Compressor
{
private int blockSize100k; // 0...9
private readonly int blockSize100k; // 0...9
private int currentByte = -1;
private int runLength = 0;
private int last; // index into the block of the last char processed
private int outBlockFillThreshold;
private CompressionState cstate;
private readonly CRC32 crc = new CRC32(true);
BitWriter bw;
private readonly int outBlockFillThreshold;
private readonly CompressionState cstate;
private readonly CRC32 crc = new(true);
readonly BitWriter bw;
int runs;
/*
@@ -132,9 +132,9 @@ namespace SabreTools.IO.Compression.BZip2
* Possibly because the number of elems to sort is usually small, typically
* &lt;= 20.
*/
private static readonly int[] increments = { 1, 4, 13, 40, 121, 364, 1093, 3280,
private static readonly int[] increments = [ 1, 4, 13, 40, 121, 364, 1093, 3280,
9841, 29524, 88573, 265720, 797161,
2391484 };
2391484 ];
/// <summary>
/// BZip2Compressor writes its compressed data out via a BitWriter. This
@@ -1175,9 +1175,13 @@ namespace SabreTools.IO.Compression.BZip2
while (ll_i != tmp)
{
j++;
#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER
(yy[j], tmp) = (tmp, yy[j]);
#else
byte tmp2 = tmp;
tmp = yy[j];
yy[j] = tmp2;
#endif
}
yy[0] = tmp;
@@ -1690,9 +1694,13 @@ namespace SabreTools.IO.Compression.BZip2
while (ll_i != tmp)
{
j++;
#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER
(pos[j], tmp) = (tmp, pos[j]);
#else
byte tmp2 = tmp;
tmp = pos[j];
pos[j] = tmp2;
#endif
}
pos[0] = tmp;
@@ -1939,4 +1947,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}

View File

@@ -64,7 +64,7 @@ namespace SabreTools.IO.Compression.BZip2
public class BZip2InputStream : System.IO.Stream
{
bool _disposed;
bool _leaveOpen;
readonly bool _leaveOpen;
Int64 totalBytesRead;
private int last;
@@ -79,7 +79,7 @@ namespace SabreTools.IO.Compression.BZip2
private bool blockRandomised;
private int bsBuff;
private int bsLive;
private readonly CRC32 crc = new CRC32(true);
private readonly CRC32 crc = new(true);
private int nInUse;
private Stream input;
private int currentChar = -1;
@@ -491,8 +491,7 @@ namespace SabreTools.IO.Compression.BZip2
this.blockRandomised = (GetBits(1) == 1);
// Lazily allocate data
if (this.data == null)
this.data = new DecompressionState(this.blockSize100k);
this.data ??= new DecompressionState(this.blockSize100k);
// currBlockNo++;
getAndMoveToFrontDecode();
@@ -1383,4 +1382,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}

View File

@@ -92,14 +92,14 @@ namespace SabreTools.IO.Compression.BZip2
public class BZip2OutputStream : System.IO.Stream
{
int totalBytesWrittenIn;
bool leaveOpen;
readonly bool leaveOpen;
BZip2Compressor compressor;
uint combinedCRC;
Stream output;
BitWriter bw;
int blockSize100k; // 0...9
readonly int blockSize100k; // 0...9
private TraceBits desiredTrace = TraceBits.Crc | TraceBits.Write;
private readonly TraceBits desiredTrace = TraceBits.Crc | TraceBits.Write;
/// <summary>
/// Constructs a new <c>BZip2OutputStream</c>, that sends its

View File

@@ -79,7 +79,6 @@
// by multiple distinct threads, for different blocks of data.
//
using System;
using System.IO;
namespace SabreTools.IO.Compression.BZip2
@@ -88,7 +87,7 @@ namespace SabreTools.IO.Compression.BZip2
{
uint accumulator;
int nAccumulatedBits;
Stream output;
readonly Stream output;
int totalBytesWrittenOut;
public BitWriter(Stream s)
@@ -243,4 +242,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}

View File

@@ -29,7 +29,6 @@ using System;
using Interop = System.Runtime.InteropServices;
#nullable disable
#pragma warning disable CS0618
namespace SabreTools.IO.Compression.BZip2
{
/// <summary>
@@ -101,13 +100,13 @@ namespace SabreTools.IO.Compression.BZip2
_TotalBytesRead = 0;
int count = input.Read(buffer, 0, readSize);
if (output != null) output.Write(buffer, 0, count);
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);
output?.Write(buffer, 0, count);
_TotalBytesRead += count;
}
@@ -279,7 +278,7 @@ namespace SabreTools.IO.Compression.BZip2
crc32Table[i] = dwCrc;
}
i++;
} while (i!=0);
} while (i != 0);
}
#if VERBOSE
@@ -303,10 +302,10 @@ namespace SabreTools.IO.Compression.BZip2
private uint gf2_matrix_times(uint[] matrix, uint vec)
{
uint sum = 0;
int i=0;
int i = 0;
while (vec != 0)
{
if ((vec & 0x01)== 0x01)
if ((vec & 0x01) == 0x01)
sum ^= matrix[i];
vec >>= 1;
i++;
@@ -341,8 +340,8 @@ namespace SabreTools.IO.Compression.BZip2
if (length == 0)
return;
uint crc1= ~_register;
uint crc2= (uint) crc;
uint crc1 = ~_register;
uint crc2 = (uint)crc;
// put operator for one zero bit in odd
odd[0] = this.dwPolynomial; // the CRC-32 polynomial
@@ -359,15 +358,16 @@ namespace SabreTools.IO.Compression.BZip2
// put operator for four zero bits in odd
gf2_matrix_square(odd, even);
uint len2 = (uint) length;
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 {
do
{
// apply zeros operator for this bit of len2
gf2_matrix_square(even, odd);
if ((len2 & 1)== 1)
if ((len2 & 1) == 1)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
@@ -376,7 +376,7 @@ namespace SabreTools.IO.Compression.BZip2
// another iteration of the loop with odd and even swapped
gf2_matrix_square(odd, even);
if ((len2 & 1)==1)
if ((len2 & 1) == 1)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
@@ -385,7 +385,7 @@ namespace SabreTools.IO.Compression.BZip2
crc1 ^= crc2;
_register= ~crc1;
_register = ~crc1;
//return (int) crc1;
return;
@@ -417,7 +417,7 @@ namespace SabreTools.IO.Compression.BZip2
/// </para>
/// </remarks>
public CRC32(bool reverseBits) :
this( unchecked((int)0xEDB88320), reverseBits)
this(unchecked((int)0xEDB88320), reverseBits)
{
}
@@ -450,7 +450,7 @@ namespace SabreTools.IO.Compression.BZip2
public CRC32(int polynomial, bool reverseBits)
{
this.reverseBits = reverseBits;
this.dwPolynomial = (uint) polynomial;
this.dwPolynomial = (uint)polynomial;
this.GenerateLookupTable();
}
@@ -469,9 +469,9 @@ namespace SabreTools.IO.Compression.BZip2
}
// private member vars
private UInt32 dwPolynomial;
private readonly UInt32 dwPolynomial;
private Int64 _TotalBytesRead;
private bool reverseBits;
private readonly bool reverseBits;
private UInt32[] crc32Table;
private const int BUFFER_SIZE = 8192;
private UInt32 _register = 0xFFFFFFFFU;
@@ -502,8 +502,8 @@ namespace SabreTools.IO.Compression.BZip2
private static readonly Int64 UnsetLengthLimit = -99;
internal System.IO.Stream _innerStream;
private CRC32 _Crc32;
private Int64 _lengthLimit = -99;
private readonly CRC32 _Crc32;
private readonly Int64 _lengthLimit = -99;
private bool _leaveOpen;
/// <summary>
@@ -812,4 +812,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}

View File

@@ -28,8 +28,8 @@ namespace SabreTools.IO.Compression.BZip2
{
internal static class Rand
{
private static int[] RNUMS =
{
private static readonly 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,
@@ -82,7 +82,7 @@ namespace SabreTools.IO.Compression.BZip2
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
936, 638
};
];
/// <summary>
@@ -96,4 +96,4 @@ namespace SabreTools.IO.Compression.BZip2
}
}
}
}

View File

@@ -41,7 +41,7 @@ namespace SabreTools.IO.Compression.Blast
/// 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").
@@ -168,29 +168,29 @@ namespace SabreTools.IO.Compression.Blast
/// 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

View File

@@ -138,7 +138,7 @@ namespace SabreTools.IO.Compression.Blast
/// 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

View File

@@ -106,10 +106,6 @@ namespace SabreTools.IO.Compression.Deflate
// 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>
@@ -172,9 +168,7 @@ namespace SabreTools.IO.Compression.Deflate
}
return (uint)((s2 << 16) | s1);
}
#pragma warning restore 3001
#pragma warning restore 3002
}
}
}

View File

@@ -75,4 +75,4 @@ namespace SabreTools.IO.Compression.Deflate
FinishStarted, // finish started, need only more output at next deflate
FinishDone // finish done, accept no more input or output
}
}
}

View File

@@ -29,7 +29,6 @@ using System;
using Interop = System.Runtime.InteropServices;
#nullable disable
#pragma warning disable CS0618
namespace SabreTools.IO.Compression.Deflate
{
/// <summary>
@@ -101,13 +100,13 @@ namespace SabreTools.IO.Compression.Deflate
_TotalBytesRead = 0;
int count = input.Read(buffer, 0, readSize);
if (output != null) output.Write(buffer, 0, count);
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);
output?.Write(buffer, 0, count);
_TotalBytesRead += count;
}
@@ -469,9 +468,9 @@ namespace SabreTools.IO.Compression.Deflate
}
// private member vars
private UInt32 dwPolynomial;
private readonly UInt32 dwPolynomial;
private Int64 _TotalBytesRead;
private bool reverseBits;
private readonly bool reverseBits;
private UInt32[] crc32Table;
private const int BUFFER_SIZE = 8192;
private UInt32 _register = 0xFFFFFFFFU;
@@ -502,8 +501,8 @@ namespace SabreTools.IO.Compression.Deflate
private static readonly Int64 UnsetLengthLimit = -99;
internal System.IO.Stream _innerStream;
private CRC32 _Crc32;
private Int64 _lengthLimit = -99;
private readonly CRC32 _Crc32;
private readonly Int64 _lengthLimit = -99;
private bool _leaveOpen;
/// <summary>
@@ -812,4 +811,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}

View File

@@ -165,4 +165,4 @@ namespace SabreTools.IO.Compression.Deflate
Level9 = 9,
}
}
}

View File

@@ -103,4 +103,4 @@ namespace SabreTools.IO.Compression.Deflate
Decompress = 1,
}
}
}

View File

@@ -116,4 +116,4 @@ namespace SabreTools.IO.Compression.Deflate
HuffmanOnly = 2,
}
}
}

View File

@@ -116,7 +116,7 @@ namespace SabreTools.IO.Compression.Deflate
static Config()
{
Table = new Config[] {
Table = [
new Config(0, 0, 0, 0, DeflateFlavor.Store),
new Config(4, 4, 8, 4, DeflateFlavor.Fast),
new Config(4, 5, 16, 8, DeflateFlavor.Fast),
@@ -128,7 +128,7 @@ namespace SabreTools.IO.Compression.Deflate
new Config(8, 32, 128, 256, DeflateFlavor.Slow),
new Config(32, 128, 258, 1024, DeflateFlavor.Slow),
new Config(32, 258, 258, 4096, DeflateFlavor.Slow),
};
];
}
private static readonly Config[] Table;
@@ -137,8 +137,8 @@ namespace SabreTools.IO.Compression.Deflate
private CompressFunc DeflateFunction;
private static readonly System.String[] _ErrorMessage = new System.String[]
{
private static readonly System.String[] _ErrorMessage =
[
"need dictionary",
"stream end",
"",
@@ -149,7 +149,7 @@ namespace SabreTools.IO.Compression.Deflate
"buffer error",
"incompatible version",
""
};
];
// preset dictionary flag in zlib header
private static readonly int PRESET_DICT = 0x20;
@@ -256,9 +256,9 @@ namespace SabreTools.IO.Compression.Deflate
internal short[] dyn_dtree; // distance tree
internal short[] bl_tree; // Huffman tree for bit lengths
internal Tree treeLiterals = new Tree(); // desc for literal tree
internal Tree treeDistances = new Tree(); // desc for distance tree
internal Tree treeBitLengths = new Tree(); // desc for bit length tree
internal Tree treeLiterals = new(); // desc for literal tree
internal Tree treeDistances = new(); // desc for distance tree
internal Tree treeBitLengths = new(); // desc for bit length tree
// number of codes at each bit length for an optimal tree
internal short[] bl_count = new short[InternalConstants.MAX_BITS + 1];
@@ -1868,4 +1868,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}

View File

@@ -653,13 +653,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <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();
}
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();
}
@@ -683,14 +681,12 @@ namespace SabreTools.IO.Compression.Deflate
/// <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);
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();
}
ZlibBaseStream.CompressBuffer(b, compressor);
return ms.ToArray();
}
@@ -710,13 +706,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <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);
using var input = new System.IO.MemoryStream(compressed);
System.IO.Stream decompressor =
new DeflateStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
@@ -736,13 +730,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <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);
using var input = new System.IO.MemoryStream(compressed);
System.IO.Stream decompressor =
new DeflateStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
}

View File

@@ -31,4 +31,4 @@ namespace SabreTools.IO.Compression.Deflate
/// </summary>
FAIL,
}
}
}

View File

@@ -130,4 +130,4 @@ namespace SabreTools.IO.Compression.Deflate
Finish,
}
}
}

View File

@@ -547,7 +547,8 @@ namespace SabreTools.IO.Compression.Deflate
virtual public FlushType FlushMode
{
get { return (this._baseStream._flushMode); }
set {
set
{
if (_disposed) throw new ObjectDisposedException("GZipStream");
this._baseStream._flushMode = value;
}
@@ -852,7 +853,7 @@ namespace SabreTools.IO.Compression.Deflate
#endregion
internal static readonly System.DateTime _unixEpoch = new System.DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
internal static readonly System.DateTime _unixEpoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
#if SILVERLIGHT || NETCF
internal static readonly System.Text.Encoding iso8859dash1 = new Ionic.Encoding.Iso8859Dash1Encoding();
#else
@@ -944,13 +945,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>The string in compressed form</returns>
public static byte[] CompressString(String s)
{
using (var ms = new MemoryStream())
{
System.IO.Stream compressor =
new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
ZlibBaseStream.CompressString(s, compressor);
return ms.ToArray();
}
using var ms = new MemoryStream();
System.IO.Stream compressor =
new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
ZlibBaseStream.CompressString(s, compressor);
return ms.ToArray();
}
@@ -972,14 +971,12 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>The data in compressed form</returns>
public static byte[] CompressBuffer(byte[] b)
{
using (var ms = new MemoryStream())
{
System.IO.Stream compressor =
new GZipStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression );
using var ms = new MemoryStream();
System.IO.Stream compressor =
new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
ZlibBaseStream.CompressBuffer(b, compressor);
return ms.ToArray();
}
ZlibBaseStream.CompressBuffer(b, compressor);
return ms.ToArray();
}
@@ -997,11 +994,9 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>The uncompressed string</returns>
public static String UncompressString(byte[] compressed)
{
using (var input = new MemoryStream(compressed))
{
Stream decompressor = new GZipStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
using var input = new MemoryStream(compressed);
Stream decompressor = new GZipStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
@@ -1019,13 +1014,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <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 GZipStream( input, CompressionMode.Decompress );
using var input = new System.IO.MemoryStream(compressed);
System.IO.Stream decompressor =
new GZipStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}

View File

@@ -1,45 +1,45 @@
// Inftree.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
// 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.
// 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):
// 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,
// 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
//
// 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,
@@ -50,7 +50,7 @@
// 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
@@ -83,31 +83,31 @@ namespace SabreTools.IO.Compression.Deflate
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,
internal static readonly int[] fixed_tl = [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};
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 };
internal static readonly int[] fixed_td = [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 };
internal static readonly int[] cplens = [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 };
internal static readonly int[] cplext = [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 };
internal static readonly int[] cpdist = [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 };
internal static readonly int[] cpdext = [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[] 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
@@ -432,4 +432,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}
}

View File

@@ -71,8 +71,7 @@ namespace SabreTools.IO.Compression.Deflate
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 };
internal static readonly int[] border = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
private enum InflateBlockMode
{
@@ -98,7 +97,7 @@ namespace SabreTools.IO.Compression.Deflate
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 InflateCodes codes = new(); // if CODES, current state
internal int last; // true if this block is the last block
@@ -115,7 +114,7 @@ namespace SabreTools.IO.Compression.Deflate
internal System.Object checkfn; // check function
internal uint check; // check on output
internal InfTree inftree = new InfTree();
internal InfTree inftree = new();
internal InflateBlocks(ZlibCodec codec, System.Object checkfn, int w)
{
@@ -545,8 +544,8 @@ namespace SabreTools.IO.Compression.Deflate
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[] bl = [9]; // must be <= 9 for lookahead assumptions
int[] bd = [6]; // must be <= 9 for lookahead assumptions
int[] tl = new int[1];
int[] td = new int[1];
@@ -734,4 +733,4 @@ namespace SabreTools.IO.Compression.Deflate
return r;
}
}
}
}

View File

@@ -720,4 +720,4 @@ namespace SabreTools.IO.Compression.Deflate
return ZlibConstants.Z_OK;
}
}
}
}

View File

@@ -135,8 +135,7 @@ namespace SabreTools.IO.Compression.Deflate
internal int End()
{
if (blocks != null)
blocks.Free();
blocks?.Free();
blocks = null;
return ZlibConstants.Z_OK;
}
@@ -392,7 +391,7 @@ namespace SabreTools.IO.Compression.Deflate
}
private static readonly byte[] mark = new byte[] { 0, 0, 0xff, 0xff };
private static readonly byte[] mark = [0, 0, 0xff, 0xff];
internal int Sync()
{
@@ -462,4 +461,4 @@ namespace SabreTools.IO.Compression.Deflate
return blocks.SyncPoint();
}
}
}
}

View File

@@ -467,4 +467,4 @@ namespace SabreTools.IO.Compression.Deflate
#endregion
}
}
}

View File

@@ -90,25 +90,25 @@ namespace SabreTools.IO.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 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);
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;
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;
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;
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;
internal static readonly int REPZ_11_138 = 18;
}
}
}

View File

@@ -66,10 +66,10 @@ namespace SabreTools.IO.Compression.Deflate
internal static class InternalInflateConstants
{
// And'ing with mask[n] masks the lower n bits
internal static readonly int[] InflateMask = new int[] {
internal static readonly int[] InflateMask = [
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff };
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff ];
}
}
}

View File

@@ -159,4 +159,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}

View File

@@ -90,7 +90,7 @@ namespace SabreTools.IO.Compression.Deflate
{
internal sealed class StaticTree
{
internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
internal static readonly short[] lengthAndLiteralsTreeCodes = [
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,
@@ -127,13 +127,13 @@ namespace SabreTools.IO.Compression.Deflate
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[] {
internal static readonly short[] distTreeCodes = [
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 };
3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 ];
internal static readonly StaticTree Literals;
internal static readonly StaticTree Distances;
@@ -161,4 +161,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}

View File

@@ -1,20 +1,20 @@
// Tree.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
// 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.
// 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):
// last saved (in emacs):
// Time-stamp: <2009-October-28 13:29:50>
//
// ------------------------------------------------------------------
@@ -25,22 +25,22 @@
// 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
//
// 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,
@@ -51,7 +51,7 @@
// 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
@@ -66,74 +66,74 @@ namespace SabreTools.IO.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[]
{
internal static readonly int[] ExtraLengthBits =
[
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[]
{
internal static readonly int[] ExtraDistanceBits =
[
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};
internal static readonly int[] extra_blbits = [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 = [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,
private static readonly sbyte[] _dist_code =
[
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,
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[]
{
];
internal static readonly sbyte[] LengthCode =
[
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,
@@ -150,27 +150,27 @@ namespace SabreTools.IO.Compression.Deflate
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[]
{
internal static readonly int[] LengthBase =
[
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[]
{
internal static readonly int[] DistanceBase =
[
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>
/// <remarks>
/// No side effects. _dist_code[256] and _dist_code[257] are never used.
/// </remarks>
internal static int DistanceCode(int dist)
@@ -183,7 +183,7 @@ namespace SabreTools.IO.Compression.Deflate
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
@@ -192,7 +192,7 @@ namespace SabreTools.IO.Compression.Deflate
// 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)
internal void gen_bitlen(DeflateManager s)
{
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
@@ -205,14 +205,14 @@ namespace SabreTools.IO.Compression.Deflate
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];
@@ -221,12 +221,12 @@ namespace SabreTools.IO.Compression.Deflate
{
bits = max_length; overflow++;
}
tree[n * 2 + 1] = (short) bits;
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)
@@ -237,24 +237,24 @@ namespace SabreTools.IO.Compression.Deflate
s.static_len += f * (stree[n * 2 + 1] + xbits);
}
if (overflow == 0)
return ;
return;
// This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase:
do
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[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];
@@ -265,35 +265,35 @@ namespace SabreTools.IO.Compression.Deflate
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;
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)
internal void build_tree(DeflateManager s)
{
short[] tree = dyn_tree;
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
int elems = staticTree.elems;
int elems = staticTree.elems;
int n, m; // iterate over heap elements
int max_code = -1; // largest code with non zero frequency
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)
@@ -306,14 +306,14 @@ namespace SabreTools.IO.Compression.Deflate
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);
node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0);
tree[node * 2] = 1;
s.depth[node] = 0;
s.opt_len--;
@@ -322,93 +322,94 @@ namespace SabreTools.IO.Compression.Deflate
// 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
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;
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)
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);
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)));
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
do
{
res |= code & 1;
code >>= 1; //SharedUtils.URShift(code, 1);
@@ -418,4 +419,4 @@ namespace SabreTools.IO.Compression.Deflate
return res >> 1;
}
}
}
}

View File

@@ -28,7 +28,6 @@ using System;
using System.IO;
#nullable disable
#pragma warning disable CA2022
namespace SabreTools.IO.Compression.Deflate
{
internal class ZlibBaseStream : System.IO.Stream
@@ -49,7 +48,7 @@ namespace SabreTools.IO.Compression.Deflate
protected internal CompressionStrategy Strategy = CompressionStrategy.Default;
// workitem 7159
CRC32 crc;
readonly CRC32 crc;
protected internal string _GzipFileName;
protected internal string _GzipComment;
protected internal DateTime _GzipMtime;
@@ -115,8 +114,7 @@ namespace SabreTools.IO.Compression.Deflate
{
get
{
if (_workingBuffer == null)
_workingBuffer = new byte[_bufferSize];
_workingBuffer ??= new byte[_bufferSize];
return _workingBuffer;
}
}
@@ -127,8 +125,7 @@ namespace SabreTools.IO.Compression.Deflate
{
// workitem 7159
// calculate the CRC on the unccompressed data (before writing)
if (crc != null)
crc.SlurpBlock(buffer, offset, count);
crc?.SlurpBlock(buffer, offset, count);
if (_streamMode == StreamMode.Undefined)
_streamMode = StreamMode.Writer;
@@ -142,7 +139,7 @@ namespace SabreTools.IO.Compression.Deflate
z.InputBuffer = buffer;
_z.NextIn = offset;
_z.AvailableBytesIn = count;
bool done = false;
bool done;
do
{
_z.OutputBuffer = workingBuffer;
@@ -175,7 +172,7 @@ namespace SabreTools.IO.Compression.Deflate
if (_streamMode == StreamMode.Writer)
{
bool done = false;
bool done;
do
{
_z.OutputBuffer = workingBuffer;
@@ -363,7 +360,7 @@ namespace SabreTools.IO.Compression.Deflate
list.Add(_buf1[0]);
}
} while (!done);
byte[] a = list.ToArray();
byte[] a = [.. list];
return GZipStream.iso8859dash1.GetString(a, 0, a.Length);
}
@@ -406,7 +403,7 @@ namespace SabreTools.IO.Compression.Deflate
if ((header[3] & 0x10) == 0x010)
_GzipComment = ReadZeroTerminatedString();
if ((header[3] & 0x02) == 0x02)
Read(_buf1, 0, 1); // CRC16, ignore
_ = Read(_buf1, 0, 1); // CRC16, ignore
return totalBytesRead;
}
@@ -448,7 +445,7 @@ namespace SabreTools.IO.Compression.Deflate
if (offset < buffer.GetLowerBound(0)) throw new ArgumentOutOfRangeException("offset");
if ((offset + count) > buffer.GetLength(0)) throw new ArgumentOutOfRangeException("count");
int rc = 0;
int rc;
// set up the output of the deflate/inflate codec:
_z.OutputBuffer = buffer;
@@ -519,8 +516,7 @@ namespace SabreTools.IO.Compression.Deflate
rc = (count - _z.AvailableBytesOut);
// calculate CRC after reading
if (crc != null)
crc.SlurpBlock(buffer, offset, rc);
crc?.SlurpBlock(buffer, offset, rc);
return rc;
}
@@ -584,40 +580,36 @@ namespace SabreTools.IO.Compression.Deflate
// workitem 8460
byte[] working = new byte[1024];
var encoding = System.Text.Encoding.UTF8;
using (var output = new MemoryStream())
using var output = new MemoryStream();
using (decompressor)
{
using (decompressor)
int n;
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
{
int n;
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
{
output.Write(working, 0, n);
}
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();
}
// 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 var output = new MemoryStream();
using (decompressor)
{
using (decompressor)
int n;
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
{
int n;
while ((n = decompressor.Read(working, 0, working.Length)) != 0)
{
output.Write(working, 0, n);
}
output.Write(working, 0, n);
}
return output.ToArray();
}
return output.ToArray();
}
/// <summary>

View File

@@ -1,20 +1,20 @@
// ZlibCodec.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
// 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.
// 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):
// last saved (in emacs):
// Time-stamp: <2009-November-03 15:40:51>
//
// ------------------------------------------------------------------
@@ -28,22 +28,22 @@
// 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
//
// 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,
@@ -54,7 +54,7 @@
// 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
@@ -68,7 +68,6 @@ using System;
using Interop=System.Runtime.InteropServices;
#nullable disable
#pragma warning disable CS0618
namespace SabreTools.IO.Compression.Deflate
{
/// <summary>
@@ -83,7 +82,7 @@ namespace SabreTools.IO.Compression.Deflate
/// </remarks>
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000D")]
[Interop.ComVisible(true)]
#if !NETCF
#if !NETCF
[Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
#endif
sealed public class ZlibCodec
@@ -94,15 +93,15 @@ namespace SabreTools.IO.Compression.Deflate
public byte[] InputBuffer;
/// <summary>
/// An index into the InputBuffer array, indicating where to start reading.
/// 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.
/// 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.
/// 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;
@@ -118,15 +117,15 @@ namespace SabreTools.IO.Compression.Deflate
public byte[] OutputBuffer;
/// <summary>
/// An index into the OutputBuffer array, indicating where to start writing.
/// 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.
/// 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.
/// 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;
@@ -152,13 +151,13 @@ namespace SabreTools.IO.Compression.Deflate
public CompressionLevel CompressLevel = CompressionLevel.Default;
/// <summary>
/// The number of Window Bits to use.
/// 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
/// 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.
/// a 32k window.
/// </remarks>
public int WindowBits = ZlibConstants.WindowBitsDefault;
@@ -188,9 +187,9 @@ namespace SabreTools.IO.Compression.Deflate
/// 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.
/// 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() { }
@@ -216,10 +215,10 @@ namespace SabreTools.IO.Compression.Deflate
}
/// <summary>
/// Initialize the inflation state.
/// Initialize the inflation state.
/// </summary>
/// <remarks>
/// It is not necessary to call this before using the ZlibCodec to inflate data;
/// 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>
@@ -252,20 +251,20 @@ namespace SabreTools.IO.Compression.Deflate
}
/// <summary>
/// Initialize the ZlibCodec for inflation, with the specified number of window bits.
/// 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,
/// <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;
this.WindowBits = windowBits;
return InitializeInflate(windowBits, true);
}
/// <summary>
/// Initialize the inflation state with an explicit flag to govern the handling of
/// RFC1950 header bytes.
/// RFC1950 header bytes.
/// </summary>
///
/// <remarks>
@@ -277,9 +276,9 @@ namespace SabreTools.IO.Compression.Deflate
/// false.
/// </remarks>
///
/// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte pair when reading
/// <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,
/// <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)
@@ -294,7 +293,7 @@ namespace SabreTools.IO.Compression.Deflate
/// 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
/// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
/// AvailableBytesOut before calling this method.
/// </remarks>
/// <example>
@@ -304,48 +303,48 @@ namespace SabreTools.IO.Compression.Deflate
/// 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
///
/// // pass 1: inflate
/// do
/// {
/// decompressor.NextOut = 0;
/// decompressor.AvailableBytesOut = buffer.Length;
/// rc = decompressor.Inflate(FlushType.None);
///
///
/// if (rc != ZlibConstants.Z_OK &amp;&amp; rc != ZlibConstants.Z_STREAM_END)
/// throw new Exception("inflating: " + decompressor.Message);
///
///
/// ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
/// }
/// while (decompressor.AvailableBytesIn &gt; 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 &amp;&amp; rc != ZlibConstants.Z_OK)
/// throw new Exception("inflating: " + decompressor.Message);
///
///
/// if (buffer.Length - decompressor.AvailableBytesOut &gt; 0)
/// ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
/// }
/// while (decompressor.AvailableBytesIn &gt; 0 || decompressor.AvailableBytesOut == 0);
///
///
/// decompressor.EndInflate();
/// }
///
@@ -362,10 +361,10 @@ namespace SabreTools.IO.Compression.Deflate
/// <summary>
/// Ends an inflation session.
/// Ends an inflation session.
/// </summary>
/// <remarks>
/// Call this after successively calling Inflate(). This will cause all buffers to be flushed.
/// 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>
@@ -401,32 +400,32 @@ namespace SabreTools.IO.Compression.Deflate
/// 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 &amp;&amp; compressor.TotalBytesOut &lt; 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>
@@ -452,7 +451,7 @@ namespace SabreTools.IO.Compression.Deflate
/// <summary>
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
/// 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>
@@ -460,7 +459,7 @@ namespace SabreTools.IO.Compression.Deflate
/// 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.
/// 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>
@@ -473,8 +472,8 @@ namespace SabreTools.IO.Compression.Deflate
/// <summary>
/// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
/// and the specified number of window bits.
/// 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.
@@ -509,8 +508,10 @@ namespace SabreTools.IO.Compression.Deflate
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;
dstate = new DeflateManager
{
WantRfc1950HeaderBytes = wantRfc1950Header
};
return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy);
}
@@ -528,59 +529,59 @@ namespace SabreTools.IO.Compression.Deflate
/// 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
///
/// // pass 1: deflate
/// do
/// {
/// compressor.NextOut = 0;
/// compressor.AvailableBytesOut = buffer.Length;
/// rc = compressor.Deflate(FlushType.None);
///
///
/// if (rc != ZlibConstants.Z_OK &amp;&amp; rc != ZlibConstants.Z_STREAM_END)
/// throw new Exception("deflating: " + compressor.Message);
///
///
/// ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
/// }
/// while (compressor.AvailableBytesIn &gt; 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 &amp;&amp; rc != ZlibConstants.Z_OK)
/// throw new Exception("deflating: " + compressor.Message);
///
///
/// if (buffer.Length - compressor.AvailableBytesOut &gt; 0)
/// ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
/// }
/// while (compressor.AvailableBytesIn &gt; 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 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)
@@ -717,4 +718,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}

View File

@@ -1,20 +1,20 @@
// ZlibConstants.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
// 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.
// 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):
// last saved (in emacs):
// Time-stamp: <2009-November-03 18:50:19>
//
// ------------------------------------------------------------------
@@ -25,22 +25,22 @@
// 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
//
// 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,
@@ -51,7 +51,7 @@
// 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
@@ -91,7 +91,7 @@ namespace SabreTools.IO.Compression.Deflate
public const int Z_STREAM_END = 1;
/// <summary>
/// The operation ended in need of a dictionary.
/// The operation ended in need of a dictionary.
/// </summary>
public const int Z_NEED_DICT = 2;

View File

@@ -117,4 +117,4 @@ namespace SabreTools.IO.Compression.Deflate
}
}
}
}

View File

@@ -545,7 +545,7 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>the number of bytes read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (_disposed) throw new ObjectDisposedException("ZlibStream");
if (_disposed) throw new ObjectDisposedException("ZlibStream");
return _baseStream.Read(buffer, offset, count);
}
@@ -607,7 +607,7 @@ namespace SabreTools.IO.Compression.Deflate
/// <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");
if (_disposed) throw new ObjectDisposedException("ZlibStream");
_baseStream.Write(buffer, offset, count);
}
#endregion
@@ -633,13 +633,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <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();
}
using var ms = new MemoryStream();
Stream compressor =
new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
ZlibBaseStream.CompressString(s, compressor);
return ms.ToArray();
}
@@ -661,14 +659,12 @@ namespace SabreTools.IO.Compression.Deflate
/// <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 );
using var ms = new MemoryStream();
Stream compressor =
new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression);
ZlibBaseStream.CompressBuffer(b, compressor);
return ms.ToArray();
}
ZlibBaseStream.CompressBuffer(b, compressor);
return ms.ToArray();
}
@@ -686,13 +682,11 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>The uncompressed string</returns>
public static String UncompressString(byte[] compressed)
{
using (var input = new MemoryStream(compressed))
{
Stream decompressor =
new ZlibStream(input, CompressionMode.Decompress);
using var input = new MemoryStream(compressed);
Stream decompressor =
new ZlibStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
return ZlibBaseStream.UncompressString(compressed, decompressor);
}
@@ -710,16 +704,14 @@ namespace SabreTools.IO.Compression.Deflate
/// <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 );
using var input = new MemoryStream(compressed);
Stream decompressor =
new ZlibStream(input, CompressionMode.Decompress);
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
return ZlibBaseStream.UncompressBuffer(compressed, decompressor);
}
}
}
}

View File

@@ -55,4 +55,4 @@ namespace SabreTools.IO.Compression.LZX
/// <remarks>Variable</remarks>
public byte[]? TokenSequence { get; set; }
}
}
}

View File

@@ -21,4 +21,4 @@ namespace SabreTools.IO.Compression.LZX
/// </summary>
public BlockData? BlockData { get; set; }
}
}
}

View File

@@ -5,4 +5,4 @@ namespace SabreTools.IO.Compression.LZX
{
// No common fields between all block data
}
}
}

View File

@@ -30,4 +30,4 @@ namespace SabreTools.IO.Compression.LZX
/// <remarks>8 bits</remarks>
public byte BlocksizeLSB { get; set; }
}
}
}

View File

@@ -22,4 +22,4 @@ namespace SabreTools.IO.Compression.LZX
/// </summary>
public Block[]? Blocks { get; set; }
}
}
}

View File

@@ -43,4 +43,4 @@ namespace SabreTools.IO.Compression.LZX
/// </summary>
public ushort? TranslationSizeLowWord { get; set; }
}
}
}

View File

@@ -35,4 +35,4 @@ namespace SabreTools.IO.Compression.LZX
public const int LZX_LENTABLE_SAFETY = 64; /* we allow length table decoding overruns */
}
}
}

View File

@@ -6,10 +6,10 @@ namespace SabreTools.IO.Compression.LZX
/// begins. Following the zero padding, new 32-bit values for R0, R1, and R2 are output in little-endian
/// form, followed by the uncompressed data bytes themselves. Finally, if the uncompressed data length
/// is odd, one extra byte of zero padding is encoded to realign the following bitstream.
///
///
/// Then the bitstream of byte-swapped 16-bit integers resumes for the next Block Type field (if there
/// are subsequent blocks).
///
///
/// The decoded R0, R1, and R2 values are used as initial repeated offset values to decode the
/// subsequent compressed block if present.
/// </summary>
@@ -51,4 +51,4 @@ namespace SabreTools.IO.Compression.LZX
/// </summary>
public byte AlignmentByte { get; set; }
}
}
}

View File

@@ -48,4 +48,4 @@ namespace SabreTools.IO.Compression.LZX
/// <remarks>Variable</remarks>
public byte[]? TokenSequence { get; set; }
}
}
}

View File

@@ -4,7 +4,7 @@ namespace SabreTools.IO.Compression.MSZIP
/// Each MSZIP block MUST consist of a 2-byte MSZIP signature and one or more RFC 1951 blocks. The
/// 2-byte MSZIP signature MUST consist of the bytes 0x43 and 0x4B. The MSZIP signature MUST be
/// the first 2 bytes in the MSZIP block. The MSZIP signature is shown in the following packet diagram.
///
///
/// Each MSZIP block is the result of a single deflate compression operation, as defined in [RFC1951].
/// The compressor that performs the compression operation MUST generate one or more RFC 1951
/// blocks, as defined in [RFC1951]. The number, deflation mode, and type of RFC 1951 blocks in each
@@ -12,7 +12,7 @@ namespace SabreTools.IO.Compression.MSZIP
/// each MSZIP block MUST be marked as the "end" of the stream(1), as defined by [RFC1951]
/// section 3.2.3. Decoding trees MUST be discarded after each RFC 1951 block, but the history buffer
/// MUST be maintained.Each MSZIP block MUST represent no more than 32 KB of uncompressed data.
///
///
/// The maximum compressed size of each MSZIP block is 32 KB + 12 bytes. This enables the MSZIP
/// block to contain 32 KB of data split between two noncompressed RFC 1951 blocks, each of which
/// has a value of BTYPE = 00.
@@ -25,4 +25,4 @@ namespace SabreTools.IO.Compression.MSZIP
/// </summary>
public ushort Signature { get; set; }
}
}
}

View File

@@ -47,4 +47,4 @@ namespace SabreTools.IO.Compression.Quantum
20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42
];
}
}
}

View File

@@ -6,7 +6,7 @@ using static SabreTools.IO.Compression.Quantum.Constants;
namespace SabreTools.IO.Compression.Quantum
{
/// <see href="www.russotto.net/quantumcomp.html"/>
/// <see href="www.russotto.net/quantumcomp.html"/>
public class Decompressor
{
/// <summary>
@@ -362,9 +362,13 @@ namespace SabreTools.IO.Compression.Quantum
{
if (model.Symbols![i]!.CumulativeFrequency < model.Symbols![j]!.CumulativeFrequency)
{
#if NETCOREAPP || NETSTANDARD2_1_OR_GREATER
(model.Symbols[j], model.Symbols[i]) = (model.Symbols[i], model.Symbols[j]);
#else
var temp = model.Symbols[i];
model.Symbols[i] = model.Symbols[j];
model.Symbols[j] = temp;
#endif
}
}
}

View File

@@ -42,4 +42,4 @@ namespace SabreTools.IO.Compression.Quantum
/// </summary>
SELECTOR_6_LENGTH = 7,
}
}
}

View File

@@ -21,4 +21,4 @@ namespace SabreTools.IO.Compression.Quantum
/// <remarks>The initial time_to_reorder value is 4</remarks>
public int TimeToReorder { get; set; }
}
}
}

View File

@@ -12,4 +12,4 @@ namespace SabreTools.IO.Compression.Quantum
/// </summary>
public ushort CumulativeFrequency { get; set; }
}
}
}

View File

@@ -96,7 +96,7 @@ namespace SabreTools.IO.Compression.SZDD
/// </summary>
public static Decompressor CreateSZDD(Stream source)
{
// Create the decompressor
// Create the decompressors
var decompressor = new Decompressor(source);
// Set the format and return

View File

@@ -215,4 +215,4 @@ namespace SabreTools.IO.Compression.zlib
return result;
}
}
}
}

View File

@@ -3,26 +3,26 @@ using System.Threading;
namespace SabreTools.IO.Compression.zlib
{
public unsafe static class MemoryStats
{
private static int _allocations;
public static int Allocations
{
get
{
return _allocations;
}
}
public unsafe static class MemoryStats
{
private static int _allocations;
internal static void Allocated()
{
Interlocked.Increment(ref _allocations);
}
public static int Allocations
{
get
{
return _allocations;
}
}
internal static void Freed()
{
Interlocked.Decrement(ref _allocations);
}
}
}
internal static void Allocated()
{
Interlocked.Increment(ref _allocations);
}
internal static void Freed()
{
Interlocked.Decrement(ref _allocations);
}
}
}

View File

@@ -3,86 +3,86 @@ using System.Runtime.InteropServices;
namespace SabreTools.IO.Compression.zlib
{
public unsafe class UnsafeArray1D<T> where T : struct
{
private readonly T[] _data;
private readonly GCHandle _pinHandle;
public bool IsFreed { get; private set; }
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;
internal GCHandle PinHandle => _pinHandle;
public T this[int index]
{
get => _data[index];
set
{
_data[index] = value;
}
}
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 this[uint index]
{
get => _data[index];
set
{
_data[index] = value;
}
}
public T[] Data => _data;
public T[] Data => _data;
public UnsafeArray1D(int size)
{
if (size < 0)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
public UnsafeArray1D(int size)
{
if (size < 0)
{
throw new ArgumentOutOfRangeException(nameof(size));
}
_data = new T[size];
_pinHandle = GCHandle.Alloc(_data, GCHandleType.Pinned);
IsFreed = false;
}
_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));
}
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;
}
_data = data ?? throw new ArgumentNullException(nameof(data));
_pinHandle = GCHandle.Alloc(_data, GCHandleType.Pinned);
IsFreed = false;
}
public void Free()
{
if (!IsFreed)
{
_pinHandle.Free();
IsFreed = true;
}
}
public void Free()
{
if (!IsFreed)
{
_pinHandle.Free();
IsFreed = true;
}
}
~UnsafeArray1D()
{
if (!IsFreed)
_pinHandle.Free();
}
~UnsafeArray1D()
{
if (!IsFreed)
_pinHandle.Free();
}
public void* ToPointer()
{
return _pinHandle.AddrOfPinnedObject().ToPointer();
}
public void* ToPointer()
{
return _pinHandle.AddrOfPinnedObject().ToPointer();
}
public static implicit operator void*(UnsafeArray1D<T> array)
{
return array.ToPointer();
}
public static implicit operator void*(UnsafeArray1D<T> array)
{
return array.ToPointer();
}
public static void* operator +(UnsafeArray1D<T> array, int delta)
{
return array.ToPointer();
}
}
}
public static void* operator +(UnsafeArray1D<T> array, int delta)
{
return array.ToPointer();
}
}
}

View File

@@ -2,44 +2,44 @@
namespace SabreTools.IO.Compression.zlib
{
public unsafe class UnsafeArray2D<T> where T : struct
{
private readonly UnsafeArray1D<T>[] _data;
private long[] _pinAddresses;
private readonly GCHandle _pinAddressesHandle;
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 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();
}
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);
}
_pinAddressesHandle = GCHandle.Alloc(_pinAddresses, GCHandleType.Pinned);
}
~UnsafeArray2D()
{
_pinAddressesHandle.Free();
}
~UnsafeArray2D()
{
_pinAddressesHandle.Free();
}
public void* ToPointer() => _pinAddressesHandle.AddrOfPinnedObject().ToPointer();
public void* ToPointer() => _pinAddressesHandle.AddrOfPinnedObject().ToPointer();
public static implicit operator void*(UnsafeArray2D<T> array)
{
return array.ToPointer();
}
}
}
public static implicit operator void*(UnsafeArray2D<T> array)
{
return array.ToPointer();
}
}
}

View File

@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace SabreTools.IO.Compression.zlib
{

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace SabreTools.IO.Compression.zlib
{

View File

@@ -3070,7 +3070,7 @@ namespace SabreTools.IO.Compression.zlib
public static int deflateSetDictionary(z_stream_s strm, byte* dictionary, uint dictLength)
{
internal_state? s;
uint str = 0; uint n = 0 ;
uint str = 0; uint n = 0 ;
int wrap = 0;
uint avail = 0;
byte* next;
@@ -3270,7 +3270,7 @@ namespace SabreTools.IO.Compression.zlib
public static uint deflateBound(z_stream_s strm, uint sourceLen)
{
internal_state? s;
uint complen = 0; uint wraplen = 0 ;
uint complen = 0; uint wraplen = 0 ;
complen = (uint)(sourceLen + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5);
if ((deflateStateCheck(strm)) != 0)
return (uint)(complen + 6);
@@ -4452,7 +4452,7 @@ namespace SabreTools.IO.Compression.zlib
public static uint multmodp(uint a, uint b)
{
uint m = 0; uint p = 0 ;
uint m = 0; uint p = 0 ;
m = (uint)((uint)(1) << 31);
p = (uint)(0);
for (;;)
@@ -4559,7 +4559,7 @@ namespace SabreTools.IO.Compression.zlib
public static void _tr_flush_block(internal_state s, sbyte* buf, uint stored_len, int last)
{
uint opt_lenb = 0; uint static_lenb = 0 ;
uint opt_lenb = 0; uint static_lenb = 0 ;
int max_blindex = (int)(0);
if ((s.level) > (0))
{
@@ -4819,7 +4819,7 @@ namespace SabreTools.IO.Compression.zlib
int _base_ = (int)(desc.stat_desc.extra_base);
int max_length = (int)(desc.stat_desc.max_length);
int h = 0;
int n = 0; int m = 0 ;
int n = 0; int m = 0 ;
int bits = 0;
int xbits = 0;
ushort f = 0;
@@ -4836,7 +4836,7 @@ namespace SabreTools.IO.Compression.zlib
bits = (int)(tree[tree[n].dl.dad].dl.len + 1);
if ((bits) > (max_length))
{
bits = (int)(max_length); //Nanook ,
bits = (int)(max_length); //Nanook ,
overflow++;
}
tree[n].dl.len = ((ushort)(bits));
@@ -4913,11 +4913,11 @@ namespace SabreTools.IO.Compression.zlib
ct_data_s[] tree = desc.dyn_tree!;
ct_data_s[] stree = desc.stat_desc.static_tree;
int elems = (int)(desc.stat_desc.elems);
int n = 0; int m = 0 ;
int n = 0; int m = 0 ;
int max_code = (int)(-1);
int node = 0;
s.heap_len = (int)(0); //Nanook ,
s.heap_max = (int)(2 * (256 + 1 + 29) + 1);
s.heap_max = (int)(2 * (256 + 1 + 29) + 1);
for (n = (int)(0); (n) < (elems); n++)
{
if (tree[n].fc.freq != 0)
@@ -4984,7 +4984,7 @@ namespace SabreTools.IO.Compression.zlib
if ((nextlen) == (0))
{
max_count = (int)(138); //Nanook ,
min_count = ( int ) ( 3 ) ;
min_count = ( int ) ( 3 ) ;
}
tree[max_code + 1].dl.len = ((ushort)(0xffff));
for (n = (int)(0); (n) <= (max_code); n++)
@@ -5018,17 +5018,17 @@ namespace SabreTools.IO.Compression.zlib
prevlen = (int)(curlen);
if ((nextlen) == (0))
{
max_count = (int)(138); //Nanook ,
max_count = (int)(138); //Nanook ,
min_count = (int)(3);
}
else if ((curlen) == (nextlen))
{
max_count = (int)(6); //Nanook ,
max_count = (int)(6); //Nanook ,
min_count = (int)(3);
}
else
{
max_count = (int)(7); //Nanook ,
max_count = (int)(7); //Nanook ,
min_count = (int)(4);
}
}
@@ -5045,7 +5045,7 @@ namespace SabreTools.IO.Compression.zlib
int min_count = (int)(4);
if ((nextlen) == (0))
{
max_count = (int)(138); //Nanook ,
max_count = (int)(138); //Nanook ,
min_count = (int)(3);
}
for (n = (int)(0); (n) <= (max_code); n++)
@@ -5286,17 +5286,17 @@ namespace SabreTools.IO.Compression.zlib
prevlen = (int)(curlen);
if ((nextlen) == (0))
{
max_count = (int)(138); //Nanook ,
max_count = (int)(138); //Nanook ,
min_count = (int)(3);
}
else if ((curlen) == (nextlen))
{
max_count = (int)(6); //Nanook ,
max_count = (int)(6); //Nanook ,
min_count = (int)(3);
}
else
{
max_count = (int)(7); //Nanook ,
max_count = (int)(7); //Nanook ,
min_count = (int)(4);
}
}
@@ -5648,7 +5648,7 @@ namespace SabreTools.IO.Compression.zlib
do
{
res |= (uint)(code & 1);
code >>= 1; //Nanook ,
code >>= 1; //Nanook ,
res <<= 1;
}
while ((--len) > (0));
@@ -5721,7 +5721,7 @@ namespace SabreTools.IO.Compression.zlib
public static void slide_hash(internal_state s)
{
uint n = 0; uint m = 0 ;
uint n = 0; uint m = 0 ;
ushort* p;
uint wsize = (uint)(s.w_size);
n = (uint)(s.hash_size);
@@ -5823,7 +5823,7 @@ namespace SabreTools.IO.Compression.zlib
public static block_state deflate_stored(internal_state s, int flush)
{
uint min_block = (uint)((s.pending_buf_size - 5) > (s.w_size) ? (s.w_size) : (s.pending_buf_size - 5));
uint len = 0; uint left = 0 ; uint have = 0 ; uint last = ( uint ) ( 0 ) ;
uint len = 0; uint left = 0 ; uint have = 0 ; uint last = ( uint ) ( 0 ) ;
uint used = (uint)(s.strm!.avail_in);
do
{
@@ -6096,7 +6096,7 @@ namespace SabreTools.IO.Compression.zlib
s.head[s.ins_h] = ((ushort)(s.strstart));
}
s.prev_length = (uint)(s.match_length); //Nanook ,
s.prev_length = (uint)(s.match_length); //Nanook ,
s.prev_match = (uint)(s.match_start);
s.match_length = (uint)(3 - 1);
if (((hash_head != 0) && ((s.prev_length) < (s.max_lazy_match))) && ((s.strstart - hash_head) <= ((s).w_size - (258 + 3 + 1))))
@@ -6233,7 +6233,7 @@ namespace SabreTools.IO.Compression.zlib
{
int bflush = 0;
uint prev = 0;
byte* scan; byte * strend ;
byte* scan; byte * strend ;
for (;;)
{
if ((s.lookahead) <= (258))
@@ -6521,7 +6521,7 @@ namespace SabreTools.IO.Compression.zlib
match = s.window + cur_match;
if ((((match[best_len] != scan_end) || (match[best_len - 1] != scan_end1)) || (*match != *scan)) || (*++match != scan[1]))
continue;
scan += 2; //Nanook ,
scan += 2; //Nanook ,
match++;
do
{

View File

@@ -44,4 +44,4 @@ namespace SabreTools.IO.Compression.zlib
};
}
}
}
}

View File

@@ -122,4 +122,4 @@ namespace SabreTools.IO.Encryption
progress?.Invoke($"{blockCount + 1} / {blockCount + 1} MB... Done!\r\n");
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using SabreTools.Hashing;
using SabreTools.IO.Extensions;
@@ -12,6 +13,86 @@ namespace SabreTools.IO.Encryption
{
#region Constants
/// <summary>
/// Converts ASCII characters to lowercase
/// </summary>
/// <remarks>Converts slash (0x2F) to backslash (0x5C)</remarks>
private static readonly byte[] AsciiToLowerTable =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x5C,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
];
/// <summary>
/// Converts ASCII characters to uppercase
/// </summary>
/// <remarks>Converts slash (0x2F) to backslash (0x5C)</remarks>
private static readonly byte[] AsciiToUpperTable =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x5C,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
];
/// <summary>
/// Converts ASCII characters to uppercase
/// </summary>
/// <remarks>Does NOT convert slash (0x2F) to backslash (0x5C)</remarks>
private static readonly byte[] AsciiToUpperTable_Slash =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
];
private const uint MPQ_HASH_TABLE_INDEX = 0x000;
private const uint MPQ_HASH_NAME_A = 0x100;
private const uint MPQ_HASH_NAME_B = 0x200;
private const uint MPQ_HASH_FILE_KEY = 0x300;
private const uint MPQ_HASH_KEY2_MIX = 0x400;
private const uint STORM_BUFFER_SIZE = 0x500;
@@ -175,5 +256,108 @@ namespace SabreTools.IO.Encryption
Buffer.BlockCopy(castBlock, 0, block, 0, block.Length >> 2);
return block;
}
#region Hashing
//
// Note: Implementation of this function in WorldEdit.exe and storm.dll
// incorrectly treats the character as signed, which leads to the
// a buffer underflow if the character in the file name >= 0x80:
// The following steps happen when *pbKey == 0xBF and hashType == 0x0000
// (calculating hash index)
//
// 1) Result of AsciiToUpperTable_Slash[*pbKey++] is sign-extended to 0xffffffbf
// 2) The "ch" is added to hashType (0xffffffbf + 0x0000 => 0xffffffbf)
// 3) The result is used as index to the StormBuffer table,
// thus dereferences a random value BEFORE the begin of StormBuffer.
//
// As result, MPQs containing files with non-ANSI characters will not work between
// various game versions and localizations. Even WorldEdit, after importing a file
// with Korean characters in the name, cannot open the file back.
//
/// <summary>
/// Hash a string representing a filename based on the hash type
/// using upper-case normalization
/// </summary>
/// <param name="filename">Filename to hash</param>
/// <param name="hashType">Hash type to perform</param>
/// <returns>Value representing the hashed filename</returns>
public uint HashString(string filename, uint hashType)
{
uint seed1 = 0x7FED7FED;
uint seed2 = 0xEEEEEEEE;
byte[] key = Encoding.ASCII.GetBytes(filename);
int keyPtr = 0;
while (key[keyPtr] != 0)
{
// Convert the input character to uppercase
// Convert slash (0x2F) to backslash (0x5C)
byte ch = AsciiToUpperTable[key[keyPtr++]];
seed1 = _stormBuffer[hashType + ch] ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
}
return seed1;
}
/// <summary>
/// Hash a string representing a filename based on the hash type
/// using upper-case normalization
/// </summary>
/// <param name="filename">Filename to hash</param>
/// <param name="hashType">Hash type to perform</param>
/// <returns>Value representing the hashed filename</returns>
/// <remarks>This preserves slashes when hashing</remarks>
public uint HashStringSlash(string filename, uint hashType)
{
uint seed1 = 0x7FED7FED;
uint seed2 = 0xEEEEEEEE;
byte[] key = Encoding.ASCII.GetBytes(filename);
int keyPtr = 0;
while (key[keyPtr] != 0)
{
// Convert the input character to uppercase
// DON'T convert slash (0x2F) to backslash (0x5C)
byte ch = AsciiToUpperTable_Slash[key[keyPtr++]];
seed1 = _stormBuffer[hashType + ch] ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
}
return seed1;
}
/// <summary>
/// Hash a string representing a filename based on the hash type
/// using lower-case normalization
/// </summary>
/// <param name="filename">Filename to hash</param>
/// <param name="hashType">Hash type to perform</param>
/// <returns>Value representing the hashed filename</returns>
public uint HashStringLower(string filename, uint hashType)
{
uint seed1 = 0x7FED7FED;
uint seed2 = 0xEEEEEEEE;
byte[] key = Encoding.ASCII.GetBytes(filename);
int keyPtr = 0;
while (key[keyPtr] != 0)
{
// Convert the input character to lower
// DON'T convert slash (0x2F) to backslash (0x5C)
byte ch = AsciiToLowerTable[key[keyPtr++]];
seed1 = _stormBuffer[hashType + ch] ^ (seed1 + seed2);
seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
}
return seed1;
}
#endregion
}
}

View File

@@ -3,7 +3,7 @@
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
internal sealed class ExtensionAttribute : Attribute {}
internal sealed class ExtensionAttribute : Attribute { }
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,7 @@ using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using SabreTools.Numerics;
namespace SabreTools.IO.Extensions
{
@@ -15,6 +16,24 @@ namespace SabreTools.IO.Extensions
/// TODO: Handle proper negative values for Int24 and Int48
public static class BinaryWriterExtensions
{
/// <inheritdoc cref="BinaryWriter.Write(byte)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothUInt8 value)
{
writer.Write(value.LittleEndian);
writer.Write(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(sbyte)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothInt8 value)
{
writer.Write(value.LittleEndian);
writer.Write(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(byte[])"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, byte[] value)
@@ -39,6 +58,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(short)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothInt16 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(ushort)"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, ushort value)
@@ -48,6 +76,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(ushort)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothUInt16 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <inheritdoc cref="BinaryWriter.Write(Half)"/>
@@ -125,6 +162,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(int)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothInt32 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(uint)"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, uint value)
@@ -134,6 +180,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(uint)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothUInt32 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(float)"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, float value)
@@ -208,6 +263,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(long)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothInt64 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(ulong)"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, ulong value)
@@ -217,6 +281,15 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <inheritdoc cref="BinaryWriter.Write(ulong)"/>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this BinaryWriter writer, BothUInt64 value)
{
writer.Write(value.LittleEndian);
writer.WriteBigEndian(value.BigEndian);
return true;
}
/// <inheritdoc cref="BinaryWriter.Write(double)"/>
/// <remarks>Writes in big-endian format</remarks>
public static bool WriteBigEndian(this BinaryWriter writer, double value)
@@ -342,6 +415,14 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedAnsiString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.ASCII);
#if NET5_0_OR_GREATER
/// <summary>
/// Write a null-terminated Latin1 string to the underlying stream
/// </summary>
public static bool WriteNullTerminatedLatin1String(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.Latin1);
#endif
/// <summary>
/// Write a null-terminated UTF-8 string to the underlying stream
/// </summary>
@@ -354,6 +435,12 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedUnicodeString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.Unicode);
/// <summary>
/// Write a null-terminated UTF-16 (Unicode) string to the underlying stream
/// </summary>
public static bool WriteNullTerminatedBigEndianUnicodeString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.BigEndianUnicode);
/// <summary>
/// Write a null-terminated UTF-32 string to the underlying stream
/// </summary>
@@ -379,6 +466,27 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write a byte-prefixed Latin1 string to the underlying stream
/// </summary>
public static bool WritePrefixedLatin1String(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
writer.Write((byte)value.Length);
// Write the buffer
return WriteFromBuffer(writer, buffer);
}
#endif
/// <summary>
/// Write a ushort-prefixed Unicode string to the underlying stream
/// </summary>
@@ -398,6 +506,25 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <summary>
/// Write a ushort-prefixed Unicode string to the underlying stream
/// </summary>
public static bool WritePrefixedBigEndianUnicodeString(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
writer.Write((ushort)value.Length);
// Write the buffer
return WriteFromBuffer(writer, buffer);
}
/// <summary>
/// Write a <typeparamref name="T"/> to the underlying stream
/// </summary>
@@ -437,7 +564,7 @@ namespace SabreTools.IO.Extensions
{
writer.Write((Half)value);
return true;
}
}
#endif
#if NET7_0_OR_GREATER
else if (type == typeof(Int128))
@@ -556,8 +683,7 @@ namespace SabreTools.IO.Extensions
return false;
// Get the array
Array? arr = fi.GetValue(instance) as Array;
if (arr == null)
if (fi.GetValue(instance) is not Array arr)
return false;
// Get the number of elements expected
@@ -585,8 +711,7 @@ namespace SabreTools.IO.Extensions
private static bool WriteStringType(BinaryWriter writer, Encoding encoding, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
string? fieldValue = fi.GetValue(instance) as string;
if (fieldValue == null)
if (fi.GetValue(instance) is not string fieldValue)
return true;
switch (marshalAsAttr?.Value)

View File

@@ -16,6 +16,19 @@ namespace SabreTools.IO.Extensions
return array == null || array.Length == 0;
}
/// <summary>
/// Indicates if an array contains all ASCII numeric digits
/// </summary>
public static bool IsNumericArray(this byte[] arr)
{
// Empty arrays cannot be numeric
if (arr.Length == 0)
return false;
// '0' to '9'
return Array.TrueForAll(arr, b => b >= 0x30 && b <= 0x39);
}
#region Matching
/// <summary>

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@ using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using SabreTools.Numerics;
namespace SabreTools.IO.Extensions
{
@@ -20,6 +21,17 @@ namespace SabreTools.IO.Extensions
public static bool Write(this byte[] content, ref int offset, byte value)
=> WriteFromBuffer(content, ref offset, [value]);
/// <summary>
/// Write a UInt8 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt8 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.Write(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt8[] and increment the pointer to an array
/// </summary>
@@ -42,6 +54,17 @@ namespace SabreTools.IO.Extensions
public static bool Write(this byte[] content, ref int offset, sbyte value)
=> WriteFromBuffer(content, ref offset, [(byte)value]);
/// <summary>
/// Write a Int8 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt8 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.Write(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Char and increment the pointer to an array
/// </summary>
@@ -80,6 +103,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt16 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
@@ -100,6 +134,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt16 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt16 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
// Half was introduced in net5.0 but doesn't have a BitConverter implementation until net6.0
#if NET6_0_OR_GREATER
/// <summary>
@@ -199,6 +244,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt32 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
@@ -219,6 +275,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt32 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt32 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Single and increment the pointer to an array
/// </summary>
@@ -315,6 +382,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a Int64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothInt64 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
@@ -335,6 +413,17 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a UInt64 and increment the pointer to an array
/// </summary>
/// <remarks>Writes in both-endian format</remarks>
public static bool WriteBothEndian(this byte[] content, ref int offset, BothUInt64 value)
{
bool actual = content.Write(ref offset, value.LittleEndian);
actual &= content.WriteBigEndian(ref offset, value.BigEndian);
return actual;
}
/// <summary>
/// Write a Double and increment the pointer to an array
/// </summary>
@@ -426,7 +515,7 @@ namespace SabreTools.IO.Extensions
public static bool Write(this byte[] content, ref int offset, Int128 value)
{
byte[] buffer = ((BigInteger)value).ToByteArray();
byte[] padded = new byte[16];
Array.Copy(buffer, 0, padded, 16 - buffer.Length, buffer.Length);
return WriteFromBuffer(content, ref offset, padded);
@@ -440,7 +529,7 @@ namespace SabreTools.IO.Extensions
{
byte[] buffer = ((BigInteger)value).ToByteArray();
Array.Reverse(buffer);
byte[] padded = new byte[16];
Array.Copy(buffer, 0, padded, 16 - buffer.Length, buffer.Length);
return WriteFromBuffer(content, ref offset, padded);
@@ -452,7 +541,7 @@ namespace SabreTools.IO.Extensions
public static bool Write(this byte[] content, ref int offset, UInt128 value)
{
byte[] buffer = ((BigInteger)value).ToByteArray();
byte[] padded = new byte[16];
Array.Copy(buffer, 0, padded, 16 - buffer.Length, buffer.Length);
return WriteFromBuffer(content, ref offset, padded);
@@ -466,7 +555,7 @@ namespace SabreTools.IO.Extensions
{
byte[] buffer = ((BigInteger)value).ToByteArray();
Array.Reverse(buffer);
byte[] padded = new byte[16];
Array.Copy(buffer, 0, padded, 16 - buffer.Length, buffer.Length);
return WriteFromBuffer(content, ref offset, padded);
@@ -494,6 +583,14 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedAnsiString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.ASCII);
#if NET5_0_OR_GREATER
/// <summary>
/// Write a null-terminated Latin1 string to the byte array
/// </summary>
public static bool WriteNullTerminatedLatin1String(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.Latin1);
#endif
/// <summary>
/// Write a null-terminated UTF-8 string to the byte array
/// </summary>
@@ -506,6 +603,12 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedUnicodeString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.Unicode);
/// <summary>
/// Write a null-terminated UTF-16 (Unicode) string to the byte array
/// </summary>
public static bool WriteNullTerminatedBigEndianUnicodeString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.BigEndianUnicode);
/// <summary>
/// Write a null-terminated UTF-32 string to the byte array
/// </summary>
@@ -532,6 +635,28 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write a byte-prefixed Latin1 string to the byte array
/// </summary>
public static bool WritePrefixedLatin1String(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
if (!content.Write(ref offset, (byte)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(content, ref offset, buffer);
}
#endif
/// <summary>
/// Write a ushort-prefixed Unicode string to the byte array
/// </summary>
@@ -552,6 +677,26 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a ushort-prefixed Unicode string to the byte array
/// </summary>
public static bool WritePrefixedBigEndianUnicodeString(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
if (!content.Write(ref offset, (ushort)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a <typeparamref name="T"/> to the byte array
/// </summary>
@@ -707,8 +852,7 @@ namespace SabreTools.IO.Extensions
return false;
// Get the array
Array? arr = fi.GetValue(instance) as Array;
if (arr == null)
if (fi.GetValue(instance) is not Array arr)
return false;
// Get the number of elements expected
@@ -736,8 +880,7 @@ namespace SabreTools.IO.Extensions
private static bool WriteStringType(byte[] content, ref int offset, Encoding encoding, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
string? fieldValue = fi.GetValue(instance) as string;
if (fieldValue == null)
if (fi.GetValue(instance) is not string fieldValue)
return true;
switch (marshalAsAttr?.Value)

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
namespace SabreTools.IO.Extensions
{
public static class DictionaryExtensions
{
/// <summary>
/// Merge a dictionary into an existing one, if possible
/// </summary>
/// <param name="dict">Source dictionary to add to</param>
/// <param name="other">Second dictionary to add from</param>
/// <remarks>This only performs a shallow copy</remarks>
public static void MergeWith(this Dictionary<string, List<string>> dict, Dictionary<string, List<string>> other)
{
// Ignore if there are no values to append
if (other.Count == 0)
return;
// Loop through and add from the new dictionary
foreach (var kvp in other)
{
// Ignore empty values
if (kvp.Value.Count == 0)
continue;
if (!dict.ContainsKey(kvp.Key))
dict[kvp.Key] = [];
dict[kvp.Key].AddRange(kvp.Value);
}
}
}
}

View File

@@ -14,37 +14,37 @@ namespace SabreTools.IO.Extensions
/// <summary>
/// Get an attribute of the requested type
/// </summary>
public static T? GetAttribute<T>(FieldInfo? fi) where T : Attribute
public static TAttribute? GetAttribute<TAttribute>(FieldInfo? fi) where TAttribute : Attribute
{
// If the field info is invalid
if (fi == null)
return null;
// Get all matching attributes
var attributes = fi.GetCustomAttributes(typeof(T), true);
var attributes = fi.GetCustomAttributes(typeof(TAttribute), true);
if (attributes == null || attributes.Length == 0)
return null;
// Get the first attribute that matches
return attributes[0] as T;
return attributes[0] as TAttribute;
}
/// <summary>
/// Get an attribute of the requested type
/// </summary>
public static T? GetAttribute<T>(Type? type) where T : Attribute
public static TAttribute? GetAttribute<TAttribute>(Type? type) where TAttribute : Attribute
{
// If the field info is invalid
if (type == null)
return null;
// Get all matching attributes
var attributes = type.GetCustomAttributes(typeof(T), true);
var attributes = type.GetCustomAttributes(typeof(TAttribute), true);
if (attributes == null || attributes.Length == 0)
return null;
// Get the first attribute that matches
return attributes[0] as T;
return attributes[0] as TAttribute;
}
/// <summary>

View File

@@ -100,6 +100,14 @@ namespace SabreTools.IO.Extensions
/// <param name="input">Input stream to try seeking on</param>
/// <param name="offset">Optional offset to seek to</param>
public static long SeekIfPossible(this Stream input, long offset = 0)
=> input.SeekIfPossible(offset, offset < 0 ? SeekOrigin.End : SeekOrigin.Begin);
/// <summary>
/// Seek to a specific point in the stream, if possible
/// </summary>
/// <param name="input">Input stream to try seeking on</param>
/// <param name="offset">Optional offset to seek to</param>
public static long SeekIfPossible(this Stream input, long offset, SeekOrigin origin)
{
// If the input is not seekable, just return the current position
if (!input.CanSeek)
@@ -116,10 +124,7 @@ namespace SabreTools.IO.Extensions
// Attempt to seek to the offset
try
{
if (offset < 0)
return input.Seek(offset, SeekOrigin.End);
else
return input.Seek(offset, SeekOrigin.Begin);
return input.Seek(offset, origin);
}
catch
{

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More