summaryrefslogtreecommitdiff
path: root/tools/d2c/id
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-04-02 11:58:42 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-04-02 12:39:30 (GMT)
commit1db4ef323b7a76093356ae76268132f3760e1631 (patch)
treefec36ee0ec1b6b2010b62ca4177edca0e31e2114 /tools/d2c/id
parent1bc80837dde03a32b5ab185067f7bd4c499a9850 (diff)
Rewritten the whole instruction definition format.
Diffstat (limited to 'tools/d2c/id')
-rw-r--r--tools/d2c/id/Makefile.am37
-rw-r--r--tools/d2c/id/decl.h40
-rw-r--r--tools/d2c/id/grammar.y107
-rw-r--r--tools/d2c/id/manager.c126
-rw-r--r--tools/d2c/id/manager.h47
-rw-r--r--tools/d2c/id/tokens.l33
6 files changed, 390 insertions, 0 deletions
diff --git a/tools/d2c/id/Makefile.am b/tools/d2c/id/Makefile.am
new file mode 100644
index 0000000..c3d18b8
--- /dev/null
+++ b/tools/d2c/id/Makefile.am
@@ -0,0 +1,37 @@
+
+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 id_
+
+AM_LFLAGS = -P id_ -o lex.yy.c --header-file=tokens.h \
+ -Dyyget_lineno=id_get_lineno \
+ -Dyy_scan_string=id__scan_string \
+ -Dyy_delete_buffer=id__delete_buffer
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
+
+
+noinst_LTLIBRARIES = libd2cid.la
+
+.NOTPARALLEL: $(noinst_LTLIBRARIES)
+
+libd2cid_la_SOURCES = \
+ decl.h \
+ manager.h manager.c \
+ tokens.l \
+ grammar.y
+
+# _GNU_SOURCE : asprintf
+libd2cid_la_CFLAGS = -D_GNU_SOURCE
+
+
+# Automake fait les choses à moitié
+CLEANFILES = grammar.h grammar.c grammar.output tokens.c tokens.h
+
+# Pareil : de tous les fichiers générés, seule la sortie de Flex saute pour les distributions !
+EXTRA_DIST = tokens.h
diff --git a/tools/d2c/id/decl.h b/tools/d2c/id/decl.h
new file mode 100644
index 0000000..0c3b3e2
--- /dev/null
+++ b/tools/d2c/id/decl.h
@@ -0,0 +1,40 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * decl.h - déclarations de prototypes utiles
+ *
+ * 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _TOOLS_D2C_ID_DECL_H
+#define _TOOLS_D2C_ID_DECL_H
+
+
+#include <stdbool.h>
+
+
+#include "manager.h"
+
+
+
+/* Interprête des données relatives à un identifiant. */
+bool load_id_from_raw_line(instr_id *, const char *);
+
+
+
+#endif /* _TOOLS_D2C_BITS_DECL_H */
diff --git a/tools/d2c/id/grammar.y b/tools/d2c/id/grammar.y
new file mode 100644
index 0000000..4799fcc
--- /dev/null
+++ b/tools/d2c/id/grammar.y
@@ -0,0 +1,107 @@
+
+%{
+
+#include "tokens.h"
+
+
+/* Affiche un message d'erreur suite à l'analyse en échec. */
+static int yyerror(instr_id *, char *);
+
+%}
+
+
+%code requires {
+
+#include "decl.h"
+
+}
+
+
+%union {
+
+ unsigned int value; /* Valeur numérique */
+
+}
+
+
+%define api.pure full
+
+%parse-param { instr_id *id }
+
+%code provides {
+
+#define YY_DECL \
+ int id_lex(YYSTYPE *yylvalp)
+
+YY_DECL;
+
+}
+
+
+%token VALUE
+
+%type <value> VALUE
+
+
+
+%%
+
+
+id : VALUE { set_instruction_id_value(id, $1); }
+
+
+%%
+
+
+/******************************************************************************
+* *
+* Paramètres : id = 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(instr_id *id, char *msg)
+{
+ printf("id yyerror line %d: %s\n", yyget_lineno(), msg);
+
+ return 0;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : id = structure à constituer à partir de données lues. *
+* raw = données brutes à analyser. *
+* *
+* Description : Interprête des données relatives à un identifiant. *
+* *
+* Retour : true si l'opération s'est bien déroulée, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool load_id_from_raw_line(instr_id *id, 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(id);
+
+ result = (status == 0);
+
+ yy_delete_buffer(state);
+
+ return result;
+
+}
diff --git a/tools/d2c/id/manager.c b/tools/d2c/id/manager.c
new file mode 100644
index 0000000..7cfc916
--- /dev/null
+++ b/tools/d2c/id/manager.c
@@ -0,0 +1,126 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * manager.c - enregistrement de la définition d'un identifiant
+ *
+ * Copyright (C) 2016-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 "manager.h"
+
+
+#include <assert.h>
+#include <malloc.h>
+#include <stdbool.h>
+
+
+
+/* Mémorisation de la définition d'un identifiant */
+struct _instr_id
+{
+ unsigned int value; /* Identifiant numérique unique*/
+ bool set; /* Validité de la valeur */
+
+};
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Crée un nouveau gestionnaire de définitions d'identifiant. *
+* *
+* Retour : Nouvelle structure prête à emploi. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+instr_id *create_instruction_id(void)
+{
+ instr_id *result; /* Définition vierge à renvoyer*/
+
+ result = (instr_id *)calloc(1, sizeof(instr_id));
+
+ result->set = false;
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : id = gestionnaire de définition d'identifiant à libérer. *
+* *
+* Description : Supprime de la mémoire un gestionnaire d'identifiant. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void delete_instruction_id(instr_id *id)
+{
+ free(id);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : id = gestionnaire de définition d'identifiant à traiter. *
+* value = valeur à attribuer à une instruction. *
+* *
+* Description : Associe une valeur unique à une instruction. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void set_instruction_id_value(instr_id *id, unsigned int value)
+{
+ id->value = value;
+ id->set = true;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : id = gestionnaire de définition d'identifiant à traiter. *
+* *
+* Description : Associe une valeur unique à une instruction. *
+* *
+* Retour : Valeur attribuée à une instruction. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+unsigned int get_instruction_id_value(const instr_id *id)
+{
+ assert(id->set);
+
+ return id->value;
+
+}
diff --git a/tools/d2c/id/manager.h b/tools/d2c/id/manager.h
new file mode 100644
index 0000000..d24fbd7
--- /dev/null
+++ b/tools/d2c/id/manager.h
@@ -0,0 +1,47 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * manager.h - prototypes pour l'enregistrement de la définition d'un identifiant
+ *
+ * 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _TOOLS_D2C_ID_MANAGER_H
+#define _TOOLS_D2C_ID_MANAGER_H
+
+
+
+/* Mémorisation de la définition d'un identifiant */
+typedef struct _instr_id instr_id;
+
+
+/* Crée un nouveau gestionnaire de définitions d'identifiant. */
+instr_id *create_instruction_id(void);
+
+/* Supprime de la mémoire un gestionnaire d'identifiant. */
+void delete_instruction_id(instr_id *);
+
+/* Associe une valeur unique à une instruction. */
+void set_instruction_id_value(instr_id *, unsigned int);
+
+/* Associe une valeur unique à une instruction. */
+unsigned int get_instruction_id_value(const instr_id *);
+
+
+
+#endif /* _TOOLS_D2C_ID_MANAGER_H */
diff --git a/tools/d2c/id/tokens.l b/tools/d2c/id/tokens.l
new file mode 100644
index 0000000..24f18f1
--- /dev/null
+++ b/tools/d2c/id/tokens.l
@@ -0,0 +1,33 @@
+
+%top {
+
+#include <stdlib.h>
+
+#include "grammar.h"
+
+}
+
+
+%option noyywrap
+%option nounput
+%option noinput
+%option yylineno
+%option noyy_top_state
+
+
+%%
+
+
+" " { }
+
+[0-9_]* { yylvalp->value = strtoul(yytext, NULL, 10); return VALUE; }
+
+. {
+ char *msg;
+ asprintf(&msg, "Unhandled token in d2c id block: '%s'", yytext);
+ YY_FATAL_ERROR(msg);
+ free(msg);
+ }
+
+
+%%