diff options
| author | Cyrille Bagard <nocbos@gmail.com> | 2018-01-30 20:53:24 (GMT) | 
|---|---|---|
| committer | Cyrille Bagard <nocbos@gmail.com> | 2018-01-30 20:53:24 (GMT) | 
| commit | 4132223466dcabc19a1b6f0178adf1c1a35b07ad (patch) | |
| tree | ad5ef2262e8e1855c9c7f23a0f11f560039d54c6 /src/mangling/dex | |
| parent | 896b31fbbef2fba442566a422fa4d409771b61dd (diff) | |
Moved the mangling related code.
Diffstat (limited to 'src/mangling/dex')
| -rw-r--r-- | src/mangling/dex/Makefile.am | 46 | ||||
| -rw-r--r-- | src/mangling/dex/context.c | 154 | ||||
| -rw-r--r-- | src/mangling/dex/context.h | 52 | ||||
| -rw-r--r-- | src/mangling/dex/shorty_gram.y | 138 | ||||
| -rw-r--r-- | src/mangling/dex/shorty_tok.l | 28 | ||||
| -rw-r--r-- | src/mangling/dex/type_gram.y | 159 | ||||
| -rw-r--r-- | src/mangling/dex/type_tok.l | 150 | 
7 files changed, 727 insertions, 0 deletions
| diff --git a/src/mangling/dex/Makefile.am b/src/mangling/dex/Makefile.am new file mode 100644 index 0000000..547d686 --- /dev/null +++ b/src/mangling/dex/Makefile.am @@ -0,0 +1,46 @@ + +BUILT_SOURCES = libmanglingdexshorty_la-shorty_gram.h libmanglingdextype_la-type_gram.h + +AM_YFLAGS = -d + +noinst_LTLIBRARIES = libmanglingdex.la libmanglingdexshorty.la libmanglingdextype.la + +libmanglingdex_la_SOURCES =				\ +	context.h context.c + +libmanglingdex_la_LDFLAGS =  + +libmanglingdex_la_LIBADD =				\ +	libmanglingdexshorty.la				\ +	libmanglingdextype.la + + +libmanglingdexshorty_la_SOURCES =		\ +	shorty_gram.y						\ +	shorty_tok.l + +libmanglingdexshorty_la_YFLAGS = -d -p shorty_ -o y.tab.c + +libmanglingdexshorty_la_LFLAGS = -P shorty_ -o lex.yy.c + + +libmanglingdextype_la_SOURCES =			\ +	type_gram.y							\ +	type_tok.l + +libmanglingdextype_la_YFLAGS = -d -p type_ -o y.tab.c + +libmanglingdextype_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 = \ +	libmanglingdexshorty_la-shorty_gram.h libmanglingdexshorty_la-shorty_gram.c \ +	libmanglingdexshorty_la-shorty_tok.c \ +	libmanglingdextype_la-type_gram.h libmanglingdextype_la-type_gram.c \ +	libmanglingdextype_la-type_tok.c diff --git a/src/mangling/dex/context.c b/src/mangling/dex/context.c new file mode 100644 index 0000000..9dd9b9c --- /dev/null +++ b/src/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-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 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/mangling/dex/context.h b/src/mangling/dex/context.h new file mode 100644 index 0000000..1af7a9d --- /dev/null +++ b/src/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-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 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/mangling/dex/shorty_gram.y b/src/mangling/dex/shorty_gram.y new file mode 100644 index 0000000..b688896 --- /dev/null +++ b/src/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/mangling/dex/shorty_tok.l b/src/mangling/dex/shorty_tok.l new file mode 100644 index 0000000..92c264a --- /dev/null +++ b/src/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/mangling/dex/type_gram.y b/src/mangling/dex/type_gram.y new file mode 100644 index 0000000..79c9320 --- /dev/null +++ b/src/mangling/dex/type_gram.y @@ -0,0 +1,159 @@ + +%{ + +#include <stdbool.h> + + +#include "context.h" +#include "../context-int.h" + +typedef void *yyscan_t; + +/* Affiche un message d'erreur concernant l'analyse. */ +static int type_error(GDexDemangler *, yyscan_t, 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" +#include "../../common/extstr.h" + +} + +%union { + +    GDataType *type;                        /* Type reconstruit            */ +    size_t adeep;                           /* Dimension d'un tableau      */ +    char *text;                             /* Chaîne de caractères        */ + +} + + + +%define api.pure full +%parse-param { GDexDemangler *demangler } { yyscan_t scanner } +%lex-param { yyscan_t scanner } + + +%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 simple_name + + +%{ + +/* Déclarations issues de l'analyseur syntaxique... */ + +typedef struct yy_buffer_state *YY_BUFFER_STATE; + +extern int type_lex_init(yyscan_t *scanner); +extern YY_BUFFER_STATE type__scan_string(const char *, yyscan_t); +extern void type__delete_buffer(YY_BUFFER_STATE, yyscan_t); +extern int type_lex(YYSTYPE *, yyscan_t); +extern int type_lex_destroy(yyscan_t); + +%} + + +%% + + +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: +    simple_name                         { $$ = g_class_enum_type_new(CET_CLASS, $1); } +    | full_class_name SLASH simple_name { +                                            $$ = g_class_enum_type_new(CET_CLASS, $3); +                                            g_data_type_set_namespace($$, $1); +                                            g_object_unref($1); +                                        } +simple_name: +    TEXT                                { $$ = strdup($1); } +    | simple_name TEXT                  { $$ = stradd($1, $2); } + +%% + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : demangler = contexte associé à la procédure de décodage.     * +*                scanner   = données internes aux analyseurs.                 * +*                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, yyscan_t scanner, 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) +{ +    yyscan_t scanner;                       /* Données internes            */ +	YY_BUFFER_STATE buffer;                 /* Tampon pour bison           */ +	int ret;                                /* Bilan de l'appel            */ + +    type_lex_init(&scanner); + +    buffer = type__scan_string(desc, scanner); +	ret = yyparse(demangler, scanner); +	type__delete_buffer(buffer, scanner); + +    type_lex_destroy(scanner); + +    return (ret == 0); + +} diff --git a/src/mangling/dex/type_tok.l b/src/mangling/dex/type_tok.l new file mode 100644 index 0000000..918846a --- /dev/null +++ b/src/mangling/dex/type_tok.l @@ -0,0 +1,150 @@ + +%{ + +#include "context.h" +#include "libformatmanglingdextype_la-type_gram.h" + +/* See lemoda.net/c/reentrant-parser */ + +%} + + +%option noyywrap +%option yylineno +%option nounput +/*%option noinput*/ +%option reentrant +%option bison-bridge + +%x string + +ASCII       [A-Za-z0-9] +SIMPLE      {ASCII}|"$"|"-"|"_" + +%% + +"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; } +"["*                    { yylval->adeep = strlen(yytext); return ARRAY; } +<string>"/"             { return SLASH; } +<string>";"             { BEGIN(INITIAL); return SEMICOLON; } + +<string>{SIMPLE}*       { yylval->text = yytext; return TEXT; } + +<string>.               { +                            unsigned char next; +                            char mutf8[4]; + +                            switch ((unsigned char)yytext[0]) +                            { +                                /* U+00a1 ... U+1fff */ +                                case 0x00 ... 0x1f: + +                                    next = input(yyscanner); + +                                    if (yytext[0] == 0x00 && next < 0xa1) +                                    { +                                        REJECT; +                                    } + +                                    else +                                    { +                                        mutf8[0] = yytext[0]; +                                        mutf8[1] = next; +                                        mutf8[2] = '\0'; + +                                        strcpy(yylval->text, mutf8); return TEXT; + +                                    } + +                                    break; + +                                /* U+2010 ... U+2027 / U+2030 ... U+d7ff */ +                                case 0x20: + +                                    next = input(yyscanner); + +                                    switch (next) +                                    { +                                        case 0x10 ... 0x27: +                                        case 0x30 ... 0xff: + +                                            mutf8[0] = yytext[0]; +                                            mutf8[1] = next; +                                            mutf8[2] = '\0'; + +                                            strcpy(yylval->text, mutf8); return TEXT; +                                            break; + +                                        default: +                                            REJECT; +                                            break; + +                                    } + +                                    break; + +                                /* ~ U+2030 ... U+d7ff */ +                                case 0x21 ... 0xd7: + +                                    next = input(yyscanner); + +                                    mutf8[0] = yytext[0]; +                                    mutf8[1] = next; +                                    mutf8[2] = '\0'; + +                                    strcpy(yylval->text, mutf8); return TEXT; +                                    break; + +                                /* U+e000 ... U+ffef */ +                                case 0xe0 ... 0xff: + +                                    next = input(yyscanner); + +                                    if (yytext[0] == 0xff && next > 0xef) +                                    { +                                        REJECT; +                                    } + +                                    else +                                    { +                                        mutf8[0] = yytext[0]; +                                        mutf8[1] = next; +                                        mutf8[2] = '\0'; + +                                        strcpy(yylval->text, mutf8); return TEXT; + +                                    } + +                                    break; + +                                /* U+10000 ... U+10ffff */ +                                /* +                                case 0x10: + +                                    mutf8[0] = yytext[0]; +                                    mutf8[1] = input(yyscanner); +                                    mutf8[2] = input(yyscanner); +                                    mutf8[3] = '\0'; + +                                    strcpy(yylval->text, mutf8); return TEXT; +                                    break; +                                */ + +                                default: +                                    REJECT; +                                    break; + +                            } + +                        } + +%% | 
