zstd.go 4.5 KB

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