decode.go 1.9 KB

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