Subversion Repositories Kolibri OS

Rev

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