Subversion Repositories Kolibri OS

Rev

Rev 6854 | Rev 6860 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6617 IgorA 1
; trees.asm -- output deflated data using Huffman coding
2
; Copyright (C) 1995-2012 Jean-loup Gailly
3
; detect_data_type() function provided freely by Cosmin Truta, 2006
4
; For conditions of distribution and use, see copyright notice in zlib.h
5
 
6
;  ALGORITHM
7
 
8
;      The "deflation" process uses several Huffman trees. The more
9
;      common source values are represented by shorter bit sequences.
10
 
11
;      Each code tree is stored in a compressed form which is itself
12
; a Huffman encoding of the lengths of all the code strings (in
13
; ascending order by source values).  The actual code strings are
14
; reconstructed from the lengths in the inflate process, as described
15
; in the deflate specification.
16
 
17
;  REFERENCES
18
 
19
;      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
20
;      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
21
 
22
;      Storer, James A.
23
;          Data Compression:  Methods and Theory, pp. 49-50.
24
;          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
25
 
26
;      Sedgewick, R.
27
;          Algorithms, p290.
28
;          Addison-Wesley, 1983. ISBN 0-201-06672-6.
29
 
30
; ===========================================================================
31
; Constants
32
 
33
 
34
MAX_BL_BITS equ 7
35
; Bit length codes must not exceed MAX_BL_BITS bits
36
 
37
END_BLOCK equ 256
38
; end of block literal code
39
 
40
REP_3_6     equ 16
41
; repeat previous bit length 3-6 times (2 bits of repeat count)
42
 
43
REPZ_3_10   equ 17
44
; repeat a zero length 3-10 times  (3 bits of repeat count)
45
 
46
REPZ_11_138 equ 18
47
; repeat a zero length 11-138 times  (7 bits of repeat count)
48
 
49
align 4
50
extra_lbits dd \ ;int [LENGTH_CODES] ;extra bits for each length code
51
	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
52
 
53
align 4
54
extra_dbits dd \ ;int [D_CODES] ;extra bits for each distance code
55
	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
56
 
57
align 4
58
extra_blbits dd \ ;int [BL_CODES] ;extra bits for each bit length code
59
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7
60
 
61
align 4
62
bl_order db 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15
63
; The lengths of the bit length codes are sent in order of decreasing
64
; probability, to avoid transmitting the lengths for unused bit length codes.
65
 
66
 
67
; ===========================================================================
68
; Local data. These are initialized only once.
69
 
70
 
71
DIST_CODE_LEN equ 512 ;see definition of array dist_code below
72
 
73
if GEN_TREES_H eq 1 ;| !(STDC)
74
; non ANSI compilers may not accept trees.inc
75
 
76
align 4
77
static_ltree rb sizeof.ct_data * (L_CODES+2)
78
; The static literal tree. Since the bit lengths are imposed, there is no
79
; need for the L_CODES extra codes used during heap construction. However
80
; The codes 286 and 287 are needed to build a canonical tree (see _tr_init
81
; below).
82
 
83
align 4
84
static_dtree rb sizeof.ct_data * D_CODES
85
; The static distance tree. (Actually a trivial tree since all codes use
86
; 5 bits.)
87
 
88
align 4
89
_dist_code rb DIST_CODE_LEN ;uch[]
90
; Distance codes. The first 256 values correspond to the distances
91
; 3 .. 258, the last 256 values correspond to the top 8 bits of
92
; the 15 bit distances.
93
 
94
align 4
95
_length_code rb MAX_MATCH-MIN_MATCH+1 ;uch[]
96
; length code for each normalized match length (0 == MIN_MATCH)
97
 
98
align 4
99
base_length rd LENGTH_CODES ;int[]
100
; First normalized length for each code (0 = MIN_MATCH)
101
 
102
align 4
103
base_dist rd D_CODES ;int[]
104
; First normalized distance for each code (0 = distance of 1)
105
 
106
else
107
include 'trees.inc'
108
end if ;GEN_TREES_H
109
 
110
struct static_tree_desc ;_s
111
	static_tree dd ? ;const ct_data * ;static tree or NULL
112
	extra_bits  dd ? ;const intf * ;extra bits for each code or NULL
113
	extra_base  dd ? ;int ;base index for extra_bits
114
	elems       dd ? ;int ;max number of elements in the tree
115
	max_length  dd ? ;int ;max bit length for the codes
116
ends
117
 
118
align 4
119
static_l_desc static_tree_desc static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS
120
 
121
align 4
122
static_d_desc static_tree_desc static_dtree, extra_dbits, 0, D_CODES, MAX_BITS
123
 
124
align 4
125
static_bl_desc static_tree_desc 0, extra_blbits, 0, BL_CODES, MAX_BL_BITS
126
 
127
; ===========================================================================
128
; Local (static) routines in this file.
129
 
130
 
131
macro send_code s, c, tree
132
{
133
if DEBUG eq 1
134
;	if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c))
135
end if
136
push eax ebx
137
if c eq eax
138
else
139
	mov eax,c
140
end if
141
	imul eax,sizeof.ct_data
142
	add eax,tree
143
	movzx ebx,word[eax+Len]
144
	push ebx
145
	movzx ebx,word[eax+Code]
146
	push ebx
147
	stdcall send_bits, s ;tree[c].Code, tree[c].Len
148
pop ebx eax
149
}
150
; Send a code of the given tree[c] and tree must not have side effects
151
 
152
; ===========================================================================
153
; Output a short LSB first on the stream.
154
; IN assertion: there is enough room in pendingBuf.
155
 
156
macro put_short s, w
157
{
6741 IgorA 158
	mov eax,[s+deflate_state.pending]
6617 IgorA 159
	add eax,[s+deflate_state.pending_buf]
160
	mov word[eax],w
6741 IgorA 161
	add dword[s+deflate_state.pending],2
6617 IgorA 162
}
163
 
164
; ===========================================================================
165
; Send a value on a given number of bits.
166
; IN assertion: length <= 16 and value fits in length bits.
167
 
168
;void (s, value, length)
169
;    deflate_state* s
170
;    int value  ;value to send
171
;    int length ;number of bits
172
align 4
173
proc send_bits uses eax ecx edi, s:dword, value:dword, length:dword
174
;    Tracevv((stderr," l %2d v %4x ", length, value));
175
;if DEBUG eq 1
176
	mov eax,[length]
177
	cmp eax,0
178
	jle @f
179
	cmp eax,15
180
	jle .end1
181
	@@:
6639 IgorA 182
		zlib_assert 'invalid length' ;Assert(..>0 && ..<=15)
6617 IgorA 183
	.end1:
184
	mov edi,[s]
6847 IgorA 185
	;;add [edi+deflate_state.bits_sent],eax
6617 IgorA 186
 
187
	; If not enough room in bi_buf, use (valid) bits from bi_buf and
188
	; (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
189
	; unused bits in value.
190
 
191
	mov ecx,Buf_size
192
	sub ecx,eax
193
	cmp [edi+deflate_state.bi_valid],ecx
194
	jle @f ;if (..>..)
195
		mov eax,[value]
196
		mov ecx,[edi+deflate_state.bi_valid]
197
		shl eax,cl
198
		or [edi+deflate_state.bi_buf],ax
199
		mov cx,[edi+deflate_state.bi_buf]
200
		put_short edi, cx
201
		mov eax,[value]
202
		mov ecx,Buf_size
203
		sub ecx,[edi+deflate_state.bi_valid]
6851 IgorA 204
		sar eax,cl
6617 IgorA 205
		mov [edi+deflate_state.bi_buf],ax
206
		mov eax,[length]
207
		sub eax,Buf_size
208
		jmp .end0
209
	@@: ;else
210
		mov eax,[value]
211
		mov ecx,[edi+deflate_state.bi_valid]
212
		shl eax,cl
213
		or [edi+deflate_state.bi_buf],ax
214
		mov eax,[length]
215
	.end0:
216
	add [edi+deflate_state.bi_valid],eax
217
;else ;!DEBUG
218
 
219
;{ int len = length;
220
;  if (s->bi_valid > (int)Buf_size - len) {
221
;    int val = value;
222
;    s->bi_buf |= (uint_16)val << s->bi_valid;
223
;    put_short(s, s->bi_buf);
224
;    s->bi_buf = (uint_16)val >> (Buf_size - s->bi_valid);
225
;    s->bi_valid += len - Buf_size;
226
;  } else {
227
;    s->bi_buf |= (uint_16)(value) << s->bi_valid;
228
;    s->bi_valid += len;
229
;  }
230
;}
231
;end if ;DEBUG
232
	ret
233
endp
234
 
235
; the arguments must not have side effects
236
 
237
; ===========================================================================
238
; Initialize the various 'constant' tables.
239
 
240
;int static_init_done = 0
241
 
242
;void ()
243
align 4
244
proc tr_static_init
245
if GEN_TREES_H eq 1
246
 
247
;    int n      ;iterates over tree elements
248
;    int bits   ;bit counter
249
;    int length ;length value
250
;    int code   ;code value
251
;    int dist   ;distance index
252
;    uint_16 bl_count[MAX_BITS+1];
253
	; number of codes at each bit length for an optimal tree
254
 
255
;    if (static_init_done) return;
256
 
257
	; For some embedded targets, global variables are not initialized:
258
;if NO_INIT_GLOBAL_POINTERS
259
;    static_l_desc.static_tree = static_ltree;
260
;    static_l_desc.extra_bits = extra_lbits;
261
;    static_d_desc.static_tree = static_dtree;
262
;    static_d_desc.extra_bits = extra_dbits;
263
;    static_bl_desc.extra_bits = extra_blbits;
264
;end if
265
 
266
	; Initialize the mapping length (0..255) -> length code (0..28)
267
;    length = 0;
268
;    for (code = 0; code < LENGTH_CODES-1; code++) {
269
;        base_length[code] = length;
270
;        for (n = 0; n < (1<
271
;            _length_code[length++] = (uch)code;
272
;        }
273
;    }
274
;    Assert (length == 256, "tr_static_init: length != 256");
275
	; Note that the length 255 (match length 258) can be represented
276
	; in two different ways: code 284 + 5 bits or code 285, so we
277
	; overwrite length_code[255] to use the best encoding:
278
 
279
;    _length_code[length-1] = (uch)code;
280
 
281
	; Initialize the mapping dist (0..32K) -> dist code (0..29)
282
;    dist = 0;
283
;    for (code = 0 ; code < 16; code++) {
284
;        base_dist[code] = dist;
285
;        for (n = 0; n < (1<
286
;            _dist_code[dist++] = (uch)code;
287
;        }
288
;    }
289
;    Assert (dist == 256, "tr_static_init: dist != 256");
290
;    dist >>= 7; /* from now on, all distances are divided by 128 */
291
;    for ( ; code < D_CODES; code++) {
292
;        base_dist[code] = dist << 7;
293
;        for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
294
;            _dist_code[256 + dist++] = (uch)code;
295
;        }
296
;    }
297
;    Assert (dist == 256, "tr_static_init: 256+dist != 512");
298
 
299
	; Construct the codes of the static literal tree
300
;    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
301
;    n = 0;
302
;    while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
303
;    while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
304
;    while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
305
;    while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
306
	; Codes 286 and 287 do not exist, but we must include them in the
307
	; tree construction to get a canonical Huffman tree (longest code
308
	; all ones)
309
 
310
;    gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
311
 
312
	; The static distance tree is trivial:
313
;    for (n = 0; n < D_CODES; n++) {
314
;        static_dtree[n].Len = 5;
315
;        static_dtree[n].Code = bi_reverse((unsigned)n, 5);
316
;    }
317
;    static_init_done = 1;
318
 
319
if GEN_TREES_H eq 1
320
	call gen_trees_header
321
end if
322
end if ;(GEN_TREES_H) | !(STDC)
323
	ret
324
endp
325
 
326
; ===========================================================================
327
; Genererate the file trees.h describing the static trees.
328
 
329
;#  define SEPARATOR(i, last, width) \
330
;      ((i) == (last)? "\n};\n\n" :    \
331
;       ((i) % (width) == (width)-1 ? ",\n" : ", "))
332
 
333
;void ()
334
align 4
335
proc gen_trees_header
336
;    FILE *header = fopen("trees.inc", "w");
337
;    int i;
338
 
339
;    Assert (header != NULL, "Can't open trees.inc");
340
;    fprintf(header,
341
;            "/* header created automatically with -DGEN_TREES_H */\n\n");
342
 
343
;    fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
344
;    for (i = 0; i < L_CODES+2; i++) {
345
;        fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
346
;                static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
347
;    }
348
 
349
;    fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
350
;    for (i = 0; i < D_CODES; i++) {
351
;        fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
352
;                static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
353
;    }
354
 
355
;    fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
356
;    for (i = 0; i < DIST_CODE_LEN; i++) {
357
;        fprintf(header, "%2u%s", _dist_code[i],
358
;                SEPARATOR(i, DIST_CODE_LEN-1, 20));
359
;    }
360
 
361
;    fprintf(header,
362
;        "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
363
;    for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
364
;        fprintf(header, "%2u%s", _length_code[i],
365
;                SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
366
;    }
367
 
368
;    fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
369
;    for (i = 0; i < LENGTH_CODES; i++) {
370
;        fprintf(header, "%1u%s", base_length[i],
371
;                SEPARATOR(i, LENGTH_CODES-1, 20));
372
;    }
373
 
374
;    fprintf(header, "local const int base_dist[D_CODES] = {\n");
375
;    for (i = 0; i < D_CODES; i++) {
376
;        fprintf(header, "%5u%s", base_dist[i],
377
;                SEPARATOR(i, D_CODES-1, 10));
378
;    }
379
 
380
;    fclose(header);
381
	ret
382
endp
383
 
384
; ===========================================================================
385
; Initialize the tree data structures for a new zlib stream.
386
 
6847 IgorA 387
;void (deflate_state* s)
6617 IgorA 388
align 4
389
proc _tr_init uses eax edi, s:dword
390
	mov edi,[s]
391
	call tr_static_init
392
 
393
	mov eax,edi
394
	add eax,deflate_state.dyn_ltree
395
	mov [edi+deflate_state.l_desc.dyn_tree],eax
396
	mov [edi+deflate_state.l_desc.stat_desc],static_l_desc
397
 
398
	add eax,deflate_state.dyn_dtree-deflate_state.dyn_ltree
399
	mov [edi+deflate_state.d_desc.dyn_tree],eax
400
	mov [edi+deflate_state.d_desc.stat_desc],static_d_desc
401
 
402
	add eax,deflate_state.bl_tree-deflate_state.dyn_dtree
403
	mov [edi+deflate_state.bl_desc.dyn_tree],eax
404
	mov [edi+deflate_state.bl_desc.stat_desc],static_bl_desc;
405
 
406
	mov word[edi+deflate_state.bi_buf],0
407
	mov dword[edi+deflate_state.bi_valid],0
408
if DEBUG eq 1
409
	mov dword[edi+deflate_state.compressed_len],0
410
	mov dword[edi+deflate_state.bits_sent],0
411
end if
412
 
413
	; Initialize the first block of the first file:
414
	stdcall init_block,edi
415
	ret
416
endp
417
 
418
; ===========================================================================
419
; Initialize a new block.
420
 
6847 IgorA 421
;void (deflate_state* s)
6617 IgorA 422
align 4
423
proc init_block uses eax ecx edi, s:dword
424
	mov edi,[s]
425
 
426
	; Initialize the trees.
427
	mov eax,edi
428
	add eax,deflate_state.dyn_ltree+Freq
429
	mov ecx,L_CODES
430
	@@:
431
		mov word[eax],0
432
		add eax,sizeof.ct_data
433
		loop @b
434
	mov eax,edi
435
	add eax,deflate_state.dyn_dtree+Freq
436
	mov ecx,D_CODES
437
	@@:
438
		mov word[eax],0
439
		add eax,sizeof.ct_data
440
		loop @b
441
	mov eax,edi
442
	add eax,deflate_state.bl_tree+Freq
443
	mov ecx,BL_CODES
444
	@@:
445
		mov word[eax],0
446
		add eax,sizeof.ct_data
447
		loop @b
448
 
6799 IgorA 449
	mov word[edi+sizeof.ct_data*END_BLOCK+deflate_state.dyn_ltree+Freq],1
6617 IgorA 450
	mov dword[edi+deflate_state.static_len],0
451
	mov dword[edi+deflate_state.opt_len],0
452
	mov dword[edi+deflate_state.matches],0
453
	mov dword[edi+deflate_state.last_lit],0
454
	ret
455
endp
456
 
457
SMALLEST equ 1
458
; Index within the heap array of least frequent node in the Huffman tree
459
 
460
 
461
; ===========================================================================
462
; Remove the smallest element from the heap and recreate the heap with
463
; one less element. Updates heap and heap_len.
464
 
465
macro pqremove s, tree, top
466
{
467
	mov eax,s
6847 IgorA 468
	add eax,deflate_state.heap+4*SMALLEST
6617 IgorA 469
	movzx top,word[eax]
470
push ebx
471
	mov ebx,[s+deflate_state.heap_len]
6847 IgorA 472
	mov ebx,[s+deflate_state.heap+4*ebx]
473
	mov [eax],ebx
6617 IgorA 474
	dec dword[s+deflate_state.heap_len]
475
pop ebx
476
	stdcall pqdownheap, s, tree, SMALLEST
477
}
478
 
479
; ===========================================================================
480
; Compares to subtrees, using the tree depth as tie breaker when
481
; the subtrees have equal frequency. This minimizes the worst case length.
482
 
483
macro smaller tree, n, m, depth, m_end
484
{
485
;if (..<.. || (..==.. && depth[n] <= depth[m]))
486
local .end0
6847 IgorA 487
	mov eax,n
6617 IgorA 488
	imul eax,sizeof.ct_data
489
	add eax,tree
490
	mov ax,word[eax+Freq]
6847 IgorA 491
	mov ebx,m
6617 IgorA 492
	imul ebx,sizeof.ct_data
493
	add ebx,tree
494
	mov bx,word[ebx+Freq]
495
	cmp ax,bx
496
	jl .end0
497
	jne m_end
6847 IgorA 498
	mov eax,n
6815 IgorA 499
	mov al,byte[eax+depth]
6847 IgorA 500
	mov ebx,m
6815 IgorA 501
	cmp al,byte[ebx+depth]
6617 IgorA 502
	jg m_end
503
	.end0:
504
}
505
 
506
; ===========================================================================
507
; Restore the heap property by moving down the tree starting at node k,
508
; exchanging a node with the smallest of its two sons if necessary, stopping
509
; when the heap property is re-established (each father smaller than its
510
; two sons).
511
 
512
;void (s, tree, k)
513
;    deflate_state* s
514
;    ct_data* tree ;the tree to restore
515
;    int      k    ;node to move down
516
align 4
517
proc pqdownheap, s:dword, tree:dword, k:dword
518
pushad
6815 IgorA 519
	;ecx - v dw
6617 IgorA 520
	mov edi,[s]
6815 IgorA 521
	mov esi,[k]
6847 IgorA 522
	mov ecx,[edi+deflate_state.heap+4*esi]
6617 IgorA 523
	shl esi,1
524
	;esi = j ;left son of k
525
	.cycle0: ;while (..<=..)
526
		cmp esi,[edi+deflate_state.heap_len]
527
		jg .cycle0end
528
		; Set j to the smallest of the two sons:
529
		;;cmp esi,[edi+deflate_state.heap_len]
530
		jge .end1 ;if (..<.. &&
531
		mov edx,esi
6847 IgorA 532
		shl edx,2
6617 IgorA 533
		add edx,edi
534
		add edx,deflate_state.heap
6847 IgorA 535
		smaller [tree], dword[edx+4], dword[edx], edi+deflate_state.depth, .end1
6617 IgorA 536
			inc esi
537
		.end1:
538
		; Exit if v is smaller than both sons
6847 IgorA 539
		mov edx,[edi+deflate_state.heap+4*esi]
540
		smaller [tree], ecx, edx, edi+deflate_state.depth, .end2
6617 IgorA 541
			jmp .cycle0end ;break
542
		.end2:
543
		; Exchange v with the smallest son
6815 IgorA 544
		;;mov dx,[edi+deflate_state.heap+2*esi]
6617 IgorA 545
		mov eax,[k]
6847 IgorA 546
		mov [edi+deflate_state.heap+4*eax],edx
6617 IgorA 547
		mov [k],esi
548
		; And continue down the tree, setting j to the left son of k
549
		shl esi,1
550
		jmp .cycle0
6815 IgorA 551
align 4
6617 IgorA 552
	.cycle0end:
553
	mov eax,[k]
6847 IgorA 554
	mov [edi+deflate_state.heap+4*eax],ecx
6617 IgorA 555
popad
556
	ret
557
endp
558
 
559
; ===========================================================================
560
; Compute the optimal bit lengths for a tree and update the total bit length
561
; for the current block.
562
; IN assertion: the fields freq and dad are set, heap[heap_max] and
563
;    above are the tree nodes sorted by increasing frequency.
564
; OUT assertions: the field len is set to the optimal bit length, the
565
;     array bl_count contains the frequencies for each bit length.
566
;     The length opt_len is updated; static_len is also updated if stree is
567
;     not null.
568
 
6854 IgorA 569
;void (deflate_state* s, tree_desc* desc)
570
align 16
6617 IgorA 571
proc gen_bitlen, s:dword, desc:dword
572
locals
573
	tree  dd ? ;ct_data* ;= desc.dyn_tree
574
	max_code dd ? ;int   ;= desc.max_code
575
	stree dd ? ;ct_data* ;= desc.stat_desc.static_tree
576
	extra dd ? ;intf*    ;= desc.stat_desc.extra_bits
577
	base  dd ? ;int      ;= desc.stat_desc.extra_base
578
	max_length dd ? ;int ;= desc.stat_desc.max_length
579
	h     dd ? ;int ;heap index
580
	m     dd ? ;int ;iterate over the tree elements
581
	bits  dd ? ;int ;bit length
582
	xbits dd ? ;int ;extra bits
583
	f     dw ? ;uint_16 ;frequency
584
	overflow dd 0 ;int ;number of elements with bit length too large
585
endl
586
pushad
587
	mov edi,[s]
588
	mov edx,[desc]
589
	mov eax,[edx+tree_desc.dyn_tree]
590
	mov [tree],eax
591
	mov eax,[edx+tree_desc.max_code]
592
	mov [max_code],eax
593
	mov ebx,[edx+tree_desc.stat_desc]
594
	mov eax,[ebx+static_tree_desc.static_tree]
595
	mov [stree],eax
596
	mov eax,[ebx+static_tree_desc.extra_bits]
597
	mov [extra],eax
598
	mov eax,[ebx+static_tree_desc.extra_base]
599
	mov [base],eax
600
	mov eax,[ebx+static_tree_desc.max_length]
601
	mov [max_length],eax
602
 
603
	xor ecx,ecx
604
	.cycle0:
605
	cmp ecx,MAX_BITS
606
	jg .cycle0end ;for (..;..<=..;..)
607
		mov word[edi+deflate_state.bl_count+2*ecx],0
608
		inc ecx
609
		jmp .cycle0
610
align 4
611
	.cycle0end:
612
 
613
	; In a first pass, compute the optimal bit lengths (which may
614
	; overflow in the case of the bit length tree).
615
 
616
	mov eax,[edi+deflate_state.heap_max]
6847 IgorA 617
	mov eax,[edi+deflate_state.heap+4*eax]
6617 IgorA 618
	imul eax,sizeof.ct_data
619
	add eax,[tree]
620
	mov word[eax+Len],0 ;root of the heap
621
 
622
	mov eax,[edi+deflate_state.heap_max]
623
	inc eax
624
	mov [h],eax
6854 IgorA 625
	jmp @f
626
align 4
6617 IgorA 627
	.cycle1:
6854 IgorA 628
	inc dword[h]
629
	@@:
6617 IgorA 630
	cmp dword[h],HEAP_SIZE
631
	jge .cycle1end ;for (..;..<..;..)
6854 IgorA 632
		mov    eax,[h]
633
		mov    ecx,[edi+4*eax+deflate_state.heap]
6617 IgorA 634
		;ecx = n
6854 IgorA 635
		mov edx,[tree]
636
		movzx eax,word[edx+sizeof.ct_data*ecx+Dad]
637
		movzx eax,word[edx+sizeof.ct_data*eax+Len]
6617 IgorA 638
		inc eax
639
		mov [bits],eax ;bits = tree[tree[n].Dad].Len + 1
640
		mov eax,[max_length]
641
		cmp [bits],eax
642
		jle @f ;if (..>..)
643
			mov [bits],eax
644
			inc dword[overflow]
645
		@@:
6854 IgorA 646
		mov eax,[bits]
647
		mov [edx+sizeof.ct_data*ecx+Len],ax
6617 IgorA 648
		; We overwrite tree[n].Dad which is no longer needed
649
 
650
		cmp ecx,[max_code]
6854 IgorA 651
		jg .cycle1 ;if (..>..) continue ;not a leaf node
6617 IgorA 652
 
6854 IgorA 653
		inc word[edi+2*eax+deflate_state.bl_count]
6617 IgorA 654
		mov dword[xbits],0
655
		cmp ecx,[base]
656
		jl @f ;if (..>=..)
657
			mov eax,ecx
658
			sub eax,[base]
659
			shl eax,2 ;*= sizeof.dd
660
			add eax,[extra]
661
			mov eax,[eax]
662
			mov [xbits],eax
663
		@@:
6854 IgorA 664
		movzx eax,word[edx+sizeof.ct_data*ecx+Freq]
6617 IgorA 665
		mov [f],ax
666
		mov esi,[bits]
667
		add esi,[xbits]
668
		imul eax,esi
669
		add [edi+deflate_state.opt_len],eax
670
		cmp dword[stree],0
671
		je @f ;if (..)
672
			movzx eax,word[f]
673
			mov esi,sizeof.ct_data
674
			imul esi,ecx
6855 IgorA 675
			add esi,[stree]
6617 IgorA 676
			movzx esi,word[esi+Len]
677
			add esi,[xbits]
678
			imul eax,esi
679
			add [edi+deflate_state.static_len],eax
680
		@@:
681
		jmp .cycle1
682
align 4
683
	.cycle1end:
684
	cmp dword[overflow],0
685
	je .end_f ;if (..==0) return
686
 
687
;    Trace((stderr,"\nbit length overflow\n"));
688
	; This happens for example on obj2 and pic of the Calgary corpus
689
 
690
	; Find the first bit length which could increase:
691
	.cycle2: ;do
692
		mov eax,[max_length]
693
		dec eax
694
		mov [bits],eax
695
		shl eax,1 ;*= sizeof.dw
696
		add eax,edi
697
		add eax,deflate_state.bl_count
698
		@@:
699
		cmp word[eax],0
700
		jne @f ;while (..==0) bits--
701
			dec dword[bits]
702
			sub eax,2
703
			jmp @b
6813 IgorA 704
align 4
6617 IgorA 705
		@@:
706
		dec word[eax]     ;move one leaf down the tree
707
		add word[eax+2],2 ;move one overflow item as its brother
708
		mov eax,[max_length]
709
		dec word[edi+deflate_state.bl_count+2*eax]
710
		; The brother of the overflow item also moves one step up,
711
		; but this does not affect bl_count[max_length]
712
 
713
		sub dword[overflow],2
714
		cmp dword[overflow],0
715
		jg .cycle2 ;while (..>0)
716
 
717
	; Now recompute all bit lengths, scanning in increasing frequency.
718
	; h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
719
	; lengths instead of fixing only the wrong ones. This idea is taken
720
	; from 'ar' written by Haruhiko Okumura.)
721
 
722
	mov eax,[max_length]
723
	mov [bits],eax
724
	.cycle3:
725
	cmp dword[bits],0
726
	je .end_f ;for (..;..!=0;..)
727
		mov eax,[bits]
6854 IgorA 728
		movzx ecx,word[edi+2*eax+deflate_state.bl_count]
6617 IgorA 729
		.cycle4: ;while (..!=0)
6854 IgorA 730
		test ecx,ecx
731
		jz .cycle4end
6617 IgorA 732
			dec dword[h]
733
			mov eax,[h]
6854 IgorA 734
			mov eax,[edi+4*eax+deflate_state.heap]
6617 IgorA 735
			mov [m],eax ;m = s.heap[--h]
736
			cmp eax,[max_code]
6813 IgorA 737
			jg .cycle4 ;if (..>..) continue
6617 IgorA 738
			mov esi,[m]
739
			mov eax,[bits]
6854 IgorA 740
			cmp word[edx+sizeof.ct_data*esi+Len],ax
6617 IgorA 741
			je @f ;if (..!=..)
742
;                Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
743
				movzx ebx,word[esi+Len]
744
				sub eax,ebx
745
				movzx ebx,word[esi+Freq]
746
				imul eax,ebx ;eax = (bits - tree[m].Len) * tree[m].Freq
747
				add [edi+deflate_state.opt_len],eax
748
				mov eax,[bits]
749
				mov word[esi+Len],ax
750
			@@:
751
			dec ecx
752
			jmp .cycle4
6813 IgorA 753
align 4
6617 IgorA 754
		.cycle4end:
755
		dec dword[bits]
756
		jmp .cycle3
6813 IgorA 757
align 4
6617 IgorA 758
.end_f:
759
popad
760
	ret
761
endp
762
 
763
; ===========================================================================
764
; Generate the codes for a given tree and bit counts (which need not be
765
; optimal).
766
; IN assertion: the array bl_count contains the bit length statistics for
767
; the given tree and the field len is set for all tree elements.
768
; OUT assertion: the field code is set for all tree elements of non
769
;     zero code length.
770
 
771
;void (tree, max_code, bl_count)
772
;    ct_data *tree     ;the tree to decorate
773
;    int max_code      ;largest code with non zero frequency
774
;    uint_16p bl_count ;number of codes at each bit length
775
align 4
776
proc gen_codes uses eax ebx ecx edx edi, tree:dword, max_code:dword, bl_count:dword
777
locals
778
	u_code dw 0 ;uint_16 ;running code value
779
	bits   dd 1 ;int ;bit index
780
	next_code rw MAX_BITS+1 ;uint_16[] ;next code value for each bit length
781
endl
782
	; The distribution counts are first used to generate the code values
783
	; without bit reversal.
784
	mov ebx,ebp
785
	sub ebx,2*(MAX_BITS+1)
786
 
787
	.cycle0: ;for (..;..<=..;..)
788
	cmp dword[bits],MAX_BITS
789
	jg .cycle0end
790
		mov eax,[bits]
791
		dec eax
792
		shl eax,1
793
		add eax,[bl_count]
794
		mov ax,word[eax]
795
		add ax,[u_code]
796
		shl ax,1 ;ax = (u_code + bl_count[bits-1]) << 1
797
		mov [u_code],ax
798
		mov ecx,[bits]
799
		mov word[ebx+2*ecx],ax ;next_code[bits] = u_code
800
		inc dword[bits]
801
		jmp .cycle0
802
	.cycle0end:
803
	; Check that the bit counts in bl_count are consistent. The last code
804
	; must be all ones.
805
 
806
	mov eax,[bl_count]
807
	mov ax,word[eax+2*MAX_BITS]
808
	add ax,[u_code]
809
	dec ax
810
	cmp ax,(1 shl MAX_BITS)-1
811
	je @f
6639 IgorA 812
		zlib_assert 'inconsistent bit counts' ;Assert(..==..)
6617 IgorA 813
	@@:
814
;    Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
815
 
816
	xor ecx,ecx ;n = 0
817
	.cycle1: ;for (..;..<=..;..)
818
	cmp ecx,[max_code]
819
	jg .cycle1end
820
		mov edx,sizeof.ct_data
821
		imul edx,ecx
822
		add edx,[tree] ;edx = &tree[n]
823
		movzx edi,word[edx+Len]
824
		cmp edi,0
825
		jne @f ;if (..==0) continue
826
			inc ecx
827
			jmp .cycle1
828
		@@:
829
		; Now reverse the bits
830
		movzx eax,word[ebx+2*edi]
831
		stdcall bi_reverse, eax, edi
832
		mov word[edx+Code],ax
833
		inc word[ebx+2*edi]
834
 
835
;        Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
836
;             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
837
		inc ecx
838
		jmp .cycle1
839
	.cycle1end:
840
	ret
841
endp
842
 
843
; ===========================================================================
844
; Construct one Huffman tree and assigns the code bit strings and lengths.
845
; Update the total bit length for the current block.
846
; IN assertion: the field freq is set for all tree elements.
847
; OUT assertions: the fields len and code are set to the optimal bit length
848
;     and corresponding code. The length opt_len is updated; static_len is
849
;     also updated if stree is not null. The field max_code is set.
850
 
851
;void (s, desc)
852
;    deflate_state* s
853
;    tree_desc *desc ;the tree descriptor
854
align 4
855
proc build_tree uses eax ebx ecx edx edi, s:dword, desc:dword
856
locals
857
	tree     dd  ? ;ct_data* ;= desc.dyn_tree
858
	stree    dd  ? ;ct_data* ;= desc.stat_desc.static_tree
859
	elems    dd  ? ;int      ;= desc.stat_desc.elems
860
	m        dd  ? ;int ;iterate over heap elements
861
	max_code dd -1 ;int ;largest code with non zero frequency
862
	node     dd  ? ;int ;new node being created
863
endl
864
	; Construct the initial heap, with least frequent element in
865
	; heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
866
	; heap[0] is not used.
867
	mov ebx,[desc]
868
	mov eax,[ebx+tree_desc.dyn_tree]
869
	mov [tree],eax
870
	mov ecx,[ebx+tree_desc.stat_desc]
871
	mov eax,[ecx+static_tree_desc.static_tree]
872
	mov [stree],eax
873
	mov ecx,[ecx+static_tree_desc.elems]
874
	mov [elems],ecx
875
	mov edi,[s]
6639 IgorA 876
	zlib_debug 'build_tree cycle0 ecx = %d',ecx
6617 IgorA 877
 
878
	mov dword[edi+deflate_state.heap_len],0
879
	mov dword[edi+deflate_state.heap_max],HEAP_SIZE
880
 
6813 IgorA 881
	mov edx,[tree]
882
	xor ecx,ecx
6617 IgorA 883
	.cycle0: ;for (..;..<..;..)
6813 IgorA 884
	cmp ecx,[elems]
6854 IgorA 885
	jge .cycle1
6813 IgorA 886
		cmp word[edx+Freq],0
6617 IgorA 887
		je @f ;if (..!=0)
888
			inc dword[edi+deflate_state.heap_len]
889
			mov eax,[edi+deflate_state.heap_len]
6813 IgorA 890
			mov [max_code],ecx
6847 IgorA 891
			mov dword[edi+deflate_state.heap+4*eax],ecx
6813 IgorA 892
			mov byte[edi+deflate_state.depth+ecx],0
6617 IgorA 893
			jmp .end0
894
align 4
895
		@@: ;else
6813 IgorA 896
			mov word[edx+Len],0
6617 IgorA 897
		.end0:
6813 IgorA 898
		add edx,sizeof.ct_data
899
		inc ecx
900
		jmp .cycle0
6617 IgorA 901
 
902
	; The pkzip format requires that at least one distance code exists,
903
	; and that at least one bit should be sent even if there is only one
904
	; possible code. So to avoid special checks later on we force at least
905
	; two codes of non zero frequency.
906
 
6854 IgorA 907
align 4
6617 IgorA 908
	.cycle1: ;while (..<..)
909
		cmp dword[edi+deflate_state.heap_len],2
910
		jge .cycle1end
911
		inc dword[edi+deflate_state.heap_len]
912
		xor eax,eax
913
		cmp dword[max_code],2
914
		jge @f
915
			inc dword[max_code]
916
			mov eax,[max_code]
917
		@@:
918
		mov ecx,[edi+deflate_state.heap_len]
6847 IgorA 919
		mov [edi+deflate_state.heap+4*ecx],eax
6617 IgorA 920
		mov [node],eax
921
		imul eax,sizeof.ct_data
922
		add eax,[tree]
923
		mov word[eax+Freq],1
924
		mov eax,[node]
925
		mov byte[edi+deflate_state.depth+eax],0
926
		dec dword[edi+deflate_state.opt_len]
927
		cmp dword[stree],0
928
		je .cycle1 ;if (..)
929
			mov eax,[node]
930
			imul eax,sizeof.ct_data
931
			add eax,[stree]
932
			movzx eax,word[eax+Len]
933
			sub [edi+deflate_state.static_len],eax
934
		; node is 0 or 1 so it does not have extra bits
935
		jmp .cycle1
936
align 4
937
	.cycle1end:
938
	mov eax,[max_code]
939
	mov [ebx+tree_desc.max_code],eax
940
 
941
	; The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
942
	; establish sub-heaps of increasing lengths:
943
 
944
	mov ecx,[edi+deflate_state.heap_len]
6854 IgorA 945
	sar ecx,1
6617 IgorA 946
	.cycle2: ;for (..;..>=..;..)
947
		cmp ecx,1
948
		jl .cycle2end
949
		stdcall pqdownheap, edi, [tree], ecx
950
		dec ecx
951
		jmp .cycle2
952
align 4
953
	.cycle2end:
954
 
955
	; Construct the Huffman tree by repeatedly combining the least two
956
	; frequent nodes.
957
 
958
	mov eax,[elems]
959
	mov [node],eax ;next internal node of the tree
960
	.cycle3: ;do
961
		pqremove edi, [tree], ecx ;n = node of least frequency
962
		movzx edx,word[eax]
963
		mov [m],edx ;m = node of next least frequency
964
 
965
		mov eax,[edi+deflate_state.heap_max]
966
		dec eax
6847 IgorA 967
		mov [edi+deflate_state.heap+4*eax],ecx ;keep the nodes sorted by frequency
6617 IgorA 968
		dec eax
969
		mov [edi+deflate_state.heap_max],eax
6847 IgorA 970
		mov [edi+deflate_state.heap+4*eax],edx
6617 IgorA 971
 
972
		; Create a new node father of n and m
973
		;;mov edx,[m]
974
		imul edx,sizeof.ct_data
975
		add edx,[tree]
976
		mov ax,word[edx+Freq]
977
		mov edx,ecx
978
		imul edx,sizeof.ct_data
979
		add edx,[tree]
980
		add ax,word[edx+Freq]
981
		mov edx,[node]
982
		imul edx,sizeof.ct_data
983
		add edx,[tree]
984
		mov word[edx+Freq],ax
985
 
986
		mov eax,ecx
987
		add eax,edi
6813 IgorA 988
		mov al,byte[eax+deflate_state.depth]
6617 IgorA 989
		mov edx,[m]
990
		add edx,edi
6813 IgorA 991
		mov ah,byte[edx+deflate_state.depth]
6617 IgorA 992
		cmp al,ah
6815 IgorA 993
		jge @f ;if (al>=ah) al=al : al=ah
6617 IgorA 994
			mov al,ah
995
		@@:
996
		inc al
997
		mov edx,[node]
998
		add edx,edi
6813 IgorA 999
		mov byte[edx+deflate_state.depth],al
6617 IgorA 1000
 
1001
		mov eax,[node]
1002
		mov edx,[m]
1003
		imul edx,sizeof.ct_data
1004
		add edx,[tree]
1005
		mov [edx+Dad],ax
1006
		mov edx,ecx
1007
		imul edx,sizeof.ct_data
1008
		add edx,[tree]
1009
		mov [edx+Dad],ax
1010
;if DUMP_BL_TREE eq 1
1011
;        if (tree == s->bl_tree) {
1012
;            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
1013
;                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
1014
;        }
1015
;end if
1016
		; and insert the new node in the heap
1017
		mov ecx,[node]
6847 IgorA 1018
		mov [edi+deflate_state.heap+4*SMALLEST],ecx
6617 IgorA 1019
		inc dword[node]
1020
		stdcall pqdownheap, edi, [tree], SMALLEST
1021
		cmp dword[edi+deflate_state.heap_len],2
1022
		jge .cycle3 ;while (..>=..)
1023
 
6847 IgorA 1024
	mov ecx,[edi+deflate_state.heap+4*SMALLEST]
6617 IgorA 1025
	dec dword[edi+deflate_state.heap_max]
1026
	mov eax,[edi+deflate_state.heap_max]
6847 IgorA 1027
	mov [edi+deflate_state.heap+4*eax],ecx
6617 IgorA 1028
 
1029
	; At this point, the fields freq and dad are set. We can now
1030
	; generate the bit lengths.
1031
 
1032
	stdcall gen_bitlen, edi, [desc]
1033
 
1034
	; The field len is now set, we can generate the bit codes
1035
	mov eax,edi
1036
	add eax,deflate_state.bl_count
1037
	stdcall gen_codes, [tree], [max_code], eax
1038
	ret
1039
endp
1040
 
1041
; ===========================================================================
1042
; Scan a literal or distance tree to determine the frequencies of the codes
1043
; in the bit length tree.
1044
 
1045
;void (s, tree, max_code)
1046
;    deflate_state* s
1047
;    ct_data *tree ;the tree to be scanned
1048
;    int max_code  ;and its largest code of non zero frequency
1049
align 4
1050
proc scan_tree uses eax ebx ecx edi, s:dword, tree:dword, max_code:dword
1051
locals
1052
	n dd ? ;int ;iterates over all tree elements
1053
	prevlen  dd -1 ;int ;last emitted length
1054
	curlen    dd ? ;int ;length of current code
1055
	nextlen   dd ? ;int ;= tree[0].Len ;length of next code
1056
	count     dd 0 ;int ;repeat count of the current code
1057
	max_count dd 7 ;int ;max repeat count
1058
	min_count dd 4 ;int ;min repeat count
1059
endl
1060
	mov edi,[s]
1061
	mov eax,[tree]
1062
	movzx eax,word[eax+Len]
1063
	mov [nextlen],eax
6815 IgorA 1064
	test eax,eax
1065
	jnz @f ;if (..==0)
6617 IgorA 1066
		mov dword[max_count],138
1067
		mov dword[min_count],3
1068
	@@:
1069
	mov eax,[max_code]
1070
	inc eax
1071
	imul eax,sizeof.ct_data
1072
	add eax,[tree]
1073
	mov word[eax+Len],0xffff ;guard
1074
 
1075
	xor ecx,ecx
6815 IgorA 1076
align 4
6617 IgorA 1077
	.cycle0:
1078
		cmp ecx,[max_code]
1079
		jg .cycle0end ;for (..;..<=..;..)
1080
		mov eax,[nextlen]
1081
		mov [curlen],eax
6815 IgorA 1082
		inc ecx
1083
		mov eax,sizeof.ct_data
1084
		imul eax,ecx
6617 IgorA 1085
		add eax,[tree]
1086
		movzx eax,word[eax+Len]
1087
		mov [nextlen],eax
1088
		inc dword[count]
1089
		mov ebx,[count]
1090
		cmp ebx,[max_count]
1091
		jge .end0
1092
		mov eax,[nextlen]
1093
		cmp [curlen],eax
6815 IgorA 1094
		je .cycle0 ;if (..<.. && ..==..) continue
1095
align 4
6617 IgorA 1096
		.end0:
1097
		cmp ebx,[min_count]
1098
		jge .end1 ;else if (..<..)
1099
			mov eax,[curlen]
1100
			imul eax,sizeof.ct_data
1101
			add eax,edi
6813 IgorA 1102
			add word[eax+deflate_state.bl_tree+Freq],bx
6617 IgorA 1103
			jmp .end4
6815 IgorA 1104
align 4
6617 IgorA 1105
		.end1:
1106
		cmp dword[curlen],0
1107
		je .end2 ;else if (..!=0)
1108
			mov eax,[curlen]
1109
			cmp eax,[prevlen]
1110
			je @f ;if (..!=..)
1111
				imul eax,sizeof.ct_data
1112
				add eax,edi
6813 IgorA 1113
				inc word[eax+deflate_state.bl_tree+Freq]
6617 IgorA 1114
			@@:
1115
			mov eax,REP_3_6
1116
			imul eax,sizeof.ct_data
1117
			add eax,edi
6813 IgorA 1118
			inc word[eax+deflate_state.bl_tree+Freq]
6617 IgorA 1119
			jmp .end4
6815 IgorA 1120
align 4
6617 IgorA 1121
		.end2:
1122
		cmp ebx,10
1123
		jg .end3 ;else if (..<=..)
1124
			mov eax,REPZ_3_10
1125
			imul eax,sizeof.ct_data
1126
			add eax,edi
6813 IgorA 1127
			inc word[eax+deflate_state.bl_tree+Freq]
6617 IgorA 1128
			jmp .end4
6815 IgorA 1129
align 4
6617 IgorA 1130
		.end3: ;else
1131
			mov eax,REPZ_11_138
1132
			imul eax,sizeof.ct_data
1133
			add eax,edi
6813 IgorA 1134
			inc word[eax+deflate_state.bl_tree+Freq]
6617 IgorA 1135
		.end4:
1136
		mov eax,[curlen]
1137
		mov [prevlen],eax
6855 IgorA 1138
		xor eax,eax
1139
		mov dword[count],eax
1140
		cmp dword[nextlen],eax
6617 IgorA 1141
		jne .end5 ;if (..==0)
1142
			mov dword[max_count],138
1143
			mov dword[min_count],3
6815 IgorA 1144
			jmp .cycle0
1145
align 4
6617 IgorA 1146
		.end5:
1147
		cmp eax,[nextlen]
1148
		jne .end6 ;else if (..==..)
1149
			mov dword[max_count],6
1150
			mov dword[min_count],3
6815 IgorA 1151
			jmp .cycle0
1152
align 4
6617 IgorA 1153
		.end6: ;else
1154
			mov dword[max_count],7
1155
			mov dword[min_count],4
1156
		jmp .cycle0
6815 IgorA 1157
align 4
6617 IgorA 1158
	.cycle0end:
1159
	ret
1160
endp
1161
 
1162
; ===========================================================================
1163
; Send a literal or distance tree in compressed form, using the codes in
1164
; bl_tree.
1165
 
1166
;void (s, tree, max_code)
1167
;    deflate_state* s
1168
;    ct_data *tree ;the tree to be scanned
1169
;    int max_code  ;and its largest code of non zero frequency
6855 IgorA 1170
align 16
6617 IgorA 1171
proc send_tree uses eax ebx ecx edi, s:dword, tree:dword, max_code:dword
1172
locals
1173
	n dd ? ;int ;iterates over all tree elements
1174
	prevlen  dd -1 ;int ;last emitted length
1175
	curlen    dd ? ;int ;length of current code
1176
	nextlen   dd ? ;int ;= tree[0].Len ;length of next code
1177
	count     dd 0 ;int ;repeat count of the current code
1178
	max_count dd 7 ;int ;max repeat count
1179
	min_count dd 4 ;int ;min repeat count
1180
endl
1181
	mov edi,[s]
1182
	; *** tree[max_code+1].Len = -1 ;guard already set
1183
	mov eax,[tree]
1184
	movzx eax,word[eax+Len]
1185
	mov [nextlen],eax
6813 IgorA 1186
	xor ecx,ecx
1187
	test eax,eax
1188
	jnz .cycle0 ;if (..==0)
6617 IgorA 1189
		mov dword[max_count],138
1190
		mov dword[min_count],3
6813 IgorA 1191
align 4
6617 IgorA 1192
	.cycle0: ;for (..;..<=..;..)
1193
	cmp ecx,[max_code]
1194
	jg .cycle0end
1195
		mov eax,[nextlen]
1196
		mov [curlen],eax
1197
		mov eax,ecx
1198
		inc eax
1199
		imul eax,sizeof.ct_data
1200
		add eax,[tree]
1201
		movzx eax,word[eax+Len]
1202
		mov [nextlen],eax
1203
		inc dword[count]
1204
		mov ebx,[count]
1205
		cmp ebx,[max_count]
1206
		jge .end0
1207
		mov eax,[nextlen]
1208
		cmp [curlen],eax
1209
		jne .end0 ;if (..<.. && ..==..)
1210
			inc ecx
1211
			jmp .cycle0 ;continue
6813 IgorA 1212
align 4
6617 IgorA 1213
		.end0:
1214
		cmp ebx,[min_count]
1215
		jge .end1 ;else if (..<..)
1216
			@@: ;do
1217
				mov ebx,edi
1218
				add ebx,deflate_state.bl_tree
1219
				send_code edi, [curlen], ebx
1220
				dec dword[count]
6813 IgorA 1221
				jnz @b ;while (..!=0)
6617 IgorA 1222
			jmp .end4
1223
align 4
1224
		.end1:
1225
		cmp dword[curlen],0
1226
		je .end2 ;else if (..!=0)
1227
			mov eax,[curlen]
1228
			cmp eax,[prevlen]
1229
			je @f ;if (..!=..)
1230
				mov ebx,edi
1231
				add ebx,deflate_state.bl_tree
1232
				send_code edi, eax, ebx
1233
				dec dword[count]
1234
			@@:
1235
			cmp dword[count],3
1236
			jl @f
1237
			cmp dword[count],6
1238
			jle .end8
1239
			@@:
6639 IgorA 1240
				zlib_assert ' 3_6?' ;Assert(..>=.. && ..<=..)
6617 IgorA 1241
			.end8:
1242
			mov ebx,edi
1243
			add ebx,deflate_state.bl_tree
1244
			send_code edi, REP_3_6, ebx
1245
			mov ebx,[count]
1246
			sub ebx,3
1247
			stdcall send_bits, edi, ebx, 2
1248
			jmp .end4
1249
		.end2:
1250
		cmp ebx,10
1251
		jg .end3 ;else if (..<=..)
1252
			mov ebx,edi
1253
			add ebx,deflate_state.bl_tree
1254
			send_code edi, REPZ_3_10, ebx
1255
			mov ebx,[count]
1256
			sub ebx,3
1257
			stdcall send_bits, edi, ebx, 3
1258
			jmp .end4
1259
		.end3: ;else
1260
			mov ebx,edi
1261
			add ebx,deflate_state.bl_tree
1262
			send_code edi, REPZ_11_138, ebx
1263
			mov ebx,[count]
1264
			sub ebx,11
1265
			stdcall send_bits, edi, ebx, 7
1266
		.end4:
1267
		mov eax,[curlen]
1268
		mov [prevlen],eax
6855 IgorA 1269
		xor eax,eax
1270
		mov dword[count],eax
1271
		cmp [nextlen],eax
6617 IgorA 1272
		jne .end5 ;if (..==0)
1273
			mov dword[max_count],138
1274
			mov dword[min_count],3
1275
			jmp .end7
1276
		.end5:
1277
		mov eax,[curlen]
1278
		cmp eax,[nextlen]
1279
		jne .end6 ;else if (..==..)
1280
			mov dword[max_count],6
1281
			mov dword[min_count],3
1282
			jmp .end7
1283
		.end6: ;else
1284
			mov dword[max_count],7
1285
			mov dword[min_count],4
1286
		.end7:
1287
		inc ecx
1288
		jmp .cycle0
1289
align 4
1290
	.cycle0end:
1291
	ret
1292
endp
1293
 
1294
; ===========================================================================
1295
; Construct the Huffman tree for the bit lengths and return the index in
1296
; bl_order of the last bit length code to send.
1297
 
6847 IgorA 1298
;int (deflate_state* s)
6617 IgorA 1299
align 4
1300
proc build_bl_tree uses edi, s:dword
1301
locals
1302
	max_blindex dd ? ;int ;index of last bit length code of non zero freq
1303
endl
1304
	mov edi,[s]
1305
	; Determine the bit length frequencies for literal and distance trees
1306
	mov eax,edi
1307
	add eax,deflate_state.dyn_ltree
1308
	stdcall scan_tree, edi, eax, [edi+deflate_state.l_desc.max_code]
6799 IgorA 1309
	add eax,deflate_state.dyn_dtree-deflate_state.dyn_ltree
6617 IgorA 1310
	stdcall scan_tree, edi, eax, [edi+deflate_state.d_desc.max_code]
1311
 
1312
	; Build the bit length tree:
6799 IgorA 1313
	add eax,deflate_state.bl_desc-deflate_state.dyn_dtree
6617 IgorA 1314
	stdcall build_tree, edi, eax
1315
	; opt_len now includes the length of the tree representations, except
1316
	; the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
1317
 
1318
	; Determine the number of bit length codes to send. The pkzip format
1319
	; requires that at least 4 bit length codes be sent. (appnote.txt says
1320
	; 3 but the actual value used is 4.)
1321
 
1322
	mov dword[max_blindex],BL_CODES-1
1323
	.cycle0: ;for (..;..>=..;..)
1324
		cmp dword[max_blindex],3
1325
		jl .cycle0end
1326
		dec dword[max_blindex]
1327
		mov eax,[max_blindex]
1328
		add eax,bl_order
1329
		movzx eax,byte[eax]
1330
		imul eax,sizeof.ct_data
1331
		add eax,edi
6813 IgorA 1332
		cmp word[eax+deflate_state.bl_tree+Len],0
6617 IgorA 1333
		jne .cycle0end ;if (..!=0) break
1334
		jmp .cycle0
6813 IgorA 1335
align 4
6617 IgorA 1336
	.cycle0end:
1337
	; Update opt_len to include the bit length tree and counts
1338
	mov eax,[max_blindex]
1339
	inc eax
1340
	imul eax,3
1341
	add eax,5+5+4
1342
	add [edi+deflate_state.opt_len],eax
1343
;    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", s->opt_len, s->static_len));
1344
 
1345
	mov eax,[max_blindex]
1346
	ret
1347
endp
1348
 
1349
; ===========================================================================
1350
; Send the header for a block using dynamic Huffman trees: the counts, the
1351
; lengths of the bit length codes, the literal tree and the distance tree.
1352
; IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
1353
 
1354
;void (s, lcodes, dcodes, blcodes)
1355
;    deflate_state* s
1356
;    int lcodes, dcodes, blcodes ;number of codes for each tree
1357
align 4
1358
proc send_all_trees uses eax ebx ecx edi, s:dword, lcodes:dword, dcodes:dword, blcodes:dword
1359
;ecx = index in bl_order
1360
	cmp dword[lcodes],257
1361
	jl @f
1362
	cmp dword[dcodes],1
1363
	jl @f
1364
	cmp dword[blcodes],4
1365
	jge .end0
1366
	@@:
6639 IgorA 1367
		zlib_assert 'not enough codes' ;Assert(..>=.. && ..>=.. && ..>=..)
6617 IgorA 1368
	.end0:
1369
	cmp dword[lcodes],L_CODES
1370
	jg @f
1371
	cmp dword[dcodes],D_CODES
1372
	jg @f
1373
	cmp dword[blcodes],BL_CODES
1374
	jle .end1
1375
	@@:
6639 IgorA 1376
		zlib_assert 'too many codes' ;Assert(..<=.. && ..<=.. && ..<=..)
6617 IgorA 1377
	.end1:
1378
;    Tracev((stderr, "\nbl counts: "));
1379
	mov edi,[s]
1380
	mov eax,[lcodes]
1381
	sub eax,257
1382
	stdcall send_bits, edi, eax, 5 ;not +255 as stated in appnote.txt
1383
	mov eax,[dcodes]
1384
	dec eax
1385
	stdcall send_bits, edi, eax, 5
1386
	mov eax,[blcodes]
1387
	sub eax,4
1388
	stdcall send_bits, edi, eax, 4 ;not -3 as stated in appnote.txt
1389
	xor ecx,ecx
1390
	.cycle0:
1391
		cmp ecx,[blcodes]
1392
		jge .cycle0end ;for (..;..<..;..)
1393
;        Tracev((stderr, "\nbl code %2d ", bl_order[ecx]));
6851 IgorA 1394
		movzx eax,byte[ecx+bl_order]
1395
		movzx eax,word[edi+sizeof.ct_data*eax+deflate_state.bl_tree+Len]
6813 IgorA 1396
		stdcall send_bits, edi, eax, 3
6617 IgorA 1397
		inc ecx
1398
		jmp .cycle0
1399
align 4
1400
	.cycle0end:
1401
;    Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
1402
 
1403
	mov ebx,[lcodes]
1404
	dec ebx
1405
	mov eax,edi
1406
	add eax,deflate_state.dyn_ltree
1407
	stdcall send_tree, edi, eax, ebx ;literal tree
1408
;    Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
1409
 
1410
	mov ebx,[dcodes]
1411
	dec ebx
1412
	add eax,deflate_state.dyn_dtree-deflate_state.dyn_ltree
1413
	stdcall send_tree, edi, eax, ebx ;distance tree
1414
;    Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
1415
	ret
1416
endp
1417
 
1418
; ===========================================================================
1419
; Send a stored block
1420
 
1421
;void (s, buf, stored_len, last)
1422
;    deflate_state* s
1423
;    charf *buf     ;input block
1424
;    ulg stored_len ;length of input block
1425
;    int last       ;one if this is the last block for a file
1426
align 4
1427
proc _tr_stored_block uses eax edi, s:dword, buf:dword, stored_len:dword, last:dword
1428
	mov edi,[s]
1429
	mov eax,[last]
1430
	add eax,STORED_BLOCK shl 1
1431
	stdcall send_bits, edi, eax, 3 ;send block type
1432
if DEBUG eq 1
1433
	mov eax,[edi+deflate_state.compressed_len]
1434
	add eax,3+7
1435
	and eax,not 7
1436
	mov [edi+deflate_state.compressed_len],eax
1437
	mov eax,[stored_len]
1438
	add eax,4
1439
	shl eax,3
1440
	add [edi+deflate_state.compressed_len],eax
1441
end if
1442
	stdcall copy_block, edi, [buf], [stored_len], 1 ;with header
1443
	ret
1444
endp
1445
 
1446
; ===========================================================================
1447
; Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
1448
 
6847 IgorA 1449
;void (deflate_state* s)
1450
;align 4
1451
;proc _tr_flush_bits, s:dword
1452
;	stdcall bi_flush, [s]
1453
;	ret
1454
;endp
6617 IgorA 1455
 
6847 IgorA 1456
_tr_flush_bits equ bi_flush
1457
 
6617 IgorA 1458
; ===========================================================================
1459
; Send one empty static block to give enough lookahead for inflate.
1460
; This takes 10 bits, of which 7 may remain in the bit buffer.
1461
 
6847 IgorA 1462
;void (deflate_state* s)
6617 IgorA 1463
align 4
1464
proc _tr_align uses edi, s:dword
1465
	mov edi,[s]
1466
	stdcall send_bits, edi, STATIC_TREES shl 1, 3
1467
	send_code edi, END_BLOCK, static_ltree
1468
if DEBUG eq 1
1469
	add [edi+deflate_state.compressed_len],10 ;3 for block type, 7 for EOB
1470
end if
1471
	stdcall bi_flush, edi
1472
	ret
1473
endp
1474
 
1475
; ===========================================================================
1476
; Determine the best encoding for the current block: dynamic trees, static
1477
; trees or store, and output the encoded block to the zip file.
1478
 
1479
;void (s, buf, stored_len, last)
1480
;    deflate_state* s
1481
;    charf *buf     ;input block, or NULL if too old
1482
;    ulg stored_len ;length of input block
1483
;    int last       ;one if this is the last block for a file
1484
align 4
1485
proc _tr_flush_block uses eax ebx edi, s:dword, buf:dword, stored_len:dword, last:dword
1486
locals
1487
	opt_lenb dd ? ;ulg
1488
	static_lenb dd ? ;opt_len and static_len in bytes
1489
	max_blindex dd 0 ;int ;index of last bit length code of non zero freq
1490
endl
1491
	; Build the Huffman trees unless a stored block is forced
1492
	mov edi,[s]
1493
	cmp word[edi+deflate_state.level],0
1494
	jle .end0 ;if (..>0)
1495
 
1496
		; Check if the file is binary or text
1497
		mov ebx,[edi+deflate_state.strm]
6797 IgorA 1498
		cmp dword[ebx+z_stream.data_type],Z_UNKNOWN
6617 IgorA 1499
		jne @f ;if (..==..)
1500
			stdcall detect_data_type, edi
6797 IgorA 1501
			mov [ebx+z_stream.data_type],eax
6617 IgorA 1502
		@@:
1503
 
1504
		; Construct the literal and distance trees
1505
		mov eax,edi
1506
		add eax,deflate_state.l_desc
1507
		stdcall build_tree, edi, eax
1508
;        Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, s->static_len));
1509
 
1510
		mov eax,edi
1511
		add eax,deflate_state.d_desc
1512
		stdcall build_tree, edi, eax
1513
;        Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, s->static_len));
1514
		; At this point, opt_len and static_len are the total bit lengths of
1515
		; the compressed block data, excluding the tree representations.
1516
 
1517
		; Build the bit length tree for the above two trees, and get the index
1518
		; in bl_order of the last bit length code to send.
1519
 
1520
		stdcall build_bl_tree, edi
1521
		mov [max_blindex],eax
1522
 
1523
		; Determine the best encoding. Compute the block lengths in bytes.
1524
		mov eax,[edi+deflate_state.opt_len]
1525
		add eax,3+7
1526
		shr eax,3
1527
		mov [opt_lenb],eax
1528
		mov eax,[edi+deflate_state.static_len]
1529
		add eax,3+7
1530
		shr eax,3
1531
		mov [static_lenb],eax
1532
 
1533
;        Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
1534
;                opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
1535
;                s->last_lit));
1536
 
1537
		cmp eax,[opt_lenb]
6851 IgorA 1538
		ja .end1 ;if (..<=..)
6617 IgorA 1539
			mov [opt_lenb],eax
1540
		jmp .end1
1541
	.end0: ;else
1542
		cmp dword[buf],0
1543
		jne @f
6639 IgorA 1544
			zlib_assert 'lost buf' ;Assert(..!=0)
6617 IgorA 1545
		@@:
1546
		mov eax,[stored_len]
1547
		add eax,5
1548
		mov [static_lenb],eax
1549
		mov [opt_lenb],eax ;force a stored block
1550
	.end1:
1551
 
1552
if FORCE_STORED eq 1
1553
	cmp dword[buf],0
1554
	je .end2 ;if (..!=0) ;force stored block
1555
else
1556
	mov eax,[stored_len]
1557
	add eax,4
1558
	cmp eax,[opt_lenb]
6851 IgorA 1559
	ja .end2
6617 IgorA 1560
	cmp dword[buf],0
1561
	je .end2 ;if (..<=.. && ..!=0)
1562
		;4: two words for the lengths
1563
end if
1564
		; The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
1565
		; Otherwise we can't have processed more than WSIZE input bytes since
1566
		; the last block flush, because compression would have been
1567
		; successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
1568
		; transform a block into a stored block.
1569
 
1570
		stdcall _tr_stored_block, edi, [buf], [stored_len], [last]
1571
		jmp .end4
1572
	.end2:
1573
if FORCE_STATIC eq 1
1574
	cmp dword[static_lenb],0
1575
	jl .end3 ;else if (..>=0) ;force static trees
1576
else
1577
	cmp word[edi+deflate_state.strategy],Z_FIXED
1578
	je @f
1579
	mov eax,[opt_lenb]
1580
	cmp [static_lenb],eax
1581
	je @f ;else if (..==.. || ..==..)
1582
		jmp .end3
1583
	@@:
1584
end if
1585
		mov eax,STATIC_TREES shl 1
1586
		add eax,[last]
1587
		stdcall send_bits, edi, eax, 3
1588
		stdcall compress_block, edi, static_ltree, static_dtree
1589
if DEBUG eq 1
1590
		mov eax,[edi+deflate_state.static_len]
1591
		add eax,3
1592
		add [edi+deflate_state.compressed_len],eax
1593
end if
1594
		jmp .end4
1595
	.end3: ;else
1596
		mov eax,DYN_TREES shl 1
1597
		add eax,[last]
1598
		stdcall send_bits, edi, eax, 3
1599
		mov eax,[max_blindex]
1600
		inc eax
1601
		push eax
1602
		mov eax,[edi+deflate_state.d_desc.max_code]
1603
		inc eax
1604
		push eax
1605
		mov eax,[edi+deflate_state.l_desc.max_code]
1606
		inc eax
1607
		stdcall send_all_trees, edi, eax ;, ..., ...
1608
		mov eax,edi
1609
		add eax,deflate_state.dyn_dtree
1610
		push eax
1611
		add eax,deflate_state.dyn_ltree-deflate_state.dyn_dtree
1612
		stdcall compress_block, edi, eax ;, ...
1613
if DEBUG eq 1
1614
		mov eax,[edi+deflate_state.opt_len]
1615
		add eax,3
1616
		add [edi+deflate_state.compressed_len],eax
1617
end if
1618
	.end4:
1619
;    Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1620
	; The above check is made mod 2^32, for files larger than 512 MB
1621
	; and uLong implemented on 32 bits.
1622
 
1623
	stdcall init_block,edi
1624
 
1625
	cmp dword[last],0
1626
	je @f ;if (..)
1627
		stdcall bi_windup,edi
1628
if DEBUG eq 1
1629
		add [edi+deflate_state.compressed_len],7 ;align on byte boundary
1630
end if
1631
	@@:
1632
;    Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1633
;           s->compressed_len-7*last));
1634
	ret
1635
endp
1636
 
1637
; ===========================================================================
1638
; Save the match info and tally the frequency counts. Return true if
1639
; the current block must be flushed.
1640
 
1641
;int (s, dist, lc)
1642
;    deflate_state* s
1643
;    unsigned dist ;distance of matched string
1644
;    unsigned lc   ;match length-MIN_MATCH or unmatched char (if dist==0)
1645
align 4
1646
proc _tr_tally uses ebx edi, s:dword, dist:dword, lc:dword
1647
	mov edi,[s]
1648
	mov eax,[edi+deflate_state.last_lit]
1649
	shl eax,1
1650
	add eax,[edi+deflate_state.d_buf]
1651
	mov ebx,[dist]
1652
	mov word[eax],bx
1653
	mov eax,[edi+deflate_state.last_lit]
1654
	add eax,[edi+deflate_state.l_buf]
1655
	mov ebx,[lc]
1656
	mov byte[eax],bl
1657
	inc dword[edi+deflate_state.last_lit]
1658
	cmp dword[dist],0
1659
	jne @f ;if (..==0)
1660
		; lc is the unmatched char
1661
		mov eax,[lc]
6851 IgorA 1662
		inc word[edi+sizeof.ct_data*eax+deflate_state.dyn_ltree+Freq]
6617 IgorA 1663
		jmp .end0
6815 IgorA 1664
align 4
6617 IgorA 1665
	@@: ;else
1666
		inc dword[edi+deflate_state.matches]
1667
		; Here, lc is the match length - MIN_MATCH
1668
		dec dword[dist] ;dist = match distance - 1
1669
		MAX_DIST edi
1670
		cmp word[dist],ax
1671
		jge @f
1672
		cmp word[lc],MAX_MATCH-MIN_MATCH
1673
		jg @f
1674
		d_code [dist]
1675
		cmp ax,D_CODES
1676
		jl .end2
1677
		@@:
6639 IgorA 1678
			zlib_assert '_tr_tally: bad match' ;Assert(..<.. && ..<=.. && ..<..)
6617 IgorA 1679
		.end2:
1680
		mov eax,[lc]
6815 IgorA 1681
		movzx eax,byte[eax+_length_code]
6851 IgorA 1682
		inc word[edi+sizeof.ct_data*eax+deflate_state.dyn_ltree+sizeof.ct_data*(LITERALS+1)+Freq]
6617 IgorA 1683
		d_code [dist]
6851 IgorA 1684
		inc word[edi+sizeof.ct_data*eax+deflate_state.dyn_dtree+Freq]
6617 IgorA 1685
	.end0:
1686
 
1687
if TRUNCATE_BLOCK eq 1
1688
	; Try to guess if it is profitable to stop the current block here
1689
	mov eax,[edi+deflate_state.last_lit]
1690
	and eax,0x1fff
6847 IgorA 1691
	jnz .end1
6617 IgorA 1692
	cmp word[edi+deflate_state.level],2
6847 IgorA 1693
	jle .end1 ;if (..==0 && ..>..)
6617 IgorA 1694
	; Compute an upper bound for the compressed length
1695
;        ulg out_length = (ulg)s->last_lit*8L;
1696
;        ulg in_length = (ulg)((long)s->strstart - s->block_start);
1697
;        int dcode;
1698
;        for (dcode = 0; dcode < D_CODES; dcode++) {
1699
;            out_length += (ulg)s->dyn_dtree[dcode].Freq *
1700
;                (5L+extra_dbits[dcode]);
1701
;        }
1702
;        out_length >>= 3;
1703
;        Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1704
;               s->last_lit, in_length, out_length,
1705
;               100L - out_length*100L/in_length));
1706
;        if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1707
	.end1:
1708
end if
6851 IgorA 1709
	mov ebx,[edi+deflate_state.lit_bufsize]
1710
	dec ebx
6617 IgorA 1711
	xor eax,eax
6851 IgorA 1712
	cmp [edi+deflate_state.last_lit],ebx
1713
	sete al ;return (..==..)
1714
 
6617 IgorA 1715
	; We avoid equality with lit_bufsize because of wraparound at 64K
1716
	; on 16 bit machines and because stored blocks are restricted to
1717
	; 64K-1 bytes.
1718
	ret
1719
endp
1720
 
1721
; ===========================================================================
1722
; Send the block data compressed using the given Huffman trees
1723
 
1724
;void (s, ltree, dtree)
6799 IgorA 1725
;    deflate_state* s
6617 IgorA 1726
;    ct_data *ltree ;literal tree
1727
;    ct_data *dtree ;distance tree
1728
align 4
1729
proc compress_block uses eax edi, s:dword, ltree:dword, dtree:dword
1730
locals
1731
	dist  dd ? ;unsigned ;distance of matched string
1732
	lc    dd ? ;int      ;match length or unmatched char (if dist == 0)
1733
	lx    dd 0 ;unsigned ;running index in l_buf
1734
	u_code dd ? ;unsigned ;the code to send
1735
endl
1736
	mov edi,[s]
1737
	cmp dword[edi+deflate_state.last_lit],0
1738
	je .end0 ;if (..!=0)
1739
	.cycle0: ; do
1740
		mov eax,[lx]
1741
		shl eax,1
1742
		add eax,[edi+deflate_state.d_buf]
1743
		movzx eax,word[eax]
1744
		mov [dist],eax
1745
		mov eax,[lx]
1746
		add eax,[edi+deflate_state.l_buf]
1747
		movzx eax,byte[eax]
1748
		mov [lc],eax
1749
		inc dword[lx]
1750
		cmp dword[dist],0
1751
		jne @f ;if (..==0)
1752
			send_code edi, [lc], [ltree] ;send a literal byte
1753
;            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1754
			jmp .end1
1755
		@@: ;else
1756
			; Here, lc is the match length - MIN_MATCH
1757
			mov eax,[lc]
1758
			add eax,_length_code
1759
			movzx eax,byte[eax]
1760
			mov [u_code],eax
1761
			add eax,LITERALS+1
1762
			send_code edi, eax, [ltree] ;send the length code
1763
			mov eax,[u_code]
6847 IgorA 1764
			mov eax,[4*eax+extra_lbits]
1765
			test eax,eax
1766
			jz @f ;if (..!=0)
1767
				push eax ;extra
6617 IgorA 1768
				mov eax,[u_code]
6847 IgorA 1769
				mov eax,[4*eax+base_length]
6617 IgorA 1770
				sub [lc],eax
6847 IgorA 1771
				stdcall send_bits, edi, [lc] ;, ... ;send the extra length bits
6617 IgorA 1772
			@@:
1773
			dec dword[dist] ;dist is now the match distance - 1
1774
			d_code [dist]
1775
			mov [u_code],eax
1776
			cmp eax,D_CODES
1777
			jl @f
6639 IgorA 1778
				zlib_assert 'bad d_code' ;Assert(..<..)
6617 IgorA 1779
			@@:
1780
			send_code edi, [u_code], [dtree] ;send the distance code
1781
			mov eax,[u_code]
6847 IgorA 1782
			mov eax,[4*eax+extra_dbits]
1783
			test eax,eax
1784
			jz .end1 ;if (..!=0)
1785
				push eax ;extra
6617 IgorA 1786
				mov eax,[u_code]
6847 IgorA 1787
				mov eax,[4*eax+base_dist]
6617 IgorA 1788
				sub [dist],eax
6847 IgorA 1789
				stdcall send_bits, edi, [dist] ;, ... ;send the extra distance bits
6617 IgorA 1790
		.end1: ;literal or match pair ?
1791
 
1792
		; Check that the overlay between pending_buf and d_buf+l_buf is ok:
1793
		mov eax,[lx]
1794
		shl eax,1
1795
		add eax,[edi+deflate_state.lit_bufsize]
6741 IgorA 1796
		cmp [edi+deflate_state.pending],eax
6617 IgorA 1797
		jl @f
6639 IgorA 1798
			zlib_assert 'pendingBuf overflow' ;Assert(..<..)
6617 IgorA 1799
		@@:
1800
		mov eax,[edi+deflate_state.last_lit]
1801
		cmp [lx],eax
6851 IgorA 1802
		jb .cycle0 ;while (..<..)
6617 IgorA 1803
align 4
1804
	.end0:
1805
 
1806
	send_code edi, END_BLOCK, [ltree]
1807
	ret
1808
endp
1809
 
1810
; ===========================================================================
1811
; Check if the data type is TEXT or BINARY, using the following algorithm:
1812
; - TEXT if the two conditions below are satisfied:
1813
;    a) There are no non-portable control characters belonging to the
1814
;       "black list" (0..6, 14..25, 28..31).
1815
;    b) There is at least one printable character belonging to the
1816
;       "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255).
1817
; - BINARY otherwise.
1818
; - The following partially-portable control characters form a
1819
;   "gray list" that is ignored in this detection algorithm:
1820
;   (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}).
1821
; IN assertion: the fields Freq of dyn_ltree are set.
1822
 
6851 IgorA 1823
;int (deflate_state* s)
6617 IgorA 1824
align 4
1825
proc detect_data_type uses ebx ecx edi, s:dword
1826
	; black_mask is the bit mask of black-listed bytes
1827
	; set bits 0..6, 14..25, and 28..31
1828
	; 0xf3ffc07f = binary 11110011111111111100000001111111
1829
locals
1830
	black_mask dd 0xf3ffc07f
1831
endl
1832
	mov edi,[s]
1833
 
1834
	; Check for non-textual ("black-listed") bytes.
1835
	xor ecx,ecx
1836
	mov ebx,edi
1837
	add ebx,deflate_state.dyn_ltree+Freq
1838
	.cycle0:
1839
	cmp ecx,31
1840
	jg .cycle0end ;for (..;..<=..;..,..)
1841
		bt dword[black_mask],0
1842
		jnc @f
1843
		cmp word[ebx],0
1844
		je @f ;if (..&.. && ..!=0)
1845
			mov eax,Z_BINARY
1846
			jmp .end_f
1847
		@@:
1848
		shr dword[black_mask],1
1849
		add ebx,sizeof.ct_data
1850
		inc ecx
1851
		jmp .cycle0
1852
	.cycle0end:
1853
 
1854
	; Check for textual ("white-listed") bytes.
1855
	mov ebx,edi
1856
	add ebx,deflate_state.dyn_ltree+Freq+9*sizeof.ct_data
1857
	cmp word[ebx],0
1858
	jne @f
1859
	add ebx,sizeof.ct_data
1860
	cmp word[ebx],0
1861
	jne @f
1862
	add ebx,3*sizeof.ct_data
1863
	cmp word[ebx],0
1864
	je .end0
1865
	@@: ;if (..!=0 || ..!=0 || ..!= 0)
1866
		mov eax,Z_TEXT
1867
		jmp .end_f
1868
	.end0:
1869
	mov ecx,32
1870
	mov ebx,edi
6799 IgorA 1871
	add ebx,deflate_state.dyn_ltree+Freq+32*sizeof.ct_data
6617 IgorA 1872
	.cycle1:
1873
	cmp ecx,LITERALS
1874
	jge .cycle1end ;for (..;..<..;..,..)
1875
		cmp word[ebx],0
1876
		je @f ;if (..!=0)
1877
			mov eax,Z_TEXT
1878
			jmp .end_f
1879
		@@:
1880
		add ebx,sizeof.ct_data
1881
		inc ecx
1882
		jmp .cycle1
1883
	.cycle1end:
1884
 
1885
	; There are no "black-listed" or "white-listed" bytes:
1886
	; this stream either is empty or has tolerated ("gray-listed") bytes only.
1887
 
1888
	mov eax,Z_BINARY
1889
.end_f:
1890
	ret
1891
endp
1892
 
1893
; ===========================================================================
1894
; Reverse the first len bits of a code, using straightforward code (a faster
1895
; method would use a table)
1896
; IN assertion: 1 <= len <= 15
1897
 
1898
;unsigned (code, len)
1899
;    unsigned code ;the value to invert
1900
;    int len       ;its bit length
1901
align 4
1902
proc bi_reverse uses ebx, p1code:dword, len:dword
1903
	xor eax,eax
1904
	@@: ;do
1905
		mov ebx,[p1code]
1906
		and ebx,1
1907
		or eax,ebx
1908
		shr dword[p1code],1
1909
		shl eax,1
1910
		dec dword[len]
1911
		cmp dword[len],0
1912
		jg @b ;while (..>..)
6813 IgorA 1913
	shr eax,1
6617 IgorA 1914
	ret
1915
endp
1916
 
1917
; ===========================================================================
1918
; Flush the bit buffer, keeping at most 7 bits in it.
1919
 
6851 IgorA 1920
;void (deflate_state* s)
6617 IgorA 1921
align 4
1922
proc bi_flush uses eax ecx edi, s:dword
1923
	mov edi,[s]
1924
	cmp dword[edi+deflate_state.bi_valid],16
1925
	jne @f ;if (..==..)
1926
		mov cx,[edi+deflate_state.bi_buf]
1927
		put_short edi,cx
1928
		mov word[edi+deflate_state.bi_buf],0
1929
		mov dword[edi+deflate_state.bi_valid],0
1930
		jmp .end0
1931
	@@: ;else if (..>=..)
1932
		cmp dword[edi+deflate_state.bi_valid],8
1933
		jl .end0
1934
		mov cl,byte[edi+deflate_state.bi_buf]
1935
		put_byte edi,cl
1936
		shr word[edi+deflate_state.bi_buf],8
1937
		sub dword[edi+deflate_state.bi_valid],8
1938
	.end0:
1939
	ret
1940
endp
1941
 
1942
; ===========================================================================
1943
; Flush the bit buffer and align the output on a byte boundary
1944
 
6847 IgorA 1945
;void (deflate_state* s)
6617 IgorA 1946
align 4
1947
proc bi_windup uses eax ecx edi, s:dword
1948
	mov edi,[s]
1949
	cmp dword[edi+deflate_state.bi_valid],8
1950
	jle @f ;if (..>..)
1951
		mov cx,[edi+deflate_state.bi_buf]
1952
		put_short edi, cx
1953
		jmp .end0
1954
	@@: ;else if (..>0)
1955
		cmp dword[edi+deflate_state.bi_valid],0
1956
		jle .end0
1957
		mov cl,byte[edi+deflate_state.bi_buf]
1958
		put_byte edi, cl
1959
	.end0:
1960
	mov word[edi+deflate_state.bi_buf],0
1961
	mov dword[edi+deflate_state.bi_valid],0
1962
if DEBUG eq 1
1963
	mov eax,[edi+deflate_state.bits_sent]
1964
	add eax,7
1965
	and eax,not 7
1966
	mov [edi+deflate_state.bits_sent],eax
1967
end if
1968
	ret
1969
endp
1970
 
1971
; ===========================================================================
1972
; Copy a stored block, storing first the length and its
1973
; one's complement if requested.
1974
 
1975
;void (s, buf, len, header)
1976
;    deflate_state* s
1977
;    charf    *buf   ;the input data
1978
;    unsigned len    ;its length
1979
;    int      header ;true if block header must be written
1980
align 4
1981
proc copy_block uses eax ebx ecx edi esi, s:dword, buf:dword, len:dword, p4header:dword
1982
	mov edi,[s]
1983
	stdcall bi_windup,edi ;align on byte boundary
1984
 
1985
	cmp dword[p4header],0
1986
	je @f ;if (..)
1987
		mov ecx,[len]
1988
		put_short edi, cx
1989
		not cx
1990
		put_short edi, cx
1991
if DEBUG eq 1
1992
		add dword[edi+deflate_state.bits_sent],2*16
1993
end if
1994
	@@:
1995
if DEBUG eq 1
1996
	mov ecx,[len]
1997
	shl ecx,3
1998
	add [edi+deflate_state.bits_sent],ecx
1999
end if
2000
	mov ecx,[len]
6851 IgorA 2001
;	test ecx,ecx
2002
;	jz .end_f
6617 IgorA 2003
	mov esi,[buf]
6815 IgorA 2004
	jmp .end0
2005
align 4
6617 IgorA 2006
	@@: ;while (len--)
2007
		lodsb
2008
		mov bl,al
2009
		put_byte edi, bl
6815 IgorA 2010
	.end0:
6617 IgorA 2011
		loop @b
6851 IgorA 2012
;	.end_f:
6617 IgorA 2013
	ret
2014
endp