From 4c611d6f41d82603a5d37baf88b0bb213044eb60 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 12 Aug 2015 19:09:56 +0000
Subject: Improved the python plugin code.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@570 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                             |  7 ++++++
 plugins/pychrysa/arch/instruction.c   |  2 +-
 plugins/pychrysa/glibext/bincontent.c | 46 +++++++++++++++++++++++++++++++----
 plugins/pychrysa/gui/editem.c         |  6 ++---
 4 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 050ae77..dac9be0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 15-08-12  Cyrille Bagard <nocbos@gmail.com>
 
+	* plugins/pychrysa/arch/instruction.c:
+	* plugins/pychrysa/glibext/bincontent.c:
+	* plugins/pychrysa/gui/editem.c:
+	Improve the python plugin code.
+
+15-08-12  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/format/dbg_format.c:
 	* src/format/dbg_format.h:
 	* src/format/dbg_format-int.h:
diff --git a/plugins/pychrysa/arch/instruction.c b/plugins/pychrysa/arch/instruction.c
index d675226..2a06ede 100644
--- a/plugins/pychrysa/arch/instruction.c
+++ b/plugins/pychrysa/arch/instruction.c
@@ -518,7 +518,7 @@ static void py_arch_instruction_iterator_dealloc(PyArchInstructionIter *self)
 {
     PyObject *first;                        /* Récupération de la première */
 
-    first = pychrysalide_get_pygobject(G_OBJECT(self->head));
+    first = pygobject_new(G_OBJECT(self->head));
 
     Py_DECREF(first);
     g_object_unref(G_OBJECT(self->head));
diff --git a/plugins/pychrysa/glibext/bincontent.c b/plugins/pychrysa/glibext/bincontent.c
index a418c34..085c9af 100644
--- a/plugins/pychrysa/glibext/bincontent.c
+++ b/plugins/pychrysa/glibext/bincontent.c
@@ -40,6 +40,9 @@ static PyObject *py_binary_content_new(PyTypeObject *, PyObject *, PyObject *);
 /* 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 *);
+
 /* Lit un nombre non signé sur un octet. */
 static PyObject *py_binary_content_read_u8(PyObject *, PyObject *);
 
@@ -114,7 +117,36 @@ static PyObject *py_binary_content_get_checksum(PyObject *self, PyObject *args)
     checksum = g_binary_content_get_cheksum(content);
 
     result = PyUnicode_FromString(checksum);
-    Py_INCREF(result);
+
+    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;
 
@@ -146,11 +178,11 @@ static PyObject *py_binary_content_read_u8(PyObject *self, PyObject *args)
 
     content = G_BIN_CONTENT(pygobject_get(self));
 
-    printf("Passage\n");
+    //printf("Passage\n");
 
     ret = PyArg_ParseTuple(args, "O", &addr_obj);
 
-    printf("ret == %d\n", ret);
+    //printf("ret == %d\n", ret);
 
     if (!ret) return NULL;
 
@@ -160,7 +192,7 @@ static PyObject *py_binary_content_read_u8(PyObject *self, PyObject *args)
     status = g_binary_content_read_u8(content, addr, &val);
     if (!status) return NULL;
 
-    printf("val :: 0x%02hhx\n", val);
+    //printf("val :: 0x%02hhx\n", val);
 
     result = PyBytes_FromStringAndSize((char *)&val, 1);;
     //Py_INCREF(result);
@@ -193,7 +225,11 @@ PyTypeObject *get_python_binary_content_type(void)
     static PyMethodDef py_binary_content_methods[] = {
         { "get_cheksum", py_binary_content_get_checksum,
           METH_NOARGS,
-          "Compute a SHA256 hash as chechsum of handled data."
+          "get_cheksum($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_u8", py_binary_content_read_u8,
           METH_VARARGS,
diff --git a/plugins/pychrysa/gui/editem.c b/plugins/pychrysa/gui/editem.c
index 6db03ce..e2289b0 100644
--- a/plugins/pychrysa/gui/editem.c
+++ b/plugins/pychrysa/gui/editem.c
@@ -94,7 +94,7 @@ static void _update_editor_item_for_binary_python_wrapper(GEditorItem *item, GLo
      * par la procédure de pygobject, qui obligerait à connaître le type précis
      * de l'instance GLib manipulée.
      */
-    target = pychrysalide_get_pygobject(G_OBJECT(item));
+    target = pygobject_new(G_OBJECT(item));
 
     args = PyTuple_New(1);
     PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(binary)));
@@ -136,7 +136,7 @@ static void _update_editor_item_for_view_python_wrapper(GEditorItem *item, GtkVi
      * par la procédure de pygobject, qui obligerait à connaître le type précis
      * de l'instance GLib manipulée.
      */
-    target = pychrysalide_get_pygobject(G_OBJECT(item));
+    target = pygobject_new(G_OBJECT(item));
 
     args = PyTuple_New(1);
     PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(view)));
@@ -178,7 +178,7 @@ static void _update_editor_item_for_view_content_python_wrapper(GEditorItem *ite
      * par la procédure de pygobject, qui obligerait à connaître le type précis
      * de l'instance GLib manipulée.
      */
-    target = pychrysalide_get_pygobject(G_OBJECT(item));
+    target = pygobject_new(G_OBJECT(item));
 
     args = PyTuple_New(1);
     PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(view)));
-- 
cgit v0.11.2-87-g4458