-
Notifications
You must be signed in to change notification settings - Fork 0
/
adler32.c
67 lines (54 loc) · 1.12 KB
/
adler32.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
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
//#define __debug__
#define BUFFLEN 128
static int adler32(FILE *fp, uint32_t *result);
int main(int argc, char **argv)
{
FILE *adler;
uint32_t res;
char buffer[BUFFLEN];
if(argc != 2){
fprintf(stderr, "please give a stream or file for input.\n");
exit(EXIT_FAILURE);
}
adler = fopen(argv[1], "r");
if(adler == NULL){
snprintf(buffer, BUFFLEN, "%s", argv[1]);
perror(buffer);
exit(EXIT_FAILURE);
}
if(adler32(adler, &res) != 0){
fprintf(stderr, "adler32 checksum failure.\n");
exit(EXIT_FAILURE);
}
printf("adler32 checksum for %s is %#x\n", argv[1], res);
return 0;
}
static int adler32(FILE *adler, uint32_t *result)
{
int c;
uint32_t A;
uint32_t B;
if(adler == NULL){
fprintf(stderr, "invalid function parameter.\n");
return -1;
}
A = 1;
B = 0;
while((c = fgetc(adler)) != EOF){
#ifdef __debug__
printf("c: %u\t\t\tA: %u\t\t\tB:%u\n", c, A, B);
#endif
A = (A + c) % 65521;
B = (A + B) % 65521;
}
if(!feof(adler)){
perror("[!] adler32 check failure");
return -1;
}
*result = (B << 16) | A;
return 0;
}
//0x4a3bfc25