Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
6617 IgorA 1
; deflate.asm -- compress data using the deflation algorithm
2
; Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
3
; For conditions of distribution and use, see copyright notice in zlib.inc
4
 
5
;  ALGORITHM
6
 
7
;      The "deflation" process depends on being able to identify portions
8
;      of the input text which are identical to earlier input (within a
9
;      sliding window trailing behind the input currently being processed).
10
 
11
;      The most straightforward technique turns out to be the fastest for
12
;      most input files: try all possible matches and select the longest.
13
;      The key feature of this algorithm is that insertions into the string
14
;      dictionary are very simple and thus fast, and deletions are avoided
15
;      completely. Insertions are performed at each input character, whereas
16
;      string matches are performed only when the previous match ends. So it
17
;      is preferable to spend more time in matches to allow very fast string
18
;      insertions and avoid deletions. The matching algorithm for small
19
;      strings is inspired from that of Rabin & Karp. A brute force approach
20
;      is used to find longer strings when a small match has been found.
21
;      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
22
;      (by Leonid Broukhis).
23
;         A previous version of this file used a more sophisticated algorithm
24
;      (by Fiala and Greene) which is guaranteed to run in linear amortized
25
;      time, but has a larger average cost, uses more memory and is patented.
26
;      However the F&G algorithm may be faster for some highly redundant
27
;      files if the parameter max_chain_length (described below) is too large.
28
 
29
;  ACKNOWLEDGEMENTS
30
 
31
;      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
32
;      I found it in 'freeze' written by Leonid Broukhis.
33
;      Thanks to many people for bug reports and testing.
34
 
35
;  REFERENCES
36
 
37
;      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
38
;      Available in http://tools.ietf.org/html/rfc1951
39
 
40
;      A description of the Rabin and Karp algorithm is given in the book
41
;         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
42
 
43
;      Fiala,E.R., and Greene,D.H.
44
;         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
45
 
46
 
47
deflate_copyright db ' deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler ',0
48
 
49
;  If you use the zlib library in a product, an acknowledgment is welcome
50
;  in the documentation of your product. If for some reason you cannot
51
;  include such an acknowledgment, I would appreciate that you keep this
52
;  copyright string in the executable of your product.
53
 
54
; ===========================================================================
55
;  Function prototypes.
56
 
57
;enum block_state
58
need_more   equ 1 ;block not completed, need more input or more output
59
block_done  equ 2 ;block flush performed
60
finish_started equ 3 ;finish started, need only more output at next deflate
61
finish_done equ 4 ;finish done, accept no more input or output
62
 
63
; ===========================================================================
64
; Local data
65
 
66
NIL equ 0
67
; Tail of hash chains
68
 
69
TOO_FAR equ 4096
70
; Matches of length 3 are discarded if their distance exceeds TOO_FAR
71
 
72
; Values for max_lazy_match, good_match and max_chain_length, depending on
73
; the desired pack level (0..9). The values given below have been tuned to
74
; exclude worst case performance for pathological files. Better values may be
75
; found for specific files.
76
 
77
struct config_s ;config
78
	good_length dw ? ;uint_16 ;reduce lazy search above this match length
79
	max_lazy    dw ? ;uint_16 ;do not perform lazy search above this match length
80
	nice_length dw ? ;uint_16 ;quit search above this match length
81
	max_chain   dw ? ;uint_16
82
	co_func     dd ? ;compress_func
83
ends
84
 
85
align 16
86
configuration_table:
87
	config_s  0,   0,   0,    0, deflate_stored  ;store only
88
	config_s  4,   4,   8,    4, deflate_fast ;max speed, no lazy matches
89
if FASTEST eq 0
90
	config_s  4,   5,  16,    8, deflate_fast
91
	config_s  4,   6,  32,   32, deflate_fast
92
	config_s  4,   4,  16,   16, deflate_slow ;lazy matches
93
	config_s  8,  16,  32,   32, deflate_slow
94
	config_s  8,  16, 128,  128, deflate_slow
95
	config_s  8,  32, 128,  256, deflate_slow
96
	config_s 32, 128, 258, 1024, deflate_slow
97
	config_s 32, 258, 258, 4096, deflate_slow ;max compression
98
end if
99
 
100
; Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
101
; For deflate_fast() (levels <= 3) good is ignored and lazy has a different
102
; meaning.
103
 
104
 
105
EQUAL equ 0
106
; result of memcmp for equal strings
107
 
108
; rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH
109
macro RANK f, reg
110
{
111
local .end0
112
	xor reg,reg
113
	cmp f,4
114
	jle .end0
115
		sub reg,9
116
	.end0:
117
	add reg,f
118
	add reg,f
119
}
120
 
121
; ===========================================================================
122
; Update a hash value with the given input byte
123
; IN  assertion: all calls to to UPDATE_HASH are made with consecutive
124
;    input characters, so that a running hash key can be computed from the
125
;    previous key instead of complete recalculation each time.
126
 
127
macro UPDATE_HASH s,h,c
128
{
129
push ebx ecx
130
	mov ebx,h
131
	mov ecx,[s+deflate_state.hash_shift]
132
	shl ebx,cl
133
	xor ebx,c
134
	and ebx,[s+deflate_state.hash_mask]
135
	mov h,ebx
136
pop ecx ebx
137
}
138
 
139
; ===========================================================================
140
; Insert string str in the dictionary and set match_head to the previous head
141
; of the hash chain (the most recent string with same hash key). Return
142
; the previous length of the hash chain.
143
; If this file is compiled with -DFASTEST, the compression level is forced
144
; to 1, and no hash chains are maintained.
145
; IN  assertion: all calls to to INSERT_STRING are made with consecutive
146
;    input characters and the first MIN_MATCH bytes of str are valid
147
;    (except for the last MIN_MATCH-1 bytes of the input file).
148
 
149
macro INSERT_STRING s, str, match_head
150
{
151
	mov eax,[s+deflate_state.window]
152
	add eax,str
153
	add eax,MIN_MATCH-1
154
	movzx eax,byte[eax]
155
	UPDATE_HASH s, [s+deflate_state.ins_h], eax
156
	mov eax,[s+deflate_state.ins_h]
157
	shl eax,2
158
	add eax,[s+deflate_state.head]
159
	mov eax,[eax]
160
	mov match_head,eax
161
if FASTEST eq 0
162
push ebx
163
	mov ebx,[s+deflate_state.w_mask]
164
	and ebx,str
165
	add ebx,[s+deflate_state.prev]
166
	mov byte[ebx],al
167
pop ebx
168
end if
169
	mov eax,[s+deflate_state.ins_h]
170
	shl eax,2
171
	add eax,[s+deflate_state.head]
172
	push str
173
	pop dword[eax]
174
}
175
 
176
; ===========================================================================
177
; Initialize the hash table (avoiding 64K overflow for 16 bit systems).
178
; prev[] will be initialized on the fly.
179
 
180
macro CLEAR_HASH s
181
{
182
	mov eax,[s+deflate_state.hash_size]
183
	dec eax
184
	shl eax,2
185
	add eax,[s+deflate_state.head]
186
	mov dword[eax],NIL
187
	mov eax,[s+deflate_state.hash_size]
188
	dec eax
189
	shl eax,2 ;sizeof(*s.head)
190
	stdcall zmemzero, [s+deflate_state.head], eax
191
}
192
 
193
align 4
194
proc deflateInit, strm:dword, level:dword
195
	stdcall deflateInit_, [strm], [level], ZLIB_VERSION, sizeof.z_stream
196
	ret
197
endp
198
 
199
; =========================================================================
200
;int (strm, level, version, stream_size)
6639 IgorA 201
;    z_streamp strm
202
;    int level
203
;    const char *version
204
;    int stream_size
6617 IgorA 205
align 4
206
proc deflateInit_, strm:dword, level:dword, version:dword, stream_size:dword
207
	stdcall deflateInit2_, [strm], [level], Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,\
208
			Z_DEFAULT_STRATEGY, [version], [stream_size]
209
	; To do: ignore strm->next_in if we use it as window
210
	ret
211
endp
212
 
213
align 4
214
proc deflateInit2, strm:dword, level:dword, method:dword, windowBits:dword, memLevel:dword, strategy:dword
215
	stdcall deflateInit2_, [strm],[level],[method],[windowBits],[memLevel],\
216
		[strategy], ZLIB_VERSION, sizeof.z_stream
217
	ret
218
endp
219
 
220
; =========================================================================
221
;int (strm, level, method, windowBits, memLevel, strategy,
222
;                  version, stream_size)
6639 IgorA 223
;    z_streamp strm
224
;    int  level
225
;    int  method
226
;    int  windowBits
227
;    int  memLevel
228
;    int  strategy
229
;    const char *version
230
;    int stream_size
6617 IgorA 231
align 4
232
proc deflateInit2_ uses ebx ecx edx edi, strm:dword, level:dword, method:dword,\
233
	windowBits:dword, memLevel:dword, strategy:dword, version:dword, stream_size:dword
234
locals
235
	wrap dd 1 ;int
236
	overlay dd ? ;uint_16p
237
endl
238
	; We overlay pending_buf and d_buf+l_buf. This works since the average
239
	; output size for (length,distance) codes is <= 24 bits.
240
 
241
	mov eax,[version]
242
	cmp eax,Z_NULL
243
	je @f
244
	mov ebx,dword[ZLIB_VERSION]
245
	cmp dword[eax],ebx
246
	jne @f
247
	cmp dword[stream_size],sizeof.z_stream
248
	je .end0
249
	@@: ;if (..==0 || ..[0]!=..[0] || ..!=..)
250
		mov eax,Z_VERSION_ERROR
251
		jmp .end_f
252
	.end0:
253
	mov ebx,[strm]
254
	cmp ebx,Z_NULL
255
	jne @f ;if (..==0) return ..
256
		mov eax,Z_STREAM_ERROR
257
		jmp .end_f
258
	@@:
259
 
260
	mov dword[ebx+z_stream.msg],Z_NULL
261
	cmp dword[ebx+z_stream.zalloc],0
262
	jne @f ;if (..==0)
263
if Z_SOLO eq 1
264
		mov eax,Z_STREAM_ERROR
265
		jmp .end_f
266
else
267
		mov dword[ebx+z_stream.zalloc],zcalloc
268
		mov dword[ebx+z_stream.opaque],0
269
end if
270
	@@:
271
	cmp dword[ebx+z_stream.zfree],0
272
	jne @f ;if (..==0)
273
if Z_SOLO eq 1
274
		mov eax,Z_STREAM_ERROR
275
		jmp .end_f
276
else
277
		mov dword[ebx+z_stream.zfree],zcfree
278
end if
279
	@@:
280
 
281
if FASTEST eq 1
282
	cmp dword[level],0
283
	je @f ;if (..!=0)
284
		mov dword[level],1
285
	@@:
286
else
287
	cmp dword[level],Z_DEFAULT_COMPRESSION
288
	jne @f ;if (..==0)
289
		mov dword[level],6
290
	@@:
291
end if
292
 
293
	cmp dword[windowBits],0
294
	jge @f ;if (..<0) ;suppress zlib wrapper
295
		mov dword[wrap],0
296
		neg dword[windowBits]
297
		inc dword[windowBits]
298
		jmp .end1
299
	@@:
300
if GZIP eq 1
301
	cmp dword[windowBits],15
302
	jle .end1 ;else if (..>15)
303
		mov dword[wrap],2 ;write gzip wrapper instead
304
		sub dword[windowBits],16
305
end if
306
	.end1:
307
	cmp dword[memLevel],1
308
	jl .end2
309
	cmp dword[memLevel],MAX_MEM_LEVEL
310
	jg .end2
311
	cmp dword[method],Z_DEFLATED
312
	jne .end2
313
	cmp dword[windowBits],8
314
	jl .end2
315
	cmp dword[windowBits],15
316
	jg .end2
317
	cmp dword[level],0
318
	jl .end2
319
	cmp dword[level],9
320
	jg .end2
321
	cmp dword[strategy],0
322
	jl .end2
323
	cmp dword[strategy],Z_FIXED
324
	jle @f
325
	.end2: ;if (..<.. || ..>.. || ..!=.. || ..<.. || ..>.. || ..<0 || ..>.. || ..<0 || ..>..)
326
		mov eax,Z_STREAM_ERROR
327
		jmp .end_f
328
	@@:
329
	cmp dword[windowBits],8
330
	jne @f ;if (..==..)
331
		inc dword[windowBits] ;until 256-byte window bug fixed
332
	@@:
333
	ZALLOC ebx, 1, sizeof.deflate_state
334
	;eax = s
335
	cmp eax,Z_NULL
336
	jne @f ;if (..==0)
337
		mov eax,Z_MEM_ERROR
338
		jmp .end_f
339
	@@:
340
	mov edi,eax ;edi = s
341
	mov [ebx+z_stream.state],edi
342
	mov [edi+deflate_state.strm],ebx
343
 
344
	mov eax,[wrap]
345
	mov [edi+deflate_state.wrap],eax
346
	mov [edi+deflate_state.gzhead],Z_NULL
347
	mov ecx,[windowBits]
348
	mov [edi+deflate_state.w_bits],ecx
349
	xor eax,eax
350
	inc eax
351
	shl eax,cl
352
	mov [edi+deflate_state.w_size],eax
353
	dec eax
354
	mov [edi+deflate_state.w_mask],eax
355
 
356
	mov ecx,[memLevel]
357
	add ecx,7
358
	mov [edi+deflate_state.hash_bits],ecx
359
	xor eax,eax
360
	inc eax
361
	shl eax,cl
362
	mov [edi+deflate_state.hash_size],eax
363
	dec eax
364
	mov [edi+deflate_state.hash_mask],eax
365
	add ecx,MIN_MATCH-1
366
	xor edx,edx
367
	mov eax,ecx
368
	mov ecx,MIN_MATCH
369
	div ecx
370
	mov [edi+deflate_state.hash_shift],eax
371
 
372
	ZALLOC ebx, [edi+deflate_state.w_size], 2 ;2*sizeof(Byte)
373
	mov [edi+deflate_state.window],eax
374
	ZALLOC ebx, [edi+deflate_state.w_size], 4 ;sizeof(Pos)
375
	mov [edi+deflate_state.prev],eax
376
	ZALLOC ebx, [edi+deflate_state.hash_size], 4 ;sizeof(Pos)
377
	mov [edi+deflate_state.head],eax
378
 
379
	mov dword[edi+deflate_state.high_water],0 ;nothing written to s->window yet
380
 
381
	mov ecx,[memLevel]
382
	add ecx,6
383
	xor eax,eax
384
	inc eax
385
	shl eax,cl
386
	mov [edi+deflate_state.lit_bufsize],eax ;16K elements by default
387
 
388
	ZALLOC ebx, eax, 4 ;sizeof(uint_16)+2
389
	mov [overlay],eax
390
	mov [edi+deflate_state.pending_buf],eax
391
	mov eax,[edi+deflate_state.lit_bufsize]
392
	imul eax,4 ;sizeof(uint_16)+2
393
	mov [edi+deflate_state.pending_buf_size],eax
394
 
395
	cmp dword[edi+deflate_state.window],Z_NULL
396
	je .end3
397
	cmp dword[edi+deflate_state.prev],Z_NULL
398
	je .end3
399
	cmp dword[edi+deflate_state.head],Z_NULL
400
	je .end3
401
	cmp dword[edi+deflate_state.pending_buf],Z_NULL
402
	je .end3
403
		jmp @f
404
	.end3: ;if (..==0 || ..==0 || ..==0 || ..==0)
405
		mov dword[edi+deflate_state.status],FINISH_STATE
406
		ERR_MSG Z_MEM_ERROR
407
		mov [ebx+z_stream.msg],eax
408
		stdcall deflateEnd, ebx
409
		mov eax,Z_MEM_ERROR
410
		jmp .end_f
411
	@@:
412
	mov eax,[edi+deflate_state.lit_bufsize]
413
	shr eax,1 ;/=sizeof(uint_16)
414
	add eax,[overlay]
415
	mov [edi+deflate_state.d_buf],eax
416
	mov eax,[edi+deflate_state.lit_bufsize]
417
	imul eax,3 ;1+sizeof(uint_16)
418
	add eax,[edi+deflate_state.pending_buf]
419
	mov [edi+deflate_state.l_buf],eax
420
 
421
	mov eax,[level]
422
	mov [edi+deflate_state.level],ax
423
	mov eax,[strategy]
424
	mov [edi+deflate_state.strategy],ax
425
	mov eax,[method]
426
	mov [edi+deflate_state.method],al
427
 
428
	stdcall deflateReset, ebx
429
.end_f:
430
zlib_debug 'deflateInit2_ strategy = %d',[strategy]
431
	ret
432
endp
433
 
434
; =========================================================================
435
;int (strm, dictionary, dictLength)
6639 IgorA 436
;    z_streamp strm
437
;    const Bytef *dictionary
438
;    uInt  dictLength
6617 IgorA 439
align 4
440
proc deflateSetDictionary uses ebx edi, strm:dword, dictionary:dword, dictLength:dword
441
locals
442
;    deflate_state *s;
443
;    uInt str, n;
444
	wrap dd ? ;int
445
	avail dd ? ;unsigned
446
;    z_const unsigned char *next;
447
endl
448
	mov ebx,[strm]
449
	cmp ebx,Z_NULL
450
	je @f
451
	mov edi,[ebx+z_stream.state]
452
	cmp edi,Z_NULL
453
	je @f
454
	cmp dword[dictionary],Z_NULL
455
	je @f ;if (..==0 || ..==0 || ..==0)
456
		jmp .end0
457
	@@:
458
		mov eax,Z_STREAM_ERROR
459
		jmp .end_f
460
	.end0:
461
 
462
	mov eax,[edi+deflate_state.wrap]
463
	mov [wrap],eax
464
;    if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
465
;        return Z_STREAM_ERROR;
466
 
467
	; when using zlib wrappers, compute Adler-32 for provided dictionary
468
;    if (wrap == 1)
469
;        strm->adler = adler32(strm->adler, dictionary, dictLength);
470
;    s->wrap = 0;                    /* avoid computing Adler-32 in read_buf */
471
 
472
	; if dictionary would fill window, just replace the history
473
;    if (dictLength >= s->w_size) {
474
;        if (wrap == 0) {            /* already empty otherwise */
475
;            CLEAR_HASH(s);
476
;            s->strstart = 0;
477
;            s->block_start = 0L;
478
;            s->insert = 0;
479
;        }
480
;        dictionary += dictLength - s->w_size;  /* use the tail */
481
;        dictLength = s->w_size;
482
;    }
483
 
484
	; insert dictionary into window and hash
485
;    avail = strm->avail_in;
486
;    next = strm->next_in;
487
;    strm->avail_in = dictLength;
488
;    strm->next_in = (z_const Bytef *)dictionary;
489
;    fill_window(s);
490
;    while (s->lookahead >= MIN_MATCH) {
491
;        str = s->strstart;
492
;        n = s->lookahead - (MIN_MATCH-1);
493
;        do {
494
;            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
495
if FASTEST eq 0
496
;            s->prev[str & s->w_mask] = s->head[s->ins_h];
497
end if
498
;            s->head[s->ins_h] = (Pos)str;
499
;            str++;
500
;        } while (--n);
501
;        s->strstart = str;
502
;        s->lookahead = MIN_MATCH-1;
503
;        fill_window(s);
504
;    }
505
;    s->strstart += s->lookahead;
506
;    s->block_start = (long)s->strstart;
507
;    s->insert = s->lookahead;
508
;    s->lookahead = 0;
509
;    s->match_length = s->prev_length = MIN_MATCH-1;
510
;    s->match_available = 0;
511
;    strm->next_in = next;
512
;    strm->avail_in = avail;
513
;    s->wrap = wrap;
514
	mov eax,Z_OK
515
.end_f:
516
	ret
517
endp
518
 
519
; =========================================================================
520
;int (strm)
6639 IgorA 521
;    z_streamp strm
6617 IgorA 522
align 4
523
proc deflateResetKeep uses ebx edi, strm:dword
524
;    deflate_state *s;
525
 
526
	mov ebx,[strm]
527
	cmp ebx,Z_NULL
528
	je @f
529
	mov edi,[ebx+z_stream.state]
530
	cmp edi,Z_NULL
531
	je @f
532
	cmp dword[ebx+z_stream.zalloc],0
533
	je @f
534
	cmp dword[ebx+z_stream.zfree],0
535
	je @f ;if (..==0 || ..==0 || ..==0 || ..==0)
536
		jmp .end0
537
	@@:
538
		mov eax,Z_STREAM_ERROR
539
		jmp .end_f
540
	.end0:
541
 
542
	mov dword[ebx+z_stream.total_out],0
543
	mov dword[ebx+z_stream.total_in],0
544
	mov dword[ebx+z_stream.msg],Z_NULL ;use zfree if we ever allocate msg dynamically
545
	mov word[ebx+z_stream.data_type],Z_UNKNOWN
546
 
6741 IgorA 547
	mov dword[edi+deflate_state.pending],0
6617 IgorA 548
	mov eax,[edi+deflate_state.pending_buf]
549
	mov [edi+deflate_state.pending_out],eax
550
 
551
	cmp dword[edi+deflate_state.wrap],0
552
	jge @f ;if (..<0)
553
		neg dword[edi+deflate_state.wrap]
554
		inc dword[edi+deflate_state.wrap] ;was made negative by deflate(..., Z_FINISH)
555
	@@:
556
	mov eax,BUSY_STATE
557
	cmp dword[edi+deflate_state.wrap],0
558
	je @f
559
		mov eax,INIT_STATE
560
	@@:
561
	mov dword[edi+deflate_state.status],eax
562
	stdcall adler32, 0, Z_NULL, 0
563
if GZIP eq 1
564
	cmp dword[edi+deflate_state.wrap],2
565
	jne @f
6639 IgorA 566
		xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 567
	@@:
568
end if
569
	mov dword[ebx+z_stream.adler],eax
570
	mov dword[edi+deflate_state.last_flush],Z_NO_FLUSH
571
 
572
	stdcall _tr_init, edi
573
 
574
	mov eax,Z_OK
575
.end_f:
576
	ret
577
endp
578
 
579
; =========================================================================
580
;int (strm)
6639 IgorA 581
;    z_streamp strm
6617 IgorA 582
align 4
583
proc deflateReset uses ebx, strm:dword
584
	mov ebx,[strm]
6639 IgorA 585
	zlib_debug 'deflateReset'
6617 IgorA 586
	stdcall deflateResetKeep, ebx
587
	cmp eax,0
588
	jne @f ;if (..==Z_OK)
589
		stdcall lm_init, [ebx+z_stream.state]
590
	@@:
591
	ret
592
endp
593
 
594
; =========================================================================
595
;int (strm, head)
6639 IgorA 596
;    z_streamp strm
597
;    gz_headerp head
6617 IgorA 598
align 4
599
proc deflateSetHeader uses ebx, strm:dword, head:dword
600
	mov ebx,[strm]
601
	cmp ebx,Z_NULL
602
	je @f
603
	mov ebx,[ebx+z_stream.state]
604
	cmp ebx,Z_NULL
605
	jne .end0
606
	@@: ;if (..==0 || ..==0) return ..
607
		mov eax,Z_STREAM_ERROR
608
		jmp .end_f
609
	.end0:
610
	cmp dword[ebx+deflate_state.wrap],2
611
	je @f ;if (..!=..) return ..
612
		mov eax,Z_STREAM_ERROR
613
		jmp .end_f
614
	@@:
615
	mov eax,[head]
616
	mov [ebx+deflate_state.gzhead],eax
617
	mov eax,Z_OK
618
.end_f:
619
	ret
620
endp
621
 
622
; =========================================================================
623
;int (strm, pending, bits)
6639 IgorA 624
;    unsigned *pending
625
;    int *bits
626
;    z_streamp strm
6617 IgorA 627
align 4
628
proc deflatePending uses ebx edi, strm:dword, pending:dword, bits:dword
629
	mov ebx,[strm]
630
	cmp ebx,Z_NULL
631
	je @f
632
	mov edi,[ebx+z_stream.state]
633
	cmp edi,Z_NULL
634
	jne .end0
635
	@@: ;if (..==0 || ..==0) return ..
636
		mov eax,Z_STREAM_ERROR
637
		jmp .end_f
638
	.end0:
639
	cmp dword[pending],Z_NULL
640
	je @f ;if (..!=..)
641
		mov eax,[pending]
6741 IgorA 642
		mov ebx,[edi+deflate_state.pending]
6617 IgorA 643
		mov [eax],ebx
644
	@@:
645
	cmp dword[bits],Z_NULL
646
	je @f ;if (..!=..)
647
		mov eax,[bits]
648
		mov ebx,[edi+deflate_state.bi_valid]
649
		mov [eax],ebx
650
	@@:
651
	mov eax,Z_OK
652
.end_f:
653
	ret
654
endp
655
 
656
; =========================================================================
657
;int (strm, bits, value)
6639 IgorA 658
;    z_streamp strm
659
;    int bits
660
;    int value
6617 IgorA 661
align 4
662
proc deflatePrime uses ebx edi, strm:dword, bits:dword, value:dword
663
;    int put;
664
 
665
	mov ebx,[strm]
666
	cmp ebx,Z_NULL
667
	je @f
668
	mov edi,[ebx+z_stream.state] ;s = strm.state
669
	cmp edi,Z_NULL
670
	jne .end0
671
	@@: ;if (..==0 || ..==0) return ..
672
		mov eax,Z_STREAM_ERROR
673
		jmp .end_f
674
	.end0:
675
;    if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
676
;        return Z_BUF_ERROR;
677
;    do {
678
;        put = Buf_size - s->bi_valid;
679
;        if (put > bits)
680
;            put = bits;
681
;        s->bi_buf |= (uint_16)((value & ((1 << put) - 1)) << s->bi_valid);
682
;        s->bi_valid += put;
683
;        _tr_flush_bits(s);
684
;        value >>= put;
685
;        bits -= put;
686
;    } while (bits);
687
	mov eax,Z_OK
688
.end_f:
689
	ret
690
endp
691
 
692
; =========================================================================
693
;int (strm, level, strategy)
6639 IgorA 694
;    z_streamp strm
695
;    int level
696
;    int strategy
6617 IgorA 697
align 4
698
proc deflateParams uses ebx edi, strm:dword, level:dword, strategy:dword
699
;    compress_func func;
700
;    int err = Z_OK;
701
 
702
	mov ebx,[strm]
703
	cmp ebx,Z_NULL
704
	je @f
705
	mov edi,[ebx+z_stream.state] ;s = strm.state
706
	cmp edi,Z_NULL
707
	jne .end0
708
	@@: ;if (..==0 || ..==0) return ..
709
		mov eax,Z_STREAM_ERROR
710
		jmp .end_f
711
	.end0:
712
 
713
if FASTEST eq 1
714
	cmp dword[level],0
715
	je @f ;if (..!=0)
716
		mov dword[level],1
717
	@@:
718
else
719
	cmp dword[level],Z_DEFAULT_COMPRESSION
720
	jne @f ;if (..==0)
721
		mov dword[level],6
722
	@@:
723
end if
724
;    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
725
;        return Z_STREAM_ERROR;
726
;    }
727
;    func = configuration_table[s->level].func;
728
 
729
;    if ((strategy != s->strategy || func != configuration_table[level].func) &&
730
;        strm->total_in != 0) {
731
	; Flush the last buffer:
732
;        err = deflate(strm, Z_BLOCK);
733
;        if (err == Z_BUF_ERROR && s->pending == 0)
734
;            err = Z_OK;
735
;    }
736
;    if (s->level != level) {
737
;        s->level = level;
738
;        s->max_lazy_match   = configuration_table[level].max_lazy;
739
;        s->good_match       = configuration_table[level].good_length;
740
;        s->nice_match       = configuration_table[level].nice_length;
741
;        s->max_chain_length = configuration_table[level].max_chain;
742
;    }
743
;    s->strategy = strategy;
744
;    return err;
745
.end_f:
746
	ret
747
endp
748
 
749
; =========================================================================
750
;int (strm, good_length, max_lazy, nice_length, max_chain)
6639 IgorA 751
;    z_streamp strm
752
;    int good_length
753
;    int max_lazy
754
;    int nice_length
755
;    int max_chain
6617 IgorA 756
align 4
757
proc deflateTune uses ebx, strm:dword, good_length:dword, max_lazy:dword,\
758
			nice_length:dword, max_chain:dword
759
	mov ebx,[strm]
760
	cmp ebx,Z_NULL
761
	je @f
762
	cmp dword[ebx+z_stream.state],Z_NULL
763
	jne .end0
764
	@@: ;if (..==0 || ..==0) return ..
765
		mov eax,Z_STREAM_ERROR
766
		jmp .end_f
767
	.end0:
768
	mov ebx,[ebx+z_stream.state] ;s = strm.state
769
	mov eax,[good_length]
770
	mov [ebx+deflate_state.good_match],eax
771
	mov eax,[max_lazy]
772
	mov [ebx+deflate_state.max_lazy_match],eax
773
	mov eax,[nice_length]
774
	mov [ebx+deflate_state.nice_match],eax
775
	mov eax,[max_chain]
776
	mov [ebx+deflate_state.max_chain_length],eax
777
	mov eax,Z_OK
778
.end_f:
779
	ret
780
endp
781
 
782
; =========================================================================
783
; For the default windowBits of 15 and memLevel of 8, this function returns
784
; a close to exact, as well as small, upper bound on the compressed size.
785
; They are coded as constants here for a reason--if the #define's are
786
; changed, then this function needs to be changed as well.  The return
787
; value for 15 and 8 only works for those exact settings.
788
 
789
; For any setting other than those defaults for windowBits and memLevel,
790
; the value returned is a conservative worst case for the maximum expansion
791
; resulting from using fixed blocks instead of stored blocks, which deflate
792
; can emit on compressed data for some combinations of the parameters.
793
 
794
; This function could be more sophisticated to provide closer upper bounds for
795
; every combination of windowBits and memLevel.  But even the conservative
796
; upper bound of about 14% expansion does not seem onerous for output buffer
797
; allocation.
798
 
799
;uLong (strm, sourceLen)
6639 IgorA 800
;    z_streamp strm
801
;    uLong sourceLen
6617 IgorA 802
align 4
803
proc deflateBound, strm:dword, sourceLen:dword
804
;    deflate_state *s;
805
;    uLong complen, wraplen;
806
;    Bytef *str;
6639 IgorA 807
	zlib_debug 'deflateBound'
6617 IgorA 808
 
809
	; conservative upper bound for compressed data
810
;    complen = sourceLen +
811
;              ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5;
812
 
813
	; if can't get parameters, return conservative bound plus zlib wrapper
814
;    if (strm == Z_NULL || strm->state == Z_NULL)
815
;        return complen + 6;
816
 
817
	; compute wrapper length
818
;    s = strm->state;
819
;    switch (s->wrap) {
820
;    case 0:                                 /* raw deflate */
821
;        wraplen = 0;
822
;        break;
823
;    case 1:                                 /* zlib wrapper */
824
;        wraplen = 6 + (s->strstart ? 4 : 0);
825
;        break;
826
;    case 2:                                 /* gzip wrapper */
827
;        wraplen = 18;
828
;        if (s->gzhead != Z_NULL) {          /* user-supplied gzip header */
829
;            if (s->gzhead->extra != Z_NULL)
830
;                wraplen += 2 + s->gzhead->extra_len;
831
;            str = s->gzhead->name;
832
;            if (str != Z_NULL)
833
;                do {
834
;                    wraplen++;
835
;                } while (*str++);
836
;            str = s->gzhead->comment;
837
;            if (str != Z_NULL)
838
;                do {
839
;                    wraplen++;
840
;                } while (*str++);
841
;            if (s->gzhead->hcrc)
842
;                wraplen += 2;
843
;        }
844
;        break;
845
;    default:                                /* for compiler happiness */
846
;        wraplen = 6;
847
;    }
848
 
849
	; if not default parameters, return conservative bound
850
;    if (s->w_bits != 15 || s->hash_bits != 8 + 7)
851
;        return complen + wraplen;
852
 
853
	; default settings: return tight bound for that case
854
;    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
855
;           (sourceLen >> 25) + 13 - 6 + wraplen;
856
.end_f:
857
	ret
858
endp
859
 
860
; =========================================================================
861
; Put a short in the pending buffer. The 16-bit value is put in MSB order.
862
; IN assertion: the stream state is correct and there is enough room in
863
; pending_buf.
864
 
865
;void (s, b)
6639 IgorA 866
;    deflate_state *s
867
;    uInt b
6617 IgorA 868
align 4
869
proc putShortMSB uses ebx ecx, s:dword, b:dword
870
	mov ebx,[s]
871
	mov ecx,[b]
872
	put_byte ebx, ch
873
	put_byte ebx, cl
874
	ret
875
endp
876
 
877
; =========================================================================
878
; Flush as much pending output as possible. All deflate() output goes
879
; through this function so some applications may wish to modify it
880
; to avoid allocating a large strm->next_out buffer and copying into it.
881
; (See also read_buf()).
882
 
883
;void (strm)
6639 IgorA 884
;    z_streamp strm
6617 IgorA 885
align 4
886
proc flush_pending uses eax ebx ecx edx, strm:dword
887
;ecx - len
888
;edx - deflate_state *s
889
;ebx - strm
6639 IgorA 890
	zlib_debug 'flush_pending'
6617 IgorA 891
	mov ebx,[strm]
892
	mov edx,[ebx+z_stream.state]
893
 
894
	stdcall _tr_flush_bits, edx
6741 IgorA 895
	mov ecx,[edx+deflate_state.pending]
6617 IgorA 896
	cmp cx,[ebx+z_stream.avail_out]
897
	jle @f ;if (..>..)
898
		movzx ecx,word[ebx+z_stream.avail_out]
899
	@@:
900
	cmp ecx,0
901
	je @f
902
 
903
	stdcall zmemcpy, [ebx+z_stream.next_out], [edx+deflate_state.pending_out], ecx
904
	add [ebx+z_stream.next_out],ecx
905
	add [edx+deflate_state.pending_out],ecx
906
	add [ebx+z_stream.total_out],ecx
907
	sub [ebx+z_stream.avail_out],cx
6741 IgorA 908
	sub [edx+deflate_state.pending],ecx
909
	cmp dword[edx+deflate_state.pending],0
6617 IgorA 910
	jne @f ;if (..==0)
911
		mov eax,[edx+deflate_state.pending_buf]
912
		mov [edx+deflate_state.pending_out],eax
913
	@@:
914
	ret
915
endp
916
 
917
; =========================================================================
918
;int (strm, flush)
6639 IgorA 919
;    z_streamp strm
920
;    int flush
6617 IgorA 921
align 4
922
proc deflate uses ebx ecx edx edi esi, strm:dword, flush:dword
923
locals
924
	old_flush dd ? ;int ;value of flush param for previous deflate call
925
	val dd ?
926
endl
927
	mov ebx,[strm]
928
zlib_debug 'deflate strm = %d',ebx
929
	cmp ebx,Z_NULL
930
	je @f
931
	mov edi,[ebx+z_stream.state] ;s = strm.state
932
	cmp edi,Z_NULL
933
	je @f
934
	cmp dword[flush],Z_BLOCK
935
	jg @f
936
	cmp dword[flush],0
6652 IgorA 937
	jge .end10 ;if (..==0 || ..==0 || ..>.. || ..<0)
6617 IgorA 938
	@@:
939
		mov eax,Z_STREAM_ERROR
940
		jmp .end_f
941
	.end10:
942
	cmp dword[ebx+z_stream.next_out],Z_NULL
943
	je .beg0
944
	cmp dword[ebx+z_stream.next_in],Z_NULL
945
	jne @f
6704 IgorA 946
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 947
	jne .beg0
948
	@@:
949
	cmp dword[edi+deflate_state.status],FINISH_STATE
950
	jne .end0
951
	cmp dword[flush],Z_FINISH
952
	je .end0
6652 IgorA 953
	.beg0: ;if (..==0 || (..==0 && ..!=0) || (..==.. && ..!=..))
6617 IgorA 954
		ERR_RETURN ebx, Z_STREAM_ERROR
955
		jmp .end_f
956
	.end0:
957
	cmp word[ebx+z_stream.avail_out],0
958
	jne @f ;if (..==0)
959
		ERR_RETURN ebx, Z_BUF_ERROR
960
		jmp .end_f
961
	@@:
962
 
963
	mov dword[edi+deflate_state.strm],ebx ;just in case
964
	mov eax,[edi+deflate_state.last_flush]
965
	mov [old_flush],eax
966
	mov eax,[flush]
967
	mov [edi+deflate_state.last_flush],eax
968
 
969
	; Write the header
970
	cmp dword[edi+deflate_state.status],INIT_STATE
971
	jne .end2 ;if (..==..)
972
if GZIP eq 1
973
		cmp dword[edi+deflate_state.wrap],2
974
		jne .end1 ;if (..==..)
6639 IgorA 975
			xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 976
			mov [ebx+z_stream.adler],eax
977
			put_byte edi, 31
978
			put_byte edi, 139
979
			put_byte edi, 8
980
			cmp dword[edi+deflate_state.gzhead],Z_NULL
981
			jne .end3 ;if (..==0)
982
				put_byte edi, 0
983
				put_dword edi, 0
984
				xor cl,cl
985
				cmp word[edi+deflate_state.level],2
986
				jge @f
987
					mov cl,4
988
				@@:
989
				cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
990
				jl @f
991
					mov cl,4
992
				@@:
993
				cmp word[edi+deflate_state.level],9
994
				jne @f
995
					mov cl,2
996
				@@: ;..==.. ? 2 : (..>=.. || ..<.. ? 4 : 0)
997
				put_byte edi, cl
998
				put_byte edi, OS_CODE
999
				mov dword[edi+deflate_state.status],BUSY_STATE
1000
				jmp .end2
1001
			.end3: ;else
1002
				mov edx,[edi+deflate_state.gzhead]
1003
				xor cl,cl
1004
				cmp [edx+gz_header.text],0
1005
				je @f
1006
					inc cl
1007
				@@:
1008
				cmp [edx+gz_header.hcrc],0
1009
				je @f
1010
					add cl,2
1011
				@@:
1012
				cmp [edx+gz_header.extra],Z_NULL
1013
				je @f
1014
					add cl,4
1015
				@@:
1016
				cmp [edx+gz_header.name],Z_NULL
1017
				je @f
1018
					add cl,8
1019
				@@:
1020
				cmp [edx+gz_header.comment],Z_NULL
1021
				je @f
1022
					add cl,16
1023
				@@:
1024
				put_byte edi, cl
1025
				mov ecx,[edx+gz_header.time]
1026
				put_dword edi, ecx
1027
				xor cl,cl
1028
				cmp word[edi+deflate_state.level],2
1029
				jge @f
1030
					mov cl,4
1031
				@@:
1032
				cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
1033
				jl @f
1034
					mov cl,4
1035
				@@:
1036
				cmp word[edi+deflate_state.level],9
1037
				jne @f
1038
					mov cl,2
1039
				@@: ;..==.. ? 2 : (..>=.. || ..<.. ? 4 : 0)
1040
				put_byte edi, cl
1041
				mov ecx,[edx+gz_header.os]
1042
				put_byte edi, cl
1043
				cmp dword[edx+gz_header.extra],Z_NULL
1044
				je @f ;if (..!=0)
1045
					mov ecx,[edx+gz_header.extra_len]
1046
					put_byte edi, cl
1047
					put_byte edi, ch
1048
				@@:
1049
				cmp dword[edx+gz_header.hcrc],0
1050
				je @f ;if (..)
1051
					stdcall calc_crc32, [ebx+z_stream.adler],\
6741 IgorA 1052
						[edi+deflate_state.pending_buf], [edi+deflate_state.pending]
6617 IgorA 1053
					mov [ebx+z_stream.adler],eax
1054
				@@:
1055
				mov dword[edi+deflate_state.gzindex],0
1056
				mov dword[edi+deflate_state.status],EXTRA_STATE
1057
			jmp .end2
1058
		.end1: ;else
1059
end if
1060
			mov edx,[edi+deflate_state.w_bits]
1061
			sub edx,8
1062
			shl edx,4
1063
			add edx,Z_DEFLATED
1064
			shl edx,8 ;edx = header
1065
			;esi = level_flags
1066
 
1067
			mov esi,3
1068
			cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
6652 IgorA 1069
			jge @f
6617 IgorA 1070
			cmp word[edi+deflate_state.level],2
6652 IgorA 1071
			jge .end30 ;if (..>=.. || ..<..)
1072
			@@:
6617 IgorA 1073
				xor esi,esi
1074
				jmp .end4
6652 IgorA 1075
			.end30:
6617 IgorA 1076
			cmp word[edi+deflate_state.level],6
1077
			jge @f ;else if (..<..)
1078
				mov esi,1
1079
				jmp .end4
1080
			@@:
1081
			;;cmp word[edi+deflate_state.level],6
1082
			jne .end4 ;else if (..==..)
1083
				mov esi,2
1084
			.end4:
1085
			shl esi,6
1086
			or edx,esi
1087
			cmp dword[edi+deflate_state.strstart],0
1088
			je @f ;if (..!=0)
1089
				or edx,PRESET_DICT
1090
			@@:
1091
			mov esi,edx
1092
			mov eax,edx
1093
			xor edx,edx
1094
			mov ecx,31
1095
			div ecx
1096
			add esi,31
1097
			sub esi,edx ;esi = header
1098
 
1099
			mov dword[edi+deflate_state.status],BUSY_STATE
1100
			stdcall putShortMSB, edi, esi
1101
 
1102
			; Save the adler32 of the preset dictionary:
1103
			cmp dword[edi+deflate_state.strstart],0
1104
			je @f ;if (..!=0)
1105
				mov ecx,[ebx+z_stream.adler]
1106
				bswap ecx
1107
				put_dword edi, ecx
1108
			@@:
6639 IgorA 1109
			xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 1110
			mov [ebx+z_stream.adler],eax
1111
	.end2:
1112
if GZIP eq 1
1113
	mov edx,[edi+deflate_state.gzhead]
1114
	cmp dword[edi+deflate_state.status],EXTRA_STATE
1115
	jne .end5 ;if (..==..)
1116
		cmp dword[edx+gz_header.extra],Z_NULL
1117
		je .end21 ;if (..!=..)
6741 IgorA 1118
			mov esi,[edi+deflate_state.pending]
6617 IgorA 1119
			;esi = beg ;start of bytes to update crc
1120
 
1121
			movzx ecx,word[edx+gz_header.extra_len]
1122
			.cycle0: ;while (..<..)
1123
			cmp dword[edi+deflate_state.gzindex],ecx
1124
			jge .cycle0end
6741 IgorA 1125
				mov eax,[edi+deflate_state.pending]
6617 IgorA 1126
				cmp eax,[edi+deflate_state.pending_buf_size]
1127
				jne .end24 ;if (..==..)
1128
					mov dword[edx+gz_header.hcrc],0
1129
					je @f
6741 IgorA 1130
					cmp [edi+deflate_state.pending],esi
6617 IgorA 1131
					jle @f ;if (.. && ..>..)
6741 IgorA 1132
						mov ecx,[edi+deflate_state.pending]
6617 IgorA 1133
						sub ecx,esi
1134
						mov eax,[edi+deflate_state.pending_buf]
1135
						add eax,esi
1136
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1137
						mov [ebx+z_stream.adler],eax
1138
					@@:
1139
					stdcall flush_pending, ebx
6741 IgorA 1140
					mov esi,[edi+deflate_state.pending]
6617 IgorA 1141
					cmp esi,[edi+deflate_state.pending_buf_size]
1142
					je .cycle0end ;if (..==..) break
1143
				.end24:
1144
				push ebx
1145
					mov ebx,[edi+deflate_state.gzindex]
1146
					add ebx,[edx+gz_header.extra]
1147
					mov bl,[ebx]
1148
					put_byte edi, bl
1149
				pop ebx
1150
				inc dword[edi+deflate_state.gzindex]
1151
				jmp .cycle0
1152
			.cycle0end:
1153
			mov dword[edx+gz_header.hcrc],0
1154
			je @f
6741 IgorA 1155
			cmp [edi+deflate_state.pending],esi
6617 IgorA 1156
			jle @f ;if (.. && ..>..)
6741 IgorA 1157
				mov ecx,[edi+deflate_state.pending]
6617 IgorA 1158
				sub ecx,esi
1159
				mov eax,[edi+deflate_state.pending_buf]
1160
				add eax,esi
1161
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1162
				mov [ebx+z_stream.adler],eax
1163
			@@:
1164
			mov eax,[edx+gz_header.extra_len]
1165
			cmp dword[edi+deflate_state.gzindex],eax
1166
			jne .end5 ;if (..==..)
1167
				mov dword[edi+deflate_state.gzindex],0
1168
				mov dword[edi+deflate_state.status],NAME_STATE
1169
			jmp .end5
1170
		.end21: ;else
1171
			mov dword[edi+deflate_state.status],NAME_STATE
1172
	.end5:
1173
	cmp dword[edi+deflate_state.status],NAME_STATE
1174
	jne .end6 ;if (..==..)
1175
		cmp dword[edx+gz_header.name],Z_NULL
1176
		je .end22 ;if (..!=..)
6741 IgorA 1177
			mov esi,[edi+deflate_state.pending]
6617 IgorA 1178
			;esi = beg ;start of bytes to update crc
1179
 
1180
			.cycle1: ;do
6741 IgorA 1181
				mov eax,[edi+deflate_state.pending]
6617 IgorA 1182
				cmp eax,[edi+deflate_state.pending_buf_size]
1183
				jne .end25 ;if (..==..)
1184
					mov dword[edx+gz_header.hcrc],0
1185
					je @f
6741 IgorA 1186
					cmp [edi+deflate_state.pending],esi
6617 IgorA 1187
					jle @f ;if (.. && ..>..)
6741 IgorA 1188
						mov ecx,[edi+deflate_state.pending]
6617 IgorA 1189
						sub ecx,esi
1190
						mov eax,[edi+deflate_state.pending_buf]
1191
						add eax,esi
1192
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1193
						mov [ebx+z_stream.adler],eax
1194
					@@:
1195
					stdcall flush_pending, ebx
6741 IgorA 1196
					mov esi,[edi+deflate_state.pending]
1197
					cmp esi,[edi+deflate_state.pending_buf_size]
6617 IgorA 1198
					jne .end25 ;if (..==..)
1199
						mov dword[val],1
1200
						jmp .cycle1end
1201
				.end25:
1202
				push ebx
1203
					mov ebx,[edi+deflate_state.gzindex]
1204
					add ebx,[edx+gz_header.name]
1205
					movzx ebx,byte[ebx]
1206
					mov [val],ebx
1207
					inc dword[edi+deflate_state.gzindex]
1208
					put_byte edi, bl
1209
				pop ebx
1210
				cmp dword[val],0
1211
				jne .cycle1 ;while (val != 0)
1212
			.cycle1end:
1213
			mov dword[edx+gz_header.hcrc],0
1214
			je @f
6741 IgorA 1215
			cmp [edi+deflate_state.pending],esi
6617 IgorA 1216
			jle @f ;if (.. && ..>..)
6741 IgorA 1217
				mov ecx,[edi+deflate_state.pending]
6617 IgorA 1218
				sub ecx,esi
1219
				mov eax,[edi+deflate_state.pending_buf]
1220
				add eax,esi
1221
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1222
				mov [ebx+z_stream.adler],eax
1223
			@@:
1224
			cmp dword[val],0
1225
			jne .end6 ;if (val == 0)
1226
				mov dword[edi+deflate_state.gzindex],0
1227
				mov dword[edi+deflate_state.status],COMMENT_STATE
1228
			jmp .end6
1229
		.end22: ;else
1230
			mov dword[edi+deflate_state.status],COMMENT_STATE;
1231
	.end6:
1232
	cmp dword[edi+deflate_state.status],COMMENT_STATE
1233
	jne .end7 ;if (..==..)
1234
		cmp dword[edx+gz_header.comment],Z_NULL
1235
		je .end23 ;if (..!=..)
6741 IgorA 1236
			mov esi,[edi+deflate_state.pending]
6617 IgorA 1237
			;esi = beg ;start of bytes to update crc
1238
 
1239
			.cycle2: ;do
6741 IgorA 1240
				mov eax,[edi+deflate_state.pending]
6617 IgorA 1241
				cmp eax,[edi+deflate_state.pending_buf_size]
1242
				jne .end26 ;if (..==..)
1243
					mov dword[edx+gz_header.hcrc],0
1244
					je @f
6741 IgorA 1245
					cmp [edi+deflate_state.pending],esi
6617 IgorA 1246
					jle @f ;if (.. && ..>..)
6741 IgorA 1247
						mov ecx,[edi+deflate_state.pending]
6617 IgorA 1248
						sub ecx,esi
1249
						mov eax,[edi+deflate_state.pending_buf]
1250
						add eax,esi
1251
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1252
						mov [ebx+z_stream.adler],eax
1253
					@@:
1254
					stdcall flush_pending, ebx
6741 IgorA 1255
					mov esi,[edi+deflate_state.pending]
1256
					cmp esi,[edi+deflate_state.pending_buf_size]
6617 IgorA 1257
					jne .end26 ;if (..==..)
1258
						mov dword[val],1
1259
						jmp .cycle2end
1260
				.end26:
1261
				push ebx
1262
					mov ebx,[edi+deflate_state.gzindex]
1263
					add ebx,[edx+gz_header.comment]
1264
					movzx ebx,byte[ebx]
1265
					mov [val],ebx
1266
					inc dword[edi+deflate_state.gzindex]
1267
					put_byte edi, bl
1268
				pop ebx
1269
				cmp dword[val],0
1270
				jne .cycle2 ;while (val != 0)
1271
			.cycle2end:
1272
			mov dword[edx+gz_header.hcrc],0
1273
			je @f
6741 IgorA 1274
			cmp [edi+deflate_state.pending],esi
6617 IgorA 1275
			jle @f ;if (.. && ..>..)
6741 IgorA 1276
				mov ecx,[edi+deflate_state.pending]
6617 IgorA 1277
				sub ecx,esi
1278
				mov eax,[edi+deflate_state.pending_buf]
1279
				add eax,esi
1280
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1281
				mov [ebx+z_stream.adler],eax
1282
			@@:
1283
			cmp dword[val],0
1284
			jne .end7 ;if (val == 0)
1285
				mov dword[edi+deflate_state.status],HCRC_STATE
1286
			jmp .end7
1287
		.end23: ;else
1288
			mov dword[edi+deflate_state.status],HCRC_STATE
1289
	.end7:
1290
	cmp dword[edi+deflate_state.status],HCRC_STATE
1291
	jne .end8 ;if (..==..)
1292
		cmp dword[edx+gz_header.hcrc],0
1293
		je .end9 ;if (..)
6741 IgorA 1294
			mov ecx,[edi+deflate_state.pending]
6617 IgorA 1295
			add ecx,2
1296
			cmp ecx,[edi+deflate_state.pending_buf_size]
1297
			jle @f ;if (..>..)
1298
				stdcall flush_pending, ebx
1299
			@@:
6741 IgorA 1300
			mov ecx,[edi+deflate_state.pending]
6617 IgorA 1301
			add ecx,2
1302
			cmp ecx,[edi+deflate_state.pending_buf_size]
1303
			jg @f ;if (..<=..)
1304
				mov ecx,[ebx+z_stream.adler]
1305
				put_byte edi, cl
1306
				put_byte edi, ch
6639 IgorA 1307
				xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 1308
				mov [ebx+z_stream.adler],eax
1309
				mov dword[edi+deflate_state.status],BUSY_STATE
1310
			@@:
1311
			jmp .end8
1312
		.end9: ;else
1313
			mov dword[edi+deflate_state.status],BUSY_STATE
1314
	.end8:
1315
end if
1316
 
1317
	; Flush as much pending output as possible
6741 IgorA 1318
	cmp dword[edi+deflate_state.pending],0
6617 IgorA 1319
	je .end13 ;if (..!=0)
1320
		stdcall flush_pending, ebx
1321
		cmp word[ebx+z_stream.avail_out],0
1322
		jne @f ;if (..==0)
1323
			; Since avail_out is 0, deflate will be called again with
1324
			; more output space, but possibly with both pending and
1325
			; avail_in equal to zero. There won't be anything to do,
1326
			; but this is not an error situation so make sure we
1327
			; return OK instead of BUF_ERROR at next call of deflate:
1328
 
1329
			mov dword[edi+deflate_state.last_flush],-1
1330
			mov eax,Z_OK
1331
			jmp .end_f
1332
		@@:
1333
		; Make sure there is something to do and avoid duplicate consecutive
1334
		; flushes. For repeated and useless calls with Z_FINISH, we keep
1335
		; returning Z_STREAM_END instead of Z_BUF_ERROR.
1336
		jmp @f
1337
	.end13:
6704 IgorA 1338
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1339
	jne @f
1340
	RANK dword[old_flush],esi
1341
	RANK dword[flush],eax
1342
	cmp eax,esi
1343
	jg @f
1344
	cmp dword[flush],Z_FINISH
1345
	je @f ;else if (..==0 && ..<=.. && ..!=..)
1346
		ERR_RETURN ebx, Z_BUF_ERROR
1347
		jmp .end_f
1348
	@@:
1349
 
1350
	; User must not provide more input after the first FINISH:
1351
	cmp dword[edi+deflate_state.status],FINISH_STATE
1352
	jne @f
6704 IgorA 1353
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1354
	je @f ;if (..==.. && ..!=0)
1355
		ERR_RETURN ebx, Z_BUF_ERROR
1356
		jmp .end_f
1357
	@@:
1358
 
1359
	; Start a new block or continue the current one.
1360
 
6704 IgorA 1361
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1362
	jne @f
1363
	cmp dword[edi+deflate_state.lookahead],0
1364
	jne @f
1365
	cmp dword[flush],Z_NO_FLUSH
1366
	je .end11
1367
	cmp dword[edi+deflate_state.status],FINISH_STATE
1368
	je .end11
1369
	@@: ;if (..!=0 || ..!=0 || (..!=.. && ..!=..))
1370
		;edx = bstate
1371
		cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
1372
		jne @f
1373
			stdcall deflate_huff, edi, [flush]
1374
			jmp .end20
1375
		@@:
1376
		cmp word[edi+deflate_state.strategy],Z_RLE
1377
		jne @f
1378
			stdcall deflate_rle, edi, [flush]
1379
			jmp .end20
1380
		@@:
1381
		movzx eax,word[edi+deflate_state.level]
1382
		imul eax,sizeof.config_s
1383
		add eax,configuration_table+config_s.co_func
1384
		stdcall dword[eax], edi, [flush]
1385
		.end20:
1386
		mov edx,eax
1387
 
1388
		cmp edx,finish_started
1389
		je @f
1390
		cmp edx,finish_done
6652 IgorA 1391
		jne .end18
6617 IgorA 1392
		@@: ;if (..==.. || ..==..)
1393
			mov dword[edi+deflate_state.status],FINISH_STATE
1394
		.end18:
1395
		cmp edx,need_more
1396
		je @f
1397
		cmp edx,finish_started
6652 IgorA 1398
		jne .end19
6617 IgorA 1399
		@@: ;if (..==.. || ..==..)
1400
			cmp word[ebx+z_stream.avail_out],0
1401
			jne @f ;if (..==0)
1402
				mov dword[edi+deflate_state.last_flush],-1 ;avoid BUF_ERROR next call, see above
1403
			@@:
1404
			mov eax,Z_OK
1405
			jmp .end_f
1406
			; If flush != Z_NO_FLUSH && avail_out == 0, the next call
1407
			; of deflate should use the same flush parameter to make sure
1408
			; that the flush is complete. So we don't have to output an
1409
			; empty block here, this will be done at next call. This also
1410
			; ensures that for a very small output buffer, we emit at most
1411
			; one empty block.
1412
 
1413
		.end19:
1414
		cmp edx,block_done
1415
		jne .end11 ;if (..==..)
1416
			cmp dword[flush],Z_PARTIAL_FLUSH
1417
			jne @f ;if (..==..)
1418
				stdcall _tr_align, edi
1419
				jmp .end16
1420
			@@:
1421
			cmp dword[flush],Z_BLOCK
1422
			je .end16 ;else if (..!=..) ;FULL_FLUSH or SYNC_FLUSH
1423
				stdcall _tr_stored_block, edi, 0, 0, 0
1424
				; For a full flush, this empty block will be recognized
1425
				; as a special marker by inflate_sync().
1426
 
1427
			cmp dword[flush],Z_FULL_FLUSH
1428
			jne .end16 ;if (..==..)
1429
				CLEAR_HASH edi ;forget history
1430
				cmp dword[edi+deflate_state.lookahead],0
1431
				jne .end16 ;if (..==0)
1432
					mov dword[edi+deflate_state.strstart],0
1433
					mov dword[edi+deflate_state.block_start],0
1434
					mov dword[edi+deflate_state.insert],0
1435
		.end16:
1436
		stdcall flush_pending, ebx
1437
		cmp word[ebx+z_stream.avail_out],0
1438
		jne .end11 ;if (..==0)
1439
			mov dword[edi+deflate_state.last_flush],-1 ;avoid BUF_ERROR at next call, see above
1440
			mov eax,Z_OK
1441
			jmp .end_f
1442
	.end11:
1443
	cmp word[ebx+z_stream.avail_out],0
1444
	jg @f
6639 IgorA 1445
		zlib_assert 'bug2' ;Assert(..>0)
6617 IgorA 1446
	@@:
1447
 
1448
	cmp dword[flush],Z_FINISH
1449
	je @f ;if (..!=0)
1450
		mov eax,Z_OK
1451
		jmp .end_f
1452
	@@:
1453
	cmp dword[edi+deflate_state.wrap],0
1454
	jg @f ;if (..<=0)
1455
		mov eax,Z_STREAM_END
1456
		jmp .end_f
1457
	@@:
1458
 
1459
	; Write the trailer
1460
if GZIP eq 1
1461
	cmp dword[edi+deflate_state.wrap],2
1462
	jne @f ;if (..==..)
1463
		mov ecx,[ebx+z_stream.adler]
1464
		put_dword edi, ecx
1465
		mov ecx,[ebx+z_stream.total_in]
1466
		put_dword edi, ecx
1467
		jmp .end17
1468
	@@: ;else
1469
end if
1470
		mov ecx,[ebx+z_stream.adler]
1471
		bswap ecx
1472
		put_dword edi, ecx
1473
	.end17:
1474
	stdcall flush_pending, ebx
1475
	; If avail_out is zero, the application will call deflate again
1476
	; to flush the rest.
1477
 
6741 IgorA 1478
	cmp dword[edi+deflate_state.pending],0
6617 IgorA 1479
	jle @f ;if (..>0) ;write the trailer only once!
6741 IgorA 1480
		neg dword[edi+deflate_state.pending]
1481
		inc dword[edi+deflate_state.pending]
6617 IgorA 1482
	@@:
1483
	mov eax,Z_OK
6741 IgorA 1484
	cmp dword[edi+deflate_state.pending],0
6617 IgorA 1485
	je .end_f
1486
		mov eax,Z_STREAM_END
1487
.end_f:
1488
zlib_debug '  deflate.ret = %d',eax
1489
	ret
1490
endp
1491
 
1492
; =========================================================================
1493
;int (strm)
6639 IgorA 1494
;    z_streamp strm
6617 IgorA 1495
align 4
1496
proc deflateEnd uses ebx ecx edx, strm:dword
1497
	mov ebx,[strm]
1498
zlib_debug 'deflateEnd'
1499
	cmp ebx,Z_NULL
1500
	je @f
1501
	mov edx,[ebx+z_stream.state]
1502
	cmp edx,Z_NULL
1503
	jne .end0
1504
	@@: ;if (..==0 || ..==0) return ..
1505
		mov eax,Z_STREAM_ERROR
1506
		jmp .end_f
1507
	.end0:
1508
 
1509
	mov ecx,[edx+deflate_state.status]
1510
	cmp ecx,INIT_STATE
1511
	je @f
1512
	cmp ecx,EXTRA_STATE
1513
	je @f
1514
	cmp ecx,NAME_STATE
1515
	je @f
1516
	cmp ecx,COMMENT_STATE
1517
	je @f
1518
	cmp ecx,HCRC_STATE
1519
	je @f
1520
	cmp ecx,BUSY_STATE
1521
	je @f
1522
	cmp ecx,FINISH_STATE
1523
	je @f ;if (..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=..)
1524
		mov eax,Z_STREAM_ERROR
1525
		jmp .end_f
1526
	@@:
1527
 
1528
	; Deallocate in reverse order of allocations:
1529
	TRY_FREE ebx, dword[edx+deflate_state.pending_buf]
1530
	TRY_FREE ebx, dword[edx+deflate_state.head]
1531
	TRY_FREE ebx, dword[edx+deflate_state.prev]
1532
	TRY_FREE ebx, dword[edx+deflate_state.window]
1533
 
1534
	ZFREE ebx, dword[ebx+z_stream.state]
1535
	mov dword[ebx+z_stream.state],Z_NULL
1536
 
1537
	mov eax,Z_DATA_ERROR
1538
	cmp ecx,BUSY_STATE
1539
	je .end_f
1540
		mov eax,Z_OK
1541
.end_f:
1542
	ret
1543
endp
1544
 
1545
; =========================================================================
1546
; Copy the source state to the destination state.
1547
; To simplify the source, this is not supported for 16-bit MSDOS (which
1548
; doesn't have enough memory anyway to duplicate compression states).
1549
 
1550
;int (dest, source)
6639 IgorA 1551
;    z_streamp dest
1552
;    z_streamp source
6617 IgorA 1553
align 4
6639 IgorA 1554
proc deflateCopy uses ebx edx edi esi, dest:dword, source:dword
1555
;ebx = overlay ;uint_16p
1556
;edi = ds ;deflate_state*
1557
;esi = ss ;deflate_state*
6617 IgorA 1558
 
1559
	mov esi,[source]
1560
	cmp esi,Z_NULL
1561
	je @f
1562
	mov edx,[dest]
1563
	cmp edx,Z_NULL
1564
	je @f
1565
	mov esi,[esi+z_stream.state]
1566
	cmp esi,Z_NULL
1567
	jne .end0
1568
	@@: ;if (..==0 || ..==0 || ..==0)
1569
		mov eax,Z_STREAM_ERROR
1570
		jmp .end_f
1571
	.end0:
1572
 
1573
	stdcall zmemcpy, edx, [source], sizeof.z_stream
1574
 
1575
	ZALLOC edx, 1, sizeof.deflate_state
1576
	cmp eax,0
1577
	jne @f ;if (..==0) return ..
1578
		mov eax,Z_MEM_ERROR
1579
		jmp .end_f
1580
	@@:
1581
	mov edi,eax
1582
	mov [edx+z_stream.state],eax
1583
	stdcall zmemcpy, edi, esi, sizeof.deflate_state
1584
	mov dword[edi+deflate_state.strm],edx
1585
 
1586
	ZALLOC edx, [edi+deflate_state.w_size], 2 ;2*sizeof.db
1587
	mov dword[edi+deflate_state.window],eax
1588
	ZALLOC edx, [edi+deflate_state.w_size], 4 ;sizeof.dd
1589
	mov dword[edi+deflate_state.prev],eax
1590
	ZALLOC edx, [edi+deflate_state.hash_size], 4 ;sizeof.dd
1591
	mov dword[edi+deflate_state.head],eax
1592
	ZALLOC edx, [edi+deflate_state.lit_bufsize], 4 ;sizeof.dw+2
6639 IgorA 1593
	mov ebx,eax
6617 IgorA 1594
	mov dword[edi+deflate_state.pending_buf],eax
1595
 
1596
	cmp dword[edi+deflate_state.window],Z_NULL
1597
	je @f
1598
	cmp dword[edi+deflate_state.prev],Z_NULL
1599
	je @f
1600
	cmp dword[edi+deflate_state.head],Z_NULL
1601
	je @f
1602
	cmp dword[edi+deflate_state.pending_buf],Z_NULL
1603
	jne .end1
1604
	@@: ;if (..==0 || ..==0 || ..==0 || ..==0)
1605
		stdcall deflateEnd, edx
1606
		mov eax,Z_MEM_ERROR
1607
		jmp .end_f
1608
	.end1:
1609
 
1610
	; following zmemcpy do not work for 16-bit MSDOS
1611
	mov eax,[edi+deflate_state.w_size]
1612
	shl eax,1 ;*= 2*sizeof.db
1613
	stdcall zmemcpy, [edi+deflate_state.window], [esi+deflate_state.window], eax
6639 IgorA 1614
	mov eax,[edi+deflate_state.w_size]
1615
	shl eax,2 ;*= sizeof.dd
1616
	stdcall zmemcpy, [edi+deflate_state.prev], [esi+deflate_state.prev], eax
1617
	mov eax,[edi+deflate_state.hash_size]
1618
	shl eax,2 ;*= sizeof.dd
1619
	stdcall zmemcpy, [edi+deflate_state.head], [esi+deflate_state.head], eax
1620
	stdcall zmemcpy, [edi+deflate_state.pending_buf], [esi+deflate_state.pending_buf], [edi+deflate_state.pending_buf_size]
6617 IgorA 1621
 
6639 IgorA 1622
	mov eax,[edi+deflate_state.pending_buf]
1623
	add eax,[esi+deflate_state.pending_out]
1624
	sub eax,[esi+deflate_state.pending_buf]
1625
	mov [edi+deflate_state.pending_out],eax
1626
	mov eax,[edi+deflate_state.lit_bufsize]
1627
	shr eax,1 ;/=sizeof.uint_16
1628
	add eax,ebx
1629
	mov [edi+deflate_state.d_buf],eax
1630
	mov eax,[edi+deflate_state.lit_bufsize]
1631
	imul eax,3 ;*=1+sizeof.uint_16
1632
	add eax,[edi+deflate_state.pending_buf]
1633
	mov [edi+deflate_state.l_buf],eax
6617 IgorA 1634
 
1635
	mov eax,edi
1636
	add eax,deflate_state.dyn_ltree
1637
	mov [edi+deflate_state.l_desc.dyn_tree],eax
1638
	add eax,deflate_state.dyn_dtree-deflate_state.dyn_ltree
1639
	mov [edi+deflate_state.d_desc.dyn_tree],eax
1640
	add eax,deflate_state.bl_tree-deflate_state.dyn_dtree
1641
	mov [edi+deflate_state.bl_desc.dyn_tree],eax
1642
 
1643
	mov eax,Z_OK
1644
.end_f:
1645
	ret
1646
endp
1647
 
1648
; ===========================================================================
1649
; Read a new buffer from the current input stream, update the adler32
1650
; and total number of bytes read.  All deflate() input goes through
1651
; this function so some applications may wish to modify it to avoid
1652
; allocating a large strm->next_in buffer and copying from it.
1653
; (See also flush_pending()).
1654
 
1655
;int (strm, buf, size)
6639 IgorA 1656
;    z_streamp strm
1657
;    Bytef *buf
1658
;    unsigned size
6617 IgorA 1659
align 4
1660
proc read_buf uses ebx ecx, strm:dword, buf:dword, size:dword
1661
	mov ebx,[strm]
6704 IgorA 1662
	mov eax,[ebx+z_stream.avail_in]
6617 IgorA 1663
 
1664
	cmp eax,[size]
1665
	jle @f ;if (..>..)
1666
		mov eax,[size]
1667
	@@:
1668
	cmp eax,0
1669
	jg @f
1670
		xor eax,eax
1671
		jmp .end_f ;if (..==0) return 0
1672
	@@:
1673
 
6704 IgorA 1674
	sub [ebx+z_stream.avail_in],eax
6617 IgorA 1675
 
1676
	stdcall zmemcpy, [buf],[ebx+z_stream.next_in],eax
1677
	mov ecx,[ebx+z_stream.state]
1678
	cmp [ecx+deflate_state.wrap],1
1679
	jne @f ;if (..==..)
1680
		push eax
1681
		stdcall adler32, [ebx+z_stream.adler], [buf], eax
1682
		mov [ebx+z_stream.adler],eax
1683
		pop eax
1684
		jmp .end0
1685
	@@:
1686
if GZIP eq 1
1687
	cmp [ecx+deflate_state.wrap],2
1688
	jne .end0 ;else if (..==..)
1689
		push eax
1690
		stdcall calc_crc32, [ebx+z_stream.adler], [buf], eax
1691
		mov [ebx+z_stream.adler],eax
1692
		pop eax
1693
end if
1694
	.end0:
1695
	add [ebx+z_stream.next_in],eax
1696
	add [ebx+z_stream.total_in],eax
1697
 
1698
.end_f:
1699
;zlib_debug '  read_buf.ret = %d',eax
1700
	ret
1701
endp
1702
 
1703
; ===========================================================================
1704
; Initialize the "longest match" routines for a new zlib stream
1705
 
1706
;void (s)
1707
;    deflate_state *s
1708
align 4
1709
proc lm_init uses eax ebx edi, s:dword
1710
	mov edi,[s]
1711
	mov eax,[edi+deflate_state.w_size]
1712
	shl eax,1
1713
	mov [edi+deflate_state.window_size],eax
1714
 
1715
	CLEAR_HASH edi
1716
 
1717
	; Set the default configuration parameters:
1718
 
1719
	movzx eax,word[edi+deflate_state.level]
1720
	imul eax,sizeof.config_s
1721
	add eax,configuration_table
1722
	movzx ebx,word[eax+config_s.max_lazy]
1723
	mov [edi+deflate_state.max_lazy_match],ebx
1724
	movzx ebx,word[eax+config_s.good_length]
1725
	mov [edi+deflate_state.good_match],ebx
1726
	movzx ebx,word[eax+config_s.nice_length]
1727
	mov [edi+deflate_state.nice_match],ebx
1728
	movzx ebx,word[eax+config_s.max_chain]
1729
	mov [edi+deflate_state.max_chain_length],ebx
1730
 
1731
	mov dword[edi+deflate_state.strstart],0
1732
	mov dword[edi+deflate_state.block_start],0
1733
	mov dword[edi+deflate_state.lookahead],0
1734
	mov dword[edi+deflate_state.insert],0
1735
	mov dword[edi+deflate_state.prev_length],MIN_MATCH-1
1736
	mov dword[edi+deflate_state.match_length],MIN_MATCH-1
1737
	mov dword[edi+deflate_state.match_available],0
1738
	mov dword[edi+deflate_state.ins_h],0
1739
if FASTEST eq 0
1740
;if ASMV
1741
;    call match_init ;initialize the asm code
1742
;end if
1743
end if
1744
	ret
1745
endp
1746
 
1747
;uInt (s, cur_match)
6639 IgorA 1748
;    deflate_state *s
1749
;    IPos cur_match ;current match
6617 IgorA 1750
align 4
1751
proc longest_match uses ebx ecx edx edi esi, s:dword, cur_match:dword
1752
if FASTEST eq 0
1753
; ===========================================================================
1754
; Set match_start to the longest match starting at the given string and
1755
; return its length. Matches shorter or equal to prev_length are discarded,
1756
; in which case the result is equal to prev_length and match_start is
1757
; garbage.
1758
; IN assertions: cur_match is the head of the hash chain for the current
1759
;   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1760
; OUT assertion: the match length is not greater than s->lookahead.
1761
 
1762
;#ifndef ASMV
1763
; For 80x86 and 680x0, an optimized version will be provided in match.asm or
1764
; match.S. The code will be functionally equivalent.
1765
 
1766
;    unsigned chain_length = s->max_chain_length;/* max hash chain length */
1767
;    register Bytef *scan = s->window + s->strstart; /* current string */
1768
;    register Bytef *match;                       /* matched string */
1769
;    register int len;                           /* length of current match */
1770
;    int best_len = s->prev_length;              /* best match length so far */
1771
;    int nice_match = s->nice_match;             /* stop if match long enough */
1772
;    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1773
;        s->strstart - (IPos)MAX_DIST(s) : NIL;
1774
	; Stop when cur_match becomes <= limit. To simplify the code,
1775
	; we prevent matches with the string of window index 0.
1776
 
1777
;    Posf *prev = s->prev;
1778
;    uInt wmask = s->w_mask;
1779
 
1780
;    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1781
;    register Byte scan_end1  = scan[best_len-1];
1782
;    register Byte scan_end   = scan[best_len];
1783
 
1784
	; The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1785
	; It is easy to get rid of this optimization if necessary.
1786
 
1787
;    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1788
 
1789
	; Do not waste too much time if we already have a good match:
1790
;    if (s->prev_length >= s->good_match) {
1791
;        chain_length >>= 2;
1792
;    }
1793
	; Do not look for matches beyond the end of the input. This is necessary
1794
	; to make deflate deterministic.
1795
 
1796
;    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1797
 
1798
;    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1799
 
1800
;    do {
1801
;        Assert(cur_match < s->strstart, "no future");
1802
;        match = s->window + cur_match;
1803
 
1804
	; Skip to next match if the match length cannot increase
1805
	; or if the match length is less than 2.  Note that the checks below
1806
	; for insufficient lookahead only occur occasionally for performance
1807
	; reasons.  Therefore uninitialized memory will be accessed, and
1808
	; conditional jumps will be made that depend on those values.
1809
	; However the length of the match is limited to the lookahead, so
1810
	; the output of deflate is not affected by the uninitialized values.
1811
 
1812
;        if (match[best_len]   != scan_end  ||
1813
;            match[best_len-1] != scan_end1 ||
1814
;            *match            != *scan     ||
1815
;            *++match          != scan[1])      continue;
1816
 
1817
	; The check at best_len-1 can be removed because it will be made
1818
	; again later. (This heuristic is not always a win.)
1819
	; It is not necessary to compare scan[2] and match[2] since they
1820
	; are always equal when the other bytes match, given that
1821
	; the hash keys are equal and that HASH_BITS >= 8.
1822
 
1823
;        scan += 2, match++;
1824
;        Assert(*scan == *match, "match[2]?");
1825
 
1826
	; We check for insufficient lookahead only every 8th comparison;
1827
	; the 256th check will be made at strstart+258.
1828
 
1829
;        do {
1830
;        } while (*++scan == *++match && *++scan == *++match &&
1831
;                 *++scan == *++match && *++scan == *++match &&
1832
;                 *++scan == *++match && *++scan == *++match &&
1833
;                 *++scan == *++match && *++scan == *++match &&
1834
;                 scan < strend);
1835
 
1836
;        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1837
 
1838
;        len = MAX_MATCH - (int)(strend - scan);
1839
;        scan = strend - MAX_MATCH;
1840
 
1841
;        if (len > best_len) {
1842
;            s->match_start = cur_match;
1843
;            best_len = len;
1844
;            if (len >= nice_match) break;
1845
;            scan_end1  = scan[best_len-1];
1846
;            scan_end   = scan[best_len];
1847
;        }
1848
;    } while ((cur_match = prev[cur_match & wmask]) > limit
1849
;             && --chain_length != 0);
1850
 
1851
;    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1852
;    return s->lookahead;
1853
;end if /* ASMV */
1854
 
1855
else ;FASTEST
1856
 
1857
; ---------------------------------------------------------------------------
1858
; Optimized version for FASTEST only
1859
	mov edx,[s]
6639 IgorA 1860
	zlib_debug 'longest_match'
6617 IgorA 1861
 
1862
	; The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1863
	; It is easy to get rid of this optimization if necessary.
1864
 
1865
if MAX_MATCH <> 258
1866
	cmp dword[edx+deflate_state.hash_bits],8
1867
	jge @f
6639 IgorA 1868
		zlib_assert 'Code too clever' ;Assert(..>=.. && ..==..)
6617 IgorA 1869
	@@:
1870
end if
1871
	mov eax,[edx+deflate_state.window_size]
1872
	sub eax,MIN_LOOKAHEAD
1873
	cmp [edx+deflate_state.strstart],eax
1874
	jle @f
6639 IgorA 1875
		zlib_assert 'need lookahead' ;Assert(..<=..)
6617 IgorA 1876
	@@:
1877
	mov eax,[edx+deflate_state.strstart]
1878
	cmp [cur_match],eax
1879
	jl @f
6639 IgorA 1880
		zlib_assert 'no future' ;Assert(..<..)
6617 IgorA 1881
	@@:
1882
 
1883
	mov esi,[edx+deflate_state.window]
1884
	mov edi,esi
1885
	add esi,[cur_match]
1886
	add edi,[edx+deflate_state.strstart]
1887
	;edi = scan
1888
	;esi = match
1889
 
1890
	; Return failure if the match length is less than 2:
1891
 
1892
	lodsw
1893
	cmp ax,word[edi]
1894
	je @f ;if (word[edi] != word[esi]) return
1895
		mov eax,MIN_MATCH-1
1896
		jmp .end_f
1897
	@@:
1898
 
1899
	; The check at best_len-1 can be removed because it will be made
1900
	; again later. (This heuristic is not always a win.)
1901
	; It is not necessary to compare scan[2] and match[2] since they
1902
	; are always equal when the other bytes match, given that
1903
	; the hash keys are equal and that HASH_BITS >= 8.
1904
 
1905
	add edi,2
1906
	mov al,byte[edi]
1907
	cmp al,byte[esi]
1908
	je @f
6639 IgorA 1909
		zlib_assert 'match[2]?' ;Assert(..==..)
6617 IgorA 1910
	@@:
1911
 
1912
	; We check for insufficient lookahead only every 8th comparison;
1913
	; the 256th check will be made at strstart+258.
1914
 
1915
	mov ebx,edi
1916
	mov ecx,MAX_MATCH
1917
align 4
1918
	@@:
1919
		lodsb
1920
		scasb
1921
		loope @b
1922
 
1923
	mov eax,[edx+deflate_state.window_size]
1924
	dec eax
1925
	add eax,[edx+deflate_state.window]
1926
	cmp edi,eax
1927
	jle @f
6639 IgorA 1928
		zlib_assert 'wild scan' ;Assert(..<=..)
6617 IgorA 1929
	@@:
1930
	sub edi,ebx
1931
	;edi = len
1932
 
1933
	cmp edi,MIN_MATCH
1934
	jge @f ;if (..<..)
1935
		mov eax,MIN_MATCH-1
1936
		jmp .end_f
1937
	@@:
1938
	mov eax,[cur_match]
1939
	mov [edx+deflate_state.match_start],eax
1940
	mov eax,[edx+deflate_state.lookahead]
1941
	cmp edi,eax
1942
	jg @f ;if (len <= s.lookahead) ? len : s.lookahead
1943
		mov eax,edi
1944
	@@:
1945
end if ;FASTEST
1946
.end_f:
1947
;zlib_debug '  longest_match.ret = %d',eax
1948
	ret
1949
endp
1950
 
1951
 
1952
; ===========================================================================
1953
; Check that the match at match_start is indeed a match.
1954
 
1955
;void (s, start, match, length)
6639 IgorA 1956
;    deflate_state *s
1957
;    IPos start, match
1958
;    int length
6617 IgorA 1959
align 4
1960
proc check_match, s:dword, start:dword, p3match:dword, length:dword
1961
if DEBUG eq 1
1962
	; check that the match is indeed a match
1963
;    if (zmemcmp(s->window + match,
1964
;                s->window + start, length) != EQUAL) {
1965
;        fprintf(stderr, " start %u, match %u, length %d\n",
1966
;                start, match, length);
1967
;        do {
1968
;            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1969
;        } while (--length != 0);
1970
;        z_error("invalid match");
1971
;    }
1972
;    if (z_verbose > 1) {
1973
;        fprintf(stderr,"\\[%d,%d]", start-match, length);
1974
;        do { putc(s->window[start++], stderr); } while (--length != 0);
1975
;    }
1976
end if ;DEBUG
1977
	ret
1978
endp
1979
 
1980
 
1981
; ===========================================================================
1982
; Fill the window when the lookahead becomes insufficient.
1983
; Updates strstart and lookahead.
1984
 
1985
; IN assertion: lookahead < MIN_LOOKAHEAD
1986
; OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1987
;    At least one byte has been read, or avail_in == 0; reads are
1988
;    performed for at least two bytes (required for the zip translate_eol
1989
;    option -- not supported here).
1990
 
1991
;void (s)
1992
;    deflate_state *s
1993
align 4
1994
proc fill_window, s:dword
1995
pushad
1996
;esi = p, str, curr
1997
;ebx = more ;Amount of free space at the end of the window.
1998
	;Объем свободного пространства в конце окна.
1999
;ecx = wsize ;uInt
2000
;edx = s.strm
6639 IgorA 2001
	zlib_debug 'fill_window'
6617 IgorA 2002
	mov edi,[s]
2003
	cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2004
	jl @f
6639 IgorA 2005
		zlib_assert 'already enough lookahead' ;Assert(..<..)
6617 IgorA 2006
	@@:
2007
 
2008
	mov ecx,[edi+deflate_state.w_size]
2009
	mov edx,[edi+deflate_state.strm]
2010
	.cycle0: ;do
6639 IgorA 2011
	zlib_debug 'do'
6617 IgorA 2012
		mov ebx,[edi+deflate_state.window_size]
2013
		sub ebx,[edi+deflate_state.lookahead]
2014
		sub ebx,[edi+deflate_state.strstart]
2015
 
2016
		; If the window is almost full and there is insufficient lookahead,
2017
		; move the upper half to the lower one to make room in the upper half.
2018
 
2019
		MAX_DIST edi
2020
		add eax,ecx
2021
		cmp [edi+deflate_state.strstart],eax
2022
		jl .end0 ;if (..>=..)
2023
			push ecx
2024
			mov eax,[edi+deflate_state.window]
2025
			add eax,ecx
2026
			stdcall zmemcpy, [edi+deflate_state.window], eax
2027
			sub [edi+deflate_state.match_start],ecx
2028
			sub [edi+deflate_state.strstart],ecx ;we now have strstart >= MAX_DIST
2029
			sub [edi+deflate_state.block_start],ecx
2030
 
2031
			; Slide the hash table (could be avoided with 32 bit values
2032
			; at the expense of memory usage). We slide even when level == 0
2033
			; to keep the hash table consistent if we switch back to level > 0
2034
			; later. (Using level 0 permanently is not an optimal usage of
2035
			; zlib, so we don't care about this pathological case.)
2036
 
2037
			push ebx ecx
2038
			;ebx = wsize
2039
			;ecx = n
2040
			mov ebx,ecx
2041
			mov ecx,[edi+deflate_state.hash_size]
2042
			mov esi,ecx
2043
			shl esi,2
2044
			add esi,[edi+deflate_state.head]
2045
			.cycle1: ;do
2046
				sub esi,4
2047
				mov eax,[esi]
2048
				mov dword[esi],NIL
2049
				cmp eax,ebx
2050
				jl @f
2051
					sub eax,ebx
2052
					mov dword[esi],eax
2053
				@@:
2054
			loop .cycle1 ;while (..)
2055
 
2056
			mov ecx,ebx
2057
if FASTEST eq 0
2058
			mov esi,ecx
2059
			shl esi,2
2060
			add esi,[edi+deflate_state.prev]
2061
			.cycle2: ;do
2062
				sub esi,4
2063
				mov eax,[esi]
2064
				mov dword[esi],NIL
2065
				cmp eax,ebx
2066
				jl @f
2067
					sub eax,ebx
2068
					mov dword[esi],eax
2069
				@@:
2070
				; If n is not on any hash chain, prev[n] is garbage but
2071
				; its value will never be used.
2072
 
2073
			loop .cycle2 ;while (..)
2074
end if
2075
			pop ecx ebx
2076
			add ebx,ecx
2077
		.end0:
6704 IgorA 2078
		cmp dword[edx+z_stream.avail_in],0
6617 IgorA 2079
		je .cycle0end ;if (..==0) break
2080
 
2081
		; If there was no sliding:
2082
		;    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
2083
		;    more == window_size - lookahead - strstart
2084
		; => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
2085
		; => more >= window_size - 2*WSIZE + 2
2086
		; In the BIG_MEM or MMAP case (not yet supported),
2087
		;   window_size == input_size + MIN_LOOKAHEAD  &&
2088
		;   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
2089
		; Otherwise, window_size == 2*WSIZE so more >= 2.
2090
		; If there was sliding, more >= WSIZE. So in all cases, more >= 2.
2091
 
2092
		cmp ebx,2
2093
		jge @f
6639 IgorA 2094
			zlib_assert 'more < 2' ;Assert(..>=..)
6617 IgorA 2095
		@@:
2096
		mov eax,[edi+deflate_state.window]
2097
		add eax,[edi+deflate_state.strstart]
2098
		add eax,[edi+deflate_state.lookahead]
2099
		stdcall read_buf, edx, eax, ebx
2100
		add [edi+deflate_state.lookahead],eax
2101
 
2102
		; Initialize the hash value now that we have some input:
2103
		mov eax,[edi+deflate_state.lookahead]
2104
		add eax,[edi+deflate_state.insert]
2105
		cmp eax,MIN_MATCH
2106
		jl .end1 ;if (..>=..)
2107
			mov esi,[edi+deflate_state.strstart]
2108
			sub esi,[edi+deflate_state.insert]
2109
			;esi = str
2110
			mov eax,[edi+deflate_state.window]
2111
			add eax,esi
2112
			mov [edi+deflate_state.ins_h],eax
2113
			inc eax
2114
			movzx eax,byte[eax]
2115
            UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2116
if MIN_MATCH <> 3
2117
;            Call UPDATE_HASH() MIN_MATCH-3 more times
2118
end if
2119
			.cycle3: ;while (..)
2120
			cmp dword[edi+deflate_state.insert],0
2121
			je .end1
2122
				mov eax,esi
2123
				add eax,MIN_MATCH-1
2124
				add eax,[edi+deflate_state.window]
2125
				movzx eax,byte[eax]
2126
				UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2127
if FASTEST eq 0
2128
				mov eax,[edi+deflate_state.ins_h]
2129
				shl eax,2
2130
				add eax,[edi+deflate_state.head]
2131
				push ebx
2132
				mov ebx,[edi+deflate_state.w_mask]
2133
				and ebx,esi
2134
				shl ebx,2
2135
				add ebx,[edi+deflate_state.prev]
2136
				mov eax,[eax]
2137
				mov [ebx],eax
2138
				pop ebx
2139
end if
2140
				mov eax,[edi+deflate_state.ins_h]
2141
				shl eax,2
2142
				add eax,[edi+deflate_state.head]
2143
				mov [eax],esi
2144
				inc esi
2145
				dec dword[edi+deflate_state.insert]
2146
				mov eax,[edi+deflate_state.lookahead]
2147
				add eax,[edi+deflate_state.insert]
2148
				cmp eax,MIN_MATCH
2149
				jl .end1 ;if (..<..) break
2150
			jmp .cycle3
2151
		.end1:
2152
		; If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
2153
		; but this is not important since only literal bytes will be emitted.
2154
 
2155
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2156
		jge .cycle0end
6704 IgorA 2157
		cmp dword[edx+z_stream.avail_in],0
6617 IgorA 2158
		jne .cycle0
2159
	.cycle0end: ;while (..<.. && ..!=..)
2160
 
2161
	; If the WIN_INIT bytes after the end of the current data have never been
2162
	; written, then zero those bytes in order to avoid memory check reports of
2163
	; the use of uninitialized (or uninitialised as Julian writes) bytes by
2164
	; the longest match routines.  Update the high water mark for the next
2165
	; time through here.  WIN_INIT is set to MAX_MATCH since the longest match
2166
	; routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
2167
 
2168
	mov eax,[edi+deflate_state.window_size]
2169
	cmp [edi+deflate_state.high_water],eax
2170
	jge .end2 ;if (..<..)
2171
		mov esi,[edi+deflate_state.lookahead]
2172
		add esi,[edi+deflate_state.strstart]
2173
		;esi = curr
2174
 
2175
		cmp [edi+deflate_state.high_water],esi
2176
		jge .end3 ;if (..<..)
2177
			; Previous high water mark below current data -- zero WIN_INIT
2178
			; bytes or up to end of window, whichever is less.
2179
 
2180
			mov eax,[edi+deflate_state.window_size]
2181
			sub eax,esi
2182
			cmp eax,WIN_INIT
2183
			jle @f ;if (..>..)
2184
				mov eax,WIN_INIT
2185
			@@:
2186
			mov edx,[edi+deflate_state.window]
2187
			add edx,esi
2188
			stdcall zmemzero, edx, eax
2189
			add eax,esi
2190
			mov [edi+deflate_state.high_water],eax
2191
			jmp .end2
2192
		.end3: ;else if (..<..)
2193
		mov eax,esi
2194
		add eax,WIN_INIT
2195
		cmp [edi+deflate_state.high_water],eax
2196
		jge .end2
2197
			; High water mark at or above current data, but below current data
2198
			; plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
2199
			; to end of window, whichever is less.
2200
 
2201
			;eax = esi+WIN_INIT
2202
			sub eax,[edi+deflate_state.high_water]
2203
			mov edx,[edi+deflate_state.window_size]
2204
			sub edx,[edi+deflate_state.high_water]
2205
			cmp eax,edx ;if (..>..)
2206
			jle @f
2207
				mov eax,edx
2208
			@@:
2209
			mov edx,[edi+deflate_state.window]
2210
			add edx,[edi+deflate_state.high_water]
2211
			stdcall zmemzero, edx, eax
2212
			add [edi+deflate_state.high_water],eax
2213
	.end2:
2214
 
2215
	mov eax,[edi+deflate_state.window_size]
2216
	sub eax,MIN_LOOKAHEAD
2217
	cmp [edi+deflate_state.strstart],eax
2218
	jle @f
6639 IgorA 2219
		zlib_assert 'not enough room for search' ;Assert(..<=..)
6617 IgorA 2220
	@@:
2221
popad
2222
	ret
2223
endp
2224
 
2225
; ===========================================================================
2226
; Flush the current block, with given end-of-file flag.
2227
; IN assertion: strstart is set to the end of the current match.
2228
 
2229
macro FLUSH_BLOCK_ONLY s, last
2230
{
2231
local .end0
2232
	push dword last
2233
	mov eax,[s+deflate_state.strstart]
2234
	sub eax,[s+deflate_state.block_start]
2235
	push eax
2236
	xor eax,eax
2237
	cmp dword[s+deflate_state.block_start],0
2238
	jl .end0
2239
		mov eax,[s+deflate_state.block_start]
2240
		add eax,[s+deflate_state.window]
2241
	.end0:
2242
	stdcall _tr_flush_block, s, eax
2243
	mov eax,[s+deflate_state.strstart]
2244
	mov [s+deflate_state.block_start],eax
2245
	stdcall flush_pending, [s+deflate_state.strm]
2246
;   Tracev((stderr,"[FLUSH]"));
2247
}
2248
 
2249
; Same but force premature exit if necessary.
2250
macro FLUSH_BLOCK s, last
2251
{
2252
local .end0
2253
	FLUSH_BLOCK_ONLY s, last
2254
	mov eax,[s+deflate_state.strm]
2255
	cmp word[eax+z_stream.avail_out],0
2256
	jne .end0 ;if (..==0)
2257
if last eq 1
2258
		mov eax,finish_started
2259
else
2260
		mov eax,need_more
2261
end if
2262
		jmp .end_f
2263
	.end0:
2264
}
2265
 
2266
; ===========================================================================
2267
; Copy without compression as much as possible from the input stream, return
2268
; the current block state.
2269
; This function does not insert new strings in the dictionary since
2270
; uncompressible data is probably not useful. This function is used
2271
; only for the level=0 compression option.
2272
; NOTE: this function should be optimized to avoid extra copying from
2273
; window to pending_buf.
2274
 
2275
;block_state (s, flush)
6639 IgorA 2276
;    deflate_state *s
2277
;    int flush
6617 IgorA 2278
align 4
2279
proc deflate_stored uses ebx ecx edi, s:dword, flush:dword
2280
; Stored blocks are limited to 0xffff bytes, pending_buf is limited
2281
; to pending_buf_size, and each stored block has a 5 byte header:
2282
	mov edi,[s]
2283
zlib_debug 'deflate_stored'
2284
 
2285
	mov ecx,0xffff
2286
	mov eax,[edi+deflate_state.pending_buf_size]
2287
	sub eax,5
2288
	cmp ecx,eax
2289
	jle @f ;if (..>..)
2290
		mov ecx,eax
2291
	@@:
2292
	;ecx = max_block_size
2293
 
2294
	; Copy as much as possible from input to output:
2295
	.cycle0: ;for (;;) {
2296
		; Fill the window as much as possible:
2297
		cmp dword[edi+deflate_state.lookahead],1
2298
		jg .end0 ;if (..<=..)
2299
;            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
2300
;                   s->block_start >= (long)s->w_size, "slide too late");
2301
 
2302
			stdcall fill_window, edi
2303
			cmp dword[edi+deflate_state.lookahead],0
2304
			jne @f
2305
			cmp dword[flush],Z_NO_FLUSH
2306
			jne @f ;if (..==0 && ..==..)
2307
				mov eax,need_more
2308
				jmp .end_f
2309
			@@:
2310
			cmp dword[edi+deflate_state.lookahead],0
2311
			je .cycle0end ;if (..==0) break ;flush the current block
2312
		.end0:
2313
;        Assert(s->block_start >= 0, "block gone");
2314
 
2315
		mov eax,[edi+deflate_state.lookahead]
2316
		add [edi+deflate_state.strstart],eax
2317
		mov dword[edi+deflate_state.lookahead],0
2318
 
2319
		; Emit a stored block if pending_buf will be full:
2320
		mov ebx,[edi+deflate_state.block_start]
2321
		add ebx,ecx
2322
		cmp dword[edi+deflate_state.strstart],0
2323
		je @f
2324
		cmp [edi+deflate_state.strstart],ebx
2325
		jl .end1
2326
		@@: ;if (..==0 || ..>=..)
2327
			; strstart == 0 is possible when wraparound on 16-bit machine
2328
			mov eax,[edi+deflate_state.strstart]
2329
			sub eax,ebx
2330
			mov [edi+deflate_state.lookahead],eax
2331
			mov [edi+deflate_state.strstart],ebx
2332
			FLUSH_BLOCK edi, 0
2333
		.end1:
2334
		; Flush if we may have to slide, otherwise block_start may become
2335
		; negative and the data will be gone:
2336
 
2337
		MAX_DIST edi
2338
		mov ebx,[edi+deflate_state.strstart]
2339
		sub ebx,[edi+deflate_state.block_start]
2340
		cmp ebx,eax
2341
		jl .cycle0 ;if (..>=..)
2342
			FLUSH_BLOCK edi, 0
2343
		jmp .cycle0
2344
align 4
2345
	.cycle0end:
2346
	mov dword[edi+deflate_state.insert],0
2347
	cmp dword[flush],Z_FINISH
2348
	jne @f ;if (..==..)
2349
		FLUSH_BLOCK edi, 1
2350
		mov eax,finish_done
2351
		jmp .end_f
2352
	@@:
2353
	mov eax,[edi+deflate_state.block_start]
2354
	cmp [edi+deflate_state.strstart],eax
2355
	jle @f ;if (..>..)
2356
		FLUSH_BLOCK edi, 0
2357
	@@:
2358
	mov eax,block_done
2359
.end_f:
2360
	ret
2361
endp
2362
 
2363
; ===========================================================================
2364
; Compress as much as possible from the input stream, return the current
2365
; block state.
2366
; This function does not perform lazy evaluation of matches and inserts
2367
; new strings in the dictionary only for unmatched strings or for short
2368
; matches. It is used only for the fast compression options.
2369
 
2370
;block_state (s, flush)
2371
;    deflate_state *s
2372
;    int flush
2373
align 4
2374
proc deflate_fast uses ebx ecx edi, s:dword, flush:dword
2375
locals
2376
	bflush dd ? ;int  ;set if current block must be flushed
2377
endl
2378
;ecx = hash_head ;IPos ;head of the hash chain
2379
	mov edi,[s]
6652 IgorA 2380
	zlib_debug 'deflate_fast'
6617 IgorA 2381
 
2382
	.cycle0: ;for (..)
2383
	; Make sure that we always have enough lookahead, except
2384
	; at the end of the input file. We need MAX_MATCH bytes
2385
	; for the next match, plus MIN_MATCH bytes to insert the
2386
	; string following the next match.
2387
 
2388
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2389
		jge .end0 ;if (..<..)
2390
			stdcall fill_window, edi
2391
			cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2392
			jge @f ;if (..<.. && ..==..)
2393
			cmp dword[flush],Z_NO_FLUSH
2394
			jne @f
2395
				mov eax,need_more
2396
				jmp .end_f
2397
align 4
2398
			@@:
2399
			cmp dword[edi+deflate_state.lookahead],0
2400
			je .cycle0end ;if (..==0) break ;flush the current block
2401
align 4
2402
		.end0:
2403
 
2404
		; Insert the string window[strstart .. strstart+2] in the
2405
		; dictionary, and set hash_head to the head of the hash chain:
2406
 
2407
		mov ecx,NIL
2408
		cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2409
		jl @f ;if (..>=..)
2410
			INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2411
		@@:
2412
 
2413
		; Find the longest match, discarding those <= prev_length.
2414
		; At this point we have always match_length < MIN_MATCH
2415
 
2416
		cmp ecx,NIL
2417
		je @f
2418
		MAX_DIST edi
2419
		mov ebx,[edi+deflate_state.strstart]
2420
		sub ebx,ecx
2421
		cmp ebx,eax
2422
		jg @f ;if (..!=0 && ..<=..)
2423
			; To simplify the code, we prevent matches with the string
2424
			; of window index 0 (in particular we have to avoid a match
2425
			; of the string with itself at the start of the input file).
2426
 
2427
			stdcall longest_match, edi, ecx
2428
			mov [edi+deflate_state.match_length],eax
2429
			; longest_match() sets match_start
2430
		@@:
2431
		cmp dword[edi+deflate_state.match_length],MIN_MATCH
2432
		jl .end1 ;if (..>=..)
2433
			stdcall check_match, edi, [edi+deflate_state.strstart], [edi+deflate_state.match_start], [edi+deflate_state.match_length]
2434
 
2435
			mov eax,[edi+deflate_state.strstart]
2436
			sub eax,[edi+deflate_state.match_start]
2437
			mov ebx,[edi+deflate_state.match_length]
2438
			sub ebx,MIN_MATCH
2439
			_tr_tally_dist edi, eax, ebx, [bflush]
2440
 
2441
			mov eax,[edi+deflate_state.match_length]
2442
			sub [edi+deflate_state.lookahead],eax
2443
 
2444
			; Insert new strings in the hash table only if the match length
2445
			; is not too large. This saves time but degrades compression.
2446
 
2447
if FASTEST eq 0
2448
			;;mov eax,[edi+deflate_state.match_length]
2449
			cmp eax,[edi+deflate_state.max_insert_length]
2450
			jg .end3
2451
			cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2452
			jl .end3 ;if (..<=.. && ..>=..)
2453
				dec dword[edi+deflate_state.match_length] ;string at strstart already in table
2454
				.cycle1: ;do {
2455
					inc dword[edi+deflate_state.strstart]
2456
					INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2457
					; strstart never exceeds WSIZE-MAX_MATCH, so there are
2458
					; always MIN_MATCH bytes ahead.
2459
 
2460
					dec dword[edi+deflate_state.match_length]
2461
					cmp dword[edi+deflate_state.match_length],0
2462
					jne .cycle1 ;while (..!=0)
2463
				inc dword[edi+deflate_state.strstart]
2464
				jmp .end2
2465
			.end3: ;else
2466
end if
2467
 
2468
				mov eax,[edi+deflate_state.match_length]
2469
				add [edi+deflate_state.strstart],eax
2470
				mov dword[edi+deflate_state.match_length],0
2471
				mov eax,[edi+deflate_state.window]
2472
				add eax,[edi+deflate_state.strstart]
2473
				mov [edi+deflate_state.ins_h],eax
2474
				inc eax
2475
				movzx eax,byte[eax]
2476
				UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2477
if MIN_MATCH <> 3
2478
;                Call UPDATE_HASH() MIN_MATCH-3 more times
2479
end if
2480
				; If lookahead < MIN_MATCH, ins_h is garbage, but it does not
2481
				; matter since it will be recomputed at next deflate call.
2482
			jmp .end2
2483
		.end1: ;else
2484
			; No match, output a literal byte
2485
			mov eax,[edi+deflate_state.window]
2486
			add eax,[edi+deflate_state.strstart]
2487
			movzx eax,byte[eax]
2488
			Tracevv eax,
2489
			_tr_tally_lit edi, eax, [bflush]
2490
			dec dword[edi+deflate_state.lookahead]
2491
			inc dword[edi+deflate_state.strstart]
2492
		.end2:
2493
		cmp dword[bflush],0
2494
		je .cycle0 ;if (..)
2495
			FLUSH_BLOCK edi, 0
2496
		jmp .cycle0
2497
align 4
2498
	.cycle0end:
2499
	mov eax,[edi+deflate_state.strstart]
2500
	cmp eax,MIN_MATCH-1
2501
	jl @f
2502
		mov eax,MIN_MATCH-1
2503
	@@:
2504
	mov [edi+deflate_state.insert],eax
2505
	cmp dword[flush],Z_FINISH
2506
	jne @f ;if (..==..)
2507
		FLUSH_BLOCK edi, 1
2508
		mov eax,finish_done
2509
		jmp .end_f
2510
	@@:
2511
	cmp dword[edi+deflate_state.last_lit],0
2512
	je @f ;if (..)
2513
		FLUSH_BLOCK edi, 0
2514
	@@:
2515
	mov eax,block_done
2516
.end_f:
2517
	ret
2518
endp
2519
 
2520
; ===========================================================================
2521
; Same as above, but achieves better compression. We use a lazy
2522
; evaluation for matches: a match is finally adopted only if there is
2523
; no better match at the next window position.
2524
 
2525
;block_state (s, flush)
2526
;    deflate_state *s
2527
;    int flush
2528
align 4
2529
proc deflate_slow uses ebx ecx edx edi, s:dword, flush:dword
2530
locals
2531
	bflush dd ? ;int  ;set if current block must be flushed
2532
endl
2533
;ecx = hash_head ;IPos ;head of the hash chain
2534
	mov edi,[s]
6652 IgorA 2535
	zlib_debug 'deflate_slow'
6617 IgorA 2536
 
2537
	; Process the input block.
2538
	.cycle0: ;for (;;)
2539
	; Make sure that we always have enough lookahead, except
2540
	; at the end of the input file. We need MAX_MATCH bytes
2541
	; for the next match, plus MIN_MATCH bytes to insert the
2542
	; string following the next match.
2543
 
2544
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2545
		jge .end0 ;if (..<..)
2546
			stdcall fill_window, edi
2547
			cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2548
			jge @f ;if (..<.. && ..==..)
2549
			cmp dword[flush],Z_NO_FLUSH
2550
			jne @f
2551
				mov eax,need_more
2552
				jmp .end_f
2553
align 4
2554
			@@:
2555
			cmp dword[edi+deflate_state.lookahead],0
2556
			je .cycle0end ;if (..==0) break ;flush the current block
2557
align 4
2558
		.end0:
2559
 
2560
		; Insert the string window[strstart .. strstart+2] in the
2561
		; dictionary, and set hash_head to the head of the hash chain:
2562
 
2563
		mov ecx,NIL
2564
		cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2565
		jl @f ;if (..>=..)
2566
			INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2567
		@@:
2568
 
2569
		; Find the longest match, discarding those <= prev_length.
2570
 
2571
		mov eax,[edi+deflate_state.match_length]
2572
		mov [edi+deflate_state.prev_length],eax
2573
		mov eax,[edi+deflate_state.match_start]
2574
		mov [edi+deflate_state.prev_match],eax
2575
		mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2576
 
2577
		cmp ecx,NIL
2578
		je @f
2579
		mov eax,[edi+deflate_state.prev_length]
2580
		cmp eax,[edi+deflate_state.max_lazy_match]
2581
		jge @f
2582
		MAX_DIST edi
2583
		mov ebx,[edi+deflate_state.strstart]
2584
		sub ebx,ecx
2585
		cmp ebx,eax
2586
		jg .end1 ;if (..!=0 && ..<.. && ..<=..)
2587
			; To simplify the code, we prevent matches with the string
2588
			; of window index 0 (in particular we have to avoid a match
2589
			; of the string with itself at the start of the input file).
2590
 
2591
			stdcall longest_match, edi, ecx
2592
			mov [edi+deflate_state.match_length],eax
2593
			; longest_match() sets match_start
2594
 
2595
			cmp dword[edi+deflate_state.match_length],5
2596
			jg .end1
2597
			cmp word[edi+deflate_state.strategy],Z_FILTERED
2598
			jne .end1
2599
;            if (..<=.. && (..==..
2600
;#if TOO_FAR <= 32767
2601
;                || (s->match_length == MIN_MATCH &&
2602
;                    s->strstart - s->match_start > TOO_FAR)
2603
;end if
2604
;                ))
2605
 
2606
				; If prev_match is also MIN_MATCH, match_start is garbage
2607
				; but we will ignore the current match anyway.
2608
 
2609
				mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2610
		.end1:
2611
		; If there was a match at the previous step and the current
2612
		; match is not better, output the previous match:
2613
 
2614
 
2615
		mov eax,[edi+deflate_state.prev_length]
2616
		cmp eax,MIN_MATCH
2617
		jl .end2:
2618
		cmp [edi+deflate_state.match_length],eax
2619
		jg .end2: ;if (..>=.. && ..<=..)
2620
			mov edx,[edi+deflate_state.strstart]
2621
			add edx,[edi+deflate_state.lookahead]
2622
			sub edx,MIN_MATCH
2623
			;edx = max_insert
2624
			; Do not insert strings in hash table beyond this.
2625
 
2626
			mov eax,[edi+deflate_state.strstart]
2627
			dec eax
2628
			stdcall check_match, edi, eax, [edi+deflate_state.prev_match], [edi+deflate_state.prev_length]
2629
 
2630
			mov eax,[edi+deflate_state.strstart]
2631
			dec eax
2632
			sub eax,[edi+deflate_state.prev_match]
2633
			mov ebx,[edi+deflate_state.prev_length]
2634
			sub ebx,MIN_MATCH
2635
			_tr_tally_dist edi, eax, ebx, [bflush]
2636
 
2637
			; Insert in hash table all strings up to the end of the match.
2638
			; strstart-1 and strstart are already inserted. If there is not
2639
			; enough lookahead, the last two strings are not inserted in
2640
			; the hash table.
2641
 
2642
			mov eax,[edi+deflate_state.prev_length]
2643
			dec eax
2644
			sub [edi+deflate_state.lookahead],eax
2645
			sub dword[edi+deflate_state.prev_length],2
2646
			.cycle1: ;do
2647
				inc dword[edi+deflate_state.strstart]
2648
				cmp [edi+deflate_state.strstart],edx
2649
				jg @f ;if (..<=..)
2650
					INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2651
				@@:
2652
				dec dword[edi+deflate_state.prev_length]
2653
				cmp dword[edi+deflate_state.prev_length],0
2654
				jne .cycle1 ;while (..!=0)
2655
			mov dword[edi+deflate_state.match_available],0
2656
			mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2657
			inc dword[edi+deflate_state.strstart]
2658
 
2659
			cmp dword[bflush],0
2660
			je .cycle0 ;if (..)
2661
				FLUSH_BLOCK edi, 0
2662
			jmp .cycle0
2663
		.end2: ;else if (..)
2664
		cmp dword[edi+deflate_state.match_available],0
2665
		je .end3
2666
			; If there was no match at the previous position, output a
2667
			; single literal. If there was a match but the current match
2668
			; is longer, truncate the previous match to a single literal.
2669
 
2670
			mov eax,[edi+deflate_state.strstart]
2671
			dec eax
2672
			add eax,[edi+deflate_state.window]
2673
			movzx eax,byte[eax]
2674
			Tracevv eax,
2675
			_tr_tally_lit edi, eax, [bflush]
2676
			cmp dword[bflush],0
2677
			je @f ;if (..)
2678
				FLUSH_BLOCK_ONLY edi, 0
2679
			@@:
2680
			inc dword[edi+deflate_state.strstart]
2681
			dec dword[edi+deflate_state.lookahead]
2682
			mov eax,[edi+deflate_state.strm]
2683
			cmp word[eax+z_stream.avail_out],0
2684
			jne .cycle0 ;if (..==0) return ..
2685
				mov eax,need_more
2686
				jmp .end_f
2687
			jmp .cycle0 ;.end4
2688
		.end3: ;else
2689
			; There is no previous match to compare with, wait for
2690
			; the next step to decide.
2691
 
2692
			mov dword[edi+deflate_state.match_available],1
2693
			inc dword[edi+deflate_state.strstart]
2694
			dec dword[edi+deflate_state.lookahead]
2695
		;.end4:
2696
		jmp .cycle0
2697
	.cycle0end:
2698
	cmp dword[flush],Z_NO_FLUSH
2699
	jne @f
6639 IgorA 2700
		zlib_assert 'no flush?' ;Assert (..!=..)
6617 IgorA 2701
	@@:
2702
	cmp dword[edi+deflate_state.match_available],0
2703
	je @f ;if (..)
2704
		mov eax,[edi+deflate_state.strstart]
2705
		dec eax
2706
		add eax,[edi+deflate_state.window]
2707
		movzx eax,byte[eax]
2708
		Tracevv eax,
2709
		_tr_tally_lit edi, eax, [bflush]
2710
		mov dword[edi+deflate_state.match_available],0
2711
	@@:
2712
	mov eax,[edi+deflate_state.strstart]
2713
	cmp eax,MIN_MATCH-1
2714
	jl @f
2715
		mov eax,MIN_MATCH-1
2716
	@@:
2717
	mov [edi+deflate_state.insert],eax
2718
	cmp dword[flush],Z_FINISH
2719
	jne @f ;if (..==..)
2720
		FLUSH_BLOCK edi, 1
2721
		mov eax,finish_done
2722
		jmp .end_f
2723
	@@:
2724
	cmp dword[edi+deflate_state.last_lit],0
2725
	je @f ;if (..)
2726
		FLUSH_BLOCK edi, 0
2727
	@@:
2728
	mov eax,block_done
2729
.end_f:
2730
	ret
2731
endp
2732
 
2733
; ===========================================================================
2734
; For Z_RLE, simply look for runs of bytes, generate matches only of distance
2735
; one.  Do not maintain a hash table.  (It will be regenerated if this run of
2736
; deflate switches away from Z_RLE.)
2737
 
2738
;block_state (s, flush)
6639 IgorA 2739
;    deflate_state *s
2740
;    int flush
6617 IgorA 2741
align 4
2742
proc deflate_rle uses ecx edx edi esi, s:dword, flush:dword
2743
locals
2744
	bflush dd ? ;int ;set if current block must be flushed
2745
endl
2746
	mov edx,[s]
6652 IgorA 2747
	zlib_debug 'deflate_rle'
2748
align 4
6617 IgorA 2749
	.cycle0: ;for (;;)
2750
		; Make sure that we always have enough lookahead, except
2751
		; at the end of the input file. We need MAX_MATCH bytes
2752
		; for the longest run, plus one for the unrolled loop.
2753
		cmp dword[edx+deflate_state.lookahead],MAX_MATCH
2754
		jg .end0 ;if (..<=..)
2755
			stdcall fill_window, edx
2756
			cmp dword[edx+deflate_state.lookahead],MAX_MATCH
2757
			jg @f
2758
			cmp dword[flush],Z_NO_FLUSH
2759
			jne @f ;if (..<=.. && ..==..)
2760
				mov eax,need_more
2761
				jmp .end_f
2762
align 4
2763
			@@:
2764
			cmp dword[edx+deflate_state.lookahead],0
2765
			je .cycle0end ;flush the current block
2766
align 4
2767
		.end0:
2768
 
2769
		; See how many times the previous byte repeats
2770
		mov dword[edx+deflate_state.match_length],0
2771
		cmp dword[edx+deflate_state.lookahead],MIN_MATCH
2772
		jl .end1
2773
		cmp dword[edx+deflate_state.strstart],0
2774
		jle .end1 ;if (..>=.. && ..>..)
2775
			mov esi,[edx+deflate_state.window]
2776
			add esi,[edx+deflate_state.strstart]
2777
			dec esi
2778
			lodsb
2779
			mov edi,esi
2780
			scasb
2781
			jnz .end2
2782
			scasb
2783
			jnz .end2
2784
			scasb
2785
			jnz .end2 ;if (..==.. && ..==.. && ..==..)
6652 IgorA 2786
				;edi = scan ;scan goes up to strend for length of run
2787
				; al = prev ;byte at distance one to match
6617 IgorA 2788
				;ecx = strend-scan
2789
				mov ecx,MAX_MATCH-2
2790
				repz scasb
2791
				sub edi,[edx+deflate_state.window]
2792
				sub edi,[edx+deflate_state.strstart]
2793
				mov [edx+deflate_state.match_length],edi
2794
				mov eax,[edx+deflate_state.lookahead]
2795
				cmp [edx+deflate_state.match_length],eax
2796
				jle .end2
2797
					mov [edx+deflate_state.match_length],eax
2798
			.end2:
2799
			mov eax,[edx+deflate_state.window_size]
2800
			dec eax
2801
			add eax,[edx+deflate_state.window]
2802
			cmp edi,eax
2803
			jle .end1
6639 IgorA 2804
				zlib_assert 'wild scan' ;Assert(..<=..)
6617 IgorA 2805
		.end1:
2806
 
2807
		; Emit match if have run of MIN_MATCH or longer, else emit literal
2808
		cmp dword[edx+deflate_state.match_length],MIN_MATCH
2809
		jl @f ;if (..>=..)
2810
			push dword[edx+deflate_state.match_length]
2811
			mov eax,[edx+deflate_state.strstart]
2812
			dec eax
2813
			stdcall check_match, edx, [edx+deflate_state.strstart], eax
2814
 
2815
			mov eax,[edx+deflate_state.match_length]
2816
			sub eax,MIN_MATCH
2817
			_tr_tally_dist edx, 1, eax, [bflush]
2818
 
2819
			mov eax,[edx+deflate_state.match_length]
2820
			sub [edx+deflate_state.lookahead],eax
2821
			add [edx+deflate_state.strstart],eax
2822
			mov dword[edx+deflate_state.match_length],0
2823
			jmp .end3
2824
		@@: ;else
2825
			; No match, output a literal byte
2826
			mov eax,[edx+deflate_state.strstart]
2827
			add eax,[edx+deflate_state.window]
2828
			movzx eax,byte[eax]
2829
			Tracevv eax,
2830
			_tr_tally_lit edx, eax, [bflush]
2831
			dec dword[edx+deflate_state.lookahead]
2832
			inc dword[edx+deflate_state.strstart]
2833
		.end3:
2834
		cmp dword[bflush],0
2835
		je .cycle0 ;if (..)
2836
			FLUSH_BLOCK edx, 0
2837
		jmp .cycle0
2838
align 4
2839
	.cycle0end:
2840
	mov dword[edx+deflate_state.insert],0
2841
	cmp dword[flush],Z_FINISH
2842
	jne @f ;if (..==..)
2843
		FLUSH_BLOCK edx, 1
2844
		mov eax,finish_done
2845
		jmp .end_f
2846
	@@:
2847
	cmp dword[edx+deflate_state.last_lit],0
2848
	je @f ;if (..)
2849
		FLUSH_BLOCK edx, 0
2850
	@@:
2851
	mov eax,block_done
2852
.end_f:
2853
	ret
2854
endp
2855
 
2856
; ===========================================================================
2857
; For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
2858
; (It will be regenerated if this run of deflate switches away from Huffman.)
2859
 
2860
;block_state (s, flush)
6639 IgorA 2861
;    deflate_state *s
2862
;    int flush
6617 IgorA 2863
align 4
2864
proc deflate_huff uses ebx edi, s:dword, flush:dword
2865
locals
2866
	bflush dd ? ;int ;set if current block must be flushed
2867
endl
2868
	mov edi,[s]
6652 IgorA 2869
	zlib_debug 'deflate_huff'
2870
align 4
6617 IgorA 2871
	.cycle0: ;for (;;)
2872
		; Make sure that we have a literal to write.
2873
		cmp dword[edi+deflate_state.lookahead],0
2874
		jne .end0 ;if (..==0)
2875
			stdcall fill_window, edi
2876
			cmp dword[edi+deflate_state.lookahead],0
2877
			jne .end0 ;if (..==0)
2878
				cmp dword[flush],Z_NO_FLUSH
6652 IgorA 2879
				jne .cycle0end ;if (..==..)
6617 IgorA 2880
					mov eax,need_more
2881
					jmp .end_f
6652 IgorA 2882
				;flush the current block
6617 IgorA 2883
align 4
2884
		.end0:
2885
 
2886
		; Output a literal byte
2887
		mov dword[edi+deflate_state.match_length],0
2888
		mov eax,[edi+deflate_state.strstart]
2889
		add eax,[edi+deflate_state.window]
2890
		movzx eax,byte[eax]
2891
		Tracevv eax,
2892
		_tr_tally_lit edi, eax, [bflush]
2893
		dec dword[edi+deflate_state.lookahead]
2894
		inc dword[edi+deflate_state.strstart]
2895
		cmp dword[bflush],0
2896
		je @f ;if (..)
2897
			FLUSH_BLOCK edi, 0
2898
		@@:
2899
		jmp .cycle0
2900
align 4
2901
	.cycle0end:
2902
	mov dword[edi+deflate_state.insert],0
2903
	cmp dword[flush],Z_FINISH
2904
	jne @f ;if (..==..)
2905
		FLUSH_BLOCK edi, 1
2906
		mov eax,finish_done
2907
		jmp .end_f
2908
	@@:
2909
	cmp dword[edi+deflate_state.last_lit],0
2910
	je @f ;if (..)
2911
		FLUSH_BLOCK edi, 0
2912
	@@:
2913
	mov eax,block_done
2914
.end_f:
2915
	ret
2916
endp