summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/common
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/common
parenteb9b7fd76451db5c9f07a800c0394480e4b88c9c (diff)
Changed the Python bindings source directory and updated code.
Diffstat (limited to 'plugins/pychrysalide/common')
-rw-r--r--plugins/pychrysalide/common/Makefile.am17
-rw-r--r--plugins/pychrysalide/common/bits.c653
-rw-r--r--plugins/pychrysalide/common/bits.h48
-rw-r--r--plugins/pychrysalide/common/fnv1a.c145
-rw-r--r--plugins/pychrysalide/common/fnv1a.h42
-rw-r--r--plugins/pychrysalide/common/module.c99
-rw-r--r--plugins/pychrysalide/common/module.h39
-rw-r--r--plugins/pychrysalide/common/pathname.c203
-rw-r--r--plugins/pychrysalide/common/pathname.h42
9 files changed, 1288 insertions, 0 deletions
diff --git a/plugins/pychrysalide/common/Makefile.am b/plugins/pychrysalide/common/Makefile.am
new file mode 100644
index 0000000..0637229
--- /dev/null
+++ b/plugins/pychrysalide/common/Makefile.am
@@ -0,0 +1,17 @@
+
+noinst_LTLIBRARIES = libpychrysacommon.la
+
+libpychrysacommon_la_SOURCES = \
+ bits.h bits.c \
+ fnv1a.h fnv1a.c \
+ module.h module.c \
+ pathname.h pathname.c
+
+
+libpychrysacommon_la_LDFLAGS =
+
+
+AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
+ -I../../../src
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS)
diff --git a/plugins/pychrysalide/common/bits.c b/plugins/pychrysalide/common/bits.c
new file mode 100644
index 0000000..33f81c3
--- /dev/null
+++ b/plugins/pychrysalide/common/bits.c
@@ -0,0 +1,653 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * bits.c - équivalent Python du fichier "common/bits.c"
+ *
+ * Copyright (C) 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 "bits.h"
+
+
+#include "../helpers.h"
+
+
+
+/* Encapsulation d'un champ de bits */
+typedef struct _py_bitfield_t
+{
+ PyObject_HEAD /* A laisser en premier */
+
+ bitfield_t *native; /* Champ de bits représenté */
+
+} py_bitfield_t;
+
+
+
+/* Crée un nouvel objet Python de type 'bitfield_t'. */
+static PyObject *py_bitfield_new(PyTypeObject *, PyObject *, PyObject *);
+
+/* Crée une copie d'un champ de bits classique. */
+static PyObject *py_bitfield_dup(PyObject *, PyObject *);
+
+/* Effectue une opération de type 'and' avec le type 'bitfield'. */
+static PyObject *py_bitfield_nb_and(PyObject *, PyObject *);
+
+/* Effectue une opération de type 'or' avec le type 'bitfield'. */
+static PyObject *py_bitfield_nb_or(PyObject *, PyObject *);
+
+/* Effectue une comparaison avec un objet Python 'bitfield'. */
+static PyObject *py_bitfield_richcompare(PyObject *, PyObject *, int);
+
+/* Bascule à 0 un champ de bits dans son intégralité. */
+static PyObject *py_bitfield_reset_all(PyObject *, PyObject *);
+
+/* Bascule à 1 un champ de bits dans son intégralité. */
+static PyObject *py_bitfield_set_all(PyObject *, PyObject *);
+
+/* Bascule à 0 une partie d'un champ de bits. */
+static PyObject *py_bitfield_reset(PyObject *, PyObject *);
+
+/* Bascule à 1 une partie d'un champ de bits. */
+static PyObject *py_bitfield_set(PyObject *, PyObject *);
+
+/* Détermine si un bit est à 1 dans un champ de bits. */
+static PyObject *py_bitfield_test(PyObject *, PyObject *);
+
+/* Détermine si un ensemble de bits est à 0 dans un champ. */
+static PyObject *py_bitfield_test_none(PyObject *, PyObject *);
+
+/* Détermine si un ensemble de bits est à 1 dans un champ. */
+static PyObject *py_bitfield_test_all(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : type = type de l'objet à instancier. *
+* args = arguments fournis à l'appel. *
+* kwds = arguments de type key=val fournis. *
+* *
+* Description : Crée un nouvel objet Python de type 'bitfield_t'. *
+* *
+* Retour : Instance Python mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ py_bitfield_t *result; /* Instance à retourner */
+ unsigned long length; /* Taille du champ à créer */
+ int state; /* Initialisation par défaut */
+ int ret; /* Bilan de lecture des args. */
+
+ ret = PyArg_ParseTuple(args, "kp", &length, &state);
+ if (!ret) return NULL;
+
+ result = (py_bitfield_t *)type->tp_alloc(type, 0);
+
+ result->native = create_bit_field(length, state);
+
+ return (PyObject *)result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à dupliquer. *
+* args = non utilisé ici. *
+* *
+* Description : Crée une copie d'un champ de bits classique. *
+* *
+* Retour : Champ de bits mis en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_dup(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ py_bitfield_t *bf; /* Instance à manipuler */
+
+ bf = (py_bitfield_t *)self;
+
+ result = build_from_internal_bitfield(bf->native);;
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : o1 = premier élément concerné par l'opération. *
+* o2 = second élément concerné par l'opération. *
+* *
+* Description : Effectue une opération de type 'and' avec le type 'bitfield'.*
+* *
+* Retour : Résultat de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_nb_and(PyObject *o1, PyObject *o2)
+{
+ PyObject *result; /* Résultat à retourner */
+ int ret; /* Bilan de compatibilité */
+ py_bitfield_t *bf_1; /* Instance à manipuler #1 */
+ py_bitfield_t *bf_2; /* Instance à manipuler #2 */
+ py_bitfield_t *new; /* Nouvelle version en place */
+
+ ret = PyObject_IsInstance(o2, (PyObject *)get_python_bitfield_type());
+ if (!ret)
+ {
+ result = NULL;
+ goto pbna_done;
+ }
+
+ bf_1 = (py_bitfield_t *)o1;
+ bf_2 = (py_bitfield_t *)o2;
+
+ result = build_from_internal_bitfield(bf_1->native);
+
+ new = (py_bitfield_t *)result;
+
+ and_bit_field(new->native, bf_2->native);
+
+ pbna_done:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : o1 = premier élément concerné par l'opération. *
+* o2 = second élément concerné par l'opération. *
+* *
+* Description : Effectue une opération de type 'or' avec le type 'bitfield'. *
+* *
+* Retour : Résultat de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_nb_or(PyObject *o1, PyObject *o2)
+{
+ PyObject *result; /* Résultat à retourner */
+ int ret; /* Bilan de compatibilité */
+ py_bitfield_t *bf_1; /* Instance à manipuler #1 */
+ py_bitfield_t *bf_2; /* Instance à manipuler #2 */
+ py_bitfield_t *new; /* Nouvelle version en place */
+
+ ret = PyObject_IsInstance(o2, (PyObject *)get_python_bitfield_type());
+ if (!ret)
+ {
+ result = NULL;
+ goto pbna_done;
+ }
+
+ bf_1 = (py_bitfield_t *)o1;
+ bf_2 = (py_bitfield_t *)o2;
+
+ result = build_from_internal_bitfield(bf_1->native);
+
+ new = (py_bitfield_t *)result;
+
+ or_bit_field(new->native, bf_2->native);
+
+ pbna_done:
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : a = premier object Python à consulter. *
+* b = second object Python à consulter. *
+* op = type de comparaison menée. *
+* *
+* Description : Effectue une comparaison avec un objet Python 'bitfield'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_richcompare(PyObject *a, PyObject *b, int op)
+{
+ PyObject *result; /* Bilan à retourner */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf_a; /* Instance à manipuler #1 */
+ py_bitfield_t *bf_b; /* Instance à manipuler #2 */
+ int status; /* Résultat d'une comparaison */
+
+ ret = PyObject_IsInstance(b, (PyObject *)get_python_bitfield_type());
+ if (!ret)
+ {
+ result = Py_NotImplemented;
+ goto cmp_done;
+ }
+
+ bf_a = (py_bitfield_t *)a;
+ bf_b = (py_bitfield_t *)b;
+
+ status = compare_bit_fields(bf_a->native, bf_b->native);
+
+ result = status_to_rich_cmp_state(status, op);
+
+ cmp_done:
+
+ Py_INCREF(result);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à modifier. *
+* args = non utilisé ici. *
+* *
+* Description : Bascule à 0 un champ de bits dans son intégralité. *
+* *
+* Retour : Rien (None). *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_reset_all(PyObject *self, PyObject *args)
+{
+ py_bitfield_t *bf; /* Instance à manipuler */
+
+ bf = (py_bitfield_t *)self;
+
+ reset_all_in_bit_field(bf->native);
+
+ Py_RETURN_NONE;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à modifier. *
+* args = non utilisé ici. *
+* *
+* Description : Bascule à 1 un champ de bits dans son intégralité. *
+* *
+* Retour : Rien (None). *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_set_all(PyObject *self, PyObject *args)
+{
+ py_bitfield_t *bf; /* Instance à manipuler */
+
+ bf = (py_bitfield_t *)self;
+
+ set_all_in_bit_field(bf->native);
+
+ Py_RETURN_NONE;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à consulter. *
+* args = arguments fournis pour la conduite de l'opération. *
+* *
+* Description : Bascule à 0 une partie d'un champ de bits. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_reset(PyObject *self, PyObject *args)
+{
+ unsigned long first; /* Indice du premier bit testé */
+ unsigned long count; /* Nombre de bits à analyser */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf; /* Instance à manipuler */
+
+ ret = PyArg_ParseTuple(args, "kk", &first, &count);
+ if (!ret) return NULL;
+
+ bf = (py_bitfield_t *)self;
+
+ reset_in_bit_field(bf->native, first, count);
+
+ Py_RETURN_NONE;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à consulter. *
+* args = arguments fournis pour la conduite de l'opération. *
+* *
+* Description : Bascule à 1 une partie d'un champ de bits. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_set(PyObject *self, PyObject *args)
+{
+ unsigned long first; /* Indice du premier bit testé */
+ unsigned long count; /* Nombre de bits à analyser */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf; /* Instance à manipuler */
+
+ ret = PyArg_ParseTuple(args, "kk", &first, &count);
+ if (!ret) return NULL;
+
+ bf = (py_bitfield_t *)self;
+
+ set_in_bit_field(bf->native, first, count);
+
+ Py_RETURN_NONE;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à consulter. *
+* args = arguments fournis pour la conduite de l'opération. *
+* *
+* Description : Détermine si un bit est à 1 dans un champ de bits. *
+* *
+* Retour : true si le bit correspondant est à l'état haut. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_test(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Bilan à faire remonter */
+ unsigned long n; /* Indice du bit à traiter */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf; /* Instance à manipuler */
+ bool status; /* Bilan d'analyse */
+
+ ret = PyArg_ParseTuple(args, "k", &n);
+ if (!ret) return NULL;
+
+ bf = (py_bitfield_t *)self;
+
+ status = test_in_bit_field(bf->native, n);
+
+ result = status ? Py_True : Py_False;
+
+ Py_INCREF(result);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à consulter. *
+* args = arguments fournis pour la conduite de l'opération. *
+* *
+* Description : Détermine si un ensemble de bits est à 0 dans un champ. *
+* *
+* Retour : True si les bits correspondants sont à l'état bas. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_test_none(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Bilan à faire remonter */
+ unsigned long first; /* Indice du premier bit testé */
+ unsigned long count; /* Nombre de bits à analyser */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf; /* Instance à manipuler */
+ bool status; /* Bilan d'analyse */
+
+ ret = PyArg_ParseTuple(args, "kk", &first, &count);
+ if (!ret) return NULL;
+
+ bf = (py_bitfield_t *)self;
+
+ status = test_none_in_bit_field(bf->native, first, count);
+
+ result = status ? Py_True : Py_False;
+
+ Py_INCREF(result);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = champ de bits à consulter. *
+* args = arguments fournis pour la conduite de l'opération. *
+* *
+* Description : Détermine si un ensemble de bits est à 1 dans un champ. *
+* *
+* Retour : True si les bits correspondants sont à l'état haut. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_bitfield_test_all(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Bilan à faire remonter */
+ unsigned long first; /* Indice du premier bit testé */
+ unsigned long count; /* Nombre de bits à analyser */
+ int ret; /* Bilan de lecture des args. */
+ py_bitfield_t *bf; /* Instance à manipuler */
+ bool status; /* Bilan d'analyse */
+
+ ret = PyArg_ParseTuple(args, "kk", &first, &count);
+ if (!ret) return NULL;
+
+ bf = (py_bitfield_t *)self;
+
+ status = test_all_in_bit_field(bf->native, first, count);
+
+ result = status ? Py_True : Py_False;
+
+ Py_INCREF(result);
+
+ 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_bitfield_type(void)
+{
+ static PyNumberMethods py_bitfield_nb_proto = {
+
+ .nb_and = py_bitfield_nb_and,
+ .nb_or = py_bitfield_nb_or
+
+ };
+
+ static PyMethodDef py_bitfield_methods[] = {
+ {
+ "dup", py_bitfield_dup,
+ METH_NOARGS,
+ "dup(self, /)\n--\n\nDuplicate a bitfield."
+ },
+ {
+ "reset_all", py_bitfield_reset_all,
+ METH_NOARGS,
+ "reset_all(self, /)\n--\n\nSwitch to 0 all bits in a bitfield."
+ },
+ {
+ "set_all", py_bitfield_set_all,
+ METH_NOARGS,
+ "set_all(self, /)\n--\n\nSwitch to 1 all bits in a bitfield."
+ },
+ {
+ "reset", py_bitfield_reset,
+ METH_VARARGS,
+ "reset(self, first, count, /)\n--\n\nSwitch to 0 a part of bits in a bitfield."
+ },
+ {
+ "set", py_bitfield_set,
+ METH_VARARGS,
+ "set(self, first, count, /)\n--\n\nSwitch to 1 a part of bits in a bitfield."
+ },
+ {
+ "test", py_bitfield_test,
+ METH_VARARGS,
+ "test(self, n, /)\n--\n\nTest if a given bit is set in a bitfield."
+ },
+ {
+ "test_none", py_bitfield_test_none,
+ METH_VARARGS,
+ "test_none(self, first, count, /)\n--\n\nTest a range of bits to 0."
+ },
+ {
+ "test_all", py_bitfield_test_all,
+ METH_VARARGS,
+ "test_all(self, first, count, /)\n--\n\nTest a range of bits to 1."
+ },
+ { NULL }
+ };
+
+ static PyGetSetDef py_bitfield_getseters[] = {
+ { NULL }
+ };
+
+ static PyTypeObject py_bitfield_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.common.bitfield",
+ .tp_basicsize = sizeof(py_bitfield_t),
+
+ .tp_as_number = &py_bitfield_nb_proto,
+
+ .tp_flags = Py_TPFLAGS_DEFAULT,
+
+ .tp_doc = "Python object for bitfield_t.",
+
+ .tp_richcompare = py_bitfield_richcompare,
+
+ .tp_methods = py_bitfield_methods,
+ .tp_getset = py_bitfield_getseters,
+ .tp_new = (newfunc)py_bitfield_new
+
+ };
+
+ return &py_bitfield_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.common.bitfield'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_python_bitfield(PyObject *module)
+{
+ PyTypeObject *py_bitfield_type; /* Type Python pour 'bitfield' */
+ int ret; /* Bilan d'un appel */
+
+ py_bitfield_type = get_python_bitfield_type();
+
+ if (PyType_Ready(py_bitfield_type) != 0)
+ return false;
+
+ Py_INCREF(py_bitfield_type);
+ ret = PyModule_AddObject(module, "bitfield", (PyObject *)py_bitfield_type);
+
+ return (ret == 0);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : field = structure interne à copier en objet Python. *
+* *
+* Description : Convertit une structure de type 'bitfield' en objet Python. *
+* *
+* Retour : Object Python résultant de la conversion opérée. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PyObject *build_from_internal_bitfield(const bitfield_t *field)
+{
+ py_bitfield_t *result; /* Instance à retourner */
+ PyTypeObject *type; /* Type à instancier */
+
+ type = get_python_bitfield_type();
+
+ result = (py_bitfield_t *)type->tp_alloc(type, 0);
+
+ result->native = dup_bit_field(field);
+
+ return (PyObject *)result;
+
+}
diff --git a/plugins/pychrysalide/common/bits.h b/plugins/pychrysalide/common/bits.h
new file mode 100644
index 0000000..3e2af73
--- /dev/null
+++ b/plugins/pychrysalide/common/bits.h
@@ -0,0 +1,48 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * bits.h - prototypes pour l'équivalent Python du fichier "common/bits.h"
+ *
+ * Copyright (C) 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
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_COMMON_BITS_H
+#define _PLUGINS_PYCHRYSALIDE_COMMON_BITS_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+#include <common/bits.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_bitfield_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.common.bitfield'. */
+bool register_python_bitfield(PyObject *);
+
+/* Convertit une structure de type 'bitfield' en objet Python. */
+PyObject *build_from_internal_bitfield(const bitfield_t *);
+
+
+
+#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_BITS_H */
diff --git a/plugins/pychrysalide/common/fnv1a.c b/plugins/pychrysalide/common/fnv1a.c
new file mode 100644
index 0000000..cc2c342
--- /dev/null
+++ b/plugins/pychrysalide/common/fnv1a.c
@@ -0,0 +1,145 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * fnv1a.c - équivalent Python du fichier "common/fnv1a.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 "fnv1a.h"
+
+
+#include <pygobject.h>
+
+
+#include <common/fnv1a.h>
+
+
+
+/* Détermine l'empreinte FNV1a d'une chaîne de caractères. */
+static PyObject *py_fnv1a_hash(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : self = NULL car méthode statique. *
+* args = arguments fournis lors de l'appel à la fonction. *
+* *
+* Description : Détermine l'empreinte FNV1a d'une chaîne de caractères. *
+* *
+* Retour : Numéro de révision. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_fnv1a_hash(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ const char *str; /* Chaîne à traiter. */
+ int ret; /* Bilan de lecture des args. */
+ fnv64_t value; /* Empreinte calculée */
+
+ ret = PyArg_ParseTuple(args, "s", &str);
+ if (!ret) Py_RETURN_NONE;
+
+ value = fnv_64a_hash(str);
+
+ result = Py_BuildValue("K", (unsigned long long)value);
+
+ 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_fnv1a_type(void)
+{
+ static PyMethodDef py_fnv1a_methods[] = {
+
+ { "hash", py_fnv1a_hash,
+ METH_VARARGS | METH_STATIC,
+ "hash(str, /)\n--\n\nCompute the FNV-1a hash from a given string."
+ },
+ { NULL }
+
+ };
+
+ static PyTypeObject py_fnv1a_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.core.fnv1a",
+ .tp_basicsize = sizeof(PyObject),
+
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT,
+
+ .tp_doc = "Python version for Chrysalide of the Fowler-Noll-Vo hash function.",
+
+ .tp_methods = py_fnv1a_methods
+
+ };
+
+ return &py_fnv1a_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.core.fnv1a'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_python_fnv1a(PyObject *module)
+{
+ PyTypeObject *py_fnv1a_type; /* Type Python pour 'fnv1a' */
+ int ret; /* Bilan d'un appel */
+
+ py_fnv1a_type = get_python_fnv1a_type();
+
+ //py_fnv1a_type->tp_new = PyType_GenericNew;
+
+ if (PyType_Ready(py_fnv1a_type) != 0)
+ return false;
+
+ Py_INCREF(py_fnv1a_type);
+ ret = PyModule_AddObject(module, "fnv1a", (PyObject *)py_fnv1a_type);
+
+ return (ret == 0);
+
+}
diff --git a/plugins/pychrysalide/common/fnv1a.h b/plugins/pychrysalide/common/fnv1a.h
new file mode 100644
index 0000000..0a40770
--- /dev/null
+++ b/plugins/pychrysalide/common/fnv1a.h
@@ -0,0 +1,42 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * fnv1a.h - prototypes pour l'équivalent Python du fichier "common/fnv1a.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
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_COMMON_FNV1A_H
+#define _PLUGINS_PYCHRYSALIDE_COMMON_FNV1A_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_fnv1a_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.common.fnv1a'. */
+bool register_python_fnv1a(PyObject *);
+
+
+
+#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_FNV1A_H */
diff --git a/plugins/pychrysalide/common/module.c b/plugins/pychrysalide/common/module.c
new file mode 100644
index 0000000..7ff7528
--- /dev/null
+++ b/plugins/pychrysalide/common/module.c
@@ -0,0 +1,99 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * module.c - intégration du répertoire common en tant que module
+ *
+ * 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 "module.h"
+
+
+#include "bits.h"
+#include "fnv1a.h"
+#include "pathname.h"
+#include "../access.h"
+
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Ajoute le module 'common' au module Python. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool add_common_module_to_python_module(PyObject *super)
+{
+ bool result; /* Bilan à retourner */
+ PyObject *module; /* Sous-module mis en place */
+ int ret; /* Bilan d'un appel */
+
+ static PyModuleDef py_chrysalide_common_module = {
+
+ .m_base = PyModuleDef_HEAD_INIT,
+
+ .m_name = "pychrysalide.common",
+ .m_doc = "Python module for Chrysalide.common",
+
+ .m_size = -1,
+
+ };
+
+ result = false;
+
+ module = PyModule_Create(&py_chrysalide_common_module);
+ if (module == NULL) return false;
+
+ ret = PyState_AddModule(super, &py_chrysalide_common_module);
+ if (ret != 0) goto acmtpm_exit;
+
+ ret = _PyImport_FixupBuiltin(module, "pychrysalide.common");
+ if (ret != 0) goto acmtpm_exit;
+
+ Py_INCREF(module);
+ ret = PyModule_AddObject(super, "common", module);
+ if (ret != 0) goto acmtpm_exit;
+
+ result = true;
+
+ result &= register_python_bitfield(module);
+ result &= register_python_fnv1a(module);
+ result &= register_python_pathname(module);
+
+ if (result)
+ register_access_to_python_module("pychrysalide.common", module);
+
+ acmtpm_exit:
+
+ if (!result)
+ {
+ printf("something went wrong in %s...\n", __FUNCTION__);
+ /* ... */
+
+ }
+
+ return result;
+
+}
diff --git a/plugins/pychrysalide/common/module.h b/plugins/pychrysalide/common/module.h
new file mode 100644
index 0000000..446f6fe
--- /dev/null
+++ b/plugins/pychrysalide/common/module.h
@@ -0,0 +1,39 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * module.h - prototypes pour l'intégration du répertoire common en tant que module
+ *
+ * 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
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_COMMON_MODULE_H
+#define _PLUGINS_PYCHRYSALIDE_COMMON_MODULE_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Ajoute le module 'common' au module Python. */
+bool add_common_module_to_python_module(PyObject *);
+
+
+
+#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_MODULE_H */
diff --git a/plugins/pychrysalide/common/pathname.c b/plugins/pychrysalide/common/pathname.c
new file mode 100644
index 0000000..40977df
--- /dev/null
+++ b/plugins/pychrysalide/common/pathname.c
@@ -0,0 +1,203 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * pathname.c - équivalent Python du fichier "common/pathname.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 "pathname.h"
+
+
+#include <malloc.h>
+#include <pygobject.h>
+
+
+#include <i18n.h>
+
+
+#include <common/pathname.h>
+
+
+
+/* Calcule le chemin relatif entre deux fichiers donnés. */
+static PyObject *py_build_relative_filename(PyObject *, PyObject *);
+
+/* Calcule le chemin absolu d'un fichier par rapport à un autre. */
+static PyObject *py_build_absolute_filename(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : self = NULL car méthode statique. *
+* args = arguments fournis lors de l'appel à la fonction. *
+* *
+* Description : Calcule le chemin relatif entre deux fichiers donnés. *
+* *
+* Retour : Chemin relatif obtenu. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_build_relative_filename(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ const char *ref; /* Fichier de référence */
+ const char *target; /* Fichier à cibler */
+ int ret; /* Bilan de lecture des args. */
+ char *relative; /* Chemin d'accès construit */
+
+ ret = PyArg_ParseTuple(args, "ss", &ref, &target);
+ if (!ret) Py_RETURN_NONE;
+
+ relative = build_relative_filename(ref, target);
+
+ result = PyUnicode_FromString(relative);
+
+ free(relative);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = NULL car méthode statique. *
+* args = arguments fournis lors de l'appel à la fonction. *
+* *
+* Description : Calcule le chemin absolu d'un fichier par rapport à un autre.*
+* *
+* Retour : Chemin absolu obtenu. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_build_absolute_filename(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ const char *ref; /* Fichier de référence */
+ const char *target; /* Fichier à cibler */
+ int ret; /* Bilan de lecture des args. */
+ char *relative; /* Chemin d'accès construit */
+
+ ret = PyArg_ParseTuple(args, "ss", &ref, &target);
+ if (!ret) Py_RETURN_NONE;
+
+ relative = build_absolute_filename(ref, target);
+
+ if (relative == NULL)
+ {
+ PyErr_SetString(PyExc_ValueError, _("Relative path is too deep."));
+ result = NULL;
+ }
+ else
+ {
+ result = PyUnicode_FromString(relative);
+
+ free(relative);
+
+ }
+
+ 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_pathname_type(void)
+{
+ static PyMethodDef py_pathname_methods[] = {
+
+ { "build_relative_filename", py_build_relative_filename,
+ METH_VARARGS | METH_STATIC,
+ "build_relative_filename(ref, target, /)\n--\n\nCompute the relative path between two files."
+ },
+ { "build_absolute_filename", py_build_absolute_filename,
+ METH_VARARGS | METH_STATIC,
+ "build_absolute_filename(ref, target, /)\n--\n\nCompute the absolute path for a file."
+ },
+ { NULL }
+
+ };
+
+ static PyTypeObject py_pathname_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.core.pathname",
+ .tp_basicsize = sizeof(PyObject),
+
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT,
+
+ .tp_doc = "Path manipulations in Python for Chrysalide.",
+
+ .tp_methods = py_pathname_methods
+
+ };
+
+ return &py_pathname_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.core.pathname'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_python_pathname(PyObject *module)
+{
+ PyTypeObject *py_pathname_type; /* Type Python pour 'pathname' */
+ int ret; /* Bilan d'un appel */
+
+ py_pathname_type = get_python_pathname_type();
+
+ //py_pathname_type->tp_new = PyType_GenericNew;
+
+ if (PyType_Ready(py_pathname_type) != 0)
+ return false;
+
+ Py_INCREF(py_pathname_type);
+ ret = PyModule_AddObject(module, "pathname", (PyObject *)py_pathname_type);
+
+ return (ret == 0);
+
+}
diff --git a/plugins/pychrysalide/common/pathname.h b/plugins/pychrysalide/common/pathname.h
new file mode 100644
index 0000000..a3940c9
--- /dev/null
+++ b/plugins/pychrysalide/common/pathname.h
@@ -0,0 +1,42 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * pathname.h - prototypes pour l'équivalent Python du fichier "common/pathname.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
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_COMMON_PATHNAME_H
+#define _PLUGINS_PYCHRYSALIDE_COMMON_PATHNAME_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_pathname_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.common.pathname'. */
+bool register_python_pathname(PyObject *);
+
+
+
+#endif /* _PLUGINS_PYCHRYSALIDE_COMMON_PATHNAME_H */