crc.c (f74fd6ec7ad22a2ee3f40cad8977ee86abefb9c5) | crc.c (3fa15ce5d8c2bff56d3e11f1a47e264ae30514d3) |
---|---|
1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * James W. Williams of NASA Goddard Space Flight Center. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 31 unchanged lines hidden (view full) --- 40#endif 41static const char rcsid[] = 42 "$FreeBSD$"; 43#endif /* not lint */ 44 45#include <sys/types.h> 46#include <unistd.h> 47 | 1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * James W. Williams of NASA Goddard Space Flight Center. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 31 unchanged lines hidden (view full) --- 40#endif 41static const char rcsid[] = 42 "$FreeBSD$"; 43#endif /* not lint */ 44 45#include <sys/types.h> 46#include <unistd.h> 47 |
48#include "extern.h" 49 |
|
48static const u_int32_t crctab[] = { 49 0x0, 50 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 51 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 52 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 53 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 54 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 55 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, --- 49 unchanged lines hidden (view full) --- 105 * other programs can use it. It takes a file descriptor to read from and 106 * locations to store the crc and the number of bytes read. It returns 0 on 107 * success and 1 on failure. Errno is set on failure. 108 */ 109u_int32_t crc_total = ~0; /* The crc over a number of files. */ 110 111int 112crc(fd, cval, clen) | 50static const u_int32_t crctab[] = { 51 0x0, 52 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 53 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 54 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 55 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 56 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 57 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, --- 49 unchanged lines hidden (view full) --- 107 * other programs can use it. It takes a file descriptor to read from and 108 * locations to store the crc and the number of bytes read. It returns 0 on 109 * success and 1 on failure. Errno is set on failure. 110 */ 111u_int32_t crc_total = ~0; /* The crc over a number of files. */ 112 113int 114crc(fd, cval, clen) |
113 register int fd; | 115 int fd; |
114 u_int32_t *cval, *clen; 115{ | 116 u_int32_t *cval, *clen; 117{ |
116 register u_char *p; 117 register int nr; 118 register u_int32_t crc, len; | 118 u_char *p; 119 int nr; 120 u_int32_t lcrc, len; |
119 u_char buf[16 * 1024]; 120 121#define COMPUTE(var, ch) (var) = (var) << 8 ^ crctab[(var) >> 24 ^ (ch)] 122 | 121 u_char buf[16 * 1024]; 122 123#define COMPUTE(var, ch) (var) = (var) << 8 ^ crctab[(var) >> 24 ^ (ch)] 124 |
123 crc = len = 0; | 125 lcrc = len = 0; |
124 crc_total = ~crc_total; 125 while ((nr = read(fd, buf, sizeof(buf))) > 0) 126 for (len += nr, p = buf; nr--; ++p) { | 126 crc_total = ~crc_total; 127 while ((nr = read(fd, buf, sizeof(buf))) > 0) 128 for (len += nr, p = buf; nr--; ++p) { |
127 COMPUTE(crc, *p); | 129 COMPUTE(lcrc, *p); |
128 COMPUTE(crc_total, *p); 129 } 130 if (nr < 0) 131 return (1); 132 133 *clen = len; 134 135 /* Include the length of the file. */ 136 for (; len != 0; len >>= 8) { | 130 COMPUTE(crc_total, *p); 131 } 132 if (nr < 0) 133 return (1); 134 135 *clen = len; 136 137 /* Include the length of the file. */ 138 for (; len != 0; len >>= 8) { |
137 COMPUTE(crc, len & 0xff); | 139 COMPUTE(lcrc, len & 0xff); |
138 COMPUTE(crc_total, len & 0xff); 139 } 140 | 140 COMPUTE(crc_total, len & 0xff); 141 } 142 |
141 *cval = ~crc; | 143 *cval = ~lcrc; |
142 crc_total = ~crc_total; 143 return (0); 144} | 144 crc_total = ~crc_total; 145 return (0); 146} |