Reformat and optimize code, including trailing whitespace and end of file newline.

This commit is contained in:
Mike Auty
2014-06-10 15:40:50 +01:00
parent 2222dd546a
commit a97bd19988
22 changed files with 187 additions and 125 deletions
+17 -1
View File
@@ -6,11 +6,14 @@ Created on 17 Feb 2013
import struct
import collections
from volatility.framework import interfaces
from volatility.framework.objects import templates
class Void(interfaces.objects.ObjectInterface):
"""Returns an object to represent void/unknown types"""
@classmethod
def template_size(cls, arguments):
"""Dummy size for Void objects"""
@@ -25,6 +28,7 @@ class Void(interfaces.objects.ObjectInterface):
def template_replace_child(cls, old_child, new_child, arguments):
"""Dummy method that does nothing for Void objects"""
class PrimitiveObject(interfaces.objects.ObjectInterface):
"""PrimitiveObject is an interface for any objects that should simulate a Python primitive"""
@@ -59,6 +63,7 @@ class PrimitiveObject(interfaces.objects.ObjectInterface):
def template_replace_child(cls, old_child, new_child, arguments):
"""Since this template can't ever have children, this method can be empty"""
class Integer(PrimitiveObject, int):
"""Primitive Object that handles standard numeric types"""
@@ -72,6 +77,7 @@ class Integer(PrimitiveObject, int):
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Integer objects require an integer to be written")
class Float(PrimitiveObject, float):
"""Primitive Object that handles double or floating point numbers"""
@@ -85,6 +91,7 @@ class Float(PrimitiveObject, float):
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Float objects require a float to be written")
class Bytes(PrimitiveObject, bytes):
"""Primitive Object that handles specific series of bytes"""
@@ -104,9 +111,10 @@ class Bytes(PrimitiveObject, bytes):
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("Bytes objects require a bytes type to be written")
class String(PrimitiveObject, str):
"""Primitive Object that handles string values
length: specifies the maximum possible length that the string could hold in memory
"""
@@ -126,8 +134,10 @@ class String(PrimitiveObject, str):
return self._context.memory.write(self._layer_name, self._offset, data)
raise TypeError("String objects require a string to be written")
class Pointer(Integer):
"""Pointer which points to another object"""
def __init__(self, context, layer_name, offset, structure_name, size = None,
parent = None, struct_format = None, target = None):
if not isinstance(target, templates.ObjectTemplate):
@@ -170,8 +180,10 @@ class Pointer(Integer):
if arguments['target'] == old_child:
arguments['target'] = new_child
class BitField(PrimitiveObject, int):
"""Object containing a field which is made up of bits rather than whole bytes"""
def __new__(cls, context, layer_name, offset, structure_name, size = None,
parent = None, target = None, start_bit = 0, end_bit = 0, **kwargs):
value = target(context = context,
@@ -192,6 +204,7 @@ class BitField(PrimitiveObject, int):
def write(self, value):
raise NotImplementedError("Writing to BitFields is not yet implemented")
class Enumeration(interfaces.objects.ObjectInterface):
"""Returns an object made up of choices"""
# FIXME: Add in body for the enumeration object
@@ -202,8 +215,10 @@ class Enumeration(interfaces.objects.ObjectInterface):
def write(self, value):
raise NotImplementedError("Writing to Enumerations is not yet implemented")
class Array(interfaces.objects.ObjectInterface, collections.Sequence):
"""Object which can contain a fixed number of an object type"""
def __init__(self, context, layer_name, offset, structure_name, size = None,
parent = None, count = 0, target = None):
if not isinstance(target, templates.ObjectTemplate):
@@ -251,6 +266,7 @@ class Array(interfaces.objects.ObjectInterface, collections.Sequence):
def write(self, value):
raise NotImplementedError("Writing to Arrays is not yet implemented")
class Struct(interfaces.objects.ObjectInterface):
"""Object which can contain members that are other objects"""
+13 -8
View File
@@ -6,15 +6,17 @@ Created on 1 Mar 2013
from volatility.framework import interfaces, validity
class ObjectTemplate(interfaces.objects.Template, validity.ValidityRoutines):
"""Factory class that produces objects that adhere to the Object interface on demand
This is effectively a method of currying, but adds more structure to avoid abuse.
It also allows inspection of information that should already be known:
* Structure size
* Members, etc
etc.
"""
def __init__(self, object_class = None, structure_name = None, **kwargs):
interfaces.objects.Template.__init__(self, structure_name = structure_name, **kwargs)
self.class_check(object_class, interfaces.objects.ObjectInterface)
@@ -36,34 +38,37 @@ class ObjectTemplate(interfaces.objects.Template, validity.ValidityRoutines):
@property
def children(self):
"""A function that returns a list of child templates of a template
This is used to traverse the template tree
"""
return self.object_class.template_children(self._kwargs)
def replace_child(self, old_child, new_child):
"""A function for replacing one child with another
We pass in the kwargs directly so they can be changed
"""
self.object_class.template_replace_child(old_child, new_child, self._kwargs)
def __call__(self, context, layer_name, offset, parent = None):
"""Constructs the object
Returns: an object adhereing to the Object interface
Returns: an object adhereing to the Object interface
"""
# We always use the template size (as calculated by the object class)
# over the one passed in by an argument
self._kwargs['size'] = self.size
self._kwargs['structure_name'] = self.structure_name
return self.object_class(context = context, layer_name = layer_name, offset = offset, parent = parent, **self._kwargs)
return self.object_class(context = context, layer_name = layer_name, offset = offset, parent = parent,
**self._kwargs)
class ReferenceTemplate(interfaces.objects.Template):
"""Factory class that produces objects based on a delayed reference type
It should not return any attributes
It should not return any attributes
"""
def __call__(self, context, *args, **kwargs):
template = context.symbol_space.get_structure(self._structure_name)
return template(context = context, *args, **kwargs)