crc32.c (d7d1005304be06c8b9e61169e0fc5e43e1cee133) crc32.c (4933ffaed9927902d8e810b6989996c0e1221321)
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/types.h>
15
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 */
16#define CRC(crc, ch) (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff])
17
18/* generated using the AUTODIN II polynomial
19 * x^32 + x^26 + x^23 + x^22 + x^16 +
20 * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
21 */
20unsigned long crctab[256] = {
22static const u_int32_t crctab[256] = {
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,

--- 54 unchanged lines hidden (view full) ---

83 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
84 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
85};
86
87#include <stdio.h>
88#include <sys/types.h>
89#include <unistd.h>
90
23 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
24 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
25 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
26 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
27 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
28 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
29 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
30 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,

--- 54 unchanged lines hidden (view full) ---

85 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
86 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
87};
88
89#include <stdio.h>
90#include <sys/types.h>
91#include <unistd.h>
92
91u_long crc32_total = 0 ;
93u_int32_t crc32_total = 0 ;
92
93int
94crc32(fd, cval, clen)
95 register int fd;
94
95int
96crc32(fd, cval, clen)
97 register int fd;
96 u_long *cval, *clen;
98 u_int32_t *cval, *clen;
97{
99{
98 u_long crc = ~0;
100 u_int32_t crc = ~0;
99 char buf[BUFSIZ], *p ;
100 int len, nr ;
101
102 len = 0 ;
103 crc32_total = ~crc32_total ;
104 while ((nr = read(fd, buf, sizeof(buf))))
105 for (len += nr, p = buf; nr--; ++p) {
106 CRC(crc, *p) ;
107 CRC(crc32_total, *p) ;
108 }
109 if (nr < 0)
110 return 1 ;
111
112 *clen = len ;
113 *cval = ~crc ;
114 crc32_total = ~crc32_total ;
115 return 0 ;
116}
101 char buf[BUFSIZ], *p ;
102 int len, nr ;
103
104 len = 0 ;
105 crc32_total = ~crc32_total ;
106 while ((nr = read(fd, buf, sizeof(buf))))
107 for (len += nr, p = buf; nr--; ++p) {
108 CRC(crc, *p) ;
109 CRC(crc32_total, *p) ;
110 }
111 if (nr < 0)
112 return 1 ;
113
114 *clen = len ;
115 *cval = ~crc ;
116 crc32_total = ~crc32_total ;
117 return 0 ;
118}