summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/format
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2020-04-19 15:41:55 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2020-04-19 15:41:55 (GMT)
commitfad679ef8cd654646c9234ff8fd39507adad9b8e (patch)
tree70f672b2e0be0b3a4ec34b8ebf756ac30ba3108c /plugins/pychrysalide/format
parent409657d9a1b5122d5e367bff127b66cfbd3a2d47 (diff)
Updated the interface for flat formats.
Diffstat (limited to 'plugins/pychrysalide/format')
-rw-r--r--plugins/pychrysalide/format/flat.c109
-rw-r--r--plugins/pychrysalide/format/flat.h3
2 files changed, 65 insertions, 47 deletions
diff --git a/plugins/pychrysalide/format/flat.c b/plugins/pychrysalide/format/flat.c
index 39ef1f6..2c8e9fd 100644
--- a/plugins/pychrysalide/format/flat.c
+++ b/plugins/pychrysalide/format/flat.c
@@ -41,9 +41,6 @@
/* Crée un nouvel objet Python de type 'FlatFormat'. */
static PyObject *py_flat_format_new(PyTypeObject *, PyObject *, PyObject *);
-/* Indique le type d'architecture visée par le format. */
-static PyObject *py_flat_format_set_target_machine(PyObject *, PyObject *);
-
/******************************************************************************
@@ -64,13 +61,27 @@ static PyObject *py_flat_format_new(PyTypeObject *type, PyObject *args, PyObject
{
PyObject *result; /* Instance à retourner */
GBinContent *content; /* Instance GLib du contenu */
+ const char *machine; /* Identifiant d'architecture */
+ unsigned int endian; /* Boutisme de l'architecture */
int ret; /* Bilan de lecture des args. */
GExeFormat *format; /* Création GLib à transmettre */
- ret = PyArg_ParseTuple(args, "O&", convert_to_binary_content, &content);
+#define FLAT_FORMAT_DOC \
+ "FlatFormat is suitable for all executable contents without a proper" \
+ " file format, such as shellcodes ou eBPF programs.\n" \
+ "\n" \
+ "Instances can be created using the following constructor:\n" \
+ "\n" \
+ " FlatFormat(content, machine, endian)" \
+ "\n" \
+ "Where content is a pychrysalide.analysis.BinContent object, machine" \
+ " defines the target architecture and endian provides the right" \
+ " endianness of the data."
+
+ ret = PyArg_ParseTuple(args, "O&sI", convert_to_binary_content, &content, &machine, &endian);
if (!ret) return NULL;
- format = g_flat_format_new(content);
+ format = g_flat_format_new(content, machine, endian);
if (format == NULL)
{
@@ -91,42 +102,6 @@ static PyObject *py_flat_format_new(PyTypeObject *type, PyObject *args, PyObject
/******************************************************************************
* *
-* Paramètres : self = description de l'exécutable à consulter. *
-* args = arguments accompagnant l'appel. *
-* *
-* Description : Indique le type d'architecture visée par le format. *
-* *
-* Retour : Rien, donc None en Python. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-static PyObject *py_flat_format_set_target_machine(PyObject *self, PyObject *args)
-{
- PyObject *result; /* Instance à retourner */
- GFlatFormat *format; /* Version GLib du format */
- const char *machine; /* Identifiant d'architecture */
- int ret; /* Bilan de lecture des args. */
-
- format = G_FLAT_FORMAT(pygobject_get(self));
- assert(format != NULL);
-
- ret = PyArg_ParseTuple(args, "s", &machine);
- if (!ret) return NULL;
-
- g_flat_format_set_target_machine(format, machine);
-
- result = Py_None;
- Py_INCREF(result);
-
- return result;
-
-}
-
-
-/******************************************************************************
-* *
* Paramètres : - *
* *
* Description : Fournit un accès à une définition de type à diffuser. *
@@ -140,11 +115,6 @@ static PyObject *py_flat_format_set_target_machine(PyObject *self, PyObject *arg
PyTypeObject *get_python_flat_format_type(void)
{
static PyMethodDef py_flat_format_methods[] = {
- {
- "set_machine", py_flat_format_set_target_machine,
- METH_VARARGS,
- "set_machine($self, machine, /)\n--\n\nSet the architecture linked to the flat format."
- },
{ NULL }
};
@@ -161,7 +131,7 @@ PyTypeObject *get_python_flat_format_type(void)
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = "PyChrysalide flat format",
+ .tp_doc = FLAT_FORMAT_DOC,
.tp_methods = py_flat_format_methods,
.tp_getset = py_flat_format_getseters,
@@ -211,3 +181,48 @@ bool ensure_python_flat_format_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 format à plat. *
+* *
+* Retour : Bilan de l'opération, voire indications supplémentaires. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+int convert_to_flat_format(PyObject *arg, void *dst)
+{
+ int result; /* Bilan à retourner */
+
+ result = PyObject_IsInstance(arg, (PyObject *)get_python_flat_format_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 flat format");
+ break;
+
+ case 1:
+ *((GFlatFormat **)dst) = G_FLAT_FORMAT(pygobject_get(arg));
+ break;
+
+ default:
+ assert(false);
+ break;
+
+ }
+
+ return result;
+
+}
diff --git a/plugins/pychrysalide/format/flat.h b/plugins/pychrysalide/format/flat.h
index d7447dd..e3fd65a 100644
--- a/plugins/pychrysalide/format/flat.h
+++ b/plugins/pychrysalide/format/flat.h
@@ -37,6 +37,9 @@ PyTypeObject *get_python_flat_format_type(void);
/* Prend en charge l'objet 'pychrysalide.format.FlatFormat'. */
bool ensure_python_flat_format_is_registered(void);
+/* Tente de convertir en format à plat. */
+int convert_to_flat_format(PyObject *, void *);
+
#endif /* _PLUGINS_PYCHRYSALIDE_FORMAT_FLAT_H */