-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksumCalc.c
More file actions
52 lines (39 loc) · 1.19 KB
/
checksumCalc.c
File metadata and controls
52 lines (39 loc) · 1.19 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include "ocarina.h"
#define BUFFER_SIZE 4946
unsigned int uShort(unsigned char *data, int bufSize);
int main(int argc, char **argv){
//This makes sure that the comman is the right length
if(argc != 3){
printf("Invalid use!");
}
//Opens the file to read from
FILE *file = fopen(argv[1], "r");
//The save file into one buffer
unsigned char buffer[BUFFER_SIZE];
//Safe way to get string->hex->decimal for the offset
size_t offset = strtoul(argv[2], (char **)0, 16);
//Offsets the file
fseek(file, offset, SEEK_SET);
//Reads the file into the buffer
size_t size = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, file);
//Final value of the checksum
unsigned int checksum = uShort(buffer, size);
//This is anding the value to only get the 4 byte answer we need
checksum = checksum & 0x00ffff;
printf("0x%X\n", checksum); //4dc5
return 0;
}
unsigned int uShort(unsigned char *data, int bufSize){
int z;
unsigned int checksum = 0;
//Calculates the checksum
for(z = 0; z < bufSize - 1; z += 2){
//Adds the bytes in together
checksum += ((ushort)data[z] << 8) + data[z + 1];
}
return checksum;
}