summaryrefslogtreecommitdiff
path: root/src/format/mangling/dex
diff options
context:
space:
mode:
Diffstat (limited to 'src/format/mangling/dex')
-rw-r--r--src/format/mangling/dex/Makefile.am46
-rw-r--r--src/format/mangling/dex/context.c154
-rw-r--r--src/format/mangling/dex/context.h52
-rw-r--r--src/format/mangling/dex/shorty_gram.y138
-rw-r--r--src/format/mangling/dex/shorty_tok.l28
-rw-r--r--src/format/mangling/dex/type_gram.y149
-rw-r--r--src/format/mangling/dex/type_tok.l37
7 files changed, 604 insertions, 0 deletions
diff --git a/src/format/mangling/dex/Makefile.am b/src/format/mangling/dex/Makefile.am
new file mode 100644
index 0000000..bdcbf8c
--- /dev/null
+++ b/src/format/mangling/dex/Makefile.am
@@ -0,0 +1,46 @@
+
+BUILT_SOURCES = libformatmanglingdexshorty_la-shorty_gram.h libformatmanglingdextype_la-type_gram.h
+
+AM_YFLAGS = -d
+
+noinst_LTLIBRARIES = libformatmanglingdex.la libformatmanglingdexshorty.la libformatmanglingdextype.la
+
+libformatmanglingdex_la_SOURCES = \
+ context.h context.c
+
+libformatmanglingdex_la_LDFLAGS =
+
+libformatmanglingdex_la_LIBADD = \
+ libformatmanglingdexshorty.la \
+ libformatmanglingdextype.la
+
+
+libformatmanglingdexshorty_la_SOURCES = \
+ shorty_gram.y \
+ shorty_tok.l
+
+libformatmanglingdexshorty_la_YFLAGS = -d -p shorty_ -o y.tab.c
+
+libformatmanglingdexshorty_la_LFLAGS = -P shorty_ -o lex.yy.c
+
+
+libformatmanglingdextype_la_SOURCES = \
+ type_gram.y \
+ type_tok.l
+
+libformatmanglingdextype_la_YFLAGS = -d -p type_ -o y.tab.c
+
+libformatmanglingdextype_la_LFLAGS = -P type_ -o lex.yy.c
+
+
+AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS)
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS)
+
+
+# Automake fait les choses à moitié
+CLEANFILES = \
+ libformatmanglingdexshorty_la-shorty_gram.h libformatmanglingdexshorty_la-shorty_gram.c \
+ libformatmanglingdexshorty_la-shorty_tok.c \
+ libformatmanglingdextype_la-type_gram.h libformatmanglingdextype_la-type_gram.c \
+ libformatmanglingdextype_la-type_tok.c
diff --git a/src/format/mangling/dex/context.c b/src/format/mangling/dex/context.c
new file mode 100644
index 0000000..6fc12d4
--- /dev/null
+++ b/src/format/mangling/dex/context.c
@@ -0,0 +1,154 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * context.c - contextes de décodage DEX
+ *
+ * Copyright (C) 2015 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA 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.
+ *
+ * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "context.h"
+
+
+#include "../context-int.h"
+
+
+
+/* Contexte de décodage DEX (instance) */
+struct _GDexDemangler
+{
+ GDemanglingContext parent; /* A laisser en premier */
+
+};
+
+/* Contexte de décodage DEX (classe) */
+struct _GDexDemanglerClass
+{
+ GDemanglingContextClass parent; /* A laisser en premier */
+
+};
+
+
+/* Initialise la classe des contextes de décodage DEX. */
+static void g_dex_demangler_class_init(GDexDemanglerClass *);
+
+/* Initialise une instance de contexte pour décodage DEX. */
+static void g_dex_demangler_init(GDexDemangler *);
+
+/* Supprime toutes les références externes. */
+static void g_dex_demangler_dispose(GDexDemangler *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_dex_demangler_finalize(GDexDemangler *);
+
+
+/* Procède au décodage d'une chaîne de caractères. */
+extern bool demangle_dex_routine(GDexDemangler *, const char *);
+
+/* Procède au décodage d'une chaîne de caractères. */
+extern bool demangle_dex_type(GDexDemangler *, const char *);
+
+
+
+/* Indique le type défini pour un contexte de décodage DEX. */
+G_DEFINE_TYPE(GDexDemangler, g_dex_demangler, G_TYPE_DEMANGLING_CONTEXT);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des contextes de décodage DEX. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_dex_demangler_class_init(GDexDemanglerClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+ GDemanglingContextClass *context; /* Version parente */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_dex_demangler_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_dex_demangler_finalize;
+
+ context = G_DEMANGLING_CONTEXT_CLASS(klass);
+
+ context->demangle_type = (demangle_fc)demangle_dex_type;
+ context->demangle_routine = (demangle_fc)demangle_dex_routine;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = instance à initialiser. *
+* *
+* Description : Initialise une instance de contexte pour décodage DEX. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_dex_demangler_init(GDexDemangler *demangler)
+{
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_dex_demangler_dispose(GDexDemangler *demangler)
+{
+ G_OBJECT_CLASS(g_dex_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_dex_demangler_finalize(GDexDemangler *demangler)
+{
+ G_OBJECT_CLASS(g_dex_demangler_parent_class)->finalize(G_OBJECT(demangler));
+
+}
diff --git a/src/format/mangling/dex/context.h b/src/format/mangling/dex/context.h
new file mode 100644
index 0000000..6242b61
--- /dev/null
+++ b/src/format/mangling/dex/context.h
@@ -0,0 +1,52 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * context.h - prototypes internes liés aux contextes de décodage DEX
+ *
+ * Copyright (C) 2015 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA 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.
+ *
+ * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _FORMAT_MANGLING_DEX_CONTEXT_H
+#define _FORMAT_MANGLING_DEX_CONTEXT_H
+
+
+#include <glib-object.h>
+
+
+
+#define G_TYPE_DEX_DEMANGLER g_dex_demangler_get_type()
+#define G_DEX_DEMANGLER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_dex_demangler_get_type(), GDexDemangler))
+#define G_IS_DEX_DEMANGLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_dex_demangler_get_type()))
+#define G_DEX_DEMANGLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_DEX_DEMANGLER, GDexDemanglerClass))
+#define G_IS_DEX_DEMANGLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_DEX_DEMANGLER))
+#define G_DEX_DEMANGLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_DEX_DEMANGLER, GDexDemanglerClass))
+
+
+/* Contexte de décodage DEX (instance) */
+typedef struct _GDexDemangler GDexDemangler;
+
+/* Contexte de décodage DEX (classe) */
+typedef struct _GDexDemanglerClass GDexDemanglerClass;
+
+
+/* Indique le type défini pour un contexte de décodage DEX. */
+GType g_dex_demangler_get_type(void);
+
+
+
+#endif /* _FORMAT_MANGLING_DEX_CONTEXT_H */
diff --git a/src/format/mangling/dex/shorty_gram.y b/src/format/mangling/dex/shorty_gram.y
new file mode 100644
index 0000000..55d849d
--- /dev/null
+++ b/src/format/mangling/dex/shorty_gram.y
@@ -0,0 +1,138 @@
+
+%{
+
+#include <stdbool.h>
+
+
+#include "context.h"
+#include "../context-int.h"
+
+
+
+/* Affiche un message d'erreur concernant l'analyse. */
+static int shorty_error(GDexDemangler *, char *);
+
+/* Procède au décodage d'une chaîne de caractères. */
+bool demangle_dex_routine(GDexDemangler *, const char *);
+
+
+%}
+
+
+%code requires {
+
+#include "../../../analysis/types/basic.h"
+#include "../../../analysis/types/cse.h"
+
+}
+
+%union {
+
+ GDataType *type; /* Type reconstruit */
+
+}
+
+
+%parse-param { GDexDemangler *demangler }
+
+%token V Z B S C I J F D L
+
+%type <type> shorty_return_type shorty_field_type
+
+
+%{
+
+/* Déclarations issues de l'analyseur syntaxique... */
+
+typedef struct yy_buffer_state *YY_BUFFER_STATE;
+
+extern YY_BUFFER_STATE shorty__scan_string(const char *);
+extern void shorty__delete_buffer(YY_BUFFER_STATE);
+extern int shorty_lex(void);
+
+%}
+
+
+%%
+
+
+shorty_descriptor:
+ shorty_return_type shorty_field_type_list {
+ GBinRoutine *routine;
+ routine = G_DEMANGLING_CONTEXT(demangler)->routine;
+ g_binary_routine_set_return_type(routine, $1);
+ }
+
+shorty_field_type_list:
+ /* empty */
+ | shorty_field_type shorty_field_type_list {
+ GBinRoutine *routine;
+ GBinVariable *var;
+ routine = G_DEMANGLING_CONTEXT(demangler)->routine;
+ var = g_binary_variable_new($1);
+ g_binary_routine_add_arg(routine, var);
+ }
+
+shorty_return_type:
+ V { $$ = g_basic_type_new(BTP_VOID); }
+ | shorty_field_type { $$ = $1; }
+
+shorty_field_type:
+ 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 { $$ = g_class_enum_type_new(CET_CLASS, ""); }
+
+
+%%
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = contexte associé à la procédure de décodage. *
+* msg = indications humaines sur l'événement. *
+* *
+* Description : Affiche un message d'erreur concernant l'analyse. *
+* *
+* Retour : Valeur historique, ignorée. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+static int shorty_error(GDexDemangler *demangler, char *msg)
+{
+ return -1;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = 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_dex_routine(GDexDemangler *demangler, const char *desc)
+{
+ YY_BUFFER_STATE buffer; /* Tampon pour bison */
+ int ret; /* Bilan de l'appel */
+
+ buffer = shorty__scan_string(desc);
+ ret = yyparse(demangler);
+ shorty__delete_buffer(buffer);
+
+ return (ret == 0);
+
+}
diff --git a/src/format/mangling/dex/shorty_tok.l b/src/format/mangling/dex/shorty_tok.l
new file mode 100644
index 0000000..92c264a
--- /dev/null
+++ b/src/format/mangling/dex/shorty_tok.l
@@ -0,0 +1,28 @@
+
+%{
+
+#include "context.h"
+#include "libformatmanglingdexshorty_la-shorty_gram.h"
+
+%}
+
+
+%option noyywrap
+%option yylineno
+%option nounput
+%option noinput
+
+%%
+
+"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" { return L; }
+
+%%
diff --git a/src/format/mangling/dex/type_gram.y b/src/format/mangling/dex/type_gram.y
new file mode 100644
index 0000000..1176bd2
--- /dev/null
+++ b/src/format/mangling/dex/type_gram.y
@@ -0,0 +1,149 @@
+
+%{
+
+#include <stdbool.h>
+
+
+#include "context.h"
+#include "../context-int.h"
+
+
+
+/* Affiche un message d'erreur concernant l'analyse. */
+static int type_error(GDexDemangler *, char *);
+
+/* Procède au décodage d'une chaîne de caractères. */
+bool demangle_dex_type(GDexDemangler *, const char *);
+
+
+%}
+
+
+%code requires {
+
+#include "../../../analysis/types/basic.h"
+#include "../../../analysis/types/cse.h"
+
+}
+
+%union {
+
+ GDataType *type; /* Type reconstruit */
+ size_t adeep; /* Dimension d'un tableau */
+ char *text; /* Chaîne de caractères */
+
+}
+
+
+%parse-param { GDexDemangler *demangler }
+
+%token V Z B S C I J F D
+%token ARRAY
+%token L SEMICOLON
+%token SLASH DOLLAR
+%token TEXT
+
+%type <type> type_descriptor field_type_descriptor non_array_field_type_descriptor full_class_name
+
+%type <text> TEXT
+
+
+%{
+
+/* Déclarations issues de l'analyseur syntaxique... */
+
+typedef struct yy_buffer_state *YY_BUFFER_STATE;
+
+extern YY_BUFFER_STATE type__scan_string(const char *);
+extern void type__delete_buffer(YY_BUFFER_STATE);
+extern int type_lex(void);
+
+%}
+
+
+%%
+
+
+input:
+ type_descriptor { G_DEMANGLING_CONTEXT(demangler)->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);
+ g_object_unref($1);
+ }
+ | full_class_name DOLLAR TEXT {
+ $$ = g_class_enum_type_new(CET_CLASS, $3);
+ g_data_type_set_namespace($$, $1);
+ g_object_unref($1);
+ }
+
+
+%%
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = contexte associé à la procédure de décodage. *
+* msg = indications humaines sur l'événement. *
+* *
+* Description : Affiche un message d'erreur concernant l'analyse. *
+* *
+* Retour : Valeur historique, ignorée. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+static int type_error(GDexDemangler *demangler, char *msg)
+{
+ return -1;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : demangler = 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_dex_type(GDexDemangler *demangler, const char *desc)
+{
+ YY_BUFFER_STATE buffer; /* Tampon pour bison */
+ int ret; /* Bilan de l'appel */
+
+ buffer = type__scan_string(desc);
+ ret = yyparse(demangler);
+ type__delete_buffer(buffer);
+
+ return (ret == 0);
+
+}
diff --git a/src/format/mangling/dex/type_tok.l b/src/format/mangling/dex/type_tok.l
new file mode 100644
index 0000000..7b8a8d3
--- /dev/null
+++ b/src/format/mangling/dex/type_tok.l
@@ -0,0 +1,37 @@
+
+%{
+
+#include "context.h"
+#include "libformatmanglingdextype_la-type_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; }
+"["* { type_lval.adeep = strlen(yytext); return ARRAY; }
+<string>"/" { return SLASH; }
+<string>"$" { return DOLLAR; }
+<string>";" { BEGIN(INITIAL); return SEMICOLON; }
+
+<string>[A-Za-z0-9_-]* { type_lval.text = yytext; return TEXT; }
+
+
+%%