From d7f116da599a0de65cce8fe3e52cb357bab0d1a8 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 20 Mar 2021 14:04:57 -0400 Subject: [PATCH] more refactoring --- CMakeLists.txt | 2 +- gen_qst_header.c | 26 +------------------------- retvals.h | 9 +++++++++ textconv.c | 32 ++++++++++++++++++++++++++++++++ textconv.h | 11 +++++++++++ utils.c | 1 + utils.h | 5 +---- 7 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 retvals.h create mode 100644 textconv.c create mode 100644 textconv.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d6b8649..294879f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ 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 quests.c utils.c) +add_executable(gen_qst_header gen_qst_header.c textconv.c quests.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/gen_qst_header.c b/gen_qst_header.c index c07dea0..595c729 100644 --- a/gen_qst_header.c +++ b/gen_qst_header.c @@ -3,35 +3,11 @@ #include #include -#include #include #include "utils.h" #include "quests.h" - -int sjis_to_utf8(char *s, size_t length) { - if (!s) - return ERROR_INVALID_PARAMS; - - iconv_t conv; - size_t in_size, out_size; - - char *outbuf = malloc(length); - - in_size = length; - out_size = length; - char *in = s; - char *out = outbuf; - conv = iconv_open("SHIFT_JIS", "UTF-8"); - iconv(conv, &in, &in_size, &out, &out_size); - iconv_close(conv); - - memset(s, 0, length); - memcpy(s, outbuf, length); - free(outbuf); - - return SUCCESS; -} +#include "textconv.h" int write_qst_header(const char *filename, const QST_HEADER *header) { FILE *fp = fopen(filename, "wb"); diff --git a/retvals.h b/retvals.h new file mode 100644 index 0000000..59f6fc4 --- /dev/null +++ b/retvals.h @@ -0,0 +1,9 @@ +#ifndef RETVALS_H_INCLUDED +#define RETVALS_H_INCLUDED + +#define SUCCESS 0 +#define ERROR_INVALID_PARAMS 1 +#define ERROR_FILE_NOT_FOUND 2 +#define ERROR_CREATING_FILE 3 + +#endif diff --git a/textconv.c b/textconv.c new file mode 100644 index 0000000..232e66d --- /dev/null +++ b/textconv.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#include + +#include "textconv.h" +#include "retvals.h" + +int sjis_to_utf8(char *s, size_t length) { + if (!s) + return ERROR_INVALID_PARAMS; + + iconv_t conv; + size_t in_size, out_size; + + char *outbuf = malloc(length); + + in_size = length; + out_size = length; + char *in = s; + char *out = outbuf; + conv = iconv_open("SHIFT_JIS", "UTF-8"); + iconv(conv, &in, &in_size, &out, &out_size); + iconv_close(conv); + + memset(s, 0, length); + memcpy(s, outbuf, length); + free(outbuf); + + return SUCCESS; +} diff --git a/textconv.h b/textconv.h new file mode 100644 index 0000000..1979c76 --- /dev/null +++ b/textconv.h @@ -0,0 +1,11 @@ +#ifndef TEXTCONV_H_INCLUDED +#define TEXTCONV_H_INCLUDED + +#include +#include + +#include "retvals.h" + +int sjis_to_utf8(char *s, size_t length); + +#endif diff --git a/utils.c b/utils.c index 7d14c4c..ab6cdaf 100644 --- a/utils.c +++ b/utils.c @@ -4,6 +4,7 @@ #include #include "utils.h" +#include "retvals.h" int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size) { if (!out_file_size || !out_file_data) diff --git a/utils.h b/utils.h index 619b356..5a715ed 100644 --- a/utils.h +++ b/utils.h @@ -3,10 +3,7 @@ #include -#define SUCCESS 0 -#define ERROR_INVALID_PARAMS 1 -#define ERROR_FILE_NOT_FOUND 2 -#define ERROR_CREATING_FILE 3 +#include "retvals.h" int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size); int get_filesize(const char *filename, size_t *out_size);