diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2024-11-23 15:59:19 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2024-11-23 15:59:19 (GMT) |
commit | 411f03130cf45194689bc344f19a3b77c33a31ae (patch) | |
tree | f047b62015eb37e30629398f9adcb977a5a6c6f6 /plugins/pychrysalide/format | |
parent | 80d779749adf228078b61f268bf952ba91a277f0 (diff) |
Restore more features for formats.
Diffstat (limited to 'plugins/pychrysalide/format')
-rw-r--r-- | plugins/pychrysalide/format/executable.c | 392 | ||||
-rw-r--r-- | plugins/pychrysalide/format/flat.c | 8 | ||||
-rw-r--r-- | plugins/pychrysalide/format/known.c | 173 | ||||
-rw-r--r-- | plugins/pychrysalide/format/program.c | 188 |
4 files changed, 542 insertions, 219 deletions
diff --git a/plugins/pychrysalide/format/executable.c b/plugins/pychrysalide/format/executable.c index d886e9a..7d05578 100644 --- a/plugins/pychrysalide/format/executable.c +++ b/plugins/pychrysalide/format/executable.c @@ -36,9 +36,10 @@ #include "program.h" #include "../access.h" #include "../helpers.h" +#include "../analysis/content.h" //#include "../arch/processor.h" -//#include "../arch/vmpa.h" -//#include "../glibext/binportion.h" +#include "../arch/vmpa.h" +#include "../glibext/portion.h" @@ -56,23 +57,35 @@ static int py_executable_format_init(PyObject *, PyObject *, PyObject *); /* Indique le type d'architecture visée par le format. */ static char *py_executable_format_get_target_machine_wrapper(const GExecutableFormat *); +/* Fournit l'adresse principale associée à un format. */ +static bool py_executable_format_get_main_address_wrapper(GExecutableFormat *, vmpa2t *); + +/* Etend la définition des portions au sein d'un binaire. */ +static bool py_executable_format_refine_portions_wrapper(GExecutableFormat *); + /* ------------------------ DECLARATION DE FORMAT EXECUTABLE ------------------------ */ -/* Enregistre une portion artificielle pour le format. */ -//static PyObject *py_executable_format_register_user_portion(PyObject *, PyObject *); +/* Procède à l'enregistrement d'une portion dans un format. */ +static PyObject *py_executable_format_include_portion(PyObject *, PyObject *); /* Fournit l'emplacement correspondant à une position physique. */ -//static PyObject *py_executable_format_translate_offset_into_vmpa(PyObject *, PyObject *); +static PyObject *py_executable_format_translate_offset_into_vmpa(PyObject *, PyObject *); /* Fournit l'emplacement correspondant à une adresse virtuelle. */ -//static PyObject *py_executable_format_translate_address_into_vmpa(PyObject *, PyObject *); +static PyObject *py_executable_format_translate_address_into_vmpa(PyObject *, PyObject *); /* Indique le type d'architecture visée par le format. */ static PyObject *py_executable_format_get_target_machine(PyObject *, void *); +/* Fournit l'adresse principale associée à un format. */ +static PyObject *py_executable_format_get_main_address(PyObject *, void *); + +/* Indique le type d'architecture visée par le format. */ +static PyObject *py_executable_format_get_portions(PyObject *, void *); + /* ---------------------------------------------------------------------------------- */ @@ -97,6 +110,9 @@ static void py_executable_format_init_gclass(GExecutableFormatClass *class, gpoi { class->get_machine = py_executable_format_get_target_machine_wrapper; + class->get_main_addr = py_executable_format_get_main_address_wrapper; + class->refine_portions = py_executable_format_refine_portions_wrapper; + } @@ -116,24 +132,42 @@ static void py_executable_format_init_gclass(GExecutableFormatClass *class, gpoi static int py_executable_format_init(PyObject *self, PyObject *args, PyObject *kwds) { + GBinContent *content; /* Contenu à intégrer au format*/ int ret; /* Bilan de lecture des args. */ + GExecutableFormat *format; /* Format à manipuler */ #define EXECUTABLE_FORMAT_DOC \ "The ExecutableFormat class provides support for formats containing"\ " code to run.\n" \ "\n" \ - "The following method has to be defined for new classes:\n" \ + "The following methods have to be defined for new classes:\n" \ "* pychrysalide.format.ExecutableFormat._get_target_machine();\n" \ + "* pychrysalide.format.ExecutableFormat._get_main_address().\n" \ "\n" \ + "The following method may be defined for new classes:\n" \ + "* pychrysalide.format.ExecutableFormat._refine_portions().\n" \ "\n" \ "Calls to the *__init__* constructor of this abstract object expect"\ - " no particular argument." + " only one argument: a binary content, provided as a" \ + " pychrysalide.analysis.BinContent instance." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", 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 */ + + format = G_EXECUTABLE_FORMAT(pygobject_get(self)); + + if (!g_executable_format_create(format, content)) + return -1; + return 0; } @@ -203,41 +237,221 @@ static char *py_executable_format_get_target_machine_wrapper(const GExecutableFo } +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à consulter. * +* addr = adresse principale trouvée si possible. [OUT] * +* * +* Description : Fournit l'adresse principale associée à un format. * +* * +* Retour : Validité de l'adresse transmise. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_executable_format_get_main_address_wrapper(GExecutableFormat *format, vmpa2t *addr) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Valeur retournée */ + vmpa2t *tmp; /* Zone de stockage Python */ + int ret; /* Bilan d'une conversion */ + +#define EXECUTABLE_FORMAT_GET_MAIN_ADDRESS_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _get_main_address, "$self", \ + METH_NOARGS, \ + "Abstract method used to provide the main address of code for" \ + " the executable format.\n" \ + "\n" \ + "The return value has to be a pychrysalide.arch.vmpa instance or" \ + " *None* in case of failure." \ + ) + + result = false; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(format)); + + if (has_python_method(pyobj, "_get_main_address")) + { + pyret = run_python_method(pyobj, "_get_main_address", NULL); + + if (pyret != NULL) + { + if (pyret == Py_None) + { + init_vmpa(addr, VMPA_NO_PHYSICAL, VMPA_NO_VIRTUAL); + result = true; + } + + else + { + ret = convert_any_to_vmpa(pyret, &tmp); + + result = (ret == 1 || ret == Py_CLEANUP_SUPPORTED); + + if (result) + { + copy_vmpa(addr, tmp); + + if (ret == Py_CLEANUP_SUPPORTED) + clean_vmpa_arg(tmp); + + } + + else + { + /** + * L'erreur Python peut être effacée. + * + * Elle sera remontée : + * - au code C via le retour (false) : + * - à Python lors de l'accès à la propriétée. + */ + PyErr_Clear(); + + } + + } + + Py_DECREF(pyret); + + } + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : format = informations chargées à consulter. * +* * +* Description : Etend la définition des portions au sein d'un binaire. * +* * +* Retour : Bilan des définitions de portions. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_executable_format_refine_portions_wrapper(GExecutableFormat *format) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Valeur retournée */ + +#define EXECUTABLE_FORMAT_REFINE_PORTIONS_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _refine_portions, "$self", \ + METH_NOARGS, \ + "Abstract method used to extend the definition of the format" \ + " with binary portions.\n" \ + "\n" \ + "Extra portions should be included with calls to" \ + " pychrysalide.format.ExecutableFormat.include_portion().\n" \ + "\n" \ + "The return value has to be a boolean value: *True* in case of" \ + " success, *False* in case of failure." \ +) + + result = true; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(format)); + + if (has_python_method(pyobj, "_refine_portions")) + { + pyret = run_python_method(pyobj, "_refine_portions", NULL); + + if (pyret != NULL) + { + result = (pyret == Py_True); + Py_DECREF(pyret); + } + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + /* ---------------------------------------------------------------------------------- */ /* DECLARATION DE FORMAT EXECUTABLE */ /* ---------------------------------------------------------------------------------- */ -#if 0 /****************************************************************************** * * * Paramètres : self = description de l'exécutable à consulter. * * args = arguments accompagnant l'appel. * * * -* Description : Enregistre une portion artificielle pour le format. * +* Description : Procède à l'enregistrement d'une portion dans un format. * * * -* Retour : - * +* Retour : Bilan de l'opération : True si inclusion, False sinon. * * * * Remarques : - * * * ******************************************************************************/ -static PyObject *py_executable_format_register_user_portion(PyObject *self, PyObject *args) +static PyObject *py_executable_format_include_portion(PyObject *self, PyObject *args) { - GBinPortion *portion; /* Portion binaire à conserver */ + PyObject *result; /* Bilan à retourner */ + GBinaryPortion *portion; /* Portion binaire à conserver */ + vmpa2t *origin; /* Source de l'inclusion */ int ret; /* Bilan de lecture des args. */ GExecutableFormat *format; /* Version GLib du format */ + bool status; /* Bilan de l'inclusion */ + +#define EXECUTABLE_FORMAT_INCLUDE_PORTION_METHOD PYTHON_METHOD_DEF \ +( \ + include_portion, "$self, portion, /, origin=None", \ + METH_VARARGS, py_executable_format, \ + "Register a new portion inside the content of an executable format.\n" \ + "\n" \ + "The *portion* argument is a pychrysalide.glibext.BinaryPortion" \ + " instance. The optional *origin* arguement specifies the source of the"\ + " operation, as a pychrysalide.arch.vmpa definition, which may be used" \ + " for tracking errors.\n" \ + "\n" \ + "The return value is a boolean value: *True* in case of success," \ + " *False* in case of failure." \ +) + + origin = NULL; - ret = PyArg_ParseTuple(args, "O&", convert_to_binary_portion, &portion); + ret = PyArg_ParseTuple(args, "O&|O&", convert_to_binary_portion, &portion, convert_any_to_vmpa, &origin); if (!ret) return NULL; format = G_EXECUTABLE_FORMAT(pygobject_get(self)); - g_object_ref(G_OBJECT(portion)); - g_exe_format_register_user_portion(format, portion); + status = g_executable_format_include_portion(format, portion, origin); + + result = status ? Py_True : Py_False; + Py_INCREF(result); - Py_RETURN_NONE; + if (origin != NULL) + clean_vmpa_arg(origin); + + return result; } @@ -264,13 +478,24 @@ static PyObject *py_executable_format_translate_offset_into_vmpa(PyObject *self, vmpa2t pos; /* Position complète déterminée*/ bool status; /* Bilan de l'opération */ - format = G_EXECUTABLE_FORMAT(pygobject_get(self)); - assert(format != NULL); +#define EXECUTABLE_FORMAT_TRANSLATE_OFFSET_INTO_VMPA_METHOD PYTHON_METHOD_DEF \ +( \ + translate_offset_into_vmpa, "$self, addr", \ + METH_VARARGS, py_executable_format, \ + "Translate a physical offset to a full location.\n" \ + "\n" \ + "The *off* argument is a physical offset provided as an integer value.\n" \ + "\n" \ + "The returned position is a pychrysalide.arch.vmpa instance or *None* in" \ + " case of failure." \ +) ret = PyArg_ParseTuple(args, "K", &off); if (!ret) return NULL; - status = g_exe_format_translate_offset_into_vmpa(format, off, &pos); + format = G_EXECUTABLE_FORMAT(pygobject_get(self)); + + status = g_executable_format_translate_offset_into_vmpa(format, off, &pos); if (status) result = build_from_internal_vmpa(&pos); @@ -308,13 +533,24 @@ static PyObject *py_executable_format_translate_address_into_vmpa(PyObject *self vmpa2t pos; /* Position complète déterminée*/ bool status; /* Bilan de l'opération */ - format = G_EXECUTABLE_FORMAT(pygobject_get(self)); - assert(format != NULL); +#define EXECUTABLE_FORMAT_TRANSLATE_ADDRESS_INTO_VMPA_METHOD PYTHON_METHOD_DEF \ +( \ + translate_address_into_vmpa, "$self, addr", \ + METH_VARARGS, py_executable_format, \ + "Translate a virtual address to a full location.\n" \ + "\n" \ + "The *addr* argument is a virtual address provided as an integer value.\n" \ + "\n" \ + "The returned position is a pychrysalide.arch.vmpa instance or *None* in" \ + " case of failure." \ +) ret = PyArg_ParseTuple(args, "K", &addr); if (!ret) return NULL; - status = g_exe_format_translate_address_into_vmpa(format, addr, &pos); + format = G_EXECUTABLE_FORMAT(pygobject_get(self)); + + status = g_executable_format_translate_address_into_vmpa(format, addr, &pos); if (status) result = build_from_internal_vmpa(&pos); @@ -328,7 +564,6 @@ static PyObject *py_executable_format_translate_address_into_vmpa(PyObject *self return result; } -#endif /****************************************************************************** @@ -382,6 +617,91 @@ static PyObject *py_executable_format_get_target_machine(PyObject *self, void *c /****************************************************************************** * * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit l'adresse principale associée à un format. * +* * +* Retour : Validité de l'adresse transmise. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_executable_format_get_main_address(PyObject *self, void *closure) +{ + PyObject *result; /* Trouvailles à retourner */ + GExecutableFormat *format; /* Format exécutable manipulé */ + vmpa2t addr; /* Point d'entrée principal */ + bool status; /* Validité de l'adresse */ + +#define EXECUTABLE_FORMAT_MAIN_ADDRESS_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + main_address, py_executable_format, \ + "Main address of code for the executable format.\n" \ + "\n" \ + "This property provide a pychrysalide.arch.vmpa instance or" \ + " *None* in case of failure." \ +) + + format = G_EXECUTABLE_FORMAT(pygobject_get(self)); + + status = g_executable_format_get_main_address(format, &addr); + + if (status) + result = build_from_internal_vmpa(&addr); + + else + { + PyErr_SetString(PyExc_AttributeError, _("unable to define a value for the main address")); + result = NULL; + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Indique le type d'architecture visée par le format. * +* * +* Retour : Identifiant de l'architecture ciblée par le format. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_executable_format_get_portions(PyObject *self, void *closure) +{ + PyObject *result; /* Trouvailles à retourner */ + GExecutableFormat *format; /* Format exécutable manipulé */ + GBinaryPortion *portions; /* Portion principale du format*/ + +#define EXECUTABLE_FORMAT_PORTIONS_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + portions, py_executable_format, \ + "Root portion of the executable format, provided as a" \ + " pychrysalide.glibext.BinaryPortion instance." \ +) + + format = G_EXECUTABLE_FORMAT(pygobject_get(self)); + + portions = g_executable_format_get_portions(format); + + result = pygobject_new(G_OBJECT(portions)); + unref_object(portions); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * @@ -396,28 +716,18 @@ PyTypeObject *get_python_executable_format_type(void) { static PyMethodDef py_executable_format_methods[] = { EXECUTABLE_FORMAT_GET_TARGET_MACHINE_WRAPPER, -#if 0 - { - "register_user_portion", py_executable_format_register_user_portion, - METH_VARARGS, - "register_user_portion($self, portion, /)\n--\n\nRemember a given user-defined binary portion as part of the executable format content." - }, - { - "translate_offset_into_vmpa", py_executable_format_translate_offset_into_vmpa, - METH_VARARGS, - "translate_offset_into_vmpa($self, off, /)\n--\n\nTranslate a physical offset to a full location." - }, - { - "translate_address_into_vmpa", py_executable_format_translate_address_into_vmpa, - METH_VARARGS, - "translate_address_into_vmpa($self, addr, /)\n--\n\nTranslate a physical offset to a full location." - }, -#endif + EXECUTABLE_FORMAT_GET_MAIN_ADDRESS_WRAPPER, + EXECUTABLE_FORMAT_REFINE_PORTIONS_WRAPPER, + EXECUTABLE_FORMAT_INCLUDE_PORTION_METHOD, + EXECUTABLE_FORMAT_TRANSLATE_OFFSET_INTO_VMPA_METHOD, + EXECUTABLE_FORMAT_TRANSLATE_ADDRESS_INTO_VMPA_METHOD, { NULL } }; static PyGetSetDef py_executable_format_getseters[] = { EXECUTABLE_FORMAT_TARGET_MACHINE_ATTRIB, + EXECUTABLE_FORMAT_MAIN_ADDRESS_ATTRIB, + EXECUTABLE_FORMAT_PORTIONS_ATTRIB, { NULL } }; diff --git a/plugins/pychrysalide/format/flat.c b/plugins/pychrysalide/format/flat.c index a115c57..81f0dba 100644 --- a/plugins/pychrysalide/format/flat.c +++ b/plugins/pychrysalide/format/flat.c @@ -41,7 +41,6 @@ CREATE_DYN_CONSTRUCTOR(flat_format, G_TYPE_FLAT_FORMAT); - /* Initialise une instance sur la base du dérivé de GObject. */ static int py_flat_format_init(PyObject *, PyObject *, PyObject *); @@ -77,9 +76,10 @@ static int py_flat_format_init(PyObject *self, PyObject *args, PyObject *kwds) "\n" \ " FlatFormat(content, machine, endian)" \ "\n" \ - "Where content is a pychrysalide.analysis.BinContent object, machine" \ - " defines the target architecture as a string value and endian provides"\ - " the right endianness of the data, as pychrysalide.SourceEndian value." + "Where *content* is a pychrysalide.analysis.BinContent object," \ + " *machine* defines the target architecture as a string value and" \ + " *endian* provides the right endianness of the data, as a" \ + " pychrysalide.SourceEndian value." /* Récupération des paramètres */ diff --git a/plugins/pychrysalide/format/known.c b/plugins/pychrysalide/format/known.c index e38c975..5df2a8f 100644 --- a/plugins/pychrysalide/format/known.c +++ b/plugins/pychrysalide/format/known.c @@ -55,13 +55,8 @@ static char *py_known_format_get_key_wrapper(const GKnownFormat *); /* Fournit une description humaine du format. */ static char *py_known_format_get_description_wrapper(const GKnownFormat *); -#if 0 /* Assure l'interprétation d'un format en différé. */ -static bool py_known_format_analyze_wrapper(GKnownFormat *, wgroup_id_t, GtkStatusStack *); - -/* Réalise un traitement post-désassemblage. */ -static void py_known_format_complete_analysis_wrapper(GKnownFormat *, wgroup_id_t, GtkStatusStack *); -#endif +static bool py_known_format_analyze_wrapper(GKnownFormat *); @@ -69,10 +64,7 @@ static void py_known_format_complete_analysis_wrapper(GKnownFormat *, wgroup_id_ /* Assure l'interprétation d'un format en différé. */ -//static PyObject *py_known_format_analyze(PyObject *, PyObject *); - -/* Réalise un traitement post-désassemblage. */ -//static PyObject *py_known_format_complete_analysis(PyObject *, PyObject *); +static PyObject *py_known_format_analyze(PyObject *, PyObject *); /* Indique la désignation interne du format. */ static PyObject *py_known_format_get_key(PyObject *, void *); @@ -108,8 +100,7 @@ static void py_known_format_init_gclass(GKnownFormatClass *class, gpointer unuse class->get_key = py_known_format_get_key_wrapper; class->get_desc = py_known_format_get_description_wrapper; - //class->analyze = py_known_format_analyze_wrapper; - //class->complete = py_known_format_complete_analysis_wrapper; + class->analyze = py_known_format_analyze_wrapper; } @@ -143,9 +134,6 @@ static int py_known_format_init(PyObject *self, PyObject *args, PyObject *kwds) "* pychrysalide.format.KnownFormat._get_description();\n" \ "* pychrysalide.format.KnownFormat._analyze().\n" \ "\n" \ - "The following method may also be defined for new classes too:\n" \ - "* pychrysalide.format.KnownFormat._complete_analysis().\n" \ - "\n" \ "Calls to the *__init__* constructor of this abstract object expect"\ " only one argument: a binary content, provided as a" \ " pychrysalide.analysis.BinContent instance." @@ -298,12 +286,10 @@ static char *py_known_format_get_description_wrapper(const GKnownFormat *format) } -#if 0 + /****************************************************************************** * * * Paramètres : format = format chargé dont l'analyse est lancée. * -* gid = groupe de travail dédié. * -* status = barre de statut à tenir informée. * * * * Description : Assure l'interprétation d'un format en différé. * * * @@ -313,12 +299,11 @@ static char *py_known_format_get_description_wrapper(const GKnownFormat *format) * * ******************************************************************************/ -static bool py_known_format_analyze_wrapper(GKnownFormat *format, wgroup_id_t gid, GtkStatusStack *status) +static bool py_known_format_analyze_wrapper(GKnownFormat *format) { bool result; /* Bilan à retourner */ PyGILState_STATE gstate; /* Sauvegarde d'environnement */ PyObject *pyobj; /* Objet Python concerné */ - PyObject *args; /* Arguments pour l'appel */ PyObject *pyret; /* Bilan d'exécution */ #define KNOWN_FORMAT_ANALYZE_WRAPPER PYTHON_WRAPPER_DEF \ @@ -328,12 +313,8 @@ static bool py_known_format_analyze_wrapper(GKnownFormat *format, wgroup_id_t gi "Abstract method used to start the analysis of the known" \ " format and return its status.\n" \ "\n" \ - "The identifier refers to the working queue used to process" \ - " the analysis. A reference to the main status bar may also be" \ - " provided, as a pychrysalide.gtkext.StatusStack instance if" \ - " running in graphical mode or None otherwise.\n" \ - "\n" \ - "The expected result of the call is a boolean." \ + "The expected result of the call is a boolean value: *True* in" \ + " case of success, *False* in case of failure." \ ) result = false; @@ -344,16 +325,10 @@ static bool py_known_format_analyze_wrapper(GKnownFormat *format, wgroup_id_t gi if (has_python_method(pyobj, "_analyze")) { - args = PyTuple_New(2); - - PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(gid)); - PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(status))); - - pyret = run_python_method(pyobj, "_analyze", args); + pyret = run_python_method(pyobj, "_analyze", NULL); result = (pyret == Py_True); - Py_DECREF(args); Py_XDECREF(pyret); } @@ -367,71 +342,12 @@ static bool py_known_format_analyze_wrapper(GKnownFormat *format, wgroup_id_t gi } -/****************************************************************************** -* * -* Paramètres : format = format chargé dont l'analyse est lancée. * -* gid = groupe de travail dédié. * -* status = barre de statut à tenir informée. * -* * -* Description : Réalise un traitement post-désassemblage. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void py_known_format_complete_analysis_wrapper(GKnownFormat *format, wgroup_id_t gid, GtkStatusStack *status) -{ - PyGILState_STATE gstate; /* Sauvegarde d'environnement */ - PyObject *pyobj; /* Objet Python concerné */ - PyObject *args; /* Arguments pour l'appel */ - PyObject *pyret; /* Bilan d'exécution */ - -#define KNOWN_FORMAT_COMPLETE_ANALYSIS_WRAPPER PYTHON_VOID_WRAPPER_DEF \ -( \ - _complete_analysis, "$self, gid, status, /", \ - METH_VARARGS, \ - "Abstract method used to complete an analysis of a known format.\n" \ - "\n" \ - "The identifier refers to the working queue used to process the" \ - " analysis. A reference to the main status bar may also be" \ - " provided, as a pychrysalide.gtkext.StatusStack instance if" \ - " running in graphical mode or None otherwise.\n" \ -) - - gstate = PyGILState_Ensure(); - - pyobj = pygobject_new(G_OBJECT(format)); - - if (has_python_method(pyobj, "_complete_analysis")) - { - args = PyTuple_New(2); - - PyTuple_SetItem(args, 0, PyLong_FromUnsignedLong(gid)); - PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(status))); - - pyret = run_python_method(pyobj, "_complete_analysis", args); - - Py_DECREF(args); - Py_XDECREF(pyret); - - } - - Py_DECREF(pyobj); - - PyGILState_Release(gstate); - -} -#endif - /* ---------------------------------------------------------------------------------- */ /* DEFINITION DU FORMAT CONNU */ /* ---------------------------------------------------------------------------------- */ -#if 0 /****************************************************************************** * * * Paramètres : self = objet représentant un format connu. * @@ -448,33 +364,24 @@ static void py_known_format_complete_analysis_wrapper(GKnownFormat *format, wgro static PyObject *py_known_format_analyze(PyObject *self, PyObject *args) { PyObject *result; /* Bilan à retourner */ - int ret; /* Bilan de lecture des args. */ GKnownFormat *format; /* Format connu manipulé */ bool status; /* Bilan de l'opération */ #define KNOWN_FORMAT_ANALYZE_METHOD PYTHON_METHOD_DEF \ ( \ - analyze, "$self, gid, status, /", \ - METH_VARARGS, py_known_format, \ + analyze, "$self", \ + METH_NOARGS, py_known_format, \ "Start the analysis of the known format and return its status." \ "\n" \ "Once this analysis is done, a few early symbols and the" \ " mapped sections are expected to be defined, if any.\n" \ "\n" \ - "The identifier refers to the working queue used to process" \ - " the analysis. A reference to the main status bar may also be" \ - " provided, as a pychrysalide.gtkext.StatusStack instance if" \ - " running in graphical mode or None otherwise.\n" \ - "\n" \ "The return value is a boolean status of the operation." \ ) - ret = PyArg_ParseTuple(args, "");//|KO!", &gid, &status); - if (!ret) return NULL; - format = G_KNOWN_FORMAT(pygobject_get(self)); - status = g_known_format_analyze(format, 0, NULL); + status = g_known_format_analyze(format); result = status ? Py_True : Py_False; Py_INCREF(result); @@ -486,58 +393,6 @@ static PyObject *py_known_format_analyze(PyObject *self, PyObject *args) /****************************************************************************** * * -* Paramètres : self = objet représentant un format connu. * -* args = arguments fournis pour l'opération. * -* * -* Description : Réalise un traitement post-désassemblage. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_known_format_complete_analysis(PyObject *self, PyObject *args) -{ - PyObject *result; /* Bilan à retourner */ - int ret; /* Bilan de lecture des args. */ - GKnownFormat *format; /* Format connu manipulé */ - -#define KNOWN_FORMAT_COMPLETE_ANALYSIS_METHOD PYTHON_METHOD_DEF \ -( \ - complete_analysis, "$self, gid, status, /", \ - METH_VARARGS, py_known_format, \ - "Complete an analysis of a known format.\n" \ - "\n" \ - "This process is usually done once the disassembling process" \ - " is completed.\n" \ - "\n" \ - "The identifier refers to the working queue used to process" \ - " the analysis. A reference to the main status bar may also be" \ - " provided, as a pychrysalide.gtkext.StatusStack instance if" \ - " running in graphical mode or None otherwise.\n" \ - "\n" \ - "The return value is a boolean status of the operation." \ -) - - ret = PyArg_ParseTuple(args, "");//|KO!", &gid, &status); - if (!ret) return NULL; - - format = G_KNOWN_FORMAT(pygobject_get(self)); - - g_known_format_complete_analysis(format, 0, NULL); - - result = Py_None; - Py_INCREF(result); - - return result; - -} -#endif - - -/****************************************************************************** -* * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * @@ -718,10 +573,8 @@ PyTypeObject *get_python_known_format_type(void) static PyMethodDef py_known_format_methods[] = { KNOWN_FORMAT_GET_KEY_WRAPPER, KNOWN_FORMAT_GET_DESCRIPTION_WRAPPER, - //KNOWN_FORMAT_ANALYZE_WRAPPER, - //KNOWN_FORMAT_COMPLETE_ANALYSIS_WRAPPER, - //KNOWN_FORMAT_ANALYZE_METHOD, - //KNOWN_FORMAT_COMPLETE_ANALYSIS_METHOD, + KNOWN_FORMAT_ANALYZE_WRAPPER, + KNOWN_FORMAT_ANALYZE_METHOD, { NULL } }; diff --git a/plugins/pychrysalide/format/program.c b/plugins/pychrysalide/format/program.c index d96c2db..28c1540 100644 --- a/plugins/pychrysalide/format/program.c +++ b/plugins/pychrysalide/format/program.c @@ -35,6 +35,8 @@ #include "../access.h" #include "../constants.h" #include "../helpers.h" +#include "../analysis/content.h" +#include "../arch/vmpa.h" /* @@ -44,7 +46,6 @@ #include "symiter.h" #include "../analysis/constants.h" #include "../analysis/content.h" -#include "../arch/vmpa.h" #include "../arch/constants.h" */ @@ -64,6 +65,9 @@ static int py_program_format_init(PyObject *, PyObject *, PyObject *); /* Indique le boutisme employé par le format binaire analysé. */ static SourceEndian py_program_format_get_endianness_wrapper(const GProgramFormat *); +/* Fournit l'emplacement d'une section donnée. */ +static bool py_program_format_get_section_range_by_name_wrapper(const GProgramFormat *, const char *, mrange_t *); + /* ---------------------------- FORMAT BINAIRE GENERIQUE ---------------------------- */ @@ -78,7 +82,14 @@ static PyObject *py_program_format_unset_flag(PyObject *, PyObject *); /* Détermine si un format possède un fanion particulier. */ static PyObject *py_program_format_has_flag(PyObject *, PyObject *); +#endif + + +/* Fournit l'emplacement d'une section donnée. */ +static PyObject *py_program_format_get_section_range_by_name(PyObject *, PyObject *); + +#if 0 /* Enregistre une adresse comme début d'une zone de code. */ static PyObject *py_program_format_register_code_point(PyObject *, PyObject *); @@ -143,6 +154,7 @@ static PyObject *py_program_format_get_errors(PyObject *, void *); static void py_program_format_init_gclass(GProgramFormatClass *class, gpointer unused) { class->get_endian = py_program_format_get_endianness_wrapper; + class->get_range_by_name = py_program_format_get_section_range_by_name_wrapper; } @@ -163,25 +175,42 @@ static void py_program_format_init_gclass(GProgramFormatClass *class, gpointer u static int py_program_format_init(PyObject *self, PyObject *args, PyObject *kwds) { + GBinContent *content; /* Contenu à intégrer au format*/ int ret; /* Bilan de lecture des args. */ - -#define PROGRAM_FORMAT_DOC \ - "The ProgramFormat class is the major part of binary format" \ - " support. It is the core class used by loading most of the binary" \ - " files.\n" \ - "\n" \ - "\n" \ - "The following method has to be defined for new classes:\n" \ - "* pychrysalide.format.ProgramFormat._get_endianness().\n" \ - "\n" \ - "Calls to the *__init__* constructor of this abstract object expect"\ - " no particular argument." + GProgramFormat *format; /* Format à manipuler */ + +#define PROGRAM_FORMAT_DOC \ + "The ProgramFormat class is the major part of binary format" \ + " support. It is the core class used by loading most of the binary" \ + " files.\n" \ + "\n" \ + "The following method has to be defined for new classes:\n" \ + "* pychrysalide.format.ProgramFormat._get_endianness().\n" \ + "\n" \ + "Other optional method may be defined for new classes:\n" \ + "* pychrysalide.format.ProgramFormat._get_section_range_by_name().\n" \ + "\n" \ + "Calls to the *__init__* constructor of this abstract object expect" \ + " only one argument: a binary content, provided as a" \ + " pychrysalide.analysis.BinContent instance." + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", 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 */ + + format = G_PROGRAM_FORMAT(pygobject_get(self)); + + if (!g_program_format_create(format, content)) + return -1; + return 0; } @@ -252,11 +281,84 @@ static SourceEndian py_program_format_get_endianness_wrapper(const GProgramForma } +/****************************************************************************** +* * +* Paramètres : format = description du programme à consulter. * +* name = nom de la section recherchée. * +* range = emplacement en mémoire à renseigner. [OUT] * +* * +* Description : Fournit l'emplacement d'une section donnée. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool py_program_format_get_section_range_by_name_wrapper(const GProgramFormat *format, const char *name, mrange_t *range) +{ + bool result; /* Bilan à retourner */ + PyGILState_STATE gstate; /* Sauvegarde d'environnement */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Valeur retournée */ + int ret; /* Bilan d'une conversion */ + +#define PROGRAM_FORMAT_GET_SECTION_RANGE_BY_NAME_WRAPPER PYTHON_WRAPPER_DEF \ +( \ + _get_section_range_by_name_wrapper, "$self, name", \ + METH_VARARGS, \ + "Abstract method used to compute the area of a section identified by" \ + " its name.\n" \ + "\n" \ + "The expected returned value is a pychrysalide.arch.mrange instance or" \ + " *None* in case of failure." \ +) + + result = false; + + gstate = PyGILState_Ensure(); + + pyobj = pygobject_new(G_OBJECT(format)); + + if (has_python_method(pyobj, "_get_section_range_by_name")) + { + pyret = run_python_method(pyobj, "_get_section_range_by_name", NULL); + + if (pyret != NULL) + { + if (pyret == Py_None) + result = false; + + else + { + ret = convert_any_to_mrange(pyret, range); + + result = (ret == 1); + + if (!result) + PyErr_Clear(); + + } + + Py_DECREF(pyret); + + } + + } + + Py_DECREF(pyobj); + + PyGILState_Release(gstate); + + return result; + +} + + /* ---------------------------------------------------------------------------------- */ /* FORMAT BINAIRE GENERIQUE */ /* ---------------------------------------------------------------------------------- */ - #if 0 /****************************************************************************** * * @@ -402,10 +504,66 @@ static PyObject *py_program_format_has_flag(PyObject *self, PyObject *args) return result; } +#endif /****************************************************************************** * * +* Paramètres : self = serveur à manipuler. * +* args = arguments d'appel non utilisés ici. * +* * +* Description : Fournit l'emplacement d'une section donnée. * +* * +* Retour : Emplacement ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_program_format_get_section_range_by_name(PyObject *self, PyObject *args) +{ + PyObject *result; /* Emplacement à retourner */ + const char *name; /* Nom de section ciblée */ + int ret; /* Bilan de lecture des args. */ + GProgramFormat *format; /* Elément à manipuler */ + mrange_t range; /* Emplacement obtenu ? */ + bool status; /* Bilan de l'opération */ + +#define PROGRAM_FORMAT_GET_SECTION_RANGE_BY_NAME_METHOD PYTHON_METHOD_DEF \ +( \ + get_section_range_by_name, "$self, name, /", \ + METH_VARARGS, py_program_format, \ + "Compute the area of a section identified by its name.\n" \ + "\n" \ + "The *name* argument is a string value.\n" \ + "\n" \ + "The returned value is a pychrysalide.arch.mrange instance or" \ + " *None* in case of failure." \ +) + + ret = PyArg_ParseTuple(args, "s", &name); + if (!ret) return NULL; + + format = G_PROGRAM_FORMAT(pygobject_get(self)); + + status = g_program_format_get_section_range_by_name(format, name, &range); + + if (status) + result = build_from_internal_mrange(&range); + + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + +#if 0 +/****************************************************************************** +* * * Paramètres : self = classe représentant un format. * * args = arguments fournis à l'appel. * * * @@ -1036,6 +1194,8 @@ PyTypeObject *get_python_program_format_type(void) { static PyMethodDef py_program_format_methods[] = { PROGRAM_FORMAT_GET_ENDIANNESS_WRAPPER, + PROGRAM_FORMAT_GET_SECTION_RANGE_BY_NAME_WRAPPER, + PROGRAM_FORMAT_GET_SECTION_RANGE_BY_NAME_METHOD, /* PROGRAM_FORMAT_SET_FLAG_METHOD, PROGRAM_FORMAT_UNSET_FLAG_METHOD, |