check.c 2.8 KB

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