zstd.go raw

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