Add editorconfig, fix issues

This commit is contained in:
Matt Nadareski
2026-01-25 17:04:11 -05:00
parent 6bd77c7377
commit b3e410180a
90 changed files with 10437 additions and 10105 deletions

167
.editorconfig Normal file
View File

@@ -0,0 +1,167 @@
# top-most EditorConfig file
root = true
# C# files
[*.cs]
# Indentation and spacing
charset = utf-8
indent_size = 4
indent_style = space
tab_width = 4
trim_trailing_whitespace = true
# New line preferences
end_of_line = lf
insert_final_newline = true
max_line_length = unset
# using directive preferences
csharp_using_directive_placement = outside_namespace
dotnet_diagnostic.IDE0005.severity = error
# Code-block preferences
csharp_style_namespace_declarations = block_scoped
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_top_level_statements = false
# Expression-level preferences
csharp_prefer_simple_default_expression = true
csharp_style_inlined_variable_declaration = true
csharp_style_unused_value_assignment_preference = discard_variable
csharp_style_unused_value_expression_statement_preference = discard_variable
dotnet_diagnostic.IDE0001.severity = warning
dotnet_diagnostic.IDE0002.severity = warning
dotnet_diagnostic.IDE0004.severity = warning
dotnet_diagnostic.IDE0010.severity = error
dotnet_diagnostic.IDE0051.severity = warning
dotnet_diagnostic.IDE0052.severity = warning
dotnet_diagnostic.IDE0072.severity = warning
dotnet_diagnostic.IDE0080.severity = warning
dotnet_diagnostic.IDE0100.severity = error
dotnet_diagnostic.IDE0110.severity = error
dotnet_diagnostic.IDE0120.severity = warning
dotnet_diagnostic.IDE0121.severity = warning
dotnet_diagnostic.IDE0240.severity = error
dotnet_diagnostic.IDE0241.severity = error
dotnet_style_coalesce_expression = true
dotnet_style_namespace_match_folder = false
dotnet_style_null_propagation = true
dotnet_style_prefer_auto_properties = true
dotnet_style_prefer_collection_expression = when_types_loosely_match
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
dotnet_style_prefer_compound_assignment = true
csharp_style_prefer_simple_property_accessors = true
dotnet_style_prefer_simplified_interpolation = true
dotnet_style_prefer_simplified_boolean_expressions = true
csharp_style_prefer_unbound_generic_type_in_nameof = true
# Field preferences
dotnet_diagnostic.IDE0044.severity = warning
dotnet_style_readonly_field = true
# Language keyword vs. framework types preferences
dotnet_diagnostic.IDE0049.severity = error
dotnet_style_predefined_type_for_locals_parameters_members = true
dotnet_style_predefined_type_for_member_access = true
# Modifier preferences
csharp_prefer_static_local_function = true
csharp_style_prefer_readonly_struct = true
dotnet_diagnostic.IDE0036.severity = warning
dotnet_diagnostic.IDE0040.severity = error
dotnet_diagnostic.IDE0380.severity = error
dotnet_style_require_accessibility_modifiers = always
# New-line preferences
dotnet_diagnostic.IDE2000.severity = warning
dotnet_diagnostic.IDE2002.severity = warning
dotnet_diagnostic.IDE2003.severity = warning
dotnet_diagnostic.IDE2004.severity = warning
dotnet_diagnostic.IDE2005.severity = warning
dotnet_diagnostic.IDE2006.severity = warning
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false
dotnet_style_allow_multiple_blank_lines_experimental = false
dotnet_style_allow_statement_immediately_after_block_experimental = false
# Null-checking preferences
csharp_style_conditional_delegate_call = true
# Parameter preferences
dotnet_code_quality_unused_parameters = all
dotnet_diagnostic.IDE0280.severity = error
# Parentheses preferences
dotnet_diagnostic.IDE0047.severity = warning
dotnet_diagnostic.IDE0048.severity = warning
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_operators = always_for_clarity
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
# Pattern-matching preferences
dotnet_diagnostic.IDE0019.severity = warning
dotnet_diagnostic.IDE0020.severity = warning
dotnet_diagnostic.IDE0038.severity = warning
dotnet_diagnostic.IDE0066.severity = none
dotnet_diagnostic.IDE0083.severity = warning
dotnet_diagnostic.IDE0260.severity = warning
csharp_style_pattern_matching_over_as_with_null_check = true
csharp_style_pattern_matching_over_is_with_cast_check = true
csharp_style_prefer_not_pattern = true
csharp_style_prefer_pattern_matching = true
# this. and Me. preferences
dotnet_style_qualification_for_event = false
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_method = false
dotnet_style_qualification_for_property = false
# var preferences
csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = true
# .NET formatting options
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = true
# C# formatting options
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

View File

@@ -20,7 +20,7 @@ namespace SabreTools.IO.Test.Extensions
{
// Handle test setup
expected ??= PathTool.GetRuntimeDirectory();
if (expected != null)
if (expected is not null)
expected = Path.GetFullPath(expected);
string actual = dir.Ensure(create: false);

View File

@@ -31,11 +31,11 @@ namespace SabreTools.Text.Compare
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
if (x is null || y is null)
{
if (x == null && y != null)
if (x is null && y is not null)
return -1;
else if (x != null && y == null)
else if (x is not null && y is null)
return 1;
else
return 0;

View File

@@ -8,13 +8,13 @@
public static int ComparePaths(string? left, string? right)
{
// If both strings are null, return
if (left == null && right == null)
if (left is null && right is null)
return 0;
// If one is null, then say that's less than
if (left == null)
if (left is null)
return -1;
if (right == null)
if (right is null)
return 1;
// Normalize the path seperators

View File

@@ -31,11 +31,11 @@ namespace SabreTools.Text.Compare
public override int Compare(string? x, string? y)
{
if (x == null || y == null)
if (x is null || y is null)
{
if (x == null && y != null)
if (x is null && y is not null)
return -1;
else if (x != null && y == null)
else if (x is not null && y is null)
return 1;
else
return 0;

View File

@@ -54,6 +54,10 @@
namespace SabreTools.IO.Compression.BZip2
{
#pragma warning disable IDE0047
#pragma warning disable IDE2000
#pragma warning disable IDE2002
#pragma warning disable IDE2003
// /**
// * Checks if the signature matches what is expected for a bzip2 file.
// *

File diff suppressed because it is too large Load Diff

View File

@@ -58,6 +58,17 @@ using System.IO;
#nullable disable
namespace SabreTools.IO.Compression.BZip2
{
#pragma warning disable IDE0001
#pragma warning disable IDE0002
#pragma warning disable IDE0004
#pragma warning disable IDE0036
#pragma warning disable IDE0040
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2002
#pragma warning disable IDE2003
/// <summary>
/// A read-only decorator stream that performs BZip2 decompression on Read.
/// </summary>
@@ -208,7 +219,7 @@ namespace SabreTools.IO.Compression.BZip2
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
offset, count, buffer.Length));
if (this.input == null)
if (this.input is null)
throw new IOException("the stream is not open");
@@ -408,7 +419,7 @@ namespace SabreTools.IO.Compression.BZip2
{
if (!_disposed)
{
if (disposing && (this.input != null))
if (disposing && (this.input is not null))
this.input.Close();
_disposed = true;
}
@@ -551,7 +562,7 @@ namespace SabreTools.IO.Compression.BZip2
public override void Close()
{
Stream inShadow = this.input;
if (inShadow != null)
if (inShadow is not null)
{
try
{
@@ -1093,7 +1104,7 @@ namespace SabreTools.IO.Compression.BZip2
private void SetupBlock()
{
if (this.data == null)
if (this.data is null)
return;
int i;
@@ -1371,7 +1382,7 @@ namespace SabreTools.IO.Compression.BZip2
// it can happen, if the compressor mixed small and large
// blocks. Normally only the last block will be smaller
// than others.
if ((ttShadow == null) || (ttShadow.Length < length))
if ((ttShadow is null) || (ttShadow.Length < length))
{
this.tt = ttShadow = new int[length];
}

View File

@@ -47,7 +47,6 @@
* under the License.
*/
// Design Notes:
//
// This class follows the classic Decorator pattern: it is a Stream that
@@ -85,6 +84,14 @@ using System.IO;
#nullable disable
namespace SabreTools.IO.Compression.BZip2
{
#pragma warning disable IDE0001
#pragma warning disable IDE0004
#pragma warning disable IDE0040
#pragma warning disable IDE0048
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2002
#pragma warning disable IDE2003
/// <summary>
/// A write-only decorator stream that compresses data as it is
/// written using the BZip2 algorithm.
@@ -218,7 +225,7 @@ namespace SabreTools.IO.Compression.BZip2
/// </remarks>
public override void Close()
{
if (output != null)
if (output is not null)
{
Stream o = this.output;
Finish();
@@ -233,7 +240,7 @@ namespace SabreTools.IO.Compression.BZip2
/// </summary>
public override void Flush()
{
if (this.output != null)
if (this.output is not null)
{
this.bw.Flush();
this.output.Flush();
@@ -348,7 +355,7 @@ namespace SabreTools.IO.Compression.BZip2
if (offset + count > buffer.Length)
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
offset, count, buffer.Length));
if (this.output == null)
if (this.output is null)
throw new IOException("the stream is not open");
if (count == 0) return; // nothing to do
@@ -429,7 +436,7 @@ namespace SabreTools.IO.Compression.BZip2
{
get
{
if (this.output == null) throw new ObjectDisposedException("BZip2Stream");
if (this.output is null) throw new ObjectDisposedException("BZip2Stream");
return this.output.CanWrite;
}
}

View File

@@ -1,245 +1,249 @@
// BitWriter.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// 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: <2011-July-25 18:57:31>
//
// ------------------------------------------------------------------
//
// This module defines the BitWriter class, which writes bits at a time
// to an output stream. It's used by the BZip2Compressor class, and by
// the BZip2OutputStream class and its parallel variant,
// ParallelBZip2OutputStream.
//
// ------------------------------------------------------------------
//
// Design notes:
//
// BZip2 employs byte-shredding in its data format - rather than
// aligning all data items in a compressed .bz2 file on byte barriers,
// the BZip2 format uses portions of bytes to represent independent
// pieces of information. This "shredding" starts with the first
// "randomised" bit - just 12 bytes or so into a bz2 file or stream. But
// the approach is used extensively in bzip2 files - sometimes 5 bits
// are used, sometimes 24 or 3 bits, sometimes just 1 bit, and so on.
// It's not possible to send this information directly to a stream in
// this form; Streams in .NET accept byte-oriented input. Therefore,
// when actually writing a bz2 file, the output data must be organized
// into a byte-aligned format before being written to the output stream.
//
// This BitWriter class provides the byte-shredding necessary for BZip2
// output. Think of this class as an Adapter that enables Bit-oriented
// output to a standard byte-oriented .NET stream. This class writes
// data out to the captive output stream only after the data bits have
// been accumulated and aligned. For example, suppose that during
// operation, the BZip2 compressor emits 5 bits, then 24 bits, then 32
// bits. When the first 5 bits are sent to the BitWriter, nothing is
// written to the output stream; instead these 5 bits are simply stored
// in the internal accumulator. When the next 24 bits are written, the
// first 3 bits are gathered with the accumulated bits. The resulting
// 5+3 constitutes an entire byte; the BitWriter then actually writes
// that byte to the output stream. This leaves 21 bits. BitWriter writes
// 2 more whole bytes (16 more bits), in 8-bit chunks, leaving 5 in the
// accumulator. BitWriter then follows the same procedure with the 32
// new bits. And so on.
//
// A quick tour of the implementation:
//
// The accumulator is a uint - so it can accumulate at most 4 bytes of
// information. In practice because of the design of this class, it
// never accumulates more than 3 bytes.
//
// The Flush() method emits all whole bytes available. After calling
// Flush(), there may be between 0-7 bits yet to be emitted into the
// output stream.
//
// FinishAndPad() emits all data, including the last partial byte and
// any necessary padding. In effect, it establishes a byte-alignment
// barrier. To support bzip2, FinishAndPad() should be called only once
// for a bz2 file, after the last bit of data has been written through
// this adapter. Other binary file formats may use byte-alignment at
// various points within the file, and FinishAndPad() would support that
// scenario.
//
// The internal fn Reset() is used to reset the state of the adapter;
// this class is used by BZip2Compressor, instances of which get re-used
// by multiple distinct threads, for different blocks of data.
//
using System.IO;
namespace SabreTools.IO.Compression.BZip2
{
internal class BitWriter
{
uint accumulator;
int nAccumulatedBits;
readonly Stream output;
int totalBytesWrittenOut;
public BitWriter(Stream s)
{
this.output = s;
}
/// <summary>
/// Delivers the remaining bits, left-aligned, in a byte.
/// </summary>
/// <remarks>
/// <para>
/// This is valid only if NumRemainingBits is less than 8;
/// in other words it is valid only after a call to Flush().
/// </para>
/// </remarks>
public byte RemainingBits
{
get
{
return (byte)(this.accumulator >> (32 - this.nAccumulatedBits) & 0xff);
}
}
public int NumRemainingBits
{
get
{
return this.nAccumulatedBits;
}
}
public int TotalBytesWrittenOut
{
get
{
return this.totalBytesWrittenOut;
}
}
/// <summary>
/// Reset the BitWriter.
/// </summary>
/// <remarks>
/// <para>
/// This is useful when the BitWriter writes into a MemoryStream, and
/// is used by a BZip2Compressor, which itself is re-used for multiple
/// distinct data blocks.
/// </para>
/// </remarks>
public void Reset()
{
this.accumulator = 0;
this.nAccumulatedBits = 0;
this.totalBytesWrittenOut = 0;
this.output.Seek(0, SeekOrigin.Begin);
this.output.SetLength(0);
}
/// <summary>
/// Write some number of bits from the given value, into the output.
/// </summary>
/// <remarks>
/// <para>
/// The nbits value should be a max of 25, for safety. For performance
/// reasons, this method does not check!
/// </para>
/// </remarks>
public void WriteBits(int nbits, uint value)
{
int nAccumulated = this.nAccumulatedBits;
uint u = this.accumulator;
while (nAccumulated >= 8)
{
this.output.WriteByte((byte)(u >> 24 & 0xff));
this.totalBytesWrittenOut++;
u <<= 8;
nAccumulated -= 8;
}
this.accumulator = u | (value << (32 - nAccumulated - nbits));
this.nAccumulatedBits = nAccumulated + nbits;
// Console.WriteLine("WriteBits({0}, 0x{1:X2}) => {2:X8} n({3})",
// nbits, value, accumulator, nAccumulatedBits);
// Console.ReadLine();
// At this point the accumulator may contain up to 31 bits waiting for
// output.
}
/// <summary>
/// Write a full 8-bit byte into the output.
/// </summary>
public void WriteByte(byte b)
{
WriteBits(8, b);
}
/// <summary>
/// Write four 8-bit bytes into the output.
/// </summary>
public void WriteInt(uint u)
{
WriteBits(8, (u >> 24) & 0xff);
WriteBits(8, (u >> 16) & 0xff);
WriteBits(8, (u >> 8) & 0xff);
WriteBits(8, u & 0xff);
}
/// <summary>
/// Write all available byte-aligned bytes.
/// </summary>
/// <remarks>
/// <para>
/// This method writes no new output, but flushes any accumulated
/// bits. At completion, the accumulator may contain up to 7
/// bits.
/// </para>
/// <para>
/// This is necessary when re-assembling output from N independent
/// compressors, one for each of N blocks. The output of any
/// particular compressor will in general have some fragment of a byte
/// remaining. This fragment needs to be accumulated into the
/// parent BZip2OutputStream.
/// </para>
/// </remarks>
public void Flush()
{
WriteBits(0, 0);
}
/// <summary>
/// Writes all available bytes, and emits padding for the final byte as
/// necessary. This must be the last method invoked on an instance of
/// BitWriter.
/// </summary>
public void FinishAndPad()
{
Flush();
if (this.NumRemainingBits > 0)
{
byte b = (byte)((this.accumulator >> 24) & 0xff);
this.output.WriteByte(b);
this.totalBytesWrittenOut++;
}
}
}
}
// BitWriter.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// 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: <2011-July-25 18:57:31>
//
// ------------------------------------------------------------------
//
// This module defines the BitWriter class, which writes bits at a time
// to an output stream. It's used by the BZip2Compressor class, and by
// the BZip2OutputStream class and its parallel variant,
// ParallelBZip2OutputStream.
//
// ------------------------------------------------------------------
//
// Design notes:
//
// BZip2 employs byte-shredding in its data format - rather than
// aligning all data items in a compressed .bz2 file on byte barriers,
// the BZip2 format uses portions of bytes to represent independent
// pieces of information. This "shredding" starts with the first
// "randomised" bit - just 12 bytes or so into a bz2 file or stream. But
// the approach is used extensively in bzip2 files - sometimes 5 bits
// are used, sometimes 24 or 3 bits, sometimes just 1 bit, and so on.
// It's not possible to send this information directly to a stream in
// this form; Streams in .NET accept byte-oriented input. Therefore,
// when actually writing a bz2 file, the output data must be organized
// into a byte-aligned format before being written to the output stream.
//
// This BitWriter class provides the byte-shredding necessary for BZip2
// output. Think of this class as an Adapter that enables Bit-oriented
// output to a standard byte-oriented .NET stream. This class writes
// data out to the captive output stream only after the data bits have
// been accumulated and aligned. For example, suppose that during
// operation, the BZip2 compressor emits 5 bits, then 24 bits, then 32
// bits. When the first 5 bits are sent to the BitWriter, nothing is
// written to the output stream; instead these 5 bits are simply stored
// in the internal accumulator. When the next 24 bits are written, the
// first 3 bits are gathered with the accumulated bits. The resulting
// 5+3 constitutes an entire byte; the BitWriter then actually writes
// that byte to the output stream. This leaves 21 bits. BitWriter writes
// 2 more whole bytes (16 more bits), in 8-bit chunks, leaving 5 in the
// accumulator. BitWriter then follows the same procedure with the 32
// new bits. And so on.
//
// A quick tour of the implementation:
//
// The accumulator is a uint - so it can accumulate at most 4 bytes of
// information. In practice because of the design of this class, it
// never accumulates more than 3 bytes.
//
// The Flush() method emits all whole bytes available. After calling
// Flush(), there may be between 0-7 bits yet to be emitted into the
// output stream.
//
// FinishAndPad() emits all data, including the last partial byte and
// any necessary padding. In effect, it establishes a byte-alignment
// barrier. To support bzip2, FinishAndPad() should be called only once
// for a bz2 file, after the last bit of data has been written through
// this adapter. Other binary file formats may use byte-alignment at
// various points within the file, and FinishAndPad() would support that
// scenario.
//
// The internal fn Reset() is used to reset the state of the adapter;
// this class is used by BZip2Compressor, instances of which get re-used
// by multiple distinct threads, for different blocks of data.
//
using System.IO;
#pragma warning disable IDE0040
#pragma warning disable IDE0048
#pragma warning disable IDE2000
#pragma warning disable IDE2002
namespace SabreTools.IO.Compression.BZip2
{
internal class BitWriter
{
uint accumulator;
int nAccumulatedBits;
readonly Stream output;
int totalBytesWrittenOut;
public BitWriter(Stream s)
{
this.output = s;
}
/// <summary>
/// Delivers the remaining bits, left-aligned, in a byte.
/// </summary>
/// <remarks>
/// <para>
/// This is valid only if NumRemainingBits is less than 8;
/// in other words it is valid only after a call to Flush().
/// </para>
/// </remarks>
public byte RemainingBits
{
get
{
return (byte)(this.accumulator >> (32 - this.nAccumulatedBits) & 0xff);
}
}
public int NumRemainingBits
{
get
{
return this.nAccumulatedBits;
}
}
public int TotalBytesWrittenOut
{
get
{
return this.totalBytesWrittenOut;
}
}
/// <summary>
/// Reset the BitWriter.
/// </summary>
/// <remarks>
/// <para>
/// This is useful when the BitWriter writes into a MemoryStream, and
/// is used by a BZip2Compressor, which itself is re-used for multiple
/// distinct data blocks.
/// </para>
/// </remarks>
public void Reset()
{
this.accumulator = 0;
this.nAccumulatedBits = 0;
this.totalBytesWrittenOut = 0;
this.output.Seek(0, SeekOrigin.Begin);
this.output.SetLength(0);
}
/// <summary>
/// Write some number of bits from the given value, into the output.
/// </summary>
/// <remarks>
/// <para>
/// The nbits value should be a max of 25, for safety. For performance
/// reasons, this method does not check!
/// </para>
/// </remarks>
public void WriteBits(int nbits, uint value)
{
int nAccumulated = this.nAccumulatedBits;
uint u = this.accumulator;
while (nAccumulated >= 8)
{
this.output.WriteByte((byte)(u >> 24 & 0xff));
this.totalBytesWrittenOut++;
u <<= 8;
nAccumulated -= 8;
}
this.accumulator = u | (value << (32 - nAccumulated - nbits));
this.nAccumulatedBits = nAccumulated + nbits;
// Console.WriteLine("WriteBits({0}, 0x{1:X2}) => {2:X8} n({3})",
// nbits, value, accumulator, nAccumulatedBits);
// Console.ReadLine();
// At this point the accumulator may contain up to 31 bits waiting for
// output.
}
/// <summary>
/// Write a full 8-bit byte into the output.
/// </summary>
public void WriteByte(byte b)
{
WriteBits(8, b);
}
/// <summary>
/// Write four 8-bit bytes into the output.
/// </summary>
public void WriteInt(uint u)
{
WriteBits(8, (u >> 24) & 0xff);
WriteBits(8, (u >> 16) & 0xff);
WriteBits(8, (u >> 8) & 0xff);
WriteBits(8, u & 0xff);
}
/// <summary>
/// Write all available byte-aligned bytes.
/// </summary>
/// <remarks>
/// <para>
/// This method writes no new output, but flushes any accumulated
/// bits. At completion, the accumulator may contain up to 7
/// bits.
/// </para>
/// <para>
/// This is necessary when re-assembling output from N independent
/// compressors, one for each of N blocks. The output of any
/// particular compressor will in general have some fragment of a byte
/// remaining. This fragment needs to be accumulated into the
/// parent BZip2OutputStream.
/// </para>
/// </remarks>
public void Flush()
{
WriteBits(0, 0);
}
/// <summary>
/// Writes all available bytes, and emits padding for the final byte as
/// necessary. This must be the last method invoked on an instance of
/// BitWriter.
/// </summary>
public void FinishAndPad()
{
Flush();
if (this.NumRemainingBits > 0)
{
byte b = (byte)((this.accumulator >> 24) & 0xff);
this.output.WriteByte(b);
this.totalBytesWrittenOut++;
}
}
}
}

View File

@@ -31,6 +31,15 @@ using Interop = System.Runtime.InteropServices;
#nullable disable
namespace SabreTools.IO.Compression.BZip2
{
#pragma warning disable IDE0001
#pragma warning disable IDE0002
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2002
#pragma warning disable IDE2003
#pragma warning disable IDE2004
/// <summary>
/// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
/// can set the polynomial and enable or disable bit
@@ -90,7 +99,7 @@ namespace SabreTools.IO.Compression.BZip2
/// <returns>the CRC32 calculation</returns>
public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
{
if (input == null)
if (input is null)
throw new Exception("The input stream must not be null.");
unchecked
@@ -142,7 +151,7 @@ namespace SabreTools.IO.Compression.BZip2
/// <param name="count">how many bytes within the block to slurp</param>
public void SlurpBlock(byte[] block, int offset, int count)
{
if (block == null)
if (block is null)
throw new Exception("The data buffer must not be null.");
// bzip algorithm

View File

@@ -1,99 +1,96 @@
// Rand.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// 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: <2011-July-31 15:09:16>
//
// ------------------------------------------------------------------
//
// This module defines a helper class for the BZip2 classes. This code
// is derived from the original BZip2 source code.
//
// ------------------------------------------------------------------
namespace SabreTools.IO.Compression.BZip2
{
internal static class Rand
{
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,
419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
936, 638
];
/// <summary>
/// Returns the "random" number at a specific index.
/// </summary>
/// <param name='i'>the index</param>
/// <returns>the random number</returns>
internal static int Rnums(int i)
{
return RNUMS[i];
}
}
}
// Rand.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// 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: <2011-July-31 15:09:16>
//
// ------------------------------------------------------------------
//
// This module defines a helper class for the BZip2 classes. This code
// is derived from the original BZip2 source code.
//
// ------------------------------------------------------------------
namespace SabreTools.IO.Compression.BZip2
{
internal static class Rand
{
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,
419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
936, 638
];
/// <summary>
/// Returns the "random" number at a specific index.
/// </summary>
/// <param name='i'>the index</param>
/// <returns>the random number</returns>
internal static int Rnums(int i)
{
return RNUMS[i];
}
}
}

View File

@@ -46,7 +46,7 @@ namespace SabreTools.IO.Compression.Blast
/// compressed data is not corrupted when read or written. For example:
/// fopen(..., "rb") and fopen(..., "wb").
/// </summary>
public unsafe class Decompressor
public class Decompressor
{
#region Huffman Encoding

View File

@@ -90,7 +90,7 @@ namespace SabreTools.IO.Compression.Blast
// Assumes lengths are within bounds
for (symbol = 0; symbol < n; symbol++)
{
(Count[length[symbol]])++;
Count[length[symbol]]++;
}
// No codes! Complete, but decode() will fail
@@ -182,7 +182,7 @@ namespace SabreTools.IO.Compression.Blast
len++;
}
left = (MAXBITS + 1) - len;
left = MAXBITS + 1 - len;
if (left == 0)
break;

View File

@@ -1,174 +1,177 @@
// Zlib.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009-2011 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-August-03 19:52:28>
//
// ------------------------------------------------------------------
//
// This module defines classes for ZLIB compression and
// decompression. This code is derived from the jzlib implementation of
// zlib, but significantly modified. The object model is not the same,
// and many of the behaviors are new or different. Nonetheless, in
// keeping with the license for jzlib, the copyright to that code is
// included below.
//
// ------------------------------------------------------------------
//
// The following notice applies to jzlib:
//
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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.
//
// -----------------------------------------------------------------------
//
// jzlib is based on zlib-1.1.3.
//
// The following notice applies to zlib:
//
// -----------------------------------------------------------------------
//
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
//
// The ZLIB software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Jean-loup Gailly jloup@gzip.org
// Mark Adler madler@alumni.caltech.edu
//
// -----------------------------------------------------------------------
namespace SabreTools.IO.Compression.Deflate
{
/// <summary>
/// Computes an Adler-32 checksum.
/// </summary>
/// <remarks>
/// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
/// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
/// is a required part of the "ZLIB" standard. Applications will almost never need to
/// use this class directly.
/// </remarks>
///
/// <exclude/>
public sealed class Adler
{
// largest prime smaller than 65536
private static readonly uint BASE = 65521;
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private static readonly int NMAX = 5552;
/// <summary>
/// Calculates the Adler32 checksum.
/// </summary>
/// <remarks>
/// <para>
/// This is used within ZLIB. You probably don't need to use this directly.
/// </para>
/// </remarks>
/// <example>
/// To compute an Adler32 checksum on a byte array:
/// <code>
/// var adler = Adler.Adler32(0, null, 0, 0);
/// adler = Adler.Adler32(adler, buffer, index, length);
/// </code>
/// </example>
public static uint Adler32(uint adler, byte[] buf, int index, int len)
{
if (buf == null)
return 1;
uint s1 = (uint)(adler & 0xffff);
uint s2 = (uint)((adler >> 16) & 0xffff);
while (len > 0)
{
int k = len < NMAX ? len : NMAX;
len -= k;
while (k >= 16)
{
//s1 += (buf[index++] & 0xff); s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
k -= 16;
}
if (k != 0)
{
do
{
s1 += buf[index++];
s2 += s1;
}
while (--k != 0);
}
s1 %= BASE;
s2 %= BASE;
}
return (uint)((s2 << 16) | s1);
}
}
}
// Zlib.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009-2011 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-August-03 19:52:28>
//
// ------------------------------------------------------------------
//
// This module defines classes for ZLIB compression and
// decompression. This code is derived from the jzlib implementation of
// zlib, but significantly modified. The object model is not the same,
// and many of the behaviors are new or different. Nonetheless, in
// keeping with the license for jzlib, the copyright to that code is
// included below.
//
// ------------------------------------------------------------------
//
// The following notice applies to jzlib:
//
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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.
//
// -----------------------------------------------------------------------
//
// jzlib is based on zlib-1.1.3.
//
// The following notice applies to zlib:
//
// -----------------------------------------------------------------------
//
// Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
//
// The ZLIB software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Jean-loup Gailly jloup@gzip.org
// Mark Adler madler@alumni.caltech.edu
//
// -----------------------------------------------------------------------
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0004
#pragma warning disable IDE2002
#pragma warning disable IDE2003
/// <summary>
/// Computes an Adler-32 checksum.
/// </summary>
/// <remarks>
/// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
/// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
/// is a required part of the "ZLIB" standard. Applications will almost never need to
/// use this class directly.
/// </remarks>
///
/// <exclude/>
public sealed class Adler
{
// largest prime smaller than 65536
private static readonly uint BASE = 65521;
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private static readonly int NMAX = 5552;
/// <summary>
/// Calculates the Adler32 checksum.
/// </summary>
/// <remarks>
/// <para>
/// This is used within ZLIB. You probably don't need to use this directly.
/// </para>
/// </remarks>
/// <example>
/// To compute an Adler32 checksum on a byte array:
/// <code>
/// var adler = Adler.Adler32(0, null, 0, 0);
/// adler = Adler.Adler32(adler, buffer, index, length);
/// </code>
/// </example>
public static uint Adler32(uint adler, byte[] buf, int index, int len)
{
if (buf is null)
return 1;
uint s1 = (uint)(adler & 0xffff);
uint s2 = (uint)((adler >> 16) & 0xffff);
while (len > 0)
{
int k = len < NMAX ? len : NMAX;
len -= k;
while (k >= 16)
{
//s1 += (buf[index++] & 0xff); s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
s1 += buf[index++]; s2 += s1;
k -= 16;
}
if (k != 0)
{
do
{
s1 += buf[index++];
s2 += s1;
}
while (--k != 0);
}
s1 %= BASE;
s2 %= BASE;
}
return (uint)((s2 << 16) | s1);
}
}
}

View File

@@ -31,6 +31,15 @@ using Interop = System.Runtime.InteropServices;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0001
#pragma warning disable IDE0002
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2002
#pragma warning disable IDE2003
#pragma warning disable IDE2004
/// <summary>
/// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
/// can set the polynomial and enable or disable bit
@@ -90,7 +99,7 @@ namespace SabreTools.IO.Compression.Deflate
/// <returns>the CRC32 calculation</returns>
public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
{
if (input == null)
if (input is null)
throw new Exception("The input stream must not be null.");
unchecked
@@ -142,7 +151,7 @@ namespace SabreTools.IO.Compression.Deflate
/// <param name="count">how many bytes within the block to slurp</param>
public void SlurpBlock(byte[] block, int offset, int count)
{
if (block == null)
if (block is null)
throw new Exception("The data buffer must not be null.");
// bzip algorithm
@@ -278,7 +287,7 @@ namespace SabreTools.IO.Compression.Deflate
crc32Table[i] = dwCrc;
}
i++;
} while (i!=0);
} while (i != 0);
}
#if VERBOSE
@@ -302,10 +311,10 @@ namespace SabreTools.IO.Compression.Deflate
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++;
@@ -340,8 +349,8 @@ namespace SabreTools.IO.Compression.Deflate
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
@@ -358,15 +367,16 @@ namespace SabreTools.IO.Compression.Deflate
// 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;
@@ -375,7 +385,7 @@ namespace SabreTools.IO.Compression.Deflate
// 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;
@@ -384,7 +394,7 @@ namespace SabreTools.IO.Compression.Deflate
crc1 ^= crc2;
_register= ~crc1;
_register = ~crc1;
//return (int) crc1;
return;
@@ -416,7 +426,7 @@ namespace SabreTools.IO.Compression.Deflate
/// </para>
/// </remarks>
public CRC32(bool reverseBits) :
this( unchecked((int)0xEDB88320), reverseBits)
this(unchecked((int)0xEDB88320), reverseBits)
{
}
@@ -449,7 +459,7 @@ namespace SabreTools.IO.Compression.Deflate
public CRC32(int polynomial, bool reverseBits)
{
this.reverseBits = reverseBits;
this.dwPolynomial = (uint) polynomial;
this.dwPolynomial = (uint)polynomial;
this.GenerateLookupTable();
}

View File

@@ -164,5 +164,4 @@ namespace SabreTools.IO.Compression.Deflate
/// </summary>
Level9 = 9,
}
}

View File

@@ -102,5 +102,4 @@ namespace SabreTools.IO.Compression.Deflate
/// </summary>
Decompress = 1,
}
}

View File

@@ -115,5 +115,4 @@ namespace SabreTools.IO.Compression.Deflate
/// </summary>
HuffmanOnly = 2,
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -129,5 +129,4 @@ namespace SabreTools.IO.Compression.Deflate
/// <summary>Signals the end of the compression/decompression stream.</summary>
Finish,
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,435 +1,440 @@
// Inftree.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// 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,
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
sealed class InfTree
{
private const int MANY = 1440;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
internal const int fixed_bl = 9;
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 = [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];
//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 = [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 = [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 = [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 = [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 = [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[] c = null; // bit length count table
internal int[] r = null; // table entry for structure assignment
internal int[] u = null; // table stack
internal int[] x = null; // bit offsets, then code stack
private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
{
// Given a list of code lengths and a maximum table size, make a set of
// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
// if the given code set is incomplete (the tables are still built in this
// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
// lengths), or Z_MEM_ERROR if not enough memory.
int a; // counter for codes of length k
int f; // i repeats in table every f entries
int g; // maximum code length
int h; // table level
int i; // counter, current code
int j; // counter
int k; // number of bits in current code
int l; // bits per table (returned in m)
int mask; // (1 << w) - 1, to avoid cc -O bug on HP
int p; // pointer into c[], b[], or v[]
int q; // points to current table
int w; // bits before this table == (l * h)
int xp; // pointer into x
int y; // number of dummy codes added
int z; // number of entries in current table
// Generate counts for each bit length
p = 0; i = n;
do
{
c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
}
while (i != 0);
if (c[0] == n)
{
// null input--all zero length codes
t[0] = -1;
m[0] = 0;
return Z_OK;
}
// Find minimum and maximum length, bound *m by those
l = m[0];
for (j = 1; j <= BMAX; j++)
if (c[j] != 0)
break;
k = j; // minimum code length
if (l < j)
{
l = j;
}
for (i = BMAX; i != 0; i--)
{
if (c[i] != 0)
break;
}
g = i; // maximum code length
if (l > i)
{
l = i;
}
m[0] = l;
// Adjust last length count to fill out codes, if needed
for (y = 1 << j; j < i; j++, y <<= 1)
{
if ((y -= c[j]) < 0)
{
return Z_DATA_ERROR;
}
}
if ((y -= c[i]) < 0)
{
return Z_DATA_ERROR;
}
c[i] += y;
// Generate starting offsets into the value table for each length
x[1] = j = 0;
p = 1; xp = 2;
while (--i != 0)
{
// note that i == g from above
x[xp] = (j += c[p]);
xp++;
p++;
}
// Make a table of values in order of bit lengths
i = 0; p = 0;
do
{
if ((j = b[bindex + p]) != 0)
{
v[x[j]++] = i;
}
p++;
}
while (++i < n);
n = x[g]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries
x[0] = i = 0; // first Huffman code is zero
p = 0; // grab values in bit order
h = -1; // no tables yet--level -1
w = -l; // bits decoded == (l * h)
u[0] = 0; // just to keep compilers happy
q = 0; // ditto
z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code)
for (; k <= g; k++)
{
a = c[k];
while (a-- != 0)
{
// here i is the Huffman code of length k bits for value *p
// make tables up to required level
while (k > w + l)
{
h++;
w += l; // previous table always l bits
// compute minimum size table less than or equal to l bits
z = g - w;
z = (z > l) ? l : z; // table size upper limit
if ((f = 1 << (j = k - w)) > a + 1)
{
// try a k-w bit table
// too few codes for k-w bit table
f -= (a + 1); // deduct codes from patterns left
xp = k;
if (j < z)
{
while (++j < z)
{
// try smaller tables up to z bits
if ((f <<= 1) <= c[++xp])
break; // enough codes to use up j bits
f -= c[xp]; // else deduct codes from patterns
}
}
}
z = 1 << j; // table entries for j-bit table
// allocate new table
if (hn[0] + z > MANY)
{
// (note: doesn't matter for fixed)
return Z_DATA_ERROR; // overflow of MANY
}
u[h] = q = hn[0]; // DEBUG
hn[0] += z;
// connect to last table, if there is one
if (h != 0)
{
x[h] = i; // save pattern for backing up
r[0] = (sbyte)j; // bits in this table
r[1] = (sbyte)l; // bits to dump before this table
j = SharedUtils.URShift(i, (w - l));
r[2] = (int)(q - u[h - 1] - j); // offset to this table
Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
}
else
{
t[0] = q; // first table is returned result
}
}
// set up table entry in r
r[1] = (sbyte)(k - w);
if (p >= n)
{
r[0] = 128 + 64; // out of values--invalid code
}
else if (v[p] < s)
{
r[0] = (sbyte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
r[2] = v[p++]; // simple code is just the value
}
else
{
r[0] = (sbyte)(e[v[p] - s] + 16 + 64); // non-simple--look up in lists
r[2] = d[v[p++] - s];
}
// fill code-like entries with r
f = 1 << (k - w);
for (j = SharedUtils.URShift(i, w); j < z; j += f)
{
Array.Copy(r, 0, hp, (q + j) * 3, 3);
}
// backwards increment the k-bit code i
for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
{
i ^= j;
}
i ^= j;
// backup over finished tables
mask = (1 << w) - 1; // needed on HP, cc -O bug
while ((i & mask) != x[h])
{
h--; // don't need to update q
w -= l;
mask = (1 << w) - 1;
}
}
}
// Return Z_BUF_ERROR if we were given an incomplete table
return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}
internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
{
int result;
initWorkArea(19);
hn[0] = 0;
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed dynamic bit lengths tree";
}
else if (result == Z_BUF_ERROR || bb[0] == 0)
{
z.Message = "incomplete dynamic bit lengths tree";
result = Z_DATA_ERROR;
}
return result;
}
internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
{
int result;
// build literal/length tree
initWorkArea(288);
hn[0] = 0;
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (result != Z_OK || bl[0] == 0)
{
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed literal/length tree";
}
else if (result != Z_MEM_ERROR)
{
z.Message = "incomplete literal/length tree";
result = Z_DATA_ERROR;
}
return result;
}
// build distance tree
initWorkArea(288);
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
if (result != Z_OK || (bd[0] == 0 && nl > 257))
{
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed distance tree";
}
else if (result == Z_BUF_ERROR)
{
z.Message = "incomplete distance tree";
result = Z_DATA_ERROR;
}
else if (result != Z_MEM_ERROR)
{
z.Message = "empty distance tree with lengths";
result = Z_DATA_ERROR;
}
return result;
}
return Z_OK;
}
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
{
bl[0] = fixed_bl;
bd[0] = fixed_bd;
tl[0] = fixed_tl;
td[0] = fixed_td;
return Z_OK;
}
private void initWorkArea(int vsize)
{
if (hn == null)
{
hn = new int[1];
v = new int[vsize];
c = new int[BMAX + 1];
r = new int[3];
u = new int[BMAX];
x = new int[BMAX + 1];
}
else
{
if (v.Length < vsize)
{
v = new int[vsize];
}
Array.Clear(v, 0, vsize);
Array.Clear(c, 0, BMAX + 1);
r[0] = 0; r[1] = 0; r[2] = 0;
// for(int i=0; i<BMAX; i++){u[i]=0;}
//Array.Copy(c, 0, u, 0, BMAX);
Array.Clear(u, 0, BMAX);
// for(int i=0; i<BMAX+1; i++){x[i]=0;}
//Array.Copy(c, 0, x, 0, BMAX + 1);
Array.Clear(x, 0, BMAX + 1);
}
}
}
}
// Inftree.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// 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,
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0004
#pragma warning disable IDE0040
#pragma warning disable IDE0047
#pragma warning disable IDE0051
#pragma warning disable IDE2003
sealed class InfTree
{
private const int MANY = 1440;
private const int Z_OK = 0;
private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = -1;
private const int Z_STREAM_ERROR = -2;
private const int Z_DATA_ERROR = -3;
private const int Z_MEM_ERROR = -4;
private const int Z_BUF_ERROR = -5;
private const int Z_VERSION_ERROR = -6;
internal const int fixed_bl = 9;
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 = [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];
//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 = [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 = [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 = [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 = [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 = [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[] c = null; // bit length count table
internal int[] r = null; // table entry for structure assignment
internal int[] u = null; // table stack
internal int[] x = null; // bit offsets, then code stack
private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v)
{
// Given a list of code lengths and a maximum table size, make a set of
// tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
// if the given code set is incomplete (the tables are still built in this
// case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
// lengths), or Z_MEM_ERROR if not enough memory.
int a; // counter for codes of length k
int f; // i repeats in table every f entries
int g; // maximum code length
int h; // table level
int i; // counter, current code
int j; // counter
int k; // number of bits in current code
int l; // bits per table (returned in m)
int mask; // (1 << w) - 1, to avoid cc -O bug on HP
int p; // pointer into c[], b[], or v[]
int q; // points to current table
int w; // bits before this table == (l * h)
int xp; // pointer into x
int y; // number of dummy codes added
int z; // number of entries in current table
// Generate counts for each bit length
p = 0; i = n;
do
{
c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
}
while (i != 0);
if (c[0] == n)
{
// null input--all zero length codes
t[0] = -1;
m[0] = 0;
return Z_OK;
}
// Find minimum and maximum length, bound *m by those
l = m[0];
for (j = 1; j <= BMAX; j++)
if (c[j] != 0)
break;
k = j; // minimum code length
if (l < j)
{
l = j;
}
for (i = BMAX; i != 0; i--)
{
if (c[i] != 0)
break;
}
g = i; // maximum code length
if (l > i)
{
l = i;
}
m[0] = l;
// Adjust last length count to fill out codes, if needed
for (y = 1 << j; j < i; j++, y <<= 1)
{
if ((y -= c[j]) < 0)
{
return Z_DATA_ERROR;
}
}
if ((y -= c[i]) < 0)
{
return Z_DATA_ERROR;
}
c[i] += y;
// Generate starting offsets into the value table for each length
x[1] = j = 0;
p = 1; xp = 2;
while (--i != 0)
{
// note that i == g from above
x[xp] = (j += c[p]);
xp++;
p++;
}
// Make a table of values in order of bit lengths
i = 0; p = 0;
do
{
if ((j = b[bindex + p]) != 0)
{
v[x[j]++] = i;
}
p++;
}
while (++i < n);
n = x[g]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries
x[0] = i = 0; // first Huffman code is zero
p = 0; // grab values in bit order
h = -1; // no tables yet--level -1
w = -l; // bits decoded == (l * h)
u[0] = 0; // just to keep compilers happy
q = 0; // ditto
z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code)
for (; k <= g; k++)
{
a = c[k];
while (a-- != 0)
{
// here i is the Huffman code of length k bits for value *p
// make tables up to required level
while (k > w + l)
{
h++;
w += l; // previous table always l bits
// compute minimum size table less than or equal to l bits
z = g - w;
z = (z > l) ? l : z; // table size upper limit
if ((f = 1 << (j = k - w)) > a + 1)
{
// try a k-w bit table
// too few codes for k-w bit table
f -= (a + 1); // deduct codes from patterns left
xp = k;
if (j < z)
{
while (++j < z)
{
// try smaller tables up to z bits
if ((f <<= 1) <= c[++xp])
break; // enough codes to use up j bits
f -= c[xp]; // else deduct codes from patterns
}
}
}
z = 1 << j; // table entries for j-bit table
// allocate new table
if (hn[0] + z > MANY)
{
// (note: doesn't matter for fixed)
return Z_DATA_ERROR; // overflow of MANY
}
u[h] = q = hn[0]; // DEBUG
hn[0] += z;
// connect to last table, if there is one
if (h != 0)
{
x[h] = i; // save pattern for backing up
r[0] = (sbyte)j; // bits in this table
r[1] = (sbyte)l; // bits to dump before this table
j = SharedUtils.URShift(i, (w - l));
r[2] = (int)(q - u[h - 1] - j); // offset to this table
Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
}
else
{
t[0] = q; // first table is returned result
}
}
// set up table entry in r
r[1] = (sbyte)(k - w);
if (p >= n)
{
r[0] = 128 + 64; // out of values--invalid code
}
else if (v[p] < s)
{
r[0] = (sbyte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
r[2] = v[p++]; // simple code is just the value
}
else
{
r[0] = (sbyte)(e[v[p] - s] + 16 + 64); // non-simple--look up in lists
r[2] = d[v[p++] - s];
}
// fill code-like entries with r
f = 1 << (k - w);
for (j = SharedUtils.URShift(i, w); j < z; j += f)
{
Array.Copy(r, 0, hp, (q + j) * 3, 3);
}
// backwards increment the k-bit code i
for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
{
i ^= j;
}
i ^= j;
// backup over finished tables
mask = (1 << w) - 1; // needed on HP, cc -O bug
while ((i & mask) != x[h])
{
h--; // don't need to update q
w -= l;
mask = (1 << w) - 1;
}
}
}
// Return Z_BUF_ERROR if we were given an incomplete table
return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
}
internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
{
int result;
initWorkArea(19);
hn[0] = 0;
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed dynamic bit lengths tree";
}
else if (result == Z_BUF_ERROR || bb[0] == 0)
{
z.Message = "incomplete dynamic bit lengths tree";
result = Z_DATA_ERROR;
}
return result;
}
internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
{
int result;
// build literal/length tree
initWorkArea(288);
hn[0] = 0;
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (result != Z_OK || bl[0] == 0)
{
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed literal/length tree";
}
else if (result != Z_MEM_ERROR)
{
z.Message = "incomplete literal/length tree";
result = Z_DATA_ERROR;
}
return result;
}
// build distance tree
initWorkArea(288);
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
if (result != Z_OK || (bd[0] == 0 && nl > 257))
{
if (result == Z_DATA_ERROR)
{
z.Message = "oversubscribed distance tree";
}
else if (result == Z_BUF_ERROR)
{
z.Message = "incomplete distance tree";
result = Z_DATA_ERROR;
}
else if (result != Z_MEM_ERROR)
{
z.Message = "empty distance tree with lengths";
result = Z_DATA_ERROR;
}
return result;
}
return Z_OK;
}
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
{
bl[0] = fixed_bl;
bd[0] = fixed_bd;
tl[0] = fixed_tl;
td[0] = fixed_td;
return Z_OK;
}
private void initWorkArea(int vsize)
{
if (hn is null)
{
hn = new int[1];
v = new int[vsize];
c = new int[BMAX + 1];
r = new int[3];
u = new int[BMAX];
x = new int[BMAX + 1];
}
else
{
if (v.Length < vsize)
{
v = new int[vsize];
}
Array.Clear(v, 0, vsize);
Array.Clear(c, 0, BMAX + 1);
r[0] = 0; r[1] = 0; r[2] = 0;
// for(int i=0; i<BMAX; i++){u[i]=0;}
//Array.Copy(c, 0, u, 0, BMAX);
Array.Clear(u, 0, BMAX);
// for(int i=0; i<BMAX+1; i++){x[i]=0;}
//Array.Copy(c, 0, x, 0, BMAX + 1);
Array.Clear(x, 0, BMAX + 1);
}
}
}
}

View File

@@ -66,6 +66,14 @@ using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0004
#pragma warning disable IDE0010
#pragma warning disable IDE0040
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2003
sealed class InflateBlocks
{
private const int MANY = 1440;
@@ -135,7 +143,7 @@ namespace SabreTools.IO.Compression.Deflate
bitb = 0;
readAt = writeAt = 0;
if (checkfn != null)
if (checkfn is not null)
_codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
return oldCheck;
}
@@ -362,7 +370,7 @@ namespace SabreTools.IO.Compression.Deflate
return Flush(r);
}
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
if (blens == null || blens.Length < t)
if (blens is null || blens.Length < t)
{
blens = new int[t];
}
@@ -710,7 +718,7 @@ namespace SabreTools.IO.Compression.Deflate
_codec.TotalBytesOut += nBytes;
// update check information
if (checkfn != null)
if (checkfn is not null)
_codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
// copy as far as end of window

View File

@@ -66,6 +66,12 @@ using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0004
#pragma warning disable IDE0040
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE2000
#pragma warning disable IDE2003
sealed class InflateCodes
{
// waiting for "i:"=input,

View File

@@ -1,464 +1,467 @@
// Inflate.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2010-January-08 18:32:12>
//
// ------------------------------------------------------------------
//
// This module defines classes for decompression. This code is derived
// from the jzlib implementation of zlib, but significantly modified.
// The object model is not the same, and many of the behaviors are
// different. Nonetheless, in keeping with the license for jzlib, I am
// reproducing the copyright to that code 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
internal sealed class InflateManager
{
// preset dictionary flag in zlib header
private const int PRESET_DICT = 0x20;
private const int Z_DEFLATED = 8;
private enum InflateManagerMode
{
METHOD = 0, // waiting for method byte
FLAG = 1, // waiting for flag byte
DICT4 = 2, // four dictionary check bytes to go
DICT3 = 3, // three dictionary check bytes to go
DICT2 = 4, // two dictionary check bytes to go
DICT1 = 5, // one dictionary check byte to go
DICT0 = 6, // waiting for inflateSetDictionary
BLOCKS = 7, // decompressing blocks
CHECK4 = 8, // four check bytes to go
CHECK3 = 9, // three check bytes to go
CHECK2 = 10, // two check bytes to go
CHECK1 = 11, // one check byte to go
DONE = 12, // finished check, done
BAD = 13, // got an error--stay here
}
private InflateManagerMode mode; // current inflate mode
internal ZlibCodec _codec; // pointer back to this zlib stream
// mode dependent information
internal int method; // if FLAGS, method byte
// if CHECK, check values to compare
internal uint computedCheck; // computed check value
internal uint expectedCheck; // stream check value
// if BAD, inflateSync's marker bytes count
internal int marker;
// mode independent information
//internal int nowrap; // flag for no wrapper
private bool _handleRfc1950HeaderBytes = true;
internal bool HandleRfc1950HeaderBytes
{
get { return _handleRfc1950HeaderBytes; }
set { _handleRfc1950HeaderBytes = value; }
}
internal int wbits; // log2(window size) (8..15, defaults to 15)
internal InflateBlocks blocks; // current inflate_blocks state
public InflateManager() { }
public InflateManager(bool expectRfc1950HeaderBytes)
{
_handleRfc1950HeaderBytes = expectRfc1950HeaderBytes;
}
internal int Reset()
{
_codec.TotalBytesIn = _codec.TotalBytesOut = 0;
_codec.Message = null;
mode = HandleRfc1950HeaderBytes ? InflateManagerMode.METHOD : InflateManagerMode.BLOCKS;
blocks.Reset();
return ZlibConstants.Z_OK;
}
internal int End()
{
blocks?.Free();
blocks = null;
return ZlibConstants.Z_OK;
}
internal int Initialize(ZlibCodec codec, int w)
{
_codec = codec;
_codec.Message = null;
blocks = null;
// handle undocumented nowrap option (no zlib header or check)
//nowrap = 0;
//if (w < 0)
//{
// w = - w;
// nowrap = 1;
//}
// set window size
if (w < 8 || w > 15)
{
End();
throw new ZlibException("Bad window size.");
//return ZlibConstants.Z_STREAM_ERROR;
}
wbits = w;
blocks = new InflateBlocks(codec,
HandleRfc1950HeaderBytes ? this : null,
1 << w);
// reset state
Reset();
return ZlibConstants.Z_OK;
}
internal int Inflate(FlushType flush)
{
int b;
if (_codec.InputBuffer == null)
throw new ZlibException("InputBuffer is null. ");
// int f = (flush == FlushType.Finish)
// ? ZlibConstants.Z_BUF_ERROR
// : ZlibConstants.Z_OK;
// workitem 8870
int f = ZlibConstants.Z_OK;
int r = ZlibConstants.Z_BUF_ERROR;
while (true)
{
switch (mode)
{
case InflateManagerMode.METHOD:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
if (((method = _codec.InputBuffer[_codec.NextIn++]) & 0xf) != Z_DEFLATED)
{
mode = InflateManagerMode.BAD;
_codec.Message = String.Format("unknown compression method (0x{0:X2})", method);
marker = 5; // can't try inflateSync
break;
}
if ((method >> 4) + 8 > wbits)
{
mode = InflateManagerMode.BAD;
_codec.Message = String.Format("invalid window size ({0})", (method >> 4) + 8);
marker = 5; // can't try inflateSync
break;
}
mode = InflateManagerMode.FLAG;
break;
case InflateManagerMode.FLAG:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
b = (_codec.InputBuffer[_codec.NextIn++]) & 0xff;
if ((((method << 8) + b) % 31) != 0)
{
mode = InflateManagerMode.BAD;
_codec.Message = "incorrect header check";
marker = 5; // can't try inflateSync
break;
}
mode = ((b & PRESET_DICT) == 0)
? InflateManagerMode.BLOCKS
: InflateManagerMode.DICT4;
break;
case InflateManagerMode.DICT4:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
mode = InflateManagerMode.DICT3;
break;
case InflateManagerMode.DICT3:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
mode = InflateManagerMode.DICT2;
break;
case InflateManagerMode.DICT2:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
mode = InflateManagerMode.DICT1;
break;
case InflateManagerMode.DICT1:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
_codec._Adler32 = expectedCheck;
mode = InflateManagerMode.DICT0;
return ZlibConstants.Z_NEED_DICT;
case InflateManagerMode.DICT0:
mode = InflateManagerMode.BAD;
_codec.Message = "need dictionary";
marker = 0; // can try inflateSync
return ZlibConstants.Z_STREAM_ERROR;
case InflateManagerMode.BLOCKS:
r = blocks.Process(r);
if (r == ZlibConstants.Z_DATA_ERROR)
{
mode = InflateManagerMode.BAD;
marker = 0; // can try inflateSync
break;
}
if (r == ZlibConstants.Z_OK) r = f;
if (r != ZlibConstants.Z_STREAM_END)
return r;
r = f;
computedCheck = blocks.Reset();
if (!HandleRfc1950HeaderBytes)
{
mode = InflateManagerMode.DONE;
return ZlibConstants.Z_STREAM_END;
}
mode = InflateManagerMode.CHECK4;
break;
case InflateManagerMode.CHECK4:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
mode = InflateManagerMode.CHECK3;
break;
case InflateManagerMode.CHECK3:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
mode = InflateManagerMode.CHECK2;
break;
case InflateManagerMode.CHECK2:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
mode = InflateManagerMode.CHECK1;
break;
case InflateManagerMode.CHECK1:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
if (computedCheck != expectedCheck)
{
mode = InflateManagerMode.BAD;
_codec.Message = "incorrect data check";
marker = 5; // can't try inflateSync
break;
}
mode = InflateManagerMode.DONE;
return ZlibConstants.Z_STREAM_END;
case InflateManagerMode.DONE:
return ZlibConstants.Z_STREAM_END;
case InflateManagerMode.BAD:
throw new ZlibException(String.Format("Bad state ({0})", _codec.Message));
default:
throw new ZlibException("Stream error.");
}
}
}
internal int SetDictionary(byte[] dictionary, bool check = true)
{
int index = 0;
int length = dictionary.Length;
if (check)
{
if (mode != InflateManagerMode.DICT0)
throw new ZlibException("Stream error.");
if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
{
return ZlibConstants.Z_DATA_ERROR;
}
}
_codec._Adler32 = Adler.Adler32(0, null, 0, 0);
if (length >= (1 << wbits))
{
length = (1 << wbits) - 1;
index = dictionary.Length - length;
}
blocks.SetDictionary(dictionary, index, length);
mode = InflateManagerMode.BLOCKS;
return ZlibConstants.Z_OK;
}
private static readonly byte[] mark = [0, 0, 0xff, 0xff];
internal int Sync()
{
int n; // number of bytes to look at
int p; // pointer to bytes
int m; // number of marker bytes found in a row
long r, w; // temporaries to save total_in and total_out
// set up
if (mode != InflateManagerMode.BAD)
{
mode = InflateManagerMode.BAD;
marker = 0;
}
if ((n = _codec.AvailableBytesIn) == 0)
return ZlibConstants.Z_BUF_ERROR;
p = _codec.NextIn;
m = marker;
// search
while (n != 0 && m < 4)
{
if (_codec.InputBuffer[p] == mark[m])
{
m++;
}
else if (_codec.InputBuffer[p] != 0)
{
m = 0;
}
else
{
m = 4 - m;
}
p++; n--;
}
// restore
_codec.TotalBytesIn += p - _codec.NextIn;
_codec.NextIn = p;
_codec.AvailableBytesIn = n;
marker = m;
// return no joy or set up to restart on a new block
if (m != 4)
{
return ZlibConstants.Z_DATA_ERROR;
}
r = _codec.TotalBytesIn;
w = _codec.TotalBytesOut;
Reset();
_codec.TotalBytesIn = r;
_codec.TotalBytesOut = w;
mode = InflateManagerMode.BLOCKS;
return ZlibConstants.Z_OK;
}
// Returns true if inflate is currently at the end of a block generated
// by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
// implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
// but removes the length bytes of the resulting empty stored block. When
// decompressing, PPP checks that at the end of input packet, inflate is
// waiting for these length bytes.
internal int SyncPoint(ZlibCodec z)
{
return blocks.SyncPoint();
}
}
}
// Inflate.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2010-January-08 18:32:12>
//
// ------------------------------------------------------------------
//
// This module defines classes for decompression. This code is derived
// from the jzlib implementation of zlib, but significantly modified.
// The object model is not the same, and many of the behaviors are
// different. Nonetheless, in keeping with the license for jzlib, I am
// reproducing the copyright to that code 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
using System;
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0047
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2003
internal sealed class InflateManager
{
// preset dictionary flag in zlib header
private const int PRESET_DICT = 0x20;
private const int Z_DEFLATED = 8;
private enum InflateManagerMode
{
METHOD = 0, // waiting for method byte
FLAG = 1, // waiting for flag byte
DICT4 = 2, // four dictionary check bytes to go
DICT3 = 3, // three dictionary check bytes to go
DICT2 = 4, // two dictionary check bytes to go
DICT1 = 5, // one dictionary check byte to go
DICT0 = 6, // waiting for inflateSetDictionary
BLOCKS = 7, // decompressing blocks
CHECK4 = 8, // four check bytes to go
CHECK3 = 9, // three check bytes to go
CHECK2 = 10, // two check bytes to go
CHECK1 = 11, // one check byte to go
DONE = 12, // finished check, done
BAD = 13, // got an error--stay here
}
private InflateManagerMode mode; // current inflate mode
internal ZlibCodec _codec; // pointer back to this zlib stream
// mode dependent information
internal int method; // if FLAGS, method byte
// if CHECK, check values to compare
internal uint computedCheck; // computed check value
internal uint expectedCheck; // stream check value
// if BAD, inflateSync's marker bytes count
internal int marker;
// mode independent information
//internal int nowrap; // flag for no wrapper
private bool _handleRfc1950HeaderBytes = true;
internal bool HandleRfc1950HeaderBytes
{
get { return _handleRfc1950HeaderBytes; }
set { _handleRfc1950HeaderBytes = value; }
}
internal int wbits; // log2(window size) (8..15, defaults to 15)
internal InflateBlocks blocks; // current inflate_blocks state
public InflateManager() { }
public InflateManager(bool expectRfc1950HeaderBytes)
{
_handleRfc1950HeaderBytes = expectRfc1950HeaderBytes;
}
internal int Reset()
{
_codec.TotalBytesIn = _codec.TotalBytesOut = 0;
_codec.Message = null;
mode = HandleRfc1950HeaderBytes ? InflateManagerMode.METHOD : InflateManagerMode.BLOCKS;
blocks.Reset();
return ZlibConstants.Z_OK;
}
internal int End()
{
blocks?.Free();
blocks = null;
return ZlibConstants.Z_OK;
}
internal int Initialize(ZlibCodec codec, int w)
{
_codec = codec;
_codec.Message = null;
blocks = null;
// handle undocumented nowrap option (no zlib header or check)
//nowrap = 0;
//if (w < 0)
//{
// w = - w;
// nowrap = 1;
//}
// set window size
if (w < 8 || w > 15)
{
End();
throw new ZlibException("Bad window size.");
//return ZlibConstants.Z_STREAM_ERROR;
}
wbits = w;
blocks = new InflateBlocks(codec,
HandleRfc1950HeaderBytes ? this : null,
1 << w);
// reset state
Reset();
return ZlibConstants.Z_OK;
}
internal int Inflate(FlushType flush)
{
int b;
if (_codec.InputBuffer is null)
throw new ZlibException("InputBuffer is null. ");
// int f = (flush == FlushType.Finish)
// ? ZlibConstants.Z_BUF_ERROR
// : ZlibConstants.Z_OK;
// workitem 8870
int f = ZlibConstants.Z_OK;
int r = ZlibConstants.Z_BUF_ERROR;
while (true)
{
switch (mode)
{
case InflateManagerMode.METHOD:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
if (((method = _codec.InputBuffer[_codec.NextIn++]) & 0xf) != Z_DEFLATED)
{
mode = InflateManagerMode.BAD;
_codec.Message = String.Format("unknown compression method (0x{0:X2})", method);
marker = 5; // can't try inflateSync
break;
}
if ((method >> 4) + 8 > wbits)
{
mode = InflateManagerMode.BAD;
_codec.Message = String.Format("invalid window size ({0})", (method >> 4) + 8);
marker = 5; // can't try inflateSync
break;
}
mode = InflateManagerMode.FLAG;
break;
case InflateManagerMode.FLAG:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
b = (_codec.InputBuffer[_codec.NextIn++]) & 0xff;
if ((((method << 8) + b) % 31) != 0)
{
mode = InflateManagerMode.BAD;
_codec.Message = "incorrect header check";
marker = 5; // can't try inflateSync
break;
}
mode = ((b & PRESET_DICT) == 0)
? InflateManagerMode.BLOCKS
: InflateManagerMode.DICT4;
break;
case InflateManagerMode.DICT4:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
mode = InflateManagerMode.DICT3;
break;
case InflateManagerMode.DICT3:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
mode = InflateManagerMode.DICT2;
break;
case InflateManagerMode.DICT2:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
mode = InflateManagerMode.DICT1;
break;
case InflateManagerMode.DICT1:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
_codec._Adler32 = expectedCheck;
mode = InflateManagerMode.DICT0;
return ZlibConstants.Z_NEED_DICT;
case InflateManagerMode.DICT0:
mode = InflateManagerMode.BAD;
_codec.Message = "need dictionary";
marker = 0; // can try inflateSync
return ZlibConstants.Z_STREAM_ERROR;
case InflateManagerMode.BLOCKS:
r = blocks.Process(r);
if (r == ZlibConstants.Z_DATA_ERROR)
{
mode = InflateManagerMode.BAD;
marker = 0; // can try inflateSync
break;
}
if (r == ZlibConstants.Z_OK) r = f;
if (r != ZlibConstants.Z_STREAM_END)
return r;
r = f;
computedCheck = blocks.Reset();
if (!HandleRfc1950HeaderBytes)
{
mode = InflateManagerMode.DONE;
return ZlibConstants.Z_STREAM_END;
}
mode = InflateManagerMode.CHECK4;
break;
case InflateManagerMode.CHECK4:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck = (uint)((_codec.InputBuffer[_codec.NextIn++] << 24) & 0xff000000);
mode = InflateManagerMode.CHECK3;
break;
case InflateManagerMode.CHECK3:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 16) & 0x00ff0000);
mode = InflateManagerMode.CHECK2;
break;
case InflateManagerMode.CHECK2:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--;
_codec.TotalBytesIn++;
expectedCheck += (uint)((_codec.InputBuffer[_codec.NextIn++] << 8) & 0x0000ff00);
mode = InflateManagerMode.CHECK1;
break;
case InflateManagerMode.CHECK1:
if (_codec.AvailableBytesIn == 0) return r;
r = f;
_codec.AvailableBytesIn--; _codec.TotalBytesIn++;
expectedCheck += (uint)(_codec.InputBuffer[_codec.NextIn++] & 0x000000ff);
if (computedCheck != expectedCheck)
{
mode = InflateManagerMode.BAD;
_codec.Message = "incorrect data check";
marker = 5; // can't try inflateSync
break;
}
mode = InflateManagerMode.DONE;
return ZlibConstants.Z_STREAM_END;
case InflateManagerMode.DONE:
return ZlibConstants.Z_STREAM_END;
case InflateManagerMode.BAD:
throw new ZlibException(String.Format("Bad state ({0})", _codec.Message));
default:
throw new ZlibException("Stream error.");
}
}
}
internal int SetDictionary(byte[] dictionary, bool check = true)
{
int index = 0;
int length = dictionary.Length;
if (check)
{
if (mode != InflateManagerMode.DICT0)
throw new ZlibException("Stream error.");
if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
{
return ZlibConstants.Z_DATA_ERROR;
}
}
_codec._Adler32 = Adler.Adler32(0, null, 0, 0);
if (length >= (1 << wbits))
{
length = (1 << wbits) - 1;
index = dictionary.Length - length;
}
blocks.SetDictionary(dictionary, index, length);
mode = InflateManagerMode.BLOCKS;
return ZlibConstants.Z_OK;
}
private static readonly byte[] mark = [0, 0, 0xff, 0xff];
internal int Sync()
{
int n; // number of bytes to look at
int p; // pointer to bytes
int m; // number of marker bytes found in a row
long r, w; // temporaries to save total_in and total_out
// set up
if (mode != InflateManagerMode.BAD)
{
mode = InflateManagerMode.BAD;
marker = 0;
}
if ((n = _codec.AvailableBytesIn) == 0)
return ZlibConstants.Z_BUF_ERROR;
p = _codec.NextIn;
m = marker;
// search
while (n != 0 && m < 4)
{
if (_codec.InputBuffer[p] == mark[m])
{
m++;
}
else if (_codec.InputBuffer[p] != 0)
{
m = 0;
}
else
{
m = 4 - m;
}
p++; n--;
}
// restore
_codec.TotalBytesIn += p - _codec.NextIn;
_codec.NextIn = p;
_codec.AvailableBytesIn = n;
marker = m;
// return no joy or set up to restart on a new block
if (m != 4)
{
return ZlibConstants.Z_DATA_ERROR;
}
r = _codec.TotalBytesIn;
w = _codec.TotalBytesOut;
Reset();
_codec.TotalBytesIn = r;
_codec.TotalBytesOut = w;
mode = InflateManagerMode.BLOCKS;
return ZlibConstants.Z_OK;
}
// Returns true if inflate is currently at the end of a block generated
// by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
// implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
// but removes the length bytes of the resulting empty stored block. When
// decompressing, PPP checks that at the end of input packet, inflate is
// waiting for these length bytes.
internal int SyncPoint(ZlibCodec z)
{
return blocks.SyncPoint();
}
}
}

View File

@@ -93,7 +93,7 @@ namespace SabreTools.IO.Compression.Deflate
out var foundFilename);
// If the extracted data is invalid
if (status != ExtractionStatus.GOOD || destination == null)
if (status != ExtractionStatus.GOOD || destination is null)
return status;
// Ensure directory separators are consistent
@@ -106,7 +106,7 @@ namespace SabreTools.IO.Compression.Deflate
// Ensure the full output directory exists
filename = Path.Combine(outputDirectory, filename);
var directoryName = Path.GetDirectoryName(filename);
if (directoryName != null && !Directory.Exists(directoryName))
if (directoryName is not null && !Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
// Write the output file
@@ -182,11 +182,11 @@ namespace SabreTools.IO.Compression.Deflate
long zipHeaderBytes = source.Position - current;
// Always trust the PKZIP CRC-32 value over what is supplied
if (zipHeader != null)
if (zipHeader is not null)
expected.Crc32 = zipHeader.CRC32;
// If the filename is [NULL], replace with the zip filename
if (zipHeader?.FileName != null)
if (zipHeader?.FileName is not null)
{
filename = zipHeader.FileName;
if (includeDebug) Console.WriteLine($"Filename from PKZIP header: {filename}");
@@ -197,7 +197,7 @@ namespace SabreTools.IO.Compression.Deflate
// Extract the file
var actual = Inflate(source, destination);
if (actual == null)
if (actual is null)
{
if (includeDebug) Console.Error.WriteLine($"Could not extract {filename}");
return ExtractionStatus.FAIL;
@@ -244,7 +244,7 @@ namespace SabreTools.IO.Compression.Deflate
// Extract the file
var actual = Inflate(source, destination);
if (actual == null)
if (actual is null)
{
if (includeDebug) Console.Error.WriteLine($"Could not extract");
return ExtractionStatus.FAIL;

View File

@@ -88,6 +88,8 @@
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0047
#pragma warning disable IDE2002
internal static class InternalConstants
{
internal static readonly int MAX_BITS = 15;

View File

@@ -88,6 +88,10 @@
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0002
#pragma warning disable IDE0049
#pragma warning disable IDE2000
#pragma warning disable IDE2002
internal class SharedUtils
{
/// <summary>

View File

@@ -160,5 +160,4 @@ namespace SabreTools.IO.Compression.Deflate
BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
}
}
}

View File

@@ -1,422 +1,428 @@
// Tree.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2009-October-28 13:29:50>
//
// ------------------------------------------------------------------
//
// This module defines classes for zlib compression and
// decompression. This code is derived 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
#nullable disable
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 =
[
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 =
[
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 = [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 =
[
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,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
];
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,
18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
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,
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, 28
];
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 =
[
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>
/// No side effects. _dist_code[256] and _dist_code[257] are never used.
/// </remarks>
internal static int DistanceCode(int dist)
{
return (dist < 256)
? _dist_code[dist]
: _dist_code[256 + SharedUtils.URShift(dist, 7)];
}
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
// above are the tree nodes sorted by increasing frequency.
// OUT assertions: the field len is set to the optimal bit length, the
// 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)
{
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
int[] extra = staticTree.extraBits;
int base_Renamed = staticTree.extraBase;
int max_length = staticTree.maxLength;
int h; // heap index
int n, m; // iterate over the tree elements
int bits; // bit length
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];
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
if (bits > max_length)
{
bits = max_length; overflow++;
}
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)
xbits = extra[n - base_Renamed];
f = tree[n * 2];
s.opt_len += f * (bits + xbits);
if (stree != null)
s.static_len += f * (stree[n * 2 + 1] + xbits);
}
if (overflow == 0)
return;
// This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase:
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[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];
while (n != 0)
{
m = s.heap[--h];
if (m > max_code)
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;
}
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)
{
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
int elems = staticTree.elems;
int n, m; // iterate over heap elements
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)
{
s.heap[++s.heap_len] = max_code = n;
s.depth[n] = 0;
}
else
{
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);
tree[node * 2] = 1;
s.depth[node] = 0;
s.opt_len--;
if (stree != null)
s.static_len -= stree[node * 2 + 1];
// 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
{
// 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;
// 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)
{
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);
}
// 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)));
}
}
// 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
{
res |= code & 1;
code >>= 1; //SharedUtils.URShift(code, 1);
res <<= 1;
}
while (--len > 0);
return res >> 1;
}
}
}
// Tree.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2009-October-28 13:29:50>
//
// ------------------------------------------------------------------
//
// This module defines classes for zlib compression and
// decompression. This code is derived 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
#nullable disable
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0004
#pragma warning disable IDE0040
#pragma warning disable IDE0047
#pragma warning disable IDE0048
#pragma warning disable IDE2000
#pragma warning disable IDE2003
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 =
[
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 =
[
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 = [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 =
[
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,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
];
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,
18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
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,
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, 28
];
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 =
[
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>
/// No side effects. _dist_code[256] and _dist_code[257] are never used.
/// </remarks>
internal static int DistanceCode(int dist)
{
return (dist < 256)
? _dist_code[dist]
: _dist_code[256 + SharedUtils.URShift(dist, 7)];
}
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
// above are the tree nodes sorted by increasing frequency.
// OUT assertions: the field len is set to the optimal bit length, the
// 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)
{
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
int[] extra = staticTree.extraBits;
int base_Renamed = staticTree.extraBase;
int max_length = staticTree.maxLength;
int h; // heap index
int n, m; // iterate over the tree elements
int bits; // bit length
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];
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
if (bits > max_length)
{
bits = max_length; overflow++;
}
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)
xbits = extra[n - base_Renamed];
f = tree[n * 2];
s.opt_len += f * (bits + xbits);
if (stree is not null)
s.static_len += f * (stree[n * 2 + 1] + xbits);
}
if (overflow == 0)
return;
// This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase:
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[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];
while (n != 0)
{
m = s.heap[--h];
if (m > max_code)
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;
}
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)
{
short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes;
int elems = staticTree.elems;
int n, m; // iterate over heap elements
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)
{
s.heap[++s.heap_len] = max_code = n;
s.depth[n] = 0;
}
else
{
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);
tree[node * 2] = 1;
s.depth[node] = 0;
s.opt_len--;
if (stree is not null)
s.static_len -= stree[node * 2 + 1];
// 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
{
// 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;
// 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)
{
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);
}
// 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)));
}
}
// 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
{
res |= code & 1;
code >>= 1; //SharedUtils.URShift(code, 1);
res <<= 1;
}
while (--len > 0);
return res >> 1;
}
}
}

View File

@@ -28,6 +28,10 @@
#pragma warning disable CS0649
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0001
#pragma warning disable IDE0048
#pragma warning disable IDE2000
#pragma warning disable IDE2002
internal class WorkItem
{
public byte[] buffer;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,128 +1,126 @@
// ZlibConstants.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2009-November-03 18:50:19>
//
// ------------------------------------------------------------------
//
// This module defines constants used by the zlib class library. This
// code is derived from the jzlib implementation of zlib, but
// significantly modified. In keeping with the license for jzlib, the
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
using System;
namespace SabreTools.IO.Compression.Deflate
{
/// <summary>
/// A bunch of constants used in the Zlib interface.
/// </summary>
public static class ZlibConstants
{
/// <summary>
/// The maximum number of window bits for the Deflate algorithm.
/// </summary>
public const int WindowBitsMax = 15; // 32K LZ77 window
/// <summary>
/// The default number of window bits for the Deflate algorithm.
/// </summary>
public const int WindowBitsDefault = WindowBitsMax;
/// <summary>
/// indicates everything is A-OK
/// </summary>
public const int Z_OK = 0;
/// <summary>
/// Indicates that the last operation reached the end of the stream.
/// </summary>
public const int Z_STREAM_END = 1;
/// <summary>
/// The operation ended in need of a dictionary.
/// </summary>
public const int Z_NEED_DICT = 2;
/// <summary>
/// There was an error with the stream - not enough data, not open and readable, etc.
/// </summary>
public const int Z_STREAM_ERROR = -2;
/// <summary>
/// There was an error with the data - not enough data, bad data, etc.
/// </summary>
public const int Z_DATA_ERROR = -3;
/// <summary>
/// There was an error with the working buffer.
/// </summary>
public const int Z_BUF_ERROR = -5;
/// <summary>
/// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
/// </summary>
#if NETCF
public const int WorkingBufferSizeDefault = 8192;
#else
public const int WorkingBufferSizeDefault = 16384;
#endif
/// <summary>
/// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
/// </summary>
public const int WorkingBufferSizeMin = 1024;
}
}
// ZlibConstants.cs
// ------------------------------------------------------------------
//
// 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.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2009-November-03 18:50:19>
//
// ------------------------------------------------------------------
//
// This module defines constants used by the zlib class library. This
// code is derived from the jzlib implementation of zlib, but
// significantly modified. In keeping with the license for jzlib, the
// 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
// 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,
// INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// 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
// Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
// and contributors of zlib.
//
// -----------------------------------------------------------------------
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE2002
/// <summary>
/// A bunch of constants used in the Zlib interface.
/// </summary>
public static class ZlibConstants
{
/// <summary>
/// The maximum number of window bits for the Deflate algorithm.
/// </summary>
public const int WindowBitsMax = 15; // 32K LZ77 window
/// <summary>
/// The default number of window bits for the Deflate algorithm.
/// </summary>
public const int WindowBitsDefault = WindowBitsMax;
/// <summary>
/// indicates everything is A-OK
/// </summary>
public const int Z_OK = 0;
/// <summary>
/// Indicates that the last operation reached the end of the stream.
/// </summary>
public const int Z_STREAM_END = 1;
/// <summary>
/// The operation ended in need of a dictionary.
/// </summary>
public const int Z_NEED_DICT = 2;
/// <summary>
/// There was an error with the stream - not enough data, not open and readable, etc.
/// </summary>
public const int Z_STREAM_ERROR = -2;
/// <summary>
/// There was an error with the data - not enough data, bad data, etc.
/// </summary>
public const int Z_DATA_ERROR = -3;
/// <summary>
/// There was an error with the working buffer.
/// </summary>
public const int Z_BUF_ERROR = -5;
/// <summary>
/// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
/// </summary>
#if NETCF
public const int WorkingBufferSizeDefault = 8192;
#else
public const int WorkingBufferSizeDefault = 16384;
#endif
/// <summary>
/// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
/// </summary>
public const int WorkingBufferSizeMin = 1024;
}
}

View File

@@ -86,12 +86,13 @@
//
// -----------------------------------------------------------------------
using Interop = System.Runtime.InteropServices;
namespace SabreTools.IO.Compression.Deflate
{
#pragma warning disable IDE0001
#pragma warning disable IDE0049
#pragma warning disable IDE2002
/// <summary>
/// A general purpose exception class for exceptions in the Zlib library.
/// </summary>

File diff suppressed because it is too large Load Diff

View File

@@ -26,7 +26,7 @@ namespace SabreTools.IO.Compression.LZX
/* 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_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;

View File

@@ -58,7 +58,7 @@ namespace SabreTools.IO.Compression.MSZIP
byte[] buffer = new byte[_bufferSize];
var blockStream = new Deflate.DeflateStream(source, Deflate.CompressionMode.Decompress, leaveOpen: true);
if (_history != null)
if (_history is not null)
blockStream.SetDictionary(_history, check: false);
int read = blockStream.Read(buffer, 0, _bufferSize);

View File

@@ -283,9 +283,9 @@ namespace SabreTools.IO.Compression.Quantum
/// </summary>
private void GetCode(int prevFrequency, int currentFrequency, int totalFrequency)
{
uint range = (ushort)((CS_H - CS_L) + 1);
CS_H = (ushort)(CS_L + (prevFrequency * range) / totalFrequency - 1);
CS_L = (ushort)(CS_L + (currentFrequency * range) / totalFrequency);
uint range = (ushort)(CS_H - CS_L + 1);
CS_H = (ushort)(CS_L + (prevFrequency * range / totalFrequency) - 1);
CS_L = (ushort)(CS_L + (currentFrequency * range / totalFrequency));
while (true)
{
@@ -306,7 +306,7 @@ namespace SabreTools.IO.Compression.Quantum
CS_L <<= 1;
CS_H = (ushort)((CS_H << 1) | 1);
CS_C = (ushort)((CS_C << 1) | _bitStream.ReadBit() ?? 0);
CS_C = (ushort)((CS_C << 1) | (_bitStream.ReadBit() ?? 0));
}
}

View File

@@ -143,7 +143,7 @@ namespace SabreTools.IO.Compression.SZDD
{
// Get the control byte
byte? control = _source.ReadNextByte();
if (control == null)
if (control is null)
break;
for (int cbit = 0x01; (cbit & 0xFF) != 0; cbit <<= 1)
@@ -153,7 +153,7 @@ namespace SabreTools.IO.Compression.SZDD
{
// Read the literal byte
byte? literal = _source.ReadNextByte();
if (literal == null)
if (literal is null)
break;
// Store the data in the window and write
@@ -168,12 +168,12 @@ namespace SabreTools.IO.Compression.SZDD
// Read the match position
int? matchpos = _source.ReadNextByte();
if (matchpos == null)
if (matchpos is null)
break;
// Read the match length
int? matchlen = _source.ReadNextByte();
if (matchlen == null)
if (matchlen is null)
break;
// Adjust the position and length
@@ -213,7 +213,7 @@ namespace SabreTools.IO.Compression.SZDD
{
// Read the next byte
byte? next = _source.ReadNextByte();
if (next == null)
if (next is null)
break;
// XOR with 0xFF if required

View File

@@ -23,7 +23,7 @@ namespace SabreTools.IO.Compression.zlib
public static void free(void* a)
{
if (a == null)
if (a is null)
return;
var ptr = new IntPtr(a);
@@ -57,7 +57,7 @@ namespace SabreTools.IO.Compression.zlib
finally
{
if (temp != null)
if (temp is not null)
free(temp);
}
}
@@ -117,7 +117,7 @@ namespace SabreTools.IO.Compression.zlib
public static void* realloc(void* a, long newSize)
{
if (a == null)
if (a is null)
return malloc(newSize);
var ptr = new IntPtr(a);
@@ -207,7 +207,7 @@ namespace SabreTools.IO.Compression.zlib
--length;
}
if (end != null)
if (end is not null)
{
*end = ptr;
}

View File

@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading;
namespace SabreTools.IO.Compression.zlib
{
#pragma warning disable IDE0036
#pragma warning disable IDE0380
public unsafe static class MemoryStats
{
private static int _allocations;

View File

@@ -2,6 +2,7 @@
namespace SabreTools.IO.Compression.zlib
{
#pragma warning disable IDE0044
public unsafe class UnsafeArray2D<T> where T : struct
{
private readonly UnsafeArray1D<T>[] _data;

View File

@@ -3,6 +3,10 @@ using System.IO;
namespace SabreTools.IO.Compression.zlib
{
#pragma warning disable IDE0036
#pragma warning disable IDE0380
#pragma warning disable IDE2002
#pragma warning disable IDE2003
public class ZlibDeflateStream : Stream
{
private readonly bool _leaveOpen;
@@ -70,14 +74,14 @@ namespace SabreTools.IO.Compression.zlib
unsafe public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null) throw new ArgumentNullException();
if (buffer is null) throw new ArgumentNullException();
if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
if ((offset + count) > buffer.Length) throw new ArgumentException();
int err = 0;
int hdr = 0;
if (_s == null)
if (_s is null)
{
_s = new ZLib.z_stream_s();
ZLib.deflateInit_(_s, this.Level, this.Version, 0); //0 = sizeof(z_stream_s) not used
@@ -131,7 +135,7 @@ namespace SabreTools.IO.Compression.zlib
unsafe public long BlockFlush()
{
//finish previous stream
if (_s != null)
if (_s is not null)
{
int err = 0;
fixed (byte* o = _b)

View File

@@ -3,6 +3,10 @@ using System.IO;
namespace SabreTools.IO.Compression.zlib
{
#pragma warning disable IDE0004
#pragma warning disable IDE0036
#pragma warning disable IDE0047
#pragma warning disable IDE2003
public class ZlibInflateStream : Stream
{
private readonly bool _leaveOpen;
@@ -76,7 +80,7 @@ namespace SabreTools.IO.Compression.zlib
unsafe public override int Read(byte[] buffer, int offset, int count)
{
if (buffer == null) throw new ArgumentNullException();
if (buffer is null) throw new ArgumentNullException();
if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
if ((offset + count) > buffer.Length) throw new ArgumentException();
if (_complete)
@@ -84,7 +88,7 @@ namespace SabreTools.IO.Compression.zlib
int err = 0;
int hdr = 0;
if (_s == null)
if (_s is null)
{
_s = new ZLib.z_stream_s();
ZLib.inflateInit_(_s, this.Version, 0); //0 = sizeof(z_stream_s) not used
@@ -143,7 +147,7 @@ namespace SabreTools.IO.Compression.zlib
public long BlockFlush(int maxRead)
{
this.MaxRead = maxRead;
if (_s != null)
if (_s is not null)
{
ZLib.deflateEnd(_s);
_s = null;

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,6 @@
namespace SabreTools.IO.Compression.zlib
{
#pragma warning disable IDE0047
public static class zlibConst
{
public const int Z_NO_FLUSH = 0;

View File

@@ -4,6 +4,7 @@ using System.Text;
using SabreTools.Hashing;
using SabreTools.IO.Extensions;
#pragma warning disable IDE0051
namespace SabreTools.IO.Encryption
{
/// <summary>
@@ -123,13 +124,13 @@ namespace SabreTools.IO.Encryption
{
for (uint index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
{
seed = (seed * 125 + 3) % 0x2AAAAB;
seed = ((seed * 125) + 3) % 0x2AAAAB;
uint temp1 = (seed & 0xFFFF) << 0x10;
seed = (seed * 125 + 3) % 0x2AAAAB;
uint temp2 = (seed & 0xFFFF);
seed = ((seed * 125) + 3) % 0x2AAAAB;
uint temp2 = seed & 0xFFFF;
_stormBuffer[index2] = (temp1 | temp2);
_stormBuffer[index2] = temp1 | temp2;
}
}
}
@@ -199,7 +200,7 @@ namespace SabreTools.IO.Encryption
// Verify the MD5 of the table, if present
byte[]? actualHash = HashTool.GetByteArrayHashArray(readBytes, HashType.MD5);
if (expectedHash != null && actualHash != null && !actualHash.EqualsExactly(expectedHash))
if (expectedHash is not null && actualHash is not null && !actualHash.EqualsExactly(expectedHash))
{
Console.WriteLine("Table is corrupt!");
return null;
@@ -232,7 +233,7 @@ namespace SabreTools.IO.Encryption
/// <summary>
/// Decrypt a single block of data
/// </summary>
public unsafe byte[] DecryptBlock(byte[] block, long length, uint key)
public byte[] DecryptBlock(byte[] block, long length, uint key)
{
uint seed = 0xEEEEEEEE;

View File

@@ -148,9 +148,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24BigEndian(this BinaryReader reader)
{
byte[] buffer = reader.ReadBytes(3);
return (int)(buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16));
return buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16);
}
/// <summary>
@@ -160,9 +160,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24LittleEndian(this BinaryReader reader)
{
byte[] buffer = reader.ReadBytes(3);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16);
}
/// <summary>
@@ -206,10 +206,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32BigEndian(this BinaryReader reader)
{
byte[] buffer = reader.ReadBytes(4);
return (int)(buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24));
return buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24);
}
/// <inheritdoc cref="BinaryReader.ReadInt32"/>
@@ -217,10 +217,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32LittleEndian(this BinaryReader reader)
{
byte[] buffer = reader.ReadBytes(4);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24);
}
/// <inheritdoc cref="BinaryReader.ReadInt32"/>
@@ -815,7 +815,7 @@ namespace SabreTools.IO.Extensions
{
// Try to create an instance of the type
var instance = Activator.CreateInstance(type);
if (instance == null)
if (instance is null)
return null;
// Get the layout information
@@ -836,7 +836,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
reader.BaseStream.Seek(currentOffset + fieldOffset?.Value ?? 0, SeekOrigin.Begin);
reader.BaseStream.Seek(currentOffset + (fieldOffset?.Value ?? 0), SeekOrigin.Begin);
}
SetField(reader, encoding, fields, instance, fi);
@@ -881,7 +881,7 @@ namespace SabreTools.IO.Extensions
private static Array ReadArrayType(BinaryReader reader, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return new object[0];
// Get the number of elements expected
@@ -897,7 +897,7 @@ namespace SabreTools.IO.Extensions
for (int i = 0; i < elementCount; i++)
{
var value = ReadType(reader, elementType);
if (value != null && elementType.IsEnum)
if (value is not null && elementType.IsEnum)
arr.SetValue(Enum.ToObject(elementType, value), i);
else
arr.SetValue(value, i);
@@ -913,7 +913,7 @@ namespace SabreTools.IO.Extensions
private static string? ReadStringType(BinaryReader reader, Encoding encoding, FieldInfo? fi)
{
// If the FieldInfo is null
if (fi == null)
if (fi is null)
return null;
// Get all MarshalAs attributes for the field, if possible
@@ -923,6 +923,7 @@ namespace SabreTools.IO.Extensions
// Use the first found attribute
var marshalAsAttr = attributes[0] as MarshalAsAttribute;
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -954,6 +955,7 @@ namespace SabreTools.IO.Extensions
default:
return null;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -400,7 +400,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedString(this BinaryWriter writer, string? value, Encoding encoding)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Add the null terminator and write
@@ -453,7 +453,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedAnsiString(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -473,7 +473,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedLatin1String(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -493,7 +493,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedUnicodeString(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -512,7 +512,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedBigEndianUnicodeString(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -553,7 +553,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteType(this BinaryWriter writer, object? value, Type type)
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Handle special struct cases
@@ -589,7 +589,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
int typeSize = Marshal.SizeOf(type);
@@ -617,7 +617,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Get the layout information
@@ -638,7 +638,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
writer.BaseStream.Seek(currentOffset + fieldOffset?.Value ?? 0, SeekOrigin.Begin);
writer.BaseStream.Seek(currentOffset + (fieldOffset?.Value ?? 0), SeekOrigin.Begin);
}
if (!GetField(writer, encoding, fields, value, fi))
@@ -679,7 +679,7 @@ namespace SabreTools.IO.Extensions
private static bool WriteArrayType(BinaryWriter writer, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return false;
// Get the array
@@ -714,6 +714,7 @@ namespace SabreTools.IO.Extensions
if (fi.GetValue(instance) is not string fieldValue)
return true;
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -748,6 +749,7 @@ namespace SabreTools.IO.Extensions
default:
return false;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -13,7 +13,7 @@ namespace SabreTools.IO.Extensions
/// </summary>
public static bool IsNullOrEmpty(this Array? array)
{
return array == null || array.Length == 0;
return array is null || array.Length == 0;
}
/// <summary>
@@ -358,7 +358,7 @@ namespace SabreTools.IO.Extensions
byte bitMask = (byte)(8 - numBits), carry, wrap = 0;
for (int i = 0; i < output.Length; i++)
{
carry = (byte)((255 << bitMask & output[i]) >> bitMask);
carry = (byte)(((255 << bitMask) & output[i]) >> bitMask);
// Make sure the first byte carries to the end
if (i == 0)
@@ -428,7 +428,7 @@ namespace SabreTools.IO.Extensions
public static string? ToHexString(this byte[]? bytes)
{
// If we get null in, we send null out
if (bytes == null)
if (bytes is null)
return null;
string hex = BitConverter.ToString(bytes);
@@ -474,7 +474,7 @@ namespace SabreTools.IO.Extensions
public static List<string>? ReadStringsFrom(this byte[]? input, int charLimit = 5)
{
// Validate the data
if (input == null || input.Length == 0)
if (input is null || input.Length == 0)
return null;
#if NET5_0_OR_GREATER
@@ -511,7 +511,7 @@ namespace SabreTools.IO.Extensions
public static HashSet<string> ReadStringsWithEncoding(this byte[]? bytes, int charLimit, Encoding encoding)
#endif
{
if (bytes == null || bytes.Length == 0)
if (bytes is null || bytes.Length == 0)
return [];
if (charLimit <= 0 || charLimit > bytes.Length)
return [];

View File

@@ -239,9 +239,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24BigEndian(this byte[] content, ref int offset)
{
byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3);
return (int)(buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16));
return buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16);
}
/// <summary>
@@ -251,9 +251,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24LittleEndian(this byte[] content, ref int offset)
{
byte[] buffer = ReadExactlyToBuffer(content, ref offset, 3);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16);
}
/// <summary>
@@ -311,10 +311,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32BigEndian(this byte[] content, ref int offset)
{
byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4);
return (int)(buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24));
return buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24);
}
/// <summary>
@@ -324,10 +324,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32LittleEndian(this byte[] content, ref int offset)
{
byte[] buffer = ReadExactlyToBuffer(content, ref offset, 4);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24);
}
/// <summary>
@@ -1041,7 +1041,7 @@ namespace SabreTools.IO.Extensions
{
// Try to create an instance of the type
var instance = Activator.CreateInstance(type);
if (instance == null)
if (instance is null)
return null;
// Get the layout information
@@ -1062,7 +1062,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
offset = currentOffset + fieldOffset?.Value ?? 0;
offset = currentOffset + (fieldOffset?.Value ?? 0);
}
SetField(content, ref offset, encoding, fields, instance, fi);
@@ -1107,7 +1107,7 @@ namespace SabreTools.IO.Extensions
private static Array ReadArrayType(byte[] content, ref int offset, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return new object[0];
// Get the number of elements expected
@@ -1123,7 +1123,7 @@ namespace SabreTools.IO.Extensions
for (int i = 0; i < elementCount; i++)
{
var value = ReadType(content, ref offset, elementType);
if (value != null && elementType.IsEnum)
if (value is not null && elementType.IsEnum)
arr.SetValue(Enum.ToObject(elementType, value), i);
else
arr.SetValue(value, i);
@@ -1140,6 +1140,7 @@ namespace SabreTools.IO.Extensions
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -1171,6 +1172,7 @@ namespace SabreTools.IO.Extensions
default:
return null;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -568,7 +568,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedString(this byte[] content, ref int offset, string? value, Encoding encoding)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Add the null terminator and write
@@ -621,7 +621,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedAnsiString(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -642,7 +642,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedLatin1String(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -663,7 +663,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedUnicodeString(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -683,7 +683,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedBigEndianUnicodeString(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -725,7 +725,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteType(this byte[] content, ref int offset, object? value, Type type)
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Handle special struct cases
@@ -758,7 +758,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
int typeSize = Marshal.SizeOf(type);
@@ -786,7 +786,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Get the layout information
@@ -807,7 +807,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
offset = currentOffset + fieldOffset?.Value ?? 0;
offset = currentOffset + (fieldOffset?.Value ?? 0);
}
if (!GetField(content, ref offset, encoding, fields, value, fi))
@@ -848,7 +848,7 @@ namespace SabreTools.IO.Extensions
private static bool WriteArrayType(byte[] content, ref int offset, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return false;
// Get the array
@@ -883,6 +883,7 @@ namespace SabreTools.IO.Extensions
if (fi.GetValue(instance) is not string fieldValue)
return true;
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -916,6 +917,7 @@ namespace SabreTools.IO.Extensions
default:
return false;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -50,7 +50,7 @@ namespace SabreTools.IO.Extensions
try
{
var file = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (file == null)
if (file is null)
return Encoding.Default;
// Read the BOM
@@ -59,7 +59,9 @@ namespace SabreTools.IO.Extensions
file.Dispose();
// Disable warning about UTF7 usage
#if NET5_0_OR_GREATER
#pragma warning disable SYSLIB0001
#endif
// Analyze the BOM
if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7;
@@ -69,7 +71,10 @@ namespace SabreTools.IO.Extensions
if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
return Encoding.Default;
// Restore warning about UTF7 usage
#if NET5_0_OR_GREATER
#pragma warning restore SYSLIB0001
#endif
}
catch
{
@@ -109,7 +114,7 @@ namespace SabreTools.IO.Extensions
public static List<string>? ListEmpty(this string? root)
{
// Check null or empty first
if (root == null)
if (root is null)
return null;
// Then, check if the root exists

View File

@@ -17,12 +17,12 @@ namespace SabreTools.IO.Extensions
public static TAttribute? GetAttribute<TAttribute>(FieldInfo? fi) where TAttribute : Attribute
{
// If the field info is invalid
if (fi == null)
if (fi is null)
return null;
// Get all matching attributes
var attributes = fi.GetCustomAttributes(typeof(TAttribute), true);
if (attributes == null || attributes.Length == 0)
if (attributes is null || attributes.Length == 0)
return null;
// Get the first attribute that matches
@@ -35,12 +35,12 @@ namespace SabreTools.IO.Extensions
public static TAttribute? GetAttribute<TAttribute>(Type? type) where TAttribute : Attribute
{
// If the field info is invalid
if (type == null)
if (type is null)
return null;
// Get all matching attributes
var attributes = type.GetCustomAttributes(typeof(TAttribute), true);
if (attributes == null || attributes.Length == 0)
if (attributes is null || attributes.Length == 0)
return null;
// Get the first attribute that matches
@@ -54,7 +54,7 @@ namespace SabreTools.IO.Extensions
{
LayoutKind layoutKind = LayoutKind.Auto;
if (layoutAttr != null)
if (layoutAttr is not null)
layoutKind = layoutAttr.Value;
else if (type.IsAutoLayout)
layoutKind = LayoutKind.Auto;
@@ -156,7 +156,7 @@ namespace SabreTools.IO.Extensions
// Get the size field
var sizeField = fields[marshalAsAttr.SizeParamIndex];
if (sizeField == null)
if (sizeField is null)
return -1;
// Cast based on the field type

View File

@@ -14,7 +14,7 @@ namespace SabreTools.IO.Extensions
public static bool AlignToBoundary(this Stream? input, int alignment)
{
// If the stream is invalid
if (input == null || input.Length == 0 || !input.CanRead)
if (input is null || input.Length == 0 || !input.CanRead)
return false;
// If already at the end of the stream
@@ -46,7 +46,7 @@ namespace SabreTools.IO.Extensions
/// </remarks>
public static byte[]? ReadFrom(this Stream? input, long offset, int length, bool retainPosition)
{
if (input == null || !input.CanRead || !input.CanSeek)
if (input is null || !input.CanRead || !input.CanSeek)
return null;
if (offset < 0 || offset >= input.Length)
return null;
@@ -88,7 +88,7 @@ namespace SabreTools.IO.Extensions
{
// Read the data as a byte array first
byte[]? data = input.ReadFrom(position, length, retainPosition: true);
if (data == null)
if (data is null)
return null;
return data.ReadStringsFrom(charLimit);
@@ -141,7 +141,7 @@ namespace SabreTools.IO.Extensions
/// <returns>True if segment could be read fully, false otherwise</returns>
public static bool SegmentValid(this Stream? input, long offset, long count)
{
if (input == null)
if (input is null)
return false;
if (offset < 0 || offset > input.Length)
return false;

View File

@@ -234,9 +234,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24BigEndian(this Stream stream)
{
byte[] buffer = ReadExactlyToBuffer(stream, 3);
return (int)(buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16));
return buffer[2]
| (buffer[1] << 8)
| (buffer[0] << 16);
}
/// <summary>
@@ -246,9 +246,9 @@ namespace SabreTools.IO.Extensions
public static int ReadInt24LittleEndian(this Stream stream)
{
byte[] buffer = ReadExactlyToBuffer(stream, 3);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16);
}
/// <summary>
@@ -306,10 +306,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32BigEndian(this Stream stream)
{
byte[] buffer = ReadExactlyToBuffer(stream, 4);
return (int)(buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24));
return buffer[3]
| (buffer[2] << 8)
| (buffer[1] << 16)
| (buffer[0] << 24);
}
/// <summary>
@@ -319,10 +319,10 @@ namespace SabreTools.IO.Extensions
public static int ReadInt32LittleEndian(this Stream stream)
{
byte[] buffer = ReadExactlyToBuffer(stream, 4);
return (int)(buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24));
return buffer[0]
| (buffer[1] << 8)
| (buffer[2] << 16)
| (buffer[3] << 24);
}
/// <summary>
@@ -1036,7 +1036,7 @@ namespace SabreTools.IO.Extensions
{
// Try to create an instance of the type
var instance = Activator.CreateInstance(type);
if (instance == null)
if (instance is null)
return null;
// Get the layout information
@@ -1057,7 +1057,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
stream.Seek(currentOffset + fieldOffset?.Value ?? 0, SeekOrigin.Begin);
stream.Seek(currentOffset + (fieldOffset?.Value ?? 0), SeekOrigin.Begin);
}
SetField(stream, encoding, fields, instance, fi);
@@ -1102,7 +1102,7 @@ namespace SabreTools.IO.Extensions
private static Array ReadArrayType(Stream stream, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return new object[0];
// Get the number of elements expected
@@ -1118,7 +1118,7 @@ namespace SabreTools.IO.Extensions
for (int i = 0; i < elementCount; i++)
{
var value = ReadType(stream, elementType);
if (value != null && elementType.IsEnum)
if (value is not null && elementType.IsEnum)
arr.SetValue(Enum.ToObject(elementType, value), i);
else
arr.SetValue(value, i);
@@ -1135,6 +1135,7 @@ namespace SabreTools.IO.Extensions
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -1166,6 +1167,7 @@ namespace SabreTools.IO.Extensions
default:
return null;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -569,7 +569,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedString(this Stream stream, string? value, Encoding encoding)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Add the null terminator and write
@@ -622,7 +622,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedAnsiString(this Stream stream, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -643,7 +643,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedLatin1String(this Stream stream, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -664,7 +664,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedUnicodeString(this Stream stream, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -684,7 +684,7 @@ namespace SabreTools.IO.Extensions
public static bool WritePrefixedBigEndianUnicodeString(this Stream stream, string? value)
{
// If the value is null
if (value == null)
if (value is null)
return false;
// Get the buffer
@@ -726,7 +726,7 @@ namespace SabreTools.IO.Extensions
public static bool WriteType(this Stream stream, object? value, Type type)
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Handle special struct cases
@@ -759,7 +759,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
int typeSize = Marshal.SizeOf(type);
@@ -787,7 +787,7 @@ namespace SabreTools.IO.Extensions
try
{
// Null values cannot be written
if (value == null)
if (value is null)
return true;
// Get the layout information
@@ -808,7 +808,7 @@ namespace SabreTools.IO.Extensions
if (layoutKind == LayoutKind.Explicit)
{
var fieldOffset = MarshalHelpers.GetAttribute<FieldOffsetAttribute>(fi);
stream.Seek(currentOffset + fieldOffset?.Value ?? 0, SeekOrigin.Begin);
stream.Seek(currentOffset + (fieldOffset?.Value ?? 0), SeekOrigin.Begin);
}
if (!GetField(stream, encoding, fields, value, fi))
@@ -849,7 +849,7 @@ namespace SabreTools.IO.Extensions
private static bool WriteArrayType(Stream stream, FieldInfo[] fields, object instance, FieldInfo fi)
{
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
if (marshalAsAttr == null)
if (marshalAsAttr is null)
return false;
// Get the array
@@ -885,6 +885,7 @@ namespace SabreTools.IO.Extensions
if (fi.GetValue(instance) is not string fieldValue)
return true;
#pragma warning disable IDE0010
switch (marshalAsAttr?.Value)
{
case UnmanagedType.AnsiBStr:
@@ -918,6 +919,7 @@ namespace SabreTools.IO.Extensions
default:
return false;
}
#pragma warning restore IDE0010
}
/// <summary>

View File

@@ -11,7 +11,7 @@ namespace SabreTools.IO.Extensions
/// <inheritdoc cref="string.Contains(string, StringComparison)"/>
public static bool OptionalContains(this string? self, string value, StringComparison comparisonType)
{
if (self == null)
if (self is null)
return false;
#if NETFRAMEWORK || NETSTANDARD2_0
@@ -28,7 +28,7 @@ namespace SabreTools.IO.Extensions
/// <inheritdoc cref="string.EndsWith(string, StringComparison)"/>
public static bool OptionalEndsWith(this string? self, string value, StringComparison comparisonType)
{
if (self == null)
if (self is null)
return false;
return self.EndsWith(value, comparisonType);
@@ -41,7 +41,7 @@ namespace SabreTools.IO.Extensions
/// <inheritdoc cref="string.Equals(string, StringComparison)"/>
public static bool OptionalEquals(this string? self, string value, StringComparison comparisonType)
{
if (self == null)
if (self is null)
return false;
return self.Equals(value, comparisonType);
@@ -54,7 +54,7 @@ namespace SabreTools.IO.Extensions
/// <inheritdoc cref="string.StartsWith(string, StringComparison)"/>
public static bool OptionalStartsWith(this string? self, string value, StringComparison comparisonType)
{
if (self == null)
if (self is null)
return false;
return self.StartsWith(value, comparisonType);

View File

@@ -18,7 +18,7 @@ namespace SabreTools.IO.Extensions
public static void WriteRequiredAttributeString(this XmlTextWriter writer, string localName, string? value, bool throwOnError = false)
{
// Throw an exception if we are configured to
if (value == null && throwOnError)
if (value is null && throwOnError)
throw new ArgumentNullException(nameof(value));
writer.WriteAttributeString(localName, value ?? string.Empty);
@@ -34,11 +34,11 @@ namespace SabreTools.IO.Extensions
public static void WriteRequiredElementString(this XmlTextWriter writer, string localName, string? value, bool throwOnError = false)
{
// Throw an exception if we are configured to
if (value == null && throwOnError)
if (value is null && throwOnError)
throw new ArgumentNullException(nameof(value));
writer.WriteStartElement(localName);
if (value == null)
if (value is null)
writer.WriteRaw(string.Empty);
else
writer.WriteString(value);

View File

@@ -161,7 +161,7 @@ namespace SabreTools.IO
private bool Parse(Stream? stream)
{
// If the stream is invalid or unreadable, we can't process it
if (stream == null || !stream.CanRead || stream.Position >= stream.Length - 1)
if (stream is null || !stream.CanRead || stream.Position >= stream.Length - 1)
return false;
// Keys are case-insensitive by default
@@ -195,6 +195,9 @@ namespace SabreTools.IO
this[key] = reader.KeyValuePair?.Value;
break;
case IniRowType.None:
case IniRowType.Comment:
case IniRowType.Invalid:
default:
// No-op
break;

View File

@@ -8,6 +8,6 @@
/// <summary>
/// Nullable typed data to be matched
/// </summary>
T? Needle { get; }
public T? Needle { get; }
}
}

View File

@@ -212,11 +212,11 @@ namespace SabreTools.IO.Logging
public static void HandleLogEvent(object? sender, LogEventArgs args)
{
// Null args means we can't handle it
if (args == null)
if (args is null)
return;
// If we have an exception and we're throwing on that
if (ThrowOnError && args.Exception != null)
if (ThrowOnError && args.Exception is not null)
throw args.Exception;
// If we have a warning or error, set the flags accordingly
@@ -227,14 +227,14 @@ namespace SabreTools.IO.Logging
// Setup the statement based on the inputs
string logLine;
if (args.Exception != null)
if (args.Exception is not null)
{
logLine = $"{(args.Statement != null ? args.Statement + ": " : string.Empty)}{args.Exception}";
logLine = $"{(args.Statement is not null ? args.Statement + ": " : string.Empty)}{args.Exception}";
}
else if (args.TotalCount != null && args.CurrentCount != null)
else if (args.TotalCount is not null && args.CurrentCount is not null)
{
double percentage = ((double)args.CurrentCount.Value / args.TotalCount.Value) * 100;
logLine = $"{percentage:N2}%{(args.Statement != null ? ": " + args.Statement : string.Empty)}";
double percentage = (double)args.CurrentCount.Value / args.TotalCount.Value * 100;
logLine = $"{percentage:N2}%{(args.Statement is not null ? ": " + args.Statement : string.Empty)}";
}
else
{

View File

@@ -44,7 +44,7 @@ namespace SabreTools.IO
bool includeDebug = false)
{
var contentMatches = FindAllMatches(file, stack, matchSets, any, includeDebug, true);
if (contentMatches == null || contentMatches.Count == 0)
if (contentMatches is null || contentMatches.Count == 0)
return null;
return contentMatches[0];
@@ -68,7 +68,7 @@ namespace SabreTools.IO
bool stopAfterFirst)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || matchSets.Count == 0)
if (stack is null || stack.Length == 0 || matchSets.Count == 0)
return [];
// Initialize the list of matches
@@ -91,11 +91,11 @@ namespace SabreTools.IO
matchString.Append(matcher.SetName);
// Invoke the version delegate, if it exists
if (matcher.GetArrayVersion != null)
if (matcher.GetArrayVersion is not null)
{
// A null version returned means the check didn't pass at the version step
var version = matcher.GetArrayVersion(file, stack, positions);
if (version == null)
if (version is null)
continue;
// Trim and add the version
@@ -158,7 +158,7 @@ namespace SabreTools.IO
bool includeDebug = false)
{
var contentMatches = FindAllMatches(file, stack, matchSets, any, includeDebug, true);
if (contentMatches == null || contentMatches.Count == 0)
if (contentMatches is null || contentMatches.Count == 0)
return null;
return contentMatches[0];
@@ -182,7 +182,7 @@ namespace SabreTools.IO
bool stopAfterFirst)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || matchSets.Count == 0)
if (stack is null || stack.Length == 0 || matchSets.Count == 0)
return [];
// Initialize the list of matches
@@ -205,11 +205,11 @@ namespace SabreTools.IO
matchString.Append(matcher.SetName);
// Invoke the version delegate, if it exists
if (matcher.GetStreamVersion != null)
if (matcher.GetStreamVersion is not null)
{
// A null version returned means the check didn't pass at the version step
var version = matcher.GetStreamVersion(file, stack, positions);
if (version == null)
if (version is null)
continue;
// Trim and add the version
@@ -270,7 +270,7 @@ namespace SabreTools.IO
public static string? GetFirstMatch(string stack, List<PathMatchSet> matchSets, bool any = false)
{
var contentMatches = FindAllMatches([stack], matchSets, any, true);
if (contentMatches == null || contentMatches.Count == 0)
if (contentMatches is null || contentMatches.Count == 0)
return null;
return contentMatches[0];
@@ -286,7 +286,7 @@ namespace SabreTools.IO
public static string? GetFirstMatch(List<string> stack, List<PathMatchSet> matchSets, bool any = false)
{
var contentMatches = FindAllMatches(stack, matchSets, any, true);
if (contentMatches == null || contentMatches.Count == 0)
if (contentMatches is null || contentMatches.Count == 0)
return null;
return contentMatches[0];
@@ -303,7 +303,7 @@ namespace SabreTools.IO
private static List<string> FindAllMatches(List<string>? stack, List<PathMatchSet> matchSets, bool any, bool stopAfterFirst)
{
// If either set is null or empty
if (stack == null || stack.Count == 0 || matchSets.Count == 0)
if (stack is null || stack.Count == 0 || matchSets.Count == 0)
return [];
// Initialize the list of matches
@@ -317,7 +317,7 @@ namespace SabreTools.IO
if (any)
{
string? anyMatch = matcher.MatchesAny(stack);
if (anyMatch != null)
if (anyMatch is not null)
matches = [anyMatch];
}
else
@@ -334,11 +334,11 @@ namespace SabreTools.IO
matchString.Append(matcher.SetName);
// Invoke the version delegate, if it exists
if (matcher.GetVersion != null)
if (matcher.GetVersion is not null)
{
// A null version returned means the check didn't pass at the version step
var version = matcher.GetVersion(matches[0], stack);
if (version == null)
if (version is null)
continue;
// Trim and add the version

View File

@@ -91,7 +91,7 @@ namespace SabreTools.IO.Matching
public int Match(byte[]? stack, bool reverse = false)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Needle.Length == 0)
if (stack is null || stack.Length == 0 || Needle.Length == 0)
return -1;
// Get the adjusted end value for comparison
@@ -186,7 +186,7 @@ namespace SabreTools.IO.Matching
for (int i = 0; i < Needle.Length; i++)
{
// A null value is a wildcard
if (Needle[i] == null)
if (Needle[i] is null)
continue;
else if (stack[i + index] != Needle[i])
return false;
@@ -208,7 +208,7 @@ namespace SabreTools.IO.Matching
public int Match(Stream? stack, bool reverse = false)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Needle.Length == 0)
if (stack is null || stack.Length == 0 || Needle.Length == 0)
return -1;
// Get the adjusted end value for comparison
@@ -312,7 +312,7 @@ namespace SabreTools.IO.Matching
byte stackValue = (byte)stack.ReadByte();
// A null value is a wildcard
if (Needle[i] == null)
if (Needle[i] is null)
{
continue;
}

View File

@@ -140,7 +140,7 @@ namespace SabreTools.IO.Matching
public List<int> MatchesAll(byte[]? stack)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Matchers.Count == 0)
if (stack is null || stack.Length == 0 || Matchers.Count == 0)
return [];
// Initialize the position list
@@ -167,7 +167,7 @@ namespace SabreTools.IO.Matching
public int MatchesAny(byte[]? stack)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Matchers.Count == 0)
if (stack is null || stack.Length == 0 || Matchers.Count == 0)
return -1;
// Loop through all content matches and make sure all pass
@@ -193,7 +193,7 @@ namespace SabreTools.IO.Matching
public List<int> MatchesAll(Stream? stack)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Matchers.Count == 0)
if (stack is null || stack.Length == 0 || Matchers.Count == 0)
return [];
// Initialize the position list
@@ -220,7 +220,7 @@ namespace SabreTools.IO.Matching
public int MatchesAny(Stream? stack)
{
// If either set is null or empty
if (stack == null || stack.Length == 0 || Matchers.Count == 0)
if (stack is null || stack.Length == 0 || Matchers.Count == 0)
return -1;
// Loop through all content matches and make sure all pass

View File

@@ -58,7 +58,7 @@ namespace SabreTools.IO.Matching
/// <param name="stack">Array of strings to search for the given content</param>
/// <returns>Matched item on success, null on error</returns>
public string? Match(string[]? stack)
=> Match(stack == null ? null : new List<string>(stack));
=> Match(stack is null ? null : new List<string>(stack));
/// <summary>
/// Get if this match can be found in a stack
@@ -68,7 +68,7 @@ namespace SabreTools.IO.Matching
public string? Match(List<string>? stack)
{
// If either set is null or empty
if (stack == null || stack.Count == 0 || Needle.Length == 0)
if (stack is null || stack.Count == 0 || Needle.Length == 0)
return null;
// Preprocess the needle, if necessary

View File

@@ -92,7 +92,7 @@ namespace SabreTools.IO.Matching
/// <param name="stack">List of strings to search for the given content</param>
/// <returns>Matched item on success, null on error</returns>
public List<string> MatchesAll(string[]? stack)
=> MatchesAll(stack == null ? null : new List<string>(stack));
=> MatchesAll(stack is null ? null : new List<string>(stack));
/// <summary>
/// Determine whether all path matches pass
@@ -102,7 +102,7 @@ namespace SabreTools.IO.Matching
public List<string> MatchesAll(List<string>? stack)
{
// If either set is null or empty, we can't do anything
if (stack == null || stack.Count == 0 || Matchers.Count == 0)
if (stack is null || stack.Count == 0 || Matchers.Count == 0)
return [];
// Initialize the value list
@@ -112,7 +112,7 @@ namespace SabreTools.IO.Matching
foreach (var pathMatch in Matchers)
{
string? value = pathMatch.Match(stack);
if (value == null)
if (value is null)
return [];
else
values.Add(value);
@@ -127,7 +127,7 @@ namespace SabreTools.IO.Matching
/// <param name="stack">List of strings to search for the given content</param>
/// <returns>Matched item on success, null on error</returns>
public string? MatchesAny(string[]? stack)
=> MatchesAny(stack == null ? null : new List<string>(stack));
=> MatchesAny(stack is null ? null : new List<string>(stack));
/// <summary>
/// Determine whether any path matches pass
@@ -137,14 +137,14 @@ namespace SabreTools.IO.Matching
public string? MatchesAny(List<string>? stack)
{
// If either set is null or empty, we can't do anything
if (stack == null || stack.Count == 0 || Matchers.Count == 0)
if (stack is null || stack.Count == 0 || Matchers.Count == 0)
return null;
// Loop through all path matches and make sure all pass
foreach (var pathMatch in Matchers)
{
string? value = pathMatch.Match(stack);
if (value != null)
if (value is not null)
return value;
}

View File

@@ -26,15 +26,15 @@ namespace SabreTools.Numerics
public static BothInt16 operator +(BothInt16 a)
{
short le = (short)(+a.LittleEndian);
short be = (short)(+a.BigEndian);
short le = (short)+a.LittleEndian;
short be = (short)+a.BigEndian;
return new BothInt16(le, be);
}
public static BothInt16 operator -(BothInt16 a)
{
short le = (short)(-a.LittleEndian);
short be = (short)(-a.BigEndian);
short le = (short)-a.LittleEndian;
short be = (short)-a.BigEndian;
return new BothInt16(le, be);
}
@@ -83,8 +83,8 @@ namespace SabreTools.Numerics
public static BothInt16 operator ~(BothInt16 a)
{
short le = (short)(~a.LittleEndian);
short be = (short)(~a.BigEndian);
short le = (short)~a.LittleEndian;
short be = (short)~a.BigEndian;
return new BothInt16(le, be);
}

View File

@@ -12,29 +12,29 @@ namespace SabreTools.Numerics
public static BothInt32 operator ++(BothInt32 a)
{
int le = (int)(a.LittleEndian + 1);
int be = (int)(a.BigEndian + 1);
int le = a.LittleEndian + 1;
int be = a.BigEndian + 1;
return new BothInt32(le, be);
}
public static BothInt32 operator --(BothInt32 a)
{
int le = (int)(a.LittleEndian - 1);
int be = (int)(a.BigEndian - 1);
int le = a.LittleEndian - 1;
int be = a.BigEndian - 1;
return new BothInt32(le, be);
}
public static BothInt32 operator +(BothInt32 a)
{
int le = (int)(+a.LittleEndian);
int be = (int)(+a.BigEndian);
int le = +a.LittleEndian;
int be = +a.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator -(BothInt32 a)
{
int le = (int)(-a.LittleEndian);
int be = (int)(-a.BigEndian);
int le = -a.LittleEndian;
int be = -a.BigEndian;
return new BothInt32(le, be);
}
@@ -44,36 +44,36 @@ namespace SabreTools.Numerics
public static BothInt32 operator *(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian * b.LittleEndian);
int be = (int)(a.BigEndian * b.BigEndian);
int le = a.LittleEndian * b.LittleEndian;
int be = a.BigEndian * b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator /(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian / b.LittleEndian);
int be = (int)(a.BigEndian / b.BigEndian);
int le = a.LittleEndian / b.LittleEndian;
int be = a.BigEndian / b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator %(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian % b.LittleEndian);
int be = (int)(a.BigEndian % b.BigEndian);
int le = a.LittleEndian % b.LittleEndian;
int be = a.BigEndian % b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator +(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian + b.LittleEndian);
int be = (int)(a.BigEndian + b.BigEndian);
int le = a.LittleEndian + b.LittleEndian;
int be = a.BigEndian + b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator -(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian - b.LittleEndian);
int be = (int)(a.BigEndian - b.BigEndian);
int le = a.LittleEndian - b.LittleEndian;
int be = a.BigEndian - b.BigEndian;
return new BothInt32(le, be);
}
@@ -83,8 +83,8 @@ namespace SabreTools.Numerics
public static BothInt32 operator ~(BothInt32 a)
{
int le = (int)(~a.LittleEndian);
int be = (int)(~a.BigEndian);
int le = ~a.LittleEndian;
int be = ~a.BigEndian;
return new BothInt32(le, be);
}
@@ -94,22 +94,22 @@ namespace SabreTools.Numerics
public static BothInt32 operator <<(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian << b.LittleEndian);
int be = (int)(a.BigEndian << b.BigEndian);
int le = a.LittleEndian << b.LittleEndian;
int be = a.BigEndian << b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator >>(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian >> b.LittleEndian);
int be = (int)(a.BigEndian >> b.BigEndian);
int le = a.LittleEndian >> b.LittleEndian;
int be = a.BigEndian >> b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator >>>(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian >>> b.LittleEndian);
int be = (int)(a.BigEndian >>> b.BigEndian);
int le = a.LittleEndian >>> b.LittleEndian;
int be = a.BigEndian >>> b.BigEndian;
return new BothInt32(le, be);
}
@@ -119,22 +119,22 @@ namespace SabreTools.Numerics
public static BothInt32 operator &(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian & b.LittleEndian);
int be = (int)(a.BigEndian & b.BigEndian);
int le = a.LittleEndian & b.LittleEndian;
int be = a.BigEndian & b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator |(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian | b.LittleEndian);
int be = (int)(a.BigEndian | b.BigEndian);
int le = a.LittleEndian | b.LittleEndian;
int be = a.BigEndian | b.BigEndian;
return new BothInt32(le, be);
}
public static BothInt32 operator ^(BothInt32 a, BothInt32 b)
{
int le = (int)(a.LittleEndian ^ b.LittleEndian);
int be = (int)(a.BigEndian ^ b.BigEndian);
int le = a.LittleEndian ^ b.LittleEndian;
int be = a.BigEndian ^ b.BigEndian;
return new BothInt32(le, be);
}

View File

@@ -12,29 +12,29 @@ namespace SabreTools.Numerics
public static BothInt64 operator ++(BothInt64 a)
{
long le = (long)(a.LittleEndian + 1);
long be = (long)(a.BigEndian + 1);
long le = a.LittleEndian + 1;
long be = a.BigEndian + 1;
return new BothInt64(le, be);
}
public static BothInt64 operator --(BothInt64 a)
{
long le = (long)(a.LittleEndian - 1);
long be = (long)(a.BigEndian - 1);
long le = a.LittleEndian - 1;
long be = a.BigEndian - 1;
return new BothInt64(le, be);
}
public static BothInt64 operator +(BothInt64 a)
{
long le = (long)(+a.LittleEndian);
long be = (long)(+a.BigEndian);
long le = +a.LittleEndian;
long be = +a.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator -(BothInt64 a)
{
long le = (long)(-a.LittleEndian);
long be = (long)(-a.BigEndian);
long le = -a.LittleEndian;
long be = -a.BigEndian;
return new BothInt64(le, be);
}
@@ -44,36 +44,36 @@ namespace SabreTools.Numerics
public static BothInt64 operator *(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian * b.LittleEndian);
long be = (long)(a.BigEndian * b.BigEndian);
long le = a.LittleEndian * b.LittleEndian;
long be = a.BigEndian * b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator /(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian / b.LittleEndian);
long be = (long)(a.BigEndian / b.BigEndian);
long le = a.LittleEndian / b.LittleEndian;
long be = a.BigEndian / b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator %(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian % b.LittleEndian);
long be = (long)(a.BigEndian % b.BigEndian);
long le = a.LittleEndian % b.LittleEndian;
long be = a.BigEndian % b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator +(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian + b.LittleEndian);
long be = (long)(a.BigEndian + b.BigEndian);
long le = a.LittleEndian + b.LittleEndian;
long be = a.BigEndian + b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator -(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian - b.LittleEndian);
long be = (long)(a.BigEndian - b.BigEndian);
long le = a.LittleEndian - b.LittleEndian;
long be = a.BigEndian - b.BigEndian;
return new BothInt64(le, be);
}
@@ -83,8 +83,8 @@ namespace SabreTools.Numerics
public static BothInt64 operator ~(BothInt64 a)
{
long le = (long)(~a.LittleEndian);
long be = (long)(~a.BigEndian);
long le = ~a.LittleEndian;
long be = ~a.BigEndian;
return new BothInt64(le, be);
}
@@ -94,22 +94,22 @@ namespace SabreTools.Numerics
public static BothInt64 operator <<(BothInt64 a, BothInt32 b)
{
long le = (long)(a.LittleEndian << b.LittleEndian);
long be = (long)(a.BigEndian << b.BigEndian);
long le = a.LittleEndian << b.LittleEndian;
long be = a.BigEndian << b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator >>(BothInt64 a, BothInt32 b)
{
long le = (long)(a.LittleEndian >> b.LittleEndian);
long be = (long)(a.BigEndian >> b.BigEndian);
long le = a.LittleEndian >> b.LittleEndian;
long be = a.BigEndian >> b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator >>>(BothInt64 a, BothInt32 b)
{
long le = (long)(a.LittleEndian >>> b.LittleEndian);
long be = (long)(a.BigEndian >>> b.BigEndian);
long le = a.LittleEndian >>> b.LittleEndian;
long be = a.BigEndian >>> b.BigEndian;
return new BothInt64(le, be);
}
@@ -119,22 +119,22 @@ namespace SabreTools.Numerics
public static BothInt64 operator &(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian & b.LittleEndian);
long be = (long)(a.BigEndian & b.BigEndian);
long le = a.LittleEndian & b.LittleEndian;
long be = a.BigEndian & b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator |(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian | b.LittleEndian);
long be = (long)(a.BigEndian | b.BigEndian);
long le = a.LittleEndian | b.LittleEndian;
long be = a.BigEndian | b.BigEndian;
return new BothInt64(le, be);
}
public static BothInt64 operator ^(BothInt64 a, BothInt64 b)
{
long le = (long)(a.LittleEndian ^ b.LittleEndian);
long be = (long)(a.BigEndian ^ b.BigEndian);
long le = a.LittleEndian ^ b.LittleEndian;
long be = a.BigEndian ^ b.BigEndian;
return new BothInt64(le, be);
}

View File

@@ -26,15 +26,15 @@ namespace SabreTools.Numerics
public static BothInt8 operator +(BothInt8 a)
{
sbyte le = (sbyte)(+a.LittleEndian);
sbyte be = (sbyte)(+a.BigEndian);
sbyte le = (sbyte)+a.LittleEndian;
sbyte be = (sbyte)+a.BigEndian;
return new BothInt8(le, be);
}
public static BothInt8 operator -(BothInt8 a)
{
sbyte le = (sbyte)(-a.LittleEndian);
sbyte be = (sbyte)(-a.BigEndian);
sbyte le = (sbyte)-a.LittleEndian;
sbyte be = (sbyte)-a.BigEndian;
return new BothInt8(le, be);
}
@@ -83,8 +83,8 @@ namespace SabreTools.Numerics
public static BothInt8 operator ~(BothInt8 a)
{
sbyte le = (sbyte)(~a.LittleEndian);
sbyte be = (sbyte)(~a.BigEndian);
sbyte le = (sbyte)~a.LittleEndian;
sbyte be = (sbyte)~a.BigEndian;
return new BothInt8(le, be);
}

View File

@@ -69,8 +69,8 @@ namespace SabreTools.Numerics
public static BothUInt16 operator ~(BothUInt16 a)
{
ushort le = (ushort)(~a.LittleEndian);
ushort be = (ushort)(~a.BigEndian);
ushort le = (ushort)~a.LittleEndian;
ushort be = (ushort)~a.BigEndian;
return new BothUInt16(le, be);
}

View File

@@ -12,15 +12,15 @@ namespace SabreTools.Numerics
public static BothUInt32 operator ++(BothUInt32 a)
{
uint le = (uint)(a.LittleEndian + 1);
uint be = (uint)(a.BigEndian + 1);
uint le = a.LittleEndian + 1;
uint be = a.BigEndian + 1;
return new BothUInt32(le, be);
}
public static BothUInt32 operator --(BothUInt32 a)
{
uint le = (uint)(a.LittleEndian - 1);
uint be = (uint)(a.BigEndian - 1);
uint le = a.LittleEndian - 1;
uint be = a.BigEndian - 1;
return new BothUInt32(le, be);
}
@@ -30,36 +30,36 @@ namespace SabreTools.Numerics
public static BothUInt32 operator *(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian * b.LittleEndian);
uint be = (uint)(a.BigEndian * b.BigEndian);
uint le = a.LittleEndian * b.LittleEndian;
uint be = a.BigEndian * b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator /(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian / b.LittleEndian);
uint be = (uint)(a.BigEndian / b.BigEndian);
uint le = a.LittleEndian / b.LittleEndian;
uint be = a.BigEndian / b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator %(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian % b.LittleEndian);
uint be = (uint)(a.BigEndian % b.BigEndian);
uint le = a.LittleEndian % b.LittleEndian;
uint be = a.BigEndian % b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator +(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian + b.LittleEndian);
uint be = (uint)(a.BigEndian + b.BigEndian);
uint le = a.LittleEndian + b.LittleEndian;
uint be = a.BigEndian + b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator -(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian - b.LittleEndian);
uint be = (uint)(a.BigEndian - b.BigEndian);
uint le = a.LittleEndian - b.LittleEndian;
uint be = a.BigEndian - b.BigEndian;
return new BothUInt32(le, be);
}
@@ -69,8 +69,8 @@ namespace SabreTools.Numerics
public static BothUInt32 operator ~(BothUInt32 a)
{
uint le = (uint)(~a.LittleEndian);
uint be = (uint)(~a.BigEndian);
uint le = ~a.LittleEndian;
uint be = ~a.BigEndian;
return new BothUInt32(le, be);
}
@@ -80,22 +80,22 @@ namespace SabreTools.Numerics
public static BothUInt32 operator <<(BothUInt32 a, BothInt32 b)
{
uint le = (uint)(a.LittleEndian << b.LittleEndian);
uint be = (uint)(a.BigEndian << b.BigEndian);
uint le = a.LittleEndian << b.LittleEndian;
uint be = a.BigEndian << b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator >>(BothUInt32 a, BothInt32 b)
{
uint le = (uint)(a.LittleEndian >> b.LittleEndian);
uint be = (uint)(a.BigEndian >> b.BigEndian);
uint le = a.LittleEndian >> b.LittleEndian;
uint be = a.BigEndian >> b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator >>>(BothUInt32 a, BothInt32 b)
{
uint le = (uint)(a.LittleEndian >>> b.LittleEndian);
uint be = (uint)(a.BigEndian >>> b.BigEndian);
uint le = a.LittleEndian >>> b.LittleEndian;
uint be = a.BigEndian >>> b.BigEndian;
return new BothUInt32(le, be);
}
@@ -105,22 +105,22 @@ namespace SabreTools.Numerics
public static BothUInt32 operator &(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian & b.LittleEndian);
uint be = (uint)(a.BigEndian & b.BigEndian);
uint le = a.LittleEndian & b.LittleEndian;
uint be = a.BigEndian & b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator |(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian | b.LittleEndian);
uint be = (uint)(a.BigEndian | b.BigEndian);
uint le = a.LittleEndian | b.LittleEndian;
uint be = a.BigEndian | b.BigEndian;
return new BothUInt32(le, be);
}
public static BothUInt32 operator ^(BothUInt32 a, BothUInt32 b)
{
uint le = (uint)(a.LittleEndian ^ b.LittleEndian);
uint be = (uint)(a.BigEndian ^ b.BigEndian);
uint le = a.LittleEndian ^ b.LittleEndian;
uint be = a.BigEndian ^ b.BigEndian;
return new BothUInt32(le, be);
}

View File

@@ -12,15 +12,15 @@ namespace SabreTools.Numerics
public static BothUInt64 operator ++(BothUInt64 a)
{
ulong le = (ulong)(a.LittleEndian + 1);
ulong be = (ulong)(a.BigEndian + 1);
ulong le = a.LittleEndian + 1;
ulong be = a.BigEndian + 1;
return new BothUInt64(le, be);
}
public static BothUInt64 operator --(BothUInt64 a)
{
ulong le = (ulong)(a.LittleEndian - 1);
ulong be = (ulong)(a.BigEndian - 1);
ulong le = a.LittleEndian - 1;
ulong be = a.BigEndian - 1;
return new BothUInt64(le, be);
}
@@ -30,36 +30,36 @@ namespace SabreTools.Numerics
public static BothUInt64 operator *(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian * b.LittleEndian);
ulong be = (ulong)(a.BigEndian * b.BigEndian);
ulong le = a.LittleEndian * b.LittleEndian;
ulong be = a.BigEndian * b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator /(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian / b.LittleEndian);
ulong be = (ulong)(a.BigEndian / b.BigEndian);
ulong le = a.LittleEndian / b.LittleEndian;
ulong be = a.BigEndian / b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator %(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian % b.LittleEndian);
ulong be = (ulong)(a.BigEndian % b.BigEndian);
ulong le = a.LittleEndian % b.LittleEndian;
ulong be = a.BigEndian % b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator +(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian + b.LittleEndian);
ulong be = (ulong)(a.BigEndian + b.BigEndian);
ulong le = a.LittleEndian + b.LittleEndian;
ulong be = a.BigEndian + b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator -(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian - b.LittleEndian);
ulong be = (ulong)(a.BigEndian - b.BigEndian);
ulong le = a.LittleEndian - b.LittleEndian;
ulong be = a.BigEndian - b.BigEndian;
return new BothUInt64(le, be);
}
@@ -69,8 +69,8 @@ namespace SabreTools.Numerics
public static BothUInt64 operator ~(BothUInt64 a)
{
ulong le = (ulong)(~a.LittleEndian);
ulong be = (ulong)(~a.BigEndian);
ulong le = ~a.LittleEndian;
ulong be = ~a.BigEndian;
return new BothUInt64(le, be);
}
@@ -80,22 +80,22 @@ namespace SabreTools.Numerics
public static BothUInt64 operator <<(BothUInt64 a, BothInt32 b)
{
ulong le = (ulong)(a.LittleEndian << b.LittleEndian);
ulong be = (ulong)(a.BigEndian << b.BigEndian);
ulong le = a.LittleEndian << b.LittleEndian;
ulong be = a.BigEndian << b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator >>(BothUInt64 a, BothInt32 b)
{
ulong le = (ulong)(a.LittleEndian >> b.LittleEndian);
ulong be = (ulong)(a.BigEndian >> b.BigEndian);
ulong le = a.LittleEndian >> b.LittleEndian;
ulong be = a.BigEndian >> b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator >>>(BothUInt64 a, BothInt32 b)
{
ulong le = (ulong)(a.LittleEndian >>> b.LittleEndian);
ulong be = (ulong)(a.BigEndian >>> b.BigEndian);
ulong le = a.LittleEndian >>> b.LittleEndian;
ulong be = a.BigEndian >>> b.BigEndian;
return new BothUInt64(le, be);
}
@@ -105,22 +105,22 @@ namespace SabreTools.Numerics
public static BothUInt64 operator &(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian & b.LittleEndian);
ulong be = (ulong)(a.BigEndian & b.BigEndian);
ulong le = a.LittleEndian & b.LittleEndian;
ulong be = a.BigEndian & b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator |(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian | b.LittleEndian);
ulong be = (ulong)(a.BigEndian | b.BigEndian);
ulong le = a.LittleEndian | b.LittleEndian;
ulong be = a.BigEndian | b.BigEndian;
return new BothUInt64(le, be);
}
public static BothUInt64 operator ^(BothUInt64 a, BothUInt64 b)
{
ulong le = (ulong)(a.LittleEndian ^ b.LittleEndian);
ulong be = (ulong)(a.BigEndian ^ b.BigEndian);
ulong le = a.LittleEndian ^ b.LittleEndian;
ulong be = a.BigEndian ^ b.BigEndian;
return new BothUInt64(le, be);
}

View File

@@ -69,8 +69,8 @@ namespace SabreTools.Numerics
public static BothUInt8 operator ~(BothUInt8 a)
{
byte le = (byte)(~a.LittleEndian);
byte be = (byte)(~a.BigEndian);
byte le = (byte)~a.LittleEndian;
byte be = (byte)~a.BigEndian;
return new BothUInt8(le, be);
}

View File

@@ -12,7 +12,7 @@ namespace SabreTools.IO
{
public static void CopyTo(this Stream source, Stream destination)
{
if (destination == null)
if (destination is null)
{
throw new ArgumentNullException("destination");
}

View File

@@ -102,9 +102,9 @@ namespace SabreTools.IO
private static bool PathsEqual(string? path1, string? path2, bool caseSenstive = false)
{
// Handle null path cases
if (path1 == null && path2 == null)
if (path1 is null && path2 is null)
return true;
else if (path1 == null ^ path2 == null)
else if (path1 is null ^ path2 is null)
return false;
// Normalize the paths before comparing
@@ -123,7 +123,7 @@ namespace SabreTools.IO
private static string? NormalizeDirectorySeparators(string? input)
{
// Null inputs are skipped
if (input == null)
if (input is null)
return null;
// Replace '\' with '/'

View File

@@ -121,7 +121,7 @@ namespace SabreTools.IO.Readers
/// </summary>
public bool ReadNextLine()
{
if (_reader.BaseStream == null)
if (_reader.BaseStream is null)
return false;
if (!_reader.BaseStream.CanRead || _reader.EndOfStream)
@@ -138,7 +138,7 @@ namespace SabreTools.IO.Readers
/// </summary>
private void ProcessLine()
{
if (CurrentLine == null)
if (CurrentLine is null)
return;
// Standalone (special case for DC dats)

View File

@@ -88,7 +88,7 @@ namespace SabreTools.IO.Readers
/// </summary>
public bool ReadNextLine()
{
if (_reader.BaseStream == null)
if (_reader.BaseStream is null)
return false;
if (!_reader.BaseStream.CanRead || _reader.EndOfStream)
@@ -105,7 +105,7 @@ namespace SabreTools.IO.Readers
/// </summary>
private void ProcessLine()
{
if (CurrentLine == null)
if (CurrentLine is null)
return;
// Comment

View File

@@ -107,7 +107,7 @@ namespace SabreTools.IO.Readers
if (!Header)
throw new InvalidOperationException("No header line expected");
if (HeaderValues != null)
if (HeaderValues is not null)
throw new InvalidOperationException("No more than 1 header row in a file allowed");
return ReadNextLine();
@@ -118,7 +118,7 @@ namespace SabreTools.IO.Readers
/// </summary>
public bool ReadNextLine()
{
if (_reader.BaseStream == null)
if (_reader.BaseStream is null)
return false;
if (!_reader.BaseStream.CanRead || _reader.EndOfStream)
@@ -128,7 +128,7 @@ namespace SabreTools.IO.Readers
CurrentLine = fullLine;
LineNumber++;
if (fullLine == null)
if (fullLine is null)
return false;
// If we have quotes, we need to split specially
@@ -140,7 +140,7 @@ namespace SabreTools.IO.Readers
foreach (Match? match in lineSplitRegex.Matches(fullLine))
{
string? curr = match?.Value;
if (curr == null)
if (curr is null)
continue;
if (curr.Length == 0)
temp.Add("");
@@ -162,7 +162,7 @@ namespace SabreTools.IO.Readers
}
// If we don't have a header yet and are expecting one, read this as the header
if (Header && HeaderValues == null)
if (Header && HeaderValues is null)
{
HeaderValues = Line;
_fieldCount = HeaderValues.Count;
@@ -185,13 +185,13 @@ namespace SabreTools.IO.Readers
throw new ArgumentException("No header expected so no keys can be used");
// If we don't have the key, return null
if (HeaderValues == null)
if (HeaderValues is null)
throw new ArgumentException($"Current line doesn't have key {key}");
if (!HeaderValues.Contains(key))
return null;
int index = HeaderValues.IndexOf(key);
if (Line == null)
if (Line is null)
throw new ArgumentException($"Current line doesn't have index {index}");
if (Line.Count < index)
throw new ArgumentException($"Current line doesn't have index {index}");
@@ -204,7 +204,7 @@ namespace SabreTools.IO.Readers
/// </summary>
public string GetValue(int index)
{
if (Line == null)
if (Line is null)
throw new ArgumentException($"Current line doesn't have index {index}");
if (Line.Count < index)
throw new ArgumentException($"Current line doesn't have index {index}");

View File

@@ -64,11 +64,11 @@ namespace SabreTools.IO.Streams
return null;
// If we don't have a value cached
if (_bitBuffer == null)
if (_bitBuffer is null)
{
// Read the next byte, if possible
_bitBuffer = ReadSourceByte();
if (_bitBuffer == null)
if (_bitBuffer is null)
return null;
// Reset the bit index
@@ -99,7 +99,7 @@ namespace SabreTools.IO.Streams
{
// Read the next bit
byte? bitValue = ReadBit();
if (bitValue == null)
if (bitValue is null)
return null;
// Append the bit shifted by the current index
@@ -121,7 +121,7 @@ namespace SabreTools.IO.Streams
{
// Read the next bit
byte? bitValue = ReadBit();
if (bitValue == null)
if (bitValue is null)
return null;
// Append the bit shifted by the current index

View File

@@ -217,7 +217,6 @@ namespace SabreTools.IO.Streams
{
return _source.Read(buffer, offset, count);
}
}
catch
{

View File

@@ -86,7 +86,7 @@ namespace SabreTools.IO.Transform
// Interleave the streams
using var interleaved = Interleave(evenStream, oddStream, type);
if (interleaved == null)
if (interleaved is null)
return false;
// Open the output file

View File

@@ -29,7 +29,7 @@ namespace SabreTools.IO.Transform
// Split the stream
if (!BlockSplit(inputStream, type, out Stream? evenStream, out Stream? oddStream))
return false;
else if (evenStream == null || oddStream == null)
else if (evenStream is null || oddStream is null)
return false;
// Get the base filename for output files
@@ -39,7 +39,7 @@ namespace SabreTools.IO.Transform
baseFilename = Path.Combine(outputDir, baseFilename);
// Create the output directory, if possible
if (outputDir != null && !Directory.Exists(outputDir))
if (outputDir is not null && !Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
// Open the output files
@@ -148,7 +148,7 @@ namespace SabreTools.IO.Transform
baseFilename = Path.Combine(outputDir, baseFilename);
// Create the output directory, if possible
if (outputDir != null && !Directory.Exists(outputDir))
if (outputDir is not null && !Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
// Loop while there is data left

View File

@@ -24,7 +24,7 @@ namespace SabreTools.IO.Transform
// Create the output directory if it doesn't already
string? outputDirectory = Path.GetDirectoryName(Path.GetFullPath(output));
if (outputDirectory != null && !Directory.Exists(outputDirectory))
if (outputDirectory is not null && !Directory.Exists(outputDirectory))
Directory.CreateDirectory(outputDirectory);
try
@@ -34,7 +34,7 @@ namespace SabreTools.IO.Transform
// Transform the stream
var transformed = Process(inputStream, operation);
if (transformed == null)
if (transformed is null)
return false;
// Open the output file

View File

@@ -214,13 +214,13 @@ namespace SabreTools.IO.Writers
try
{
// If we're writing quotes, don't write out quote characters internally
if ((quoteOverride == null && Quotes) || (quoteOverride == true))
if ((quoteOverride is null && Quotes) || (quoteOverride == true))
name = name.Replace("\"", "''");
AutoComplete(Token.StartAttribute);
_writer.Write(name);
_writer.Write(" ");
if ((quoteOverride == null && Quotes) || (quoteOverride == true))
if ((quoteOverride is null && Quotes) || (quoteOverride == true))
_writer.Write("\"");
}
catch
@@ -269,7 +269,7 @@ namespace SabreTools.IO.Writers
public void WriteRequiredAttributeString(string name, string? value, bool? quoteOverride = null, bool throwOnError = false)
{
// Throw an exception if we are configured to
if (value == null && throwOnError)
if (value is null && throwOnError)
throw new ArgumentNullException(nameof(value));
WriteAttributeString(name, value ?? string.Empty, quoteOverride);
@@ -301,7 +301,7 @@ namespace SabreTools.IO.Writers
throw new ArgumentException("Name cannot be null or empty", nameof(name));
// If we're writing quotes, don't write out quote characters internally
if ((quoteOverride == null && Quotes)
if ((quoteOverride is null && Quotes)
|| (quoteOverride == true))
{
name = name.Replace("\"", "''");
@@ -311,13 +311,14 @@ namespace SabreTools.IO.Writers
AutoComplete(Token.Standalone);
_writer.Write(name);
_writer.Write(" ");
if ((quoteOverride == null && Quotes)
if ((quoteOverride is null && Quotes)
|| (quoteOverride == true))
{
_writer.Write("\"");
}
_writer.Write(value);
if ((quoteOverride == null && Quotes)
if ((quoteOverride is null && Quotes)
|| (quoteOverride == true))
{
_writer.Write("\"");
@@ -340,7 +341,7 @@ namespace SabreTools.IO.Writers
public void WriteRequiredStandalone(string name, string? value, bool? quoteOverride = null, bool throwOnError = false)
{
// Throw an exception if we are configured to
if (value == null && throwOnError)
if (value is null && throwOnError)
throw new ArgumentNullException(nameof(value));
WriteStandalone(name, value ?? string.Empty, quoteOverride);
@@ -422,7 +423,7 @@ namespace SabreTools.IO.Writers
else if (_currentState == State.Error)
throw new InvalidOperationException();
State newState = StateTable[(int)token * 7 + (int)_currentState];
State newState = StateTable[((int)token * 7) + (int)_currentState];
if (newState == State.Error)
throw new InvalidOperationException();
@@ -484,6 +485,7 @@ namespace SabreTools.IO.Writers
break;
case Token.None:
default:
throw new InvalidOperationException();
}
@@ -543,7 +545,7 @@ namespace SabreTools.IO.Writers
/// </summary>
private void WriteEndAttributeQuote(bool? quoteOverride = null)
{
if ((quoteOverride == null && Quotes) || (quoteOverride == true))
if ((quoteOverride is null && Quotes) || (quoteOverride == true))
_writer.Write("\"");
}

View File

@@ -52,7 +52,7 @@ namespace SabreTools.IO.Writers
/// </summary>
public void WriteSection(string? value)
{
if (_writer.BaseStream == null)
if (_writer.BaseStream is null)
return;
if (string.IsNullOrEmpty(value))
@@ -66,7 +66,7 @@ namespace SabreTools.IO.Writers
/// </summary>
public void WriteKeyValuePair(string key, string? value)
{
if (_writer.BaseStream == null)
if (_writer.BaseStream is null)
return;
if (string.IsNullOrEmpty(key))
@@ -81,7 +81,7 @@ namespace SabreTools.IO.Writers
/// </summary>
public void WriteComment(string? value)
{
if (_writer.BaseStream == null)
if (_writer.BaseStream is null)
return;
value ??= string.Empty;
@@ -93,7 +93,7 @@ namespace SabreTools.IO.Writers
/// </summary>
public void WriteString(string? value)
{
if (_writer.BaseStream == null)
if (_writer.BaseStream is null)
return;
value ??= string.Empty;
@@ -105,7 +105,7 @@ namespace SabreTools.IO.Writers
/// </summary>
public void WriteLine()
{
if (_writer.BaseStream == null)
if (_writer.BaseStream is null)
return;
_writer.WriteLine();