1 1.4.6.2 yamt /* $NetBSD: crc32.c,v 1.4.6.2 2009/05/04 08:13:50 yamt Exp $ */ 2 1.4.6.2 yamt 3 1.4.6.2 yamt /* crc32.c -- compute the CRC-32 of a data stream 4 1.4.6.2 yamt * 5 1.4.6.2 yamt * Adapted from zlib's crc code. 6 1.4.6.2 yamt * 7 1.4.6.2 yamt * Copyright (C) 1995-2005 Mark Adler 8 1.4.6.2 yamt * For conditions of distribution and use, see copyright notice in zlib.h 9 1.4.6.2 yamt * 10 1.4.6.2 yamt * Thanks to Rodney Brown <rbrown64 (at) csc.com.au> for his contribution of faster 11 1.4.6.2 yamt * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing 12 1.4.6.2 yamt * tables for updating the shift register in one step with three exclusive-ors 13 1.4.6.2 yamt * instead of four steps with four exclusive-ors. This results in about a 14 1.4.6.2 yamt * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. 15 1.4.6.2 yamt */ 16 1.4.6.2 yamt 17 1.4.6.2 yamt /* @(#) Id */ 18 1.4.6.2 yamt 19 1.4.6.2 yamt #include <sys/param.h> 20 1.4.6.2 yamt #include <machine/endian.h> 21 1.4.6.2 yamt 22 1.4.6.2 yamt typedef uint32_t u4; 23 1.4.6.2 yamt 24 1.4.6.2 yamt /* Definitions for doing the crc four data bytes at a time. */ 25 1.4.6.2 yamt #define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ 26 1.4.6.2 yamt (((w)&0xff00)<<8)+(((w)&0xff)<<24)) 27 1.4.6.2 yamt 28 1.4.6.2 yamt /* ======================================================================== 29 1.4.6.2 yamt * Tables of CRC-32s of all single-byte values, made by make_crc_table(). 30 1.4.6.2 yamt */ 31 1.4.6.2 yamt #include <lib/libkern/libkern.h> 32 1.4.6.2 yamt #include "crc32.h" 33 1.4.6.2 yamt 34 1.4.6.2 yamt #if BYTE_ORDER == LITTLE_ENDIAN 35 1.4.6.2 yamt /* ========================================================================= */ 36 1.4.6.2 yamt #define DOLIT4 c ^= *buf4++; \ 37 1.4.6.2 yamt c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 38 1.4.6.2 yamt crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 39 1.4.6.2 yamt #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 40 1.4.6.2 yamt 41 1.4.6.2 yamt /* ========================================================================= */ 42 1.4.6.2 yamt uint32_t crc32(uint32_t crc, const uint8_t *buf, size_t len) 43 1.4.6.2 yamt { 44 1.4.6.2 yamt register u4 c; 45 1.4.6.2 yamt register const u4 *buf4; 46 1.4.6.2 yamt 47 1.4.6.2 yamt if (buf == NULL) return 0UL; 48 1.4.6.2 yamt 49 1.4.6.2 yamt c = (u4)crc; 50 1.4.6.2 yamt c = ~c; 51 1.4.6.2 yamt while (len && ((uintptr_t)buf & 3)) { 52 1.4.6.2 yamt c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 53 1.4.6.2 yamt len--; 54 1.4.6.2 yamt } 55 1.4.6.2 yamt 56 1.4.6.2 yamt buf4 = (const u4 *)(const void *)buf; 57 1.4.6.2 yamt while (len >= 32) { 58 1.4.6.2 yamt DOLIT32; 59 1.4.6.2 yamt len -= 32; 60 1.4.6.2 yamt } 61 1.4.6.2 yamt while (len >= 4) { 62 1.4.6.2 yamt DOLIT4; 63 1.4.6.2 yamt len -= 4; 64 1.4.6.2 yamt } 65 1.4.6.2 yamt buf = (const unsigned char *)buf4; 66 1.4.6.2 yamt 67 1.4.6.2 yamt if (len) do { 68 1.4.6.2 yamt c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 69 1.4.6.2 yamt } while (--len); 70 1.4.6.2 yamt c = ~c; 71 1.4.6.2 yamt return (uint32_t)c; 72 1.4.6.2 yamt } 73 1.4.6.2 yamt 74 1.4.6.2 yamt #else /* BIG_ENDIAN */ 75 1.4.6.2 yamt 76 1.4.6.2 yamt /* ========================================================================= */ 77 1.4.6.2 yamt #define DOBIG4 c ^= *++buf4; \ 78 1.4.6.2 yamt c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 79 1.4.6.2 yamt crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 80 1.4.6.2 yamt #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 81 1.4.6.2 yamt 82 1.4.6.2 yamt /* ========================================================================= */ 83 1.4.6.2 yamt uint32_t crc32(uint32_t crc, const uint8_t *buf, size_t len) 84 1.4.6.2 yamt { 85 1.4.6.2 yamt register u4 c; 86 1.4.6.2 yamt register const u4 *buf4; 87 1.4.6.2 yamt 88 1.4.6.2 yamt if (buf == NULL) return 0UL; 89 1.4.6.2 yamt 90 1.4.6.2 yamt c = REV((u4)crc); 91 1.4.6.2 yamt c = ~c; 92 1.4.6.2 yamt while (len && ((uintptr_t)buf & 3)) { 93 1.4.6.2 yamt c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 94 1.4.6.2 yamt len--; 95 1.4.6.2 yamt } 96 1.4.6.2 yamt 97 1.4.6.2 yamt buf4 = (const u4 *)(const void *)buf; 98 1.4.6.2 yamt buf4--; 99 1.4.6.2 yamt while (len >= 32) { 100 1.4.6.2 yamt DOBIG32; 101 1.4.6.2 yamt len -= 32; 102 1.4.6.2 yamt } 103 1.4.6.2 yamt while (len >= 4) { 104 1.4.6.2 yamt DOBIG4; 105 1.4.6.2 yamt len -= 4; 106 1.4.6.2 yamt } 107 1.4.6.2 yamt buf4++; 108 1.4.6.2 yamt buf = (const unsigned char *)buf4; 109 1.4.6.2 yamt 110 1.4.6.2 yamt if (len) do { 111 1.4.6.2 yamt c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 112 1.4.6.2 yamt } while (--len); 113 1.4.6.2 yamt c = ~c; 114 1.4.6.2 yamt return (uint32_t)(REV(c)); 115 1.4.6.2 yamt } 116 1.4.6.2 yamt #endif 117