block.h 20 KB

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