
    Xf;                        d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZ  ej        e          Z G d dej        j
                  Z G d de          Z G d	 d
ej                  Z G d d          ZdS )zkObjects are the core of volatility, and provide pythonic access to
interpreted values of data from a layer.    N)AnyDictListMappingOptional)	constants
interfacesc                   t     e Zd ZdZdeeef         ddfdZdedef fdZdedefd	Z	d
 Z
defdZd Z xZS )ReadOnlyMappingzA read-only mapping of various values that offer attribute access as
    well.

    This ensures that the data stored in the mapping should not be
    modified, making an immutable mapping.
    
dictionaryreturnNc                     || _         d S N_dict)selfr   s     Z/home/panchajanya/Development/work/volatility3/volatility3/framework/interfaces/objects.py__init__zReadOnlyMapping.__init__   s    


    attrc                     |dk    r!t                                          |          S || j        v r| j        |         S t          d| j        j         d|           )z!Returns the item as an attribute.r   zObject has no attribute: .)super__getattribute__r   AttributeError	__class____name__)r   r   r   s     r   __getattr__zReadOnlyMapping.__getattr__   sh    7??77++D1114::d##H(?HH$HH
 
 	
r   namec                     | j         |         S )zReturns the item requested.r   )r   r   s     r   __getitem__zReadOnlyMapping.__getitem__'   s    z$r   c                 4    | j                                         S )z,Returns an iterator of the dictionary items.)r   __iter__r   s    r   r#   zReadOnlyMapping.__iter__+   s    z""$$$r   c                 *    t          | j                  S )z.Returns the length of the internal dictionary.)lenr   r$   s    r   __len__zReadOnlyMapping.__len__/   s    4:r   c                 B    t          |           t          |          k    S r   )dict)r   others     r   __eq__zReadOnlyMapping.__eq__3   s    DzzT%[[((r   )r   
__module____qualname____doc__r   strr   r   r   r!   r#   intr'   r+   __classcell__r   s   @r   r   r      s          738#4          
 
 
 
 
 
 
 
           % % %    ) ) ) ) ) ) )r   r   c                   v     e Zd ZdZ	 	 	 	 ddededee         ded         dee         d	ee         f fd
Z xZS )ObjectInformationa  Contains common information useful/pertinent only to an individual
    object (like an instance)

    This typically contains information such as the layer the object belongs to, the offset where it was constructed,
    and if it is a subordinate object, its parent.

    This is primarily used to reduce the number of parameters passed to object constructors and keep them all together
    in a single place.  These values are based on the :class:`ReadOnlyMapping` class, to prevent their modification.
    N
layer_nameoffsetmember_nameparentObjectInterfacenative_layer_namesizec           	      \    t                                          |||||p||d           dS )a  Constructs a container for basic information about an object.

        Args:
            layer_name: Layer from which the data for the object will be read
            offset: Offset within the layer at which the data for the object will be read
            member_name: If the object was accessed as a member of a parent object, this was the name used to access it
            parent: If the object was accessed as a member of a parent object, this is the parent object
            native_layer_name: If this object references other objects (such as a pointer), what layer those objects live in
            size: The size that the whole structure consumes in bytes
        r5   r6   r7   r8   r:   r;   N)r   r   )r   r5   r6   r7   r8   r:   r;   r   s          r   r   zObjectInformation.__init__B   sL    & 	( * %6%D* 		
 		
 		
 		
 		
r   )NNNN)	r   r,   r-   r.   r/   r0   r   r   r1   r2   s   @r   r4   r4   7   s          &*.2+/"
 

 
 c]	

 *+
 $C=
 sm
 
 
 
 
 
 
 
 
 
r   r4   c                       e Zd ZdZdddeddddfd	Zd
edefdZede	fd            Z
ej        defd            ZdefdZdedd fdZdedefdZdedefdZdee         defdZ G d dej                  ZdS )r9   zQA base object required to be the ancestor of every object used in
    volatility.context#interfaces.context.ContextInterface	type_nameobject_infor4   r   Nc                     |j         |j                 j        }|j        |z  }||d}t	          j        i |||          | _        || _        dS )a:  Constructs an Object adhering to the ObjectInterface.

        Args:
            context: The context associated with the object
            type_name: The name of the type structure for the object
            object_info: Basic information relevant to the object (layer, offset, member_name, parent, etc)
        )rA   r6   N)layersr5   address_maskr6   collectionsChainMap_vol_context)r   r?   rA   rB   kwargsmasknormalized_offsetvol_info_dicts           r   r   zObjectInterface.__init__e   sU    4 ~k45B'.5&/;LMM(]KPP	r   r   c                     t           )z7Method for ensuring volatility members can be returned.)r   r   r   s     r   r   zObjectInterface.__getattr__   s    r   c                 *    t          | j                  S )z3Returns the volatility specific object information.r   rH   r$   s    r   volzObjectInterface.vol   s     ty)))r   valuec                     dS )z[Writes the new value into the format at the offset the object
        currently resides at.N )r   rS   s     r   writezObjectInterface.write         r   c                 8   t           j        | j        j        vrt	          d| j        j                   | j        j        d| j        j                            t           j                           }|| j        j        vrt          d| j        j                   |S )zReturns the symbol table name for this particular object.

        Raises:
            ValueError: If the object's symbol does not contain an explicit table
            KeyError: If the table_name is not valid within the object's context
        z&Unable to determine table for symbol: Nz=Symbol table not found in context's symbol_space for symbol: )	r   BANGrR   rA   
ValueErrorindexrI   symbol_spaceKeyError)r   
table_names     r   get_symbol_table_namez%ObjectInterface.get_symbol_table_name   s     >!333M9KMM   X'(R$(*<*B*B9>*R*R(RS
T]777dPTPXPbdd   r   new_type_namec                    t           j        |vr&|                                 }|t           j        z   |z   }| j        j                            |          }|                                } |j        di | t          | j	        j
        | j	        j        | j	        j        | j	        j        | j	        j        |j                  } || j        |          S )zReturns a new object at the offset and from the layer that the
        current object inhabits.

        .. note:: If new type name does not include a symbol table, the
           symbol table for the current object is used
        r=   )r?   rB   rU   )r   rY   r_   rI   r\   get_typeclone
update_volr4   rR   r5   r6   r7   r8   r:   r;   )r   r`   
additionalsymbol_tableobject_templaterB   s         r   castzObjectInterface.cast   s     >..5577L(9>9MIM-4==mLL)//11""00Z000'x*8?,8?"h8 %
 
 
 t}+NNNNr   r7   c                     dS )zReturns whether the object would contain a member called
        member_name.

        Args:
            member_name: Name to test whether a member exists within the type structure
        FrU   r   r7   s     r   
has_memberzObjectInterface.has_member   s	     ur   c                     |                      |          rCt          j        t                    5  t	          | |          }	 ddd           dS # 1 swxY w Y   dS )zReturns whether the dereferenced type has a valid member.

        Args:
            member_name: Name of the member to test access to determine if the member is valid or not
        NTF)rk   
contextlibsuppress	Exceptiongetattr)r   r7   _s      r   has_valid_memberz ObjectInterface.has_valid_member   s     ??;'' 	$Y//  D+..                us   AAAmember_namesc                 :     t           fd|D                       S )zReturns whether the object has all of the members listed in member_names

        Args:
            member_names: List of names to test as to members with those names validity
        c                 :    g | ]}                     |          S rU   )rr   ).0r7   r   s     r   
<listcomp>z5ObjectInterface.has_valid_members.<locals>.<listcomp>   s'    WWW;D))+66WWWr   )all)r   rs   s   ` r   has_valid_membersz!ObjectInterface.has_valid_members   s(     WWWW,WWWXXXr   c                      e Zd ZU dZg Zee         ed<   ee	j
        dddefd                        Zee	j
        ddded         fd                        Zee	j
        	 	 	 	 	 	 	 	 dd                        Zee	j
        dddedefd                        Zee	j
        dddeddfd                        Zee	j
        dddedefd                        Zd
S ) ObjectInterface.VolTemplateProxya  A container for proxied methods that the ObjectTemplate of this
        object will call.  This is primarily to keep methods together for easy
        organization/management, there is no significant need for it to be a
        separate class.

        The methods of this class *must* be class methods rather than
        standard methods, to allow for code reuse. Each method also
        takes a template since the templates may contain the necessary
        data about the yet-to-be-constructed object.  It allows objects
        to control how their templates respond without needing to write
        new templates for each and every potential object type.
        _methodstemplateTemplater   c                     dS )z(Returns the size of the template object.NrU   clsr}   s     r   r;   z%ObjectInterface.VolTemplateProxy.size   rW   r   c                     g S )z%Returns the children of the template.rU   r   s     r   childrenz)ObjectInterface.VolTemplateProxy.children   s	     Ir   	old_child	new_childNc                 :    t          d|j        j                   )z,Substitutes the old_child for the new_child.z3Template does not contain any children to replace: r]   rR   rA   )r   r}   r   r   s       r   replace_childz.ObjectInterface.VolTemplateProxy.replace_child   s&     ^hlF\^^  r   childc                 :    t          d|j        j                   )z]Returns the relative offset from the head of the parent data to
            the child member.(Template does not contain any children: r   r   r}   r   s      r   relative_child_offsetz6ObjectInterface.VolTemplateProxy.relative_child_offset  s&    
 S8<;QSS  r   interfaces.objects.Templatec                 :    t          d|j        j                   )z9Returns the template of the child member from the parent.r   r   r   s      r   child_templatez/ObjectInterface.VolTemplateProxy.child_template  s&     S8<;QSS  r   r7   c                     dS )zQReturns whether the object would contain a member called
            member_name.FrU   )r   r}   r7   s      r   rk   z+ObjectInterface.VolTemplateProxy.has_member  s	    
 5r   )r}   r~   r   r~   r   r~   r   N)r   r,   r-   r.   r|   r   r/   __annotations__classmethodabcabstractmethodr0   r;   r   r   r   r   boolrk   rU   r   r   VolTemplateProxyr{      s        	 	 !$s)   				;
 	;s 	; 	; 	; 
	 
	; 
			J 	4
3C 	 	 	 
	 
	 
			%	2<	IS		 	 	 
	 
	 
			 	C 	C 	 	 	 
	 
	 
			%	.1	*	 	 	 
	 
	 
			j 	s 	t 	 	 	 
	 
	 	 	r   r   	metaclass)r   r,   r-   r.   r/   r   r   r   propertyr   rR   r   r   rV   r_   rh   r   rk   rr   r   ry   ABCMetar   rU   r   r   r9   r9   a   s         6    )	  
       B      *_ * * * X*
 	!3 ! ! ! !s    $O# O8I O O O O0c d    C D    Yd3i YD Y Y Y Y= = = = =S[ = = = = = =r   r9   r   c                   z    e Zd ZdZdeddf fdZedefd            Zede	d          fd            Z
eej        defd                        Zej        d	edefd
            Zej        d	eddfd            Zej        dd            Zej        dedefd            ZddZddZdedefdZdddedefdZ xZS )r~   a  Class for all Factories that take offsets, and data layers and produce
    objects.

    This is effectively a class for currying object calls.  It creates a callable that can be called with the following
    parameters:

    Args:
        context: The context containing the memory layers and symbols required to construct the object
        object_info: Basic information about the object, see the ObjectInformation class for more information

    Returns:
        The constructed object

    The keyword arguments handed to the constructor, along with the type_name are stored for later retrieval.
    These will be access as `object.vol.<keyword>` or `template.vol.<keyword>` for each object and should contain
    as least the basic information that each object will require before it is instantiated (so `offset` and `parent`
    are explicitly not recorded here).  This dictionary can be updated after construction, but any changes made
    after that point will *not* be cloned.  This is so that templates such as those for string objects may
    contain different length limits, without affecting all other strings using the same template from a SymbolTable,
    constructed at resolution time and then cached.
    rA   r   Nc                     t                                                       i }t          j        ||d|i          | _        dS )z7Stores the keyword arguments for later object creation.rA   N)r   r   rF   rG   rH   )r   rA   	arguments
empty_dictr   s       r   r   zTemplate.__init__4  sD     	%'
(	K#;
 
			r   c                 *    t          | j                  S )zReturns a volatility information object, much like the
        :class:`~volatility3.framework.interfaces.objects.ObjectInformation`
        provides.rQ   r$   s    r   rR   zTemplate.vol=  s    
 ty)))r   c                     g S )zThe children of this template (such as member types, sub-types and
        base-types where they are relevant).

        Used to traverse the template tree.
        rU   r$   s    r   r   zTemplate.childrenD  s	     	r   c                     dS )z!Returns the size of the template.NrU   r$   s    r   r;   zTemplate.sizeM  rW   r   r   c                     dS )zQReturns the relative offset of the `child` member from its parent
        offset.NrU   r   r   s     r   r   zTemplate.relative_child_offsetR  rW   r   r   c                     dS )z4Returns the `child` member template from its parent.NrU   r   s     r   r   zTemplate.child_templateW  rW   r   r   r   c                     dS )z>Replaces `old_child` with `new_child` in the list of children.NrU   )r   r   r   s      r   r   zTemplate.replace_child[  rW   r   r7   c                     dS )zNReturns whether the object would contain a member called
        `member_name`NrU   rj   s     r   rk   zTemplate.has_member_  rW   r   c                 X     | j         di | j        j                                        }|S )zpReturns a copy of the original Template as constructed (without
        `update_vol` additions having been made)rU   )r   rH   parentsr   )r   rc   s     r   rc   zTemplate.cloned  s1     ??!2!<!<!>!>??r   c                 :    | j                             |           dS )z`Updates the keyword arguments with values that will **not** be
        carried across to clones.N)rH   update)r   new_argumentss     r   rd   zTemplate.update_volj  s      		'''''r   r   c                 v    |dk    r|| j         v r| j         |         S t          | j        j         d|           )zaExposes any other values stored in ._vol as attributes (for example,
        enumeration choices)rH   z object has no attribute )rH   r   r   r   rO   s     r   r   zTemplate.__getattr__o  sN     6>>ty  y&~&GGGG
 
 	
r   r?   r@   rB   c                     dS )zConstructs the object.NrU   )r   r?   rB   s      r   __call__zTemplate.__call__y  rW   r   )r   r~   r   r~   r   N)r   r~   )r   N)r   r,   r-   r.   r/   r   r   r   rR   r   r   r   r   r0   r;   r   r   r   r   rk   rc   rd   r   r   r4   r9   r   r1   r2   s   @r   r~   r~     s        ,
# 
t 
 
 
 
 
 
 *_ * * * X* $z*    X 0c 0 0 0  X0 	3 3     	CC C,I C C C C 	M M M M 	c d       ( ( ( (

 
 
 
 
 
%6% '% 
	% % % % % % % %r   r~   )r.   r   rF   collections.abcrm   loggingtypingr   r   r   r   r   volatility3.frameworkr   r	   	getLoggerr   vollogr   r4   r   r9   r~   rU   r   r   <module>r      sk  , , 



              5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7		8	$	$") ") ") ") ")ko- ") ") ")J'
 '
 '
 '
 '
 '
 '
 '
Ty y y y y y y y yxa% a% a% a% a% a% a% a% a% a%r   