lz4.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. const (
  13. // Extension is the LZ4 frame file name extension
  14. Extension = ".lz4"
  15. // Version is the LZ4 frame format version
  16. Version = 1
  17. frameMagic uint32 = 0x184D2204
  18. frameSkipMagic uint32 = 0x184D2A50
  19. // The following constants are used to setup the compression algorithm.
  20. minMatch = 4 // the minimum size of the match sequence size (4 bytes)
  21. winSizeLog = 16 // LZ4 64Kb window size limit
  22. winSize = 1 << winSizeLog
  23. winMask = winSize - 1 // 64Kb window of previous data for dependent blocks
  24. compressedBlockFlag = 1 << 31
  25. compressedBlockMask = compressedBlockFlag - 1
  26. // hashLog determines the size of the hash table used to quickly find a previous match position.
  27. // Its value influences the compression speed and memory usage, the lower the faster,
  28. // but at the expense of the compression ratio.
  29. // 16 seems to be the best compromise.
  30. hashLog = 16
  31. hashTableSize = 1 << hashLog
  32. hashShift = uint((minMatch * 8) - hashLog)
  33. mfLimit = 8 + minMatch // The last match cannot start within the last 12 bytes.
  34. skipStrength = 6 // variable step for fast scan
  35. )
  36. // map the block max size id with its value in bytes: 64Kb, 256Kb, 1Mb and 4Mb.
  37. var (
  38. bsMapID = map[byte]int{4: 64 << 10, 5: 256 << 10, 6: 1 << 20, 7: 4 << 20}
  39. bsMapValue = make(map[int]byte, len(bsMapID))
  40. )
  41. // Reversed.
  42. func init() {
  43. for i, v := range bsMapID {
  44. bsMapValue[v] = i
  45. }
  46. }
  47. // Header describes the various flags that can be set on a Writer or obtained from a Reader.
  48. // The default values match those of the LZ4 frame format definition
  49. // (http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html).
  50. //
  51. // NB. in a Reader, in case of concatenated frames, the Header values may change between Read() calls.
  52. // It is the caller responsibility to check them if necessary.
  53. type Header struct {
  54. BlockChecksum bool // Compressed blocks checksum flag.
  55. NoChecksum bool // Frame checksum flag.
  56. BlockMaxSize int // Size of the uncompressed data block (one of [64KB, 256KB, 1MB, 4MB]). Default=4MB.
  57. Size uint64 // Frame total size. It is _not_ computed by the Writer.
  58. CompressionLevel int // Compression level (higher is better, use 0 for fastest compression).
  59. done bool // Header processed flag (Read or Write and checked).
  60. }