about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorserpilliere <devnull@localhost>2012-06-21 14:39:26 +0200
committerserpilliere <devnull@localhost>2012-06-21 14:39:26 +0200
commit3af3f8e0f4ad616c9077fec6efa58cb6c7240812 (patch)
tree80223b63273dc9c54ffddddc0052140b7529163d
parent50c79519af9c768eb1cddc02792b5c68a433bd4c (diff)
downloadmiasm-3af3f8e0f4ad616c9077fec6efa58cb6c7240812.tar.gz
miasm-3af3f8e0f4ad616c9077fec6efa58cb6c7240812.zip
tools: add modint; remove numpy dependency
-rw-r--r--example/expression/manip_expression6.py2
-rw-r--r--miasm/arch/arm_arch.py2
-rw-r--r--miasm/arch/ia32_arch.py12
-rw-r--r--miasm/arch/ia32_sem.py7
-rw-r--r--miasm/core/asmbloc.py9
-rw-r--r--miasm/core/memory_pool.py2
-rw-r--r--miasm/core/parse_ad.py32
-rw-r--r--miasm/expression/expression.py16
-rw-r--r--miasm/expression/expression_eval_abstract.py8
-rw-r--r--miasm/tools/modint.py246
10 files changed, 280 insertions, 56 deletions
diff --git a/example/expression/manip_expression6.py b/example/expression/manip_expression6.py
index 65d43f04..024de0f4 100644
--- a/example/expression/manip_expression6.py
+++ b/example/expression/manip_expression6.py
@@ -101,6 +101,8 @@ to_test = [(ExprInt32(5)+c+a+b-a+ExprInt32(1)-ExprInt32(5)),
            ExprCond(ExprInt32(1), a, b),
            ExprCond(ExprInt32(0), b, a),
 
+           ExprInt32(0x80000000)[31:32],
+
 
            ]
 
diff --git a/miasm/arch/arm_arch.py b/miasm/arch/arm_arch.py
index 5cffb5d7..46da2c78 100644
--- a/miasm/arch/arm_arch.py
+++ b/miasm/arch/arm_arch.py
@@ -15,7 +15,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-from numpy import uint8, uint16, uint32, uint64, int8, int16, int32, int64
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
 import shlex
 import struct
 
diff --git a/miasm/arch/ia32_arch.py b/miasm/arch/ia32_arch.py
index 5509a7c6..8672b447 100644
--- a/miasm/arch/ia32_arch.py
+++ b/miasm/arch/ia32_arch.py
@@ -15,7 +15,7 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-from numpy import uint8, uint16, uint32, uint64, int8, int16, int32, int64
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
 import struct
 import logging
 from miasm.core.parse_ad import parse_ad, ad_to_generic
@@ -30,22 +30,16 @@ log.addHandler(console_handler)
 log.setLevel(logging.WARN)
 
 
-tab_int_size = {int8:8,
-                uint8:8,
-                int16:16,
+tab_int_size = {uint8:8,
                 uint16:16,
-                int32:32,
                 uint32:32,
-                int64:64,
                 uint64:64
                 }
 
 tab_size2int = {x86_afs.u08:uint8,
                 x86_afs.u16:uint16,
                 x86_afs.u32:uint32,
-                x86_afs.s08:int8,
-                x86_afs.s16:int16,
-                x86_afs.s32:int32}
+                }
 
 tab_max_uint = {x86_afs.u08:0xFF, x86_afs.u16:0xFFFF, x86_afs.u32:0xFFFFFFFF, x86_afs.u64:0xFFFFFFFFFFFFFFFFL}
 
diff --git a/miasm/arch/ia32_sem.py b/miasm/arch/ia32_sem.py
index d9f8e557..622719d4 100644
--- a/miasm/arch/ia32_sem.py
+++ b/miasm/arch/ia32_sem.py
@@ -373,11 +373,6 @@ all_registers = [
 
     ]
 
-tab_intsize = {8:int8,
-               16:int16,
-               32:int32,
-               64:int64
-               }
 tab_uintsize ={8:uint8,
                16:uint16,
                32:uint32,
@@ -409,7 +404,7 @@ OF(A-B) = ((A XOR D) AND (A XOR B)) < 0
 
 # XXX TODO make default check against 0 or not 0 (same eq as in C)
 def get_op_msb(a):
-    return ExprOp('&', ExprOp('>>', a, ExprInt_from(a, a.get_size()-1)), ExprInt_from(a, 1))
+    return a[a.get_size()-1:a.get_size()]
 
 
 def update_flag_zf(a):
diff --git a/miasm/core/asmbloc.py b/miasm/core/asmbloc.py
index 0ad2a1fb..ddf3816c 100644
--- a/miasm/core/asmbloc.py
+++ b/miasm/core/asmbloc.py
@@ -19,7 +19,8 @@ import re
 import logging
 import shlex
 import struct
-from numpy import uint8, uint16, uint32, uint64, int8, int16, int32, int64
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
+from miasm.tools.modint import int8, int16, int32, int64
 from collections import defaultdict
 log_asmbloc = logging.getLogger("asmbloc")
 console_handler = logging.StreamHandler()
@@ -27,13 +28,9 @@ console_handler.setFormatter(logging.Formatter("%(levelname)-5s: %(message)s"))
 log_asmbloc.addHandler(console_handler)
 log_asmbloc.setLevel(logging.WARN)
 
-tab_int_size = {int8:8,
-                uint8:8,
-                int16:16,
+tab_int_size = {uint8:8,
                 uint16:16,
-                int32:32,
                 uint32:32,
-                int64:64,
                 uint64:64
                 }
 
diff --git a/miasm/core/memory_pool.py b/miasm/core/memory_pool.py
index a0ddaf1b..ff68de47 100644
--- a/miasm/core/memory_pool.py
+++ b/miasm/core/memory_pool.py
@@ -19,7 +19,7 @@ import array
 import struct
 import cPickle
 import StringIO
-from numpy import uint32
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
 
 from elfesteem import *
 
diff --git a/miasm/core/parse_ad.py b/miasm/core/parse_ad.py
index 90cfc0d1..3de0a3b2 100644
--- a/miasm/core/parse_ad.py
+++ b/miasm/core/parse_ad.py
@@ -16,19 +16,19 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 from miasm.arch.ia32_reg import x86_afs
-from numpy import int32, uint32
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
 
 def dict_add(a, b):
     tmp = dict(a)
     for k in b:
-        #special case 
+        #special case
         if k == x86_afs.symb:
             if k in tmp:
                 tmp[k] = dict_add(tmp[k], b[k])
             else:
                 tmp[k] = dict(b[k])
             continue
-        #normal case 
+        #normal case
         if k in tmp:
             tmp[k]+=b[k]
         else:
@@ -40,7 +40,7 @@ def dict_add(a, b):
 def dict_sub(a, b):
     tmp = dict(a)
     for k in b:
-        #special case 
+        #special case
         if k == x86_afs.symb:
             if k in tmp:
                 tmp[k] = dict_sub(tmp[k], b[k])
@@ -73,7 +73,7 @@ def dict_mul(a, b):
             else:
                 ret[k] = b[x86_afs.imm]*a[k]
         return ret
-    
+
     raise 'bad dict mul %s'%(str(a)+str(b))
 
 keywords = ("BYTE", "WORD", "DWORD", "SINGLE", "DOUBLE",
@@ -85,7 +85,7 @@ tokens = keywords +(
     'NUMBER',
     'PLUS','MINUS','TIMES','DIVIDE',
     'LPAREN','RPAREN','LBRA','RBRA', 'COLON',
-    'OFFSET','NAME', 
+    'OFFSET','NAME',
     )
 
 # Tokens
@@ -128,7 +128,7 @@ t_ignore = " \t"
 def t_newline(t):
     r'\n+'
     t.lexer.lineno += t.value.count("\n")
-    
+
 def t_error(t):
     print("Illegal character '%s'" % t.value[0])
     t.lexer.skip(1)
@@ -200,8 +200,8 @@ def p_expression_8(t):
         size = x86_afs.u32
     elif t[1] in x86_afs.reg_sg:
         size = x86_afs.u32
-        
-        
+
+
     else:
         #raise 'bad reg size'
         t[0] = {x86_afs.symb:{t[1]:1}}
@@ -292,11 +292,11 @@ def parse_ad(a):
         l[x86_afs.ad] = False
     else:
         l[x86_afs.size] = l[x86_afs.ad]
-        
+
     if not x86_afs.size in l:
         l[x86_afs.size] = x86_afs.u32
-        
-        
+
+
 
     return l
 
@@ -304,7 +304,7 @@ import ply.yacc as yacc
 yacc.yacc()
 
 def ad_to_generic(a):
-    
+
     #opt imm
     out = []
     to_add = []
@@ -312,7 +312,7 @@ def ad_to_generic(a):
     if a[x86_afs.ad]:
         a[x86_afs.ad] = True
 
-        
+
         #imm can always be encoded in u32
         to_add.append({x86_afs.imm:x86_afs.u32})
 
@@ -334,7 +334,7 @@ def ad_to_generic(a):
             to_add.append({x86_afs.imm:x86_afs.s08})
         if i<=0xFF and i >=0 :
             to_add.append({x86_afs.imm:x86_afs.u08})
-            
+
     for kv in to_add:
         tmp = dict(a)
         tmp.update(kv)
@@ -346,5 +346,5 @@ def ad_to_generic(a):
             out_unik.append(o)
 
     return out_unik
- 
+
 
diff --git a/miasm/expression/expression.py b/miasm/expression/expression.py
index b6f32a6b..aa164ed4 100644
--- a/miasm/expression/expression.py
+++ b/miasm/expression/expression.py
@@ -15,7 +15,8 @@
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
-from numpy import uint8, uint16, uint32, uint64, int8, int16, int32, int64
+from miasm.tools.modint import uint1, uint8, uint16, uint32, uint64
+from miasm.tools.modint import int8, int16, int32, int64
 tip = 'tip'
 
 def slice_rest(size, start, stop):
@@ -35,13 +36,9 @@ size2type ={8:uint8,
             64:uint64
             }
 
-tab_int_size = {int8:8,
-                uint8:8,
-                int16:16,
+tab_int_size = {uint8:8,
                 uint16:16,
-                int32:32,
                 uint32:32,
-                int64:64,
                 uint64:64
                 }
 
@@ -49,8 +46,7 @@ my_size_mask = {1:1, 8:0xFF, 16:0xFFFF, 32:0xFFFFFFFF,  64:0xFFFFFFFFFFFFFFFFL}
 
 
 def is_int(a):
-    t = [int8, int16, int32, int64,
-         uint8, uint16, uint32, uint64]
+    t = [uint1, uint8, uint16, uint32, uint64]
     return any([isinstance(a, x) for x in t])
 
 
@@ -200,7 +196,7 @@ class ExprInt(Expr):
     def get_w(self):
         return set()
     def get_size(self):
-        return 8*self.arg.nbytes
+        return self.arg.size
     def __contains__(self, e):
         return self == e
     def __eq__(self, a):
@@ -763,7 +759,7 @@ def canonize_expr_list_compose(l):
     l.sort(cmp=compare_exprs_compose)
     return l
 
-tab_uintsize ={1:uint8,
+tab_uintsize ={1:uint8,# XXX todo hack
                8:uint8,
                16:uint16,
                32:uint32,
diff --git a/miasm/expression/expression_eval_abstract.py b/miasm/expression/expression_eval_abstract.py
index 153208d2..43feb031 100644
--- a/miasm/expression/expression_eval_abstract.py
+++ b/miasm/expression/expression_eval_abstract.py
@@ -19,12 +19,10 @@ from miasm.expression.expression import *
 import struct
 import logging
 import cPickle
-import numpy
 from miasm.expression.expression_helper import *
 
 
 
-numpy.seterr(over='ignore', under='ignore')
 
 mymaxuint = {8:0xFFL,
              16:0xFFFFL,
@@ -35,13 +33,9 @@ mymaxuint = {8:0xFFL,
 
 #expression evaluation in integer domain
 
-tab_int_size = {int8:8,
-                uint8:8,
-                int16:16,
+tab_int_size = {uint8:8,
                 uint16:16,
-                int32:32,
                 uint32:32,
-                int64:64,
                 uint64:64
                 }
 
diff --git a/miasm/tools/modint.py b/miasm/tools/modint.py
new file mode 100644
index 00000000..dda29b4a
--- /dev/null
+++ b/miasm/tools/modint.py
@@ -0,0 +1,246 @@
+import os
+
+class moduint(object):
+    def __init__(self, arg):
+        self.arg = arg%self.__class__.limit
+        assert(self.arg >= 0 and self.arg < self.__class__.limit)
+    def __repr__(self):
+        return self.__class__.__name__+'('+hex(self.arg)+')'
+    def __hash__(self):
+        return hash(self.arg)
+    @classmethod
+    def maxcast(c1, c2):
+        c2 = c2.__class__
+        if c1.size > c2.size:
+            return c1
+        else:
+            return c2
+    def __cmp__(self, y):
+        if isinstance(y, moduint):
+            return cmp(self.arg, y.arg)
+        else:
+            return cmp(self.arg, y)
+    def __add__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg + y.arg)
+        else:
+            return self.__class__(self.arg + y)
+    def __and__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg & y.arg)
+        else:
+            return self.__class__(self.arg & y)
+    def __div__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg / y.arg)
+        else:
+            return self.__class__(self.arg / y)
+    def __int__(self):
+        return int(self.arg)
+    def __long__(self):
+        return long(self.arg)
+    def __invert__(self):
+        return self.__class__(~self.arg)
+    def __lshift__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg << y.arg)
+        else:
+            return self.__class__(self.arg << y)
+    def __mod__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg % y.arg)
+        else:
+            return self.__class__(self.arg % y)
+    def __mul__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg * y.arg)
+        else:
+            return self.__class__(self.arg * y)
+    def __neg__(self):
+        return self.__class__(-self.arg)
+    def __or__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg | y.arg)
+        else:
+            return self.__class__(self.arg | y)
+    def __radd__(self, y):
+        return self.__add__(y)
+    def __rand__(self, y):
+        return self.__and__(y)
+    def __rdiv__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(y.arg / self.arg)
+        else:
+            return self.__class__(y / self.arg)
+    def __rlshift__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(y.arg << self.arg)
+        else:
+            return self.__class__(y << self.arg)
+    def __rmod__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(y.arg % self.arg )
+        else:
+            return self.__class__(y % self.arg)
+    def __rmul__(self, y):
+        return self.__mul__(y)
+    def __ror__(self, y):
+        return self.__or__(y)
+    def __rrshift__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(y.arg >> self.arg )
+        else:
+            return self.__class__(y >> self.arg)
+    def __rshift__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg >> y.arg)
+        else:
+            return self.__class__(self.arg >> y)
+    def __rsub__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(y.arg - self.arg)
+        else:
+            return self.__class__(y - self.arg)
+    def __rxor__(self, y):
+        return self.__xor__(y)
+    def __sub__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg - y.arg)
+        else:
+            return self.__class__(self.arg - y)
+    def __xor__(self, y):
+        if isinstance(y, moduint):
+            cls = self.maxcast(y)
+            return cls(self.arg ^ y.arg)
+        else:
+            return self.__class__(self.arg ^ y)
+    def __hex__(self):
+        return hex(self.arg)
+
+class modint(moduint):
+    def __init__(self, arg):
+        a = arg%self.__class__.limit
+        if a >= self.__class__.limit/2:
+            a -= self.__class__.limit
+        self.arg = a
+        assert(self.arg >= -self.__class__.limit/2 and self.arg < self.__class__.limit)
+
+
+class uint1(moduint):
+    size = 1
+    limit = 1<<size
+
+class uint8(moduint):
+    size = 8
+    limit = 1<<size
+
+class uint16(moduint):
+    size = 16
+    limit = 1<<size
+
+class uint32(moduint):
+    size = 32
+    limit = 1<<size
+
+class uint64(moduint):
+    size = 64
+    limit = 1<<size
+
+class uint128(moduint):
+    size = 128
+    limit = 1<<size
+
+class int8(modint):
+    size = 8
+    limit = 1<<size
+
+class int16(modint):
+    size = 16
+    limit = 1<<size
+
+class int32(modint):
+    size = 32
+    limit = 1<<size
+
+class int64(modint):
+    size = 64
+    limit = 1<<size
+
+class int128(modint):
+    size = 128
+    limit = 1<<size
+
+
+
+if __name__ == "__main__":
+    a = uint8(0x42)
+    b = uint8(0xFF)
+    c = uint8(0x4)
+
+    d = uint1(0)
+    e = uint1(1)
+
+    f = uint8(0x1)
+
+
+    print a, b, c
+    print a+b, a+c, b+c
+    print a == a, a == b, a == 0x42, a == 0x78
+    print a != b, a != a
+    print d, e
+    print d+e, d+d, e+e, e+e+e, e+0x11
+
+    assert(f == 1)
+    assert(f+1 == 2)
+    assert(2 == f+1)
+    assert(f+0xff==0)
+    assert(f&0==0)
+    assert(f&0xff==f)
+    assert(0xff&f==f)
+    assert(f/1==f)
+    assert(1/f==f)
+    assert(int(f)==1)
+    assert(long(f)==1)
+    assert(~f==0xfe)
+    assert(f<<1==2)
+    assert(f<<8==0)
+    assert(1<<f==2)
+    assert(0x80<<f==0)
+    assert(f%2==f)
+    assert(f%1==0)
+    assert(2%f==0)
+    assert(f*2==2)
+    assert(2*f==2)
+    assert(f*f==1)
+    assert(f*uint8(0x80)==0x80)
+    assert(-f==0xff)
+    assert(f|f==f)
+    assert(f|0==f)
+    assert(2|f==3)
+    assert(f>>0==f)
+    assert(f>>1==0)
+    assert(0x10>>f==0x8)
+    assert(0x100>>f==0x80) # XXXX
+    assert(0x1000>>f==0x0) # XXXX
+    assert(f^f==0)
+    assert(f^0==f)
+    assert(0^f==f)
+    assert(1^f==0)
+
+    print e+c, c+e, c-e, e-c
+    print 1000*a
+    print hex(a)