lz4.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "fmt"
  14. "strings"
  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. // The following constants are used to setup the compression algorithm.
  24. minMatch = 4 // the minimum size of the match sequence size (4 bytes)
  25. winSizeLog = 16 // LZ4 64Kb window size limit
  26. winSize = 1 << winSizeLog
  27. winMask = winSize - 1 // 64Kb window of previous data for dependent blocks
  28. compressedBlockFlag = 1 << 31
  29. compressedBlockMask = compressedBlockFlag - 1
  30. // hashLog determines the size of the hash table used to quickly find a previous match position.
  31. // Its value influences the compression speed and memory usage, the lower the faster,
  32. // but at the expense of the compression ratio.
  33. // 16 seems to be the best compromise.
  34. hashLog = 16
  35. hashTableSize = 1 << hashLog
  36. hashShift = uint((minMatch * 8) - hashLog)
  37. mfLimit = 8 + minMatch // The last match cannot start within the last 12 bytes.
  38. )
  39. // map the block max size id with its value in bytes: 64Kb, 256Kb, 1Mb and 4Mb.
  40. var (
  41. bsMapID = map[byte]int{4: 64 << 10, 5: 256 << 10, 6: 1 << 20, 7: 4 << 20}
  42. bsMapValue = make(map[int]byte, len(bsMapID))
  43. )
  44. // Reversed.
  45. func init() {
  46. for i, v := range bsMapID {
  47. bsMapValue[v] = i
  48. }
  49. }
  50. // Header describes the various flags that can be set on a Writer or obtained from a Reader.
  51. // The default values match those of the LZ4 frame format definition
  52. // (http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html).
  53. //
  54. // NB. in a Reader, in case of concatenated frames, the Header values may change between Read() calls.
  55. // It is the caller responsibility to check them if necessary.
  56. type Header struct {
  57. BlockChecksum bool // Compressed blocks checksum flag.
  58. NoChecksum bool // Frame checksum flag.
  59. BlockMaxSize int // Size of the uncompressed data block (one of [64KB, 256KB, 1MB, 4MB]). Default=4MB.
  60. Size uint64 // Frame total size. It is _not_ computed by the Writer.
  61. CompressionLevel int // Compression level (higher is better, use 0 for fastest compression).
  62. done bool // Header processed flag (Read or Write and checked).
  63. }
  64. func (h Header) String() string {
  65. var s strings.Builder
  66. s.WriteString(fmt.Sprintf("%T{", h))
  67. if h.BlockChecksum {
  68. s.WriteString("BlockChecksum: true ")
  69. }
  70. if h.NoChecksum {
  71. s.WriteString("NoChecksum: true ")
  72. }
  73. if bs := h.BlockMaxSize; bs != 0 && bs != 4<<20 {
  74. s.WriteString(fmt.Sprintf("BlockMaxSize: %d ", bs))
  75. }
  76. if l := h.CompressionLevel; l != 0 {
  77. s.WriteString(fmt.Sprintf("CompressionLevel: %d ", l))
  78. }
  79. s.WriteByte('}')
  80. return s.String()
  81. }