2009-09-29 20:47:05 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package zlib
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"compress/flate"
|
2012-02-10 00:49:19 -07:00
|
|
|
"fmt"
|
2009-12-15 16:33:31 -07:00
|
|
|
"hash"
|
|
|
|
"hash/adler32"
|
|
|
|
"io"
|
2009-09-29 20:47:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// These constants are copied from the flate package, so that code that imports
|
|
|
|
// "compress/zlib" does not also have to import "compress/flate".
|
|
|
|
const (
|
2009-12-15 16:33:31 -07:00
|
|
|
NoCompression = flate.NoCompression
|
|
|
|
BestSpeed = flate.BestSpeed
|
|
|
|
BestCompression = flate.BestCompression
|
|
|
|
DefaultCompression = flate.DefaultCompression
|
2009-09-29 20:47:05 -06:00
|
|
|
)
|
|
|
|
|
2011-04-15 16:32:03 -06:00
|
|
|
// A Writer takes data written to it and writes the compressed
|
|
|
|
// form of that data to an underlying writer (see NewWriter).
|
|
|
|
type Writer struct {
|
2012-02-10 00:49:19 -07:00
|
|
|
w io.Writer
|
|
|
|
level int
|
|
|
|
dict []byte
|
|
|
|
compressor *flate.Writer
|
|
|
|
digest hash.Hash32
|
|
|
|
err error
|
|
|
|
scratch [4]byte
|
|
|
|
wroteHeader bool
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 00:49:19 -07:00
|
|
|
// NewWriter creates a new Writer that satisfies writes by compressing data
|
|
|
|
// written to w.
|
|
|
|
//
|
|
|
|
// It is the caller's responsibility to call Close on the WriteCloser when done.
|
|
|
|
// Writes may be buffered and not flushed until Close.
|
|
|
|
func NewWriter(w io.Writer) *Writer {
|
|
|
|
z, _ := NewWriterLevelDict(w, DefaultCompression, nil)
|
|
|
|
return z
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 00:49:19 -07:00
|
|
|
// NewWriterLevel is like NewWriter but specifies the compression level instead
|
|
|
|
// of assuming DefaultCompression.
|
|
|
|
//
|
|
|
|
// The compression level can be DefaultCompression, NoCompression, or any
|
|
|
|
// integer value between BestSpeed and BestCompression inclusive. The error
|
|
|
|
// returned will be nil if the level is valid.
|
2011-11-01 20:04:37 -06:00
|
|
|
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
|
2012-02-10 00:49:19 -07:00
|
|
|
return NewWriterLevelDict(w, level, nil)
|
2011-04-15 16:32:03 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 00:49:19 -07:00
|
|
|
// NewWriterLevelDict is like NewWriterLevel but specifies a dictionary to
|
|
|
|
// compress with.
|
|
|
|
//
|
|
|
|
// The dictionary may be nil. If not, its contents should not be modified until
|
|
|
|
// the Writer is closed.
|
|
|
|
func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) {
|
|
|
|
if level < DefaultCompression || level > BestCompression {
|
|
|
|
return nil, fmt.Errorf("zlib: invalid compression level: %d", level)
|
|
|
|
}
|
|
|
|
return &Writer{
|
|
|
|
w: w,
|
|
|
|
level: level,
|
|
|
|
dict: dict,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeHeader writes the ZLIB header.
|
|
|
|
func (z *Writer) writeHeader() (err error) {
|
|
|
|
z.wroteHeader = true
|
2009-09-29 20:47:05 -06:00
|
|
|
// ZLIB has a two-byte header (as documented in RFC 1950).
|
|
|
|
// The first four bits is the CINFO (compression info), which is 7 for the default deflate window size.
|
|
|
|
// The next four bits is the CM (compression method), which is 8 for deflate.
|
2009-12-15 16:33:31 -07:00
|
|
|
z.scratch[0] = 0x78
|
2009-09-29 20:47:05 -06:00
|
|
|
// The next two bits is the FLEVEL (compression level). The four values are:
|
|
|
|
// 0=fastest, 1=fast, 2=default, 3=best.
|
2011-04-15 16:32:03 -06:00
|
|
|
// The next bit, FDICT, is set if a dictionary is given.
|
2009-09-29 20:47:05 -06:00
|
|
|
// The final five FCHECK bits form a mod-31 checksum.
|
2012-02-10 00:49:19 -07:00
|
|
|
switch z.level {
|
2009-09-29 20:47:05 -06:00
|
|
|
case 0, 1:
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[1] = 0 << 6
|
2009-09-29 20:47:05 -06:00
|
|
|
case 2, 3, 4, 5:
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[1] = 1 << 6
|
2009-09-29 20:47:05 -06:00
|
|
|
case 6, -1:
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[1] = 2 << 6
|
2009-09-29 20:47:05 -06:00
|
|
|
case 7, 8, 9:
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[1] = 3 << 6
|
2009-09-29 20:47:05 -06:00
|
|
|
default:
|
2012-02-10 00:49:19 -07:00
|
|
|
panic("unreachable")
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2012-02-10 00:49:19 -07:00
|
|
|
if z.dict != nil {
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[1] |= 1 << 5
|
|
|
|
}
|
|
|
|
z.scratch[1] += uint8(31 - (uint16(z.scratch[0])<<8+uint16(z.scratch[1]))%31)
|
2012-02-10 00:49:19 -07:00
|
|
|
if _, err = z.w.Write(z.scratch[0:2]); err != nil {
|
|
|
|
return err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2012-02-10 00:49:19 -07:00
|
|
|
if z.dict != nil {
|
2011-04-15 16:32:03 -06:00
|
|
|
// The next four bytes are the Adler-32 checksum of the dictionary.
|
2012-02-10 00:49:19 -07:00
|
|
|
checksum := adler32.Checksum(z.dict)
|
2011-04-15 16:32:03 -06:00
|
|
|
z.scratch[0] = uint8(checksum >> 24)
|
|
|
|
z.scratch[1] = uint8(checksum >> 16)
|
|
|
|
z.scratch[2] = uint8(checksum >> 8)
|
|
|
|
z.scratch[3] = uint8(checksum >> 0)
|
2012-02-10 00:49:19 -07:00
|
|
|
if _, err = z.w.Write(z.scratch[0:4]); err != nil {
|
|
|
|
return err
|
2011-04-15 16:32:03 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-10 00:49:19 -07:00
|
|
|
z.compressor, err = flate.NewWriterDict(z.w, z.level, z.dict)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
z.digest = adler32.New()
|
2012-02-10 00:49:19 -07:00
|
|
|
return nil
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 15:42:07 -07:00
|
|
|
// Write writes a compressed form of p to the underlying io.Writer. The
|
|
|
|
// compressed bytes are not necessarily flushed until the Writer is closed or
|
|
|
|
// explicitly flushed.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (z *Writer) Write(p []byte) (n int, err error) {
|
2012-02-10 00:49:19 -07:00
|
|
|
if !z.wroteHeader {
|
|
|
|
z.err = z.writeHeader()
|
|
|
|
}
|
2009-09-29 20:47:05 -06:00
|
|
|
if z.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
if len(p) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, nil
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2010-05-07 15:32:34 -06:00
|
|
|
n, err = z.compressor.Write(p)
|
2009-09-29 20:47:05 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
z.err = err
|
|
|
|
return
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
z.digest.Write(p)
|
|
|
|
return
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 15:42:07 -07:00
|
|
|
// Flush flushes the Writer to its underlying io.Writer.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (z *Writer) Flush() error {
|
2012-02-10 00:49:19 -07:00
|
|
|
if !z.wroteHeader {
|
|
|
|
z.err = z.writeHeader()
|
|
|
|
}
|
2011-04-15 16:32:03 -06:00
|
|
|
if z.err != nil {
|
|
|
|
return z.err
|
|
|
|
}
|
|
|
|
z.err = z.compressor.Flush()
|
|
|
|
return z.err
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:32:34 -06:00
|
|
|
// Calling Close does not close the wrapped io.Writer originally passed to NewWriter.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (z *Writer) Close() error {
|
2012-02-10 00:49:19 -07:00
|
|
|
if !z.wroteHeader {
|
|
|
|
z.err = z.writeHeader()
|
|
|
|
}
|
2009-09-29 20:47:05 -06:00
|
|
|
if z.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2010-05-07 15:32:34 -06:00
|
|
|
z.err = z.compressor.Close()
|
2009-09-29 20:47:05 -06:00
|
|
|
if z.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
checksum := z.digest.Sum32()
|
2009-09-29 20:47:05 -06:00
|
|
|
// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
|
2009-12-15 16:33:31 -07:00
|
|
|
z.scratch[0] = uint8(checksum >> 24)
|
|
|
|
z.scratch[1] = uint8(checksum >> 16)
|
|
|
|
z.scratch[2] = uint8(checksum >> 8)
|
|
|
|
z.scratch[3] = uint8(checksum >> 0)
|
|
|
|
_, z.err = z.w.Write(z.scratch[0:4])
|
|
|
|
return z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|