1
0

block.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**
  2. * \file lzma/block.h
  3. * \brief .xz Block handling
  4. */
  5. /*
  6. * Author: Lasse Collin
  7. *
  8. * This file has been put into the public domain.
  9. * You can do whatever you want with this file.
  10. *
  11. * See ../lzma.h for information about liblzma as a whole.
  12. */
  13. #ifndef LZMA_H_INTERNAL
  14. # error Never include this file directly. Use <lzma.h> instead.
  15. #endif
  16. /**
  17. * \brief Options for the Block and Block Header encoders and decoders
  18. *
  19. * Different Block handling functions use different parts of this structure.
  20. * Some read some members, other functions write, and some do both. Only the
  21. * members listed for reading need to be initialized when the specified
  22. * functions are called. The members marked for writing will be assigned
  23. * new values at some point either by calling the given function or by
  24. * later calls to lzma_code().
  25. */
  26. typedef struct {
  27. /**
  28. * \brief Block format version
  29. *
  30. * To prevent API and ABI breakages when new features are needed,
  31. * a version number is used to indicate which fields in this
  32. * structure are in use:
  33. * - liblzma >= 5.0.0: version = 0 is supported.
  34. * - liblzma >= 5.1.4beta: Support for version = 1 was added,
  35. * which adds the ignore_check field.
  36. *
  37. * If version is greater than one, most Block related functions
  38. * will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works
  39. * with any version value).
  40. *
  41. * Read by:
  42. * - All functions that take pointer to lzma_block as argument,
  43. * including lzma_block_header_decode().
  44. *
  45. * Written by:
  46. * - lzma_block_header_decode()
  47. */
  48. uint32_t version;
  49. /**
  50. * \brief Size of the Block Header field
  51. *
  52. * This is always a multiple of four.
  53. *
  54. * Read by:
  55. * - lzma_block_header_encode()
  56. * - lzma_block_header_decode()
  57. * - lzma_block_compressed_size()
  58. * - lzma_block_unpadded_size()
  59. * - lzma_block_total_size()
  60. * - lzma_block_decoder()
  61. * - lzma_block_buffer_decode()
  62. *
  63. * Written by:
  64. * - lzma_block_header_size()
  65. * - lzma_block_buffer_encode()
  66. */
  67. uint32_t header_size;
  68. # define LZMA_BLOCK_HEADER_SIZE_MIN 8
  69. # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
  70. /**
  71. * \brief Type of integrity Check
  72. *
  73. * The Check ID is not stored into the Block Header, thus its value
  74. * must be provided also when decoding.
  75. *
  76. * Read by:
  77. * - lzma_block_header_encode()
  78. * - lzma_block_header_decode()
  79. * - lzma_block_compressed_size()
  80. * - lzma_block_unpadded_size()
  81. * - lzma_block_total_size()
  82. * - lzma_block_encoder()
  83. * - lzma_block_decoder()
  84. * - lzma_block_buffer_encode()
  85. * - lzma_block_buffer_decode()
  86. */
  87. lzma_check check;
  88. /**
  89. * \brief Size of the Compressed Data in bytes
  90. *
  91. * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
  92. * will store this value to the Block Header. Block encoder doesn't
  93. * care about this value, but will set it once the encoding has been
  94. * finished.
  95. *
  96. * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
  97. * verify that the size of the Compressed Data field matches
  98. * compressed_size.
  99. *
  100. * Usually you don't know this value when encoding in streamed mode,
  101. * and thus cannot write this field into the Block Header.
  102. *
  103. * In non-streamed mode you can reserve space for this field before
  104. * encoding the actual Block. After encoding the data, finish the
  105. * Block by encoding the Block Header. Steps in detail:
  106. *
  107. * - Set compressed_size to some big enough value. If you don't know
  108. * better, use LZMA_VLI_MAX, but remember that bigger values take
  109. * more space in Block Header.
  110. *
  111. * - Call lzma_block_header_size() to see how much space you need to
  112. * reserve for the Block Header.
  113. *
  114. * - Encode the Block using lzma_block_encoder() and lzma_code().
  115. * It sets compressed_size to the correct value.
  116. *
  117. * - Use lzma_block_header_encode() to encode the Block Header.
  118. * Because space was reserved in the first step, you don't need
  119. * to call lzma_block_header_size() anymore, because due to
  120. * reserving, header_size has to be big enough. If it is "too big",
  121. * lzma_block_header_encode() will add enough Header Padding to
  122. * make Block Header to match the size specified by header_size.
  123. *
  124. * Read by:
  125. * - lzma_block_header_size()
  126. * - lzma_block_header_encode()
  127. * - lzma_block_compressed_size()
  128. * - lzma_block_unpadded_size()
  129. * - lzma_block_total_size()
  130. * - lzma_block_decoder()
  131. * - lzma_block_buffer_decode()
  132. *
  133. * Written by:
  134. * - lzma_block_header_decode()
  135. * - lzma_block_compressed_size()
  136. * - lzma_block_encoder()
  137. * - lzma_block_decoder()
  138. * - lzma_block_buffer_encode()
  139. * - lzma_block_buffer_decode()
  140. */
  141. lzma_vli compressed_size;
  142. /**
  143. * \brief Uncompressed Size in bytes
  144. *
  145. * This is handled very similarly to compressed_size above.
  146. *
  147. * uncompressed_size is needed by fewer functions than
  148. * compressed_size. This is because uncompressed_size isn't
  149. * needed to validate that Block stays within proper limits.
  150. *
  151. * Read by:
  152. * - lzma_block_header_size()
  153. * - lzma_block_header_encode()
  154. * - lzma_block_decoder()
  155. * - lzma_block_buffer_decode()
  156. *
  157. * Written by:
  158. * - lzma_block_header_decode()
  159. * - lzma_block_encoder()
  160. * - lzma_block_decoder()
  161. * - lzma_block_buffer_encode()
  162. * - lzma_block_buffer_decode()
  163. */
  164. lzma_vli uncompressed_size;
  165. /**
  166. * \brief Array of filters
  167. *
  168. * There can be 1-4 filters. The end of the array is marked with
  169. * .id = LZMA_VLI_UNKNOWN.
  170. *
  171. * Read by:
  172. * - lzma_block_header_size()
  173. * - lzma_block_header_encode()
  174. * - lzma_block_encoder()
  175. * - lzma_block_decoder()
  176. * - lzma_block_buffer_encode()
  177. * - lzma_block_buffer_decode()
  178. *
  179. * Written by:
  180. * - lzma_block_header_decode(): Note that this does NOT free()
  181. * the old filter options structures. All unused filters[] will
  182. * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
  183. * decoding fails, all filters[] are guaranteed to be
  184. * LZMA_VLI_UNKNOWN and NULL.
  185. *
  186. * \note Because of the array is terminated with
  187. * .id = LZMA_VLI_UNKNOWN, the actual array must
  188. * have LZMA_FILTERS_MAX + 1 members or the Block
  189. * Header decoder will overflow the buffer.
  190. */
  191. lzma_filter *filters;
  192. /**
  193. * \brief Raw value stored in the Check field
  194. *
  195. * After successful coding, the first lzma_check_size(check) bytes
  196. * of this array contain the raw value stored in the Check field.
  197. *
  198. * Note that CRC32 and CRC64 are stored in little endian byte order.
  199. * Take it into account if you display the Check values to the user.
  200. *
  201. * Written by:
  202. * - lzma_block_encoder()
  203. * - lzma_block_decoder()
  204. * - lzma_block_buffer_encode()
  205. * - lzma_block_buffer_decode()
  206. */
  207. uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
  208. /*
  209. * Reserved space to allow possible future extensions without
  210. * breaking the ABI. You should not touch these, because the names
  211. * of these variables may change. These are and will never be used
  212. * with the currently supported options, so it is safe to leave these
  213. * uninitialized.
  214. */
  215. void *reserved_ptr1;
  216. void *reserved_ptr2;
  217. void *reserved_ptr3;
  218. uint32_t reserved_int1;
  219. uint32_t reserved_int2;
  220. lzma_vli reserved_int3;
  221. lzma_vli reserved_int4;
  222. lzma_vli reserved_int5;
  223. lzma_vli reserved_int6;
  224. lzma_vli reserved_int7;
  225. lzma_vli reserved_int8;
  226. lzma_reserved_enum reserved_enum1;
  227. lzma_reserved_enum reserved_enum2;
  228. lzma_reserved_enum reserved_enum3;
  229. lzma_reserved_enum reserved_enum4;
  230. /**
  231. * \brief A flag to Block decoder to not verify the Check field
  232. *
  233. * This field is supported by liblzma >= 5.1.4beta if .version >= 1.
  234. *
  235. * If this is set to true, the integrity check won't be calculated
  236. * and verified. Unless you know what you are doing, you should
  237. * leave this to false. (A reason to set this to true is when the
  238. * file integrity is verified externally anyway and you want to
  239. * speed up the decompression, which matters mostly when using
  240. * SHA-256 as the integrity check.)
  241. *
  242. * If .version >= 1, read by:
  243. * - lzma_block_decoder()
  244. * - lzma_block_buffer_decode()
  245. *
  246. * Written by (.version is ignored):
  247. * - lzma_block_header_decode() always sets this to false
  248. */
  249. lzma_bool ignore_check;
  250. lzma_bool reserved_bool2;
  251. lzma_bool reserved_bool3;
  252. lzma_bool reserved_bool4;
  253. lzma_bool reserved_bool5;
  254. lzma_bool reserved_bool6;
  255. lzma_bool reserved_bool7;
  256. lzma_bool reserved_bool8;
  257. } lzma_block;
  258. /**
  259. * \brief Decode the Block Header Size field
  260. *
  261. * To decode Block Header using lzma_block_header_decode(), the size of the
  262. * Block Header has to be known and stored into lzma_block.header_size.
  263. * The size can be calculated from the first byte of a Block using this macro.
  264. * Note that if the first byte is 0x00, it indicates beginning of Index; use
  265. * this macro only when the byte is not 0x00.
  266. *
  267. * There is no encoding macro, because Block Header encoder is enough for that.
  268. */
  269. #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
  270. /**
  271. * \brief Calculate Block Header Size
  272. *
  273. * Calculate the minimum size needed for the Block Header field using the
  274. * settings specified in the lzma_block structure. Note that it is OK to
  275. * increase the calculated header_size value as long as it is a multiple of
  276. * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
  277. * just means that lzma_block_header_encode() will add Header Padding.
  278. *
  279. * \return - LZMA_OK: Size calculated successfully and stored to
  280. * block->header_size.
  281. * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
  282. * filter options.
  283. * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
  284. *
  285. * \note This doesn't check that all the options are valid i.e. this
  286. * may return LZMA_OK even if lzma_block_header_encode() or
  287. * lzma_block_encoder() would fail. If you want to validate the
  288. * filter chain, consider using lzma_memlimit_encoder() which as
  289. * a side-effect validates the filter chain.
  290. */
  291. extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
  292. lzma_nothrow lzma_attr_warn_unused_result;
  293. /**
  294. * \brief Encode Block Header
  295. *
  296. * The caller must have calculated the size of the Block Header already with
  297. * lzma_block_header_size(). If a value larger than the one calculated by
  298. * lzma_block_header_size() is used, the Block Header will be padded to the
  299. * specified size.
  300. *
  301. * \param out Beginning of the output buffer. This must be
  302. * at least block->header_size bytes.
  303. * \param block Block options to be encoded.
  304. *
  305. * \return - LZMA_OK: Encoding was successful. block->header_size
  306. * bytes were written to output buffer.
  307. * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
  308. * - LZMA_PROG_ERROR: Invalid arguments, for example
  309. * block->header_size is invalid or block->filters is NULL.
  310. */
  311. extern LZMA_API(lzma_ret) lzma_block_header_encode(
  312. const lzma_block *block, uint8_t *out)
  313. lzma_nothrow lzma_attr_warn_unused_result;
  314. /**
  315. * \brief Decode Block Header
  316. *
  317. * block->version should (usually) be set to the highest value supported
  318. * by the application. If the application sets block->version to a value
  319. * higher than supported by the current liblzma version, this function will
  320. * downgrade block->version to the highest value supported by it. Thus one
  321. * should check the value of block->version after calling this function if
  322. * block->version was set to a non-zero value and the application doesn't
  323. * otherwise know that the liblzma version being used is new enough to
  324. * support the specified block->version.
  325. *
  326. * The size of the Block Header must have already been decoded with
  327. * lzma_block_header_size_decode() macro and stored to block->header_size.
  328. *
  329. * The integrity check type from Stream Header must have been stored
  330. * to block->check.
  331. *
  332. * block->filters must have been allocated, but they don't need to be
  333. * initialized (possible existing filter options are not freed).
  334. *
  335. * \param block Destination for Block options.
  336. * \param allocator lzma_allocator for custom allocator functions.
  337. * Set to NULL to use malloc() (and also free()
  338. * if an error occurs).
  339. * \param in Beginning of the input buffer. This must be
  340. * at least block->header_size bytes.
  341. *
  342. * \return - LZMA_OK: Decoding was successful. block->header_size
  343. * bytes were read from the input buffer.
  344. * - LZMA_OPTIONS_ERROR: The Block Header specifies some
  345. * unsupported options such as unsupported filters. This can
  346. * happen also if block->version was set to a too low value
  347. * compared to what would be required to properly represent
  348. * the information stored in the Block Header.
  349. * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
  350. * the CRC32 doesn't match.
  351. * - LZMA_PROG_ERROR: Invalid arguments, for example
  352. * block->header_size is invalid or block->filters is NULL.
  353. */
  354. extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
  355. const lzma_allocator *allocator, const uint8_t *in)
  356. lzma_nothrow lzma_attr_warn_unused_result;
  357. /**
  358. * \brief Validate and set Compressed Size according to Unpadded Size
  359. *
  360. * Block Header stores Compressed Size, but Index has Unpadded Size. If the
  361. * application has already parsed the Index and is now decoding Blocks,
  362. * it can calculate Compressed Size from Unpadded Size. This function does
  363. * exactly that with error checking:
  364. *
  365. * - Compressed Size calculated from Unpadded Size must be positive integer,
  366. * that is, Unpadded Size must be big enough that after Block Header and
  367. * Check fields there's still at least one byte for Compressed Size.
  368. *
  369. * - If Compressed Size was present in Block Header, the new value
  370. * calculated from Unpadded Size is compared against the value
  371. * from Block Header.
  372. *
  373. * \note This function must be called _after_ decoding the Block Header
  374. * field so that it can properly validate Compressed Size if it
  375. * was present in Block Header.
  376. *
  377. * \return - LZMA_OK: block->compressed_size was set successfully.
  378. * - LZMA_DATA_ERROR: unpadded_size is too small compared to
  379. * block->header_size and lzma_check_size(block->check).
  380. * - LZMA_PROG_ERROR: Some values are invalid. For example,
  381. * block->header_size must be a multiple of four and
  382. * between 8 and 1024 inclusive.
  383. */
  384. extern LZMA_API(lzma_ret) lzma_block_compressed_size(
  385. lzma_block *block, lzma_vli unpadded_size)
  386. lzma_nothrow lzma_attr_warn_unused_result;
  387. /**
  388. * \brief Calculate Unpadded Size
  389. *
  390. * The Index field stores Unpadded Size and Uncompressed Size. The latter
  391. * can be taken directly from the lzma_block structure after coding a Block,
  392. * but Unpadded Size needs to be calculated from Block Header Size,
  393. * Compressed Size, and size of the Check field. This is where this function
  394. * is needed.
  395. *
  396. * \return Unpadded Size on success, or zero on error.
  397. */
  398. extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
  399. lzma_nothrow lzma_attr_pure;
  400. /**
  401. * \brief Calculate the total encoded size of a Block
  402. *
  403. * This is equivalent to lzma_block_unpadded_size() except that the returned
  404. * value includes the size of the Block Padding field.
  405. *
  406. * \return On success, total encoded size of the Block. On error,
  407. * zero is returned.
  408. */
  409. extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
  410. lzma_nothrow lzma_attr_pure;
  411. /**
  412. * \brief Initialize .xz Block encoder
  413. *
  414. * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
  415. * filter chain supports it), and LZMA_FINISH.
  416. *
  417. * \return - LZMA_OK: All good, continue with lzma_code().
  418. * - LZMA_MEM_ERROR
  419. * - LZMA_OPTIONS_ERROR
  420. * - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
  421. * that is not supported by this build of liblzma. Initializing
  422. * the encoder failed.
  423. * - LZMA_PROG_ERROR
  424. */
  425. extern LZMA_API(lzma_ret) lzma_block_encoder(
  426. lzma_stream *strm, lzma_block *block)
  427. lzma_nothrow lzma_attr_warn_unused_result;
  428. /**
  429. * \brief Initialize .xz Block decoder
  430. *
  431. * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
  432. * LZMA_FINISH is not required. It is supported only for convenience.
  433. *
  434. * \return - LZMA_OK: All good, continue with lzma_code().
  435. * - LZMA_PROG_ERROR
  436. * - LZMA_MEM_ERROR
  437. */
  438. extern LZMA_API(lzma_ret) lzma_block_decoder(
  439. lzma_stream *strm, lzma_block *block)
  440. lzma_nothrow lzma_attr_warn_unused_result;
  441. /**
  442. * \brief Calculate maximum output size for single-call Block encoding
  443. *
  444. * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
  445. * See the documentation of lzma_stream_buffer_bound().
  446. */
  447. extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
  448. lzma_nothrow;
  449. /**
  450. * \brief Single-call .xz Block encoder
  451. *
  452. * In contrast to the multi-call encoder initialized with
  453. * lzma_block_encoder(), this function encodes also the Block Header. This
  454. * is required to make it possible to write appropriate Block Header also
  455. * in case the data isn't compressible, and different filter chain has to be
  456. * used to encode the data in uncompressed form using uncompressed chunks
  457. * of the LZMA2 filter.
  458. *
  459. * When the data isn't compressible, header_size, compressed_size, and
  460. * uncompressed_size are set just like when the data was compressible, but
  461. * it is possible that header_size is too small to hold the filter chain
  462. * specified in block->filters, because that isn't necessarily the filter
  463. * chain that was actually used to encode the data. lzma_block_unpadded_size()
  464. * still works normally, because it doesn't read the filters array.
  465. *
  466. * \param block Block options: block->version, block->check,
  467. * and block->filters must have been initialized.
  468. * \param allocator lzma_allocator for custom allocator functions.
  469. * Set to NULL to use malloc() and free().
  470. * \param in Beginning of the input buffer
  471. * \param in_size Size of the input buffer
  472. * \param out Beginning of the output buffer
  473. * \param out_pos The next byte will be written to out[*out_pos].
  474. * *out_pos is updated only if encoding succeeds.
  475. * \param out_size Size of the out buffer; the first byte into
  476. * which no data is written to is out[out_size].
  477. *
  478. * \return - LZMA_OK: Encoding was successful.
  479. * - LZMA_BUF_ERROR: Not enough output buffer space.
  480. * - LZMA_UNSUPPORTED_CHECK
  481. * - LZMA_OPTIONS_ERROR
  482. * - LZMA_MEM_ERROR
  483. * - LZMA_DATA_ERROR
  484. * - LZMA_PROG_ERROR
  485. */
  486. extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
  487. lzma_block *block, const lzma_allocator *allocator,
  488. const uint8_t *in, size_t in_size,
  489. uint8_t *out, size_t *out_pos, size_t out_size)
  490. lzma_nothrow lzma_attr_warn_unused_result;
  491. /**
  492. * \brief Single-call uncompressed .xz Block encoder
  493. *
  494. * This is like lzma_block_buffer_encode() except this doesn't try to
  495. * compress the data and instead encodes the data using LZMA2 uncompressed
  496. * chunks. The required output buffer size can be determined with
  497. * lzma_block_buffer_bound().
  498. *
  499. * Since the data won't be compressed, this function ignores block->filters.
  500. * This function doesn't take lzma_allocator because this function doesn't
  501. * allocate any memory from the heap.
  502. */
  503. extern LZMA_API(lzma_ret) lzma_block_uncomp_encode(lzma_block *block,
  504. const uint8_t *in, size_t in_size,
  505. uint8_t *out, size_t *out_pos, size_t out_size)
  506. lzma_nothrow lzma_attr_warn_unused_result;
  507. /**
  508. * \brief Single-call .xz Block decoder
  509. *
  510. * This is single-call equivalent of lzma_block_decoder(), and requires that
  511. * the caller has already decoded Block Header and checked its memory usage.
  512. *
  513. * \param block Block options just like with lzma_block_decoder().
  514. * \param allocator lzma_allocator for custom allocator functions.
  515. * Set to NULL to use malloc() and free().
  516. * \param in Beginning of the input buffer
  517. * \param in_pos The next byte will be read from in[*in_pos].
  518. * *in_pos is updated only if decoding succeeds.
  519. * \param in_size Size of the input buffer; the first byte that
  520. * won't be read is in[in_size].
  521. * \param out Beginning of the output buffer
  522. * \param out_pos The next byte will be written to out[*out_pos].
  523. * *out_pos is updated only if encoding succeeds.
  524. * \param out_size Size of the out buffer; the first byte into
  525. * which no data is written to is out[out_size].
  526. *
  527. * \return - LZMA_OK: Decoding was successful.
  528. * - LZMA_OPTIONS_ERROR
  529. * - LZMA_DATA_ERROR
  530. * - LZMA_MEM_ERROR
  531. * - LZMA_BUF_ERROR: Output buffer was too small.
  532. * - LZMA_PROG_ERROR
  533. */
  534. extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
  535. lzma_block *block, const lzma_allocator *allocator,
  536. const uint8_t *in, size_t *in_pos, size_t in_size,
  537. uint8_t *out, size_t *out_pos, size_t out_size)
  538. lzma_nothrow;