Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define FILE_HEADER_INFO "Full Info:\n"\
- "\tMagic number:\t\t0x%08x\n" \
- "\tHeader checksum:\t0x%08x\n" \
- "\tHeader length:\t\t0x%08x\n" \
- "\tFiles number:\t\t0x%08x\n" \
- "\tLanguache:\t\t0x%08x\n" \
- "\tDevice class:\t\t0x%08x\n"
- #define FILE_ITEM_HEADER_INFO "\tFile name:\t\t%s\n" \
- "\tStart:\t\t\t0x%08x\n" \
- "\tLength:\t\t\t0x%08x\n" \
- "\tChecksum:\t\t0x%08x\n"
- typedef struct {
- uint32_t magic_namber;
- uint32_t header_checksum;
- uint32_t total_length;
- uint32_t file_number;
- uint32_t languache;
- uint32_t device_class;
- } file_header_t;
- typedef struct {
- char file_name[32];
- uint32_t file_start;
- uint32_t file_length;
- uint32_t checksum;
- } file_item_header_t;
- char *hexlify(char *str) {
- int l,i;
- char *t;
- l = strlen(str)*2;
- t = malloc(l);
- if (t) {
- for(i=0; i<l; i++) {
- sprintf(t+2*i, "%02x", str[i]);
- }
- return t;
- }
- return NULL;
- }
- char fromhex(char c) {
- if (isxdigit(c)) {
- if (isdigit(c)) {
- c -= '0';
- } else {
- c = tolower(c);
- c = c - 'a' + 10;
- }
- } else { c = 0; }
- return c;
- }
- char *unhexlify(char *hstr) {
- int l, i;
- char *t;
- char c;
- l = strlen(hstr)/2;
- t = malloc(l);
- if (t) {
- for(i=0; i<l; i++) {
- c = fromhex(hstr[2*i+1]) + 16 * fromhex(hstr[2*i]);
- t[i] = c;
- }
- }
- return t;
- }
- char *get_key(char* short_key) {
- int short_key_len = strlen(short_key);
- char *key = calloc((16 * short_key_len + 1), sizeof(char));
- int i, j;
- char first;
- // The longer key just rotates the short_key left by 1 byte each time
- for (i = 0; i < short_key_len; i++) {
- strncat(key, short_key, short_key_len);
- first = short_key[0];
- for (j = 0; j < short_key_len - 1; j++)
- short_key[j] = short_key[j + 1];
- short_key[short_key_len - 1] = first;
- }
- return key;
- }
- void* decode(void *data, size_t data_length, const char *key) {
- char c1, c2;
- int pos = 0;
- // XOR decode the header
- while (pos != data_length) {
- c1 = ((char*)data)[pos];
- c2 = key[pos];
- ((char*)data)[pos] = c1 ^ c2;
- pos++;
- }
- return data;
- }
- int main(int argc, char const *argv[]) {
- char *short_key = unhexlify("BACDBCFED6CADDD3BAB9A3ABBFCBB5BE");
- //char *short_key = unhexlify("68656C6C6F20776F726C64");
- int i, j, index,
- write_to_file = 0,
- file_header_length = sizeof(file_header_t),
- file_item_header_length = sizeof(file_item_header_t);
- char *data, *file_item,
- *key = get_key(short_key);
- char answer;
- uint32_t calced_checksum;
- file_item_header_t *file_item_header = malloc(file_item_header_length);
- file_header_t *file_header = calloc(1, sizeof(file_header_t));
- FILE *file = fopen(argv[1], "r+");
- fread(file_header, sizeof(file_header_t), 1, file);
- file_header = (file_header_t*)decode(file_header, file_header_length, key);
- printf(FILE_HEADER_INFO, file_header->magic_namber, file_header->header_checksum,
- file_header->total_length, file_header->file_number, file_header->languache,
- file_header->device_class);
- fseek(file, 0, SEEK_SET);
- data = malloc(file_header->total_length * sizeof(char));
- if (NULL == data) return -1;
- fread(data, sizeof(char), file_header->total_length, file);
- data = (char*)decode(data, file_header->total_length, key);
- // Header checksum is just a byte sum from the checksum to length
- calced_checksum = 0;
- for (i = 11; i < file_header->total_length; i++)
- calced_checksum += (unsigned char)data[i];
- printf("\tCalced checksum:\t0x%08x\n\n", calced_checksum);
- index = 64;
- for (i = 0; i < file_header->file_number; i++) {
- printf("Information about file part #%i\n", i);
- memcpy(file_item_header, (data + index), file_item_header_length);
- printf(FILE_ITEM_HEADER_INFO, file_item_header->file_name,
- file_item_header->file_start, file_item_header->file_length,
- file_item_header->checksum);
- file_item = malloc(file_item_header->file_length);
- fseek(file, file_item_header->file_start, SEEK_SET);
- fread(file_item, sizeof(char), file_item_header->file_length, file);
- calced_checksum = 0;
- for (j = 0; j < file_item_header->file_length; j++)
- calced_checksum += (unsigned char)file_item[j];
- printf("\tCalced checksum:\t0x%08x\n\n", calced_checksum);
- if (calced_checksum != file_item_header->checksum) {
- printf("Checksums of file item #%i and it's header checksum mismatch. Correct checksum in header? (y/n): ", i);
- answer = getchar();
- if ('y' == answer) {
- write_to_file = 1;
- printf("Replacing checksum in file item header (0x%08x) with 0x%08x\n", file_item_header->checksum, calced_checksum);
- file_item_header->checksum = calced_checksum;
- memcpy((data + index), file_item_header, file_item_header_length);
- printf("Done.\n");
- }
- else
- printf("Continue.\n");
- }
- free(file_item);
- index += file_item_header_length;
- }
- if (write_to_file) {
- printf("Save changes to file.\n");
- fseek(file, 0, SEEK_SET);
- data = (char*)decode(data, file_header->total_length, key);
- fwrite(data, sizeof(char), file_header->total_length, file);
- }
- free(data);
- free(key);
- free(short_key);
- fclose(file);
- printf("Everything done.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement