https://bitbucket.org/daniel_fort/magic-lantern
Raw File
Tip revision: ef33f4f8629c2d9727272a704c7d05f1d9cd26ac authored by a1ex on 18 April 2016, 19:53:28 UTC
Close branch play_mode
Tip revision: ef33f4f
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