check.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * \file lzma/check.h
  3. * \brief Integrity checks
  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 Type of the integrity check (Check ID)
  17. *
  18. * The .xz format supports multiple types of checks that are calculated
  19. * from the uncompressed data. They vary in both speed and ability to
  20. * detect errors.
  21. */
  22. typedef enum {
  23. LZMA_CHECK_NONE = 0,
  24. /**<
  25. * No Check is calculated.
  26. *
  27. * Size of the Check field: 0 bytes
  28. */
  29. LZMA_CHECK_CRC32 = 1,
  30. /**<
  31. * CRC32 using the polynomial from the IEEE 802.3 standard
  32. *
  33. * Size of the Check field: 4 bytes
  34. */
  35. LZMA_CHECK_CRC64 = 4,
  36. /**<
  37. * CRC64 using the polynomial from the ECMA-182 standard
  38. *
  39. * Size of the Check field: 8 bytes
  40. */
  41. LZMA_CHECK_SHA256 = 10
  42. /**<
  43. * SHA-256
  44. *
  45. * Size of the Check field: 32 bytes
  46. */
  47. } lzma_check;
  48. /**
  49. * \brief Maximum valid Check ID
  50. *
  51. * The .xz file format specification specifies 16 Check IDs (0-15). Some
  52. * of them are only reserved, that is, no actual Check algorithm has been
  53. * assigned. When decoding, liblzma still accepts unknown Check IDs for
  54. * future compatibility. If a valid but unsupported Check ID is detected,
  55. * liblzma can indicate a warning; see the flags LZMA_TELL_NO_CHECK,
  56. * LZMA_TELL_UNSUPPORTED_CHECK, and LZMA_TELL_ANY_CHECK in container.h.
  57. */
  58. #define LZMA_CHECK_ID_MAX 15
  59. /**
  60. * \brief Test if the given Check ID is supported
  61. *
  62. * LZMA_CHECK_NONE and LZMA_CHECK_CRC32 are always supported (even if
  63. * liblzma is built with limited features).
  64. *
  65. * \note It is safe to call this with a value that is not in the
  66. * range [0, 15]; in that case the return value is always false.
  67. *
  68. * \param check Check ID
  69. *
  70. * \return lzma_bool:
  71. * - true if Check ID is supported by this liblzma build.
  72. * - false otherwise.
  73. */
  74. extern LZMA_API(lzma_bool) lzma_check_is_supported(lzma_check check)
  75. lzma_nothrow lzma_attr_const;
  76. /**
  77. * \brief Get the size of the Check field with the given Check ID
  78. *
  79. * Although not all Check IDs have a check algorithm associated, the size of
  80. * every Check is already frozen. This function returns the size (in bytes) of
  81. * the Check field with the specified Check ID. The values are:
  82. * { 0, 4, 4, 4, 8, 8, 8, 16, 16, 16, 32, 32, 32, 64, 64, 64 }
  83. *
  84. * \param check Check ID
  85. *
  86. * \return Size of the Check field in bytes. If the argument is not in
  87. * the range [0, 15], UINT32_MAX is returned.
  88. */
  89. extern LZMA_API(uint32_t) lzma_check_size(lzma_check check)
  90. lzma_nothrow lzma_attr_const;
  91. /**
  92. * \brief Maximum size of a Check field
  93. */
  94. #define LZMA_CHECK_SIZE_MAX 64
  95. /**
  96. * \brief Calculate CRC32
  97. *
  98. * Calculate CRC32 using the polynomial from the IEEE 802.3 standard.
  99. *
  100. * \param buf Pointer to the input buffer
  101. * \param size Size of the input buffer
  102. * \param crc Previously returned CRC value. This is used to
  103. * calculate the CRC of a big buffer in smaller chunks.
  104. * Set to zero when starting a new calculation.
  105. *
  106. * \return Updated CRC value, which can be passed to this function
  107. * again to continue CRC calculation.
  108. */
  109. extern LZMA_API(uint32_t) lzma_crc32(
  110. const uint8_t *buf, size_t size, uint32_t crc)
  111. lzma_nothrow lzma_attr_pure;
  112. /**
  113. * \brief Calculate CRC64
  114. *
  115. * Calculate CRC64 using the polynomial from the ECMA-182 standard.
  116. *
  117. * This function is used similarly to lzma_crc32().
  118. *
  119. * \param buf Pointer to the input buffer
  120. * \param size Size of the input buffer
  121. * \param crc Previously returned CRC value. This is used to
  122. * calculate the CRC of a big buffer in smaller chunks.
  123. * Set to zero when starting a new calculation.
  124. *
  125. * \return Updated CRC value, which can be passed to this function
  126. * again to continue CRC calculation.
  127. */
  128. extern LZMA_API(uint64_t) lzma_crc64(
  129. const uint8_t *buf, size_t size, uint64_t crc)
  130. lzma_nothrow lzma_attr_pure;
  131. /**
  132. * \brief Get the type of the integrity check
  133. *
  134. * This function can be called only immediately after lzma_code() has
  135. * returned LZMA_NO_CHECK, LZMA_UNSUPPORTED_CHECK, or LZMA_GET_CHECK.
  136. * Calling this function in any other situation has undefined behavior.
  137. *
  138. * \param strm Pointer to lzma_stream meeting the above conditions.
  139. *
  140. * \return Check ID in the lzma_stream, or undefined if called improperly.
  141. */
  142. extern LZMA_API(lzma_check) lzma_get_check(const lzma_stream *strm)
  143. lzma_nothrow;