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