summaryrefslogtreecommitdiff
path: root/src/glibext
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2015-02-11 17:05:54 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2015-02-11 17:05:54 (GMT)
commitbf879f2562545ab7de23f9d38364b7bd4b43fb2c (patch)
tree6154160307cbca304ea9e1de178d8c2dfc8e0928 /src/glibext
parentabd96dbbe27246e9303173e5e2f47b2e4cedbcb7 (diff)
Registered all the supported formats in the system code.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@471 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/glibext')
-rw-r--r--src/glibext/Makefile.am1
-rw-r--r--src/glibext/gbincontent.c288
-rw-r--r--src/glibext/gbincontent.h73
3 files changed, 362 insertions, 0 deletions
diff --git a/src/glibext/Makefile.am b/src/glibext/Makefile.am
index 192587d..0d4b57c 100644
--- a/src/glibext/Makefile.am
+++ b/src/glibext/Makefile.am
@@ -8,6 +8,7 @@ libglibext_la_SOURCES = \
configuration.h configuration.c \
delayed-int.h \
delayed.h delayed.c \
+ gbincontent.h gbincontent.c \
gbinportion.h gbinportion.c \
gbufferline.h gbufferline.c \
gbuffersegment.h gbuffersegment.c \
diff --git a/src/glibext/gbincontent.c b/src/glibext/gbincontent.c
new file mode 100644
index 0000000..c72ac15
--- /dev/null
+++ b/src/glibext/gbincontent.c
@@ -0,0 +1,288 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * gbincontent.c - prototypes pour le chargement de données binaires en mémoire
+ *
+ * Copyright (C) 2015 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * OpenIDA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "gbincontent.h"
+
+
+#include <fcntl.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+
+
+/* Aire de contenu binaire */
+typedef struct _binary_part
+{
+ bin_t *data; /* Contenu binaire représenté */
+ mrange_t range; /* Couverture du binaire */
+
+} binary_part;
+
+
+/* Content de données binaires quelconques (instance) */
+struct _GBinContent
+{
+ GObject parent; /* A laisser en premier */
+
+ binary_part *parts; /* Parties prises en compte */
+ size_t count; /* Nombre de ces parties */
+
+};
+
+/* Content de données binaires quelconques (classe) */
+struct _GBinContentClass
+{
+ GObjectClass parent; /* A laisser en premier */
+
+};
+
+
+/* Initialise la classe des contenus de données binaires. */
+static void g_binary_content_class_init(GBinContentClass *);
+
+/* Initialise une instance de contenu de données binaires. */
+static void g_binary_content_init(GBinContent *);
+
+/* Supprime toutes les références externes. */
+static void g_binary_content_dispose(GBinContent *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_binary_content_finalize(GBinContent *);
+
+
+
+/* Indique le type défini par la GLib pour les contenus de données. */
+G_DEFINE_TYPE(GBinContent, g_binary_content, G_TYPE_OBJECT);
+
+
+/******************************************************************************
+* *
+* Paramètres : klass = classe à initialiser. *
+* *
+* Description : Initialise la classe des contenus de données binaires. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_binary_content_class_init(GBinContentClass *klass)
+{
+ GObjectClass *object; /* Autre version de la classe */
+
+ object = G_OBJECT_CLASS(klass);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_binary_content_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_binary_content_finalize;
+
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : content = instance à initialiser. *
+* *
+* Description : Initialise une instance de contenu de données binaires. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_binary_content_init(GBinContent *content)
+{
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : content = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_binary_content_dispose(GBinContent *content)
+{
+ G_OBJECT_CLASS(g_binary_content_parent_class)->dispose(G_OBJECT(content));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : content = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_binary_content_finalize(GBinContent *content)
+{
+ size_t i; /* Boucle de parcours */
+
+ for (i = 0; i < content->count; i++)
+ free(content->parts[i].data);
+
+ if (content->parts != NULL)
+ free(content->parts);
+
+ G_OBJECT_CLASS(g_binary_content_parent_class)->finalize(G_OBJECT(content));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : filename = chemin d'accès au fichier à charger. *
+* *
+* Description : Charge en mémoire le contenu d'un fichier donné. *
+* *
+* Retour : Représentation de contenu à manipuler ou NULL en cas d'échec.*
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GBinContent *g_binary_content_new_from_file(const char *filename)
+{
+ GBinContent *result; /* Structure à retourner */
+ int fd; /* Descripteur du fichier */
+ struct stat info; /* Informations sur le fichier */
+ int ret; /* Bilan d'un appel */
+ void *content; /* Contenu brut du fichier */
+ vmpa2t base; /* Localisation des données */
+
+ /* Récupération des données */
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ {
+ perror("open");
+ goto gbcnff_error;
+ }
+
+ ret = fstat(fd, &info);
+ if (ret == -1)
+ {
+ close(fd);
+ perror("fstat");
+ goto gbcnff_error;
+ }
+
+ content = mmap(NULL, info.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (content == MAP_FAILED)
+ {
+ close(fd);
+ perror("mmap");
+ goto gbcnff_error;
+ }
+
+ /* Constitution du contenu officiel */
+
+ result = g_object_new(G_TYPE_BIN_CONTENT, NULL);
+
+ result->parts = (binary_part *)calloc(1, sizeof(binary_part));
+ result->count = 1;
+
+ result->parts[0].data = (bin_t *)malloc(info.st_size);
+ memcpy(result->parts[0].data, content, info.st_size);
+
+ munmap(content, info.st_size);
+ close(fd);
+
+ init_vmpa(&base, 0, VMPA_NO_VIRTUAL);
+ init_mrange(&result->parts[0].range, &base, info.st_size);
+
+ return result;
+
+ gbcnff_error:
+
+ return NULL;
+
+}
+
+
+
+
+
+
+
+
+/******************************************************************************
+* *
+* Paramètres : content = contenu binaire à venir lire. *
+* addr = position de la tête de lecture. *
+* length = quantité d'octets à lire. *
+* out = réceptacle disponible pour ces données. [OUT] *
+* *
+* Description : Fournit une portion des données représentées. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool g_binary_content_get_raw(const GBinContent *content, const vmpa2t *addr, phys_t length, bin_t *out)
+{
+ /* FIXME */
+
+ memcpy(out, &content->parts[0].data[get_phy_addr(addr)], length);
+
+ return true;
+
+}
+
+
+
+
+
+
+
+
+const bin_t *g_binary_content_get(GBinContent *content, off_t *length)
+{
+ *length = content->parts[0].range.length;
+
+ return content->parts[0].data;
+
+}
+
+
diff --git a/src/glibext/gbincontent.h b/src/glibext/gbincontent.h
new file mode 100644
index 0000000..1bfcfa5
--- /dev/null
+++ b/src/glibext/gbincontent.h
@@ -0,0 +1,73 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * gbincontent.h - prototypes pour le chargement de données binaires en mémoire
+ *
+ * Copyright (C) 2015 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * OpenIDA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _GLIBEXT_GBINCONTENT_H
+#define _GLIBEXT_GBINCONTENT_H
+
+
+#include <glib-object.h>
+
+
+#include "../arch/archbase.h"
+#include "../arch/vmpa.h"
+#include "../common/endianness.h"
+
+
+
+#define G_TYPE_BIN_CONTENT (g_binary_content_get_type())
+#define G_BIN_CONTENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_BIN_CONTENT, GBinContent))
+#define G_IS_BIN_CONTENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_BIN_CONTENT))
+#define G_BIN_CONTENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_BIN_CONTENT, GBinContentClass))
+#define G_IS_BIN_CONTENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_BIN_CONTENT))
+#define G_BIN_CONTENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_BIN_CONTENT, GBinContentClass))
+
+
+/* Content de données binaires quelconques (instance) */
+typedef struct _GBinContent GBinContent;
+
+/* Content de données binaires quelconques (classe) */
+typedef struct _GBinContentClass GBinContentClass;
+
+
+
+/* Indique le type défini par la GLib pour les contenus de données. */
+GType g_binary_content_get_type(void);
+
+/* Charge en mémoire le contenu d'un fichier donné. */
+GBinContent *g_binary_content_new_from_file(const char *);
+
+
+
+/* Fournit une portion des données représentées. */
+bool g_binary_content_get_raw(const GBinContent *, const vmpa2t *, phys_t, bin_t *);
+
+
+
+
+const bin_t *g_binary_content_get(GBinContent *content, off_t *length);
+
+
+
+
+
+#endif /* _GLIBEXT_GBINCONTENT_H */