Source

adler32 / adler32.c

Full commit
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
uint32_t adler32(const void *buf, size_t buflength) {

    const uint8_t *buffer = (const uint8_t*)buf;

    uint32_t s1 = 1;
    uint32_t s2 = 0;

    size_t n;
    for (n = 0; n < buflength; n++) {
        s1 = (s1 + buffer[n]) % 65521;
        s2 = (s2 + s1) % 65521;
    }

    return (s2 << 16) | s1;
}

int main ( int argc, char *argv[] )
{
    if ( argc != 2 )
    {
        printf("Usage: %s [filename]", argv[0]);
    }
    else
    {
        FILE *file = fopen( argv[1], "rb" );
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int fileSize;
            char *data;
            uint32_t checksum;
            
            fseek(file, 0, SEEK_END);
            fileSize = ftell(file);
            rewind(file);
            
            data = (char*) calloc(sizeof(char), fileSize);
            fread(data, 1, fileSize, file);
            
            checksum = adler32(data, fileSize);

            printf("%i", checksum);
            
            fclose(file);
        }
    }
}