10578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* 20578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * CRC32 using the polynomial from IEEE-802.3 30578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * 40578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Authors: Lasse Collin <lasse.collin@tukaani.org> 50578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * Igor Pavlov <http://7-zip.org/> 60578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * 70578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * This file has been put into the public domain. 80578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * You can do whatever you want with this file. 90578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */ 100578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 110578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* 120578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * This is not the fastest implementation, but it is pretty compact. 130578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * The fastest versions of xz_crc32() on modern CPUs without hardware 140578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * accelerated CRC instruction are 3-5 times as fast as this version, 150578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * but they are bigger and use more memory for the lookup table. 160578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */ 170578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 180578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#include "xz_private.h" 190578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 200578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync/* 210578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * STATIC_RW_DATA is used in the pre-boot environment on some architectures. 220578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync * See <linux/decompress/mm.h> for details. 230578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync */ 240578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#ifndef STATIC_RW_DATA 250578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync# define STATIC_RW_DATA static 260578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync#endif 270578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 280578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncSTATIC_RW_DATA uint32_t xz_crc32_table[256]; 290578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 300578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN void xz_crc32_init(void) 310578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{ 320578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync const uint32_t poly = 0xEDB88320; 330578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 340578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync uint32_t i; 350578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync uint32_t j; 360578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync uint32_t r; 370578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 380578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync for (i = 0; i < 256; ++i) { 390578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync r = i; 400578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync for (j = 0; j < 8; ++j) 410578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync r = (r >> 1) ^ (poly & ~((r & 1) - 1)); 420578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 430578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync xz_crc32_table[i] = r; 440578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync } 450578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 460578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync return; 470578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync} 480578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 490578287ba49f36d031f6d61bb53f5b54f56c65a3repo syncXZ_EXTERN uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc) 500578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync{ 510578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync crc = ~crc; 520578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 530578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync while (size != 0) { 540578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8); 550578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync --size; 560578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync } 570578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync 580578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync return ~crc; 590578287ba49f36d031f6d61bb53f5b54f56c65a3repo sync} 60