1
// Copyright 2011 The Snappy-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.
14
// ErrCorrupt reports that the input is invalid.
15
ErrCorrupt = errors.New("snappy: corrupt input")
16
// ErrTooLarge reports that the uncompressed length is too large.
17
ErrTooLarge = errors.New("snappy: decoded block is too large")
18
// ErrUnsupported reports that the input isn't supported.
19
ErrUnsupported = errors.New("snappy: unsupported input")
21
errUnsupportedLiteralLength = errors.New("snappy: unsupported literal length")
24
// DecodedLen returns the length of the decoded block.
25
func DecodedLen(src []byte) (int, error) {
26
v, _, err := decodedLen(src)
30
// decodedLen returns the length of the decoded block and the number of bytes
31
// that the length header occupied.
32
func decodedLen(src []byte) (blockLen, headerLen int, err error) {
33
v, n := binary.Uvarint(src)
34
if n <= 0 || v > 0xffffffff {
35
return 0, 0, ErrCorrupt
38
const wordSize = 32 << (^uint(0) >> 32 & 1)
39
if wordSize == 32 && v > 0x7fffffff {
40
return 0, 0, ErrTooLarge
46
decodeErrCodeCorrupt = 1
47
decodeErrCodeUnsupportedLiteralLength = 2
50
// Decode returns the decoded form of src. The returned slice may be a sub-
51
// slice of dst if dst was large enough to hold the entire decoded block.
52
// Otherwise, a newly allocated slice will be returned.
54
// The dst and src must not overlap. It is valid to pass a nil dst.
56
// Decode handles the Snappy block format, not the Snappy stream format.
57
func Decode(dst, src []byte) ([]byte, error) {
58
dLen, s, err := decodedLen(src)
65
dst = make([]byte, dLen)
67
switch decode(dst, src[s:]) {
70
case decodeErrCodeUnsupportedLiteralLength:
71
return nil, errUnsupportedLiteralLength
73
return nil, ErrCorrupt
76
// NewReader returns a new Reader that decompresses from r, using the framing
78
// https://github.com/google/snappy/blob/master/framing_format.txt
79
func NewReader(r io.Reader) *Reader {
82
decoded: make([]byte, maxBlockSize),
83
buf: make([]byte, maxEncodedLenOfMaxBlockSize+checksumSize),
87
// Reader is an io.Reader that can read Snappy-compressed bytes.
89
// Reader handles the Snappy stream format, not the Snappy block format.
95
// decoded[i:j] contains decoded bytes that have not yet been passed on.
100
// Reset discards any buffered data, resets all state, and switches the Snappy
101
// reader to read from r. This permits reusing a Reader rather than allocating
103
func (r *Reader) Reset(reader io.Reader) {
111
func (r *Reader) readFull(p []byte, allowEOF bool) (ok bool) {
112
if _, r.err = io.ReadFull(r.r, p); r.err != nil {
113
if r.err == io.ErrUnexpectedEOF || (r.err == io.EOF && !allowEOF) {
121
func (r *Reader) fill() error {
123
if !r.readFull(r.buf[:4], true) {
126
chunkType := r.buf[0]
128
if chunkType != chunkTypeStreamIdentifier {
134
chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
135
if chunkLen > len(r.buf) {
136
r.err = ErrUnsupported
140
// The chunk types are specified at
141
// https://github.com/google/snappy/blob/master/framing_format.txt
143
case chunkTypeCompressedData:
144
// Section 4.2. Compressed data (chunk type 0x00).
145
if chunkLen < checksumSize {
149
buf := r.buf[:chunkLen]
150
if !r.readFull(buf, false) {
153
checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
154
buf = buf[checksumSize:]
156
n, err := DecodedLen(buf)
161
if n > len(r.decoded) {
165
if _, err := Decode(r.decoded, buf); err != nil {
169
if crc(r.decoded[:n]) != checksum {
176
case chunkTypeUncompressedData:
177
// Section 4.3. Uncompressed data (chunk type 0x01).
178
if chunkLen < checksumSize {
182
buf := r.buf[:checksumSize]
183
if !r.readFull(buf, false) {
186
checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
187
// Read directly into r.decoded instead of via r.buf.
188
n := chunkLen - checksumSize
189
if n > len(r.decoded) {
193
if !r.readFull(r.decoded[:n], false) {
196
if crc(r.decoded[:n]) != checksum {
203
case chunkTypeStreamIdentifier:
204
// Section 4.1. Stream identifier (chunk type 0xff).
205
if chunkLen != len(magicBody) {
209
if !r.readFull(r.buf[:len(magicBody)], false) {
212
for i := 0; i < len(magicBody); i++ {
213
if r.buf[i] != magicBody[i] {
221
if chunkType <= 0x7f {
222
// Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
223
r.err = ErrUnsupported
226
// Section 4.4 Padding (chunk type 0xfe).
227
// Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
228
if !r.readFull(r.buf[:chunkLen], false) {
236
// Read satisfies the io.Reader interface.
237
func (r *Reader) Read(p []byte) (int, error) {
242
if err := r.fill(); err != nil {
246
n := copy(p, r.decoded[r.i:r.j])
251
// ReadByte satisfies the io.ByteReader interface.
252
func (r *Reader) ReadByte() (byte, error) {
257
if err := r.fill(); err != nil {