summaryrefslogtreecommitdiff
path: root/adler32.c
blob: 7a1a5e7dbb99283dffeca927e4b3cb2e5f1e8834 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * adler32.c (c) 2006 Bjorn Lindeijer
 * License: GPL, v2 or later
 *
 * Calculates Adler-32 checksums for all files passed as argument.
 *
 *  Usage: adler32 [file]...
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
//#include <zlib.h>

const uint32_t MOD_ADLER = 65521;

uint32_t adler32(unsigned char *data, size_t len)
/*
    where data is the location of the data in physical memory and
    len is the length of the data in bytes
*/
{
    uint32_t a = 1, b = 0;
    size_t index;

    // Process each byte of the data in order
    for (index = 0; index < len; ++index)
    {
        a = (a + data[index]) % MOD_ADLER;
        b = (b + a) % MOD_ADLER;
    }

    return (b << 16) | a;
}


/**
 * Calculates the Adler-32 checksum for the given file.
 */
unsigned long fadler32(FILE *file)
{
    // Obtain file size
    fseek(file, 0, SEEK_END);
    long fileSize = ftell(file);
    rewind(file);

    // Calculate Adler-32 checksum
    unsigned char *buffer = (unsigned char*) malloc(fileSize);
    fread(buffer, 1, fileSize, file);
    //unsigned long adler = adler32(0L, Z_NULL, 0);
    //adler = adler32(adler, (Bytef*) buffer, fileSize);
    unsigned long adler = adler32(buffer, fileSize);
    free(buffer);

    return adler;
}

/**
 * Prints out usage and exists.
 */
void print_usage()
{
    printf("Usage: adler32 mode [file]...\n");
    exit(0);
}

int main(int argc, char *argv[])
{
    int i;                            /**< Loops through arguments. */
    if (argc < 2)
    {
        print_usage();
    }
    int mode = atoi(argv[1]);
    for (i = 2; i < argc; ++i)
    {
        FILE *file = fopen(argv[i], "r");
        if (!file)
        {
            printf("Error while opening '%s' for reading!\n", argv[i]);
            exit(1);
        }

        unsigned long adler = fadler32(file);
        switch (mode)
        {
            case 0:
            default:
                printf("%s %lx\n", argv[i], adler);
                break;
            case 1:
                printf("%lx", adler);
                break;
        }
        fclose(file);
    }
    return 0;
}