~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/checksummer.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdint.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
 
 
5
const int MOD_ADLER = 65521;
 
6
 
 
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 */
 
9
{
 
10
    uint64_t a = 1, b = 0;
 
11
    int32_t index;
 
12
 
 
13
    /* Process each byte of the data in order */
 
14
    for (index = 0; index < len; ++index)
 
15
    {
 
16
        a = (a + data[index]) % MOD_ADLER;
 
17
        b = (b + a) % MOD_ADLER;
 
18
    }
 
19
 
 
20
    return (b << 16) | a;
 
21
}
 
22
 
 
23
int main(int argc, char* argv[]) {
 
24
    long bufsize;
 
25
 
 
26
    if (argc != 2) {
 
27
        fputs("Need 1 argument\n", stderr);
 
28
        return (EXIT_FAILURE);
 
29
    }
 
30
 
 
31
    unsigned char *source = NULL;
 
32
    FILE *fp = fopen(argv[1], "rb");
 
33
    if (fp != NULL) {
 
34
        /* Go to the end of the file. */
 
35
        if (fseek(fp, 0L, SEEK_END) == 0) {
 
36
            /* Get the size of the file. */
 
37
            bufsize = ftell(fp);
 
38
            if (bufsize == -1) { fputs("Couldn't get size\n", stderr); return (EXIT_FAILURE); }
 
39
    
 
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); }
 
43
    
 
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); }
 
46
    
 
47
            /* Read the entire file into memory. */
 
48
            size_t newLen = fread(source, sizeof(char), bufsize, fp);
 
49
            if (newLen == 0) {
 
50
                fputs("Error reading file\n", stderr);
 
51
                //return (EXIT_FAILURE);
 
52
            } else {
 
53
                source[++newLen] = '\0'; /* Just to be safe. */
 
54
            }
 
55
        } else {
 
56
          fputs("Couldn't seek to end\n", stderr);
 
57
          return (EXIT_FAILURE);
 
58
        }
 
59
        fclose(fp);
 
60
    } else {
 
61
      fputs("Couldn't open\n", stderr);
 
62
      return (EXIT_FAILURE);
 
63
    }
 
64
 
 
65
    printf("%d\n", (uint32_t) adler32(source, bufsize));
 
66
    
 
67
    free(source); /* Don't forget to call free() later! */
 
68
 
 
69
    return (EXIT_SUCCESS);
 
70
 
 
71
}