home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / xsum.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  7KB  |  238 lines

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