lzo.txt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. LZO stream format as understood by Linux's LZO decompressor
  2. ===========================================================
  3. Introduction
  4. This is not a specification. No specification seems to be publicly available
  5. for the LZO stream format. This document describes what input format the LZO
  6. decompressor as implemented in the Linux kernel understands. The file subject
  7. of this analysis is lib/lzo/lzo1x_decompress_safe.c. No analysis was made on
  8. the compressor nor on any other implementations though it seems likely that
  9. the format matches the standard one. The purpose of this document is to
  10. better understand what the code does in order to propose more efficient fixes
  11. for future bug reports.
  12. Description
  13. The stream is composed of a series of instructions, operands, and data. The
  14. instructions consist in a few bits representing an opcode, and bits forming
  15. the operands for the instruction, whose size and position depend on the
  16. opcode and on the number of literals copied by previous instruction. The
  17. operands are used to indicate :
  18. - a distance when copying data from the dictionary (past output buffer)
  19. - a length (number of bytes to copy from dictionary)
  20. - the number of literals to copy, which is retained in variable "state"
  21. as a piece of information for next instructions.
  22. Optionally depending on the opcode and operands, extra data may follow. These
  23. extra data can be a complement for the operand (eg: a length or a distance
  24. encoded on larger values), or a literal to be copied to the output buffer.
  25. The first byte of the block follows a different encoding from other bytes, it
  26. seems to be optimized for literal use only, since there is no dictionary yet
  27. prior to that byte.
  28. Lengths are always encoded on a variable size starting with a small number
  29. of bits in the operand. If the number of bits isn't enough to represent the
  30. length, up to 255 may be added in increments by consuming more bytes with a
  31. rate of at most 255 per extra byte (thus the compression ratio cannot exceed
  32. around 255:1). The variable length encoding using #bits is always the same :
  33. length = byte & ((1 << #bits) - 1)
  34. if (!length) {
  35. length = ((1 << #bits) - 1)
  36. length += 255*(number of zero bytes)
  37. length += first-non-zero-byte
  38. }
  39. length += constant (generally 2 or 3)
  40. For references to the dictionary, distances are relative to the output
  41. pointer. Distances are encoded using very few bits belonging to certain
  42. ranges, resulting in multiple copy instructions using different encodings.
  43. Certain encodings involve one extra byte, others involve two extra bytes
  44. forming a little-endian 16-bit quantity (marked LE16 below).
  45. After any instruction except the large literal copy, 0, 1, 2 or 3 literals
  46. are copied before starting the next instruction. The number of literals that
  47. were copied may change the meaning and behaviour of the next instruction. In
  48. practice, only one instruction needs to know whether 0, less than 4, or more
  49. literals were copied. This is the information stored in the <state> variable
  50. in this implementation. This number of immediate literals to be copied is
  51. generally encoded in the last two bits of the instruction but may also be
  52. taken from the last two bits of an extra operand (eg: distance).
  53. End of stream is declared when a block copy of distance 0 is seen. Only one
  54. instruction may encode this distance (0001HLLL), it takes one LE16 operand
  55. for the distance, thus requiring 3 bytes.
  56. IMPORTANT NOTE : in the code some length checks are missing because certain
  57. instructions are called under the assumption that a certain number of bytes
  58. follow because it has already been guaranteed before parsing the instructions.
  59. They just have to "refill" this credit if they consume extra bytes. This is
  60. an implementation design choice independent on the algorithm or encoding.
  61. Byte sequences
  62. First byte encoding :
  63. 0..17 : follow regular instruction encoding, see below. It is worth
  64. noting that codes 16 and 17 will represent a block copy from
  65. the dictionary which is empty, and that they will always be
  66. invalid at this place.
  67. 18..21 : copy 0..3 literals
  68. state = (byte - 17) = 0..3 [ copy <state> literals ]
  69. skip byte
  70. 22..255 : copy literal string
  71. length = (byte - 17) = 4..238
  72. state = 4 [ don't copy extra literals ]
  73. skip byte
  74. Instruction encoding :
  75. 0 0 0 0 X X X X (0..15)
  76. Depends on the number of literals copied by the last instruction.
  77. If last instruction did not copy any literal (state == 0), this
  78. encoding will be a copy of 4 or more literal, and must be interpreted
  79. like this :
  80. 0 0 0 0 L L L L (0..15) : copy long literal string
  81. length = 3 + (L ?: 15 + (zero_bytes * 255) + non_zero_byte)
  82. state = 4 (no extra literals are copied)
  83. If last instruction used to copy between 1 to 3 literals (encoded in
  84. the instruction's opcode or distance), the instruction is a copy of a
  85. 2-byte block from the dictionary within a 1kB distance. It is worth
  86. noting that this instruction provides little savings since it uses 2
  87. bytes to encode a copy of 2 other bytes but it encodes the number of
  88. following literals for free. It must be interpreted like this :
  89. 0 0 0 0 D D S S (0..15) : copy 2 bytes from <= 1kB distance
  90. length = 2
  91. state = S (copy S literals after this block)
  92. Always followed by exactly one byte : H H H H H H H H
  93. distance = (H << 2) + D + 1
  94. If last instruction used to copy 4 or more literals (as detected by
  95. state == 4), the instruction becomes a copy of a 3-byte block from the
  96. dictionary from a 2..3kB distance, and must be interpreted like this :
  97. 0 0 0 0 D D S S (0..15) : copy 3 bytes from 2..3 kB distance
  98. length = 3
  99. state = S (copy S literals after this block)
  100. Always followed by exactly one byte : H H H H H H H H
  101. distance = (H << 2) + D + 2049
  102. 0 0 0 1 H L L L (16..31)
  103. Copy of a block within 16..48kB distance (preferably less than 10B)
  104. length = 2 + (L ?: 7 + (zero_bytes * 255) + non_zero_byte)
  105. Always followed by exactly one LE16 : D D D D D D D D : D D D D D D S S
  106. distance = 16384 + (H << 14) + D
  107. state = S (copy S literals after this block)
  108. End of stream is reached if distance == 16384
  109. 0 0 1 L L L L L (32..63)
  110. Copy of small block within 16kB distance (preferably less than 34B)
  111. length = 2 + (L ?: 31 + (zero_bytes * 255) + non_zero_byte)
  112. Always followed by exactly one LE16 : D D D D D D D D : D D D D D D S S
  113. distance = D + 1
  114. state = S (copy S literals after this block)
  115. 0 1 L D D D S S (64..127)
  116. Copy 3-4 bytes from block within 2kB distance
  117. state = S (copy S literals after this block)
  118. length = 3 + L
  119. Always followed by exactly one byte : H H H H H H H H
  120. distance = (H << 3) + D + 1
  121. 1 L L D D D S S (128..255)
  122. Copy 5-8 bytes from block within 2kB distance
  123. state = S (copy S literals after this block)
  124. length = 5 + L
  125. Always followed by exactly one byte : H H H H H H H H
  126. distance = (H << 3) + D + 1
  127. Authors
  128. This document was written by Willy Tarreau <w@1wt.eu> on 2014/07/19 during an
  129. analysis of the decompression code available in Linux 3.16-rc5. The code is
  130. tricky, it is possible that this document contains mistakes or that a few
  131. corner cases were overlooked. In any case, please report any doubt, fix, or
  132. proposed updates to the author(s) so that the document can be updated.