1 #include "stdlib.h"
2 /****************************************************************
3 Copyright 1990, 1993, 1994 by AT&T, Lucent Technologies and Bellcore.
4 
5 Permission to use, copy, modify, and distribute this software and its
6 documentation for any purpose and without fee is hereby granted,
7 provided that the above copyright notice appear in all copies and that
8 both that the copyright notice and this permission notice and warranty
9 disclaimer appear in supporting documentation, and that the names of
10 AT&T, Bell Laboratories, Lucent or Bellcore or any of their entities
11 not be used in advertising or publicity pertaining to distribution of
12 the software without specific, written prior permission.
13 
14 AT&T, Lucent and Bellcore disclaim all warranties with regard to this
15 software, including all implied warranties of merchantability and
16 fitness.  In no event shall AT&T or Bellcore be liable for any
17 special, indirect or consequential damages or any damages whatsoever
18 resulting from loss of use, data or profits, whether in an action of
19 contract, negligence or other tortious action, arising out of or in
20 connection with the use or performance of this software.
21 ****************************************************************/
22 
23 #define _POSIX_SOURCE
24 #include "stdio.h"
25 #ifndef KR_headers
26 #include "stdlib.h"
27 #include "sys/types.h"
28 #include "fcntl.h"	/* for declaration of open, O_RDONLY */
29 #include "unistd.h"	/* for read, close */
30 #endif
31 #ifdef MSDOS
32 #include "io.h"
33 #endif
34 #ifndef O_RDONLY
35 #define O_RDONLY 0
36 #endif
37 #ifndef O_BINARY
38 #define O_BINARY O_RDONLY
39 #endif
40 
41  char *progname;
42  static int ignore_cr;
43 
44  void
45 #ifdef KR_headers
usage(rc)46 usage(rc)
47 #else
48 usage(int rc)
49 #endif
50 {
51 	fprintf(stderr, "usage: %s [-r] [file [file...]]\n\
52 	option -r ignores carriage return characters\n", progname);
53 	exit(rc);
54 	}
55 
56 typedef unsigned char Uchar;
57 
58  long
59 #ifdef KR_headers
sum32(sum,x,n)60 sum32(sum, x, n)
61  register long sum;
62  register Uchar *x;
63  int n;
64 #else
65 sum32(register long sum, register Uchar *x, int n)
66 #endif
67 {
68 	register Uchar *xe;
69 	static long crc_table[256] = {
70 		0,		151466134,	302932268,	453595578,
71 		-9583591,	-160762737,	-312236747,	-463170141,
72 		-19167182,	-136529756,	-321525474,	-439166584,
73 		28724267,	145849533,	330837255,	448732561,
74 		-38334364,	-189783822,	-273059512,	-423738914,
75 		47895677,	199091435,	282375505,	433292743,
76 		57448534,	174827712,	291699066,	409324012,
77 		-67019697,	-184128295,	-300991133,	-418902539,
78 		-76668728,	-227995554,	-379567644,	-530091662,
79 		67364049,	218420295,	369985021,	520795499,
80 		95791354,	213031020,	398182870,	515701056,
81 		-86479645,	-203465611,	-388624945,	-506380967,
82 		114897068,	266207290,	349655424,	500195606,
83 		-105581387,	-256654301,	-340093543,	-490887921,
84 		-134039394,	-251295736,	-368256590,	-485758684,
85 		124746887,	241716241,	358686123,	476458301,
86 		-153337456,	-2395898,	-455991108,	-304803798,
87 		162629001,	11973919,	465560741,	314102835,
88 		134728098,	16841012,	436840590,	319723544,
89 		-144044613,	-26395347,	-446403433,	-329032703,
90 		191582708,	40657250,	426062040,	274858062,
91 		-200894995,	-50223749,	-435620671,	-284179369,
92 		-172959290,	-55056048,	-406931222,	-289830788,
93 		182263263,	64630089,	416513267,	299125861,
94 		229794136,	78991822,	532414580,	381366498,
95 		-220224191,	-69691945,	-523123603,	-371788549,
96 		-211162774,	-93398532,	-513308602,	-396314416,
97 		201600371,	84090341,	503991391,	386759881,
98 		-268078788,	-117292630,	-502591472,	-351526778,
99 		258520357,	107972019,	493278217,	341959839,
100 		249493774,	131713432,	483432482,	366454964,
101 		-239911657,	-122417791,	-474129349,	-356881235,
102 		-306674912,	-457198666,	-4791796,	-156118374,
103 		315967289,	466778031,	14362133,	165418627,
104 		325258002,	442776452,	23947838,	141187752,
105 		-334573813,	-452329571,	-33509849,	-150495567,
106 		269456196,	419996626,	33682024,	184992510,
107 		-278767779,	-429561909,	-43239823,	-194312473,
108 		-288089226,	-405591072,	-52790694,	-170046772,
109 		297394031,	415166457,	62373443,	179343061,
110 		383165416,	533828478,	81314500,	232780370,
111 		-373594127,	-524527769,	-72022307,	-223201717,
112 		-401789990,	-519431348,	-100447498,	-217810336,
113 		392228803,	510123861,	91131631,	208256633,
114 		-345918580,	-496598246,	-110112096,	-261561802,
115 		336361365,	487278339,	100800185,	251995695,
116 		364526526,	482151208,	129260178,	246639108,
117 		-354943065,	-472854735,	-119955829,	-237064675,
118 		459588272,	308539942,	157983644,	7181066,
119 		-469170519,	-317835713,	-167286907,	-16754925,
120 		-440448382,	-323454444,	-139383890,	-21619912,
121 		450006683,	332774925,	148697015,	31186721,
122 		-422325548,	-271261118,	-186797064,	-36011154,
123 		431888077,	280569435,	196114401,	45565815,
124 		403200742,	286222960,	168180682,	50400092,
125 		-412770561,	-295522711,	-177471533,	-59977915,
126 		-536157576,	-384970002,	-234585260,	-83643454,
127 		526853729,	375396087,	225003341,	74348507,
128 		517040714,	399923932,	215944038,	98057200,
129 		-507728301,	-390357307,	-206385281,	-88735767,
130 		498987548,	347783818,	263426864,	112501670,
131 		-489671163,	-338229613,	-253864151,	-103192641,
132 		-479823314,	-362722632,	-244835582,	-126932076,
133 		470531639,	353144481,	235265819,	117632909
134 		};
135 
136 	xe = x + n;
137 	while(x < xe)
138 		sum = crc_table[(sum ^ *x++) & 0xff] ^ (sum >> 8 & 0xffffff);
139 	return sum;
140 	}
141 
142  int
143 #ifdef KR_headers
cr_purge(buf,n)144 cr_purge(buf, n)
145  Uchar *buf;
146  int n;
147 #else
148 cr_purge(Uchar *buf, int n)
149 #endif
150 {
151 	register Uchar *b, *b1, *be;
152 	b = buf;
153 	be = b + n;
154 	while(b < be)
155 		if (*b++ == '\r') {
156 			b1 = b - 1;
157 			while(b < be)
158 				if ((*b1 = *b++) != '\r')
159 					b1++;
160 			return b1 - buf;
161 			}
162 	return n;
163 	}
164 
165 static Uchar Buf[16*1024];
166 
167  void
168 #ifdef KR_headers
process(s,x)169 process(s, x)
170  char *s;
171  int x;
172 #else
173 process(char *s, int x)
174 #endif
175 {
176 	register int n;
177 	long fsize, sum;
178 
179 	sum = 0;
180 	fsize = 0;
181 	while((n = read(x, (char *)Buf, sizeof(Buf))) > 0) {
182 		if (ignore_cr)
183 			n = cr_purge(Buf, n);
184 		fsize += n;
185 		sum = sum32(sum, Buf, n);
186 		}
187 	sum &= 0xffffffff;
188         if (n==0)
189 		printf("%s\t%lx\t%ld\n", s, sum & 0xffffffff, fsize);
190         else { perror(s); }
191 	close(x);
192 	}
193 
194 #ifdef KR_headers
main(argc,argv)195 main(argc, argv)
196  char **argv;
197 #else
198 main(int argc, char **argv)
199 #endif
200 {
201 	int x;
202 	char *s;
203 	static int rc;
204 
205 	progname = *argv;
206 	argc = argc;		/* turn off "not used" warning */
207 	s = *++argv;
208 	if (s && *s == '-') {
209 		switch(s[1]) {
210 			case '?':
211 				usage(0);
212 			case 'r':
213 				ignore_cr = 1;
214 			case '-':
215 				break;
216 			default:
217 				fprintf(stderr, "invalid option %s\n", s);
218 				usage(1);
219 			}
220 		s = *++argv;
221 		}
222 	if (s) do {
223 		x = open(s, O_RDONLY|O_BINARY);
224 		if (x < 0) {
225 			fprintf(stderr, "%s: can't open %s\n", progname, s);
226 			rc |= 1;
227 			}
228 		else
229 			process(s, x);
230 		}
231 		while(s = *++argv);
232 	else {
233 		process("/dev/stdin", fileno(stdin));
234 		}
235 	return rc;
236 	}
237