Subversion Repositories Kolibri OS

Rev

Rev 6652 | Rev 6741 | 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
 
547
	mov word[edi+deflate_state.pending],0
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]
642
		movzx ebx,word[edi+deflate_state.pending]
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
895
	movzx ecx,word[edx+deflate_state.pending]
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
908
	sub [edx+deflate_state.pending],cx
909
	cmp word[edx+deflate_state.pending],0
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
					movzx eax,word[edi+deflate_state.pending]
1052
					stdcall calc_crc32, [ebx+z_stream.adler],\
1053
						[edi+deflate_state.pending_buf], eax
1054
					mov [ebx+z_stream.adler],eax
1055
				@@:
1056
				mov dword[edi+deflate_state.gzindex],0
1057
				mov dword[edi+deflate_state.status],EXTRA_STATE
1058
			jmp .end2
1059
		.end1: ;else
1060
end if
1061
			mov edx,[edi+deflate_state.w_bits]
1062
			sub edx,8
1063
			shl edx,4
1064
			add edx,Z_DEFLATED
1065
			shl edx,8 ;edx = header
1066
			;esi = level_flags
1067
 
1068
			mov esi,3
1069
			cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
6652 IgorA 1070
			jge @f
6617 IgorA 1071
			cmp word[edi+deflate_state.level],2
6652 IgorA 1072
			jge .end30 ;if (..>=.. || ..<..)
1073
			@@:
6617 IgorA 1074
				xor esi,esi
1075
				jmp .end4
6652 IgorA 1076
			.end30:
6617 IgorA 1077
			cmp word[edi+deflate_state.level],6
1078
			jge @f ;else if (..<..)
1079
				mov esi,1
1080
				jmp .end4
1081
			@@:
1082
			;;cmp word[edi+deflate_state.level],6
1083
			jne .end4 ;else if (..==..)
1084
				mov esi,2
1085
			.end4:
1086
			shl esi,6
1087
			or edx,esi
1088
			cmp dword[edi+deflate_state.strstart],0
1089
			je @f ;if (..!=0)
1090
				or edx,PRESET_DICT
1091
			@@:
1092
			mov esi,edx
1093
			mov eax,edx
1094
			xor edx,edx
1095
			mov ecx,31
1096
			div ecx
1097
			add esi,31
1098
			sub esi,edx ;esi = header
1099
 
1100
			mov dword[edi+deflate_state.status],BUSY_STATE
1101
			stdcall putShortMSB, edi, esi
1102
 
1103
			; Save the adler32 of the preset dictionary:
1104
			cmp dword[edi+deflate_state.strstart],0
1105
			je @f ;if (..!=0)
1106
				mov ecx,[ebx+z_stream.adler]
1107
				bswap ecx
1108
				put_dword edi, ecx
1109
			@@:
6639 IgorA 1110
			xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 1111
			mov [ebx+z_stream.adler],eax
1112
	.end2:
1113
if GZIP eq 1
1114
	mov edx,[edi+deflate_state.gzhead]
1115
	cmp dword[edi+deflate_state.status],EXTRA_STATE
1116
	jne .end5 ;if (..==..)
1117
		cmp dword[edx+gz_header.extra],Z_NULL
1118
		je .end21 ;if (..!=..)
1119
			movzx esi,word[edi+deflate_state.pending]
1120
			;esi = beg ;start of bytes to update crc
1121
 
1122
			movzx ecx,word[edx+gz_header.extra_len]
1123
			.cycle0: ;while (..<..)
1124
			cmp dword[edi+deflate_state.gzindex],ecx
1125
			jge .cycle0end
1126
				movzx eax,word[edi+deflate_state.pending]
1127
				cmp eax,[edi+deflate_state.pending_buf_size]
1128
				jne .end24 ;if (..==..)
1129
					mov dword[edx+gz_header.hcrc],0
1130
					je @f
1131
					cmp [edi+deflate_state.pending],si
1132
					jle @f ;if (.. && ..>..)
1133
						movzx ecx,word[edi+deflate_state.pending]
1134
						sub ecx,esi
1135
						mov eax,[edi+deflate_state.pending_buf]
1136
						add eax,esi
1137
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1138
						mov [ebx+z_stream.adler],eax
1139
					@@:
1140
					stdcall flush_pending, ebx
1141
					movzx esi,word[edi+deflate_state.pending]
1142
					cmp esi,[edi+deflate_state.pending_buf_size]
1143
					je .cycle0end ;if (..==..) break
1144
				.end24:
1145
				push ebx
1146
					mov ebx,[edi+deflate_state.gzindex]
1147
					add ebx,[edx+gz_header.extra]
1148
					mov bl,[ebx]
1149
					put_byte edi, bl
1150
				pop ebx
1151
				inc dword[edi+deflate_state.gzindex]
1152
				jmp .cycle0
1153
			.cycle0end:
1154
			mov dword[edx+gz_header.hcrc],0
1155
			je @f
1156
			cmp [edi+deflate_state.pending],si
1157
			jle @f ;if (.. && ..>..)
1158
				movzx ecx,word[edi+deflate_state.pending]
1159
				sub ecx,esi
1160
				mov eax,[edi+deflate_state.pending_buf]
1161
				add eax,esi
1162
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1163
				mov [ebx+z_stream.adler],eax
1164
			@@:
1165
			mov eax,[edx+gz_header.extra_len]
1166
			cmp dword[edi+deflate_state.gzindex],eax
1167
			jne .end5 ;if (..==..)
1168
				mov dword[edi+deflate_state.gzindex],0
1169
				mov dword[edi+deflate_state.status],NAME_STATE
1170
			jmp .end5
1171
		.end21: ;else
1172
			mov dword[edi+deflate_state.status],NAME_STATE
1173
	.end5:
1174
	cmp dword[edi+deflate_state.status],NAME_STATE
1175
	jne .end6 ;if (..==..)
1176
		cmp dword[edx+gz_header.name],Z_NULL
1177
		je .end22 ;if (..!=..)
1178
			movzx esi,word[edi+deflate_state.pending]
1179
			;esi = beg ;start of bytes to update crc
1180
 
1181
			.cycle1: ;do
1182
				movzx eax,word[edi+deflate_state.pending]
1183
				cmp eax,[edi+deflate_state.pending_buf_size]
1184
				jne .end25 ;if (..==..)
1185
					mov dword[edx+gz_header.hcrc],0
1186
					je @f
1187
					cmp [edi+deflate_state.pending],si
1188
					jle @f ;if (.. && ..>..)
1189
						movzx ecx,word[edi+deflate_state.pending]
1190
						sub ecx,esi
1191
						mov eax,[edi+deflate_state.pending_buf]
1192
						add eax,esi
1193
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1194
						mov [ebx+z_stream.adler],eax
1195
					@@:
1196
					stdcall flush_pending, ebx
1197
					movzx esi,word[edi+deflate_state.pending]
1198
					movzx eax,word[edi+deflate_state.pending]
1199
					cmp eax,[edi+deflate_state.pending_buf_size]
1200
					jne .end25 ;if (..==..)
1201
						mov dword[val],1
1202
						jmp .cycle1end
1203
				.end25:
1204
				push ebx
1205
					mov ebx,[edi+deflate_state.gzindex]
1206
					add ebx,[edx+gz_header.name]
1207
					movzx ebx,byte[ebx]
1208
					mov [val],ebx
1209
					inc dword[edi+deflate_state.gzindex]
1210
					put_byte edi, bl
1211
				pop ebx
1212
				cmp dword[val],0
1213
				jne .cycle1 ;while (val != 0)
1214
			.cycle1end:
1215
			mov dword[edx+gz_header.hcrc],0
1216
			je @f
1217
			cmp [edi+deflate_state.pending],si
1218
			jle @f ;if (.. && ..>..)
1219
				movzx ecx,word[edi+deflate_state.pending]
1220
				sub ecx,esi
1221
				mov eax,[edi+deflate_state.pending_buf]
1222
				add eax,esi
1223
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1224
				mov [ebx+z_stream.adler],eax
1225
			@@:
1226
			cmp dword[val],0
1227
			jne .end6 ;if (val == 0)
1228
				mov dword[edi+deflate_state.gzindex],0
1229
				mov dword[edi+deflate_state.status],COMMENT_STATE
1230
			jmp .end6
1231
		.end22: ;else
1232
			mov dword[edi+deflate_state.status],COMMENT_STATE;
1233
	.end6:
1234
	cmp dword[edi+deflate_state.status],COMMENT_STATE
1235
	jne .end7 ;if (..==..)
1236
		cmp dword[edx+gz_header.comment],Z_NULL
1237
		je .end23 ;if (..!=..)
1238
			movzx esi,word[edi+deflate_state.pending]
1239
			;esi = beg ;start of bytes to update crc
1240
 
1241
			.cycle2: ;do
1242
				movzx eax,word[edi+deflate_state.pending]
1243
				cmp eax,[edi+deflate_state.pending_buf_size]
1244
				jne .end26 ;if (..==..)
1245
					mov dword[edx+gz_header.hcrc],0
1246
					je @f
1247
					cmp [edi+deflate_state.pending],si
1248
					jle @f ;if (.. && ..>..)
1249
						movzx ecx,word[edi+deflate_state.pending]
1250
						sub ecx,esi
1251
						mov eax,[edi+deflate_state.pending_buf]
1252
						add eax,esi
1253
						stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1254
						mov [ebx+z_stream.adler],eax
1255
					@@:
1256
					stdcall flush_pending, ebx
1257
					movzx esi,word[edi+deflate_state.pending]
1258
					movzx eax,word[edi+deflate_state.pending]
1259
					cmp eax,[edi+deflate_state.pending_buf_size]
1260
					jne .end26 ;if (..==..)
1261
						mov dword[val],1
1262
						jmp .cycle2end
1263
				.end26:
1264
				push ebx
1265
					mov ebx,[edi+deflate_state.gzindex]
1266
					add ebx,[edx+gz_header.comment]
1267
					movzx ebx,byte[ebx]
1268
					mov [val],ebx
1269
					inc dword[edi+deflate_state.gzindex]
1270
					put_byte edi, bl
1271
				pop ebx
1272
				cmp dword[val],0
1273
				jne .cycle2 ;while (val != 0)
1274
			.cycle2end:
1275
			mov dword[edx+gz_header.hcrc],0
1276
			je @f
1277
			cmp [edi+deflate_state.pending],si
1278
			jle @f ;if (.. && ..>..)
1279
				movzx ecx,word[edi+deflate_state.pending]
1280
				sub ecx,esi
1281
				mov eax,[edi+deflate_state.pending_buf]
1282
				add eax,esi
1283
				stdcall calc_crc32, [ebx+z_stream.adler], eax, ecx
1284
				mov [ebx+z_stream.adler],eax
1285
			@@:
1286
			cmp dword[val],0
1287
			jne .end7 ;if (val == 0)
1288
				mov dword[edi+deflate_state.status],HCRC_STATE
1289
			jmp .end7
1290
		.end23: ;else
1291
			mov dword[edi+deflate_state.status],HCRC_STATE
1292
	.end7:
1293
	cmp dword[edi+deflate_state.status],HCRC_STATE
1294
	jne .end8 ;if (..==..)
1295
		cmp dword[edx+gz_header.hcrc],0
1296
		je .end9 ;if (..)
1297
			movzx ecx,word[edi+deflate_state.pending]
1298
			add ecx,2
1299
			cmp ecx,[edi+deflate_state.pending_buf_size]
1300
			jle @f ;if (..>..)
1301
				stdcall flush_pending, ebx
1302
			@@:
1303
			movzx ecx,word[edi+deflate_state.pending]
1304
			add ecx,2
1305
			cmp ecx,[edi+deflate_state.pending_buf_size]
1306
			jg @f ;if (..<=..)
1307
				mov ecx,[ebx+z_stream.adler]
1308
				put_byte edi, cl
1309
				put_byte edi, ch
6639 IgorA 1310
				xor eax,eax ;stdcall calc_crc32, 0, Z_NULL, 0
6617 IgorA 1311
				mov [ebx+z_stream.adler],eax
1312
				mov dword[edi+deflate_state.status],BUSY_STATE
1313
			@@:
1314
			jmp .end8
1315
		.end9: ;else
1316
			mov dword[edi+deflate_state.status],BUSY_STATE
1317
	.end8:
1318
end if
1319
 
1320
	; Flush as much pending output as possible
1321
	cmp word[edi+deflate_state.pending],0
1322
	je .end13 ;if (..!=0)
1323
		stdcall flush_pending, ebx
1324
		cmp word[ebx+z_stream.avail_out],0
1325
		jne @f ;if (..==0)
1326
			; Since avail_out is 0, deflate will be called again with
1327
			; more output space, but possibly with both pending and
1328
			; avail_in equal to zero. There won't be anything to do,
1329
			; but this is not an error situation so make sure we
1330
			; return OK instead of BUF_ERROR at next call of deflate:
1331
 
1332
			mov dword[edi+deflate_state.last_flush],-1
1333
			mov eax,Z_OK
1334
			jmp .end_f
1335
		@@:
1336
		; Make sure there is something to do and avoid duplicate consecutive
1337
		; flushes. For repeated and useless calls with Z_FINISH, we keep
1338
		; returning Z_STREAM_END instead of Z_BUF_ERROR.
1339
		jmp @f
1340
	.end13:
6704 IgorA 1341
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1342
	jne @f
1343
	RANK dword[old_flush],esi
1344
	RANK dword[flush],eax
1345
	cmp eax,esi
1346
	jg @f
1347
	cmp dword[flush],Z_FINISH
1348
	je @f ;else if (..==0 && ..<=.. && ..!=..)
1349
		ERR_RETURN ebx, Z_BUF_ERROR
1350
		jmp .end_f
1351
	@@:
1352
 
1353
	; User must not provide more input after the first FINISH:
1354
	cmp dword[edi+deflate_state.status],FINISH_STATE
1355
	jne @f
6704 IgorA 1356
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1357
	je @f ;if (..==.. && ..!=0)
1358
		ERR_RETURN ebx, Z_BUF_ERROR
1359
		jmp .end_f
1360
	@@:
1361
 
1362
	; Start a new block or continue the current one.
1363
 
6704 IgorA 1364
	cmp dword[ebx+z_stream.avail_in],0
6617 IgorA 1365
	jne @f
1366
	cmp dword[edi+deflate_state.lookahead],0
1367
	jne @f
1368
	cmp dword[flush],Z_NO_FLUSH
1369
	je .end11
1370
	cmp dword[edi+deflate_state.status],FINISH_STATE
1371
	je .end11
1372
	@@: ;if (..!=0 || ..!=0 || (..!=.. && ..!=..))
1373
		;edx = bstate
1374
		cmp word[edi+deflate_state.strategy],Z_HUFFMAN_ONLY
1375
		jne @f
1376
			stdcall deflate_huff, edi, [flush]
1377
			jmp .end20
1378
		@@:
1379
		cmp word[edi+deflate_state.strategy],Z_RLE
1380
		jne @f
1381
			stdcall deflate_rle, edi, [flush]
1382
			jmp .end20
1383
		@@:
1384
		movzx eax,word[edi+deflate_state.level]
1385
		imul eax,sizeof.config_s
1386
		add eax,configuration_table+config_s.co_func
1387
		stdcall dword[eax], edi, [flush]
1388
		.end20:
1389
		mov edx,eax
1390
 
1391
		cmp edx,finish_started
1392
		je @f
1393
		cmp edx,finish_done
6652 IgorA 1394
		jne .end18
6617 IgorA 1395
		@@: ;if (..==.. || ..==..)
1396
			mov dword[edi+deflate_state.status],FINISH_STATE
1397
		.end18:
1398
		cmp edx,need_more
1399
		je @f
1400
		cmp edx,finish_started
6652 IgorA 1401
		jne .end19
6617 IgorA 1402
		@@: ;if (..==.. || ..==..)
1403
			cmp word[ebx+z_stream.avail_out],0
1404
			jne @f ;if (..==0)
1405
				mov dword[edi+deflate_state.last_flush],-1 ;avoid BUF_ERROR next call, see above
1406
			@@:
1407
			mov eax,Z_OK
1408
			jmp .end_f
1409
			; If flush != Z_NO_FLUSH && avail_out == 0, the next call
1410
			; of deflate should use the same flush parameter to make sure
1411
			; that the flush is complete. So we don't have to output an
1412
			; empty block here, this will be done at next call. This also
1413
			; ensures that for a very small output buffer, we emit at most
1414
			; one empty block.
1415
 
1416
		.end19:
1417
		cmp edx,block_done
1418
		jne .end11 ;if (..==..)
1419
			cmp dword[flush],Z_PARTIAL_FLUSH
1420
			jne @f ;if (..==..)
1421
				stdcall _tr_align, edi
1422
				jmp .end16
1423
			@@:
1424
			cmp dword[flush],Z_BLOCK
1425
			je .end16 ;else if (..!=..) ;FULL_FLUSH or SYNC_FLUSH
1426
				stdcall _tr_stored_block, edi, 0, 0, 0
1427
				; For a full flush, this empty block will be recognized
1428
				; as a special marker by inflate_sync().
1429
 
1430
			cmp dword[flush],Z_FULL_FLUSH
1431
			jne .end16 ;if (..==..)
1432
				CLEAR_HASH edi ;forget history
1433
				cmp dword[edi+deflate_state.lookahead],0
1434
				jne .end16 ;if (..==0)
1435
					mov dword[edi+deflate_state.strstart],0
1436
					mov dword[edi+deflate_state.block_start],0
1437
					mov dword[edi+deflate_state.insert],0
1438
		.end16:
1439
		stdcall flush_pending, ebx
1440
		cmp word[ebx+z_stream.avail_out],0
1441
		jne .end11 ;if (..==0)
1442
			mov dword[edi+deflate_state.last_flush],-1 ;avoid BUF_ERROR at next call, see above
1443
			mov eax,Z_OK
1444
			jmp .end_f
1445
	.end11:
1446
	cmp word[ebx+z_stream.avail_out],0
1447
	jg @f
6639 IgorA 1448
		zlib_assert 'bug2' ;Assert(..>0)
6617 IgorA 1449
	@@:
1450
 
1451
	cmp dword[flush],Z_FINISH
1452
	je @f ;if (..!=0)
1453
		mov eax,Z_OK
1454
		jmp .end_f
1455
	@@:
1456
	cmp dword[edi+deflate_state.wrap],0
1457
	jg @f ;if (..<=0)
1458
		mov eax,Z_STREAM_END
1459
		jmp .end_f
1460
	@@:
1461
 
1462
	; Write the trailer
1463
if GZIP eq 1
1464
	cmp dword[edi+deflate_state.wrap],2
1465
	jne @f ;if (..==..)
1466
		mov ecx,[ebx+z_stream.adler]
1467
		put_dword edi, ecx
1468
		mov ecx,[ebx+z_stream.total_in]
1469
		put_dword edi, ecx
1470
		jmp .end17
1471
	@@: ;else
1472
end if
1473
		mov ecx,[ebx+z_stream.adler]
1474
		bswap ecx
1475
		put_dword edi, ecx
1476
	.end17:
1477
	stdcall flush_pending, ebx
1478
	; If avail_out is zero, the application will call deflate again
1479
	; to flush the rest.
1480
 
1481
	cmp word[edi+deflate_state.pending],0
1482
	jle @f ;if (..>0) ;write the trailer only once!
1483
		neg word[edi+deflate_state.pending]
1484
		inc word[edi+deflate_state.pending]
1485
	@@:
1486
	mov eax,Z_OK
1487
	cmp word[edi+deflate_state.pending],0
1488
	je .end_f
1489
		mov eax,Z_STREAM_END
1490
.end_f:
1491
zlib_debug '  deflate.ret = %d',eax
1492
	ret
1493
endp
1494
 
1495
; =========================================================================
1496
;int (strm)
6639 IgorA 1497
;    z_streamp strm
6617 IgorA 1498
align 4
1499
proc deflateEnd uses ebx ecx edx, strm:dword
1500
	mov ebx,[strm]
1501
zlib_debug 'deflateEnd'
1502
	cmp ebx,Z_NULL
1503
	je @f
1504
	mov edx,[ebx+z_stream.state]
1505
	cmp edx,Z_NULL
1506
	jne .end0
1507
	@@: ;if (..==0 || ..==0) return ..
1508
		mov eax,Z_STREAM_ERROR
1509
		jmp .end_f
1510
	.end0:
1511
 
1512
	mov ecx,[edx+deflate_state.status]
1513
	cmp ecx,INIT_STATE
1514
	je @f
1515
	cmp ecx,EXTRA_STATE
1516
	je @f
1517
	cmp ecx,NAME_STATE
1518
	je @f
1519
	cmp ecx,COMMENT_STATE
1520
	je @f
1521
	cmp ecx,HCRC_STATE
1522
	je @f
1523
	cmp ecx,BUSY_STATE
1524
	je @f
1525
	cmp ecx,FINISH_STATE
1526
	je @f ;if (..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=.. && ..!=..)
1527
		mov eax,Z_STREAM_ERROR
1528
		jmp .end_f
1529
	@@:
1530
 
1531
	; Deallocate in reverse order of allocations:
1532
	TRY_FREE ebx, dword[edx+deflate_state.pending_buf]
1533
	TRY_FREE ebx, dword[edx+deflate_state.head]
1534
	TRY_FREE ebx, dword[edx+deflate_state.prev]
1535
	TRY_FREE ebx, dword[edx+deflate_state.window]
1536
 
1537
	ZFREE ebx, dword[ebx+z_stream.state]
1538
	mov dword[ebx+z_stream.state],Z_NULL
1539
 
1540
	mov eax,Z_DATA_ERROR
1541
	cmp ecx,BUSY_STATE
1542
	je .end_f
1543
		mov eax,Z_OK
1544
.end_f:
1545
	ret
1546
endp
1547
 
1548
; =========================================================================
1549
; Copy the source state to the destination state.
1550
; To simplify the source, this is not supported for 16-bit MSDOS (which
1551
; doesn't have enough memory anyway to duplicate compression states).
1552
 
1553
;int (dest, source)
6639 IgorA 1554
;    z_streamp dest
1555
;    z_streamp source
6617 IgorA 1556
align 4
6639 IgorA 1557
proc deflateCopy uses ebx edx edi esi, dest:dword, source:dword
1558
;ebx = overlay ;uint_16p
1559
;edi = ds ;deflate_state*
1560
;esi = ss ;deflate_state*
6617 IgorA 1561
 
1562
	mov esi,[source]
1563
	cmp esi,Z_NULL
1564
	je @f
1565
	mov edx,[dest]
1566
	cmp edx,Z_NULL
1567
	je @f
1568
	mov esi,[esi+z_stream.state]
1569
	cmp esi,Z_NULL
1570
	jne .end0
1571
	@@: ;if (..==0 || ..==0 || ..==0)
1572
		mov eax,Z_STREAM_ERROR
1573
		jmp .end_f
1574
	.end0:
1575
 
1576
	stdcall zmemcpy, edx, [source], sizeof.z_stream
1577
 
1578
	ZALLOC edx, 1, sizeof.deflate_state
1579
	cmp eax,0
1580
	jne @f ;if (..==0) return ..
1581
		mov eax,Z_MEM_ERROR
1582
		jmp .end_f
1583
	@@:
1584
	mov edi,eax
1585
	mov [edx+z_stream.state],eax
1586
	stdcall zmemcpy, edi, esi, sizeof.deflate_state
1587
	mov dword[edi+deflate_state.strm],edx
1588
 
1589
	ZALLOC edx, [edi+deflate_state.w_size], 2 ;2*sizeof.db
1590
	mov dword[edi+deflate_state.window],eax
1591
	ZALLOC edx, [edi+deflate_state.w_size], 4 ;sizeof.dd
1592
	mov dword[edi+deflate_state.prev],eax
1593
	ZALLOC edx, [edi+deflate_state.hash_size], 4 ;sizeof.dd
1594
	mov dword[edi+deflate_state.head],eax
1595
	ZALLOC edx, [edi+deflate_state.lit_bufsize], 4 ;sizeof.dw+2
6639 IgorA 1596
	mov ebx,eax
6617 IgorA 1597
	mov dword[edi+deflate_state.pending_buf],eax
1598
 
1599
	cmp dword[edi+deflate_state.window],Z_NULL
1600
	je @f
1601
	cmp dword[edi+deflate_state.prev],Z_NULL
1602
	je @f
1603
	cmp dword[edi+deflate_state.head],Z_NULL
1604
	je @f
1605
	cmp dword[edi+deflate_state.pending_buf],Z_NULL
1606
	jne .end1
1607
	@@: ;if (..==0 || ..==0 || ..==0 || ..==0)
1608
		stdcall deflateEnd, edx
1609
		mov eax,Z_MEM_ERROR
1610
		jmp .end_f
1611
	.end1:
1612
 
1613
	; following zmemcpy do not work for 16-bit MSDOS
1614
	mov eax,[edi+deflate_state.w_size]
1615
	shl eax,1 ;*= 2*sizeof.db
1616
	stdcall zmemcpy, [edi+deflate_state.window], [esi+deflate_state.window], eax
6639 IgorA 1617
	mov eax,[edi+deflate_state.w_size]
1618
	shl eax,2 ;*= sizeof.dd
1619
	stdcall zmemcpy, [edi+deflate_state.prev], [esi+deflate_state.prev], eax
1620
	mov eax,[edi+deflate_state.hash_size]
1621
	shl eax,2 ;*= sizeof.dd
1622
	stdcall zmemcpy, [edi+deflate_state.head], [esi+deflate_state.head], eax
1623
	stdcall zmemcpy, [edi+deflate_state.pending_buf], [esi+deflate_state.pending_buf], [edi+deflate_state.pending_buf_size]
6617 IgorA 1624
 
6639 IgorA 1625
	mov eax,[edi+deflate_state.pending_buf]
1626
	add eax,[esi+deflate_state.pending_out]
1627
	sub eax,[esi+deflate_state.pending_buf]
1628
	mov [edi+deflate_state.pending_out],eax
1629
	mov eax,[edi+deflate_state.lit_bufsize]
1630
	shr eax,1 ;/=sizeof.uint_16
1631
	add eax,ebx
1632
	mov [edi+deflate_state.d_buf],eax
1633
	mov eax,[edi+deflate_state.lit_bufsize]
1634
	imul eax,3 ;*=1+sizeof.uint_16
1635
	add eax,[edi+deflate_state.pending_buf]
1636
	mov [edi+deflate_state.l_buf],eax
6617 IgorA 1637
 
1638
	mov eax,edi
1639
	add eax,deflate_state.dyn_ltree
1640
	mov [edi+deflate_state.l_desc.dyn_tree],eax
1641
	add eax,deflate_state.dyn_dtree-deflate_state.dyn_ltree
1642
	mov [edi+deflate_state.d_desc.dyn_tree],eax
1643
	add eax,deflate_state.bl_tree-deflate_state.dyn_dtree
1644
	mov [edi+deflate_state.bl_desc.dyn_tree],eax
1645
 
1646
	mov eax,Z_OK
1647
.end_f:
1648
	ret
1649
endp
1650
 
1651
; ===========================================================================
1652
; Read a new buffer from the current input stream, update the adler32
1653
; and total number of bytes read.  All deflate() input goes through
1654
; this function so some applications may wish to modify it to avoid
1655
; allocating a large strm->next_in buffer and copying from it.
1656
; (See also flush_pending()).
1657
 
1658
;int (strm, buf, size)
6639 IgorA 1659
;    z_streamp strm
1660
;    Bytef *buf
1661
;    unsigned size
6617 IgorA 1662
align 4
1663
proc read_buf uses ebx ecx, strm:dword, buf:dword, size:dword
1664
	mov ebx,[strm]
6704 IgorA 1665
	mov eax,[ebx+z_stream.avail_in]
6617 IgorA 1666
 
1667
	cmp eax,[size]
1668
	jle @f ;if (..>..)
1669
		mov eax,[size]
1670
	@@:
1671
	cmp eax,0
1672
	jg @f
1673
		xor eax,eax
1674
		jmp .end_f ;if (..==0) return 0
1675
	@@:
1676
 
6704 IgorA 1677
	sub [ebx+z_stream.avail_in],eax
6617 IgorA 1678
 
1679
	stdcall zmemcpy, [buf],[ebx+z_stream.next_in],eax
1680
	mov ecx,[ebx+z_stream.state]
1681
	cmp [ecx+deflate_state.wrap],1
1682
	jne @f ;if (..==..)
1683
		push eax
1684
		stdcall adler32, [ebx+z_stream.adler], [buf], eax
1685
		mov [ebx+z_stream.adler],eax
1686
		pop eax
1687
		jmp .end0
1688
	@@:
1689
if GZIP eq 1
1690
	cmp [ecx+deflate_state.wrap],2
1691
	jne .end0 ;else if (..==..)
1692
		push eax
1693
		stdcall calc_crc32, [ebx+z_stream.adler], [buf], eax
1694
		mov [ebx+z_stream.adler],eax
1695
		pop eax
1696
end if
1697
	.end0:
1698
	add [ebx+z_stream.next_in],eax
1699
	add [ebx+z_stream.total_in],eax
1700
 
1701
.end_f:
1702
;zlib_debug '  read_buf.ret = %d',eax
1703
	ret
1704
endp
1705
 
1706
; ===========================================================================
1707
; Initialize the "longest match" routines for a new zlib stream
1708
 
1709
;void (s)
1710
;    deflate_state *s
1711
align 4
1712
proc lm_init uses eax ebx edi, s:dword
1713
	mov edi,[s]
1714
	mov eax,[edi+deflate_state.w_size]
1715
	shl eax,1
1716
	mov [edi+deflate_state.window_size],eax
1717
 
1718
	CLEAR_HASH edi
1719
 
1720
	; Set the default configuration parameters:
1721
 
1722
	movzx eax,word[edi+deflate_state.level]
1723
	imul eax,sizeof.config_s
1724
	add eax,configuration_table
1725
	movzx ebx,word[eax+config_s.max_lazy]
1726
	mov [edi+deflate_state.max_lazy_match],ebx
1727
	movzx ebx,word[eax+config_s.good_length]
1728
	mov [edi+deflate_state.good_match],ebx
1729
	movzx ebx,word[eax+config_s.nice_length]
1730
	mov [edi+deflate_state.nice_match],ebx
1731
	movzx ebx,word[eax+config_s.max_chain]
1732
	mov [edi+deflate_state.max_chain_length],ebx
1733
 
1734
	mov dword[edi+deflate_state.strstart],0
1735
	mov dword[edi+deflate_state.block_start],0
1736
	mov dword[edi+deflate_state.lookahead],0
1737
	mov dword[edi+deflate_state.insert],0
1738
	mov dword[edi+deflate_state.prev_length],MIN_MATCH-1
1739
	mov dword[edi+deflate_state.match_length],MIN_MATCH-1
1740
	mov dword[edi+deflate_state.match_available],0
1741
	mov dword[edi+deflate_state.ins_h],0
1742
if FASTEST eq 0
1743
;if ASMV
1744
;    call match_init ;initialize the asm code
1745
;end if
1746
end if
1747
	ret
1748
endp
1749
 
1750
;uInt (s, cur_match)
6639 IgorA 1751
;    deflate_state *s
1752
;    IPos cur_match ;current match
6617 IgorA 1753
align 4
1754
proc longest_match uses ebx ecx edx edi esi, s:dword, cur_match:dword
1755
if FASTEST eq 0
1756
; ===========================================================================
1757
; Set match_start to the longest match starting at the given string and
1758
; return its length. Matches shorter or equal to prev_length are discarded,
1759
; in which case the result is equal to prev_length and match_start is
1760
; garbage.
1761
; IN assertions: cur_match is the head of the hash chain for the current
1762
;   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1763
; OUT assertion: the match length is not greater than s->lookahead.
1764
 
1765
;#ifndef ASMV
1766
; For 80x86 and 680x0, an optimized version will be provided in match.asm or
1767
; match.S. The code will be functionally equivalent.
1768
 
1769
;    unsigned chain_length = s->max_chain_length;/* max hash chain length */
1770
;    register Bytef *scan = s->window + s->strstart; /* current string */
1771
;    register Bytef *match;                       /* matched string */
1772
;    register int len;                           /* length of current match */
1773
;    int best_len = s->prev_length;              /* best match length so far */
1774
;    int nice_match = s->nice_match;             /* stop if match long enough */
1775
;    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1776
;        s->strstart - (IPos)MAX_DIST(s) : NIL;
1777
	; Stop when cur_match becomes <= limit. To simplify the code,
1778
	; we prevent matches with the string of window index 0.
1779
 
1780
;    Posf *prev = s->prev;
1781
;    uInt wmask = s->w_mask;
1782
 
1783
;    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1784
;    register Byte scan_end1  = scan[best_len-1];
1785
;    register Byte scan_end   = scan[best_len];
1786
 
1787
	; The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1788
	; It is easy to get rid of this optimization if necessary.
1789
 
1790
;    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1791
 
1792
	; Do not waste too much time if we already have a good match:
1793
;    if (s->prev_length >= s->good_match) {
1794
;        chain_length >>= 2;
1795
;    }
1796
	; Do not look for matches beyond the end of the input. This is necessary
1797
	; to make deflate deterministic.
1798
 
1799
;    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1800
 
1801
;    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1802
 
1803
;    do {
1804
;        Assert(cur_match < s->strstart, "no future");
1805
;        match = s->window + cur_match;
1806
 
1807
	; Skip to next match if the match length cannot increase
1808
	; or if the match length is less than 2.  Note that the checks below
1809
	; for insufficient lookahead only occur occasionally for performance
1810
	; reasons.  Therefore uninitialized memory will be accessed, and
1811
	; conditional jumps will be made that depend on those values.
1812
	; However the length of the match is limited to the lookahead, so
1813
	; the output of deflate is not affected by the uninitialized values.
1814
 
1815
;        if (match[best_len]   != scan_end  ||
1816
;            match[best_len-1] != scan_end1 ||
1817
;            *match            != *scan     ||
1818
;            *++match          != scan[1])      continue;
1819
 
1820
	; The check at best_len-1 can be removed because it will be made
1821
	; again later. (This heuristic is not always a win.)
1822
	; It is not necessary to compare scan[2] and match[2] since they
1823
	; are always equal when the other bytes match, given that
1824
	; the hash keys are equal and that HASH_BITS >= 8.
1825
 
1826
;        scan += 2, match++;
1827
;        Assert(*scan == *match, "match[2]?");
1828
 
1829
	; We check for insufficient lookahead only every 8th comparison;
1830
	; the 256th check will be made at strstart+258.
1831
 
1832
;        do {
1833
;        } while (*++scan == *++match && *++scan == *++match &&
1834
;                 *++scan == *++match && *++scan == *++match &&
1835
;                 *++scan == *++match && *++scan == *++match &&
1836
;                 *++scan == *++match && *++scan == *++match &&
1837
;                 scan < strend);
1838
 
1839
;        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1840
 
1841
;        len = MAX_MATCH - (int)(strend - scan);
1842
;        scan = strend - MAX_MATCH;
1843
 
1844
;        if (len > best_len) {
1845
;            s->match_start = cur_match;
1846
;            best_len = len;
1847
;            if (len >= nice_match) break;
1848
;            scan_end1  = scan[best_len-1];
1849
;            scan_end   = scan[best_len];
1850
;        }
1851
;    } while ((cur_match = prev[cur_match & wmask]) > limit
1852
;             && --chain_length != 0);
1853
 
1854
;    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1855
;    return s->lookahead;
1856
;end if /* ASMV */
1857
 
1858
else ;FASTEST
1859
 
1860
; ---------------------------------------------------------------------------
1861
; Optimized version for FASTEST only
1862
	mov edx,[s]
6639 IgorA 1863
	zlib_debug 'longest_match'
6617 IgorA 1864
 
1865
	; The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1866
	; It is easy to get rid of this optimization if necessary.
1867
 
1868
if MAX_MATCH <> 258
1869
	cmp dword[edx+deflate_state.hash_bits],8
1870
	jge @f
6639 IgorA 1871
		zlib_assert 'Code too clever' ;Assert(..>=.. && ..==..)
6617 IgorA 1872
	@@:
1873
end if
1874
	mov eax,[edx+deflate_state.window_size]
1875
	sub eax,MIN_LOOKAHEAD
1876
	cmp [edx+deflate_state.strstart],eax
1877
	jle @f
6639 IgorA 1878
		zlib_assert 'need lookahead' ;Assert(..<=..)
6617 IgorA 1879
	@@:
1880
	mov eax,[edx+deflate_state.strstart]
1881
	cmp [cur_match],eax
1882
	jl @f
6639 IgorA 1883
		zlib_assert 'no future' ;Assert(..<..)
6617 IgorA 1884
	@@:
1885
 
1886
	mov esi,[edx+deflate_state.window]
1887
	mov edi,esi
1888
	add esi,[cur_match]
1889
	add edi,[edx+deflate_state.strstart]
1890
	;edi = scan
1891
	;esi = match
1892
 
1893
	; Return failure if the match length is less than 2:
1894
 
1895
	lodsw
1896
	cmp ax,word[edi]
1897
	je @f ;if (word[edi] != word[esi]) return
1898
		mov eax,MIN_MATCH-1
1899
		jmp .end_f
1900
	@@:
1901
 
1902
	; The check at best_len-1 can be removed because it will be made
1903
	; again later. (This heuristic is not always a win.)
1904
	; It is not necessary to compare scan[2] and match[2] since they
1905
	; are always equal when the other bytes match, given that
1906
	; the hash keys are equal and that HASH_BITS >= 8.
1907
 
1908
	add edi,2
1909
	mov al,byte[edi]
1910
	cmp al,byte[esi]
1911
	je @f
6639 IgorA 1912
		zlib_assert 'match[2]?' ;Assert(..==..)
6617 IgorA 1913
	@@:
1914
 
1915
	; We check for insufficient lookahead only every 8th comparison;
1916
	; the 256th check will be made at strstart+258.
1917
 
1918
	mov ebx,edi
1919
	mov ecx,MAX_MATCH
1920
align 4
1921
	@@:
1922
		lodsb
1923
		scasb
1924
		loope @b
1925
 
1926
	mov eax,[edx+deflate_state.window_size]
1927
	dec eax
1928
	add eax,[edx+deflate_state.window]
1929
	cmp edi,eax
1930
	jle @f
6639 IgorA 1931
		zlib_assert 'wild scan' ;Assert(..<=..)
6617 IgorA 1932
	@@:
1933
	sub edi,ebx
1934
	;edi = len
1935
 
1936
	cmp edi,MIN_MATCH
1937
	jge @f ;if (..<..)
1938
		mov eax,MIN_MATCH-1
1939
		jmp .end_f
1940
	@@:
1941
	mov eax,[cur_match]
1942
	mov [edx+deflate_state.match_start],eax
1943
	mov eax,[edx+deflate_state.lookahead]
1944
	cmp edi,eax
1945
	jg @f ;if (len <= s.lookahead) ? len : s.lookahead
1946
		mov eax,edi
1947
	@@:
1948
end if ;FASTEST
1949
.end_f:
1950
;zlib_debug '  longest_match.ret = %d',eax
1951
	ret
1952
endp
1953
 
1954
 
1955
; ===========================================================================
1956
; Check that the match at match_start is indeed a match.
1957
 
1958
;void (s, start, match, length)
6639 IgorA 1959
;    deflate_state *s
1960
;    IPos start, match
1961
;    int length
6617 IgorA 1962
align 4
1963
proc check_match, s:dword, start:dword, p3match:dword, length:dword
1964
if DEBUG eq 1
1965
	; check that the match is indeed a match
1966
;    if (zmemcmp(s->window + match,
1967
;                s->window + start, length) != EQUAL) {
1968
;        fprintf(stderr, " start %u, match %u, length %d\n",
1969
;                start, match, length);
1970
;        do {
1971
;            fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1972
;        } while (--length != 0);
1973
;        z_error("invalid match");
1974
;    }
1975
;    if (z_verbose > 1) {
1976
;        fprintf(stderr,"\\[%d,%d]", start-match, length);
1977
;        do { putc(s->window[start++], stderr); } while (--length != 0);
1978
;    }
1979
end if ;DEBUG
1980
	ret
1981
endp
1982
 
1983
 
1984
; ===========================================================================
1985
; Fill the window when the lookahead becomes insufficient.
1986
; Updates strstart and lookahead.
1987
 
1988
; IN assertion: lookahead < MIN_LOOKAHEAD
1989
; OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1990
;    At least one byte has been read, or avail_in == 0; reads are
1991
;    performed for at least two bytes (required for the zip translate_eol
1992
;    option -- not supported here).
1993
 
1994
;void (s)
1995
;    deflate_state *s
1996
align 4
1997
proc fill_window, s:dword
1998
pushad
1999
;esi = p, str, curr
2000
;ebx = more ;Amount of free space at the end of the window.
2001
	;Объем свободного пространства в конце окна.
2002
;ecx = wsize ;uInt
2003
;edx = s.strm
6639 IgorA 2004
	zlib_debug 'fill_window'
6617 IgorA 2005
	mov edi,[s]
2006
	cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2007
	jl @f
6639 IgorA 2008
		zlib_assert 'already enough lookahead' ;Assert(..<..)
6617 IgorA 2009
	@@:
2010
 
2011
	mov ecx,[edi+deflate_state.w_size]
2012
	mov edx,[edi+deflate_state.strm]
2013
	.cycle0: ;do
6639 IgorA 2014
	zlib_debug 'do'
6617 IgorA 2015
		mov ebx,[edi+deflate_state.window_size]
2016
		sub ebx,[edi+deflate_state.lookahead]
2017
		sub ebx,[edi+deflate_state.strstart]
2018
 
2019
		; If the window is almost full and there is insufficient lookahead,
2020
		; move the upper half to the lower one to make room in the upper half.
2021
 
2022
		MAX_DIST edi
2023
		add eax,ecx
2024
		cmp [edi+deflate_state.strstart],eax
2025
		jl .end0 ;if (..>=..)
2026
			push ecx
2027
			mov eax,[edi+deflate_state.window]
2028
			add eax,ecx
2029
			stdcall zmemcpy, [edi+deflate_state.window], eax
2030
			sub [edi+deflate_state.match_start],ecx
2031
			sub [edi+deflate_state.strstart],ecx ;we now have strstart >= MAX_DIST
2032
			sub [edi+deflate_state.block_start],ecx
2033
 
2034
			; Slide the hash table (could be avoided with 32 bit values
2035
			; at the expense of memory usage). We slide even when level == 0
2036
			; to keep the hash table consistent if we switch back to level > 0
2037
			; later. (Using level 0 permanently is not an optimal usage of
2038
			; zlib, so we don't care about this pathological case.)
2039
 
2040
			push ebx ecx
2041
			;ebx = wsize
2042
			;ecx = n
2043
			mov ebx,ecx
2044
			mov ecx,[edi+deflate_state.hash_size]
2045
			mov esi,ecx
2046
			shl esi,2
2047
			add esi,[edi+deflate_state.head]
2048
			.cycle1: ;do
2049
				sub esi,4
2050
				mov eax,[esi]
2051
				mov dword[esi],NIL
2052
				cmp eax,ebx
2053
				jl @f
2054
					sub eax,ebx
2055
					mov dword[esi],eax
2056
				@@:
2057
			loop .cycle1 ;while (..)
2058
 
2059
			mov ecx,ebx
2060
if FASTEST eq 0
2061
			mov esi,ecx
2062
			shl esi,2
2063
			add esi,[edi+deflate_state.prev]
2064
			.cycle2: ;do
2065
				sub esi,4
2066
				mov eax,[esi]
2067
				mov dword[esi],NIL
2068
				cmp eax,ebx
2069
				jl @f
2070
					sub eax,ebx
2071
					mov dword[esi],eax
2072
				@@:
2073
				; If n is not on any hash chain, prev[n] is garbage but
2074
				; its value will never be used.
2075
 
2076
			loop .cycle2 ;while (..)
2077
end if
2078
			pop ecx ebx
2079
			add ebx,ecx
2080
		.end0:
6704 IgorA 2081
		cmp dword[edx+z_stream.avail_in],0
6617 IgorA 2082
		je .cycle0end ;if (..==0) break
2083
 
2084
		; If there was no sliding:
2085
		;    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
2086
		;    more == window_size - lookahead - strstart
2087
		; => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
2088
		; => more >= window_size - 2*WSIZE + 2
2089
		; In the BIG_MEM or MMAP case (not yet supported),
2090
		;   window_size == input_size + MIN_LOOKAHEAD  &&
2091
		;   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
2092
		; Otherwise, window_size == 2*WSIZE so more >= 2.
2093
		; If there was sliding, more >= WSIZE. So in all cases, more >= 2.
2094
 
2095
		cmp ebx,2
2096
		jge @f
6639 IgorA 2097
			zlib_assert 'more < 2' ;Assert(..>=..)
6617 IgorA 2098
		@@:
2099
		mov eax,[edi+deflate_state.window]
2100
		add eax,[edi+deflate_state.strstart]
2101
		add eax,[edi+deflate_state.lookahead]
2102
		stdcall read_buf, edx, eax, ebx
2103
		add [edi+deflate_state.lookahead],eax
2104
 
2105
		; Initialize the hash value now that we have some input:
2106
		mov eax,[edi+deflate_state.lookahead]
2107
		add eax,[edi+deflate_state.insert]
2108
		cmp eax,MIN_MATCH
2109
		jl .end1 ;if (..>=..)
2110
			mov esi,[edi+deflate_state.strstart]
2111
			sub esi,[edi+deflate_state.insert]
2112
			;esi = str
2113
			mov eax,[edi+deflate_state.window]
2114
			add eax,esi
2115
			mov [edi+deflate_state.ins_h],eax
2116
			inc eax
2117
			movzx eax,byte[eax]
2118
            UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2119
if MIN_MATCH <> 3
2120
;            Call UPDATE_HASH() MIN_MATCH-3 more times
2121
end if
2122
			.cycle3: ;while (..)
2123
			cmp dword[edi+deflate_state.insert],0
2124
			je .end1
2125
				mov eax,esi
2126
				add eax,MIN_MATCH-1
2127
				add eax,[edi+deflate_state.window]
2128
				movzx eax,byte[eax]
2129
				UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2130
if FASTEST eq 0
2131
				mov eax,[edi+deflate_state.ins_h]
2132
				shl eax,2
2133
				add eax,[edi+deflate_state.head]
2134
				push ebx
2135
				mov ebx,[edi+deflate_state.w_mask]
2136
				and ebx,esi
2137
				shl ebx,2
2138
				add ebx,[edi+deflate_state.prev]
2139
				mov eax,[eax]
2140
				mov [ebx],eax
2141
				pop ebx
2142
end if
2143
				mov eax,[edi+deflate_state.ins_h]
2144
				shl eax,2
2145
				add eax,[edi+deflate_state.head]
2146
				mov [eax],esi
2147
				inc esi
2148
				dec dword[edi+deflate_state.insert]
2149
				mov eax,[edi+deflate_state.lookahead]
2150
				add eax,[edi+deflate_state.insert]
2151
				cmp eax,MIN_MATCH
2152
				jl .end1 ;if (..<..) break
2153
			jmp .cycle3
2154
		.end1:
2155
		; If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
2156
		; but this is not important since only literal bytes will be emitted.
2157
 
2158
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2159
		jge .cycle0end
6704 IgorA 2160
		cmp dword[edx+z_stream.avail_in],0
6617 IgorA 2161
		jne .cycle0
2162
	.cycle0end: ;while (..<.. && ..!=..)
2163
 
2164
	; If the WIN_INIT bytes after the end of the current data have never been
2165
	; written, then zero those bytes in order to avoid memory check reports of
2166
	; the use of uninitialized (or uninitialised as Julian writes) bytes by
2167
	; the longest match routines.  Update the high water mark for the next
2168
	; time through here.  WIN_INIT is set to MAX_MATCH since the longest match
2169
	; routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
2170
 
2171
	mov eax,[edi+deflate_state.window_size]
2172
	cmp [edi+deflate_state.high_water],eax
2173
	jge .end2 ;if (..<..)
2174
		mov esi,[edi+deflate_state.lookahead]
2175
		add esi,[edi+deflate_state.strstart]
2176
		;esi = curr
2177
 
2178
		cmp [edi+deflate_state.high_water],esi
2179
		jge .end3 ;if (..<..)
2180
			; Previous high water mark below current data -- zero WIN_INIT
2181
			; bytes or up to end of window, whichever is less.
2182
 
2183
			mov eax,[edi+deflate_state.window_size]
2184
			sub eax,esi
2185
			cmp eax,WIN_INIT
2186
			jle @f ;if (..>..)
2187
				mov eax,WIN_INIT
2188
			@@:
2189
			mov edx,[edi+deflate_state.window]
2190
			add edx,esi
2191
			stdcall zmemzero, edx, eax
2192
			add eax,esi
2193
			mov [edi+deflate_state.high_water],eax
2194
			jmp .end2
2195
		.end3: ;else if (..<..)
2196
		mov eax,esi
2197
		add eax,WIN_INIT
2198
		cmp [edi+deflate_state.high_water],eax
2199
		jge .end2
2200
			; High water mark at or above current data, but below current data
2201
			; plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
2202
			; to end of window, whichever is less.
2203
 
2204
			;eax = esi+WIN_INIT
2205
			sub eax,[edi+deflate_state.high_water]
2206
			mov edx,[edi+deflate_state.window_size]
2207
			sub edx,[edi+deflate_state.high_water]
2208
			cmp eax,edx ;if (..>..)
2209
			jle @f
2210
				mov eax,edx
2211
			@@:
2212
			mov edx,[edi+deflate_state.window]
2213
			add edx,[edi+deflate_state.high_water]
2214
			stdcall zmemzero, edx, eax
2215
			add [edi+deflate_state.high_water],eax
2216
	.end2:
2217
 
2218
	mov eax,[edi+deflate_state.window_size]
2219
	sub eax,MIN_LOOKAHEAD
2220
	cmp [edi+deflate_state.strstart],eax
2221
	jle @f
6639 IgorA 2222
		zlib_assert 'not enough room for search' ;Assert(..<=..)
6617 IgorA 2223
	@@:
2224
popad
2225
	ret
2226
endp
2227
 
2228
; ===========================================================================
2229
; Flush the current block, with given end-of-file flag.
2230
; IN assertion: strstart is set to the end of the current match.
2231
 
2232
macro FLUSH_BLOCK_ONLY s, last
2233
{
2234
local .end0
2235
	push dword last
2236
	mov eax,[s+deflate_state.strstart]
2237
	sub eax,[s+deflate_state.block_start]
2238
	push eax
2239
	xor eax,eax
2240
	cmp dword[s+deflate_state.block_start],0
2241
	jl .end0
2242
		mov eax,[s+deflate_state.block_start]
2243
		add eax,[s+deflate_state.window]
2244
	.end0:
2245
	stdcall _tr_flush_block, s, eax
2246
	mov eax,[s+deflate_state.strstart]
2247
	mov [s+deflate_state.block_start],eax
2248
	stdcall flush_pending, [s+deflate_state.strm]
2249
;   Tracev((stderr,"[FLUSH]"));
2250
}
2251
 
2252
; Same but force premature exit if necessary.
2253
macro FLUSH_BLOCK s, last
2254
{
2255
local .end0
2256
	FLUSH_BLOCK_ONLY s, last
2257
	mov eax,[s+deflate_state.strm]
2258
	cmp word[eax+z_stream.avail_out],0
2259
	jne .end0 ;if (..==0)
2260
if last eq 1
2261
		mov eax,finish_started
2262
else
2263
		mov eax,need_more
2264
end if
2265
		jmp .end_f
2266
	.end0:
2267
}
2268
 
2269
; ===========================================================================
2270
; Copy without compression as much as possible from the input stream, return
2271
; the current block state.
2272
; This function does not insert new strings in the dictionary since
2273
; uncompressible data is probably not useful. This function is used
2274
; only for the level=0 compression option.
2275
; NOTE: this function should be optimized to avoid extra copying from
2276
; window to pending_buf.
2277
 
2278
;block_state (s, flush)
6639 IgorA 2279
;    deflate_state *s
2280
;    int flush
6617 IgorA 2281
align 4
2282
proc deflate_stored uses ebx ecx edi, s:dword, flush:dword
2283
; Stored blocks are limited to 0xffff bytes, pending_buf is limited
2284
; to pending_buf_size, and each stored block has a 5 byte header:
2285
	mov edi,[s]
2286
zlib_debug 'deflate_stored'
2287
 
2288
	mov ecx,0xffff
2289
	mov eax,[edi+deflate_state.pending_buf_size]
2290
	sub eax,5
2291
	cmp ecx,eax
2292
	jle @f ;if (..>..)
2293
		mov ecx,eax
2294
	@@:
2295
	;ecx = max_block_size
2296
 
2297
	; Copy as much as possible from input to output:
2298
	.cycle0: ;for (;;) {
2299
		; Fill the window as much as possible:
2300
		cmp dword[edi+deflate_state.lookahead],1
2301
		jg .end0 ;if (..<=..)
2302
;            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
2303
;                   s->block_start >= (long)s->w_size, "slide too late");
2304
 
2305
			stdcall fill_window, edi
2306
			cmp dword[edi+deflate_state.lookahead],0
2307
			jne @f
2308
			cmp dword[flush],Z_NO_FLUSH
2309
			jne @f ;if (..==0 && ..==..)
2310
				mov eax,need_more
2311
				jmp .end_f
2312
			@@:
2313
			cmp dword[edi+deflate_state.lookahead],0
2314
			je .cycle0end ;if (..==0) break ;flush the current block
2315
		.end0:
2316
;        Assert(s->block_start >= 0, "block gone");
2317
 
2318
		mov eax,[edi+deflate_state.lookahead]
2319
		add [edi+deflate_state.strstart],eax
2320
		mov dword[edi+deflate_state.lookahead],0
2321
 
2322
		; Emit a stored block if pending_buf will be full:
2323
		mov ebx,[edi+deflate_state.block_start]
2324
		add ebx,ecx
2325
		cmp dword[edi+deflate_state.strstart],0
2326
		je @f
2327
		cmp [edi+deflate_state.strstart],ebx
2328
		jl .end1
2329
		@@: ;if (..==0 || ..>=..)
2330
			; strstart == 0 is possible when wraparound on 16-bit machine
2331
			mov eax,[edi+deflate_state.strstart]
2332
			sub eax,ebx
2333
			mov [edi+deflate_state.lookahead],eax
2334
			mov [edi+deflate_state.strstart],ebx
2335
			FLUSH_BLOCK edi, 0
2336
		.end1:
2337
		; Flush if we may have to slide, otherwise block_start may become
2338
		; negative and the data will be gone:
2339
 
2340
		MAX_DIST edi
2341
		mov ebx,[edi+deflate_state.strstart]
2342
		sub ebx,[edi+deflate_state.block_start]
2343
		cmp ebx,eax
2344
		jl .cycle0 ;if (..>=..)
2345
			FLUSH_BLOCK edi, 0
2346
		jmp .cycle0
2347
align 4
2348
	.cycle0end:
2349
	mov dword[edi+deflate_state.insert],0
2350
	cmp dword[flush],Z_FINISH
2351
	jne @f ;if (..==..)
2352
		FLUSH_BLOCK edi, 1
2353
		mov eax,finish_done
2354
		jmp .end_f
2355
	@@:
2356
	mov eax,[edi+deflate_state.block_start]
2357
	cmp [edi+deflate_state.strstart],eax
2358
	jle @f ;if (..>..)
2359
		FLUSH_BLOCK edi, 0
2360
	@@:
2361
	mov eax,block_done
2362
.end_f:
2363
	ret
2364
endp
2365
 
2366
; ===========================================================================
2367
; Compress as much as possible from the input stream, return the current
2368
; block state.
2369
; This function does not perform lazy evaluation of matches and inserts
2370
; new strings in the dictionary only for unmatched strings or for short
2371
; matches. It is used only for the fast compression options.
2372
 
2373
;block_state (s, flush)
2374
;    deflate_state *s
2375
;    int flush
2376
align 4
2377
proc deflate_fast uses ebx ecx edi, s:dword, flush:dword
2378
locals
2379
	bflush dd ? ;int  ;set if current block must be flushed
2380
endl
2381
;ecx = hash_head ;IPos ;head of the hash chain
2382
	mov edi,[s]
6652 IgorA 2383
	zlib_debug 'deflate_fast'
6617 IgorA 2384
 
2385
	.cycle0: ;for (..)
2386
	; Make sure that we always have enough lookahead, except
2387
	; at the end of the input file. We need MAX_MATCH bytes
2388
	; for the next match, plus MIN_MATCH bytes to insert the
2389
	; string following the next match.
2390
 
2391
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2392
		jge .end0 ;if (..<..)
2393
			stdcall fill_window, edi
2394
			cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2395
			jge @f ;if (..<.. && ..==..)
2396
			cmp dword[flush],Z_NO_FLUSH
2397
			jne @f
2398
				mov eax,need_more
2399
				jmp .end_f
2400
align 4
2401
			@@:
2402
			cmp dword[edi+deflate_state.lookahead],0
2403
			je .cycle0end ;if (..==0) break ;flush the current block
2404
align 4
2405
		.end0:
2406
 
2407
		; Insert the string window[strstart .. strstart+2] in the
2408
		; dictionary, and set hash_head to the head of the hash chain:
2409
 
2410
		mov ecx,NIL
2411
		cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2412
		jl @f ;if (..>=..)
2413
			INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2414
		@@:
2415
 
2416
		; Find the longest match, discarding those <= prev_length.
2417
		; At this point we have always match_length < MIN_MATCH
2418
 
2419
		cmp ecx,NIL
2420
		je @f
2421
		MAX_DIST edi
2422
		mov ebx,[edi+deflate_state.strstart]
2423
		sub ebx,ecx
2424
		cmp ebx,eax
2425
		jg @f ;if (..!=0 && ..<=..)
2426
			; To simplify the code, we prevent matches with the string
2427
			; of window index 0 (in particular we have to avoid a match
2428
			; of the string with itself at the start of the input file).
2429
 
2430
			stdcall longest_match, edi, ecx
2431
			mov [edi+deflate_state.match_length],eax
2432
			; longest_match() sets match_start
2433
		@@:
2434
		cmp dword[edi+deflate_state.match_length],MIN_MATCH
2435
		jl .end1 ;if (..>=..)
2436
			stdcall check_match, edi, [edi+deflate_state.strstart], [edi+deflate_state.match_start], [edi+deflate_state.match_length]
2437
 
2438
			mov eax,[edi+deflate_state.strstart]
2439
			sub eax,[edi+deflate_state.match_start]
2440
			mov ebx,[edi+deflate_state.match_length]
2441
			sub ebx,MIN_MATCH
2442
			_tr_tally_dist edi, eax, ebx, [bflush]
2443
 
2444
			mov eax,[edi+deflate_state.match_length]
2445
			sub [edi+deflate_state.lookahead],eax
2446
 
2447
			; Insert new strings in the hash table only if the match length
2448
			; is not too large. This saves time but degrades compression.
2449
 
2450
if FASTEST eq 0
2451
			;;mov eax,[edi+deflate_state.match_length]
2452
			cmp eax,[edi+deflate_state.max_insert_length]
2453
			jg .end3
2454
			cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2455
			jl .end3 ;if (..<=.. && ..>=..)
2456
				dec dword[edi+deflate_state.match_length] ;string at strstart already in table
2457
				.cycle1: ;do {
2458
					inc dword[edi+deflate_state.strstart]
2459
					INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2460
					; strstart never exceeds WSIZE-MAX_MATCH, so there are
2461
					; always MIN_MATCH bytes ahead.
2462
 
2463
					dec dword[edi+deflate_state.match_length]
2464
					cmp dword[edi+deflate_state.match_length],0
2465
					jne .cycle1 ;while (..!=0)
2466
				inc dword[edi+deflate_state.strstart]
2467
				jmp .end2
2468
			.end3: ;else
2469
end if
2470
 
2471
				mov eax,[edi+deflate_state.match_length]
2472
				add [edi+deflate_state.strstart],eax
2473
				mov dword[edi+deflate_state.match_length],0
2474
				mov eax,[edi+deflate_state.window]
2475
				add eax,[edi+deflate_state.strstart]
2476
				mov [edi+deflate_state.ins_h],eax
2477
				inc eax
2478
				movzx eax,byte[eax]
2479
				UPDATE_HASH edi, [edi+deflate_state.ins_h], eax
2480
if MIN_MATCH <> 3
2481
;                Call UPDATE_HASH() MIN_MATCH-3 more times
2482
end if
2483
				; If lookahead < MIN_MATCH, ins_h is garbage, but it does not
2484
				; matter since it will be recomputed at next deflate call.
2485
			jmp .end2
2486
		.end1: ;else
2487
			; No match, output a literal byte
2488
			mov eax,[edi+deflate_state.window]
2489
			add eax,[edi+deflate_state.strstart]
2490
			movzx eax,byte[eax]
2491
			Tracevv eax,
2492
			_tr_tally_lit edi, eax, [bflush]
2493
			dec dword[edi+deflate_state.lookahead]
2494
			inc dword[edi+deflate_state.strstart]
2495
		.end2:
2496
		cmp dword[bflush],0
2497
		je .cycle0 ;if (..)
2498
			FLUSH_BLOCK edi, 0
2499
		jmp .cycle0
2500
align 4
2501
	.cycle0end:
2502
	mov eax,[edi+deflate_state.strstart]
2503
	cmp eax,MIN_MATCH-1
2504
	jl @f
2505
		mov eax,MIN_MATCH-1
2506
	@@:
2507
	mov [edi+deflate_state.insert],eax
2508
	cmp dword[flush],Z_FINISH
2509
	jne @f ;if (..==..)
2510
		FLUSH_BLOCK edi, 1
2511
		mov eax,finish_done
2512
		jmp .end_f
2513
	@@:
2514
	cmp dword[edi+deflate_state.last_lit],0
2515
	je @f ;if (..)
2516
		FLUSH_BLOCK edi, 0
2517
	@@:
2518
	mov eax,block_done
2519
.end_f:
2520
	ret
2521
endp
2522
 
2523
; ===========================================================================
2524
; Same as above, but achieves better compression. We use a lazy
2525
; evaluation for matches: a match is finally adopted only if there is
2526
; no better match at the next window position.
2527
 
2528
;block_state (s, flush)
2529
;    deflate_state *s
2530
;    int flush
2531
align 4
2532
proc deflate_slow uses ebx ecx edx edi, s:dword, flush:dword
2533
locals
2534
	bflush dd ? ;int  ;set if current block must be flushed
2535
endl
2536
;ecx = hash_head ;IPos ;head of the hash chain
2537
	mov edi,[s]
6652 IgorA 2538
	zlib_debug 'deflate_slow'
6617 IgorA 2539
 
2540
	; Process the input block.
2541
	.cycle0: ;for (;;)
2542
	; Make sure that we always have enough lookahead, except
2543
	; at the end of the input file. We need MAX_MATCH bytes
2544
	; for the next match, plus MIN_MATCH bytes to insert the
2545
	; string following the next match.
2546
 
2547
		cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2548
		jge .end0 ;if (..<..)
2549
			stdcall fill_window, edi
2550
			cmp dword[edi+deflate_state.lookahead],MIN_LOOKAHEAD
2551
			jge @f ;if (..<.. && ..==..)
2552
			cmp dword[flush],Z_NO_FLUSH
2553
			jne @f
2554
				mov eax,need_more
2555
				jmp .end_f
2556
align 4
2557
			@@:
2558
			cmp dword[edi+deflate_state.lookahead],0
2559
			je .cycle0end ;if (..==0) break ;flush the current block
2560
align 4
2561
		.end0:
2562
 
2563
		; Insert the string window[strstart .. strstart+2] in the
2564
		; dictionary, and set hash_head to the head of the hash chain:
2565
 
2566
		mov ecx,NIL
2567
		cmp dword[edi+deflate_state.lookahead],MIN_MATCH
2568
		jl @f ;if (..>=..)
2569
			INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2570
		@@:
2571
 
2572
		; Find the longest match, discarding those <= prev_length.
2573
 
2574
		mov eax,[edi+deflate_state.match_length]
2575
		mov [edi+deflate_state.prev_length],eax
2576
		mov eax,[edi+deflate_state.match_start]
2577
		mov [edi+deflate_state.prev_match],eax
2578
		mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2579
 
2580
		cmp ecx,NIL
2581
		je @f
2582
		mov eax,[edi+deflate_state.prev_length]
2583
		cmp eax,[edi+deflate_state.max_lazy_match]
2584
		jge @f
2585
		MAX_DIST edi
2586
		mov ebx,[edi+deflate_state.strstart]
2587
		sub ebx,ecx
2588
		cmp ebx,eax
2589
		jg .end1 ;if (..!=0 && ..<.. && ..<=..)
2590
			; To simplify the code, we prevent matches with the string
2591
			; of window index 0 (in particular we have to avoid a match
2592
			; of the string with itself at the start of the input file).
2593
 
2594
			stdcall longest_match, edi, ecx
2595
			mov [edi+deflate_state.match_length],eax
2596
			; longest_match() sets match_start
2597
 
2598
			cmp dword[edi+deflate_state.match_length],5
2599
			jg .end1
2600
			cmp word[edi+deflate_state.strategy],Z_FILTERED
2601
			jne .end1
2602
;            if (..<=.. && (..==..
2603
;#if TOO_FAR <= 32767
2604
;                || (s->match_length == MIN_MATCH &&
2605
;                    s->strstart - s->match_start > TOO_FAR)
2606
;end if
2607
;                ))
2608
 
2609
				; If prev_match is also MIN_MATCH, match_start is garbage
2610
				; but we will ignore the current match anyway.
2611
 
2612
				mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2613
		.end1:
2614
		; If there was a match at the previous step and the current
2615
		; match is not better, output the previous match:
2616
 
2617
 
2618
		mov eax,[edi+deflate_state.prev_length]
2619
		cmp eax,MIN_MATCH
2620
		jl .end2:
2621
		cmp [edi+deflate_state.match_length],eax
2622
		jg .end2: ;if (..>=.. && ..<=..)
2623
			mov edx,[edi+deflate_state.strstart]
2624
			add edx,[edi+deflate_state.lookahead]
2625
			sub edx,MIN_MATCH
2626
			;edx = max_insert
2627
			; Do not insert strings in hash table beyond this.
2628
 
2629
			mov eax,[edi+deflate_state.strstart]
2630
			dec eax
2631
			stdcall check_match, edi, eax, [edi+deflate_state.prev_match], [edi+deflate_state.prev_length]
2632
 
2633
			mov eax,[edi+deflate_state.strstart]
2634
			dec eax
2635
			sub eax,[edi+deflate_state.prev_match]
2636
			mov ebx,[edi+deflate_state.prev_length]
2637
			sub ebx,MIN_MATCH
2638
			_tr_tally_dist edi, eax, ebx, [bflush]
2639
 
2640
			; Insert in hash table all strings up to the end of the match.
2641
			; strstart-1 and strstart are already inserted. If there is not
2642
			; enough lookahead, the last two strings are not inserted in
2643
			; the hash table.
2644
 
2645
			mov eax,[edi+deflate_state.prev_length]
2646
			dec eax
2647
			sub [edi+deflate_state.lookahead],eax
2648
			sub dword[edi+deflate_state.prev_length],2
2649
			.cycle1: ;do
2650
				inc dword[edi+deflate_state.strstart]
2651
				cmp [edi+deflate_state.strstart],edx
2652
				jg @f ;if (..<=..)
2653
					INSERT_STRING edi, [edi+deflate_state.strstart], ecx
2654
				@@:
2655
				dec dword[edi+deflate_state.prev_length]
2656
				cmp dword[edi+deflate_state.prev_length],0
2657
				jne .cycle1 ;while (..!=0)
2658
			mov dword[edi+deflate_state.match_available],0
2659
			mov dword[edi+deflate_state.match_length],MIN_MATCH-1
2660
			inc dword[edi+deflate_state.strstart]
2661
 
2662
			cmp dword[bflush],0
2663
			je .cycle0 ;if (..)
2664
				FLUSH_BLOCK edi, 0
2665
			jmp .cycle0
2666
		.end2: ;else if (..)
2667
		cmp dword[edi+deflate_state.match_available],0
2668
		je .end3
2669
			; If there was no match at the previous position, output a
2670
			; single literal. If there was a match but the current match
2671
			; is longer, truncate the previous match to a single literal.
2672
 
2673
			mov eax,[edi+deflate_state.strstart]
2674
			dec eax
2675
			add eax,[edi+deflate_state.window]
2676
			movzx eax,byte[eax]
2677
			Tracevv eax,
2678
			_tr_tally_lit edi, eax, [bflush]
2679
			cmp dword[bflush],0
2680
			je @f ;if (..)
2681
				FLUSH_BLOCK_ONLY edi, 0
2682
			@@:
2683
			inc dword[edi+deflate_state.strstart]
2684
			dec dword[edi+deflate_state.lookahead]
2685
			mov eax,[edi+deflate_state.strm]
2686
			cmp word[eax+z_stream.avail_out],0
2687
			jne .cycle0 ;if (..==0) return ..
2688
				mov eax,need_more
2689
				jmp .end_f
2690
			jmp .cycle0 ;.end4
2691
		.end3: ;else
2692
			; There is no previous match to compare with, wait for
2693
			; the next step to decide.
2694
 
2695
			mov dword[edi+deflate_state.match_available],1
2696
			inc dword[edi+deflate_state.strstart]
2697
			dec dword[edi+deflate_state.lookahead]
2698
		;.end4:
2699
		jmp .cycle0
2700
	.cycle0end:
2701
	cmp dword[flush],Z_NO_FLUSH
2702
	jne @f
6639 IgorA 2703
		zlib_assert 'no flush?' ;Assert (..!=..)
6617 IgorA 2704
	@@:
2705
	cmp dword[edi+deflate_state.match_available],0
2706
	je @f ;if (..)
2707
		mov eax,[edi+deflate_state.strstart]
2708
		dec eax
2709
		add eax,[edi+deflate_state.window]
2710
		movzx eax,byte[eax]
2711
		Tracevv eax,
2712
		_tr_tally_lit edi, eax, [bflush]
2713
		mov dword[edi+deflate_state.match_available],0
2714
	@@:
2715
	mov eax,[edi+deflate_state.strstart]
2716
	cmp eax,MIN_MATCH-1
2717
	jl @f
2718
		mov eax,MIN_MATCH-1
2719
	@@:
2720
	mov [edi+deflate_state.insert],eax
2721
	cmp dword[flush],Z_FINISH
2722
	jne @f ;if (..==..)
2723
		FLUSH_BLOCK edi, 1
2724
		mov eax,finish_done
2725
		jmp .end_f
2726
	@@:
2727
	cmp dword[edi+deflate_state.last_lit],0
2728
	je @f ;if (..)
2729
		FLUSH_BLOCK edi, 0
2730
	@@:
2731
	mov eax,block_done
2732
.end_f:
2733
	ret
2734
endp
2735
 
2736
; ===========================================================================
2737
; For Z_RLE, simply look for runs of bytes, generate matches only of distance
2738
; one.  Do not maintain a hash table.  (It will be regenerated if this run of
2739
; deflate switches away from Z_RLE.)
2740
 
2741
;block_state (s, flush)
6639 IgorA 2742
;    deflate_state *s
2743
;    int flush
6617 IgorA 2744
align 4
2745
proc deflate_rle uses ecx edx edi esi, s:dword, flush:dword
2746
locals
2747
	bflush dd ? ;int ;set if current block must be flushed
2748
endl
2749
	mov edx,[s]
6652 IgorA 2750
	zlib_debug 'deflate_rle'
2751
align 4
6617 IgorA 2752
	.cycle0: ;for (;;)
2753
		; Make sure that we always have enough lookahead, except
2754
		; at the end of the input file. We need MAX_MATCH bytes
2755
		; for the longest run, plus one for the unrolled loop.
2756
		cmp dword[edx+deflate_state.lookahead],MAX_MATCH
2757
		jg .end0 ;if (..<=..)
2758
			stdcall fill_window, edx
2759
			cmp dword[edx+deflate_state.lookahead],MAX_MATCH
2760
			jg @f
2761
			cmp dword[flush],Z_NO_FLUSH
2762
			jne @f ;if (..<=.. && ..==..)
2763
				mov eax,need_more
2764
				jmp .end_f
2765
align 4
2766
			@@:
2767
			cmp dword[edx+deflate_state.lookahead],0
2768
			je .cycle0end ;flush the current block
2769
align 4
2770
		.end0:
2771
 
2772
		; See how many times the previous byte repeats
2773
		mov dword[edx+deflate_state.match_length],0
2774
		cmp dword[edx+deflate_state.lookahead],MIN_MATCH
2775
		jl .end1
2776
		cmp dword[edx+deflate_state.strstart],0
2777
		jle .end1 ;if (..>=.. && ..>..)
2778
			mov esi,[edx+deflate_state.window]
2779
			add esi,[edx+deflate_state.strstart]
2780
			dec esi
2781
			lodsb
2782
			mov edi,esi
2783
			scasb
2784
			jnz .end2
2785
			scasb
2786
			jnz .end2
2787
			scasb
2788
			jnz .end2 ;if (..==.. && ..==.. && ..==..)
6652 IgorA 2789
				;edi = scan ;scan goes up to strend for length of run
2790
				; al = prev ;byte at distance one to match
6617 IgorA 2791
				;ecx = strend-scan
2792
				mov ecx,MAX_MATCH-2
2793
				repz scasb
2794
				sub edi,[edx+deflate_state.window]
2795
				sub edi,[edx+deflate_state.strstart]
2796
				mov [edx+deflate_state.match_length],edi
2797
				mov eax,[edx+deflate_state.lookahead]
2798
				cmp [edx+deflate_state.match_length],eax
2799
				jle .end2
2800
					mov [edx+deflate_state.match_length],eax
2801
			.end2:
2802
			mov eax,[edx+deflate_state.window_size]
2803
			dec eax
2804
			add eax,[edx+deflate_state.window]
2805
			cmp edi,eax
2806
			jle .end1
6639 IgorA 2807
				zlib_assert 'wild scan' ;Assert(..<=..)
6617 IgorA 2808
		.end1:
2809
 
2810
		; Emit match if have run of MIN_MATCH or longer, else emit literal
2811
		cmp dword[edx+deflate_state.match_length],MIN_MATCH
2812
		jl @f ;if (..>=..)
2813
			push dword[edx+deflate_state.match_length]
2814
			mov eax,[edx+deflate_state.strstart]
2815
			dec eax
2816
			stdcall check_match, edx, [edx+deflate_state.strstart], eax
2817
 
2818
			mov eax,[edx+deflate_state.match_length]
2819
			sub eax,MIN_MATCH
2820
			_tr_tally_dist edx, 1, eax, [bflush]
2821
 
2822
			mov eax,[edx+deflate_state.match_length]
2823
			sub [edx+deflate_state.lookahead],eax
2824
			add [edx+deflate_state.strstart],eax
2825
			mov dword[edx+deflate_state.match_length],0
2826
			jmp .end3
2827
		@@: ;else
2828
			; No match, output a literal byte
2829
			mov eax,[edx+deflate_state.strstart]
2830
			add eax,[edx+deflate_state.window]
2831
			movzx eax,byte[eax]
2832
			Tracevv eax,
2833
			_tr_tally_lit edx, eax, [bflush]
2834
			dec dword[edx+deflate_state.lookahead]
2835
			inc dword[edx+deflate_state.strstart]
2836
		.end3:
2837
		cmp dword[bflush],0
2838
		je .cycle0 ;if (..)
2839
			FLUSH_BLOCK edx, 0
2840
		jmp .cycle0
2841
align 4
2842
	.cycle0end:
2843
	mov dword[edx+deflate_state.insert],0
2844
	cmp dword[flush],Z_FINISH
2845
	jne @f ;if (..==..)
2846
		FLUSH_BLOCK edx, 1
2847
		mov eax,finish_done
2848
		jmp .end_f
2849
	@@:
2850
	cmp dword[edx+deflate_state.last_lit],0
2851
	je @f ;if (..)
2852
		FLUSH_BLOCK edx, 0
2853
	@@:
2854
	mov eax,block_done
2855
.end_f:
2856
	ret
2857
endp
2858
 
2859
; ===========================================================================
2860
; For Z_HUFFMAN_ONLY, do not look for matches.  Do not maintain a hash table.
2861
; (It will be regenerated if this run of deflate switches away from Huffman.)
2862
 
2863
;block_state (s, flush)
6639 IgorA 2864
;    deflate_state *s
2865
;    int flush
6617 IgorA 2866
align 4
2867
proc deflate_huff uses ebx edi, s:dword, flush:dword
2868
locals
2869
	bflush dd ? ;int ;set if current block must be flushed
2870
endl
2871
	mov edi,[s]
6652 IgorA 2872
	zlib_debug 'deflate_huff'
2873
align 4
6617 IgorA 2874
	.cycle0: ;for (;;)
2875
		; Make sure that we have a literal to write.
2876
		cmp dword[edi+deflate_state.lookahead],0
2877
		jne .end0 ;if (..==0)
2878
			stdcall fill_window, edi
2879
			cmp dword[edi+deflate_state.lookahead],0
2880
			jne .end0 ;if (..==0)
2881
				cmp dword[flush],Z_NO_FLUSH
6652 IgorA 2882
				jne .cycle0end ;if (..==..)
6617 IgorA 2883
					mov eax,need_more
2884
					jmp .end_f
6652 IgorA 2885
				;flush the current block
6617 IgorA 2886
align 4
2887
		.end0:
2888
 
2889
		; Output a literal byte
2890
		mov dword[edi+deflate_state.match_length],0
2891
		mov eax,[edi+deflate_state.strstart]
2892
		add eax,[edi+deflate_state.window]
2893
		movzx eax,byte[eax]
2894
		Tracevv eax,
2895
		_tr_tally_lit edi, eax, [bflush]
2896
		dec dword[edi+deflate_state.lookahead]
2897
		inc dword[edi+deflate_state.strstart]
2898
		cmp dword[bflush],0
2899
		je @f ;if (..)
2900
			FLUSH_BLOCK edi, 0
2901
		@@:
2902
		jmp .cycle0
2903
align 4
2904
	.cycle0end:
2905
	mov dword[edi+deflate_state.insert],0
2906
	cmp dword[flush],Z_FINISH
2907
	jne @f ;if (..==..)
2908
		FLUSH_BLOCK edi, 1
2909
		mov eax,finish_done
2910
		jmp .end_f
2911
	@@:
2912
	cmp dword[edi+deflate_state.last_lit],0
2913
	je @f ;if (..)
2914
		FLUSH_BLOCK edi, 0
2915
	@@:
2916
	mov eax,block_done
2917
.end_f:
2918
	ret
2919
endp