From 3ba750662837541a5df510046c0a3fc521cca785 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 17 Feb 2015 21:27:52 +0000
Subject: Defined, computed and stored the checksum in the binary content
 manager.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@474 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                          | 16 ++++++++
 src/analysis/binary-int.h          |  1 -
 src/analysis/binary.c              | 76 ++++----------------------------------
 src/analysis/binary.h              |  3 --
 src/analysis/disass/disassembler.c | 31 +++++++---------
 src/format/format.c                | 12 ++++++
 src/format/format.h                |  4 ++
 src/glibext/gbincontent.c          | 52 ++++++++++++++++++++++++++
 src/glibext/gbincontent.h          |  2 +
 9 files changed, 106 insertions(+), 91 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3fe266f..ad647ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+15-02-17  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/analysis/binary.c:
+	* src/analysis/binary.h:
+	* src/analysis/binary-int.h:
+	Remove the computed checksum in loaded binaries.
+
+	* src/analysis/disass/disassembler.c:
+	* src/format/format.c:
+	* src/format/format.h:
+	Update code.
+
+	* src/glibext/gbincontent.c:
+	* src/glibext/gbincontent.h:
+	Define, compute and store the checksum in the binary content manager.
+
 15-02-16  Cyrille Bagard <nocbos@gmail.com>
 
 	* src/analysis/binary.c:
diff --git a/src/analysis/binary-int.h b/src/analysis/binary-int.h
index ec98bf9..c2657ae 100644
--- a/src/analysis/binary-int.h
+++ b/src/analysis/binary-int.h
@@ -62,7 +62,6 @@ struct _GLoadedBinary
 
     off_t bin_length;                       /* Taille des données brutes   */
     bin_t *bin_data;                        /* Données binaires brutes     */
-    GChecksum *checksum;                    /* Calcul de l'empreinte       */
 
     GExeFormat *format;                     /* Format du binaire           */
     GArchProcessor *proc;                   /* Architecture du binaire     */
diff --git a/src/analysis/binary.c b/src/analysis/binary.c
index 911e239..b94071c 100644
--- a/src/analysis/binary.c
+++ b/src/analysis/binary.c
@@ -179,9 +179,6 @@ static void g_loaded_binary_init(GLoadedBinary *binary)
 
 static void g_loaded_binary_dispose(GLoadedBinary *binary)
 {
-    if (binary->checksum != NULL)
-        g_checksum_free(binary->checksum);
-
     if (binary->format != NULL)
         g_object_unref(G_OBJECT(binary->format));
     if (binary->proc != NULL)
@@ -237,14 +234,6 @@ GLoadedBinary *g_loaded_binary_new_from_xml(xmlXPathContextPtr context, const ch
 
 
 
-    char *host;                             /* Nom du serveur à contacter  */
-    int port;                               /* Numéro du port associé      */
-    char *author;                           /* Identification à diffuser   */
-
-
-
-    bool status;                            /* Etat de la connexion à la BD*/
-
     result = NULL;
 
     type = get_node_prop_value(context, path, "type");
@@ -272,36 +261,6 @@ GLoadedBinary *g_loaded_binary_new_from_xml(xmlXPathContextPtr context, const ch
 
 
 
-
-    printf("data :: %p  length :: %d\n", result->bin_data, result->bin_length);
-
-
-
-    /* Détermination de l'identifiant */
-
-    result->checksum = g_checksum_new(G_CHECKSUM_SHA256);
-    g_checksum_update(result->checksum, result->bin_data, result->bin_length);
-
-
-    result->local = g_db_client_new(g_loaded_binary_get_name(result, false),
-                                    g_loaded_binary_get_cheksum(result),
-                                    result->collections);
-
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_HOST, &host))
-        /* ... */;
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_LOCAL_PORT, &port))
-        /* ... */;
-
-    if (!g_generic_config_get_value(get_main_configuration(), MPK_AUTHOR_NAME, &author))
-        /* ... */;
-
-
-    status = g_db_client_start(result->local, host, port, author);
-
-    printf("DB status :: %d\n", status);
-
     return result;
 
  glbnfx_error:
@@ -716,6 +675,9 @@ bool g_loaded_binary_connect(GLoadedBinary *binary)
 {
     bool result;                            /* Bilan à retourner           */
 
+    GBinContent *content;                   /* Contenu bianire manipulé    */
+    const gchar *checksum;                  /* Identifiant de binaire      */
+
     char *host;                             /* Nom du serveur à contacter  */
     int port;                               /* Numéro du port associé      */
     char *author;                           /* Identification à diffuser   */
@@ -724,13 +686,11 @@ bool g_loaded_binary_connect(GLoadedBinary *binary)
 
 
     /* Détermination de l'identifiant */
-
-    binary->checksum = g_checksum_new(G_CHECKSUM_SHA256);
-    g_checksum_update(binary->checksum, binary->bin_data, binary->bin_length);
-
+    content = g_binary_format_get_conten_(G_BIN_FORMAT(binary->format));
+    checksum = g_binary_content_get_cheksum(content);
 
     binary->local = g_db_client_new(g_loaded_binary_get_name(binary, false),
-                                    g_loaded_binary_get_cheksum(binary),
+                                    checksum,
                                     binary->collections);
 
 
@@ -867,10 +827,7 @@ void g_loaded_binary_analyse(GLoadedBinary *binary)
 {
     /* Détermination de l'identifiant */
 
-    /* déplacé
-    binary->checksum = g_checksum_new(G_CHECKSUM_SHA256);
-    g_checksum_update(binary->checksum, binary->bin_data, binary->bin_length);
-    */
+
 
     /* Contacts avec les serveurs */
 
@@ -909,25 +866,6 @@ const char *g_loaded_binary_get_name(const GLoadedBinary *binary, bool full)
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : binary = élément binaire à consulter.                        *
-*                                                                             *
-*  Description : Fournit une empreinte unique (SHA256) pour le binaire.       *
-*                                                                             *
-*  Retour      : Chaîne représentant l'empreinte du binaire.                  *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-const gchar *g_loaded_binary_get_cheksum(const GLoadedBinary *binary)
-{
-    return g_checksum_get_string(binary->checksum);
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : binary = élément binaire à consulter.                        *
 *                length = taille en octets des données chargées. [OUT]        *
 *                                                                             *
 *  Description : Fournit les détails du contenu binaire chargé en mémoire.    *
diff --git a/src/analysis/binary.h b/src/analysis/binary.h
index fe6185f..b0fb369 100644
--- a/src/analysis/binary.h
+++ b/src/analysis/binary.h
@@ -152,9 +152,6 @@ void g_loaded_binary_analyse(GLoadedBinary *);
 /* Fournit le nom associé à l'élément binaire. */
 const char *g_loaded_binary_get_name(const GLoadedBinary *, bool);
 
-/* Fournit une empreinte unique (SHA256) pour le binaire. */
-const gchar *g_loaded_binary_get_cheksum(const GLoadedBinary *);
-
 /* Fournit les détails du contenu binaire chargé en mémoire. */
 bin_t *g_loaded_binary_get_data(const GLoadedBinary *, off_t *);
 
diff --git a/src/analysis/disass/disassembler.c b/src/analysis/disass/disassembler.c
index c39073c..de9830c 100644
--- a/src/analysis/disass/disassembler.c
+++ b/src/analysis/disass/disassembler.c
@@ -89,7 +89,7 @@ static void g_delayed_disassembly_process(GDelayedDisassembly *, GtkExtStatusBar
 
 
 /* Construit la description d'introduction du désassemblage. */
-static void build_disass_prologue(GCodeBuffer *, const char *, const uint8_t *, off_t);
+static void build_disass_prologue(GCodeBuffer *, const char *, const char *);
 
 
 
@@ -443,14 +443,12 @@ static void g_delayed_disassembly_process_old(GDelayedDisassembly *disass, GtkEx
 *                                                                             *
 ******************************************************************************/
 
-static void build_disass_prologue(GCodeBuffer *buffer, const char *filename, const uint8_t *data, off_t length)
+static void build_disass_prologue(GCodeBuffer *buffer, const char *filename, const char *checksum)
 {
     GLangOutput *output;                    /* Modèle de sortie adéquat    */
     GBufferLine *line;                      /* Ligne de destination        */
     size_t len;                             /* Taille du texte             */
     char *content;                          /* Contenu textuel d'une ligne */
-    GChecksum *checksum;                    /* Calcul de l'empreinte       */
-    const gchar *hex;                       /* Valeur hexadécimale du SHA  */
 
     output = g_asm_output_new();
 
@@ -464,7 +462,7 @@ static void build_disass_prologue(GCodeBuffer *buffer, const char *filename, con
     g_buffer_line_start_merge_at(line, BLC_PHYSICAL);
 
     line = g_lang_output_continue_comments(output, buffer,
-                                           SL(_("Chrysalide is free software - © 2008-2014 Cyrille Bagard")));
+                                           SL(_("Chrysalide is free software - © 2008-2015 Cyrille Bagard")));
     g_buffer_line_start_merge_at(line, BLC_PHYSICAL);
 
     line = g_lang_output_continue_comments(output, buffer, NULL, 0);
@@ -484,17 +482,10 @@ static void build_disass_prologue(GCodeBuffer *buffer, const char *filename, con
 
     /* Checksum SHA256 */
 
-    checksum = g_checksum_new(G_CHECKSUM_SHA256);
-
-    g_checksum_update(checksum, data, length);
-    hex = g_checksum_get_string(checksum);
-
-    len = strlen(_("Sha256: ")) + strlen(hex);
+    len = strlen(_("Sha256: ")) + strlen(checksum);
     content = (char *)calloc(len + 1, sizeof(char));
 
-    snprintf(content, len + 1, "%s%s", _("Sha256: "), hex);
-
-    g_checksum_free(checksum);
+    snprintf(content, len + 1, "%s%s", _("Sha256: "), checksum);
 
     line = g_lang_output_continue_comments(output, buffer, content, len - 1);
     g_buffer_line_start_merge_at(line, BLC_PHYSICAL);
@@ -535,15 +526,19 @@ static void build_disass_prologue(GCodeBuffer *buffer, const char *filename, con
 
 void disassemble_binary(GLoadedBinary *binary, GArchInstruction **instrs, GCodeBuffer **buffer, disassembly_ack_fc ack)
 {
-    const uint8_t *data;                    /* Données binaires brutes     */
-    off_t length;                           /* Quantité de ces données     */
+    GBinFormat *format;                     /* Format associé au binaire   */
+    GBinContent *content;                   /* Contenu bianire manipulé    */
+    const gchar *checksum;                  /* Identifiant de binaire      */
     GDelayedDisassembly *disass;            /* Désassemblage à mener       */
     GWorkQueue *queue;                      /* Gestionnaire de différés    */
 
     *buffer = g_code_buffer_new(BLC_ASSEMBLY);
 
-    data = g_loaded_binary_get_data(binary, &length);
-    build_disass_prologue(*buffer, g_loaded_binary_get_name(binary, true), data, length);
+    format = G_BIN_FORMAT(g_loaded_binary_get_format(binary));
+    content = g_binary_format_get_conten_(format);
+    checksum = g_binary_content_get_cheksum(content);
+
+    build_disass_prologue(*buffer, g_loaded_binary_get_name(binary, true), checksum);
 
     disass = g_delayed_disassembly_new(binary, instrs, *buffer);
     g_signal_connect(disass, "work-completed", G_CALLBACK(ack), binary);
diff --git a/src/format/format.c b/src/format/format.c
index fa00856..29c151d 100644
--- a/src/format/format.c
+++ b/src/format/format.c
@@ -134,6 +134,18 @@ const bin_t *g_binary_format_get_content(const GBinFormat *format, off_t *length
 }
 
 
+
+
+GBinContent *g_binary_format_get_conten_(const GBinFormat *format)
+{
+
+    return format->conten_;
+
+}
+
+
+
+
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : format = description de l'exécutable à consulter.            *
diff --git a/src/format/format.h b/src/format/format.h
index b01a9d5..ebad980 100644
--- a/src/format/format.h
+++ b/src/format/format.h
@@ -57,6 +57,10 @@ GType g_binary_format_get_type(void);
 /* Fournit une référence vers le contenu binaire analysé. */
 const bin_t *g_binary_format_get_content(const GBinFormat *, off_t *);
 
+
+GBinContent *g_binary_format_get_conten_(const GBinFormat *);
+
+
 /* Fournit un contexte initialisé pour un désassemblage. */
 void g_binary_format_setup_disassembling_context(const GBinFormat *, GProcContext *);
 
diff --git a/src/glibext/gbincontent.c b/src/glibext/gbincontent.c
index e0c64d4..a9fe907 100644
--- a/src/glibext/gbincontent.c
+++ b/src/glibext/gbincontent.c
@@ -55,6 +55,9 @@ struct _GBinContent
     binary_part *parts;                     /* Parties prises en compte    */
     size_t count;                           /* Nombre de ces parties       */
 
+    GChecksum *checksum;                    /* Calcul de l'empreinte       */
+    bool cs_computed;                       /* Calcul effectué ?           */
+
 };
 
 /* Content de données binaires quelconques (classe) */
@@ -125,6 +128,10 @@ static void g_binary_content_class_init(GBinContentClass *klass)
 
 static void g_binary_content_init(GBinContent *content)
 {
+    content->checksum = g_checksum_new(G_CHECKSUM_SHA256);
+    assert(content->checksum != NULL);
+
+    content->cs_computed = false;
 
 }
 
@@ -143,6 +150,8 @@ static void g_binary_content_init(GBinContent *content)
 
 static void g_binary_content_dispose(GBinContent *content)
 {
+    g_checksum_free(content->checksum);
+
     G_OBJECT_CLASS(g_binary_content_parent_class)->dispose(G_OBJECT(content));
 
 }
@@ -248,6 +257,49 @@ GBinContent *g_binary_content_new_from_file(const char *filename)
 
 /******************************************************************************
 *                                                                             *
+*  Paramètres  : binary = contenu binaire à consulter.                        *
+*                                                                             *
+*  Description : Fournit une empreinte unique (SHA256) pour les données.      *
+*                                                                             *
+*  Retour      : Chaîne représentant l'empreinte du contenu binaire.          *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+const gchar *g_binary_content_get_cheksum(GBinContent *content)
+{
+    size_t i;                               /* Boucle de parcours          */
+    binary_part *part;                      /* Bloc parcouru pour analyse  */
+
+    if (!content->cs_computed)
+    {
+        g_checksum_reset(content->checksum);
+
+        for (i = 0; i < content->count; i++)
+        {
+            part = &content->parts[i];
+            g_checksum_update(content->checksum, part->data, get_mrange_length(&part->range));
+        }
+
+        content->cs_computed = true;
+
+    }
+
+    return g_checksum_get_string(content->checksum);
+
+}
+
+
+
+
+
+
+
+
+
+/******************************************************************************
+*                                                                             *
 *  Paramètres  : content = contenu binaire à venir lire.                      *
 *                addr    = position de la tête de lecture globale demandée.   *
 *                start   = position de la tête de lecture dans la zone. [OUT] *
diff --git a/src/glibext/gbincontent.h b/src/glibext/gbincontent.h
index e2f5933..246d626 100644
--- a/src/glibext/gbincontent.h
+++ b/src/glibext/gbincontent.h
@@ -56,6 +56,8 @@ GType g_binary_content_get_type(void);
 /* Charge en mémoire le contenu d'un fichier donné. */
 GBinContent *g_binary_content_new_from_file(const char *);
 
+/* Fournit une empreinte unique (SHA256) pour les données. */
+const gchar *g_binary_content_get_cheksum(GBinContent *);
 
 
 /* Fournit une portion des données représentées. */
-- 
cgit v0.11.2-87-g4458