-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
560 lines (499 loc) · 10.4 KB
/
reader.go
File metadata and controls
560 lines (499 loc) · 10.4 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package gobzip2
import (
"bufio"
"io"
"math/bits"
)
// Reader decompresses bzip2-compressed data read from the underlying reader.
type Reader struct {
br *bitReader
err error
// Stream state
blockSize100k int
combinedCRC uint32
// Block output buffer
tt []uint32
outBuf []byte
outPos int
// Set to true when we need to read a new block/stream
needBlock bool
// Set to true after stream trailer has been read
streamEnd bool
}
// NewReader returns a new Reader that decompresses bzip2 data from r.
func NewReader(r io.Reader) *Reader {
br, ok := r.(io.ByteReader)
if !ok {
br = bufio.NewReader(r)
}
return &Reader{
br: newBitReader(br),
needBlock: true,
}
}
// Read decompresses data into p.
func (r *Reader) Read(p []byte) (int, error) {
n := 0
for n < len(p) {
if r.err != nil {
if n > 0 {
return n, nil
}
return 0, r.err
}
// Serve from output buffer
if r.outPos < len(r.outBuf) {
copied := copy(p[n:], r.outBuf[r.outPos:])
r.outPos += copied
n += copied
continue
}
// Need a new block
if r.needBlock {
r.err = r.readNext()
continue
}
// Should not reach here
r.needBlock = true
}
return n, nil
}
// Close releases resources. It does not close the underlying reader.
func (r *Reader) Close() error {
return nil
}
// Reset discards internal state and switches to reading from src.
func (r *Reader) Reset(src io.Reader) {
br, ok := src.(io.ByteReader)
if !ok {
br = bufio.NewReader(src)
}
*r = Reader{
br: newBitReader(br),
needBlock: true,
tt: r.tt,
outBuf: r.outBuf,
}
}
// readNext reads the next block or stream header and decodes it fully into outBuf.
func (r *Reader) readNext() error {
if r.blockSize100k == 0 || r.streamEnd {
// Need to read a stream header
err := r.readStreamHeader()
if err != nil {
return err
}
}
return r.readBlock()
}
func (r *Reader) readStreamHeader() error {
br := r.br
b := br.readBits(8)
if br.Err() != nil {
if r.combinedCRC != 0 || r.streamEnd {
// Already read at least one complete stream; EOF here is normal
return io.EOF
}
return br.Err()
}
z := br.readBits(8)
h := br.readBits(8)
if br.Err() != nil {
return br.Err()
}
if b != 'B' || z != 'Z' || h != 'h' {
return StructuralError("bad magic value")
}
level := br.readBits(8)
if br.Err() != nil {
return br.Err()
}
if level < '1' || level > '9' {
return StructuralError("bad block size")
}
r.blockSize100k = int(level - '0')
r.combinedCRC = 0
r.streamEnd = false
maxBlock := r.blockSize100k * 100000
if cap(r.tt) < maxBlock {
r.tt = make([]uint32, maxBlock)
} else {
r.tt = r.tt[:maxBlock]
}
return nil
}
func (r *Reader) readBlock() error {
br := r.br
// Read 6-byte magic
magic := [6]byte{
byte(br.readBits(8)),
byte(br.readBits(8)),
byte(br.readBits(8)),
byte(br.readBits(8)),
byte(br.readBits(8)),
byte(br.readBits(8)),
}
if br.Err() != nil {
return br.Err()
}
// Check for end-of-stream marker: 0x17 0x72 0x45 0x38 0x50 0x90
if magic == [6]byte{0x17, 0x72, 0x45, 0x38, 0x50, 0x90} {
storedCombinedCRC := br.readBits(32)
if br.Err() != nil {
return br.Err()
}
if storedCombinedCRC != r.combinedCRC {
return &ChecksumError{Expected: storedCombinedCRC, Got: r.combinedCRC}
}
r.streamEnd = true
// Try to read another concatenated stream
return r.readNext()
}
// Check for block header: 0x31 0x41 0x59 0x26 0x53 0x59
if magic != [6]byte{0x31, 0x41, 0x59, 0x26, 0x53, 0x59} {
return StructuralError("bad block magic")
}
storedBlockCRC := br.readBits(32)
blockRandomised := br.readBit()
origPtr := int(br.readBits(24))
if br.Err() != nil {
return br.Err()
}
// Read byte usage bitmap
var inUse [256]bool
inUse16 := br.readBits(16)
for i := 0; i < 16; i++ {
if inUse16&(1<<uint(15-i)) != 0 {
bits16 := br.readBits(16)
for j := 0; j < 16; j++ {
if bits16&(1<<uint(15-j)) != 0 {
inUse[i*16+j] = true
}
}
}
}
if br.Err() != nil {
return br.Err()
}
var seqToUnseq [256]byte
nInUse := 0
for i := 0; i < 256; i++ {
if inUse[i] {
seqToUnseq[nInUse] = byte(i)
nInUse++
}
}
if nInUse == 0 {
return StructuralError("no symbols in block")
}
alphaSize := nInUse + 2
// Read selectors
numGroups := int(br.readBits(3))
if numGroups < 2 || numGroups > nGroups {
return StructuralError("bad number of Huffman groups")
}
numSelectors := int(br.readBits(15))
if numSelectors < 1 {
return StructuralError("bad number of selectors")
}
selectorMtf := make([]byte, numSelectors)
for i := 0; i < numSelectors; i++ {
j := 0
for br.readBit() {
j++
if j >= numGroups {
return StructuralError("bad selector")
}
}
selectorMtf[i] = byte(j)
}
if numSelectors > maxSelectors {
numSelectors = maxSelectors
selectorMtf = selectorMtf[:maxSelectors]
}
if br.Err() != nil {
return br.Err()
}
// Undo MTF on selectors
selector := make([]byte, numSelectors)
{
var pos [nGroups]byte
for i := 0; i < numGroups; i++ {
pos[i] = byte(i)
}
for i := 0; i < numSelectors; i++ {
v := selectorMtf[i]
tmp := pos[v]
for v > 0 {
pos[v] = pos[v-1]
v--
}
pos[0] = tmp
selector[i] = tmp
}
}
// Read Huffman tables
var codeLens [nGroups][maxAlphaSize]byte
for t := 0; t < numGroups; t++ {
curr := int(br.readBits(5))
for i := 0; i < alphaSize; i++ {
for {
if curr < 1 || curr > 20 {
return StructuralError("bad Huffman code length")
}
if !br.readBit() {
break
}
if br.readBit() {
curr--
} else {
curr++
}
}
codeLens[t][i] = byte(curr)
}
}
if br.Err() != nil {
return br.Err()
}
// Create decode tables
var (
limit [nGroups][maxAlphaSize]int32
base [nGroups][maxAlphaSize]int32
perm [nGroups][maxAlphaSize]int32
minLens [nGroups]int
)
for t := 0; t < numGroups; t++ {
minLen, maxLen := 32, 0
for i := 0; i < alphaSize; i++ {
l := int(codeLens[t][i])
if l > maxLen {
maxLen = l
}
if l < minLen {
minLen = l
}
}
createDecodeTables(limit[t][:], base[t][:], perm[t][:], codeLens[t][:], minLen, maxLen, alphaSize)
minLens[t] = minLen
}
// Decode MTF values
eob := nInUse + 1
nblockMAX := r.blockSize100k * 100000
groupNo := -1
groupPos := 0
var gLimit, gBase, gPerm []int32
var gMinlen int
var unzftab [256]int32
var mtfDec mtfDecoder
mtfDec.init()
nblock := 0
getSymbol := func() (int, error) {
if groupPos == 0 {
groupNo++
if groupNo >= numSelectors {
return 0, StructuralError("ran out of selectors")
}
groupPos = groupSize
gSel := int(selector[groupNo])
gMinlen = minLens[gSel]
gLimit = limit[gSel][:]
gPerm = perm[gSel][:]
gBase = base[gSel][:]
}
groupPos--
zn := gMinlen
zvec := int32(br.readBits(uint(zn)))
for {
if zn > 20 {
return 0, StructuralError("Huffman code too long")
}
if zvec <= gLimit[zn] {
break
}
zn++
zvec = (zvec << 1) | int32(br.readBits(1))
}
if br.Err() != nil {
return 0, br.Err()
}
idx := zvec - gBase[zn]
if idx < 0 || idx >= int32(maxAlphaSize) {
return 0, StructuralError("bad Huffman code")
}
return int(gPerm[idx]), nil
}
nextSym, err := getSymbol()
if err != nil {
return err
}
for {
if nextSym == eob {
break
}
if nextSym == symRUNA || nextSym == symRUNB {
es := -1
N := 1
for {
if N >= 2*1024*1024 {
return StructuralError("RUNA/RUNB overflow")
}
if nextSym == symRUNA {
es += 1 * N
} else {
es += 2 * N
}
N *= 2
nextSym, err = getSymbol()
if err != nil {
return err
}
if nextSym != symRUNA && nextSym != symRUNB {
break
}
}
es++
uc := seqToUnseq[mtfDec.first()]
unzftab[uc] += int32(es)
for es > 0 {
if nblock >= nblockMAX {
return StructuralError("block overflow")
}
r.tt[nblock] = uint32(uc)
nblock++
es--
}
continue
}
if nblock >= nblockMAX {
return StructuralError("block overflow")
}
uc := seqToUnseq[mtfDec.decode(nextSym-1)]
unzftab[uc]++
r.tt[nblock] = uint32(uc)
nblock++
nextSym, err = getSymbol()
if err != nil {
return err
}
}
if origPtr < 0 || origPtr >= nblock {
return StructuralError("bad origPtr")
}
// Build cftab
var cftab [257]int32
for i := 0; i < 256; i++ {
cftab[i+1] = unzftab[i]
}
for i := 1; i <= 256; i++ {
cftab[i] += cftab[i-1]
}
if cftab[256] != int32(nblock) {
return StructuralError("cftab inconsistency")
}
// Compute T^(-1)
{
var c [257]int32
copy(c[:], cftab[:])
for i := 0; i < nblock; i++ {
uc := byte(r.tt[i] & 0xff)
r.tt[c[uc]] |= uint32(i) << 8
c[uc]++
}
}
// Walk the inverse BWT chain and RLE-decode into outBuf.
// This follows the C code's unRLE_obuf_to_output_FAST logic exactly.
r.outBuf = r.outBuf[:0]
blockCRC := crcInit()
tPos := r.tt[origPtr] >> 8
nblockUsed := 0
// Randomization state
var rNToGo, rTPos int32
getByte := func() byte {
b := byte(r.tt[tPos] & 0xff)
tPos = r.tt[tPos] >> 8
nblockUsed++
if blockRandomised {
if rNToGo == 0 {
rNToGo = randNums[rTPos]
rTPos++
if rTPos == 512 {
rTPos = 0
}
}
rNToGo--
if rNToGo == 0 {
b ^= 1
}
}
return b
}
emit := func(b byte, count int) {
for i := 0; i < count; i++ {
blockCRC = crcUpdate(blockCRC, b)
}
for i := 0; i < count; i++ {
r.outBuf = append(r.outBuf, b)
}
}
// State machine matching the C code.
// nblock is the number of entries in the chain. We call getByte() exactly nblock times.
if nblock > 0 {
k0 := getByte() // consumes 1; nblockUsed == 1 after this
for {
// End of block?
if nblockUsed == nblock {
emit(k0, 1)
break
}
// Get next byte
k1 := getByte()
if k1 != k0 {
emit(k0, 1)
k0 = k1
continue
}
// 2nd identical byte
if nblockUsed == nblock {
emit(k0, 2)
break
}
k1 = getByte()
if k1 != k0 {
emit(k0, 2)
k0 = k1
continue
}
// 3rd identical byte
if nblockUsed == nblock {
emit(k0, 3)
break
}
k1 = getByte()
if k1 != k0 {
emit(k0, 3)
k0 = k1
continue
}
// 4th identical byte - next is repeat count
if nblockUsed == nblock {
emit(k0, 4)
break
}
repCount := int(getByte())
emit(k0, repCount+4)
if nblockUsed == nblock {
break
}
k0 = getByte()
}
}
blockCRC = crcFinal(blockCRC)
if blockCRC != storedBlockCRC {
return &ChecksumError{Expected: storedBlockCRC, Got: blockCRC}
}
r.combinedCRC = bits.RotateLeft32(r.combinedCRC, 1) ^ blockCRC
r.outPos = 0
r.needBlock = false
return nil
}