diff options
Diffstat (limited to 'plugins/kaitai/import.c')
-rw-r--r-- | plugins/kaitai/import.c | 142 |
1 files changed, 128 insertions, 14 deletions
diff --git a/plugins/kaitai/import.c b/plugins/kaitai/import.c index 88cd03a..516a091 100644 --- a/plugins/kaitai/import.c +++ b/plugins/kaitai/import.c @@ -29,13 +29,23 @@ #include <string.h> +#include <common/environment.h> +#include <core/logs.h> + + /* Charge un type Kaitai à partir d'une définition voisine. */ static GKaitaiType *import_relative_kaitai_definition(const char *, const char *); +/* Charge un type Kaitai depuis un emplacement de $KSPATH. */ +static GKaitaiType *import_kaitai_definition_from_env(const char *); + /* Charge un interpréteur pour une définition voisine. */ static GKaitaiStruct *load_relative_kaitai_definition(const char *, const char *); +/* Charge un interpréteur depuis un emplacement de $KSPATH. */ +static GKaitaiStruct *load_kaitai_definition_from_env(const char *); + /****************************************************************************** @@ -82,6 +92,63 @@ static GKaitaiType *import_relative_kaitai_definition(const char *target, const /****************************************************************************** * * +* Paramètres : target = désignation de la définition à retrouver. * +* * +* Description : Charge un type Kaitai depuis un emplacement de $KSPATH. * +* * +* Retour : Type valide en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GKaitaiType *import_kaitai_definition_from_env(const char *target) +{ + GKaitaiType *result; /* Structure chargée à renvoyer*/ + char *paths; /* Emplacements de greffons */ + char *save; /* Sauvegarde pour ré-entrance */ + char *path; /* Chemin à fouiller */ + char *filename; /* Nom de fichier à tester */ + int ret; /* Bilan d'une construction */ + + result = NULL; + + paths = get_env_var("KSPATH"); + + save = NULL; /* gcc... */ + + for (path = strtok_r(paths, ":", &save); + path != NULL; + path = strtok_r(NULL, ":", &save)) + { + ret = asprintf(&filename, "%s%c%s.ksy", path, G_DIR_SEPARATOR, target); + if (ret == -1) + { + LOG_ERROR_N("asprintf"); + continue; + } + + result = g_kaitai_type_new_as_import(target, filename); + + free(filename); + + if (result != NULL) + { + log_variadic_message(LMT_PROCESS, _("Found a required Kaitai definition: %s"), filename); + break; + } + + } + + free(paths); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : target = désignation de la définition à retrouver. * * reference = éventuel fichier pour les positions relatives. * * * @@ -100,15 +167,10 @@ GKaitaiType *import_kaitai_definition(const char *target, const char *reference) result = NULL; if (reference != NULL) - { result = import_relative_kaitai_definition(target, reference); - if (result != NULL) - goto done; - - } - - done: + if (result == NULL) + result = import_kaitai_definition_from_env(target); return result; @@ -159,6 +221,63 @@ static GKaitaiStruct *load_relative_kaitai_definition(const char *target, const /****************************************************************************** * * +* Paramètres : target = désignation de la définition à retrouver. * +* * +* Description : Charge un interpréteur depuis un emplacement de $KSPATH. * +* * +* Retour : Interprétateur valide en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GKaitaiStruct *load_kaitai_definition_from_env(const char *target) +{ + GKaitaiStruct *result; /* Structure chargée à renvoyer*/ + char *paths; /* Emplacements de greffons */ + char *save; /* Sauvegarde pour ré-entrance */ + char *path; /* Chemin à fouiller */ + char *filename; /* Nom de fichier à tester */ + int ret; /* Bilan d'une construction */ + + result = NULL; + + paths = get_env_var("KSPATH"); + + save = NULL; /* gcc... */ + + for (path = strtok_r(paths, ":", &save); + path != NULL; + path = strtok_r(NULL, ":", &save)) + { + ret = asprintf(&filename, "%s%c%s.ksy", path, G_DIR_SEPARATOR, target); + if (ret == -1) + { + LOG_ERROR_N("asprintf"); + continue; + } + + result = g_kaitai_structure_new_from_file(filename); + + free(filename); + + if (result != NULL) + { + log_variadic_message(LMT_PROCESS, _("Found a required Kaitai definition: %s"), filename); + break; + } + + } + + free(paths); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : target = désignation de la définition à retrouver. * * reference = éventuel fichier pour les positions relatives. * * * @@ -177,15 +296,10 @@ GKaitaiStruct *load_kaitai_definition(const char *target, const char *reference) result = NULL; if (reference != NULL) - { result = load_relative_kaitai_definition(target, reference); - if (result != NULL) - goto done; - - } - - done: + if (result == NULL) + result = load_kaitai_definition_from_env(target); return result; |