lz4.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = 10 + minMatch // The last match cannot start within the last 14 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. // getBuffer returns a buffer to its pool.
  61. func getBuffer(size int) []byte {
  62. idx := blockSizeValueToIndex(size) - 4
  63. return bsMapValue[idx].Get().([]byte)
  64. }
  65. // putBuffer returns a buffer to its pool.
  66. func putBuffer(size int, buf []byte) {
  67. if cap(buf) > 0 {
  68. idx := blockSizeValueToIndex(size) - 4
  69. bsMapValue[idx].Put(buf[:cap(buf)])
  70. }
  71. }
  72. func blockSizeIndexToValue(i byte) int {
  73. return 1 << (16 + 2*uint(i))
  74. }
  75. func isValidBlockSize(size int) bool {
  76. const blockSizeMask = blockSize64K | blockSize256K | blockSize1M | blockSize4M
  77. return size&blockSizeMask > 0 && bits.OnesCount(uint(size)) == 1
  78. }
  79. func blockSizeValueToIndex(size int) byte {
  80. return 4 + byte(bits.TrailingZeros(uint(size)>>16)/2)
  81. }
  82. // Header describes the various flags that can be set on a Writer or obtained from a Reader.
  83. // The default values match those of the LZ4 frame format definition
  84. // (http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html).
  85. //
  86. // NB. in a Reader, in case of concatenated frames, the Header values may change between Read() calls.
  87. // It is the caller's responsibility to check them if necessary.
  88. type Header struct {
  89. BlockChecksum bool // Compressed blocks checksum flag.
  90. NoChecksum bool // Frame checksum flag.
  91. BlockMaxSize int // Size of the uncompressed data block (one of [64KB, 256KB, 1MB, 4MB]). Default=4MB.
  92. Size uint64 // Frame total size. It is _not_ computed by the Writer.
  93. CompressionLevel int // Compression level (higher is better, use 0 for fastest compression).
  94. done bool // Header processed flag (Read or Write and checked).
  95. }
  96. func (h *Header) Reset() {
  97. h.done = false
  98. }