diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2019-06-16 20:20:46 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2019-11-01 10:16:01 (GMT) |
commit | 33b5dc9af0404eabeb0e60245ab1ca1dc3713a17 (patch) | |
tree | fda7b9996c7e90b96507988d54cc73161b309621 /plugins/yaml/python | |
parent | a8598ae89acd2963143b7a6b248fbecbc0e16025 (diff) |
Added support for Yaml content.
Diffstat (limited to 'plugins/yaml/python')
-rw-r--r-- | plugins/yaml/python/Makefile.am | 22 | ||||
-rw-r--r-- | plugins/yaml/python/line.c | 430 | ||||
-rw-r--r-- | plugins/yaml/python/line.h | 45 | ||||
-rw-r--r-- | plugins/yaml/python/module.c | 120 | ||||
-rw-r--r-- | plugins/yaml/python/module.h | 41 | ||||
-rw-r--r-- | plugins/yaml/python/node.c | 363 | ||||
-rw-r--r-- | plugins/yaml/python/node.h | 45 | ||||
-rw-r--r-- | plugins/yaml/python/reader.c | 375 | ||||
-rw-r--r-- | plugins/yaml/python/reader.h | 45 | ||||
-rw-r--r-- | plugins/yaml/python/tree.c | 366 | ||||
-rw-r--r-- | plugins/yaml/python/tree.h | 45 |
11 files changed, 1897 insertions, 0 deletions
diff --git a/plugins/yaml/python/Makefile.am b/plugins/yaml/python/Makefile.am new file mode 100644 index 0000000..8a8d960 --- /dev/null +++ b/plugins/yaml/python/Makefile.am @@ -0,0 +1,22 @@ + +noinst_LTLIBRARIES = libyamlpython.la + +libyamlpython_la_SOURCES = \ + line.h line.c \ + module.h module.c \ + node.h node.c \ + reader.h reader.c \ + tree.h tree.c + +libyamlpython_la_LDFLAGS = + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libyamlpython_la_SOURCES:%c=) + + +AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ + -I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) diff --git a/plugins/yaml/python/line.c b/plugins/yaml/python/line.c new file mode 100644 index 0000000..11898d2 --- /dev/null +++ b/plugins/yaml/python/line.c @@ -0,0 +1,430 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * line.c - équivalent Python du fichier "plugins/yaml/line.c" + * + * Copyright (C) 2019 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 "line.h" + + +#include <pygobject.h> + + +#include <plugins/pychrysalide/helpers.h> + + +#include "../line.h" + + + +/* Crée un nouvel objet Python de type 'YamlLine'. */ +static PyObject *py_yaml_line_new(PyTypeObject *, PyObject *, PyObject *); + +/* Fournit la taille de l'indentation d'une ligne Yaml. */ +static PyObject *py_yaml_line_get_indent(PyObject *, void *); + +/* Indique si la ligne représente un élément de liste. */ +static PyObject *py_yaml_line_is_list_item(PyObject *, void *); + +/* Fournit la charge utile associée à une ligne Yaml. */ +static PyObject *py_yaml_line_get_payload(PyObject *, void *); + +/* Fournit la clef associée à une ligne Yaml si elle existe. */ +static PyObject *py_yaml_line_get_key(PyObject *, void *); + +/* Fournit la valeur associée à une ligne Yaml si elle existe. */ +static PyObject *py_yaml_line_get_value(PyObject *, void *); + + + +/****************************************************************************** +* * +* 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 'YamlLine'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Instance à retourner */ + const char *raw; /* Données Yaml brutes */ + Py_ssize_t index; /* Indice de ligne associée */ + int ret; /* Bilan de lecture des args. */ + GYamlLine *line; /* Création GLib à transmettre */ + +#define YAML_LINE_DOC \ + "YamlLine handles a line of Yaml data.\n" \ + "\n" \ + "The data may be a couple of key/value, a comment, aso.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " YamlTree(raw, number)" \ + "\n" \ + "Where raw is a string providing raw data and number the index" \ + " of the line in the overall stream." + + ret = PyArg_ParseTuple(args, "sn", &raw, &index); + if (!ret) return NULL; + + line = g_yaml_line_new(raw, index); + + if (line == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + g_object_ref_sink(G_OBJECT(line)); + result = pygobject_new(G_OBJECT(line)); + g_object_unref(line); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la taille de l'indentation d'une ligne Yaml. * +* * +* Retour : Taille de l'indentation rencontrée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_get_indent(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlLine *line; /* Version GLib du type */ + size_t indent; /* Taille de l'indentation */ + +#define YAML_LINE_INDENT_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + indent, py_yaml_line, \ + "Quantity of characters used for the indentation." \ +) + + line = G_YAML_LINE(pygobject_get(self)); + + indent = g_yaml_line_count_indent(line); + + result = PyLong_FromSize_t(indent); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Indique si la ligne représente un élément de liste. * +* * +* Retour : Statut de l'état lié à une liste d'éléments. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_is_list_item(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlLine *line; /* Version GLib du type */ + bool status; /* Statut de la ligne */ + +#define YAML_LINE_IS_LIST_ITEM_ATTRIB PYTHON_IS_DEF_FULL \ +( \ + list_item, py_yaml_line, \ + "Tell if the line starts a new list item." \ +) + + line = G_YAML_LINE(pygobject_get(self)); + + status = g_yaml_line_is_list_item(line); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la charge utile associée à une ligne Yaml. * +* * +* Retour : Contenu sous forme de chaîne de caractères. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_get_payload(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlLine *line; /* Version GLib du type */ + const char *payload; /* Chaîne à transmettre */ + +#define YAML_LINE_PAYLOAD_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + payload, py_yaml_line, \ + "Payload of the Yaml line." \ +) + + line = G_YAML_LINE(pygobject_get(self)); + + payload = g_yaml_line_get_payload(line); + + result = PyUnicode_FromString(payload); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la clef associée à une ligne Yaml si elle existe. * +* * +* Retour : Clef sous forme de chaîne de caractères ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_get_key(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlLine *line; /* Version GLib du type */ + const char *key; /* Chaîne à transmettre */ + +#define YAML_LINE_KEY_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + key, py_yaml_line, \ + "Key linked to the Yaml line or None." \ +) + + line = G_YAML_LINE(pygobject_get(self)); + + key = g_yaml_line_get_key(line); + + if (key == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + result = PyUnicode_FromString(key); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la valeur associée à une ligne Yaml si elle existe. * +* * +* Retour : Valeur sous forme de chaîne de caractères ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_line_get_value(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlLine *line; /* Version GLib du type */ + const char *value; /* Chaîne à transmettre */ + +#define YAML_LINE_VALUE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + value, py_yaml_line, \ + "Value linked to the Yaml line or None." \ +) + + line = G_YAML_LINE(pygobject_get(self)); + + value = g_yaml_line_get_value(line); + + if (value == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + result = PyUnicode_FromString(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_yaml_line_type(void) +{ + static PyMethodDef py_yaml_line_methods[] = { + { NULL } + }; + + static PyGetSetDef py_yaml_line_getseters[] = { + YAML_LINE_INDENT_ATTRIB, + YAML_LINE_IS_LIST_ITEM_ATTRIB, + YAML_LINE_PAYLOAD_ATTRIB, + YAML_LINE_KEY_ATTRIB, + YAML_LINE_VALUE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_yaml_line_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.yaml.YamlLine", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = YAML_LINE_DOC, + + .tp_methods = py_yaml_line_methods, + .tp_getset = py_yaml_line_getseters, + .tp_new = py_yaml_line_new + + }; + + return &py_yaml_line_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.plugins.....YamlLine. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_yaml_line(PyObject *module) +{ + PyTypeObject *type; /* Type Python 'YamlLine' */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_yaml_line_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_YAML_LINE, type, &PyGObject_Type)) + return false; + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en ligne de données au format Yaml. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_yaml_line(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_yaml_line_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml line"); + break; + + case 1: + *((GYamlLine **)dst) = G_YAML_LINE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/yaml/python/line.h b/plugins/yaml/python/line.h new file mode 100644 index 0000000..00dcbd9 --- /dev/null +++ b/plugins/yaml/python/line.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * line.h - prototypes pour l'équivalent Python du fichier "plugins/yaml/line.h" + * + * Copyright (C) 2019 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_YAML_PYTHON_LINE_H +#define _PLUGINS_YAML_PYTHON_LINE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_yaml_line_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.yaml.YamlLine'. */ +bool register_python_yaml_line(PyObject *); + +/* Tente de convertir en ligne de données au format Yaml. */ +int convert_to_yaml_line(PyObject *, void *); + + + +#endif /* _PLUGINS_YAML_PYTHON_LINE_H */ diff --git a/plugins/yaml/python/module.c b/plugins/yaml/python/module.c new file mode 100644 index 0000000..542a551 --- /dev/null +++ b/plugins/yaml/python/module.c @@ -0,0 +1,120 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire yaml en tant que module + * + * Copyright (C) 2019 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 <assert.h> +#include <Python.h> + + +#include <plugins/pychrysalide/access.h> +#include <plugins/pychrysalide/helpers.h> + + +#include "line.h" +#include "node.h" +#include "reader.h" +#include "tree.h" + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Ajoute le module 'plugins.yaml' au module Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_yaml_module_to_python_module(void) +{ + bool result; /* Bilan à retourner */ + PyObject *super; /* Module à compléter */ + PyObject *module; /* Sous-module mis en place */ + +#define PYCHRYSALIDE_PLUGINS_YAML_DOC \ + "yaml is a module providing access to Yaml content." + + static PyModuleDef py_chrysalide_yaml_module = { + + .m_base = PyModuleDef_HEAD_INIT, + + .m_name = "pychrysalide.plugins.yaml", + .m_doc = PYCHRYSALIDE_PLUGINS_YAML_DOC, + + .m_size = -1, + + }; + + result = false; + + super = get_access_to_python_module("pychrysalide.plugins"); + + module = build_python_module(super, &py_chrysalide_yaml_module); + + result = (module != NULL); + + assert(result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Intègre les objets du module 'plugins.yaml'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_yaml_module(void) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Module à recompléter */ + + result = true; + + module = get_access_to_python_module("pychrysalide.plugins.yaml"); + + if (result) result = register_python_yaml_line(module); + if (result) result = register_python_yaml_node(module); + if (result) result = register_python_yaml_reader(module); + if (result) result = register_python_yaml_tree(module); + + assert(result); + + return result; + +} diff --git a/plugins/yaml/python/module.h b/plugins/yaml/python/module.h new file mode 100644 index 0000000..c4782f7 --- /dev/null +++ b/plugins/yaml/python/module.h @@ -0,0 +1,41 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire yaml en tant que module + * + * Copyright (C) 2019 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_YAML_PYTHON_MODULE_H +#define _PLUGINS_YAML_PYTHON_MODULE_H + + +#include <stdbool.h> + + + +/* Ajoute le module 'plugins.yaml' au module Python. */ +bool add_yaml_module_to_python_module(void); + +/* Intègre les objets du module 'plugins.yaml'. */ +bool populate_yaml_module(void); + + + +#endif /* _PLUGINS_YAML_PYTHON_MODULE_H */ diff --git a/plugins/yaml/python/node.c b/plugins/yaml/python/node.c new file mode 100644 index 0000000..d1aabee --- /dev/null +++ b/plugins/yaml/python/node.c @@ -0,0 +1,363 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * node.c - équivalent Python du fichier "plugins/yaml/node.c" + * + * Copyright (C) 2019 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 "node.h" + + +#include <pygobject.h> + + +#include <plugins/pychrysalide/helpers.h> + + +#include "line.h" +#include "../node.h" + + + +/* Crée un nouvel objet Python de type 'YamlNode'. */ +static PyObject *py_yaml_node_new(PyTypeObject *, PyObject *, PyObject *); + +/* Recherche le noeud correspondant à un chemin. */ +static PyObject *py_yaml_node_find_node_by_path(PyObject *, PyObject *); + +/* Fournit la ligne principale associée à un noeud. */ +static PyObject *py_yaml_node_get_yaml_line(PyObject *, void *); + +/* Fournit la liste des noeuds intégrés dans un noeud Yaml. */ +static PyObject *py_yaml_node_get_children(PyObject *, void *); + + + +/****************************************************************************** +* * +* 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 'YamlNode'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_node_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Instance à retourner */ + GYamlLine *key; /* Ligne principale du noeud */ + int ret; /* Bilan de lecture des args. */ + GYamlNode *node; /* Création GLib à transmettre */ + +#define YAML_NODE_DOC \ + "YamlNode handle a node in a Yaml tree.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " YamlNode(key)\n" \ + "\n" \ + "Where key is the main Yaml line for the node." + + ret = PyArg_ParseTuple(args, "O&", &convert_to_yaml_line, &key); + if (!ret) return NULL; + + node = g_yaml_node_new(key); + + g_object_ref_sink(G_OBJECT(node)); + result = pygobject_new(G_OBJECT(node)); + g_object_unref(node); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = variable non utilisée ici. * +* args = arguments fournis à l'appel. * +* * +* Description : Recherche le noeud correspondant à un chemin. * +* * +* Retour : Eventuel noeud trouvé ou NULL si aucun. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_node_find_node_by_path(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *path; /* Chemin d'accès à traiter */ + int ret; /* Bilan de lecture des args. */ + GYamlNode *node; /* Version GLib du type */ + GYamlNode *found; /* Création GLib à transmettre */ + +#define YAML_NODE_FIND_BY_PATH PYTHON_METHOD_DEF \ +( \ + find_node_by_path, "path", \ + METH_VARARGS, py_yaml_node, \ + "Find a child node in a Yaml node by its path.\n" \ + "\n" \ + "Paths are node keys separated by '/', such as '/my/path/to/node'." \ +) + + ret = PyArg_ParseTuple(args, "s", &path); + if (!ret) return NULL; + + node = G_YAML_NODE(pygobject_get(self)); + + found = g_yaml_node_find_node_by_path(node, path); + + if (found == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + result = pygobject_new(G_OBJECT(found)); + g_object_unref(found); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la ligne principale associée à un noeud. * +* * +* Retour : Ligne Yaml à l'origine du noeud. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_node_get_yaml_line(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlNode *node; /* Version GLib du type */ + GYamlLine *line; /* Line Yaml associée */ + +#define YAML_NODE_YAML_LINE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + yaml_line, py_yaml_node, \ + "Orginal Yaml line linked to the node." \ +) + + node = G_YAML_NODE(pygobject_get(self)); + + line = g_yaml_node_get_yaml_line(node); + + result = pygobject_new(G_OBJECT(line)); + + g_object_unref(G_OBJECT(line)); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la liste des noeuds intégrés dans un noeud Yaml. * +* * +* Retour : Enfants d'un noeud issu d'une arborescence Yaml. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_node_get_children(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlNode *node; /* Version GLib du type */ + size_t count; /* Quantité de noeuds à traiter*/ + GYamlNode **nodes; /* Liste des noeuds à la racine*/ + size_t i; /* Boucle de parcours */ +#ifndef NDEBUG + int ret; /* Bilan d'une insertion */ +#endif + +#define YAML_NODE_CHILDREN_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + children, py_yaml_node, \ + "List of nodes which are children of the current node." \ +) + + node = G_YAML_NODE(pygobject_get(self)); + + nodes = g_yaml_node_get_children(node, &count); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { +#ifndef NDEBUG + ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); + assert(ret == 0); +#else + PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); +#endif + + g_object_unref(G_OBJECT(nodes[i])); + + } + + free(nodes); + + 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_yaml_node_type(void) +{ + static PyMethodDef py_yaml_node_methods[] = { + YAML_NODE_FIND_BY_PATH, + { NULL } + }; + + static PyGetSetDef py_yaml_node_getseters[] = { + YAML_NODE_YAML_LINE_ATTRIB, + YAML_NODE_CHILDREN_ATTRIB, + { NULL } + }; + + static PyTypeObject py_yaml_node_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.yaml.YamlNode", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = YAML_NODE_DOC, + + .tp_methods = py_yaml_node_methods, + .tp_getset = py_yaml_node_getseters, + .tp_new = py_yaml_node_new + + }; + + return &py_yaml_node_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.plugins.....YamlNode. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_yaml_node(PyObject *module) +{ + PyTypeObject *type; /* Type Python 'YamlNode' */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_yaml_node_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_YAML_NODE, type, &PyGObject_Type)) + return false; + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en noeud d'arborescence de format Yaml. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_yaml_node(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_yaml_node_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml node"); + break; + + case 1: + *((GYamlNode **)dst) = G_YAML_NODE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/yaml/python/node.h b/plugins/yaml/python/node.h new file mode 100644 index 0000000..dc3686b --- /dev/null +++ b/plugins/yaml/python/node.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * node.h - prototypes pour l'équivalent Python du fichier "plugins/yaml/node.h" + * + * Copyright (C) 2019 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_YAML_PYTHON_NODE_H +#define _PLUGINS_YAML_PYTHON_NODE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_yaml_node_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.yaml.YamlNode'. */ +bool register_python_yaml_node(PyObject *); + +/* Tente de convertir en noeud d'arborescence de format Yaml. */ +int convert_to_yaml_node(PyObject *, void *); + + + +#endif /* _PLUGINS_YAML_PYTHON_NODE_H */ diff --git a/plugins/yaml/python/reader.c b/plugins/yaml/python/reader.c new file mode 100644 index 0000000..43db6bb --- /dev/null +++ b/plugins/yaml/python/reader.c @@ -0,0 +1,375 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * reader.c - équivalent Python du fichier "plugins/yaml/reader.c" + * + * Copyright (C) 2019 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 "reader.h" + + +#include <pygobject.h> + + +#include <plugins/pychrysalide/helpers.h> + + +#include "../reader.h" + + + +#define YAML_READER_DOC \ + "YamlReader is the class which aims to provide a reader interface to Yaml content.\n" \ + "\n" \ + "When no input error, the Yaml content can be retrieved line by line or thanks to a tree." + + + +/* Crée un lecteur pour contenu au format Yaml. */ +static PyObject *py_yaml_reader_new_from_content(PyObject *, PyObject *); + +/* Crée un lecteur pour contenu au format Yaml. */ +static PyObject *py_yaml_reader_new_from_path(PyObject *, PyObject *); + +/* Fournit la liste des lignes lues depuis un contenu Yaml. */ +static PyObject *py_yaml_reader_get_lines(PyObject *, void *); + +/* Fournit l'arborescence associée à la lecture de lignes Yaml. */ +static PyObject *py_yaml_reader_get_tree(PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : self = variable non utilisée ici. * +* args = arguments fournis à l'appel. * +* * +* Description : Crée un lecteur pour contenu au format Yaml. * +* * +* Retour : Instance mise en place ou None en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_reader_new_from_content(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *content; /* Contenu brut au format Yaml */ + int length; /* Taille de ce contenu */ + int ret; /* Bilan de lecture des args. */ + GYamlReader *reader; /* Création GLib à transmettre */ + +#define YAML_READER_NEW_FROM_CONTENT PYTHON_METHOD_DEF \ +( \ + new_from_content, "content", \ + METH_STATIC | METH_VARARGS, py_yaml_reader, \ + "Load a Yaml content." \ +) + + ret = PyArg_ParseTuple(args, "s#", &content, &length); + if (!ret) return NULL; + + reader = g_yaml_reader_new_from_content(content, length); + + if (reader == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + g_object_ref_sink(G_OBJECT(reader)); + result = pygobject_new(G_OBJECT(reader)); + g_object_unref(reader); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = variable non utilisée ici. * +* args = arguments fournis à l'appel. * +* * +* Description : Crée un lecteur pour contenu au format Yaml. * +* * +* Retour : Instance mise en place ou None en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_reader_new_from_path(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *path; /* Chemin d'accès à un contenu */ + int ret; /* Bilan de lecture des args. */ + GYamlReader *reader; /* Création GLib à transmettre */ + +#define YAML_READER_NEW_FROM_PATH PYTHON_METHOD_DEF \ +( \ + new_from_path, "path", \ + METH_STATIC | METH_VARARGS, py_yaml_reader, \ + "Load a Yaml content from a path.\n" \ + "\n" \ + "The path can be a filename or a resource URI." \ +) + + ret = PyArg_ParseTuple(args, "s", &path); + if (!ret) return NULL; + + reader = g_yaml_reader_new_from_path(path); + + if (reader == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + g_object_ref_sink(G_OBJECT(reader)); + result = pygobject_new(G_OBJECT(reader)); + g_object_unref(reader); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la liste des lignes lues depuis un contenu Yaml. * +* * +* Retour : Liste de lignes correspondant au contenu Yaml lu. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_reader_get_lines(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlReader *reader; /* Version GLib du type */ + size_t count; /* Quantité de lignes à traiter*/ + GYamlLine **lines; /* Liste de lignes lues */ + size_t i; /* Boucle de parcours */ +#ifndef NDEBUG + int ret; /* Bilan d'une insertion */ +#endif + +#define YAML_READER_LINES_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + lines, py_yaml_reader, \ + "List of Yaml lines processed by the reader." \ +) + + reader = G_YAML_READER(pygobject_get(self)); + + lines = g_yaml_reader_get_lines(reader, &count); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { +#ifndef NDEBUG + ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(lines[i]))); + assert(ret == 0); +#else + PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(lines[i]))); +#endif + + g_object_unref(G_OBJECT(lines[i])); + + } + + free(lines); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit l'arborescence associée à la lecture de lignes Yaml. * +* * +* Retour : Arborescence constituée par la lecture du contenu Yaml. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_reader_get_tree(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlReader *reader; /* Version GLib du type */ + GYamlTree *tree; /* Arborescence associée */ + +#define YAML_READER_TREE_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + tree, py_yaml_reader, \ + "Tree of all nodes built from the Yaml content." \ +) + + reader = G_YAML_READER(pygobject_get(self)); + + tree = g_yaml_reader_get_tree(reader); + + result = pygobject_new(G_OBJECT(tree)); + + g_object_unref(G_OBJECT(tree)); + + 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_yaml_reader_type(void) +{ + static PyMethodDef py_yaml_reader_methods[] = { + YAML_READER_NEW_FROM_CONTENT, + YAML_READER_NEW_FROM_PATH, + { NULL } + }; + + static PyGetSetDef py_yaml_reader_getseters[] = { + YAML_READER_LINES_ATTRIB, + YAML_READER_TREE_ATTRIB, + { NULL } + }; + + static PyTypeObject py_yaml_reader_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.yaml.YamlReader", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = YAML_READER_DOC, + + .tp_methods = py_yaml_reader_methods, + .tp_getset = py_yaml_reader_getseters, + .tp_new = no_python_constructor_allowed + + }; + + return &py_yaml_reader_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.plugins.....YamlReader.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_yaml_reader(PyObject *module) +{ + PyTypeObject *type; /* Type Python 'YamlReader' */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_yaml_reader_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_YAML_READER, type, &PyGObject_Type)) + return false; + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en lecteur de données au format Yaml. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_yaml_reader(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_yaml_reader_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml reader"); + break; + + case 1: + *((GYamlReader **)dst) = G_YAML_READER(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/yaml/python/reader.h b/plugins/yaml/python/reader.h new file mode 100644 index 0000000..19d238b --- /dev/null +++ b/plugins/yaml/python/reader.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * reader.h - prototypes pour l'équivalent Python du fichier "plugins/yaml/reader.h" + * + * Copyright (C) 2019 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_YAML_PYTHON_READER_H +#define _PLUGINS_YAML_PYTHON_READER_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_yaml_reader_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.yaml.YamlReader'. */ +bool register_python_yaml_reader(PyObject *); + +/* Tente de convertir en lecteur de données au format Yaml. */ +int convert_to_yaml_reader(PyObject *, void *); + + + +#endif /* _PLUGINS_YAML_PYTHON_READER_H */ diff --git a/plugins/yaml/python/tree.c b/plugins/yaml/python/tree.c new file mode 100644 index 0000000..9e1a2a3 --- /dev/null +++ b/plugins/yaml/python/tree.c @@ -0,0 +1,366 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tree.c - équivalent Python du fichier "plugins/yaml/tree.c" + * + * Copyright (C) 2019 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 "tree.h" + + +#include <pygobject.h> + + +#include <i18n.h> +#include <plugins/pychrysalide/helpers.h> + + +#include "line.h" +#include "../tree.h" + + + +/* Crée un nouvel objet Python de type 'YamlTree'. */ +static PyObject *py_yaml_tree_new(PyTypeObject *, PyObject *, PyObject *); + +/* Recherche le noeud correspondant à un chemin. */ +static PyObject *py_yaml_tree_find_node_by_path(PyObject *, PyObject *); + +/* Fournit la liste des premiers noeuds de l'arborescence Yaml. */ +static PyObject *py_yaml_tree_get_root_nodes(PyObject *, void *); + + + +/****************************************************************************** +* * +* 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 'YamlTree'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_tree_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Instance à retourner */ + Py_ssize_t indent; /* Indice de ligne associée */ + PyObject *tuple; /* Liste de lignes Yaml */ + int ret; /* Bilan de lecture des args. */ + size_t count; /* Nombre d'éléments présents */ + GYamlLine **lines; /* Lignes au format Yaml */ + size_t i; /* Boucle de parcours #1 */ + PyObject *item; /* Elément de la liste fournie */ + size_t k; /* Boucle de parcours #2 */ + GYamlTree *tree; /* Création GLib à transmettre */ + +#define YAML_TREE_DOC \ + "YamlTree offers a hierarchical access to Yaml lines as a tree.\n" \ + "\n" \ + "Instances can be created using the following constructor:\n" \ + "\n" \ + " YamlTree(indent, lines)" \ + "\n" \ + "Where indent provides the length of a single level of indentation" \ + " and lines are a tuple of Yaml lines used to built the tree." + + ret = PyArg_ParseTuple(args, "nO!", &indent, &PyTuple_Type, &tuple); + if (!ret) return NULL; + + count = PyTuple_Size(tuple); + + lines = (GYamlLine **)malloc(count * sizeof(GYamlLine *)); + + for (i = 0; i < count; i++) + { + item = PyTuple_GetItem(tuple, i); + + ret = convert_to_yaml_line(item, &lines[i]); + + if (ret == 0) + g_object_ref(G_OBJECT(lines[i])); + + else + goto arg_error; + + } + + tree = g_yaml_tree_new(indent, lines, count); + + arg_error: + + for (k = 0; k < i; k++) + g_object_unref(G_OBJECT(lines[i])); + + free(lines); + + /* S'il y a eu une erreur... */ + if (i < count) return NULL; + + if (tree == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + g_object_ref_sink(G_OBJECT(tree)); + result = pygobject_new(G_OBJECT(tree)); + g_object_unref(tree); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = variable non utilisée ici. * +* args = arguments fournis à l'appel. * +* * +* Description : Recherche le noeud correspondant à un chemin. * +* * +* Retour : Eventuel noeud trouvé ou NULL si aucun. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_tree_find_node_by_path(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + const char *path; /* Chemin d'accès à traiter */ + int ret; /* Bilan de lecture des args. */ + GYamlTree *tree; /* Version GLib du type */ + GYamlNode *found; /* Création GLib à transmettre */ + +#define YAML_TREE_FIND_BY_PATH PYTHON_METHOD_DEF \ +( \ + find_node_by_path, "path", \ + METH_VARARGS, py_yaml_tree, \ + "Find a node in a Yaml tree by its path.\n" \ + "\n" \ + "Paths are node keys separated by '/', such as '/my/path/to/node'." \ +) + + ret = PyArg_ParseTuple(args, "s", &path); + if (!ret) return NULL; + + tree = G_YAML_TREE(pygobject_get(self)); + + found = g_yaml_tree_find_node_by_path(tree, path); + + if (found == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + result = pygobject_new(G_OBJECT(found)); + g_object_unref(found); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit la liste des premiers noeuds de l'arborescence Yaml. * +* * +* Retour : Noeuds constituant les racines de l'arborescence. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_yaml_tree_get_root_nodes(PyObject *self, void *closure) +{ + PyObject *result; /* Résultat à retourner */ + GYamlTree *tree; /* Version GLib du type */ + size_t count; /* Quantité de noeuds à traiter*/ + GYamlNode **nodes; /* Liste des noeuds à la racine*/ + size_t i; /* Boucle de parcours */ +#ifndef NDEBUG + int ret; /* Bilan d'une insertion */ +#endif + +#define YAML_TREE_ROOT_NODES_ATTRIB PYTHON_GET_DEF_FULL \ +( \ + root_nodes, py_yaml_tree, \ + "List of Yaml nodes which are the roots of all tree nodes." \ +) + + tree = G_YAML_TREE(pygobject_get(self)); + + nodes = g_yaml_tree_get_root_nodes(tree, &count); + + result = PyTuple_New(count); + + for (i = 0; i < count; i++) + { +#ifndef NDEBUG + ret = PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); + assert(ret == 0); +#else + PyTuple_SetItem(result, i, pygobject_new(G_OBJECT(nodes[i]))); +#endif + + g_object_unref(G_OBJECT(nodes[i])); + + } + + free(nodes); + + 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_yaml_tree_type(void) +{ + static PyMethodDef py_yaml_tree_methods[] = { + YAML_TREE_FIND_BY_PATH, + { NULL } + }; + + static PyGetSetDef py_yaml_tree_getseters[] = { + YAML_TREE_ROOT_NODES_ATTRIB, + { NULL } + }; + + static PyTypeObject py_yaml_tree_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.plugins.yaml.YamlTree", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = YAML_TREE_DOC, + + .tp_methods = py_yaml_tree_methods, + .tp_getset = py_yaml_tree_getseters, + .tp_new = py_yaml_tree_new + + }; + + return &py_yaml_tree_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.plugins.....YamlTree. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_yaml_tree(PyObject *module) +{ + PyTypeObject *type; /* Type Python 'YamlTree' */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_yaml_tree_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_YAML_TREE, type, &PyGObject_Type)) + return false; + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en arborescence de lignes au format Yaml. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_yaml_tree(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_yaml_tree_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to Yaml tree"); + break; + + case 1: + *((GYamlTree **)dst) = G_YAML_TREE(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/yaml/python/tree.h b/plugins/yaml/python/tree.h new file mode 100644 index 0000000..df9d5b8 --- /dev/null +++ b/plugins/yaml/python/tree.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * tree.h - prototypes pour l'équivalent Python du fichier "plugins/yaml/tree.h" + * + * Copyright (C) 2019 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_YAML_PYTHON_TREE_H +#define _PLUGINS_YAML_PYTHON_TREE_H + + +#include <Python.h> +#include <stdbool.h> + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_yaml_tree_type(void); + +/* Prend en charge l'objet 'pychrysalide.plugins.yaml.YamlTree'. */ +bool register_python_yaml_tree(PyObject *); + +/* Tente de convertir en arborescence de lignes au format Yaml. */ +int convert_to_yaml_tree(PyObject *, void *); + + + +#endif /* _PLUGINS_YAML_PYTHON_TREE_H */ |