This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unlz4gb.s
81 lines (68 loc) · 1.94 KB
/
unlz4gb.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
; unlz4gb.s (c) 2018 tmk, see https://github.com/gitendo/lz4gb
; This Source Code Form is subject to the terms of the Mozilla Public
; License, v. 2.0. If a copy of the MPL was not distributed with this
; file, You can obtain one at http://mozilla.org/MPL/2.0/.
; hl - compressed data
; de - where to decompress
unlz4gb:
;-------------------------------------------------------------------------------
xor a ; init
ld b,a
ld c,a
_loop
ld a,(hl+) ; load token, move to next byte
push af ; store it for matchlength comparision
swap a ; switch nibbles
and $0f ; isolate length of literals
jr z,_no_literal ; 0 = no literal
ld c,a ; initial data length
cp 15 ;
call z,get_length ; calculate total data length
call copy ; copy data
_no_literal
pop af ; restore token
and $0f ; isolate matchlength
add a,4 ; minmatch = 4
ld c,a ; initial data length
cp 19
call z,get_length ; calculate total data lenght
ld a,(hl+) ; offset lo
cp (hl) ; offset hi
jr nz,_set_src
cp 0 ; end of compressed data
ret z ; done
_set_src
push hl ; store compressed data offset
ld h,(hl) ; offset hi
ld l,a ; offset lo
add hl,de ; hl = de + offset (instead of subtracting we add negative value here)
call copy ; copy data
pop hl ; restore compressed data offset
inc hl ; skip offset hi byte
jr _loop
get_length:
;-------------------------------------------------------------------------------
add a,(hl) ; add length byte
ld c,a ; move to counter's lo byte
adc b ; increase counter's hi byte if carry is set
sub c
ld b,a
ld a,(hl+) ; re-read and advance to next byte
inc a
ret nz ; return if not 255
ld a,c ; restore counter's lo byte
jr get_length
copy:
;-------------------------------------------------------------------------------
dec bc
inc b
inc c
_copy
ld a,(hl+)
ld (de),a
inc de
dec c
jr nz,_copy
dec b
jr nz,_copy
ret