diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2023-10-09 22:49:59 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2023-10-09 22:49:59 (GMT) |
commit | 7c6fe94c90d320813d0d78a9dbef707696f31505 (patch) | |
tree | 912b5c51469c02e6ef680c0c60739787ccff4891 /plugins/kaitai/python | |
parent | cb05b99a8c451ff80d88f988e2654c794b0f3750 (diff) |
Support some last missing features from Kaitai: bit fields, instance search order and stream EOF.
Diffstat (limited to 'plugins/kaitai/python')
-rw-r--r-- | plugins/kaitai/python/records/Makefile.am | 5 | ||||
-rw-r--r-- | plugins/kaitai/python/records/bits.c | 318 | ||||
-rw-r--r-- | plugins/kaitai/python/records/bits.h | 45 | ||||
-rw-r--r-- | plugins/kaitai/python/records/delayed.c (renamed from plugins/kaitai/python/records/value.c) | 94 | ||||
-rw-r--r-- | plugins/kaitai/python/records/delayed.h (renamed from plugins/kaitai/python/records/value.h) | 16 | ||||
-rw-r--r-- | plugins/kaitai/python/records/module.c | 6 |
6 files changed, 428 insertions, 56 deletions
diff --git a/plugins/kaitai/python/records/Makefile.am b/plugins/kaitai/python/records/Makefile.am index 1413228..3a3c672 100644 --- a/plugins/kaitai/python/records/Makefile.am +++ b/plugins/kaitai/python/records/Makefile.am @@ -2,12 +2,13 @@ noinst_LTLIBRARIES = libkaitaipythonrecords.la libkaitaipythonrecords_la_SOURCES = \ + bits.h bits.c \ + delayed.h delayed.c \ empty.h empty.c \ group.h group.c \ item.h item.c \ list.h list.c \ - module.h module.c \ - value.h value.c + module.h module.c libkaitaipythonrecords_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT diff --git a/plugins/kaitai/python/records/bits.c b/plugins/kaitai/python/records/bits.c new file mode 100644 index 0000000..f94148b --- /dev/null +++ b/plugins/kaitai/python/records/bits.c @@ -0,0 +1,318 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * bits.h - équivalent Python du fichier "plugins/kaitai/parsers/bits.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 "bits.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 "../record.h" +#include "../parsers/attribute.h" +#include "../../records/bits-int.h" + + + +CREATE_DYN_CONSTRUCTOR(record_bit_field, G_TYPE_RECORD_BIT_FIELD); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_record_bit_field_init(PyObject *, PyObject *, PyObject *); + +/* Lit la valeur d'un élément Kaitai entier représenté. */ +static PyObject *py_record_bit_field_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_bit_field_init(PyObject *self, PyObject *args, PyObject *kwds) +{ +#if 0 + 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. */ + GRecordBitField *field; /* Création GLib à transmettre */ +#endif + +#define RECORD_BIT_FIELD_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 */ + +#if 0 /* FIXME */ + + 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 */ + + field = G_RECORD_BIT_FIELD(pygobject_get(self)); + + if (!g_record_bit_field_create(field, attrib, content, &range, endian)) + { + PyErr_SetString(PyExc_ValueError, _("Unable to create record bit field.")); + return -1; + } + + return 0; + +#endif + + PyErr_SetString(PyExc_ValueError, _("Unable to create record bit field at the moment.")); + return -1; + +} + + +/****************************************************************************** +* * +* 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_bit_field_get_value(PyObject *self, void *closure) +{ + PyObject *result; /* Valeur à retourner */ + GRecordBitField *field; /* Version native de l'élément */ + resolved_value_t resolved; /* Valeur sous forme générique */ + bool status; /* Bilan d'opération */ + +#define RECORD_BIT_FIELD_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + value, py_record_bit_field, \ + "Carried value (as integer), or None in case of error." \ +) + + result = NULL; + + field = G_RECORD_BIT_FIELD(pygobject_get(self)); + + status = g_record_bit_field_get_value(field, &resolved); + + if (status) + switch (resolved.type) + { + case GVT_UNSIGNED_INTEGER: + result = PyLong_FromUnsignedLongLong(resolved.unsigned_integer); + break; + + default: + assert(false); + result = Py_None; + Py_INCREF(result); + 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_bit_field_type(void) +{ + static PyMethodDef py_record_bit_field_methods[] = { + { NULL } + }; + + static PyGetSetDef py_record_bit_field_getseters[] = { + RECORD_BIT_FIELD_VALUE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_record_bit_field_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.kaitai.records.RecordBitField", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = RECORD_BIT_FIELD_DOC, + + .tp_methods = py_record_bit_field_methods, + .tp_getset = py_record_bit_field_getseters, + + .tp_init = py_record_bit_field_init, + .tp_new = py_record_bit_field_new, + + }; + + return &py_record_bit_field_type; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Prend en charge l'objet 'pychrysalide.....RecordBitField. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_record_bit_field_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RecordItem' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_record_bit_field_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_BIT_FIELD, 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_bit_field(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_bit_field_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 bit field"); + break; + + case 1: + *((GRecordBitField **)dst) = G_RECORD_BIT_FIELD(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/kaitai/python/records/bits.h b/plugins/kaitai/python/records/bits.h new file mode 100644 index 0000000..6c833bb --- /dev/null +++ b/plugins/kaitai/python/records/bits.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * bits.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/bits.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_BITS_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_BITS_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_record_bit_field_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordBitField'. */ +bool ensure_python_record_bit_field_is_registered(void); + +/* Tente de convertir en correspondance attribut/binaire. */ +int convert_to_record_bit_field(PyObject *, void *); + + + +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_BITS_H */ diff --git a/plugins/kaitai/python/records/value.c b/plugins/kaitai/python/records/delayed.c index bd4ad74..32e3db1 100644 --- a/plugins/kaitai/python/records/value.c +++ b/plugins/kaitai/python/records/delayed.c @@ -1,6 +1,6 @@ /* Chrysalide - Outil d'analyse de fichiers binaires - * value.c - équivalent Python du fichier "plugins/kaitai/parsers/value.c" + * delayed.c - équivalent Python du fichier "plugins/kaitai/parsers/delayed.c" * * Copyright (C) 2019 Cyrille Bagard * @@ -22,7 +22,7 @@ */ -#include "value.h" +#include "delayed.h" #include <assert.h> @@ -40,17 +40,17 @@ #include "../record.h" #include "../scope.h" #include "../parsers/instance.h" -#include "../../records/value-int.h" +#include "../../records/delayed-int.h" -CREATE_DYN_CONSTRUCTOR(record_value, G_TYPE_RECORD_VALUE); +CREATE_DYN_CONSTRUCTOR(record_delayed, G_TYPE_RECORD_DELAYED); /* Initialise une instance sur la base du dérivé de GObject. */ -static int py_record_value_init(PyObject *, PyObject *, PyObject *); +static int py_record_delayed_init(PyObject *, PyObject *, PyObject *); /* Lit la valeur d'un élément Kaitai entier représenté. */ -static PyObject *py_record_value_get_value(PyObject *, void *); +static PyObject *py_record_delayed_get_value(PyObject *, void *); @@ -68,31 +68,37 @@ static PyObject *py_record_value_get_value(PyObject *, void *); * * ******************************************************************************/ -static int py_record_value_init(PyObject *self, PyObject *args, PyObject *kwds) +static int py_record_delayed_init(PyObject *self, PyObject *args, PyObject *kwds) { GKaitaiInstance *inst; /* Instance définie créatrice */ kaitai_scope_t *locals; /* Environnement local */ + GBinContent *content; /* Contenu binaire analysé */ int ret; /* Bilan de lecture des args. */ - GRecordValue *value; /* Création GLib à transmettre */ + GRecordDelayed *delayed; /* Création GLib à transmettre */ -#define RECORD_VALUE_DOC \ - "The RecordValue class stores a link to an instance used to compute a" \ +#define RECORD_DELAYED_DOC \ + "The RecordDelayed 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)" \ + " RecordDelayed(inst, locals, content)" \ "\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." + " pychrysalide.plugins.kaitai.KaitaiScope structure used as current scope." \ + " The *content* argument is a pychrysalide.analysis.BinContent instance if" \ + " the delayed instance does not define a direct value." /* Récupération des paramètres */ - ret = PyArg_ParseTuple(args, "O&O&", + content = NULL; + + ret = PyArg_ParseTuple(args, "O&O&|O&", convert_to_kaitai_instance, &inst, - convert_to_kaitai_scope, &locals); + convert_to_kaitai_scope, &locals, + convert_to_binary_content_or_none, &content); if (!ret) return -1; /* Initialisation d'un objet GLib */ @@ -102,11 +108,11 @@ static int py_record_value_init(PyObject *self, PyObject *args, PyObject *kwds) /* Eléments de base */ - value = G_RECORD_VALUE(pygobject_get(self)); + delayed = G_RECORD_DELAYED(pygobject_get(self)); - if (!g_record_value_create(value, inst, locals)) + if (!g_record_delayed_create(delayed, inst, locals, content)) { - PyErr_SetString(PyExc_ValueError, _("Unable to create record value.")); + PyErr_SetString(PyExc_ValueError, _("Unable to create record delayed.")); return -1; } @@ -128,24 +134,24 @@ static int py_record_value_init(PyObject *self, PyObject *args, PyObject *kwds) * * ******************************************************************************/ -static PyObject *py_record_value_get_value(PyObject *self, void *closure) +static PyObject *py_record_delayed_get_value(PyObject *self, void *closure) { PyObject *result; /* Valeur à retourner */ - GRecordValue *value; /* Version native de l'élément */ + GRecordDelayed *delayed; /* 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 \ +#define RECORD_DELAYED_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ ( \ - value, py_record_value, \ + value, py_record_delayed, \ "Carried value (as integer, bytes), or None in case of error." \ ) result = NULL; - value = G_RECORD_VALUE(pygobject_get(self)); + delayed = G_RECORD_DELAYED(pygobject_get(self)); - status = g_record_value_compute_and_aggregate_value(value, &resolved); + status = g_record_delayed_compute_and_aggregate_value(delayed, &resolved); if (status) switch (resolved.type) @@ -216,37 +222,37 @@ static PyObject *py_record_value_get_value(PyObject *self, void *closure) * * ******************************************************************************/ -PyTypeObject *get_python_record_value_type(void) +PyTypeObject *get_python_record_delayed_type(void) { - static PyMethodDef py_record_value_methods[] = { + static PyMethodDef py_record_delayed_methods[] = { { NULL } }; - static PyGetSetDef py_record_value_getseters[] = { - RECORD_VALUE_VALUE_ATTRIB, + static PyGetSetDef py_record_delayed_getseters[] = { + RECORD_DELAYED_VALUE_ATTRIB, { NULL } }; - static PyTypeObject py_record_value_type = { + static PyTypeObject py_record_delayed_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "pychrysalide.plugins.kaitai.records.RecordValue", + .tp_name = "pychrysalide.plugins.kaitai.records.RecordDelayed", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = RECORD_VALUE_DOC, + .tp_doc = RECORD_DELAYED_DOC, - .tp_methods = py_record_value_methods, - .tp_getset = py_record_value_getseters, + .tp_methods = py_record_delayed_methods, + .tp_getset = py_record_delayed_getseters, - .tp_init = py_record_value_init, - .tp_new = py_record_value_new, + .tp_init = py_record_delayed_init, + .tp_new = py_record_delayed_new, }; - return &py_record_value_type; + return &py_record_delayed_type; } @@ -255,7 +261,7 @@ PyTypeObject *get_python_record_value_type(void) * * * Paramètres : - * * * -* Description : Prend en charge l'objet 'pychrysalide...records.RecordValue. * +* Description : Prend en charge l'objet 'pychrysalide..records.RecordDelayed.* * * * Retour : Bilan de l'opération. * * * @@ -263,13 +269,13 @@ PyTypeObject *get_python_record_value_type(void) * * ******************************************************************************/ -bool ensure_python_record_value_is_registered(void) +bool ensure_python_record_delayed_is_registered(void) { - PyTypeObject *type; /* Type Python 'RecordValue' */ + PyTypeObject *type; /* Type Python 'RecordDelayed' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ - type = get_python_record_value_type(); + type = get_python_record_delayed_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { @@ -280,7 +286,7 @@ bool ensure_python_record_value_is_registered(void) if (!ensure_python_match_record_is_registered()) return false; - if (!register_class_for_pygobject(dict, G_TYPE_RECORD_VALUE, type)) + if (!register_class_for_pygobject(dict, G_TYPE_RECORD_DELAYED, type)) return false; } @@ -303,11 +309,11 @@ bool ensure_python_record_value_is_registered(void) * * ******************************************************************************/ -int convert_to_record_value(PyObject *arg, void *dst) +int convert_to_record_delayed(PyObject *arg, void *dst) { int result; /* Bilan à retourner */ - result = PyObject_IsInstance(arg, (PyObject *)get_python_record_value_type()); + result = PyObject_IsInstance(arg, (PyObject *)get_python_record_delayed_type()); switch (result) { @@ -317,11 +323,11 @@ int convert_to_record_value(PyObject *arg, void *dst) break; case 0: - PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record value"); + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to record delayed"); break; case 1: - *((GRecordValue **)dst) = G_RECORD_VALUE(pygobject_get(arg)); + *((GRecordDelayed **)dst) = G_RECORD_DELAYED(pygobject_get(arg)); break; default: diff --git a/plugins/kaitai/python/records/value.h b/plugins/kaitai/python/records/delayed.h index 16cadcb..ba2d23a 100644 --- a/plugins/kaitai/python/records/value.h +++ b/plugins/kaitai/python/records/delayed.h @@ -1,6 +1,6 @@ /* Chrysalide - Outil d'analyse de fichiers binaires - * value.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/value.h" + * delayed.h - prototypes pour l'équivalent Python du fichier "plugins/kaitai/records/delayed.h" * * Copyright (C) 2019 Cyrille Bagard * @@ -22,8 +22,8 @@ */ -#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H -#define _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H +#ifndef _PLUGINS_KAITAI_PYTHON_RECORDS_DELAYED_H +#define _PLUGINS_KAITAI_PYTHON_RECORDS_DELAYED_H #include <Python.h> @@ -32,14 +32,14 @@ /* Fournit un accès à une définition de type à diffuser. */ -PyTypeObject *get_python_record_value_type(void); +PyTypeObject *get_python_record_delayed_type(void); -/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordValue'. */ -bool ensure_python_record_value_is_registered(void); +/* Prend en charge l'objet 'pychrysalide.plugins.kaitai.records.RecordDelayed'. */ +bool ensure_python_record_delayed_is_registered(void); /* Tente de convertir en valeur calculée. */ -int convert_to_record_value(PyObject *, void *); +int convert_to_record_delayed(PyObject *, void *); -#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_VALUE_H */ +#endif /* _PLUGINS_KAITAI_PYTHON_RECORDS_DELAYED_H */ diff --git a/plugins/kaitai/python/records/module.c b/plugins/kaitai/python/records/module.c index ea33c31..af97434 100644 --- a/plugins/kaitai/python/records/module.c +++ b/plugins/kaitai/python/records/module.c @@ -32,11 +32,12 @@ #include <plugins/pychrysalide/helpers.h> +#include "bits.h" +#include "delayed.h" #include "empty.h" #include "group.h" #include "item.h" #include "list.h" -#include "value.h" @@ -109,11 +110,12 @@ bool populate_kaitai_records_module(void) result = true; + if (result) result = ensure_python_record_bit_field_is_registered(); + if (result) result = ensure_python_record_delayed_is_registered(); 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); |