Subversion Repositories Kolibri OS

Rev

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