From 0ef389dec9e802aad8bbd41dcd5538d81870fde6 Mon Sep 17 00:00:00 2001 From: Mike Auty Date: Sun, 14 Feb 2016 02:49:28 +0000 Subject: [PATCH] Tidy up Bytes and String to require all the appropriate parameters. --- volatility/framework/objects/__init__.py | 39 ++++++++++++++++++------ volatility/framework/symbols/native.py | 4 +-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/volatility/framework/objects/__init__.py b/volatility/framework/objects/__init__.py index 0c5d93bd1..8e6a7b09b 100644 --- a/volatility/framework/objects/__init__.py +++ b/volatility/framework/objects/__init__.py @@ -92,16 +92,16 @@ class Bytes(PrimitiveObject, bytes): struct_format = str(length) + "s") self._vol['length'] = length - def __new__(cls, context, structure_name, object_info, **kwargs): + def __new__(cls, context, structure_name, object_info, length = 1, **kwargs): """Creates the appropriate class and returns it so that the native type is inherritted The only reason the **kwargs is added, is so that the inherriting types can override __init__ without needing to override __new__""" - return bytes.__new__(cls, - cls._struct_value(context, - struct_format = str(kwargs["length"]) + "s", - layer_name = object_info.layer_name, - offset = object_info.offset)) + return cls._struct_type.__new__(cls, + cls._struct_value(context, + struct_format = str(length) + "s", + layer_name = object_info.layer_name, + offset = object_info.offset)) # TODO: Fix up strings unpacking to include an encoding @@ -112,14 +112,33 @@ class String(PrimitiveObject, str): """ _struct_type = str - def __init__(self, context, structure_name, object_info, struct_format, length = 1, encoding = 'ascii'): - self._struct_format = str(length) + 's' - self._vol['length'] = length + def __init__(self, context, structure_name, object_info, max_length = 1, encoding = "utf-8", errors = None): PrimitiveObject.__init__(self, context = context, structure_name = structure_name, object_info = object_info, - struct_format = struct_format) + struct_format = str(max_length) + 's') + self._vol["max_length"] = max_length + self._vol['encoding'] = encoding + self._vol['errors'] = errors + + def __new__(cls, context, structure_name, object_info, max_length = 1, encoding = "utf-8", errors = None, **kwargs): + """Creates the appropriate class and returns it so that the native type is inherited + + The only reason the **kwargs is added, is so that the inherriting types can override __init__ + without needing to override __new__""" + params = {} + if encoding: + params['encoding'] = encoding + if errors: + params['errors'] = errors + value = cls._struct_type.__new__(cls, + cls._struct_value(context, + struct_format = str(max_length) + "s", + layer_name = object_info.layer_name, + offset = object_info.offset), + **params) + return value class Pointer(Integer): diff --git a/volatility/framework/symbols/native.py b/volatility/framework/symbols/native.py index 0a593696a..3da7b33a0 100644 --- a/volatility/framework/symbols/native.py +++ b/volatility/framework/symbols/native.py @@ -36,7 +36,7 @@ class NativeTable(interfaces.symbols.NativeTableInterface): symbol_space is used to resolve any target symbols if they don't exist in this list """ - # TODO: Add strings and bytes to this set + # NOTE: These need updating whenever the object init signatures change additional = {} obj = None if structure_name == 'void': @@ -52,7 +52,7 @@ class NativeTable(interfaces.symbols.NativeTableInterface): additional = {"start_bit": 0, "end_bit": 0} elif structure_name == 'String': obj = objects.String - additional = {"length": 0} + additional = {"max_length": 0} elif structure_name == 'Bytes': obj = objects.Bytes additional = {"length": 0}