From 648198353f0bbefc2d1192e621499e72f817c40b Mon Sep 17 00:00:00 2001 From: Jamiras Date: Mon, 16 Oct 2023 08:15:41 -0600 Subject: [PATCH] add explicitly sized fields for addresses/values/ids --- src/rc_libretro.c | 63 ++++++++++++++++-------------- src/rc_util.c | 16 ++++---- src/rc_util.h | 15 +++---- src/rcheevos/rc_internal.h | 2 - src/rcheevos/runtime_progress.c | 69 +++++++++++++++++---------------- 5 files changed, 84 insertions(+), 81 deletions(-) diff --git a/src/rc_libretro.c b/src/rc_libretro.c index eab44ef1..c2ab4192 100644 --- a/src/rc_libretro.c +++ b/src/rc_libretro.c @@ -676,46 +676,49 @@ void rc_libretro_hash_set_init(struct rc_libretro_hash_set_t* hash_set, rc_file_seek(file_handle, 0, SEEK_SET); m3u_contents = (char*)malloc((size_t)file_len + 1); - rc_file_read(file_handle, m3u_contents, (int)file_len); - m3u_contents[file_len] = '\0'; - - rc_file_close(file_handle); - - ptr = m3u_contents; - do + if (m3u_contents) { - /* ignore whitespace */ - while (isspace((int)*ptr)) - ++ptr; + rc_file_read(file_handle, m3u_contents, (int)file_len); + m3u_contents[file_len] = '\0'; + + rc_file_close(file_handle); - if (*ptr == '#') + ptr = m3u_contents; + do { - /* ignore comment unless it's the special SAVEDISK extension */ - if (memcmp(ptr, "#SAVEDISK:", 10) == 0) + /* ignore whitespace */ + while (isspace((int)*ptr)) + ++ptr; + + if (*ptr == '#') { - /* get the path to the save disk from the frontend, assign it a bogus hash so - * it doesn't get hashed later */ - if (get_image_path(index, image_path, sizeof(image_path))) + /* ignore comment unless it's the special SAVEDISK extension */ + if (memcmp(ptr, "#SAVEDISK:", 10) == 0) { - const char save_disk_hash[33] = "[SAVE DISK]"; - rc_libretro_hash_set_add(hash_set, image_path, -1, save_disk_hash); - ++index; + /* get the path to the save disk from the frontend, assign it a bogus hash so + * it doesn't get hashed later */ + if (get_image_path(index, image_path, sizeof(image_path))) + { + const char save_disk_hash[33] = "[SAVE DISK]"; + rc_libretro_hash_set_add(hash_set, image_path, -1, save_disk_hash); + ++index; + } } } - } - else - { - /* non-empty line, tally a file */ - ++index; - } + else + { + /* non-empty line, tally a file */ + ++index; + } - /* find the end of the line */ - while (*ptr && *ptr != '\n') - ++ptr; + /* find the end of the line */ + while (*ptr && *ptr != '\n') + ++ptr; - } while (*ptr); + } while (*ptr); - free(m3u_contents); + free(m3u_contents); + } if (hash_set->entries_count > 0) { diff --git a/src/rc_util.c b/src/rc_util.c index b747272f..982a635e 100644 --- a/src/rc_util.c +++ b/src/rc_util.c @@ -49,7 +49,7 @@ void rc_buffer_destroy(rc_buffer_t* buffer) #endif } -char* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount) +uint8_t* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount) { rc_buffer_chunk_t* chunk = &buffer->chunk; size_t remaining; @@ -70,9 +70,9 @@ char* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount) if (!chunk->next) break; - chunk->next->start = (char*)chunk->next + chunk_header_size; + chunk->next->start = (uint8_t*)chunk->next + chunk_header_size; chunk->next->write = chunk->next->start; - chunk->next->end = (char*)chunk->next + alloc_size; + chunk->next->end = (uint8_t*)chunk->next + alloc_size; chunk->next->next = NULL; } @@ -82,7 +82,7 @@ char* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount) return NULL; } -void rc_buffer_consume(rc_buffer_t* buffer, const char* start, char* end) +void rc_buffer_consume(rc_buffer_t* buffer, const uint8_t* start, uint8_t* end) { rc_buffer_chunk_t* chunk = &buffer->chunk; do @@ -104,14 +104,14 @@ void rc_buffer_consume(rc_buffer_t* buffer, const char* start, char* end) void* rc_buffer_alloc(rc_buffer_t* buffer, size_t amount) { - char* ptr = rc_buffer_reserve(buffer, amount); + uint8_t* ptr = rc_buffer_reserve(buffer, amount); rc_buffer_consume(buffer, ptr, ptr + amount); return (void*)ptr; } char* rc_buffer_strncpy(rc_buffer_t* buffer, const char* src, size_t len) { - char* dst = rc_buffer_reserve(buffer, len + 1); + char* dst = (char*)rc_buffer_reserve(buffer, len + 1); memcpy(dst, src, len); dst[len] = '\0'; rc_buffer_consume(buffer, dst, dst + len + 2); @@ -133,9 +133,9 @@ void rc_format_md5(char checksum[33], const unsigned char digest[16]) ); } -unsigned rc_djb2(const char* input) +uint32_t rc_djb2(const char* input) { - unsigned result = 5381; + uint32_t result = 5381; char c; while ((c = *input++) != '\0') diff --git a/src/rc_util.h b/src/rc_util.h index 51b1d330..28475c85 100644 --- a/src/rc_util.h +++ b/src/rc_util.h @@ -2,6 +2,7 @@ #define RC_UTIL_H #include +#include #ifdef __cplusplus extern "C" { @@ -12,11 +13,11 @@ extern "C" { */ typedef struct rc_buffer_chunk_t { /* The current location where data is being written */ - char* write; + uint8_t* write; /* The first byte past the end of data where writing cannot occur */ - char* end; + uint8_t* end; /* The first byte of the data */ - char* start; + uint8_t* start; /* The next block in the allocated memory chain */ struct rc_buffer_chunk_t* next; } @@ -29,19 +30,19 @@ typedef struct rc_buffer_t { /* The chunk data (will point at the local data member) */ struct rc_buffer_chunk_t chunk; /* Small chunk of memory pre-allocated for the chunk */ - char data[256]; + uint8_t data[256]; } rc_buffer_t; void rc_buffer_init(rc_buffer_t* buffer); void rc_buffer_destroy(rc_buffer_t* buffer); -char* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount); -void rc_buffer_consume(rc_buffer_t* buffer, const char* start, char* end); +uint8_t* rc_buffer_reserve(rc_buffer_t* buffer, size_t amount); +void rc_buffer_consume(rc_buffer_t* buffer, const uint8_t* start, uint8_t* end); void* rc_buffer_alloc(rc_buffer_t* buffer, size_t amount); char* rc_buffer_strcpy(rc_buffer_t* buffer, const char* src); char* rc_buffer_strncpy(rc_buffer_t* buffer, const char* src, size_t len); -unsigned rc_djb2(const char* input); +uint32_t rc_djb2(const char* input); void rc_format_md5(char checksum[33], const unsigned char digest[16]); diff --git a/src/rcheevos/rc_internal.h b/src/rcheevos/rc_internal.h index 03931e51..1c549407 100644 --- a/src/rcheevos/rc_internal.h +++ b/src/rcheevos/rc_internal.h @@ -135,8 +135,6 @@ void* rc_alloc(void* pointer, int* offset, int size, int alignment, rc_scratch_t void* rc_alloc_scratch(void* pointer, int* offset, int size, int alignment, rc_scratch_t* scratch, int scratch_object_pointer_offset); char* rc_alloc_str(rc_parse_state_t* parse, const char* text, int length); -unsigned rc_djb2(const char* input); - rc_memref_t* rc_alloc_memref(rc_parse_state_t* parse, uint32_t address, uint8_t size, uint8_t is_indirect); int rc_parse_memref(const char** memaddr, uint8_t* size, uint32_t* address); void rc_update_memref_values(rc_memref_t* memref, rc_peek_t peek, void* ud); diff --git a/src/rcheevos/runtime_progress.c b/src/rcheevos/runtime_progress.c index 32353faa..d1e639df 100644 --- a/src/rcheevos/runtime_progress.c +++ b/src/rcheevos/runtime_progress.c @@ -1,6 +1,7 @@ #include "rc_runtime.h" #include "rc_internal.h" +#include "../rc_util.h" #include "../rhash/md5.h" #include @@ -19,10 +20,10 @@ typedef struct rc_runtime_progress_t { const rc_runtime_t* runtime; - int offset; - unsigned char* buffer; + uint32_t offset; + uint8_t* buffer; - int chunk_size_offset; + uint32_t chunk_size_offset; lua_State* L; } rc_runtime_progress_t; @@ -39,7 +40,7 @@ typedef struct rc_runtime_progress_t { #define RC_COND_FLAG_OPERAND2_IS_INDIRECT_MEMREF 0x00100000 #define RC_COND_FLAG_OPERAND2_MEMREF_CHANGED_THIS_FRAME 0x00200000 -static void rc_runtime_progress_write_uint(rc_runtime_progress_t* progress, unsigned value) +static void rc_runtime_progress_write_uint(rc_runtime_progress_t* progress, uint32_t value) { if (progress->buffer) { progress->buffer[progress->offset + 0] = value & 0xFF; value >>= 8; @@ -51,9 +52,9 @@ static void rc_runtime_progress_write_uint(rc_runtime_progress_t* progress, unsi progress->offset += 4; } -static unsigned rc_runtime_progress_read_uint(rc_runtime_progress_t* progress) +static uint32_t rc_runtime_progress_read_uint(rc_runtime_progress_t* progress) { - unsigned value = progress->buffer[progress->offset + 0] | + uint32_t value = progress->buffer[progress->offset + 0] | (progress->buffer[progress->offset + 1] << 8) | (progress->buffer[progress->offset + 2] << 16) | (progress->buffer[progress->offset + 3] << 24); @@ -119,7 +120,7 @@ static void rc_runtime_progress_init(rc_runtime_progress_t* progress, const rc_r static int rc_runtime_progress_write_memrefs(rc_runtime_progress_t* progress) { rc_memref_t* memref = progress->runtime->memrefs; - unsigned int flags = 0; + uint32_t flags = 0; rc_runtime_progress_start_chunk(progress, RC_RUNTIME_CHUNK_MEMREFS); @@ -150,9 +151,9 @@ static int rc_runtime_progress_write_memrefs(rc_runtime_progress_t* progress) static int rc_runtime_progress_read_memrefs(rc_runtime_progress_t* progress) { - unsigned entries; - unsigned address, flags, value, prior; - char size; + uint32_t entries; + uint32_t address, flags, value, prior; + uint8_t size; rc_memref_t* memref; rc_memref_t* first_unmatched_memref = progress->runtime->memrefs; @@ -207,7 +208,7 @@ static int rc_runtime_progress_is_indirect_memref(rc_operand_t* oper) static int rc_runtime_progress_write_condset(rc_runtime_progress_t* progress, rc_condset_t* condset) { rc_condition_t* cond; - unsigned flags; + uint32_t flags; rc_runtime_progress_write_uint(progress, condset->is_paused); @@ -251,7 +252,7 @@ static int rc_runtime_progress_write_condset(rc_runtime_progress_t* progress, rc static int rc_runtime_progress_read_condset(rc_runtime_progress_t* progress, rc_condset_t* condset) { rc_condition_t* cond; - unsigned flags; + uint32_t flags; condset->is_paused = (char)rc_runtime_progress_read_uint(progress); @@ -307,7 +308,7 @@ static unsigned rc_runtime_progress_should_serialize_variable_condset(const rc_c static int rc_runtime_progress_write_variable(rc_runtime_progress_t* progress, const rc_value_t* variable) { - unsigned flags; + uint32_t flags; flags = rc_runtime_progress_should_serialize_variable_condset(variable->conditions); if (variable->value.changed) @@ -328,7 +329,7 @@ static int rc_runtime_progress_write_variable(rc_runtime_progress_t* progress, c static int rc_runtime_progress_write_variables(rc_runtime_progress_t* progress) { - unsigned count = 0; + uint32_t count = 0; const rc_value_t* variable; for (variable = progress->runtime->variables; variable; variable = variable->next) @@ -341,7 +342,7 @@ static int rc_runtime_progress_write_variables(rc_runtime_progress_t* progress) for (variable = progress->runtime->variables; variable; variable = variable->next) { - unsigned djb2 = rc_djb2(variable->name); + uint32_t djb2 = rc_djb2(variable->name); rc_runtime_progress_write_uint(progress, djb2); rc_runtime_progress_write_variable(progress, variable); @@ -353,7 +354,7 @@ static int rc_runtime_progress_write_variables(rc_runtime_progress_t* progress) static int rc_runtime_progress_read_variable(rc_runtime_progress_t* progress, rc_value_t* variable) { - unsigned flags = rc_runtime_progress_read_uint(progress); + uint32_t flags = rc_runtime_progress_read_uint(progress); variable->value.changed = (flags & RC_MEMREF_FLAG_CHANGED_THIS_FRAME) ? 1 : 0; variable->value.value = rc_runtime_progress_read_uint(progress); variable->value.prior = rc_runtime_progress_read_uint(progress); @@ -375,14 +376,14 @@ static int rc_runtime_progress_read_variables(rc_runtime_progress_t* progress) struct rc_pending_value_t { rc_value_t* variable; - unsigned djb2; + uint32_t djb2; }; struct rc_pending_value_t local_pending_variables[32]; struct rc_pending_value_t* pending_variables; rc_value_t* variable; - unsigned count, serialized_count; + uint32_t count, serialized_count; int result; - unsigned i; + uint32_t i; serialized_count = rc_runtime_progress_read_uint(progress); if (serialized_count == 0) @@ -413,7 +414,7 @@ static int rc_runtime_progress_read_variables(rc_runtime_progress_t* progress) result = RC_OK; for (; serialized_count > 0 && result == RC_OK; --serialized_count) { - unsigned djb2 = rc_runtime_progress_read_uint(progress); + uint32_t djb2 = rc_runtime_progress_read_uint(progress); for (i = 0; i < count; ++i) { if (pending_variables[i].djb2 == djb2) { variable = pending_variables[i].variable; @@ -491,7 +492,7 @@ static int rc_runtime_progress_read_trigger(rc_runtime_progress_t* progress, rc_ static int rc_runtime_progress_write_achievements(rc_runtime_progress_t* progress) { - unsigned i; + uint32_t i; int offset = 0; int result; @@ -532,8 +533,8 @@ static int rc_runtime_progress_write_achievements(rc_runtime_progress_t* progres static int rc_runtime_progress_read_achievement(rc_runtime_progress_t* progress) { - unsigned id = rc_runtime_progress_read_uint(progress); - unsigned i; + uint32_t id = rc_runtime_progress_read_uint(progress); + uint32_t i; for (i = 0; i < progress->runtime->trigger_count; ++i) { rc_runtime_trigger_t* runtime_trigger = &progress->runtime->triggers[i]; @@ -553,8 +554,8 @@ static int rc_runtime_progress_read_achievement(rc_runtime_progress_t* progress) static int rc_runtime_progress_write_leaderboards(rc_runtime_progress_t* progress) { - unsigned i; - unsigned flags; + uint32_t i; + uint32_t flags; int offset = 0; int result; @@ -610,8 +611,8 @@ static int rc_runtime_progress_write_leaderboards(rc_runtime_progress_t* progres static int rc_runtime_progress_read_leaderboard(rc_runtime_progress_t* progress) { - unsigned id = rc_runtime_progress_read_uint(progress); - unsigned i; + uint32_t id = rc_runtime_progress_read_uint(progress); + uint32_t i; int result; for (i = 0; i < progress->runtime->lboard_count; ++i) { @@ -621,7 +622,7 @@ static int rc_runtime_progress_read_leaderboard(rc_runtime_progress_t* progress) if (runtime_lboard->lboard->state == RC_TRIGGER_STATE_UNUPDATED) { /* only update state if definition hasn't changed (md5 matches) */ if (rc_runtime_progress_match_md5(progress, runtime_lboard->md5)) { - unsigned flags = rc_runtime_progress_read_uint(progress); + uint32_t flags = rc_runtime_progress_read_uint(progress); result = rc_runtime_progress_read_trigger(progress, &runtime_lboard->lboard->start); if (result != RC_OK) @@ -762,15 +763,15 @@ int rc_runtime_serialize_progress(void* buffer, const rc_runtime_t* runtime, lua return rc_runtime_progress_serialize_internal(&progress); } -int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char* serialized, lua_State* L) +int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const uint8_t* serialized, lua_State* L) { rc_runtime_progress_t progress; md5_state_t state; unsigned char md5[16]; - unsigned chunk_id; - unsigned chunk_size; - unsigned next_chunk_offset; - unsigned i; + uint32_t chunk_id; + uint32_t chunk_size; + uint32_t next_chunk_offset; + uint32_t i; int seen_rich_presence = 0; int result = RC_OK; @@ -780,7 +781,7 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char* } rc_runtime_progress_init(&progress, runtime, L); - progress.buffer = (unsigned char*)serialized; + progress.buffer = (uint8_t*)serialized; if (rc_runtime_progress_read_uint(&progress) != RC_RUNTIME_MARKER) { rc_runtime_reset(runtime);