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. 4 5package snappy 6 7import ( 8 "encoding/binary" 9 "errors" 10 "io" 11) 12 13var ( 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") 20 21 errUnsupportedLiteralLength = errors.New("snappy: unsupported literal length") 22) 23 24// DecodedLen returns the length of the decoded block. 25func DecodedLen(src []byte) (int, error) { 26 v, _, err := decodedLen(src) 27 return v, err 28} 29 30// decodedLen returns the length of the decoded block and the number of bytes 31// that the length header occupied. 32func 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 36 } 37 38 const wordSize = 32 << (^uint(0) >> 32 & 1) 39 if wordSize == 32 && v > 0x7fffffff { 40 return 0, 0, ErrTooLarge 41 } 42 return int(v), n, nil 43} 44 45const ( 46 decodeErrCodeCorrupt = 1 47 decodeErrCodeUnsupportedLiteralLength = 2 48) 49 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. 53// 54// The dst and src must not overlap. It is valid to pass a nil dst. 55// 56// Decode handles the Snappy block format, not the Snappy stream format. 57func Decode(dst, src []byte) ([]byte, error) { 58 dLen, s, err := decodedLen(src) 59 if err != nil { 60 return nil, err 61 } 62 if dLen <= len(dst) { 63 dst = dst[:dLen] 64 } else { 65 dst = make([]byte, dLen) 66 } 67 switch decode(dst, src[s:]) { 68 case 0: 69 return dst, nil 70 case decodeErrCodeUnsupportedLiteralLength: 71 return nil, errUnsupportedLiteralLength 72 } 73 return nil, ErrCorrupt 74} 75 76// NewReader returns a new Reader that decompresses from r, using the framing 77// format described at 78// https://github.com/google/snappy/blob/master/framing_format.txt 79func NewReader(r io.Reader) *Reader { 80 return &Reader{ 81 r: r, 82 decoded: make([]byte, maxBlockSize), 83 buf: make([]byte, maxEncodedLenOfMaxBlockSize+checksumSize), 84 } 85} 86 87// Reader is an io.Reader that can read Snappy-compressed bytes. 88// 89// Reader handles the Snappy stream format, not the Snappy block format. 90type Reader struct { 91 r io.Reader 92 err error 93 decoded []byte 94 buf []byte 95 // decoded[i:j] contains decoded bytes that have not yet been passed on. 96 i, j int 97 readHeader bool 98} 99 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 102// a new one. 103func (r *Reader) Reset(reader io.Reader) { 104 r.r = reader 105 r.err = nil 106 r.i = 0 107 r.j = 0 108 r.readHeader = false 109} 110 111func (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) { 114 r.err = ErrCorrupt 115 } 116 return false 117 } 118 return true 119} 120 121// Read satisfies the io.Reader interface. 122func (r *Reader) Read(p []byte) (int, error) { 123 if r.err != nil { 124 return 0, r.err 125 } 126 for { 127 if r.i < r.j { 128 n := copy(p, r.decoded[r.i:r.j]) 129 r.i += n 130 return n, nil 131 } 132 if !r.readFull(r.buf[:4], true) { 133 return 0, r.err 134 } 135 chunkType := r.buf[0] 136 if !r.readHeader { 137 if chunkType != chunkTypeStreamIdentifier { 138 r.err = ErrCorrupt 139 return 0, r.err 140 } 141 r.readHeader = true 142 } 143 chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16 144 if chunkLen > len(r.buf) { 145 r.err = ErrUnsupported 146 return 0, r.err 147 } 148 149 // The chunk types are specified at 150 // https://github.com/google/snappy/blob/master/framing_format.txt 151 switch chunkType { 152 case chunkTypeCompressedData: 153 // Section 4.2. Compressed data (chunk type 0x00). 154 if chunkLen < checksumSize { 155 r.err = ErrCorrupt 156 return 0, r.err 157 } 158 buf := r.buf[:chunkLen] 159 if !r.readFull(buf, false) { 160 return 0, r.err 161 } 162 checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24 163 buf = buf[checksumSize:] 164 165 n, err := DecodedLen(buf) 166 if err != nil { 167 r.err = err 168 return 0, r.err 169 } 170 if n > len(r.decoded) { 171 r.err = ErrCorrupt 172 return 0, r.err 173 } 174 if _, err := Decode(r.decoded, buf); err != nil { 175 r.err = err 176 return 0, r.err 177 } 178 if crc(r.decoded[:n]) != checksum { 179 r.err = ErrCorrupt 180 return 0, r.err 181 } 182 r.i, r.j = 0, n 183 continue 184 185 case chunkTypeUncompressedData: 186 // Section 4.3. Uncompressed data (chunk type 0x01). 187 if chunkLen < checksumSize { 188 r.err = ErrCorrupt 189 return 0, r.err 190 } 191 buf := r.buf[:checksumSize] 192 if !r.readFull(buf, false) { 193 return 0, r.err 194 } 195 checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24 196 // Read directly into r.decoded instead of via r.buf. 197 n := chunkLen - checksumSize 198 if n > len(r.decoded) { 199 r.err = ErrCorrupt 200 return 0, r.err 201 } 202 if !r.readFull(r.decoded[:n], false) { 203 return 0, r.err 204 } 205 if crc(r.decoded[:n]) != checksum { 206 r.err = ErrCorrupt 207 return 0, r.err 208 } 209 r.i, r.j = 0, n 210 continue 211 212 case chunkTypeStreamIdentifier: 213 // Section 4.1. Stream identifier (chunk type 0xff). 214 if chunkLen != len(magicBody) { 215 r.err = ErrCorrupt 216 return 0, r.err 217 } 218 if !r.readFull(r.buf[:len(magicBody)], false) { 219 return 0, r.err 220 } 221 for i := 0; i < len(magicBody); i++ { 222 if r.buf[i] != magicBody[i] { 223 r.err = ErrCorrupt 224 return 0, r.err 225 } 226 } 227 continue 228 } 229 230 if chunkType <= 0x7f { 231 // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f). 232 r.err = ErrUnsupported 233 return 0, r.err 234 } 235 // Section 4.4 Padding (chunk type 0xfe). 236 // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd). 237 if !r.readFull(r.buf[:chunkLen], false) { 238 return 0, r.err 239 } 240 } 241} 242