summaryrefslogtreecommitdiff
path: root/tools/d2c/format
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2016-01-30 17:23:16 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2016-01-30 17:23:16 (GMT)
commit792b330c1bbe573a591687d25e14d4cd1eccd3c6 (patch)
tree9a3e0955205c0221ea5db03b1f3a4d56846efed2 /tools/d2c/format
parentc922b6b9c841a97dd948755658cff07b2c6762ff (diff)
Introduced a format switch to distinguish different kinds of definitions.
Diffstat (limited to 'tools/d2c/format')
-rw-r--r--tools/d2c/format/Makefile.am31
-rw-r--r--tools/d2c/format/decl.h37
-rw-r--r--tools/d2c/format/grammar.y107
-rw-r--r--tools/d2c/format/manager.c145
-rw-r--r--tools/d2c/format/manager.h50
-rw-r--r--tools/d2c/format/tokens.l26
6 files changed, 396 insertions, 0 deletions
diff --git a/tools/d2c/format/Makefile.am b/tools/d2c/format/Makefile.am
new file mode 100644
index 0000000..7afd98d
--- /dev/null
+++ b/tools/d2c/format/Makefile.am
@@ -0,0 +1,31 @@
+
+BUILT_SOURCES = grammar.h
+
+
+# On évite d'utiliser les variables personnalisées de type *_la_[YL]FLAGS
+# afin de conserver des noms de fichiers simples, ie sans le nom de la
+# bibliothèque de sortie en préfixe.
+
+AM_YFLAGS = -v -d -p format_
+
+AM_LFLAGS = -P format_ -o lex.yy.c --header-file=tokens.h \
+ -Dyylval=format_lval -Dyyget_lineno=format_get_lineno \
+ -Dyy_scan_string=format__scan_string \
+ -Dyy_delete_buffer=format__delete_buffer
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
+
+
+noinst_LTLIBRARIES = libd2cformat.la
+
+.NOTPARALLEL: $(noinst_LTLIBRARIES)
+
+libd2cformat_la_SOURCES = \
+ decl.h \
+ manager.h manager.c \
+ tokens.l \
+ grammar.y
+
+
+# Automake fait les choses à moitié
+CLEANFILES = grammar.h grammar.c grammar.output tokens.c tokens.h
diff --git a/tools/d2c/format/decl.h b/tools/d2c/format/decl.h
new file mode 100644
index 0000000..0b7775a
--- /dev/null
+++ b/tools/d2c/format/decl.h
@@ -0,0 +1,37 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * decl.h - déclarations de prototypes utiles
+ *
+ * Copyright (C) 2016 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 _TOOLS_D2C_FORMAT_DECL_H
+#define _TOOLS_D2C_FORMAT_DECL_H
+
+
+#include "manager.h"
+
+
+
+/* Interprête des données relatives à une définition de format. */
+bool load_format_from_raw_line(operands_format *, const char *);
+
+
+
+#endif /* _TOOLS_D2C_BITS_DECL_H */
diff --git a/tools/d2c/format/grammar.y b/tools/d2c/format/grammar.y
new file mode 100644
index 0000000..062d616
--- /dev/null
+++ b/tools/d2c/format/grammar.y
@@ -0,0 +1,107 @@
+
+%{
+
+#include "tokens.h"
+
+
+/* Affiche un message d'erreur suite à l'analyse en échec. */
+static int yyerror(operands_format *, char *);
+
+%}
+
+
+%code requires {
+
+#include "decl.h"
+
+}
+
+
+%union {
+
+ char *string; /* Chaîne de caractères */
+
+}
+
+
+%define api.pure full
+
+%parse-param { operands_format *format }
+
+%code provides {
+
+#define YY_DECL \
+ int format_lex(YYSTYPE *yylvalp)
+
+YY_DECL;
+
+}
+
+
+%token OPS_TYPE
+
+%type <string> OPS_TYPE
+
+
+
+%%
+
+
+type : OPS_TYPE { set_operands_format_type(format, $1); }
+
+
+%%
+
+
+/******************************************************************************
+* *
+* Paramètres : format = structure impliquée dans le processus. *
+* msg = message d'erreur. *
+* *
+* Description : Affiche un message d'erreur suite à l'analyse en échec. *
+* *
+* Retour : 0 *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static int yyerror(operands_format *format, char *msg)
+{
+ printf("bits yyerror line %d: %s\n", yyget_lineno(), msg);
+
+ return 0;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : format = structure à constituer à partir de données lues. *
+* raw = données brutes à analyser. *
+* *
+* Description : Interprête des données relatives à une définition de format. *
+* *
+* Retour : true si l'opération s'est bien déroulée, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool load_format_from_raw_line(operands_format *format, const char *raw)
+{
+ bool result; /* Bilan à faire remonter */
+ YY_BUFFER_STATE state; /* Support d'analyse */
+ int status; /* Bilan de l'analyse */
+
+ state = yy_scan_string(raw);
+
+ status = yyparse(format);
+
+ result = (status == 0);
+
+ yy_delete_buffer(state);
+
+ return result;
+
+}
diff --git a/tools/d2c/format/manager.c b/tools/d2c/format/manager.c
new file mode 100644
index 0000000..070b70c
--- /dev/null
+++ b/tools/d2c/format/manager.c
@@ -0,0 +1,145 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * manager.c - enregistrement de la définition des opérandes attendus
+ *
+ * Copyright (C) 2014 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 "manager.h"
+
+
+#include <malloc.h>
+
+
+#include "../helpers.h"
+
+
+
+/* Mémorisation de la définition d'opérandes */
+struct _operands_format
+{
+ char *type; /* Définitions des opérandes */
+
+};
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Crée un nouveau gestionnaire de définitions d'opérandes. *
+* *
+* Retour : Nouvelle structure prête à emploi. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+operands_format *create_operands_format(void)
+{
+ operands_format *result; /* Définition vierge à renvoyer*/
+
+ result = (operands_format *)calloc(1, sizeof(operands_format));
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : format = gestionnaire de définition d'opérandes à libérer. *
+* *
+* Description : Supprime de la mémoire un gestionnaire de définitions. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void delete_operands_format(operands_format *format)
+{
+ if (format->type != NULL)
+ free(format->type);
+
+ free(format);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : format = gestionnaire de définition d'opérandes à traiter. *
+* type = définitions des opérandes à charger. *
+* *
+* Description : Précise le type d'opérandes dont la définition est à charger.*
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void set_operands_format_type(operands_format *format, char *type)
+{
+ if (format->type != NULL)
+ free(format->type);
+
+ format->type = make_string_upper(type);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : spec = spécification servant de base à l'opération. *
+* fd = descripteur d'un flux ouvert en écriture. *
+* arch = architecture visée par l'opération. *
+* prefix = préfixe pour le type de définitions d'opérandes. *
+* exit = exprime le besoin d'une voie de sortie. [OUT] *
+* *
+* Description : Définit le chargement des opérandes prévus par la définition.*
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool define_operands_loading(const operands_format *format, int fd, const char *arch, const char *prefix, bool *exit)
+{
+ if (format->type == NULL)
+ {
+ fprintf(stderr, "Error: no type defined for operands.\n");
+ return false;
+ }
+
+ *exit = true;
+
+ dprintf(fd, "\tif (!%s_read_operands(result, format, content, pos, endian, %s%s))\n",
+ arch, prefix, format->type);
+
+ dprintf(fd, "\t\tgoto bad_exit;\n");
+ dprintf(fd, "\n");
+
+ return true;
+
+}
diff --git a/tools/d2c/format/manager.h b/tools/d2c/format/manager.h
new file mode 100644
index 0000000..4555710
--- /dev/null
+++ b/tools/d2c/format/manager.h
@@ -0,0 +1,50 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * manager.h - prototypes pour l'enregistrement de la définition des opérandes attendus
+ *
+ * Copyright (C) 2014 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 _TOOLS_D2C_FORMAT_MANAGER_H
+#define _TOOLS_D2C_FORMAT_MANAGER_H
+
+
+#include <stdbool.h>
+
+
+
+/* Mémorisation de la définition d'opérandes */
+typedef struct _operands_format operands_format;
+
+
+/* Crée un nouveau gestionnaire de définitions d'opérandes. */
+operands_format *create_operands_format(void);
+
+/* Supprime de la mémoire un gestionnaire de définitions. */
+void delete_operands_format(operands_format *);
+
+/* Précise le type d'opérandes dont la définition est à charger. */
+void set_operands_format_type(operands_format *, char *);
+
+/* Définit le chargement des opérandes prévus par la définition. */
+bool define_operands_loading(const operands_format *, int, const char *, const char *, bool *);
+
+
+
+#endif /* _TOOLS_D2C_FORMAT_MANAGER_H */
diff --git a/tools/d2c/format/tokens.l b/tools/d2c/format/tokens.l
new file mode 100644
index 0000000..12ca7f0
--- /dev/null
+++ b/tools/d2c/format/tokens.l
@@ -0,0 +1,26 @@
+
+%top {
+
+#include "grammar.h"
+
+}
+
+
+%option noyywrap
+%option nounput
+%option noinput
+%option yylineno
+%option noyy_top_state
+
+%x bsize
+
+
+%%
+
+
+" " { }
+
+[A-Za-z0-9_]* { yylvalp->string = strdup(yytext); return OPS_TYPE; }
+
+
+%%