-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecompress.go
206 lines (182 loc) · 3.63 KB
/
decompress.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (C) 2019-2021 Hatching B.V.
// All rights reserved.
package aplib
import (
"bufio"
"bytes"
"io"
)
var DecompressMaxSize = 4 * 1024 * 1024
type aPdecompress struct {
src *bufio.Reader
dst bytes.Buffer
tag uint8
bitcount uint32
}
func (a *aPdecompress) getBit() *int {
if a.bitcount == 0 {
ch, err := a.src.ReadByte()
if err != nil {
return nil
}
a.tag = ch
a.bitcount = 7
} else {
a.bitcount--
}
bit := int((a.tag >> 7) & 1)
a.tag <<= 1
return &bit
}
func (a *aPdecompress) getGamma() *uint32 {
result := uint32(1)
cont := true
for cont {
if result&0x80000000 != 0 {
return nil
}
if bit := a.getBit(); bit == nil {
return nil
} else {
result += result + uint32(*bit)
}
if bit := a.getBit(); bit == nil {
return nil
} else {
cont = *bit != 0
}
}
return &result
}
func Decompress2(r io.Reader) []byte {
a := aPdecompress{
src: bufio.NewReader(r),
}
r0, lwm, done := 0xffffffff, false, false
ch, err := a.src.ReadByte()
if err != nil {
return nil
}
a.dst.Write([]byte{ch})
for !done {
if bit := a.getBit(); bit == nil {
return nil
} else if *bit != 0 {
if bit := a.getBit(); bit == nil {
return nil
} else if *bit != 0 {
if bit := a.getBit(); bit == nil {
return nil
} else if *bit != 0 {
var offs int
for i := 4; i != 0; i-- {
if bit := a.getBit(); bit == nil {
return nil
} else {
offs += offs + *bit
}
}
if offs != 0 {
if offs > a.dst.Len() {
return nil
}
ch := a.dst.Bytes()[a.dst.Len()-offs]
a.dst.Write([]byte{ch})
} else {
a.dst.Write([]byte{0})
}
lwm = false
} else {
ch, err := a.src.ReadByte()
if err != nil {
return nil
}
offs := int(ch)
length := 2 + (offs & 1)
offs >>= 1
if offs != 0 {
if offs > a.dst.Len() {
return nil
}
for ; length != 0; length-- {
ch := a.dst.Bytes()[a.dst.Len()-offs]
a.dst.Write([]byte{ch})
}
} else {
done = true
}
r0 = offs
lwm = true
}
} else {
if offs := a.getGamma(); offs == nil {
return nil
} else if lwm == false && *offs == 2 {
*offs = uint32(r0)
if length := a.getGamma(); length == nil {
return nil
} else if a.dst.Len()+int(*length) > DecompressMaxSize {
return nil
} else if int(*offs) > a.dst.Len() {
return nil
} else {
for ; *length != 0; *length -= 1 {
ch := a.dst.Bytes()[a.dst.Len()-int(*offs)]
a.dst.Write([]byte{ch})
}
}
} else {
if lwm == false {
*offs -= 3
} else {
*offs -= 2
}
if *offs > 0x00fffffe {
return nil
}
*offs <<= 8
ch, err := a.src.ReadByte()
if err != nil {
return nil
}
*offs += uint32(ch)
if length := a.getGamma(); length == nil {
return nil
} else if a.dst.Len()+int(*length) > DecompressMaxSize {
return nil
} else {
if *offs >= 32000 {
*length++
}
if *offs >= 1280 {
*length++
}
if *offs < 128 {
*length += 2
}
if *offs == 0 || int(*offs) > a.dst.Len() {
return nil
}
for ; *length != 0; *length -= 1 {
ch := a.dst.Bytes()[a.dst.Len()-int(*offs)]
a.dst.Write([]byte{ch})
}
r0 = int(*offs)
}
}
lwm = true
}
} else {
ch, err := a.src.ReadByte()
if err != nil {
return nil
}
a.dst.Write([]byte{ch})
lwm = false
}
}
return a.dst.Bytes()
}
func Decompress(buf []byte) []byte {
return Decompress2(bytes.NewReader(buf))
}