reader.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 zlib implements reading and writing of zlib format compressed data,
  6. as specified in RFC 1950.
  7. The implementation provides filters that uncompress during reading
  8. and compress during writing. For example, to write compressed data
  9. to a buffer:
  10. var b bytes.Buffer
  11. w := zlib.NewWriter(&b)
  12. w.Write([]byte("hello, world\n"))
  13. w.Close()
  14. and to read that data back:
  15. r, err := zlib.NewReader(&b)
  16. io.Copy(os.Stdout, r)
  17. r.Close()
  18. */
  19. package zlib
  20. import (
  21. "bufio"
  22. "compress/zlib"
  23. "hash"
  24. "hash/adler32"
  25. "io"
  26. "github.com/klauspost/compress/flate"
  27. )
  28. const zlibDeflate = 8
  29. var (
  30. // ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
  31. ErrChecksum = zlib.ErrChecksum
  32. // ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
  33. ErrDictionary = zlib.ErrDictionary
  34. // ErrHeader is returned when reading ZLIB data that has an invalid header.
  35. ErrHeader = zlib.ErrHeader
  36. )
  37. type reader struct {
  38. r flate.Reader
  39. decompressor io.ReadCloser
  40. digest hash.Hash32
  41. err error
  42. scratch [4]byte
  43. }
  44. // Resetter resets a ReadCloser returned by NewReader or NewReaderDict to
  45. // to switch to a new underlying Reader. This permits reusing a ReadCloser
  46. // instead of allocating a new one.
  47. type Resetter interface {
  48. // Reset discards any buffered data and resets the Resetter as if it was
  49. // newly initialized with the given reader.
  50. Reset(r io.Reader, dict []byte) error
  51. }
  52. // NewReader creates a new ReadCloser.
  53. // Reads from the returned ReadCloser read and decompress data from r.
  54. // If r does not implement io.ByteReader, the decompressor may read more
  55. // data than necessary from r.
  56. // It is the caller's responsibility to call Close on the ReadCloser when done.
  57. //
  58. // The ReadCloser returned by NewReader also implements Resetter.
  59. func NewReader(r io.Reader) (io.ReadCloser, error) {
  60. return NewReaderDict(r, nil)
  61. }
  62. // NewReaderDict is like NewReader but uses a preset dictionary.
  63. // NewReaderDict ignores the dictionary if the compressed data does not refer to it.
  64. // If the compressed data refers to a different dictionary, NewReaderDict returns ErrDictionary.
  65. //
  66. // The ReadCloser returned by NewReaderDict also implements Resetter.
  67. func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error) {
  68. z := new(reader)
  69. err := z.Reset(r, dict)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return z, nil
  74. }
  75. func (z *reader) Read(p []byte) (int, error) {
  76. if z.err != nil {
  77. return 0, z.err
  78. }
  79. var n int
  80. n, z.err = z.decompressor.Read(p)
  81. z.digest.Write(p[0:n])
  82. if z.err != io.EOF {
  83. // In the normal case we return here.
  84. return n, z.err
  85. }
  86. // Finished file; check checksum.
  87. if _, err := io.ReadFull(z.r, z.scratch[0:4]); err != nil {
  88. if err == io.EOF {
  89. err = io.ErrUnexpectedEOF
  90. }
  91. z.err = err
  92. return n, z.err
  93. }
  94. // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
  95. checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
  96. if checksum != z.digest.Sum32() {
  97. z.err = ErrChecksum
  98. return n, z.err
  99. }
  100. return n, io.EOF
  101. }
  102. // Calling Close does not close the wrapped io.Reader originally passed to NewReader.
  103. // In order for the ZLIB checksum to be verified, the reader must be
  104. // fully consumed until the io.EOF.
  105. func (z *reader) Close() error {
  106. if z.err != nil && z.err != io.EOF {
  107. return z.err
  108. }
  109. z.err = z.decompressor.Close()
  110. return z.err
  111. }
  112. func (z *reader) Reset(r io.Reader, dict []byte) error {
  113. *z = reader{decompressor: z.decompressor, digest: z.digest}
  114. if fr, ok := r.(flate.Reader); ok {
  115. z.r = fr
  116. } else {
  117. z.r = bufio.NewReader(r)
  118. }
  119. // Read the header (RFC 1950 section 2.2.).
  120. _, z.err = io.ReadFull(z.r, z.scratch[0:2])
  121. if z.err != nil {
  122. if z.err == io.EOF {
  123. z.err = io.ErrUnexpectedEOF
  124. }
  125. return z.err
  126. }
  127. h := uint(z.scratch[0])<<8 | uint(z.scratch[1])
  128. if (z.scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) {
  129. z.err = ErrHeader
  130. return z.err
  131. }
  132. haveDict := z.scratch[1]&0x20 != 0
  133. if haveDict {
  134. _, z.err = io.ReadFull(z.r, z.scratch[0:4])
  135. if z.err != nil {
  136. if z.err == io.EOF {
  137. z.err = io.ErrUnexpectedEOF
  138. }
  139. return z.err
  140. }
  141. checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
  142. if checksum != adler32.Checksum(dict) {
  143. z.err = ErrDictionary
  144. return z.err
  145. }
  146. }
  147. if z.decompressor == nil {
  148. if haveDict {
  149. z.decompressor = flate.NewReaderDict(z.r, dict)
  150. } else {
  151. z.decompressor = flate.NewReader(z.r)
  152. }
  153. } else {
  154. z.decompressor.(flate.Resetter).Reset(z.r, dict)
  155. }
  156. if z.digest != nil {
  157. z.digest.Reset()
  158. } else {
  159. z.digest = adler32.New()
  160. }
  161. return nil
  162. }