Read satisfies the io.Reader interface.
(p []byte)
| 242 | |
| 243 | // Read satisfies the io.Reader interface. |
| 244 | func (r *Reader) Read(p []byte) (int, error) { |
| 245 | if r.err != nil { |
| 246 | return 0, r.err |
| 247 | } |
| 248 | for { |
| 249 | if r.i < r.j { |
| 250 | n := copy(p, r.decoded[r.i:r.j]) |
| 251 | r.i += n |
| 252 | return n, nil |
| 253 | } |
| 254 | if !r.readFull(r.buf[:4], true) { |
| 255 | return 0, r.err |
| 256 | } |
| 257 | chunkType := r.buf[0] |
| 258 | if !r.readHeader { |
| 259 | if chunkType != chunkTypeStreamIdentifier { |
| 260 | r.err = ErrCorrupt |
| 261 | return 0, r.err |
| 262 | } |
| 263 | r.readHeader = true |
| 264 | } |
| 265 | chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16 |
| 266 | |
| 267 | // The chunk types are specified at |
| 268 | // https://github.com/google/snappy/blob/master/framing_format.txt |
| 269 | switch chunkType { |
| 270 | case chunkTypeCompressedData: |
| 271 | r.blockStart += int64(r.j) |
| 272 | // Section 4.2. Compressed data (chunk type 0x00). |
| 273 | if chunkLen < checksumSize { |
| 274 | r.err = ErrCorrupt |
| 275 | return 0, r.err |
| 276 | } |
| 277 | if !r.ensureBufferSize(chunkLen) { |
| 278 | if r.err == nil { |
| 279 | r.err = ErrUnsupported |
| 280 | } |
| 281 | return 0, r.err |
| 282 | } |
| 283 | buf := r.buf[:chunkLen] |
| 284 | if !r.readFull(buf, false) { |
| 285 | return 0, r.err |
| 286 | } |
| 287 | checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24 |
| 288 | buf = buf[checksumSize:] |
| 289 | |
| 290 | n, err := DecodedLen(buf) |
| 291 | if err != nil { |
| 292 | r.err = err |
| 293 | return 0, r.err |
| 294 | } |
| 295 | if r.snappyFrame && n > maxSnappyBlockSize { |
| 296 | r.err = ErrCorrupt |
| 297 | return 0, r.err |
| 298 | } |
| 299 | |
| 300 | if n > len(r.decoded) { |
| 301 | if n > r.maxBlock { |