From 8cfbcf2db62a65766e02618840c6296ba7d083d0 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 15 Mar 2020 21:04:26 +0100
Subject: Improved the Python API for ELF dynamic items.

---
 plugins/elf/python/dynamic.c | 16 ++++------------
 plugins/elf/python/dynamic.h | 41 +++++++++++++++++++++++++++++++++++++++++
 plugins/elf/python/format.c  | 17 +++--------------
 3 files changed, 48 insertions(+), 26 deletions(-)

diff --git a/plugins/elf/python/dynamic.c b/plugins/elf/python/dynamic.c
index aaf4f6a..974b751 100644
--- a/plugins/elf/python/dynamic.c
+++ b/plugins/elf/python/dynamic.c
@@ -145,22 +145,14 @@ PyObject *py_elf_format_get_needed(PyObject *self, void *closure)
 
     needed = list_elf_needed_objects(format, &count);
 
-    if (count > 0)
-    {
-        result = PyTuple_New(count);
+    result = PyTuple_New(count);
 
-        for (i = 0; i < count; i++)
-            PyTuple_SetItem(result, i, PyUnicode_FromString(needed[i]));
+    for (i = 0; i < count; i++)
+        PyTuple_SetItem(result, i, PyUnicode_FromString(needed[i]));
 
+    if (needed != NULL)
         free(needed);
 
-    }
-    else
-    {
-        result = Py_None;
-        Py_INCREF(result);
-    }
-
     return result;
 
 }
diff --git a/plugins/elf/python/dynamic.h b/plugins/elf/python/dynamic.h
index ca76c65..691eb83 100644
--- a/plugins/elf/python/dynamic.h
+++ b/plugins/elf/python/dynamic.h
@@ -33,12 +33,53 @@
 /* Retrouve un élément dans la section dynamique par son indice. */
 PyObject *py_elf_format_find_dynamic_item_by_index(PyObject *, PyObject *);
 
+#define ELF_FORMAT_FIND_DYNAMIC_ITEM_BY_INDEX_METHOD PYTHON_METHOD_DEF      \
+(                                                                           \
+    find_dynamic_item_by_index, "$self, index, /",                          \
+    METH_VARARGS, py_elf_format,                                            \
+    "Find an item from the dynamic item using a given index."               \
+    "\n"                                                                    \
+    "The result is an instance of type pychrysalide.PyStructObject"         \
+    " on success, None otherwise."                                          \
+    "\n"                                                                    \
+    "The provided information about a found dynamic item is composed of"    \
+    " the following properties :\n"                                         \
+    "* d_tag;\n"                                                            \
+    "* d_un.d_val;\n"                                                       \
+    "* d_un.d_ptr."                                                         \
+)
+
 /* Retrouve un élément dans la section dynamique par son type. */
 PyObject *py_elf_format_find_dynamic_item_by_type(PyObject *, PyObject *);
 
+#define ELF_FORMAT_FIND_DYNAMIC_ITEM_BY_TYPE_METHOD PYTHON_METHOD_DEF       \
+(                                                                           \
+    find_dynamic_item_by_type, "$self, type, /",                            \
+    METH_VARARGS, py_elf_format,                                            \
+    "Find an item from the dynamic item using a given type."                \
+    "\n"                                                                    \
+    "The result is an instance of type pychrysalide.PyStructObject"         \
+    " on success, None otherwise."                                          \
+    "\n"                                                                    \
+    "The provided information about a found dynamic item is composed of"    \
+    " the following properties :\n"                                         \
+    "* d_tag;\n"                                                            \
+    "* d_un.d_val;\n"                                                       \
+    "* d_un.d_ptr."                                                         \
+)
+
 /* Fournit la liste des objets partagés requis. */
 PyObject *py_elf_format_get_needed(PyObject *, void *);
 
+#define ELF_FORMAT_NEEDED_ATTRIB PYTHON_GET_DEF_FULL            \
+(                                                               \
+    needed, py_elf_format,                                      \
+    "Provide the list of requiered shared objects."             \
+    "\n"                                                        \
+    "The result is a tuple of strings or an empty tuple if"     \
+    " no external library is required by the binary."           \
+)
+
 
 
 #endif  /* _PLUGINS_ELF_PYTHON_DYNAMIC_H */
diff --git a/plugins/elf/python/format.c b/plugins/elf/python/format.c
index 87ec5e6..e0195c4 100644
--- a/plugins/elf/python/format.c
+++ b/plugins/elf/python/format.c
@@ -174,16 +174,8 @@ PyTypeObject *get_python_elf_format_type(void)
             METH_VARARGS,
             "find_sections_by_type($self, type, /)\n--\n\nFind sections using a given type."
         },
-        {
-            "find_dynamic_item_by_index", py_elf_format_find_dynamic_item_by_index,
-            METH_VARARGS,
-            "find_dynamic_item_by_index($self, index, /)\n--\n\nFind an item from the dynamic segment using a given index."
-        },
-        {
-            "find_dynamic_item_by_type", py_elf_format_find_dynamic_item_by_type,
-            METH_VARARGS,
-            "find_dynamic_item_by_type($self, type, /)\n--\n\nFind an item from the dynamic segment using a given type."
-        },
+        ELF_FORMAT_FIND_DYNAMIC_ITEM_BY_INDEX_METHOD,
+        ELF_FORMAT_FIND_DYNAMIC_ITEM_BY_TYPE_METHOD,
         { NULL }
     };
 
@@ -212,10 +204,7 @@ PyTypeObject *get_python_elf_format_type(void)
             "sizeof_rel", py_elf_format_get_sizeof_rel, NULL,
             "Provide the size of Elf_Rel structures for the loaded format.", NULL
         },
-        {
-            "needed", py_elf_format_get_needed, NULL,
-            "Provide the list of requiered shared objects.", NULL
-        },
+        ELF_FORMAT_NEEDED_ATTRIB,
         { NULL }
     };
 
-- 
cgit v0.11.2-87-g4458