From f99b0be52afa564cf988656fd948b143fbd77892 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 20 Mar 2021 12:38:52 -0400 Subject: [PATCH] refactor utility/helper functions --- CMakeLists.txt | 4 +-- decrypt_packets.c | 33 ++---------------------- gen_qst_header.c | 32 +++-------------------- utils.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 11 ++++++++ 5 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 utils.c create mode 100644 utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a05ca90..b4bcc4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,11 +10,11 @@ find_package(Iconv REQUIRED) find_library(SYLVERANT_LIBRARY sylverant REQUIRED) # decrypt_packets -add_executable(decrypt_packets decrypt_packets.c) +add_executable(decrypt_packets decrypt_packets.c utils.c) target_link_libraries(decrypt_packets ${SYLVERANT_LIBRARY}) # gen_qst_header -add_executable(gen_qst_header gen_qst_header.c) +add_executable(gen_qst_header gen_qst_header.c utils.c) target_link_libraries(gen_qst_header ${SYLVERANT_LIBRARY} ${ICONV_LIBRARIES}) target_compile_definitions(gen_qst_header PRIVATE ICONV_CONST=${ICONV_CONST}) target_include_directories(gen_qst_header PRIVATE ${ICONV_INCLUDE_DIR}) diff --git a/decrypt_packets.c b/decrypt_packets.c index 8a9cc35..cd01d15 100644 --- a/decrypt_packets.c +++ b/decrypt_packets.c @@ -1,43 +1,14 @@ #include #include -#include #include #include -void* read_file(const char *filename, uint32_t *out_file_size) { - if (!out_file_size) - return NULL; - - FILE *fp = fopen(filename, "rb"); - if (!fp) - return NULL; - - fseek(fp, 0, SEEK_END); - *out_file_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - uint8_t *result = malloc(*out_file_size); - - uint32_t read, next; - uint8_t buffer[1024]; - - next = 0; - - do { - read = fread(buffer, 1, 1024, fp); - if (read) { - memcpy(&result[next], buffer, read); - next += read; - } - } while (read); - - return result; -} +#include "utils.h" int main(int argc, char *argv[]) { if (argc != 3) { - printf("Usage: pso_decrypt server-packet-data.bin client-packet-data.bin\n"); + printf("Usage: decrypt_packets server-packet-data.bin client-packet-data.bin\n"); return 1; } diff --git a/gen_qst_header.c b/gen_qst_header.c index 03a55e1..4909613 100644 --- a/gen_qst_header.c +++ b/gen_qst_header.c @@ -6,6 +6,8 @@ #include #include +#include "utils.h" + typedef struct __attribute__((packed)) { uint32_t object_code_offset; uint32_t function_offset_table_offset; @@ -50,34 +52,6 @@ int sjis_to_utf8(char *s, size_t length) { return 0; } -int get_filesize(const char *filename, size_t *out_size) { - FILE *fp = fopen(filename, "rb"); - if (!fp) - return 1; - - fseek(fp, 0, SEEK_END); - *out_size = ftell(fp); - fclose(fp); - - return 0; -} - -const char* path_to_filename(const char *path) { - const char *pos = strrchr(path, '/'); - if (pos) { - return pos+1; - } else { - return path; - } -} - -char* append_file_extension(const char *filename, const char *extension) { - char *new_filename = malloc(strlen(filename) + strlen(extension)); - strcpy(new_filename, filename); - strcat(new_filename, extension); - return new_filename; -} - void generate_qst_header(const char *src_file, size_t src_file_size, QUEST_BIN_HEADER *bin_header, QST_HEADER *out_header) { memset(out_header, 0, sizeof(QST_HEADER)); @@ -105,7 +79,7 @@ void generate_qst_header(const char *src_file, size_t src_file_size, QUEST_BIN_H } int write_qst_header(const char *src_file, QST_HEADER *header) { - char *header_file = append_file_extension(src_file, ".hdr"); + char *header_file = append_string(src_file, ".hdr"); FILE *fp = fopen(header_file, "wb"); if (!fp) { diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..c593914 --- /dev/null +++ b/utils.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +void* read_file(const char *filename, uint32_t *out_file_size) { + if (!out_file_size) + return NULL; + + FILE *fp = fopen(filename, "rb"); + if (!fp) + return NULL; + + fseek(fp, 0, SEEK_END); + *out_file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + uint8_t *result = malloc(*out_file_size); + + uint32_t read, next; + uint8_t buffer[1024]; + + next = 0; + + do { + read = fread(buffer, 1, 1024, fp); + if (read) { + memcpy(&result[next], buffer, read); + next += read; + } + } while (read); + + return result; +} + +int get_filesize(const char *filename, size_t *out_size) { + FILE *fp = fopen(filename, "rb"); + if (!fp) + return 1; + + fseek(fp, 0, SEEK_END); + *out_size = ftell(fp); + fclose(fp); + + return 0; +} + +const char* path_to_filename(const char *path) { + const char *pos = strrchr(path, '/'); + if (pos) { + return pos+1; + } else { + return path; + } +} + +char* append_string(const char *a, const char *b) { + if (!a) + return NULL; + + char *result = malloc(strlen(a) + strlen(b)); + strcpy(result, a); + strcat(result, b); + return result; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..2e776d5 --- /dev/null +++ b/utils.h @@ -0,0 +1,11 @@ +#ifndef UTILS_H_INCLUDED +#define UTILS_H_INCLUDED + +#include + +void* read_file(const char *filename, uint32_t *out_file_size); +int get_filesize(const char *filename, size_t *out_size); +const char* path_to_filename(const char *path); +char* append_string(const char *a, const char *b); + +#endif