wok view memtest/stuff/unlzsa2.S @ rev 25492

Up foomatic-db (4.0-20221117)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Nov 18 09:54:27 2022 +0000 (17 months ago)
parents 742d338c8574
children e9e2e1c1770c
line source
1 // Lzsa2Decode:
2 #ifndef FLAT32
3 // input ds:si=inStream, es:di=outStream
4 // output outStream[], ds:si, es:di
5 .code16
6 #define AX %ax
7 #define BX %bx
8 #define BP %bp
9 #define SI %si
10 #define DI %di
11 #else
12 // input esi=inStream, edi=outStream
13 // output outStream[], ds:esi, es:edi
14 .code32
15 #define AX %eax
16 #define BX %ebx
17 #define BP %ebp
18 #define SI %esi
19 #define DI %edi
20 #endif
22 MATCH_RUN_LEN = 7
23 LITERALS_RUN_LEN = 3
24 MIN_MATCH_SIZE = 2
25 MIN_LITERALS_SIZE = 0
27 #define PACKED_ONLY // assume no copy block, optional
28 //#define PARANOIA // cover rare cases, optional
30 lzsa2main:
31 #ifdef PARANOIA
32 cld
33 #endif
34 #ifndef RAW_FORMAT
35 # if defined(PARANOIA) && !defined(FLAT32) && !defined(FLAT16)
36 xorw %cx, %cx
37 call normalize
38 # endif
39 # ifndef NO_LZSA2_HEADER
40 lodsw
41 cmpw $0x9E7B, %ax // magic
42 jne lzsa2main
43 lodsb
44 testb $0x20, %al // lzsa2
45 je lzsa2main
46 # endif
47 lzsa2block: // uncompress chunk
48 # if !defined(FLAT32) && !defined(FLAT16)
49 xorw %cx, %cx
50 call normalize
51 # endif
52 lodsw // block size
53 xchgw %ax, %cx
54 lodsb
55 # ifndef PACKED_ONLY
56 orb %al, %al
57 jns lzsa2compressed
58 # if !defined(FLAT32) && !defined(FLAT16OUT)
59 movw %cx, %dx
60 andb $0x7F, %ch
61 andw $0x8000, %dx
62 copy32k:
63 call lzsa2movStr
64 xchg %dx, %cx
65 incw %cx
66 loop copy32k
67 # else
68 movsb // copy block
69 copylp:
70 movsb // copy block
71 loop copylp // handle 64K case
72 # endif
73 jmp lzsa2block
74 lzsa2compressed:
75 jne lzsa2chunk // 64Kb block
76 # endif
77 jcxz lzsa2quit // bail if we hit EOD
78 # if !defined(FLAT16)
79 movw %cx, %dx
80 xorw %cx, %cx
81 call normalize
82 addw %si, %dx
83 # else
84 movw %si, %dx
85 addw %cx, %dx
86 # endif
87 shll $16, %edx
88 #else
89 # if !defined(FLAT16)
90 xorw %cx, %cx
91 call normalize
92 # endif
93 #endif
94 movb $1, %dh // no nibble stored
95 lzsa2chunk: // uncompress chunk
96 lodsb // get token XYZ|LL|MMM
97 #if !defined(FLAT32) && !defined(FLAT16OUT)
98 pushw %ax
99 #else
100 movb %al, %bl // keep token in bl
101 #endif
102 shrb $3, %al // shift literals length into place
103 movw $LITERALS_RUN_LEN*256+MIN_LITERALS_SIZE, %cx
104 call lzsa2len // %ch = LITERALS_RUN_LEN
105 #if !defined(FLAT32) && !defined(FLAT16OUT)
106 call lzsa2movLit // copy %cx literals from %ds:%si to %es:%di
107 popw %bx
108 #else
109 rep movsb // copy %cx literals from %ds:%si to %es:%di
110 #endif
111 #ifndef RAW_FORMAT
112 maxsi:
113 shld $16, %edx, %eax
114 cmpw %ax, %si
115 jae lzsa2block // bail if we hit EOD
116 #endif
117 #ifdef FLAT32
118 orl $-1, %eax // set offset bits 31-8 to 1
119 #else
120 movb $-1, %ah // set offset bits 15-8 to 1
121 #endif
122 // XYZ
123 testb $0xC0, %bl // check match offset mode in token (X bit)
124 bt $5, %bx // move bit 5 to carry
125 js rep_match_or_large_offset
126 jne offset_9_bit
127 // 00Z 5-bit offset: read a nibble for offset bits 1-4 and use the inverted bit Z of the token as bit 0 of the offset.
128 // set bits 5-15 of the offset to 1.
129 call getByteFromNibble
130 jmp get_match_length
131 offset_9_bit:
132 // 01Z 9-bit offset: read a byte for offset bits 0-7 and use the inverted bit Z for bit 8 of the offset.
133 // set bits 9-15 of the offset to 1.
134 sbbb %cl, %ah // clear bit 8 if Z bit is clear
135 jmp get_match_length_0
137 getNibble:
138 negb %dh
139 jns gotnibble
140 lodsb
141 movb %al, %dl
142 gotnibble:
143 rolb $4, %dl
144 movb %dl, %al
145 andb $0xF, %al
146 lzsa2quit:
147 ret
149 rep_match_or_large_offset:
150 jpe rep_match_or_16_bit
151 //10Z 13-bit offset: read a nibble for offset bits 9-12 and use the inverted bit Z for bit 8 of the offset,
152 // then read a byte for offset bits 0-7. set bits 13-15 of the offset to 1.
153 // substract 512 from the offset to get the final value.
154 call getByteFromNibble
155 subb $2, %al // substract 512
156 jmp get_match_length_1
157 rep_match_or_16_bit:
158 jc repeat_match // rep-match
159 //110 16-bit offset: read a byte for offset bits 8-15, then another byte for offset bits 0-7.
160 lodsb // Get 2-byte match offset
161 get_match_length_1:
162 xchgb %al, %ah
163 get_match_length_0:
164 lodsb // load match offset bits 0-7
165 get_match_length:
166 xchgw %ax, %bp // bp: offset
167 repeat_match:
168 //111 repeat offset: reuse the offset value of the previous match command.
170 xchg AX, BX // %ax: original token
171 movw $MATCH_RUN_LEN*256+MIN_MATCH_SIZE, %cx
172 call lzsa2len
173 #ifdef RAW_FORMAT
174 jz lzsa2quit // bail if we hit EOD
175 #endif
176 #if !defined(FLAT32) && !defined(FLAT16OUT)
177 pushw %ds
178 pushw %si
179 movw %di, %si
180 addw %bp, %si
181 movw %es, %ax
182 jc dxok
183 subb $0x10, %ah
184 dxok:
185 movw %ax, %ds
186 call lzsa2movStr // copy string
187 popw %si
188 popw %ds
189 #else
190 xchg AX, SI // save %si
191 lea (BP,DI), SI
192 rep movsb %es:(SI), %es:(DI)
193 xchg AX, SI // restore %si
194 #endif
195 jmp lzsa2chunk
197 getByteFromNibble:
198 pushfw
199 call getNibble // get nibble for offset bits 0-3
200 popfw
201 rclb $1, %al
202 xorb $0xE1, %al // set offset bits 7-5 to 1
203 ret
205 lzsa2len: // get length in %ecx
206 andb %ch, %al
207 cbw // clear %ah
208 cmpb %al, %ch
209 jne lzsa2minNumber // S=0-2, L=0-6
210 call getNibble
211 cmp $0xF, %al
212 jne lzsa2noExtraByte
213 lodsb
214 addb $0xF, %cl
215 lzsa2noExtraByte:
216 addb %ch, %cl
217 lzsa2minNumber:
218 addb %cl, %al
219 jnc lzsa2gotNumber // 0-255
220 #if 0
221 je lzsa2BigNumber
222 movb %al, %ah // S=256-767, L=256-1791
223 lodsb
224 .byte 0xB1 // mask lodsb with movb $0xAD, %cl
225 lzsa2BigNumber:
226 #endif
227 lodsw // 0-65535
228 lzsa2gotNumber:
229 xchgw %ax, %cx
230 ret
232 #if !defined(FLAT32) && !defined(FLAT16OUT)
233 # if defined(PARANOIA)
234 lzsa2movlp:
235 decw %ch
236 rep movsb
237 incw %ch
238 # endif
239 normalize:
240 lzsa2movStr:
241 movw %si, %ax
242 andw $0xF, %si
243 shrw $4, %ax
244 movw %ds, %bx
245 addw %ax, %bx
246 movw %bx, %ds
247 lzsa2movLit:
248 movw %di, %ax
249 andw $0xF, %di
250 shrw $4, %ax
251 movw %es, %bx
252 addw %ax, %bx
253 movw %bx, %es
254 # if defined(PARANOIA)
255 cmpb $0xFF, %ch // catch FFFX case
256 je lzsa2movlp
257 # endif
258 rep movsb
259 ret
260 #endif