diff --git a/SabreTools.Models/Compression/Deflate/Constants.cs b/SabreTools.Models/Compression/Deflate/Constants.cs new file mode 100644 index 0000000..a41e679 --- /dev/null +++ b/SabreTools.Models/Compression/Deflate/Constants.cs @@ -0,0 +1,136 @@ +namespace SabreTools.Models.Compression.Deflate +{ + /// + public static class Constants + { + /// + /// Bits in base literal/length lookup table + /// + public const int ZIPLBITS = 9; + + /// + /// Bits in base distance lookup table + /// + public const int ZIPDBITS = 6; + + /// + /// Maximum bit length of any code + /// + public const int ZIPBMAX = 16; + + /// + /// Maximum number of codes in any set + /// + public const int ZIPN_MAX = 288; + + #region Fixed Huffman Codes + + /// + /// Fixed Huffman code lengths for the literal / length alphabet + /// + public static readonly uint[] FixedLiteralLengths = + [ + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, + 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, + 8, 8, 8, 8, 8, 8, 8, 8, + ]; + + /// + /// Fixed Huffman distance codes for the literal / length alphabet + /// + public static readonly uint[] FixedDistanceCodes = + [ + 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, + ]; + + #endregion + + #region Literal and Length Alphabets + + /// + /// Extra bits for distance codes + /// + public static readonly ushort[] DistanceExtraBits = + [ + 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 + ]; + + /// + /// Copy offsets for distance codes 0..29 + /// + public static readonly ushort[] DistanceOffsets = + [ + 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 + ]; + + /// + /// Extra bits for literal codes 257..285 + /// + public static readonly ushort[] LiteralExtraBits = + [ + 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, + ]; + + /// + /// Copy lengths for literal codes 257..285 + /// + public static readonly ushort[] LiteralLengths = + [ + 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, + ]; + + /// + /// Order of the bit length code lengths + /// + public static readonly byte[] BitLengthOrder = + [ + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 + ]; + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.Models/Compression/Deflate/FixedCompressedDataHeader.cs b/SabreTools.Models/Compression/Deflate/FixedCompressedDataHeader.cs index d3e8d78..39c3b8e 100644 --- a/SabreTools.Models/Compression/Deflate/FixedCompressedDataHeader.cs +++ b/SabreTools.Models/Compression/Deflate/FixedCompressedDataHeader.cs @@ -1,5 +1,3 @@ -using System; - namespace SabreTools.Models.Compression.Deflate { /// @@ -14,80 +12,12 @@ namespace SabreTools.Models.Compression.Deflate /// /// Huffman code lengths for the literal / length alphabet /// - public override uint[]? LiteralLengths - { - get - { - // If we have cached lengths, use those - if (_literalLengths != null) - return _literalLengths; - - // Otherwise, build it from scratch - _literalLengths = new uint[288]; - - // Literal Value 0 - 143, 8 bits - for (int i = 0; i < 144; i++) - _literalLengths[i] = 8; - - // Literal Value 144 - 255, 9 bits - for (int i = 144; i < 256; i++) - _literalLengths[i] = 9; - - // Literal Value 256 - 279, 7 bits - for (int i = 256; i < 280; i++) - _literalLengths[i] = 7; - - // Literal Value 280 - 287, 8 bits - for (int i = 280; i < 288; i++) - _literalLengths[i] = 8; - - return _literalLengths; - } - set - { - throw new FieldAccessException(); - } - } + public override uint[]? LiteralLengths => Constants.FixedLiteralLengths; /// /// Huffman distance codes for the literal / length alphabet /// - public override uint[]? DistanceCodes - { - get - { - // If we have cached distances, use those - if (_distanceCodes != null) - return _distanceCodes; - - // Otherwise, build it from scratch - _distanceCodes = new uint[30]; - - // Fixed length, 5 bits - for (int i = 0; i < 30; i++) - _distanceCodes[i] = 5; - - return _distanceCodes; - } - set - { - throw new FieldAccessException(); - } - } - - #endregion - - #region Instance Variables - - /// - /// Huffman code lengths for the literal / length alphabet - /// - private uint[]? _literalLengths = null; - - /// - /// Huffman distance codes for the literal / length alphabet - /// - private uint[]? _distanceCodes = null; + public override uint[]? DistanceCodes => Constants.FixedDistanceCodes; #endregion } diff --git a/SabreTools.Models/Compression/MSZIP/Constants.cs b/SabreTools.Models/Compression/MSZIP/Constants.cs index a1bb475..537dc9f 100644 --- a/SabreTools.Models/Compression/MSZIP/Constants.cs +++ b/SabreTools.Models/Compression/MSZIP/Constants.cs @@ -8,73 +8,6 @@ namespace SabreTools.Models.Compression.MSZIP /// public const ushort ZIPWSIZE = 0x8000; - /// - /// Bits in base literal/length lookup table - /// - public const int ZIPLBITS = 9; - - /// - /// Bits in base distance lookup table - /// - public const int ZIPDBITS = 6; - - /// - /// Maximum bit length of any code - /// - public const int ZIPBMAX = 16; - - /// - /// Maximum number of codes in any set - /// - public const int ZIPN_MAX = 288; - - #region THOSE_ZIP_CONSTS - - /// - /// Order of the bit length code lengths - /// - public static readonly byte[] BitLengthOrder = - [ - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 - ]; - - /// - /// Copy lengths for literal codes 257..285 - /// - public static readonly ushort[] CopyLengths = - [ - 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 - ]; - - /// - /// Extra bits for literal codes 257..285 - /// - /// 99 == invalid - public static readonly ushort[] LiteralExtraBits = - [ - 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, 99, 99 - ]; - - /// - /// Copy offsets for distance codes 0..29 - /// - public static readonly ushort[] CopyOffsets = - [ - 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 - ]; - - /// - /// Extra bits for distance codes - /// - public static readonly ushort[] DistanceExtraBits = - [ - 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 - ]; - /// /// And'ing with Zipmask[n] masks the lower n bits /// @@ -83,7 +16,5 @@ namespace SabreTools.Models.Compression.MSZIP 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff ]; - - #endregion } } \ No newline at end of file