| 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 |
"github.com/klauspost/compress/internal/regmask" |
| 13 |
) |
| 14 |
|
| 15 |
func peekBufio(fr *bufio.Reader) ([]byte, error) { |
| 16 |
if fr.Buffered() == 0 { |
| 17 |
if _, err := fr.Peek(1); err != nil && fr.Buffered() == 0 { |
| 18 |
return nil, err |
| 19 |
} |
| 20 |
} |
| 21 |
return fr.Peek(fr.Buffered()) |
| 22 |
} |
| 23 |
|
| 24 |
// Decode a single Huffman block from f. |
| 25 |
// hl and hd are the Huffman states for the lit/length values |
| 26 |
// and the distance values, respectively. If hd == nil, using the |
| 27 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 28 |
func (f *decompressor) huffmanBytesBuffer() { |
| 29 |
const ( |
| 30 |
stateInit = iota // Zero value must be stateInit |
| 31 |
stateDict |
| 32 |
) |
| 33 |
fr := f.r.(*bytes.Buffer) |
| 34 |
|
| 35 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 36 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 37 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 38 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 39 |
|
| 40 |
switch f.stepState { |
| 41 |
case stateInit: |
| 42 |
goto readLiteral |
| 43 |
case stateDict: |
| 44 |
goto copyHistory |
| 45 |
} |
| 46 |
|
| 47 |
readLiteral: |
| 48 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 49 |
{ |
| 50 |
var v int |
| 51 |
{ |
| 52 |
// Inlined v, err := f.huffSym(f.hl) |
| 53 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 54 |
// with single element, huffSym must error on these two edge cases. In both |
| 55 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 56 |
// satisfy the n == 0 check below. |
| 57 |
n := uint(f.hl.maxRead) |
| 58 |
for { |
| 59 |
for fnb < n { |
| 60 |
c, err := fr.ReadByte() |
| 61 |
if err != nil { |
| 62 |
f.b, f.nb = fb, fnb |
| 63 |
f.err = noEOF(err) |
| 64 |
return |
| 65 |
} |
| 66 |
f.roffset++ |
| 67 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 68 |
fnb += 8 |
| 69 |
} |
| 70 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 71 |
n = uint(chunk & huffmanCountMask) |
| 72 |
if n > huffmanChunkBits { |
| 73 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 74 |
n = uint(chunk & huffmanCountMask) |
| 75 |
} |
| 76 |
if n <= fnb { |
| 77 |
if n == 0 { |
| 78 |
f.b, f.nb = fb, fnb |
| 79 |
if debugDecode { |
| 80 |
fmt.Println("huffsym: n==0") |
| 81 |
} |
| 82 |
f.err = CorruptInputError(f.roffset) |
| 83 |
return |
| 84 |
} |
| 85 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 86 |
fnb = fnb - n |
| 87 |
v = int(chunk >> huffmanValueShift) |
| 88 |
break |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
var length int |
| 94 |
switch { |
| 95 |
case v < 256: |
| 96 |
dict.writeByte(byte(v)) |
| 97 |
if dict.availWrite() == 0 { |
| 98 |
f.toRead = dict.readFlush() |
| 99 |
f.step = huffmanBytesBuffer |
| 100 |
f.stepState = stateInit |
| 101 |
f.b, f.nb = fb, fnb |
| 102 |
return |
| 103 |
} |
| 104 |
goto readLiteral |
| 105 |
case v == 256: |
| 106 |
f.b, f.nb = fb, fnb |
| 107 |
f.finishBlock() |
| 108 |
return |
| 109 |
// otherwise, reference to older data |
| 110 |
case v < 265: |
| 111 |
length = v - (257 - 3) |
| 112 |
case v < maxNumLit: |
| 113 |
val := decCodeToLen[(v - 257)] |
| 114 |
length = int(val.length) + 3 |
| 115 |
n := uint(val.extra) |
| 116 |
for fnb < n { |
| 117 |
c, err := fr.ReadByte() |
| 118 |
if err != nil { |
| 119 |
f.b, f.nb = fb, fnb |
| 120 |
if debugDecode { |
| 121 |
fmt.Println("morebits n>0:", err) |
| 122 |
} |
| 123 |
f.err = err |
| 124 |
return |
| 125 |
} |
| 126 |
f.roffset++ |
| 127 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 128 |
fnb += 8 |
| 129 |
} |
| 130 |
length += int(fb & bitMask32[n]) |
| 131 |
fb >>= n & regmask.Shift32ByUint |
| 132 |
fnb -= n |
| 133 |
default: |
| 134 |
if debugDecode { |
| 135 |
fmt.Println(v, ">= maxNumLit") |
| 136 |
} |
| 137 |
f.err = CorruptInputError(f.roffset) |
| 138 |
f.b, f.nb = fb, fnb |
| 139 |
return |
| 140 |
} |
| 141 |
|
| 142 |
var dist uint32 |
| 143 |
if f.hd == nil { |
| 144 |
for fnb < 5 { |
| 145 |
c, err := fr.ReadByte() |
| 146 |
if err != nil { |
| 147 |
f.b, f.nb = fb, fnb |
| 148 |
if debugDecode { |
| 149 |
fmt.Println("morebits f.nb<5:", err) |
| 150 |
} |
| 151 |
f.err = err |
| 152 |
return |
| 153 |
} |
| 154 |
f.roffset++ |
| 155 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 156 |
fnb += 8 |
| 157 |
} |
| 158 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 159 |
fb >>= 5 |
| 160 |
fnb -= 5 |
| 161 |
} else { |
| 162 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 163 |
// with single element, huffSym must error on these two edge cases. In both |
| 164 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 165 |
// satisfy the n == 0 check below. |
| 166 |
n := uint(f.hd.maxRead) |
| 167 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 168 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 169 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 170 |
for { |
| 171 |
for fnb < n { |
| 172 |
c, err := fr.ReadByte() |
| 173 |
if err != nil { |
| 174 |
f.b, f.nb = fb, fnb |
| 175 |
f.err = noEOF(err) |
| 176 |
return |
| 177 |
} |
| 178 |
f.roffset++ |
| 179 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 180 |
fnb += 8 |
| 181 |
} |
| 182 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 183 |
n = uint(chunk & huffmanCountMask) |
| 184 |
if n > huffmanChunkBits { |
| 185 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 186 |
n = uint(chunk & huffmanCountMask) |
| 187 |
} |
| 188 |
if n <= fnb { |
| 189 |
if n == 0 { |
| 190 |
f.b, f.nb = fb, fnb |
| 191 |
if debugDecode { |
| 192 |
fmt.Println("huffsym: n==0") |
| 193 |
} |
| 194 |
f.err = CorruptInputError(f.roffset) |
| 195 |
return |
| 196 |
} |
| 197 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 198 |
fnb = fnb - n |
| 199 |
dist = uint32(chunk >> huffmanValueShift) |
| 200 |
break |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
switch { |
| 206 |
case dist < 4: |
| 207 |
dist++ |
| 208 |
case dist < maxNumDist: |
| 209 |
nb := uint(dist-2) >> 1 |
| 210 |
// have 1 bit in bottom of dist, need nb more. |
| 211 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 212 |
for fnb < nb { |
| 213 |
c, err := fr.ReadByte() |
| 214 |
if err != nil { |
| 215 |
f.b, f.nb = fb, fnb |
| 216 |
if debugDecode { |
| 217 |
fmt.Println("morebits f.nb<nb:", err) |
| 218 |
} |
| 219 |
f.err = err |
| 220 |
return |
| 221 |
} |
| 222 |
f.roffset++ |
| 223 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 224 |
fnb += 8 |
| 225 |
} |
| 226 |
extra |= fb & bitMask32[nb] |
| 227 |
fb >>= nb & regmask.Shift32ByUint |
| 228 |
fnb -= nb |
| 229 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 230 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 231 |
default: |
| 232 |
f.b, f.nb = fb, fnb |
| 233 |
if debugDecode { |
| 234 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 235 |
} |
| 236 |
f.err = CorruptInputError(f.roffset) |
| 237 |
return |
| 238 |
} |
| 239 |
|
| 240 |
// No check on length; encoding can be prescient. |
| 241 |
if dist > uint32(dict.histSize()) { |
| 242 |
f.b, f.nb = fb, fnb |
| 243 |
if debugDecode { |
| 244 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 245 |
} |
| 246 |
f.err = CorruptInputError(f.roffset) |
| 247 |
return |
| 248 |
} |
| 249 |
|
| 250 |
f.copyLen, f.copyDist = length, int(dist) |
| 251 |
goto copyHistory |
| 252 |
} |
| 253 |
|
| 254 |
copyHistory: |
| 255 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 256 |
{ |
| 257 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 258 |
if cnt == 0 { |
| 259 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 260 |
} |
| 261 |
f.copyLen -= cnt |
| 262 |
|
| 263 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 264 |
f.toRead = dict.readFlush() |
| 265 |
f.step = huffmanBytesBuffer // We need to continue this work |
| 266 |
f.stepState = stateDict |
| 267 |
f.b, f.nb = fb, fnb |
| 268 |
return |
| 269 |
} |
| 270 |
goto readLiteral |
| 271 |
} |
| 272 |
// Not reached |
| 273 |
} |
| 274 |
|
| 275 |
// Decode a single Huffman block from f. |
| 276 |
// hl and hd are the Huffman states for the lit/length values |
| 277 |
// and the distance values, respectively. If hd == nil, using the |
| 278 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 279 |
func (f *decompressor) huffmanBytesReader() { |
| 280 |
const ( |
| 281 |
stateInit = iota // Zero value must be stateInit |
| 282 |
stateDict |
| 283 |
) |
| 284 |
fr := f.r.(*bytes.Reader) |
| 285 |
|
| 286 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 287 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 288 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 289 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 290 |
|
| 291 |
switch f.stepState { |
| 292 |
case stateInit: |
| 293 |
goto readLiteral |
| 294 |
case stateDict: |
| 295 |
goto copyHistory |
| 296 |
} |
| 297 |
|
| 298 |
readLiteral: |
| 299 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 300 |
{ |
| 301 |
var v int |
| 302 |
{ |
| 303 |
// Inlined v, err := f.huffSym(f.hl) |
| 304 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 305 |
// with single element, huffSym must error on these two edge cases. In both |
| 306 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 307 |
// satisfy the n == 0 check below. |
| 308 |
n := uint(f.hl.maxRead) |
| 309 |
for { |
| 310 |
for fnb < n { |
| 311 |
c, err := fr.ReadByte() |
| 312 |
if err != nil { |
| 313 |
f.b, f.nb = fb, fnb |
| 314 |
f.err = noEOF(err) |
| 315 |
return |
| 316 |
} |
| 317 |
f.roffset++ |
| 318 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 319 |
fnb += 8 |
| 320 |
} |
| 321 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 322 |
n = uint(chunk & huffmanCountMask) |
| 323 |
if n > huffmanChunkBits { |
| 324 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 325 |
n = uint(chunk & huffmanCountMask) |
| 326 |
} |
| 327 |
if n <= fnb { |
| 328 |
if n == 0 { |
| 329 |
f.b, f.nb = fb, fnb |
| 330 |
if debugDecode { |
| 331 |
fmt.Println("huffsym: n==0") |
| 332 |
} |
| 333 |
f.err = CorruptInputError(f.roffset) |
| 334 |
return |
| 335 |
} |
| 336 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 337 |
fnb = fnb - n |
| 338 |
v = int(chunk >> huffmanValueShift) |
| 339 |
break |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
var length int |
| 345 |
switch { |
| 346 |
case v < 256: |
| 347 |
dict.writeByte(byte(v)) |
| 348 |
if dict.availWrite() == 0 { |
| 349 |
f.toRead = dict.readFlush() |
| 350 |
f.step = huffmanBytesReader |
| 351 |
f.stepState = stateInit |
| 352 |
f.b, f.nb = fb, fnb |
| 353 |
return |
| 354 |
} |
| 355 |
goto readLiteral |
| 356 |
case v == 256: |
| 357 |
f.b, f.nb = fb, fnb |
| 358 |
f.finishBlock() |
| 359 |
return |
| 360 |
// otherwise, reference to older data |
| 361 |
case v < 265: |
| 362 |
length = v - (257 - 3) |
| 363 |
case v < maxNumLit: |
| 364 |
val := decCodeToLen[(v - 257)] |
| 365 |
length = int(val.length) + 3 |
| 366 |
n := uint(val.extra) |
| 367 |
for fnb < n { |
| 368 |
c, err := fr.ReadByte() |
| 369 |
if err != nil { |
| 370 |
f.b, f.nb = fb, fnb |
| 371 |
if debugDecode { |
| 372 |
fmt.Println("morebits n>0:", err) |
| 373 |
} |
| 374 |
f.err = err |
| 375 |
return |
| 376 |
} |
| 377 |
f.roffset++ |
| 378 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 379 |
fnb += 8 |
| 380 |
} |
| 381 |
length += int(fb & bitMask32[n]) |
| 382 |
fb >>= n & regmask.Shift32ByUint |
| 383 |
fnb -= n |
| 384 |
default: |
| 385 |
if debugDecode { |
| 386 |
fmt.Println(v, ">= maxNumLit") |
| 387 |
} |
| 388 |
f.err = CorruptInputError(f.roffset) |
| 389 |
f.b, f.nb = fb, fnb |
| 390 |
return |
| 391 |
} |
| 392 |
|
| 393 |
var dist uint32 |
| 394 |
if f.hd == nil { |
| 395 |
for fnb < 5 { |
| 396 |
c, err := fr.ReadByte() |
| 397 |
if err != nil { |
| 398 |
f.b, f.nb = fb, fnb |
| 399 |
if debugDecode { |
| 400 |
fmt.Println("morebits f.nb<5:", err) |
| 401 |
} |
| 402 |
f.err = err |
| 403 |
return |
| 404 |
} |
| 405 |
f.roffset++ |
| 406 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 407 |
fnb += 8 |
| 408 |
} |
| 409 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 410 |
fb >>= 5 |
| 411 |
fnb -= 5 |
| 412 |
} else { |
| 413 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 414 |
// with single element, huffSym must error on these two edge cases. In both |
| 415 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 416 |
// satisfy the n == 0 check below. |
| 417 |
n := uint(f.hd.maxRead) |
| 418 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 419 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 420 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 421 |
for { |
| 422 |
for fnb < n { |
| 423 |
c, err := fr.ReadByte() |
| 424 |
if err != nil { |
| 425 |
f.b, f.nb = fb, fnb |
| 426 |
f.err = noEOF(err) |
| 427 |
return |
| 428 |
} |
| 429 |
f.roffset++ |
| 430 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 431 |
fnb += 8 |
| 432 |
} |
| 433 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 434 |
n = uint(chunk & huffmanCountMask) |
| 435 |
if n > huffmanChunkBits { |
| 436 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 437 |
n = uint(chunk & huffmanCountMask) |
| 438 |
} |
| 439 |
if n <= fnb { |
| 440 |
if n == 0 { |
| 441 |
f.b, f.nb = fb, fnb |
| 442 |
if debugDecode { |
| 443 |
fmt.Println("huffsym: n==0") |
| 444 |
} |
| 445 |
f.err = CorruptInputError(f.roffset) |
| 446 |
return |
| 447 |
} |
| 448 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 449 |
fnb = fnb - n |
| 450 |
dist = uint32(chunk >> huffmanValueShift) |
| 451 |
break |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
switch { |
| 457 |
case dist < 4: |
| 458 |
dist++ |
| 459 |
case dist < maxNumDist: |
| 460 |
nb := uint(dist-2) >> 1 |
| 461 |
// have 1 bit in bottom of dist, need nb more. |
| 462 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 463 |
for fnb < nb { |
| 464 |
c, err := fr.ReadByte() |
| 465 |
if err != nil { |
| 466 |
f.b, f.nb = fb, fnb |
| 467 |
if debugDecode { |
| 468 |
fmt.Println("morebits f.nb<nb:", err) |
| 469 |
} |
| 470 |
f.err = err |
| 471 |
return |
| 472 |
} |
| 473 |
f.roffset++ |
| 474 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 475 |
fnb += 8 |
| 476 |
} |
| 477 |
extra |= fb & bitMask32[nb] |
| 478 |
fb >>= nb & regmask.Shift32ByUint |
| 479 |
fnb -= nb |
| 480 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 481 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 482 |
default: |
| 483 |
f.b, f.nb = fb, fnb |
| 484 |
if debugDecode { |
| 485 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 486 |
} |
| 487 |
f.err = CorruptInputError(f.roffset) |
| 488 |
return |
| 489 |
} |
| 490 |
|
| 491 |
// No check on length; encoding can be prescient. |
| 492 |
if dist > uint32(dict.histSize()) { |
| 493 |
f.b, f.nb = fb, fnb |
| 494 |
if debugDecode { |
| 495 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 496 |
} |
| 497 |
f.err = CorruptInputError(f.roffset) |
| 498 |
return |
| 499 |
} |
| 500 |
|
| 501 |
f.copyLen, f.copyDist = length, int(dist) |
| 502 |
goto copyHistory |
| 503 |
} |
| 504 |
|
| 505 |
copyHistory: |
| 506 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 507 |
{ |
| 508 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 509 |
if cnt == 0 { |
| 510 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 511 |
} |
| 512 |
f.copyLen -= cnt |
| 513 |
|
| 514 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 515 |
f.toRead = dict.readFlush() |
| 516 |
f.step = huffmanBytesReader // We need to continue this work |
| 517 |
f.stepState = stateDict |
| 518 |
f.b, f.nb = fb, fnb |
| 519 |
return |
| 520 |
} |
| 521 |
goto readLiteral |
| 522 |
} |
| 523 |
// Not reached |
| 524 |
} |
| 525 |
|
| 526 |
// Decode a single Huffman block from f. |
| 527 |
// hl and hd are the Huffman states for the lit/length values |
| 528 |
// and the distance values, respectively. If hd == nil, using the |
| 529 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 530 |
func (f *decompressor) huffmanBufioReader() { |
| 531 |
const ( |
| 532 |
stateInit = iota // Zero value must be stateInit |
| 533 |
stateDict |
| 534 |
) |
| 535 |
fr := f.r.(*bufio.Reader) |
| 536 |
|
| 537 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 538 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 539 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 540 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 541 |
pbuf, _ := fr.Peek(fr.Buffered()) |
| 542 |
pos := 0 |
| 543 |
|
| 544 |
switch f.stepState { |
| 545 |
case stateInit: |
| 546 |
goto readLiteral |
| 547 |
case stateDict: |
| 548 |
goto copyHistory |
| 549 |
} |
| 550 |
|
| 551 |
readLiteral: |
| 552 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 553 |
{ |
| 554 |
var v int |
| 555 |
{ |
| 556 |
// Inlined v, err := f.huffSym(f.hl) |
| 557 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 558 |
// with single element, huffSym must error on these two edge cases. In both |
| 559 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 560 |
// satisfy the n == 0 check below. |
| 561 |
n := uint(f.hl.maxRead) |
| 562 |
for { |
| 563 |
for fnb < n { |
| 564 |
if pos >= len(pbuf) { |
| 565 |
fr.Discard(pos) |
| 566 |
var err error |
| 567 |
pbuf, err = peekBufio(fr) |
| 568 |
pos = 0 |
| 569 |
if len(pbuf) == 0 { |
| 570 |
f.b, f.nb = fb, fnb |
| 571 |
f.err = noEOF(err) |
| 572 |
return |
| 573 |
} |
| 574 |
} |
| 575 |
c := pbuf[pos] |
| 576 |
pos++ |
| 577 |
f.roffset++ |
| 578 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 579 |
fnb += 8 |
| 580 |
} |
| 581 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 582 |
n = uint(chunk & huffmanCountMask) |
| 583 |
if n > huffmanChunkBits { |
| 584 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 585 |
n = uint(chunk & huffmanCountMask) |
| 586 |
} |
| 587 |
if n <= fnb { |
| 588 |
if n == 0 { |
| 589 |
fr.Discard(pos) |
| 590 |
f.b, f.nb = fb, fnb |
| 591 |
if debugDecode { |
| 592 |
fmt.Println("huffsym: n==0") |
| 593 |
} |
| 594 |
f.err = CorruptInputError(f.roffset) |
| 595 |
return |
| 596 |
} |
| 597 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 598 |
fnb = fnb - n |
| 599 |
v = int(chunk >> huffmanValueShift) |
| 600 |
break |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
var length int |
| 606 |
switch { |
| 607 |
case v < 256: |
| 608 |
dict.writeByte(byte(v)) |
| 609 |
if dict.availWrite() == 0 { |
| 610 |
fr.Discard(pos) |
| 611 |
f.toRead = dict.readFlush() |
| 612 |
f.step = huffmanBufioReader |
| 613 |
f.stepState = stateInit |
| 614 |
f.b, f.nb = fb, fnb |
| 615 |
return |
| 616 |
} |
| 617 |
goto readLiteral |
| 618 |
case v == 256: |
| 619 |
fr.Discard(pos) |
| 620 |
f.b, f.nb = fb, fnb |
| 621 |
f.finishBlock() |
| 622 |
return |
| 623 |
// otherwise, reference to older data |
| 624 |
case v < 265: |
| 625 |
length = v - (257 - 3) |
| 626 |
case v < maxNumLit: |
| 627 |
val := decCodeToLen[(v - 257)] |
| 628 |
length = int(val.length) + 3 |
| 629 |
n := uint(val.extra) |
| 630 |
for fnb < n { |
| 631 |
if pos >= len(pbuf) { |
| 632 |
fr.Discard(pos) |
| 633 |
var err error |
| 634 |
pbuf, err = peekBufio(fr) |
| 635 |
pos = 0 |
| 636 |
if len(pbuf) == 0 { |
| 637 |
f.b, f.nb = fb, fnb |
| 638 |
if debugDecode { |
| 639 |
fmt.Println("morebits n>0:", err) |
| 640 |
} |
| 641 |
f.err = err |
| 642 |
return |
| 643 |
} |
| 644 |
} |
| 645 |
c := pbuf[pos] |
| 646 |
pos++ |
| 647 |
f.roffset++ |
| 648 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 649 |
fnb += 8 |
| 650 |
} |
| 651 |
length += int(fb & bitMask32[n]) |
| 652 |
fb >>= n & regmask.Shift32ByUint |
| 653 |
fnb -= n |
| 654 |
default: |
| 655 |
fr.Discard(pos) |
| 656 |
if debugDecode { |
| 657 |
fmt.Println(v, ">= maxNumLit") |
| 658 |
} |
| 659 |
f.err = CorruptInputError(f.roffset) |
| 660 |
f.b, f.nb = fb, fnb |
| 661 |
return |
| 662 |
} |
| 663 |
|
| 664 |
var dist uint32 |
| 665 |
if f.hd == nil { |
| 666 |
for fnb < 5 { |
| 667 |
if pos >= len(pbuf) { |
| 668 |
fr.Discard(pos) |
| 669 |
var err error |
| 670 |
pbuf, err = peekBufio(fr) |
| 671 |
pos = 0 |
| 672 |
if len(pbuf) == 0 { |
| 673 |
f.b, f.nb = fb, fnb |
| 674 |
if debugDecode { |
| 675 |
fmt.Println("morebits f.nb<5:", err) |
| 676 |
} |
| 677 |
f.err = err |
| 678 |
return |
| 679 |
} |
| 680 |
} |
| 681 |
c := pbuf[pos] |
| 682 |
pos++ |
| 683 |
f.roffset++ |
| 684 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 685 |
fnb += 8 |
| 686 |
} |
| 687 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 688 |
fb >>= 5 |
| 689 |
fnb -= 5 |
| 690 |
} else { |
| 691 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 692 |
// with single element, huffSym must error on these two edge cases. In both |
| 693 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 694 |
// satisfy the n == 0 check below. |
| 695 |
n := uint(f.hd.maxRead) |
| 696 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 697 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 698 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 699 |
for { |
| 700 |
for fnb < n { |
| 701 |
if pos >= len(pbuf) { |
| 702 |
fr.Discard(pos) |
| 703 |
var err error |
| 704 |
pbuf, err = peekBufio(fr) |
| 705 |
pos = 0 |
| 706 |
if len(pbuf) == 0 { |
| 707 |
f.b, f.nb = fb, fnb |
| 708 |
f.err = noEOF(err) |
| 709 |
return |
| 710 |
} |
| 711 |
} |
| 712 |
c := pbuf[pos] |
| 713 |
pos++ |
| 714 |
f.roffset++ |
| 715 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 716 |
fnb += 8 |
| 717 |
} |
| 718 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 719 |
n = uint(chunk & huffmanCountMask) |
| 720 |
if n > huffmanChunkBits { |
| 721 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 722 |
n = uint(chunk & huffmanCountMask) |
| 723 |
} |
| 724 |
if n <= fnb { |
| 725 |
if n == 0 { |
| 726 |
fr.Discard(pos) |
| 727 |
f.b, f.nb = fb, fnb |
| 728 |
if debugDecode { |
| 729 |
fmt.Println("huffsym: n==0") |
| 730 |
} |
| 731 |
f.err = CorruptInputError(f.roffset) |
| 732 |
return |
| 733 |
} |
| 734 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 735 |
fnb = fnb - n |
| 736 |
dist = uint32(chunk >> huffmanValueShift) |
| 737 |
break |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
switch { |
| 743 |
case dist < 4: |
| 744 |
dist++ |
| 745 |
case dist < maxNumDist: |
| 746 |
nb := uint(dist-2) >> 1 |
| 747 |
// have 1 bit in bottom of dist, need nb more. |
| 748 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 749 |
for fnb < nb { |
| 750 |
if pos >= len(pbuf) { |
| 751 |
fr.Discard(pos) |
| 752 |
var err error |
| 753 |
pbuf, err = peekBufio(fr) |
| 754 |
pos = 0 |
| 755 |
if len(pbuf) == 0 { |
| 756 |
f.b, f.nb = fb, fnb |
| 757 |
if debugDecode { |
| 758 |
fmt.Println("morebits f.nb<nb:", err) |
| 759 |
} |
| 760 |
f.err = err |
| 761 |
return |
| 762 |
} |
| 763 |
} |
| 764 |
c := pbuf[pos] |
| 765 |
pos++ |
| 766 |
f.roffset++ |
| 767 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 768 |
fnb += 8 |
| 769 |
} |
| 770 |
extra |= fb & bitMask32[nb] |
| 771 |
fb >>= nb & regmask.Shift32ByUint |
| 772 |
fnb -= nb |
| 773 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 774 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 775 |
default: |
| 776 |
fr.Discard(pos) |
| 777 |
f.b, f.nb = fb, fnb |
| 778 |
if debugDecode { |
| 779 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 780 |
} |
| 781 |
f.err = CorruptInputError(f.roffset) |
| 782 |
return |
| 783 |
} |
| 784 |
|
| 785 |
// No check on length; encoding can be prescient. |
| 786 |
if dist > uint32(dict.histSize()) { |
| 787 |
fr.Discard(pos) |
| 788 |
f.b, f.nb = fb, fnb |
| 789 |
if debugDecode { |
| 790 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 791 |
} |
| 792 |
f.err = CorruptInputError(f.roffset) |
| 793 |
return |
| 794 |
} |
| 795 |
|
| 796 |
f.copyLen, f.copyDist = length, int(dist) |
| 797 |
goto copyHistory |
| 798 |
} |
| 799 |
|
| 800 |
copyHistory: |
| 801 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 802 |
{ |
| 803 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 804 |
if cnt == 0 { |
| 805 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 806 |
} |
| 807 |
f.copyLen -= cnt |
| 808 |
|
| 809 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 810 |
fr.Discard(pos) |
| 811 |
f.toRead = dict.readFlush() |
| 812 |
f.step = huffmanBufioReader // We need to continue this work |
| 813 |
f.stepState = stateDict |
| 814 |
f.b, f.nb = fb, fnb |
| 815 |
return |
| 816 |
} |
| 817 |
goto readLiteral |
| 818 |
} |
| 819 |
// Not reached |
| 820 |
} |
| 821 |
|
| 822 |
// Decode a single Huffman block from f. |
| 823 |
// hl and hd are the Huffman states for the lit/length values |
| 824 |
// and the distance values, respectively. If hd == nil, using the |
| 825 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 826 |
func (f *decompressor) huffmanStringsReader() { |
| 827 |
const ( |
| 828 |
stateInit = iota // Zero value must be stateInit |
| 829 |
stateDict |
| 830 |
) |
| 831 |
fr := f.r.(*strings.Reader) |
| 832 |
|
| 833 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 834 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 835 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 836 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 837 |
|
| 838 |
switch f.stepState { |
| 839 |
case stateInit: |
| 840 |
goto readLiteral |
| 841 |
case stateDict: |
| 842 |
goto copyHistory |
| 843 |
} |
| 844 |
|
| 845 |
readLiteral: |
| 846 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 847 |
{ |
| 848 |
var v int |
| 849 |
{ |
| 850 |
// Inlined v, err := f.huffSym(f.hl) |
| 851 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 852 |
// with single element, huffSym must error on these two edge cases. In both |
| 853 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 854 |
// satisfy the n == 0 check below. |
| 855 |
n := uint(f.hl.maxRead) |
| 856 |
for { |
| 857 |
for fnb < n { |
| 858 |
c, err := fr.ReadByte() |
| 859 |
if err != nil { |
| 860 |
f.b, f.nb = fb, fnb |
| 861 |
f.err = noEOF(err) |
| 862 |
return |
| 863 |
} |
| 864 |
f.roffset++ |
| 865 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 866 |
fnb += 8 |
| 867 |
} |
| 868 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 869 |
n = uint(chunk & huffmanCountMask) |
| 870 |
if n > huffmanChunkBits { |
| 871 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 872 |
n = uint(chunk & huffmanCountMask) |
| 873 |
} |
| 874 |
if n <= fnb { |
| 875 |
if n == 0 { |
| 876 |
f.b, f.nb = fb, fnb |
| 877 |
if debugDecode { |
| 878 |
fmt.Println("huffsym: n==0") |
| 879 |
} |
| 880 |
f.err = CorruptInputError(f.roffset) |
| 881 |
return |
| 882 |
} |
| 883 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 884 |
fnb = fnb - n |
| 885 |
v = int(chunk >> huffmanValueShift) |
| 886 |
break |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
var length int |
| 892 |
switch { |
| 893 |
case v < 256: |
| 894 |
dict.writeByte(byte(v)) |
| 895 |
if dict.availWrite() == 0 { |
| 896 |
f.toRead = dict.readFlush() |
| 897 |
f.step = huffmanStringsReader |
| 898 |
f.stepState = stateInit |
| 899 |
f.b, f.nb = fb, fnb |
| 900 |
return |
| 901 |
} |
| 902 |
goto readLiteral |
| 903 |
case v == 256: |
| 904 |
f.b, f.nb = fb, fnb |
| 905 |
f.finishBlock() |
| 906 |
return |
| 907 |
// otherwise, reference to older data |
| 908 |
case v < 265: |
| 909 |
length = v - (257 - 3) |
| 910 |
case v < maxNumLit: |
| 911 |
val := decCodeToLen[(v - 257)] |
| 912 |
length = int(val.length) + 3 |
| 913 |
n := uint(val.extra) |
| 914 |
for fnb < n { |
| 915 |
c, err := fr.ReadByte() |
| 916 |
if err != nil { |
| 917 |
f.b, f.nb = fb, fnb |
| 918 |
if debugDecode { |
| 919 |
fmt.Println("morebits n>0:", err) |
| 920 |
} |
| 921 |
f.err = err |
| 922 |
return |
| 923 |
} |
| 924 |
f.roffset++ |
| 925 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 926 |
fnb += 8 |
| 927 |
} |
| 928 |
length += int(fb & bitMask32[n]) |
| 929 |
fb >>= n & regmask.Shift32ByUint |
| 930 |
fnb -= n |
| 931 |
default: |
| 932 |
if debugDecode { |
| 933 |
fmt.Println(v, ">= maxNumLit") |
| 934 |
} |
| 935 |
f.err = CorruptInputError(f.roffset) |
| 936 |
f.b, f.nb = fb, fnb |
| 937 |
return |
| 938 |
} |
| 939 |
|
| 940 |
var dist uint32 |
| 941 |
if f.hd == nil { |
| 942 |
for fnb < 5 { |
| 943 |
c, err := fr.ReadByte() |
| 944 |
if err != nil { |
| 945 |
f.b, f.nb = fb, fnb |
| 946 |
if debugDecode { |
| 947 |
fmt.Println("morebits f.nb<5:", err) |
| 948 |
} |
| 949 |
f.err = err |
| 950 |
return |
| 951 |
} |
| 952 |
f.roffset++ |
| 953 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 954 |
fnb += 8 |
| 955 |
} |
| 956 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 957 |
fb >>= 5 |
| 958 |
fnb -= 5 |
| 959 |
} else { |
| 960 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 961 |
// with single element, huffSym must error on these two edge cases. In both |
| 962 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 963 |
// satisfy the n == 0 check below. |
| 964 |
n := uint(f.hd.maxRead) |
| 965 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 966 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 967 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 968 |
for { |
| 969 |
for fnb < n { |
| 970 |
c, err := fr.ReadByte() |
| 971 |
if err != nil { |
| 972 |
f.b, f.nb = fb, fnb |
| 973 |
f.err = noEOF(err) |
| 974 |
return |
| 975 |
} |
| 976 |
f.roffset++ |
| 977 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 978 |
fnb += 8 |
| 979 |
} |
| 980 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 981 |
n = uint(chunk & huffmanCountMask) |
| 982 |
if n > huffmanChunkBits { |
| 983 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 984 |
n = uint(chunk & huffmanCountMask) |
| 985 |
} |
| 986 |
if n <= fnb { |
| 987 |
if n == 0 { |
| 988 |
f.b, f.nb = fb, fnb |
| 989 |
if debugDecode { |
| 990 |
fmt.Println("huffsym: n==0") |
| 991 |
} |
| 992 |
f.err = CorruptInputError(f.roffset) |
| 993 |
return |
| 994 |
} |
| 995 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 996 |
fnb = fnb - n |
| 997 |
dist = uint32(chunk >> huffmanValueShift) |
| 998 |
break |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
switch { |
| 1004 |
case dist < 4: |
| 1005 |
dist++ |
| 1006 |
case dist < maxNumDist: |
| 1007 |
nb := uint(dist-2) >> 1 |
| 1008 |
// have 1 bit in bottom of dist, need nb more. |
| 1009 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 1010 |
for fnb < nb { |
| 1011 |
c, err := fr.ReadByte() |
| 1012 |
if err != nil { |
| 1013 |
f.b, f.nb = fb, fnb |
| 1014 |
if debugDecode { |
| 1015 |
fmt.Println("morebits f.nb<nb:", err) |
| 1016 |
} |
| 1017 |
f.err = err |
| 1018 |
return |
| 1019 |
} |
| 1020 |
f.roffset++ |
| 1021 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1022 |
fnb += 8 |
| 1023 |
} |
| 1024 |
extra |= fb & bitMask32[nb] |
| 1025 |
fb >>= nb & regmask.Shift32ByUint |
| 1026 |
fnb -= nb |
| 1027 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 1028 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 1029 |
default: |
| 1030 |
f.b, f.nb = fb, fnb |
| 1031 |
if debugDecode { |
| 1032 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 1033 |
} |
| 1034 |
f.err = CorruptInputError(f.roffset) |
| 1035 |
return |
| 1036 |
} |
| 1037 |
|
| 1038 |
// No check on length; encoding can be prescient. |
| 1039 |
if dist > uint32(dict.histSize()) { |
| 1040 |
f.b, f.nb = fb, fnb |
| 1041 |
if debugDecode { |
| 1042 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 1043 |
} |
| 1044 |
f.err = CorruptInputError(f.roffset) |
| 1045 |
return |
| 1046 |
} |
| 1047 |
|
| 1048 |
f.copyLen, f.copyDist = length, int(dist) |
| 1049 |
goto copyHistory |
| 1050 |
} |
| 1051 |
|
| 1052 |
copyHistory: |
| 1053 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 1054 |
{ |
| 1055 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 1056 |
if cnt == 0 { |
| 1057 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 1058 |
} |
| 1059 |
f.copyLen -= cnt |
| 1060 |
|
| 1061 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 1062 |
f.toRead = dict.readFlush() |
| 1063 |
f.step = huffmanStringsReader // We need to continue this work |
| 1064 |
f.stepState = stateDict |
| 1065 |
f.b, f.nb = fb, fnb |
| 1066 |
return |
| 1067 |
} |
| 1068 |
goto readLiteral |
| 1069 |
} |
| 1070 |
// Not reached |
| 1071 |
} |
| 1072 |
|
| 1073 |
// Decode a single Huffman block from f. |
| 1074 |
// hl and hd are the Huffman states for the lit/length values |
| 1075 |
// and the distance values, respectively. If hd == nil, using the |
| 1076 |
// fixed distance encoding associated with fixed Huffman blocks. |
| 1077 |
func (f *decompressor) huffmanGenericReader() { |
| 1078 |
const ( |
| 1079 |
stateInit = iota // Zero value must be stateInit |
| 1080 |
stateDict |
| 1081 |
) |
| 1082 |
fr := f.r.(Reader) |
| 1083 |
|
| 1084 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 1085 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 1086 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 1087 |
fnb, fb, dict := f.nb, f.b, &f.dict |
| 1088 |
|
| 1089 |
switch f.stepState { |
| 1090 |
case stateInit: |
| 1091 |
goto readLiteral |
| 1092 |
case stateDict: |
| 1093 |
goto copyHistory |
| 1094 |
} |
| 1095 |
|
| 1096 |
readLiteral: |
| 1097 |
// Read literal and/or (length, distance) according to RFC section 3.2.3. |
| 1098 |
{ |
| 1099 |
var v int |
| 1100 |
{ |
| 1101 |
// Inlined v, err := f.huffSym(f.hl) |
| 1102 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 1103 |
// with single element, huffSym must error on these two edge cases. In both |
| 1104 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 1105 |
// satisfy the n == 0 check below. |
| 1106 |
n := uint(f.hl.maxRead) |
| 1107 |
for { |
| 1108 |
for fnb < n { |
| 1109 |
c, err := fr.ReadByte() |
| 1110 |
if err != nil { |
| 1111 |
f.b, f.nb = fb, fnb |
| 1112 |
f.err = noEOF(err) |
| 1113 |
return |
| 1114 |
} |
| 1115 |
f.roffset++ |
| 1116 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1117 |
fnb += 8 |
| 1118 |
} |
| 1119 |
chunk := f.hl.chunks[fb&(huffmanNumChunks-1)] |
| 1120 |
n = uint(chunk & huffmanCountMask) |
| 1121 |
if n > huffmanChunkBits { |
| 1122 |
chunk = f.hl.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hl.linkMask] |
| 1123 |
n = uint(chunk & huffmanCountMask) |
| 1124 |
} |
| 1125 |
if n <= fnb { |
| 1126 |
if n == 0 { |
| 1127 |
f.b, f.nb = fb, fnb |
| 1128 |
if debugDecode { |
| 1129 |
fmt.Println("huffsym: n==0") |
| 1130 |
} |
| 1131 |
f.err = CorruptInputError(f.roffset) |
| 1132 |
return |
| 1133 |
} |
| 1134 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 1135 |
fnb = fnb - n |
| 1136 |
v = int(chunk >> huffmanValueShift) |
| 1137 |
break |
| 1138 |
} |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
var length int |
| 1143 |
switch { |
| 1144 |
case v < 256: |
| 1145 |
dict.writeByte(byte(v)) |
| 1146 |
if dict.availWrite() == 0 { |
| 1147 |
f.toRead = dict.readFlush() |
| 1148 |
f.step = huffmanGenericReader |
| 1149 |
f.stepState = stateInit |
| 1150 |
f.b, f.nb = fb, fnb |
| 1151 |
return |
| 1152 |
} |
| 1153 |
goto readLiteral |
| 1154 |
case v == 256: |
| 1155 |
f.b, f.nb = fb, fnb |
| 1156 |
f.finishBlock() |
| 1157 |
return |
| 1158 |
// otherwise, reference to older data |
| 1159 |
case v < 265: |
| 1160 |
length = v - (257 - 3) |
| 1161 |
case v < maxNumLit: |
| 1162 |
val := decCodeToLen[(v - 257)] |
| 1163 |
length = int(val.length) + 3 |
| 1164 |
n := uint(val.extra) |
| 1165 |
for fnb < n { |
| 1166 |
c, err := fr.ReadByte() |
| 1167 |
if err != nil { |
| 1168 |
f.b, f.nb = fb, fnb |
| 1169 |
if debugDecode { |
| 1170 |
fmt.Println("morebits n>0:", err) |
| 1171 |
} |
| 1172 |
f.err = err |
| 1173 |
return |
| 1174 |
} |
| 1175 |
f.roffset++ |
| 1176 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1177 |
fnb += 8 |
| 1178 |
} |
| 1179 |
length += int(fb & bitMask32[n]) |
| 1180 |
fb >>= n & regmask.Shift32ByUint |
| 1181 |
fnb -= n |
| 1182 |
default: |
| 1183 |
if debugDecode { |
| 1184 |
fmt.Println(v, ">= maxNumLit") |
| 1185 |
} |
| 1186 |
f.err = CorruptInputError(f.roffset) |
| 1187 |
f.b, f.nb = fb, fnb |
| 1188 |
return |
| 1189 |
} |
| 1190 |
|
| 1191 |
var dist uint32 |
| 1192 |
if f.hd == nil { |
| 1193 |
for fnb < 5 { |
| 1194 |
c, err := fr.ReadByte() |
| 1195 |
if err != nil { |
| 1196 |
f.b, f.nb = fb, fnb |
| 1197 |
if debugDecode { |
| 1198 |
fmt.Println("morebits f.nb<5:", err) |
| 1199 |
} |
| 1200 |
f.err = err |
| 1201 |
return |
| 1202 |
} |
| 1203 |
f.roffset++ |
| 1204 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1205 |
fnb += 8 |
| 1206 |
} |
| 1207 |
dist = uint32(bits.Reverse8(uint8(fb & 0x1F << 3))) |
| 1208 |
fb >>= 5 |
| 1209 |
fnb -= 5 |
| 1210 |
} else { |
| 1211 |
// Since a huffmanDecoder can be empty or be composed of a degenerate tree |
| 1212 |
// with single element, huffSym must error on these two edge cases. In both |
| 1213 |
// cases, the chunks slice will be 0 for the invalid sequence, leading it |
| 1214 |
// satisfy the n == 0 check below. |
| 1215 |
n := uint(f.hd.maxRead) |
| 1216 |
// Optimization. Compiler isn't smart enough to keep f.b,f.nb in registers, |
| 1217 |
// but is smart enough to keep local variables in registers, so use nb and b, |
| 1218 |
// inline call to moreBits and reassign b,nb back to f on return. |
| 1219 |
for { |
| 1220 |
for fnb < n { |
| 1221 |
c, err := fr.ReadByte() |
| 1222 |
if err != nil { |
| 1223 |
f.b, f.nb = fb, fnb |
| 1224 |
f.err = noEOF(err) |
| 1225 |
return |
| 1226 |
} |
| 1227 |
f.roffset++ |
| 1228 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1229 |
fnb += 8 |
| 1230 |
} |
| 1231 |
chunk := f.hd.chunks[fb&(huffmanNumChunks-1)] |
| 1232 |
n = uint(chunk & huffmanCountMask) |
| 1233 |
if n > huffmanChunkBits { |
| 1234 |
chunk = f.hd.links[chunk>>huffmanValueShift][(fb>>huffmanChunkBits)&f.hd.linkMask] |
| 1235 |
n = uint(chunk & huffmanCountMask) |
| 1236 |
} |
| 1237 |
if n <= fnb { |
| 1238 |
if n == 0 { |
| 1239 |
f.b, f.nb = fb, fnb |
| 1240 |
if debugDecode { |
| 1241 |
fmt.Println("huffsym: n==0") |
| 1242 |
} |
| 1243 |
f.err = CorruptInputError(f.roffset) |
| 1244 |
return |
| 1245 |
} |
| 1246 |
fb = fb >> (n & regmask.Shift32ByUint) |
| 1247 |
fnb = fnb - n |
| 1248 |
dist = uint32(chunk >> huffmanValueShift) |
| 1249 |
break |
| 1250 |
} |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
switch { |
| 1255 |
case dist < 4: |
| 1256 |
dist++ |
| 1257 |
case dist < maxNumDist: |
| 1258 |
nb := uint(dist-2) >> 1 |
| 1259 |
// have 1 bit in bottom of dist, need nb more. |
| 1260 |
extra := (dist & 1) << (nb & regmask.Shift32ByUint) |
| 1261 |
for fnb < nb { |
| 1262 |
c, err := fr.ReadByte() |
| 1263 |
if err != nil { |
| 1264 |
f.b, f.nb = fb, fnb |
| 1265 |
if debugDecode { |
| 1266 |
fmt.Println("morebits f.nb<nb:", err) |
| 1267 |
} |
| 1268 |
f.err = err |
| 1269 |
return |
| 1270 |
} |
| 1271 |
f.roffset++ |
| 1272 |
fb |= uint32(c) << (fnb & regmask.Shift32ByUint) |
| 1273 |
fnb += 8 |
| 1274 |
} |
| 1275 |
extra |= fb & bitMask32[nb] |
| 1276 |
fb >>= nb & regmask.Shift32ByUint |
| 1277 |
fnb -= nb |
| 1278 |
dist = 1<<((nb+1)®mask.Shift32ByUint) + 1 + extra |
| 1279 |
// slower: dist = bitMask32[nb+1] + 2 + extra |
| 1280 |
default: |
| 1281 |
f.b, f.nb = fb, fnb |
| 1282 |
if debugDecode { |
| 1283 |
fmt.Println("dist too big:", dist, maxNumDist) |
| 1284 |
} |
| 1285 |
f.err = CorruptInputError(f.roffset) |
| 1286 |
return |
| 1287 |
} |
| 1288 |
|
| 1289 |
// No check on length; encoding can be prescient. |
| 1290 |
if dist > uint32(dict.histSize()) { |
| 1291 |
f.b, f.nb = fb, fnb |
| 1292 |
if debugDecode { |
| 1293 |
fmt.Println("dist > dict.histSize():", dist, dict.histSize()) |
| 1294 |
} |
| 1295 |
f.err = CorruptInputError(f.roffset) |
| 1296 |
return |
| 1297 |
} |
| 1298 |
|
| 1299 |
f.copyLen, f.copyDist = length, int(dist) |
| 1300 |
goto copyHistory |
| 1301 |
} |
| 1302 |
|
| 1303 |
copyHistory: |
| 1304 |
// Perform a backwards copy according to RFC section 3.2.3. |
| 1305 |
{ |
| 1306 |
cnt := dict.tryWriteCopy(f.copyDist, f.copyLen) |
| 1307 |
if cnt == 0 { |
| 1308 |
cnt = dict.writeCopy(f.copyDist, f.copyLen) |
| 1309 |
} |
| 1310 |
f.copyLen -= cnt |
| 1311 |
|
| 1312 |
if dict.availWrite() == 0 || f.copyLen > 0 { |
| 1313 |
f.toRead = dict.readFlush() |
| 1314 |
f.step = huffmanGenericReader // We need to continue this work |
| 1315 |
f.stepState = stateDict |
| 1316 |
f.b, f.nb = fb, fnb |
| 1317 |
return |
| 1318 |
} |
| 1319 |
goto readLiteral |
| 1320 |
} |
| 1321 |
// Not reached |
| 1322 |
} |
| 1323 |
|
| 1324 |
func (f *decompressor) huffmanBlockDecoder() { |
| 1325 |
switch f.r.(type) { |
| 1326 |
case *bytes.Buffer: |
| 1327 |
f.huffmanBytesBuffer() |
| 1328 |
case *bytes.Reader: |
| 1329 |
f.huffmanBytesReader() |
| 1330 |
case *bufio.Reader: |
| 1331 |
f.huffmanBufioReader() |
| 1332 |
case *strings.Reader: |
| 1333 |
f.huffmanStringsReader() |
| 1334 |
case Reader: |
| 1335 |
f.huffmanGenericReader() |
| 1336 |
default: |
| 1337 |
f.huffmanGenericReader() |
| 1338 |
} |
| 1339 |
} |