Subversion Repositories Kolibri OS

Rev

Rev 6780 | Rev 6799 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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