Skip to content

Commit

Permalink
Improve code parsing (#23)
Browse files Browse the repository at this point in the history
* improve code parsing

* cli tools fix
  • Loading branch information
bucanero authored Mar 31, 2024
1 parent 97cc965 commit 1b734c8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/apollo.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void free_patch_var_list(void);
int apply_bsd_patch_code(const char* file_path, const code_entry_t* code);
int apply_ggenie_patch_code(const char* file_path, const code_entry_t* code);
int apply_cheat_patch_code(const char* file_path, const char* title_id, const code_entry_t* code, apollo_host_cb_t host_cb);
void load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_t get_files_cb, const char* save_path);
int load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_t get_files_cb, const char* save_path);


//--- Apollo encryption functions ---
Expand Down
43 changes: 26 additions & 17 deletions source/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#define LOG dbglogger_log


void remove_char(char * str, int len, char seek);


uint64_t x_to_u64(const char *hex)
{
uint64_t result, t;
Expand Down Expand Up @@ -98,6 +95,22 @@ int write_buffer(const char *file_path, const uint8_t *buf, size_t size)
return 0;
}

static void clean_eol(char * str)
{
for (int x = 0, len = strlen(str); x < len; x++)
if (strncmp(&str[x], "\r\n", 2) == 0)
str[x] = ' ';
else if (str[x] == '\r')
str[x] = '\n';
}

static void remove_char(char * str, int len, char seek)
{
for (int x = 0; x < len; x++)
if (str[x] == seek)
str[x] = '\n';
}

/*
* Function: str_ends_with()
* File: saves.c
Expand Down Expand Up @@ -155,9 +168,8 @@ static void get_patch_code(char* buffer, int code_id, code_entry_t* entry)
wildcard_match_icase(line, "GROUP:*")) && (i++ == code_id))
{
LOG("Reading patch code for '%s'...", line);
line = strtok(NULL, "\n");

while (line)
for (line = strtok(NULL, "\n"); line != NULL; line = strtok(NULL, "\n"))
{
if ((wildcard_match(line, "; --- * ---")) ||
(wildcard_match(line, ":*")) ||
Expand Down Expand Up @@ -204,38 +216,37 @@ static void get_patch_code(char* buffer, int code_id, code_entry_t* entry)
}

}
line = strtok(NULL, "\n");
}
}
if (i > code_id || !line)
break;

line = strtok(NULL, "\n");
}

// LOG("Result (%s)", res);
entry->codes = res;
}

void load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_t get_files_opt, const char* save_path)
int load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_t get_files_opt, const char* save_path)
{
int code_count = 0;
code_entry_t * code;
char * file_opt = NULL;
char filePath[256] = "";
char group = 0;
size_t bufferLen = strlen(buffer);
size_t bufferLen = strlen(buffer);
list_node_t* node = list_tail(list_codes);

remove_char(buffer, bufferLen, '\r');

char *line = strtok(buffer, "\n");

while (line)
clean_eol(buffer);
for (char *line = strtok(buffer, "\n"); line != NULL; line = strtok(NULL, "\n"))
{
str_rtrim(line);
if (wildcard_match(line, ":*"))
{
char* tmp_mask;

strcpy(filePath, line+1);
strncpy(filePath, line+1, sizeof(filePath)-1);
LOG("FILE: %s\n", filePath);

if (strrchr(filePath, '\\'))
Expand Down Expand Up @@ -325,8 +336,6 @@ void load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_
end = str_ends_with(code->name, " ---");
if (end) *end = 0;
}

line = strtok(NULL, "\n");
}

while ((node = list_next(node)) != NULL)
Expand All @@ -342,5 +351,5 @@ void load_patch_code_list(char* buffer, list_t* list_codes, apollo_get_files_cb_
LOG("[%d] Name: %s\nFile: %s\nCode (%d): %s\n", code_count, code->name, code->file, code->type, code->codes);
}

return;
return code_count;
}
40 changes: 13 additions & 27 deletions source/patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ static list_t* var_list = NULL;
static apollo_host_cb_t host_callback = NULL;


void remove_char(char * str, int len, char seek)
{
for (int x = 0; x < len; x++)
if (str[x] == seek)
str[x] = '\n';
}

static long search_data(const char* data, size_t size, int start, const char* search, int len, int count)
{
int k = 1;
Expand Down Expand Up @@ -282,16 +275,14 @@ static void swap_u64_data(uint64_t* data, int count)

int apply_bsd_patch_code(const char* filepath, const code_entry_t* code)
{
char *data;
char *data, *bsd_code;
size_t dsize;
long pointer = 0;
long range_start = 0, range_end = 0;
uint8_t carry = 0, eof = 0;
uint32_t old_val = 0;
custom_crc_t custom_crc = {0,0,0,0,0,0};
int codelen = strlen(code->codes);
char *line = strtok(code->codes, "\n");


LOG("Applying [%s] to '%s'...", code->name, filepath);
if (read_buffer(filepath, (uint8_t**) &data, &dsize) != SUCCESS)
{
Expand All @@ -303,8 +294,9 @@ int apply_bsd_patch_code(const char* filepath, const code_entry_t* code)
if (!var_list)
var_list = list_alloc();

while (line)
{
bsd_code = strdup(code->codes);
for (char *line = strtok(bsd_code, "\n"); line != NULL; line = strtok(NULL, "\n"))
{
// carry(*)
if (wildcard_match_icase(line, "carry(*)"))
{
Expand Down Expand Up @@ -1632,7 +1624,7 @@ int apply_bsd_patch_code(const char* filepath, const code_entry_t* code)
}

LOG("[%s]:read(0x%X , 0x%X)", var->name, read_s, read_l);
_log_dump("read()", var->data, var->len);
_log_dump("read()", (uint8_t*) read, var->len);
}

// set [*]:right(*,*)*
Expand Down Expand Up @@ -2707,39 +2699,35 @@ int apply_bsd_patch_code(const char* filepath, const code_entry_t* code)
}

}

line = strtok(NULL, "\n");
}

write_buffer(filepath, (uint8_t*) data, dsize);

bsd_end:
free(data);
// remove 0x00 from previous strtok(...)
remove_char(code->codes, codelen, '\0');
free(bsd_code);

return (dsize);
}

int apply_ggenie_patch_code(const char* filepath, const code_entry_t* code)
{
char *data;
char *data, *gg_code;
size_t dsize;
long pointer = 0, end_pointer = 0;
uint32_t ptr_value = 0;
char tmp3[4], tmp4[5], tmp6[7], tmp8[9];
int codelen = strlen(code->codes);
char *line = strtok(code->codes, "\n");


LOG("Applying [%s] to '%s'...", code->name, filepath);
if (read_buffer(filepath, (uint8_t**) &data, &dsize) != SUCCESS)
{
LOG("Can't load file '%s'", filepath);
return 0;
}

while (line)
{
gg_code = strdup(code->codes);
for (char *line = strtok(gg_code, "\n"); line != NULL; line = strtok(NULL, "\n"))
{
switch (line[0])
{
case '0':
Expand Down Expand Up @@ -3662,14 +3650,12 @@ int apply_ggenie_patch_code(const char* filepath, const code_entry_t* code)
default:
break;
}
line = strtok(NULL, "\n");
}

write_buffer(filepath, (uint8_t*) data, dsize);

free(data);
// remove 0x00 from previous strtok(...)
remove_char(code->codes, codelen, '\0');
free(gg_code);

return (dsize);
}
Expand Down
2 changes: 2 additions & 0 deletions tools/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ int main(int argc, char **argv)
printf("[*] Could Not Access The File (%s)\n", argv[1]);
return -1;
}
data = realloc(data, len+1);
data[len] = 0;

code_entry_t* code = calloc(1, sizeof(code_entry_t));
code->name = argv[1];
Expand Down
2 changes: 2 additions & 0 deletions tools/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ int main(int argc, char **argv)
printf("[*] Could Not Access The File (%s)\n", argv[1]);
return -1;
}
data = realloc(data, len+1);
data[len] = 0;

code_entry_t* code = calloc(1, sizeof(code_entry_t));
code->name = argv[1];
Expand Down

0 comments on commit 1b734c8

Please sign in to comment.