5
const int MOD_ADLER = 65521;
7
uint64_t adler32(unsigned char *data, int32_t len) /* where data is the location of the data in physical memory and
8
len is the length of the data in bytes */
10
uint64_t a = 1, b = 0;
13
/* Process each byte of the data in order */
14
for (index = 0; index < len; ++index)
16
a = (a + data[index]) % MOD_ADLER;
17
b = (b + a) % MOD_ADLER;
23
int main(int argc, char* argv[]) {
27
fputs("Need 1 argument\n", stderr);
28
return (EXIT_FAILURE);
31
unsigned char *source = NULL;
32
FILE *fp = fopen(argv[1], "rb");
34
/* Go to the end of the file. */
35
if (fseek(fp, 0L, SEEK_END) == 0) {
36
/* Get the size of the file. */
38
if (bufsize == -1) { fputs("Couldn't get size\n", stderr); return (EXIT_FAILURE); }
40
/* Allocate our buffer to that size. */
41
source = malloc(sizeof(char) * (bufsize + 1));
42
if (source == NULL) { fputs("Couldn't allocate\n", stderr); return (EXIT_FAILURE); }
44
/* Go back to the start of the file. */
45
if (fseek(fp, 0L, SEEK_SET) == -1) { fputs("Couldn't seek\n", stderr); return (EXIT_FAILURE); }
47
/* Read the entire file into memory. */
48
size_t newLen = fread(source, sizeof(char), bufsize, fp);
50
fputs("Error reading file\n", stderr);
51
//return (EXIT_FAILURE);
53
source[++newLen] = '\0'; /* Just to be safe. */
56
fputs("Couldn't seek to end\n", stderr);
57
return (EXIT_FAILURE);
61
fputs("Couldn't open\n", stderr);
62
return (EXIT_FAILURE);
65
printf("%d\n", (uint32_t) adler32(source, bufsize));
67
free(source); /* Don't forget to call free() later! */
69
return (EXIT_SUCCESS);