https://bitbucket.org/hudson/magic-lantern
Raw File
Tip revision: 8254fe0b9c1703b20d1755f40012752d07ceec41 authored by a1ex on 19 June 2016, 18:03:04 UTC
Close branch dnlit/constsh-edited-part-1-of-fix-for-2520-1461182905684.
Tip revision: 8254fe0
xor_chk.c

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


int main (int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("Invalid parameter count (%d)\n", argc);
        return -1;
    }
    FILE *f = fopen(argv[1], "rb+");
    
    if(!f)
    {
        printf("Failed to open file\n");
        return -1;
    }
    
    uint32_t checksum = 0;
    uint32_t data = 0;
    uint32_t read = 0;
    
    /* make sure we read from the beginning */
    fseek(f, 0, SEEK_SET);
    
    while(!feof(f))
    {
        if(fread(&data, 4, 1, f) != 1)
        {
            break;
        }
        
        checksum ^= data;
        read++;
    }

    /* modify checksum */
    data ^= checksum;
    
    fseek(f, -4, SEEK_END);
   
    if(fwrite(&data, 4, 1, f) != 1)
    {
        printf("Failed to write\n");
        return -1;
    }
    
    fclose(f);
    
    return 0;
}
back to top