refactor utility/helper functions

This commit is contained in:
Gered 2021-03-20 12:38:52 -04:00
parent 32d5fdcbad
commit f99b0be52a
5 changed files with 83 additions and 62 deletions

View file

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

View file

@ -1,43 +1,14 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <malloc.h>
#include <sylverant/encryption.h>
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;
}

View file

@ -6,6 +6,8 @@
#include <iconv.h>
#include <sylverant/prs.h>
#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) {

65
utils.c Normal file
View file

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

11
utils.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED
#include <stdint.h>
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