xz-file-format.txt 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. The .xz File Format
  2. ===================
  3. Version 1.1.0 (2022-12-11)
  4. 0. Preface
  5. 0.1. Notices and Acknowledgements
  6. 0.2. Getting the Latest Version
  7. 0.3. Version History
  8. 1. Conventions
  9. 1.1. Byte and Its Representation
  10. 1.2. Multibyte Integers
  11. 2. Overall Structure of .xz File
  12. 2.1. Stream
  13. 2.1.1. Stream Header
  14. 2.1.1.1. Header Magic Bytes
  15. 2.1.1.2. Stream Flags
  16. 2.1.1.3. CRC32
  17. 2.1.2. Stream Footer
  18. 2.1.2.1. CRC32
  19. 2.1.2.2. Backward Size
  20. 2.1.2.3. Stream Flags
  21. 2.1.2.4. Footer Magic Bytes
  22. 2.2. Stream Padding
  23. 3. Block
  24. 3.1. Block Header
  25. 3.1.1. Block Header Size
  26. 3.1.2. Block Flags
  27. 3.1.3. Compressed Size
  28. 3.1.4. Uncompressed Size
  29. 3.1.5. List of Filter Flags
  30. 3.1.6. Header Padding
  31. 3.1.7. CRC32
  32. 3.2. Compressed Data
  33. 3.3. Block Padding
  34. 3.4. Check
  35. 4. Index
  36. 4.1. Index Indicator
  37. 4.2. Number of Records
  38. 4.3. List of Records
  39. 4.3.1. Unpadded Size
  40. 4.3.2. Uncompressed Size
  41. 4.4. Index Padding
  42. 4.5. CRC32
  43. 5. Filter Chains
  44. 5.1. Alignment
  45. 5.2. Security
  46. 5.3. Filters
  47. 5.3.1. LZMA2
  48. 5.3.2. Branch/Call/Jump Filters for Executables
  49. 5.3.3. Delta
  50. 5.3.3.1. Format of the Encoded Output
  51. 5.4. Custom Filter IDs
  52. 5.4.1. Reserved Custom Filter ID Ranges
  53. 6. Cyclic Redundancy Checks
  54. 7. References
  55. 0. Preface
  56. This document describes the .xz file format (filename suffix
  57. ".xz", MIME type "application/x-xz"). It is intended that this
  58. this format replace the old .lzma format used by LZMA SDK and
  59. LZMA Utils.
  60. 0.1. Notices and Acknowledgements
  61. This file format was designed by Lasse Collin
  62. <lasse.collin@tukaani.org> and Igor Pavlov.
  63. Special thanks for helping with this document goes to
  64. Ville Koskinen. Thanks for helping with this document goes to
  65. Mark Adler, H. Peter Anvin, Mikko Pouru, and Lars Wirzenius.
  66. This document has been put into the public domain.
  67. 0.2. Getting the Latest Version
  68. The latest official version of this document can be downloaded
  69. from <http://tukaani.org/xz/xz-file-format.txt>.
  70. Specific versions of this document have a filename
  71. xz-file-format-X.Y.Z.txt where X.Y.Z is the version number.
  72. For example, the version 1.0.0 of this document is available
  73. at <http://tukaani.org/xz/xz-file-format-1.0.0.txt>.
  74. 0.3. Version History
  75. Version Date Description
  76. 1.1.0 2022-12-11 Added ARM64 filter and clarified 32-bit
  77. ARM endianness in Section 5.3.2,
  78. language improvements in Section 5.4
  79. 1.0.4 2009-08-27 Language improvements in Sections 1.2,
  80. 2.1.1.2, 3.1.1, 3.1.2, and 5.3.1
  81. 1.0.3 2009-06-05 Spelling fixes in Sections 5.1 and 5.4
  82. 1.0.2 2009-06-04 Typo fixes in Sections 4 and 5.3.1
  83. 1.0.1 2009-06-01 Typo fix in Section 0.3 and minor
  84. clarifications to Sections 2, 2.2,
  85. 3.3, 4.4, and 5.3.2
  86. 1.0.0 2009-01-14 The first official version
  87. 1. Conventions
  88. The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD",
  89. "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  90. document are to be interpreted as described in [RFC-2119].
  91. Indicating a warning means displaying a message, returning
  92. appropriate exit status, or doing something else to let the
  93. user know that something worth warning occurred. The operation
  94. SHOULD still finish if a warning is indicated.
  95. Indicating an error means displaying a message, returning
  96. appropriate exit status, or doing something else to let the
  97. user know that something prevented successfully finishing the
  98. operation. The operation MUST be aborted once an error has
  99. been indicated.
  100. 1.1. Byte and Its Representation
  101. In this document, byte is always 8 bits.
  102. A "null byte" has all bits unset. That is, the value of a null
  103. byte is 0x00.
  104. To represent byte blocks, this document uses notation that
  105. is similar to the notation used in [RFC-1952]:
  106. +-------+
  107. | Foo | One byte.
  108. +-------+
  109. +---+---+
  110. | Foo | Two bytes; that is, some of the vertical bars
  111. +---+---+ can be missing.
  112. +=======+
  113. | Foo | Zero or more bytes.
  114. +=======+
  115. In this document, a boxed byte or a byte sequence declared
  116. using this notation is called "a field". The example field
  117. above would be called "the Foo field" or plain "Foo".
  118. If there are many fields, they may be split to multiple lines.
  119. This is indicated with an arrow ("--->"):
  120. +=====+
  121. | Foo |
  122. +=====+
  123. +=====+
  124. ---> | Bar |
  125. +=====+
  126. The above is equivalent to this:
  127. +=====+=====+
  128. | Foo | Bar |
  129. +=====+=====+
  130. 1.2. Multibyte Integers
  131. Multibyte integers of static length, such as CRC values,
  132. are stored in little endian byte order (least significant
  133. byte first).
  134. When smaller values are more likely than bigger values (for
  135. example file sizes), multibyte integers are encoded in a
  136. variable-length representation:
  137. - Numbers in the range [0, 127] are copied as is, and take
  138. one byte of space.
  139. - Bigger numbers will occupy two or more bytes. All but the
  140. last byte of the multibyte representation have the highest
  141. (eighth) bit set.
  142. For now, the value of the variable-length integers is limited
  143. to 63 bits, which limits the encoded size of the integer to
  144. nine bytes. These limits may be increased in the future if
  145. needed.
  146. The following C code illustrates encoding and decoding of
  147. variable-length integers. The functions return the number of
  148. bytes occupied by the integer (1-9), or zero on error.
  149. #include <stddef.h>
  150. #include <inttypes.h>
  151. size_t
  152. encode(uint8_t buf[static 9], uint64_t num)
  153. {
  154. if (num > UINT64_MAX / 2)
  155. return 0;
  156. size_t i = 0;
  157. while (num >= 0x80) {
  158. buf[i++] = (uint8_t)(num) | 0x80;
  159. num >>= 7;
  160. }
  161. buf[i++] = (uint8_t)(num);
  162. return i;
  163. }
  164. size_t
  165. decode(const uint8_t buf[], size_t size_max, uint64_t *num)
  166. {
  167. if (size_max == 0)
  168. return 0;
  169. if (size_max > 9)
  170. size_max = 9;
  171. *num = buf[0] & 0x7F;
  172. size_t i = 0;
  173. while (buf[i++] & 0x80) {
  174. if (i >= size_max || buf[i] == 0x00)
  175. return 0;
  176. *num |= (uint64_t)(buf[i] & 0x7F) << (i * 7);
  177. }
  178. return i;
  179. }
  180. 2. Overall Structure of .xz File
  181. A standalone .xz files consist of one or more Streams which may
  182. have Stream Padding between or after them:
  183. +========+================+========+================+
  184. | Stream | Stream Padding | Stream | Stream Padding | ...
  185. +========+================+========+================+
  186. The sizes of Stream and Stream Padding are always multiples
  187. of four bytes, thus the size of every valid .xz file MUST be
  188. a multiple of four bytes.
  189. While a typical file contains only one Stream and no Stream
  190. Padding, a decoder handling standalone .xz files SHOULD support
  191. files that have more than one Stream or Stream Padding.
  192. In contrast to standalone .xz files, when the .xz file format
  193. is used as an internal part of some other file format or
  194. communication protocol, it usually is expected that the decoder
  195. stops after the first Stream, and doesn't look for Stream
  196. Padding or possibly other Streams.
  197. 2.1. Stream
  198. +-+-+-+-+-+-+-+-+-+-+-+-+=======+=======+ +=======+
  199. | Stream Header | Block | Block | ... | Block |
  200. +-+-+-+-+-+-+-+-+-+-+-+-+=======+=======+ +=======+
  201. +=======+-+-+-+-+-+-+-+-+-+-+-+-+
  202. ---> | Index | Stream Footer |
  203. +=======+-+-+-+-+-+-+-+-+-+-+-+-+
  204. All the above fields have a size that is a multiple of four. If
  205. Stream is used as an internal part of another file format, it
  206. is RECOMMENDED to make the Stream start at an offset that is
  207. a multiple of four bytes.
  208. Stream Header, Index, and Stream Footer are always present in
  209. a Stream. The maximum size of the Index field is 16 GiB (2^34).
  210. There are zero or more Blocks. The maximum number of Blocks is
  211. limited only by the maximum size of the Index field.
  212. Total size of a Stream MUST be less than 8 EiB (2^63 bytes).
  213. The same limit applies to the total amount of uncompressed
  214. data stored in a Stream.
  215. If an implementation supports handling .xz files with multiple
  216. concatenated Streams, it MAY apply the above limits to the file
  217. as a whole instead of limiting per Stream basis.
  218. 2.1.1. Stream Header
  219. +---+---+---+---+---+---+-------+------+--+--+--+--+
  220. | Header Magic Bytes | Stream Flags | CRC32 |
  221. +---+---+---+---+---+---+-------+------+--+--+--+--+
  222. 2.1.1.1. Header Magic Bytes
  223. The first six (6) bytes of the Stream are so called Header
  224. Magic Bytes. They can be used to identify the file type.
  225. Using a C array and ASCII:
  226. const uint8_t HEADER_MAGIC[6]
  227. = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
  228. In plain hexadecimal:
  229. FD 37 7A 58 5A 00
  230. Notes:
  231. - The first byte (0xFD) was chosen so that the files cannot
  232. be erroneously detected as being in .lzma format, in which
  233. the first byte is in the range [0x00, 0xE0].
  234. - The sixth byte (0x00) was chosen to prevent applications
  235. from misdetecting the file as a text file.
  236. If the Header Magic Bytes don't match, the decoder MUST
  237. indicate an error.
  238. 2.1.1.2. Stream Flags
  239. The first byte of Stream Flags is always a null byte. In the
  240. future, this byte may be used to indicate a new Stream version
  241. or other Stream properties.
  242. The second byte of Stream Flags is a bit field:
  243. Bit(s) Mask Description
  244. 0-3 0x0F Type of Check (see Section 3.4):
  245. ID Size Check name
  246. 0x00 0 bytes None
  247. 0x01 4 bytes CRC32
  248. 0x02 4 bytes (Reserved)
  249. 0x03 4 bytes (Reserved)
  250. 0x04 8 bytes CRC64
  251. 0x05 8 bytes (Reserved)
  252. 0x06 8 bytes (Reserved)
  253. 0x07 16 bytes (Reserved)
  254. 0x08 16 bytes (Reserved)
  255. 0x09 16 bytes (Reserved)
  256. 0x0A 32 bytes SHA-256
  257. 0x0B 32 bytes (Reserved)
  258. 0x0C 32 bytes (Reserved)
  259. 0x0D 64 bytes (Reserved)
  260. 0x0E 64 bytes (Reserved)
  261. 0x0F 64 bytes (Reserved)
  262. 4-7 0xF0 Reserved for future use; MUST be zero for now.
  263. Implementations SHOULD support at least the Check IDs 0x00
  264. (None) and 0x01 (CRC32). Supporting other Check IDs is
  265. OPTIONAL. If an unsupported Check is used, the decoder SHOULD
  266. indicate a warning or error.
  267. If any reserved bit is set, the decoder MUST indicate an error.
  268. It is possible that there is a new field present which the
  269. decoder is not aware of, and can thus parse the Stream Header
  270. incorrectly.
  271. 2.1.1.3. CRC32
  272. The CRC32 is calculated from the Stream Flags field. It is
  273. stored as an unsigned 32-bit little endian integer. If the
  274. calculated value does not match the stored one, the decoder
  275. MUST indicate an error.
  276. The idea is that Stream Flags would always be two bytes, even
  277. if new features are needed. This way old decoders will be able
  278. to verify the CRC32 calculated from Stream Flags, and thus
  279. distinguish between corrupt files (CRC32 doesn't match) and
  280. files that the decoder doesn't support (CRC32 matches but
  281. Stream Flags has reserved bits set).
  282. 2.1.2. Stream Footer
  283. +-+-+-+-+---+---+---+---+-------+------+----------+---------+
  284. | CRC32 | Backward Size | Stream Flags | Footer Magic Bytes |
  285. +-+-+-+-+---+---+---+---+-------+------+----------+---------+
  286. 2.1.2.1. CRC32
  287. The CRC32 is calculated from the Backward Size and Stream Flags
  288. fields. It is stored as an unsigned 32-bit little endian
  289. integer. If the calculated value does not match the stored one,
  290. the decoder MUST indicate an error.
  291. The reason to have the CRC32 field before the Backward Size and
  292. Stream Flags fields is to keep the four-byte fields aligned to
  293. a multiple of four bytes.
  294. 2.1.2.2. Backward Size
  295. Backward Size is stored as a 32-bit little endian integer,
  296. which indicates the size of the Index field as multiple of
  297. four bytes, minimum value being four bytes:
  298. real_backward_size = (stored_backward_size + 1) * 4;
  299. If the stored value does not match the real size of the Index
  300. field, the decoder MUST indicate an error.
  301. Using a fixed-size integer to store Backward Size makes
  302. it slightly simpler to parse the Stream Footer when the
  303. application needs to parse the Stream backwards.
  304. 2.1.2.3. Stream Flags
  305. This is a copy of the Stream Flags field from the Stream
  306. Header. The information stored to Stream Flags is needed
  307. when parsing the Stream backwards. The decoder MUST compare
  308. the Stream Flags fields in both Stream Header and Stream
  309. Footer, and indicate an error if they are not identical.
  310. 2.1.2.4. Footer Magic Bytes
  311. As the last step of the decoding process, the decoder MUST
  312. verify the existence of Footer Magic Bytes. If they don't
  313. match, an error MUST be indicated.
  314. Using a C array and ASCII:
  315. const uint8_t FOOTER_MAGIC[2] = { 'Y', 'Z' };
  316. In hexadecimal:
  317. 59 5A
  318. The primary reason to have Footer Magic Bytes is to make
  319. it easier to detect incomplete files quickly, without
  320. uncompressing. If the file does not end with Footer Magic Bytes
  321. (excluding Stream Padding described in Section 2.2), it cannot
  322. be undamaged, unless someone has intentionally appended garbage
  323. after the end of the Stream.
  324. 2.2. Stream Padding
  325. Only the decoders that support decoding of concatenated Streams
  326. MUST support Stream Padding.
  327. Stream Padding MUST contain only null bytes. To preserve the
  328. four-byte alignment of consecutive Streams, the size of Stream
  329. Padding MUST be a multiple of four bytes. Empty Stream Padding
  330. is allowed. If these requirements are not met, the decoder MUST
  331. indicate an error.
  332. Note that non-empty Stream Padding is allowed at the end of the
  333. file; there doesn't need to be a new Stream after non-empty
  334. Stream Padding. This can be convenient in certain situations
  335. [GNU-tar].
  336. The possibility of Stream Padding MUST be taken into account
  337. when designing an application that parses Streams backwards,
  338. and the application supports concatenated Streams.
  339. 3. Block
  340. +==============+=================+===============+=======+
  341. | Block Header | Compressed Data | Block Padding | Check |
  342. +==============+=================+===============+=======+
  343. 3.1. Block Header
  344. +-------------------+-------------+=================+
  345. | Block Header Size | Block Flags | Compressed Size |
  346. +-------------------+-------------+=================+
  347. +===================+======================+
  348. ---> | Uncompressed Size | List of Filter Flags |
  349. +===================+======================+
  350. +================+--+--+--+--+
  351. ---> | Header Padding | CRC32 |
  352. +================+--+--+--+--+
  353. 3.1.1. Block Header Size
  354. This field overlaps with the Index Indicator field (see
  355. Section 4.1).
  356. This field contains the size of the Block Header field,
  357. including the Block Header Size field itself. Valid values are
  358. in the range [0x01, 0xFF], which indicate the size of the Block
  359. Header as multiples of four bytes, minimum size being eight
  360. bytes:
  361. real_header_size = (encoded_header_size + 1) * 4;
  362. If a Block Header bigger than 1024 bytes is needed in the
  363. future, a new field can be added between the Block Header and
  364. Compressed Data fields. The presence of this new field would
  365. be indicated in the Block Header field.
  366. 3.1.2. Block Flags
  367. The Block Flags field is a bit field:
  368. Bit(s) Mask Description
  369. 0-1 0x03 Number of filters (1-4)
  370. 2-5 0x3C Reserved for future use; MUST be zero for now.
  371. 6 0x40 The Compressed Size field is present.
  372. 7 0x80 The Uncompressed Size field is present.
  373. If any reserved bit is set, the decoder MUST indicate an error.
  374. It is possible that there is a new field present which the
  375. decoder is not aware of, and can thus parse the Block Header
  376. incorrectly.
  377. 3.1.3. Compressed Size
  378. This field is present only if the appropriate bit is set in
  379. the Block Flags field (see Section 3.1.2).
  380. The Compressed Size field contains the size of the Compressed
  381. Data field, which MUST be non-zero. Compressed Size is stored
  382. using the encoding described in Section 1.2. If the Compressed
  383. Size doesn't match the size of the Compressed Data field, the
  384. decoder MUST indicate an error.
  385. 3.1.4. Uncompressed Size
  386. This field is present only if the appropriate bit is set in
  387. the Block Flags field (see Section 3.1.2).
  388. The Uncompressed Size field contains the size of the Block
  389. after uncompressing. Uncompressed Size is stored using the
  390. encoding described in Section 1.2. If the Uncompressed Size
  391. does not match the real uncompressed size, the decoder MUST
  392. indicate an error.
  393. Storing the Compressed Size and Uncompressed Size fields serves
  394. several purposes:
  395. - The decoder knows how much memory it needs to allocate
  396. for a temporary buffer in multithreaded mode.
  397. - Simple error detection: wrong size indicates a broken file.
  398. - Seeking forwards to a specific location in streamed mode.
  399. It should be noted that the only reliable way to determine
  400. the real uncompressed size is to uncompress the Block,
  401. because the Block Header and Index fields may contain
  402. (intentionally or unintentionally) invalid information.
  403. 3.1.5. List of Filter Flags
  404. +================+================+ +================+
  405. | Filter 0 Flags | Filter 1 Flags | ... | Filter n Flags |
  406. +================+================+ +================+
  407. The number of Filter Flags fields is stored in the Block Flags
  408. field (see Section 3.1.2).
  409. The format of each Filter Flags field is as follows:
  410. +===========+====================+===================+
  411. | Filter ID | Size of Properties | Filter Properties |
  412. +===========+====================+===================+
  413. Both Filter ID and Size of Properties are stored using the
  414. encoding described in Section 1.2. Size of Properties indicates
  415. the size of the Filter Properties field as bytes. The list of
  416. officially defined Filter IDs and the formats of their Filter
  417. Properties are described in Section 5.3.
  418. Filter IDs greater than or equal to 0x4000_0000_0000_0000
  419. (2^62) are reserved for implementation-specific internal use.
  420. These Filter IDs MUST never be used in List of Filter Flags.
  421. 3.1.6. Header Padding
  422. This field contains as many null byte as it is needed to make
  423. the Block Header have the size specified in Block Header Size.
  424. If any of the bytes are not null bytes, the decoder MUST
  425. indicate an error. It is possible that there is a new field
  426. present which the decoder is not aware of, and can thus parse
  427. the Block Header incorrectly.
  428. 3.1.7. CRC32
  429. The CRC32 is calculated over everything in the Block Header
  430. field except the CRC32 field itself. It is stored as an
  431. unsigned 32-bit little endian integer. If the calculated
  432. value does not match the stored one, the decoder MUST indicate
  433. an error.
  434. By verifying the CRC32 of the Block Header before parsing the
  435. actual contents allows the decoder to distinguish between
  436. corrupt and unsupported files.
  437. 3.2. Compressed Data
  438. The format of Compressed Data depends on Block Flags and List
  439. of Filter Flags. Excluding the descriptions of the simplest
  440. filters in Section 5.3, the format of the filter-specific
  441. encoded data is out of scope of this document.
  442. 3.3. Block Padding
  443. Block Padding MUST contain 0-3 null bytes to make the size of
  444. the Block a multiple of four bytes. This can be needed when
  445. the size of Compressed Data is not a multiple of four. If any
  446. of the bytes in Block Padding are not null bytes, the decoder
  447. MUST indicate an error.
  448. 3.4. Check
  449. The type and size of the Check field depends on which bits
  450. are set in the Stream Flags field (see Section 2.1.1.2).
  451. The Check, when used, is calculated from the original
  452. uncompressed data. If the calculated Check does not match the
  453. stored one, the decoder MUST indicate an error. If the selected
  454. type of Check is not supported by the decoder, it SHOULD
  455. indicate a warning or error.
  456. 4. Index
  457. +-----------------+===================+
  458. | Index Indicator | Number of Records |
  459. +-----------------+===================+
  460. +=================+===============+-+-+-+-+
  461. ---> | List of Records | Index Padding | CRC32 |
  462. +=================+===============+-+-+-+-+
  463. Index serves several purposes. Using it, one can
  464. - verify that all Blocks in a Stream have been processed;
  465. - find out the uncompressed size of a Stream; and
  466. - quickly access the beginning of any Block (random access).
  467. 4.1. Index Indicator
  468. This field overlaps with the Block Header Size field (see
  469. Section 3.1.1). The value of Index Indicator is always 0x00.
  470. 4.2. Number of Records
  471. This field indicates how many Records there are in the List
  472. of Records field, and thus how many Blocks there are in the
  473. Stream. The value is stored using the encoding described in
  474. Section 1.2. If the decoder has decoded all the Blocks of the
  475. Stream, and then notices that the Number of Records doesn't
  476. match the real number of Blocks, the decoder MUST indicate an
  477. error.
  478. 4.3. List of Records
  479. List of Records consists of as many Records as indicated by the
  480. Number of Records field:
  481. +========+========+
  482. | Record | Record | ...
  483. +========+========+
  484. Each Record contains information about one Block:
  485. +===============+===================+
  486. | Unpadded Size | Uncompressed Size |
  487. +===============+===================+
  488. If the decoder has decoded all the Blocks of the Stream, it
  489. MUST verify that the contents of the Records match the real
  490. Unpadded Size and Uncompressed Size of the respective Blocks.
  491. Implementation hint: It is possible to verify the Index with
  492. constant memory usage by calculating for example SHA-256 of
  493. both the real size values and the List of Records, then
  494. comparing the hash values. Implementing this using
  495. non-cryptographic hash like CRC32 SHOULD be avoided unless
  496. small code size is important.
  497. If the decoder supports random-access reading, it MUST verify
  498. that Unpadded Size and Uncompressed Size of every completely
  499. decoded Block match the sizes stored in the Index. If only
  500. partial Block is decoded, the decoder MUST verify that the
  501. processed sizes don't exceed the sizes stored in the Index.
  502. 4.3.1. Unpadded Size
  503. This field indicates the size of the Block excluding the Block
  504. Padding field. That is, Unpadded Size is the size of the Block
  505. Header, Compressed Data, and Check fields. Unpadded Size is
  506. stored using the encoding described in Section 1.2. The value
  507. MUST never be zero; with the current structure of Blocks, the
  508. actual minimum value for Unpadded Size is five.
  509. Implementation note: Because the size of the Block Padding
  510. field is not included in Unpadded Size, calculating the total
  511. size of a Stream or doing random-access reading requires
  512. calculating the actual size of the Blocks by rounding Unpadded
  513. Sizes up to the next multiple of four.
  514. The reason to exclude Block Padding from Unpadded Size is to
  515. ease making a raw copy of Compressed Data without Block
  516. Padding. This can be useful, for example, if someone wants
  517. to convert Streams to some other file format quickly.
  518. 4.3.2. Uncompressed Size
  519. This field indicates the Uncompressed Size of the respective
  520. Block as bytes. The value is stored using the encoding
  521. described in Section 1.2.
  522. 4.4. Index Padding
  523. This field MUST contain 0-3 null bytes to pad the Index to
  524. a multiple of four bytes. If any of the bytes are not null
  525. bytes, the decoder MUST indicate an error.
  526. 4.5. CRC32
  527. The CRC32 is calculated over everything in the Index field
  528. except the CRC32 field itself. The CRC32 is stored as an
  529. unsigned 32-bit little endian integer. If the calculated
  530. value does not match the stored one, the decoder MUST indicate
  531. an error.
  532. 5. Filter Chains
  533. The Block Flags field defines how many filters are used. When
  534. more than one filter is used, the filters are chained; that is,
  535. the output of one filter is the input of another filter. The
  536. following figure illustrates the direction of data flow.
  537. v Uncompressed Data ^
  538. | Filter 0 |
  539. Encoder | Filter 1 | Decoder
  540. | Filter n |
  541. v Compressed Data ^
  542. 5.1. Alignment
  543. Alignment of uncompressed input data is usually the job of
  544. the application producing the data. For example, to get the
  545. best results, an archiver tool should make sure that all
  546. PowerPC executable files in the archive stream start at
  547. offsets that are multiples of four bytes.
  548. Some filters, for example LZMA2, can be configured to take
  549. advantage of specified alignment of input data. Note that
  550. taking advantage of aligned input can be beneficial also when
  551. a filter is not the first filter in the chain. For example,
  552. if you compress PowerPC executables, you may want to use the
  553. PowerPC filter and chain that with the LZMA2 filter. Because
  554. not only the input but also the output alignment of the PowerPC
  555. filter is four bytes, it is now beneficial to set LZMA2
  556. settings so that the LZMA2 encoder can take advantage of its
  557. four-byte-aligned input data.
  558. The output of the last filter in the chain is stored to the
  559. Compressed Data field, which is is guaranteed to be aligned
  560. to a multiple of four bytes relative to the beginning of the
  561. Stream. This can increase
  562. - speed, if the filtered data is handled multiple bytes at
  563. a time by the filter-specific encoder and decoder,
  564. because accessing aligned data in computer memory is
  565. usually faster; and
  566. - compression ratio, if the output data is later compressed
  567. with an external compression tool.
  568. 5.2. Security
  569. If filters would be allowed to be chained freely, it would be
  570. possible to create malicious files, that would be very slow to
  571. decode. Such files could be used to create denial of service
  572. attacks.
  573. Slow files could occur when multiple filters are chained:
  574. v Compressed input data
  575. | Filter 1 decoder (last filter)
  576. | Filter 0 decoder (non-last filter)
  577. v Uncompressed output data
  578. The decoder of the last filter in the chain produces a lot of
  579. output from little input. Another filter in the chain takes the
  580. output of the last filter, and produces very little output
  581. while consuming a lot of input. As a result, a lot of data is
  582. moved inside the filter chain, but the filter chain as a whole
  583. gets very little work done.
  584. To prevent this kind of slow files, there are restrictions on
  585. how the filters can be chained. These restrictions MUST be
  586. taken into account when designing new filters.
  587. The maximum number of filters in the chain has been limited to
  588. four, thus there can be at maximum of three non-last filters.
  589. Of these three non-last filters, only two are allowed to change
  590. the size of the data.
  591. The non-last filters, that change the size of the data, MUST
  592. have a limit how much the decoder can compress the data: the
  593. decoder SHOULD produce at least n bytes of output when the
  594. filter is given 2n bytes of input. This limit is not
  595. absolute, but significant deviations MUST be avoided.
  596. The above limitations guarantee that if the last filter in the
  597. chain produces 4n bytes of output, the chain as a whole will
  598. produce at least n bytes of output.
  599. 5.3. Filters
  600. 5.3.1. LZMA2
  601. LZMA (Lempel-Ziv-Markov chain-Algorithm) is a general-purpose
  602. compression algorithm with high compression ratio and fast
  603. decompression. LZMA is based on LZ77 and range coding
  604. algorithms.
  605. LZMA2 is an extension on top of the original LZMA. LZMA2 uses
  606. LZMA internally, but adds support for flushing the encoder,
  607. uncompressed chunks, eases stateful decoder implementations,
  608. and improves support for multithreading. Thus, the plain LZMA
  609. will not be supported in this file format.
  610. Filter ID: 0x21
  611. Size of Filter Properties: 1 byte
  612. Changes size of data: Yes
  613. Allow as a non-last filter: No
  614. Allow as the last filter: Yes
  615. Preferred alignment:
  616. Input data: Adjustable to 1/2/4/8/16 byte(s)
  617. Output data: 1 byte
  618. The format of the one-byte Filter Properties field is as
  619. follows:
  620. Bits Mask Description
  621. 0-5 0x3F Dictionary Size
  622. 6-7 0xC0 Reserved for future use; MUST be zero for now.
  623. Dictionary Size is encoded with one-bit mantissa and five-bit
  624. exponent. The smallest dictionary size is 4 KiB and the biggest
  625. is 4 GiB.
  626. Raw value Mantissa Exponent Dictionary size
  627. 0 2 11 4 KiB
  628. 1 3 11 6 KiB
  629. 2 2 12 8 KiB
  630. 3 3 12 12 KiB
  631. 4 2 13 16 KiB
  632. 5 3 13 24 KiB
  633. 6 2 14 32 KiB
  634. ... ... ... ...
  635. 35 3 27 768 MiB
  636. 36 2 28 1024 MiB
  637. 37 3 29 1536 MiB
  638. 38 2 30 2048 MiB
  639. 39 3 30 3072 MiB
  640. 40 2 31 4096 MiB - 1 B
  641. Instead of having a table in the decoder, the dictionary size
  642. can be decoded using the following C code:
  643. const uint8_t bits = get_dictionary_flags() & 0x3F;
  644. if (bits > 40)
  645. return DICTIONARY_TOO_BIG; // Bigger than 4 GiB
  646. uint32_t dictionary_size;
  647. if (bits == 40) {
  648. dictionary_size = UINT32_MAX;
  649. } else {
  650. dictionary_size = 2 | (bits & 1);
  651. dictionary_size <<= bits / 2 + 11;
  652. }
  653. 5.3.2. Branch/Call/Jump Filters for Executables
  654. These filters convert relative branch, call, and jump
  655. instructions to their absolute counterparts in executable
  656. files. This conversion increases redundancy and thus
  657. compression ratio.
  658. Size of Filter Properties: 0 or 4 bytes
  659. Changes size of data: No
  660. Allow as a non-last filter: Yes
  661. Allow as the last filter: No
  662. Below is the list of filters in this category. The alignment
  663. is the same for both input and output data.
  664. Filter ID Alignment Description
  665. 0x04 1 byte x86 filter (BCJ)
  666. 0x05 4 bytes PowerPC (big endian) filter
  667. 0x06 16 bytes IA64 filter
  668. 0x07 4 bytes ARM filter [1]
  669. 0x08 2 bytes ARM Thumb filter [1]
  670. 0x09 4 bytes SPARC filter
  671. 0x0A 4 bytes ARM64 filter [2]
  672. [1] These are for little endian instruction encoding.
  673. This must not be confused with data endianness.
  674. A processor configured for big endian data access
  675. may still use little endian instruction encoding.
  676. The filters don't care about the data endianness.
  677. [2] 4096-byte alignment gives the best results
  678. because the address in the ADRP instruction
  679. is a multiple of 4096 bytes.
  680. If the size of Filter Properties is four bytes, the Filter
  681. Properties field contains the start offset used for address
  682. conversions. It is stored as an unsigned 32-bit little endian
  683. integer. The start offset MUST be a multiple of the alignment
  684. of the filter as listed in the table above; if it isn't, the
  685. decoder MUST indicate an error. If the size of Filter
  686. Properties is zero, the start offset is zero.
  687. Setting the start offset may be useful if an executable has
  688. multiple sections, and there are many cross-section calls.
  689. Taking advantage of this feature usually requires usage of
  690. the Subblock filter, whose design is not complete yet.
  691. 5.3.3. Delta
  692. The Delta filter may increase compression ratio when the value
  693. of the next byte correlates with the value of an earlier byte
  694. at specified distance.
  695. Filter ID: 0x03
  696. Size of Filter Properties: 1 byte
  697. Changes size of data: No
  698. Allow as a non-last filter: Yes
  699. Allow as the last filter: No
  700. Preferred alignment:
  701. Input data: 1 byte
  702. Output data: Same as the original input data
  703. The Properties byte indicates the delta distance, which can be
  704. 1-256 bytes backwards from the current byte: 0x00 indicates
  705. distance of 1 byte and 0xFF distance of 256 bytes.
  706. 5.3.3.1. Format of the Encoded Output
  707. The code below illustrates both encoding and decoding with
  708. the Delta filter.
  709. // Distance is in the range [1, 256].
  710. const unsigned int distance = get_properties_byte() + 1;
  711. uint8_t pos = 0;
  712. uint8_t delta[256];
  713. memset(delta, 0, sizeof(delta));
  714. while (1) {
  715. const int byte = read_byte();
  716. if (byte == EOF)
  717. break;
  718. uint8_t tmp = delta[(uint8_t)(distance + pos)];
  719. if (is_encoder) {
  720. tmp = (uint8_t)(byte) - tmp;
  721. delta[pos] = (uint8_t)(byte);
  722. } else {
  723. tmp = (uint8_t)(byte) + tmp;
  724. delta[pos] = tmp;
  725. }
  726. write_byte(tmp);
  727. --pos;
  728. }
  729. 5.4. Custom Filter IDs
  730. If a developer wants to use custom Filter IDs, there are two
  731. choices. The first choice is to contact Lasse Collin and ask
  732. him to allocate a range of IDs for the developer.
  733. The second choice is to generate a 40-bit random integer
  734. which the developer can use as a personal Developer ID.
  735. To minimize the risk of collisions, Developer ID has to be
  736. a randomly generated integer, not manually selected "hex word".
  737. The following command, which works on many free operating
  738. systems, can be used to generate Developer ID:
  739. dd if=/dev/urandom bs=5 count=1 | hexdump
  740. The developer can then use the Developer ID to create unique
  741. (well, hopefully unique) Filter IDs.
  742. Bits Mask Description
  743. 0-15 0x0000_0000_0000_FFFF Filter ID
  744. 16-55 0x00FF_FFFF_FFFF_0000 Developer ID
  745. 56-62 0x3F00_0000_0000_0000 Static prefix: 0x3F
  746. The resulting 63-bit integer will use 9 bytes of space when
  747. stored using the encoding described in Section 1.2. To get
  748. a shorter ID, see the beginning of this Section how to
  749. request a custom ID range.
  750. 5.4.1. Reserved Custom Filter ID Ranges
  751. Range Description
  752. 0x0000_0300 - 0x0000_04FF Reserved to ease .7z compatibility
  753. 0x0002_0000 - 0x0007_FFFF Reserved to ease .7z compatibility
  754. 0x0200_0000 - 0x07FF_FFFF Reserved to ease .7z compatibility
  755. 6. Cyclic Redundancy Checks
  756. There are several incompatible variations to calculate CRC32
  757. and CRC64. For simplicity and clarity, complete examples are
  758. provided to calculate the checks as they are used in this file
  759. format. Implementations MAY use different code as long as it
  760. gives identical results.
  761. The program below reads data from standard input, calculates
  762. the CRC32 and CRC64 values, and prints the calculated values
  763. as big endian hexadecimal strings to standard output.
  764. #include <stddef.h>
  765. #include <inttypes.h>
  766. #include <stdio.h>
  767. uint32_t crc32_table[256];
  768. uint64_t crc64_table[256];
  769. void
  770. init(void)
  771. {
  772. static const uint32_t poly32 = UINT32_C(0xEDB88320);
  773. static const uint64_t poly64
  774. = UINT64_C(0xC96C5795D7870F42);
  775. for (size_t i = 0; i < 256; ++i) {
  776. uint32_t crc32 = i;
  777. uint64_t crc64 = i;
  778. for (size_t j = 0; j < 8; ++j) {
  779. if (crc32 & 1)
  780. crc32 = (crc32 >> 1) ^ poly32;
  781. else
  782. crc32 >>= 1;
  783. if (crc64 & 1)
  784. crc64 = (crc64 >> 1) ^ poly64;
  785. else
  786. crc64 >>= 1;
  787. }
  788. crc32_table[i] = crc32;
  789. crc64_table[i] = crc64;
  790. }
  791. }
  792. uint32_t
  793. crc32(const uint8_t *buf, size_t size, uint32_t crc)
  794. {
  795. crc = ~crc;
  796. for (size_t i = 0; i < size; ++i)
  797. crc = crc32_table[buf[i] ^ (crc & 0xFF)]
  798. ^ (crc >> 8);
  799. return ~crc;
  800. }
  801. uint64_t
  802. crc64(const uint8_t *buf, size_t size, uint64_t crc)
  803. {
  804. crc = ~crc;
  805. for (size_t i = 0; i < size; ++i)
  806. crc = crc64_table[buf[i] ^ (crc & 0xFF)]
  807. ^ (crc >> 8);
  808. return ~crc;
  809. }
  810. int
  811. main()
  812. {
  813. init();
  814. uint32_t value32 = 0;
  815. uint64_t value64 = 0;
  816. uint64_t total_size = 0;
  817. uint8_t buf[8192];
  818. while (1) {
  819. const size_t buf_size
  820. = fread(buf, 1, sizeof(buf), stdin);
  821. if (buf_size == 0)
  822. break;
  823. total_size += buf_size;
  824. value32 = crc32(buf, buf_size, value32);
  825. value64 = crc64(buf, buf_size, value64);
  826. }
  827. printf("Bytes: %" PRIu64 "\n", total_size);
  828. printf("CRC-32: 0x%08" PRIX32 "\n", value32);
  829. printf("CRC-64: 0x%016" PRIX64 "\n", value64);
  830. return 0;
  831. }
  832. 7. References
  833. LZMA SDK - The original LZMA implementation
  834. http://7-zip.org/sdk.html
  835. LZMA Utils - LZMA adapted to POSIX-like systems
  836. http://tukaani.org/lzma/
  837. XZ Utils - The next generation of LZMA Utils
  838. http://tukaani.org/xz/
  839. [RFC-1952]
  840. GZIP file format specification version 4.3
  841. http://www.ietf.org/rfc/rfc1952.txt
  842. - Notation of byte boxes in section "2.1. Overall conventions"
  843. [RFC-2119]
  844. Key words for use in RFCs to Indicate Requirement Levels
  845. http://www.ietf.org/rfc/rfc2119.txt
  846. [GNU-tar]
  847. GNU tar 1.21 manual
  848. http://www.gnu.org/software/tar/manual/html_node/Blocking-Factor.html
  849. - Node 9.4.2 "Blocking Factor", paragraph that begins
  850. "gzip will complain about trailing garbage"
  851. - Note that this URL points to the latest version of the
  852. manual, and may some day not contain the note which is in
  853. 1.21. For the exact version of the manual, download GNU
  854. tar 1.21: ftp://ftp.gnu.org/pub/gnu/tar/tar-1.21.tar.gz