lz4.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Package lz4 implements reading and writing lz4 compressed data (a frame),
  2. // as specified in http://fastcompression.blogspot.fr/2013/04/lz4-streaming-format-final.html.
  3. //
  4. // Although the block level compression and decompression functions are exposed and are fully compatible
  5. // with the lz4 block format definition, they are low level and should not be used directly.
  6. // For a complete description of an lz4 compressed block, see:
  7. // http://fastcompression.blogspot.fr/2011/05/lz4-explained.html
  8. //
  9. // See https://github.com/Cyan4973/lz4 for the reference C implementation.
  10. //
  11. package lz4
  12. import "math/bits"
  13. import "sync"
  14. const (
  15. // Extension is the LZ4 frame file name extension
  16. Extension = ".lz4"
  17. // Version is the LZ4 frame format version
  18. Version = 1
  19. frameMagic uint32 = 0x184D2204
  20. frameSkipMagic uint32 = 0x184D2A50
  21. // The following constants are used to setup the compression algorithm.
  22. minMatch = 4 // the minimum size of the match sequence size (4 bytes)
  23. winSizeLog = 16 // LZ4 64Kb window size limit
  24. winSize = 1 << winSizeLog
  25. winMask = winSize - 1 // 64Kb window of previous data for dependent blocks
  26. compressedBlockFlag = 1 << 31
  27. compressedBlockMask = compressedBlockFlag - 1
  28. // hashLog determines the size of the hash table used to quickly find a previous match position.
  29. // Its value influences the compression speed and memory usage, the lower the faster,
  30. // but at the expense of the compression ratio.
  31. // 16 seems to be the best compromise for fast compression.
  32. hashLog = 16
  33. htSize = 1 << hashLog
  34. mfLimit = 8 + minMatch // The last match cannot start within the last 12 bytes.
  35. )
  36. // map the block max size id with its value in bytes: 64Kb, 256Kb, 1Mb and 4Mb.
  37. const (
  38. blockSize64K = 1 << (16 + 2*iota)
  39. blockSize256K
  40. blockSize1M
  41. blockSize4M
  42. )
  43. var (
  44. // Keep a pool of buffers for each valid block sizes.
  45. bsMapValue = [...]*sync.Pool{
  46. newBufferPool(2 * blockSize64K),
  47. newBufferPool(2 * blockSize256K),
  48. newBufferPool(2 * blockSize1M),
  49. newBufferPool(2 * blockSize4M),
  50. }
  51. )
  52. // newBufferPool returns a pool for buffers of the given size.
  53. func newBufferPool(size int) *sync.Pool {
  54. return &sync.Pool{
  55. New: func() interface{} {
  56. return make([]byte, size)
  57. },
  58. }
  59. }
  60. // putBuffer returns a buffer to its pool.
  61. func putBuffer(size int, buf []byte) {
  62. if cap(buf) > 0 {
  63. idx := blockSizeValueToIndex(size) - 4
  64. bsMapValue[idx].Put(buf[:cap(buf)])
  65. }
  66. }
  67. func blockSizeIndexToValue(i byte) int {
  68. return 1 << (16 + 2*uint(i))
  69. }
  70. func isValidBlockSize(size int) bool {
  71. const blockSizeMask = blockSize64K | blockSize256K | blockSize1M | blockSize4M
  72. return size&blockSizeMask > 0 && bits.OnesCount(uint(size)) == 1
  73. }
  74. func blockSizeValueToIndex(size int) byte {
  75. return 4 + byte(bits.TrailingZeros(uint(size)>>16)/2)
  76. }
  77. // Header describes the various flags that can be set on a Writer or obtained from a Reader.
  78. // The default values match those of the LZ4 frame format definition
  79. // (http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html).
  80. //
  81. // NB. in a Reader, in case of concatenated frames, the Header values may change between Read() calls.
  82. // It is the caller responsibility to check them if necessary.
  83. type Header struct {
  84. BlockChecksum bool // Compressed blocks checksum flag.
  85. NoChecksum bool // Frame checksum flag.
  86. BlockMaxSize int // Size of the uncompressed data block (one of [64KB, 256KB, 1MB, 4MB]). Default=4MB.
  87. Size uint64 // Frame total size. It is _not_ computed by the Writer.
  88. CompressionLevel int // Compression level (higher is better, use 0 for fastest compression).
  89. done bool // Header processed flag (Read or Write and checked).
  90. }
  91. func (h *Header) Reset() {
  92. h.done = false
  93. }