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.
55 func Decode(dst, src []byte) ([]byte, error) {
56 dLen, s, err := decodedLen(src)
63 dst = make([]byte, dLen)
65 switch decode(dst, src[s:]) {
68 case decodeErrCodeUnsupportedLiteralLength:
69 return nil, errUnsupportedLiteralLength
71 return nil, ErrCorrupt
74 // NewReader returns a new Reader that decompresses from r, using the framing
75 // format described at
76 // https://github.com/google/snappy/blob/master/framing_format.txt
77 func NewReader(r io.Reader) *Reader {
80 decoded: make([]byte, maxBlockSize),
81 buf: make([]byte, maxEncodedLenOfMaxBlockSize+checksumSize),
85 // Reader is an io.Reader that can read Snappy-compressed bytes.
91 // decoded[i:j] contains decoded bytes that have not yet been passed on.
96 // Reset discards any buffered data, resets all state, and switches the Snappy
97 // reader to read from r. This permits reusing a Reader rather than allocating
99 func (r *Reader) Reset(reader io.Reader) {
107 func (r *Reader) readFull(p []byte, allowEOF bool) (ok bool) {
108 if _, r.err = io.ReadFull(r.r, p); r.err != nil {
109 if r.err == io.ErrUnexpectedEOF || (r.err == io.EOF && !allowEOF) {
117 // Read satisfies the io.Reader interface.
118 func (r *Reader) Read(p []byte) (int, error) {
124 n := copy(p, r.decoded[r.i:r.j])
128 if !r.readFull(r.buf[:4], true) {
131 chunkType := r.buf[0]
133 if chunkType != chunkTypeStreamIdentifier {
139 chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
140 if chunkLen > len(r.buf) {
141 r.err = ErrUnsupported
145 // The chunk types are specified at
146 // https://github.com/google/snappy/blob/master/framing_format.txt
148 case chunkTypeCompressedData:
149 // Section 4.2. Compressed data (chunk type 0x00).
150 if chunkLen < checksumSize {
154 buf := r.buf[:chunkLen]
155 if !r.readFull(buf, false) {
158 checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
159 buf = buf[checksumSize:]
161 n, err := DecodedLen(buf)
166 if n > len(r.decoded) {
170 if _, err := Decode(r.decoded, buf); err != nil {
174 if crc(r.decoded[:n]) != checksum {
181 case chunkTypeUncompressedData:
182 // Section 4.3. Uncompressed data (chunk type 0x01).
183 if chunkLen < checksumSize {
187 buf := r.buf[:checksumSize]
188 if !r.readFull(buf, false) {
191 checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
192 // Read directly into r.decoded instead of via r.buf.
193 n := chunkLen - checksumSize
194 if n > len(r.decoded) {
198 if !r.readFull(r.decoded[:n], false) {
201 if crc(r.decoded[:n]) != checksum {
208 case chunkTypeStreamIdentifier:
209 // Section 4.1. Stream identifier (chunk type 0xff).
210 if chunkLen != len(magicBody) {
214 if !r.readFull(r.buf[:len(magicBody)], false) {
217 for i := 0; i < len(magicBody); i++ {
218 if r.buf[i] != magicBody[i] {
226 if chunkType <= 0x7f {
227 // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f).
228 r.err = ErrUnsupported
231 // Section 4.4 Padding (chunk type 0xfe).
232 // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd).
233 if !r.readFull(r.buf[:chunkLen], false) {