@@ -5,14 +5,13 @@ import ( "io" ) type context struct { - table [1 << 16]byte - buffer [1 << 3]byte - input []byte - hash uint16 + table [1 << 16]byte + input []byte + hash uint16 } type compressor func([]byte) error func (w compressor) Write(data []byte) (int, error) { @@ -24,11 +23,11 @@ // // It can buffer data as the predictor mandates 8-byte blocks with a header. // A call with no data will force a flush. func Compressor(writer io.Writer) io.Writer { var ctx context - ctx.input = ctx.buffer[:0] + ctx.input = make([]byte, 0, 8) // Forward declaration as it is required for recursion var write compressor write = func(data []byte) error { @@ -96,14 +95,14 @@ // Reset the flags and buffer for the next iteration buf, buf[0] = buf[:1], 0 } if remaining := len(data) % blockSize; remaining > 0 { - ctx.input = ctx.buffer[:remaining] + ctx.input = ctx.input[:remaining] copy(ctx.input, data[len(data)-remaining:]) } else { - ctx.input = ctx.buffer[:0] + ctx.input = ctx.input[:0] } return nil } @@ -120,11 +119,11 @@ // Returns an io.Reader implementation that wraps the provided io.Reader // and decompresses data according to the predictor algorithm func Decompressor(wrapped io.Reader) io.Reader { var ctx context - ctx.input = ctx.buffer[:0] + ctx.input = make([]byte, 0, 8) return decompressor(func(output []byte) (int, error) { var ( err error flags byte @@ -146,18 +145,18 @@ // This is single-iteration only but it is fine according to io.Reader's contract ?! // TODO - read all bytes from a block based on the hamming weight of the flag // and just shuffle them for predictions instead of bite-sized reads ;) // Read the flags - ctx.input = ctx.buffer[:1] + ctx.input = ctx.input[:1] readCount, err = wrapped.Read(ctx.input) if readCount == 0 || err != nil { return readCount, err } flags = ctx.input[0] - ctx.input = ctx.buffer[:8] + ctx.input = ctx.input[:8] var i uint = 0 for ; i < 8; i++ { if flags&(1< 0 { // Guess was right @@ -187,11 +186,11 @@ // Place any remaining bytes in the buffer if uint(readCount) < i { ctx.input = ctx.input[readCount:i] } else { - ctx.input = ctx.buffer[:0] + ctx.input = ctx.input[:0] } return readCount, nil }) }