zstd.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "errors"
  7. "log"
  8. "math/bits"
  9. )
  10. const debug = false
  11. const debugSequences = false
  12. const debugMatches = false
  13. // force encoder to use predefined tables.
  14. const forcePreDef = false
  15. // zstdMinMatch is the minimum zstd match length.
  16. const zstdMinMatch = 3
  17. var (
  18. // ErrReservedBlockType is returned when a reserved block type is found.
  19. // Typically this indicates wrong or corrupted input.
  20. ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
  21. // ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
  22. // Typically this indicates wrong or corrupted input.
  23. ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
  24. // ErrBlockTooSmall is returned when a block is too small to be decoded.
  25. // Typically returned on invalid input.
  26. ErrBlockTooSmall = errors.New("block too small")
  27. // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
  28. // Typically this indicates wrong or corrupted input.
  29. ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
  30. // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
  31. // Typically this indicates wrong or corrupted input.
  32. ErrWindowSizeExceeded = errors.New("window size exceeded")
  33. // ErrWindowSizeTooSmall is returned when no window size is specified.
  34. // Typically this indicates wrong or corrupted input.
  35. ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
  36. // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
  37. ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
  38. // ErrUnknownDictionary is returned if the dictionary ID is unknown.
  39. // For the time being dictionaries are not supported.
  40. ErrUnknownDictionary = errors.New("unknown dictionary")
  41. // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
  42. // This is only returned if SingleSegment is specified on the frame.
  43. ErrFrameSizeExceeded = errors.New("frame size exceeded")
  44. // ErrCRCMismatch is returned if CRC mismatches.
  45. ErrCRCMismatch = errors.New("CRC check failed")
  46. // ErrDecoderClosed will be returned if the Decoder was used after
  47. // Close has been called.
  48. ErrDecoderClosed = errors.New("decoder used after Close")
  49. )
  50. func println(a ...interface{}) {
  51. if debug {
  52. log.Println(a...)
  53. }
  54. }
  55. func printf(format string, a ...interface{}) {
  56. if debug {
  57. log.Printf(format, a...)
  58. }
  59. }
  60. // matchLen returns the maximum length.
  61. // a must be the shortest of the two.
  62. // The function also returns whether all bytes matched.
  63. func matchLen(a, b []byte) int {
  64. b = b[:len(a)]
  65. for i := 0; i < len(a)-7; i += 8 {
  66. if diff := load64(a, i) ^ load64(b, i); diff != 0 {
  67. return i + (bits.TrailingZeros64(diff) >> 3)
  68. }
  69. }
  70. checked := (len(a) >> 3) << 3
  71. a = a[checked:]
  72. b = b[checked:]
  73. // TODO: We could do a 4 check.
  74. for i := range a {
  75. if a[i] != b[i] {
  76. return int(i) + checked
  77. }
  78. }
  79. return len(a) + checked
  80. }
  81. // matchLen returns a match length in src between index s and t
  82. func matchLenIn(src []byte, s, t int32) int32 {
  83. s1 := len(src)
  84. b := src[t:]
  85. a := src[s:s1]
  86. b = b[:len(a)]
  87. // Extend the match to be as long as possible.
  88. for i := range a {
  89. if a[i] != b[i] {
  90. return int32(i)
  91. }
  92. }
  93. return int32(len(a))
  94. }
  95. func load3232(b []byte, i int32) uint32 {
  96. // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
  97. b = b[i:]
  98. b = b[:4]
  99. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  100. }
  101. func load6432(b []byte, i int32) uint64 {
  102. // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
  103. b = b[i:]
  104. b = b[:8]
  105. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  106. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  107. }
  108. func load64(b []byte, i int) uint64 {
  109. // Help the compiler eliminate bounds checks on the read so it can be done in a single read.
  110. b = b[i:]
  111. b = b[:8]
  112. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  113. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  114. }