libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit ba78c4741fe3ee2d594eaf9b97b53136d4e040c1
parent 558877dba24bea5df72296235aa61ff1ec146c2e
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue,  9 May 2017 11:41:59 +0200

Fix(Packer): Better functions separations for archive reading

Diffstat:
Minc/packer_file.h | 13+++++++++++++
Msrc/packer.c | 46+---------------------------------------------
Msrc/packer_file.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 45 deletions(-)

diff --git a/inc/packer_file.h b/inc/packer_file.h @@ -81,4 +81,17 @@ bool read_files_from_dir(const char *dir_name, mlist_t **files, mlist_t **dirs); */ bool get_file_information(packer_file_t *file); +/*! + * \brief Read a file from a binary archive + * + * \param[in] content Binary content + * \param[in,out] ctr Cursor + * + * Read a file from binary stream, from the cursor position + * + * \return A freshly filled and allocated packer_file_t struct on success, + * NULL on failure + */ +packer_file_t *read_packer_file_from_binary(const char *content, off_t *ctr); + #endif /* PACKER_FILE_H */ diff --git a/src/packer.c b/src/packer.c @@ -740,57 +740,13 @@ static bool read_package_files(char *content, packer_t *ctx, off_t total_size) { return false; packer_file_t *file = NULL; - z_stream stream; off_t ctr = 0; while (total_size > ctr) { - - file = malloc(sizeof(*file)); + file = read_packer_file_from_binary(content, &ctr); if (file == NULL) goto error; - file->content = NULL; - - /* File name */ - file->fn = strdup(content + ctr); - ctr += strlen(file->fn) + 1; - - /* File content size */ - memcpy(&file->file_size, content + ctr, sizeof(file->file_size)); - ctr += sizeof(file->file_size); - - if (file->file_size > 0) { - /* File hash */ - memcpy(file->sum, content + ctr, crypto_hash_sha256_BYTES); - ctr += crypto_hash_sha256_BYTES; - /* Actual file content */ - stream.zalloc = NULL; - stream.zfree = NULL; - stream.opaque = NULL; - stream.avail_in = file->file_size; - stream.avail_out = file->file_size; - - inflateInit(&stream); - - file->content = malloc(file->file_size + 1); - if (file->content == NULL) - { - free(file->fn); - free(file); - goto error; - } - - /* Decompress file */ - stream.next_in = (unsigned char *)content + ctr; - stream.next_out = (unsigned char *)file->content; - - inflate(&stream, Z_NO_FLUSH); - file->content[file->file_size - stream.avail_out] = '\0'; - inflateEnd(&stream); - - - ctr += file->file_size; - } list_add(ctx->files, file, sizeof(*file)); free(file); } diff --git a/src/packer_file.c b/src/packer_file.c @@ -165,3 +165,62 @@ bool get_file_information(packer_file_t *file) { return true; } +packer_file_t *read_packer_file_from_binary(const char *content, off_t *ctr) { + packer_file_t *file = NULL; + z_stream stream; + + file = malloc(sizeof(*file)); + if (file == NULL) + return NULL; + + file->content = NULL; + + /* File name */ + file->fn = strdup(content + *ctr); + if (file->fn == NULL) + { + free(file); + return NULL; + } + + *ctr += strlen(file->fn) + 1; + + /* File content size */ + memcpy(&file->file_size, content + *ctr, sizeof(file->file_size)); + *ctr += sizeof(file->file_size); + + if (file->file_size > 0) { + /* File hash */ + memcpy(file->sum, content + *ctr, crypto_hash_sha256_BYTES); + *ctr += crypto_hash_sha256_BYTES; + + /* Actual file content */ + stream.zalloc = NULL; + stream.zfree = NULL; + stream.opaque = NULL; + stream.avail_in = file->file_size; + stream.avail_out = file->file_size; + + inflateInit(&stream); + + file->content = malloc(file->file_size + 1); + if (file->content == NULL) + { + free(file->fn); + free(file); + return NULL; + } + + /* Decompress file */ + stream.next_in = (unsigned char *)content + *ctr; + stream.next_out = (unsigned char *)file->content; + + inflate(&stream, Z_NO_FLUSH); + file->content[file->file_size - stream.avail_out] = '\0'; + inflateEnd(&stream); + + + *ctr += file->file_size; + } + return file; +}