lz4.go 3.9 KB

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