| 1 |
;=========================================================================== |
| 2 |
; Copyright (c) 1990-2005 Info-ZIP. All rights reserved. |
| 3 |
; |
| 4 |
; See the accompanying file LICENSE, version 2005-Feb-10 or later |
| 5 |
; (the contents of which are also included in zip.h) for terms of use. |
| 6 |
; If, for some reason, all these files are missing, the Info-ZIP license |
| 7 |
; also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html |
| 8 |
;=========================================================================== |
| 9 |
; |
| 10 |
; match32.asm by Jean-loup Gailly. |
| 11 |
|
| 12 |
; match32.asm, optimized version of longest_match() in deflate.c |
| 13 |
; To be used only with 32 bit flat model. To simplify the code, the option |
| 14 |
; -DDYN_ALLOC is not supported. |
| 15 |
; This file is only optional. If you don't have an assembler, use the |
| 16 |
; C version (add -DNO_ASM to CFLAGS in makefile and remove match.o |
| 17 |
; from OBJI). If you have reduced WSIZE in zip.h, then make sure this is |
| 18 |
; assembled with an equivalent -DWSIZE=<whatever>. |
| 19 |
; |
| 20 |
; Win32 (Windows NT) version - 1994/04/13 by Steve Salisbury |
| 21 |
; * works with Microsoft MASM 6.1X and Microsoft Visual C++ / 32-bit edition |
| 22 |
; |
| 23 |
; Adapted to work with Borland Turbo Assembler 5.0 by Cosmin Truta, 1997 |
| 24 |
; |
| 25 |
; Adapted to work with OpenWatcom WASM by Chr. Spieler, 2005 |
| 26 |
; (Define the symbol WATCOM_DSEG to activate the specific Watcom C |
| 27 |
; data segment naming convention.) |
| 28 |
; |
| 29 |
;============================================================================== |
| 30 |
; |
| 31 |
; Do NOT assemble this source if external crc32 routine from zlib gets used. |
| 32 |
; |
| 33 |
IFNDEF USE_ZLIB |
| 34 |
; |
| 35 |
.386p |
| 36 |
ifdef ASM_NEW |
| 37 |
.MODEL FLAT |
| 38 |
endif |
| 39 |
|
| 40 |
name match |
| 41 |
|
| 42 |
ifdef ASM_NEW |
| 43 |
_BSS segment public use32 |
| 44 |
else |
| 45 |
_BSS segment public use32 'DATA' |
| 46 |
endif |
| 47 |
extrn _match_start : dword |
| 48 |
extrn _prev_length : dword |
| 49 |
extrn _good_match : dword |
| 50 |
ifndef FULL_SEARCH |
| 51 |
extrn _nice_match : dword |
| 52 |
endif |
| 53 |
extrn _strstart : dword |
| 54 |
extrn _max_chain_length : dword |
| 55 |
extrn _prev : word |
| 56 |
extrn _window : byte |
| 57 |
_BSS ends |
| 58 |
|
| 59 |
ifdef WATCOM_DSEG |
| 60 |
DGROUP group _BSS |
| 61 |
endif |
| 62 |
|
| 63 |
ifdef ASM_NEW |
| 64 |
_TEXT segment public use32 |
| 65 |
else |
| 66 |
_TEXT segment para public use32 'CODE' |
| 67 |
endif |
| 68 |
assume CS: _TEXT |
| 69 |
assume DS: _BSS, ES: _BSS, FS: _BSS |
| 70 |
public _match_init |
| 71 |
public _longest_match |
| 72 |
|
| 73 |
ifndef WSIZE |
| 74 |
WSIZE equ 32768 ; keep in sync with zip.h ! |
| 75 |
endif |
| 76 |
MIN_MATCH equ 3 |
| 77 |
MAX_MATCH equ 258 |
| 78 |
MIN_LOOKAHEAD equ (MAX_MATCH+MIN_MATCH+1) |
| 79 |
MAX_DIST equ (WSIZE-MIN_LOOKAHEAD) |
| 80 |
|
| 81 |
; initialize or check the variables used in match.asm. |
| 82 |
|
| 83 |
_match_init proc near |
| 84 |
ret |
| 85 |
_match_init endp |
| 86 |
|
| 87 |
; ----------------------------------------------------------------------- |
| 88 |
; Set match_start to the longest match starting at the given string and |
| 89 |
; return its length. Matches shorter or equal to prev_length are discarded, |
| 90 |
; in which case the result is equal to prev_length and match_start is |
| 91 |
; garbage. |
| 92 |
; IN assertions: cur_match is the head of the hash chain for the current |
| 93 |
; string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 |
| 94 |
|
| 95 |
; int longest_match(cur_match) |
| 96 |
|
| 97 |
_longest_match proc near |
| 98 |
|
| 99 |
cur_match equ dword ptr [esp+20] |
| 100 |
; return address ; esp+16 |
| 101 |
push ebp ; esp+12 |
| 102 |
push edi ; esp+8 |
| 103 |
push esi ; esp+4 |
| 104 |
push ebx ; esp |
| 105 |
|
| 106 |
; match equ esi |
| 107 |
; scan equ edi |
| 108 |
; chain_length equ ebp |
| 109 |
; best_len equ ebx |
| 110 |
; limit equ edx |
| 111 |
|
| 112 |
mov esi,cur_match |
| 113 |
mov edx,_strstart |
| 114 |
mov ebp,_max_chain_length ; chain_length = max_chain_length |
| 115 |
mov edi,edx |
| 116 |
sub edx,MAX_DIST ; limit = strstart-MAX_DIST |
| 117 |
cld ; string ops increment esi and edi |
| 118 |
jae short limit_ok |
| 119 |
sub edx,edx ; limit = NIL |
| 120 |
limit_ok: |
| 121 |
add edi,2+offset _window ; edi = offset(window + strstart + 2) |
| 122 |
mov ebx,_prev_length ; best_len = prev_length |
| 123 |
mov cx,[edi-2] ; cx = scan[0..1] |
| 124 |
mov ax,[ebx+edi-3] ; ax = scan[best_len-1..best_len] |
| 125 |
cmp ebx,_good_match ; do we have a good match already? |
| 126 |
jb short do_scan |
| 127 |
shr ebp,2 ; chain_length >>= 2 |
| 128 |
jmp short do_scan |
| 129 |
|
| 130 |
align 4 ; align destination of branch |
| 131 |
long_loop: |
| 132 |
; at this point, edi == scan+2, esi == cur_match |
| 133 |
mov ax,[ebx+edi-3] ; ax = scan[best_len-1..best_len] |
| 134 |
mov cx,[edi-2] ; cx = scan[0..1] |
| 135 |
short_loop: |
| 136 |
; at this point, edi == scan+2, esi == cur_match, |
| 137 |
; ax = scan[best_len-1..best_len] and cx = scan[0..1] |
| 138 |
and esi,WSIZE-1 |
| 139 |
dec ebp ; --chain_length |
| 140 |
mov si,_prev[esi+esi] ; cur_match = prev[cur_match] |
| 141 |
; top word of esi is still 0 |
| 142 |
jz short the_end |
| 143 |
cmp esi,edx ; cur_match <= limit ? |
| 144 |
jbe short the_end |
| 145 |
do_scan: |
| 146 |
cmp ax,word ptr _window[ebx+esi-1] ; check match at best_len-1 |
| 147 |
jne short_loop |
| 148 |
cmp cx,word ptr _window[esi] ; check min_match_length match |
| 149 |
jne short_loop |
| 150 |
|
| 151 |
lea esi,_window[esi+2] ; esi = match |
| 152 |
mov ecx,(MAX_MATCH-2)/2 ; scan for at most MAX_MATCH bytes |
| 153 |
mov eax,edi ; eax = scan+2 |
| 154 |
repe cmpsw ; loop until mismatch |
| 155 |
je short maxmatch ; match of length MAX_MATCH? |
| 156 |
mismatch: |
| 157 |
mov cl,[edi-2] ; mismatch on first or second byte? |
| 158 |
xchg eax,edi ; edi = scan+2, eax = end of scan |
| 159 |
sub cl,[esi-2] ; cl = 0 if first bytes equal |
| 160 |
sub eax,edi ; eax = len |
| 161 |
sub esi,2+offset _window ; esi = match - (2 + offset(window)) |
| 162 |
sub esi,eax ; esi = cur_match (= match - len) |
| 163 |
sub cl,1 ; set carry if cl == 0 (can't use DEC) |
| 164 |
adc eax,0 ; eax = carry ? len+1 : len |
| 165 |
cmp eax,ebx ; len > best_len ? |
| 166 |
jle long_loop |
| 167 |
mov _match_start,esi ; match_start = cur_match |
| 168 |
mov ebx,eax ; ebx = best_len = len |
| 169 |
ifdef FULL_SEARCH |
| 170 |
cmp eax,MAX_MATCH ; len >= MAX_MATCH ? |
| 171 |
else |
| 172 |
cmp eax,_nice_match ; len >= nice_match ? |
| 173 |
endif |
| 174 |
jl long_loop |
| 175 |
the_end: |
| 176 |
mov eax,ebx ; result = eax = best_len |
| 177 |
pop ebx |
| 178 |
pop esi |
| 179 |
pop edi |
| 180 |
pop ebp |
| 181 |
ret |
| 182 |
maxmatch: ; come here if maximum match |
| 183 |
cmpsb ; increment esi and edi |
| 184 |
jmp mismatch ; force match_length = MAX_LENGTH |
| 185 |
|
| 186 |
_longest_match endp |
| 187 |
|
| 188 |
_TEXT ends |
| 189 |
; |
| 190 |
ENDIF ; !USE_ZLIB |
| 191 |
; |
| 192 |
end |