more refactoring

This commit is contained in:
Gered 2021-03-20 14:04:57 -04:00
parent 50532638dc
commit d7f116da59
7 changed files with 56 additions and 30 deletions

View file

@ -14,7 +14,7 @@ add_executable(decrypt_packets decrypt_packets.c utils.c)
target_link_libraries(decrypt_packets ${SYLVERANT_LIBRARY}) target_link_libraries(decrypt_packets ${SYLVERANT_LIBRARY})
# gen_qst_header # 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_link_libraries(gen_qst_header ${SYLVERANT_LIBRARY} ${ICONV_LIBRARIES})
target_compile_definitions(gen_qst_header PRIVATE ICONV_CONST=${ICONV_CONST}) target_compile_definitions(gen_qst_header PRIVATE ICONV_CONST=${ICONV_CONST})
target_include_directories(gen_qst_header PRIVATE ${ICONV_INCLUDE_DIR}) target_include_directories(gen_qst_header PRIVATE ${ICONV_INCLUDE_DIR})

View file

@ -3,35 +3,11 @@
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include <iconv.h>
#include <sylverant/prs.h> #include <sylverant/prs.h>
#include "utils.h" #include "utils.h"
#include "quests.h" #include "quests.h"
#include "textconv.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;
}
int write_qst_header(const char *filename, const QST_HEADER *header) { int write_qst_header(const char *filename, const QST_HEADER *header) {
FILE *fp = fopen(filename, "wb"); FILE *fp = fopen(filename, "wb");

9
retvals.h Normal file
View file

@ -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

32
textconv.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iconv.h>
#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;
}

11
textconv.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef TEXTCONV_H_INCLUDED
#define TEXTCONV_H_INCLUDED
#include <stdio.h>
#include <iconv.h>
#include "retvals.h"
int sjis_to_utf8(char *s, size_t length);
#endif

View file

@ -4,6 +4,7 @@
#include <malloc.h> #include <malloc.h>
#include "utils.h" #include "utils.h"
#include "retvals.h"
int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size) { int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size) {
if (!out_file_size || !out_file_data) if (!out_file_size || !out_file_data)

View file

@ -3,10 +3,7 @@
#include <stdint.h> #include <stdint.h>
#define SUCCESS 0 #include "retvals.h"
#define ERROR_INVALID_PARAMS 1
#define ERROR_FILE_NOT_FOUND 2
#define ERROR_CREATING_FILE 3
int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size); 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); int get_filesize(const char *filename, size_t *out_size);