diff options
Diffstat (limited to 'plugins/pychrysalide')
-rw-r--r-- | plugins/pychrysalide/arch/constants.c | 122 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/constants.h | 6 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/instruction.c | 40 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/raw.c | 94 | ||||
-rw-r--r-- | plugins/pychrysalide/arch/raw.h | 3 |
5 files changed, 210 insertions, 55 deletions
diff --git a/plugins/pychrysalide/arch/constants.c b/plugins/pychrysalide/arch/constants.c index 241b009..e4055c0 100644 --- a/plugins/pychrysalide/arch/constants.c +++ b/plugins/pychrysalide/arch/constants.c @@ -25,6 +25,7 @@ #include "constants.h" +#include <arch/raw.h> #include <arch/vmpa.h> @@ -36,6 +37,86 @@ * * * Paramètres : type = type dont le dictionnaire est à compléter. * * * +* Description : Définit les constantes relatives aux instructions. * +* * +* Retour : true en cas de succès de l'opération, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool define_arch_instruction_constants(PyTypeObject *type) +{ + bool result; /* Bilan à retourner */ + PyObject *values; /* Groupe de valeurs à établir */ + + values = PyDict_New(); + + result = add_const_to_group(values, "NONE", AIF_NONE); + if (result) result = add_const_to_group(values, "ROUTINE_START", AIF_ROUTINE_START); + if (result) result = add_const_to_group(values, "RETURN_POINT", AIF_RETURN_POINT); + if (result) result = add_const_to_group(values, "CALL", AIF_CALL); + if (result) result = add_const_to_group(values, "LOW_USER", AIF_LOW_USER); + if (result) result = add_const_to_group(values, "HIGH_USER", AIF_HIGH_USER); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_type(type, true, "ArchInstrFlag", values, + "Flags for some instruction properties."); + + values = PyDict_New(); + + result = add_const_to_group(values, "FETCH", IPH_FETCH); + if (result) result = add_const_to_group(values, "LINK", IPH_LINK); + if (result) result = add_const_to_group(values, "POST", IPH_POST); + if (result) result = add_const_to_group(values, "COUNT", IPH_COUNT); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_type(type, false, "InstrProcessHook", values, + "Kind of hook for instruction processing after disassembling."); + + values = PyDict_New(); + + result = add_const_to_group(values, "EXEC_FLOW", ILT_EXEC_FLOW); + if (result) result = add_const_to_group(values, "JUMP", ILT_JUMP); + if (result) result = add_const_to_group(values, "CASE_JUMP", ILT_CASE_JUMP); + if (result) result = add_const_to_group(values, "JUMP_IF_TRUE", ILT_JUMP_IF_TRUE); + if (result) result = add_const_to_group(values, "JUMP_IF_FALSE", ILT_JUMP_IF_FALSE); + if (result) result = add_const_to_group(values, "LOOP", ILT_LOOP); + if (result) result = add_const_to_group(values, "CALL", ILT_CALL); + if (result) result = add_const_to_group(values, "CATCH_EXCEPTION", ILT_CATCH_EXCEPTION); + if (result) result = add_const_to_group(values, "REF", ILT_REF); + if (result) result = add_const_to_group(values, "COUNT", ILT_COUNT); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_type(type, false, "InstructionLinkType", values, + "Kind of link between two instructions."); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : type = type dont le dictionnaire est à compléter. * +* * * Description : Définit les constantes relatives aux emplacements. * * * * Retour : true en cas de succès de l'opération, false sinon. * @@ -68,3 +149,44 @@ bool define_arch_vmpa_constants(PyTypeObject *type) return result; } + + +/****************************************************************************** +* * +* Paramètres : type = type dont le dictionnaire est à compléter. * +* * +* Description : Définit les constantes relatives aux instructions brutes. * +* * +* Retour : true en cas de succès de l'opération, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool define_raw_instruction_constants(PyTypeObject *type) +{ + bool result; /* Bilan à retourner */ + PyObject *values; /* Groupe de valeurs à établir */ + + values = PyDict_New(); + + result = add_const_to_group(values, "PADDING", RIF_PADDING); + if (result) result = add_const_to_group(values, "STRING", RIF_STRING); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_type(type, true, "RawInstrFlag", values, + "Flags for some instruction properties.\n" + "\n" + "They can be seen as an extension of" \ + " pychrysalide.arch.ArchInstruction.ArchInstrFlag"); + + exit: + + return result; + +} diff --git a/plugins/pychrysalide/arch/constants.h b/plugins/pychrysalide/arch/constants.h index 5273bbd..0be832b 100644 --- a/plugins/pychrysalide/arch/constants.h +++ b/plugins/pychrysalide/arch/constants.h @@ -31,9 +31,15 @@ +/* Définit les constantes relatives aux instructions. */ +bool define_arch_instruction_constants(PyTypeObject *); + /* Définit les constantes relatives aux emplacements. */ bool define_arch_vmpa_constants(PyTypeObject *); +/* Définit les constantes relatives aux instructions brutes. */ +bool define_raw_instruction_constants(PyTypeObject *); + #endif /* _PLUGINS_PYCHRYSALIDE_ARCH_CONSTANTS_H */ diff --git a/plugins/pychrysalide/arch/instruction.c b/plugins/pychrysalide/arch/instruction.c index 757949a..3b3033f 100644 --- a/plugins/pychrysalide/arch/instruction.c +++ b/plugins/pychrysalide/arch/instruction.c @@ -35,6 +35,7 @@ #include <plugins/dt.h> +#include "constants.h" #include "operand.h" #include "vmpa.h" #include "../access.h" @@ -149,9 +150,6 @@ static int py_arch_instruction_set_range(PyObject *, PyObject *, void *); /* Fournit le nom humain de l'instruction manipulée. */ static PyObject *py_arch_instruction_get_keyword(PyObject *, void *); -/* Définit les constantes pour les instructions. */ -static bool py_arch_instruction_define_constants(PyTypeObject *); - /* ---------------------------------------------------------------------------------- */ @@ -839,40 +837,6 @@ static PyObject *py_arch_instruction_get_keyword(PyObject *self, void *unused) /****************************************************************************** * * -* Paramètres : obj_type = type dont le dictionnaire est à compléter. * -* * -* Description : Définit les constantes pour les instructions. * -* * -* Retour : true en cas de succès de l'opération, false sinon. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static bool py_arch_instruction_define_constants(PyTypeObject *obj_type) -{ - bool result; /* Bilan à retourner */ - - result = true; - - result &= PyDict_AddULongMacro(obj_type, ILT_EXEC_FLOW); - result &= PyDict_AddULongMacro(obj_type, ILT_JUMP); - result &= PyDict_AddULongMacro(obj_type, ILT_CASE_JUMP); - result &= PyDict_AddULongMacro(obj_type, ILT_JUMP_IF_TRUE); - result &= PyDict_AddULongMacro(obj_type, ILT_JUMP_IF_FALSE); - result &= PyDict_AddULongMacro(obj_type, ILT_LOOP); - result &= PyDict_AddULongMacro(obj_type, ILT_CALL); - result &= PyDict_AddULongMacro(obj_type, ILT_CATCH_EXCEPTION); - result &= PyDict_AddULongMacro(obj_type, ILT_REF); - result &= PyDict_AddULongMacro(obj_type, ILT_COUNT); - - return result; - -} - - -/****************************************************************************** -* * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * @@ -989,7 +953,7 @@ bool ensure_python_arch_instruction_is_registered(void) &PyGObject_Type, get_python_line_generator_type(), NULL)) return false; - if (!py_arch_instruction_define_constants(type)) + if (!define_arch_instruction_constants(type)) return false; } diff --git a/plugins/pychrysalide/arch/raw.c b/plugins/pychrysalide/arch/raw.c index 318eb6b..da6f759 100644 --- a/plugins/pychrysalide/arch/raw.c +++ b/plugins/pychrysalide/arch/raw.c @@ -31,23 +31,29 @@ #include <arch/raw.h> +#include "constants.h" #include "instruction.h" #include "../access.h" #include "../helpers.h" +#define RAW_INSTRUCTION_DOC \ + "The RawInstruction object handles data which is not (yet?) disassembled" \ + " as code in a binary." + + /* Indique si le contenu de l'instruction est du bourrage. */ -static PyObject *py_arch_instruction_is_padding(PyObject *, void *); +static PyObject *py_raw_instruction_get_padding(PyObject *, void *); /* Marque l'instruction comme ne contenant que du bourrage. */ -static int py_arch_instruction_mark_as_padding(PyObject *, PyObject *, void *); +static int py_raw_instruction_set_padding(PyObject *, PyObject *, void *); /* Indique si le contenu de l'instruction est un texte. */ -static PyObject *py_arch_instruction_is_string(PyObject *, void *); +static PyObject *py_raw_instruction_get_string(PyObject *, void *); /* Marque l'instruction comme contenant une chaîne de texte. */ -static int py_arch_instruction_mark_as_string(PyObject *, PyObject *, void *); +static int py_raw_instruction_set_string(PyObject *, PyObject *, void *); @@ -64,12 +70,18 @@ static int py_arch_instruction_mark_as_string(PyObject *, PyObject *, void *); * * ******************************************************************************/ -static PyObject *py_arch_instruction_is_padding(PyObject *self, void *closure) +static PyObject *py_raw_instruction_get_padding(PyObject *self, void *closure) { PyObject *result; /* Conversion à retourner */ GRawInstruction *instr; /* Version native */ bool state; /* Etat courant à consulter */ +#define RAW_INSTRUCTION_PADDING_ATTRIB PYTHON_GETSET_DEF_FULL \ +( \ + padding, py_raw_instruction, \ + "Report if the instruction is seen as padding." \ +) + instr = G_RAW_INSTRUCTION(pygobject_get(self)); state = g_raw_instruction_is_padding(instr); @@ -97,7 +109,7 @@ static PyObject *py_arch_instruction_is_padding(PyObject *self, void *closure) * * ******************************************************************************/ -static int py_arch_instruction_mark_as_padding(PyObject *self, PyObject *value, void *closure) +static int py_raw_instruction_set_padding(PyObject *self, PyObject *value, void *closure) { bool state; /* Nouvel état à définir */ GRawInstruction *instr; /* Version native */ @@ -129,12 +141,18 @@ static int py_arch_instruction_mark_as_padding(PyObject *self, PyObject *value, * * ******************************************************************************/ -static PyObject *py_arch_instruction_is_string(PyObject *self, void *closure) +static PyObject *py_raw_instruction_get_string(PyObject *self, void *closure) { PyObject *result; /* Conversion à retourner */ GRawInstruction *instr; /* Version native */ bool state; /* Etat courant à consulter */ +#define RAW_INSTRUCTION_STRING_ATTRIB PYTHON_GETSET_DEF_FULL \ +( \ + string, py_raw_instruction, \ + "Report if the instruction is seen as a string." \ +) + instr = G_RAW_INSTRUCTION(pygobject_get(self)); state = g_raw_instruction_is_string(instr); @@ -162,7 +180,7 @@ static PyObject *py_arch_instruction_is_string(PyObject *self, void *closure) * * ******************************************************************************/ -static int py_arch_instruction_mark_as_string(PyObject *self, PyObject *value, void *closure) +static int py_raw_instruction_set_string(PyObject *self, PyObject *value, void *closure) { bool state; /* Nouvel état à définir */ GRawInstruction *instr; /* Version native */ @@ -200,14 +218,8 @@ PyTypeObject *get_python_raw_instruction_type(void) }; static PyGetSetDef py_raw_instruction_getseters[] = { - { - "is_padding", py_arch_instruction_is_padding, py_arch_instruction_mark_as_padding, - "Report if the instruction is seen as padding.", NULL - }, - { - "is_string", py_arch_instruction_is_string, py_arch_instruction_mark_as_string, - "Report if the instruction is seen as a string.", NULL - }, + RAW_INSTRUCTION_PADDING_ATTRIB, + RAW_INSTRUCTION_STRING_ATTRIB, { NULL } }; @@ -220,7 +232,7 @@ PyTypeObject *get_python_raw_instruction_type(void) .tp_flags = Py_TPFLAGS_DEFAULT, - .tp_doc = "PyChrysalide raw instruction for a all architectures.", + .tp_doc = RAW_INSTRUCTION_DOC, .tp_methods = py_raw_instruction_methods, .tp_getset = py_raw_instruction_getseters, @@ -264,8 +276,56 @@ bool ensure_python_raw_instruction_is_registered(void) if (!register_class_for_pygobject(dict, G_TYPE_RAW_INSTRUCTION, type, get_python_arch_instruction_type())) return false; + if (!define_raw_instruction_constants(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 instruction brute. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_raw_instruction(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_raw_instruction_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 raw instruction"); + break; + + case 1: + *((GRawInstruction **)dst) = G_RAW_INSTRUCTION(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/arch/raw.h b/plugins/pychrysalide/arch/raw.h index 552db41..c73192e 100644 --- a/plugins/pychrysalide/arch/raw.h +++ b/plugins/pychrysalide/arch/raw.h @@ -37,6 +37,9 @@ PyTypeObject *get_python_raw_instruction_type(void); /* Prend en charge l'objet 'pychrysalide.arch.RawInstruction'. */ bool ensure_python_raw_instruction_is_registered(void); +/* Tente de convertir en instruction brute. */ +int convert_to_raw_instruction(PyObject *, void *); + #endif /* _PLUGINS_PYCHRYSALIDE_ARCH_RAW_H */ |