diff options
Diffstat (limited to 'src/analysis')
| -rw-r--r-- | src/analysis/Makefile.am | 1 | ||||
| -rw-r--r-- | src/analysis/cattribs.c | 344 | ||||
| -rw-r--r-- | src/analysis/cattribs.h | 63 | ||||
| -rw-r--r-- | src/analysis/content-int.h | 9 | ||||
| -rw-r--r-- | src/analysis/content.c | 60 | ||||
| -rw-r--r-- | src/analysis/content.h | 7 | ||||
| -rw-r--r-- | src/analysis/contents/file.c | 63 | 
7 files changed, 547 insertions, 0 deletions
diff --git a/src/analysis/Makefile.am b/src/analysis/Makefile.am index c4a66de..39b960c 100644 --- a/src/analysis/Makefile.am +++ b/src/analysis/Makefile.am @@ -5,6 +5,7 @@ libanalysis_la_SOURCES =				\  	binary.h binary.c					\  	block-int.h							\  	block.h block.c						\ +	cattribs.h cattribs.c				\  	content-int.h						\  	content.h content.c					\  	loaded-int.h						\ diff --git a/src/analysis/cattribs.c b/src/analysis/cattribs.c new file mode 100644 index 0000000..ebebb83 --- /dev/null +++ b/src/analysis/cattribs.c @@ -0,0 +1,344 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cattribs.c - rassemblement des attributs utiles au chargement d'un contenu binaire + * + * Copyright (C) 2019 Cyrille Bagard + * + *  This file is part of Chrysalide. + * + *  Chrysalide is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  Chrysalide is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "cattribs.h" + + +#include <malloc.h> +#include <string.h> + + +#include "../glibext/configuration.h" + + + +/* Ensemble d'attributs pour contenu binaire (instance) */ +struct _GContentAttributes +{ +    GObject parent;                         /* A laisser en premier        */ + +    char *filename;                         /* Fichier de base du chemin   */ + +    GGenConfig **configs;                   /* Paramètres par niveaux      */ +    size_t count;                           /* Quantité de ces niveaux     */ + +}; + +/* Ensemble d'attributs pour contenu binaire (classe) */ +struct _GContentAttributesClass +{ +    GObjectClass parent;                    /* A laisser en premier        */ + +}; + + +/* Initialise la classe des ensembles d'attributs pour contenus. */ +static void g_content_attributes_class_init(GContentAttributesClass *); + +/* Initialise un ensemble d'attributs pour contenu binaire. */ +static void g_content_attributes_init(GContentAttributes *); + +/* Supprime toutes les références externes. */ +static void g_content_attributes_dispose(GContentAttributes *); + +/* Procède à la libération totale de la mémoire. */ +static void g_content_attributes_finalize(GContentAttributes *); + + + +/* Indique le type défini pour un ensemble d'attributs de contenu binaire. */ +G_DEFINE_TYPE(GContentAttributes, g_content_attributes, G_TYPE_OBJECT); + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : klass = classe à initialiser.                                * +*                                                                             * +*  Description : Initialise la classe des ensembles d'attributs pour contenus.* +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_content_attributes_class_init(GContentAttributesClass *klass) +{ +    GObjectClass *object;                   /* Autre version de la classe  */ + +    object = G_OBJECT_CLASS(klass); + +    object->dispose = (GObjectFinalizeFunc/* ! */)g_content_attributes_dispose; +    object->finalize = (GObjectFinalizeFunc)g_content_attributes_finalize; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : binary = instance à initialiser.                             * +*                                                                             * +*  Description : Initialise un ensemble d'attributs pour contenu binaire.     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_content_attributes_init(GContentAttributes *attribs) +{ +    attribs->filename = NULL; + +    attribs->configs = malloc(sizeof(GGenConfig *)); +    attribs->count = 1; + +    attribs->configs[0] = g_generic_config_new(); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : attribs = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Supprime toutes les références externes.                     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_content_attributes_dispose(GContentAttributes *attribs) +{ +    size_t i;                               /* Boucle de parcours          */ + +    for (i = 0; i < attribs->count; i++) +        g_clear_object(&attribs->configs[i]); + +    G_OBJECT_CLASS(g_content_attributes_parent_class)->dispose(G_OBJECT(attribs)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : attribs = instance d'objet GLib à traiter.                   * +*                                                                             * +*  Description : Procède à la libération totale de la mémoire.                * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_content_attributes_finalize(GContentAttributes *attribs) +{ +    free(attribs->configs); + +    G_OBJECT_CLASS(g_content_attributes_parent_class)->finalize(G_OBJECT(attribs)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : path = chemin d'accès à un contenu à charger.                * +*                                                                             * +*  Description : Construit un ensemble d'attribut pour contenu binaire.       * +*                                                                             * +*  Retour      : Instance mise en place ou NULL en cas d'échec.               * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GContentAttributes *g_content_attributes_new(const char *path) +{ +    GContentAttributes *result;             /* Adresse à retourner         */ +    GGenConfig *config;                     /* Niveau de config. courant   */ +    const char *iter;                       /* Boucle de parcours          */ +    const char *next;                       /* Prochain marqueur rencontré */ +    char *part;                             /* Clef et sa valeur           */ +    char *eq;                               /* Signe '=' rencontré         */ + +    result = g_object_new(G_TYPE_CONTENT_ATTRIBUTES, NULL); + +    iter = strchr(path, '&'); + +    if (iter == NULL) +        result->filename = strdup(path); + +    else +    { +        if (iter != path) +            result->filename = strndup(path, iter - path); + +        config = result->configs[0]; + +        do +        { +            iter++; + +            next = strchr(iter, '&'); + +            if (next == NULL) +                next = path + strlen(path); + +            /* Présence de deux '&' consécutifs */ +            if (iter == next) +            { +                result->configs = realloc(result->configs, ++result->count * sizeof(GGenConfig *)); + +                result->configs[result->count - 1] = g_generic_config_new(); + +                config = result->configs[result->count - 1]; + +            } + +            /* Traitement d'une nouvelle combinaison */ +            else +            { +                part = strndup(iter, next - iter); + +                eq = strchr(part, '='); + +                if (eq != NULL) +                { +                    *eq = '\0'; + +                    if (eq[1] != '\0') +                        g_generic_config_create_or_udpdate_param(config, part, CPT_STRING, NULL, eq + 1); + +                } + +                free(part); + +            } + +            iter = strchr(iter, '&'); + +        } +        while (iter != NULL); + +    } + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : attribs = ensemble d'attributs de contenu à consulter.       * +*                                                                             * +*  Description : Fournit le fichier de base compris dans le chemin initial.   * +*                                                                             * +*  Retour      : Nom de fichier renvoyant vers un contenu à charger ou NULL.  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +const char *g_content_attributes_get_filename(const GContentAttributes *attribs) +{ +    char *result;                           /* Nom de fichier à retourner  */ + +    result = attribs->filename; + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : attribs = ensemble d'attributs de contenu à consulter.       * +*                count   = taille de la liste de clefs renvoyées. [OUT]       * +*                                                                             * +*  Description : Fournit l'ensemble des clefs d'un ensemble d'attributs.      * +*                                                                             * +*  Retour      : Liste de clefs des attributes conservés dans l'ensemble.     * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +const char **g_content_attributes_get_keys(const GContentAttributes *attribs, size_t *count) +{ +    const char **result;                    /* Liste à retourner           */ +    GList *list;                            /* Liste de paramètres         */ +    GList *iter;                            /* Boucle de parcours          */ +    GCfgParam *param;                       /* Paramètre d'un ensemble     */ +    const char *key;                        /* Clef d'un paramètre         */ + +    result = NULL; +    *count = 0; + +    list = g_generic_config_list_params(attribs->configs[0]); + +    for (iter = g_list_first(list); iter != NULL; iter = g_list_next(iter)) +    { +        param = G_CFG_PARAM(iter->data); + +        key = g_config_param_get_path(param); + +        result = realloc(result, ++(*count) * sizeof(char *)); + +        result[*count - 1] = strdup(key); + +    } + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : attribs = ensemble d'attributs de contenu à consulter.       * +*                key     = désignation de l'attribut visé par la procédure.   * +*                                                                             * +*  Description : Indique la valeur d'un attribut appartenant à un ensemble.   * +*                                                                             * +*  Retour      : Valeur de l'attribut recherché, s'il a été trouvé.           * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +const char *g_content_attributes_get_value(const GContentAttributes *attribs, const char *key) +{ +    const char *result;                     /* Trouvaille à retourner      */ +    bool status; + +    status = g_generic_config_get_value(attribs->configs[0], key, &result); + +    if (!status) +        result = NULL; + +    return result; + +} diff --git a/src/analysis/cattribs.h b/src/analysis/cattribs.h new file mode 100644 index 0000000..c2e8a2e --- /dev/null +++ b/src/analysis/cattribs.h @@ -0,0 +1,63 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * cattribs.h - prototypes pour le rassemblement des attributs utiles au chargement d'un contenu binaire + * + * Copyright (C) 2019 Cyrille Bagard + * + *  This file is part of Chrysalide. + * + *  Chrysalide is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  Chrysalide is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _ANALYSIS_CATTRIBS_H +#define _ANALYSIS_CATTRIBS_H + + +#include <glib-object.h> + + +#define G_TYPE_CONTENT_ATTRIBUTES            g_content_attributes_get_type() +#define G_CONTENT_ATTRIBUTES(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_CONTENT_ATTRIBUTES, GContentAttributes)) +#define G_IS_CONTENT_ATTRIBUTES(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_CONTENT_ATTRIBUTES)) +#define G_CONTENT_ATTRIBUTES_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_CONTENT_ATTRIBUTES, GContentAttributesClass)) +#define G_IS_CONTENT_ATTRIBUTES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_CONTENT_ATTRIBUTES)) +#define G_CONTENT_ATTRIBUTES_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_CONTENT_ATTRIBUTES, GContentAttributesClass)) + + +/* Ensemble d'attributs pour contenu binaire (instance) */ +typedef struct _GContentAttributes GContentAttributes; + +/* Ensemble d'attributs pour contenu binaire (classe) */ +typedef struct _GContentAttributesClass GContentAttributesClass; + + +/* Indique le type défini pour un ensemble d'attributs de contenu binaire. */ +GType g_content_attributes_get_type(void); + +/* Construit un ensemble d'attribut pour contenu binaire. */ +GContentAttributes *g_content_attributes_new(const char *); + +/* Fournit le fichier de base compris dans le chemin initial. */ +const char *g_content_attributes_get_filename(const GContentAttributes *); + +/* Fournit l'ensemble des clefs d'un ensemble d'attributs. */ +const char **g_content_attributes_get_keys(const GContentAttributes *, size_t *); + +/* Indique la valeur d'un attribut appartenant à un ensemble. */ +const char *g_content_attributes_get_value(const GContentAttributes *, const char *); + + + +#endif  /* _ANALYSIS_CATTRIBS_H */ diff --git a/src/analysis/content-int.h b/src/analysis/content-int.h index f3d698e..24247ed 100644 --- a/src/analysis/content-int.h +++ b/src/analysis/content-int.h @@ -29,6 +29,12 @@ +/* Associe un ensemble d'attributs au contenu binaire. */ +typedef void (* set_content_attributes) (GBinContent *, GContentAttributes *); + +/* Fournit l'ensemble des attributs associés à un contenu. */ +typedef GContentAttributes * (* get_content_attributes) (const GBinContent *); +  /* Donne l'origine d'un contenu binaire. */  typedef GBinContent * (* get_content_root_fc) (GBinContent *); @@ -86,6 +92,9 @@ struct _GBinContentIface  {      GTypeInterface base_iface;              /* A laisser en premier        */ +    set_content_attributes set_attribs;     /* Enregistrement d'attributs  */ +    get_content_attributes get_attribs;     /* Fourniture d'attributs      */ +      get_content_root_fc get_root;           /* Renvoie à l'origine         */      describe_content_fc describe;           /* Fournit une description     */ diff --git a/src/analysis/content.c b/src/analysis/content.c index dbece37..41a00bc 100644 --- a/src/analysis/content.c +++ b/src/analysis/content.c @@ -98,6 +98,66 @@ GBinContent *g_binary_content_new_from_xml(xmlXPathContextPtr context, const cha  /******************************************************************************  *                                                                             * +*  Paramètres  : content = contenu binaire à actualiser.                      * +*                attribs = jeu d'attributs à lier au contenu courant.         * +*                                                                             * +*  Description : Associe un ensemble d'attributs au contenu binaire.          * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void g_binary_content_set_attributes(GBinContent *content, GContentAttributes *attribs) +{ +    GContentAttributes *old;                /* Ancien jeu d'attributs      */ +    GBinContentIface *iface;                /* Interface utilisée          */ + +    iface = G_BIN_CONTENT_GET_IFACE(content); + +    old = iface->get_attribs(content); + +    if (old != NULL) +        g_object_unref(G_OBJECT(old)); + +    iface->set_attribs(content, attribs); + +    g_object_ref_sink(G_OBJECT(attribs)); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : content = contenu binaire à consulter.                       * +*                                                                             * +*  Description : Fournit l'ensemble des attributs associés à un contenu.      * +*                                                                             * +*  Retour      : Jeu d'attributs liés au contenu courant.                     * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GContentAttributes *g_binary_content_get_attributes(const GBinContent *content) +{ +    GContentAttributes *result;             /* Instance à retourner        */ +    GBinContentIface *iface;                /* Interface utilisée          */ + +    iface = G_BIN_CONTENT_GET_IFACE(content); + +    result = iface->get_attribs(content); + +    g_object_ref(G_OBJECT(result)); + +    return result; + +} + + +/****************************************************************************** +*                                                                             *  *  Paramètres  : content = contenu binaire à consulter.                       *  *                                                                             *  *  Description : Donne l'origine d'un contenu binaire.                        * diff --git a/src/analysis/content.h b/src/analysis/content.h index f16a164..3e12aa3 100644 --- a/src/analysis/content.h +++ b/src/analysis/content.h @@ -29,6 +29,7 @@  #include <glib-object.h> +#include "cattribs.h"  #include "../arch/vmpa.h"  #include "../common/endianness.h"  #include "../common/leb128.h" @@ -57,6 +58,12 @@ GType g_binary_content_get_type(void) G_GNUC_CONST;  /* Charge en mémoire un contenu binaire à partir d'XML. */  GBinContent *g_binary_content_new_from_xml(xmlXPathContextPtr, const char *, const char *); +/* Associe un ensemble d'attributs au contenu binaire. */ +void g_binary_content_set_attributes(GBinContent *, GContentAttributes *); + +/* Fournit l'ensemble des attributs associés à un contenu. */ +GContentAttributes *g_binary_content_get_attributes(const GBinContent *); +  /* Donne l'origine d'un contenu binaire. */  GBinContent *g_binary_content_get_root(GBinContent *); diff --git a/src/analysis/contents/file.c b/src/analysis/contents/file.c index 5beb0e3..951828f 100644 --- a/src/analysis/contents/file.c +++ b/src/analysis/contents/file.c @@ -44,6 +44,8 @@ struct _GFileContent  {      GObject parent;                         /* A laisser en premier        */ +    GContentAttributes *attribs;            /* Attributs liés au contenu   */ +      char *filename;                         /* Fichier chargé en mémoire   */      bin_t *data;                            /* Contenu binaire représenté  */ @@ -74,6 +76,12 @@ static void g_file_content_dispose(GFileContent *);  /* Procède à la libération totale de la mémoire. */  static void g_file_content_finalize(GFileContent *); +/* Associe un ensemble d'attributs au contenu binaire. */ +static void g_file_content_set_attributes(GFileContent *, GContentAttributes *); + +/* Fournit l'ensemble des attributs associés à un contenu. */ +static GContentAttributes *g_file_content_get_attributes(const GFileContent *); +  /* Donne l'origine d'un contenu binaire. */  static GBinContent *g_file_content_get_root(GFileContent *); @@ -170,6 +178,13 @@ static void g_file_content_class_init(GFileContentClass *klass)  static void g_file_content_init(GFileContent *content)  { +    GContentAttributes *empty;              /* Jeu d'attributs vide        */ + +    content->attribs = NULL; + +    empty = g_content_attributes_new(""); + +    g_binary_content_set_attributes(G_BIN_CONTENT(content), empty);  } @@ -188,6 +203,9 @@ static void g_file_content_init(GFileContent *content)  static void g_file_content_interface_init(GBinContentInterface *iface)  { +    iface->set_attribs = (set_content_attributes)g_file_content_set_attributes; +    iface->get_attribs = (get_content_attributes)g_file_content_get_attributes; +      iface->get_root = (get_content_root_fc)g_file_content_get_root;      iface->describe = (describe_content_fc)g_file_content_describe; @@ -231,6 +249,8 @@ static void g_file_content_interface_init(GBinContentInterface *iface)  static void g_file_content_dispose(GFileContent *content)  { +    g_clear_object(&content->attribs); +      G_OBJECT_CLASS(g_file_content_parent_class)->dispose(G_OBJECT(content));  } @@ -385,6 +405,49 @@ GBinContent *g_file_content_new_from_xml(xmlXPathContextPtr context, const char  /******************************************************************************  *                                                                             * +*  Paramètres  : content = contenu binaire à actualiser.                      * +*                attribs = jeu d'attributs à lier au contenu courant.         * +*                                                                             * +*  Description : Associe un ensemble d'attributs au contenu binaire.          * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_file_content_set_attributes(GFileContent *content, GContentAttributes *attribs) +{ +    content->attribs = attribs; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : content = contenu binaire à consulter.                       * +*                                                                             * +*  Description : Fournit l'ensemble des attributs associés à un contenu.      * +*                                                                             * +*  Retour      : Jeu d'attributs liés au contenu courant.                     * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static GContentAttributes *g_file_content_get_attributes(const GFileContent *content) +{ +    GContentAttributes *result;             /* Instance à retourner        */ + +    result = content->attribs; + +    return result; + +} + + +/****************************************************************************** +*                                                                             *  *  Paramètres  : content = contenu binaire à consulter.                       *  *                                                                             *  *  Description : Donne l'origine d'un contenu binaire.                        *  | 
