From 0164ddde7b01a6e3e3aa84ebdeb4ea9b381f063a Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 27 Jul 2008 23:43:10 +0000
Subject: Fixed a bug when reading immediate values.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@8 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog          |  8 +++++
 src/arch/operand.c | 13 ++++----
 src/binary.c       | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 105 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e6085d5..b2e5bcb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-07-28  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/arch/operand.c:
+	Fix a bug when reading immediate values (|| != |).
+
+	* src/binary.c:
+	Map the content of a given file into memory.
+
 2008-07-27  Cyrille Bagard <nocbos@gmail.com>
 
 	* configure.ac:
diff --git a/src/arch/operand.c b/src/arch/operand.c
index cd7c2b4..33807bc 100644
--- a/src/arch/operand.c
+++ b/src/arch/operand.c
@@ -133,19 +133,18 @@ bool fill_imm_operand(asm_operand *operand, AsmOperandSize size, const uint8_t *
             *pos += 1;
             break;
         case AOS_16_BITS:
-            operand->value.val16 = data[*pos] || (data[*pos + 1] << 8);
+            operand->value.val16 = data[*pos] | (uint16_t)data[*pos + 1] << 8;
             *pos += 2;
             break;
         case AOS_32_BITS:
-            operand->value.val32 = data[*pos] || (data[*pos + 1] << 8) || (data[*pos + 2] << 16);
+            operand->value.val32 = data[*pos] | (uint32_t)data[*pos + 1] << 8
+                | (uint32_t)data[*pos + 2] << 16 | (uint32_t)data[*pos + 3] << 24;
             *pos += 4;
             break;
         case AOS_64_BITS:
-            /*
-            operand->value.val64 = data[*pos] || (data[*pos + 1] << 8) || (data[*pos + 2] << 16)
-                || (data[*pos + 3] << 24) || (data[*pos + 4] << 32) || (data[*pos + 5] << 40)
-                || (data[*pos + 6] << 48) || (data[*pos + 7] << 56);
-            */
+            operand->value.val64 = data[*pos] | (uint64_t)data[*pos + 1] << 8 | (uint64_t)data[*pos + 2] << 16
+                | (uint64_t)data[*pos + 3] << 24 | (uint64_t)data[*pos + 4] << 32 | (uint64_t)data[*pos + 5] << 40
+                | (uint64_t)data[*pos + 6] << 48 | (uint64_t)data[*pos + 7] << 56;
             *pos += 8;
             break;
     }
diff --git a/src/binary.c b/src/binary.c
index fcbb49f..a026021 100644
--- a/src/binary.c
+++ b/src/binary.c
@@ -24,18 +24,97 @@
 #include "binary.h"
 
 
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+
+
 #include "arch/processor.h"
 
 
 
+
+/* Charge en mémoire le contenu d'un fichier. */
+uint8_t *map_binary_file(const char *, size_t *);
+
+
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : filename = nom du fichier à charger.                         *
+*                length   = taille des données mises en mémoire. [OUT]        *
+*                                                                             *
+*  Description : Charge en mémoire le contenu d'un fichier.                   *
+*                                                                             *
+*  Retour      : Adresse du contenu binaire ou NULL en cas d'échec.           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+uint8_t *map_binary_file(const char *filename, size_t *length)
+{
+    uint8_t *result;                        /* Données à retourner         */
+    int fd;                                 /* Fichier ouvert en lecture   */
+    struct stat info;                       /* Informations sur le fichier */
+    int ret;                                /* Bilan d'un appel            */
+
+    fd = open(filename, 0, O_RDONLY);
+    if (fd == -1)
+    {
+        perror("open()");
+        return NULL;
+    }
+
+    ret = fstat(fd, &info);
+    if (ret == -1)
+    {
+        perror("fstat()");
+        close(fd);
+        return NULL;
+    }
+
+    *length = info.st_size;
+
+    result = (uint8_t *)mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
+    if (result == MAP_FAILED)
+    {
+        perror("mmap()");
+        result = NULL;
+    }
+
+    ret = close(fd);
+    if (ret == -1)
+        perror("close()");
+
+    return result;
+
+}
+
+
+
+
+
+
+
 void fill_snippet(GtkSnippet *snippet)
 {
+    size_t length;
+    uint8_t *bin_data;
+    int ret;
+
+
     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";
+    uint8_t *data = "\x66\xba\x0c\x00\x00\x00\x66\xb9\x28\x00\xee\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";
 
-    uint8_t *data = "\x66\xbb\x00\x00\x00\x00\x66\xb8\x01\x00\x00\x00\xcd\x80\x90";
+    //uint8_t *data = "\x66\xbb\x00\x00\x00\x00\x66\xb8\x01\x00\x00\x00\xcd\x80\x90";
 
     off_t pos;
     off_t len;
@@ -48,8 +127,17 @@ void fill_snippet(GtkSnippet *snippet)
     proc = create_x86_processor();
 
     pos = 0;
-    len = 15;
+    len = 0x28;
+
+
+
+    bin_data = map_binary_file("/tmp/hello", &length);
+
+    printf(" ~~ bin_data ~~ :: %p\n", bin_data);
+
+
 
+    ret = munmap(bin_data, length);
 
 
     gtk_snippet_set_processor(snippet, proc);
-- 
cgit v0.11.2-87-g4458