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