/* prchksum.c - print checksum for a file. Originally coded by folks at Digi Corrected for byte-wide variables by Kees Cook Feb 13, 2001 # Copyright (C) 2001 Kees Cook # kees@outflux.net, http://outflux.net/ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public Licen # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211 # http://www.gnu.org/copyleft/gpl.html */ #include main(argc,argv) char **argv; { FILE *fp; int index; int wordflag=0; int c,v,sum,last,mask; index = 1; if(argc > 2) { if(argv[1][0] != '-') usage(); if(argv[1][1] != 'w') usage(); wordflag = 1; index = 2; } fp = fopen(argv[index],"rb"); if(fp == NULL) { fprintf(stderr,"Couldn't open file '%s'",argv[index]); perror(""); usage(); } sum=0; mask=wordflag ? 0xffff : 0x00ff; while(1) { v = fgetc(fp); if(v == EOF) break; if (wordflag) { v <<= 8; c = fgetc(fp); if(c == EOF) { printf("File ended on odd byte, can't do word checksum\n"); exit(1); } v += c; } last = v; sum += v; sum &= mask; } fclose(fp); if (wordflag) { printf("Word checksum = 0x%04x\nLast word = 0x%04x\n",sum,last); } else { printf("Byte checksum = 0x%02x\nLast byte = 0x%02x\n",sum,last); } } usage() { printf("\nprchksum [-w] filename\n"); printf("\nPrints checksum for file. Does not modify file."); printf("\n-w flag selects word mode\n"); exit(1); }