2019-05-09 14:51:29 +00:00
|
|
|
; decompress_small.S - space-efficient decompressor implementation for 8088
|
|
|
|
;
|
|
|
|
; Copyright (C) 2019 Emmanuel Marty
|
|
|
|
;
|
|
|
|
; This software is provided 'as-is', without any express or implied
|
|
|
|
; warranty. In no event will the authors be held liable for any damages
|
|
|
|
; arising from the use of this software.
|
|
|
|
;
|
|
|
|
; Permission is granted to anyone to use this software for any purpose,
|
|
|
|
; including commercial applications, and to alter it and redistribute it
|
|
|
|
; freely, subject to the following restrictions:
|
|
|
|
;
|
|
|
|
; 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
; claim that you wrote the original software. If you use this software
|
|
|
|
; in a product, an acknowledgment in the product documentation would be
|
|
|
|
; appreciated but is not required.
|
|
|
|
; 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
; misrepresented as being the original software.
|
|
|
|
; 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
|
|
|
segment .text
|
|
|
|
bits 16
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
|
|
; Decompress raw LZSA2 block
|
|
|
|
; inputs:
|
|
|
|
; * ds:si: raw LZSA2 block
|
|
|
|
; * es:di: output buffer
|
|
|
|
; output:
|
|
|
|
; * ax: decompressed size
|
|
|
|
; ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
lzsa2_decompress:
|
|
|
|
push di ; remember decompression offset
|
|
|
|
cld ; make string operations (lods, movs, stos..) move forward
|
|
|
|
|
|
|
|
xor cx,cx
|
2019-05-11 09:42:00 +00:00
|
|
|
mov bx,0100H
|
2019-05-09 14:51:29 +00:00
|
|
|
xor bp,bp
|
|
|
|
|
|
|
|
.decode_token:
|
|
|
|
mov ax,cx ; clear ah - cx is zero from above or from after rep movsb in .copy_match
|
|
|
|
lodsb ; read token byte: XYZ|LL|MMMM
|
|
|
|
mov dx,ax ; keep token in dl
|
|
|
|
|
|
|
|
and al,018H ; isolate literals length in token (LL)
|
|
|
|
mov cl,3
|
|
|
|
shr al,cl ; shift literals length into place
|
|
|
|
|
|
|
|
cmp al,03H ; LITERALS_RUN_LEN_V2?
|
|
|
|
jne .got_literals ; no, we have the full literals count from the token, go copy
|
|
|
|
|
|
|
|
call .get_nibble ; get extra literals length nibble
|
|
|
|
add al,cl ; add len from token to nibble
|
|
|
|
cmp al,012H ; LITERALS_RUN_LEN_V2 + 15 ?
|
|
|
|
jne .got_literals ; if not, we have the full literals count, go copy
|
|
|
|
|
|
|
|
lodsb ; grab extra length byte
|
2019-06-07 21:22:34 +00:00
|
|
|
add al,012H ; overflow?
|
|
|
|
jnc .got_literals ; if not, we have the full literals count, go copy
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
lodsw ; grab 16-bit extra length
|
|
|
|
|
|
|
|
.got_literals:
|
|
|
|
xchg cx,ax
|
|
|
|
rep movsb ; copy cx literals from ds:si to es:di
|
|
|
|
|
2019-07-09 23:49:01 +00:00
|
|
|
test dl,0C0h ; check match offset mode in token (X bit)
|
2019-05-09 14:51:29 +00:00
|
|
|
js .rep_match_or_large_offset
|
|
|
|
|
2019-07-09 23:49:01 +00:00
|
|
|
;;cmp dl,040H ; check if this is a 5 or 9-bit offset (Y bit)
|
|
|
|
; discovered via the test with bit 6 set
|
|
|
|
xchg cx,ax ; clear ah - cx is zero from the rep movsb above
|
|
|
|
jne .offset_9_bit
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
; 5 bit offset
|
2019-07-09 23:49:01 +00:00
|
|
|
cmp dl,020H ; test bit 5
|
|
|
|
call .get_nibble_x
|
2019-07-10 01:30:29 +00:00
|
|
|
jmp short .dec_offset_top
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
.offset_9_bit: ; 9 bit offset
|
|
|
|
lodsb ; get 8 bit offset from stream in A
|
|
|
|
dec ah ; set offset bits 15-8 to 1
|
|
|
|
test dl,020H ; test bit Z (offset bit 8)
|
2019-06-08 11:35:03 +00:00
|
|
|
je .get_match_length
|
2019-07-10 01:30:29 +00:00
|
|
|
.dec_offset_top:
|
2019-05-09 14:51:29 +00:00
|
|
|
dec ah ; clear bit 8 if Z bit is clear
|
2019-07-10 01:30:29 +00:00
|
|
|
; or set offset bits 15-8 to 1
|
2019-05-09 14:51:29 +00:00
|
|
|
jmp short .get_match_length
|
|
|
|
|
|
|
|
.rep_match_or_large_offset:
|
2019-07-12 00:20:38 +00:00
|
|
|
;;cmp dl,0c0H ; check if this is a 13-bit offset or a 16-bit offset/rep match (Y bit)
|
|
|
|
jpe .rep_match_or_16_bit
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
; 13 bit offset
|
|
|
|
|
2019-07-09 23:49:01 +00:00
|
|
|
cmp dl,0A0H ; test bit 5 (knowing that bit 7 is also set)
|
|
|
|
xchg ah,al
|
|
|
|
call .get_nibble_x
|
|
|
|
sub al,2 ; substract 512
|
2019-07-10 01:30:29 +00:00
|
|
|
jmp short .get_match_length_1
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
.rep_match_or_16_bit:
|
|
|
|
test dl,020H ; test bit Z (offset bit 8)
|
|
|
|
jne .repeat_match ; rep-match
|
|
|
|
|
|
|
|
; 16 bit offset
|
2019-07-10 01:30:29 +00:00
|
|
|
lodsb ; Get 2-byte match offset
|
|
|
|
|
|
|
|
.get_match_length_1:
|
2019-06-07 21:22:34 +00:00
|
|
|
xchg ah,al
|
2019-07-10 01:30:29 +00:00
|
|
|
lodsb ; load match offset bits 0-7
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
.get_match_length:
|
2019-07-04 02:38:27 +00:00
|
|
|
xchg bp,ax ; bp: offset
|
2019-05-09 14:51:29 +00:00
|
|
|
.repeat_match:
|
2019-07-04 02:38:27 +00:00
|
|
|
xchg ax,dx ; ax: original token
|
2019-05-09 14:51:29 +00:00
|
|
|
and al,07H ; isolate match length in token (MMM)
|
|
|
|
add al,2 ; add MIN_MATCH_SIZE_V2
|
|
|
|
|
|
|
|
cmp al,09H ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2?
|
|
|
|
jne .got_matchlen ; no, we have the full match length from the token, go copy
|
|
|
|
|
|
|
|
call .get_nibble ; get extra literals length nibble
|
|
|
|
add al,cl ; add len from token to nibble
|
|
|
|
cmp al,018H ; MIN_MATCH_SIZE_V2 + MATCH_RUN_LEN_V2 + 15?
|
|
|
|
jne .got_matchlen ; no, we have the full match length from the token, go copy
|
|
|
|
|
|
|
|
lodsb ; grab extra length byte
|
2019-06-07 21:22:34 +00:00
|
|
|
add al,018H ; overflow?
|
|
|
|
jnc .got_matchlen ; if not, we have the entire length
|
|
|
|
je short .done_decompressing ; detect EOD code
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
lodsw ; grab 16-bit length
|
|
|
|
|
|
|
|
.got_matchlen:
|
|
|
|
xchg cx,ax ; copy match length into cx
|
|
|
|
push ds ; save ds:si (current pointer to compressed data)
|
|
|
|
xchg si,ax
|
|
|
|
push es
|
|
|
|
pop ds
|
2019-07-10 01:44:37 +00:00
|
|
|
lea si,[bp+di] ; ds:si now points at back reference in output data
|
2019-05-09 14:51:29 +00:00
|
|
|
rep movsb ; copy match
|
|
|
|
xchg si,ax ; restore ds:si
|
|
|
|
pop ds
|
|
|
|
jmp .decode_token ; go decode another token
|
|
|
|
|
|
|
|
.done_decompressing:
|
|
|
|
pop ax ; retrieve the original decompression offset
|
2019-07-04 02:38:27 +00:00
|
|
|
xchg di,ax ; compute decompressed size
|
2019-05-09 14:51:29 +00:00
|
|
|
sub ax,di
|
|
|
|
ret ; done
|
|
|
|
|
2019-07-09 23:49:01 +00:00
|
|
|
.get_nibble_x:
|
|
|
|
cmc ; carry set if bit 4 was set
|
|
|
|
rcr al,1
|
|
|
|
call .get_nibble ; get nibble for offset bits 0-3
|
|
|
|
or al,cl ; merge nibble
|
|
|
|
rol al,1
|
|
|
|
xor al,0E1H ; set offset bits 7-5 to 1
|
|
|
|
ret
|
|
|
|
|
2019-05-09 14:51:29 +00:00
|
|
|
.get_nibble:
|
2019-05-11 09:42:00 +00:00
|
|
|
neg bh ; nibble ready?
|
2019-05-09 14:51:29 +00:00
|
|
|
jns .has_nibble
|
|
|
|
|
2019-05-11 09:42:00 +00:00
|
|
|
xchg bx,ax
|
2019-05-09 14:51:29 +00:00
|
|
|
lodsb ; load two nibbles
|
2019-07-04 02:38:27 +00:00
|
|
|
xchg bx,ax
|
2019-05-09 14:51:29 +00:00
|
|
|
|
|
|
|
.has_nibble:
|
|
|
|
mov cl,4 ; swap 4 high and low bits of nibble
|
|
|
|
ror bl,cl
|
|
|
|
mov cl,0FH
|
|
|
|
and cl,bl
|
|
|
|
ret
|