github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/compress/flate/inflate.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package flate implements the DEFLATE compressed data format, described in 6 // RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file 7 // formats. 8 package flate 9 10 import ( 11 "bufio" 12 "io" 13 "strconv" 14 "sync" 15 ) 16 17 const ( 18 maxCodeLen = 16 // max length of Huffman code 19 maxHist = 32768 // max history required 20 // The next three numbers come from the RFC section 3.2.7, with the 21 // additional proviso in section 3.2.5 which implies that distance codes 22 // 30 and 31 should never occur in compressed data. 23 maxNumLit = 286 24 maxNumDist = 30 25 numCodes = 19 // number of codes in Huffman meta-code 26 ) 27 28 // Initialize the fixedHuffmanDecoder only once upon first use. 29 var fixedOnce sync.Once 30 var fixedHuffmanDecoder huffmanDecoder 31 32 // A CorruptInputError reports the presence of corrupt input at a given offset. 33 type CorruptInputError int64 34 35 func (e CorruptInputError) Error() string { 36 return "flate: corrupt input before offset " + strconv.FormatInt(int64(e), 10) 37 } 38 39 // An InternalError reports an error in the flate code itself. 40 type InternalError string 41 42 func (e InternalError) Error() string { return "flate: internal error: " + string(e) } 43 44 // A ReadError reports an error encountered while reading input. 45 type ReadError struct { 46 Offset int64 // byte offset where error occurred 47 Err error // error returned by underlying Read 48 } 49 50 func (e *ReadError) Error() string { 51 return "flate: read error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error() 52 } 53 54 // A WriteError reports an error encountered while writing output. 55 type WriteError struct { 56 Offset int64 // byte offset where error occurred 57 Err error // error returned by underlying Write 58 } 59 60 func (e *WriteError) Error() string { 61 return "flate: write error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error() 62 } 63 64 // Resetter resets a ReadCloser returned by NewReader or NewReaderDict to 65 // to switch to a new underlying Reader. This permits reusing a ReadCloser 66 // instead of allocating a new one. 67 type Resetter interface { 68 // Reset discards any buffered data and resets the Resetter as if it was 69 // newly initialized with the given reader. 70 Reset(r io.Reader, dict []byte) error 71 } 72 73 // The data structure for decoding Huffman tables is based on that of 74 // zlib. There is a lookup table of a fixed bit width (huffmanChunkBits), 75 // For codes smaller than the table width, there are multiple entries 76 // (each combination of trailing bits has the same value). For codes 77 // larger than the table width, the table contains a link to an overflow 78 // table. The width of each entry in the link table is the maximum code 79 // size minus the chunk width. 80 // 81 // Note that you can do a lookup in the table even without all bits 82 // filled. Since the extra bits are zero, and the DEFLATE Huffman codes 83 // have the property that shorter codes come before longer ones, the 84 // bit length estimate in the result is a lower bound on the actual 85 // number of bits. 86 // 87 // See the following: 88 // http://www.gzip.org/algorithm.txt 89 90 // chunk & 15 is number of bits 91 // chunk >> 4 is value, including table link 92 93 const ( 94 huffmanChunkBits = 9 95 huffmanNumChunks = 1 << huffmanChunkBits 96 huffmanCountMask = 15 97 huffmanValueShift = 4 98 ) 99 100 type huffmanDecoder struct { 101 min int // the minimum code length 102 chunks [huffmanNumChunks]uint32 // chunks as described above 103 links [][]uint32 // overflow links 104 linkMask uint32 // mask the width of the link table 105 } 106 107 // Initialize Huffman decoding tables from array of code lengths. 108 // Following this function, h is guaranteed to be initialized into a complete 109 // tree (i.e., neither over-subscribed nor under-subscribed). The exception is a 110 // degenerate case where the tree has only a single symbol with length 1. Empty 111 // trees are permitted. 112 func (h *huffmanDecoder) init(bits []int) bool { 113 // Sanity enables additional runtime tests during Huffman 114 // table construction. It's intended to be used during 115 // development to supplement the currently ad-hoc unit tests. 116 const sanity = false 117 118 if h.min != 0 { 119 *h = huffmanDecoder{} 120 } 121 122 // Count number of codes of each length, 123 // compute min and max length. 124 var count [maxCodeLen]int 125 var min, max int 126 for _, n := range bits { 127 if n == 0 { 128 continue 129 } 130 if min == 0 || n < min { 131 min = n 132 } 133 if n > max { 134 max = n 135 } 136 count[n]++ 137 } 138 139 // Empty tree. The decompressor.huffSym function will fail later if the tree 140 // is used. Technically, an empty tree is only valid for the HDIST tree and 141 // not the HCLEN and HLIT tree. However, a stream with an empty HCLEN tree 142 // is guaranteed to fail since it will attempt to use the tree to decode the 143 // codes for the HLIT and HDIST trees. Similarly, an empty HLIT tree is 144 // guaranteed to fail later since the compressed data section must be 145 // composed of at least one symbol (the end-of-block marker). 146 if max == 0 { 147 return true 148 } 149 150 code := 0 151 var nextcode [maxCodeLen]int 152 for i := min; i <= max; i++ { 153 code <<= 1 154 nextcode[i] = code 155 code += count[i] 156 } 157 158 // Check that the coding is complete (i.e., that we've 159 // assigned all 2-to-the-max possible bit sequences). 160 // Exception: To be compatible with zlib, we also need to 161 // accept degenerate single-code codings. See also 162 // TestDegenerateHuffmanCoding. 163 if code != 1<<uint(max) && !(code == 1 && max == 1) { 164 return false 165 } 166 167 h.min = min 168 if max > huffmanChunkBits { 169 numLinks := 1 << (uint(max) - huffmanChunkBits) 170 h.linkMask = uint32(numLinks - 1) 171 172 // create link tables 173 link := nextcode[huffmanChunkBits+1] >> 1 174 h.links = make([][]uint32, huffmanNumChunks-link) 175 for j := uint(link); j < huffmanNumChunks; j++ { 176 reverse := int(reverseByte[j>>8]) | int(reverseByte[j&0xff])<<8 177 reverse >>= uint(16 - huffmanChunkBits) 178 off := j - uint(link) 179 if sanity && h.chunks[reverse] != 0 { 180 panic("impossible: overwriting existing chunk") 181 } 182 h.chunks[reverse] = uint32(off<<huffmanValueShift | (huffmanChunkBits + 1)) 183 h.links[off] = make([]uint32, numLinks) 184 } 185 } 186 187 for i, n := range bits { 188 if n == 0 { 189 continue 190 } 191 code := nextcode[n] 192 nextcode[n]++ 193 chunk := uint32(i<<huffmanValueShift | n) 194 reverse := int(reverseByte[code>>8]) | int(reverseByte[code&0xff])<<8 195 reverse >>= uint(16 - n) 196 if n <= huffmanChunkBits { 197 for off := reverse; off < len(h.chunks); off += 1 << uint(n) { 198 // We should never need to overwrite 199 // an existing chunk. Also, 0 is 200 // never a valid chunk, because the 201 // lower 4 "count" bits should be 202 // between 1 and 15. 203 if sanity && h.chunks[off] != 0 { 204 panic("impossible: overwriting existing chunk") 205 } 206 h.chunks[off] = chunk 207 } 208 } else { 209 j := reverse & (huffmanNumChunks - 1) 210 if sanity && h.chunks[j]&huffmanCountMask != huffmanChunkBits+1 { 211 // Longer codes should have been 212 // associated with a link table above. 213 panic("impossible: not an indirect chunk") 214 } 215 value := h.chunks[j] >> huffmanValueShift 216 linktab := h.links[value] 217 reverse >>= huffmanChunkBits 218 for off := reverse; off < len(linktab); off += 1 << uint(n-huffmanChunkBits) { 219 if sanity && linktab[off] != 0 { 220 panic("impossible: overwriting existing chunk") 221 } 222 linktab[off] = chunk 223 } 224 } 225 } 226 227 if sanity { 228 // Above we've sanity checked that we never overwrote 229 // an existing entry. Here we additionally check that 230 // we filled the tables completely. 231 for i, chunk := range h.chunks { 232 if chunk == 0 { 233 // As an exception, in the degenerate 234 // single-code case, we allow odd 235 // chunks to be missing. 236 if code == 1 && i%2 == 1 { 237 continue 238 } 239 panic("impossible: missing chunk") 240 } 241 } 242 for _, linktab := range h.links { 243 for _, chunk := range linktab { 244 if chunk == 0 { 245 panic("impossible: missing chunk") 246 } 247 } 248 } 249 } 250 251 return true 252 } 253 254 // The actual read interface needed by NewReader. 255 // If the passed in io.Reader does not also have ReadByte, 256 // the NewReader will introduce its own buffering. 257 type Reader interface { 258 io.Reader 259 io.ByteReader 260 } 261 262 // Decompress state. 263 type decompressor struct { 264 // Input source. 265 r Reader 266 roffset int64 267 woffset int64 268 269 // Input bits, in top of b. 270 b uint32 271 nb uint 272 273 // Huffman decoders for literal/length, distance. 274 h1, h2 huffmanDecoder 275 276 // Length arrays used to define Huffman codes. 277 bits *[maxNumLit + maxNumDist]int 278 codebits *[numCodes]int 279 280 // Output history, buffer. 281 hist *[maxHist]byte 282 hp int // current output position in buffer 283 hw int // have written hist[0:hw] already 284 hfull bool // buffer has filled at least once 285 286 // Temporary buffer (avoids repeated allocation). 287 buf [4]byte 288 289 // Next step in the decompression, 290 // and decompression state. 291 step func(*decompressor) 292 final bool 293 err error 294 toRead []byte 295 hl, hd *huffmanDecoder 296 copyLen int 297 copyDist int 298 } 299 300 func (f *decompressor) nextBlock() { 301 if f.final { 302 if f.hw != f.hp { 303 f.flush((*decompressor).nextBlock) 304 return 305 } 306 f.err = io.EOF 307 return 308 } 309 for f.nb < 1+2 { 310 if f.err = f.moreBits(); f.err != nil { 311 return 312 } 313 } 314 f.final = f.b&1 == 1 315 f.b >>= 1 316 typ := f.b & 3 317 f.b >>= 2 318 f.nb -= 1 + 2 319 switch typ { 320 case 0: 321 f.dataBlock() 322 case 1: 323 // compressed, fixed Huffman tables 324 f.hl = &fixedHuffmanDecoder 325 f.hd = nil 326 f.huffmanBlock() 327 case 2: 328 // compressed, dynamic Huffman tables 329 if f.err = f.readHuffman(); f.err != nil { 330 break 331 } 332 f.hl = &f.h1 333 f.hd = &f.h2 334 f.huffmanBlock() 335 default: 336 // 3 is reserved. 337 f.err = CorruptInputError(f.roffset) 338 } 339 } 340 341 func (f *decompressor) Read(b []byte) (int, error) { 342 for { 343 if len(f.toRead) > 0 { 344 n := copy(b, f.toRead) 345 f.toRead = f.toRead[n:] 346 return n, nil 347 } 348 if f.err != nil { 349 return 0, f.err 350 } 351 f.step(f) 352 } 353 } 354 355 func (f *decompressor) Close() error { 356 if f.err == io.EOF { 357 return nil 358 } 359 return f.err 360 } 361 362 // RFC 1951 section 3.2.7. 363 // Compression with dynamic Huffman codes 364 365 var codeOrder = [...]int{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15} 366 367 func (f *decompressor) readHuffman() error { 368 // HLIT[5], HDIST[5], HCLEN[4]. 369 for f.nb < 5+5+4 { 370 if err := f.moreBits(); err != nil { 371 return err 372 } 373 } 374 nlit := int(f.b&0x1F) + 257 375 if nlit > maxNumLit { 376 return CorruptInputError(f.roffset) 377 } 378 f.b >>= 5 379 ndist := int(f.b&0x1F) + 1 380 if ndist > maxNumDist { 381 return CorruptInputError(f.roffset) 382 } 383 f.b >>= 5 384 nclen := int(f.b&0xF) + 4 385 // numCodes is 19, so nclen is always valid. 386 f.b >>= 4 387 f.nb -= 5 + 5 + 4 388 389 // (HCLEN+4)*3 bits: code lengths in the magic codeOrder order. 390 for i := 0; i < nclen; i++ { 391 for f.nb < 3 { 392 if err := f.moreBits(); err != nil { 393 return err 394 } 395 } 396 f.codebits[codeOrder[i]] = int(f.b & 0x7) 397 f.b >>= 3 398 f.nb -= 3 399 } 400 for i := nclen; i < len(codeOrder); i++ { 401 f.codebits[codeOrder[i]] = 0 402 } 403 if !f.h1.init(f.codebits[0:]) { 404 return CorruptInputError(f.roffset) 405 } 406 407 // HLIT + 257 code lengths, HDIST + 1 code lengths, 408 // using the code length Huffman code. 409 for i, n := 0, nlit+ndist; i < n; { 410 x, err := f.huffSym(&f.h1) 411 if err != nil { 412 return err 413 } 414 if x < 16 { 415 // Actual length. 416 f.bits[i] = x 417 i++ 418 continue 419 } 420 // Repeat previous length or zero. 421 var rep int 422 var nb uint 423 var b int 424 switch x { 425 default: 426 return InternalError("unexpected length code") 427 case 16: 428 rep = 3 429 nb = 2 430 if i == 0 { 431 return CorruptInputError(f.roffset) 432 } 433 b = f.bits[i-1] 434 case 17: 435 rep = 3 436 nb = 3 437 b = 0 438 case 18: 439 rep = 11 440 nb = 7 441 b = 0 442 } 443 for f.nb < nb { 444 if err := f.moreBits(); err != nil { 445 return err 446 } 447 } 448 rep += int(f.b & uint32(1<<nb-1)) 449 f.b >>= nb 450 f.nb -= nb 451 if i+rep > n { 452 return CorruptInputError(f.roffset) 453 } 454 for j := 0; j < rep; j++ { 455 f.bits[i] = b 456 i++ 457 } 458 } 459 460 if !f.h1.init(f.bits[0:nlit]) || !f.h2.init(f.bits[nlit:nlit+ndist]) { 461 return CorruptInputError(f.roffset) 462 } 463 464 return nil 465 } 466 467 // Decode a single Huffman block from f. 468 // hl and hd are the Huffman states for the lit/length values 469 // and the distance values, respectively. If hd == nil, using the 470 // fixed distance encoding associated with fixed Huffman blocks. 471 func (f *decompressor) huffmanBlock() { 472 for { 473 v, err := f.huffSym(f.hl) 474 if err != nil { 475 f.err = err 476 return 477 } 478 var n uint // number of bits extra 479 var length int 480 switch { 481 case v < 256: 482 f.hist[f.hp] = byte(v) 483 f.hp++ 484 if f.hp == len(f.hist) { 485 // After the flush, continue this loop. 486 f.flush((*decompressor).huffmanBlock) 487 return 488 } 489 continue 490 case v == 256: 491 // Done with huffman block; read next block. 492 f.step = (*decompressor).nextBlock 493 return 494 // otherwise, reference to older data 495 case v < 265: 496 length = v - (257 - 3) 497 n = 0 498 case v < 269: 499 length = v*2 - (265*2 - 11) 500 n = 1 501 case v < 273: 502 length = v*4 - (269*4 - 19) 503 n = 2 504 case v < 277: 505 length = v*8 - (273*8 - 35) 506 n = 3 507 case v < 281: 508 length = v*16 - (277*16 - 67) 509 n = 4 510 case v < 285: 511 length = v*32 - (281*32 - 131) 512 n = 5 513 case v < maxNumLit: 514 length = 258 515 n = 0 516 default: 517 f.err = CorruptInputError(f.roffset) 518 return 519 } 520 if n > 0 { 521 for f.nb < n { 522 if err = f.moreBits(); err != nil { 523 f.err = err 524 return 525 } 526 } 527 length += int(f.b & uint32(1<<n-1)) 528 f.b >>= n 529 f.nb -= n 530 } 531 532 var dist int 533 if f.hd == nil { 534 for f.nb < 5 { 535 if err = f.moreBits(); err != nil { 536 f.err = err 537 return 538 } 539 } 540 dist = int(reverseByte[(f.b&0x1F)<<3]) 541 f.b >>= 5 542 f.nb -= 5 543 } else { 544 if dist, err = f.huffSym(f.hd); err != nil { 545 f.err = err 546 return 547 } 548 } 549 550 switch { 551 case dist < 4: 552 dist++ 553 case dist < maxNumDist: 554 nb := uint(dist-2) >> 1 555 // have 1 bit in bottom of dist, need nb more. 556 extra := (dist & 1) << nb 557 for f.nb < nb { 558 if err = f.moreBits(); err != nil { 559 f.err = err 560 return 561 } 562 } 563 extra |= int(f.b & uint32(1<<nb-1)) 564 f.b >>= nb 565 f.nb -= nb 566 dist = 1<<(nb+1) + 1 + extra 567 default: 568 f.err = CorruptInputError(f.roffset) 569 return 570 } 571 572 // Copy history[-dist:-dist+length] into output. 573 if dist > len(f.hist) { 574 f.err = InternalError("bad history distance") 575 return 576 } 577 578 // No check on length; encoding can be prescient. 579 if !f.hfull && dist > f.hp { 580 f.err = CorruptInputError(f.roffset) 581 return 582 } 583 584 f.copyLen, f.copyDist = length, dist 585 if f.copyHist() { 586 return 587 } 588 } 589 } 590 591 // copyHist copies f.copyLen bytes from f.hist (f.copyDist bytes ago) to itself. 592 // It reports whether the f.hist buffer is full. 593 func (f *decompressor) copyHist() bool { 594 p := f.hp - f.copyDist 595 if p < 0 { 596 p += len(f.hist) 597 } 598 for f.copyLen > 0 { 599 n := f.copyLen 600 if x := len(f.hist) - f.hp; n > x { 601 n = x 602 } 603 if x := len(f.hist) - p; n > x { 604 n = x 605 } 606 forwardCopy(f.hist[:], f.hp, p, n) 607 p += n 608 f.hp += n 609 f.copyLen -= n 610 if f.hp == len(f.hist) { 611 // After flush continue copying out of history. 612 f.flush((*decompressor).copyHuff) 613 return true 614 } 615 if p == len(f.hist) { 616 p = 0 617 } 618 } 619 return false 620 } 621 622 func (f *decompressor) copyHuff() { 623 if f.copyHist() { 624 return 625 } 626 f.huffmanBlock() 627 } 628 629 // Copy a single uncompressed data block from input to output. 630 func (f *decompressor) dataBlock() { 631 // Uncompressed. 632 // Discard current half-byte. 633 f.nb = 0 634 f.b = 0 635 636 // Length then ones-complement of length. 637 nr, err := io.ReadFull(f.r, f.buf[0:4]) 638 f.roffset += int64(nr) 639 if err != nil { 640 if err == io.EOF { 641 err = io.ErrUnexpectedEOF 642 } 643 f.err = &ReadError{f.roffset, err} 644 return 645 } 646 n := int(f.buf[0]) | int(f.buf[1])<<8 647 nn := int(f.buf[2]) | int(f.buf[3])<<8 648 if uint16(nn) != uint16(^n) { 649 f.err = CorruptInputError(f.roffset) 650 return 651 } 652 653 if n == 0 { 654 // 0-length block means sync 655 f.flush((*decompressor).nextBlock) 656 return 657 } 658 659 f.copyLen = n 660 f.copyData() 661 } 662 663 // copyData copies f.copyLen bytes from the underlying reader into f.hist. 664 // It pauses for reads when f.hist is full. 665 func (f *decompressor) copyData() { 666 n := f.copyLen 667 for n > 0 { 668 m := len(f.hist) - f.hp 669 if m > n { 670 m = n 671 } 672 m, err := io.ReadFull(f.r, f.hist[f.hp:f.hp+m]) 673 f.roffset += int64(m) 674 if err != nil { 675 if err == io.EOF { 676 err = io.ErrUnexpectedEOF 677 } 678 f.err = &ReadError{f.roffset, err} 679 return 680 } 681 n -= m 682 f.hp += m 683 if f.hp == len(f.hist) { 684 f.copyLen = n 685 f.flush((*decompressor).copyData) 686 return 687 } 688 } 689 f.step = (*decompressor).nextBlock 690 } 691 692 func (f *decompressor) setDict(dict []byte) { 693 if len(dict) > len(f.hist) { 694 // Will only remember the tail. 695 dict = dict[len(dict)-len(f.hist):] 696 } 697 698 f.hp = copy(f.hist[:], dict) 699 if f.hp == len(f.hist) { 700 f.hp = 0 701 f.hfull = true 702 } 703 f.hw = f.hp 704 } 705 706 func (f *decompressor) moreBits() error { 707 c, err := f.r.ReadByte() 708 if err != nil { 709 if err == io.EOF { 710 err = io.ErrUnexpectedEOF 711 } 712 return err 713 } 714 f.roffset++ 715 f.b |= uint32(c) << f.nb 716 f.nb += 8 717 return nil 718 } 719 720 // Read the next Huffman-encoded symbol from f according to h. 721 func (f *decompressor) huffSym(h *huffmanDecoder) (int, error) { 722 // Since a huffmanDecoder can be empty or be composed of a degenerate tree 723 // with single element, huffSym must error on these two edge cases. In both 724 // cases, the chunks slice will be 0 for the invalid sequence, leading it 725 // satisfy the n == 0 check below. 726 n := uint(h.min) 727 for { 728 for f.nb < n { 729 if err := f.moreBits(); err != nil { 730 return 0, err 731 } 732 } 733 chunk := h.chunks[f.b&(huffmanNumChunks-1)] 734 n = uint(chunk & huffmanCountMask) 735 if n > huffmanChunkBits { 736 chunk = h.links[chunk>>huffmanValueShift][(f.b>>huffmanChunkBits)&h.linkMask] 737 n = uint(chunk & huffmanCountMask) 738 } 739 if n <= f.nb { 740 if n == 0 { 741 f.err = CorruptInputError(f.roffset) 742 return 0, f.err 743 } 744 f.b >>= n 745 f.nb -= n 746 return int(chunk >> huffmanValueShift), nil 747 } 748 } 749 } 750 751 // Flush any buffered output to the underlying writer. 752 func (f *decompressor) flush(step func(*decompressor)) { 753 f.toRead = f.hist[f.hw:f.hp] 754 f.woffset += int64(f.hp - f.hw) 755 f.hw = f.hp 756 if f.hp == len(f.hist) { 757 f.hp = 0 758 f.hw = 0 759 f.hfull = true 760 } 761 f.step = step 762 } 763 764 func makeReader(r io.Reader) Reader { 765 if rr, ok := r.(Reader); ok { 766 return rr 767 } 768 return bufio.NewReader(r) 769 } 770 771 func fixedHuffmanDecoderInit() { 772 fixedOnce.Do(func() { 773 // These come from the RFC section 3.2.6. 774 var bits [288]int 775 for i := 0; i < 144; i++ { 776 bits[i] = 8 777 } 778 for i := 144; i < 256; i++ { 779 bits[i] = 9 780 } 781 for i := 256; i < 280; i++ { 782 bits[i] = 7 783 } 784 for i := 280; i < 288; i++ { 785 bits[i] = 8 786 } 787 fixedHuffmanDecoder.init(bits[:]) 788 }) 789 } 790 791 func (f *decompressor) Reset(r io.Reader, dict []byte) error { 792 *f = decompressor{ 793 r: makeReader(r), 794 bits: f.bits, 795 codebits: f.codebits, 796 hist: f.hist, 797 step: (*decompressor).nextBlock, 798 } 799 if dict != nil { 800 f.setDict(dict) 801 } 802 return nil 803 } 804 805 // NewReader returns a new ReadCloser that can be used 806 // to read the uncompressed version of r. 807 // If r does not also implement io.ByteReader, 808 // the decompressor may read more data than necessary from r. 809 // It is the caller's responsibility to call Close on the ReadCloser 810 // when finished reading. 811 // 812 // The ReadCloser returned by NewReader also implements Resetter. 813 func NewReader(r io.Reader) io.ReadCloser { 814 fixedHuffmanDecoderInit() 815 816 var f decompressor 817 f.r = makeReader(r) 818 f.hist = new([maxHist]byte) 819 f.bits = new([maxNumLit + maxNumDist]int) 820 f.codebits = new([numCodes]int) 821 f.step = (*decompressor).nextBlock 822 return &f 823 } 824 825 // NewReaderDict is like NewReader but initializes the reader 826 // with a preset dictionary. The returned Reader behaves as if 827 // the uncompressed data stream started with the given dictionary, 828 // which has already been read. NewReaderDict is typically used 829 // to read data compressed by NewWriterDict. 830 // 831 // The ReadCloser returned by NewReader also implements Resetter. 832 func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser { 833 fixedHuffmanDecoderInit() 834 835 var f decompressor 836 f.r = makeReader(r) 837 f.hist = new([maxHist]byte) 838 f.bits = new([maxNumLit + maxNumDist]int) 839 f.codebits = new([numCodes]int) 840 f.step = (*decompressor).nextBlock 841 f.setDict(dict) 842 return &f 843 }