diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-05-23 09:45:29 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-05-23 09:45:29 +0200 |
commit | 63d29fefab5290dc96e0a03ff70603aefa995887 (patch) | |
tree | 95da0105686f9aba568a72e7a8ebd580a4fda20e /src/pkg/compress | |
parent | ad811fbb8897a9a3063274e927133915941f1dca (diff) | |
download | golang-63d29fefab5290dc96e0a03ff70603aefa995887.tar.gz |
Imported Upstream version 2011.05.22upstream-weekly/2011.05.22
Diffstat (limited to 'src/pkg/compress')
-rw-r--r-- | src/pkg/compress/bzip2/bzip2.go | 4 | ||||
-rw-r--r-- | src/pkg/compress/bzip2/huffman.go | 2 | ||||
-rw-r--r-- | src/pkg/compress/flate/deflate.go | 29 | ||||
-rw-r--r-- | src/pkg/compress/flate/huffman_bit_writer.go | 4 | ||||
-rw-r--r-- | src/pkg/compress/gzip/gzip_test.go | 2 | ||||
-rw-r--r-- | src/pkg/compress/lzw/reader.go | 13 | ||||
-rw-r--r-- | src/pkg/compress/zlib/writer.go | 2 | ||||
-rw-r--r-- | src/pkg/compress/zlib/writer_test.go | 18 |
8 files changed, 57 insertions, 17 deletions
diff --git a/src/pkg/compress/bzip2/bzip2.go b/src/pkg/compress/bzip2/bzip2.go index 9e97edec1..8b4572306 100644 --- a/src/pkg/compress/bzip2/bzip2.go +++ b/src/pkg/compress/bzip2/bzip2.go @@ -284,7 +284,7 @@ func (bz2 *reader) readBlock() (err os.Error) { repeat := 0 repeat_power := 0 - // The `C' array (used by the inverse BWT) needs to be zero initialised. + // The `C' array (used by the inverse BWT) needs to be zero initialized. for i := range bz2.c { bz2.c[i] = 0 } @@ -330,7 +330,7 @@ func (bz2 *reader) readBlock() (err os.Error) { if int(v) == numSymbols-1 { // This is the EOF symbol. Because it's always at the - // end of the move-to-front list, and nevers gets moved + // end of the move-to-front list, and never gets moved // to the front, it has this unique value. break } diff --git a/src/pkg/compress/bzip2/huffman.go b/src/pkg/compress/bzip2/huffman.go index 732bc4a21..dc05739c7 100644 --- a/src/pkg/compress/bzip2/huffman.go +++ b/src/pkg/compress/bzip2/huffman.go @@ -68,7 +68,7 @@ func newHuffmanTree(lengths []uint8) (huffmanTree, os.Error) { // each symbol (consider reflecting a tree down the middle, for // example). Since the code length assignments determine the // efficiency of the tree, each of these trees is equally good. In - // order to minimise the amount of information needed to build a tree + // order to minimize the amount of information needed to build a tree // bzip2 uses a canonical tree so that it can be reconstructed given // only the code length assignments. diff --git a/src/pkg/compress/flate/deflate.go b/src/pkg/compress/flate/deflate.go index e5b2beaef..a02a5e8d9 100644 --- a/src/pkg/compress/flate/deflate.go +++ b/src/pkg/compress/flate/deflate.go @@ -143,10 +143,18 @@ func (d *compressor) fillWindow(index int) (int, os.Error) { d.blockStart = math.MaxInt32 } for i, h := range d.hashHead { - d.hashHead[i] = max(h-wSize, -1) + v := h - wSize + if v < -1 { + v = -1 + } + d.hashHead[i] = v } for i, h := range d.hashPrev { - d.hashPrev[i] = max(h-wSize, -1) + v := -h - wSize + if v < -1 { + v = -1 + } + d.hashPrev[i] = v } } count, err := d.r.Read(d.window[d.windowEnd:]) @@ -177,10 +185,18 @@ func (d *compressor) writeBlock(tokens []token, index int, eof bool) os.Error { // Try to find a match starting at index whose length is greater than prevSize. // We only look at chainCount possibilities before giving up. func (d *compressor) findMatch(pos int, prevHead int, prevLength int, lookahead int) (length, offset int, ok bool) { - win := d.window[0 : pos+min(maxMatchLength, lookahead)] + minMatchLook := maxMatchLength + if lookahead < minMatchLook { + minMatchLook = lookahead + } + + win := d.window[0 : pos+minMatchLook] // We quit when we get a match that's at least nice long - nice := min(d.niceMatch, len(win)-pos) + nice := len(win) - pos + if d.niceMatch < nice { + nice = d.niceMatch + } // If we've got a match that's good enough, only look in 1/4 the chain. tries := d.maxChainLength @@ -344,9 +360,12 @@ Loop: } prevLength := length prevOffset := offset - minIndex := max(index-maxOffset, 0) length = minMatchLength - 1 offset = 0 + minIndex := index - maxOffset + if minIndex < 0 { + minIndex = 0 + } if chainHead >= minIndex && (isFastDeflate && lookahead > minMatchLength-1 || diff --git a/src/pkg/compress/flate/huffman_bit_writer.go b/src/pkg/compress/flate/huffman_bit_writer.go index abff82dd6..5df4510a2 100644 --- a/src/pkg/compress/flate/huffman_bit_writer.go +++ b/src/pkg/compress/flate/huffman_bit_writer.go @@ -185,7 +185,7 @@ func (w *huffmanBitWriter) writeBytes(bytes []byte) { _, w.err = w.w.Write(bytes) } -// RFC 1951 3.2.7 specifies a special run-length encoding for specifiying +// RFC 1951 3.2.7 specifies a special run-length encoding for specifying // the literal and offset lengths arrays (which are concatenated into a single // array). This method generates that run-length encoding. // @@ -279,7 +279,7 @@ func (w *huffmanBitWriter) writeCode(code *huffmanEncoder, literal uint32) { // // numLiterals The number of literals specified in codegen // numOffsets The number of offsets specified in codegen -// numCodegens Tne number of codegens used in codegen +// numCodegens The number of codegens used in codegen func (w *huffmanBitWriter) writeDynamicHeader(numLiterals int, numOffsets int, numCodegens int, isEof bool) { if w.err != nil { return diff --git a/src/pkg/compress/gzip/gzip_test.go b/src/pkg/compress/gzip/gzip_test.go index 23f351405..121e627e6 100644 --- a/src/pkg/compress/gzip/gzip_test.go +++ b/src/pkg/compress/gzip/gzip_test.go @@ -11,7 +11,7 @@ import ( ) // pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the -// writer end and ifunc at the reader end. +// writer end and cfunc at the reader end. func pipe(t *testing.T, dfunc func(*Compressor), cfunc func(*Decompressor)) { piper, pipew := io.Pipe() defer piper.Close() diff --git a/src/pkg/compress/lzw/reader.go b/src/pkg/compress/lzw/reader.go index d418bc856..a1cd2abc0 100644 --- a/src/pkg/compress/lzw/reader.go +++ b/src/pkg/compress/lzw/reader.go @@ -165,16 +165,19 @@ func decode1(pw *io.PipeWriter, r io.ByteReader, read func(*decoder) (uint16, os if _, err := w.Write(buf[i:]); err != nil { return err } - // Save what the hi code expands to. - suffix[hi] = uint8(c) - prefix[hi] = last + if last != invalidCode { + // Save what the hi code expands to. + suffix[hi] = uint8(c) + prefix[hi] = last + } default: return os.NewError("lzw: invalid code") } last, hi = code, hi+1 - if hi == overflow { + if hi >= overflow { if d.width == maxWidth { - return os.NewError("lzw: missing clear code") + last = invalidCode + continue } d.width++ overflow <<= 1 diff --git a/src/pkg/compress/zlib/writer.go b/src/pkg/compress/zlib/writer.go index f1f9b2853..8f86e9c4c 100644 --- a/src/pkg/compress/zlib/writer.go +++ b/src/pkg/compress/zlib/writer.go @@ -89,7 +89,7 @@ func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, os.Error) { } } z.w = w - z.compressor = flate.NewWriter(w, level) + z.compressor = flate.NewWriterDict(w, level, dict) z.digest = adler32.New() return z, nil } diff --git a/src/pkg/compress/zlib/writer_test.go b/src/pkg/compress/zlib/writer_test.go index f94f28470..a06689ee5 100644 --- a/src/pkg/compress/zlib/writer_test.go +++ b/src/pkg/compress/zlib/writer_test.go @@ -5,6 +5,7 @@ package zlib import ( + "bytes" "io" "io/ioutil" "os" @@ -121,3 +122,20 @@ func TestWriterDict(t *testing.T) { } } } + +func TestWriterDictIsUsed(t *testing.T) { + var input = []byte("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") + buf := bytes.NewBuffer(nil) + compressor, err := NewWriterDict(buf, BestCompression, input) + if err != nil { + t.Errorf("error in NewWriterDict: %s", err) + return + } + compressor.Write(input) + compressor.Close() + const expectedMaxSize = 25 + output := buf.Bytes() + if len(output) > expectedMaxSize { + t.Errorf("result too large (got %d, want <= %d bytes). Is the dictionary being used?", len(output), expectedMaxSize) + } +} |