about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFlorent Monjalet <florent.monjalet@gmail.com>2015-11-13 16:39:02 +0100
committerFlorent Monjalet <florent.monjalet@gmail.com>2016-01-18 14:02:31 +0100
commita089dad25524bd8b173974169576d843f651fd52 (patch)
tree7e660d0b614e6c6ac0edf74b59b0f31860aaff73
parente53ac5b2a65f79d6342b1820c68efc126c8e4986 (diff)
downloadmiasm-a089dad25524bd8b173974169576d843f651fd52.tar.gz
miasm-a089dad25524bd8b173974169576d843f651fd52.zip
MemStruct: method reorganization
Moved methods from _MetaMemStruct to MemStruct for more clarity
Diffstat (limited to '')
-rw-r--r--miasm2/analysis/mem.py92
1 files changed, 50 insertions, 42 deletions
diff --git a/miasm2/analysis/mem.py b/miasm2/analysis/mem.py
index a42df7ba..4ba3f842 100644
--- a/miasm2/analysis/mem.py
+++ b/miasm2/analysis/mem.py
@@ -335,48 +335,6 @@ class _MetaMemStruct(type):
         super(_MetaMemStruct, cls).__init__(name, bases, dct)
         cls.gen_fields()
 
-    def gen_fields(cls, fields=None):
-        if fields is None:
-            fields = cls.fields
-        cls._attrs = {}
-        offset = 0
-        for name, field in cls.fields:
-            # For reflexion
-            field.set_self_type(cls)
-            cls.gen_attr(name, field, offset)
-            offset += field.size()
-        cls._size = offset
-
-    def gen_attr(cls, name, field, offset):
-        # FIXME: move to gen_simple_arg?
-        cls._attrs[name] = {"field": field, "offset": offset}
-        cls._gen_simple_attr(name, field, offset)
-        if isinstance(field, Union):
-            cls._gen_union_attr(field, offset)
-
-    def _gen_simple_attr(cls, name, field, offset):
-        # Generate self.<name> getter and setter
-        setattr(cls, name, property(
-            # default parameter allow to bind the value of name for a given
-            # loop iteration
-            lambda self, name=name: self.get_attr(name),
-            lambda self, val, name=name: self.set_attr(name, val)
-        ))
-
-        # Generate self.deref_<name> getter and setter if this field is a
-        # Ptr
-        if isinstance(field, Ptr):
-            setattr(cls, "deref_%s" % name, property(
-                lambda self, name=name: self.deref_attr(name),
-                lambda self, val, name=name: self.set_deref_attr(name, val)
-            ))
-
-    def _gen_union_attr(cls, union_field, offset):
-        if not isinstance(union_field, Union):
-            raise ValueError("field should be an Union instance")
-        for name, field in union_field.field_list:
-            cls.gen_attr(name, field, offset)
-
     def __repr__(cls):
         return cls.__name__
 
@@ -388,6 +346,8 @@ class MemStruct(object):
 
     _size = None
 
+    # Classic usage methods
+
     def __init__(self, vm, addr=None, *args, **kwargs):
         global allocator
         super(MemStruct, self).__init__(*args, **kwargs)
@@ -495,6 +455,54 @@ class MemStruct(object):
     def __ne__(self, other):
         return not (self == other)
 
+    # Field generation methods, voluntarily public
+
+    @classmethod
+    def gen_fields(cls, fields=None):
+        if fields is None:
+            fields = cls.fields
+        cls._attrs = {}
+        offset = 0
+        for name, field in cls.fields:
+            # For reflexion
+            field.set_self_type(cls)
+            cls.gen_attr(name, field, offset)
+            offset += field.size()
+        cls._size = offset
+
+    @classmethod
+    def gen_attr(cls, name, field, offset):
+        # FIXME: move to gen_simple_arg?
+        cls._attrs[name] = {"field": field, "offset": offset}
+        cls._gen_simple_attr(name, field, offset)
+        if isinstance(field, Union):
+            cls._gen_union_attr(field, offset)
+
+    @classmethod
+    def _gen_simple_attr(cls, name, field, offset):
+        # Generate self.<name> getter and setter
+        setattr(cls, name, property(
+            # default parameter allow to bind the value of name for a given
+            # loop iteration
+            lambda self, name=name: self.get_attr(name),
+            lambda self, val, name=name: self.set_attr(name, val)
+        ))
+
+        # Generate self.deref_<name> getter and setter if this field is a
+        # Ptr
+        if isinstance(field, Ptr):
+            setattr(cls, "deref_%s" % name, property(
+                lambda self, name=name: self.deref_attr(name),
+                lambda self, val, name=name: self.set_deref_attr(name, val)
+            ))
+
+    @classmethod
+    def _gen_union_attr(cls, union_field, offset):
+        if not isinstance(union_field, Union):
+            raise ValueError("field should be an Union instance")
+        for name, field in union_field.field_list:
+            cls.gen_attr(name, field, offset)
+
 
 class MemSelf(MemStruct):
     """Special Marker class for reference to current class in a Ptr."""