summaryrefslogtreecommitdiff
path: root/plugins/kaitai/parsers/struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/kaitai/parsers/struct.c')
-rw-r--r--plugins/kaitai/parsers/struct.c68
1 files changed, 64 insertions, 4 deletions
diff --git a/plugins/kaitai/parsers/struct.c b/plugins/kaitai/parsers/struct.c
index 128a788..07126c6 100644
--- a/plugins/kaitai/parsers/struct.c
+++ b/plugins/kaitai/parsers/struct.c
@@ -33,6 +33,7 @@
#include "struct-int.h"
+#include "../import.h"
#include "../parser.h"
#include "../records/empty.h"
#include "../records/group.h"
@@ -54,6 +55,9 @@ static void g_kaitai_structure_dispose(GKaitaiStruct *);
/* Procède à la libération totale de la mémoire. */
static void g_kaitai_structure_finalize(GKaitaiStruct *);
+/* Charge les éventuelles dépendances de la définition. */
+static bool g_kaitai_structure_load_imports(GKaitaiStruct *);
+
/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */
@@ -116,6 +120,8 @@ static void g_kaitai_structure_class_init(GKaitaiStructClass *klass)
static void g_kaitai_structure_init(GKaitaiStruct *kstruct)
{
+ kstruct->filename = NULL;
+
kstruct->meta = NULL;
kstruct->seq_items = NULL;
@@ -182,6 +188,9 @@ static void g_kaitai_structure_dispose(GKaitaiStruct *kstruct)
static void g_kaitai_structure_finalize(GKaitaiStruct *kstruct)
{
+ if (kstruct->filename != NULL)
+ free(kstruct->filename);
+
if (kstruct->seq_items != NULL)
free(kstruct->seq_items);
@@ -305,6 +314,8 @@ bool g_kaitai_structure_create_from_file(GKaitaiStruct *kstruct, const char *fil
bool result; /* Bilan à retourner */
GYamlNode *root; /* Noeud racine YAML */
+ kstruct->filename = strdup(filename);
+
root = parse_yaml_from_file(filename);
if (root != NULL)
@@ -343,6 +354,7 @@ bool g_kaitai_structure_create(GKaitaiStruct *kstruct, GYamlNode *parent)
GYamlNode **nodes; /* Eventuels noeuds trouvés */
size_t count; /* Quantité de ces noeuds */
size_t i; /* Boucle de parcours */
+ size_t first; /* Premier emplacement dispo. */
bool failed; /* Détection d'un échec */
result = false;
@@ -352,6 +364,9 @@ bool g_kaitai_structure_create(GKaitaiStruct *kstruct, GYamlNode *parent)
kstruct->meta = g_kaitai_meta_new(parent);
assert(kstruct->meta != NULL);
+ result = g_kaitai_structure_load_imports(kstruct);
+ if (!result) goto bad_loading;
+
/* Séquence */
collec = g_yaml_node_find_first_by_path(parent, "/seq/");
@@ -406,13 +421,15 @@ bool g_kaitai_structure_create(GKaitaiStruct *kstruct, GYamlNode *parent)
if (count > 0)
{
- kstruct->types = calloc(count, sizeof(GKaitaiType *));
- kstruct->types_count = count;
+ first = kstruct->types_count;
+
+ kstruct->types_count += count;
+ kstruct->types = realloc(kstruct->types, kstruct->types_count * sizeof(GKaitaiType *));
for (i = 0; i < count; i++)
{
- kstruct->types[i] = g_kaitai_type_new(nodes[i]);
- if (kstruct->types[i] == NULL) break;
+ kstruct->types[first + i] = g_kaitai_type_new(nodes[i]);
+ if (kstruct->types[first + i] == NULL) break;
g_object_unref(G_OBJECT(nodes[i]));
@@ -529,6 +546,49 @@ bool g_kaitai_structure_create(GKaitaiStruct *kstruct, GYamlNode *parent)
/******************************************************************************
* *
+* Paramètres : kstruct = lecteur de définition à initialiser pleinement. *
+* *
+* Description : Charge les éventuelles dépendances de la définition. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_kaitai_structure_load_imports(GKaitaiStruct *kstruct)
+{
+ bool result; /* Bilan d'opération à renvoyer*/
+ const char * const *dependencies; /* Liste d'imports requis */
+ size_t count; /* Quantité de ces imports */
+ size_t i; /* Boucle de parcours */
+ GKaitaiType *imported; /* Structure importée */
+
+ result = true;
+
+ dependencies = g_kaitai_meta_get_dependencies(kstruct->meta, &count);
+
+ for (i = 0; i < count; i++)
+ {
+ imported = import_kaitai_definition(dependencies[i], kstruct->filename);
+ if (imported == NULL) break;
+
+ kstruct->types_count++;
+ kstruct->types = realloc(kstruct->types, kstruct->types_count * sizeof(GKaitaiType *));
+
+ kstruct->types[kstruct->types_count - 1] = imported;
+
+ }
+
+ result = (i == count);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
* Paramètres : kstruct = structure Kaitai à consulter. *
* *
* Description : Fournit la description globale d'une définition Kaitai. *