diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2023-05-24 00:38:15 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2023-05-24 00:38:15 (GMT) |
commit | 5eab5f1bf3665e948e2054817fb688963dc86935 (patch) | |
tree | 0d0547b7fd90cf51992a449bdca0d9f7b38b3174 /plugins/kaitai/python/records | |
parent | 9f4abb8a20871c64b33f88ad5538bbbe111c1d4c (diff) |
Define a first implementation of Kaitai parsing.
Diffstat (limited to 'plugins/kaitai/python/records')
-rw-r--r-- | plugins/kaitai/python/records/Makefile.am | 18 | ||||
-rw-r--r-- | plugins/kaitai/python/records/empty.c | 286 | ||||
-rw-r--r-- | plugins/kaitai/python/records/empty.h | 45 | ||||
-rw-r--r-- | plugins/kaitai/python/records/group.c | 305 | ||||
-rw-r--r-- | plugins/kaitai/python/records/group.h | 45 | ||||
-rw-r--r-- | plugins/kaitai/python/records/item.c | 394 | ||||
-rw-r--r-- | plugins/kaitai/python/records/item.h | 45 | ||||
-rw-r--r-- | plugins/kaitai/python/records/list.c | 336 | ||||
-rw-r--r-- | plugins/kaitai/python/records/list.h | 45 | ||||
-rw-r--r-- | plugins/kaitai/python/records/module.c | 122 | ||||
-rw-r--r-- | plugins/kaitai/python/records/module.h | 41 | ||||
-rw-r--r-- | plugins/kaitai/python/records/value.c | 335 | ||||
-rw-r--r-- | plugins/kaitai/python/records/value.h | 45 |
13 files changed, 2062 insertions, 0 deletions
diff --git a/plugins/kaitai/python/records/Makefile.am b/plugins/kaitai/python/records/Makefile.am new file mode 100644 index 0000000..1413228 --- /dev/null +++ b/plugins/kaitai/python/records/Makefile.am @@ -0,0 +1,18 @@ + +noinst_LTLIBRARIES = libkaitaipythonrecords.la + +libkaitaipythonrecords_la_SOURCES = \ + empty.h empty.c \ + group.h group.c \ + item.h item.c \ + list.h list.c \ + module.h module.c \ + value.h value.c + +libkaitaipythonrecords_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ + -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libkaitaipythonrecords_la_SOURCES:%c=) diff --git a/plugins/kaitai/python/records/empty.c b/plugins/kaitai/python/records/empty.c new file mode 100644 index 0000000..9861a39 --- /dev/null +++ b/plugins/kaitai/python/records/empty.c @@ -0,0 +1,286 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * empty.c - équivalent Python du fichier "plugins/kaitai/parsers/empty.c" + * + * Copyright (C) 2023 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "empty.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> +#include <plugins/pychrysalide/analysis/content.h> +#include <plugins/pychrysalide/arch/vmpa.h> + + +#include "../parser.h" +#include "../record.h" +#include "../../records/empty-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_empty, G_TYPE_RECORD_EMPTY); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_empty_init(PyObject *, PyObject *, PyObject *); + +/* Produit une absence de valeur pour la correspondance. */ +static PyObject *py_record_empty_get_value(PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_record_empty_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GKaitaiParser *parser; /* Analyseur défini créateur */ + GBinContent *content; /* Contenu binaire manipulé */ + vmpa2t *pos; /* Tête de lecture courante */ + int ret; /* Bilan de lecture des args. */ + GRecordEmpty *empty; /* Création GLib à transmettre */ + +#define RECORD_EMPTY_DOC \ + "The RecordEmpty object reflects absolutely no match and should only get" \ + " in some rare cases.\n" \ + "\n" \ + "Instances can be created using following constructor:\n" \ + "\n" \ + " RecordEmpty(parser, content, pos)" \ + "\n" \ + "Where *parser* is the creator of the record, as a" \ + " pychrysalide.plugins.kaitai.KaitaiParser instance, *content* is a" \ + " pychrysalide.analysis.BinContent instance providing the processed data" \ + " and *pos* defines the current reading location, as a" \ + " pychrysalide.arch.vmpa value." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&O&O&", + convert_to_kaitai_parser, &parser, + convert_to_binary_content, &content, + convert_any_to_vmpa, &pos); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + empty = G_RECORD_EMPTY(pygobject_get(self)); + + if (!g_record_empty_create(empty, parser, content, pos)) + { + clean_vmpa_arg(pos); + + PyErr_SetString(PyExc_ValueError, _("Unable to create Kaitai stream.")); + return -1; + + } + + clean_vmpa_arg(pos); + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Produit une absence de valeur pour la correspondance. * +* * +* Retour : None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_empty_get_value(PyObject *self, void *closure) +{ + PyObject *result; /* Valeur à retourner */ + +#define RECORD_EMPTY_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + value, py_record_empty, \ + "Always *None*.\n" \ + "\n" \ + "This attribute is only provided to mimic other" \ + " record types." \ +) + + result = Py_None; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_record_empty_type(void) +{ + static PyMethodDef py_record_empty_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_empty_getseters[] = { + RECORD_EMPTY_VALUE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_record_empty_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordEmpty", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_EMPTY_DOC, + + .tp_methods = py_record_empty_methods, + .tp_getset = py_record_empty_getseters, + + .tp_init = py_record_empty_init, + .tp_new = py_record_empty_new, + + }; + + return &py_record_empty_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide...records.RecordEmpty. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_empty_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordEmpty' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_empty_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.plugins.kaitai.records"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_match_record_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_EMPTY, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en zone de correspondance vide. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_record_empty(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_empty_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record empty"); + break; + + case 1: + *((GRecordEmpty **)dst) = G_RECORD_EMPTY(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/empty.h b/plugins/kaitai/python/records/empty.h new file mode 100644 index 0000000..ecd5fc9 --- /dev/null +++ b/plugins/kaitai/python/records/empty.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * empty.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/empty.h" + * + * Copyright (C) 2023 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_EMPTY_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_EMPTY_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_empty_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordEmpty'. */ +bool ensure_python_record_empty_is_registered(void); + +/* Tente de convertir en zone de correspondance vide. */ +int convert_to_record_empty(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_EMPTY_H */ diff --git a/plugins/kaitai/python/records/group.c b/plugins/kaitai/python/records/group.c new file mode 100644 index 0000000..a050043 --- /dev/null +++ b/plugins/kaitai/python/records/group.c @@ -0,0 +1,305 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * group.h - équivalent Python du fichier "plugins/kaitai/parsers/group.h" + * + * Copyright (C) 2023 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "group.h" + + +#include <pygobject.h> +#include <string.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> +#include <plugins/pychrysalide/analysis/content.h> +#include <plugins/pychrysalide/arch/vmpa.h> +#include <plugins/yaml/python/node.h> + + +#include "../record.h" +#include "../parsers/struct.h" +#include "../../records/group-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_group, G_TYPE_RECORD_GROUP); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_group_init(PyObject *, PyObject *, PyObject *); + +/* Assure l'encadrement des accès aux champs d'une séquence. */ +static PyObject *py_record_group_getattr(PyObject *, char *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_record_group_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GKaitaiStruct *kstruct; /* Séquence définie créatrice */ + GBinContent *content; /* Contenu binaire analysé */ + int ret; /* Bilan de lecture des args. */ + GRecordGroup *group; /* Création GLib à transmettre */ + +#define RECORD_GROUP_DOC \ + "The RecordGroup class stores a map of parsed attributes with their" \ + " relative values. Each of theses Kaitai attributes can be accessed as" \ + " usual Python attribute.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " RecordGroup(kstruct, content)" \ + "\n" \ + "Where the *kstruct* refers to a" \ + " pychrysalide.plugins.kaitai.parsers.KaitaiStructure instance as the" \ + " creator of the newly created object, and *content* points to a" \ + " pychrysalide.analysis.BinContent instance." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&O&", + convert_to_kaitai_structure, &kstruct, + convert_to_binary_content, &content); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + group = G_RECORD_GROUP(pygobject_get(self)); + + if (!g_record_group_create(group, kstruct, content)) + { + PyErr_SetString(PyExc_ValueError, _("Unable to create record group.")); + return -1; + } + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : self = structure C convertie en Python. * +* name = nom du champ auquel un accès est demandé. * +* * +* Description : Assure l'encadrement des accès aux champs d'une séquence. * +* * +* Retour : Valeur du champ demandé. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_group_getattr(PyObject *self, char *name) +{ + PyObject *result; /* Elément à retourner */ + GRecordGroup *group; /* Version native de l'objet */ + GMatchRecord *found; /* Sous-élément identifié */ + PyObject *w; /* Conversion du nom de champ */ + PyTypeObject *tp; /* Type de l'objet manipulé */ + + group = G_RECORD_GROUP(pygobject_get(self)); + + found = g_match_record_find_by_name(G_MATCH_RECORD(group), name, strlen(name), DIRECT_SEARCH_DEEP_LEVEL); + + if (found != NULL) + { + result = pygobject_new(G_OBJECT(found)); + g_object_unref(G_OBJECT(found)); + } + + else + { + w = PyUnicode_InternFromString(name); + if (w == NULL) return NULL; + + tp = Py_TYPE(self); + + if (tp->tp_base->tp_getattro != NULL) + result = tp->tp_base->tp_getattro(self, w); + + else + { + PyErr_Format(PyExc_AttributeError, + "type object '%.50s' has no attribute '%U'", + tp->tp_name, name); + result = NULL; + } + + Py_DECREF(w); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_record_group_type(void) +{ + static PyMethodDef py_record_group_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_group_getseters[] = { + { NULL } + }; + + static PyTypeObject py_record_group_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordGroup", + .tp_basicsize = sizeof(PyGObject), + + .tp_getattr = py_record_group_getattr, + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_GROUP_DOC, + + .tp_methods = py_record_group_methods, + .tp_getset = py_record_group_getseters, + + .tp_init = py_record_group_init, + .tp_new = py_record_group_new, + + }; + + return &py_record_group_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide...records.RecordGroup. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_group_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordGroup' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_group_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.plugins.kaitai.records"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_match_record_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_GROUP, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en correspondances attribut/binaire. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_record_group(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_group_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record group"); + break; + + case 1: + *((GRecordGroup **)dst) = G_RECORD_GROUP(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/group.h b/plugins/kaitai/python/records/group.h new file mode 100644 index 0000000..3e12ffc --- /dev/null +++ b/plugins/kaitai/python/records/group.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * group.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/group.h" + * + * Copyright (C) 2023 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_GROUP_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_GROUP_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_group_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordGroup'. */ +bool ensure_python_record_group_is_registered(void); + +/* Tente de convertir en correspondances attribut/binaire. */ +int convert_to_record_group(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_GROUP_H */ diff --git a/plugins/kaitai/python/records/item.c b/plugins/kaitai/python/records/item.c new file mode 100644 index 0000000..84c2c58 --- /dev/null +++ b/plugins/kaitai/python/records/item.c @@ -0,0 +1,394 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * item.h - équivalent Python du fichier "plugins/kaitai/parsers/item.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "item.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> +#include <plugins/pychrysalide/analysis/content.h> +#include <plugins/pychrysalide/arch/vmpa.h> +#include <plugins/yaml/python/node.h> + + +#include "../record.h" +#include "../parsers/attribute.h" +#include "../../records/item-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_item, G_TYPE_RECORD_ITEM); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_item_init(PyObject *, PyObject *, PyObject *); + +/* Lit la série d'octets d'un élément Kaitai entier représenté. */ +static PyObject *py_record_item_get_truncated_bytes(PyObject *, void *); + +/* Lit la valeur d'un élément Kaitai entier représenté. */ +static PyObject *py_record_item_get_value(PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_record_item_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GKaitaiAttribute *attrib; /* Attribut défini créateur */ + GBinContent *content; /* Contenu binaire analysé */ + mrange_t range; /* Espace couvert */ + SourceEndian endian; /* Boutisme à observer */ + int ret; /* Bilan de lecture des args. */ + GRecordItem *item; /* Création GLib à transmettre */ + +#define RECORD_ITEM_DOC \ + "The RecordItem class remembers a match between a described attribute and" \ + " its concret value read from parsed binary data." \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " RecordItem(content, range, endian, attrib)" \ + "\n" \ + "Where the *attrib* arguments refers to a" \ + " pychrysalide.plugins.kaitai.parsers.KaitaiAttribute instance as the" \ + " creator of the newly created object, *content* points to a" \ + " pychrysalide.analysis.BinContent instance, *range* is a" \ + " pychrysalide.arch.mrange object, *endian* states with a" \ + " pychrysalide.analysis.BinContent.SourceEndian hint the endianness used" \ + " to read integer values." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&O&O&", + convert_to_kaitai_attribute, &attrib, + convert_to_binary_content, &content, + convert_any_to_mrange, &range, + convert_to_binary_content, &endian); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + item = G_RECORD_ITEM(pygobject_get(self)); + + if (!g_record_item_create(item, attrib, content, &range, endian)) + { + PyErr_SetString(PyExc_ValueError, _("Unable to create record item.")); + return -1; + } + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Lit la série d'octets d'un élément Kaitai entier représenté. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_item_get_truncated_bytes(PyObject *self, void *closure) +{ + PyObject *result; /* Valeur à retourner */ + GRecordItem *item; /* Version native de l'élément */ + bool status; /* Bilan d'opération */ + bin_t *out; /* Données brutes à transmettre*/ + size_t len; /* Quantité de ces données */ + +#define RECORD_ITEM_TRUNCATED_BYTES_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + truncated_bytes, py_record_item, \ + "Raw bytes carried by the item (truncated if a separator" \ + " is defined in the linked attribute), or None if irrelevant" \ + " regarding to the type of the attribute." \ +) + + item = G_RECORD_ITEM(pygobject_get(self)); + + status = g_record_item_get_truncated_bytes(item, &out, &len); + + if (status) + { + result = PyBytes_FromStringAndSize((char *)out, len); + free(out); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Lit la valeur d'un élément Kaitai entier représenté. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_item_get_value(PyObject *self, void *closure) +{ + PyObject *result; /* Valeur à retourner */ + GRecordItem *item; /* Version native de l'élément */ + resolved_value_t resolved; /* Valeur sous forme générique */ + bool status; /* Bilan d'opération */ + +#define RECORD_ITEM_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + value, py_record_item, \ + "Carried value (as integer, bytes), or None in case of error." \ +) + + result = NULL; + + item = G_RECORD_ITEM(pygobject_get(self)); + + status = g_record_item_get_value(item, &resolved); + + if (status) + switch (resolved.type) + { + case GVT_ERROR: + assert(false); + PyErr_Format(PyExc_RuntimeError, + _("Error got while parsing Kaitai definition should not have been exported!")); + result = NULL; + break; + + case GVT_UNSIGNED_INTEGER: + result = PyLong_FromUnsignedLongLong(resolved.unsigned_integer); + break; + + case GVT_SIGNED_INTEGER: + result = PyLong_FromLongLong(resolved.signed_integer); + break; + + case GVT_FLOAT: + result = PyFloat_FromDouble(resolved.floating_number); + break; + + case GVT_BOOLEAN: + result = resolved.status ? Py_True : Py_False; + Py_INCREF(result); + break; + + case GVT_BYTES: + result = PyBytes_FromStringAndSize(resolved.bytes.data, resolved.bytes.len); + exit_szstr(&resolved.bytes); + break; + + case GVT_ARRAY: + result = pygobject_new(G_OBJECT(resolved.array)); + break; + + case GVT_RECORD: + result = pygobject_new(G_OBJECT(resolved.record)); + break; + + case GVT_STREAM: + result = pygobject_new(G_OBJECT(resolved.stream)); + break; + + } + + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_record_item_type(void) +{ + static PyMethodDef py_record_item_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_item_getseters[] = { + RECORD_ITEM_TRUNCATED_BYTES_ATTRIB, + RECORD_ITEM_VALUE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_record_item_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordItem", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_ITEM_DOC, + + .tp_methods = py_record_item_methods, + .tp_getset = py_record_item_getseters, + + .tp_init = py_record_item_init, + .tp_new = py_record_item_new, + + }; + + return &py_record_item_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide....records.RecordItem. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_item_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordItem' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_item_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.plugins.kaitai.records"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_match_record_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_ITEM, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en correspondance attribut/binaire. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_record_item(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_item_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record item"); + break; + + case 1: + *((GRecordItem **)dst) = G_RECORD_ITEM(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/item.h b/plugins/kaitai/python/records/item.h new file mode 100644 index 0000000..bde8a55 --- /dev/null +++ b/plugins/kaitai/python/records/item.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * item.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/item.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_ITEM_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_ITEM_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_item_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordItem'. */ +bool ensure_python_record_item_is_registered(void); + +/* Tente de convertir en correspondance attribut/binaire. */ +int convert_to_record_item(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_ITEM_H */ diff --git a/plugins/kaitai/python/records/list.c b/plugins/kaitai/python/records/list.c new file mode 100644 index 0000000..d2eecbb --- /dev/null +++ b/plugins/kaitai/python/records/list.c @@ -0,0 +1,336 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * list.h - équivalent Python du fichier "plugins/kaitai/parsers/list.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "list.h" + + +#include <pygobject.h> +#include <string.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> +#include <plugins/pychrysalide/analysis/content.h> +#include <plugins/pychrysalide/arch/vmpa.h> +#include <plugins/yaml/python/node.h> + + +#include "../record.h" +#include "../parsers/attribute.h" +#include "../../records/list-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_list, G_TYPE_RECORD_LIST); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_list_init(PyObject *, PyObject *, PyObject *); + +/* Dénombre le nombre de correspondances enregistrées. */ +static Py_ssize_t py_record_list_sq_length(PyObject *); + +/* Fournit un élément ciblé dans la liste de correspondances. */ +static PyObject *py_record_list_sq_item(PyObject *, Py_ssize_t); + + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_record_list_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int result; /* Bilan à retourner */ + GKaitaiAttribute *attrib; /* Attribut défini créateur */ + GBinContent *content; /* Contenu binaire analysé */ + vmpa2t *addr; /* Adresse de symbole à ajouter*/ + int ret; /* Bilan de lecture des args. */ + GRecordList *list; /* Création GLib à transmettre */ + +#define RECORD_LIST_DOC \ + "The RecordList class collects a list of parsed attributes with their" \ + " relative values. Each of theses Kaitai attributes can be accessed as" \ + " subscriptable Python attribute.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " RecordList(content, attrib)" \ + "\n" \ + "Where the *attrib* argument refers to the" \ + " pychrysalide.plugins.kaitai.parsers.KaitaiAttribute instance used to" \ + " create each record contained by the list and *content* points to a" \ + " pychrysalide.analysis.BinContent instance." + + result = 0; + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&O&", + convert_to_kaitai_attribute, &attrib, + convert_to_binary_content, &content, + convert_any_to_vmpa, &addr); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) + { + result = -1; + goto exit; + } + + /* Eléments de base */ + + list = G_RECORD_LIST(pygobject_get(self)); + + if (!g_record_list_create(list, attrib, content, addr)) + { + PyErr_SetString(PyExc_ValueError, _("Unable to create record list.")); + result = -1; + goto exit; + } + + exit: + + clean_vmpa_arg(addr); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = instance Python manipulée. * +* * +* Description : Dénombre le nombre de correspondances enregistrées. * +* * +* Retour : Taille de la liste représentée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static Py_ssize_t py_record_list_sq_length(PyObject *self) +{ + Py_ssize_t result; /* Quantité à retourner */ + GRecordList *list; /* Version native de l'objet */ + + list = G_RECORD_LIST(pygobject_get(self)); + + result = g_record_list_count_records(list); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = structure C convertie en Python. * +* index = indice de la correspondance visée. * +* * +* Description : Fournit un élément ciblé dans la liste de correspondances. * +* * +* Retour : Instance de correspondance particulière, voire None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_list_sq_item(PyObject *self, Py_ssize_t index) +{ + PyObject *result; /* Instance à retourner */ + GRecordList *list; /* Version native de l'objet */ + GMatchRecord *record; /* Correspondance retrouvée */ + + list = G_RECORD_LIST(pygobject_get(self)); + + record = g_record_list_get_record(list, index); + + if (record != NULL) + { + result = pygobject_new(G_OBJECT(record)); + g_object_unref(G_OBJECT(record)); + } + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_record_list_type(void) +{ + static PySequenceMethods py_record_list_sequence_methods = { + + .sq_length = py_record_list_sq_length, + .sq_item = py_record_list_sq_item, + + }; + + static PyMethodDef py_record_list_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_list_getseters[] = { + { NULL } + }; + + static PyTypeObject py_record_list_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordList", + .tp_basicsize = sizeof(PyGObject), + + .tp_as_sequence = &py_record_list_sequence_methods, + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_LIST_DOC, + + .tp_methods = py_record_list_methods, + .tp_getset = py_record_list_getseters, + + .tp_init = py_record_list_init, + .tp_new = py_record_list_new, + + }; + + return &py_record_list_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide....records.RecordList. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_list_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordList' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_list_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.plugins.kaitai.records"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_match_record_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_LIST, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en correspondance attribut/binaire. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_record_list(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_list_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record list"); + break; + + case 1: + *((GRecordList **)dst) = G_RECORD_LIST(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/list.h b/plugins/kaitai/python/records/list.h new file mode 100644 index 0000000..53572a9 --- /dev/null +++ b/plugins/kaitai/python/records/list.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * list.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/list.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_LIST_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_LIST_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_list_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordList'. */ +bool ensure_python_record_list_is_registered(void); + +/* Tente de convertir en correspondance attribut/binaire. */ +int convert_to_record_list(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_LIST_H */ diff --git a/plugins/kaitai/python/records/module.c b/plugins/kaitai/python/records/module.c new file mode 100644 index 0000000..ea33c31 --- /dev/null +++ b/plugins/kaitai/python/records/module.c @@ -0,0 +1,122 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire records en tant que module + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "module.h" + + +#include <Python.h> + + +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> + + +#include "empty.h" +#include "group.h" +#include "item.h" +#include "list.h" +#include "value.h" + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Ajoute le module 'plugins.kaitai.records' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_kaitai_records_module(void) +{ + bool result; /* Bilan à retourner */ + PyObject *super; /* Module à compléter */ + PyObject *module; /* Sous-module mis en place */ + +#define PYCHRYSALIDE_PLUGINS_KAITAI_RECORDS_DOC \ + "This module is providing objects used to link structure" \ + " definitions with their data." + + static PyModuleDef py_chrysalide_kaitai_records_module = { + + .m_base = PyModuleDef_HEAD_INIT, + + .m_name = "pychrysalide.plugins.kaitai.records", + .m_doc = PYCHRYSALIDE_PLUGINS_KAITAI_RECORDS_DOC, + + .m_size = -1, + + }; + + result = false; + + super = get_access_to_python_module("pychrysalide.plugins.kaitai"); + + module = build_python_module(super, &py_chrysalide_kaitai_records_module); + + result = (module != NULL); + + assert(result); + + if (!result) + Py_XDECREF(module); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Intègre les objets du module 'plugins.kaitai.records'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_kaitai_records_module(void) +{ + bool result; /* Bilan à retourner */ + + result = true; + + if (result) result = ensure_python_record_empty_is_registered(); + if (result) result = ensure_python_record_group_is_registered(); + if (result) result = ensure_python_record_item_is_registered(); + if (result) result = ensure_python_record_list_is_registered(); + if (result) result = ensure_python_record_value_is_registered(); + + assert(result); + + return result; + +} diff --git a/plugins/kaitai/python/records/module.h b/plugins/kaitai/python/records/module.h new file mode 100644 index 0000000..ff631dc --- /dev/null +++ b/plugins/kaitai/python/records/module.h @@ -0,0 +1,41 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire records en tant que module + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_MODULE_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_MODULE_H + + +#include <stdbool.h> + + + +/* Ajoute le module 'plugins.kaitai.records' au module Python. */ +bool add_kaitai_records_module(void); + +/* Intègre les objets du module 'plugins.kaitai.records'. */ +bool populate_kaitai_records_module(void); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_MODULE_H */ diff --git a/plugins/kaitai/python/records/value.c b/plugins/kaitai/python/records/value.c new file mode 100644 index 0000000..bd4ad74 --- /dev/null +++ b/plugins/kaitai/python/records/value.c @@ -0,0 +1,335 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * value.c - équivalent Python du fichier "plugins/kaitai/parsers/value.c" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "value.h" + + +#include <assert.h> +#include <pygobject.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> +#include <plugins/pychrysalide/analysis/content.h> +#include <plugins/pychrysalide/arch/vmpa.h> +#include <plugins/yaml/python/node.h> + + +#include "../record.h" +#include "../scope.h" +#include "../parsers/instance.h" +#include "../../records/value-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_value, G_TYPE_RECORD_VALUE); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_value_init(PyObject *, PyObject *, PyObject *); + +/* Lit la valeur d'un élément Kaitai entier représenté. */ +static PyObject *py_record_value_get_value(PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_record_value_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GKaitaiInstance *inst; /* Instance définie créatrice */ + kaitai_scope_t *locals; /* Environnement local */ + int ret; /* Bilan de lecture des args. */ + GRecordValue *value; /* Création GLib à transmettre */ + +#define RECORD_VALUE_DOC \ + "The RecordValue class stores a link to an instance used to compute a" \ + " given value." \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " RecordValue(inst, locals)" \ + "\n" \ + "Where the *inst* arguments refers to a" \ + " pychrysalide.plugins.kaitai.parsers.KaitaiInstance instance as the" \ + " creator of the newly created object, *locals* points to a" \ + " pychrysalide.plugins.kaitai.KaitaiScope structure used as current scope." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&O&", + convert_to_kaitai_instance, &inst, + convert_to_kaitai_scope, &locals); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + ret = forward_pygobjet_init(self); + if (ret == -1) return -1; + + /* Eléments de base */ + + value = G_RECORD_VALUE(pygobject_get(self)); + + if (!g_record_value_create(value, inst, locals)) + { + PyErr_SetString(PyExc_ValueError, _("Unable to create record value.")); + return -1; + } + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Lit la valeur d'un élément Kaitai entier représenté. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_record_value_get_value(PyObject *self, void *closure) +{ + PyObject *result; /* Valeur à retourner */ + GRecordValue *value; /* Version native de l'élément */ + resolved_value_t resolved; /* Valeur sous forme générique */ + bool status; /* Bilan d'opération */ + +#define RECORD_VALUE_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + value, py_record_value, \ + "Carried value (as integer, bytes), or None in case of error." \ +) + + result = NULL; + + value = G_RECORD_VALUE(pygobject_get(self)); + + status = g_record_value_compute_and_aggregate_value(value, &resolved); + + if (status) + switch (resolved.type) + { + case GVT_ERROR: + assert(false); + PyErr_Format(PyExc_RuntimeError, + _("Error got while parsing Kaitai definition should not have been exported!")); + result = NULL; + break; + + case GVT_UNSIGNED_INTEGER: + result = PyLong_FromUnsignedLongLong(resolved.unsigned_integer); + break; + + case GVT_SIGNED_INTEGER: + result = PyLong_FromLongLong(resolved.signed_integer); + break; + + case GVT_FLOAT: + result = PyFloat_FromDouble(resolved.floating_number); + break; + + case GVT_BOOLEAN: + result = resolved.status ? Py_True : Py_False; + Py_INCREF(result); + break; + + case GVT_BYTES: + result = PyBytes_FromStringAndSize(resolved.bytes.data, resolved.bytes.len); + exit_szstr(&resolved.bytes); + break; + + case GVT_ARRAY: + result = pygobject_new(G_OBJECT(resolved.array)); + break; + + case GVT_RECORD: + result = pygobject_new(G_OBJECT(resolved.record)); + break; + + case GVT_STREAM: + result = pygobject_new(G_OBJECT(resolved.stream)); + break; + + } + + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_record_value_type(void) +{ + static PyMethodDef py_record_value_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_value_getseters[] = { + RECORD_VALUE_VALUE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_record_value_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordValue", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_VALUE_DOC, + + .tp_methods = py_record_value_methods, + .tp_getset = py_record_value_getseters, + + .tp_init = py_record_value_init, + .tp_new = py_record_value_new, + + }; + + return &py_record_value_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide...records.RecordValue. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_value_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordValue' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_value_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.plugins.kaitai.records"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_match_record_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_VALUE, type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en valeur calculée. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_record_value(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_value_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record value"); + break; + + case 1: + *((GRecordValue **)dst) = G_RECORD_VALUE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/value.h b/plugins/kaitai/python/records/value.h new file mode 100644 index 0000000..16cadcb --- /dev/null +++ b/plugins/kaitai/python/records/value.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * value.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/value.h" + * + * 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_value_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordValue'. */ +bool ensure_python_record_value_is_registered(void); + +/* Tente de convertir en valeur calculée. */ +int convert_to_record_value(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H */ |