^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * CRC32 using the polynomial from IEEE-802.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Authors: Lasse Collin <lasse.collin@tukaani.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Igor Pavlov <https://7-zip.org/>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file has been put into the public domain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * You can do whatever you want with this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This is not the fastest implementation, but it is pretty compact.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * The fastest versions of xz_crc32() on modern CPUs without hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * accelerated CRC instruction are 3-5 times as fast as this version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * but they are bigger and use more memory for the lookup table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "xz_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * STATIC_RW_DATA is used in the pre-boot environment on some architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * See <linux/decompress/mm.h> for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #ifndef STATIC_RW_DATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) # define STATIC_RW_DATA static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) STATIC_RW_DATA uint32_t xz_crc32_table[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) XZ_EXTERN void xz_crc32_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) const uint32_t poly = CRC32_POLY_LE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) uint32_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) uint32_t j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) uint32_t r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) for (i = 0; i < 256; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) r = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) for (j = 0; j < 8; ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) r = (r >> 1) ^ (poly & ~((r & 1) - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) xz_crc32_table[i] = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) crc = ~crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) while (size != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) --size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ~crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }