diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-05-07 14:24:16 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-05-07 14:24:16 (GMT) |
commit | c136e2c4c1ad02ea2e363fbe71ce54c6255793e2 (patch) | |
tree | ba19cc948ac23a3930d9943e94f8e37ec86991c1 /plugins/dex/python/translate.c | |
parent | b38035259b6e09c77d87950fe409fb72d09887d3 (diff) |
Extended the Python bindings for the Dex format.
Diffstat (limited to 'plugins/dex/python/translate.c')
-rw-r--r-- | plugins/dex/python/translate.c | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/plugins/dex/python/translate.c b/plugins/dex/python/translate.c new file mode 100644 index 0000000..eb80c43 --- /dev/null +++ b/plugins/dex/python/translate.c @@ -0,0 +1,293 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * translate.c - conversion de structures Dex en objets Python + * + * Copyright (C) 2018 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 "translate.h" + + +#include <assert.h> + + +#include <plugins/pychrysalide/struct.h> + + +#include "../dex_def.h" + + + +/****************************************************************************** +* * +* Paramètres : info = ensemble d'informations Dex à décrire en Python. * +* * +* Description : Traduit des informations de champ de classe Dex en Python. * +* * +* Retour : Structure mise en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *translate_dex_field_info_to_python(const encoded_field *info) +{ + PyObject *result; /* Construction à retourner */ + PyTypeObject *base; /* Modèle d'objet à créer */ + PyObject *attrib; /* Attribut à constituer */ + int ret; /* Bilan d'une mise en place */ + + base = get_python_py_struct_type(); + + result = PyObject_CallFunction((PyObject *)base, NULL); + assert(result != NULL); + + /* Champs réguliers */ + +#define TRANSLATE_ENCODED_FIELD_PROP(_f) \ + do \ + { \ + attrib = PyLong_FromUnsignedLongLong(info->_f); \ + ret = PyDict_SetItemString(result, #_f, attrib); \ + if (ret != 0) goto tdcdtp_failed; \ + } \ + while (0); + + TRANSLATE_ENCODED_FIELD_PROP(field_idx_diff); + TRANSLATE_ENCODED_FIELD_PROP(access_flags); + + return result; + + tdcdtp_failed: + + Py_DECREF(result); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : info = ensemble d'informations Dex à décrire en Python. * +* * +* Description : Traduit des informations de méthode Dex en Python. * +* * +* Retour : Structure mise en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *translate_dex_method_info_to_python(const encoded_method *info) +{ + PyObject *result; /* Construction à retourner */ + PyTypeObject *base; /* Modèle d'objet à créer */ + PyObject *attrib; /* Attribut à constituer */ + int ret; /* Bilan d'une mise en place */ + + base = get_python_py_struct_type(); + + result = PyObject_CallFunction((PyObject *)base, NULL); + assert(result != NULL); + + /* Champs réguliers */ + +#define TRANSLATE_ENCODED_METHOD_PROP(_f) \ + do \ + { \ + attrib = PyLong_FromUnsignedLongLong(info->_f); \ + ret = PyDict_SetItemString(result, #_f, attrib); \ + if (ret != 0) goto tdcdtp_failed; \ + } \ + while (0); + + TRANSLATE_ENCODED_METHOD_PROP(method_idx_diff); + TRANSLATE_ENCODED_METHOD_PROP(access_flags); + TRANSLATE_ENCODED_METHOD_PROP(code_off); + + return result; + + tdcdtp_failed: + + Py_DECREF(result); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : info = ensemble d'informations Dex à décrire en Python. * +* * +* Description : Traduit des informations de corps de méthode Dex en Python. * +* * +* Retour : Structure mise en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *translate_dex_method_body_to_python(const code_item *body) +{ + PyObject *result; /* Construction à retourner */ + PyTypeObject *base; /* Modèle d'objet à créer */ + PyObject *attrib; /* Attribut à constituer */ + int ret; /* Bilan d'une mise en place */ + + base = get_python_py_struct_type(); + + result = PyObject_CallFunction((PyObject *)base, NULL); + assert(result != NULL); + + /* Champs réguliers */ + +#define TRANSLATE_CODE_ITEM_PROP(_f) \ + do \ + { \ + attrib = PyLong_FromUnsignedLongLong(body->_f); \ + ret = PyDict_SetItemString(result, #_f, attrib); \ + if (ret != 0) goto tdcdtp_failed; \ + } \ + while (0); + + TRANSLATE_CODE_ITEM_PROP(registers_size); + TRANSLATE_CODE_ITEM_PROP(ins_size); + TRANSLATE_CODE_ITEM_PROP(outs_size); + TRANSLATE_CODE_ITEM_PROP(tries_size); + TRANSLATE_CODE_ITEM_PROP(debug_info_off); + TRANSLATE_CODE_ITEM_PROP(insns_size); + + return result; + + tdcdtp_failed: + + Py_DECREF(result); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : item = ensemble d'informations Dex à décrire en Python. * +* * +* Description : Traduit une définition de classe Dex en Python. * +* * +* Retour : Structure mise en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *translate_dex_class_definition_to_python(const class_def_item *item) +{ + PyObject *result; /* Construction à retourner */ + PyTypeObject *base; /* Modèle d'objet à créer */ + PyObject *attrib; /* Attribut à constituer */ + int ret; /* Bilan d'une mise en place */ + + base = get_python_py_struct_type(); + + result = PyObject_CallFunction((PyObject *)base, NULL); + assert(result != NULL); + + /* Champs réguliers */ + +#define TRANSLATE_CLASS_DEF_PROP(_f) \ + do \ + { \ + attrib = PyLong_FromUnsignedLongLong(item->_f); \ + ret = PyDict_SetItemString(result, #_f, attrib); \ + if (ret != 0) goto tdcdtp_failed; \ + } \ + while (0); + + TRANSLATE_CLASS_DEF_PROP(class_idx); + TRANSLATE_CLASS_DEF_PROP(access_flags); + TRANSLATE_CLASS_DEF_PROP(superclass_idx); + TRANSLATE_CLASS_DEF_PROP(interfaces_off); + TRANSLATE_CLASS_DEF_PROP(source_file_idx); + TRANSLATE_CLASS_DEF_PROP(annotations_off); + TRANSLATE_CLASS_DEF_PROP(class_data_off); + TRANSLATE_CLASS_DEF_PROP(static_values_off); + + return result; + + tdcdtp_failed: + + Py_DECREF(result); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : item = ensemble d'informations Dex à décrire en Python. * +* * +* Description : Traduit des données de classe Dex en Python. * +* * +* Retour : Structure mise en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *translate_dex_class_data_to_python(const class_data_item *item) +{ + PyObject *result; /* Construction à retourner */ + PyTypeObject *base; /* Modèle d'objet à créer */ + PyObject *attrib; /* Attribut à constituer */ + int ret; /* Bilan d'une mise en place */ + + base = get_python_py_struct_type(); + + result = PyObject_CallFunction((PyObject *)base, NULL); + assert(result != NULL); + + /* Champs réguliers */ + +#define TRANSLATE_CLASS_DATA_PROP(_f) \ + do \ + { \ + attrib = PyLong_FromUnsignedLongLong(item->_f); \ + ret = PyDict_SetItemString(result, #_f, attrib); \ + if (ret != 0) goto tdcdtp_failed; \ + } \ + while (0); + + TRANSLATE_CLASS_DATA_PROP(static_fields_size); + TRANSLATE_CLASS_DATA_PROP(instance_fields_size); + TRANSLATE_CLASS_DATA_PROP(direct_methods_size); + TRANSLATE_CLASS_DATA_PROP(virtual_methods_size); + + return result; + + tdcdtp_failed: + + Py_DECREF(result); + + return NULL; + +} |