forked from ycl2045/rardecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.go
208 lines (183 loc) · 3.88 KB
/
huffman.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
package rardecode
import (
"errors"
"io"
)
const (
maxCodeLength = 15 // maximum code length in bits
maxQuickBits = 10
maxQuickSize = 1 << maxQuickBits
)
var (
errHuffDecodeFailed = errors.New("rardecode: huffman decode failed")
errInvalidLengthTable = errors.New("rardecode: invalid huffman code length table")
)
type huffmanDecoder struct {
limit [maxCodeLength + 1]int
pos [maxCodeLength + 1]int
symbol []int
min uint
quickbits uint
quicklen [maxQuickSize]uint
quicksym [maxQuickSize]int
}
func (h *huffmanDecoder) init(codeLengths []byte) {
var count [maxCodeLength + 1]int
for _, n := range codeLengths {
if n == 0 {
continue
}
count[n]++
}
h.pos[0] = 0
h.limit[0] = 0
h.min = 0
for i := uint(1); i <= maxCodeLength; i++ {
h.limit[i] = h.limit[i-1] + count[i]<<(maxCodeLength-i)
h.pos[i] = h.pos[i-1] + count[i-1]
if h.min == 0 && h.limit[i] > 0 {
h.min = i
}
}
if cap(h.symbol) >= len(codeLengths) {
h.symbol = h.symbol[:len(codeLengths)]
for i := range h.symbol {
h.symbol[i] = 0
}
} else {
h.symbol = make([]int, len(codeLengths))
}
copy(count[:], h.pos[:])
for i, n := range codeLengths {
if n != 0 {
h.symbol[count[n]] = i
count[n]++
}
}
if len(codeLengths) >= 298 {
h.quickbits = maxQuickBits
} else {
h.quickbits = maxQuickBits - 3
}
bits := uint(1)
for i := 0; i < 1<<h.quickbits; i++ {
v := i << (maxCodeLength - h.quickbits)
for v >= h.limit[bits] && bits < maxCodeLength {
bits++
}
h.quicklen[i] = bits
dist := v - h.limit[bits-1]
dist >>= (maxCodeLength - bits)
pos := h.pos[bits] + dist
if pos < len(h.symbol) {
h.quicksym[i] = h.symbol[pos]
} else {
h.quicksym[i] = 0
}
}
}
func (h *huffmanDecoder) readSym(r bitReader) (int, error) {
bits := uint(maxCodeLength)
v, err := r.readBits(maxCodeLength)
if err != nil {
if err != io.EOF {
return 0, err
}
// fall back to 1 bit at a time if we read past EOF
for i := uint(1); i <= maxCodeLength; i++ {
b, err := r.readBits(1)
if err != nil {
return 0, err // not enough bits return error
}
v |= b << (maxCodeLength - i)
if v < h.limit[i] {
bits = i
break
}
}
} else {
if v < h.limit[h.quickbits] {
i := v >> (maxCodeLength - h.quickbits)
r.unreadBits(maxCodeLength - h.quicklen[i])
return h.quicksym[i], nil
}
for i, n := range h.limit[h.min:] {
if v < n {
bits = h.min + uint(i)
r.unreadBits(maxCodeLength - bits)
break
}
}
}
dist := v - h.limit[bits-1]
dist >>= maxCodeLength - bits
pos := h.pos[bits] + dist
if pos > len(h.symbol) {
return 0, errHuffDecodeFailed
}
return h.symbol[pos], nil
}
// readCodeLengthTable reads a new code length table into codeLength from br.
// If addOld is set the old table is added to the new one.
func readCodeLengthTable(br bitReader, codeLength []byte, addOld bool) error {
var bitlength [20]byte
for i := 0; i < len(bitlength); i++ {
n, err := br.readBits(4)
if err != nil {
return err
}
if n == 0xf {
cnt, err := br.readBits(4)
if err != nil {
return err
}
if cnt > 0 {
// array already zero'd dont need to explicitly set
i += cnt + 1
continue
}
}
bitlength[i] = byte(n)
}
var bl huffmanDecoder
bl.init(bitlength[:])
for i := 0; i < len(codeLength); i++ {
l, err := bl.readSym(br)
if err != nil {
return err
}
if l < 16 {
if addOld {
codeLength[i] = (codeLength[i] + byte(l)) & 0xf
} else {
codeLength[i] = byte(l)
}
continue
}
var count int
var value byte
switch l {
case 16, 18:
count, err = br.readBits(3)
count += 3
default:
count, err = br.readBits(7)
count += 11
}
if err != nil {
return err
}
if l < 18 {
if i == 0 {
return errInvalidLengthTable
}
value = codeLength[i-1]
}
for ; count > 0 && i < len(codeLength); i++ {
codeLength[i] = value
count--
}
i--
}
return nil
}