Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

153 Zeilen
4.6 KiB

  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. // Reset the buffer offset when reaching this.
  30. const bufferReset = math.MaxInt32 - MaxWindowSize
  31. // fcsUnknown is used for unknown frame content size.
  32. const fcsUnknown = math.MaxUint64
  33. var (
  34. // ErrReservedBlockType is returned when a reserved block type is found.
  35. // Typically this indicates wrong or corrupted input.
  36. ErrReservedBlockType = errors.New("invalid input: reserved block type encountered")
  37. // ErrCompressedSizeTooBig is returned when a block is bigger than allowed.
  38. // Typically this indicates wrong or corrupted input.
  39. ErrCompressedSizeTooBig = errors.New("invalid input: compressed size too big")
  40. // ErrBlockTooSmall is returned when a block is too small to be decoded.
  41. // Typically returned on invalid input.
  42. ErrBlockTooSmall = errors.New("block too small")
  43. // ErrUnexpectedBlockSize is returned when a block has unexpected size.
  44. // Typically returned on invalid input.
  45. ErrUnexpectedBlockSize = errors.New("unexpected block size")
  46. // ErrMagicMismatch is returned when a "magic" number isn't what is expected.
  47. // Typically this indicates wrong or corrupted input.
  48. ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
  49. // ErrWindowSizeExceeded is returned when a reference exceeds the valid window size.
  50. // Typically this indicates wrong or corrupted input.
  51. ErrWindowSizeExceeded = errors.New("window size exceeded")
  52. // ErrWindowSizeTooSmall is returned when no window size is specified.
  53. // Typically this indicates wrong or corrupted input.
  54. ErrWindowSizeTooSmall = errors.New("invalid input: window size was too small")
  55. // ErrDecoderSizeExceeded is returned if decompressed size exceeds the configured limit.
  56. ErrDecoderSizeExceeded = errors.New("decompressed size exceeds configured limit")
  57. // ErrUnknownDictionary is returned if the dictionary ID is unknown.
  58. // For the time being dictionaries are not supported.
  59. ErrUnknownDictionary = errors.New("unknown dictionary")
  60. // ErrFrameSizeExceeded is returned if the stated frame size is exceeded.
  61. // This is only returned if SingleSegment is specified on the frame.
  62. ErrFrameSizeExceeded = errors.New("frame size exceeded")
  63. // ErrFrameSizeMismatch is returned if the stated frame size does not match the expected size.
  64. // This is only returned if SingleSegment is specified on the frame.
  65. ErrFrameSizeMismatch = errors.New("frame size does not match size on stream")
  66. // ErrCRCMismatch is returned if CRC mismatches.
  67. ErrCRCMismatch = errors.New("CRC check failed")
  68. // ErrDecoderClosed will be returned if the Decoder was used after
  69. // Close has been called.
  70. ErrDecoderClosed = errors.New("decoder used after Close")
  71. // ErrDecoderNilInput is returned when a nil Reader was provided
  72. // and an operation other than Reset/DecodeAll/Close was attempted.
  73. ErrDecoderNilInput = errors.New("nil input provided as reader")
  74. )
  75. func println(a ...interface{}) {
  76. if debug || debugDecoder || debugEncoder {
  77. log.Println(a...)
  78. }
  79. }
  80. func printf(format string, a ...interface{}) {
  81. if debug || debugDecoder || debugEncoder {
  82. log.Printf(format, a...)
  83. }
  84. }
  85. // matchLen returns the maximum length.
  86. // a must be the shortest of the two.
  87. // The function also returns whether all bytes matched.
  88. func matchLen(a, b []byte) int {
  89. b = b[:len(a)]
  90. for i := 0; i < len(a)-7; i += 8 {
  91. if diff := load64(a, i) ^ load64(b, i); diff != 0 {
  92. return i + (bits.TrailingZeros64(diff) >> 3)
  93. }
  94. }
  95. checked := (len(a) >> 3) << 3
  96. a = a[checked:]
  97. b = b[checked:]
  98. for i := range a {
  99. if a[i] != b[i] {
  100. return i + checked
  101. }
  102. }
  103. return len(a) + checked
  104. }
  105. func load3232(b []byte, i int32) uint32 {
  106. return binary.LittleEndian.Uint32(b[i:])
  107. }
  108. func load6432(b []byte, i int32) uint64 {
  109. return binary.LittleEndian.Uint64(b[i:])
  110. }
  111. func load64(b []byte, i int) uint64 {
  112. return binary.LittleEndian.Uint64(b[i:])
  113. }
  114. type byter interface {
  115. Bytes() []byte
  116. Len() int
  117. }
  118. var _ byter = &bytes.Buffer{}