stream_flags.h 8.1 KB

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