zstd.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Package zstd provides decompression of zstandard files.
  2. //
  3. // For advanced usage and examples, go to the README: https://github.com/klauspost/compress/tree/master/zstd#zstd
  4. package zstd
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "log"
  10. "math"
  11. )
  12. // enable debug printing
  13. const debug = false
  14. // enable encoding debug printing
  15. const debugEncoder = debug
  16. // enable decoding debug printing
  17. const debugDecoder = debug
  18. // Enable extra assertions.
  19. const debugAsserts = debug || false
  20. // print sequence details
  21. const debugSequences = false
  22. // print detailed matching information
  23. const debugMatches = false
  24. // force encoder to use predefined tables.
  25. const forcePreDef = false
  26. // zstdMinMatch is the minimum zstd match length.
  27. const zstdMinMatch = 3
  28. // fcsUnknown is used for unknown frame content size.
  29. const fcsUnknown = math.MaxUint64
  30. var (
  31. // ErrReservedBlockType is returned when a reserved block type is found.
  32. // Typically this indicates wrong or corrupted input.
  33. ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
  34. // ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
  35. // Typically this indicates wrong or corrupted input.
  36. ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
  37. // ErrBlockTooSmall is returned when a block is too small to be decoded.
  38. // Typically returned on invalid input.
  39. ErrBlockTooSmall = errors.New("block too small")
  40. // ErrUnexpectedBlockSize is returned when a block has unexpected size.
  41. // Typically returned on invalid input.
  42. ErrUnexpectedBlockSize = errors.New("unexpected block size")
  43. // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
  44. // Typically this indicates wrong or corrupted input.
  45. ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
  46. // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
  47. // Typically this indicates wrong or corrupted input.
  48. ErrWindowSizeExceeded = errors.New("window size exceeded")
  49. // ErrWindowSizeTooSmall is returned when no window size is specified.
  50. // Typically this indicates wrong or corrupted input.
  51. ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
  52. // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
  53. ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
  54. // ErrUnknownDictionary is returned if the dictionary ID is unknown.
  55. ErrUnknownDictionary = errors.New("unknown dictionary")
  56. // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
  57. // This is only returned if SingleSegment is specified on the frame.
  58. ErrFrameSizeExceeded = errors.New("frame size exceeded")
  59. // ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
  60. // This is only returned if SingleSegment is specified on the frame.
  61. ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
  62. // ErrCRCMismatch is returned if CRC mismatches.
  63. ErrCRCMismatch = errors.New("CRC check failed")
  64. // ErrDecoderClosed will be returned if the Decoder was used after
  65. // Close has been called.
  66. ErrDecoderClosed = errors.New("decoder used after Close")
  67. // ErrDecoderNilInput is returned when a nil Reader was provided
  68. // and an operation other than Reset/DecodeAll/Close was attempted.
  69. ErrDecoderNilInput = errors.New("nil input provided as reader")
  70. )
  71. func println(a ...interface{}) {
  72. if debug || debugDecoder || debugEncoder {
  73. log.Println(a...)
  74. }
  75. }
  76. func printf(format string, a ...interface{}) {
  77. if debug || debugDecoder || debugEncoder {
  78. log.Printf(format, a...)
  79. }
  80. }
  81. func load3232(b []byte, i int32) uint32 {
  82. return binary.LittleEndian.Uint32(b[:len(b):len(b)][i:])
  83. }
  84. func load6432(b []byte, i int32) uint64 {
  85. return binary.LittleEndian.Uint64(b[:len(b):len(b)][i:])
  86. }
  87. type byter interface {
  88. Bytes() []byte
  89. Len() int
  90. }
  91. var _ byter = &bytes.Buffer{}