check.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file check.c
  5. /// \brief Single API to access different integrity checks
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "check.h"
  11. extern LZMA_API(lzma_bool)
  12. lzma_check_is_supported(lzma_check type)
  13. {
  14. if ((unsigned int)(type) > LZMA_CHECK_ID_MAX)
  15. return false;
  16. static const lzma_bool available_checks[LZMA_CHECK_ID_MAX + 1] = {
  17. true, // LZMA_CHECK_NONE
  18. #ifdef HAVE_CHECK_CRC32
  19. true,
  20. #else
  21. false,
  22. #endif
  23. false, // Reserved
  24. false, // Reserved
  25. #ifdef HAVE_CHECK_CRC64
  26. true,
  27. #else
  28. false,
  29. #endif
  30. false, // Reserved
  31. false, // Reserved
  32. false, // Reserved
  33. false, // Reserved
  34. false, // Reserved
  35. #ifdef HAVE_CHECK_SHA256
  36. true,
  37. #else
  38. false,
  39. #endif
  40. false, // Reserved
  41. false, // Reserved
  42. false, // Reserved
  43. false, // Reserved
  44. false, // Reserved
  45. };
  46. return available_checks[(unsigned int)(type)];
  47. }
  48. extern LZMA_API(uint32_t)
  49. lzma_check_size(lzma_check type)
  50. {
  51. if ((unsigned int)(type) > LZMA_CHECK_ID_MAX)
  52. return UINT32_MAX;
  53. // See file-format.txt section 2.1.1.2.
  54. static const uint8_t check_sizes[LZMA_CHECK_ID_MAX + 1] = {
  55. 0,
  56. 4, 4, 4,
  57. 8, 8, 8,
  58. 16, 16, 16,
  59. 32, 32, 32,
  60. 64, 64, 64
  61. };
  62. return check_sizes[(unsigned int)(type)];
  63. }
  64. extern void
  65. lzma_check_init(lzma_check_state *check, lzma_check type)
  66. {
  67. switch (type) {
  68. case LZMA_CHECK_NONE:
  69. break;
  70. #ifdef HAVE_CHECK_CRC32
  71. case LZMA_CHECK_CRC32:
  72. check->state.crc32 = 0;
  73. break;
  74. #endif
  75. #ifdef HAVE_CHECK_CRC64
  76. case LZMA_CHECK_CRC64:
  77. check->state.crc64 = 0;
  78. break;
  79. #endif
  80. #ifdef HAVE_CHECK_SHA256
  81. case LZMA_CHECK_SHA256:
  82. lzma_sha256_init(check);
  83. break;
  84. #endif
  85. default:
  86. break;
  87. }
  88. return;
  89. }
  90. extern void
  91. lzma_check_update(lzma_check_state *check, lzma_check type,
  92. const uint8_t *buf, size_t size)
  93. {
  94. switch (type) {
  95. #ifdef HAVE_CHECK_CRC32
  96. case LZMA_CHECK_CRC32:
  97. check->state.crc32 = lzma_crc32(buf, size, check->state.crc32);
  98. break;
  99. #endif
  100. #ifdef HAVE_CHECK_CRC64
  101. case LZMA_CHECK_CRC64:
  102. check->state.crc64 = lzma_crc64(buf, size, check->state.crc64);
  103. break;
  104. #endif
  105. #ifdef HAVE_CHECK_SHA256
  106. case LZMA_CHECK_SHA256:
  107. lzma_sha256_update(buf, size, check);
  108. break;
  109. #endif
  110. default:
  111. break;
  112. }
  113. return;
  114. }
  115. extern void
  116. lzma_check_finish(lzma_check_state *check, lzma_check type)
  117. {
  118. switch (type) {
  119. #ifdef HAVE_CHECK_CRC32
  120. case LZMA_CHECK_CRC32:
  121. check->buffer.u32[0] = conv32le(check->state.crc32);
  122. break;
  123. #endif
  124. #ifdef HAVE_CHECK_CRC64
  125. case LZMA_CHECK_CRC64:
  126. check->buffer.u64[0] = conv64le(check->state.crc64);
  127. break;
  128. #endif
  129. #ifdef HAVE_CHECK_SHA256
  130. case LZMA_CHECK_SHA256:
  131. lzma_sha256_finish(check);
  132. break;
  133. #endif
  134. default:
  135. break;
  136. }
  137. return;
  138. }