diff --git a/decrypt_packets.c b/decrypt_packets.c index cd01d15..642f252 100644 --- a/decrypt_packets.c +++ b/decrypt_packets.c @@ -17,15 +17,15 @@ int main(int argc, char *argv[]) { uint32_t server_data_size = 0; uint32_t client_data_size = 0; + uint8_t *server_data; + uint8_t *client_data; - uint8_t *server_data = read_file(server_packet_file, &server_data_size); - if (!server_data) { + if (read_file(server_packet_file, &server_data, &server_data_size)) { printf("Error reading server packet data file: %s\n", server_packet_file); return 1; } - uint8_t *client_data = read_file(client_packet_file, &client_data_size); - if (!client_data) { + if (read_file(client_packet_file, &client_data, &client_data_size)) { printf("Error reading client packet data file: %s\n", client_packet_file); free(server_data); return 1; diff --git a/utils.c b/utils.c index c593914..7d14c4c 100644 --- a/utils.c +++ b/utils.c @@ -3,13 +3,15 @@ #include #include -void* read_file(const char *filename, uint32_t *out_file_size) { - if (!out_file_size) - return NULL; +#include "utils.h" + +int read_file(const char *filename, uint8_t** out_file_data, uint32_t *out_file_size) { + if (!out_file_size || !out_file_data) + return ERROR_INVALID_PARAMS; FILE *fp = fopen(filename, "rb"); if (!fp) - return NULL; + return ERROR_FILE_NOT_FOUND; fseek(fp, 0, SEEK_END); *out_file_size = ftell(fp); @@ -30,19 +32,23 @@ void* read_file(const char *filename, uint32_t *out_file_size) { } } while (read); - return result; + *out_file_data = result; + return SUCCESS; } int get_filesize(const char *filename, size_t *out_size) { + if (!filename || !out_size) + return ERROR_INVALID_PARAMS; + FILE *fp = fopen(filename, "rb"); if (!fp) - return 1; + return ERROR_FILE_NOT_FOUND; fseek(fp, 0, SEEK_END); *out_size = ftell(fp); fclose(fp); - return 0; + return SUCCESS; } const char* path_to_filename(const char *path) { diff --git a/utils.h b/utils.h index 2e776d5..a51f0fe 100644 --- a/utils.h +++ b/utils.h @@ -3,7 +3,11 @@ #include -void* read_file(const char *filename, uint32_t *out_file_size); +#define SUCCESS 0 +#define ERROR_INVALID_PARAMS 1 +#define ERROR_FILE_NOT_FOUND 2 + +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); const char* path_to_filename(const char *path); char* append_string(const char *a, const char *b);