Commit da8e6eec authored by Adam Langley's avatar Adam Langley

compress/bzip2: fix bugs

(Once again, proving that a fuzz tester is the first thing that I
should try.)

R=bradfitzgo
CC=golang-dev
https://golang.org/cl/4227042
parent 73aacbda
...@@ -141,6 +141,8 @@ func (bz2 *reader) read(buf []byte) (n int, err os.Error) { ...@@ -141,6 +141,8 @@ func (bz2 *reader) read(buf []byte) (n int, err os.Error) {
if bz2.lastByte == int(b) { if bz2.lastByte == int(b) {
bz2.byteRepeats++ bz2.byteRepeats++
} else {
bz2.byteRepeats = 0
} }
bz2.lastByte = int(b) bz2.lastByte = int(b)
...@@ -229,6 +231,18 @@ func (bz2 *reader) readBlock() (err os.Error) { ...@@ -229,6 +231,18 @@ func (bz2 *reader) readBlock() (err os.Error) {
treeIndexes[i] = uint8(mtfTreeDecoder.Decode(c)) treeIndexes[i] = uint8(mtfTreeDecoder.Decode(c))
} }
// The list of symbols for the move-to-front transform is taken from
// the previously decoded symbol bitmap.
symbols := make([]byte, numSymbols)
nextSymbol := 0
for i := 0; i < 256; i++ {
if symbolPresent[i] {
symbols[nextSymbol] = byte(i)
nextSymbol++
}
}
mtf := newMTFDecoder(symbols)
numSymbols += 2 // to account for RUNA and RUNB symbols numSymbols += 2 // to account for RUNA and RUNB symbols
huffmanTrees := make([]huffmanTree, numHuffmanTrees) huffmanTrees := make([]huffmanTree, numHuffmanTrees)
...@@ -259,18 +273,6 @@ func (bz2 *reader) readBlock() (err os.Error) { ...@@ -259,18 +273,6 @@ func (bz2 *reader) readBlock() (err os.Error) {
} }
} }
// The list of symbols for the move-to-front transform is taken from
// the previously decoded symbol bitmap.
symbols := make([]byte, numSymbols)
nextSymbol := 0
for i := 0; i < 256; i++ {
if symbolPresent[i] {
symbols[nextSymbol] = byte(i)
nextSymbol++
}
}
mtf := newMTFDecoder(symbols)
selectorIndex := 1 // the next tree index to use selectorIndex := 1 // the next tree index to use
currentHuffmanTree := huffmanTrees[treeIndexes[0]] currentHuffmanTree := huffmanTrees[treeIndexes[0]]
bufIndex := 0 // indexes bz2.buf, the output buffer. bufIndex := 0 // indexes bz2.buf, the output buffer.
......
This diff is collapsed.
...@@ -13,7 +13,7 @@ package bzip2 ...@@ -13,7 +13,7 @@ package bzip2
// as the symbol will be at the front of the list after the first access. // as the symbol will be at the front of the list after the first access.
type moveToFrontDecoder struct { type moveToFrontDecoder struct {
// Rather than actually keep the list in memory, the symbols are stored // Rather than actually keep the list in memory, the symbols are stored
// as a circular, double linked list which the symbol indexed by head // as a circular, double linked list with the symbol indexed by head
// at the front of the list. // at the front of the list.
symbols []byte symbols []byte
next []uint8 next []uint8
...@@ -24,6 +24,10 @@ type moveToFrontDecoder struct { ...@@ -24,6 +24,10 @@ type moveToFrontDecoder struct {
// newMTFDecoder creates a move-to-front decoder with an explicit initial list // newMTFDecoder creates a move-to-front decoder with an explicit initial list
// of symbols. // of symbols.
func newMTFDecoder(symbols []byte) *moveToFrontDecoder { func newMTFDecoder(symbols []byte) *moveToFrontDecoder {
if len(symbols) > 256 {
panic("too many symbols")
}
m := &moveToFrontDecoder{ m := &moveToFrontDecoder{
symbols: symbols, symbols: symbols,
next: make([]uint8, len(symbols)), next: make([]uint8, len(symbols)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment