This repository has been archived on 2023-07-17. You can view files and clone it, but cannot push or open issues or pull requests.
bl_mcu_sdk/components/xz/xz_crc64.c

53 lines
1007 B
C
Raw Normal View History

2021-06-04 17:51:55 +08:00
/*
* CRC64 using the polynomial from ECMA-182
*
* This file is similar to xz_crc32.c. See the comments there.
*
* Authors: Lasse Collin <lasse.collin@tukaani.org>
* Igor Pavlov <http://7-zip.org/>
*
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
#include "xz_private.h"
#ifndef STATIC_RW_DATA
2021-06-20 12:25:46 +08:00
#define STATIC_RW_DATA static
2021-06-04 17:51:55 +08:00
#endif
STATIC_RW_DATA uint64_t xz_crc64_table[256];
XZ_EXTERN void xz_crc64_init(void)
{
2021-06-20 12:25:46 +08:00
const uint64_t poly = 0xC96C5795D7870F42;
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
uint32_t i;
uint32_t j;
uint64_t r;
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
for (i = 0; i < 256; ++i) {
r = i;
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
for (j = 0; j < 8; ++j) {
r = (r >> 1) ^ (poly & ~((r & 1) - 1));
}
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
xz_crc64_table[i] = r;
}
return;
2021-06-04 17:51:55 +08:00
}
XZ_EXTERN uint64_t xz_crc64(const uint8_t *buf, size_t size, uint64_t crc)
{
2021-06-20 12:25:46 +08:00
crc = ~crc;
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
while (size != 0) {
crc = xz_crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
--size;
}
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
return ~crc;
2021-06-04 17:51:55 +08:00
}