about summary refs log tree commit diff stats
path: root/test/core
diff options
context:
space:
mode:
authorFlorent Monjalet <florent.monjalet@gmail.com>2015-12-15 16:35:31 +0100
committerFlorent Monjalet <florent.monjalet@gmail.com>2016-01-18 14:02:32 +0100
commit03b3a84e0dd4d4d01b471f1767d4aec68b9a90ad (patch)
tree55d0116b6f64987ad09c5ce580bc069c5ce060c3 /test/core
parent0379f8e91fa54fe641948f01bb98a76fab47033a (diff)
downloadmiasm-03b3a84e0dd4d4d01b471f1767d4aec68b9a90ad.tar.gz
miasm-03b3a84e0dd4d4d01b471f1767d4aec68b9a90ad.zip
Types: Support anonymous Struct/Union/BitField
See the test addition for an example. A Struct, Union, or BitField field
with no name will be considered anonymous: all its fields will be added
to the parent Struct/Union/BitField. This implements this kind of C
declaration:

struct foo {
    int a;
    union {
        int bar;
        struct {
            short baz;
            short foz;
        };
    };
}
Diffstat (limited to '')
-rw-r--r--test/core/types.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/core/types.py b/test/core/types.py
index c59a68d6..7ad8ad13 100644
--- a/test/core/types.py
+++ b/test/core/types.py
@@ -491,6 +491,33 @@ assert Array(Num("B"), 19).lval != Array(Num("B"), 20).lval
 assert MyStruct == Struct(MyStruct.__name__, MyStruct.fields).lval
 assert MyStruct.get_type() == Struct(MyStruct.__name__, MyStruct.fields)
 
+# Anonymous Unions
+class Anon(MemStruct):
+    fields = [
+        ("a", Num("B")),
+        # If a field name evaluates to False ("" or None for example) and the
+        # field type is a Struct subclass (Struct, Union, BitField), the field
+        # is considered as an anonymous struct or union. Therefore, Anon will
+        # have b1, b2 and c1, c2 attributes in that case.
+        ("", Union([("b1", Num("B")), ("b2", Num("H"))])),
+        ("", Struct("", [("c1", Num("B")), ("c2", Num("B"))])),
+        ("d", Num("B")),
+    ]
+
+anon = Anon(jitter.vm)
+anon.memset()
+anon.a = 0x07
+anon.b2 = 0x0201
+anon.c1 = 0x55
+anon.c2 = 0x77
+anon.d = 0x33
+assert anon.a == 0x07
+assert anon.b1 == 0x01
+assert anon.b2 == 0x0201
+assert anon.c1 == 0x55
+assert anon.c2 == 0x77
+assert anon.d == 0x33
+
 
 # Repr tests