crc32.c (821df508e882ca532bec415852399c6558f22f7a) | crc32.c (68d8bb3893ae5a7e96bcc5f5b6f0d430dda0216a) |
---|---|
1/* 2 * This code implements the AUTODIN II polynomial used by Ethernet, 3 * and can be used to calculate multicast address hash indices. 4 * It assumes that the low order bits will be transmitted first, 5 * and consequently the low byte should be sent first when 6 * the crc computation is finished. The crc should be complemented 7 * before transmission. 8 * The variable corresponding to the macro argument "crc" should 9 * be an unsigned long and should be preset to all ones for Ethernet 10 * use. An error-free packet will leave 0xDEBB20E3 in the crc. 11 * Spencer Garrett <srg@quick.com> 12 */ 13 | 1/* 2 * This code implements the AUTODIN II polynomial used by Ethernet, 3 * and can be used to calculate multicast address hash indices. 4 * It assumes that the low order bits will be transmitted first, 5 * and consequently the low byte should be sent first when 6 * the crc computation is finished. The crc should be complemented 7 * before transmission. 8 * The variable corresponding to the macro argument "crc" should 9 * be an unsigned long and should be preset to all ones for Ethernet 10 * use. An error-free packet will leave 0xDEBB20E3 in the crc. 11 * Spencer Garrett <srg@quick.com> 12 */ 13 |
14#include <sys/cdefs.h> 15__FBSDID("$FreeBSD$"); 16 17#include <sys/types.h> 18 19#include <stdio.h> 20#include <stdint.h> 21#include <unistd.h> 22 23#include "extern.h" 24 | |
25#define CRC(crc, ch) (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff]) 26 27/* generated using the AUTODIN II polynomial 28 * x^32 + x^26 + x^23 + x^22 + x^16 + 29 * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1 30 */ | 14#define CRC(crc, ch) (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff]) 15 16/* generated using the AUTODIN II polynomial 17 * x^32 + x^26 + x^23 + x^22 + x^16 + 18 * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1 19 */ |
31static const uint32_t crctab[256] = { | 20unsigned long crctab[256] = { |
32 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 33 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 34 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 35 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 36 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 37 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 38 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 39 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, --- 50 unchanged lines hidden (view full) --- 90 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 91 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 92 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 93 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 94 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 95 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, 96}; 97 | 21 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 22 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 23 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 24 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 25 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 26 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 27 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 28 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, --- 50 unchanged lines hidden (view full) --- 79 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 80 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 81 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 82 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 83 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 84 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d, 85}; 86 |
98uint32_t crc32_total = 0; | 87#include <stdio.h> 88#include <sys/types.h> |
99 | 89 |
100int 101crc32(int fd, uint32_t *cval, off_t *clen) | 90u_long crc32_total = 0 ; 91 92crc32(fd, cval, clen) 93 register int fd; 94 u_long *cval, *clen; |
102{ | 95{ |
103 uint32_t lcrc = ~0; 104 int nr ; 105 off_t len ; | 96 u_long crc = ~0; |
106 char buf[BUFSIZ], *p ; | 97 char buf[BUFSIZ], *p ; |
98 int len, nr ; 99 FILE *in; |
|
107 108 len = 0 ; 109 crc32_total = ~crc32_total ; | 100 101 len = 0 ; 102 crc32_total = ~crc32_total ; |
110 while ((nr = read(fd, buf, sizeof(buf))) > 0) | 103 while (nr = read(fd, buf, sizeof(buf))) |
111 for (len += nr, p = buf; nr--; ++p) { | 104 for (len += nr, p = buf; nr--; ++p) { |
112 CRC(lcrc, *p) ; | 105 CRC(crc, *p) ; |
113 CRC(crc32_total, *p) ; 114 } 115 if (nr < 0) 116 return 1 ; 117 118 *clen = len ; | 106 CRC(crc32_total, *p) ; 107 } 108 if (nr < 0) 109 return 1 ; 110 111 *clen = len ; |
119 *cval = ~lcrc ; | 112 *cval = ~crc ; |
120 crc32_total = ~crc32_total ; 121 return 0 ; 122} | 113 crc32_total = ~crc32_total ; 114 return 0 ; 115} |