From 78169fc7f8a7150cd22cfe80204cb2faaa991ba5 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 26 Mar 2017 15:44:28 +0200
Subject: Fixed the conversion of immediate operands to binary strings.

---
 ChangeLog               |  8 +++++++
 src/arch/immediate.c    | 11 +++++----
 tests/arch/immediate.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 4 deletions(-)
 create mode 100755 tests/arch/immediate.py

diff --git a/ChangeLog b/ChangeLog
index 841c7e1..1008968 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 17-03-26  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/arch/immediate.c:
+	Fix the conversion of immediate operands to binary strings.
+
+	* tests/arch/immediate.py:
+	New entry: add some tests for immediate operands.
+
+17-03-26  Cyrille Bagard <nocbos@gmail.com>
+
 	* plugins/pychrysa/arch/Makefile.am:
 	Add the 'immediate.[ch]' and 'operand.[ch]' files to
 	libpychrysaarch_la_SOURCES.
diff --git a/src/arch/immediate.c b/src/arch/immediate.c
index 5970f06..1b13ea1 100644
--- a/src/arch/immediate.c
+++ b/src/arch/immediate.c
@@ -759,7 +759,11 @@ static size_t _g_imm_operand_to_string(const GImmOperand *operand, AsmSyntax syn
     }
 
     /* Modification de la longueur fournie */
-    lmod = lmod_defs[range];
+
+    if (display != IOD_BIN)
+        lmod = lmod_defs[range];
+    else
+        lmod = "";
 
     /* Spécification de la conversion */
 
@@ -774,7 +778,8 @@ static size_t _g_imm_operand_to_string(const GImmOperand *operand, AsmSyntax syn
     else
     {
         if (operand->zpad)
-            max = range * 8 + 1;
+            max = range * 8;
+
         else
         {
             if (!msb_64(operand->raw, &max))
@@ -782,8 +787,6 @@ static size_t _g_imm_operand_to_string(const GImmOperand *operand, AsmSyntax syn
                 conv = "0";
                 max = 0;
             }
-            else
-                max++;
         }
 
         if (max > 0)
diff --git a/tests/arch/immediate.py b/tests/arch/immediate.py
new file mode 100755
index 0000000..b3ddc6d
--- /dev/null
+++ b/tests/arch/immediate.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python3-dbg
+# -*- coding: utf-8 -*-
+
+
+import pychrysalide
+from chrysacase import ChrysalideTestCase
+from pychrysalide import arch
+from pychrysalide.arch import ImmOperand
+
+
+
+class TestImmediate(ChrysalideTestCase):
+    """TestCase for arch.ImmOperand."""
+
+
+    def validateValue(self, value, size, padding, syntax, strings):
+        """Check all kinds of things with a given immediate operand."""
+
+        display = [
+            ImmOperand.IOD_BIN, ImmOperand.IOD_OCT,
+            ImmOperand.IOD_DEC,
+            ImmOperand.IOD_HEX
+        ]
+
+        for d in display:
+
+            op = ImmOperand(size, value)
+
+            self.assertTrue(op.size == size)
+            self.assertTrue(op.value == value)
+
+            op.padding = padding
+            op.display = d
+
+            string = op.to_string(syntax)
+            self.assertEqual(string, strings[d])
+
+
+    def testByteOne(self):
+        """Run sanity checks on immediate operand with value 1."""
+
+        strings = {
+            ImmOperand.IOD_BIN: 'b1',
+            ImmOperand.IOD_OCT: '01',
+            ImmOperand.IOD_DEC: '1',
+            ImmOperand.IOD_HEX: '0x1'
+        }
+
+        self.validateValue(1, arch.MDS_8_BITS_UNSIGNED, False, arch.ASX_INTEL, strings)
+
+
+    def testByteOnePadded(self):
+        """Run sanity checks on immediate operand with padded value 1."""
+
+        strings = {
+            ImmOperand.IOD_BIN: 'b00000001',
+            ImmOperand.IOD_OCT: '01',
+            ImmOperand.IOD_DEC: '1',
+            ImmOperand.IOD_HEX: '0x01'
+        }
+
+        self.validateValue(1, arch.MDS_8_BITS_UNSIGNED, True, arch.ASX_INTEL, strings)
-- 
cgit v0.11.2-87-g4458