stream_flags.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * \file lzma/stream_flags.h
  3. * \brief .xz Stream Header and Stream Footer encoder and decoder
  4. * \note Never include this file directly. Use <lzma.h> instead.
  5. */
  6. /*
  7. * Author: Lasse Collin
  8. *
  9. * This file has been put into the public domain.
  10. * You can do whatever you want with this file.
  11. */
  12. #ifndef LZMA_H_INTERNAL
  13. # error Never include this file directly. Use <lzma.h> instead.
  14. #endif
  15. /**
  16. * \brief Size of Stream Header and Stream Footer
  17. *
  18. * Stream Header and Stream Footer have the same size and they are not
  19. * going to change even if a newer version of the .xz file format is
  20. * developed in future.
  21. */
  22. #define LZMA_STREAM_HEADER_SIZE 12
  23. /**
  24. * \brief Options for encoding/decoding Stream Header and Stream Footer
  25. */
  26. typedef struct {
  27. /**
  28. * \brief Stream Flags format version
  29. *
  30. * To prevent API and ABI breakages if new features are needed in
  31. * Stream Header or Stream Footer, a version number is used to
  32. * indicate which members in this structure are in use. For now,
  33. * version must always be zero. With non-zero version, the
  34. * lzma_stream_header_encode() and lzma_stream_footer_encode()
  35. * will return LZMA_OPTIONS_ERROR.
  36. *
  37. * lzma_stream_header_decode() and lzma_stream_footer_decode()
  38. * will always set this to the lowest value that supports all the
  39. * features indicated by the Stream Flags field. The application
  40. * must check that the version number set by the decoding functions
  41. * is supported by the application. Otherwise it is possible that
  42. * the application will decode the Stream incorrectly.
  43. */
  44. uint32_t version;
  45. /**
  46. * \brief Backward Size
  47. *
  48. * Backward Size must be a multiple of four bytes. In this Stream
  49. * format version, Backward Size is the size of the Index field.
  50. *
  51. * Backward Size isn't actually part of the Stream Flags field, but
  52. * it is convenient to include in this structure anyway. Backward
  53. * Size is present only in the Stream Footer. There is no need to
  54. * initialize backward_size when encoding Stream Header.
  55. *
  56. * lzma_stream_header_decode() always sets backward_size to
  57. * LZMA_VLI_UNKNOWN so that it is convenient to use
  58. * lzma_stream_flags_compare() when both Stream Header and Stream
  59. * Footer have been decoded.
  60. */
  61. lzma_vli backward_size;
  62. /**
  63. * \brief Minimum value for lzma_stream_flags.backward_size
  64. */
  65. # define LZMA_BACKWARD_SIZE_MIN 4
  66. /**
  67. * \brief Maximum value for lzma_stream_flags.backward_size
  68. */
  69. # define LZMA_BACKWARD_SIZE_MAX (LZMA_VLI_C(1) << 34)
  70. /**
  71. * \brief Check ID
  72. *
  73. * This indicates the type of the integrity check calculated from
  74. * uncompressed data.
  75. */
  76. lzma_check check;
  77. /*
  78. * Reserved space to allow possible future extensions without
  79. * breaking the ABI. You should not touch these, because the
  80. * names of these variables may change.
  81. *
  82. * (We will never be able to use all of these since Stream Flags
  83. * is just two bytes plus Backward Size of four bytes. But it's
  84. * nice to have the proper types when they are needed.)
  85. */
  86. /** \private Reserved member. */
  87. lzma_reserved_enum reserved_enum1;
  88. /** \private Reserved member. */
  89. lzma_reserved_enum reserved_enum2;
  90. /** \private Reserved member. */
  91. lzma_reserved_enum reserved_enum3;
  92. /** \private Reserved member. */
  93. lzma_reserved_enum reserved_enum4;
  94. /** \private Reserved member. */
  95. lzma_bool reserved_bool1;
  96. /** \private Reserved member. */
  97. lzma_bool reserved_bool2;
  98. /** \private Reserved member. */
  99. lzma_bool reserved_bool3;
  100. /** \private Reserved member. */
  101. lzma_bool reserved_bool4;
  102. /** \private Reserved member. */
  103. lzma_bool reserved_bool5;
  104. /** \private Reserved member. */
  105. lzma_bool reserved_bool6;
  106. /** \private Reserved member. */
  107. lzma_bool reserved_bool7;
  108. /** \private Reserved member. */
  109. lzma_bool reserved_bool8;
  110. /** \private Reserved member. */
  111. uint32_t reserved_int1;
  112. /** \private Reserved member. */
  113. uint32_t reserved_int2;
  114. } lzma_stream_flags;
  115. /**
  116. * \brief Encode Stream Header
  117. *
  118. * \param options Stream Header options to be encoded.
  119. * options->backward_size is ignored and doesn't
  120. * need to be initialized.
  121. * \param[out] out Beginning of the output buffer of
  122. * LZMA_STREAM_HEADER_SIZE bytes.
  123. *
  124. * \return Possible lzma_ret values:
  125. * - LZMA_OK: Encoding was successful.
  126. * - LZMA_OPTIONS_ERROR: options->version is not supported by
  127. * this liblzma version.
  128. * - LZMA_PROG_ERROR: Invalid options.
  129. */
  130. extern LZMA_API(lzma_ret) lzma_stream_header_encode(
  131. const lzma_stream_flags *options, uint8_t *out)
  132. lzma_nothrow lzma_attr_warn_unused_result;
  133. /**
  134. * \brief Encode Stream Footer
  135. *
  136. * \param options Stream Footer options to be encoded.
  137. * \param[out] out Beginning of the output buffer of
  138. * LZMA_STREAM_HEADER_SIZE bytes.
  139. *
  140. * \return Possible lzma_ret values:
  141. * - LZMA_OK: Encoding was successful.
  142. * - LZMA_OPTIONS_ERROR: options->version is not supported by
  143. * this liblzma version.
  144. * - LZMA_PROG_ERROR: Invalid options.
  145. */
  146. extern LZMA_API(lzma_ret) lzma_stream_footer_encode(
  147. const lzma_stream_flags *options, uint8_t *out)
  148. lzma_nothrow lzma_attr_warn_unused_result;
  149. /**
  150. * \brief Decode Stream Header
  151. *
  152. * options->backward_size is always set to LZMA_VLI_UNKNOWN. This is to
  153. * help comparing Stream Flags from Stream Header and Stream Footer with
  154. * lzma_stream_flags_compare().
  155. *
  156. * \note When decoding .xz files that contain multiple Streams, it may
  157. * make sense to print "file format not recognized" only if
  158. * decoding of the Stream Header of the \a first Stream gives
  159. * LZMA_FORMAT_ERROR. If non-first Stream Header gives
  160. * LZMA_FORMAT_ERROR, the message used for LZMA_DATA_ERROR is
  161. * probably more appropriate.
  162. * For example, the Stream decoder in liblzma uses
  163. * LZMA_DATA_ERROR if LZMA_FORMAT_ERROR is returned by
  164. * lzma_stream_header_decode() when decoding non-first Stream.
  165. *
  166. * \param[out] options Target for the decoded Stream Header options.
  167. * \param in Beginning of the input buffer of
  168. * LZMA_STREAM_HEADER_SIZE bytes.
  169. *
  170. *
  171. * \return Possible lzma_ret values:
  172. * - LZMA_OK: Decoding was successful.
  173. * - LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given
  174. * buffer cannot be Stream Header.
  175. * - LZMA_DATA_ERROR: CRC32 doesn't match, thus the header
  176. * is corrupt.
  177. * - LZMA_OPTIONS_ERROR: Unsupported options are present
  178. * in the header.
  179. */
  180. extern LZMA_API(lzma_ret) lzma_stream_header_decode(
  181. lzma_stream_flags *options, const uint8_t *in)
  182. lzma_nothrow lzma_attr_warn_unused_result;
  183. /**
  184. * \brief Decode Stream Footer
  185. *
  186. * \note If Stream Header was already decoded successfully, but
  187. * decoding Stream Footer returns LZMA_FORMAT_ERROR, the
  188. * application should probably report some other error message
  189. * than "file format not recognized". The file likely
  190. * is corrupt (possibly truncated). The Stream decoder in liblzma
  191. * uses LZMA_DATA_ERROR in this situation.
  192. *
  193. * \param[out] options Target for the decoded Stream Footer options.
  194. * \param in Beginning of the input buffer of
  195. * LZMA_STREAM_HEADER_SIZE bytes.
  196. *
  197. * \return Possible lzma_ret values:
  198. * - LZMA_OK: Decoding was successful.
  199. * - LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given
  200. * buffer cannot be Stream Footer.
  201. * - LZMA_DATA_ERROR: CRC32 doesn't match, thus the Stream Footer
  202. * is corrupt.
  203. * - LZMA_OPTIONS_ERROR: Unsupported options are present
  204. * in Stream Footer.
  205. */
  206. extern LZMA_API(lzma_ret) lzma_stream_footer_decode(
  207. lzma_stream_flags *options, const uint8_t *in)
  208. lzma_nothrow lzma_attr_warn_unused_result;
  209. /**
  210. * \brief Compare two lzma_stream_flags structures
  211. *
  212. * backward_size values are compared only if both are not
  213. * LZMA_VLI_UNKNOWN.
  214. *
  215. * \param a Pointer to lzma_stream_flags structure
  216. * \param b Pointer to lzma_stream_flags structure
  217. *
  218. * \return Possible lzma_ret values:
  219. * - LZMA_OK: Both are equal. If either had backward_size set
  220. * to LZMA_VLI_UNKNOWN, backward_size values were not
  221. * compared or validated.
  222. * - LZMA_DATA_ERROR: The structures differ.
  223. * - LZMA_OPTIONS_ERROR: version in either structure is greater
  224. * than the maximum supported version (currently zero).
  225. * - LZMA_PROG_ERROR: Invalid value, e.g. invalid check or
  226. * backward_size.
  227. */
  228. extern LZMA_API(lzma_ret) lzma_stream_flags_compare(
  229. const lzma_stream_flags *a, const lzma_stream_flags *b)
  230. lzma_nothrow lzma_attr_pure;