Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/checksummer.c
4128 views
1
/*
2
* Copyright 2012 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <stdint.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11
12
const int MOD_ADLER = 65521;
13
14
uint64_t adler32(unsigned char *data, int32_t len) /* where data is the location of the data in physical memory and
15
len is the length of the data in bytes */
16
{
17
uint64_t a = 1, b = 0;
18
int32_t index;
19
20
/* Process each byte of the data in order */
21
for (index = 0; index < len; ++index)
22
{
23
a = (a + data[index]) % MOD_ADLER;
24
b = (b + a) % MOD_ADLER;
25
}
26
27
return (b << 16) | a;
28
}
29
30
int main(int argc, char* argv[]) {
31
long bufsize;
32
33
if (argc != 2) {
34
fputs("Need 1 argument\n", stderr);
35
return (EXIT_FAILURE);
36
}
37
38
unsigned char *source = NULL;
39
FILE *fp = fopen(argv[1], "rb");
40
if (fp != NULL) {
41
/* Go to the end of the file. */
42
if (fseek(fp, 0L, SEEK_END) == 0) {
43
/* Get the size of the file. */
44
bufsize = ftell(fp);
45
if (bufsize == -1) { fputs("Couldn't get size\n", stderr); return (EXIT_FAILURE); }
46
47
/* Allocate our buffer to that size. */
48
source = malloc(sizeof(char) * (bufsize + 1));
49
if (source == NULL) { fputs("Couldn't allocate\n", stderr); return (EXIT_FAILURE); }
50
51
/* Go back to the start of the file. */
52
if (fseek(fp, 0L, SEEK_SET) == -1) { fputs("Couldn't seek\n", stderr); return (EXIT_FAILURE); }
53
54
/* Read the entire file into memory. */
55
size_t newLen = fread(source, sizeof(char), bufsize, fp);
56
if (newLen == 0) {
57
fputs("Error reading file\n", stderr);
58
//return (EXIT_FAILURE);
59
} else {
60
source[++newLen] = '\0'; /* Just to be safe. */
61
}
62
} else {
63
fputs("Couldn't seek to end\n", stderr);
64
return (EXIT_FAILURE);
65
}
66
fclose(fp);
67
} else {
68
fputs("Couldn't open\n", stderr);
69
return (EXIT_FAILURE);
70
}
71
72
printf("%u\n", (uint32_t) adler32(source, bufsize));
73
74
free(source); /* Don't forget to call free() later! */
75
76
return (EXIT_SUCCESS);
77
}
78
79