From 7e397e656e983931085ee0d656945b2d8ca3ce5b Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 24 Jun 2020 00:01:00 +0200
Subject: Improved support for Android boot image Python classes.

---
 plugins/bootimg/python/format.c | 93 +++++++++++++++++++++++++++++++++++------
 1 file changed, 81 insertions(+), 12 deletions(-)

diff --git a/plugins/bootimg/python/format.c b/plugins/bootimg/python/format.c
index 2f66046..d5e32d3 100644
--- a/plugins/bootimg/python/format.c
+++ b/plugins/bootimg/python/format.c
@@ -29,6 +29,8 @@
 
 
 #include <i18n.h>
+#include <format/known.h>
+#include <plugins/dt.h>
 #include <plugins/pychrysalide/helpers.h>
 #include <plugins/pychrysalide/analysis/content.h>
 #include <plugins/pychrysalide/format/known.h>
@@ -42,6 +44,9 @@
 /* Crée un nouvel objet Python de type 'BootImgFormat'. */
 static PyObject *py_bootimg_format_new(PyTypeObject *, PyObject *, PyObject *);
 
+/* Initialise une instance sur la base du dérivé de GObject. */
+static int py_bootimg_format_init(PyObject *, PyObject *, PyObject *);
+
 /* Fournit l'en-tête Bootimg correspondant au format. */
 static PyObject *py_bootimg_format_get_header(PyObject *, void *);
 
@@ -69,7 +74,66 @@ static PyObject *py_bootimg_format_get_ramdisk(PyObject *, void *);
 
 static PyObject *py_bootimg_format_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    PyObject *result;                       /* Instance à retourner        */
+    PyObject *result;                       /* Objet à retourner           */
+    PyTypeObject *base;                     /* Type de base à dériver      */
+    bool first_time;                        /* Evite les multiples passages*/
+    GType gtype;                            /* Nouveau type de processeur  */
+    bool status;                            /* Bilan d'un enregistrement   */
+
+    /* Validations diverses */
+
+    base = get_python_bootimg_format_type();
+
+    if (type == base)
+        goto simple_way;
+
+    /* Mise en place d'un type dédié */
+
+    first_time = (g_type_from_name(type->tp_name) == 0);
+
+    gtype = build_dynamic_type(G_TYPE_BOOTIMG_FORMAT, type->tp_name, NULL, NULL, NULL);
+
+    if (first_time)
+    {
+        status = register_class_for_dynamic_pygobject(gtype, type, base);
+
+        if (!status)
+        {
+            result = NULL;
+            goto exit;
+        }
+
+    }
+
+    /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */
+
+ simple_way:
+
+    result = PyType_GenericNew(type, args, kwds);
+
+ exit:
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : self = objet à initialiser (théoriquement).                  *
+*                args = arguments fournis à l'appel.                          *
+*                kwds = arguments de type key=val fournis.                    *
+*                                                                             *
+*  Description : Initialise une instance sur la base du dérivé de GObject.    *
+*                                                                             *
+*  Retour      : 0.                                                           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static int py_bootimg_format_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
     GBinContent *content;                   /* Instance GLib du contenu    */
     int ret;                                /* Bilan de lecture des args.  */
     GBootImgFormat *format;                 /* Création GLib à transmettre */
@@ -85,23 +149,26 @@ static PyObject *py_bootimg_format_new(PyTypeObject *type, PyObject *args, PyObj
     " provided as a pychrysalide.analysis.BinContent instance."
 
     ret = PyArg_ParseTuple(args, "O&", convert_to_binary_content, &content);
-    if (!ret) return NULL;
+    if (!ret) return -1;
 
-    format = g_bootimg_format_new(content);
+    /* Initialisation d'un objet GLib */
 
-    if (format == NULL)
-    {
-        result = Py_None;
-        Py_INCREF(result);
-    }
+    ret = forward_pygobjet_init(self);
+    if (ret == -1) return -1;
 
-    else
+    /* Eléments de base */
+
+    if (!check_bootimg_format(content))
     {
-        result = pygobject_new(G_OBJECT(format));
-        g_object_unref(format);
+        PyErr_SetString(PyExc_TypeError, "bad format for an Android boot image");
+        return -1;
     }
 
-    return result;
+    format = G_BOOTIMG_FORMAT(pygobject_get(self));
+
+    g_known_format_set_content(G_KNOWN_FORMAT(format), content);
+
+    return 0;
 
 }
 
@@ -296,6 +363,8 @@ PyTypeObject *get_python_bootimg_format_type(void)
 
         .tp_methods     = py_bootimg_format_methods,
         .tp_getset      = py_bootimg_format_getseters,
+
+        .tp_init        = py_bootimg_format_init,
         .tp_new         = py_bootimg_format_new
 
     };
-- 
cgit v0.11.2-87-g4458