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 -    def __dir__(self):
     76          return sorted(self.__dict__.keys())
     77
     78      @staticmethod
     79 -    def parse(readDataInstance):
     80          """
     81          Returns a L{Directory}-like object.
     82
     83          @type readDataInstance: L{ReadData}
     84          @param readDataInstance: L{ReadData} object to read from.
     85
     86          @rtype: L{Directory}
     87          @return: L{Directory} object.
     88          """
     89          d = Directory()
     90          d.rva.value = readDataInstance.readDword()
     91          d.size.value = readDataInstance.readDword()
     92          return d
     93
     94 -    def getType(self):
     95          """Returns a value that identifies the L{Directory} object."""
     96          return consts.IMAGE_DATA_DIRECTORY
     97
     98 -class DataDirectory(list):
     99      """DataDirectory object."""
    100 -    def __init__(self,  shouldPack = True):
    101          """
    102          Array of L{Directory} objects.
    103
    104          @type shouldPack: bool
    105          @param shouldPack: If set to C{True} the L{DataDirectory} object will be packed. If set to C{False} the object won't packed.
    106          """
    107          self.shouldPack = shouldPack
    108
    109          for i in range(consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES):
    110              dir = Directory()
    111              dir.name.value = dirs[i]
    112              self.append(dir)
    113
    114 -    def __str__(self):
    115          packedRvasAndSizes = ""
    116          for directory in self:
    117              packedRvasAndSizes += str(directory)
    118          return packedRvasAndSizes
    119
    120      @staticmethod
    121 -    def parse(readDataInstance):
    122          """Returns a L{DataDirectory}-like object.
    123
    124          @type readDataInstance: L{ReadData}
    125          @param readDataInstance: L{ReadData} object to read from.
    126
    127          @rtype: L{DataDirectory}
    128          @return: The L{DataDirectory} object containing L{consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES} L{Directory} objects.
    129
    130          @raise DirectoryEntriesLengthException: The L{ReadData} instance has an incorrect number of L{Directory} objects.
    131          """
    132          if len(readDataInstance) == consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES * 8:
    133              newDataDirectory = DataDirectory()
    134              for i in range(consts.IMAGE_NUMBEROF_DIRECTORY_ENTRIES):
    135                  newDataDirectory[i].name.value = dirs[i]
    136                  newDataDirectory[i].rva.value = readDataInstance.readDword()
    137                  newDataDirectory[i].size.value = readDataInstance.readDword()
    138          else:
    139              raise excep.DirectoryEntriesLengthException("The IMAGE_NUMBEROF_DIRECTORY_ENTRIES does not match with the length of the passed argument.")
    140          return newDataDirectory
    141