about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCamille Mougey <commial@gmail.com>2015-06-11 18:05:42 +0200
committerCamille Mougey <commial@gmail.com>2015-06-11 18:05:42 +0200
commit15b777061e96f55f2bfd2f7233d67f12b8f9f575 (patch)
tree602a54119897928445fd88ea5706a0ec35cb8fc8
parente4557a93d4bafb3ae86a0fc5280592ee98c1fb9f (diff)
parent89f0c6718384557e9a1e9341310f0770eec5e0a2 (diff)
downloadmiasm-15b777061e96f55f2bfd2f7233d67f12b8f9f575.tar.gz
miasm-15b777061e96f55f2bfd2f7233d67f12b8f9f575.zip
Merge pull request #175 from serpilliere/add_simp_slice
Add simp slice
-rw-r--r--miasm2/expression/simplifications_common.py33
-rw-r--r--test/expression/simplifications.py53
2 files changed, 84 insertions, 2 deletions
diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py
index ab3e2e82..b8c78692 100644
--- a/miasm2/expression/simplifications_common.py
+++ b/miasm2/expression/simplifications_common.py
@@ -416,12 +416,43 @@ def simp_slice(e_s, e):
         new_e = ExprSlice(e.arg.arg, e.start + e.arg.start,
                           e.start + e.arg.start + (e.stop - e.start))
         return new_e
-    # Slice(Compose(A), x) => Slice(A, y)
     elif isinstance(e.arg, ExprCompose):
+        # Slice(Compose(A), x) => Slice(A, y)
         for a in e.arg.args:
             if a[1] <= e.start and a[2] >= e.stop:
                 new_e = a[0][e.start - a[1]:e.stop - a[1]]
                 return new_e
+        # Slice(Compose(A, B, C), x) => Compose(A, B, C) with truncated A/B/C
+        out = []
+        for arg, s_start, s_stop in e.arg.args:
+            # arg is before slice start
+            if e.start >= s_stop:
+                continue
+            # arg is after slice stop
+            elif e.stop <= s_start:
+                continue
+            # arg is fully included in slice
+            elif e.start <= s_start and s_stop <= e.stop:
+                out.append((arg, s_start, s_stop))
+                continue
+            # arg is truncated at start
+            if e.start > s_start:
+                slice_start = e.start - s_start
+                a_start = 0
+            else:
+                # arg is not truncated at start
+                slice_start = 0
+                a_start = s_start - e.start
+            # a is truncated at stop
+            if e.stop < s_stop:
+                slice_stop = arg.size + e.stop - s_stop - slice_start
+                a_stop = e.stop - e.start
+            else:
+                slice_stop = arg.size
+                a_stop = s_stop - e.start
+            out.append((arg[slice_start:slice_stop], a_start, a_stop))
+        return ExprCompose(out)
+
     # ExprMem(x, size)[:A] => ExprMem(x, a)
     # XXXX todo hum, is it safe?
     elif (isinstance(e.arg, ExprMem) and
diff --git a/test/expression/simplifications.py b/test/expression/simplifications.py
index d3e1c979..2088fc7c 100644
--- a/test/expression/simplifications.py
+++ b/test/expression/simplifications.py
@@ -201,8 +201,59 @@ to_test = [(ExprInt32(1) - ExprInt32(1), ExprInt32(0)),
      (ExprCompose([(a, 0, 32), (ExprInt32(0), 32, 64)]) * ExprInt64(0x123))[32:64]),
 
     (ExprInt32(0x12),
-     ExprInt32(0x12L))
+     ExprInt32(0x12L)),
+
+
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[:16],
+     a[:16]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[16:32],
+     a[16:]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[32:48],
+     b[:16]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[48:64],
+     b[16:]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[64:80],
+     c[:16]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[80:],
+     c[16:]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[80:82],
+     c[16:18]),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[16:48],
+     ExprCompose(((a[16:], 0, 16), (b[:16], 16, 32)))),
+    (ExprCompose(((a, 0, 32), (b, 32, 64), (c, 64, 96)))[48:80],
+     ExprCompose(((b[16:], 0, 16), (c[:16], 16, 32)))),
+
+    (ExprCompose(((a[0:8], 0, 8),
+                  (b[8:16], 8, 16),
+                  (ExprInt(uint48(0x0L)), 16, 64)))[12:32],
+     ExprCompose(((b[12:16], 0, 4), (ExprInt(uint16(0)), 4, 20)))
+       ),
+
+    (ExprCompose(((ExprCompose(((a[:8], 0, 8),
+                                (ExprInt(uint56(0x0L)), 8, 64)))[8:32]
+                   &
+                   ExprInt(uint24(0x1L)), 0, 24),
+                  (ExprInt(uint40(0x0L)), 24, 64))),
+     ExprInt64(0)),
 
+    (ExprCompose(((ExprCompose(((a[:8], 0, 8),
+                                (ExprInt(uint56(0x0L)), 8, 64)))[:8]
+                   &
+                   ExprInt(uint8(0x1L)), 0, 8),
+                  (ExprInt(uint56(0x0L)), 8, 64))),
+     ExprCompose(((a[:8]&ExprInt8(1), 0, 8), (ExprInt(uint56(0)), 8, 64)))),
+
+    (ExprCompose(((ExprCompose(((a[:8], 0, 8),
+                                (ExprInt(uint56(0x0L)), 8, 64)))[:32]
+                   &
+                   ExprInt(uint32(0x1L)), 0, 32),
+                  (ExprInt(uint32(0x0L)), 32, 64))),
+     ExprCompose(((ExprCompose(((ExprSlice(a, 0, 8), 0, 8),
+                                (ExprInt(uint24(0x0L)), 8, 32)))
+                   &
+                   ExprInt(uint32(0x1L)), 0, 32),
+                  (ExprInt(uint32(0x0L)), 32, 64)))
+       ),
 
 ]