From c1faae90e9218822371825ee937479acb621e286 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 27 Sep 2024 12:58:53 +0900 Subject: [PATCH] Split up handling of archive file based on version --- src/archiver.c | 41 +++++++++++++++++++++++++++++++++-------- src/archiver.h | 8 ++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/archiver.c b/src/archiver.c index 67f38cf..700ada8 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -1173,12 +1173,9 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, const SDArchiverState *state) { - uint8_t buf[1024]; - memset(buf, 0, 1024); + uint8_t buf[32]; + memset(buf, 0, 32); uint16_t u16; - uint32_t u32; - uint64_t u64; - int_fast8_t is_compressed = 0; if (fread(buf, 1, 18, in_f) != 18) { return SDAS_INVALID_FILE; @@ -1186,10 +1183,31 @@ int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, return SDAS_INVALID_FILE; } else if (fread(buf, 1, 2, in_f) != 2) { return SDAS_INVALID_FILE; - } else if (buf[0] != 0 || buf[1] != 0) { - // Version is not zero. + } + + memcpy(&u16, buf, 2); + simple_archiver_helper_16_bit_be(&u16); + + if (u16 == 0) { + return simple_archiver_parse_archive_version_0(in_f, do_extract, state); + } else if (u16 == 1) { + return simple_archiver_parse_archive_version_1(in_f, do_extract, state); + } else { + fprintf(stderr, "ERROR Unsupported archive version %u!\n", u16); return SDAS_INVALID_FILE; - } else if (fread(buf, 1, 4, in_f) != 4) { + } +} + +int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract, + const SDArchiverState *state) { + uint8_t buf[1024]; + memset(buf, 0, 1024); + uint16_t u16; + uint32_t u32; + uint64_t u64; + int_fast8_t is_compressed = 0; + + if (fread(buf, 1, 4, in_f) != 4) { return SDAS_INVALID_FILE; } @@ -1999,6 +2017,13 @@ int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, return SDAS_SUCCESS; } +int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract, + const SDArchiverState *state) { + // TODO Implement this. + fprintf(stderr, "ERROR Handling archive version 1 is unimplemented!\n"); + return SDAS_INTERNAL_ERROR; +} + int simple_archiver_de_compress(int pipe_fd_in[2], int pipe_fd_out[2], const char *cmd, void *pid_out) { #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ diff --git a/src/archiver.h b/src/archiver.h index e651b87..51b6abc 100644 --- a/src/archiver.h +++ b/src/archiver.h @@ -69,6 +69,14 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, const SDArchiverState *state); +/// Returns zero on success. +int simple_archiver_parse_archive_version_0(FILE *in_f, int_fast8_t do_extract, + const SDArchiverState *state); + +/// Returns zero on success. +int simple_archiver_parse_archive_version_1(FILE *in_f, int_fast8_t do_extract, + const SDArchiverState *state); + /// Returns zero on success. int simple_archiver_de_compress(int pipe_fd_in[2], int pipe_fd_out[2], const char *cmd, void *pid_out);