
    Xf9                     L   d Z ddlZddlZddlmZmZ ddlmZm	Z	m
Z
mZmZmZmZmZ ddlmZmZmZ ddlmZ ddlmZmZ ddlmZ  G d	 d
      Z G d d      Z G d dej                  j                        Z G d deej<                  e      Z G d de      Z  G d de!      Z"y)z<Symbols provide structural information about a set of bytes.    N)ABCabstractmethod)AnyDictIterableListMappingOptionalTupleType)	constants
exceptions
interfaces)requirements)configurationobjects)RequirementInterfacec                       e Zd ZdZ	 	 ddededeej                     dee	   ddf
dZ
edefd	       Zedee   fd
       Zedeej                     fd       Zedefd       Zedee	   fd       Zy)SymbolInterfacezBContains information about a named location in a program's memory.Nnameaddresstypeconstant_datareturnc                     || _         t        j                  | j                   v rt        dt        j                   d      d| _        || _        || _        || _        y)a  

        Args:
            name: Name of the symbol
            address: Numeric address value of the symbol
            type: Optional type structure information associated with the symbol
            constant_data: Potential constant data the symbol points at
        z7Symbol names cannot contain the symbol differentiator ()N)_namer   BANG
ValueError	_location_address_type_constant_data)selfr   r   r   r   s        Z/home/panchajanya/Development/work/volatility3/volatility3/framework/interfaces/symbols.py__init__zSymbolInterface.__init__   sY     
>>TZZ'I)..IYYZ[ 
 
+    c                     | j                   S )zReturns the name of the symbol.)r   r$   s    r%   r   zSymbolInterface.name.        zzr'   c                 N    | j                   y| j                   j                  d   S )z8Returns the name of the type that the symbol represents.N	type_name)r   volr)   s    r%   r,   zSymbolInterface.type_name3   s$     99yy}}[))r'   c                     | j                   S )z,Returns the type that the symbol represents.)r"   r)   s    r%   r   zSymbolInterface.type;   r*   r'   c                     | j                   S )zOReturns the relative address of the symbol within the compilation
        unit.)r!   r)   s    r%   r   zSymbolInterface.address@   s     }}r'   c                     | j                   S )z5Returns any constant data associated with the symbol.)r#   r)   s    r%   r   zSymbolInterface.constant_dataF   s     """r'   NN)__name__
__module____qualname____doc__strintr
   r   Templatebytesr&   propertyr   r,   r   r   r    r'   r%   r   r      s    L ,0)-,, , w''(	,
  , 
,6 c   *8C= * * hw//0     
 #x # #r'   r   c                   &   e Zd ZdZ	 	 ddedddeeeef      deeeee	j                     f      ddf
d	Zdedefd
Zedee   fd       Zedee   fd       Zdede	j$                  fdZedee   fd       Zedd       Zej.                  dd       Zdedee	j                     ddfdZdedee	j                     defdZdedee	j                     fdZdeddfdZdedee	j$                     fdZdedee   fdZd dededee   fdZ d!dZ!y)"BaseSymbolTableInterfacea  The base interface, inherited by both NativeTables and SymbolTables.

    native_types is a NativeTableInterface used for native types for the particular loaded symbol table
    table_mapping allows tables referenced by symbols to be remapped to a different table name if necessary

    Note: table_mapping is a rarely used feature (since symbol tables are typically self-contained)
    Nr   native_typesNativeTableInterfacetable_mappingclass_typesr   c                     || _         |i }|| _        || _        g | _        |r|D ]  }| j	                  |||           yy)a  

        Args:
            name: Name of the symbol table
            native_types: The native symbol table used to resolve any base/native types
            table_mapping: A dictionary mapping names of tables (which when present within the table will be changed to the mapped table)
            class_types: A dictionary of types and classes that should be instantiated instead of Struct to construct them
        N)r   r@   _native_types_sort_symbolsset_type_class)r$   r   r>   r@   rA   
class_types         r%   r&   z!BaseSymbolTableInterface.__init__U   s^     	 M*)46 ) I
##JJ0GHI r'   c                     t        d      )z{Resolves a symbol name into a symbol object.

        If the symbol isn't found, it raises a SymbolError exception
        z9Abstract property get_symbol not implemented by subclass.NotImplementedErrorr$   r   s     r%   
get_symbolz#BaseSymbolTableInterface.get_symbolr   s    
 "G
 	
r'   c                     t        d      )z(Returns an iterator of the Symbol names.z6Abstract property symbols not implemented by subclass.rH   r)   s    r%   symbolsz BaseSymbolTableInterface.symbols{   s     "D
 	
r'   c                     t        d      )z-Returns an iterator of the Symbol type names.z4Abstract property types not implemented by subclass.rH   r)   s    r%   typeszBaseSymbolTableInterface.types   s     "B
 	
r'   c                     t        d      )z}Resolves a symbol name into an object template.

        If the symbol isn't found it raises a SymbolError exception
        z5Abstract method get_type not implemented by subclass.rH   rJ   s     r%   get_typez!BaseSymbolTableInterface.get_type   s    
 "C
 	
r'   c                     t        d      )z-Returns an iterator of the Enumeration names.z;Abstract property enumerations not implemented by subclass.rH   r)   s    r%   enumerationsz%BaseSymbolTableInterface.enumerations   s     "I
 	
r'   c                     | j                   S )zOReturns None or a NativeTable for handling space specific native
        types.rC   r)   s    r%   nativesz BaseSymbolTableInterface.natives   s     !!!r'   c                     || _         y)zChecks the natives value and then applies it internally.

        WARNING: This allows changing the underlying size of all the other types referenced in the SymbolTable
        NrU   r$   values     r%   rV   z BaseSymbolTableInterface.natives   s     #r'   clazzc                     t        d      )a  Overrides the object class for a specific Symbol type.

        Name *must* be present in self.types

        Args:
            name: The name of the type to override the class for
            clazz: The actual class to override for the provided type name
        z3Abstract method set_type_class not implemented yet.rH   r$   r   rZ   s      r%   rE   z'BaseSymbolTableInterface.set_type_class   s     ""WXXr'   c                 H    	 | j                  ||       y# t        $ r Y yw xY w)a$  Calls the set_type_class function but does not throw an exception.
        Returns whether setting the type class was successful.
        Args:
            name: The name of the type to override the class for
            clazz: The actual class to override for the provided type name
        TF)rE   r   r\   s      r%   optional_set_type_classz0BaseSymbolTableInterface.optional_set_type_class   s,    	e, 		s    	!!c                     t        d      )z0Returns the class associated with a Symbol type.z3Abstract method get_type_class not implemented yet.rH   rJ   s     r%   get_type_classz'BaseSymbolTableInterface.get_type_class       !"WXXr'   c                     t        d      )zARemoves the associated class override for a specific Symbol type.z3Abstract method del_type_class not implemented yet.rH   rJ   s     r%   del_type_classz'BaseSymbolTableInterface.del_type_class   ra   r'   c                 `    | j                  |      j                  }|y| j                  |      S )zQResolves a symbol name into a symbol and then resolves the symbol's
        type.N)rK   r,   rQ   )r$   r   r,   s      r%   get_symbol_typez(BaseSymbolTableInterface.get_symbol_type   s0     OOD)33	}}Y''r'   r,   c              #      K   | j                   D ]j  }| j                  |      }|j                  !|j                  |k(  s-|j                  j                  t        j
                  |z         s]|j                   l yw)zXReturns the name of all symbols in this table that have type
        matching type_name.N)rM   rK   r,   endswithr   r   r   )r$   r,   symbol_namesymbols       r%   get_symbols_by_typez,BaseSymbolTableInterface.get_symbols_by_type   sl       << 	"K __[1F+  I-$$--inny.HIkk!	"s   -A=;A=,A=offsetsizec              #     K   |dk  rt        d      | j                  sCt        | j                  D cg c]  }| j	                  |      j
                  |f! c}      | _        | j                  }t        j                  ||df      }|t        |      k  rU||   d   |k\  rI||   d   ||z   k  r:||   d    |dz  }|t        |      k  r||   d   |k\  r||   d   ||z   k  r7yyyyyyc c}w w)zWReturns the name of all symbols in this table that live at a
        particular offset.r   z"Size must be strictly non-negative    N)	r   rD   sortedrM   rK   r   bisectbisect_leftlen)r$   rk   rl   snsort_symbolsresults         r%   get_symbols_by_locationz0BaseSymbolTableInterface.get_symbols_by_location   s     !8ABB!!!'=A\\Jr$//"%--r2J"D ))##L62,?s<(( #v-V$Q'6D=8v&q))aKF s<(( #v-V$Q'6D=8 . )8 . )	 Ks   0C/$C*BC/$C/c                      y)z-Clears the symbol cache of this symbol table.Nr;   r)   s    r%   clear_symbol_cachez+BaseSymbolTableInterface.clear_symbol_cache   s    r'   r1   )r   r?   )rY   r?   r   N)r   )r   N)"r2   r3   r4   r5   r6   r
   r   r	   r   r   ObjectInterfacer&   r   rK   r:   r   rM   rO   r8   rQ   r   rS   rV   setterrE   boolr^   r`   rc   re   rj   r7   rw   ry   r;   r'   r%   r=   r=   L   s    37MQII -I  S#X/	I
 gc40G0G+H&HIJI 
I:
s 
 
 
# 
 
 
x} 
 

S 
W%5%5 
 
hsm 
 
 " "
 ^^# #	Y3 	YtG4K4K/L 	YQU 	Y $W%<%< =	 Y3 Y40G0G+H YY3 Y4 Y(C (HW5E5E,F ("S "Xc] "c  Xc] $r'   r=   c                   l   e Zd ZdZddedefdZededdfd       Zededee   fd	       Z	e	 dd
e
de
dee   dee   fd       Zededej                  fd       Zededefd       Zededej                  fd       Zededefd       Zededefd       Zededefd       Zededdfd       Zy)SymbolSpaceInterfacezhAn interface for the container that holds all the symbol-containing
    tables for use within a context.prefixr   c                      y)zaReturns an unused table name to ensure no collision occurs when
        inserting a symbol table.Nr;   )r$   r   s     r%   free_table_namez$SymbolSpaceInterface.free_table_name      r'   
table_nameNc                      y)zClears the symbol cache for the specified table name. If no table
        name is specified, the caches of all symbol tables are cleared.Nr;   )r$   r   s     r%   ry   z'SymbolSpaceInterface.clear_symbol_cache  r   r'   r,   c                      y)z4Returns all symbols based on the type of the symbol.Nr;   r$   r,   s     r%   rj   z(SymbolSpaceInterface.get_symbols_by_type  r   r'   rk   rl   c                      y)z>Returns all symbols that exist at a specific relative address.Nr;   )r$   rk   rl   r   s       r%   rw   z,SymbolSpaceInterface.get_symbols_by_location  r   r'   c                      y)z;Look-up a type name across all the contained symbol tables.Nr;   r   s     r%   rQ   zSymbolSpaceInterface.get_type  r   r'   rh   c                      y)z=Look-up a symbol name across all the contained symbol tables.Nr;   )r$   rh   s     r%   rK   zSymbolSpaceInterface.get_symbol  r   r'   	enum_namec                      y)z>Look-up an enumeration across all the contained symbol tables.Nr;   )r$   r   s     r%   get_enumerationz$SymbolSpaceInterface.get_enumeration  r   r'   r   c                      y)z@Determines whether a type exists in the contained symbol tables.Nr;   rJ   s     r%   has_typezSymbolSpaceInterface.has_type#  r   r'   c                      y)zJDetermines whether a symbol exists in the contained symbol
        tables.Nr;   rJ   s     r%   
has_symbolzSymbolSpaceInterface.has_symbol'  r   r'   c                      y)zWDetermines whether an enumeration choice exists in the contained
        symbol tables.Nr;   rJ   s     r%   has_enumerationz$SymbolSpaceInterface.has_enumeration,  r   r'   rY   c                      y)z+Adds a symbol_list to the end of the space.Nr;   rX   s     r%   appendzSymbolSpaceInterface.append1  r   r'   )layer)r   N)r2   r3   r4   r5   r6   r   r   ry   r   rj   r7   r
   rw   r   r8   rQ   r   rK   r   r|   r   r   r   r=   r   r;   r'   r%   r~   r~      s   (%c % % KS KT K K CS CXc] C C FJMM!$M6>smM	#M M
 J# J'*:*: J J Lc Lo L L M M1A1A M M OS OT O O s t   C D   :4 : : :r'   r~   c                        e Zd ZdZ	 	 ddddededdd	eeeef      d
eeeee	j                     f      ddfdZd fdZedee   f fd       Z xZS )SymbolTableInterfacezHandles a table of symbols.Ncontextz#interfaces.context.ContextInterfaceconfig_pathr   r>   r?   r@   rA   r   c                 z    t         j                  j                  | ||       t        j                  | ||||       y)a  Instantiates an SymbolTable based on an IntermediateSymbolFormat JSON file.  This is validated against the
        appropriate schema.

        Args:
            context: The volatility context for the symbol table
            config_path: The configuration path for the symbol table
            name: The name for the symbol table (this is used in symbols e.g. table!symbol )
            isf_url: The URL pointing to the ISF file location
            native_types: The NativeSymbolTable that contains the native types for this symbol table
            table_mapping: A dictionary linking names referenced in the file with symbol tables in the context
            class_types: A dictionary of type names and classes that override StructType when they are instantiated
        )rA   N)r   ConfigurableInterfacer&   r=   )r$   r   r   r   r>   r@   rA   s          r%   r&   zSymbolTableInterface.__init__<  s:    * 	++44T7KP ))$m 	* 	
r'   c                     t         |          }| j                  j                  dz   | j                  j                  z   |d<   |S )N.class)superbuild_configuration	__class__r3   r2   )r$   configr   s     r%   r   z(SymbolTableInterface.build_configurationV  s>    ,. ..33c9DNN<S<SSwr'   c                 V    t         |          t        j                  dddd      gz   S )Nsymbol_maskzAddress mask for symbolsTr   )r   descriptionoptionaldefault)r   get_requirementsr   IntRequirement)clsr   s    r%   r   z%SymbolTableInterface.get_requirements]  s7    w')''"6	-
 
 	
r'   r1   )r   zconfiguration.HierarchicalDict)r2   r3   r4   r5   r6   r
   r   r	   r   r   rz   r&   r   classmethodr   r   r   __classcell__)r   s   @r%   r   r   6  s     & 37MQ
6
 
 	

 -
  S#X/
 gc40G0G+H&HIJ
 

4 
&:!; 
 
r'   r   c                   |    e Zd ZdZdedefdZedee   fd       Z	dede
j                  fdZedee   fd       Zy)	r?   z?Class to distinguish NativeSymbolLists from other symbol lists.r   r   c                 D    t        j                  || j                  d      )NzNativeTables never hold symbolsr   SymbolErrorr   rJ   s     r%   rK   zNativeTableInterface.get_symboll  s    $$T4996WXXr'   c                     g S Nr;   r)   s    r%   rM   zNativeTableInterface.symbolso      	r'   c                 D    t        j                  || j                  d      )Nz$NativeTables never hold enumerationsr   rJ   s     r%   r   z$NativeTableInterface.get_enumerations  s!    $$$))C
 	
r'   c                     g S r   r;   r)   s    r%   rS   z!NativeTableInterface.enumerationsx  r   r'   N)r2   r3   r4   r5   r6   r   rK   r:   r   rM   r   r8   r   rS   r;   r'   r%   r?   r?   i  sr    IYs Y Y #  
C 
G,<,< 

 hsm  r'   r?   c                        e Zd ZdZdeddfdZy)MetadataInterfacez>Interface for accessing metadata stored within a symbol table.	json_datar   Nc                     || _         y)z#Constructor that accepts json_data.N)
_json_data)r$   r   s     r%   r&   zMetadataInterface.__init__  s	    #r'   )r2   r3   r4   r5   r   r&   r;   r'   r%   r   r   }  s    H$$ $4 $r'   r   )#r5   rq   collections.abccollectionsabcr   r   typingr   r   r   r   r	   r
   r   r   volatility3.frameworkr   r   r   #volatility3.framework.configurationr    volatility3.framework.interfacesr   r   .volatility3.framework.interfaces.configurationr   r   r=   r~   r   r   r?   objectr   r;   r'   r%   <module>r      s    C   # L L L C C < C O9# 9#xq qh3:;??22 3:l0
mAA30
f3 ($ $r'   