writer.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. package zlib
  5. import (
  6. "fmt"
  7. "hash"
  8. "hash/adler32"
  9. "io"
  10. "github.com/klauspost/compress/flate"
  11. )
  12. // These constants are copied from the flate package, so that code that imports
  13. // "compress/zlib" does not also have to import "compress/flate".
  14. const (
  15. NoCompression = flate.NoCompression
  16. BestSpeed = flate.BestSpeed
  17. BestCompression = flate.BestCompression
  18. DefaultCompression = flate.DefaultCompression
  19. ConstantCompression = flate.ConstantCompression
  20. HuffmanOnly = flate.HuffmanOnly
  21. )
  22. // A Writer takes data written to it and writes the compressed
  23. // form of that data to an underlying writer (see NewWriter).
  24. type Writer struct {
  25. w io.Writer
  26. level int
  27. dict []byte
  28. compressor *flate.Writer
  29. digest hash.Hash32
  30. err error
  31. scratch [4]byte
  32. wroteHeader bool
  33. }
  34. // NewWriter creates a new Writer.
  35. // Writes to the returned Writer are compressed and written to w.
  36. //
  37. // It is the caller's responsibility to call Close on the WriteCloser when done.
  38. // Writes may be buffered and not flushed until Close.
  39. func NewWriter(w io.Writer) *Writer {
  40. z, _ := NewWriterLevelDict(w, DefaultCompression, nil)
  41. return z
  42. }
  43. // NewWriterLevel is like NewWriter but specifies the compression level instead
  44. // of assuming DefaultCompression.
  45. //
  46. // The compression level can be DefaultCompression, NoCompression, HuffmanOnly
  47. // or any integer value between BestSpeed and BestCompression inclusive.
  48. // The error returned will be nil if the level is valid.
  49. func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
  50. return NewWriterLevelDict(w, level, nil)
  51. }
  52. // NewWriterLevelDict is like NewWriterLevel but specifies a dictionary to
  53. // compress with.
  54. //
  55. // The dictionary may be nil. If not, its contents should not be modified until
  56. // the Writer is closed.
  57. func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) {
  58. if level < HuffmanOnly || level > BestCompression {
  59. return nil, fmt.Errorf("zlib: invalid compression level: %d", level)
  60. }
  61. return &Writer{
  62. w: w,
  63. level: level,
  64. dict: dict,
  65. }, nil
  66. }
  67. // Reset clears the state of the Writer z such that it is equivalent to its
  68. // initial state from NewWriterLevel or NewWriterLevelDict, but instead writing
  69. // to w.
  70. func (z *Writer) Reset(w io.Writer) {
  71. z.w = w
  72. // z.level and z.dict left unchanged.
  73. if z.compressor != nil {
  74. z.compressor.Reset(w)
  75. }
  76. if z.digest != nil {
  77. z.digest.Reset()
  78. }
  79. z.err = nil
  80. z.scratch = [4]byte{}
  81. z.wroteHeader = false
  82. }
  83. // writeHeader writes the ZLIB header.
  84. func (z *Writer) writeHeader() (err error) {
  85. z.wroteHeader = true
  86. // ZLIB has a two-byte header (as documented in RFC 1950).
  87. // The first four bits is the CINFO (compression info), which is 7 for the default deflate window size.
  88. // The next four bits is the CM (compression method), which is 8 for deflate.
  89. z.scratch[0] = 0x78
  90. // The next two bits is the FLEVEL (compression level). The four values are:
  91. // 0=fastest, 1=fast, 2=default, 3=best.
  92. // The next bit, FDICT, is set if a dictionary is given.
  93. // The final five FCHECK bits form a mod-31 checksum.
  94. switch z.level {
  95. case -2, 0, 1:
  96. z.scratch[1] = 0 << 6
  97. case 2, 3, 4, 5:
  98. z.scratch[1] = 1 << 6
  99. case 6, -1:
  100. z.scratch[1] = 2 << 6
  101. case 7, 8, 9:
  102. z.scratch[1] = 3 << 6
  103. default:
  104. panic("unreachable")
  105. }
  106. if z.dict != nil {
  107. z.scratch[1] |= 1 << 5
  108. }
  109. z.scratch[1] += uint8(31 - (uint16(z.scratch[0])<<8+uint16(z.scratch[1]))%31)
  110. if _, err = z.w.Write(z.scratch[0:2]); err != nil {
  111. return err
  112. }
  113. if z.dict != nil {
  114. // The next four bytes are the Adler-32 checksum of the dictionary.
  115. checksum := adler32.Checksum(z.dict)
  116. z.scratch[0] = uint8(checksum >> 24)
  117. z.scratch[1] = uint8(checksum >> 16)
  118. z.scratch[2] = uint8(checksum >> 8)
  119. z.scratch[3] = uint8(checksum >> 0)
  120. if _, err = z.w.Write(z.scratch[0:4]); err != nil {
  121. return err
  122. }
  123. }
  124. if z.compressor == nil {
  125. // Initialize deflater unless the Writer is being reused
  126. // after a Reset call.
  127. z.compressor, err = flate.NewWriterDict(z.w, z.level, z.dict)
  128. if err != nil {
  129. return err
  130. }
  131. z.digest = adler32.New()
  132. }
  133. return nil
  134. }
  135. // Write writes a compressed form of p to the underlying io.Writer. The
  136. // compressed bytes are not necessarily flushed until the Writer is closed or
  137. // explicitly flushed.
  138. func (z *Writer) Write(p []byte) (n int, err error) {
  139. if !z.wroteHeader {
  140. z.err = z.writeHeader()
  141. }
  142. if z.err != nil {
  143. return 0, z.err
  144. }
  145. if len(p) == 0 {
  146. return 0, nil
  147. }
  148. n, err = z.compressor.Write(p)
  149. if err != nil {
  150. z.err = err
  151. return
  152. }
  153. z.digest.Write(p)
  154. return
  155. }
  156. // Flush flushes the Writer to its underlying io.Writer.
  157. func (z *Writer) Flush() error {
  158. if !z.wroteHeader {
  159. z.err = z.writeHeader()
  160. }
  161. if z.err != nil {
  162. return z.err
  163. }
  164. z.err = z.compressor.Flush()
  165. return z.err
  166. }
  167. // Close closes the Writer, flushing any unwritten data to the underlying
  168. // io.Writer, but does not close the underlying io.Writer.
  169. func (z *Writer) Close() error {
  170. if !z.wroteHeader {
  171. z.err = z.writeHeader()
  172. }
  173. if z.err != nil {
  174. return z.err
  175. }
  176. z.err = z.compressor.Close()
  177. if z.err != nil {
  178. return z.err
  179. }
  180. checksum := z.digest.Sum32()
  181. // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
  182. z.scratch[0] = uint8(checksum >> 24)
  183. z.scratch[1] = uint8(checksum >> 16)
  184. z.scratch[2] = uint8(checksum >> 8)
  185. z.scratch[3] = uint8(checksum >> 0)
  186. _, z.err = z.w.Write(z.scratch[0:4])
  187. return z.err
  188. }