Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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 | |
| 13 | const ( |
| 14 | // Extension is the LZ4 frame file name extension |
| 15 | Extension = ".lz4" |
| 16 | // Version is the LZ4 frame format version |
| 17 | Version = 1 |
| 18 | |
| 19 | frameMagic uint32 = 0x184D2204 |
| 20 | frameSkipMagic uint32 = 0x184D2A50 |
| 21 | |
| 22 | // The following constants are used to setup the compression algorithm. |
| 23 | minMatch = 4 // the minimum size of the match sequence size (4 bytes) |
| 24 | winSizeLog = 16 // LZ4 64Kb window size limit |
| 25 | winSize = 1 << winSizeLog |
| 26 | winMask = winSize - 1 // 64Kb window of previous data for dependent blocks |
| 27 | compressedBlockFlag = 1 << 31 |
| 28 | compressedBlockMask = compressedBlockFlag - 1 |
| 29 | |
| 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 for fast compression. |
| 34 | hashLog = 16 |
| 35 | htSize = 1 << hashLog |
| 36 | |
| 37 | mfLimit = 8 + minMatch // The last match cannot start within the last 12 bytes. |
| 38 | ) |
| 39 | |
| 40 | // map the block max size id with its value in bytes: 64Kb, 256Kb, 1Mb and 4Mb. |
| 41 | const ( |
| 42 | blockSize64K = 64 << 10 |
| 43 | blockSize256K = 256 << 10 |
| 44 | blockSize1M = 1 << 20 |
| 45 | blockSize4M = 4 << 20 |
| 46 | ) |
| 47 | |
| 48 | var ( |
| 49 | bsMapID = map[byte]int{4: blockSize64K, 5: blockSize256K, 6: blockSize1M, 7: blockSize4M} |
| 50 | bsMapValue = map[int]byte{blockSize64K: 4, blockSize256K: 5, blockSize1M: 6, blockSize4M: 7} |
| 51 | ) |
| 52 | |
| 53 | // Header describes the various flags that can be set on a Writer or obtained from a Reader. |
| 54 | // The default values match those of the LZ4 frame format definition |
| 55 | // (http://fastcompression.blogspot.com/2013/04/lz4-streaming-format-final.html). |
| 56 | // |
| 57 | // NB. in a Reader, in case of concatenated frames, the Header values may change between Read() calls. |
| 58 | // It is the caller responsibility to check them if necessary. |
| 59 | type Header struct { |
| 60 | BlockChecksum bool // Compressed blocks checksum flag. |
| 61 | NoChecksum bool // Frame checksum flag. |
| 62 | BlockMaxSize int // Size of the uncompressed data block (one of [64KB, 256KB, 1MB, 4MB]). Default=4MB. |
| 63 | Size uint64 // Frame total size. It is _not_ computed by the Writer. |
| 64 | CompressionLevel int // Compression level (higher is better, use 0 for fastest compression). |
| 65 | done bool // Header processed flag (Read or Write and checked). |
| 66 | } |