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 (
|
|
|
|
"compress/flate";
|
|
|
|
"hash";
|
|
|
|
"hash/adler32";
|
|
|
|
"io";
|
|
|
|
"os";
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-10-06 15:55:39 -06:00
|
|
|
NoCompression = flate.NoCompression;
|
|
|
|
BestSpeed = flate.BestSpeed;
|
|
|
|
BestCompression = flate.BestCompression;
|
|
|
|
DefaultCompression = flate.DefaultCompression;
|
2009-09-29 20:47:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type writer struct {
|
2009-10-06 15:55:39 -06:00
|
|
|
w io.Writer;
|
|
|
|
deflater io.WriteCloser;
|
|
|
|
digest hash.Hash32;
|
|
|
|
err os.Error;
|
|
|
|
scratch [4]byte;
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeflater calls NewDeflaterLevel with the default compression level.
|
|
|
|
func NewDeflater(w io.Writer) (io.WriteCloser, os.Error) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return NewDeflaterLevel(w, DefaultCompression)
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeflater creates a new io.WriteCloser that satisfies writes by compressing data written to w.
|
|
|
|
// It is the caller's responsibility to call Close on the WriteCloser when done.
|
|
|
|
// level is the compression level, which can be DefaultCompression, NoCompression,
|
|
|
|
// or any integer value between BestSpeed and BestCompression (inclusive).
|
|
|
|
func NewDeflaterLevel(w io.Writer, level int) (io.WriteCloser, os.Error) {
|
|
|
|
z := new(writer);
|
|
|
|
// 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.
|
|
|
|
z.scratch[0] = 0x78;
|
|
|
|
// The next two bits is the FLEVEL (compression level). The four values are:
|
|
|
|
// 0=fastest, 1=fast, 2=default, 3=best.
|
|
|
|
// The next bit, FDICT, is unused, in this implementation.
|
|
|
|
// The final five FCHECK bits form a mod-31 checksum.
|
|
|
|
switch level {
|
|
|
|
case 0, 1:
|
2009-11-09 13:07:39 -07:00
|
|
|
z.scratch[1] = 0x01
|
2009-09-29 20:47:05 -06:00
|
|
|
case 2, 3, 4, 5:
|
2009-11-09 13:07:39 -07:00
|
|
|
z.scratch[1] = 0x5e
|
2009-09-29 20:47:05 -06:00
|
|
|
case 6, -1:
|
2009-11-09 13:07:39 -07:00
|
|
|
z.scratch[1] = 0x9c
|
2009-09-29 20:47:05 -06:00
|
|
|
case 7, 8, 9:
|
2009-11-09 13:07:39 -07:00
|
|
|
z.scratch[1] = 0xda
|
2009-09-29 20:47:05 -06:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, os.NewError("level out of range")
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
_, err := w.Write(z.scratch[0:2]);
|
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
z.w = w;
|
|
|
|
z.deflater = flate.NewDeflater(w, level);
|
|
|
|
z.digest = adler32.New();
|
|
|
|
return z, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *writer) Write(p []byte) (n int, err os.Error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
n, err = z.deflater.Write(p);
|
|
|
|
if err != nil {
|
|
|
|
z.err = err;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
z.digest.Write(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling Close does not close the wrapped io.Writer originally passed to NewDeflater.
|
|
|
|
func (z *writer) Close() os.Error {
|
|
|
|
if z.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
z.err = z.deflater.Close();
|
|
|
|
if z.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return z.err
|
2009-09-29 20:47:05 -06:00
|
|
|
}
|
|
|
|
checksum := z.digest.Sum32();
|
|
|
|
// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
|
2009-11-09 22:13:17 -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);
|
2009-09-29 20:47:05 -06:00
|
|
|
_, z.err = z.w.Write(z.scratch[0:4]);
|
|
|
|
return z.err;
|
|
|
|
}
|