From e146d7d8c7af50d2592379570c66acaf1dfa73c9 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 4 Dec 2018 23:49:03 +0100 Subject: Updated the Java descriptor demangling process. --- configure.ac | 2 + plugins/Makefile.am | 2 +- plugins/javadesc/Makefile.am | 32 ++++ plugins/javadesc/context.c | 209 ++++++++++++++++++++++ plugins/javadesc/context.h | 52 ++++++ plugins/javadesc/core.c | 64 +++++++ plugins/javadesc/core.h | 38 ++++ plugins/javadesc/demangler.c | 175 ++++++++++++++++++ plugins/javadesc/demangler.h | 58 ++++++ plugins/javadesc/field.c | 344 ++++++++++++++++++++++++++++++++++++ plugins/javadesc/field.h | 38 ++++ plugins/javadesc/method.c | 152 ++++++++++++++++ plugins/javadesc/method.h | 38 ++++ plugins/javadesc/python/Makefile.am | 19 ++ plugins/javadesc/python/demangler.c | 145 +++++++++++++++ plugins/javadesc/python/demangler.h | 42 +++++ plugins/javadesc/python/module.c | 61 +++++++ plugins/javadesc/python/module.h | 38 ++++ src/mangling/Makefile.am | 27 +-- src/mangling/java.h | 59 ------- src/mangling/java_gram.y | 279 ----------------------------- src/mangling/java_tok.l | 37 ---- tests/mangling/java.py | 30 ++++ 23 files changed, 1540 insertions(+), 401 deletions(-) create mode 100644 plugins/javadesc/Makefile.am create mode 100644 plugins/javadesc/context.c create mode 100644 plugins/javadesc/context.h create mode 100644 plugins/javadesc/core.c create mode 100644 plugins/javadesc/core.h create mode 100644 plugins/javadesc/demangler.c create mode 100644 plugins/javadesc/demangler.h create mode 100644 plugins/javadesc/field.c create mode 100644 plugins/javadesc/field.h create mode 100644 plugins/javadesc/method.c create mode 100644 plugins/javadesc/method.h create mode 100644 plugins/javadesc/python/Makefile.am create mode 100644 plugins/javadesc/python/demangler.c create mode 100644 plugins/javadesc/python/demangler.h create mode 100644 plugins/javadesc/python/module.c create mode 100644 plugins/javadesc/python/module.h delete mode 100644 src/mangling/java.h delete mode 100644 src/mangling/java_gram.y delete mode 100644 src/mangling/java_tok.l create mode 100644 tests/mangling/java.py diff --git a/configure.ac b/configure.ac index 89ec08d..8450923 100644 --- a/configure.ac +++ b/configure.ac @@ -366,6 +366,8 @@ AC_CONFIG_FILES([Makefile plugins/fmtp/Makefile plugins/itanium/Makefile plugins/itanium/python/Makefile + plugins/javadesc/Makefile + plugins/javadesc/python/Makefile plugins/libcsem/Makefile plugins/lnxsyscalls/Makefile plugins/mobicore/Makefile diff --git a/plugins/Makefile.am b/plugins/Makefile.am index e47a5fb..8731354 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -4,4 +4,4 @@ if HAVE_PYTHON3_CONFIG endif # androhelpers -SUBDIRS = $(PYTHON3_SUBDIRS) arm dex dalvik devdbg dexbnf dwarf elf fmtp itanium libcsem lnxsyscalls mobicore readdex readelf readmc ropgadgets +SUBDIRS = $(PYTHON3_SUBDIRS) arm dex dalvik devdbg dexbnf dwarf elf fmtp itanium javadesc libcsem lnxsyscalls mobicore readdex readelf readmc ropgadgets diff --git a/plugins/javadesc/Makefile.am b/plugins/javadesc/Makefile.am new file mode 100644 index 0000000..bc55192 --- /dev/null +++ b/plugins/javadesc/Makefile.am @@ -0,0 +1,32 @@ + +lib_LTLIBRARIES = libjavadesc.la + +libdir = $(pluginslibdir) + + +libjavadesc_la_SOURCES = \ + context.h context.c \ + core.h core.c \ + demangler.h demangler.c \ + field.h field.c \ + method.h method.c + +libjavadesc_la_LIBADD = \ + python/libjavadescpython.la + +libjavadesc_la_LDFLAGS = \ + -L$(top_srcdir)/src/.libs -lchrysacore \ + -Wl,-rpath,$(abs_top_srcdir)/plugins/pychrysalide/.libs \ + -L$(top_srcdir)/plugins/pychrysalide/.libs -l:pychrysalide.so + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libjavadesc_la_SOURCES:%c=) + + +AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) -I$(top_srcdir)/src + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) + +SUBDIRS = python diff --git a/plugins/javadesc/context.c b/plugins/javadesc/context.c new file mode 100644 index 0000000..9ed5454 --- /dev/null +++ b/plugins/javadesc/context.c @@ -0,0 +1,209 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * context.c - fourniture de contexte aux phases de décodage Java + * + * 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 Chrysalide. If not, see . + */ + + +#include "context.h" + + +#include + + +#include "field.h" +#include "method.h" + + + +/* Contexte de décodage Java (instance) */ +struct _GJavaDemangling +{ + GDemanglingContext parent; /* A laisser en premier */ + +}; + +/* Contexte de décodage Java (classe) */ +struct _GJavaDemanglingClass +{ + GDemanglingContextClass parent; /* A laisser en premier */ + +}; + + +/* Initialise la classe des contextes de décodage. */ +static void g_java_demangling_class_init(GJavaDemanglingClass *); + +/* Initialise une instance de contexte pour décodage. */ +static void g_java_demangling_init(GJavaDemangling *); + +/* Supprime toutes les références externes. */ +static void g_java_demangling_dispose(GJavaDemangling *); + +/* Procède à la libération totale de la mémoire. */ +static void g_java_demangling_finalize(GJavaDemangling *); + +/* Décode une définition de type pour Java. */ +static GDataType *g_java_demangling_decode_type(GJavaDemangling *); + +/* Décode une définition de routine pour Java. */ +static GBinRoutine *g_java_demangling_decode_routine(GJavaDemangling *); + + + +/* Indique le type défini pour un contexte de décodage. */ +G_DEFINE_TYPE(GJavaDemangling, g_java_demangling, G_TYPE_DEMANGLING_CONTEXT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des contextes de décodage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangling_class_init(GJavaDemanglingClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + GDemanglingContextClass *context; /* Version de base du contexte */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_java_demangling_dispose; + object->finalize = (GObjectFinalizeFunc)g_java_demangling_finalize; + + context = G_DEMANGLING_CONTEXT_CLASS(klass); + + context->decode_type = (decode_type_fc)g_java_demangling_decode_type; + context->decode_routine = (decode_routine_fc)g_java_demangling_decode_routine; + +} + + +/****************************************************************************** +* * +* Paramètres : context = instance à initialiser. * +* * +* Description : Initialise une instance de contexte pour décodage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangling_init(GJavaDemangling *context) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : context = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangling_dispose(GJavaDemangling *context) +{ + G_OBJECT_CLASS(g_java_demangling_parent_class)->dispose(G_OBJECT(context)); + +} + + +/****************************************************************************** +* * +* Paramètres : context = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangling_finalize(GJavaDemangling *context) +{ + G_OBJECT_CLASS(g_java_demangling_parent_class)->finalize(G_OBJECT(context)); + +} + + +/****************************************************************************** +* * +* Paramètres : context = environnement de décodage à manipuler. * +* * +* Description : Décode une définition de type pour Java. * +* * +* Retour : Nouvelle instance créée ou NULL en cas d'erreur fatale. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *g_java_demangling_decode_type(GJavaDemangling *context) +{ + GDataType *result; /* Type construit à retourner */ + GDemanglingContext *base; /* Autre version du contexte */ + + base = G_DEMANGLING_CONTEXT(context); + + result = jtd_field_descriptor(&base->buffer); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : context = environnement de décodage à manipuler. * +* * +* Description : Décode une définition de routine pour Java. * +* * +* Retour : Nouvelle instance créée ou NULL en cas d'erreur fatale. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GBinRoutine *g_java_demangling_decode_routine(GJavaDemangling *context) +{ + GBinRoutine *result; /* Routine en place à retourner*/ + GDemanglingContext *base; /* Autre version du contexte */ + + base = G_DEMANGLING_CONTEXT(context); + + result = jmd_method_descriptor(&base->buffer); + + return result; + +} diff --git a/plugins/javadesc/context.h b/plugins/javadesc/context.h new file mode 100644 index 0000000..004bbb2 --- /dev/null +++ b/plugins/javadesc/context.h @@ -0,0 +1,52 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * context.h - prototypes pour la fourniture de contexte aux phases de décodage Java + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _PLUGINS_JAVADESC_CONTEXT_H +#define _PLUGINS_JAVADESC_CONTEXT_H + + +#include + + + +#define G_TYPE_JAVA_DEMANGLING g_java_demangling_get_type() +#define G_JAVA_DEMANGLING(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_JAVA_DEMANGLING, GJavaDemangling)) +#define G_IS_JAVA_DEMANGLING(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_JAVA_DEMANGLING)) +#define G_JAVA_DEMANGLING_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_JAVA_DEMANGLING, GJavaDemanglingClass)) +#define G_IS_JAVA_DEMANGLING_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_JAVA_DEMANGLING)) +#define G_JAVA_DEMANGLING_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_JAVA_DEMANGLING, GJavaDemanglingClass)) + + +/* Contexte de décodage Java (instance) */ +typedef struct _GJavaDemangling GJavaDemangling; + +/* Contexte de décodage Java (classe) */ +typedef struct _GJavaDemanglingClass GJavaDemanglingClass; + + +/* Indique le type défini pour un contexte de décodage Java. */ +GType g_java_demangling_get_type(void); + + + +#endif /* _PLUGINS_JAVADESC_CONTEXT_H */ diff --git a/plugins/javadesc/core.c b/plugins/javadesc/core.c new file mode 100644 index 0000000..e374749 --- /dev/null +++ b/plugins/javadesc/core.c @@ -0,0 +1,64 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * core.c - intégration du décodage pour symboles Java + * + * 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 Chrysalide. If not, see . + */ + + +#include "core.h" + + +#include +#include + + +#include "demangler.h" +#include "python/module.h" + + + +DEFINE_CHRYSALIDE_PLUGIN("javadesc", "Symbol demangler for Java", "0.1.0", + RL("PyChrysalide"), AL(PGA_PLUGIN_INIT)); + + + +/****************************************************************************** +* * +* Paramètres : plugin = greffon à manipuler. * +* * +* Description : Prend acte du chargement du greffon. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin) +{ + bool result; /* Bilan à retourner */ + + result = register_demangler_type("java", G_TYPE_JAVA_DEMANGLER); + + if (result) + result = add_mangling_javadesc_module_to_python_module(); + + return result; + +} diff --git a/plugins/javadesc/core.h b/plugins/javadesc/core.h new file mode 100644 index 0000000..8938c2e --- /dev/null +++ b/plugins/javadesc/core.h @@ -0,0 +1,38 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * core.h - prototypes pour l'intégration du décodage pour symboles Java + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _PLUGINS_JAVADESC_CORE_H +#define _PLUGINS_JAVADESC_CORE_H + + +#include +#include + + + +/* Prend acte du chargement du greffon. */ +G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *); + + + +#endif /* _PLUGINS_JAVADESC_CORE_H */ diff --git a/plugins/javadesc/demangler.c b/plugins/javadesc/demangler.c new file mode 100644 index 0000000..78b62f1 --- /dev/null +++ b/plugins/javadesc/demangler.c @@ -0,0 +1,175 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * demangler.c - décodage des noms d'éléments Java + * + * 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 Chrysalide. If not, see . + */ + + +#include "demangler.h" + + +#include + + +#include "context.h" + + + +/* Décodeur de désignations Java (instance) */ +struct _GJavaDemangler +{ + GCompDemangler parent; /* A laisser en premier */ + +}; + +/* Décodeur de désignations Java (classe) */ +struct _GJavaDemanglerClass +{ + GCompDemanglerClass parent; /* A laisser en premier */ + +}; + + +/* Initialise la classe des décodeurs de désignations. */ +static void g_java_demangler_class_init(GJavaDemanglerClass *); + +/* Initialise une instance de décodeur de désignations. */ +static void g_java_demangler_init(GJavaDemangler *); + +/* Supprime toutes les références externes. */ +static void g_java_demangler_dispose(GJavaDemangler *); + +/* Procède à la libération totale de la mémoire. */ +static void g_java_demangler_finalize(GJavaDemangler *); + + + +/* Indique le type défini pour un décodeur de désignations. */ +G_DEFINE_TYPE(GJavaDemangler, g_java_demangler, G_TYPE_COMP_DEMANGLER); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des décodeurs de désignations Java. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangler_class_init(GJavaDemanglerClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + GCompDemanglerClass *demangler; /* Version parente basique */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_java_demangler_dispose; + object->finalize = (GObjectFinalizeFunc)g_java_demangler_finalize; + + demangler = G_COMP_DEMANGLER_CLASS(klass); + + demangler->can_demangle = (can_be_demangled_fc)NULL; + + demangler->ns_sep = "."; + demangler->context_type = G_TYPE_JAVA_DEMANGLING; + +} + + +/****************************************************************************** +* * +* Paramètres : demangler = instance à initialiser. * +* * +* Description : Initialise une instance de décodeur de désignations Java. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangler_init(GJavaDemangler *demangler) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : demangler = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangler_dispose(GJavaDemangler *demangler) +{ + G_OBJECT_CLASS(g_java_demangler_parent_class)->dispose(G_OBJECT(demangler)); + +} + + +/****************************************************************************** +* * +* Paramètres : demangler = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_java_demangler_finalize(GJavaDemangler *demangler) +{ + G_OBJECT_CLASS(g_java_demangler_parent_class)->finalize(G_OBJECT(demangler)); + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Met en place un nouveau décodeur de symboles pour Java. * +* * +* Retour : Instance obtenue ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GCompDemangler *g_java_demangler_new(void) +{ + GJavaDemangler *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_JAVA_DEMANGLER, NULL); + + return G_COMP_DEMANGLER(result); + +} diff --git a/plugins/javadesc/demangler.h b/plugins/javadesc/demangler.h new file mode 100644 index 0000000..965ed51 --- /dev/null +++ b/plugins/javadesc/demangler.h @@ -0,0 +1,58 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * demangler.h - prototypes pour le décodage des noms d'éléments Java + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _PLUGINS_JAVADESC_DEMANGLER_H +#define _PLUGINS_JAVADESC_DEMANGLER_H + + +#include + + +#include + + + +#define G_TYPE_JAVA_DEMANGLER g_java_demangler_get_type() +#define G_JAVA_DEMANGLER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_JAVA_DEMANGLER, GJavaDemangler)) +#define G_IS_JAVA_DEMANGLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_JAVA_DEMANGLER)) +#define G_JAVA_DEMANGLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_JAVA_DEMANGLER, GJavaDemanglerClass)) +#define G_IS_JAVA_DEMANGLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_JAVA_DEMANGLER)) +#define G_JAVA_DEMANGLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_JAVA_DEMANGLER, GJavaDemanglerClass)) + + +/* Décodeur de désignations Java (instance) */ +typedef struct _GJavaDemangler GJavaDemangler; + +/* Décodeur de désignations Java (classe) */ +typedef struct _GJavaDemanglerClass GJavaDemanglerClass; + + +/* Indique le type défini pour un décodeur de désignations Java. */ +GType g_java_demangler_get_type(void); + +/* Met en place un nouveau décodeur de symboles pour Java. */ +GCompDemangler *g_java_demangler_new(void); + + + +#endif /* _PLUGINS_JAVADESC_DEMANGLER_H */ diff --git a/plugins/javadesc/field.c b/plugins/javadesc/field.c new file mode 100644 index 0000000..95b0b8f --- /dev/null +++ b/plugins/javadesc/field.c @@ -0,0 +1,344 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * type.c - décodage de types pour Java + * + * 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 Chrysalide. If not, see . + */ + + +#include "field.h" + + +#include +#include + + +#include +#include +#include + + + +/* Extrait un type particulier dans un décodage Java. */ +static GDataType *jtd_object_type_descriptor(input_buffer *); + +/* Extrait un type particulier dans un décodage Java. */ +static GDataType *jtd_array_type_descriptor(input_buffer *); + +/* Extrait un type particulier dans un décodage Java. */ +static GDataType *jtd_base_type_descriptor(input_buffer *, char); + + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* * +* Description : Extrait un type particulier dans un décodage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *jtd_object_type_descriptor(input_buffer *buffer) +{ + GDataType *result; /* Classe à retourner */ + char *name; /* Désignation récupérée */ + size_t len; /* Taille de cette désignation */ + char next; /* Prochain caractère obtenu */ + GDataType *root; /* Espace de noms racine */ + GDataType *ns; /* Espace de noms à attribuer */ + GDataType *parent; /* Espace de noms parent */ + + result = NULL; + + name = NULL; + len = 0; + + do + { + next = get_input_buffer_next_char(buffer); + + if (next == ';') + break; + + else if (next == '/' || next == '$') + { + /** + * Il faut obligatoirement avoir déjà traité un nom de paquet ! + */ + if (len == 0) + break; + + name = realloc(name, ++len * sizeof(char)); + + name[len - 1] = '\0'; + + root = g_class_enum_type_new(CET_CLASS, name); + + /** + * Pour éviter les fuites si aucun paquet n'est présent... + */ + name = NULL; + + result = jtd_object_type_descriptor(buffer); + + if (result == NULL) + g_object_unref(G_OBJECT(root)); + + else + { + ns = g_data_type_get_namespace(result); + + if (ns == NULL) + g_data_type_set_namespace(result, root, strdup(".")); + + else + { + while ((parent = g_data_type_get_namespace(ns)) != NULL) + { + g_object_unref(G_OBJECT(ns)); + ns = parent; + } + + g_data_type_set_namespace(ns, root, strdup(".")); + + g_object_unref(G_OBJECT(ns)); + + } + + } + + break; + + } + + else if (next != '\0') + { + name = realloc(name, ++len * sizeof(char)); + + name[len - 1] = next; + + } + + else + { + if (name != NULL) + { + free(name); + name = NULL; + } + + break; + + } + + } + while (1); + + if (name != NULL) + result = g_class_enum_type_new(CET_CLASS, name); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* * +* Description : Extrait un type particulier dans un décodage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *jtd_array_type_descriptor(input_buffer *buffer) +{ + GDataType *result; /* Type à retourner */ + size_t dim; /* Dimension éventuelle */ + char ahead; /* Caractère déterminant lu */ + GDataType *descriptor; /* (Sous-)type à charger */ + + /** + * La règle traitée est la suivante : + * + * ArrayType: + * [ ComponentType + * + */ + + + dim = 0; + + do + { + dim++; + + ahead = peek_input_buffer_char(buffer); + + if (ahead == '[') + advance_input_buffer(buffer, 1); + else + break; + + } + while (1); + + descriptor = jtd_field_descriptor(buffer); + + if (descriptor == NULL) + result = NULL; + + else + { + result = g_encapsulated_type_new(ECT_ARRAY, descriptor); + + g_encapsulated_type_set_dimension(G_ENCAPSULATED_TYPE(result), dim); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* ahead = caractère déjà dépilé de ces données. * +* * +* Description : Extrait un type particulier dans un décodage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *jtd_base_type_descriptor(input_buffer *buffer, char ahead) +{ + GDataType *result; /* Type à retourner */ + + /** + * La règle traitée est la suivante : + * + * BaseType: + * B + * C + * D + * F + * I + * J + * S + * Z + * + */ + + switch (ahead) + { + case 'B': + result = g_basic_type_new(BTP_SCHAR); + break; + + case 'C': + result = g_basic_type_new(BTP_UCHAR); + break; + + case 'D': + result = g_basic_type_new(BTP_DOUBLE); + break; + + case 'F': + result = g_basic_type_new(BTP_FLOAT); + break; + + case 'I': + result = g_basic_type_new(BTP_INT); + break; + + case 'J': + result = g_basic_type_new(BTP_LONG); + break; + + case 'S': + result = g_basic_type_new(BTP_SHORT); + break; + + case 'Z': + result = g_basic_type_new(BTP_BOOL); + break; + + default: + result = NULL; + break; + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* * +* Description : Extrait un type particulier dans un décodage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GDataType *jtd_field_descriptor(input_buffer *buffer) +{ + GDataType *result; /* Type à retourner */ + char ahead; /* Caractère déterminant lu */ + + /** + * La règle traitée est la suivante : + * + * FieldType: + * BaseType + * ObjectType + * ArrayType + * + * Cf. § 4.3.2. Field Descriptors + */ + + ahead = get_input_buffer_next_char(buffer); + + if (ahead == 'L') + result = jtd_object_type_descriptor(buffer); + + else if (ahead == '[') + result = jtd_array_type_descriptor(buffer); + + else + result = jtd_base_type_descriptor(buffer, ahead); + + return result; + +} diff --git a/plugins/javadesc/field.h b/plugins/javadesc/field.h new file mode 100644 index 0000000..9a802e7 --- /dev/null +++ b/plugins/javadesc/field.h @@ -0,0 +1,38 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * type.h - prototypes pour le décodage de types pour Java + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _PLUGINS_JAVADESC_TYPE_H +#define _PLUGINS_JAVADESC_TYPE_H + + +#include +#include + + + +/* Extrait un type particulier dans un décodage Java. */ +GDataType *jtd_field_descriptor(input_buffer *); + + + +#endif /* _PLUGINS_JAVADESC_TYPE_H */ diff --git a/plugins/javadesc/method.c b/plugins/javadesc/method.c new file mode 100644 index 0000000..b78665d --- /dev/null +++ b/plugins/javadesc/method.c @@ -0,0 +1,152 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * shorty.c - décodage de routines pour Java + * + * 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 Chrysalide. If not, see . + */ + + +#include "method.h" + + +#include "field.h" + + + +/* Extrait un type particulier dans un décodage Java. */ +static GDataType *jmd_method_return_type(input_buffer *); + + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* * +* Description : Extrait un type particulier dans un décodage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static GDataType *jmd_method_return_type(input_buffer *buffer) +{ + GDataType *result; /* Type à retourner */ + char ahead; /* Caractère déterminant lu */ + + /** + * La règle traitée est la suivante : + * + * ReturnDescriptor: + * FieldType + * V + * + */ + + ahead = peek_input_buffer_char(buffer); + + if (ahead == 'V') + { + advance_input_buffer(buffer, 1); + result = g_basic_type_new(BTP_VOID); + } + + else + result = jtd_field_descriptor(buffer); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : buffer = tampon contenant les données utiles. * +* * +* Description : Extrait un routine particulière depuis un codage Java. * +* * +* Retour : Nouveau type mis en place ou NULL en cas d'erreur. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GBinRoutine *jmd_method_descriptor(input_buffer *buffer) +{ + GBinRoutine *result; /* Type à retourner */ + char ahead; /* Caractère déterminant lu */ + GDataType *type; /* Description de type obtenue */ + GBinVariable *var; /* Argument de routine */ + + /** + * La règle traitée est la suivante : + * + * MethodDescriptor: + * ( {ParameterDescriptor} ) ReturnDescriptor + * + */ + + if (!check_input_buffer_char(buffer, '(')) + goto exit; + + result = g_binary_routine_new(); + + /* Arguments */ + + for (ahead = peek_input_buffer_char(buffer); + ahead != ')'; + ahead = peek_input_buffer_char(buffer)) + { + type = jtd_field_descriptor(buffer); + + if (type == NULL) + goto error; + + else + { + var = g_binary_variable_new(type); + g_binary_routine_add_arg(result, var); + } + + } + + if (!check_input_buffer_char(buffer, ')')) + goto error; + + /* Retour */ + + type = jmd_method_return_type(buffer); + + if (type == NULL) + goto error; + + else + g_binary_routine_set_return_type(result, type); + + return result; + + error: + + g_object_unref(G_OBJECT(result)); + + exit: + + return NULL; + +} diff --git a/plugins/javadesc/method.h b/plugins/javadesc/method.h new file mode 100644 index 0000000..3914c38 --- /dev/null +++ b/plugins/javadesc/method.h @@ -0,0 +1,38 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * shorty.h - prototypes pour le décodage de routines pour Java + * + * 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 Chrysalide. If not, see . + */ + + +#ifndef _PLUGINS_JAVADESC_SHORTY_H +#define _PLUGINS_JAVADESC_SHORTY_H + + +#include +#include + + + +/* Extrait un routine particulière depuis un codage Java. */ +GBinRoutine *jmd_method_descriptor(input_buffer *); + + + +#endif /* _PLUGINS_JAVADESC_SHORTY_H */ diff --git a/plugins/javadesc/python/Makefile.am b/plugins/javadesc/python/Makefile.am new file mode 100644 index 0000000..06deb3d --- /dev/null +++ b/plugins/javadesc/python/Makefile.am @@ -0,0 +1,19 @@ + +noinst_LTLIBRARIES = libjavadescpython.la + +libjavadescpython_la_SOURCES = \ + demangler.h demangler.c \ + module.h module.c + +libjavadescpython_la_LDFLAGS = + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libjavadescpython_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/javadesc/python/demangler.c b/plugins/javadesc/python/demangler.c new file mode 100644 index 0000000..a62ca99 --- /dev/null +++ b/plugins/javadesc/python/demangler.c @@ -0,0 +1,145 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * demangler.c - équivalent Python du fichier "plugins/javadesc/demangler.c" + * + * 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 "demangler.h" + + +#include + + +#include +#include + + +#include "../demangler.h" + + + +/* Crée un nouvel objet Python de type 'JavaDemangler'. */ +static PyObject *py_java_demangler_new(PyTypeObject *, PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* 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 'JavaDemangler'. * +* * +* Retour : Instance Python mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_java_demangler_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Instance à retourner */ + GCompDemangler *demangler; /* Instance à transposer */ + + demangler = g_java_demangler_new(); + + result = pygobject_new(G_OBJECT(demangler)); + + g_object_unref(G_OBJECT(demangler)); + + return (PyObject *)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_java_demangler_type(void) +{ + static PyMethodDef py_java_demangler_methods[] = { + { NULL } + }; + + static PyGetSetDef py_java_demangler_getseters[] = { + { NULL } + }; + + static PyTypeObject py_java_demangler_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.mangling.JavaDemangler", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = "PyChrysalide Java demangler", + + .tp_methods = py_java_demangler_methods, + .tp_getset = py_java_demangler_getseters, + .tp_new = py_java_demangler_new + + }; + + return &py_java_demangler_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.....JavaDemangler'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_java_demangler(PyObject *module) +{ + PyTypeObject *py_java_demangler_type; /* Type Python 'JavaDemangler' */ + PyObject *dict; /* Dictionnaire du module */ + + py_java_demangler_type = get_python_java_demangler_type(); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_JAVA_DEMANGLER, + py_java_demangler_type, get_python_compiler_demangler_type())) + return false; + + return true; + +} diff --git a/plugins/javadesc/python/demangler.h b/plugins/javadesc/python/demangler.h new file mode 100644 index 0000000..a4b48f1 --- /dev/null +++ b/plugins/javadesc/python/demangler.h @@ -0,0 +1,42 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * demangler.h - prototypes pour l'équivalent Python du fichier "plugins/javadesc/demangler.h" + * + * 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 + */ + + +#ifndef _PLUGINS_JAVADESC_PYTHON_DEMANGLER_H +#define _PLUGINS_JAVADESC_PYTHON_DEMANGLER_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_java_demangler_type(void); + +/* Prend en charge l'objet 'pychrysalide.format.mangling.JavaDemangler'. */ +bool register_python_java_demangler(PyObject *); + + + +#endif /* _PLUGINS_JAVADESC_PYTHON_DEMANGLER_H */ diff --git a/plugins/javadesc/python/module.c b/plugins/javadesc/python/module.c new file mode 100644 index 0000000..5211ba3 --- /dev/null +++ b/plugins/javadesc/python/module.c @@ -0,0 +1,61 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire javadesc en tant que module + * + * 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 "module.h" + + +#include + + +#include + + +#include "demangler.h" + + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Etend le module 'mangling' avec des compléments pour Java. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_mangling_javadesc_module_to_python_module(void) +{ + bool result; /* Bilan à retourner */ + PyObject *super; /* Module à compléter */ + + super = get_access_to_python_module("pychrysalide.mangling"); + + result = register_python_java_demangler(super); + + return result; + +} diff --git a/plugins/javadesc/python/module.h b/plugins/javadesc/python/module.h new file mode 100644 index 0000000..a3fc190 --- /dev/null +++ b/plugins/javadesc/python/module.h @@ -0,0 +1,38 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire javadesc en tant que module + * + * 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 + */ + + +#ifndef _PLUGINS_JAVADESC_PYTHON_MODULE_H +#define _PLUGINS_JAVADESC_PYTHON_MODULE_H + + +#include + + + +/* Etend le module 'mangling' avec des compléments pour Java. */ +bool add_mangling_javadesc_module_to_python_module(void); + + + +#endif /* _PLUGINS_JAVADESC_PYTHON_MODULE_H */ diff --git a/src/mangling/Makefile.am b/src/mangling/Makefile.am index 3ed04ef..0a24dc1 100644 --- a/src/mangling/Makefile.am +++ b/src/mangling/Makefile.am @@ -1,9 +1,5 @@ -BUILT_SOURCES = libjavamangling_la-java_gram.h - -AM_YFLAGS = -d - -noinst_LTLIBRARIES = libjavamangling.la libmangling.la +noinst_LTLIBRARIES = libmangling.la libmangling_la_SOURCES = \ context-int.h \ @@ -13,8 +9,7 @@ libmangling_la_SOURCES = \ libmangling_la_LDFLAGS = -libmangling_la_LIBADD = \ - libjavamangling.la +libmangling_la_LIBADD = devdir = $(includedir)/chrysalide/$(subdir:src/%=%) @@ -22,26 +17,8 @@ devdir = $(includedir)/chrysalide/$(subdir:src/%=%) dev_HEADERS = $(libmangling_la_SOURCES:%c=) -# Partie Java - -libjavamangling_la_SOURCES = \ - java.h \ - java_gram.y \ - java_tok.l - -libjavamangling_la_YFLAGS = -d -p java_ -o y.tab.c - -libjavamangling_la_LFLAGS = -P java_ -o lex.yy.c - - - - AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) - -# Automake fait les choses à moitié -CLEANFILES = java_gram.h java_gram.c libjavamangling_la-java_tok.c - SUBDIRS = diff --git a/src/mangling/java.h b/src/mangling/java.h deleted file mode 100644 index f56aef7..0000000 --- a/src/mangling/java.h +++ /dev/null @@ -1,59 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * java.h - prototypes pour le décodage des noms d'éléments selon Java - * - * Copyright (C) 2010-2017 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 Chrysalide. If not, see . - */ - - -#ifndef _FORMAT_MANGLING_JAVA_H -#define _FORMAT_MANGLING_JAVA_H - - -#include - - -#include "context.h" -#include "demangler.h" - - - -/* ------------------------- CONTEXTE POUR LE DECODAGE JAVA ------------------------- */ - - -/* Contexte de décodage Java (instance) */ -typedef struct _GJavaDContext GJavaDContext; - -/* Contexte de décodage Java (classe) */ -typedef struct _GJavaDContextClass GJavaDContextClass; - - -/* Prépare de quoi effectuer un décodage Java. */ -GDemanglingContext *g_java_dcontext_new(void); - - - -/* ----------------------- TRAITEMENT DE L'ENCODAGE TYPE JAVA ----------------------- */ - - -/* Procède au décodage d'une chaîne de caractères. */ -bool demangle_java_type(GJavaDContext *, const char *); - - - -#endif /* _FORMAT_MANGLING_JAVA_H */ diff --git a/src/mangling/java_gram.y b/src/mangling/java_gram.y deleted file mode 100644 index 9f167c2..0000000 --- a/src/mangling/java_gram.y +++ /dev/null @@ -1,279 +0,0 @@ - -%{ - - - -#include "context-int.h" -#include "java.h" -#include "../analysis/types/cse.h" - - - -/* ------------------------- CONTEXTE POUR LE DECODAGE JAVA ------------------------- */ - - -#define G_TYPE_JAVA_DCONTEXT g_java_dcontext_get_type() -#define G_JAVA_DCONTEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_java_dcontext_get_type(), GJavaDContext)) -#define G_IS_JAVA_DCONTEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_java_dcontext_get_type())) -#define G_JAVA_DCONTEXT_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), g_java_dcontext_get_type(), GJavaDContextIface)) -#define G_JAVA_DCONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_JAVA_DCONTEXT, GJavaDContextClass)) - - -/* Contexte de décodage Java (instance) */ -struct _GJavaDContext -{ - GDemanglingContext parent; /* A laisser en premier */ - -}; - -/* Contexte de décodage Java (classe) */ -struct _GJavaDContextClass -{ - GDemanglingContextClass parent; /* A laisser en premier */ - -}; - - -/* Indique le type défini pour un contexte de décodage. */ -static GType g_java_dcontext_get_type(void); - -/* Initialise la classe des contextes de décodage. */ -static void g_java_dcontext_class_init(GJavaDContextClass *); - -/* Initialise une instance de contexte pour décodage. */ -static void g_java_dcontext_init(GJavaDContext *); - -/* Affiche un message d'erreur concernant l'analyse. */ -static int java_error(/*const YYLTYPE *yyloc, bool hunt, char **ucode, unsigned char *index, */GJavaDContext *, char *); - - - - - - -%} - - -%union { - - void /*GDataType*/ *type; - - size_t adeep; /* Dimension d'un tableau */ - - char *text; /* Chaîne de caractères */ - -} - -%parse-param { GJavaDContext *context } - -%token V Z B S C I J F D -%token ARRAY -%token L SEMICOLON -%token SLASH DOLLAR -%token TEXT - -%type type_descriptor field_type_descriptor non_array_field_type_descriptor full_class_name - -%type TEXT - - -%{ - -/* De lexi.c... */ -/*int yylex(YYSTYPE *, YYLTYPE *);*/ -void java_restart(FILE *); -typedef struct yy_buffer_state *YY_BUFFER_STATE; -extern YY_BUFFER_STATE java__scan_string(const char *); -extern void java__delete_buffer(YY_BUFFER_STATE); - - extern int java_lex (void); - - -/* Affiche un message d'erreur concernant l'analyse. */ -/*int yyerror(const YYLTYPE *, bool, char **, unsigned char *, char *);*/ - -%} - - -%% - - -input: - type_descriptor { G_DEMANGLING_CONTEXT(context)->type = $1; } - ; - -type_descriptor: - V { $$ = g_basic_type_new(BTP_VOID); } - | field_type_descriptor { $$ = $1; } - ; - -field_type_descriptor: - non_array_field_type_descriptor { $$ = $1; } - | ARRAY non_array_field_type_descriptor { $$ = $2; } - ; - -non_array_field_type_descriptor: - Z { $$ = g_basic_type_new(BTP_BOOL); } - | B { $$ = g_basic_type_new(BTP_UCHAR); } - | S { $$ = g_basic_type_new(BTP_SHORT); } - | C { $$ = g_basic_type_new(BTP_CHAR); } - | I { $$ = g_basic_type_new(BTP_INT); } - | J { $$ = g_basic_type_new(BTP_LONG); } - | F { $$ = g_basic_type_new(BTP_FLOAT); } - | D { $$ = g_basic_type_new(BTP_DOUBLE); } - | L full_class_name SEMICOLON { $$ = $2; } - ; - -full_class_name: - TEXT { $$ = g_class_enum_type_new(CET_CLASS, $1); } - | full_class_name SLASH TEXT { - $$ = g_class_enum_type_new(CET_CLASS, $3); - g_data_type_set_namespace($$, $1, "."); - } - | full_class_name DOLLAR TEXT { - $$ = g_class_enum_type_new(CET_CLASS, $3); - g_data_type_set_namespace($$, $1, "."); - } - ; - - -%% - - - -/* ---------------------------------------------------------------------------------- */ -/* CONTEXTE POUR LE DECODAGE JAVA */ -/* ---------------------------------------------------------------------------------- */ - - -/* Indique le type défini pour un contexte de décodage. */ -G_DEFINE_TYPE(GJavaDContext, g_java_dcontext, G_TYPE_DEMANGLING_CONTEXT); - - -/****************************************************************************** -* * -* Paramètres : klass = classe à initialiser. * -* * -* Description : Initialise la classe des contextes de décodage. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_java_dcontext_class_init(GJavaDContextClass *klass) -{ - -} - - -/****************************************************************************** -* * -* Paramètres : context = instance à initialiser. * -* * -* Description : Initialise une instance de contexte pour décodage. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_java_dcontext_init(GJavaDContext *context) -{ - -} - - -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Prépare de quoi effectuer un décodage Java. * -* * -* Retour : Instance du contexte mis en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -GDemanglingContext *g_java_dcontext_new(void) -{ - GDemanglingContext *result; /* Structure à retourner */ - - result = g_object_new(G_TYPE_JAVA_DCONTEXT, NULL); - - return result; - -} - - - -/* ---------------------------------------------------------------------------------- */ -/* TRAITEMENT DE L'ENCODAGE TYPE JAVA */ -/* ---------------------------------------------------------------------------------- */ - - - - - - - - - - - - - -/** - * Affiche un message d'erreur concernant l'analyse. - * @param yyloc informations concernant les coordonnées du soucis. - * @param hunt indique le type de passe réalisée. - * @param ucode code résultant compilé. - * @param index indice de commande à mettre à jour. - * @param msg indications humaines sur l'événement. - * @return 0. - */ -static int java_error(/*const YYLTYPE *yyloc, bool hunt, char **ucode, unsigned char *index, */GJavaDContext *context, char *msg) -{ - - - - fprintf(stderr, "ERREUR !\n"); - fprintf(stderr, "%s\n", msg); - - return -1; - -} - - - - - - -/****************************************************************************** -* * -* Paramètres : context = contexte de décodage à utiliser. * -* desc = chaîne de caractères à décoder. * -* * -* Description : Procède au décodage d'une chaîne de caractères. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool demangle_java_type(GJavaDContext *context, const char *desc) -{ - YY_BUFFER_STATE buffer; /* Tampon pour bison */ - int ret; /* Bilan de l'appel */ - - buffer = java__scan_string(desc); - ret = yyparse(context); - java__delete_buffer(buffer); - - return (ret == 0); - -} diff --git a/src/mangling/java_tok.l b/src/mangling/java_tok.l deleted file mode 100644 index 4adf623..0000000 --- a/src/mangling/java_tok.l +++ /dev/null @@ -1,37 +0,0 @@ - -%{ - -#include "java.h" -#include "libjavamangling_la-java_gram.h" - -%} - - -%option noyywrap -%option yylineno -%option nounput -%option noinput - -%x string - - -%% - -V { return V; } -Z { return Z; } -B { return B; } -S { return S; } -C { return C; } -I { return I; } -J { return J; } -F { return F; } -D { return D; } -L { BEGIN(string); return L; } -[[]* { java_lval.adeep = strlen(yytext); return ARRAY; } -[/] { return SLASH; } -[$] { return DOLLAR; } -[;] { BEGIN(INITIAL); return SEMICOLON; } - -[A-Za-z0-9]* { java_lval.text = yytext; return TEXT; } - -%% diff --git a/tests/mangling/java.py b/tests/mangling/java.py new file mode 100644 index 0000000..072852f --- /dev/null +++ b/tests/mangling/java.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3-dbg +# -*- coding: utf-8 -*- + + +# Tests validant le décodage des types et des routines pour le format Java + + +from chrysacase import ChrysalideTestCase +from pychrysalide.mangling import JavaDemangler + + +class TestJavaMangling(ChrysalideTestCase): + """TestCase for pychrysalide.mangling.JavaDemangler.""" + + def testJavaSpecMangling(self): + """Check Java demangling samples from specifications.""" + + demangler = JavaDemangler() + + demangled = demangler.decode_type('I') + self.assertEqual(str(demangled), 'int') + + demangled = demangler.decode_type('Ljava/lang/Object;') + self.assertEqual(str(demangled), 'java.lang.Object') + + demangled = demangler.decode_type('[[[D') + self.assertEqual(str(demangled), 'double [][][]') + + demangled = demangler.decode_routine('(IDLjava/lang/Thread;)Ljava/lang/Object;') + self.assertEqual(str(demangled), 'java.lang.Object (int, double, java.lang.Thread)') -- cgit v0.11.2-87-g4458