summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/analysis/content.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-01-16 19:02:56 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-01-16 19:02:56 (GMT)
commit9da8f8b37e3edebc917b4e223dd2447cd7cbc818 (patch)
tree3f330b13e7ca2a0a163882be3043ca9571f25211 /plugins/pychrysalide/analysis/content.c
parenteb9b7fd76451db5c9f07a800c0394480e4b88c9c (diff)
Changed the Python bindings source directory and updated code.
Diffstat (limited to 'plugins/pychrysalide/analysis/content.c')
-rw-r--r--plugins/pychrysalide/analysis/content.c460
1 files changed, 460 insertions, 0 deletions
diff --git a/plugins/pychrysalide/analysis/content.c b/plugins/pychrysalide/analysis/content.c
new file mode 100644
index 0000000..1f36eb8
--- /dev/null
+++ b/plugins/pychrysalide/analysis/content.c
@@ -0,0 +1,460 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * content.c - prototypes pour l'équivalent Python du fichier "analysis/content.c"
+ *
+ * Copyright (C) 2015-2017 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 "content.h"
+
+
+#include <assert.h>
+#include <pygobject.h>
+
+
+#include <i18n.h>
+
+
+#include <analysis/content.h>
+#include <common/endianness.h>
+
+
+#include "../arch/vmpa.h"
+
+
+
+/* Fournit une empreinte unique (SHA256) pour les données. */
+static PyObject *py_binary_content_get_checksum(PyObject *, PyObject *);
+
+/* Détermine le nombre d'octets lisibles. */
+static PyObject *py_binary_content_compute_size(PyObject *, PyObject *);
+
+/* Fournit une portion des données représentées. */
+static PyObject *py_binary_content_read_raw(PyObject *, PyObject *);
+
+/* Lit un nombre non signé sur un octet. */
+static PyObject *py_binary_content_read_u8(PyObject *, PyObject *);
+
+/* Lit un nombre non signé sur deux octets. */
+static PyObject *py_binary_content_read_u16(PyObject *, PyObject *);
+
+/* Lit un nombre non signé sur quatre octets. */
+static PyObject *py_binary_content_read_u32(PyObject *, PyObject *);
+
+/* Lit un nombre non signé sur huit octets. */
+static PyObject *py_binary_content_read_u64(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Fournit une empreinte unique (SHA256) pour les données. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_get_checksum(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ const gchar *checksum; /* Empreinte fournie */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+
+ checksum = g_binary_content_get_checksum(content);
+
+ result = PyUnicode_FromString(checksum);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Détermine le nombre d'octets lisibles. *
+* *
+* Retour : Quantité représentée. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_compute_size(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ phys_t size; /* Quantité d'octets dispos. */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+
+ size = g_binary_content_compute_size(content);
+
+ result = PyLong_FromUnsignedLongLong(size);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Fournit une portion des données représentées. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_read_raw(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ PyObject *addr_obj; /* Objet pour une position */
+ vmpa2t *addr; /* Position interne associée */
+ unsigned long long length; /* Quantité de données à lire */
+ int ret; /* Bilan de lecture des args. */
+ const bin_t *val; /* Valeur lue à faire suivre */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+ assert(content != NULL);
+
+ ret = PyArg_ParseTuple(args, "OK", &addr_obj, &length);
+ if (!ret) return NULL;
+
+ addr = get_internal_vmpa(addr_obj);
+ assert(addr != NULL);
+
+ val = g_binary_content_get_raw_access(content, addr, length);
+ if (val == NULL)
+ {
+ PyErr_SetString(PyExc_Exception, _("Invalid read access."));
+ return NULL;
+ }
+
+ result = PyBytes_FromStringAndSize((char *)val, length);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Lit un nombre non signé sur un octet. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_read_u8(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ int ret; /* Bilan de lecture des args. */
+ PyObject *addr_obj; /* Objet pour une position */
+ vmpa2t *addr; /* Position interne associée */
+ uint8_t val; /* Valeur lue à faire suivre */
+ bool status; /* Bilan de l'opération */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+ assert(content != NULL);
+
+ ret = PyArg_ParseTuple(args, "O", &addr_obj);
+ if (!ret) return NULL;
+
+ addr = get_internal_vmpa(addr_obj);
+ assert(addr != NULL);
+
+ status = g_binary_content_read_u8(content, addr, &val);
+ if (!status)
+ {
+ PyErr_SetString(PyExc_Exception, _("Invalid read access."));
+ return NULL;
+ }
+
+ result = PyLong_FromUnsignedLong(val);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Lit un nombre non signé sur deux octets. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_read_u16(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ int ret; /* Bilan de lecture des args. */
+ PyObject *addr_obj; /* Objet pour une position */
+ unsigned long endianness; /* Boutisme de la lecture */
+ vmpa2t *addr; /* Position interne associée */
+ uint16_t val; /* Valeur lue à faire suivre */
+ bool status; /* Bilan de l'opération */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+ assert(content != NULL);
+
+ ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
+ if (!ret) return NULL;
+
+ addr = get_internal_vmpa(addr_obj);
+ assert(addr != NULL);
+
+ status = g_binary_content_read_u16(content, addr, endianness, &val);
+ if (!status)
+ {
+ PyErr_SetString(PyExc_Exception, _("Invalid read access."));
+ return NULL;
+ }
+
+ result = PyLong_FromUnsignedLong(val);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Lit un nombre non signé sur quatre octets. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_read_u32(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ int ret; /* Bilan de lecture des args. */
+ PyObject *addr_obj; /* Objet pour une position */
+ unsigned long endianness; /* Boutisme de la lecture */
+ vmpa2t *addr; /* Position interne associée */
+ uint32_t val; /* Valeur lue à faire suivre */
+ bool status; /* Bilan de l'opération */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+ assert(content != NULL);
+
+ ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
+ if (!ret) return NULL;
+
+ addr = get_internal_vmpa(addr_obj);
+ assert(addr != NULL);
+
+ status = g_binary_content_read_u32(content, addr, endianness, &val);
+ if (!status)
+ {
+ PyErr_SetString(PyExc_Exception, _("Invalid read access."));
+ return NULL;
+ }
+
+ result = PyLong_FromUnsignedLong(val);
+
+ return result;
+
+}
+
+/******************************************************************************
+* *
+* Paramètres : self = contenu binaire à manipuler. *
+* args = non utilisé ici. *
+* *
+* Description : Lit un nombre non signé sur huit octets. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_binary_content_read_u64(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GBinContent *content; /* Version GLib du format */
+ int ret; /* Bilan de lecture des args. */
+ PyObject *addr_obj; /* Objet pour une position */
+ unsigned long endianness; /* Boutisme de la lecture */
+ vmpa2t *addr; /* Position interne associée */
+ uint64_t val; /* Valeur lue à faire suivre */
+ bool status; /* Bilan de l'opération */
+
+ content = G_BIN_CONTENT(pygobject_get(self));
+ assert(content != NULL);
+
+ ret = PyArg_ParseTuple(args, "Ok", &addr_obj, &endianness);
+ if (!ret) return NULL;
+
+ addr = get_internal_vmpa(addr_obj);
+ assert(addr != NULL);
+
+ status = g_binary_content_read_u64(content, addr, endianness, &val);
+ if (!status)
+ {
+ PyErr_SetString(PyExc_Exception, _("Invalid read access."));
+ return NULL;
+ }
+
+ result = PyLong_FromUnsignedLongLong(val);
+
+ 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_binary_content_type(void)
+{
+ static PyMethodDef py_binary_content_methods[] = {
+ {
+ "get_checksum", py_binary_content_get_checksum,
+ METH_NOARGS,
+ "get_checksum($self, /)\n--\n\nCompute a SHA256 hash as chechsum of handled data."
+ },
+ {
+ "compute_size", py_binary_content_compute_size,
+ METH_NOARGS,
+ "compute_size($self, /)\n--\n\nCompute the quantity of readable bytes."
+ },
+ {
+ "read_raw", py_binary_content_read_raw,
+ METH_VARARGS,
+ "read_raw($self, addr, length, /)\n--\n\nRead bytes from a given position."
+ },
+ {
+ "read_u8", py_binary_content_read_u8,
+ METH_VARARGS,
+ "read_u8($self, addr, /)\n--\n\nRead an unsigned byte from a given position."
+ },
+ {
+ "read_u16", py_binary_content_read_u16,
+ METH_VARARGS,
+ "read_u16($self, addr, endianness, /)\n--\n\nRead two unsigned bytes from a given position."
+ },
+ {
+ "read_u32", py_binary_content_read_u32,
+ METH_VARARGS,
+ "read_u32($self, addr, endianness, /)\n--\n\nRead four unsigned bytes from a given position."
+ },
+ {
+ "read_u64", py_binary_content_read_u64,
+ METH_VARARGS,
+ "read_u64($self, addr, endianness, /)\n--\n\nRead eight unsigned bytes from a given position."
+ },
+ { NULL }
+ };
+
+ static PyGetSetDef py_binary_content_getseters[] = {
+ { NULL }
+ };
+
+ static PyTypeObject py_binary_content_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.analysis.BinContent",
+ .tp_basicsize = sizeof(PyObject),
+
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+
+ .tp_doc = "PyChrysalide binary content",
+
+ .tp_methods = py_binary_content_methods,
+ .tp_getset = py_binary_content_getseters
+
+ };
+
+ return &py_binary_content_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.analysis.BinContent'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_python_binary_content(PyObject *module)
+{
+ PyTypeObject *py_binary_content_type; /* Type Python 'BinContent' */
+ PyObject *dict; /* Dictionnaire du module */
+
+ py_binary_content_type = get_python_binary_content_type();
+
+ dict = PyModule_GetDict(module);
+ pyg_register_interface(dict, "BinContent", G_TYPE_BIN_CONTENT, py_binary_content_type);
+
+ return true;
+
+}