decode.go raw

   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  
   5  package snappy
   6  
   7  import (
   8  	"io"
   9  
  10  	"github.com/klauspost/compress/s2"
  11  )
  12  
  13  var (
  14  	// ErrCorrupt reports that the input is invalid.
  15  	ErrCorrupt = s2.ErrCorrupt
  16  	// ErrTooLarge reports that the uncompressed length is too large.
  17  	ErrTooLarge = s2.ErrTooLarge
  18  	// ErrUnsupported reports that the input isn't supported.
  19  	ErrUnsupported = s2.ErrUnsupported
  20  )
  21  
  22  const (
  23  	// maxBlockSize is the maximum size of the input to encodeBlock. It is not
  24  	// part of the wire format per se, but some parts of the encoder assume
  25  	// that an offset fits into a uint16.
  26  	//
  27  	// Also, for the framing format (Writer type instead of Encode function),
  28  	// https://github.com/google/snappy/blob/master/framing_format.txt says
  29  	// that "the uncompressed data in a chunk must be no longer than 65536
  30  	// bytes".
  31  	maxBlockSize = 65536
  32  )
  33  
  34  // DecodedLen returns the length of the decoded block.
  35  func DecodedLen(src []byte) (int, error) {
  36  	return s2.DecodedLen(src)
  37  }
  38  
  39  // Decode returns the decoded form of src. The returned slice may be a sub-
  40  // slice of dst if dst was large enough to hold the entire decoded block.
  41  // Otherwise, a newly allocated slice will be returned.
  42  //
  43  // The dst and src must not overlap. It is valid to pass a nil dst.
  44  //
  45  // Decode handles the Snappy block format, not the Snappy stream format.
  46  func Decode(dst, src []byte) ([]byte, error) {
  47  	return s2.Decode(dst, src)
  48  }
  49  
  50  // NewReader returns a new Reader that decompresses from r, using the framing
  51  // format described at
  52  // https://github.com/google/snappy/blob/master/framing_format.txt
  53  func NewReader(r io.Reader) *Reader {
  54  	return s2.NewReader(r, s2.ReaderMaxBlockSize(maxBlockSize))
  55  }
  56  
  57  // Reader is an io.Reader that can read Snappy-compressed bytes.
  58  //
  59  // Reader handles the Snappy stream format, not the Snappy block format.
  60  type Reader = s2.Reader
  61