88 Commits
1.7.1 ... 1.9.0

Author SHA1 Message Date
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
Matt Nadareski
b12d122721 Bump version 2025-10-07 09:28:17 -04:00
Matt Nadareski
20f1679557 Update Hashing to 1.5.1 2025-10-07 09:23:21 -04:00
Matt Nadareski
7ccedbeac5 Move Compare to better namespace 2025-09-30 21:25:56 -04:00
Matt Nadareski
72910cc1c0 Add AES/CTR encryption helpers 2025-09-30 19:33:28 -04:00
Matt Nadareski
8f4ea0da16 Add BouncyCastle as a dependency 2025-09-30 19:30:23 -04:00
Matt Nadareski
eb4975b261 Fix namespace in readme 2025-09-30 18:29:30 -04:00
Matt Nadareski
995c19d903 Add byte array math operations from NDecrypt; fix issues and add tests 2025-09-30 18:02:05 -04:00
Matt Nadareski
f0fe9af467 Require exact versions for build 2025-09-30 11:06:54 -04:00
Matt Nadareski
d33b47d15a Revert "Start allowing larger numeric types for reads"
This reverts commit e4a0a08d13.
2025-09-25 12:24:06 -04:00
Matt Nadareski
e4a0a08d13 Start allowing larger numeric types for reads 2025-09-25 12:08:22 -04:00
Matt Nadareski
24a69166f0 Add more info about namespaces to the readme 2025-09-25 09:43:38 -04:00
Matt Nadareski
6c13cdcf31 Bump version 2025-09-24 08:07:49 -04:00
Matt Nadareski
4138c271e5 This should be internal to the Compare namespace 2025-09-23 11:06:40 -04:00
Matt Nadareski
f80d31597b Reintegrate Matching and reorganize as needed 2025-09-23 10:59:01 -04:00
Matt Nadareski
5054aeb077 Bump version 2025-09-22 17:48:18 -04:00
Matt Nadareski
d2e9b8d6e5 Fix byte array test 2025-09-22 17:44:41 -04:00
Matt Nadareski
2c29aee834 Remove Models from references 2025-09-22 11:03:40 -04:00
Matt Nadareski
576bafcb87 Create minimal model for InflateWrapper 2025-09-22 11:03:07 -04:00
Matt Nadareski
2b310ac528 SZDD no longer uses models 2025-09-22 10:55:14 -04:00
Matt Nadareski
4f6b6d7b59 Reduce Models use another notch 2025-09-22 10:52:26 -04:00
Matt Nadareski
17e55ee233 Move BufferedStream out of SZDD 2025-09-22 10:50:53 -04:00
Matt Nadareski
8b78906d1d Move MoPaQ encryption constants from Models 2025-09-22 10:37:27 -04:00
Matt Nadareski
cff2dcf4cc Move LZX models from Models 2025-09-22 10:35:04 -04:00
Matt Nadareski
a56942cb73 Move Quantum compression models from Models 2025-09-22 10:31:14 -04:00
Matt Nadareski
5ed661b77c Move MSZIP "model" from Models 2025-09-22 10:27:03 -04:00
Matt Nadareski
a0a0cd0386 Add more complete UTF-8 first-byte tests 2025-09-21 16:34:46 -04:00
Matt Nadareski
bcc0fca4ad Ensure 7-bit ASCII never reads above 0x7F 2025-09-21 16:27:03 -04:00
Matt Nadareski
843e821e5f Use extended check in slow path too 2025-09-21 16:07:20 -04:00
Matt Nadareski
630b01283e Latin1 instead of ASCII for .NET 5.0 and beyond 2025-09-21 15:12:16 -04:00
Matt Nadareski
22abb96013 Add remarks about what encodings are used 2025-09-21 15:02:33 -04:00
Matt Nadareski
314de12661 Fix tests, remove UTF-8 checks from irrelevant places 2025-09-21 14:03:45 -04:00
Matt Nadareski
a0b24031b5 Remove duplicate code from Stream implementation 2025-09-21 13:58:46 -04:00
Matt Nadareski
b4628485c3 Sync stream implementation with byte one 2025-09-21 13:58:13 -04:00
Matt Nadareski
4610ddc9b9 Don't read the string unless it's long enough 2025-09-21 13:53:16 -04:00
Matt Nadareski
e392ddc8d7 Fix code formatting 2025-09-21 13:52:05 -04:00
Matt Nadareski
1908d1b32e More generically support single-byte encodings 2025-09-21 13:50:08 -04:00
Matt Nadareski
9d73195f86 Big-endian unicode support because it's there 2025-09-21 13:42:05 -04:00
Matt Nadareski
335a486f17 Special handling of empty string builders 2025-09-21 13:41:06 -04:00
Matt Nadareski
d3e41ac187 Handle invalid offsets in byte array extensions 2025-09-21 11:43:07 -04:00
Matt Nadareski
8ddd9f3f78 Bump version 2025-09-20 22:16:06 -04:00
Matt Nadareski
54ad538c08 Short-circuit fixed-width encodings 2025-09-20 22:10:54 -04:00
Matt Nadareski
e6bc9ab3e3 Add OptionalEndsWith string extension 2025-09-20 18:04:37 -04:00
Matt Nadareski
94934b00a9 There 2025-09-10 21:53:52 -04:00
Matt Nadareski
e49f56fccc Add an enumerable extension from BOS 2025-09-06 15:42:48 -04:00
Matt Nadareski
79c64ddfa8 .NET Standard had issues with that last one 2025-09-06 15:37:24 -04:00
Matt Nadareski
b22384d5f3 Add neat string extensions from BOS 2025-09-06 15:32:36 -04:00
Matt Nadareski
955c1b5641 Bump version 2025-09-05 09:46:17 -04:00
Matt Nadareski
535f9f928d Update Models to 1.7.1 2025-09-05 09:21:15 -04:00
Matt Nadareski
f0cb15c2e4 Fix comments 2025-09-05 09:15:05 -04:00
Matt Nadareski
ec99304c51 Implement the 16KiB limit 2025-09-03 09:05:08 -04:00
Matt Nadareski
aefc931055 Of all things 2025-09-03 01:29:06 -04:00
Matt Nadareski
e7fe342379 Fix missed compatibility issue in string reading 2025-09-03 01:04:34 -04:00
Matt Nadareski
f372999b1b So that's why 2025-09-03 00:23:46 -04:00
Matt Nadareski
2679975945 TFM support thing 2025-09-03 00:22:46 -04:00
Matt Nadareski
54dd7f2f8f Add new extension tests 2025-09-03 00:20:02 -04:00
Matt Nadareski
aee5891c50 Backport thing 2025-09-03 00:15:41 -04:00
154 changed files with 21514 additions and 1190 deletions

View File

@@ -9,17 +9,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
with:
submodules: recursive
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v4
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
6.0.x
8.0.x
9.0.x
10.0.x
- name: Run tests
run: dotnet test
@@ -27,8 +27,16 @@ jobs:
- 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.14.0
uses: ncipollo/release-action@v1.20.0
with:
allowUpdates: True
artifacts: "*.nupkg,*.snupkg"

View File

@@ -6,15 +6,15 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
- name: Setup .NET
uses: actions/setup-dotnet@v4
uses: actions/setup-dotnet@v5
with:
dotnet-version: |
6.0.x
8.0.x
9.0.x
10.0.x
- name: Build
run: dotnet build

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright (c) 2018-2025 Matt Nadareski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -39,16 +39,40 @@ Various compression implementations that are used across multiple projects. Most
| [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:
@@ -63,8 +87,41 @@ For a generic INI implementation, see `SabreTools.IO.IniFile`.
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.IO.Transform`
File and stream implementations of common data transformations:
- Combine using either ordered concatenation or interleaving
- Split by even/odd chunks or based on block size
- Convert data either by bit-swapping, byte-swapping, word-swapping, or word/byte-swapping
### `SabreTools.Numerics`
Custom numeric types and related functionality.
#### Supported Numeric Types
| Type Name | Description |
| --- | --- |
| `BothInt8` | Both-endian `Int8` value |
| `BothUInt8` | Both-endian `UInt8` value |
| `BothInt16` | Both-endian `Int16` value |
| `BothUInt16` | Both-endian `UInt16` value |
| `BothInt32` | Both-endian `Int32` value |
| `BothUInt32` | Both-endian `UInt32` value |
| `BothInt64` | Both-endian `Int64` value |
| `BothUInt64` | Both-endian `UInt64` value |
**Both-endian** or **bi-endian** numbers are represented by a little-endian value followed by a big-endian value, where both values are the same number.
### `SabreTools.Text.Compare`
Classes focused on string comparison by natural sorting. For example, "5" would be sorted before "100".
## Releases

View File

@@ -0,0 +1,38 @@
using System;
using System.Linq;
using SabreTools.Text.Compare;
using Xunit;
namespace SabreTools.IO.Test.Compare
{
public class NaturalComparerTests
{
[Fact]
public void ListSort_Numeric()
{
// Setup arrays
string[] sortable = ["0", "100", "5", "2", "1000"];
string[] expected = ["0", "2", "5", "100", "1000"];
// Run sorting on array
Array.Sort(sortable, new NaturalComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
[Fact]
public void ListSort_Mixed()
{
// Setup arrays
string[] sortable = ["b3b", "c", "b", "a", "a1"];
string[] expected = ["a", "a1", "b", "b3b", "c"];
// Run sorting on array
Array.Sort(sortable, new NaturalComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
}
}

View File

@@ -0,0 +1,66 @@
using SabreTools.Text.Compare;
using Xunit;
namespace SabreTools.IO.Test.Compare
{
public class NaturalComparerUtilTests
{
[Fact]
public void CompareNumeric_BothNull_Equal()
{
int actual = NaturalComparerUtil.ComparePaths(null, null);
Assert.Equal(0, actual);
}
[Fact]
public void CompareNumeric_SingleNull_Ordered()
{
int actual = NaturalComparerUtil.ComparePaths(null, "notnull");
Assert.Equal(-1, actual);
actual = NaturalComparerUtil.ComparePaths("notnull", null);
Assert.Equal(1, actual);
}
[Fact]
public void CompareNumeric_BothEqual_Equal()
{
int actual = NaturalComparerUtil.ComparePaths("notnull", "notnull");
Assert.Equal(0, actual);
}
[Fact]
public void CompareNumeric_BothEqualWithPath_Equal()
{
int actual = NaturalComparerUtil.ComparePaths("notnull/file.ext", "notnull/file.ext");
Assert.Equal(0, actual);
}
[Fact]
public void CompareNumeric_BothEqualWithAltPath_Equal()
{
int actual = NaturalComparerUtil.ComparePaths("notnull/file.ext", "notnull\\file.ext");
Assert.Equal(0, actual);
}
[Fact]
public void CompareNumeric_NumericNonDecimalString_Ordered()
{
int actual = NaturalComparerUtil.ComparePaths("100", "10");
Assert.Equal(1, actual);
actual = NaturalComparerUtil.ComparePaths("10", "100");
Assert.Equal(-1, actual);
}
[Fact]
public void CompareNumeric_NumericDecimalString_Ordered()
{
int actual = NaturalComparerUtil.ComparePaths("100.100", "100.10");
Assert.Equal(1, actual);
actual = NaturalComparerUtil.ComparePaths("100.10", "100.100");
Assert.Equal(-1, actual);
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Linq;
using SabreTools.Text.Compare;
using Xunit;
namespace SabreTools.IO.Test.Compare
{
public class NaturalReversedComparerTests
{
[Fact]
public void ListSort_Numeric()
{
// Setup arrays
string[] sortable = ["0", "100", "5", "2", "1000"];
string[] expected = ["1000", "100", "5", "2", "0"];
// Run sorting on array
Array.Sort(sortable, new NaturalReversedComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
[Fact]
public void ListSort_Mixed()
{
// Setup arrays
string[] sortable = ["b3b", "c", "b", "a", "a1"];
string[] expected = ["c", "b3b", "b", "a1", "a"];
// Run sorting on array
Array.Sort(sortable, new NaturalReversedComparer());
// Check the output
Assert.True(sortable.SequenceEqual(expected));
}
}
}

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

@@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Text;
using SabreTools.IO.Extensions;
using Xunit;
@@ -35,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());
}
@@ -55,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());
}
@@ -65,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());
}
@@ -95,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());
}
@@ -105,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());
}
@@ -126,19 +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());
}
#if NET6_0_OR_GREATER
[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());
}
@@ -148,19 +193,18 @@ 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());
}
#endif
[Fact]
public void WriteInt24Test()
{
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());
}
@@ -170,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());
@@ -181,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());
}
@@ -191,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());
@@ -202,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());
}
@@ -212,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());
}
@@ -233,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());
}
@@ -254,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());
@@ -265,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());
}
@@ -275,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());
@@ -286,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());
}
@@ -296,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());
@@ -307,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());
}
@@ -317,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());
}
@@ -338,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());
}
@@ -359,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());
@@ -370,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());
}
@@ -380,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());
@@ -391,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());
@@ -402,19 +494,18 @@ 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());
}
#if NET7_0_OR_GREATER
[Fact]
public void WriteInt128Test()
{
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());
@@ -425,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());
}
@@ -436,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());
@@ -447,12 +538,11 @@ 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());
}
#endif
[Fact]
public void WriteNullTerminatedAnsiStringTest()
@@ -536,16 +626,13 @@ namespace SabreTools.IO.Test.Extensions
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
#if NET6_0_OR_GREATER
// Half
stream = new MemoryStream(new byte[2], 0, 2, true, true);
bw = new BinaryWriter(stream);
actual = bw.WriteType<Half>(BitConverter.Int16BitsToHalf(0x0100));
Assert.True(actual);
ValidateBytes([.. _bytes.Take(2)], stream.GetBuffer());
#endif
#if NET7_0_OR_GREATER
// Int128
stream = new MemoryStream(new byte[16], 0, 16, true, true);
bw = new BinaryWriter(stream);
@@ -559,7 +646,6 @@ namespace SabreTools.IO.Test.Extensions
actual = bw.WriteType<UInt128>((UInt128)new BigInteger(_bytes));
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
#endif
// Enum
stream = new MemoryStream(new byte[4], 0, 4, true, true);
@@ -586,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());
@@ -611,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,510 @@ 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]
public void FindAllPositions_EmptyStack_NoMatches()
{
byte[] stack = [];
var positions = stack.FindAllPositions([0x01]);
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
var positions = stack.FindAllPositions(Array.Empty<byte>());
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
var positions = stack.FindAllPositions([0x01, 0x02]);
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_InvalidStart_NoMatches()
{
byte[] stack = [0x01];
var positions = stack.FindAllPositions([0x01, 0x02], start: -1);
Assert.Empty(positions);
positions = stack.FindAllPositions([0x01, 0x02], start: 2);
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_InvalidEnd_NoMatches()
{
byte[] stack = [0x01];
var positions = stack.FindAllPositions([0x01, 0x02], end: -2);
Assert.Empty(positions);
positions = stack.FindAllPositions([0x01, 0x02], end: 0);
Assert.Empty(positions);
positions = stack.FindAllPositions([0x01, 0x02], end: 2);
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
var positions = stack.FindAllPositions([0x01, 0x02]);
int position = Assert.Single(positions);
Assert.Equal(0, position);
}
[Fact]
public void FindAllPositions_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
var positions = stack.FindAllPositions([0x01, 0x02]);
Assert.Empty(positions);
}
[Fact]
public void FindAllPositions_Multiple_Matches()
{
byte[] stack = [0x01, 0x01];
var positions = stack.FindAllPositions([0x01]);
Assert.Equal(2, positions.Count);
}
#endregion
#region FirstPosition
[Fact]
public void FirstPosition_EmptyStack_NoMatches()
{
byte[] stack = [];
int position = stack.FirstPosition([0x01]);
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
int position = stack.FirstPosition(Array.Empty<byte>());
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
int position = stack.FirstPosition([0x01, 0x02]);
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_InvalidStart_NoMatches()
{
byte[] stack = [0x01];
int position = stack.FirstPosition([0x01, 0x02], start: -1);
Assert.Equal(-1, position);
position = stack.FirstPosition([0x01, 0x02], start: 2);
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_InvalidEnd_NoMatches()
{
byte[] stack = [0x01];
int position = stack.FirstPosition([0x01, 0x02], end: -2);
Assert.Equal(-1, position);
position = stack.FirstPosition([0x01, 0x02], end: 0);
Assert.Equal(-1, position);
position = stack.FirstPosition([0x01, 0x02], end: 2);
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
int position = stack.FirstPosition([0x01, 0x02]);
Assert.Equal(0, position);
}
[Fact]
public void FirstPosition_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
int position = stack.FirstPosition([0x01, 0x02]);
Assert.Equal(-1, position);
}
[Fact]
public void FirstPosition_Multiple_Matches()
{
byte[] stack = [0x01, 0x01];
int position = stack.FirstPosition([0x01]);
Assert.Equal(0, position);
}
#endregion
#region LastPosition
[Fact]
public void LastPosition_EmptyStack_NoMatches()
{
byte[] stack = [];
int position = stack.LastPosition([0x01]);
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
int position = stack.LastPosition(Array.Empty<byte>());
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
int position = stack.LastPosition([0x01, 0x02]);
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_InvalidStart_NoMatches()
{
byte[] stack = [0x01];
int position = stack.LastPosition([0x01, 0x02], start: -1);
Assert.Equal(-1, position);
position = stack.LastPosition([0x01, 0x02], start: 2);
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_InvalidEnd_NoMatches()
{
byte[] stack = [0x01];
int position = stack.LastPosition([0x01, 0x02], end: -2);
Assert.Equal(-1, position);
position = stack.LastPosition([0x01, 0x02], end: 0);
Assert.Equal(-1, position);
position = stack.LastPosition([0x01, 0x02], end: 2);
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
int position = stack.LastPosition([0x01, 0x02]);
Assert.Equal(0, position);
}
[Fact]
public void LastPosition_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
int position = stack.LastPosition([0x01, 0x02]);
Assert.Equal(-1, position);
}
[Fact]
public void LastPosition_Multiple_Matches()
{
byte[] stack = [0x01, 0x01];
int position = stack.LastPosition([0x01]);
Assert.Equal(1, position);
}
#endregion
#region EqualsExactly
[Fact]
public void EqualsExactly_EmptyStack_NoMatches()
{
byte[] stack = [];
bool found = stack.EqualsExactly([0x01]);
Assert.False(found);
}
[Fact]
public void EqualsExactly_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.EqualsExactly(Array.Empty<byte>());
Assert.False(found);
}
[Fact]
public void EqualsExactly_ShorterNeedle_NoMatches()
{
byte[] stack = [0x01, 0x02];
bool found = stack.EqualsExactly([0x01]);
Assert.False(found);
}
[Fact]
public void EqualsExactly_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.EqualsExactly([0x01, 0x02]);
Assert.False(found);
}
[Fact]
public void EqualsExactly_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
bool found = stack.EqualsExactly([0x01, 0x02]);
Assert.True(found);
}
[Fact]
public void EqualsExactly_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
bool found = stack.EqualsExactly([0x01, 0x02]);
Assert.False(found);
}
#endregion
#region StartsWith
[Fact]
public void StartsWith_EmptyStack_NoMatches()
{
byte[] stack = [];
bool found = stack.StartsWith([0x01]);
Assert.False(found);
}
[Fact]
public void StartsWith_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.StartsWith(Array.Empty<byte>());
Assert.False(found);
}
[Fact]
public void StartsWith_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.StartsWith([0x01, 0x02]);
Assert.False(found);
}
[Fact]
public void StartsWith_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
bool found = stack.StartsWith([0x01, 0x02]);
Assert.True(found);
}
[Fact]
public void StartsWith_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
bool found = stack.StartsWith([0x01, 0x02]);
Assert.False(found);
}
[Fact]
public void StartsWith_Multiple_Matches()
{
byte[] stack = [0x01, 0x01];
bool found = stack.StartsWith([0x01]);
Assert.True(found);
}
#endregion
#region EndsWith
[Fact]
public void EndsWith_EmptyStack_NoMatches()
{
byte[] stack = [];
bool found = stack.EndsWith([0x01]);
Assert.False(found);
}
[Fact]
public void EndsWith_EmptyNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.EndsWith(Array.Empty<byte>());
Assert.False(found);
}
[Fact]
public void EndsWith_LongerNeedle_NoMatches()
{
byte[] stack = [0x01];
bool found = stack.StartsWith([0x01, 0x02]);
Assert.False(found);
}
[Fact]
public void EndsWith_Matching_Matches()
{
byte[] stack = [0x01, 0x02];
bool found = stack.EndsWith([0x01, 0x02]);
Assert.True(found);
}
[Fact]
public void EndsWith_Mismatch_NoMatches()
{
byte[] stack = [0x01, 0x03];
bool found = stack.EndsWith([0x01, 0x02]);
Assert.False(found);
}
[Fact]
public void EndsWith_Multiple_Matches()
{
byte[] stack = [0x01, 0x01];
bool found = stack.EndsWith([0x01]);
Assert.True(found);
}
#endregion
#region Add
[Theory]
[InlineData(new byte[0], 0, new byte[0])]
[InlineData(new byte[0], 1234, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD2 })]
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD2 }, 0, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD2 })]
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xD2 }, 1234, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0xA4 })]
public void Add_NumericInput(byte[] self, uint add, byte[] expected)
{
byte[] actual = self.Add(add);
Assert.Equal(expected.Length, actual.Length);
if (actual.Length > 0)
Assert.True(actual.EqualsExactly(expected));
}
[Theory]
[InlineData(new byte[0], new byte[0], new byte[0])]
[InlineData(new byte[0], new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[0], new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0x00, 0x00 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x00, 0x00 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0x09, 0xA4 })]
[InlineData(new byte[] { 0xAB, 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0xAB, 0x09, 0xA4 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0xAB, 0x04, 0xD2 }, new byte[] { 0xAB, 0x09, 0xA4 })]
public void Add_ArrayInput(byte[] self, byte[] add, byte[] expected)
{
byte[] actual = self.Add(add);
Assert.Equal(expected.Length, actual.Length);
if (actual.Length > 0)
Assert.True(actual.EqualsExactly(expected));
}
#endregion
#region RotateLeft
[Theory]
[InlineData(new byte[0], 0, new byte[0])]
[InlineData(new byte[] { 0x01 }, 0, new byte[] { 0x01 })]
[InlineData(new byte[] { 0x01 }, 1, new byte[] { 0x02 })]
[InlineData(new byte[] { 0x80 }, 1, new byte[] { 0x01 })]
[InlineData(new byte[] { 0x00, 0x01 }, 0, new byte[] { 0x00, 0x01 })]
[InlineData(new byte[] { 0x00, 0x01 }, 1, new byte[] { 0x00, 0x02 })]
[InlineData(new byte[] { 0x00, 0x80 }, 1, new byte[] { 0x01, 0x00 })]
[InlineData(new byte[] { 0x80, 0x00 }, 1, new byte[] { 0x00, 0x01 })]
public void RotateLeftTest(byte[] self, int numBits, byte[] expected)
{
byte[] actual = self.RotateLeft(numBits);
Assert.Equal(expected.Length, actual.Length);
if (actual.Length > 0)
Assert.True(actual.EqualsExactly(expected));
}
#endregion
#region Xor
[Theory]
[InlineData(new byte[0], new byte[0], new byte[0])]
[InlineData(new byte[0], new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[0], new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0x00, 0x00 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x00, 0x00 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0x00, 0x00 })]
[InlineData(new byte[] { 0xAB, 0x04, 0xD2 }, new byte[] { 0x04, 0xD2 }, new byte[] { 0xAB, 0x00, 0x00 })]
[InlineData(new byte[] { 0x04, 0xD2 }, new byte[] { 0xAB, 0x04, 0xD2 }, new byte[] { 0xAB, 0x00, 0x00 })]
public void XorTest(byte[] self, byte[] add, byte[] expected)
{
byte[] actual = self.Xor(add);
Assert.Equal(expected.Length, actual.Length);
if (actual.Length > 0)
Assert.True(actual.EqualsExactly(expected));
}
#endregion
#region ToHexString
[Fact]
@@ -90,6 +594,174 @@ namespace SabreTools.IO.Test.Extensions
#endregion
#region ReadStringsFrom
[Fact]
public void ReadStringsFrom_Null_Null()
{
byte[]? arr = null;
var actual = arr.ReadStringsFrom(3);
Assert.Null(actual);
}
[Fact]
public void ReadStringsFrom_Empty_Null()
{
byte[]? arr = [];
var actual = arr.ReadStringsFrom(3);
Assert.Null(actual);
}
[Theory]
[InlineData(-1)]
[InlineData(0)]
[InlineData(2048)]
public void ReadStringsFrom_InvalidLimit_Empty(int charLimit)
{
byte[]? arr = new byte[1024];
var actual = arr.ReadStringsFrom(charLimit);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsFrom_NoValidStrings_Empty()
{
byte[]? arr = new byte[1024];
var actual = arr.ReadStringsFrom(4);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsFrom_AsciiStrings_Filled()
{
byte[]? arr =
[
.. Encoding.ASCII.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("DATA"),
.. new byte[] { 0x00 },
];
var actual = arr.ReadStringsFrom(4);
Assert.NotNull(actual);
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsFrom_Latin1Strings_Filled()
{
byte[]? arr =
[
.. Encoding.Latin1.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("DATA"),
.. new byte[] { 0x00 },
];
var actual = arr.ReadStringsFrom(4);
Assert.NotNull(actual);
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsFrom_UTF16_Filled()
{
byte[]? arr =
[
.. Encoding.Unicode.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("DATA"),
.. new byte[] { 0x00 },
];
var actual = arr.ReadStringsFrom(4);
Assert.NotNull(actual);
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsFrom_Mixed_Filled()
{
byte[]? arr =
[
.. Encoding.ASCII.GetBytes("TEST1"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("TWO1"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("DATA1"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("TEST2"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("TWO2"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("DATA2"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("TEST3"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("TWO3"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("DATA3"),
.. new byte[] { 0x00 },
];
var actual = arr.ReadStringsFrom(5);
Assert.NotNull(actual);
Assert.Equal(6, actual.Count);
}
/// <summary>
/// This test is here mainly for performance testing
/// and should not be enabled unless there are changes
/// to the core reading methods that need comparison.
/// </summary>
// [Fact]
// public void ReadStringsFrom_Mixed_MASSIVE()
// {
// byte[]? arr =
// [
// .. Encoding.ASCII.GetBytes("TEST1"),
// .. new byte[] { 0x00 },
// .. Encoding.ASCII.GetBytes("TWO1"),
// .. new byte[] { 0x00 },
// .. Encoding.ASCII.GetBytes("DATA1"),
// .. new byte[] { 0x00 },
// .. Encoding.UTF8.GetBytes("TEST2"),
// .. new byte[] { 0x00 },
// .. Encoding.UTF8.GetBytes("TWO2"),
// .. new byte[] { 0x00 },
// .. Encoding.UTF8.GetBytes("DATA2"),
// .. new byte[] { 0x00 },
// .. Encoding.Unicode.GetBytes("TEST3"),
// .. new byte[] { 0x00 },
// .. Encoding.Unicode.GetBytes("TWO3"),
// .. new byte[] { 0x00 },
// .. Encoding.Unicode.GetBytes("DATA3"),
// .. new byte[] { 0x00 },
// ];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// arr = [.. arr, .. arr, .. arr, .. arr];
// // arr = [.. arr, .. arr, .. arr, .. arr];
// // arr = [.. arr, .. arr, .. arr, .. arr];
// var actual = arr.ReadStringsFrom(5);
// Assert.NotNull(actual);
// Assert.NotEmpty(actual);
// }
#endregion
#region ReadStringsWithEncoding
[Fact]
@@ -134,6 +806,8 @@ namespace SabreTools.IO.Test.Extensions
[
.. Encoding.ASCII.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("ONE"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("DATA"),
@@ -143,6 +817,22 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsWithEncoding_InvalidAsciiChars_Empty()
{
byte[]? arr =
[
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,
.. Enumerable.Range(0x80, 0x80).Select(i => (byte)i),
];
var actual = arr.ReadStringsWithEncoding(1, Encoding.ASCII);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsWithEncoding_Latin1_Filled()
{
@@ -150,6 +840,8 @@ namespace SabreTools.IO.Test.Extensions
[
.. Encoding.Latin1.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("ONE"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.Latin1.GetBytes("DATA"),
@@ -159,6 +851,25 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsWithEncoding_InvalidLatin1Chars_Empty()
{
byte[]? arr =
[
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,
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,
];
var actual = arr.ReadStringsWithEncoding(1, Encoding.Latin1);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsWithEncoding_UTF8_Filled()
{
@@ -166,6 +877,8 @@ namespace SabreTools.IO.Test.Extensions
[
.. Encoding.UTF8.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("ONE"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("DATA"),
@@ -175,6 +888,24 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsWithEncoding_InvalidUTF8Chars_Empty()
{
byte[]? arr =
[
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,
.. Enumerable.Range(0x80, 0x42).Select(i => (byte)i),
0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC,
0xFD, 0xFE, 0xFF,
];
var actual = arr.ReadStringsWithEncoding(1, Encoding.UTF8);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsWithEncoding_UTF16_Filled()
{
@@ -182,6 +913,8 @@ namespace SabreTools.IO.Test.Extensions
[
.. Encoding.Unicode.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("ONE"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("DATA"),
@@ -191,6 +924,21 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsWithEncoding_InvalidUTF16Chars_Empty()
{
byte[]? arr =
[
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,
];
var actual = arr.ReadStringsWithEncoding(1, Encoding.Unicode);
Assert.NotNull(actual);
Assert.Empty(actual);
}
[Fact]
public void ReadStringsWithEncoding_UTF32_Filled()
{
@@ -198,6 +946,8 @@ namespace SabreTools.IO.Test.Extensions
[
.. Encoding.UTF32.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.UTF32.GetBytes("ONE"),
.. new byte[] { 0x00 },
.. Encoding.UTF32.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.UTF32.GetBytes("DATA"),
@@ -207,6 +957,21 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsWithEncoding_InvalidUTF32Chars_Empty()
{
byte[]? arr =
[
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,
];
var actual = arr.ReadStringsWithEncoding(1, Encoding.UTF32);
Assert.NotNull(actual);
Assert.Empty(actual);
}
#endregion
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,6 @@
using System;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Text;
using SabreTools.IO.Extensions;
using Xunit;
@@ -34,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);
@@ -56,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);
@@ -67,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);
@@ -100,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);
@@ -111,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);
@@ -133,19 +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);
}
#if NET6_0_OR_GREATER
[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);
@@ -156,19 +201,18 @@ 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);
}
#endif
[Fact]
public void WriteInt24Test()
{
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);
@@ -179,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);
@@ -190,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);
@@ -201,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);
@@ -212,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);
@@ -223,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);
@@ -245,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);
@@ -267,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);
@@ -278,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);
@@ -289,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);
@@ -300,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);
@@ -311,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);
@@ -322,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);
@@ -333,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);
@@ -355,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);
@@ -377,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);
@@ -388,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);
@@ -399,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);
@@ -410,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);
@@ -421,19 +513,18 @@ 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);
}
#if NET7_0_OR_GREATER
[Fact]
public void WriteInt128Test()
{
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);
@@ -444,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);
}
@@ -455,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);
@@ -466,12 +557,11 @@ 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);
}
#endif
[Fact]
public void WriteNullTerminatedAnsiStringTest()
@@ -555,16 +645,13 @@ namespace SabreTools.IO.Test.Extensions
Assert.True(actual);
ValidateBytes(_bytes, buffer);
#if NET6_0_OR_GREATER
// Half
offset = 0;
buffer = new byte[2];
actual = buffer.WriteType<Half>(ref offset, BitConverter.Int16BitsToHalf(0x0100));
Assert.True(actual);
ValidateBytes([.. _bytes.Take(2)], buffer);
#endif
#if NET7_0_OR_GREATER
// Int128
offset = 0;
buffer = new byte[16];
@@ -578,7 +665,6 @@ namespace SabreTools.IO.Test.Extensions
actual = buffer.WriteType<UInt128>(ref offset, (UInt128)new BigInteger(_bytes));
Assert.True(actual);
ValidateBytes(_bytes, buffer);
#endif
// Enum
offset = 0;
@@ -605,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);
@@ -630,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

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SabreTools.IO.Extensions;
using Xunit;
@@ -9,6 +10,22 @@ namespace SabreTools.IO.Test.Extensions
{
public class EnumerableExtensionsTests
{
#region IterateWithAction
[Fact]
public void IterateWithActionTest()
{
List<int> source = [1, 2, 3, 4];
int actual = 0;
source.IterateWithAction(i => Interlocked.Add(ref actual, i));
Assert.Equal(10, actual);
}
#endregion
#region SafeEnumerate
[Fact]
public void SafeEnumerate_Empty()
{
@@ -60,8 +77,10 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(2, list.Count);
}
#endregion
/// <summary>
/// Fake enumerable that uses <see cref="ErrorEnumerator"/>
/// Fake enumerable that uses <see cref="ErrorEnumerator"/>
/// </summary>
private class ErrorEnumerable : IEnumerable<string>
{

View File

@@ -200,29 +200,25 @@ namespace SabreTools.IO.Test.Extensions
Stream? stream = new MemoryStream(bytes);
var actual = stream.ReadStringsFrom(0, bytes.Length, 4);
Assert.NotNull(actual);
// ASCII and UTF-8 are identical for the character range
Assert.Equal(4, actual.Count);
Assert.Equal(2, actual.Count);
}
[Fact]
public void ReadStringsFrom_UTF8_Filled()
public void ReadStringsFrom_Latin1Strings_Filled()
{
byte[]? bytes =
[
.. Encoding.UTF8.GetBytes("TEST"),
.. Encoding.Latin1.GetBytes("TEST"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("TWO"),
.. Encoding.Latin1.GetBytes("TWO"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("DATA"),
.. Encoding.Latin1.GetBytes("DATA"),
.. new byte[] { 0x00 },
];
Stream? stream = new MemoryStream(bytes);
var actual = stream.ReadStringsFrom(0, bytes.Length, 4);
Assert.NotNull(actual);
// ASCII and UTF-8 are identical for the character range
Assert.Equal(4, actual.Count);
Assert.Equal(2, actual.Count);
}
[Fact]
@@ -254,11 +250,11 @@ namespace SabreTools.IO.Test.Extensions
.. new byte[] { 0x00 },
.. Encoding.ASCII.GetBytes("DATA1"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("TEST2"),
.. Encoding.Latin1.GetBytes("TEST2"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("TWO2"),
.. Encoding.Latin1.GetBytes("TWO2"),
.. new byte[] { 0x00 },
.. Encoding.UTF8.GetBytes("DATA2"),
.. Encoding.Latin1.GetBytes("DATA2"),
.. new byte[] { 0x00 },
.. Encoding.Unicode.GetBytes("TEST3"),
.. new byte[] { 0x00 },
@@ -270,9 +266,7 @@ namespace SabreTools.IO.Test.Extensions
Stream? stream = new MemoryStream(bytes);
var actual = stream.ReadStringsFrom(0, bytes.Length, 5);
Assert.NotNull(actual);
// ASCII and UTF-8 are identical for the character range
Assert.Equal(10, actual.Count);
Assert.Equal(6, actual.Count);
}
#endregion
@@ -319,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

@@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Text;
using SabreTools.IO.Extensions;
using Xunit;
@@ -34,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());
@@ -54,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());
}
@@ -63,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());
@@ -92,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());
@@ -102,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());
@@ -122,18 +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());
}
#if NET6_0_OR_GREATER
[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());
@@ -143,18 +184,17 @@ 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());
}
#endif
[Fact]
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());
@@ -164,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());
@@ -174,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());
@@ -184,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());
@@ -194,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());
@@ -204,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());
@@ -224,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());
@@ -244,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());
@@ -254,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());
@@ -264,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());
@@ -274,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());
@@ -284,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());
@@ -294,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());
@@ -304,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());
@@ -324,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());
@@ -344,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());
@@ -354,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());
@@ -364,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());
@@ -374,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());
@@ -384,18 +468,17 @@ 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());
}
#if NET7_0_OR_GREATER
[Fact]
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());
@@ -405,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());
}
@@ -415,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());
@@ -425,12 +508,11 @@ 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());
}
#endif
[Fact]
public void WriteNullTerminatedAnsiStringTest()
@@ -507,15 +589,12 @@ namespace SabreTools.IO.Test.Extensions
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
#if NET6_0_OR_GREATER
// Half
stream = new MemoryStream(new byte[2], 0, 2, true, true);
actual = stream.WriteType<Half>(BitConverter.Int16BitsToHalf(0x0100));
Assert.True(actual);
ValidateBytes([.. _bytes.Take(2)], stream.GetBuffer());
#endif
#if NET7_0_OR_GREATER
// Int128
stream = new MemoryStream(new byte[16], 0, 16, true, true);
actual = stream.WriteType<Int128>((Int128)new BigInteger(_bytes));
@@ -527,7 +606,6 @@ namespace SabreTools.IO.Test.Extensions
actual = stream.WriteType<UInt128>((UInt128)new BigInteger(_bytes));
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
#endif
// Enum
stream = new MemoryStream(new byte[4], 0, 4, true, true);
@@ -552,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());
@@ -576,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

@@ -0,0 +1,76 @@
using SabreTools.IO.Extensions;
using Xunit;
namespace SabreTools.IO.Test.Extensions
{
public class StringExtensionsTests
{
#region OptionalContains
[Theory]
[InlineData(null, "ANY", false)]
[InlineData("", "ANY", false)]
[InlineData("ANY", "ANY", true)]
[InlineData("ANYTHING", "ANY", true)]
[InlineData("THING", "ANY", false)]
[InlineData("THINGANY", "ANY", true)]
public void OptionalContainsTest(string? haystack, string needle, bool expected)
{
bool actual = haystack.OptionalContains(needle);
Assert.Equal(expected, actual);
}
#endregion
#region OptionalEndsWith
[Theory]
[InlineData(null, "ANY", false)]
[InlineData("", "ANY", false)]
[InlineData("ANY", "ANY", true)]
[InlineData("ANYTHING", "ANY", false)]
[InlineData("THING", "ANY", false)]
[InlineData("THINGANY", "ANY", true)]
public void OptionalEndsWithTest(string? haystack, string needle, bool expected)
{
bool actual = haystack.OptionalEndsWith(needle);
Assert.Equal(expected, actual);
}
#endregion
#region OptionalEquals
[Theory]
[InlineData(null, "ANY", false)]
[InlineData("", "ANY", false)]
[InlineData("ANY", "ANY", true)]
[InlineData("ANYTHING", "ANY", false)]
[InlineData("THING", "ANY", false)]
[InlineData("THINGANY", "ANY", false)]
public void OptionalEqualsTest(string? haystack, string needle, bool expected)
{
bool actual = haystack.OptionalEquals(needle);
Assert.Equal(expected, actual);
}
#endregion
#region OptionalStartsWith
[Theory]
[InlineData(null, "ANY", false)]
[InlineData("", "ANY", false)]
[InlineData("ANY", "ANY", true)]
[InlineData("ANYTHING", "ANY", true)]
[InlineData("THING", "ANY", false)]
[InlineData("THINGANY", "ANY", false)]
public void OptionalStartsWithTest(string? haystack, string needle, bool expected)
{
bool actual = haystack.OptionalStartsWith(needle);
Assert.Equal(expected, actual);
}
#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

@@ -0,0 +1,324 @@
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test
{
public class MatchUtilTests
{
#region Array
[Fact]
public void ArrayGetAllMatches_NullStack_NoMatches()
{
byte[]? stack = null;
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void ArrayGetAllMatches_EmptyStack_NoMatches()
{
byte[] stack = [];
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void ArrayGetAllMatches_EmptyMatchSets_NoMatches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets = [];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void ArrayGetAllMatches_Matching_Matches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[] { 0x01 }, "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
string setName = Assert.Single(matches);
Assert.Equal("name", setName);
}
[Fact]
public void ArrayGetAllMatches_PartialMatchingAny_Matches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets, any: true);
string setName = Assert.Single(matches);
Assert.Equal("name", setName);
}
[Fact]
public void ArrayGetAllMatches_PartialMatchingAll_NoMatches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets, any: false);
Assert.Empty(matches);
}
[Fact]
public void ArrayGetFirstMatch_NullStack_NoMatches()
{
byte[]? stack = null;
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void ArrayGetFirstMatch_EmptyStack_NoMatches()
{
byte[] stack = [];
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void ArrayGetFirstMatch_EmptyMatchSets_NoMatches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets = [];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void ArrayGetFirstMatch_Matching_Matches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[] { 0x01 }, "name")];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Equal("name", setName);
}
[Fact]
public void ArrayGetFirstMatch_PartialMatchingAny_Matches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets, any: true);
Assert.Equal("name", setName);
}
[Fact]
public void ArrayGetFirstMatch_PartialMatchingAll_NoMatches()
{
byte[] stack = [0x01];
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets, any: false);
Assert.Null(setName);
}
[Fact]
public void ExactSizeArrayMatch()
{
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
string expected = "match";
var matchers = new List<ContentMatchSet>
{
new(check, expected),
};
string? actual = MatchUtil.GetFirstMatch("testfile", source, matchers, any: false);
Assert.Equal(expected, actual);
}
#endregion
#region Stream
[Fact]
public void StreamGetAllMatches_NullStack_NoMatches()
{
Stream? stack = null;
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void StreamGetAllMatches_EmptyStack_NoMatches()
{
Stream stack = new MemoryStream();
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void StreamGetAllMatches_EmptyMatchSets_NoMatches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets = [];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
Assert.Empty(matches);
}
[Fact]
public void StreamGetAllMatches_Matching_Matches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[] { 0x01 }, "name")];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets);
string setName = Assert.Single(matches);
Assert.Equal("name", setName);
}
[Fact]
public void StreamGetAllMatches_PartialMatchingAny_Matches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets, any: true);
string setName = Assert.Single(matches);
Assert.Equal("name", setName);
}
[Fact]
public void StreamGetAllMatches_PartialMatchingAll_NoMatches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
var matches = MatchUtil.GetAllMatches("file", stack, matchSets, any: false);
Assert.Empty(matches);
}
[Fact]
public void StreamGetFirstMatch_NullStack_NoMatches()
{
Stream? stack = null;
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void StreamGetFirstMatch_EmptyStack_NoMatches()
{
Stream stack = new MemoryStream();
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[1], "name")];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void StreamGetFirstMatch_EmptyMatchSets_NoMatches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets = [];
string? match = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Null(match);
}
[Fact]
public void StreamGetFirstMatch_Matching_Matches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets = [new ContentMatchSet(new byte[] { 0x01 }, "name")];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets);
Assert.Equal("name", setName);
}
[Fact]
public void StreamGetFirstMatch_PartialMatchingAny_Matches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets, any: true);
Assert.Equal("name", setName);
}
[Fact]
public void StreamGetFirstMatch_PartialMatchingAll_NoMatches()
{
Stream stack = new MemoryStream([0x01]);
List<ContentMatchSet> matchSets =
[
new ContentMatchSet([
new byte[] { 0x00 },
new ContentMatch([0x01]),
], "name")
];
string? setName = MatchUtil.GetFirstMatch("file", stack, matchSets, any: false);
Assert.Null(setName);
}
[Fact]
public void ExactSizeStreamMatch()
{
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
var stream = new MemoryStream(source);
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
string expected = "match";
var matchers = new List<ContentMatchSet>
{
new(check, expected),
};
string? actual = MatchUtil.GetFirstMatch("testfile", stream, matchers, any: false);
Assert.Equal(expected, actual);
}
#endregion
#region Path
#endregion
}
}

View File

@@ -0,0 +1,202 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test.Matching
{
public class ContentMatchSetTests
{
[Fact]
public void InvalidNeedle_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new ContentMatchSet(Array.Empty<byte>(), "name"));
Assert.Throws<InvalidDataException>(() => new ContentMatchSet(Array.Empty<byte>(), ArrayVersionMock, "name"));
Assert.Throws<InvalidDataException>(() => new ContentMatchSet(Array.Empty<byte>(), StreamVersionMock, "name"));
}
[Fact]
public void InvalidNeedles_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new ContentMatchSet([], "name"));
Assert.Throws<InvalidDataException>(() => new ContentMatchSet([], ArrayVersionMock, "name"));
Assert.Throws<InvalidDataException>(() => new ContentMatchSet([], StreamVersionMock, "name"));
}
[Fact]
public void GenericConstructor_NoDelegates()
{
var needles = new List<ContentMatch> { new byte[] { 0x01, 0x02, 0x03, 0x04 } };
var cms = new ContentMatchSet(needles, "name");
Assert.Null(cms.GetArrayVersion);
Assert.Null(cms.GetStreamVersion);
}
[Fact]
public void ArrayConstructor_SingleDelegate()
{
var needles = new List<ContentMatch> { new byte[] { 0x01, 0x02, 0x03, 0x04 } };
var cms = new ContentMatchSet(needles, ArrayVersionMock, "name");
Assert.NotNull(cms.GetArrayVersion);
Assert.Null(cms.GetStreamVersion);
}
[Fact]
public void StreamConstructor_SingleDelegate()
{
var needles = new List<ContentMatch> { new byte[] { 0x01, 0x02, 0x03, 0x04 } };
var cms = new ContentMatchSet(needles, StreamVersionMock, "name");
Assert.Null(cms.GetArrayVersion);
Assert.NotNull(cms.GetStreamVersion);
}
#region Array
[Fact]
public void MatchesAll_NullArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll((byte[]?)null);
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_EmptyArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll([]);
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_MatchingArray_Matches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll([0x01, 0x02, 0x03, 0x04]);
int position = Assert.Single(actual);
Assert.Equal(0, position);
}
[Fact]
public void MatchesAll_MismatchedArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll([0x01, 0x03]);
Assert.Empty(actual);
}
[Fact]
public void MatchesAny_NullArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny((byte[]?)null);
Assert.Equal(-1, actual);
}
[Fact]
public void MatchesAny_EmptyArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny([]);
Assert.Equal(-1, actual);
}
[Fact]
public void MatchesAny_MatchingArray_Matches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny([0x01, 0x02, 0x03, 0x04]);
Assert.Equal(0, actual);
}
[Fact]
public void MatchesAny_MismatchedArray_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny([0x01, 0x03]);
Assert.Equal(-1, actual);
}
#endregion
#region Stream
[Fact]
public void MatchesAll_NullStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll((Stream?)null);
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_EmptyStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll(new MemoryStream());
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_MatchingStream_Matches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll(new MemoryStream([0x01, 0x02, 0x03, 0x04]));
int position = Assert.Single(actual);
Assert.Equal(0, position);
}
[Fact]
public void MatchesAll_MismatchedStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
var actual = cms.MatchesAll([0x01, 0x03]);
Assert.Empty(actual);
}
[Fact]
public void MatchesAny_NullStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny((Stream?)null);
Assert.Equal(-1, actual);
}
[Fact]
public void MatchesAny_EmptyStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny(new MemoryStream());
Assert.Equal(-1, actual);
}
[Fact]
public void MatchesAny_MatchingStream_Matches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny(new MemoryStream([0x01, 0x02, 0x03, 0x04]));
Assert.Equal(0, actual);
}
[Fact]
public void MatchesAny_MismatchedStream_NoMatches()
{
var cms = new ContentMatchSet(new byte[] { 0x01, 0x02, 0x03, 0x04 }, "name");
int actual = cms.MatchesAny([0x01, 0x03]);
Assert.Equal(-1, actual);
}
#endregion
#region Mock Delegates
/// <inheritdoc cref="GetArrayVersion"/>
private static string? ArrayVersionMock(string path, byte[]? content, List<int> positions) => null;
/// <inheritdoc cref="GetStreamVersion"/>
private static string? StreamVersionMock(string path, Stream? content, List<int> positions) => null;
#endregion
}
}

View File

@@ -0,0 +1,271 @@
using System;
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test.Matching
{
public class ContentMatchTests
{
[Fact]
public void InvalidNeedle_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new ContentMatch(Array.Empty<byte>()));
Assert.Throws<InvalidDataException>(() => new ContentMatch(Array.Empty<byte?>()));
}
[Fact]
public void InvalidStart_ThrowsException()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new ContentMatch(new byte[1], start: -1));
Assert.Throws<ArgumentOutOfRangeException>(() => new ContentMatch(new byte?[1], start: -1));
}
[Fact]
public void InvalidEnd_ThrowsException()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new ContentMatch(new byte[1], end: -2));
Assert.Throws<ArgumentOutOfRangeException>(() => new ContentMatch(new byte?[1], end: -2));
}
[Fact]
public void ImplicitOperatorArray_Success()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = (ContentMatch)needle;
Assert.NotNull(cm);
}
[Fact]
public void ImplicitOperatorNullableArray_Success()
{
byte?[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = (ContentMatch)needle;
Assert.NotNull(cm);
}
#region Byte Array
[Fact]
public void NullArray_NoMatch()
{
var cm = new ContentMatch(new byte?[1]);
int actual = cm.Match((byte[]?)null);
Assert.Equal(-1, actual);
}
[Fact]
public void EmptyArray_NoMatch()
{
var cm = new ContentMatch(new byte?[1]);
int actual = cm.Match([]);
Assert.Equal(-1, actual);
}
[Fact]
public void LargerNeedleArray_NoMatch()
{
var cm = new ContentMatch(new byte?[2]);
int actual = cm.Match(new byte[1]);
Assert.Equal(-1, actual);
}
[Fact]
public void EqualLengthMatchingArray_Match()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(needle);
Assert.Equal(0, actual);
}
[Fact]
public void EqualLengthMatchingArrayReverse_Match()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(needle, reverse: true);
Assert.Equal(0, actual);
}
[Fact]
public void EqualLengthMismatchedArray_NoMatch()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new byte[4]);
Assert.Equal(-1, actual);
}
[Fact]
public void EqualLengthMismatchedArrayReverse_NoMatch()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new byte[4], reverse: true);
Assert.Equal(-1, actual);
}
[Fact]
public void InequalLengthMatchingArray_Match()
{
byte[] stack = [0x01, 0x02, 0x03, 0x04];
byte[] needle = [0x02, 0x03];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack);
Assert.Equal(1, actual);
}
[Fact]
public void InequalLengthMatchingArrayReverse_Match()
{
byte[] stack = [0x01, 0x02, 0x03, 0x04];
byte[] needle = [0x02, 0x03];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack, reverse: true);
Assert.Equal(1, actual);
}
[Fact]
public void InequalLengthMismatchedArray_NoMatch()
{
byte[] stack = [0x01, 0x02, 0x03, 0x04];
byte[] needle = [0x02, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack);
Assert.Equal(-1, actual);
}
[Fact]
public void InequalLengthMismatchedArrayReverse_NoMatch()
{
byte[] stack = [0x01, 0x02, 0x03, 0x04];
byte[] needle = [0x02, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack, reverse: true);
Assert.Equal(-1, actual);
}
#endregion
#region Stream
[Fact]
public void NullStream_NoMatch()
{
var cm = new ContentMatch(new byte?[1]);
int actual = cm.Match((Stream?)null);
Assert.Equal(-1, actual);
}
[Fact]
public void EmptyStream_NoMatch()
{
var cm = new ContentMatch(new byte?[1]);
int actual = cm.Match(new MemoryStream());
Assert.Equal(-1, actual);
}
[Fact]
public void LargerNeedleStream_NoMatch()
{
var cm = new ContentMatch(new byte?[2]);
int actual = cm.Match(new MemoryStream(new byte[1]));
Assert.Equal(-1, actual);
}
[Fact]
public void EqualLengthMatchingStream_Match()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new MemoryStream(needle));
Assert.Equal(0, actual);
}
[Fact]
public void EqualLengthMatchingStreamReverse_Match()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new MemoryStream(needle), reverse: true);
Assert.Equal(0, actual);
}
[Fact]
public void EqualLengthMismatchedStream_NoMatch()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new MemoryStream(new byte[4]));
Assert.Equal(-1, actual);
}
[Fact]
public void EqualLengthMismatchedStreamReverse_NoMatch()
{
byte[] needle = [0x01, 0x02, 0x03, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(new MemoryStream(new byte[4]), reverse: true);
Assert.Equal(-1, actual);
}
[Fact]
public void InequalLengthMatchingStream_Match()
{
Stream stack = new MemoryStream([0x01, 0x02, 0x03, 0x04]);
byte[] needle = [0x02, 0x03];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack);
Assert.Equal(1, actual);
}
[Fact]
public void InequalLengthMatchingStreamReverse_Match()
{
Stream stack = new MemoryStream([0x01, 0x02, 0x03, 0x04]);
byte[] needle = [0x02, 0x03];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack, reverse: true);
Assert.Equal(1, actual);
}
[Fact]
public void InequalLengthMismatchedStream_NoMatch()
{
Stream stack = new MemoryStream([0x01, 0x02, 0x03, 0x04]);
byte[] needle = [0x02, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack);
Assert.Equal(-1, actual);
}
[Fact]
public void InequalLengthMismatchedStreamReverse_NoMatch()
{
Stream stack = new MemoryStream([0x01, 0x02, 0x03, 0x04]);
byte[] needle = [0x02, 0x04];
var cm = new ContentMatch(needle);
int actual = cm.Match(stack, reverse: true);
Assert.Equal(-1, actual);
}
#endregion
}
}

View File

@@ -0,0 +1,22 @@
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test.Matching
{
/// <remarks>
/// All other test cases are covered by <see cref="PathMatchTests"/>
/// </remarks>
public class FilePathMatchTests
{
[Fact]
public void ConstructorFormatsNeedle()
{
string needle = "test";
string expected = $"{Path.DirectorySeparatorChar}{needle}";
var fpm = new FilePathMatch(needle);
Assert.Equal(expected, fpm.Needle);
}
}
}

View File

@@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test.Matching
{
public class PathMatchSetTests
{
[Fact]
public void InvalidNeedle_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new PathMatchSet(string.Empty, "name"));
Assert.Throws<InvalidDataException>(() => new PathMatchSet(string.Empty, PathVersionMock, "name"));
}
[Fact]
public void InvalidNeedles_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new PathMatchSet([], "name"));
Assert.Throws<InvalidDataException>(() => new PathMatchSet([], PathVersionMock, "name"));
}
[Fact]
public void GenericConstructor_NoDelegates()
{
var needles = new List<PathMatch> { "test" };
var cms = new PathMatchSet(needles, "name");
Assert.Null(cms.GetVersion);
}
[Fact]
public void VersionConstructor_SingleDelegate()
{
var needles = new List<PathMatch> { "test" };
var cms = new PathMatchSet(needles, PathVersionMock, "name");
Assert.NotNull(cms.GetVersion);
}
#region Array
[Fact]
public void MatchesAll_NullArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll((string[]?)null);
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_EmptyArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(Array.Empty<string>());
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_MatchingArray_Matches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(new string[] { "test" });
string path = Assert.Single(actual);
Assert.Equal("test", path);
}
[Fact]
public void MatchesAll_MismatchedArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(new string[] { "not" });
Assert.Empty(actual);
}
[Fact]
public void MatchesAny_NullArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny((string[]?)null);
Assert.Null(actual);
}
[Fact]
public void MatchesAny_EmptyArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(Array.Empty<string>());
Assert.Null(actual);
}
[Fact]
public void MatchesAny_MatchingArray_Matches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(new string[] { "test" });
Assert.Equal("test", actual);
}
[Fact]
public void MatchesAny_MismatchedArray_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(new string[] { "not" });
Assert.Null(actual);
}
#endregion
#region List
[Fact]
public void MatchesAll_NullList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll((List<string>?)null);
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_EmptyList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(new List<string>());
Assert.Empty(actual);
}
[Fact]
public void MatchesAll_MatchingList_Matches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(new List<string> { "test" });
string path = Assert.Single(actual);
Assert.Equal("test", path);
}
[Fact]
public void MatchesAll_MismatchedList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
var actual = cms.MatchesAll(new List<string> { "not" });
Assert.Empty(actual);
}
[Fact]
public void MatchesAny_NullList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny((List<string>?)null);
Assert.Null(actual);
}
[Fact]
public void MatchesAny_EmptyList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(new List<string>());
Assert.Null(actual);
}
[Fact]
public void MatchesAny_MatchingList_Matches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(new List<string> { "test" });
Assert.Equal("test", actual);
}
[Fact]
public void MatchesAny_MismatchedList_NoMatches()
{
var cms = new PathMatchSet("test", "name");
string? actual = cms.MatchesAny(new List<string> { "not" });
Assert.Null(actual);
}
#endregion
#region Mock Delegates
/// <inheritdoc cref="GetPathVersion"/>
private static string? PathVersionMock(string path, List<string>? files) => null;
#endregion
}
}

View File

@@ -0,0 +1,293 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Matching;
using Xunit;
namespace SabreTools.IO.Test.Matching
{
public class PathMatchTests
{
[Fact]
public void InvalidNeedle_ThrowsException()
{
Assert.Throws<InvalidDataException>(() => new PathMatch(string.Empty));
}
[Fact]
public void ImplicitOperatorArray_Success()
{
string needle = "test";
var pm = (PathMatch)needle;
Assert.NotNull(pm);
}
#region Array
[Fact]
public void NullArray_NoMatch()
{
var pm = new PathMatch("test");
string? actual = pm.Match((string[]?)null);
Assert.Null(actual);
}
[Fact]
public void EmptyArray_NoMatch()
{
var pm = new PathMatch("test");
string? actual = pm.Match(Array.Empty<string>());
Assert.Null(actual);
}
[Fact]
public void SingleItemArrayMatching_Match()
{
string needle = "test";
string[] stack = [needle];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void SingleItemArrayMismatched_NoMatch()
{
string needle = "test";
string[] stack = ["not"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
[Fact]
public void MultiItemArrayMatching_Match()
{
string needle = "test";
string[] stack = ["not", needle, "far"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void MultiItemArrayMismatched_NoMatch()
{
string needle = "test";
string[] stack = ["not", "too", "far"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
#endregion
#region List
[Fact]
public void NullList_NoMatch()
{
var pm = new PathMatch("test");
string? actual = pm.Match((List<string>?)null);
Assert.Null(actual);
}
[Fact]
public void EmptyList_NoMatch()
{
var pm = new PathMatch("test");
string? actual = pm.Match(new List<string>());
Assert.Null(actual);
}
[Fact]
public void SingleItemListMatching_Match()
{
string needle = "test";
List<string> stack = [needle];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void SingleItemListMismatched_NoMatch()
{
string needle = "test";
List<string> stack = ["not"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
[Fact]
public void MultiItemListMatching_Match()
{
string needle = "test";
List<string> stack = ["not", needle, "far"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void MultiItemListMismatched_NoMatch()
{
string needle = "test";
List<string> stack = ["not", "too", "far"];
var pm = new PathMatch(needle);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
#endregion
#region Match Case
[Fact]
public void MatchCaseEqual_Match()
{
string needle = "test";
List<string> stack = [needle];
var pm = new PathMatch(needle, matchCase: true);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void NoMatchCaseEqual_Match()
{
string needle = "test";
List<string> stack = [needle];
var pm = new PathMatch(needle, matchCase: false);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void MatchCaseInequal_NoMatch()
{
string needle = "test";
List<string> stack = [needle.ToUpperInvariant()];
var pm = new PathMatch(needle, matchCase: true);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
[Fact]
public void NoMatchCaseInequal_Match()
{
string needle = "test";
List<string> stack = [needle.ToUpperInvariant()];
var pm = new PathMatch(needle, matchCase: false);
string? actual = pm.Match(stack);
Assert.Equal(needle.ToUpperInvariant(), actual);
}
[Fact]
public void MatchCaseContains_Match()
{
string needle = "test";
List<string> stack = [$"prefix_{needle}_postfix"];
var pm = new PathMatch(needle, matchCase: true);
string? actual = pm.Match(stack);
Assert.Equal($"prefix_{needle}_postfix", actual);
}
[Fact]
public void NoMatchCaseContains_Match()
{
string needle = "test";
List<string> stack = [$"prefix_{needle}_postfix"];
var pm = new PathMatch(needle, matchCase: false);
string? actual = pm.Match(stack);
Assert.Equal($"prefix_{needle}_postfix", actual);
}
#endregion
#region Use Ends With
[Fact]
public void EndsWithEqual_Match()
{
string needle = "test";
List<string> stack = [needle];
var pm = new PathMatch(needle, useEndsWith: true);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void NoEndsWithEqual_Match()
{
string needle = "test";
List<string> stack = [needle];
var pm = new PathMatch(needle, useEndsWith: false);
string? actual = pm.Match(stack);
Assert.Equal(needle, actual);
}
[Fact]
public void EndsWithInequal_Match()
{
string needle = "test";
List<string> stack = [needle.ToUpperInvariant()];
var pm = new PathMatch(needle, useEndsWith: true);
string? actual = pm.Match(stack);
Assert.Equal(needle.ToUpperInvariant(), actual);
}
[Fact]
public void NoEndsWithInequal_Match()
{
string needle = "test";
List<string> stack = [needle.ToUpperInvariant()];
var pm = new PathMatch(needle, useEndsWith: false);
string? actual = pm.Match(stack);
Assert.Equal(needle.ToUpperInvariant(), actual);
}
[Fact]
public void EndsWithContains_NoMatch()
{
string needle = "test";
List<string> stack = [$"prefix_{needle}_postfix"];
var pm = new PathMatch(needle, useEndsWith: true);
string? actual = pm.Match(stack);
Assert.Null(actual);
}
[Fact]
public void NoEndsWithContains_Match()
{
string needle = "test";
List<string> stack = [$"prefix_{needle}_postfix"];
var pm = new PathMatch(needle, useEndsWith: false);
string? actual = pm.Match(stack);
Assert.Equal($"prefix_{needle}_postfix", actual);
}
#endregion
}
}

View File

@@ -0,0 +1,249 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothInt16Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(short le, short be, bool expected)
{
var val = new BothInt16(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
short expected = 1;
var val = new BothInt16(expected, expected);
short to = (short)val;
Assert.Equal(expected, to);
BothInt16 back = (BothInt16)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(short le, int expected)
{
short compare = 1;
var val = new BothInt16(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((short)1).GetTypeCode();
var val = new BothInt16(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothInt16(1, 1);
bool expectedBool = Convert.ToBoolean((short)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((short)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((short)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((short)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((short)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((short)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((short)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((short)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((short)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((short)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((short)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((short)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((short)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((short)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((short)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(short le, short be, bool expected)
{
var val = new BothInt16(le, be);
var equalTo = new BothInt16(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(short le, short be, bool expected)
{
var val = new BothInt16(le, be);
short equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothInt16(2, 2);
short expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt16(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt16(2, 2);
expected = 2;
BothInt16 actual = +valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = -2;
actual = -valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothInt16(3, 3);
var valB = new BothInt16(2, 2);
short expected = 6;
BothInt16 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothInt16(2, 2);
short expected = ~2;
BothInt16 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothInt16(2, 2);
var valB = new BothInt16(1, 1);
short expected = 2 << 1;
BothInt16 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothInt16(3, 3);
var valB = new BothInt16(2, 2);
short expected = 3 & 2;
BothInt16 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,249 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothInt32Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(int le, int be, bool expected)
{
var val = new BothInt32(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
int expected = 1;
var val = new BothInt32(expected, expected);
int to = (int)val;
Assert.Equal(expected, to);
BothInt32 back = (BothInt32)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(int le, int expected)
{
int compare = 1;
var val = new BothInt32(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((int)1).GetTypeCode();
var val = new BothInt32(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothInt32(1, 1);
bool expectedBool = Convert.ToBoolean((int)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((int)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((int)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((int)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((int)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((int)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((int)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((int)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((int)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((int)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((int)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((int)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((int)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((int)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((int)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(int le, int be, bool expected)
{
var val = new BothInt32(le, be);
var equalTo = new BothInt32(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(int le, int be, bool expected)
{
var val = new BothInt32(le, be);
int equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothInt32(2, 2);
int expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt32(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt32(2, 2);
expected = 2;
BothInt32 actual = +valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = -2;
actual = -valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothInt32(3, 3);
var valB = new BothInt32(2, 2);
int expected = 6;
BothInt32 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothInt32(2, 2);
int expected = ~2;
BothInt32 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothInt32(2, 2);
var valB = new BothInt32(1, 1);
int expected = 2 << 1;
BothInt32 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothInt32(3, 3);
var valB = new BothInt32(2, 2);
int expected = 3 & 2;
BothInt32 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,249 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothInt64Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(long le, long be, bool expected)
{
var val = new BothInt64(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
long expected = 1;
var val = new BothInt64(expected, expected);
long to = (long)val;
Assert.Equal(expected, to);
BothInt64 back = (BothInt64)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(long le, int expected)
{
long compare = 1;
var val = new BothInt64(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((long)1).GetTypeCode();
var val = new BothInt64(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothInt64(1, 1);
bool expectedBool = Convert.ToBoolean((long)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((long)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((long)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((long)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((long)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((long)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((long)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((long)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((long)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((long)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((long)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((long)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((long)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((long)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((long)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(long le, long be, bool expected)
{
var val = new BothInt64(le, be);
var equalTo = new BothInt64(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(long le, long be, bool expected)
{
var val = new BothInt64(le, be);
long equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothInt64(2, 2);
long expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt64(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt64(2, 2);
expected = 2;
BothInt64 actual = +valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = -2;
actual = -valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothInt64(3, 3);
var valB = new BothInt64(2, 2);
long expected = 6;
BothInt64 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothInt64(2, 2);
long expected = ~2;
BothInt64 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothInt64(2, 2);
var valB = new BothInt32(1, 1);
long expected = 2 << 1;
BothInt64 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothInt64(3, 3);
var valB = new BothInt64(2, 2);
long expected = 3 & 2;
BothInt64 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,249 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothInt8Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(sbyte le, sbyte be, bool expected)
{
var val = new BothInt8(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
sbyte expected = 1;
var val = new BothInt8(expected, expected);
sbyte to = (sbyte)val;
Assert.Equal(expected, to);
BothInt8 back = (BothInt8)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(sbyte le, int expected)
{
sbyte compare = 1;
var val = new BothInt8(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((sbyte)1).GetTypeCode();
var val = new BothInt8(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothInt8(1, 1);
bool expectedBool = Convert.ToBoolean((sbyte)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((sbyte)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((sbyte)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((sbyte)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((sbyte)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((sbyte)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((sbyte)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((sbyte)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((sbyte)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((sbyte)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((sbyte)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((sbyte)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((sbyte)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((sbyte)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((sbyte)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(sbyte le, sbyte be, bool expected)
{
var val = new BothInt8(le, be);
var equalTo = new BothInt8(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(sbyte le, sbyte be, bool expected)
{
var val = new BothInt8(le, be);
sbyte equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothInt8(2, 2);
sbyte expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt8(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothInt8(2, 2);
expected = 2;
BothInt8 actual = +valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = -2;
actual = -valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothInt8(3, 3);
var valB = new BothInt8(2, 2);
sbyte expected = 6;
BothInt8 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothInt8(2, 2);
sbyte expected = ~2;
BothInt8 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothInt8(2, 2);
var valB = new BothInt8(1, 1);
sbyte expected = 2 << 1;
BothInt8 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothInt8(3, 3);
var valB = new BothInt8(2, 2);
sbyte expected = 3 & 2;
BothInt8 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,238 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothUInt16Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(ushort le, ushort be, bool expected)
{
var val = new BothUInt16(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
ushort expected = 1;
var val = new BothUInt16(expected, expected);
ushort to = (ushort)val;
Assert.Equal(expected, to);
BothUInt16 back = (BothUInt16)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(ushort le, int expected)
{
ushort compare = 1;
var val = new BothUInt16(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((ushort)1).GetTypeCode();
var val = new BothUInt16(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothUInt16(1, 1);
bool expectedBool = Convert.ToBoolean((ushort)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((ushort)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((ushort)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((ushort)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((ushort)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((ushort)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((ushort)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((ushort)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((ushort)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((ushort)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((ushort)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((ushort)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((ushort)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((ushort)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((ushort)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(ushort le, ushort be, bool expected)
{
var val = new BothUInt16(le, be);
var equalTo = new BothUInt16(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(ushort le, ushort be, bool expected)
{
var val = new BothUInt16(le, be);
ushort equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothUInt16(2, 2);
ushort expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothUInt16(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothUInt16(3, 3);
var valB = new BothUInt16(2, 2);
ushort expected = 6;
BothUInt16 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothUInt16(2, 2);
ushort expected = 65533;
BothUInt16 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothUInt16(2, 2);
var valB = new BothUInt16(1, 1);
ushort expected = 2 << 1;
BothUInt16 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothUInt16(3, 3);
var valB = new BothUInt16(2, 2);
ushort expected = 3 & 2;
BothUInt16 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,238 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothUInt32Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(uint le, uint be, bool expected)
{
var val = new BothUInt32(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
uint expected = 1;
var val = new BothUInt32(expected, expected);
uint to = (uint)val;
Assert.Equal(expected, to);
BothUInt32 back = (BothUInt32)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(uint le, int expected)
{
uint compare = 1;
var val = new BothUInt32(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((uint)1).GetTypeCode();
var val = new BothUInt32(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothUInt32(1, 1);
bool expectedBool = Convert.ToBoolean((uint)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((uint)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((uint)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((uint)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((uint)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((uint)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((uint)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((uint)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((uint)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((uint)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((uint)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((uint)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((uint)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((uint)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((uint)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(uint le, uint be, bool expected)
{
var val = new BothUInt32(le, be);
var equalTo = new BothUInt32(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(uint le, uint be, bool expected)
{
var val = new BothUInt32(le, be);
uint equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothUInt32(2, 2);
uint expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothUInt32(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothUInt32(3, 3);
var valB = new BothUInt32(2, 2);
uint expected = 6;
BothUInt32 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothUInt32(2, 2);
uint expected = ~((uint)2);
BothUInt32 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothUInt32(2, 2);
var valB = new BothInt32(1, 1);
uint expected = 2 << 1;
BothUInt32 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothUInt32(3, 3);
var valB = new BothUInt32(2, 2);
uint expected = 3 & 2;
BothUInt32 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,238 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothUInt64Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(ulong le, ulong be, bool expected)
{
var val = new BothUInt64(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
ulong expected = 1;
var val = new BothUInt64(expected, expected);
ulong to = (ulong)val;
Assert.Equal(expected, to);
BothUInt64 back = (BothUInt64)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(ulong le, int expected)
{
ulong compare = 1;
var val = new BothUInt64(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((ulong)1).GetTypeCode();
var val = new BothUInt64(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothUInt64(1, 1);
bool expectedBool = Convert.ToBoolean((ulong)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((ulong)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((ulong)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((ulong)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((ulong)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((ulong)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((ulong)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((ulong)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((ulong)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((ulong)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((ulong)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((ulong)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((ulong)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((ulong)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((ulong)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(ulong le, ulong be, bool expected)
{
var val = new BothUInt64(le, be);
var equalTo = new BothUInt64(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(ulong le, ulong be, bool expected)
{
var val = new BothUInt64(le, be);
ulong equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothUInt64(2, 2);
ulong expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothUInt64(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothUInt64(3, 3);
var valB = new BothUInt64(2, 2);
ulong expected = 6;
BothUInt64 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothUInt64(2, 2);
ulong expected = ~((ulong)2);
BothUInt64 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothUInt64(2, 2);
var valB = new BothInt32(1, 1);
ulong expected = 2 << 1;
BothUInt64 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothUInt64(3, 3);
var valB = new BothUInt64(2, 2);
ulong expected = 3 & 2;
BothUInt64 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

View File

@@ -0,0 +1,238 @@
using System;
using SabreTools.Numerics;
using Xunit;
namespace SabreTools.IO.Test.Numerics
{
public class BothUInt8Tests
{
[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 1, false)]
public void IsValidTest(byte le, byte be, bool expected)
{
var val = new BothUInt8(le, be);
Assert.Equal(le, val.LittleEndian);
Assert.Equal(be, val.BigEndian);
Assert.Equal(expected, val.IsValid);
}
[Fact]
public void ImplicitConversionTest()
{
byte expected = 1;
var val = new BothUInt8(expected, expected);
byte to = (byte)val;
Assert.Equal(expected, to);
BothUInt8 back = (BothUInt8)to;
Assert.Equal(expected, back.LittleEndian);
Assert.Equal(expected, back.BigEndian);
}
[Theory]
[InlineData(0, -1)]
[InlineData(1, 0)]
[InlineData(2, 1)]
public void CompareToTest(byte le, int expected)
{
byte compare = 1;
var val = new BothUInt8(le, le);
int actual = val.CompareTo(compare);
Assert.Equal(expected, actual);
}
[Fact]
public void GetTypeCodeTest()
{
TypeCode expected = ((byte)1).GetTypeCode();
var val = new BothUInt8(1, 1);
Assert.Equal(expected, val.GetTypeCode());
}
[Fact]
public void ToTypesTest()
{
var val = new BothUInt8(1, 1);
bool expectedBool = Convert.ToBoolean((byte)1);
Assert.Equal(expectedBool, val.ToBoolean(null));
char expectedChar = Convert.ToChar((byte)1);
Assert.Equal(expectedChar, val.ToChar(null));
sbyte expectedSByte = Convert.ToSByte((byte)1);
Assert.Equal(expectedSByte, val.ToSByte(null));
byte expectedByte = Convert.ToByte((byte)1);
Assert.Equal(expectedByte, val.ToByte(null));
short expectedInt16 = Convert.ToInt16((byte)1);
Assert.Equal(expectedInt16, val.ToInt16(null));
ushort expectedUInt16 = Convert.ToUInt16((byte)1);
Assert.Equal(expectedUInt16, val.ToUInt16(null));
int expectedInt32 = Convert.ToInt32((byte)1);
Assert.Equal(expectedInt32, val.ToInt32(null));
uint expectedUInt32 = Convert.ToUInt32((byte)1);
Assert.Equal(expectedUInt32, val.ToUInt32(null));
long expectedInt64 = Convert.ToInt64((byte)1);
Assert.Equal(expectedInt64, val.ToInt64(null));
ulong expectedUInt64 = Convert.ToUInt64((byte)1);
Assert.Equal(expectedUInt64, val.ToUInt64(null));
float expectedSingle = Convert.ToSingle((byte)1);
Assert.Equal(expectedSingle, val.ToSingle(null));
double expectedDouble = Convert.ToDouble((byte)1);
Assert.Equal(expectedDouble, val.ToDouble(null));
decimal expectedDecimal = Convert.ToDecimal((byte)1);
Assert.Equal(expectedDecimal, val.ToDecimal(null));
Assert.Throws<InvalidCastException>(() => val.ToDateTime(null));
string expectedString = Convert.ToString((byte)1);
Assert.Equal(expectedString, val.ToString(null));
ulong expectedObject = Convert.ToUInt64((byte)1);
Assert.Equal(expectedObject, val.ToType(typeof(ulong), null));
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(0, 1, false)]
[InlineData(1, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BothEndian(byte le, byte be, bool expected)
{
var val = new BothUInt8(le, be);
var equalTo = new BothUInt8(1, 1);
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, false)]
[InlineData(1, 1, true)]
public void Equals_BaseType(byte le, byte be, bool expected)
{
var val = new BothUInt8(le, be);
byte equalTo = 1;
bool actual = val.Equals(equalTo);
Assert.Equal(expected, actual);
}
[Fact]
public void ArithmeticUnaryOperatorsTest()
{
var valA = new BothUInt8(2, 2);
byte expected = 3;
valA++;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
valA = new BothUInt8(2, 2);
expected = 1;
valA--;
Assert.Equal(expected, valA.LittleEndian);
Assert.Equal(expected, valA.BigEndian);
}
[Fact]
public void ArithmeticBinaryOperatorsTest()
{
var valA = new BothUInt8(3, 3);
var valB = new BothUInt8(2, 2);
byte expected = 6;
BothUInt8 actual = valA * valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA / valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA % valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 5;
actual = valA + valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 1;
actual = valA - valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseUnaryOperatorsTest()
{
var valA = new BothUInt8(2, 2);
byte expected = 253;
BothUInt8 actual = ~valA;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void ShiftBinaryOperatorsTest()
{
var valA = new BothUInt8(2, 2);
var valB = new BothUInt8(1, 1);
byte expected = 2 << 1;
BothUInt8 actual = valA << valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >> 1;
actual = valA >> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 2 >>> 1;
actual = valA >>> valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
[Fact]
public void BitwiseBinaryOperatorsTest()
{
var valA = new BothUInt8(3, 3);
var valB = new BothUInt8(2, 2);
byte expected = 3 & 2;
BothUInt8 actual = valA & valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 | 2;
actual = valA | valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
expected = 3 ^ 2;
actual = valA ^ valB;
Assert.Equal(expected, actual.LittleEndian);
Assert.Equal(expected, actual.BigEndian);
}
}
}

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,9 +24,9 @@
<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.4">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

View File

@@ -0,0 +1,40 @@
using System.IO;
using Xunit;
namespace SabreTools.IO.Test.Streams
{
public class BufferedStreamTests
{
#region ReadNextByte
[Fact]
public void ReadNextByte_Empty_Null()
{
var source = new MemoryStream();
var stream = new IO.Streams.BufferedStream(source);
byte? actual = stream.ReadNextByte();
Assert.Null(actual);
}
[Fact]
public void ReadNextByte_Filled_ValidPosition_Byte()
{
var source = new MemoryStream(new byte[1024]);
var stream = new IO.Streams.BufferedStream(source);
byte? actual = stream.ReadNextByte();
Assert.Equal((byte)0x00, actual);
}
[Fact]
public void ReadNextByte_Filled_InvalidPosition_Null()
{
var source = new MemoryStream(new byte[1024]);
source.Seek(0, SeekOrigin.End);
var stream = new IO.Streams.BufferedStream(source);
byte? actual = stream.ReadNextByte();
Assert.Null(actual);
}
#endregion
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.IO;
using SabreTools.IO.Extensions;
using SabreTools.IO.Streams;
using Xunit;
@@ -416,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

@@ -0,0 +1,92 @@
/*
*
* Links for info and original source code:
*
* https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
* http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer
*
* Exact code implementation used with permission, originally by motoschifo
*
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SabreTools.Text.Compare
{
public class NaturalComparer : Comparer<string>, IDisposable
{
private readonly Dictionary<string, string[]> _table;
public NaturalComparer()
{
_table = [];
}
public void Dispose()
{
_table.Clear();
}
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
{
if (x == null && y != null)
return -1;
else if (x != null && y == null)
return 1;
else
return 0;
}
if (x.ToLowerInvariant() == y.ToLowerInvariant())
return x.CompareTo(y);
if (!_table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)");
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
_table.Add(x, x1);
}
if (!_table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
_table.Add(y, y1);
}
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
return PartCompare(x1[i], y1[i]);
}
if (x1.Length > y1.Length)
return 1;
else if (y1.Length > x1.Length)
return -1;
else
return x.CompareTo(y);
}
private static int PartCompare(string left, string right)
{
if (!long.TryParse(left, out long x))
return NaturalComparerUtil.ComparePaths(left, right);
if (!long.TryParse(right, out long y))
return NaturalComparerUtil.ComparePaths(left, right);
// If we have an equal part, then make sure that "longer" ones are taken into account
if (x.CompareTo(y) == 0)
return left.Length - right.Length;
return x.CompareTo(y);
}
}
}

View File

@@ -0,0 +1,96 @@
namespace SabreTools.Text.Compare
{
internal static class NaturalComparerUtil
{
/// <summary>
/// Compare two strings by path parts
/// </summary>
public static int ComparePaths(string? left, string? right)
{
// If both strings are null, return
if (left == null && right == null)
return 0;
// If one is null, then say that's less than
if (left == null)
return -1;
if (right == null)
return 1;
// Normalize the path seperators
left = left.Replace('\\', '/');
right = right.Replace('\\', '/');
// Save the orginal adjusted strings
string leftOrig = left;
string rightOrig = right;
// Normalize strings by lower-case
left = leftOrig.ToLowerInvariant();
right = rightOrig.ToLowerInvariant();
// If the strings are the same exactly, return
if (left == right)
return leftOrig.CompareTo(rightOrig);
// Now split into path parts
string[] leftParts = left.Split('/');
string[] rightParts = right.Split('/');
// Then compare each part in turn
for (int i = 0; i < leftParts.Length && i < rightParts.Length; i++)
{
int partCompare = ComparePathSegment(leftParts[i], rightParts[i]);
if (partCompare != 0)
return partCompare;
}
// If we got out here, then it looped through at least one of the strings
if (leftParts.Length > rightParts.Length)
return 1;
if (leftParts.Length < rightParts.Length)
return -1;
return leftOrig.CompareTo(rightOrig);
}
/// <summary>
/// Compare two path segments deterministically
/// </summary>
private static int ComparePathSegment(string left, string right)
{
// If the lengths are both zero, they're equal
if (left.Length == 0 && right.Length == 0)
return 0;
// Shorter strings are sorted before
if (left.Length == 0)
return -1;
if (right.Length == 0)
return 1;
// Otherwise, loop through until we have an answer
for (int i = 0; i < left.Length && i < right.Length; i++)
{
// Get the next characters from the inputs as integers
int leftChar = left[i];
int rightChar = right[i];
// If the characters are the same, continue
if (leftChar == rightChar)
continue;
// If they're different, check which one was larger
return leftChar > rightChar ? 1 : -1;
}
// If we got out here, then it looped through at least one of the strings
if (left.Length > right.Length)
return 1;
if (left.Length < right.Length)
return -1;
return 0;
}
}
}

View File

@@ -0,0 +1,92 @@
/*
*
* Links for info and original source code:
*
* https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
* http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer
*
* Exact code implementation used with permission, originally by motoschifo
*
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace SabreTools.Text.Compare
{
public class NaturalReversedComparer : Comparer<string>, IDisposable
{
private readonly Dictionary<string, string[]> _table;
public NaturalReversedComparer()
{
_table = [];
}
public void Dispose()
{
_table.Clear();
}
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
{
if (x == null && y != null)
return -1;
else if (x != null && y == null)
return 1;
else
return 0;
}
if (y.ToLowerInvariant() == x.ToLowerInvariant())
return y.CompareTo(x);
if (!_table.TryGetValue(x, out string[]? x1))
{
//x1 = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)");
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)");
x1 = Array.FindAll(x1, s => !string.IsNullOrEmpty(s));
_table.Add(x, x1);
}
if (!_table.TryGetValue(y, out string[]? y1))
{
//y1 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)");
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)");
y1 = Array.FindAll(y1, s => !string.IsNullOrEmpty(s));
_table.Add(y, y1);
}
for (int i = 0; i < x1.Length && i < y1.Length; i++)
{
if (x1[i] != y1[i])
return PartCompare(x1[i], y1[i]);
}
if (y1.Length > x1.Length)
return 1;
else if (x1.Length > y1.Length)
return -1;
else
return y.CompareTo(x);
}
private static int PartCompare(string left, string right)
{
if (!long.TryParse(left, out long x))
return NaturalComparerUtil.ComparePaths(right, left);
if (!long.TryParse(right, out long y))
return NaturalComparerUtil.ComparePaths(right, left);
// If we have an equal part, then make sure that "longer" ones are taken into account
if (y.CompareTo(x) == 0)
return right.Length - left.Length;
return y.CompareTo(x);
}
}
}

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

@@ -3,8 +3,6 @@ using System.IO;
using System.Text;
using SabreTools.Hashing;
using SabreTools.IO.Extensions;
using SabreTools.Models.PKZIP;
using static SabreTools.Models.PKZIP.Constants;
namespace SabreTools.IO.Compression.Deflate
{
@@ -20,6 +18,46 @@ namespace SabreTools.IO.Compression.Deflate
/// </summary>
private const int BufferSize = 1024 * 1024;
/// <summary>
/// Local file header signature
/// </summary>
private const uint LocalFileHeaderSignature = 0x04034B50;
#endregion
#region Private Classes
/// <summary>
/// Minimal PKZIP local file header information
/// </summary>
private class MinLocalFileHeader
{
/// <summary>
/// Signature (0x04034B50)
/// </summary>
public uint Signature { get; set; }
/// <summary>
/// CRC-32
/// </summary>
public uint CRC32 { get; set; }
/// <summary>
/// Compressed size
/// </summary>
public uint CompressedSize { get; set; }
/// <summary>
/// Uncompressed size
/// </summary>
public uint UncompressedSize { get; set; }
/// <summary>
/// File name (variable size)
/// </summary>
public string? FileName { get; set; }
}
#endregion
#region Extraction
@@ -140,7 +178,7 @@ namespace SabreTools.IO.Compression.Deflate
long current = source.Position;
// Parse the PKZIP header, if it exists
LocalFileHeader? zipHeader = ParseLocalFileHeader(source);
MinLocalFileHeader? zipHeader = ParseLocalFileHeader(source);
long zipHeaderBytes = source.Position - current;
// Always trust the PKZIP CRC-32 value over what is supplied
@@ -269,46 +307,39 @@ namespace SabreTools.IO.Compression.Deflate
}
/// <summary>
/// Parse a Stream into a local file header
/// Parse a Stream into a minimal local file header
/// </summary>
/// <param name="data">Stream to parse</param>
/// <returns>Filled local file header on success, null on error</returns>
/// <remarks>Mirror of method in Serialization</remarks>
private static LocalFileHeader? ParseLocalFileHeader(Stream data)
/// <returns>Filled minimal local file header on success, null on error</returns>
/// <remarks>Partial mirror of method in Serialization</remarks>
private static MinLocalFileHeader? ParseLocalFileHeader(Stream data)
{
var header = new LocalFileHeader();
var header = new MinLocalFileHeader();
header.Signature = data.ReadUInt32LittleEndian();
if (header.Signature != LocalFileHeaderSignature)
return null;
header.Version = data.ReadUInt16LittleEndian();
header.Flags = (GeneralPurposeBitFlags)data.ReadUInt16LittleEndian();
header.CompressionMethod = (CompressionMethod)data.ReadUInt16LittleEndian();
header.LastModifedFileTime = data.ReadUInt16LittleEndian();
header.LastModifiedFileDate = data.ReadUInt16LittleEndian();
_ = data.ReadUInt16LittleEndian(); // Version
_ = data.ReadUInt16LittleEndian(); // Flags
_ = data.ReadUInt16LittleEndian(); // CompressionMethod
_ = data.ReadUInt16LittleEndian(); // LastModifedFileTime
_ = data.ReadUInt16LittleEndian(); // LastModifiedFileDate
header.CRC32 = data.ReadUInt32LittleEndian();
header.CompressedSize = data.ReadUInt32LittleEndian();
header.UncompressedSize = data.ReadUInt32LittleEndian();
header.FileNameLength = data.ReadUInt16LittleEndian();
header.ExtraFieldLength = data.ReadUInt16LittleEndian();
ushort fileNameLength = data.ReadUInt16LittleEndian();
ushort extraFieldLength = data.ReadUInt16LittleEndian();
if (header.FileNameLength > 0 && data.Position + header.FileNameLength <= data.Length)
if (fileNameLength > 0 && data.Position + fileNameLength <= data.Length)
{
byte[] filenameBytes = data.ReadBytes(header.FileNameLength);
if (filenameBytes.Length != header.FileNameLength)
return null;
byte[] filenameBytes = data.ReadBytes(fileNameLength);
header.FileName = Encoding.ASCII.GetString(filenameBytes);
}
// Parsing extras is skipped here, unlike in Serialization
if (header.ExtraFieldLength > 0 && data.Position + header.ExtraFieldLength <= data.Length)
{
byte[] extraBytes = data.ReadBytes(header.ExtraFieldLength);
if (extraBytes.Length != header.ExtraFieldLength)
return null;
}
if (extraFieldLength > 0 && data.Position + extraFieldLength <= data.Length)
_ = data.ReadBytes(extraFieldLength);
return header;
}
@@ -436,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

@@ -0,0 +1,58 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// An aligned offset block is identical to the verbatim block except for the presence of the aligned offset
/// tree preceding the other trees.
/// </summary>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal class AlignedOffsetBlockData : BlockData
{
/// <summary>
/// Aligned offset tree
/// </summary>
/// <remarks>8 elements, 3 bits each</remarks>
public byte[]? AlignedOffsetTree { get; set; }
/// <summary>
/// Pretree for first 256 elements of main tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeFirst256 { get; set; }
/// <summary>
/// Path lengths of first 256 elements of main tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsFirst256 { get; set; }
/// <summary>
/// Pretree for remainder of main tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeRemainder { get; set; }
/// <summary>
/// Path lengths of remaining elements of main tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsRemainder { get; set; }
/// <summary>
/// Pretree for length tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeLengthTree { get; set; }
/// <summary>
/// Path lengths of elements in length tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsLengthTree { get; set; }
/// <summary>
/// Token sequence (matches and literals)
/// </summary>
/// <remarks>Variable</remarks>
public byte[]? TokenSequence { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// An LZXD block represents a sequence of compressed data that is encoded with the same set of
/// Huffman trees, or a sequence of uncompressed data. There can be one or more LZXD blocks in a
/// compressed stream, each with its own set of Huffman trees. Blocks do not have to start or end on a
/// chunk boundary; blocks can span multiple chunks, or a single chunk can contain multiple blocks. The
/// number of chunks is related to the size of the data being compressed, while the number of blocks is
/// related to how well the data is compressed.
/// </summary>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal class Block
{
/// <summary>
/// Block header
/// </summary>
public BlockHeader? Header { get; set; }
/// <summary>
/// Block data
/// </summary>
public BlockData? BlockData { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace SabreTools.IO.Compression.LZX
{
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal abstract class BlockData
{
// No common fields between all block data
}
}

View File

@@ -0,0 +1,33 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// The Block Type field, as specified in section 2.3.1.1, indicates which type of block follows,
/// and the Block Size field, as specified in section 2.3.1.2, indicates the number of
/// uncompressed bytes represented by the block. Following the generic block
/// header is a type-specific header that describes the remainder of the block.
/// </summary>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal class BlockHeader
{
/// <remarks>3 bits</remarks>
public BlockType BlockType { get; set; }
/// <summary>
/// Block size is the high 8 bits of 24
/// </summary>
/// <remarks>8 bits</remarks>
public byte BlockSizeMSB { get; set; }
/// <summary>
/// Block size is the middle 8 bits of 24
/// </summary>
/// <remarks>8 bits</remarks>
public byte BlockSizeByte2 { get; set; }
/// <summary>
/// Block size is the low 8 bits of 24
/// </summary>
/// <remarks>8 bits</remarks>
public byte BlocksizeLSB { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// The LZXD compressor emits chunks of compressed data. A chunk represents exactly 32 KB of
/// uncompressed data until the last chunk in the stream, which can represent less than 32 KB. To
/// ensure that an exact number of input bytes represent an exact number of output bytes for each
/// chunk, after each 32 KB of uncompressed data is represented in the output compressed bitstream, the
/// output bitstream is padded with up to 15 bits of zeros to realign the bitstream on a 16-bit boundary
/// (even byte boundary) for the next 32 KB of data. This results in a compressed chunk of a byte-aligned
/// size. The compressed chunk could be smaller than 32 KB or larger than 32 KB if the data is
/// incompressible when the chunk is not the last one.
/// </summary>
internal class Chunk
{
/// <summary>
/// Chunk header
/// </summary>
public ChunkHeader? Header { get; set; }
/// <summary>
/// Block headers and data
/// </summary>
public Block[]? Blocks { get; set; }
}
}

View File

@@ -0,0 +1,46 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// The LZXD compressor emits chunks of compressed data. A chunk represents exactly 32 KB of
/// uncompressed data until the last chunk in the stream, which can represent less than 32 KB. To
/// ensure that an exact number of input bytes represent an exact number of output bytes for each
/// chunk, after each 32 KB of uncompressed data is represented in the output compressed bitstream, the
/// output bitstream is padded with up to 15 bits of zeros to realign the bitstream on a 16-bit boundary
/// (even byte boundary) for the next 32 KB of data. This results in a compressed chunk of a byte-aligned
/// size. The compressed chunk could be smaller than 32 KB or larger than 32 KB if the data is
/// incompressible when the chunk is not the last one.
/// </summary>
internal class ChunkHeader
{
/// <summary>
/// The LZXD engine encodes a compressed, chunk-size prefix field preceding each compressed chunk in
/// the compressed byte stream. The compressed, chunk-size prefix field is a byte aligned, little-endian,
/// 16-bit field. The chunk prefix chain could be followed in the compressed stream without
/// decompressing any data. The next chunk prefix is at a location computed by the absolute byte offset
/// location of this chunk prefix plus 2 (for the size of the chunk-size prefix field) plus the current chunk
/// size.
/// </summary>
public ushort ChunkSize { get; set; }
/// <summary>
/// The first bit in the first chunk in the LZXD bitstream (following the 2-byte, chunk-size prefix described
/// in section 2.2.1) indicates the presence or absence of two 16-bit fields immediately following the
/// single bit. If the bit is set, E8 translation is enabled for all the following chunks in the stream using the
/// 32-bit value derived from the two 16-bit fields as the E8_file_size provided to the compressor when E8
/// translation was enabled. Note that E8_file_size is completely independent of the length of the
/// uncompressed data. E8 call translation is disabled after the 32,768th chunk (after 1 gigabyte (GB) of
/// uncompressed data).
/// </summary>
public byte E8Translation { get; set; }
/// <summary>
/// E8 translation size, high WORD
/// </summary>
public ushort? TranslationSizeHighWord { get; set; }
/// <summary>
/// E8 translation size, low WORD
/// </summary>
public ushort? TranslationSizeLowWord { get; set; }
}
}

View File

@@ -0,0 +1,38 @@
namespace SabreTools.IO.Compression.LZX
{
internal static class Constants
{
/* some constants defined by the LZX specification */
public const int LZX_MIN_MATCH = 2;
public const int LZX_MAX_MATCH = 257;
public const int LZX_NUM_CHARS = 256;
public const int LZX_PRETREE_NUM_ELEMENTS = 20;
/// <summary>
/// aligned offset tree #elements
/// </summary>
public const int LZX_ALIGNED_NUM_ELEMENTS = 8;
/// <summary>
/// this one missing from spec!
/// </summary>
public const int LZX_NUM_PRIMARY_LENGTHS = 7;
/// <summary>
/// length tree #elements
/// </summary>
public const int LZX_NUM_SECONDARY_LENGTHS = 249;
/* LZX huffman defines: tweak tablebits as desired */
public const int LZX_PRETREE_MAXSYMBOLS = LZX_PRETREE_NUM_ELEMENTS;
public const int LZX_PRETREE_TABLEBITS = 6;
public const int LZX_MAINTREE_MAXSYMBOLS = LZX_NUM_CHARS + 50 * 8;
public const int LZX_MAINTREE_TABLEBITS = 12;
public const int LZX_LENGTH_MAXSYMBOLS = LZX_NUM_SECONDARY_LENGTHS + 1;
public const int LZX_LENGTH_TABLEBITS = 12;
public const int LZX_ALIGNED_MAXSYMBOLS = LZX_ALIGNED_NUM_ELEMENTS;
public const int LZX_ALIGNED_TABLEBITS = 7;
public const int LZX_LENTABLE_SAFETY = 64; /* we allow length table decoding overruns */
}
}

View File

@@ -0,0 +1,48 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// 3-bit block type
/// </summary>
internal enum BlockType : byte
{
/// <summary>
/// Not valid
/// </summary>
INVALID_0 = 0b000,
/// <summary>
/// Verbatim block
/// </summary>
Verbatim = 0b001,
/// <summary>
/// Aligned offset block
/// </summary>
AlignedOffset = 0b010,
/// <summary>
/// Uncompressed block
/// </summary>
Uncompressed = 0b011,
/// <summary>
/// Not valid
/// </summary>
INVALID_4 = 0b100,
/// <summary>
/// Not valid
/// </summary>
INVALID_5 = 0b101,
/// <summary>
/// Not valid
/// </summary>
INVALID_6 = 0b110,
/// <summary>
/// Not valid
/// </summary>
INVALID_7 = 0b111,
}
}

View File

@@ -0,0 +1,54 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// Following the generic block header, an uncompressed block begins with 1 to 16 bits of zero padding
/// to align the bit buffer on a 16-bit boundary. At this point, the bitstream ends and a byte stream
/// 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>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal class UncompressedBlockData : BlockData
{
/// <summary>
/// Padding to align following field on 16-bit boundary
/// </summary>
/// <remarks>Bits have a value of zero</remarks>
public ushort PaddingBits { get; set; }
/// <summary>
/// Least significant to most significant byte (little-endian DWORD ([MS-DTYP]))
/// </summary>
/// <remarks>Encoded directly in the byte stream, not in the bitstream of byte-swapped 16-bit words</remarks>
public uint R0 { get; set; }
/// <summary>
/// Least significant to most significant byte (little-endian DWORD)
/// </summary>
/// <remarks>Encoded directly in the byte stream, not in the bitstream of byte-swapped 16-bit words</remarks>
public uint R1 { get; set; }
/// <summary>
/// Least significant to most significant byte (little-endian DWORD)
/// </summary>
/// <remarks>Encoded directly in the byte stream, not in the bitstream of byte-swapped 16-bit words</remarks>
public uint R2 { get; set; }
/// <summary>
/// Can use the direct memcpy function, as specified in [IEEE1003.1]
/// </summary>
/// <remarks>Encoded directly in the byte stream, not in the bitstream of byte-swapped 16-bit words</remarks>
public byte[]? RawDataBytes { get; set; }
/// <summary>
/// Only if uncompressed size is odd
/// </summary>
public byte AlignmentByte { get; set; }
}
}

View File

@@ -0,0 +1,51 @@
namespace SabreTools.IO.Compression.LZX
{
/// <summary>
/// The fields of a verbatim block that follow the generic block header
/// </summary>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-PATCH/%5bMS-PATCH%5d.pdf"/>
internal class VerbatimBlockData : BlockData
{
/// <summary>
/// Pretree for first 256 elements of main tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeFirst256 { get; set; }
/// <summary>
/// Path lengths of first 256 elements of main tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsFirst256 { get; set; }
/// <summary>
/// Pretree for remainder of main tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeRemainder { get; set; }
/// <summary>
/// Path lengths of remaining elements of main tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsRemainder { get; set; }
/// <summary>
/// Pretree for length tree
/// </summary>
/// <remarks>20 elements, 4 bits each</remarks>
public byte[]? PretreeLengthTree { get; set; }
/// <summary>
/// Path lengths of elements in length tree
/// </summary>
/// <remarks>Encoded using pretree</remarks>
public int[]? PathLengthsLengthTree { get; set; }
/// <summary>
/// Token sequence (matches and literals)
/// </summary>
/// <remarks>Variable</remarks>
public byte[]? TokenSequence { get; set; }
}
}

View File

@@ -0,0 +1,28 @@
namespace SabreTools.IO.Compression.MSZIP
{
/// <summary>
/// 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
/// MSZIP block is determined by the compressor, as defined in [RFC1951]. The last RFC 1951 block in
/// 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.
/// </summary>
/// <see href="https://interoperability.blob.core.windows.net/files/MS-MCI/%5bMS-MCI%5d.pdf"/>
internal class BlockHeader
{
/// <summary>
/// 'CK'
/// </summary>
public ushort Signature { get; set; }
}
}

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using SabreTools.IO.Extensions;
using SabreTools.Models.Compression.MSZIP;
namespace SabreTools.IO.Compression.MSZIP
{

View File

@@ -0,0 +1,50 @@
namespace SabreTools.IO.Compression.Quantum
{
/// <see href="http://www.russotto.net/quantumcomp.html"/>
internal static class Constants
{
public static readonly int[] PositionSlot =
[
0x00000, 0x00001, 0x00002, 0x00003, 0x00004, 0x00006, 0x00008, 0x0000c,
0x00010, 0x00018, 0x00020, 0x00030, 0x00040, 0x00060, 0x00080, 0x000c0,
0x00100, 0x00180, 0x00200, 0x00300, 0x00400, 0x00600, 0x00800, 0x00c00,
0x01000, 0x01800, 0x02000, 0x03000, 0x04000, 0x06000, 0x08000, 0x0c000,
0x10000, 0x18000, 0x20000, 0x30000, 0x40000, 0x60000, 0x80000, 0xc0000,
0x100000, 0x180000
];
public static readonly int[] PositionExtraBits =
[
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, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18,
19, 19
];
public static readonly int[] LengthSlot =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
0x0a, 0x0c, 0x0e, 0x12, 0x16, 0x1a, 0x1e, 0x26,
0x2e, 0x36, 0x3e, 0x4e, 0x5e, 0x6e, 0x7e, 0x9e,
0xbe, 0xde, 0xfe
];
public static readonly int[] LengthExtraBits =
[
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
];
/// <summary>
/// Number of position slots for (tsize - 10)
/// </summary>
public static readonly int[] NumPositionSlots =
[
20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42
];
}
}

View File

@@ -2,12 +2,11 @@ using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Streams;
using SabreTools.Models.Compression.Quantum;
using static SabreTools.Models.Compression.Quantum.Constants;
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>
@@ -363,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

@@ -0,0 +1,45 @@
namespace SabreTools.IO.Compression.Quantum
{
internal enum SelectorModel
{
/// <summary>
/// Literal model, 64 entries, start at symbol 0
/// </summary>
SELECTOR_0 = 0,
/// <summary>
/// Literal model, 64 entries, start at symbol 64
/// </summary>
SELECTOR_1 = 1,
/// <summary>
/// Literal model, 64 entries, start at symbol 128
/// </summary>
SELECTOR_2 = 2,
/// <summary>
/// Literal model, 64 entries, start at symbol 192
/// </summary>
SELECTOR_3 = 3,
/// <summary>
/// LZ model, 3 character matches, max 24 entries, start at symbol 0
/// </summary>
SELECTOR_4 = 4,
/// <summary>
/// LZ model, 4 character matches, max 36 entries, start at symbol 0
/// </summary>
SELECTOR_5 = 5,
/// <summary>
/// LZ model, 5+ character matches, max 42 entries, start at symbol 0
/// </summary>
SELECTOR_6_POSITION = 6,
/// <summary>
/// LZ model, 5+ character matches, 27 entries, start at symbol 0
/// </summary>
SELECTOR_6_LENGTH = 7,
}
}

View File

@@ -0,0 +1,24 @@
namespace SabreTools.IO.Compression.Quantum
{
/// <see href="http://www.russotto.net/quantumcomp.html"/>
internal sealed class Model
{
public int Entries { get; set; }
/// <remarks>
/// All the models are initialized with the symbols in symbol
/// order in the table, and with every symbol in the table
/// having a frequency of 1
/// </remarks>
public ModelSymbol[]? Symbols { get; set; }
/// <remarks>
/// The initial total frequency is equal to the number of entries
/// in the table
/// </remarks>
public int TotalFrequency { get; set; }
/// <remarks>The initial time_to_reorder value is 4</remarks>
public int TimeToReorder { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
namespace SabreTools.IO.Compression.Quantum
{
/// <see href="http://www.russotto.net/quantumcomp.html"/>
internal sealed class ModelSymbol
{
public ushort Symbol { get; set; }
/// <summary>
/// The cumulative frequency is the frequency of all the symbols
/// which are at a higher index in the table than that symbol —
/// thus the last entry in the table has a cumulative frequency of 0.
/// </summary>
public ushort CumulativeFrequency { get; set; }
}
}

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