📄 ztvdeflate.pas
字号:
{90} (fc: (freq: 104); dl: (Len: 7)), (fc: (freq: 24); dl: (Len: 7)), (fc: (freq: 88); dl: (Len: 7)), //270
(fc: (freq: 56); dl: (Len: 7)), (fc: (freq: 120); dl: (Len: 7)), (fc: (freq: 4); dl: (Len: 7)),
(fc: (freq: 68); dl: (Len: 7)), (fc: (freq: 36); dl: (Len: 7)), (fc: (freq: 100); dl: (Len: 7)),
(fc: (freq: 20); dl: (Len: 7)), (fc: (freq: 84); dl: (Len: 7)), (fc: (freq: 52); dl: (Len: 7)),
(fc: (freq: 116); dl: (Len: 7)), (fc: (freq: 3); dl: (Len: 8)), (fc: (freq: 131); dl: (Len: 8)),
(fc: (freq: 67); dl: (Len: 8)), (fc: (freq: 195); dl: (Len: 8)), (fc: (freq: 35); dl: (Len: 8)), //285
{96} (fc: (freq: 163); dl: (Len: 8)), (fc: (freq: 99); dl: (Len: 8)), (fc: (freq: 227); dl: (Len: 8)) //288
);
static_dtree: Array[0..D_CODES - 1] Of ct_data = (
(fc: (freq: 0); dl: (Len: 5)), (fc: (freq: 16); dl: (Len: 5)), (fc: (freq: 8); dl: (Len: 5)),
(fc: (freq: 24); dl: (Len: 5)), (fc: (freq: 4); dl: (Len: 5)), (fc: (freq: 20); dl: (Len: 5)),
(fc: (freq: 12); dl: (Len: 5)), (fc: (freq: 28); dl: (Len: 5)), (fc: (freq: 2); dl: (Len: 5)),
(fc: (freq: 18); dl: (Len: 5)), (fc: (freq: 10); dl: (Len: 5)), (fc: (freq: 26); dl: (Len: 5)),
(fc: (freq: 6); dl: (Len: 5)), (fc: (freq: 22); dl: (Len: 5)), (fc: (freq: 14); dl: (Len: 5)),
(fc: (freq: 30); dl: (Len: 5)), (fc: (freq: 1); dl: (Len: 5)), (fc: (freq: 17); dl: (Len: 5)),
(fc: (freq: 9); dl: (Len: 5)), (fc: (freq: 25); dl: (Len: 5)), (fc: (freq: 5); dl: (Len: 5)),
(fc: (freq: 21); dl: (Len: 5)), (fc: (freq: 13); dl: (Len: 5)), (fc: (freq: 29); dl: (Len: 5)),
(fc: (freq: 3); dl: (Len: 5)), (fc: (freq: 19); dl: (Len: 5)), (fc: (freq: 11); dl: (Len: 5)),
(fc: (freq: 27); dl: (Len: 5)), (fc: (freq: 7); dl: (Len: 5)), (fc: (freq: 23); dl: (Len: 5))
);
_length_code: Array[0..MAX_MATCH - MIN_MATCH + 1 - 1] Of uch = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
);
_dist_code: Array[0..DIST_CODE_LEN - 1] Of uch = (
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
);
// Distance Codes
// --------------
// Extra Extra Extra Extra
// Code Bits Dist Code Bits Dist Code Bits Dist Code Bits Dist
// ---- ---- ---- ---- ---- ------ ---- ---- ---- ---- ---- ----
// 0 0 1 8 3 17-24 16 7 257-384 24 11 4097-6144
// 1 0 2 9 3 25-32 17 7 385-512 25 11 6145-8192
// 2 0 3 10 4 33-48 18 8 513-768 26 12 8193-12288
// 3 0 4 11 4 49-64 19 8 769-1024 27 12 12289-16384
// 4 1 5,6 12 5 65-96 20 9 1025-1536 28 13 16385-24576
// 5 1 7,8 13 5 97-128 21 9 1537-2048 29 13 24577-32768
// 6 2 9-12 14 6 129-192 22 10 2049-3072
// 7 2 13-16 15 6 193-256 23 10 3073-4096
//
// Extra
// Code Bits Dist
// ---- ---- ----
// .. .. ...
// 29 13 24577-32768
// 30 14 32769-49152
// 31 14 49153-65536
//
// base_dist = Dist column
// extra_dbits = Bits column
//
{$IFDEF DEFLATE64}
extra_dbits: Array[0..D_CODES_DEF64 - 1] Of _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, 14, 14);
base_dist: Array[0..D_CODES_DEF64 - 1] Of _int =
(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,
32768, 49152);
{$ELSE}
extra_dbits: Array[0..D_CODES - 1] Of _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);
base_dist: Array[0..D_CODES - 1] Of _int =
(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);
{$ENDIF}
// Length Codes
// ------------
// Extra Extra Extra Extra
// Code Bits Length Code Bits Lengths Code Bits Lengths Code Bits Lengths
// ---- ---- ------ ---- ---- ------- ---- ---- ------- ---- ---- ---------
// 257 0 3 265 1 11,12 273 3 35-42 281 5 131-162
// 258 0 4 266 1 13,14 274 3 43-50 282 5 163-194
// 259 0 5 267 1 15,16 275 3 51-58 283 5 195-226
// 260 0 6 268 1 17,18 276 3 59-66 284 5 227-258
// 261 0 7 269 2 19-22 277 4 67-82 285 0 258
// 262 0 8 270 2 23-26 278 4 83-98
// 263 0 9 271 2 27-30 279 4 99-114
// 264 0 10 272 2 31-34 280 4 115-130
//
// Extra
// Code Bits Lengths
// ---- ---- ------
// ... .. ...
// 284 5 227-258
// 285 16 3-65538
//
// extra_lbits = Bits column
//
{$IFDEF DEFLATE64}
extra_lbits: Array[0..30] Of _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, 16, 99, 99);
{$ELSE}
extra_lbits: Array[0..LENGTH_CODES - 1] Of _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);
{$ENDIF}
{ extra bits for each bit length code }
extra_blbits: Array[0..BL_CODES - 1] Of _int = (0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 3, 7);
bl_order: Array[0..BL_CODES - 1] Of uch = (16, 17, 18, 0, 8, 7, 9, 6, 10,
5, 11, 4, 12, 3, 13, 2, 14, 1, 15);
base_length: Array[0..LENGTH_CODES - 1] Of _int =
(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);
//base_length: array [0..28] of word =
// (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, 3);
{ note: the final 3 are for deflate64 only; for symbol 285,}
{ lengths are stored as length - 3}
static_l_desc: static_tree_desc =
(static_tree: (@(static_ltree)); { pointer to array of ct_data }
extra_bits: (@(extra_lbits)); { pointer to array of _int }
extra_base: LITERALS + 1;
elems: L_CODES;
max_length: MAX_BITS);
static_d_desc: static_tree_desc =
(static_tree: (@(static_dtree));
extra_bits: (@(extra_dbits));
extra_base: 0;
{$IFDEF DEFLATE64}
elems: D_CODES_DEF64;
{$ELSE}
elems: D_CODES;
{$ENDIF}
max_length: MAX_BITS);
static_bl_desc: static_tree_desc =
(static_tree: (Nil);
extra_bits: @(extra_blbits);
extra_base: 0;
elems: BL_CODES;
max_length: MAX_BL_BITS);
//-------------------------------------------------------------
Procedure INSERT_STRING(Var s: deflate_state; str: word; Var match_head: IPos);
Begin
{$IFDEF FASTEST}
s.ins_h := ((s.ins_h Shl s.hash_shift) Xor
(s.window^[(str) + (MIN_MATCH - 1)])) And s.hash_mask;
match_head := s.head[s.ins_h];
s.head[s.ins_h] := _Pos(str);
{$ELSE}
s.ins_h := ((s.ins_h Shl s.hash_shift) Xor
(s.window^[(str) + (MIN_MATCH - 1)])) And s.hash_mask;
match_head := s.head^[s.ins_h];
s.prev^[(str) And s.w_mask] := match_head;
s.head^[s.ins_h] := _Pos(str);
{$ENDIF}
End;
//-------------------------------------------------------------
Function ZALLOC(Var strm: ztv_stream; Items: _uInt; size: _uInt): voidpf;
Begin
ZALLOC := strm.ZALLOC(strm.opaque, Items, size);
End;
//-------------------------------------------------------------
Procedure ZFREE(Var strm: ztv_stream; ptr: voidpf);
Begin
strm.ZFREE(strm.opaque, ptr);
End;
//-------------------------------------------------------------
Function bi_reverse(code: _unsigned; { the value to invert }
Len: _int): _unsigned; { its bit length }
Var
res: _unsigned; {register}
Begin
res := 0;
Repeat
res := res Or (code And 1);
code := code Shr 1;
res := res Shl 1;
Dec(Len);
Until (Len <= 0);
bi_reverse := res Shr 1;
End;
//-------------------------------------------------------------
Procedure gen_codes(tree: tree_ptr; max_code: _int; Var bl_count: Array Of ushf);
Var
next_code: Array[0..MAX_BITS + 1 - 1] Of ush;
code: ush;
BITS: _int;
n: _int;
Var
Len: _int;
Begin
code := 0;
For BITS := 1 To MAX_BITS Do
Begin
code := ((code + bl_count[BITS - 1]) Shl 1);
next_code[BITS] := code;
End;
For n := 0 To max_code Do
Begin
Len := tree^[n].dl.Len;
If (Len = 0) Then
Continue;
{ reverse the bits }
tree^[n].fc.code := bi_reverse(next_code[Len], Len);
inc(next_code[Len]);
End;
End;
//-------------------------------------------------------------
Procedure tr_static_init;
{$IFDEF DEFLATE64}
{$define GEN_TREES_H} //deflate64
{$ELSE}
{$UNDEF GEN_TREES_H}
{$ENDIF}
{$IFDEF GEN_TREES_H}
Const
static_init_done: Boolean = False;
Var
n: _int; { iterates over tree elements }
BITS: _int; { bit counter }
bLength: _int; { length value }
code: _int; { code value }
dist: _int; { distance index }
bl_count: Array[0..MAX_BITS + 1 - 1] Of ush;
Begin
If (static_init_done) Then
Exit;
bLength := 0;
code := 0;
While code < LENGTH_CODES - 1 Do
Begin
base_length[code] := bLength;
For n := 0 To (1 Shl extra_lbits[code]) - 1 Do
Begin
_length_code[bLength] := uch(code);
inc(bLength);
End;
Inc(Code);
End;
// For code := 0 To LENGTH_CODES - 1 - 1 Do
// Begin
// base_length[code] := bLength;
// For n := 0 To (1 Shl extra_lbits[code]) - 1 Do
// Begin
// _length_code[bLength] := uch(code);
// inc(bLength);
// End;
// End;
Assert(bLength = 256, 'tr_static_init: blength <> 256');
_length_code[bLength - 1] := uch(code);
dist := 0;
For code := 0 To 16 - 1 Do
Begin
base_dist[code] := dist;
For n := 0 To (1 Shl extra_dbits[code]) - 1 Do
Begin
_dist_code[dist] := uch(code);
inc(dist);
End;
End;
Assert(dist = 256, 'tr_static_init: dist <> 256');
dist := dist Shr 7; { from this point, distances are divided by 128 }
//{$IFDEF DEFLATE64}
// For code := 16 To D_CODES_DEF64 - 1 Do
//{$ELSE}
For code := 16 To D_CODES - 1 Do
//{$ENDIF}
Begin
base_dist[code] := dist Shl 7;
For n := 0 To (1 Shl (extra_dbits[code] - 7)) - 1 Do
Begin
_dist_code[256 + dist] := uch(code);
inc(dist);
End;
End;
Assert(dist = 256, 'tr_static_init: 256+dist <> 512');
For BITS := 0 To MAX_BITS Do
bl_count[BITS] := 0;
n := 0;
While (n <= 143) Do
Begin
static_ltree[n].dl.Len := 8;
inc(n);
inc(bl_count[8]);
End;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -