Package pype32 :: Module datadirs
[hide private]
[frames] | no frames]

Source Code for Module pype32.datadirs

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*-  
  3   
  4  # Copyright (c) 2013, Nahuel Riva  
  5  # All rights reserved.  
  6  #  
  7  # Redistribution and use in source and binary forms, with or without  
  8  # modification, are permitted provided that the following conditions are met:  
  9  #  
 10  #     * Redistributions of source code must retain the above copyright notice,  
 11  #       this list of conditions and the following disclaimer.  
 12  #     * Redistributions in binary form must reproduce the above copyright  
 13  #       notice,this list of conditions and the following disclaimer in the  
 14  #       documentation and/or other materials provided with the distribution.  
 15  #     * Neither the name of the copyright holder nor the names of its  
 16  #       contributors may be used to endorse or promote products derived from  
 17  #       this software without specific prior written permission.  
 18  #  
 19  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
 20  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  
 21  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  
 22  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE  
 23  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  
 24  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  
 25  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  
 26  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  
 27  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
 28  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
 29  # POSSIBILITY OF SUCH DAMAGE.  
 30   
 31  """ 
 32  Data directory classes. 
 33  """ 
 34   
 35  __revision__ = "$Id$" 
 36   
 37  __all__ = [ 
 38             "Directory",  
 39             "DataDirectory",  
 40             ] 
 41              
 42  import consts 
 43  import excep 
 44  import datatypes 
 45   
 46  from struct import pack 
 47   
 48  dirs = ["EXPORT_DIRECTORY","IMPORT_DIRECTORY","RESOURCE_DIRECTORY","EXCEPTION_DIRECTORY","SECURITY_DIRECTORY",\ 
 49  "RELOCATION_DIRECTORY","DEBUG_DIRECTORY","ARCHITECTURE_DIRECTORY","RESERVED_DIRECTORY","TLS_DIRECTORY",\ 
 50  "CONFIGURATION_DIRECTORY","BOUND_IMPORT_DIRECTORY","IAT_DIRECTORY","DELAY_IMPORT_DIRECTORY","NET_METADATA_DIRECTORY",\ 
 51  "RESERVED_DIRECTORY"] 
52 53 -class Directory(object):
54 """Directory object."""
55 - def __init__(self, shouldPack = True):
56 """ 57 Class representation of the C{IMAGE_DATA_DIRECTORY} structure. 58 @see: U{http://msdn.microsoft.com/es-es/library/windows/desktop/ms680305%28v=vs.85%29.aspx} 59 60 @type shouldPack: bool 61 @param shouldPack: If set to C{True} the L{Directory} object will be packed. If set to C{False} the object won't be packed. 62 """ 63 self.name = datatypes.String("") 64 self.rva = datatypes.DWORD(0) #: L{DWORD} rva. 65 self.size = datatypes.DWORD(0) #: L{DWORD} size. 66 self.info = None #: This variable holds the information of the directory. 67 self.shouldPack = shouldPack
68
69 - def __str__(self):
70 return str(self.rva) + str(self.size)
71
72 - def __len__(self):
73 return len(str(self))
74 75 @staticmethod
76 - def parse(readDataInstance):
77 """ 78 Returns a L{Directory}-like object. 79 80 @type readDataInstance: L{ReadData} 81 @param readDataInstance: L{ReadData} object to read from. 82 83 @rtype: L{Directory} 84 @return: L{Directory} object. 85 """ 86 d = Directory() 87 d.rva.value = readDataInstance.readDword() 88 d.size.value = readDataInstance.readDword() 89 return d
90
91 - def getType(self):
92 """Returns a value that identifies the L{Directory} object.""" 93 return consts.IMAGE_DATA_DIRECTORY
94
95 -class DataDirectory(list):
96 """DataDirectory object."""
97 - def __init__(self, shouldPack = True):
98 """ 99 Array of L{Directory} objects. 100 101 @type shouldPack: bool 102 @param shouldPack: If set to C{True} the L{DataDirectory} object will be packed. If set to C{False} the object won't packed. 103 """ 104 self.shouldPack = shouldPack 105 106 for i in range(consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES): 107 dir = Directory() 108 dir.name.value = dirs[i] 109 self.append(dir)
110
111 - def __str__(self):
112 packedRvasAndSizes = "" 113 for directory in self: 114 packedRvasAndSizes += str(directory) 115 return packedRvasAndSizes
116 117 @staticmethod
118 - def parse(readDataInstance):
119 """Returns a L{DataDirectory}-like object. 120 121 @type readDataInstance: L{ReadData} 122 @param readDataInstance: L{ReadData} object to read from. 123 124 @rtype: L{DataDirectory} 125 @return: The L{DataDirectory} object containing L{consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES} L{Directory} objects. 126 127 @raise DirectoryEntriesLengthException: The L{ReadData} instance has an incorrect number of L{Directory} objects. 128 """ 129 if len(readDataInstance) == consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES * 8: 130 newDataDirectory = DataDirectory() 131 for i in range(consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES): 132 newDataDirectory[i].name.value = dirs[i] 133 newDataDirectory[i].rva.value = readDataInstance.readDword() 134 newDataDirectory[i].size.value = readDataInstance.readDword() 135 else: 136 raise excep.DirectoryEntriesLengthException("The IMAGE_NUMBEROF_DIRECTORY_ENTRIES does not match with the length of the passed argument.") 137 return newDataDirectory
138