@@ -15,8 +15,9 @@ | |||||
*/ | */ | ||||
bool parse_bool(bool *result, const ASMLine *line, const char *directive, bool auto_val) | bool parse_bool(bool *result, const ASMLine *line, const char *directive, bool auto_val) | ||||
{ | { | ||||
const char *arg = line->data + (DIRECTIVE_OFFSET(line, directive) + 1); | |||||
ssize_t len = line->length - (DIRECTIVE_OFFSET(line, directive) + 1); | |||||
size_t offset = DIRECTIVE_OFFSET(line, directive) + 1; | |||||
const char *arg = line->data + offset; | |||||
ssize_t len = line->length - offset; | |||||
if (len <= 0 || len > 5) | if (len <= 0 || len > 5) | ||||
return false; | return false; | ||||
@@ -16,6 +16,8 @@ | |||||
#include "../logging.h" | #include "../logging.h" | ||||
#include "../util.h" | #include "../util.h" | ||||
#define MAX_REGION_SIZE 32 | |||||
/* Helper defines for preprocess() */ | /* Helper defines for preprocess() */ | ||||
#define SAVE_ARG(line, first, oldval, newval) \ | #define SAVE_ARG(line, first, oldval, newval) \ | ||||
@@ -283,8 +285,23 @@ static ErrorInfo* build_asm_lines( | |||||
*/ | */ | ||||
static bool parse_region_string(uint8_t *result, const ASMLine *line) | static bool parse_region_string(uint8_t *result, const ASMLine *line) | ||||
{ | { | ||||
char buffer[32]; // Longest region ("GG International") is 17 bytes | |||||
char buffer[MAX_REGION_SIZE]; | |||||
size_t offset = DIRECTIVE_OFFSET(line, DIR_ROM_REGION) + 1; | |||||
const char *arg = line->data + offset; | |||||
ssize_t len = line->length - offset; | |||||
if (len <= 2 || len >= MAX_REGION_SIZE + 2) // Account for double quotes | |||||
return false; | |||||
if (arg[0] != '"' || arg[len - 1] != '"') | |||||
return false; | |||||
strncpy(buffer, arg + 1, len - 2); | |||||
buffer[len - 2] = '\0'; | |||||
uint8_t code = region_string_to_code(buffer); | |||||
if (code) | |||||
return (*result = code), true; | |||||
return false; | return false; | ||||
} | } | ||||
@@ -73,19 +73,19 @@ const char* region_code_to_string(uint8_t code) | |||||
*/ | */ | ||||
uint8_t region_string_to_code(const char *name) | uint8_t region_string_to_code(const char *name) | ||||
{ | { | ||||
if (!strcmp(name, "SMS ")) { | |||||
if (!strncmp(name, "SMS ", 4)) { | |||||
name += 4; | name += 4; | ||||
if (strcmp(name, "Japan")) | |||||
if (!strcmp(name, "Japan")) | |||||
return 3; | return 3; | ||||
if (strcmp(name, "Export")) | |||||
if (!strcmp(name, "Export")) | |||||
return 4; | return 4; | ||||
} else if (!strcmp(name, "GG ")) { | |||||
} else if (!strncmp(name, "GG ", 3)) { | |||||
name += 3; | name += 3; | ||||
if (strcmp(name, "Japan")) | |||||
if (!strcmp(name, "Japan")) | |||||
return 5; | return 5; | ||||
if (strcmp(name, "Export")) | |||||
if (!strcmp(name, "Export")) | |||||
return 6; | return 6; | ||||
if (strcmp(name, "International")) | |||||
if (!strcmp(name, "International")) | |||||
return 7; | return 7; | ||||
} | } | ||||
return 0; | return 0; | ||||