📄 ezzlib.pas
字号:
0: (dad: ush);
1: (len: ush);
end;
end;
ltree_type = array[0..HEAP_SIZE - 1] of ct_data;
dtree_type = array[0..2 * D_CODES + 1 - 1] of ct_data;
htree_type = array[0..2 * BL_CODES + 1 - 1] of ct_data;
tree_type = array[0..(MaxMemBlock div SizeOf(ct_data)) - 1] of ct_data;
tree_ptr = ^tree_type;
ltree_ptr = ^ltree_type;
dtree_ptr = ^dtree_type;
htree_ptr = ^htree_type;
static_tree_desc_ptr = ^static_tree_desc;
static_tree_desc = record
static_tree: tree_ptr;
extra_bits: pzIntfArray;
extra_base: int;
elems: int;
max_length: int;
end;
tree_desc_ptr = ^tree_desc;
tree_desc = record
dyn_tree: tree_ptr;
max_code: int;
stat_desc: static_tree_desc_ptr;
end;
Pos = ush;
Posf = Pos; {FAR}
IPos = uInt;
pPosf = ^Posf;
zPosfArray = array[0..(MaxMemBlock div SizeOf(Posf)) - 1] of Posf;
pzPosfArray = ^zPosfArray;
deflate_state_ptr = ^deflate_state;
deflate_state = record
strm: z_streamp;
status: int;
pending_buf: pzByteArray;
pending_buf_size: ulg;
pending_out: pBytef;
pending: int;
noheader: int;
data_type: Byte;
method: Byte;
last_flush: int;
w_size: uInt;
w_bits: uInt;
w_mask: uInt;
window: pzByteArray;
window_size: ulg;
prev: pzPosfArray;
head: pzPosfArray;
ins_h: uInt;
hash_size: uInt;
hash_bits: uInt;
hash_mask: uInt;
hash_shift: uInt;
block_start: long;
match_length: uInt;
prev_match: IPos;
match_available: boolean;
strstart: uInt;
match_start: uInt;
lookahead: uInt;
prev_length: uInt;
max_chain_length: uInt;
level: int;
strategy: int;
good_match: uInt;
nice_match: int;
dyn_ltree: ltree_type;
dyn_dtree: dtree_type;
bl_tree: htree_type;
l_desc: tree_desc;
d_desc: tree_desc;
bl_desc: tree_desc;
bl_count: array[0..MAX_BITS + 1 - 1] of ush;
heap: array[0..2 * L_CODES + 1 - 1] of int;
heap_len: int;
heap_max: int;
depth: array[0..2 * L_CODES + 1 - 1] of uch;
l_buf: puchfArray;
lit_bufsize: uInt;
last_lit: uInt;
d_buf: pushfArray;
opt_len: ulg;
static_len: ulg;
compressed_len: ulg;
matches: uInt;
last_eob_len: int;
bi_buf: ush;
bi_valid: int;
case byte of
0: (max_lazy_match: uInt);
1: (max_insert_length: uInt);
end;
const
cplens: array[0..30] of uInt
= (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);
invalid_code = 112;
cplext: array[0..30] of uInt
= (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, invalid_code, invalid_code);
cpdist: array[0..29] of uInt
= (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);
cpdext: array[0..29] of uInt
= (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);
const
BMAX = 15;
{$DEFINE USE_PTR}
function huft_build(var b: array of uIntf;
n: uInt; s: uInt; const d: array of uIntf; const e: array of uIntf;
t: ppInflate_huft; var m: uIntf; var hp: array of inflate_huft;
var hn: uInt; var v: array of uIntf): int;
{ Given a list of code lengths and a maximum table size, make a set of
tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
if the given code set is incomplete (the tables are still built in this
case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
lengths), or Z_MEM_ERROR if not enough memory. }
var
a: uInt;
c: array[0..BMAX] of uInt;
f: uInt;
g: int;
h: int;
i: uInt;
j: uInt;
k: Int;
l: int;
mask: uInt;
p: ^uIntf;
q: pInflate_huft;
r: inflate_huft;
u: array[0..BMAX - 1] of pInflate_huft;
w: int;
x: array[0..BMAX] of uInt;
{$IFDEF USE_PTR}
xp: puIntf;
{$ELSE}
xp: uInt;
{$ENDIF}
y: int;
z: uInt;
begin
FillChar(c, SizeOf(c), 0);
for i := 0 to n - 1 do
Inc(c[b[i]]);
if (c[0] = n) then begin
t^ := pInflate_huft(nil);
m := 0;
huft_build := Z_OK;
Exit;
end;
l := m;
for j := 1 to BMAX do
if (c[j] <> 0) then
break;
k := j;
if (uInt(l) < j) then
l := j;
for i := BMAX downto 1 do
if (c[i] <> 0) then
break;
g := i;
if (uInt(l) > i) then
l := i;
m := l;
y := 1 shl j;
while (j < i) do begin
Dec(y, c[j]);
if (y < 0) then begin
huft_build := Z_DATA_ERROR;
exit;
end;
Inc(j);
y := y shl 1
end;
Dec(y, c[i]);
if (y < 0) then begin
huft_build := Z_DATA_ERROR;
exit;
end;
Inc(c[i], y);
{$IFDEF USE_PTR}
x[1] := 0;
j := 0;
p := @c[1];
xp := @x[2];
dec(i);
while (i > 0) do begin
inc(j, p^);
xp^ := j;
inc(p);
inc(xp);
dec(i);
end;
{$ELSE}
x[1] := 0;
j := 0;
for i := 1 to g do begin
x[i] := j;
Inc(j, c[i]);
end;
{$ENDIF}
for i := 0 to n - 1 do begin
j := b[i];
if (j <> 0) then begin
v[x[j]] := i;
Inc(x[j]);
end;
end;
n := x[g];
i := 0;
x[0] := 0;
p := Addr(v);
h := -1;
w := -l;
u[0] := pInflate_huft(nil);
q := pInflate_huft(nil);
z := 0;
while (k <= g) do begin
a := c[k];
while (a <> 0) do begin
Dec(a);
while (k > w + l) do begin
Inc(h);
Inc(w, l);
z := g - w;
if (z > uInt(l)) then z := l;
j := k - w;
f := 1 shl j;
if (f > a + 1) then begin
Dec(f, a + 1);
{$IFDEF USE_PTR}
xp := Addr(c[k]);
if (j < z) then begin
Inc(j);
while (j < z) do begin
f := f shl 1;
Inc(xp);
if (f <= xp^) then
break;
Dec(f, xp^);
Inc(j);
end;
end;
{$ELSE}
xp := k;
if (j < z) then begin
Inc(j);
while (j < z) do begin
f := f * 2;
Inc(xp);
if (f <= c[xp]) then Break;
Dec(f, c[xp]);
Inc(j);
end;
end;
{$ENDIF}
end;
z := 1 shl j;
if (hn + z > MANY) then begin
huft_build := Z_MEM_ERROR;
exit;
end;
q := @hp[hn];
u[h] := q;
Inc(hn, z);
if (h <> 0) then begin
x[h] := i;
r.bits := Byte(l);
r.exop := Byte(j);
j := i shr (w - l);
r.base := (ptr2int(q) - ptr2int(u[h - 1])) div sizeof(q^) - j;
huft_Ptr(u[h - 1])^[j] := r;
end
else
t^ := q;
end;
r.bits := Byte(k - w);
if ptr2int(p) >= ptr2int(@(v[n])) then
r.exop := 128 + 64
else
if (p^ < s) then begin
if (p^ < 256) then
r.exop := 0
else
r.exop := 32 + 64;
r.base := p^;
Inc(p);
end
else begin
r.exop := Byte(e[p^ - s] + 16 + 64);
r.base := d[p^ - s];
Inc(p);
end;
f := 1 shl (k - w);
j := i shr w;
while (j < z) do begin
huft_Ptr(q)^[j] := r;
Inc(j, f);
end;
j := 1 shl (k - 1);
while (i and j) <> 0 do begin
i := i xor j;
j := j shr 1
end;
i := i xor j;
mask := (1 shl w) - 1;
while ((i and mask) <> x[h]) do begin
Dec(h);
Dec(w, l);
mask := (1 shl w) - 1;
end;
end;
Inc(k);
end;
{ Return Z_BUF_ERROR if we were given an incomplete table }
if (y <> 0) and (g <> 1) then
huft_build := Z_BUF_ERROR
else
huft_build := Z_OK;
end;
function inflate_trees_bits(var c: array of uIntf;
var bb: uIntf; var tb: pinflate_huft; var hp: array of Inflate_huft;
var z: z_stream): int;
var
r: int;
hn: uInt;
v: PuIntArray;
begin
hn := 0;
v := PuIntArray(ZALLOC(z, 19, sizeof(uInt)));
if (v = Z_NULL) then begin
inflate_trees_bits := Z_MEM_ERROR;
exit;
end;
r := huft_build(c, 19, 19, cplens, cplext, @tb, bb, hp, hn, v^);
if (r = Z_DATA_ERROR) then
z.msg := 'oversubscribed dynamic bit lengths tree'
else if (r = Z_BUF_ERROR) or (bb = 0) then begin
z.msg := 'incomplete dynamic bit lengths tree';
r := Z_DATA_ERROR;
end;
ZFREE(z, v);
inflate_trees_bits := r;
end;
function inflate_trees_dynamic(nl: uInt;
nd: uInt; var c: array of uIntf; var bl: uIntf; var bd: uIntf;
var tl: pInflate_huft; var td: pInflate_huft; var hp: array of Inflate_huft;
var z: z_stream): int;
var
r: int;
hn: uInt;
v: PuIntArray;
begin
hn := 0;
v := PuIntArray(ZALLOC(z, 288, sizeof(uInt)));
if (v = Z_NULL) then begin
inflate_trees_dynamic := Z_MEM_ERROR;
exit;
end;
r := huft_build(c, nl, 257, cplens, cplext, @tl, bl, hp, hn, v^);
if (r <> Z_OK) or (bl = 0) then begin
if (r = Z_DATA_ERROR) then
z.msg := 'oversubscribed literal/length tree'
else if (r <> Z_MEM_ERROR) then begin
z.msg := 'incomplete literal/length tree';
r := Z_DATA_ERROR;
end;
ZFREE(z, v);
inflate_trees_dynamic := r;
exit;
end;
r := huft_build(puIntArray(@c[nl])^, nd, 0, cpdist, cpdext, @td, bd, hp, hn, v^);
if (r <> Z_OK) or ((bd = 0) and (nl > 257)) then begin
if (r = Z_DATA_ERROR) then
z.msg := 'oversubscribed literal/length tree'
else if (r = Z_BUF_ERROR) then begin
z.msg := 'incomplete literal/length tree';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -