-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
57 lines (48 loc) · 1.04 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* =====================================================================================
*
* Filename: main.c
*
* Description: adler32 calc program
*
* Version: 1.0
* Created: 02/27/2014 12:22:10 PM
* Revision: none
* Compiler: gcc
*
* Author: Darwin
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
uint32_t fadler32(FILE *fp);
int main(int argc, char **argv)
{
if(argc < 2) {
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
exit(1);
}
FILE *fp;
fp = fopen(argv[1], "r");
if(fp != NULL) {
printf("Checksum for %s: %x\n", argv[1], fadler32(fp));
fclose(fp);
return 0;
}
else
fprintf(stderr, "Error: fopen() failed. (file doesn't exist?)\n");
return 1;
}
uint32_t fadler32(FILE *fp)
{
int i;
int c;
uint32_t a = 1, b = 0;
while((c = fgetc(fp)) != EOF) {
a = (a + c) % 65521;
b = (b + a) % 65521;
}
return (b << 16) | a;
}