diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2019-07-21 20:21:59 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2019-07-21 20:21:59 (GMT) |
commit | 6a8385724c74b07cf9ed4cb9052f1af1816e3ea5 (patch) | |
tree | a7bea8a4bb9c816da77cf5c3dd44fbd8fb89b3a7 /plugins/pychrysalide/analysis | |
parent | f0682e7b195acbd4d83148f3829479d682f9ee9f (diff) |
Created more converters for Python arguments.
Diffstat (limited to 'plugins/pychrysalide/analysis')
-rw-r--r-- | plugins/pychrysalide/analysis/binary.c | 7 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/contents/encapsulated.c | 15 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loaded.c | 45 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loaded.h | 3 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/loading.c | 18 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/project.c | 7 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/type.c | 46 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/type.h | 3 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/types/array.c | 7 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/types/encaps.c | 7 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/types/proto.c | 7 | ||||
-rw-r--r-- | plugins/pychrysalide/analysis/types/template.c | 7 |
12 files changed, 120 insertions, 52 deletions
diff --git a/plugins/pychrysalide/analysis/binary.c b/plugins/pychrysalide/analysis/binary.c index a98d11a..e137f87 100644 --- a/plugins/pychrysalide/analysis/binary.c +++ b/plugins/pychrysalide/analysis/binary.c @@ -75,16 +75,13 @@ static PyObject *py_loaded_binary_get_disassembled_cache(PyObject *, void *); static PyObject *py_loaded_binary_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - PyObject *format_obj; /* Objet pour le contenu */ - int ret; /* Bilan de lecture des args. */ GExeFormat *format; /* Instance GLib correspondante*/ + int ret; /* Bilan de lecture des args. */ GLoadedContent *binary; /* Version GLib du binaire */ - ret = PyArg_ParseTuple(args, "O!", get_python_executable_format_type(), &format_obj); + ret = PyArg_ParseTuple(args, "O&", convert_to_executable_format, &format); if (!ret) return NULL; - format = G_EXE_FORMAT(pygobject_get(format_obj)); - g_object_ref(G_OBJECT(format)); binary = g_loaded_binary_new(format); diff --git a/plugins/pychrysalide/analysis/contents/encapsulated.c b/plugins/pychrysalide/analysis/contents/encapsulated.c index 7af79e8..031c49f 100644 --- a/plugins/pychrysalide/analysis/contents/encapsulated.c +++ b/plugins/pychrysalide/analysis/contents/encapsulated.c @@ -59,23 +59,18 @@ static PyObject *py_encaps_content_new(PyTypeObject *, PyObject *, PyObject *); static PyObject *py_encaps_content_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - PyObject *base_obj; /* Base en Python */ - const char *path; /* Chemin vers le contenu final*/ - PyObject *endpoint_obj; /* Contenu final en Python */ - int ret; /* Bilan de lecture des args. */ GBinContent *base; /* Base de l'extraction */ + const char *path; /* Chemin vers le contenu final*/ GBinContent *endpoint; /* Contenu accessible au final */ + int ret; /* Bilan de lecture des args. */ GBinContent *content; /* Version GLib du contenu */ - ret = PyArg_ParseTuple(args, "O!sO!", - get_python_binary_content_type(), &base_obj, + ret = PyArg_ParseTuple(args, "O&sO&", + convert_to_binary_content, &base, &path, - get_python_binary_content_type(), &endpoint_obj); + convert_to_binary_content, &endpoint); if (!ret) return NULL; - base = G_BIN_CONTENT(pygobject_get(base_obj)); - endpoint = G_BIN_CONTENT(pygobject_get(endpoint_obj)); - content = g_encaps_content_new(base, path, endpoint); result = pygobject_new(G_OBJECT(content)); diff --git a/plugins/pychrysalide/analysis/loaded.c b/plugins/pychrysalide/analysis/loaded.c index ae14164..43e749c 100644 --- a/plugins/pychrysalide/analysis/loaded.c +++ b/plugins/pychrysalide/analysis/loaded.c @@ -345,3 +345,48 @@ bool ensure_python_loaded_content_is_registered(void) 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 contenu chargé. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_loaded_content(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_loaded_content_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 loaded content"); + break; + + case 1: + *((GLoadedContent **)dst) = G_LOADED_CONTENT(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/loaded.h b/plugins/pychrysalide/analysis/loaded.h index d87867f..8917e0c 100644 --- a/plugins/pychrysalide/analysis/loaded.h +++ b/plugins/pychrysalide/analysis/loaded.h @@ -37,6 +37,9 @@ PyTypeObject *get_python_loaded_content_type(void); /* Prend en charge l'objet 'pychrysalide.analysis.LoadedContent'. */ bool ensure_python_loaded_content_is_registered(void); +/* Tente de convertir en contenu chargé. */ +int convert_to_loaded_content(PyObject *, void *); + #endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_CONTENT_H */ diff --git a/plugins/pychrysalide/analysis/loading.c b/plugins/pychrysalide/analysis/loading.c index 0b06bd1..11a9854 100644 --- a/plugins/pychrysalide/analysis/loading.c +++ b/plugins/pychrysalide/analysis/loading.c @@ -79,16 +79,14 @@ static PyObject *py_content_explorer_populate_group(PyObject *self, PyObject *ar { PyObject *result; /* Valeur à retourner */ unsigned long long wid; /* Identifiant de groupe */ - PyObject *content_obj; /* Nouveau contenu Python */ + GBinContent *content; /* Contenu nouveau au final */ int ret; /* Bilan de lecture des args. */ GContentExplorer *explorer; /* Explorateur à manipuler */ - GBinContent *content; /* Contenu nouveau au final */ - ret = PyArg_ParseTuple(args, "KO!", &wid, get_python_binary_content_type(), &content_obj); + ret = PyArg_ParseTuple(args, "KO&", &wid, convert_to_binary_content, &content); if (!ret) return NULL; explorer = G_CONTENT_EXPLORER(pygobject_get(self)); - content = G_BIN_CONTENT(pygobject_get(content_obj)); g_content_explorer_populate_group(explorer, wid, content); @@ -116,16 +114,14 @@ static PyObject *py_content_explorer_note_detected(PyObject *self, PyObject *arg { PyObject *result; /* Valeur à retourner */ unsigned long long wid; /* Identifiant de groupe */ - PyObject *loaded_obj; /* Contenu chargé en Python */ + GLoadedContent *loaded; /* Contenu chargé au final */ int ret; /* Bilan de lecture des args. */ GContentExplorer *explorer; /* Explorateur à manipuler */ - GLoadedContent *loaded; /* Contenu chargé au final */ - ret = PyArg_ParseTuple(args, "KO!", &wid, get_python_loaded_content_type(), &loaded_obj); + ret = PyArg_ParseTuple(args, "KO&", &wid, convert_to_loaded_content, &loaded); if (!ret) return NULL; explorer = G_CONTENT_EXPLORER(pygobject_get(self)); - loaded = G_LOADED_CONTENT(pygobject_get(loaded_obj)); g_content_explorer_note_detected(explorer, wid, loaded); @@ -248,16 +244,14 @@ static PyObject *py_content_resolver_add_detected(PyObject *self, PyObject *args { PyObject *result; /* Valeur à retourner */ unsigned long long wid; /* Identifiant de groupe */ - PyObject *loaded_obj; /* Contenu chargé en Python */ + GLoadedContent *loaded; /* Contenu chargé au final */ int ret; /* Bilan de lecture des args. */ GContentResolver *resolver; /* Résolveur à manipuler */ - GLoadedContent *loaded; /* Contenu chargé au final */ - ret = PyArg_ParseTuple(args, "KO!", &wid, get_python_loaded_content_type(), &loaded_obj); + ret = PyArg_ParseTuple(args, "KO&", &wid, convert_to_loaded_content, &loaded); if (!ret) return NULL; resolver = G_CONTENT_RESOLVER(pygobject_get(self)); - loaded = G_LOADED_CONTENT(pygobject_get(loaded_obj)); g_content_resolver_add_detected(resolver, wid, loaded); diff --git a/plugins/pychrysalide/analysis/project.c b/plugins/pychrysalide/analysis/project.c index 98610d5..335a350 100644 --- a/plugins/pychrysalide/analysis/project.c +++ b/plugins/pychrysalide/analysis/project.c @@ -254,18 +254,15 @@ static PyObject *py_study_project_discover_binary_content(PyObject *self, PyObje static PyObject *py_study_project_attach_content(PyObject *self, PyObject *args) { - PyObject *content_obj; /* Objet pour le contenu */ - int ret; /* Bilan de lecture des args. */ GLoadedContent *content; /* Instance GLib correspondante*/ + int ret; /* Bilan de lecture des args. */ GStudyProject *project; /* Version GLib du format */ - ret = PyArg_ParseTuple(args, "O!", get_python_loaded_content_type(), &content_obj); + ret = PyArg_ParseTuple(args, "O&", convert_to_loaded_content, &content); if (!ret) return NULL; project = G_STUDY_PROJECT(pygobject_get(self)); - content = G_LOADED_CONTENT(pygobject_get(content_obj)); - g_study_project_attach_content(project, content); Py_RETURN_NONE; diff --git a/plugins/pychrysalide/analysis/type.c b/plugins/pychrysalide/analysis/type.c index 0134b0a..736656b 100644 --- a/plugins/pychrysalide/analysis/type.c +++ b/plugins/pychrysalide/analysis/type.c @@ -25,6 +25,7 @@ #include "type.h" +#include <assert.h> #include <malloc.h> #include <pygobject.h> @@ -455,3 +456,48 @@ bool ensure_python_data_type_is_registered(void) 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 type de donnée. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_data_type(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_data_type_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 data type"); + break; + + case 1: + *((GDataType **)dst) = G_DATA_TYPE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/type.h b/plugins/pychrysalide/analysis/type.h index e75a993..57258bb 100644 --- a/plugins/pychrysalide/analysis/type.h +++ b/plugins/pychrysalide/analysis/type.h @@ -37,6 +37,9 @@ PyTypeObject *get_python_data_type_type(void); /* Prend en charge l'objet 'pychrysalide.analysis.DataType'. */ bool ensure_python_data_type_is_registered(void); +/* Tente de convertir en type de donnée. */ +int convert_to_data_type(PyObject *, void *); + #endif /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_TYPE_H */ diff --git a/plugins/pychrysalide/analysis/types/array.c b/plugins/pychrysalide/analysis/types/array.c index 5224d36..9c3a1ea 100644 --- a/plugins/pychrysalide/analysis/types/array.c +++ b/plugins/pychrysalide/analysis/types/array.c @@ -82,16 +82,13 @@ static int py_array_type_set_dimension_expression(PyObject *, PyObject *, void * static PyObject *py_array_type_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *result; /* Instance à retourner */ - PyObject *members_obj; /* Objet du type d'éléments */ - int ret; /* Bilan de lecture des args. */ GDataType *members; /* Version GLib du type */ + int ret; /* Bilan de lecture des args. */ GDataType *dtype; /* Version GLib du type */ - ret = PyArg_ParseTuple(args, "O!", get_python_data_type_type(), &members_obj); + ret = PyArg_ParseTuple(args, "O&", convert_to_data_type, &members); if (!ret) return NULL; - members = G_DATA_TYPE(pygobject_get(members_obj)); - dtype = g_array_type_new(members); result = pygobject_new(G_OBJECT(dtype)); g_object_unref(dtype); diff --git a/plugins/pychrysalide/analysis/types/encaps.c b/plugins/pychrysalide/analysis/types/encaps.c index c2d435c..b876547 100644 --- a/plugins/pychrysalide/analysis/types/encaps.c +++ b/plugins/pychrysalide/analysis/types/encaps.c @@ -66,16 +66,13 @@ static PyObject *py_encapsulated_type_new(PyTypeObject *type, PyObject *args, Py { PyObject *result; /* Instance à retourner */ EncapsulationType encapsulation; /* Type d'encapsulation */ - PyObject *encaps_obj; /* Objet du type encapsulé */ - int ret; /* Bilan de lecture des args. */ GDataType *encapsulated; /* Type encapsulé */ + int ret; /* Bilan de lecture des args. */ GDataType *dtype; /* Version GLib du type */ - ret = PyArg_ParseTuple(args, "kO!", &encapsulation, get_python_data_type_type(), &encaps_obj); + ret = PyArg_ParseTuple(args, "kO&", &encapsulation, convert_to_data_type, &encapsulated); if (!ret) return NULL; - encapsulated = G_DATA_TYPE(pygobject_get(encaps_obj)); - dtype = g_encapsulated_type_new(encapsulation, encapsulated); result = pygobject_new(G_OBJECT(dtype)); g_object_unref(dtype); diff --git a/plugins/pychrysalide/analysis/types/proto.c b/plugins/pychrysalide/analysis/types/proto.c index a6046ab..975be1e 100644 --- a/plugins/pychrysalide/analysis/types/proto.c +++ b/plugins/pychrysalide/analysis/types/proto.c @@ -98,18 +98,15 @@ static PyObject *py_proto_type_new(PyTypeObject *type, PyObject *args, PyObject static PyObject *py_proto_type_add_arg(PyObject *self, PyObject *args) { - PyObject *arg_obj; /* Objet du type d'argument */ - int ret; /* Bilan de lecture des args. */ GDataType *arg; /* Version GLib du type */ + int ret; /* Bilan de lecture des args. */ GProtoType *type; /* Version GLib du type */ - ret = PyArg_ParseTuple(args, "O!", get_python_data_type_type(), &arg_obj); + ret = PyArg_ParseTuple(args, "O&", convert_to_data_type, &arg); if (!ret) return NULL; type = G_PROTO_TYPE(pygobject_get(self)); - arg = G_DATA_TYPE(pygobject_get(arg_obj)); - g_object_ref(G_OBJECT(arg)); g_proto_type_add_arg(type, arg); diff --git a/plugins/pychrysalide/analysis/types/template.c b/plugins/pychrysalide/analysis/types/template.c index 485eca4..0f6c002 100644 --- a/plugins/pychrysalide/analysis/types/template.c +++ b/plugins/pychrysalide/analysis/types/template.c @@ -99,18 +99,15 @@ static PyObject *py_template_type_new(PyTypeObject *type, PyObject *args, PyObje static PyObject *py_template_type_add_param(PyObject *self, PyObject *args) { - PyObject *param_obj; /* Objet du type de paramètre */ - int ret; /* Bilan de lecture des args. */ GDataType *param; /* Version GLib du type */ + int ret; /* Bilan de lecture des args. */ GTemplateType *type; /* Version GLib du type */ - ret = PyArg_ParseTuple(args, "O!", get_python_data_type_type(), ¶m_obj); + ret = PyArg_ParseTuple(args, "O&", convert_to_data_type, ¶m); if (!ret) return NULL; type = G_TEMPLATE_TYPE(pygobject_get(self)); - param = G_DATA_TYPE(pygobject_get(param_obj)); - g_object_ref(G_OBJECT(param)); g_template_type_add_param(type, param); |