The Go Programming Language

Source file src/pkg/compress/gzip/gzip.go

     1	// Copyright 2010 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 gzip
     6	
     7	import (
     8		"compress/flate"
     9		"hash"
    10		"hash/crc32"
    11		"io"
    12		"os"
    13	)
    14	
    15	// These constants are copied from the flate package, so that code that imports
    16	// "compress/gzip" does not also have to import "compress/flate".
    17	const (
    18		NoCompression      = flate.NoCompression
    19		BestSpeed          = flate.BestSpeed
    20		BestCompression    = flate.BestCompression
    21		DefaultCompression = flate.DefaultCompression
    22	)
    23	
    24	// A Compressor is an io.WriteCloser that satisfies writes by compressing data written
    25	// to its wrapped io.Writer.
    26	type Compressor struct {
    27		Header
    28		w          io.Writer
    29		level      int
    30		compressor io.WriteCloser
    31		digest     hash.Hash32
    32		size       uint32
    33		closed     bool
    34		buf        [10]byte
    35		err        os.Error
    36	}
    37	
    38	// NewWriter calls NewWriterLevel with the default compression level.
    39	func NewWriter(w io.Writer) (*Compressor, os.Error) {
    40		return NewWriterLevel(w, DefaultCompression)
    41	}
    42	
    43	// NewWriterLevel creates a new Compressor writing to the given writer.
    44	// Writes may be buffered and not flushed until Close.
    45	// Callers that wish to set the fields in Compressor.Header must
    46	// do so before the first call to Write or Close.
    47	// It is the caller's responsibility to call Close on the WriteCloser when done.
    48	// level is the compression level, which can be DefaultCompression, NoCompression,
    49	// or any integer value between BestSpeed and BestCompression (inclusive).
    50	func NewWriterLevel(w io.Writer, level int) (*Compressor, os.Error) {
    51		z := new(Compressor)
    52		z.OS = 255 // unknown
    53		z.w = w
    54		z.level = level
    55		z.digest = crc32.NewIEEE()
    56		return z, nil
    57	}
    58	
    59	// GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950).
    60	func put2(p []byte, v uint16) {
    61		p[0] = uint8(v >> 0)
    62		p[1] = uint8(v >> 8)
    63	}
    64	
    65	func put4(p []byte, v uint32) {
    66		p[0] = uint8(v >> 0)
    67		p[1] = uint8(v >> 8)
    68		p[2] = uint8(v >> 16)
    69		p[3] = uint8(v >> 24)
    70	}
    71	
    72	// writeBytes writes a length-prefixed byte slice to z.w.
    73	func (z *Compressor) writeBytes(b []byte) os.Error {
    74		if len(b) > 0xffff {
    75			return os.NewError("gzip.Write: Extra data is too large")
    76		}
    77		put2(z.buf[0:2], uint16(len(b)))
    78		_, err := z.w.Write(z.buf[0:2])
    79		if err != nil {
    80			return err
    81		}
    82		_, err = z.w.Write(b)
    83		return err
    84	}
    85	
    86	// writeString writes a string (in ISO 8859-1 (Latin-1) format) to z.w.
    87	func (z *Compressor) writeString(s string) os.Error {
    88		// GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1).
    89		// TODO(nigeltao): Convert from UTF-8 to ISO 8859-1 (Latin-1).
    90		for _, v := range s {
    91			if v == 0 || v > 0x7f {
    92				return os.NewError("gzip.Write: non-ASCII header string")
    93			}
    94		}
    95		_, err := io.WriteString(z.w, s)
    96		if err != nil {
    97			return err
    98		}
    99		// GZIP strings are NUL-terminated.
   100		z.buf[0] = 0
   101		_, err = z.w.Write(z.buf[0:1])
   102		return err
   103	}
   104	
   105	func (z *Compressor) Write(p []byte) (int, os.Error) {
   106		if z.err != nil {
   107			return 0, z.err
   108		}
   109		var n int
   110		// Write the GZIP header lazily.
   111		if z.compressor == nil {
   112			z.buf[0] = gzipID1
   113			z.buf[1] = gzipID2
   114			z.buf[2] = gzipDeflate
   115			z.buf[3] = 0
   116			if z.Extra != nil {
   117				z.buf[3] |= 0x04
   118			}
   119			if z.Name != "" {
   120				z.buf[3] |= 0x08
   121			}
   122			if z.Comment != "" {
   123				z.buf[3] |= 0x10
   124			}
   125			put4(z.buf[4:8], z.Mtime)
   126			if z.level == BestCompression {
   127				z.buf[8] = 2
   128			} else if z.level == BestSpeed {
   129				z.buf[8] = 4
   130			} else {
   131				z.buf[8] = 0
   132			}
   133			z.buf[9] = z.OS
   134			n, z.err = z.w.Write(z.buf[0:10])
   135			if z.err != nil {
   136				return n, z.err
   137			}
   138			if z.Extra != nil {
   139				z.err = z.writeBytes(z.Extra)
   140				if z.err != nil {
   141					return n, z.err
   142				}
   143			}
   144			if z.Name != "" {
   145				z.err = z.writeString(z.Name)
   146				if z.err != nil {
   147					return n, z.err
   148				}
   149			}
   150			if z.Comment != "" {
   151				z.err = z.writeString(z.Comment)
   152				if z.err != nil {
   153					return n, z.err
   154				}
   155			}
   156			z.compressor = flate.NewWriter(z.w, z.level)
   157		}
   158		z.size += uint32(len(p))
   159		z.digest.Write(p)
   160		n, z.err = z.compressor.Write(p)
   161		return n, z.err
   162	}
   163	
   164	// Calling Close does not close the wrapped io.Writer originally passed to NewWriter.
   165	func (z *Compressor) Close() os.Error {
   166		if z.err != nil {
   167			return z.err
   168		}
   169		if z.closed {
   170			return nil
   171		}
   172		z.closed = true
   173		if z.compressor == nil {
   174			z.Write(nil)
   175			if z.err != nil {
   176				return z.err
   177			}
   178		}
   179		z.err = z.compressor.Close()
   180		if z.err != nil {
   181			return z.err
   182		}
   183		put4(z.buf[0:4], z.digest.Sum32())
   184		put4(z.buf[4:8], z.size)
   185		_, z.err = z.w.Write(z.buf[0:8])
   186		return z.err
   187	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.