Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * CRC32 using the polynomial from IEEE-802.3
0003  *
0004  * Authors: Lasse Collin <lasse.collin@tukaani.org>
0005  *          Igor Pavlov <https://7-zip.org/>
0006  *
0007  * This file has been put into the public domain.
0008  * You can do whatever you want with this file.
0009  */
0010 
0011 /*
0012  * This is not the fastest implementation, but it is pretty compact.
0013  * The fastest versions of xz_crc32() on modern CPUs without hardware
0014  * accelerated CRC instruction are 3-5 times as fast as this version,
0015  * but they are bigger and use more memory for the lookup table.
0016  */
0017 
0018 #include "xz_private.h"
0019 
0020 /*
0021  * STATIC_RW_DATA is used in the pre-boot environment on some architectures.
0022  * See <linux/decompress/mm.h> for details.
0023  */
0024 #ifndef STATIC_RW_DATA
0025 #   define STATIC_RW_DATA static
0026 #endif
0027 
0028 STATIC_RW_DATA uint32_t xz_crc32_table[256];
0029 
0030 XZ_EXTERN void xz_crc32_init(void)
0031 {
0032     const uint32_t poly = CRC32_POLY_LE;
0033 
0034     uint32_t i;
0035     uint32_t j;
0036     uint32_t r;
0037 
0038     for (i = 0; i < 256; ++i) {
0039         r = i;
0040         for (j = 0; j < 8; ++j)
0041             r = (r >> 1) ^ (poly & ~((r & 1) - 1));
0042 
0043         xz_crc32_table[i] = r;
0044     }
0045 
0046     return;
0047 }
0048 
0049 XZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
0050 {
0051     crc = ~crc;
0052 
0053     while (size != 0) {
0054         crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
0055         --size;
0056     }
0057 
0058     return ~crc;
0059 }