forked from ycl2045/rardecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode50.go
294 lines (268 loc) · 5.97 KB
/
decode50.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package rardecode
import (
"errors"
"io"
)
const (
mainSize5 = 306
offsetSize5 = 64
lowoffsetSize5 = 16
lengthSize5 = 44
tableSize5 = mainSize5 + offsetSize5 + lowoffsetSize5 + lengthSize5
)
var (
errUnknownFilter = errors.New("rardecode: unknown V5 filter")
errCorruptDecodeHeader = errors.New("rardecode: corrupt decode header")
)
// decoder50 implements the decoder interface for RAR 5 compression.
// Decode input it broken up into 1 or more blocks. Each block starts with
// a header containing block length and optional code length tables to initialize
// the huffman decoders with.
type decoder50 struct {
r io.ByteReader
br bitReader // bit reader for current data block
codeLength [tableSize5]byte
lastBlock bool // current block is last block in compressed file
mainDecoder huffmanDecoder
offsetDecoder huffmanDecoder
lowoffsetDecoder huffmanDecoder
lengthDecoder huffmanDecoder
offset [4]int
length int
}
func (d *decoder50) init(r io.ByteReader, reset bool) error {
d.r = r
d.lastBlock = false
if reset {
for i := range d.offset {
d.offset[i] = 0
}
d.length = 0
for i := range d.codeLength {
d.codeLength[i] = 0
}
}
err := d.readBlockHeader()
if err == io.EOF {
return errDecoderOutOfData
}
return err
}
func (d *decoder50) readBlockHeader() error {
flags, err := d.r.ReadByte()
if err != nil {
return err
}
bytecount := (flags>>3)&3 + 1
if bytecount == 4 {
return errCorruptDecodeHeader
}
hsum, err := d.r.ReadByte()
if err != nil {
return err
}
blockBits := int(flags)&0x07 + 1
blockBytes := 0
sum := 0x5a ^ flags
for i := byte(0); i < bytecount; i++ {
n, err := d.r.ReadByte()
if err != nil {
return err
}
sum ^= n
blockBytes |= int(n) << (i * 8)
}
if sum != hsum { // bad header checksum
return errCorruptDecodeHeader
}
blockBits += (blockBytes - 1) * 8
// create bit reader for block
d.br = limitBitReader(newRarBitReader(d.r), blockBits, errDecoderOutOfData)
d.lastBlock = flags&0x40 > 0
if flags&0x80 > 0 {
// read new code length tables and reinitialize huffman decoders
cl := d.codeLength[:]
err = readCodeLengthTable(d.br, cl, false)
if err != nil {
return err
}
d.mainDecoder.init(cl[:mainSize5])
cl = cl[mainSize5:]
d.offsetDecoder.init(cl[:offsetSize5])
cl = cl[offsetSize5:]
d.lowoffsetDecoder.init(cl[:lowoffsetSize5])
cl = cl[lowoffsetSize5:]
d.lengthDecoder.init(cl)
}
return nil
}
func slotToLength(br bitReader, n int) (int, error) {
if n >= 8 {
bits := uint(n/4 - 1)
n = (4 | (n & 3)) << bits
if bits > 0 {
b, err := br.readBits(bits)
if err != nil {
return 0, err
}
n |= b
}
}
n += 2
return n, nil
}
// readFilter5Data reads an encoded integer used in V5 filters.
func readFilter5Data(br bitReader) (int, error) {
// TODO: should data really be uint? (for 32bit ints).
// It will be masked later anyway by decode window mask.
bytes, err := br.readBits(2)
if err != nil {
return 0, err
}
bytes++
var data int
for i := 0; i < bytes; i++ {
n, err := br.readBits(8)
if err != nil {
return 0, err
}
data |= n << (uint(i) * 8)
}
return data, nil
}
func readFilter(br bitReader) (*filterBlock, error) {
fb := new(filterBlock)
var err error
fb.offset, err = readFilter5Data(br)
if err != nil {
return nil, err
}
fb.length, err = readFilter5Data(br)
if err != nil {
return nil, err
}
ftype, err := br.readBits(3)
if err != nil {
return nil, err
}
switch ftype {
case 0:
n, err := br.readBits(5)
if err != nil {
return nil, err
}
fb.filter = func(buf []byte, offset int64) ([]byte, error) { return filterDelta(n+1, buf) }
case 1:
fb.filter = func(buf []byte, offset int64) ([]byte, error) { return filterE8(0xe8, true, buf, offset) }
case 2:
fb.filter = func(buf []byte, offset int64) ([]byte, error) { return filterE8(0xe9, true, buf, offset) }
case 3:
fb.filter = filterArm
default:
return nil, errUnknownFilter
}
return fb, nil
}
func (d *decoder50) decodeSym(win *window, sym int) (*filterBlock, error) {
switch {
case sym < 256:
// literal
win.writeByte(byte(sym))
return nil, nil
case sym == 256:
f, err := readFilter(d.br)
f.offset += win.buffered()
return f, err
case sym == 257:
// use previous offset and length
case sym < 262:
i := sym - 258
offset := d.offset[i]
copy(d.offset[1:i+1], d.offset[:i])
d.offset[0] = offset
sl, err := d.lengthDecoder.readSym(d.br)
if err != nil {
return nil, err
}
d.length, err = slotToLength(d.br, sl)
if err != nil {
return nil, err
}
default:
length, err := slotToLength(d.br, sym-262)
if err != nil {
return nil, err
}
offset := 1
slot, err := d.offsetDecoder.readSym(d.br)
if err != nil {
return nil, err
}
if slot < 4 {
offset += slot
} else {
bits := uint(slot/2 - 1)
offset += (2 | (slot & 1)) << bits
if bits >= 4 {
if bits > 4 {
n, err := d.br.readBits(bits - 4)
if err != nil {
return nil, err
}
offset += n << 4
}
n, err := d.lowoffsetDecoder.readSym(d.br)
if err != nil {
return nil, err
}
offset += n
} else {
n, err := d.br.readBits(bits)
if err != nil {
return nil, err
}
offset += n
}
}
if offset > 0x100 {
length++
if offset > 0x2000 {
length++
if offset > 0x40000 {
length++
}
}
}
copy(d.offset[1:], d.offset[:])
d.offset[0] = offset
d.length = length
}
win.copyBytes(d.length, d.offset[0])
return nil, nil
}
func (d *decoder50) fill(w *window) ([]*filterBlock, error) {
var fl []*filterBlock
for w.available() > 0 {
sym, err := d.mainDecoder.readSym(d.br)
if err == nil {
var f *filterBlock
f, err = d.decodeSym(w, sym)
if f != nil {
fl = append(fl, f)
}
} else if err == io.EOF {
// reached end of the block
if d.lastBlock {
return fl, io.EOF
}
err = d.readBlockHeader()
}
if err != nil {
if err == io.EOF {
return fl, errDecoderOutOfData
}
return fl, err
}
}
return fl, nil
}