]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/native/lz4_format_description.txt
Sync git svn branch with SVN repository r33359.
[simantics/platform.git] / bundles / org.simantics.fastlz / native / lz4_format_description.txt
1 LZ4 Format Description\r
2 Last revised: 2012-02-27\r
3 Author : Y. Collet\r
4 \r
5 \r
6 \r
7 This small specification intents to provide enough information\r
8 to anyone willing to produce LZ4-compatible compressed streams\r
9 using any programming language.\r
10 \r
11 LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding.\r
12 The most important design principle behind LZ4 is simplicity.\r
13 It helps to create an easy to read and maintain source code.\r
14 It also helps later on for optimisations, compactness, and speed.\r
15 There is no entropy encoder backend nor framing layer.\r
16 The latter is assumed to be handled by other parts of the system.\r
17 \r
18 This document only describes the format,\r
19 not how the LZ4 compressor nor decompressor actually work.\r
20 The correctness of the decompressor should not depend\r
21 on implementation details of the compressor, and vice versa.\r
22 \r
23 \r
24 \r
25 -- Compressed stream format --\r
26 \r
27 An LZ4 compressed stream is composed of sequences.\r
28 Schematically, a sequence is a suite of literals, followed by a match copy.\r
29 \r
30 Each sequence starts with a token.\r
31 The token is a one byte value, separated into two 4-bits fields.\r
32 Therefore each field ranges from 0 to 15.\r
33 \r
34 \r
35 The first field uses the 4 high-bits of the token.\r
36 It provides the length of literals to follow.\r
37 (Note : a literal is a not-compressed byte).\r
38 If the field value is 0, then there is no literal.\r
39 If it is 15, then we need to add some more bytes to indicate the full length.\r
40 Each additionnal byte then represent a value from 0 to 255,\r
41 which is added to the previous value to produce a total length.\r
42 When the byte value is 255, another byte is output.\r
43 There can be any number of bytes following the token. There is no "size limit".\r
44 (Sidenote this is why a not-compressible input stream is expanded by 0.4%).\r
45 \r
46 Example 1 : A length of 48 will be represented as :\r
47 - 15 : value for the 4-bits High field\r
48 - 33 : (=48-15) remaining length to reach 48\r
49 \r
50 Example 2 : A length of 280 will be represented as :\r
51 - 15  : value for the 4-bits High field\r
52 - 255 : following byte is maxed, since 280-15 >= 255\r
53 - 10  : (=280 - 15 - 255) ) remaining length to reach 280\r
54 \r
55 Example 3 : A length of 15 will be represented as :\r
56 - 15 : value for the 4-bits High field\r
57 - 0  : (=15-15) yes, the zero must be output\r
58 \r
59 Following the token and optional length bytes, are the literals themselves.\r
60 They are exactly as numerous as previously decoded (length of literals).\r
61 It's possible that there are zero literal.\r
62 \r
63 \r
64 Following the literals is the match copy operation.\r
65 \r
66 It starts by the offset.\r
67 This is a 2 bytes value, in little endian format :\r
68 the lower byte is the first one in the stream.\r
69 \r
70 The offset represents the position of the match to be copied from.\r
71 1 means "current position - 1 byte".\r
72 The maximum offset value is 65535, 65536 cannot be coded.\r
73 Note that 0 is an invalid value, not used. \r
74 \r
75 Then we need to extract the match length.\r
76 For this, we use the second token field, the low 4-bits.\r
77 Value, obviously, ranges from 0 to 15.\r
78 However here, 0 means that the copy operation will be minimal.\r
79 The minimum length of a match, called minmatch, is 4. \r
80 As a consequence, a 0 value means 4 bytes, and a value of 15 means 19+ bytes.\r
81 Similar to literal length, on reaching the highest possible value (15), \r
82 we output additional bytes, one at a time, with values ranging from 0 to 255.\r
83 They are added to total to provide the final match length.\r
84 A 255 value means there is another byte to read and add.\r
85 There is no limit to the number of optional bytes that can be output this way.\r
86 (This points towards a maximum achievable compression ratio of ~250).\r
87 \r
88 With the offset and the matchlength,\r
89 the decoder can now proceed to copy the data from the already decoded buffer.\r
90 On decoding the matchlength, we reach the end of the compressed sequence,\r
91 and therefore start another one.\r
92 \r
93 \r
94 -- Parsing restrictions --\r
95 \r
96 There are specific parsing rules to respect in order to remain compatible\r
97 with assumptions made by the decoder :\r
98 1) The last 5 bytes are always literals\r
99 2) The last match must start at least 12 bytes before end of stream\r
100 Consequently, a file with less than 13 bytes cannot be compressed.\r
101 These rules are in place to ensure that the decoder\r
102 will never read beyond the input buffer, nor write beyond the output buffer.\r
103 \r
104 Note that the last sequence is also incomplete,\r
105 and stops right after literals.\r
106 \r
107 \r
108 -- Additional notes --\r
109 \r
110 There is no assumption nor limits to the way the compressor\r
111 searches and selects matches within the source stream.\r
112 It could be a fast scan, a multi-probe, a full search using BST,\r
113 standard hash chains or MMC, well whatever.\r
114 \r
115 Advanced parsing strategies can also be implemented, such as lazy match,\r
116 or full optimal parsing.\r
117 \r
118 All these trade-off offer distinctive speed/memory/compression advantages.\r
119 Whatever the method used by the compressor, its result will be decodable\r
120 by any LZ4 decoder if it follows the format specification described above.\r
121 \r