From b4cbeedf6ed4e04dd85c92e8345b4cb8dcaab27d Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 13 Apr 2020 00:00:05 +0200
Subject: Added features to dump compressed content into memory.

---
 src/common/compression.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/compression.h |  6 ++++++
 2 files changed, 59 insertions(+)

diff --git a/src/common/compression.c b/src/common/compression.c
index 982ac6a..df0ac6e 100644
--- a/src/common/compression.c
+++ b/src/common/compression.c
@@ -26,9 +26,11 @@
 
 #include <fcntl.h>
 #include <stdio.h>
+#include <string.h>
 
 
 #include "io.h"
+#include "../analysis/contents/memory.h"
 
 
 
@@ -175,3 +177,54 @@ bool dump_archive_entry_into_file(struct archive *input, struct archive_entry *e
     return result;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : intput   = archive dont le contenu est à extraire.           *
+*                entry    = entrée de l'archive à extraire.                   *
+*                                                                             *
+*  Description : Extrait un élement d'une archive.                            *
+*                                                                             *
+*  Retour      : Nouveau contenu volatile ou NULL en cas d'échec.             *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GBinContent *dump_archive_entry_into_memory(struct archive *input, struct archive_entry *entry)
+{
+    GBinContent *result;                    /* Contenu à retourner         */
+    bin_t *full_data;                       /* Données rassemblées         */
+    phys_t full_size;                       /* Somme de toutes les tailles */
+    const void *buff;                       /* Tampon de copie             */
+    size_t size;                            /* Quantité copiée             */
+    __LA_INT64_T offset;                    /* Position de lecture         */
+    int ret;                                /* Bilan d'un appel            */
+
+    result = NULL;
+
+    full_data = NULL;
+    full_size = 0;
+
+    for (ret = archive_read_data_block(input, &buff, &size, &offset);
+         ret == ARCHIVE_OK;
+         ret = archive_read_data_block(input, &buff, &size, &offset))
+    {
+        full_data = realloc(full_data, full_size + size);
+
+        memcpy(full_data + full_size, buff, size);
+
+        full_size += size;
+
+    }
+
+    if (ret == ARCHIVE_EOF)
+        result = g_memory_content_new(full_data, full_size);
+
+    if (full_data != NULL)
+        free(full_data);
+
+    return result;
+
+}
diff --git a/src/common/compression.h b/src/common/compression.h
index 632147a..f038ff6 100644
--- a/src/common/compression.h
+++ b/src/common/compression.h
@@ -30,6 +30,9 @@
 #include <stdbool.h>
 
 
+#include "../analysis/content.h"
+
+
 
 /* Codes de retour pour la compression */
 typedef enum _CPError
@@ -47,6 +50,9 @@ CPError add_file_into_archive(struct archive *, const char *, const char *);
 /* Extrait un élement d'une archive. */
 bool dump_archive_entry_into_file(struct archive *, struct archive_entry *, const char *);
 
+/* Extrait un élement d'une archive. */
+GBinContent *dump_archive_entry_into_memory(struct archive *, struct archive_entry *);
+
 
 
 #endif  /* _COMMON_COMPRESSION_H */
-- 
cgit v0.11.2-87-g4458