lib/lzo: implement run-length encoding
[linux-2.6-block.git] / Documentation / lzo.txt
CommitLineData
7b001bff 1===========================================================
d98a0526
WT
2LZO stream format as understood by Linux's LZO decompressor
3===========================================================
4
5Introduction
7b001bff 6============
d98a0526
WT
7
8 This is not a specification. No specification seems to be publicly available
9 for the LZO stream format. This document describes what input format the LZO
10 decompressor as implemented in the Linux kernel understands. The file subject
11 of this analysis is lib/lzo/lzo1x_decompress_safe.c. No analysis was made on
12 the compressor nor on any other implementations though it seems likely that
13 the format matches the standard one. The purpose of this document is to
14 better understand what the code does in order to propose more efficient fixes
15 for future bug reports.
16
17Description
7b001bff 18===========
d98a0526
WT
19
20 The stream is composed of a series of instructions, operands, and data. The
21 instructions consist in a few bits representing an opcode, and bits forming
22 the operands for the instruction, whose size and position depend on the
23 opcode and on the number of literals copied by previous instruction. The
7b001bff 24 operands are used to indicate:
d98a0526
WT
25
26 - a distance when copying data from the dictionary (past output buffer)
27 - a length (number of bytes to copy from dictionary)
28 - the number of literals to copy, which is retained in variable "state"
29 as a piece of information for next instructions.
30
31 Optionally depending on the opcode and operands, extra data may follow. These
32 extra data can be a complement for the operand (eg: a length or a distance
33 encoded on larger values), or a literal to be copied to the output buffer.
34
35 The first byte of the block follows a different encoding from other bytes, it
36 seems to be optimized for literal use only, since there is no dictionary yet
37 prior to that byte.
38
39 Lengths are always encoded on a variable size starting with a small number
40 of bits in the operand. If the number of bits isn't enough to represent the
41 length, up to 255 may be added in increments by consuming more bytes with a
42 rate of at most 255 per extra byte (thus the compression ratio cannot exceed
7b001bff 43 around 255:1). The variable length encoding using #bits is always the same::
d98a0526
WT
44
45 length = byte & ((1 << #bits) - 1)
46 if (!length) {
47 length = ((1 << #bits) - 1)
48 length += 255*(number of zero bytes)
49 length += first-non-zero-byte
50 }
51 length += constant (generally 2 or 3)
52
53 For references to the dictionary, distances are relative to the output
54 pointer. Distances are encoded using very few bits belonging to certain
55 ranges, resulting in multiple copy instructions using different encodings.
56 Certain encodings involve one extra byte, others involve two extra bytes
57 forming a little-endian 16-bit quantity (marked LE16 below).
58
59 After any instruction except the large literal copy, 0, 1, 2 or 3 literals
60 are copied before starting the next instruction. The number of literals that
61 were copied may change the meaning and behaviour of the next instruction. In
62 practice, only one instruction needs to know whether 0, less than 4, or more
63 literals were copied. This is the information stored in the <state> variable
64 in this implementation. This number of immediate literals to be copied is
65 generally encoded in the last two bits of the instruction but may also be
66 taken from the last two bits of an extra operand (eg: distance).
67
68 End of stream is declared when a block copy of distance 0 is seen. Only one
69 instruction may encode this distance (0001HLLL), it takes one LE16 operand
70 for the distance, thus requiring 3 bytes.
71
7b001bff
MCC
72 .. important::
73
74 In the code some length checks are missing because certain instructions
75 are called under the assumption that a certain number of bytes follow
76 because it has already been guaranteed before parsing the instructions.
77 They just have to "refill" this credit if they consume extra bytes. This
78 is an implementation design choice independent on the algorithm or
79 encoding.
d98a0526 80
5ee4014a
DR
81Versions
82
830: Original version
841: LZO-RLE
85
86Version 1 of LZO implements an extension to encode runs of zeros using run
87length encoding. This improves speed for data with many zeros, which is a
88common case for zram. This modifies the bitstream in a backwards compatible way
89(v1 can correctly decompress v0 compressed data, but v0 cannot read v1 data).
90
d98a0526 91Byte sequences
7b001bff 92==============
d98a0526 93
7b001bff 94 First byte encoding::
d98a0526 95
5ee4014a
DR
96 0..16 : follow regular instruction encoding, see below. It is worth
97 noting that code 16 will represent a block copy from the
98 dictionary which is empty, and that it will always be
d98a0526
WT
99 invalid at this place.
100
5ee4014a
DR
101 17 : bitstream version. If the first byte is 17, the next byte
102 gives the bitstream version. If the first byte is not 17,
103 the bitstream version is 0.
104
d98a0526
WT
105 18..21 : copy 0..3 literals
106 state = (byte - 17) = 0..3 [ copy <state> literals ]
107 skip byte
108
109 22..255 : copy literal string
110 length = (byte - 17) = 4..238
111 state = 4 [ don't copy extra literals ]
112 skip byte
113
7b001bff 114 Instruction encoding::
d98a0526
WT
115
116 0 0 0 0 X X X X (0..15)
117 Depends on the number of literals copied by the last instruction.
118 If last instruction did not copy any literal (state == 0), this
119 encoding will be a copy of 4 or more literal, and must be interpreted
120 like this :
121
122 0 0 0 0 L L L L (0..15) : copy long literal string
123 length = 3 + (L ?: 15 + (zero_bytes * 255) + non_zero_byte)
124 state = 4 (no extra literals are copied)
125
126 If last instruction used to copy between 1 to 3 literals (encoded in
127 the instruction's opcode or distance), the instruction is a copy of a
128 2-byte block from the dictionary within a 1kB distance. It is worth
129 noting that this instruction provides little savings since it uses 2
130 bytes to encode a copy of 2 other bytes but it encodes the number of
131 following literals for free. It must be interpreted like this :
132
133 0 0 0 0 D D S S (0..15) : copy 2 bytes from <= 1kB distance
134 length = 2
135 state = S (copy S literals after this block)
136 Always followed by exactly one byte : H H H H H H H H
137 distance = (H << 2) + D + 1
138
139 If last instruction used to copy 4 or more literals (as detected by
140 state == 4), the instruction becomes a copy of a 3-byte block from the
141 dictionary from a 2..3kB distance, and must be interpreted like this :
142
143 0 0 0 0 D D S S (0..15) : copy 3 bytes from 2..3 kB distance
144 length = 3
145 state = S (copy S literals after this block)
146 Always followed by exactly one byte : H H H H H H H H
147 distance = (H << 2) + D + 2049
148
149 0 0 0 1 H L L L (16..31)
150 Copy of a block within 16..48kB distance (preferably less than 10B)
151 length = 2 + (L ?: 7 + (zero_bytes * 255) + non_zero_byte)
152 Always followed by exactly one LE16 : D D D D D D D D : D D D D D D S S
153 distance = 16384 + (H << 14) + D
154 state = S (copy S literals after this block)
155 End of stream is reached if distance == 16384
156
5ee4014a
DR
157 In version 1, this instruction is also used to encode a run of zeros if
158 distance = 0xbfff, i.e. H = 1 and the D bits are all 1.
159 In this case, it is followed by a fourth byte, X.
160 run length = ((X << 3) | (0 0 0 0 0 L L L)) + 4.
161
d98a0526
WT
162 0 0 1 L L L L L (32..63)
163 Copy of small block within 16kB distance (preferably less than 34B)
164 length = 2 + (L ?: 31 + (zero_bytes * 255) + non_zero_byte)
165 Always followed by exactly one LE16 : D D D D D D D D : D D D D D D S S
166 distance = D + 1
167 state = S (copy S literals after this block)
168
169 0 1 L D D D S S (64..127)
170 Copy 3-4 bytes from block within 2kB distance
171 state = S (copy S literals after this block)
172 length = 3 + L
173 Always followed by exactly one byte : H H H H H H H H
174 distance = (H << 3) + D + 1
175
176 1 L L D D D S S (128..255)
177 Copy 5-8 bytes from block within 2kB distance
178 state = S (copy S literals after this block)
179 length = 5 + L
180 Always followed by exactly one byte : H H H H H H H H
181 distance = (H << 3) + D + 1
182
183Authors
7b001bff 184=======
d98a0526
WT
185
186 This document was written by Willy Tarreau <w@1wt.eu> on 2014/07/19 during an
5ee4014a
DR
187 analysis of the decompression code available in Linux 3.16-rc5, and updated
188 by Dave Rodgman <dave.rodgman@arm.com> on 2018/10/30 to introduce run-length
189 encoding. The code is tricky, it is possible that this document contains
190 mistakes or that a few corner cases were overlooked. In any case, please
191 report any doubt, fix, or proposed updates to the author(s) so that the
192 document can be updated.