zstd.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "math/bits"
  12. )
  13. // enable debug printing
  14. const debug = false
  15. // enable encoding debug printing
  16. const debugEncoder = debug
  17. // enable decoding debug printing
  18. const debugDecoder = debug
  19. // Enable extra assertions.
  20. const debugAsserts = debug || false
  21. // print sequence details
  22. const debugSequences = false
  23. // print detailed matching information
  24. const debugMatches = false
  25. // force encoder to use predefined tables.
  26. const forcePreDef = false
  27. // zstdMinMatch is the minimum zstd match length.
  28. const zstdMinMatch = 3
  29. // fcsUnknown is used for unknown frame content size.
  30. const fcsUnknown = math.MaxUint64
  31. var (
  32. // ErrReservedBlockType is returned when a reserved block type is found.
  33. // Typically this indicates wrong or corrupted input.
  34. ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
  35. // ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
  36. // Typically this indicates wrong or corrupted input.
  37. ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
  38. // ErrBlockTooSmall is returned when a block is too small to be decoded.
  39. // Typically returned on invalid input.
  40. ErrBlockTooSmall = errors.New("block too small")
  41. // ErrUnexpectedBlockSize is returned when a block has unexpected size.
  42. // Typically returned on invalid input.
  43. ErrUnexpectedBlockSize = errors.New("unexpected block size")
  44. // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
  45. // Typically this indicates wrong or corrupted input.
  46. ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
  47. // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
  48. // Typically this indicates wrong or corrupted input.
  49. ErrWindowSizeExceeded = errors.New("window size exceeded")
  50. // ErrWindowSizeTooSmall is returned when no window size is specified.
  51. // Typically this indicates wrong or corrupted input.
  52. ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
  53. // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
  54. ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
  55. // ErrUnknownDictionary is returned if the dictionary ID is unknown.
  56. ErrUnknownDictionary = errors.New("unknown dictionary")
  57. // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
  58. // This is only returned if SingleSegment is specified on the frame.
  59. ErrFrameSizeExceeded = errors.New("frame size exceeded")
  60. // ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
  61. // This is only returned if SingleSegment is specified on the frame.
  62. ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
  63. // ErrCRCMismatch is returned if CRC mismatches.
  64. ErrCRCMismatch = errors.New("CRC check failed")
  65. // ErrDecoderClosed will be returned if the Decoder was used after
  66. // Close has been called.
  67. ErrDecoderClosed = errors.New("decoder used after Close")
  68. // ErrDecoderNilInput is returned when a nil Reader was provided
  69. // and an operation other than Reset/DecodeAll/Close was attempted.
  70. ErrDecoderNilInput = errors.New("nil input provided as reader")
  71. )
  72. func println(a ...interface{}) {
  73. if debug || debugDecoder || debugEncoder {
  74. log.Println(a...)
  75. }
  76. }
  77. func printf(format string, a ...interface{}) {
  78. if debug || debugDecoder || debugEncoder {
  79. log.Printf(format, a...)
  80. }
  81. }
  82. // matchLen returns the maximum common prefix length of a and b.
  83. // a must be the shortest of the two.
  84. func matchLen(a, b []byte) (n int) {
  85. for ; len(a) >= 8 && len(b) >= 8; a, b = a[8:], b[8:] {
  86. diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b)
  87. if diff != 0 {
  88. return n + bits.TrailingZeros64(diff)>>3
  89. }
  90. n += 8
  91. }
  92. for i := range a {
  93. if a[i] != b[i] {
  94. break
  95. }
  96. n++
  97. }
  98. return n
  99. }
  100. func load3232(b []byte, i int32) uint32 {
  101. return binary.LittleEndian.Uint32(b[:len(b):len(b)][i:])
  102. }
  103. func load6432(b []byte, i int32) uint64 {
  104. return binary.LittleEndian.Uint64(b[:len(b):len(b)][i:])
  105. }
  106. type byter interface {
  107. Bytes() []byte
  108. Len() int
  109. }
  110. var _ byter = &bytes.Buffer{}