From 49f75f22fe67ac356f05c7f81d3a78c48461655b Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 10 Sep 2016 17:59:44 +0200
Subject: Updated the Python bindings used to load Elf and Dex files.

---
 ChangeLog                               | 10 ++++++
 plugins/pychrysa/format/dex/dex.c       | 55 ++++++++++++++++++++++++++-------
 plugins/pychrysa/format/elf/elf.c       | 51 +++++++++++++++++++++++++++---
 tests/format/elf/non_existing_binary.py |  2 +-
 tests/format/elf/oob_section_name.py    |  2 +-
 5 files changed, 103 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e693c8d..a86d9e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+16-09-10  Cyrille Bagard <nocbos@gmail.com>
+
+	* plugins/pychrysa/format/dex/dex.c:
+	* plugins/pychrysa/format/elf/elf.c:
+	Update the Python bindings used to load Elf and Dex files.
+
+	* tests/format/elf/non_existing_binary.py:
+	* tests/format/elf/oob_section_name.py:
+	Update the test suite.
+
 16-09-05  Cyrille Bagard <nocbos@gmail.com>
 
 	* src/analysis/db/collection-int.h:
diff --git a/plugins/pychrysa/format/dex/dex.c b/plugins/pychrysa/format/dex/dex.c
index 6bb1888..68f259b 100644
--- a/plugins/pychrysa/format/dex/dex.c
+++ b/plugins/pychrysa/format/dex/dex.c
@@ -28,6 +28,9 @@
 #include <pygobject.h>
 
 
+#include <i18n.h>
+
+
 #include <format/dex/class.h>
 #include <format/dex/dex.h>
 
@@ -79,22 +82,52 @@ static PyObject *py_dex_format_new(PyTypeObject *type, PyObject *args, PyObject
     if (!ret) return NULL;
 
     ret = PyObject_IsInstance(content_obj, (PyObject *)get_python_binary_content_type());
-    if (!ret) return NULL;
+    if (!ret)
+    {
+        PyErr_SetString(PyExc_TypeError, _("The first argument must be an instance of BinContent."));
+        return NULL;
+    }
 
-    ret = PyObject_IsInstance(parent_obj, (PyObject *)get_python_executable_format_type());
-    if (!ret) return NULL;
+    content = G_BIN_CONTENT(pygobject_get(content_obj));
 
-    /*
-      FIXME
-    ret = PyObject_IsInstance(status_obj, (PyObject *)get_python_binary_content_type());
-    if (!ret) return NULL;
-    */
+    if (parent_obj == Py_None)
+        parent = NULL;
 
-    content = G_BIN_CONTENT(pygobject_get(content_obj));
-    parent = G_EXE_FORMAT(pygobject_get(parent_obj));
-    status = GTK_STATUS_STACK(pygobject_get(status_obj));
+    else
+    {
+        ret = PyObject_IsInstance(parent_obj, (PyObject *)get_python_executable_format_type());
+        if (!ret)
+        {
+            PyErr_SetString(PyExc_TypeError, _("The second argument must be a container format or None."));
+            return NULL;
+        }
+
+        parent = G_EXE_FORMAT(pygobject_get(parent_obj));
+
+    }
+
+    if (status_obj == Py_None)
+        status = NULL;
+
+    else
+    {
+        ret = PyObject_IsInstance(status_obj, (PyObject *)get_python_binary_content_type());
+        if (!ret)
+        {
+            PyErr_SetString(PyExc_TypeError, _("The third argument must be a status bar object or None."));
+            return NULL;
+        }
+
+        status = GTK_STATUS_STACK(pygobject_get(status_obj));
+
+    }
 
     format = g_dex_format_new(content, parent, status);
+    if (format == NULL)
+    {
+        PyErr_SetString(PyExc_RuntimeError, _("Unable to load the DEX format."));
+        return NULL;
+    }
 
     result = pygobject_new(G_OBJECT(format));
 
diff --git a/plugins/pychrysa/format/elf/elf.c b/plugins/pychrysa/format/elf/elf.c
index 8778d56..c1dc2b9 100644
--- a/plugins/pychrysa/format/elf/elf.c
+++ b/plugins/pychrysa/format/elf/elf.c
@@ -63,22 +63,65 @@ static PyObject *py_elf_format_new(PyTypeObject *type, PyObject *args, PyObject
 {
     PyObject *result;                       /* Instance à retourner        */
     PyObject *content_obj;                  /* Objet pour le contenu       */
+    PyObject *parent_obj;                   /* Objet pour le parent        */
+    PyObject *status_obj;                   /* Objet pour la progression   */
     int ret;                                /* Bilan de lecture des args.  */
-    GBinContent *content;                   /* Instance GLib correspondante*/
+    GBinContent *content;                   /* Instance GLib du contenu    */
+    GExeFormat *parent;                     /* Instance GLib du parent     */
+    GtkStatusStack *status;                 /* Instance GTK de suivi       */
     GBinFormat *format;                     /* Création GLib à transmettre */
 
-    ret = PyArg_ParseTuple(args, "O", &content_obj);
+
+    ret = PyArg_ParseTuple(args, "OOO", &content_obj, &parent_obj, &status_obj);
     if (!ret) return NULL;
 
     ret = PyObject_IsInstance(content_obj, (PyObject *)get_python_binary_content_type());
     if (!ret)
     {
-        PyErr_SetString(PyExc_TypeError, _("The argument must be an instance of BinContent."));
+        PyErr_SetString(PyExc_TypeError, _("The first argument must be an instance of BinContent."));
         return NULL;
     }
 
     content = G_BIN_CONTENT(pygobject_get(content_obj));
-    format = g_elf_format_new(content, NULL, NULL/*FIXME*/);
+
+    if (parent_obj == Py_None)
+        parent = NULL;
+
+    else
+    {
+        ret = PyObject_IsInstance(parent_obj, (PyObject *)get_python_executable_format_type());
+        if (!ret)
+        {
+            PyErr_SetString(PyExc_TypeError, _("The second argument must be a container format or None."));
+            return NULL;
+        }
+
+        parent = G_EXE_FORMAT(pygobject_get(parent_obj));
+
+    }
+
+    if (status_obj == Py_None)
+        status = NULL;
+
+    else
+    {
+        ret = PyObject_IsInstance(status_obj, (PyObject *)get_python_binary_content_type());
+        if (!ret)
+        {
+            PyErr_SetString(PyExc_TypeError, _("The third argument must be a status bar object or None."));
+            return NULL;
+        }
+
+        status = GTK_STATUS_STACK(pygobject_get(status_obj));
+
+    }
+
+    format = g_elf_format_new(content, parent, status);
+    if (format == NULL)
+    {
+        PyErr_SetString(PyExc_RuntimeError, _("Unable to load the ELF format."));
+        return NULL;
+    }
 
     result = pygobject_new(G_OBJECT(format));
 
diff --git a/tests/format/elf/non_existing_binary.py b/tests/format/elf/non_existing_binary.py
index 6111f03..a6eddd3 100644
--- a/tests/format/elf/non_existing_binary.py
+++ b/tests/format/elf/non_existing_binary.py
@@ -21,4 +21,4 @@ class TestNonExistingBinary(ChrysalideTestCase):
 
         with self.assertRaisesRegex(TypeError, 'The argument must be an instance of BinContent.'):
 
-            fmt = ElfFormat(cnt)
+            fmt = ElfFormat(cnt, None, None)
diff --git a/tests/format/elf/oob_section_name.py b/tests/format/elf/oob_section_name.py
index 8f91efd..a478ec7 100644
--- a/tests/format/elf/oob_section_name.py
+++ b/tests/format/elf/oob_section_name.py
@@ -44,5 +44,5 @@ class TestNonExistingBinary(ChrysalideTestCase):
         cnt = FileContent(fullname[:baselen] + 'oob_section_name')
         self.assertIsNotNone(cnt)
 
-        fmt = ElfFormat(cnt)
+        fmt = ElfFormat(cnt, None, None)
         self.assertIsInstance(fmt, ElfFormat)
-- 
cgit v0.11.2-87-g4458