check.h 4.2 KB

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