[ b a c k ]
/********************************************************************
* cs.c (check sum) is a program that computes a 16 bit checksum over
* a file that is specified by the user. A 32 bit integer is used
* to accumulate the sum and the carry is added immediately back into
* the sum.
*
* Author: William Martin - boneshakerbike@hotmail.com
* Completed: Monday, February 05, 2001
* Dependencies: None - small test program!
* Input: Keyboard - file name (must be in current
* directory and < 30 chars.
* Output: The bytes read and the checksum value in Int.
*******************************************************************/
#include <stdio.h>
#include <stdlib.h> //for size_t, a type def.
/********************************************************************
* checksum is a function that computes the checksum of a buffer
* (of a file) and prints the total to the screen.
*
* Completed: Monday, February 05, 2001
* Dependencies: none.
* Input: pointer to a buffer containing the file
* characters (must be < 50000), size of the
* read(file), and pointer to file name.
* Output: The checksum value in int printed to screen.
* Sample Call: checksum(bufer, length_of_read, file_name);
*******************************************************************/
void checksum(void *buffer, size_t length_of_read, void *file){
unsigned char *bufer = (unsigned char *)buffer;
int check_sum_array[2];// holds 16bit(2 x 8) sum
unsigned int carry = 0;
size_t i;
for (i = 0; i < length_of_read; i = i + 2){
if (i == length_of_read + 1){// for odd bit left at EOF
check_sum_array[1] += (unsigned int)(*bufer++);
}else{// normal operations
check_sum_array[0] += (unsigned int)(*bufer++);
check_sum_array[1] += (unsigned int)(*bufer++);
}
do{ //like grade school math, add from right to left.
if(check_sum_array[1] > 255){
carry = check_sum_array[1] / 256;
check_sum_array[1] = check_sum_array[1] %256;
check_sum_array[0] += carry;// add carry back
carry = 0;// reset
}
if(check_sum_array[0] > 255){
carry = check_sum_array[0] / 256;
check_sum_array[0] = check_sum_array[0] % 256;
check_sum_array[1] += carry;// add carry back
carry = 0;// reset
}
}while ((check_sum_array[1] > 255) &&
(check_sum_array[0] > 255));
}
printf("The checksum of %s is %d%d\n",
file, check_sum_array[0], check_sum_array[1]);
}
/********************************************************************
* In the main program we get the name of the file, open it, read
* it into a buffer, do a checksum, then close the file.
*
* Completed: Monday, February 05, 2001
* Dependencies: checksum(buffer, size, file_name).
* Input: keyboard, the name of file.
* Output: The bytes read then checksum() is called.
*******************************************************************/
main(){
FILE *file_pointer; // file handle name
size_t length_of_read; // size of file
char bufer[50000]; // bufer to hold file contents
char file_name[30]; // file name
//get filename and open, error will be printed
printf("Enter file name to do checksum on ==> ");
scanf("%s", file_name);
if (NULL == (file_pointer = fopen(file_name, "rb")))
{
printf("Unable to open %s for reading\n", file_name);
return -1;
}
// read file and save length.
length_of_read = fread(bufer, sizeof(char), sizeof(bufer), file_pointer);
// report file size.
printf("%d bytes read.\n", length_of_read);
// call checksum function, close file, and end program!
checksum(bufer, length_of_read, file_name);
fclose(file_pointer);
return 0;
}
[ t o p ]