[ArchiveTools] Fix TorrentZip write

Apparently, only a very specific version of Zlib stuff will work for TZIP. This implementation is copied from RomVault for full compatibility.
This commit is contained in:
Matt Nadareski
2016-10-07 12:16:33 -07:00
parent da0b25bd78
commit 67564aef1a
16 changed files with 8797 additions and 8750 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -60,377 +60,377 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System; using System;
namespace Ionic.Zlib
namespace SabreTools.Helper
{ {
sealed class InfTree sealed class InfTree
{ {
private const int MANY = 1440; private const int MANY = 1440;
private const int Z_OK = 0; private const int Z_OK = 0;
private const int Z_STREAM_END = 1; private const int Z_STREAM_END = 1;
private const int Z_NEED_DICT = 2; private const int Z_NEED_DICT = 2;
private const int Z_ERRNO = - 1; private const int Z_ERRNO = - 1;
private const int Z_STREAM_ERROR = - 2; private const int Z_STREAM_ERROR = - 2;
private const int Z_DATA_ERROR = - 3; private const int Z_DATA_ERROR = - 3;
private const int Z_MEM_ERROR = - 4; private const int Z_MEM_ERROR = - 4;
private const int Z_BUF_ERROR = - 5; private const int Z_BUF_ERROR = - 5;
private const int Z_VERSION_ERROR = - 6; private const int Z_VERSION_ERROR = - 6;
internal const int fixed_bl = 9; internal const int fixed_bl = 9;
internal const int fixed_bd = 5; 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'" //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186,
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, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8,
14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255}; 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255};
//UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] fixed_td = new int[]{80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577}; internal static readonly int[] fixed_td = new int[]{80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577};
// Tables for deflate from PKZIP's appnote.txt. // 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'" //UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] cplens = new int[]{3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; internal static readonly int[] cplens = new int[]{3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
// see note #13 above about 258 // 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'" //UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] cplext = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; internal static readonly int[] cplext = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" //UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] cpdist = new int[]{1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; internal static readonly int[] cpdist = new int[]{1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
//UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" //UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
internal static readonly int[] cpdext = new int[]{0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; internal static readonly int[] cpdext = new int[]{0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
// If BMAX needs to be larger than 16, then h and x[] should be uLong. // 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 const int BMAX = 15; // maximum bit length of any code
internal int[] hn = null; // hufts used in space internal int[] hn = null; // hufts used in space
internal int[] v = null; // work area for huft_build internal int[] v = null; // work area for huft_build
internal int[] c = null; // bit length count table internal int[] c = null; // bit length count table
internal int[] r = null; // table entry for structure assignment internal int[] r = null; // table entry for structure assignment
internal int[] u = null; // table stack internal int[] u = null; // table stack
internal int[] x = null; // bit offsets, then code 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) 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 // 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 // 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 // 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 // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
// lengths), or Z_MEM_ERROR if not enough memory. // lengths), or Z_MEM_ERROR if not enough memory.
int a; // counter for codes of length k int a; // counter for codes of length k
int f; // i repeats in table every f entries int f; // i repeats in table every f entries
int g; // maximum code length int g; // maximum code length
int h; // table level int h; // table level
int i; // counter, current code int i; // counter, current code
int j; // counter int j; // counter
int k; // number of bits in current code int k; // number of bits in current code
int l; // bits per table (returned in m) int l; // bits per table (returned in m)
int mask; // (1 << w) - 1, to avoid cc -O bug on HP int mask; // (1 << w) - 1, to avoid cc -O bug on HP
int p; // pointer into c[], b[], or v[] int p; // pointer into c[], b[], or v[]
int q; // points to current table int q; // points to current table
int w; // bits before this table == (l * h) int w; // bits before this table == (l * h)
int xp; // pointer into x int xp; // pointer into x
int y; // number of dummy codes added int y; // number of dummy codes added
int z; // number of entries in current table int z; // number of entries in current table
// Generate counts for each bit length // Generate counts for each bit length
p = 0; i = n; p = 0; i = n;
do do
{ {
c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX
} }
while (i != 0); while (i != 0);
if (c[0] == n) if (c[0] == n)
{ {
// null input--all zero length codes // null input--all zero length codes
t[0] = - 1; t[0] = - 1;
m[0] = 0; m[0] = 0;
return Z_OK; return Z_OK;
} }
// Find minimum and maximum length, bound *m by those // Find minimum and maximum length, bound *m by those
l = m[0]; l = m[0];
for (j = 1; j <= BMAX; j++) for (j = 1; j <= BMAX; j++)
if (c[j] != 0) if (c[j] != 0)
break; break;
k = j; // minimum code length k = j; // minimum code length
if (l < j) if (l < j)
{ {
l = j; l = j;
} }
for (i = BMAX; i != 0; i--) for (i = BMAX; i != 0; i--)
{ {
if (c[i] != 0) if (c[i] != 0)
break; break;
} }
g = i; // maximum code length g = i; // maximum code length
if (l > i) if (l > i)
{ {
l = i; l = i;
} }
m[0] = l; m[0] = l;
// Adjust last length count to fill out codes, if needed // Adjust last length count to fill out codes, if needed
for (y = 1 << j; j < i; j++, y <<= 1) for (y = 1 << j; j < i; j++, y <<= 1)
{ {
if ((y -= c[j]) < 0) if ((y -= c[j]) < 0)
{ {
return Z_DATA_ERROR; return Z_DATA_ERROR;
} }
} }
if ((y -= c[i]) < 0) if ((y -= c[i]) < 0)
{ {
return Z_DATA_ERROR; return Z_DATA_ERROR;
} }
c[i] += y; c[i] += y;
// Generate starting offsets into the value table for each length // Generate starting offsets into the value table for each length
x[1] = j = 0; x[1] = j = 0;
p = 1; xp = 2; p = 1; xp = 2;
while (--i != 0) while (--i != 0)
{ {
// note that i == g from above // note that i == g from above
x[xp] = (j += c[p]); x[xp] = (j += c[p]);
xp++; xp++;
p++; p++;
} }
// Make a table of values in order of bit lengths // Make a table of values in order of bit lengths
i = 0; p = 0; i = 0; p = 0;
do do
{ {
if ((j = b[bindex + p]) != 0) if ((j = b[bindex + p]) != 0)
{ {
v[x[j]++] = i; v[x[j]++] = i;
} }
p++; p++;
} }
while (++i < n); while (++i < n);
n = x[g]; // set n to length of v n = x[g]; // set n to length of v
// Generate the Huffman codes and for each, make the table entries // Generate the Huffman codes and for each, make the table entries
x[0] = i = 0; // first Huffman code is zero x[0] = i = 0; // first Huffman code is zero
p = 0; // grab values in bit order p = 0; // grab values in bit order
h = - 1; // no tables yet--level -1 h = - 1; // no tables yet--level -1
w = - l; // bits decoded == (l * h) w = - l; // bits decoded == (l * h)
u[0] = 0; // just to keep compilers happy u[0] = 0; // just to keep compilers happy
q = 0; // ditto q = 0; // ditto
z = 0; // ditto z = 0; // ditto
// go through the bit lengths (k already is bits in shortest code) // go through the bit lengths (k already is bits in shortest code)
for (; k <= g; k++) for (; k <= g; k++)
{ {
a = c[k]; a = c[k];
while (a-- != 0) while (a-- != 0)
{ {
// here i is the Huffman code of length k bits for value *p // here i is the Huffman code of length k bits for value *p
// make tables up to required level // make tables up to required level
while (k > w + l) while (k > w + l)
{ {
h++; h++;
w += l; // previous table always l bits w += l; // previous table always l bits
// compute minimum size table less than or equal to l bits // compute minimum size table less than or equal to l bits
z = g - w; z = g - w;
z = (z > l)?l:z; // table size upper limit z = (z > l)?l:z; // table size upper limit
if ((f = 1 << (j = k - w)) > a + 1) if ((f = 1 << (j = k - w)) > a + 1)
{ {
// try a k-w bit table // try a k-w bit table
// too few codes for k-w bit table // too few codes for k-w bit table
f -= (a + 1); // deduct codes from patterns left f -= (a + 1); // deduct codes from patterns left
xp = k; xp = k;
if (j < z) if (j < z)
{ {
while (++j < z) while (++j < z)
{ {
// try smaller tables up to z bits // try smaller tables up to z bits
if ((f <<= 1) <= c[++xp]) if ((f <<= 1) <= c[++xp])
break; // enough codes to use up j bits break; // enough codes to use up j bits
f -= c[xp]; // else deduct codes from patterns f -= c[xp]; // else deduct codes from patterns
} }
} }
} }
z = 1 << j; // table entries for j-bit table z = 1 << j; // table entries for j-bit table
// allocate new table // allocate new table
if (hn[0] + z > MANY) if (hn[0] + z > MANY)
{ {
// (note: doesn't matter for fixed) // (note: doesn't matter for fixed)
return Z_DATA_ERROR; // overflow of MANY return Z_DATA_ERROR; // overflow of MANY
} }
u[h] = q = hn[0]; // DEBUG u[h] = q = hn[0]; // DEBUG
hn[0] += z; hn[0] += z;
// connect to last table, if there is one // connect to last table, if there is one
if (h != 0) if (h != 0)
{ {
x[h] = i; // save pattern for backing up x[h] = i; // save pattern for backing up
r[0] = (sbyte) j; // bits in this table r[0] = (sbyte) j; // bits in this table
r[1] = (sbyte) l; // bits to dump before this table r[1] = (sbyte) l; // bits to dump before this table
j = SharedUtils.URShift(i, (w - l)); j = SharedUtils.URShift(i, (w - l));
r[2] = (int) (q - u[h - 1] - j); // offset to this table 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 Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table
} }
else else
{ {
t[0] = q; // first table is returned result t[0] = q; // first table is returned result
} }
} }
// set up table entry in r // set up table entry in r
r[1] = (sbyte) (k - w); r[1] = (sbyte) (k - w);
if (p >= n) if (p >= n)
{ {
r[0] = 128 + 64; // out of values--invalid code r[0] = 128 + 64; // out of values--invalid code
} }
else if (v[p] < s) else if (v[p] < s)
{ {
r[0] = (sbyte) (v[p] < 256?0:32 + 64); // 256 is end-of-block r[0] = (sbyte) (v[p] < 256?0:32 + 64); // 256 is end-of-block
r[2] = v[p++]; // simple code is just the value r[2] = v[p++]; // simple code is just the value
} }
else else
{ {
r[0] = (sbyte) (e[v[p] - s] + 16 + 64); // non-simple--look up in lists r[0] = (sbyte) (e[v[p] - s] + 16 + 64); // non-simple--look up in lists
r[2] = d[v[p++] - s]; r[2] = d[v[p++] - s];
} }
// fill code-like entries with r // fill code-like entries with r
f = 1 << (k - w); f = 1 << (k - w);
for (j = SharedUtils.URShift(i, w); j < z; j += f) for (j = SharedUtils.URShift(i, w); j < z; j += f)
{ {
Array.Copy(r, 0, hp, (q + j) * 3, 3); Array.Copy(r, 0, hp, (q + j) * 3, 3);
} }
// backwards increment the k-bit code i // backwards increment the k-bit code i
for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1)) for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1))
{ {
i ^= j; i ^= j;
} }
i ^= j; i ^= j;
// backup over finished tables // backup over finished tables
mask = (1 << w) - 1; // needed on HP, cc -O bug mask = (1 << w) - 1; // needed on HP, cc -O bug
while ((i & mask) != x[h]) while ((i & mask) != x[h])
{ {
h--; // don't need to update q h--; // don't need to update q
w -= l; w -= l;
mask = (1 << w) - 1; mask = (1 << w) - 1;
} }
} }
} }
// Return Z_BUF_ERROR if we were given an incomplete table // Return Z_BUF_ERROR if we were given an incomplete table
return y != 0 && g != 1?Z_BUF_ERROR:Z_OK; 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) internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
{ {
int result; int result;
initWorkArea(19); initWorkArea(19);
hn[0] = 0; hn[0] = 0;
result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v); result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
if (result == Z_DATA_ERROR) if (result == Z_DATA_ERROR)
{ {
z.Message = "oversubscribed dynamic bit lengths tree"; z.Message = "oversubscribed dynamic bit lengths tree";
} }
else if (result == Z_BUF_ERROR || bb[0] == 0) else if (result == Z_BUF_ERROR || bb[0] == 0)
{ {
z.Message = "incomplete dynamic bit lengths tree"; z.Message = "incomplete dynamic bit lengths tree";
result = Z_DATA_ERROR; result = Z_DATA_ERROR;
} }
return result; 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) 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; int result;
// build literal/length tree // build literal/length tree
initWorkArea(288); initWorkArea(288);
hn[0] = 0; hn[0] = 0;
result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v); result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
if (result != Z_OK || bl[0] == 0) if (result != Z_OK || bl[0] == 0)
{ {
if (result == Z_DATA_ERROR) if (result == Z_DATA_ERROR)
{ {
z.Message = "oversubscribed literal/length tree"; z.Message = "oversubscribed literal/length tree";
} }
else if (result != Z_MEM_ERROR) else if (result != Z_MEM_ERROR)
{ {
z.Message = "incomplete literal/length tree"; z.Message = "incomplete literal/length tree";
result = Z_DATA_ERROR; result = Z_DATA_ERROR;
} }
return result; return result;
} }
// build distance tree // build distance tree
initWorkArea(288); initWorkArea(288);
result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v); 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_OK || (bd[0] == 0 && nl > 257))
{ {
if (result == Z_DATA_ERROR) if (result == Z_DATA_ERROR)
{ {
z.Message = "oversubscribed distance tree"; z.Message = "oversubscribed distance tree";
} }
else if (result == Z_BUF_ERROR) else if (result == Z_BUF_ERROR)
{ {
z.Message = "incomplete distance tree"; z.Message = "incomplete distance tree";
result = Z_DATA_ERROR; result = Z_DATA_ERROR;
} }
else if (result != Z_MEM_ERROR) else if (result != Z_MEM_ERROR)
{ {
z.Message = "empty distance tree with lengths"; z.Message = "empty distance tree with lengths";
result = Z_DATA_ERROR; result = Z_DATA_ERROR;
} }
return result; return result;
} }
return Z_OK; return Z_OK;
} }
internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z) internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
{ {
bl[0] = fixed_bl; bl[0] = fixed_bl;
bd[0] = fixed_bd; bd[0] = fixed_bd;
tl[0] = fixed_tl; tl[0] = fixed_tl;
td[0] = fixed_td; td[0] = fixed_td;
return Z_OK; return Z_OK;
} }
private void initWorkArea(int vsize) private void initWorkArea(int vsize)
{ {
if (hn == null) if (hn == null)
{ {
hn = new int[1]; hn = new int[1];
v = new int[vsize]; v = new int[vsize];
c = new int[BMAX + 1]; c = new int[BMAX + 1];
r = new int[3]; r = new int[3];
u = new int[BMAX]; u = new int[BMAX];
x = new int[BMAX + 1]; x = new int[BMAX + 1];
} }
else else
{ {
if (v.Length < vsize) if (v.Length < vsize)
{ {
v = new int[vsize]; v = new int[vsize];
} }
Array.Clear(v,0,vsize); Array.Clear(v,0,vsize);
Array.Clear(c,0,BMAX+1); Array.Clear(c,0,BMAX+1);
r[0]=0; r[1]=0; r[2]=0; r[0]=0; r[1]=0; r[2]=0;
// for(int i=0; i<BMAX; i++){u[i]=0;} // for(int i=0; i<BMAX; i++){u[i]=0;}
//Array.Copy(c, 0, u, 0, BMAX); //Array.Copy(c, 0, u, 0, BMAX);
Array.Clear(u,0,BMAX); Array.Clear(u,0,BMAX);
// for(int i=0; i<BMAX+1; i++){x[i]=0;} // for(int i=0; i<BMAX+1; i++){x[i]=0;}
//Array.Copy(c, 0, x, 0, BMAX + 1); //Array.Copy(c, 0, x, 0, BMAX + 1);
Array.Clear(x,0,BMAX+1); Array.Clear(x,0,BMAX+1);
} }
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -27,11 +27,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using Ionic.Zlib;
using System.IO; using System.IO;
namespace Ionic.Zlib namespace SabreTools.Helper
{ {
internal class WorkItem internal class WorkItem
{ {
@@ -45,7 +44,7 @@ namespace Ionic.Zlib
public ZlibCodec compressor; public ZlibCodec compressor;
public WorkItem(int size, public WorkItem(int size,
Ionic.Zlib.CompressionLevel compressLevel, CompressionLevel compressLevel,
CompressionStrategy strategy, CompressionStrategy strategy,
int ix) int ix)
{ {
@@ -121,12 +120,12 @@ namespace Ionic.Zlib
private int _lastWritten; private int _lastWritten;
private int _latestCompressed; private int _latestCompressed;
private int _Crc32; private int _Crc32;
private Ionic.Crc.CRC32 _runningCrc; private CRC32 _runningCrc;
private object _latestLock = new object(); private object _latestLock = new object();
private System.Collections.Generic.Queue<int> _toWrite; private System.Collections.Generic.Queue<int> _toWrite;
private System.Collections.Generic.Queue<int> _toFill; private System.Collections.Generic.Queue<int> _toFill;
private Int64 _totalBytesProcessed; private Int64 _totalBytesProcessed;
private Ionic.Zlib.CompressionLevel _compressLevel; private CompressionLevel _compressLevel;
private volatile Exception _pendingException; private volatile Exception _pendingException;
private bool _handlingException; private bool _handlingException;
private object _eLock = new Object(); // protects _pendingException private object _eLock = new Object(); // protects _pendingException
@@ -488,7 +487,7 @@ namespace Ionic.Zlib
} }
_newlyCompressedBlob = new AutoResetEvent(false); _newlyCompressedBlob = new AutoResetEvent(false);
_runningCrc = new Ionic.Crc.CRC32(); _runningCrc = new CRC32();
_currentlyFilling = -1; _currentlyFilling = -1;
_lastFilled = -1; _lastFilled = -1;
_lastWritten = -1; _lastWritten = -1;
@@ -856,7 +855,7 @@ namespace Ionic.Zlib
_firstWriteDone = false; _firstWriteDone = false;
_totalBytesProcessed = 0L; _totalBytesProcessed = 0L;
_runningCrc = new Ionic.Crc.CRC32(); _runningCrc = new CRC32();
_isClosed= false; _isClosed= false;
_currentlyFilling = -1; _currentlyFilling = -1;
_lastFilled = -1; _lastFilled = -1;
@@ -1155,7 +1154,7 @@ namespace Ionic.Zlib
try try
{ {
int myItem = workitem.index; int myItem = workitem.index;
Ionic.Crc.CRC32 crc = new Ionic.Crc.CRC32(); CRC32 crc = new CRC32();
// calc CRC on the buffer // calc CRC on the buffer
crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable); crc.SlurpBlock(workitem.buffer, 0, workitem.inputBytesAvailable);

View File

@@ -61,363 +61,361 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System; namespace SabreTools.Helper
namespace Ionic.Zlib
{ {
sealed class Tree sealed class Tree
{ {
private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1); private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1);
// extra bits for each length code // extra bits for each length code
internal static readonly int[] ExtraLengthBits = new int[] internal static readonly int[] ExtraLengthBits = new int[]
{ {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 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 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
}; };
// extra bits for each distance code // extra bits for each distance code
internal static readonly int[] ExtraDistanceBits = new int[] internal static readonly int[] ExtraDistanceBits = new int[]
{ {
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 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 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
}; };
// extra bits for each bit length code // extra bits for each bit length code
internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7}; internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; internal static readonly sbyte[] bl_order = new sbyte[]{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 // The lengths of the bit length codes are sent in order of decreasing
// probability, to avoid transmitting the lengths for unused bit // probability, to avoid transmitting the lengths for unused bit
// length codes. // length codes.
internal const int Buf_size = 8 * 2; internal const int Buf_size = 8 * 2;
// see definition of array dist_code below // see definition of array dist_code below
//internal const int DIST_CODE_LEN = 512; //internal const int DIST_CODE_LEN = 512;
private static readonly sbyte[] _dist_code = new sbyte[] private static readonly sbyte[] _dist_code = new sbyte[]
{ {
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 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, 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, 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, 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,
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,
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,
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,
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, 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, 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, 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,
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,
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,
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,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
}; };
internal static readonly sbyte[] LengthCode = new sbyte[] internal static readonly sbyte[] LengthCode = new sbyte[]
{ {
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 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, 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, 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, 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, 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, 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, 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, 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,
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,
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,
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,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
}; };
internal static readonly int[] LengthBase = new int[] internal static readonly int[] LengthBase = new int[]
{ {
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 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 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
}; };
internal static readonly int[] DistanceBase = new int[] internal static readonly int[] DistanceBase = new int[]
{ {
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 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 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
}; };
/// <summary> /// <summary>
/// Map from a distance to a distance code. /// Map from a distance to a distance code.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// No side effects. _dist_code[256] and _dist_code[257] are never used. /// No side effects. _dist_code[256] and _dist_code[257] are never used.
/// </remarks> /// </remarks>
internal static int DistanceCode(int dist) internal static int DistanceCode(int dist)
{ {
return (dist < 256) return (dist < 256)
? _dist_code[dist] ? _dist_code[dist]
: _dist_code[256 + SharedUtils.URShift(dist, 7)]; : _dist_code[256 + SharedUtils.URShift(dist, 7)];
} }
internal short[] dyn_tree; // the dynamic tree internal short[] dyn_tree; // the dynamic tree
internal int max_code; // largest code with non zero frequency internal int max_code; // largest code with non zero frequency
internal StaticTree staticTree; // the corresponding static tree internal StaticTree staticTree; // the corresponding static tree
// Compute the optimal bit lengths for a tree and update the total bit length // Compute the optimal bit lengths for a tree and update the total bit length
// for the current block. // for the current block.
// IN assertion: the fields freq and dad are set, heap[heap_max] and // IN assertion: the fields freq and dad are set, heap[heap_max] and
// above are the tree nodes sorted by increasing frequency. // above are the tree nodes sorted by increasing frequency.
// OUT assertions: the field len is set to the optimal bit length, the // OUT assertions: the field len is set to the optimal bit length, the
// array bl_count contains the frequencies for each bit length. // array bl_count contains the frequencies for each bit length.
// The length opt_len is updated; static_len is also updated if stree is // The length opt_len is updated; static_len is also updated if stree is
// not null. // not null.
internal void gen_bitlen(DeflateManager s) internal void gen_bitlen(DeflateManager s)
{ {
short[] tree = dyn_tree; short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes; short[] stree = staticTree.treeCodes;
int[] extra = staticTree.extraBits; int[] extra = staticTree.extraBits;
int base_Renamed = staticTree.extraBase; int base_Renamed = staticTree.extraBase;
int max_length = staticTree.maxLength; int max_length = staticTree.maxLength;
int h; // heap index int h; // heap index
int n, m; // iterate over the tree elements int n, m; // iterate over the tree elements
int bits; // bit length int bits; // bit length
int xbits; // extra bits int xbits; // extra bits
short f; // frequency short f; // frequency
int overflow = 0; // number of elements with bit length too large int overflow = 0; // number of elements with bit length too large
for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++) for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
s.bl_count[bits] = 0; s.bl_count[bits] = 0;
// In a first pass, compute the optimal bit lengths (which may // In a first pass, compute the optimal bit lengths (which may
// overflow in the case of the bit length tree). // overflow in the case of the bit length tree).
tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
for (h = s.heap_max + 1; h < HEAP_SIZE; h++) for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
{ {
n = s.heap[h]; n = s.heap[h];
bits = tree[tree[n * 2 + 1] * 2 + 1] + 1; bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
if (bits > max_length) if (bits > max_length)
{ {
bits = max_length; overflow++; bits = max_length; overflow++;
} }
tree[n * 2 + 1] = (short) bits; tree[n * 2 + 1] = (short) bits;
// We overwrite tree[n*2+1] which is no longer needed // We overwrite tree[n*2+1] which is no longer needed
if (n > max_code) if (n > max_code)
continue; // not a leaf node continue; // not a leaf node
s.bl_count[bits]++; s.bl_count[bits]++;
xbits = 0; xbits = 0;
if (n >= base_Renamed) if (n >= base_Renamed)
xbits = extra[n - base_Renamed]; xbits = extra[n - base_Renamed];
f = tree[n * 2]; f = tree[n * 2];
s.opt_len += f * (bits + xbits); s.opt_len += f * (bits + xbits);
if (stree != null) if (stree != null)
s.static_len += f * (stree[n * 2 + 1] + xbits); s.static_len += f * (stree[n * 2 + 1] + xbits);
} }
if (overflow == 0) if (overflow == 0)
return ; return ;
// This happens for example on obj2 and pic of the Calgary corpus // This happens for example on obj2 and pic of the Calgary corpus
// Find the first bit length which could increase: // Find the first bit length which could increase:
do do
{ {
bits = max_length - 1; bits = max_length - 1;
while (s.bl_count[bits] == 0) while (s.bl_count[bits] == 0)
bits--; bits--;
s.bl_count[bits]--; // move one leaf down the tree s.bl_count[bits]--; // move one leaf down the tree
s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
s.bl_count[max_length]--; s.bl_count[max_length]--;
// The brother of the overflow item also moves one step up, // The brother of the overflow item also moves one step up,
// but this does not affect bl_count[max_length] // but this does not affect bl_count[max_length]
overflow -= 2; overflow -= 2;
} }
while (overflow > 0); while (overflow > 0);
for (bits = max_length; bits != 0; bits--) for (bits = max_length; bits != 0; bits--)
{ {
n = s.bl_count[bits]; n = s.bl_count[bits];
while (n != 0) while (n != 0)
{ {
m = s.heap[--h]; m = s.heap[--h];
if (m > max_code) if (m > max_code)
continue; continue;
if (tree[m * 2 + 1] != bits) if (tree[m * 2 + 1] != bits)
{ {
s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]); s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
tree[m * 2 + 1] = (short) bits; tree[m * 2 + 1] = (short) bits;
} }
n--; n--;
} }
} }
} }
// Construct one Huffman tree and assigns the code bit strings and lengths. // Construct one Huffman tree and assigns the code bit strings and lengths.
// Update the total bit length for the current block. // Update the total bit length for the current block.
// IN assertion: the field freq is set for all tree elements. // 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 // 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 // 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. // also updated if stree is not null. The field max_code is set.
internal void build_tree(DeflateManager s) internal void build_tree(DeflateManager s)
{ {
short[] tree = dyn_tree; short[] tree = dyn_tree;
short[] stree = staticTree.treeCodes; short[] stree = staticTree.treeCodes;
int elems = staticTree.elems; int elems = staticTree.elems;
int n, m; // iterate over heap elements int n, m; // iterate over heap elements
int max_code = -1; // largest code with non zero frequency int max_code = -1; // largest code with non zero frequency
int node; // new node being created int node; // new node being created
// Construct the initial heap, with least frequent element in // 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[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
// heap[0] is not used. // heap[0] is not used.
s.heap_len = 0; s.heap_len = 0;
s.heap_max = HEAP_SIZE; s.heap_max = HEAP_SIZE;
for (n = 0; n < elems; n++) for (n = 0; n < elems; n++)
{ {
if (tree[n * 2] != 0) if (tree[n * 2] != 0)
{ {
s.heap[++s.heap_len] = max_code = n; s.heap[++s.heap_len] = max_code = n;
s.depth[n] = 0; s.depth[n] = 0;
} }
else else
{ {
tree[n * 2 + 1] = 0; tree[n * 2 + 1] = 0;
} }
} }
// The pkzip format requires that at least one distance code exists, // 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 // 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 // possible code. So to avoid special checks later on we force at least
// two codes of non zero frequency. // two codes of non zero frequency.
while (s.heap_len < 2) while (s.heap_len < 2)
{ {
node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0); node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0);
tree[node * 2] = 1; tree[node * 2] = 1;
s.depth[node] = 0; s.depth[node] = 0;
s.opt_len--; s.opt_len--;
if (stree != null) if (stree != null)
s.static_len -= stree[node * 2 + 1]; s.static_len -= stree[node * 2 + 1];
// node is 0 or 1 so it does not have extra bits // node is 0 or 1 so it does not have extra bits
} }
this.max_code = max_code; this.max_code = max_code;
// The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
// establish sub-heaps of increasing lengths: // establish sub-heaps of increasing lengths:
for (n = s.heap_len / 2; n >= 1; n--) for (n = s.heap_len / 2; n >= 1; n--)
s.pqdownheap(tree, n); s.pqdownheap(tree, n);
// Construct the Huffman tree by repeatedly combining the least two // Construct the Huffman tree by repeatedly combining the least two
// frequent nodes. // frequent nodes.
node = elems; // next internal node of the tree node = elems; // next internal node of the tree
do do
{ {
// n = node of least frequency // n = node of least frequency
n = s.heap[1]; n = s.heap[1];
s.heap[1] = s.heap[s.heap_len--]; s.heap[1] = s.heap[s.heap_len--];
s.pqdownheap(tree, 1); s.pqdownheap(tree, 1);
m = s.heap[1]; // m = node of next least frequency 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] = n; // keep the nodes sorted by frequency
s.heap[--s.heap_max] = m; s.heap[--s.heap_max] = m;
// Create a new node father of n and m // Create a new node father of n and m
tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2])); 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); s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node; tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
// and insert the new node in the heap // and insert the new node in the heap
s.heap[1] = node++; s.heap[1] = node++;
s.pqdownheap(tree, 1); s.pqdownheap(tree, 1);
} }
while (s.heap_len >= 2); while (s.heap_len >= 2);
s.heap[--s.heap_max] = s.heap[1]; s.heap[--s.heap_max] = s.heap[1];
// At this point, the fields freq and dad are set. We can now // At this point, the fields freq and dad are set. We can now
// generate the bit lengths. // generate the bit lengths.
gen_bitlen(s); gen_bitlen(s);
// The field len is now set, we can generate the bit codes // The field len is now set, we can generate the bit codes
gen_codes(tree, max_code, s.bl_count); gen_codes(tree, max_code, s.bl_count);
} }
// Generate the codes for a given tree and bit counts (which need not be // Generate the codes for a given tree and bit counts (which need not be
// optimal). // optimal).
// IN assertion: the array bl_count contains the bit length statistics for // 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. // 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 // OUT assertion: the field code is set for all tree elements of non
// zero code length. // zero code length.
internal static void gen_codes(short[] tree, int max_code, short[] bl_count) internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
{ {
short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length
short code = 0; // running code value short code = 0; // running code value
int bits; // bit index int bits; // bit index
int n; // code index int n; // code index
// The distribution counts are first used to generate the code values // The distribution counts are first used to generate the code values
// without bit reversal. // without bit reversal.
for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++) for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
unchecked { unchecked {
next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1); next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1);
} }
// Check that the bit counts in bl_count are consistent. The last code // Check that the bit counts in bl_count are consistent. The last code
// must be all ones. // must be all ones.
//Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1, //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
// "inconsistent bit counts"); // "inconsistent bit counts");
//Tracev((stderr,"\ngen_codes: max_code %d ", max_code)); //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) for (n = 0; n <= max_code; n++)
{ {
int len = tree[n * 2 + 1]; int len = tree[n * 2 + 1];
if (len == 0) if (len == 0)
continue; continue;
// Now reverse the bits // Now reverse the bits
tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len))); tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len)));
} }
} }
// Reverse the first len bits of a code, using straightforward code (a faster // Reverse the first len bits of a code, using straightforward code (a faster
// method would use a table) // method would use a table)
// IN assertion: 1 <= len <= 15 // IN assertion: 1 <= len <= 15
internal static int bi_reverse(int code, int len) internal static int bi_reverse(int code, int len)
{ {
int res = 0; int res = 0;
do do
{ {
res |= code & 1; res |= code & 1;
code >>= 1; //SharedUtils.URShift(code, 1); code >>= 1; //SharedUtils.URShift(code, 1);
res <<= 1; res <<= 1;
} }
while (--len > 0); while (--len > 0);
return res >> 1; return res >> 1;
} }
} }
} }

View File

@@ -87,460 +87,458 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System.Runtime.InteropServices;
using System; namespace SabreTools.Helper
using Interop=System.Runtime.InteropServices;
namespace Ionic.Zlib
{ {
/// <summary> /// <summary>
/// Describes how to flush the current deflate operation. /// Describes how to flush the current deflate operation.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The different FlushType values are useful when using a Deflate in a streaming application. /// The different FlushType values are useful when using a Deflate in a streaming application.
/// </remarks> /// </remarks>
public enum FlushType public enum FlushType
{ {
/// <summary>No flush at all.</summary> /// <summary>No flush at all.</summary>
None = 0, None = 0,
/// <summary>Closes the current block, but doesn't flush it to /// <summary>Closes the current block, but doesn't flush it to
/// the output. Used internally only in hypothetical /// the output. Used internally only in hypothetical
/// scenarios. This was supposed to be removed by Zlib, but it is /// scenarios. This was supposed to be removed by Zlib, but it is
/// still in use in some edge cases. /// still in use in some edge cases.
/// </summary> /// </summary>
Partial, Partial,
/// <summary> /// <summary>
/// Use this during compression to specify that all pending output should be /// Use this during compression to specify that all pending output should be
/// flushed to the output buffer and the output should be aligned on a byte /// flushed to the output buffer and the output should be aligned on a byte
/// boundary. You might use this in a streaming communication scenario, so that /// boundary. You might use this in a streaming communication scenario, so that
/// the decompressor can get all input data available so far. When using this /// the decompressor can get all input data available so far. When using this
/// with a ZlibCodec, <c>AvailableBytesIn</c> will be zero after the call if /// with a ZlibCodec, <c>AvailableBytesIn</c> will be zero after the call if
/// enough output space has been provided before the call. Flushing will /// enough output space has been provided before the call. Flushing will
/// degrade compression and so it should be used only when necessary. /// degrade compression and so it should be used only when necessary.
/// </summary> /// </summary>
Sync, Sync,
/// <summary> /// <summary>
/// Use this during compression to specify that all output should be flushed, as /// Use this during compression to specify that all output should be flushed, as
/// with <c>FlushType.Sync</c>, but also, the compression state should be reset /// with <c>FlushType.Sync</c>, but also, the compression state should be reset
/// so that decompression can restart from this point if previous compressed /// so that decompression can restart from this point if previous compressed
/// data has been damaged or if random access is desired. Using /// data has been damaged or if random access is desired. Using
/// <c>FlushType.Full</c> too often can significantly degrade the compression. /// <c>FlushType.Full</c> too often can significantly degrade the compression.
/// </summary> /// </summary>
Full, Full,
/// <summary>Signals the end of the compression/decompression stream.</summary> /// <summary>Signals the end of the compression/decompression stream.</summary>
Finish, Finish,
} }
/// <summary> /// <summary>
/// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress. /// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
/// </summary> /// </summary>
public enum CompressionLevel public enum CompressionLevel
{ {
/// <summary> /// <summary>
/// None means that the data will be simply stored, with no change at all. /// None means that the data will be simply stored, with no change at all.
/// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None /// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None
/// cannot be opened with the default zip reader. Use a different CompressionLevel. /// cannot be opened with the default zip reader. Use a different CompressionLevel.
/// </summary> /// </summary>
None= 0, None= 0,
/// <summary> /// <summary>
/// Same as None. /// Same as None.
/// </summary> /// </summary>
Level0 = 0, Level0 = 0,
/// <summary> /// <summary>
/// The fastest but least effective compression. /// The fastest but least effective compression.
/// </summary> /// </summary>
BestSpeed = 1, BestSpeed = 1,
/// <summary> /// <summary>
/// A synonym for BestSpeed. /// A synonym for BestSpeed.
/// </summary> /// </summary>
Level1 = 1, Level1 = 1,
/// <summary> /// <summary>
/// A little slower, but better, than level 1. /// A little slower, but better, than level 1.
/// </summary> /// </summary>
Level2 = 2, Level2 = 2,
/// <summary> /// <summary>
/// A little slower, but better, than level 2. /// A little slower, but better, than level 2.
/// </summary> /// </summary>
Level3 = 3, Level3 = 3,
/// <summary> /// <summary>
/// A little slower, but better, than level 3. /// A little slower, but better, than level 3.
/// </summary> /// </summary>
Level4 = 4, Level4 = 4,
/// <summary> /// <summary>
/// A little slower than level 4, but with better compression. /// A little slower than level 4, but with better compression.
/// </summary> /// </summary>
Level5 = 5, Level5 = 5,
/// <summary> /// <summary>
/// The default compression level, with a good balance of speed and compression efficiency. /// The default compression level, with a good balance of speed and compression efficiency.
/// </summary> /// </summary>
Default = 6, Default = 6,
/// <summary> /// <summary>
/// A synonym for Default. /// A synonym for Default.
/// </summary> /// </summary>
Level6 = 6, Level6 = 6,
/// <summary> /// <summary>
/// Pretty good compression! /// Pretty good compression!
/// </summary> /// </summary>
Level7 = 7, Level7 = 7,
/// <summary> /// <summary>
/// Better compression than Level7! /// Better compression than Level7!
/// </summary> /// </summary>
Level8 = 8, Level8 = 8,
/// <summary> /// <summary>
/// The "best" compression, where best means greatest reduction in size of the input data stream. /// The "best" compression, where best means greatest reduction in size of the input data stream.
/// This is also the slowest compression. /// This is also the slowest compression.
/// </summary> /// </summary>
BestCompression = 9, BestCompression = 9,
/// <summary> /// <summary>
/// A synonym for BestCompression. /// A synonym for BestCompression.
/// </summary> /// </summary>
Level9 = 9, Level9 = 9,
} }
/// <summary> /// <summary>
/// Describes options for how the compression algorithm is executed. Different strategies /// Describes options for how the compression algorithm is executed. Different strategies
/// work better on different sorts of data. The strategy parameter can affect the compression /// work better on different sorts of data. The strategy parameter can affect the compression
/// ratio and the speed of compression but not the correctness of the compresssion. /// ratio and the speed of compression but not the correctness of the compresssion.
/// </summary> /// </summary>
public enum CompressionStrategy public enum CompressionStrategy
{ {
/// <summary> /// <summary>
/// The default strategy is probably the best for normal data. /// The default strategy is probably the best for normal data.
/// </summary> /// </summary>
Default = 0, Default = 0,
/// <summary> /// <summary>
/// The <c>Filtered</c> strategy is intended to be used most effectively with data produced by a /// The <c>Filtered</c> strategy is intended to be used most effectively with data produced by a
/// filter or predictor. By this definition, filtered data consists mostly of small /// filter or predictor. By this definition, filtered data consists mostly of small
/// values with a somewhat random distribution. In this case, the compression algorithm /// values with a somewhat random distribution. In this case, the compression algorithm
/// is tuned to compress them better. The effect of <c>Filtered</c> is to force more Huffman /// is tuned to compress them better. The effect of <c>Filtered</c> is to force more Huffman
/// coding and less string matching; it is a half-step between <c>Default</c> and <c>HuffmanOnly</c>. /// coding and less string matching; it is a half-step between <c>Default</c> and <c>HuffmanOnly</c>.
/// </summary> /// </summary>
Filtered = 1, Filtered = 1,
/// <summary> /// <summary>
/// Using <c>HuffmanOnly</c> will force the compressor to do Huffman encoding only, with no /// Using <c>HuffmanOnly</c> will force the compressor to do Huffman encoding only, with no
/// string matching. /// string matching.
/// </summary> /// </summary>
HuffmanOnly = 2, HuffmanOnly = 2,
} }
/// <summary> /// <summary>
/// An enum to specify the direction of transcoding - whether to compress or decompress. /// An enum to specify the direction of transcoding - whether to compress or decompress.
/// </summary> /// </summary>
public enum CompressionMode public enum CompressionMode
{ {
/// <summary> /// <summary>
/// Used to specify that the stream should compress the data. /// Used to specify that the stream should compress the data.
/// </summary> /// </summary>
Compress= 0, Compress= 0,
/// <summary> /// <summary>
/// Used to specify that the stream should decompress the data. /// Used to specify that the stream should decompress the data.
/// </summary> /// </summary>
Decompress = 1, Decompress = 1,
} }
/// <summary> /// <summary>
/// A general purpose exception class for exceptions in the Zlib library. /// A general purpose exception class for exceptions in the Zlib library.
/// </summary> /// </summary>
[Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")] [Guid("ebc25cf6-9120-4283-b972-0e5520d0000E")]
public class ZlibException : System.Exception public class ZlibException : System.Exception
{ {
/// <summary> /// <summary>
/// The ZlibException class captures exception information generated /// The ZlibException class captures exception information generated
/// by the Zlib library. /// by the Zlib library.
/// </summary> /// </summary>
public ZlibException() public ZlibException()
: base() : base()
{ {
} }
/// <summary> /// <summary>
/// This ctor collects a message attached to the exception. /// This ctor collects a message attached to the exception.
/// </summary> /// </summary>
/// <param name="s">the message for the exception.</param> /// <param name="s">the message for the exception.</param>
public ZlibException(System.String s) public ZlibException(System.String s)
: base(s) : base(s)
{ {
} }
} }
internal class SharedUtils internal class SharedUtils
{ {
/// <summary> /// <summary>
/// Performs an unsigned bitwise right shift with the specified number /// Performs an unsigned bitwise right shift with the specified number
/// </summary> /// </summary>
/// <param name="number">Number to operate on</param> /// <param name="number">Number to operate on</param>
/// <param name="bits">Ammount of bits to shift</param> /// <param name="bits">Ammount of bits to shift</param>
/// <returns>The resulting number from the shift operation</returns> /// <returns>The resulting number from the shift operation</returns>
public static int URShift(int number, int bits) public static int URShift(int number, int bits)
{ {
return (int)((uint)number >> bits); return (int)((uint)number >> bits);
} }
#if NOT #if NOT
/// <summary> /// <summary>
/// Performs an unsigned bitwise right shift with the specified number /// Performs an unsigned bitwise right shift with the specified number
/// </summary> /// </summary>
/// <param name="number">Number to operate on</param> /// <param name="number">Number to operate on</param>
/// <param name="bits">Ammount of bits to shift</param> /// <param name="bits">Ammount of bits to shift</param>
/// <returns>The resulting number from the shift operation</returns> /// <returns>The resulting number from the shift operation</returns>
public static long URShift(long number, int bits) public static long URShift(long number, int bits)
{ {
return (long) ((UInt64)number >> bits); return (long) ((UInt64)number >> bits);
} }
#endif #endif
/// <summary> /// <summary>
/// Reads a number of characters from the current source TextReader and writes /// Reads a number of characters from the current source TextReader and writes
/// the data to the target array at the specified index. /// the data to the target array at the specified index.
/// </summary> /// </summary>
/// ///
/// <param name="sourceTextReader">The source TextReader to read from</param> /// <param name="sourceTextReader">The source TextReader to read from</param>
/// <param name="target">Contains the array of characteres read from the source TextReader.</param> /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
/// <param name="start">The starting index of the target array.</param> /// <param name="start">The starting index of the target array.</param>
/// <param name="count">The maximum number of characters to read from the source TextReader.</param> /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
/// ///
/// <returns> /// <returns>
/// The number of characters read. The number will be less than or equal to /// The number of characters read. The number will be less than or equal to
/// count depending on the data available in the source TextReader. Returns -1 /// count depending on the data available in the source TextReader. Returns -1
/// if the end of the stream is reached. /// if the end of the stream is reached.
/// </returns> /// </returns>
public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count) public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
{ {
// Returns 0 bytes if not enough space in target // Returns 0 bytes if not enough space in target
if (target.Length == 0) return 0; if (target.Length == 0) return 0;
char[] charArray = new char[target.Length]; char[] charArray = new char[target.Length];
int bytesRead = sourceTextReader.Read(charArray, start, count); int bytesRead = sourceTextReader.Read(charArray, start, count);
// Returns -1 if EOF // Returns -1 if EOF
if (bytesRead == 0) return -1; if (bytesRead == 0) return -1;
for (int index = start; index < start + bytesRead; index++) for (int index = start; index < start + bytesRead; index++)
target[index] = (byte)charArray[index]; target[index] = (byte)charArray[index];
return bytesRead; return bytesRead;
} }
internal static byte[] ToByteArray(System.String sourceString) internal static byte[] ToByteArray(System.String sourceString)
{ {
return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString); return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
} }
internal static char[] ToCharArray(byte[] byteArray) internal static char[] ToCharArray(byte[] byteArray)
{ {
return System.Text.UTF8Encoding.UTF8.GetChars(byteArray); return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
} }
} }
internal static class InternalConstants internal static class InternalConstants
{ {
internal static readonly int MAX_BITS = 15; internal static readonly int MAX_BITS = 15;
internal static readonly int BL_CODES = 19; internal static readonly int BL_CODES = 19;
internal static readonly int D_CODES = 30; internal static readonly int D_CODES = 30;
internal static readonly int LITERALS = 256; internal static readonly int LITERALS = 256;
internal static readonly int LENGTH_CODES = 29; internal static readonly int LENGTH_CODES = 29;
internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES); internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
// Bit length codes must not exceed MAX_BL_BITS bits // Bit length codes must not exceed MAX_BL_BITS bits
internal static readonly int MAX_BL_BITS = 7; internal static readonly int MAX_BL_BITS = 7;
// repeat previous bit length 3-6 times (2 bits of repeat count) // repeat previous bit length 3-6 times (2 bits of repeat count)
internal static readonly int REP_3_6 = 16; internal static readonly int REP_3_6 = 16;
// repeat a zero length 3-10 times (3 bits of repeat count) // repeat a zero length 3-10 times (3 bits of repeat count)
internal static readonly int REPZ_3_10 = 17; internal static readonly int REPZ_3_10 = 17;
// repeat a zero length 11-138 times (7 bits of repeat count) // repeat a zero length 11-138 times (7 bits of repeat count)
internal static readonly int REPZ_11_138 = 18; internal static readonly int REPZ_11_138 = 18;
} }
internal sealed class StaticTree internal sealed class StaticTree
{ {
internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] { internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8, 12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8,
28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8, 28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8,
2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8, 2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8,
18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8, 18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8,
10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8, 10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8, 26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8,
6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8, 6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8, 22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8,
14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8, 14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8,
30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8, 30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8, 1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8,
17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8, 17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8,
9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8, 9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8,
25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8, 25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8,
5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8, 5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8, 21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8,
13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8, 13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8,
29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8, 29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8,
19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9, 19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9, 51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9, 11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9,
43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9, 43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9,
27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9, 27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9,
59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9, 59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9,
7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9, 7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9, 39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9,
23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9, 23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9,
55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9, 55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9,
15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9, 15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9,
47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9, 47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9, 31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9,
63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9, 63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9,
0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7, 0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7,
8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7, 8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7,
4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7, 4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8 3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8
}; };
internal static readonly short[] distTreeCodes = new short[] { internal static readonly short[] distTreeCodes = new short[] {
0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5, 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5,
2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5, 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5,
1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5, 1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5,
3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 }; 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
internal static readonly StaticTree Literals; internal static readonly StaticTree Literals;
internal static readonly StaticTree Distances; internal static readonly StaticTree Distances;
internal static readonly StaticTree BitLengths; internal static readonly StaticTree BitLengths;
internal short[] treeCodes; // static tree or null internal short[] treeCodes; // static tree or null
internal int[] extraBits; // extra bits for each code or null internal int[] extraBits; // extra bits for each code or null
internal int extraBase; // base index for extra_bits internal int extraBase; // base index for extra_bits
internal int elems; // max number of elements in the tree internal int elems; // max number of elements in the tree
internal int maxLength; // max bit length for the codes internal int maxLength; // max bit length for the codes
private StaticTree(short[] treeCodes, int[] extraBits, int extraBase, int elems, int maxLength) private StaticTree(short[] treeCodes, int[] extraBits, int extraBase, int elems, int maxLength)
{ {
this.treeCodes = treeCodes; this.treeCodes = treeCodes;
this.extraBits = extraBits; this.extraBits = extraBits;
this.extraBase = extraBase; this.extraBase = extraBase;
this.elems = elems; this.elems = elems;
this.maxLength = maxLength; this.maxLength = maxLength;
} }
static StaticTree() static StaticTree()
{ {
Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS); Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS); Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS);
BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS); BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
} }
} }
/// <summary> /// <summary>
/// Computes an Adler-32 checksum. /// Computes an Adler-32 checksum.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The Adler checksum is similar to a CRC checksum, but faster to compute, though less /// 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 /// 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 /// is a required part of the "ZLIB" standard. Applications will almost never need to
/// use this class directly. /// use this class directly.
/// </remarks> /// </remarks>
/// ///
/// <exclude/> /// <exclude/>
public sealed class Adler public sealed class Adler
{ {
// largest prime smaller than 65536 // largest prime smaller than 65536
private static readonly uint BASE = 65521; private static readonly uint BASE = 65521;
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private static readonly int NMAX = 5552; private static readonly int NMAX = 5552;
#pragma warning disable 3001 #pragma warning disable 3001
#pragma warning disable 3002 #pragma warning disable 3002
/// <summary> /// <summary>
/// Calculates the Adler32 checksum. /// Calculates the Adler32 checksum.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// This is used within ZLIB. You probably don't need to use this directly. /// This is used within ZLIB. You probably don't need to use this directly.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <example> /// <example>
/// To compute an Adler32 checksum on a byte array: /// To compute an Adler32 checksum on a byte array:
/// <code> /// <code>
/// var adler = Adler.Adler32(0, null, 0, 0); /// var adler = Adler.Adler32(0, null, 0, 0);
/// adler = Adler.Adler32(adler, buffer, index, length); /// adler = Adler.Adler32(adler, buffer, index, length);
/// </code> /// </code>
/// </example> /// </example>
public static uint Adler32(uint adler, byte[] buf, int index, int len) public static uint Adler32(uint adler, byte[] buf, int index, int len)
{ {
if (buf == null) if (buf == null)
return 1; return 1;
uint s1 = (uint) (adler & 0xffff); uint s1 = (uint) (adler & 0xffff);
uint s2 = (uint) ((adler >> 16) & 0xffff); uint s2 = (uint) ((adler >> 16) & 0xffff);
while (len > 0) while (len > 0)
{ {
int k = len < NMAX ? len : NMAX; int k = len < NMAX ? len : NMAX;
len -= k; len -= k;
while (k >= 16) while (k >= 16)
{ {
//s1 += (buf[index++] & 0xff); s2 += s1; //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;
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; k -= 16;
} }
if (k != 0) if (k != 0)
{ {
do do
{ {
s1 += buf[index++]; s1 += buf[index++];
s2 += s1; s2 += s1;
} }
while (--k != 0); while (--k != 0);
} }
s1 %= BASE; s1 %= BASE;
s2 %= BASE; s2 %= BASE;
} }
return (uint)((s2 << 16) | s1); return (uint)((s2 << 16) | s1);
} }
#pragma warning restore 3001 #pragma warning restore 3001
#pragma warning restore 3002 #pragma warning restore 3002
} }
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -61,68 +61,66 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System; namespace SabreTools.Helper
namespace Ionic.Zlib
{ {
/// <summary> /// <summary>
/// A bunch of constants used in the Zlib interface. /// A bunch of constants used in the Zlib interface.
/// </summary> /// </summary>
public static class ZlibConstants public static class ZlibConstants
{ {
/// <summary> /// <summary>
/// The maximum number of window bits for the Deflate algorithm. /// The maximum number of window bits for the Deflate algorithm.
/// </summary> /// </summary>
public const int WindowBitsMax = 15; // 32K LZ77 window public const int WindowBitsMax = 15; // 32K LZ77 window
/// <summary> /// <summary>
/// The default number of window bits for the Deflate algorithm. /// The default number of window bits for the Deflate algorithm.
/// </summary> /// </summary>
public const int WindowBitsDefault = WindowBitsMax; public const int WindowBitsDefault = WindowBitsMax;
/// <summary> /// <summary>
/// indicates everything is A-OK /// indicates everything is A-OK
/// </summary> /// </summary>
public const int Z_OK = 0; public const int Z_OK = 0;
/// <summary> /// <summary>
/// Indicates that the last operation reached the end of the stream. /// Indicates that the last operation reached the end of the stream.
/// </summary> /// </summary>
public const int Z_STREAM_END = 1; public const int Z_STREAM_END = 1;
/// <summary> /// <summary>
/// The operation ended in need of a dictionary. /// The operation ended in need of a dictionary.
/// </summary> /// </summary>
public const int Z_NEED_DICT = 2; public const int Z_NEED_DICT = 2;
/// <summary> /// <summary>
/// There was an error with the stream - not enough data, not open and readable, etc. /// There was an error with the stream - not enough data, not open and readable, etc.
/// </summary> /// </summary>
public const int Z_STREAM_ERROR = -2; public const int Z_STREAM_ERROR = -2;
/// <summary> /// <summary>
/// There was an error with the data - not enough data, bad data, etc. /// There was an error with the data - not enough data, bad data, etc.
/// </summary> /// </summary>
public const int Z_DATA_ERROR = -3; public const int Z_DATA_ERROR = -3;
/// <summary> /// <summary>
/// There was an error with the working buffer. /// There was an error with the working buffer.
/// </summary> /// </summary>
public const int Z_BUF_ERROR = -5; public const int Z_BUF_ERROR = -5;
/// <summary> /// <summary>
/// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes. /// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes.
/// </summary> /// </summary>
#if NETCF #if NETCF
public const int WorkingBufferSizeDefault = 8192; public const int WorkingBufferSizeDefault = 8192;
#else #else
public const int WorkingBufferSizeDefault = 16384; public const int WorkingBufferSizeDefault = 16384;
#endif #endif
/// <summary> /// <summary>
/// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes. /// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes.
/// </summary> /// </summary>
public const int WorkingBufferSizeMin = 1024; public const int WorkingBufferSizeMin = 1024;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -88,7 +88,7 @@ namespace SabreTools.Helper
} }
public bool Contains(string n) public bool Contains(string n)
{ {
return _entries.Contains(new ZipFileEntry(new MemoryStream(), n, true)); return _entries.Contains(new ZipFileEntry(new MemoryStream(), n));
} }
#endregion #endregion

View File

@@ -1,6 +1,4 @@
using OCRC; using OCRC;
using Ionic.Crc;
using Ionic.Zlib;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@@ -137,20 +135,16 @@ namespace SabreTools.Helper
/// </summary> /// </summary>
/// <param name="zipstream">Stream representing the entry</param> /// <param name="zipstream">Stream representing the entry</param>
/// <param name="filename">Internal filename to use</param> /// <param name="filename">Internal filename to use</param>
/// <param name="torrentZip">True if the file should be set with TorrentZip defaults (default), false otherwise</param> public ZipFileEntry(Stream zipstream, string filename)
public ZipFileEntry(Stream zipstream, string filename, bool torrentZip = true)
{ {
_zip64 = false; _zip64 = false;
_zipstream = zipstream; _zipstream = zipstream;
_generalPurposeBitFlag = GeneralPurposeBitFlag.DeflatingMaximumCompression; _generalPurposeBitFlag = GeneralPurposeBitFlag.DeflatingMaximumCompression;
_compressionMethod = CompressionMethod.Deflated; _compressionMethod = CompressionMethod.Deflated;
FileName = filename; _lastModFileTime = 48128;
_lastModFileDate = 8600;
if (torrentZip) FileName = filename;
{
_lastModFileTime = 48128;
_lastModFileDate = 8600;
}
} }
#endregion #endregion

View File

@@ -13,6 +13,8 @@ namespace SabreTools.Helper
{ {
public class ArchiveTools public class ArchiveTools
{ {
private const int _bufferSize = 4096 * 128;
#region Archive-to-Archive Handling #region Archive-to-Archive Handling
/// <summary> /// <summary>
@@ -759,7 +761,7 @@ namespace SabreTools.Helper
// If the archive doesn't already contain the entry, add it // If the archive doesn't already contain the entry, add it
if (outarchive.GetEntry(rom.Name) == null) if (outarchive.GetEntry(rom.Name) == null)
{ {
ZipArchiveEntry ae = outarchive.CreateEntry(rom.Name, CompressionLevel.Optimal); ZipArchiveEntry ae = outarchive.CreateEntry(rom.Name);
Stream outputStream = ae.Open(); Stream outputStream = ae.Open();
inputStreams[i].CopyTo(outputStream); inputStreams[i].CopyTo(outputStream);
outputStream.Flush(); outputStream.Flush();
@@ -888,9 +890,9 @@ namespace SabreTools.Helper
zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream); zipReturn = zipFile.OpenWriteStream(false, true, rom.Name, streamSize, CompressionMethod.Deflated, out writeStream);
// Copy the input stream to the output // Copy the input stream to the output
byte[] buffer = new byte[8 * 1024]; byte[] buffer = new byte[_bufferSize];
int len; int len;
while ((len = fs.Read(buffer, 0, buffer.Length)) > 0) while ((len = fs.Read(buffer, 0, _bufferSize)) > 0)
{ {
writeStream.Write(buffer, 0, len); writeStream.Write(buffer, 0, len);
} }
@@ -956,9 +958,9 @@ namespace SabreTools.Helper
zipFile.OpenWriteStream(false, true, roms[-index - 1].Name, istreamSize, CompressionMethod.Deflated, out writeStream); zipFile.OpenWriteStream(false, true, roms[-index - 1].Name, istreamSize, CompressionMethod.Deflated, out writeStream);
// Copy the input stream to the output // Copy the input stream to the output
byte[] ibuffer = new byte[8 * 1024]; byte[] ibuffer = new byte[_bufferSize];
int ilen; int ilen;
while ((ilen = freadStream.Read(ibuffer, 0, ibuffer.Length)) > 0) while ((ilen = freadStream.Read(ibuffer, 0, _bufferSize)) > 0)
{ {
writeStream.Write(ibuffer, 0, ilen); writeStream.Write(ibuffer, 0, ilen);
} }
@@ -977,9 +979,9 @@ namespace SabreTools.Helper
zipFile.OpenWriteStream(false, true, oldZipFile.Filename(index), istreamSize, CompressionMethod.Deflated, out writeStream); zipFile.OpenWriteStream(false, true, oldZipFile.Filename(index), istreamSize, CompressionMethod.Deflated, out writeStream);
// Copy the input stream to the output // Copy the input stream to the output
byte[] ibuffer = new byte[8 * 1024]; byte[] ibuffer = new byte[_bufferSize];
int ilen; int ilen;
while ((ilen = zreadStream.Read(ibuffer, 0, ibuffer.Length)) > 0) while ((ilen = zreadStream.Read(ibuffer, 0, _bufferSize)) > 0)
{ {
writeStream.Write(ibuffer, 0, ilen); writeStream.Write(ibuffer, 0, ilen);
} }