summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2008-07-26 23:46:10 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2008-07-26 23:46:10 (GMT)
commitdbf4d1f93e54251568854bff0ebc9c84f60857f6 (patch)
tree72fa507ada7ba5b7351f711a7f16a8d3f7d32524 /src
parentc4f9b35d4ccb5bb82c4927daddd34d7a828bff3c (diff)
Parsed x86 binary data and displayed the result.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@6 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am9
-rw-r--r--src/arch/Makefile.am22
-rw-r--r--src/arch/instruction-int.h59
-rw-r--r--src/arch/instruction.c64
-rw-r--r--src/arch/instruction.h43
-rw-r--r--src/arch/operand-int.h51
-rw-r--r--src/arch/operand.c55
-rw-r--r--src/arch/operand.h44
-rw-r--r--src/arch/processor-int.h66
-rw-r--r--src/arch/processor.c95
-rw-r--r--src/arch/processor.h49
-rw-r--r--src/arch/x86/Makefile.am20
-rw-r--r--src/arch/x86/instruction.h65
-rw-r--r--src/arch/x86/op_int.c63
-rw-r--r--src/arch/x86/opcodes.h40
-rw-r--r--src/arch/x86/processor.c228
-rw-r--r--src/arch/x86/processor.h42
-rw-r--r--src/binary.c78
-rw-r--r--src/binary.h42
-rw-r--r--src/editor.c23
-rw-r--r--src/format/Makefile.am2
-rw-r--r--src/format/elf/Makefile.am14
-rw-r--r--src/format/elf/format_elf.c24
-rw-r--r--src/format/elf/format_elf.h34
-rw-r--r--src/gtksnippet.c435
-rw-r--r--src/gtksnippet.h128
26 files changed, 1788 insertions, 7 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 07af7b5..aaa1ff3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,7 +2,9 @@
bin_PROGRAMS=openida
openida_SOURCES = \
- editor.c
+ binary.h binary.c \
+ editor.c \
+ gtksnippet.h gtksnippet.c
INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/intl $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) `pkg-config --cflags gthread-2.0`
@@ -15,4 +17,7 @@ AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
openida_LDFLAGS = $(LIBGTK_LIBS) -L/usr/X11R6/lib -ldl $(LIBXML_LIBS) `pkg-config --libs gthread-2.0`
-openida_LDADD = $(LIBINTL)
+openida_LDADD = $(LIBINTL) arch/libarch.a arch/x86/libarchx86.a format/elf/libformatelf.a
+
+
+SUBDIRS = arch format
diff --git a/src/arch/Makefile.am b/src/arch/Makefile.am
new file mode 100644
index 0000000..7f231de
--- /dev/null
+++ b/src/arch/Makefile.am
@@ -0,0 +1,22 @@
+
+lib_LIBRARIES = libarch.a
+
+libarch_a_SOURCES = \
+ instruction-int.h \
+ instruction.h instruction.c \
+ operand-int.h \
+ operand.h operand.c \
+ processor-int.h \
+ processor.h processor.c
+
+libarch_a_CFLAGS = $(AM_CFLAGS)
+
+
+INCLUDES =
+
+AM_CPPFLAGS =
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
+
+
+SUBDIRS = x86
diff --git a/src/arch/instruction-int.h b/src/arch/instruction-int.h
new file mode 100644
index 0000000..11180dc
--- /dev/null
+++ b/src/arch/instruction-int.h
@@ -0,0 +1,59 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * instruction.h - prototypes pour la définition générique interne des instructions
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_INSTRUCTION_INT_H
+#define _ARCH_INSTRUCTION_INT_H
+
+
+#include "instruction.h"
+#include "operand.h"
+
+
+#include "operand-int.h" /* TODO: remove me */
+
+
+
+
+#define MAX_OPERANDS 4
+
+
+#define DB_OPCODE 0x00
+
+
+/* Définition générique d'une instruction */
+struct _asm_instr
+{
+ char opcode;
+
+ asm_operand operands[MAX_OPERANDS];
+ unsigned int operands_count; /* Nbre. d'opérandes utilisées */
+
+
+};
+
+#define ASM_INSTRUCTION(instr) ((asm_instr *)instr)
+
+
+
+
+#endif /* _ARCH_INSTRUCTION_INT_H */
diff --git a/src/arch/instruction.c b/src/arch/instruction.c
new file mode 100644
index 0000000..1af3b97
--- /dev/null
+++ b/src/arch/instruction.c
@@ -0,0 +1,64 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * instruction.c - gestion générique des instructions
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "instruction.h"
+
+
+#include <malloc.h>
+
+
+#include "instruction-int.h"
+
+
+
+/******************************************************************************
+* *
+* Paramètres : data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Crée une instruction de type 'db' à partir de données. *
+* *
+* Retour : Instruction mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_instr *create_db_instruction(const char *data, off_t *pos, off_t len)
+{
+ asm_instr *result; /* Représentation à renvoyer */
+
+ result = (asm_instr *)calloc(1, sizeof(asm_instr));
+
+ result->opcode = DB_OPCODE;
+
+ /* TODO: check result */
+ fill_db_operand(&result->operands[0], data[(*pos)++]);
+
+ result->operands_count = 1;
+
+ return result;
+
+}
+
diff --git a/src/arch/instruction.h b/src/arch/instruction.h
new file mode 100644
index 0000000..3be2bf7
--- /dev/null
+++ b/src/arch/instruction.h
@@ -0,0 +1,43 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * instruction.h - prototypes pour la gestion générique des instructions
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_INSTRUCTION_H
+#define _ARCH_INSTRUCTION_H
+
+
+#include <sys/types.h>
+
+
+
+/* Définition générique d'une instruction */
+typedef struct _asm_instr asm_instr;
+
+
+
+/* Crée une instruction de type 'db' à partir de données. */
+asm_instr *create_db_instruction(const char *, off_t *, off_t);
+
+
+
+
+#endif /* _ARCH_INSTRUCTION_H */
diff --git a/src/arch/operand-int.h b/src/arch/operand-int.h
new file mode 100644
index 0000000..f09d9a9
--- /dev/null
+++ b/src/arch/operand-int.h
@@ -0,0 +1,51 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * operand-int.h - prototypes pour la définition générique interne des opérandes
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_OPERAND_INT_H
+#define _ARCH_OPERAND_INT_H
+
+
+#include <stdint.h>
+
+
+
+
+/* Définition générique d'une opérande */
+struct _asm_operand
+{
+
+
+ union
+ {
+ uint8_t val8; /* Valeur sur 8 bits */
+
+
+ } value;
+
+
+
+};
+
+
+
+#endif /* _ARCH_OPERAND_INT_H */
diff --git a/src/arch/operand.c b/src/arch/operand.c
new file mode 100644
index 0000000..98460c9
--- /dev/null
+++ b/src/arch/operand.c
@@ -0,0 +1,55 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * operand.c - gestion générique des opérandes
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "operand.h"
+
+
+#include "operand-int.h"
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : operand = structure dont le contenu est à définir. *
+* value = valeur immédiate à renseigner. *
+* *
+* Description : Crée une opérande pour l'instruction 'db'. *
+* *
+* Retour : true si l'opérande a été définie avec succès, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool fill_db_operand(asm_operand *operand, uint8_t value)
+{
+ operand->value.val8 = value;
+
+ return true;
+
+}
+
+
diff --git a/src/arch/operand.h b/src/arch/operand.h
new file mode 100644
index 0000000..81c9a53
--- /dev/null
+++ b/src/arch/operand.h
@@ -0,0 +1,44 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * operand.h - prototypes pour la gestion générique des opérandes
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_OPERAND_H
+#define _ARCH_OPERAND_H
+
+
+#include <stdbool.h>
+#include <stdint.h>
+
+
+
+/* Définition générique d'une opérande */
+typedef struct _asm_operand asm_operand;
+
+
+
+/* Crée une opérande pour l'instruction 'db'. */
+bool fill_db_operand(asm_operand *, uint8_t);
+
+
+
+
+#endif /* _ARCH_OPERAND_H */
diff --git a/src/arch/processor-int.h b/src/arch/processor-int.h
new file mode 100644
index 0000000..602bab6
--- /dev/null
+++ b/src/arch/processor-int.h
@@ -0,0 +1,66 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * processor.h - prototypes pour la définition générique interne des architectures
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_PROCESSOR_INT_H
+#define _ARCH_PROCESSOR_INT_H
+
+
+#include <sys/types.h>
+
+
+#include "instruction.h"
+#include "processor.h"
+
+
+
+
+typedef int AsmSyntax;
+
+
+/* Décode une instruction dans un flux de données. */
+typedef asm_instr * (* fetch_instruction) (const asm_processor *, const char *, off_t *, off_t);
+
+/* Traduit une instruction en version humainement lisible. */
+typedef void (* print_instruction) (const asm_processor *, const asm_instr *, char *, size_t, AsmSyntax);
+
+
+
+/* Définition générique d'une architecture */
+struct _asm_processor
+{
+
+ fetch_instruction fetch_instr; /* Lecture d'une instruction */
+
+ print_instruction print_instr; /* Version lisible d'une instr.*/
+
+
+};
+
+
+#define ASM_PROCESSOR(proc) ((asm_processor *)proc)
+
+
+
+
+
+#endif /* _ARCH_PROCESSOR_INT_H */
diff --git a/src/arch/processor.c b/src/arch/processor.c
new file mode 100644
index 0000000..fc59fdb
--- /dev/null
+++ b/src/arch/processor.c
@@ -0,0 +1,95 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * processor.c - gestion générique des architectures
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "processor.h"
+
+
+#include "processor-int.h"
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Décode une instruction dans un flux de données. *
+* *
+* Retour : Instruction mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_instr *decode_instruction(const asm_processor *proc, const char *data, off_t *pos, off_t len)
+{
+ asm_instr *result; /* Représentation à renvoyer */
+
+
+
+
+ result = proc->fetch_instr(proc, data, pos, len);
+
+
+#define NULL ((void *)0)
+
+
+ if (result == NULL)
+ result = create_db_instruction(data, pos, len);
+
+
+
+
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* instr = instruction à traiter. *
+* buffer = tampon de sortie mis à disposition. [OUT] *
+* len = taille de ce tampon. *
+* syntax = type de représentation demandée. *
+* *
+* Description : Traduit une instruction en version humainement lisible. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void print_hinstruction(const asm_processor *proc, const asm_instr *instr, char *buffer, size_t len/*, AsmSyntax syntax*/)
+{
+ proc->print_instr(proc, instr, buffer, len, 0);
+
+}
+
+
diff --git a/src/arch/processor.h b/src/arch/processor.h
new file mode 100644
index 0000000..1023d45
--- /dev/null
+++ b/src/arch/processor.h
@@ -0,0 +1,49 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * processor.h - prototypes pour la gestion générique des architectures
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_PROCESSOR_H
+#define _ARCH_PROCESSOR_H
+
+
+#include <sys/types.h>
+
+
+#include "instruction.h"
+
+
+
+/* Définition générique d'une architecture */
+typedef struct _asm_processor asm_processor;
+
+
+
+/* Décode une instruction dans un flux de données. */
+asm_instr *decode_instruction(const asm_processor *, const char *, off_t *, off_t);
+
+/* Traduit une instruction en version humainement lisible. */
+void print_hinstruction(const asm_processor *, const asm_instr *, char *, size_t);
+
+
+
+
+#endif /* _ARCH_PROCESSOR_H */
diff --git a/src/arch/x86/Makefile.am b/src/arch/x86/Makefile.am
new file mode 100644
index 0000000..6fe55c8
--- /dev/null
+++ b/src/arch/x86/Makefile.am
@@ -0,0 +1,20 @@
+
+lib_LIBRARIES = libarchx86.a
+
+libarchx86_a_SOURCES = \
+ instruction.h \
+ op_int.c \
+ opcodes.h \
+ processor.h processor.c
+
+libarchx86_a_CFLAGS = $(AM_CFLAGS)
+
+
+INCLUDES =
+
+AM_CPPFLAGS =
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
+
+
+SUBDIRS =
diff --git a/src/arch/x86/instruction.h b/src/arch/x86/instruction.h
new file mode 100644
index 0000000..47617ea
--- /dev/null
+++ b/src/arch/x86/instruction.h
@@ -0,0 +1,65 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * instruction.h - prototypes pour la gestion des instructions de l'architecture x86
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_X86_INSTRUCTION_H
+#define _ARCH_X86_INSTRUCTION_H
+
+
+#include "../instruction.h"
+#include "../instruction-int.h"
+
+
+
+/* Définition d'une instruction x86 */
+typedef struct _asm_x86_instr asm_x86_instr;
+
+
+
+/* Enumération de tous les opcodes */
+typedef enum _X86Opcodes
+{
+ X86_OP_INT, /* int (0xcd) */
+
+
+ X86_OP_COUNT
+
+} X86Opcodes;
+
+
+
+
+/* Définition d'une instruction x86 */
+struct _asm_x86_instr
+{
+ asm_instr base; /* A laisser en premier... */
+
+ X86Opcodes type;
+
+};
+
+
+
+
+
+
+#endif /* _ARCH_X86_INSTRUCTION_H */
diff --git a/src/arch/x86/op_int.c b/src/arch/x86/op_int.c
new file mode 100644
index 0000000..e7805e1
--- /dev/null
+++ b/src/arch/x86/op_int.c
@@ -0,0 +1,63 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * op_int.c - décodage des instructions d'interruption
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 <malloc.h>
+
+
+#include "instruction.h"
+#include "../instruction-int.h"
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Décode une instruction de type 'int'. *
+* *
+* Retour : Instruction mise en place ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_x86_instr *read_instr_int(const char *data, off_t *pos, off_t len)
+{
+ asm_x86_instr *result;
+
+ result = (asm_x86_instr *)calloc(1, sizeof(asm_x86_instr));
+
+ ASM_INSTRUCTION(result)->opcode = data[(*pos)++];
+
+ /* TODO: check result */
+ fill_db_operand(&ASM_INSTRUCTION(result)->operands[0], data[(*pos)++]);
+
+ ASM_INSTRUCTION(result)->operands_count = 1;
+
+ return result;
+
+}
+
diff --git a/src/arch/x86/opcodes.h b/src/arch/x86/opcodes.h
new file mode 100644
index 0000000..17316e7
--- /dev/null
+++ b/src/arch/x86/opcodes.h
@@ -0,0 +1,40 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * opcodes.h - prototypes pour la liste de tous les opcodes de l'architecture x86
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_X86_OPCODES_H
+#define _ARCH_X86_OPCODES_H
+
+
+#include <sys/types.h>
+
+
+#include "../instruction.h"
+
+
+
+/* Décode une instruction de type 'int'. */
+asm_x86_instr *read_instr_int(const char *, off_t *, off_t);
+
+
+
+#endif /* _ARCH_X86_OPCODES_H */
diff --git a/src/arch/x86/processor.c b/src/arch/x86/processor.c
new file mode 100644
index 0000000..4ef1377
--- /dev/null
+++ b/src/arch/x86/processor.c
@@ -0,0 +1,228 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * processor.c - gestion de l'architecture x86
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "processor.h"
+
+
+#include "../processor-int.h"
+#include "instruction.h"
+#include "opcodes.h"
+
+
+#include <malloc.h>
+
+
+
+typedef asm_x86_instr * (* read_instr) (const char *, off_t *, off_t);
+
+
+/* Carte d'identité d'un opcode */
+typedef struct _x86_opcode
+{
+ char opcode; /* Opcode + préfixe eventuel */
+ const char *name; /* Désignation humaine */
+ read_instr read; /* Décodage de l'instruction */
+
+} x86_opcode;
+
+
+#define register_opcode(target, bin, n, func) \
+do {\
+target.opcode = bin; \
+target.name = n; \
+target.read = func; \
+} while (0)
+
+
+/* Définition générique d'une architecture */
+typedef struct _asm_x86_processor
+{
+ asm_processor base; /* A laisser en premier... */
+
+ x86_opcode opcodes[X86_OP_COUNT]; /* Liste des opcodes supportés */
+
+} asm_x86_processor;
+
+
+
+
+
+/* Enregistre toutes les instructions reconnues pour x86. */
+void x86_register_instructions(asm_x86_processor *);
+
+
+/* Décode une instruction dans un flux de données. */
+asm_instr *x86_fetch_instruction(const asm_x86_processor *, const char *, off_t *, off_t);
+
+/* Traduit une instruction en version humainement lisible. */
+void x86_print_instruction(const asm_x86_processor *, const asm_x86_instr *, char *, size_t, AsmSyntax);
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Crée le support de l'architecture x86. *
+* *
+* Retour : Architecture mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_processor *create_x86_processor(void)
+{
+ asm_x86_processor *result; /* Architecture à retourner */
+
+ result = (asm_x86_processor *)calloc(1, sizeof(asm_x86_processor));
+
+ x86_register_instructions(result);
+
+ ASM_PROCESSOR(result)->fetch_instr = x86_fetch_instruction;
+ ASM_PROCESSOR(result)->print_instr = x86_print_instruction;
+
+ return ASM_PROCESSOR(result);
+
+}
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* *
+* Description : Enregistre toutes les instructions reconnues pour x86. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void x86_register_instructions(asm_x86_processor *proc)
+{
+
+
+
+ register_opcode(proc->opcodes[X86_OP_INT], 0xcd, "int", read_instr_int);
+
+
+
+
+
+}
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* data = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* len = taille totale des données à analyser. *
+* *
+* Description : Décode une instruction dans un flux de données. *
+* *
+* Retour : Instruction mise en place ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_instr *x86_fetch_instruction(const asm_x86_processor *proc, const char *data, off_t *pos, off_t len)
+{
+ asm_x86_instr *result; /* Résultat à faire remonter */
+ X86Opcodes i; /* Boucle de parcours */
+
+ result = NULL;
+
+ //printf("--------\n");
+
+ for (i = 0; i < X86_OP_COUNT; i++)
+ {
+
+ /*
+ printf(" cmp :: 0x%02hhx vs 0x%02hhx ? %d\n",
+ data[*pos], proc->opcodes[i].opcode,
+ data[*pos] == proc->opcodes[i].opcode);
+ */
+
+ if (data[*pos] == proc->opcodes[i].opcode)
+ {
+ result = proc->opcodes[i].read(data, pos, len);
+ if (result != NULL) result->type = i;
+ break;
+ }
+
+ }
+
+
+ return ASM_INSTRUCTION(result);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* instr = instruction à traiter. *
+* buffer = tampon de sortie mis à disposition. [OUT] *
+* len = taille de ce tampon. *
+* syntax = type de représentation demandée. *
+* *
+* Description : Traduit une instruction en version humainement lisible. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void x86_print_instruction(const asm_x86_processor *proc, const asm_x86_instr *instr, char *buffer, size_t len, AsmSyntax syntax)
+{
+
+ if (ASM_INSTRUCTION(instr)->opcode == DB_OPCODE)
+
+
+ snprintf(buffer, len, "db\t0x%02hhx", ASM_INSTRUCTION(instr)->operands[0].value.val8);
+
+
+ else
+
+ snprintf(buffer, len, "%s\t0x%02hhx", proc->opcodes[instr->type].name, ASM_INSTRUCTION(instr)->operands[0].value.val8);
+
+
+}
+
+
+
+
+
+
+
diff --git a/src/arch/x86/processor.h b/src/arch/x86/processor.h
new file mode 100644
index 0000000..0bd587d
--- /dev/null
+++ b/src/arch/x86/processor.h
@@ -0,0 +1,42 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * processor.h - prototypes pour la gestion de l'architecture x86
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _ARCH_X86_PROCESSOR_H
+#define _ARCH_X86_PROCESSOR_H
+
+
+#include "../instruction.h"
+#include "../processor.h"
+
+
+
+/* Crée le support de l'architecture x86. */
+asm_processor *create_x86_processor(void);
+
+
+
+
+
+
+
+#endif /* _ARCH_X86_PROCESSOR_H */
diff --git a/src/binary.c b/src/binary.c
new file mode 100644
index 0000000..79928d6
--- /dev/null
+++ b/src/binary.c
@@ -0,0 +1,78 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * binary.c - traitement des flots de code binaire
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "binary.h"
+
+
+#include "arch/processor.h"
+
+
+
+void fill_snippet(GtkSnippet *snippet)
+{
+ asm_processor *proc;
+ asm_instr *instr;
+
+ // char *data = "\x66\xba\x0c\x00\x00\x00\x66\xb9\x28\x00\x00\x00\x66\xbb\x01\x00\x00\x00\x66\xb8\x04\x00\x00\x00\xcd\x80\x66\xbb\x00\x00\x00\x00\x66\xb8\x01\x00\x00\x00\xcd\x80\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x0a";
+
+ char *data = "\x66\xbb\x00\x00\x00\x00\x66\xb8\x01\x00\x00\x00\xcd\x80\x90";
+
+ off_t pos;
+ off_t len;
+
+ char buffer[64];
+
+ uint64_t base = 0;
+ uint64_t offset = 0;
+
+ proc = create_x86_processor();
+
+ pos = 0;
+ len = 15;
+
+
+
+ gtk_snippet_set_processor(snippet, proc);
+
+
+ gtk_snippet_add_line(snippet, offset, NULL, "Simple HelloWorld !");
+
+
+ while (pos < len)
+ {
+ offset = base + pos;
+
+ instr = decode_instruction(proc, data, &pos, len);
+
+ gtk_snippet_add_line(snippet, offset, instr, NULL);
+
+
+ }
+
+ /*
+ gtk_snippet_build_content(snippet);
+ */
+
+
+}
+
diff --git a/src/binary.h b/src/binary.h
new file mode 100644
index 0000000..a6295c9
--- /dev/null
+++ b/src/binary.h
@@ -0,0 +1,42 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * binary.h - prototypes pour le traitement des flots de code binaire
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _BINARY_H
+#define _BINARY_H
+
+
+#include "gtksnippet.h"
+
+
+
+
+void fill_snippet(GtkSnippet *snippet);
+
+
+
+
+
+
+
+
+#endif /* _BINARY_H */
diff --git a/src/editor.c b/src/editor.c
index 3614379..1c33296 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -1,17 +1,17 @@
-/* Firebox Tools - Outils de configurations pour le WM Firebox
+/* OpenIDA - Outils de configurations pour le WM Firebox
* editor.c - fichier principal de l'éditeur de configuration
*
* Copyright (C) 2006-2007 Cyrille Bagard
*
- * This file is part of Firebox Tools.
+ * This file is part of OpenIDA.
*
- * Firebox Tools is free software; you can redistribute it and/or modify
+ * 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 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
- * Firebox Tools is distributed in the hope that it will be useful,
+ * 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.
@@ -49,6 +49,10 @@
/** Partie ptrace() **/
+#include "binary.h"
+#include "gtksnippet.h"
+
+
#define _(str) str
@@ -426,7 +430,12 @@ GtkWidget *create_editor(void)
gtk_paned_pack1 (GTK_PANED (hpaned1), scrolledwindow2, FALSE, TRUE);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow2), GTK_SHADOW_IN);
+ textview2 = gtk_snippet_new();
+ gtk_snippet_set_sel(GTK_SNIPPET(textview2), 44);
+ fill_snippet(textview2);
+ /*
textview2 = gtk_text_view_new ();
+ */
gtk_widget_show (textview2);
gtk_container_add (GTK_CONTAINER (scrolledwindow2), textview2);
@@ -531,6 +540,7 @@ GtkWidget *create_editor(void)
gtk_misc_set_alignment (GTK_MISC (label12), 0, 0.5);
lbl_eax = label12;
+ gtk_widget_modify_font(lbl_eax, pango_font_description_from_string ("Monospace"));
label14 = gtk_label_new (_("0x00000000"));
gtk_widget_show (label14);
@@ -540,6 +550,7 @@ GtkWidget *create_editor(void)
gtk_misc_set_alignment (GTK_MISC (label14), 0, 0.5);
lbl_ebx = label14;
+ gtk_widget_modify_font(lbl_ebx, pango_font_description_from_string ("Monospace 10"));
label15 = gtk_label_new (_("0x00000000"));
gtk_widget_show (label15);
@@ -549,6 +560,7 @@ GtkWidget *create_editor(void)
gtk_misc_set_alignment (GTK_MISC (label15), 0, 0.5);
lbl_ecx = label15;
+ gtk_widget_modify_font(lbl_ecx, pango_font_description_from_string ("Monospace 10"));
label16 = gtk_label_new (_("0x00000000"));
gtk_widget_show (label16);
@@ -558,6 +570,7 @@ GtkWidget *create_editor(void)
gtk_misc_set_alignment (GTK_MISC (label16), 0, 0.5);
lbl_edx = label16;
+ gtk_widget_modify_font(lbl_edx, pango_font_description_from_string ("Monospace 10"));
label17 = gtk_label_new (_("0x00000000"));
gtk_widget_show (label17);
diff --git a/src/format/Makefile.am b/src/format/Makefile.am
new file mode 100644
index 0000000..99afa2e
--- /dev/null
+++ b/src/format/Makefile.am
@@ -0,0 +1,2 @@
+
+SUBDIRS = elf
diff --git a/src/format/elf/Makefile.am b/src/format/elf/Makefile.am
new file mode 100644
index 0000000..3cf5f1c
--- /dev/null
+++ b/src/format/elf/Makefile.am
@@ -0,0 +1,14 @@
+
+lib_LIBRARIES = libformatelf.a
+
+libformatelf_a_SOURCES = \
+ format_elf.h format_elf.c
+
+libformatelf_a_CFLAGS = $(AM_CFLAGS)
+
+
+INCLUDES =
+
+AM_CPPFLAGS =
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
diff --git a/src/format/elf/format_elf.c b/src/format/elf/format_elf.c
new file mode 100644
index 0000000..f043d0a
--- /dev/null
+++ b/src/format/elf/format_elf.c
@@ -0,0 +1,24 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * format_elf.c - support du format ELF
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "elf.h"
diff --git a/src/format/elf/format_elf.h b/src/format/elf/format_elf.h
new file mode 100644
index 0000000..894623a
--- /dev/null
+++ b/src/format/elf/format_elf.h
@@ -0,0 +1,34 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * format_elf.h - prototypes pour le support du format ELF
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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_ELF_H
+#define _FORMAT_ELF_H
+
+
+
+
+
+
+
+
+#endif /* _FORMAT_ELF_H */
diff --git a/src/gtksnippet.c b/src/gtksnippet.c
new file mode 100644
index 0000000..a7eecb5
--- /dev/null
+++ b/src/gtksnippet.c
@@ -0,0 +1,435 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * gtksnippet.h - affichage d'un fragment de code d'assemblage
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 "gtksnippet.h"
+
+
+#include <malloc.h>
+#include <string.h>
+
+
+#define CONTENT_BUFFER_LEN 64
+
+
+
+static void gtk_snippet_class_init(GtkSnippetClass *klass);
+static void gtk_snippet_init(GtkSnippet *cpu);
+static void gtk_snippet_size_request(GtkWidget *widget,
+ GtkRequisition *requisition);
+static void gtk_snippet_size_allocate(GtkWidget *widget,
+ GtkAllocation *allocation);
+static void gtk_snippet_realize(GtkWidget *widget);
+static gboolean gtk_snippet_expose(GtkWidget *widget,
+ GdkEventExpose *event);
+static void gtk_snippet_paint(GtkWidget *widget);
+static void gtk_snippet_destroy(GtkObject *object);
+
+
+GtkType
+gtk_snippet_get_type(void)
+{
+ static GtkType gtk_snippet_type = 0;
+
+
+ if (!gtk_snippet_type) {
+ static const GtkTypeInfo gtk_snippet_info = {
+ "GtkSnippet",
+ sizeof(GtkSnippet),
+ sizeof(GtkSnippetClass),
+ (GtkClassInitFunc) gtk_snippet_class_init,
+ (GtkObjectInitFunc) gtk_snippet_init,
+ NULL,
+ NULL,
+ (GtkClassInitFunc) NULL
+ };
+ gtk_snippet_type = gtk_type_unique(GTK_TYPE_WIDGET, &gtk_snippet_info);
+ }
+
+
+ return gtk_snippet_type;
+}
+
+void
+gtk_snippet_set_state(GtkSnippet *cpu, gint num)
+{
+ cpu->sel = num;
+ gtk_snippet_paint(GTK_WIDGET(cpu));
+}
+
+
+GtkWidget * gtk_snippet_new()
+{
+ GtkSnippet *result;
+
+ result = gtk_type_new(gtk_snippet_get_type());
+
+ return GTK_WIDGET(result);
+
+}
+
+
+static void
+gtk_snippet_class_init(GtkSnippetClass *klass)
+{
+ GtkWidgetClass *widget_class;
+ GtkObjectClass *object_class;
+
+
+ widget_class = (GtkWidgetClass *) klass;
+ object_class = (GtkObjectClass *) klass;
+
+ widget_class->realize = gtk_snippet_realize;
+ widget_class->size_request = gtk_snippet_size_request;
+ widget_class->size_allocate = gtk_snippet_size_allocate;
+ widget_class->expose_event = gtk_snippet_expose;
+
+ object_class->destroy = gtk_snippet_destroy;
+}
+
+
+static void
+gtk_snippet_init(GtkSnippet *snippet)
+{
+ snippet->sel = 0;
+}
+
+
+static void
+gtk_snippet_size_request(GtkWidget *widget,
+ GtkRequisition *requisition)
+{
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_SNIPPET(widget));
+ g_return_if_fail(requisition != NULL);
+
+ requisition->width = 80;
+ requisition->height = 100;
+}
+
+
+static void
+gtk_snippet_size_allocate(GtkWidget *widget,
+ GtkAllocation *allocation)
+{
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_SNIPPET(widget));
+ g_return_if_fail(allocation != NULL);
+
+ widget->allocation = *allocation;
+
+ if (GTK_WIDGET_REALIZED(widget)) {
+ gdk_window_move_resize(
+ widget->window,
+ allocation->x, allocation->y,
+ allocation->width, allocation->height
+ );
+ }
+}
+
+
+static void
+gtk_snippet_realize(GtkWidget *widget)
+{
+ GdkWindowAttr attributes;
+ guint attributes_mask;
+
+ g_return_if_fail(widget != NULL);
+ g_return_if_fail(GTK_IS_SNIPPET(widget));
+
+ GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
+
+ attributes.window_type = GDK_WINDOW_CHILD;
+ attributes.x = widget->allocation.x;
+ attributes.y = widget->allocation.y;
+ attributes.width = widget->allocation.width;
+ attributes.height = widget->allocation.height;
+
+ printf("(%d x %d)\n", widget->allocation.width, widget->allocation.height);
+
+ attributes.wclass = GDK_INPUT_OUTPUT;
+ attributes.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
+
+ attributes_mask = GDK_WA_X | GDK_WA_Y;
+
+ widget->window = gdk_window_new(
+ gtk_widget_get_parent_window (widget),
+ & attributes, attributes_mask
+ );
+
+ gdk_window_set_user_data(widget->window, widget);
+
+ widget->style = gtk_style_attach(widget->style, widget->window);
+ gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL);
+
+
+ GTK_SNIPPET(widget)->layout = gtk_widget_create_pango_layout(widget, NULL);
+ GTK_SNIPPET(widget)->gc = gdk_gc_new(GDK_DRAWABLE(widget->window));
+
+ gtk_snippet_build_content(GTK_SNIPPET(widget));
+
+}
+
+
+static gboolean
+gtk_snippet_expose(GtkWidget *widget,
+ GdkEventExpose *event)
+{
+ g_return_val_if_fail(widget != NULL, FALSE);
+ g_return_val_if_fail(GTK_IS_SNIPPET(widget), FALSE);
+ g_return_val_if_fail(event != NULL, FALSE);
+
+ gtk_snippet_paint(widget);
+
+ return FALSE;
+}
+
+
+static void
+gtk_snippet_paint(GtkWidget *widget)
+{
+ cairo_t *cr;
+
+ cr = gdk_cairo_create(widget->window);
+
+ cairo_translate(cr, 0, 7);
+
+ cairo_set_source_rgb(cr, 1, 1, 1);
+ cairo_paint(cr);
+
+ gint pos = GTK_SNIPPET(widget)->sel;
+ gint rect = pos / 5;
+
+ cairo_set_source_rgb(cr, 0.2, 0.4, 0);
+
+ gint i;
+ for ( i = 1; i <= 20; i++) {
+ if (i > 20 - rect) {
+ cairo_set_source_rgb(cr, 0.6, 1.0, 0);
+ } else {
+ cairo_set_source_rgb(cr, 0.2, 0.4, 0);
+ }
+ cairo_rectangle(cr, 8, i*4, 30, 3);
+ cairo_rectangle(cr, 42, i*4, 30, 3);
+ cairo_fill(cr);
+ }
+
+ cairo_destroy(cr);
+
+ printf("rendering...\n");
+
+ gdk_draw_layout(GDK_DRAWABLE(widget->window), GTK_SNIPPET(widget)->gc,
+ 10, 100, GTK_SNIPPET(widget)->layout);
+
+}
+
+
+static void
+gtk_snippet_destroy(GtkObject *object)
+{
+ GtkSnippet *cpu;
+ GtkSnippetClass *klass;
+
+ g_return_if_fail(object != NULL);
+ g_return_if_fail(GTK_IS_SNIPPET(object));
+
+ cpu = GTK_SNIPPET(object);
+
+ klass = gtk_type_class(gtk_widget_get_type());
+
+ if (GTK_OBJECT_CLASS(klass)->destroy) {
+ (* GTK_OBJECT_CLASS(klass)->destroy) (object);
+ }
+}
+
+
+
+
+void gtk_snippet_set_sel(GtkSnippet *cpu, gint sel)
+{
+ cpu->sel = sel;
+
+}
+
+
+
+void gtk_snippet_test(GtkSnippet *snippet)
+{
+
+
+
+ pango_layout_set_markup(snippet->layout, "<tt>int\t<span foreground='#ff0000'>80</span></tt>", -1);
+
+
+
+
+}
+
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : snippet = composant GTK à mettre à jour. *
+* proc = architecture à associer au contenu. *
+* *
+* Description : Définit l'architecture à laquelle le contenu est lié. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_snippet_set_processor(GtkSnippet *snippet, const asm_processor *proc)
+{
+ snippet->proc = proc;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : snippet = composant GTK à mettre à jour. *
+* offset = position de l'instruction à ajouter. *
+* instr = instruction à représenter ou NULL. *
+* comment = commentaire à imprimer ou NULL. *
+* *
+* Description : Ajoute une ligne dans le bloc de représentation. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_snippet_add_line(GtkSnippet *snippet, uint64_t offset, asm_instr *instr, const char *comment)
+{
+ snippet->info = (code_line_info *)realloc(snippet->info, ++snippet->info_count * sizeof(code_line_info));
+
+ snippet->info[snippet->info_count - 1].offset = offset;
+ snippet->info[snippet->info_count - 1].instr = instr;
+ snippet->info[snippet->info_count - 1].comment = (comment != NULL ? strdup(comment) : NULL);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : snippet = composant GTK à mettre à jour. *
+* *
+* Description : Définit le contenu visuel à partir des infos enregistrées. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void gtk_snippet_build_content(GtkSnippet *snippet)
+{
+ char *content; /* Contenu à définir */
+ size_t content_len; /* Taille du contenu */
+ AdressMode mode; /* Affichage des adresses */
+ unsigned int i; /* Boucle de traitement */
+ char buffer[CONTENT_BUFFER_LEN]; /* Zone tampon à utiliser */
+
+ content_len = strlen("<tt>") + 1;
+ content = (char *)calloc(content_len, sizeof(char));
+ strcpy(content, "<tt>");
+
+ mode = ADM_32BITS; /* FIXME */
+
+ for (i = 0; i < snippet->info_count; i++)
+ {
+ if (i > 0)
+ {
+ content = (char *)realloc(content, ++content_len * sizeof(char));
+ strcat(content, "\n");
+ }
+
+ /* Adresse */
+
+ switch (mode)
+ {
+ case ADM_32BITS:
+ snprintf(buffer, CONTENT_BUFFER_LEN,
+ "<span foreground='#333333'>0x%08llx</span>",
+ snippet->info[i].offset);
+ break;
+
+ case ADM_64BITS:
+ snprintf(buffer, CONTENT_BUFFER_LEN,
+ "<span foreground='#333333'>0x%16llx</span>",
+ snippet->info[i].offset);
+ break;
+
+ }
+
+ content_len += strlen(buffer);
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, buffer);
+
+ /* Eventuelle instruction */
+
+ if (snippet->info[i].instr != NULL)
+ {
+ print_hinstruction(snippet->proc, snippet->info[i].instr, buffer, CONTENT_BUFFER_LEN);
+
+ content_len += strlen("\t") + strlen(buffer);
+
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, "\t");
+ strcat(content, buffer);
+
+ }
+
+ /* Eventuel commantaire */
+
+ if (snippet->info[i].comment != NULL)
+ {
+ content_len += strlen("\t<b><span foreground='#003300'>; ") + strlen(snippet->info[i].comment) + strlen("</span></b>");
+
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, "\t<b><span foreground='#003300'>; ");
+ strcat(content, snippet->info[i].comment);
+ strcat(content, "</span></b>");
+
+ }
+
+ }
+
+ content_len += strlen("</tt>");
+ content = (char *)realloc(content, content_len * sizeof(char));
+ strcat(content, "</tt>");
+
+ pango_layout_set_markup(snippet->layout, content, content_len - 1);
+
+}
+
+
+
diff --git a/src/gtksnippet.h b/src/gtksnippet.h
new file mode 100644
index 0000000..e66eedc
--- /dev/null
+++ b/src/gtksnippet.h
@@ -0,0 +1,128 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * format_elf.h - prototypes pour l'affichage d'un fragment de code d'assemblage
+ *
+ * Copyright (C) 2008 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * 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 _GTK_SNIPPET_H
+#define _GTK_SNIPPET_H
+
+
+#include <stdint.h>
+#include <gtk/gtk.h>
+#include <cairo.h>
+
+
+
+
+#include "arch/instruction.h"
+#include "arch/processor.h"
+
+
+
+
+
+
+
+G_BEGIN_DECLS
+
+
+#define GTK_SNIPPET(obj) GTK_CHECK_CAST(obj, gtk_snippet_get_type (), GtkSnippet)
+#define GTK_SNIPPET_CLASS(klass) GTK_CHECK_CLASS_CAST(klass, gtk_snippet_get_type(), GtkSnippetClass)
+#define GTK_IS_SNIPPET(obj) GTK_CHECK_TYPE(obj, gtk_snippet_get_type())
+
+
+typedef struct _GtkSnippet GtkSnippet;
+typedef struct _GtkSnippetClass GtkSnippetClass;
+
+
+/* Mode d'adressage à utiliser */
+typedef enum _AdressMode
+{
+ ADM_32BITS, /* Adresses sur 32 bits */
+ ADM_64BITS /* Adresses sur 64 bits */
+
+} AdressMode;
+
+
+
+typedef struct _code_line_info
+{
+ uint64_t offset; /* Position de l'instruction */
+ asm_instr *instr; /* Eventuelle instruction */
+ char *comment; /* Eventuel commentaire */
+
+} code_line_info;
+
+
+struct _GtkSnippet {
+
+ GtkWidget widget; /* Présence obligatoire en 1er */
+
+ AdressMode mode; /* Mode d'affichage */
+
+ PangoLayout *layout; /* Moteur de rendu du code ASM */
+ GdkGC *gc; /* Contexte graphique du rendu */
+
+ const asm_processor *proc; /* Architecture utilisée */
+ code_line_info *info; /* Contenu à représenter */
+ unsigned int info_count; /* Quantité d'informations */
+
+
+ gint sel;
+};
+
+struct _GtkSnippetClass {
+ GtkWidgetClass parent_class;
+};
+
+
+GtkType gtk_snippet_get_type(void);
+void gtk_snippet_set_sel(GtkSnippet *cpu, gint sel);
+
+GtkWidget * gtk_snippet_new();
+
+
+
+
+void gtk_snippet_test(GtkSnippet *cpu);
+
+
+
+/* Définit l'architecture à laquelle le contenu est lié. */
+void gtk_snippet_set_processor(GtkSnippet *, const asm_processor *);
+
+/* Ajoute une ligne dans le bloc de représentation. */
+void gtk_snippet_add_line(GtkSnippet *, uint64_t, asm_instr *, const char *);
+
+/* Définit le contenu visuel à partir des infos enregistrées. */
+void gtk_snippet_build_content(GtkSnippet *);
+
+
+
+
+G_END_DECLS
+
+
+
+
+
+
+#endif /* _GTK_SNIPPET_H */