1 | // Code generated by go generate gen_inflate.go. DO NOT EDIT.
|
---|
2 |
|
---|
3 | package flate
|
---|
4 |
|
---|
5 | import (
|
---|
6 | "bufio"
|
---|
7 | "bytes"
|
---|
8 | "fmt"
|
---|
9 | "math/bits"
|
---|
10 | "strings"
|
---|
11 | )
|
---|
12 |
|
---|
13 | // Decode a single Huffman block from f.
|
---|
14 | // hl and hd are the Huffman states for the lit/length values
|
---|
15 | // and the distance values, respectively. If hd == nil, using the
|
---|
16 | // fixed distance encoding associated with fixed Huffman blocks.
|
---|
17 | func (f *decompressor) huffmanBytesBuffer() {
|
---|
18 | const (
|
---|
19 | stateInit = iota // Zero value must be stateInit
|
---|
20 | stateDict
|
---|
21 | )
|
---|
22 | fr := f.r.(*bytes.Buffer)
|
---|
23 | moreBits := func() error {
|
---|
24 | c, err := fr.ReadByte()
|
---|
25 | if err != nil {
|
---|
26 | return noEOF(err)
|
---|
27 | }
|
---|
28 | f.roffset++
|
---|
29 | f.b |= uint32(c) << f.nb
|
---|
30 | f.nb += 8
|
---|
31 | return nil
|
---|
32 | }
|
---|
33 |
|
---|
34 | switch f.stepState {
|
---|
35 | case stateInit:
|
---|
36 | goto readLiteral
|
---|
37 | case stateDict:
|
---|
38 | goto copyHistory
|
---|
39 | }
|
---|
40 |
|
---|
41 | readLiteral:
|
---|
42 | // Read literal and/or (length, distance) according to RFC section 3.2.3.
|
---|
43 | {
|
---|
44 | var v int
|
---|
45 | {
|
---|
46 | // Inlined v, err := f.huffSym(f.hl)
|
---|
47 | // Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
---|
48 | // with single element, huffSym must error on these two edge cases. In both
|
---|
49 | // cases, the chunks slice will be 0 for the invalid sequence, leading it
|
---|
50 | // satisfy the n == 0 check below.
|
---|
51 | n := uint(f.hl.maxRead)
|
---|
52 | // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
---|
53 | // but is smart enough to keep local variables in registers, so use nb and b,
|
---|
54 | // inline call to moreBits and reassign b,nb back to f on return.
|
---|
55 | nb, b := f.nb, f.b
|
---|
56 | for {
|
---|
57 | for nb < n {
|
---|
58 | c, err := fr.ReadByte()
|
---|
59 | if err != nil {
|
---|
60 | f.b = b
|
---|
61 | f.nb = nb
|
---|
62 | f.err = noEOF(err)
|
---|
63 | return
|
---|
64 | }
|
---|
65 | f.roffset++
|
---|
66 | b |= uint32(c) << (nb & 31)
|
---|
67 | nb += 8
|
---|
68 | }
|
---|
69 | chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
|
---|
70 | n = uint(chunk & huffmanCountMask)
|
---|
71 | if n > huffmanChunkBits {
|
---|
72 | chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
|
---|
73 | n = uint(chunk & huffmanCountMask)
|
---|
74 | }
|
---|
75 | if n <= nb {
|
---|
76 | if n == 0 {
|
---|
77 | f.b = b
|
---|
78 | f.nb = nb
|
---|
79 | if debugDecode {
|
---|
80 | fmt.Println("huffsym: n==0")
|
---|
81 | }
|
---|
82 | f.err = CorruptInputError(f.roffset)
|
---|
83 | return
|
---|
84 | }
|
---|
85 | f.b = b >> (n & 31)
|
---|
86 | f.nb = nb - n
|
---|
87 | v = int(chunk >> huffmanValueShift)
|
---|
88 | break
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | var n uint // number of bits extra
|
---|
94 | var length int
|
---|
95 | var err error
|
---|
96 | switch {
|
---|
97 | case v < 256:
|
---|
98 | f.dict.writeByte(byte(v))
|
---|
99 | if f.dict.availWrite() == 0 {
|
---|
100 | f.toRead = f.dict.readFlush()
|
---|
101 | f.step = (*decompressor).huffmanBytesBuffer
|
---|
102 | f.stepState = stateInit
|
---|
103 | return
|
---|
104 | }
|
---|
105 | goto readLiteral
|
---|
106 | case v == 256:
|
---|
107 | f.finishBlock()
|
---|
108 | return
|
---|
109 | // otherwise, reference to older data
|
---|
110 | case v < 265:
|
---|
111 | length = v - (257 - 3)
|
---|
112 | n = 0
|
---|
113 | case v < 269:
|
---|
114 | length = v*2 - (265*2 - 11)
|
---|
115 | n = 1
|
---|
116 | case v < 273:
|
---|
117 | length = v*4 - (269*4 - 19)
|
---|
118 | n = 2
|
---|
119 | case v < 277:
|
---|
120 | length = v*8 - (273*8 - 35)
|
---|
121 | n = 3
|
---|
122 | case v < 281:
|
---|
123 | length = v*16 - (277*16 - 67)
|
---|
124 | n = 4
|
---|
125 | case v < 285:
|
---|
126 | length = v*32 - (281*32 - 131)
|
---|
127 | n = 5
|
---|
128 | case v < maxNumLit:
|
---|
129 | length = 258
|
---|
130 | n = 0
|
---|
131 | default:
|
---|
132 | if debugDecode {
|
---|
133 | fmt.Println(v, ">= maxNumLit")
|
---|
134 | }
|
---|
135 | f.err = CorruptInputError(f.roffset)
|
---|
136 | return
|
---|
137 | }
|
---|
138 | if n > 0 {
|
---|
139 | for f.nb < n {
|
---|
140 | if err = moreBits(); err != nil {
|
---|
141 | if debugDecode {
|
---|
142 | fmt.Println("morebits n>0:", err)
|
---|
143 | }
|
---|
144 | f.err = err
|
---|
145 | return
|
---|
146 | }
|
---|
147 | }
|
---|
148 | length += int(f.b & uint32(1<<n-1))
|
---|
149 | f.b >>= n
|
---|
150 | f.nb -= n
|
---|
151 | }
|
---|
152 |
|
---|
153 | var dist int
|
---|
154 | if f.hd == nil {
|
---|
155 | for f.nb < 5 {
|
---|
156 | if err = moreBits(); err != nil {
|
---|
157 | if debugDecode {
|
---|
158 | fmt.Println("morebits f.nb<5:", err)
|
---|
159 | }
|
---|
160 | f.err = err
|
---|
161 | return
|
---|
162 | }
|
---|
163 | }
|
---|
164 | dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
|
---|
165 | f.b >>= 5
|
---|
166 | f.nb -= 5
|
---|
167 | } else {
|
---|
168 | if dist, err = f.huffSym(f.hd); err != nil {
|
---|
169 | if debugDecode {
|
---|
170 | fmt.Println("huffsym:", err)
|
---|
171 | }
|
---|
172 | f.err = err
|
---|
173 | return
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | switch {
|
---|
178 | case dist < 4:
|
---|
179 | dist++
|
---|
180 | case dist < maxNumDist:
|
---|
181 | nb := uint(dist-2) >> 1
|
---|
182 | // have 1 bit in bottom of dist, need nb more.
|
---|
183 | extra := (dist & 1) << nb
|
---|
184 | for f.nb < nb {
|
---|
185 | if err = moreBits(); err != nil {
|
---|
186 | if debugDecode {
|
---|
187 | fmt.Println("morebits f.nb<nb:", err)
|
---|
188 | }
|
---|
189 | f.err = err
|
---|
190 | return
|
---|
191 | }
|
---|
192 | }
|
---|
193 | extra |= int(f.b & uint32(1<<nb-1))
|
---|
194 | f.b >>= nb
|
---|
195 | f.nb -= nb
|
---|
196 | dist = 1<<(nb+1) + 1 + extra
|
---|
197 | default:
|
---|
198 | if debugDecode {
|
---|
199 | fmt.Println("dist too big:", dist, maxNumDist)
|
---|
200 | }
|
---|
201 | f.err = CorruptInputError(f.roffset)
|
---|
202 | return
|
---|
203 | }
|
---|
204 |
|
---|
205 | // No check on length; encoding can be prescient.
|
---|
206 | if dist > f.dict.histSize() {
|
---|
207 | if debugDecode {
|
---|
208 | fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
|
---|
209 | }
|
---|
210 | f.err = CorruptInputError(f.roffset)
|
---|
211 | return
|
---|
212 | }
|
---|
213 |
|
---|
214 | f.copyLen, f.copyDist = length, dist
|
---|
215 | goto copyHistory
|
---|
216 | }
|
---|
217 |
|
---|
218 | copyHistory:
|
---|
219 | // Perform a backwards copy according to RFC section 3.2.3.
|
---|
220 | {
|
---|
221 | cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
|
---|
222 | if cnt == 0 {
|
---|
223 | cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
|
---|
224 | }
|
---|
225 | f.copyLen -= cnt
|
---|
226 |
|
---|
227 | if f.dict.availWrite() == 0 || f.copyLen > 0 {
|
---|
228 | f.toRead = f.dict.readFlush()
|
---|
229 | f.step = (*decompressor).huffmanBytesBuffer // We need to continue this work
|
---|
230 | f.stepState = stateDict
|
---|
231 | return
|
---|
232 | }
|
---|
233 | goto readLiteral
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | // Decode a single Huffman block from f.
|
---|
238 | // hl and hd are the Huffman states for the lit/length values
|
---|
239 | // and the distance values, respectively. If hd == nil, using the
|
---|
240 | // fixed distance encoding associated with fixed Huffman blocks.
|
---|
241 | func (f *decompressor) huffmanBytesReader() {
|
---|
242 | const (
|
---|
243 | stateInit = iota // Zero value must be stateInit
|
---|
244 | stateDict
|
---|
245 | )
|
---|
246 | fr := f.r.(*bytes.Reader)
|
---|
247 | moreBits := func() error {
|
---|
248 | c, err := fr.ReadByte()
|
---|
249 | if err != nil {
|
---|
250 | return noEOF(err)
|
---|
251 | }
|
---|
252 | f.roffset++
|
---|
253 | f.b |= uint32(c) << f.nb
|
---|
254 | f.nb += 8
|
---|
255 | return nil
|
---|
256 | }
|
---|
257 |
|
---|
258 | switch f.stepState {
|
---|
259 | case stateInit:
|
---|
260 | goto readLiteral
|
---|
261 | case stateDict:
|
---|
262 | goto copyHistory
|
---|
263 | }
|
---|
264 |
|
---|
265 | readLiteral:
|
---|
266 | // Read literal and/or (length, distance) according to RFC section 3.2.3.
|
---|
267 | {
|
---|
268 | var v int
|
---|
269 | {
|
---|
270 | // Inlined v, err := f.huffSym(f.hl)
|
---|
271 | // Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
---|
272 | // with single element, huffSym must error on these two edge cases. In both
|
---|
273 | // cases, the chunks slice will be 0 for the invalid sequence, leading it
|
---|
274 | // satisfy the n == 0 check below.
|
---|
275 | n := uint(f.hl.maxRead)
|
---|
276 | // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
---|
277 | // but is smart enough to keep local variables in registers, so use nb and b,
|
---|
278 | // inline call to moreBits and reassign b,nb back to f on return.
|
---|
279 | nb, b := f.nb, f.b
|
---|
280 | for {
|
---|
281 | for nb < n {
|
---|
282 | c, err := fr.ReadByte()
|
---|
283 | if err != nil {
|
---|
284 | f.b = b
|
---|
285 | f.nb = nb
|
---|
286 | f.err = noEOF(err)
|
---|
287 | return
|
---|
288 | }
|
---|
289 | f.roffset++
|
---|
290 | b |= uint32(c) << (nb & 31)
|
---|
291 | nb += 8
|
---|
292 | }
|
---|
293 | chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
|
---|
294 | n = uint(chunk & huffmanCountMask)
|
---|
295 | if n > huffmanChunkBits {
|
---|
296 | chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
|
---|
297 | n = uint(chunk & huffmanCountMask)
|
---|
298 | }
|
---|
299 | if n <= nb {
|
---|
300 | if n == 0 {
|
---|
301 | f.b = b
|
---|
302 | f.nb = nb
|
---|
303 | if debugDecode {
|
---|
304 | fmt.Println("huffsym: n==0")
|
---|
305 | }
|
---|
306 | f.err = CorruptInputError(f.roffset)
|
---|
307 | return
|
---|
308 | }
|
---|
309 | f.b = b >> (n & 31)
|
---|
310 | f.nb = nb - n
|
---|
311 | v = int(chunk >> huffmanValueShift)
|
---|
312 | break
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | var n uint // number of bits extra
|
---|
318 | var length int
|
---|
319 | var err error
|
---|
320 | switch {
|
---|
321 | case v < 256:
|
---|
322 | f.dict.writeByte(byte(v))
|
---|
323 | if f.dict.availWrite() == 0 {
|
---|
324 | f.toRead = f.dict.readFlush()
|
---|
325 | f.step = (*decompressor).huffmanBytesReader
|
---|
326 | f.stepState = stateInit
|
---|
327 | return
|
---|
328 | }
|
---|
329 | goto readLiteral
|
---|
330 | case v == 256:
|
---|
331 | f.finishBlock()
|
---|
332 | return
|
---|
333 | // otherwise, reference to older data
|
---|
334 | case v < 265:
|
---|
335 | length = v - (257 - 3)
|
---|
336 | n = 0
|
---|
337 | case v < 269:
|
---|
338 | length = v*2 - (265*2 - 11)
|
---|
339 | n = 1
|
---|
340 | case v < 273:
|
---|
341 | length = v*4 - (269*4 - 19)
|
---|
342 | n = 2
|
---|
343 | case v < 277:
|
---|
344 | length = v*8 - (273*8 - 35)
|
---|
345 | n = 3
|
---|
346 | case v < 281:
|
---|
347 | length = v*16 - (277*16 - 67)
|
---|
348 | n = 4
|
---|
349 | case v < 285:
|
---|
350 | length = v*32 - (281*32 - 131)
|
---|
351 | n = 5
|
---|
352 | case v < maxNumLit:
|
---|
353 | length = 258
|
---|
354 | n = 0
|
---|
355 | default:
|
---|
356 | if debugDecode {
|
---|
357 | fmt.Println(v, ">= maxNumLit")
|
---|
358 | }
|
---|
359 | f.err = CorruptInputError(f.roffset)
|
---|
360 | return
|
---|
361 | }
|
---|
362 | if n > 0 {
|
---|
363 | for f.nb < n {
|
---|
364 | if err = moreBits(); err != nil {
|
---|
365 | if debugDecode {
|
---|
366 | fmt.Println("morebits n>0:", err)
|
---|
367 | }
|
---|
368 | f.err = err
|
---|
369 | return
|
---|
370 | }
|
---|
371 | }
|
---|
372 | length += int(f.b & uint32(1<<n-1))
|
---|
373 | f.b >>= n
|
---|
374 | f.nb -= n
|
---|
375 | }
|
---|
376 |
|
---|
377 | var dist int
|
---|
378 | if f.hd == nil {
|
---|
379 | for f.nb < 5 {
|
---|
380 | if err = moreBits(); err != nil {
|
---|
381 | if debugDecode {
|
---|
382 | fmt.Println("morebits f.nb<5:", err)
|
---|
383 | }
|
---|
384 | f.err = err
|
---|
385 | return
|
---|
386 | }
|
---|
387 | }
|
---|
388 | dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
|
---|
389 | f.b >>= 5
|
---|
390 | f.nb -= 5
|
---|
391 | } else {
|
---|
392 | if dist, err = f.huffSym(f.hd); err != nil {
|
---|
393 | if debugDecode {
|
---|
394 | fmt.Println("huffsym:", err)
|
---|
395 | }
|
---|
396 | f.err = err
|
---|
397 | return
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | switch {
|
---|
402 | case dist < 4:
|
---|
403 | dist++
|
---|
404 | case dist < maxNumDist:
|
---|
405 | nb := uint(dist-2) >> 1
|
---|
406 | // have 1 bit in bottom of dist, need nb more.
|
---|
407 | extra := (dist & 1) << nb
|
---|
408 | for f.nb < nb {
|
---|
409 | if err = moreBits(); err != nil {
|
---|
410 | if debugDecode {
|
---|
411 | fmt.Println("morebits f.nb<nb:", err)
|
---|
412 | }
|
---|
413 | f.err = err
|
---|
414 | return
|
---|
415 | }
|
---|
416 | }
|
---|
417 | extra |= int(f.b & uint32(1<<nb-1))
|
---|
418 | f.b >>= nb
|
---|
419 | f.nb -= nb
|
---|
420 | dist = 1<<(nb+1) + 1 + extra
|
---|
421 | default:
|
---|
422 | if debugDecode {
|
---|
423 | fmt.Println("dist too big:", dist, maxNumDist)
|
---|
424 | }
|
---|
425 | f.err = CorruptInputError(f.roffset)
|
---|
426 | return
|
---|
427 | }
|
---|
428 |
|
---|
429 | // No check on length; encoding can be prescient.
|
---|
430 | if dist > f.dict.histSize() {
|
---|
431 | if debugDecode {
|
---|
432 | fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
|
---|
433 | }
|
---|
434 | f.err = CorruptInputError(f.roffset)
|
---|
435 | return
|
---|
436 | }
|
---|
437 |
|
---|
438 | f.copyLen, f.copyDist = length, dist
|
---|
439 | goto copyHistory
|
---|
440 | }
|
---|
441 |
|
---|
442 | copyHistory:
|
---|
443 | // Perform a backwards copy according to RFC section 3.2.3.
|
---|
444 | {
|
---|
445 | cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
|
---|
446 | if cnt == 0 {
|
---|
447 | cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
|
---|
448 | }
|
---|
449 | f.copyLen -= cnt
|
---|
450 |
|
---|
451 | if f.dict.availWrite() == 0 || f.copyLen > 0 {
|
---|
452 | f.toRead = f.dict.readFlush()
|
---|
453 | f.step = (*decompressor).huffmanBytesReader // We need to continue this work
|
---|
454 | f.stepState = stateDict
|
---|
455 | return
|
---|
456 | }
|
---|
457 | goto readLiteral
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | // Decode a single Huffman block from f.
|
---|
462 | // hl and hd are the Huffman states for the lit/length values
|
---|
463 | // and the distance values, respectively. If hd == nil, using the
|
---|
464 | // fixed distance encoding associated with fixed Huffman blocks.
|
---|
465 | func (f *decompressor) huffmanBufioReader() {
|
---|
466 | const (
|
---|
467 | stateInit = iota // Zero value must be stateInit
|
---|
468 | stateDict
|
---|
469 | )
|
---|
470 | fr := f.r.(*bufio.Reader)
|
---|
471 | moreBits := func() error {
|
---|
472 | c, err := fr.ReadByte()
|
---|
473 | if err != nil {
|
---|
474 | return noEOF(err)
|
---|
475 | }
|
---|
476 | f.roffset++
|
---|
477 | f.b |= uint32(c) << f.nb
|
---|
478 | f.nb += 8
|
---|
479 | return nil
|
---|
480 | }
|
---|
481 |
|
---|
482 | switch f.stepState {
|
---|
483 | case stateInit:
|
---|
484 | goto readLiteral
|
---|
485 | case stateDict:
|
---|
486 | goto copyHistory
|
---|
487 | }
|
---|
488 |
|
---|
489 | readLiteral:
|
---|
490 | // Read literal and/or (length, distance) according to RFC section 3.2.3.
|
---|
491 | {
|
---|
492 | var v int
|
---|
493 | {
|
---|
494 | // Inlined v, err := f.huffSym(f.hl)
|
---|
495 | // Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
---|
496 | // with single element, huffSym must error on these two edge cases. In both
|
---|
497 | // cases, the chunks slice will be 0 for the invalid sequence, leading it
|
---|
498 | // satisfy the n == 0 check below.
|
---|
499 | n := uint(f.hl.maxRead)
|
---|
500 | // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
---|
501 | // but is smart enough to keep local variables in registers, so use nb and b,
|
---|
502 | // inline call to moreBits and reassign b,nb back to f on return.
|
---|
503 | nb, b := f.nb, f.b
|
---|
504 | for {
|
---|
505 | for nb < n {
|
---|
506 | c, err := fr.ReadByte()
|
---|
507 | if err != nil {
|
---|
508 | f.b = b
|
---|
509 | f.nb = nb
|
---|
510 | f.err = noEOF(err)
|
---|
511 | return
|
---|
512 | }
|
---|
513 | f.roffset++
|
---|
514 | b |= uint32(c) << (nb & 31)
|
---|
515 | nb += 8
|
---|
516 | }
|
---|
517 | chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
|
---|
518 | n = uint(chunk & huffmanCountMask)
|
---|
519 | if n > huffmanChunkBits {
|
---|
520 | chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
|
---|
521 | n = uint(chunk & huffmanCountMask)
|
---|
522 | }
|
---|
523 | if n <= nb {
|
---|
524 | if n == 0 {
|
---|
525 | f.b = b
|
---|
526 | f.nb = nb
|
---|
527 | if debugDecode {
|
---|
528 | fmt.Println("huffsym: n==0")
|
---|
529 | }
|
---|
530 | f.err = CorruptInputError(f.roffset)
|
---|
531 | return
|
---|
532 | }
|
---|
533 | f.b = b >> (n & 31)
|
---|
534 | f.nb = nb - n
|
---|
535 | v = int(chunk >> huffmanValueShift)
|
---|
536 | break
|
---|
537 | }
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | var n uint // number of bits extra
|
---|
542 | var length int
|
---|
543 | var err error
|
---|
544 | switch {
|
---|
545 | case v < 256:
|
---|
546 | f.dict.writeByte(byte(v))
|
---|
547 | if f.dict.availWrite() == 0 {
|
---|
548 | f.toRead = f.dict.readFlush()
|
---|
549 | f.step = (*decompressor).huffmanBufioReader
|
---|
550 | f.stepState = stateInit
|
---|
551 | return
|
---|
552 | }
|
---|
553 | goto readLiteral
|
---|
554 | case v == 256:
|
---|
555 | f.finishBlock()
|
---|
556 | return
|
---|
557 | // otherwise, reference to older data
|
---|
558 | case v < 265:
|
---|
559 | length = v - (257 - 3)
|
---|
560 | n = 0
|
---|
561 | case v < 269:
|
---|
562 | length = v*2 - (265*2 - 11)
|
---|
563 | n = 1
|
---|
564 | case v < 273:
|
---|
565 | length = v*4 - (269*4 - 19)
|
---|
566 | n = 2
|
---|
567 | case v < 277:
|
---|
568 | length = v*8 - (273*8 - 35)
|
---|
569 | n = 3
|
---|
570 | case v < 281:
|
---|
571 | length = v*16 - (277*16 - 67)
|
---|
572 | n = 4
|
---|
573 | case v < 285:
|
---|
574 | length = v*32 - (281*32 - 131)
|
---|
575 | n = 5
|
---|
576 | case v < maxNumLit:
|
---|
577 | length = 258
|
---|
578 | n = 0
|
---|
579 | default:
|
---|
580 | if debugDecode {
|
---|
581 | fmt.Println(v, ">= maxNumLit")
|
---|
582 | }
|
---|
583 | f.err = CorruptInputError(f.roffset)
|
---|
584 | return
|
---|
585 | }
|
---|
586 | if n > 0 {
|
---|
587 | for f.nb < n {
|
---|
588 | if err = moreBits(); err != nil {
|
---|
589 | if debugDecode {
|
---|
590 | fmt.Println("morebits n>0:", err)
|
---|
591 | }
|
---|
592 | f.err = err
|
---|
593 | return
|
---|
594 | }
|
---|
595 | }
|
---|
596 | length += int(f.b & uint32(1<<n-1))
|
---|
597 | f.b >>= n
|
---|
598 | f.nb -= n
|
---|
599 | }
|
---|
600 |
|
---|
601 | var dist int
|
---|
602 | if f.hd == nil {
|
---|
603 | for f.nb < 5 {
|
---|
604 | if err = moreBits(); err != nil {
|
---|
605 | if debugDecode {
|
---|
606 | fmt.Println("morebits f.nb<5:", err)
|
---|
607 | }
|
---|
608 | f.err = err
|
---|
609 | return
|
---|
610 | }
|
---|
611 | }
|
---|
612 | dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
|
---|
613 | f.b >>= 5
|
---|
614 | f.nb -= 5
|
---|
615 | } else {
|
---|
616 | if dist, err = f.huffSym(f.hd); err != nil {
|
---|
617 | if debugDecode {
|
---|
618 | fmt.Println("huffsym:", err)
|
---|
619 | }
|
---|
620 | f.err = err
|
---|
621 | return
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | switch {
|
---|
626 | case dist < 4:
|
---|
627 | dist++
|
---|
628 | case dist < maxNumDist:
|
---|
629 | nb := uint(dist-2) >> 1
|
---|
630 | // have 1 bit in bottom of dist, need nb more.
|
---|
631 | extra := (dist & 1) << nb
|
---|
632 | for f.nb < nb {
|
---|
633 | if err = moreBits(); err != nil {
|
---|
634 | if debugDecode {
|
---|
635 | fmt.Println("morebits f.nb<nb:", err)
|
---|
636 | }
|
---|
637 | f.err = err
|
---|
638 | return
|
---|
639 | }
|
---|
640 | }
|
---|
641 | extra |= int(f.b & uint32(1<<nb-1))
|
---|
642 | f.b >>= nb
|
---|
643 | f.nb -= nb
|
---|
644 | dist = 1<<(nb+1) + 1 + extra
|
---|
645 | default:
|
---|
646 | if debugDecode {
|
---|
647 | fmt.Println("dist too big:", dist, maxNumDist)
|
---|
648 | }
|
---|
649 | f.err = CorruptInputError(f.roffset)
|
---|
650 | return
|
---|
651 | }
|
---|
652 |
|
---|
653 | // No check on length; encoding can be prescient.
|
---|
654 | if dist > f.dict.histSize() {
|
---|
655 | if debugDecode {
|
---|
656 | fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
|
---|
657 | }
|
---|
658 | f.err = CorruptInputError(f.roffset)
|
---|
659 | return
|
---|
660 | }
|
---|
661 |
|
---|
662 | f.copyLen, f.copyDist = length, dist
|
---|
663 | goto copyHistory
|
---|
664 | }
|
---|
665 |
|
---|
666 | copyHistory:
|
---|
667 | // Perform a backwards copy according to RFC section 3.2.3.
|
---|
668 | {
|
---|
669 | cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
|
---|
670 | if cnt == 0 {
|
---|
671 | cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
|
---|
672 | }
|
---|
673 | f.copyLen -= cnt
|
---|
674 |
|
---|
675 | if f.dict.availWrite() == 0 || f.copyLen > 0 {
|
---|
676 | f.toRead = f.dict.readFlush()
|
---|
677 | f.step = (*decompressor).huffmanBufioReader // We need to continue this work
|
---|
678 | f.stepState = stateDict
|
---|
679 | return
|
---|
680 | }
|
---|
681 | goto readLiteral
|
---|
682 | }
|
---|
683 | }
|
---|
684 |
|
---|
685 | // Decode a single Huffman block from f.
|
---|
686 | // hl and hd are the Huffman states for the lit/length values
|
---|
687 | // and the distance values, respectively. If hd == nil, using the
|
---|
688 | // fixed distance encoding associated with fixed Huffman blocks.
|
---|
689 | func (f *decompressor) huffmanStringsReader() {
|
---|
690 | const (
|
---|
691 | stateInit = iota // Zero value must be stateInit
|
---|
692 | stateDict
|
---|
693 | )
|
---|
694 | fr := f.r.(*strings.Reader)
|
---|
695 | moreBits := func() error {
|
---|
696 | c, err := fr.ReadByte()
|
---|
697 | if err != nil {
|
---|
698 | return noEOF(err)
|
---|
699 | }
|
---|
700 | f.roffset++
|
---|
701 | f.b |= uint32(c) << f.nb
|
---|
702 | f.nb += 8
|
---|
703 | return nil
|
---|
704 | }
|
---|
705 |
|
---|
706 | switch f.stepState {
|
---|
707 | case stateInit:
|
---|
708 | goto readLiteral
|
---|
709 | case stateDict:
|
---|
710 | goto copyHistory
|
---|
711 | }
|
---|
712 |
|
---|
713 | readLiteral:
|
---|
714 | // Read literal and/or (length, distance) according to RFC section 3.2.3.
|
---|
715 | {
|
---|
716 | var v int
|
---|
717 | {
|
---|
718 | // Inlined v, err := f.huffSym(f.hl)
|
---|
719 | // Since a huffmanDecoder can be empty or be composed of a degenerate tree
|
---|
720 | // with single element, huffSym must error on these two edge cases. In both
|
---|
721 | // cases, the chunks slice will be 0 for the invalid sequence, leading it
|
---|
722 | // satisfy the n == 0 check below.
|
---|
723 | n := uint(f.hl.maxRead)
|
---|
724 | // Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers,
|
---|
725 | // but is smart enough to keep local variables in registers, so use nb and b,
|
---|
726 | // inline call to moreBits and reassign b,nb back to f on return.
|
---|
727 | nb, b := f.nb, f.b
|
---|
728 | for {
|
---|
729 | for nb < n {
|
---|
730 | c, err := fr.ReadByte()
|
---|
731 | if err != nil {
|
---|
732 | f.b = b
|
---|
733 | f.nb = nb
|
---|
734 | f.err = noEOF(err)
|
---|
735 | return
|
---|
736 | }
|
---|
737 | f.roffset++
|
---|
738 | b |= uint32(c) << (nb & 31)
|
---|
739 | nb += 8
|
---|
740 | }
|
---|
741 | chunk := f.hl.chunks[b&(huffmanNumChunks-1)]
|
---|
742 | n = uint(chunk & huffmanCountMask)
|
---|
743 | if n > huffmanChunkBits {
|
---|
744 | chunk = f.hl.links[chunk>>huffmanValueShift][(b>>huffmanChunkBits)&f.hl.linkMask]
|
---|
745 | n = uint(chunk & huffmanCountMask)
|
---|
746 | }
|
---|
747 | if n <= nb {
|
---|
748 | if n == 0 {
|
---|
749 | f.b = b
|
---|
750 | f.nb = nb
|
---|
751 | if debugDecode {
|
---|
752 | fmt.Println("huffsym: n==0")
|
---|
753 | }
|
---|
754 | f.err = CorruptInputError(f.roffset)
|
---|
755 | return
|
---|
756 | }
|
---|
757 | f.b = b >> (n & 31)
|
---|
758 | f.nb = nb - n
|
---|
759 | v = int(chunk >> huffmanValueShift)
|
---|
760 | break
|
---|
761 | }
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | var n uint // number of bits extra
|
---|
766 | var length int
|
---|
767 | var err error
|
---|
768 | switch {
|
---|
769 | case v < 256:
|
---|
770 | f.dict.writeByte(byte(v))
|
---|
771 | if f.dict.availWrite() == 0 {
|
---|
772 | f.toRead = f.dict.readFlush()
|
---|
773 | f.step = (*decompressor).huffmanStringsReader
|
---|
774 | f.stepState = stateInit
|
---|
775 | return
|
---|
776 | }
|
---|
777 | goto readLiteral
|
---|
778 | case v == 256:
|
---|
779 | f.finishBlock()
|
---|
780 | return
|
---|
781 | // otherwise, reference to older data
|
---|
782 | case v < 265:
|
---|
783 | length = v - (257 - 3)
|
---|
784 | n = 0
|
---|
785 | case v < 269:
|
---|
786 | length = v*2 - (265*2 - 11)
|
---|
787 | n = 1
|
---|
788 | case v < 273:
|
---|
789 | length = v*4 - (269*4 - 19)
|
---|
790 | n = 2
|
---|
791 | case v < 277:
|
---|
792 | length = v*8 - (273*8 - 35)
|
---|
793 | n = 3
|
---|
794 | case v < 281:
|
---|
795 | length = v*16 - (277*16 - 67)
|
---|
796 | n = 4
|
---|
797 | case v < 285:
|
---|
798 | length = v*32 - (281*32 - 131)
|
---|
799 | n = 5
|
---|
800 | case v < maxNumLit:
|
---|
801 | length = 258
|
---|
802 | n = 0
|
---|
803 | default:
|
---|
804 | if debugDecode {
|
---|
805 | fmt.Println(v, ">= maxNumLit")
|
---|
806 | }
|
---|
807 | f.err = CorruptInputError(f.roffset)
|
---|
808 | return
|
---|
809 | }
|
---|
810 | if n > 0 {
|
---|
811 | for f.nb < n {
|
---|
812 | if err = moreBits(); err != nil {
|
---|
813 | if debugDecode {
|
---|
814 | fmt.Println("morebits n>0:", err)
|
---|
815 | }
|
---|
816 | f.err = err
|
---|
817 | return
|
---|
818 | }
|
---|
819 | }
|
---|
820 | length += int(f.b & uint32(1<<n-1))
|
---|
821 | f.b >>= n
|
---|
822 | f.nb -= n
|
---|
823 | }
|
---|
824 |
|
---|
825 | var dist int
|
---|
826 | if f.hd == nil {
|
---|
827 | for f.nb < 5 {
|
---|
828 | if err = moreBits(); err != nil {
|
---|
829 | if debugDecode {
|
---|
830 | fmt.Println("morebits f.nb<5:", err)
|
---|
831 | }
|
---|
832 | f.err = err
|
---|
833 | return
|
---|
834 | }
|
---|
835 | }
|
---|
836 | dist = int(bits.Reverse8(uint8(f.b & 0x1F << 3)))
|
---|
837 | f.b >>= 5
|
---|
838 | f.nb -= 5
|
---|
839 | } else {
|
---|
840 | if dist, err = f.huffSym(f.hd); err != nil {
|
---|
841 | if debugDecode {
|
---|
842 | fmt.Println("huffsym:", err)
|
---|
843 | }
|
---|
844 | f.err = err
|
---|
845 | return
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | switch {
|
---|
850 | case dist < 4:
|
---|
851 | dist++
|
---|
852 | case dist < maxNumDist:
|
---|
853 | nb := uint(dist-2) >> 1
|
---|
854 | // have 1 bit in bottom of dist, need nb more.
|
---|
855 | extra := (dist & 1) << nb
|
---|
856 | for f.nb < nb {
|
---|
857 | if err = moreBits(); err != nil {
|
---|
858 | if debugDecode {
|
---|
859 | fmt.Println("morebits f.nb<nb:", err)
|
---|
860 | }
|
---|
861 | f.err = err
|
---|
862 | return
|
---|
863 | }
|
---|
864 | }
|
---|
865 | extra |= int(f.b & uint32(1<<nb-1))
|
---|
866 | f.b >>= nb
|
---|
867 | f.nb -= nb
|
---|
868 | dist = 1<<(nb+1) + 1 + extra
|
---|
869 | default:
|
---|
870 | if debugDecode {
|
---|
871 | fmt.Println("dist too big:", dist, maxNumDist)
|
---|
872 | }
|
---|
873 | f.err = CorruptInputError(f.roffset)
|
---|
874 | return
|
---|
875 | }
|
---|
876 |
|
---|
877 | // No check on length; encoding can be prescient.
|
---|
878 | if dist > f.dict.histSize() {
|
---|
879 | if debugDecode {
|
---|
880 | fmt.Println("dist > f.dict.histSize():", dist, f.dict.histSize())
|
---|
881 | }
|
---|
882 | f.err = CorruptInputError(f.roffset)
|
---|
883 | return
|
---|
884 | }
|
---|
885 |
|
---|
886 | f.copyLen, f.copyDist = length, dist
|
---|
887 | goto copyHistory
|
---|
888 | }
|
---|
889 |
|
---|
890 | copyHistory:
|
---|
891 | // Perform a backwards copy according to RFC section 3.2.3.
|
---|
892 | {
|
---|
893 | cnt := f.dict.tryWriteCopy(f.copyDist, f.copyLen)
|
---|
894 | if cnt == 0 {
|
---|
895 | cnt = f.dict.writeCopy(f.copyDist, f.copyLen)
|
---|
896 | }
|
---|
897 | f.copyLen -= cnt
|
---|
898 |
|
---|
899 | if f.dict.availWrite() == 0 || f.copyLen > 0 {
|
---|
900 | f.toRead = f.dict.readFlush()
|
---|
901 | f.step = (*decompressor).huffmanStringsReader // We need to continue this work
|
---|
902 | f.stepState = stateDict
|
---|
903 | return
|
---|
904 | }
|
---|
905 | goto readLiteral
|
---|
906 | }
|
---|
907 | }
|
---|
908 |
|
---|
909 | func (f *decompressor) huffmanBlockDecoder() func() {
|
---|
910 | switch f.r.(type) {
|
---|
911 | case *bytes.Buffer:
|
---|
912 | return f.huffmanBytesBuffer
|
---|
913 | case *bytes.Reader:
|
---|
914 | return f.huffmanBytesReader
|
---|
915 | case *bufio.Reader:
|
---|
916 | return f.huffmanBufioReader
|
---|
917 | case *strings.Reader:
|
---|
918 | return f.huffmanStringsReader
|
---|
919 | default:
|
---|
920 | return f.huffmanBlockGeneric
|
---|
921 | }
|
---|
922 | }
|
---|