signature stringlengths 8 3.44k | body stringlengths 0 1.41M | docstring stringlengths 1 122k | id stringlengths 5 17 |
|---|---|---|---|
@property<EOL><INDENT>def group_start_date_time_as_string(self) -> str:<DEDENT> | return self._start_datetime_as_string(self.group_start_time)<EOL> | Returns: group start time as a string | f11781:c9:m17 |
@property<EOL><INDENT>def units(self) -> typing.Iterator['<STR_LIT>']:<DEDENT> | for unit_index in self._section_group['<STR_LIT>']:<EOL><INDENT>if unit_index not in self.__units.keys():<EOL><INDENT>_category = self.units_class_enum[self.group_category] <EOL>self.__units[unit_index] = _category(self.d, self.l10n, self.coa_color,<EOL>self.country_index,<EOL>self.group_category,<EOL>self.group_index, unit_index)<EOL><DEDENT>yield self.__units[unit_index]<EOL><DEDENT> | Returns: generator over all units of this group | f11781:c9:m18 |
@property<EOL><INDENT>def first_unit(self) -> '<STR_LIT>':<DEDENT> | return list(self.units)[<NUM_LIT:0>]<EOL> | Returns: gris unit of this group | f11781:c9:m19 |
def group_size(self) -> int: | return len(list(self.units))<EOL> | Returns: amount of units in this group | f11781:c9:m20 |
def get_unit_by_index(self, unit_index) -> typing.Optional['<STR_LIT>']: | if unit_index in self._section_group['<STR_LIT>'].keys():<EOL><INDENT>if unit_index not in self.__units.keys():<EOL><INDENT>_category = self.units_class_enum[self.group_category] <EOL>self.__units[unit_index] = _category(self.d, self.l10n, self.coa_color,<EOL>self.country_index,<EOL>self.group_category,<EOL>self.group_index, unit_index)<EOL><DEDENT>return self.__units[unit_index]<EOL><DEDENT>return None<EOL> | Args:
unit_index: index of unit
Returns: a unit of this group | f11781:c9:m21 |
@property<EOL><INDENT>def group_is_client_group(self) -> bool:<DEDENT> | <EOL>first_unit = self.get_unit_by_index(<NUM_LIT:1>)<EOL>if first_unit:<EOL><INDENT>return first_unit.skill == '<STR_LIT>'<EOL><DEDENT>return False<EOL> | Returns: True if this group is a client group | f11781:c9:m22 |
@property<EOL><INDENT>def group_start_position(self) -> typing.Tuple[float, float]:<DEDENT> | return self.group_route._section_route[<NUM_LIT:1>]['<STR_LIT:action>']<EOL> | Returns: group position | f11781:c9:m23 |
@property<EOL><INDENT>def unit_name(self) -> str:<DEDENT> | return self.l10n[self._unit_name_key]<EOL> | Returns: unit name | f11781:c10:m4 |
@property<EOL><INDENT>def skill(self) -> str:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: unit skill (one of 'Average', 'Good', 'High', 'Excellent', 'Random', 'Client', 'Player') | f11781:c10:m6 |
@property<EOL><INDENT>def speed(self) -> float:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: unit speed | f11781:c10:m8 |
@property<EOL><INDENT>def unit_type(self) -> str:<DEDENT> | return self._section_unit['<STR_LIT:type>']<EOL> | Returns: unit type | f11781:c10:m10 |
@property<EOL><INDENT>def unit_id(self) -> int:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: unit type | f11781:c10:m12 |
@property<EOL><INDENT>def unit_pos_x(self) -> float:<DEDENT> | return float(self._section_unit['<STR_LIT:x>'])<EOL> | Returns: unit x coordinate | f11781:c10:m14 |
@property<EOL><INDENT>def unit_pos_y(self) -> float:<DEDENT> | return float(self._section_unit['<STR_LIT:y>'])<EOL> | Returns: unit Y coordinate | f11781:c10:m16 |
@property<EOL><INDENT>def unit_position(self) -> typing.Tuple[float, float]:<DEDENT> | return self.unit_pos_x, self.unit_pos_y<EOL> | Returns: unit position | f11781:c10:m18 |
@property<EOL><INDENT>def heading(self) -> float:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: unit heading | f11781:c10:m20 |
@property<EOL><INDENT>def radio_presets(self) -> typing.Iterator['<STR_LIT>']:<DEDENT> | raise TypeError('<STR_LIT>'.format(self.unit_id, self.unit_name))<EOL> | Returns: generator over unit radio presets | f11781:c10:m22 |
@property<EOL><INDENT>def has_radio_presets(self) -> bool:<DEDENT> | return all([self.skill == '<STR_LIT>', self.unit_type in FlyingUnit.RadioPresets.radio_enum.keys()])<EOL> | Returns: true if unit has radio presets | f11781:c10:m23 |
@property<EOL><INDENT>def radio_presets(self) -> typing.Iterator['<STR_LIT>']:<DEDENT> | if self.skill == '<STR_LIT>' and self.unit_type in FlyingUnit.RadioPresets.radio_enum.keys():<EOL><INDENT>for k in self._section_unit['<STR_LIT>']:<EOL><INDENT>yield FlyingUnit.RadioPresets(self, k)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(self.unit_id, self.unit_name))<EOL><DEDENT> | Returns: radio presets for this unit | f11781:c11:m1 |
@property<EOL><INDENT>def radios(self) -> dict:<DEDENT> | try:<EOL><INDENT>return self._section_unit['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError(self.unit_type)<EOL><DEDENT> | Returns: radio section of the dic | f11781:c11:m2 |
def get_radio_by_name(self, radio_name: str) -> '<STR_LIT>': | if self.has_radio_presets:<EOL><INDENT>for k in FlyingUnit.RadioPresets.radio_enum[self.unit_type].keys():<EOL><INDENT>if radio_name == FlyingUnit.RadioPresets.radio_enum[self.unit_type][k]['<STR_LIT>']:<EOL><INDENT>return FlyingUnit.RadioPresets(self, k)<EOL><DEDENT><DEDENT>raise TypeError('<STR_LIT>'.format(radio_name, self.unit_type))<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(self.unit_id, self.unit_name))<EOL><DEDENT> | Args:
radio_name: radio name
Returns: radio presets for this radio | f11781:c11:m3 |
def get_radio_by_number(self, radio_number) -> '<STR_LIT>': | if self.has_radio_presets:<EOL><INDENT>if radio_number in FlyingUnit.RadioPresets.radio_enum[self.unit_type].keys():<EOL><INDENT>return FlyingUnit.RadioPresets(self, radio_number)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(radio_number, self.unit_type))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(self.unit_id, self.unit_name))<EOL><DEDENT> | Args:
radio_number: radio number
Returns: presets for this radio | f11781:c11:m4 |
@property<EOL><INDENT>def livery(self) -> str:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: livery for this group | f11781:c11:m5 |
@property<EOL><INDENT>def onboard_num(self) -> str:<DEDENT> | return self._section_unit['<STR_LIT>']<EOL> | Returns: onboard number for this group | f11781:c11:m7 |
@property<EOL><INDENT>def mission_file(self) -> Path:<DEDENT> | return self.temp_dir.joinpath('<STR_LIT>')<EOL> | Returns: mission file path | f11782:c0:m3 |
@property<EOL><INDENT>def dictionary_file(self) -> Path:<DEDENT> | return self.temp_dir.joinpath('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL> | Returns: l10n file path | f11782:c0:m4 |
@property<EOL><INDENT>def map_res_file(self) -> Path:<DEDENT> | return self.temp_dir.joinpath('<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL> | Returns: resource map file path | f11782:c0:m5 |
@property<EOL><INDENT>def mission(self) -> Mission:<DEDENT> | if self._mission is None:<EOL><INDENT>raise RuntimeError()<EOL><DEDENT>return self._mission<EOL> | Returns: mission | f11782:c0:m6 |
@property<EOL><INDENT>def l10n(self) -> dict:<DEDENT> | if self._l10n is None:<EOL><INDENT>raise RuntimeError()<EOL><DEDENT>return self._l10n<EOL> | Returns: l10n dictionary | f11782:c0:m8 |
@property<EOL><INDENT>def map_res(self) -> dict:<DEDENT> | if self._map_res is None:<EOL><INDENT>raise RuntimeError()<EOL><DEDENT>return self._map_res<EOL> | Returns: map resources dictionary | f11782:c0:m9 |
@property<EOL><INDENT>def resources(self):<DEDENT> | return self._resources<EOL> | Returns: resources available to this mission | f11782:c0:m10 |
@staticmethod<EOL><INDENT>def reorder(miz_file_path: typing.Union[str, Path],<EOL>target_dir: typing.Union[str, Path],<EOL>skip_options_file: bool,<EOL>):<DEDENT> | miz_file_path = Path(miz_file_path).absolute()<EOL>if not miz_file_path.exists():<EOL><INDENT>raise FileNotFoundError(miz_file_path)<EOL><DEDENT>if not miz_file_path.is_file():<EOL><INDENT>raise ValueError(f'<STR_LIT>')<EOL><DEDENT>target_dir_path = Path(target_dir).absolute()<EOL>if not target_dir_path.exists():<EOL><INDENT>target_dir_path.mkdir(parents=True)<EOL><DEDENT>else:<EOL><INDENT>if not target_dir_path.is_dir():<EOL><INDENT>raise ValueError(f'<STR_LIT>')<EOL><DEDENT><DEDENT>LOGGER.debug('<STR_LIT>', miz_file_path)<EOL>LOGGER.debug('<STR_LIT>', target_dir)<EOL>LOGGER.debug('<STR_LIT>', "<STR_LIT>" if skip_options_file else "<STR_LIT>")<EOL>if not target_dir_path.exists():<EOL><INDENT>LOGGER.debug('<STR_LIT>', target_dir_path)<EOL>target_dir_path.mkdir(exist_ok=True)<EOL><DEDENT>Miz._do_reorder(miz_file_path, skip_options_file, target_dir_path)<EOL> | Re-orders a miz file into a folder (flattened)
Args:
miz_file_path: source miz file
target_dir: folder to flatten the content into
skip_options_file: do not re-order option file | f11782:c0:m12 |
def decode(self): | LOGGER.debug('<STR_LIT>')<EOL>if not self.zip_content:<EOL><INDENT>self.unzip()<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>with open(str(self.map_res_file), encoding=ENCODING) as stream:<EOL><INDENT>self._map_res, self._map_res_qual = SLTP().decode(stream.read())<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>with open(str(self.dictionary_file), encoding=ENCODING) as stream:<EOL><INDENT>self._l10n, self._l10n_qual = SLTP().decode(stream.read())<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>with open(str(self.mission_file), encoding=ENCODING) as stream:<EOL><INDENT>mission_data, self._mission_qual = SLTP().decode(stream.read())<EOL>self._mission = Mission(mission_data, self._l10n)<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>for file in Path(self.temp_dir, '<STR_LIT>', '<STR_LIT>').iterdir():<EOL><INDENT>if file.name in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>LOGGER.debug('<STR_LIT>', file.name)<EOL>self._resources.add(file.name)<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL> | Decodes the mission files into dictionaries | f11782:c0:m13 |
def unzip(self, overwrite: bool = False): | if self.zip_content and not overwrite:<EOL><INDENT>raise FileExistsError(str(self.temp_dir))<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>try:<EOL><INDENT>with ZipFile(str(self.miz_path)) as zip_file:<EOL><INDENT>LOGGER.debug('<STR_LIT>')<EOL>self.zip_content = [f.filename for f in zip_file.infolist()]<EOL>self._extract_files_from_zip(zip_file)<EOL><DEDENT><DEDENT>except BadZipFile:<EOL><INDENT>raise BadZipFile(str(self.miz_path))<EOL><DEDENT>except: <EOL><INDENT>LOGGER.exception('<STR_LIT>', self.miz_path)<EOL>raise<EOL><DEDENT>LOGGER.debug('<STR_LIT>')<EOL>for miz_item in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if not Path(self.temp_dir.joinpath(miz_item)).exists():<EOL><INDENT>LOGGER.error('<STR_LIT>', miz_item)<EOL>raise FileNotFoundError(miz_item)<EOL><DEDENT><DEDENT>self._check_extracted_content()<EOL>LOGGER.debug('<STR_LIT>')<EOL> | Flattens a MIZ file into the temp dir
Args:
overwrite: allow overwriting exiting files | f11782:c0:m18 |
def zip(self, destination: typing.Union[str, Path] = None, encode: bool = True) -> str: | if encode:<EOL><INDENT>self._encode()<EOL><DEDENT>if destination is None:<EOL><INDENT>destination_path = self.miz_path.parent.joinpath(f'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>destination_path = Path(destination).absolute()<EOL>if destination_path.exists() and not destination_path.is_file():<EOL><INDENT>raise ValueError(f'<STR_LIT>')<EOL><DEDENT><DEDENT>LOGGER.debug('<STR_LIT>', destination_path)<EOL>destination_path.write_bytes(dummy_miz)<EOL>with ZipFile(str(destination_path), mode='<STR_LIT:w>', compression=<NUM_LIT:8>) as zip_file:<EOL><INDENT>for root, _, items in os.walk(self.temp_dir.absolute()):<EOL><INDENT>for item in items:<EOL><INDENT>item_abs_path = Path(root, item).absolute()<EOL>item_rel_path = Path(item_abs_path).relative_to(self.temp_dir)<EOL>zip_file.write(item_abs_path, arcname=item_rel_path)<EOL><DEDENT><DEDENT><DEDENT>return str(destination_path)<EOL> | Write mission, dictionary etc. to a MIZ file
:param destination: target MIZ file (if none, defaults to source MIZ + "_EMIZ"
:type destination: str or Path
:param encode: if True, re-encode the lua tables
:type encode: bool
:return: destination file
:rtype: str | f11782:c0:m19 |
def read_local_files(*file_paths: str) -> str: | def _read_single_file(file_path):<EOL><INDENT>with open(file_path) as f:<EOL><INDENT>filename = os.path.splitext(file_path)[<NUM_LIT:0>]<EOL>title = f'<STR_LIT>'<EOL>return '<STR_LIT>'.join((title, f.read()))<EOL><DEDENT><DEDENT>return '<STR_LIT:\n>' + '<STR_LIT>'.join(map(_read_single_file, file_paths))<EOL> | Reads one or more text files and returns them joined together.
A title is automatically created based on the file name.
Args:
*file_paths: list of files to aggregate
Returns: content of files | f11784:m0 |
def showDosHeaderData(peInstance): | dosFields = peInstance.dosHeader.getFields()<EOL>print("<STR_LIT>")<EOL>for field in dosFields:<EOL><INDENT>if isinstance(dosFields[field], datatypes.Array):<EOL><INDENT>print("<STR_LIT>" % (field, len(dosFields[field])))<EOL>counter = <NUM_LIT:0><EOL>for element in dosFields[field]:<EOL><INDENT>print("<STR_LIT>" % (counter, element.value))<EOL>counter += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>" % (field, dosFields[field].value))<EOL><DEDENT><DEDENT> | Prints IMAGE_DOS_HEADER fields. | f11785:m0 |
def showNtHeadersData(peInstance): | print("<STR_LIT>")<EOL>print("<STR_LIT>" % peInstance.ntHeaders.signature.value)<EOL> | Prints IMAGE_NT_HEADERS signature. | f11785:m1 |
def showFileHeaderData(peInstance): | fileHeaderFields = peInstance.ntHeaders.fileHeader.getFields() <EOL>print("<STR_LIT>")<EOL>for field in fileHeaderFields:<EOL><INDENT>print("<STR_LIT>" % (field, fileHeaderFields[field].value))<EOL><DEDENT> | Prints IMAGE_FILE_HEADER fields. | f11785:m2 |
def showOptionalHeaderData(peInstance): | print("<STR_LIT>")<EOL>ohFields = peInstance.ntHeaders.optionalHeader.getFields()<EOL>for field in ohFields:<EOL><INDENT>if not isinstance(ohFields[field], datadirs.DataDirectory):<EOL><INDENT>print("<STR_LIT>" % (field, ohFields[field].value))<EOL><DEDENT><DEDENT> | Prints IMAGE_OPTIONAL_HEADER fields. | f11785:m3 |
def showDataDirectoriesData(peInstance): | print("<STR_LIT>")<EOL>dirs = peInstance.ntHeaders.optionalHeader.dataDirectory<EOL>counter = <NUM_LIT:1><EOL>for dir in dirs:<EOL><INDENT>print("<STR_LIT>" % (counter, dir.name.value, dir.rva.value, dir.size.value))<EOL>counter += <NUM_LIT:1><EOL><DEDENT> | Prints the DATA_DIRECTORY fields. | f11785:m4 |
def showSectionsHeaders(peInstance): | print("<STR_LIT>")<EOL>print("<STR_LIT>" % peInstance.ntHeaders.fileHeader.numberOfSections.value)<EOL>for section in peInstance.sectionHeaders:<EOL><INDENT>fields = section.getFields()<EOL>for field in fields:<EOL><INDENT>if isinstance(fields[field], datatypes.String):<EOL><INDENT>fmt = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>fmt = "<STR_LIT>"<EOL><DEDENT>print(fmt % (field, fields[field].value))<EOL><DEDENT>print("<STR_LIT:\n>")<EOL><DEDENT> | Prints IMAGE_SECTION_HEADER for every section present in the file. | f11785:m5 |
def showImports(peInstance): | iidEntries = peInstance.ntHeaders.optionalHeader.dataDirectory[consts.IMPORT_DIRECTORY].info<EOL>if iidEntries:<EOL><INDENT>for iidEntry in iidEntries:<EOL><INDENT>fields = iidEntry.getFields()<EOL>print("<STR_LIT>" % iidEntry.metaData.moduleName.value)<EOL>for field in fields:<EOL><INDENT>print("<STR_LIT>" % (field, fields[field].value))<EOL><DEDENT>for iatEntry in iidEntry.iat:<EOL><INDENT>fields = iatEntry.getFields()<EOL>for field in fields:<EOL><INDENT>print("<STR_LIT>" % (field, fields[field].value))<EOL><DEDENT><DEDENT>print("<STR_LIT:\n>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT> | Shows imports information. | f11785:m6 |
def showExports(peInstance): | exports = peInstance.ntHeaders.optionalHeader.dataDirectory[consts.EXPORT_DIRECTORY].info<EOL>if exports:<EOL><INDENT>exp_fields = exports.getFields()<EOL>for field in exp_fields:<EOL><INDENT>print("<STR_LIT>" % (field, exp_fields[field].value))<EOL><DEDENT>for entry in exports.exportTable:<EOL><INDENT>entry_fields = entry.getFields()<EOL>for field in entry_fields:<EOL><INDENT>print("<STR_LIT>" % (field, entry_fields[field].value)) <EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT> | Show exports information | f11785:m7 |
def __init__(self, shouldPack = True): | self.shouldPack = shouldPack<EOL>self._attrsList = []<EOL> | @type shouldPack: bool
@param shouldPack: (Optional) If the value is set to C{True}, the class will be packed. If the value is
set to C{False}, the class will not be packed. | f11786:c0:m0 |
def getFields(self): | d = {}<EOL>for i in self._attrsList:<EOL><INDENT>key = i<EOL>value = getattr(self, i)<EOL>d[key] = value<EOL><DEDENT>return d<EOL> | Returns all the class attributues.
@rtype: dict
@return: A dictionary containing all the class attributes. | f11786:c0:m5 |
def getType(self): | raise NotImplementedError("<STR_LIT>")<EOL> | This method should be implemented in the inherited classes. When implemented, returns
an integer value to identified the corresponding class.
@raise NotImplementedError: The method wasn't implemented in the inherited class. | f11786:c0:m6 |
def __init__(self, value = <NUM_LIT:0>, endianness = "<STR_LIT:<>", signed = False, shouldPack = True): | self.value = value<EOL>self.endianness = endianness<EOL>self.signed = signed<EOL>self.shouldPack = shouldPack<EOL> | @type value: int
@param value: The value used to build the L{BYTE} object.
@type endianness: str
@param endianness: (Optional) Indicates the endianness of the L{BYTE} object. The C{<} indicates little-endian while C{>} indicates big-endian.
@type signed: bool
@param signed: (Optional) If set to C{True} the L{BYTE} object will be packed as signed. If set to C{False} it will be packed as unsigned.
@type shouldPack: bool
@param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed. | f11786:c1:m0 |
def __init__(self, value = "<STR_LIT>", shouldPack = True): | self.value = value<EOL>self.shouldPack = shouldPack<EOL> | @type value: str
@param value: the string to be built.
@type shouldPack: bool
@param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed.
@todo: Add a UnicodeString class. | f11787:c0:m0 |
def sizeof(self): | return len(self)<EOL> | Returns the size of the string. | f11787:c0:m3 |
def __init__(self, value, shouldPack = True, align = <NUM_LIT:4>): | String.__init__(self, value)<EOL>self.align = align<EOL>self.value = value + "<STR_LIT:\x00>" * (self.align - len(value) % self.align)<EOL>self.shouldPack = shouldPack<EOL> | This object represent an aligned ASCII string.
@type value: str
@param value: The string to be built.
@type shouldPack: bool
@param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed.
@type align: int
@param align: (Optional) The alignment to be used. The default alignment is 4. | f11787:c1:m0 |
def __init__(self, arrayType, shouldPack = True): | list.__init__(self)<EOL>self.arrayType = arrayType<EOL>self.shouldPack = shouldPack<EOL>if not self.arrayType in [TYPE_BYTE, TYPE_WORD, TYPE_DWORD, TYPE_QWORD]:<EOL><INDENT>raise TypeError("<STR_LIT>" % self.arrayType)<EOL><DEDENT> | @type arrayType: int
@param arrayType: The type of array to be built. This value can be C{TYPE_BYTE}, C{TYPE_WORD}, C{TYPE_DWORD} or C{TYPE_QWORD}.
@type shouldPack: bool
@param shouldPack: (Optional) If set to c{True}, the object will be packed. If set to C{False}, the object won't be packed.
@todo: Before to add an element to the array we must check if the type of that element is one we are expecting. | f11787:c2:m0 |
def sizeof(self): | return len(self)<EOL> | Returns the size of the array. | f11787:c2:m2 |
@staticmethod<EOL><INDENT>def parse(readDataInstance, arrayType, arrayLength):<DEDENT> | newArray = Array(arrayType)<EOL>dataLength = len(readDataInstance)<EOL>if arrayType is TYPE_DWORD:<EOL><INDENT>toRead = arrayLength * <NUM_LIT:4><EOL>if dataLength >= toRead: <EOL><INDENT>for i in range(arrayLength):<EOL><INDENT>newArray.append(DWORD(readDataInstance.readDword()))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.DataLengthException("<STR_LIT>")<EOL><DEDENT><DEDENT>elif arrayType is TYPE_WORD:<EOL><INDENT>toRead = arrayLength * <NUM_LIT:2><EOL>if dataLength >= toRead:<EOL><INDENT>for i in range(arrayLength):<EOL><INDENT>newArray.append(DWORD(readDataInstance.readWord()))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.DataLengthException("<STR_LIT>")<EOL><DEDENT><DEDENT>elif arrayType is TYPE_QWORD:<EOL><INDENT>toRead = arrayLength * <NUM_LIT:8><EOL>if dataLength >= toRead:<EOL><INDENT>for i in range(arrayLength):<EOL><INDENT>newArray.append(QWORD(readDataInstance.readQword()))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.DataLengthException("<STR_LIT>")<EOL><DEDENT><DEDENT>elif arrayType is TYPE_BYTE:<EOL><INDENT>for i in range(arrayLength):<EOL><INDENT>newArray.append(BYTE(readDataInstance.readByte()))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.ArrayTypeException("<STR_LIT>" % arrayType)<EOL><DEDENT>return newArray<EOL> | Returns a new L{Array} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: The L{ReadData} object containing the array data.
@type arrayType: int
@param arrayType: The type of L{Array} to be built.
@type arrayLength: int
@param arrayLength: The length of the array passed as an argument.
@rtype: L{Array}
@return: New L{Array} object. | f11787:c2:m3 |
def getType(self): | return TYPE_ARRAY<EOL> | Returns an integer value identifying the type of object. | f11787:c2:m4 |
def getType(self): | return TYPE_BYTE<EOL> | Returns L{TYPE_BYTE}. | f11787:c3:m3 |
def sizeof(self): | return len(self)<EOL> | Returns the size of L{BYTE}. | f11787:c3:m4 |
@staticmethod<EOL><INDENT>def parse(readDataInstance):<DEDENT> | return BYTE(readDataInstance.readByte())<EOL> | Returns a new L{BYTE} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} object with the corresponding data to generate a new L{BYTE} object.
@rtype: L{BYTE}
@return: A new L{BYTE} object. | f11787:c3:m5 |
def getType(self): | return TYPE_WORD<EOL> | Returns L{TYPE_WORD}. | f11787:c4:m3 |
def sizeof(self): | return len(self)<EOL> | Returns the size of L{WORD}. | f11787:c4:m4 |
@staticmethod<EOL><INDENT>def parse(readDataInstance):<DEDENT> | return WORD(readDataInstance.readWord())<EOL> | Returns a new L{WORD} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} object containing the necessary data to build a new L{WORD} object.
@rtype: L{WORD}
@return: A new L{WORD} object. | f11787:c4:m5 |
def getType(self): | return TYPE_DWORD<EOL> | Returns L{TYPE_DWORD}. | f11787:c5:m3 |
def sizeof(self): | return len(self)<EOL> | Returns the size of L{DWORD}. | f11787:c5:m4 |
@staticmethod<EOL><INDENT>def parse(readDataInstance):<DEDENT> | return DWORD(readDataInstance.readDword())<EOL> | Returns a new L{DWORD} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} object with the necessary data to build a new L{DWORD} object.
@rtype: L{DWORD}
@return: A new L{DWORD} object. | f11787:c5:m5 |
def getType(self): | return TYPE_QWORD<EOL> | Returns L{TYPE_QWORD}. | f11787:c6:m3 |
def sizeof(self): | return len(self)<EOL> | Returns the size of L{QWORD}. | f11787:c6:m4 |
@staticmethod<EOL><INDENT>def parse(readDataInstance):<DEDENT> | return QWORD(readDataInstance.readQword())<EOL> | Returns a new L{QWORD} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} object with the necessary data to build a new L{QWORD} object.
@rtype: L{QWORD}
@return: A new L{QWORD} object. | f11787:c6:m5 |
def __init__(self, pathToFile = None, data = None, fastLoad = False, verbose = False): | self.dosHeader = DosHeader() <EOL>self.dosStub = PE.getDosStub() <EOL>self.ntHeaders = NtHeaders() <EOL>self.sectionHeaders = SectionHeaders() <EOL>self.sections = Sections(self.sectionHeaders) <EOL>self.overlay = "<STR_LIT>"<EOL>self.signature = "<STR_LIT>"<EOL>self._data = data<EOL>self._pathToFile = pathToFile<EOL>self._verbose = verbose<EOL>self._fastLoad = fastLoad<EOL>self.PE_TYPE = None<EOL>if self._data and not isinstance(data, utils.ReadData):<EOL><INDENT>rd = utils.ReadData(data)<EOL>self._internalParse(rd)<EOL><DEDENT>elif self._pathToFile:<EOL><INDENT>if os.path.exists(self._pathToFile):<EOL><INDENT>stat = os.stat(self._pathToFile)<EOL>if stat.st_size == <NUM_LIT:0>:<EOL><INDENT>raise PEException("<STR_LIT>")<EOL><DEDENT>self._data = self.readFile(self._pathToFile)<EOL>rd = utils.ReadData(self._data)<EOL>if self.hasMZSignature(rd) and self.hasPESignature(rd):<EOL><INDENT>rd.setOffset(<NUM_LIT:0>)<EOL>self._internalParse(rd)<EOL><DEDENT>else:<EOL><INDENT>raise excep.PyPe32Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.NotValidPathException("<STR_LIT>")<EOL><DEDENT><DEDENT>self.validate()<EOL> | A class representation of the Portable Executable format.
@see: PE format U{http://msdn.microsoft.com/en-us/library/windows/desktop/ms680547%28v=vs.85%29.aspx}
@type pathToFile: str
@param pathToFile: Path to the file to load.
@type data: str
@param data: PE data to process.
@type fastLoad: bool
@param fastLoad: If set to C{False}, the PE class won't parse the directory data, just headers.
The L{fullLoad} method is available to load the directories in case the C{fastLoad} parameter was set to C{False}.
If set to C{True}, the entire PE will be parsed.
@type verbose: bool
@param verbose: Verbose output.
@todo: Parse the Resource directory.
@todo: Parse the Delay Imports directory.
@todo: Parse the Exception directory.
@todo: Add dump() method to show nicely all the structure of the PE file. | f11791:c0:m0 |
def hasMZSignature(self, rd): | rd.setOffset(<NUM_LIT:0>)<EOL>sign = rd.read(<NUM_LIT:2>)<EOL>if sign == "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT>return False<EOL> | Check for MZ signature.
@type rd: L{ReadData}
@param rd: A L{ReadData} object.
@rtype: bool
@return: True is the given L{ReadData} stream has the MZ signature. Otherwise, False. | f11791:c0:m1 |
def hasPESignature(self, rd): | rd.setOffset(<NUM_LIT:0>)<EOL>e_lfanew_offset = unpack("<STR_LIT>", rd.readAt(<NUM_LIT>, <NUM_LIT:4>))[<NUM_LIT:0>]<EOL>sign = rd.readAt(e_lfanew_offset, <NUM_LIT:2>)<EOL>if sign == "<STR_LIT>":<EOL><INDENT>return True<EOL><DEDENT>return False<EOL> | Check for PE signature.
@type rd: L{ReadData}
@param rd: A L{ReadData} object.
@rtype: bool
@return: True is the given L{ReadData} stream has the PE signature. Otherwise, False. | f11791:c0:m2 |
def validate(self): | <EOL>if self.dosHeader.e_magic.value != consts.MZ_SIGNATURE:<EOL><INDENT>raise excep.PEException("<STR_LIT>" % (self.dosHeader.magic.value, consts.MZ_SIGNATURE))<EOL><DEDENT>if self.dosHeader.e_lfanew.value > len(self):<EOL><INDENT>raise excep.PEException("<STR_LIT>")<EOL><DEDENT>if self.ntHeaders.signature.value != consts.PE_SIGNATURE: <EOL><INDENT>raise excep.PEException("<STR_LIT>" % (self.ntHeaders.optionaHeader.signature.value, consts.PE_SIGNATURE))<EOL><DEDENT>if self.ntHeaders.optionalHeader.numberOfRvaAndSizes.value > <NUM_LIT>:<EOL><INDENT>print(excep.PEWarning("<STR_LIT>" % self.ntHeaders.optionaHeader.numberOfRvaAndSizes.value))<EOL><DEDENT> | Performs validations over some fields of the PE structure to determine if the loaded file has a valid PE format.
@raise PEException: If an invalid value is found into the PE instance. | f11791:c0:m3 |
def readFile(self, pathToFile): | fd = open(pathToFile, "<STR_LIT:rb>")<EOL>data = fd.read()<EOL>fd.close()<EOL>return data<EOL> | Returns data from a file.
@type pathToFile: str
@param pathToFile: Path to the file.
@rtype: str
@return: The data from file. | f11791:c0:m4 |
def write(self, filename = "<STR_LIT>"): | file_data = str(self)<EOL>if filename:<EOL><INDENT>try:<EOL><INDENT>self.__write(filename, file_data)<EOL><DEDENT>except IOError:<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return file_data<EOL><DEDENT> | Writes data from L{PE} object to a file.
@rtype: str
@return: The L{PE} stream data.
@raise IOError: If the file could not be opened for write operations. | f11791:c0:m5 |
def __write(self, thePath, theData): | fd = open(thePath, "<STR_LIT:wb>")<EOL>fd.write(theData)<EOL>fd.close()<EOL> | Write data to a file.
@type thePath: str
@param thePath: The file path.
@type theData: str
@param theData: The data to write. | f11791:c0:m6 |
def _updateDirectoriesData(self, peStr): | dataDirs = self.ntHeaders.optionalHeader.dataDirectory<EOL>wr = utils.WriteData(data)<EOL>for dir in dataDirs:<EOL><INDENT>dataToWrite = str(dir.info)<EOL>if len(dataToWrite) != dir.size.value and self._verbose:<EOL><INDENT>print(excep.DataLengthException("<STR_LIT>" % (dir.size.value, len(dataToWrite))))<EOL><DEDENT>wr.setOffset(self.getOffsetFromRva(dir.rva.value))<EOL>wr.write(dataToWrite)<EOL><DEDENT>return str(wr)<EOL> | Updates the data in every L{Directory} object.
@type peStr: str
@param peStr: C{str} representation of the L{PE} object.
@rtype: str
@return: A C{str} representation of the L{PE} object. | f11791:c0:m9 |
def _getPaddingDataToSectionOffset(self): | start = self._getPaddingToSectionOffset()<EOL>end = self.sectionHeaders[<NUM_LIT:0>].pointerToRawData.value - start<EOL>return self._data[start:start+end]<EOL> | Returns the data between the last section header and the begenning of data from the first section.
@rtype: str
@return: Data between last section header and the begenning of the first section. | f11791:c0:m10 |
def _getSignature(self, readDataInstance, dataDirectoryInstance): | signature = "<STR_LIT>"<EOL>if readDataInstance is not None and dataDirectoryInstance is not None: <EOL><INDENT>securityDirectory = dataDirectoryInstance[consts.SECURITY_DIRECTORY]<EOL>if(securityDirectory.rva.value and securityDirectory.size.value):<EOL><INDENT>readDataInstance.setOffset(self.getOffsetFromRva(securityDirectory.rva.value))<EOL>signature = readDataInstance.read(securityDirectory.size.value)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.InstanceErrorException("<STR_LIT>")<EOL><DEDENT>return signature<EOL> | Returns the digital signature within a digital signed PE file.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} instance containing a PE file data.
@type dataDirectoryInstance: L{DataDirectory}
@param dataDirectoryInstance: A L{DataDirectory} object containing the information about directories.
@rtype: str
@return: A string with the digital signature.
@raise InstanceErrorException: If the C{readDataInstance} or the C{dataDirectoryInstance} were not specified. | f11791:c0:m11 |
def _getOverlay(self, readDataInstance, sectionHdrsInstance): | if readDataInstance is not None and sectionHdrsInstance is not None: <EOL><INDENT>try:<EOL><INDENT>offset = sectionHdrsInstance[-<NUM_LIT:1>].pointerToRawData.value + sectionHdrsInstance[-<NUM_LIT:1>].sizeOfRawData.value<EOL>readDataInstance.setOffset(offset)<EOL><DEDENT>except excep.WrongOffsetValueException:<EOL><INDENT>if self._verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.InstanceErrorException("<STR_LIT>")<EOL><DEDENT>return readDataInstance.data[readDataInstance.offset:]<EOL> | Returns the overlay data from the PE file.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} instance containing the PE file data.
@type sectionHdrsInstance: L{SectionHeaders}
@param sectionHdrsInstance: A L{SectionHeaders} instance containing the information about the sections present in the PE file.
@rtype: str
@return: A string with the overlay data from the PE file.
@raise InstanceErrorException: If the C{readDataInstance} or the C{sectionHdrsInstance} were not specified. | f11791:c0:m12 |
def getOffsetFromRva(self, rva): | offset = -<NUM_LIT:1><EOL>s = self.getSectionByRva(rva)<EOL>if s != offset:<EOL><INDENT>offset = (rva - self.sectionHeaders[s].virtualAddress.value) + self.sectionHeaders[s].pointerToRawData.value<EOL><DEDENT>else:<EOL><INDENT>offset = rva<EOL><DEDENT>return offset<EOL> | Converts an offset to an RVA.
@type rva: int
@param rva: The RVA to be converted.
@rtype: int
@return: An integer value representing an offset in the PE file. | f11791:c0:m13 |
def getRvaFromOffset(self, offset): | rva = -<NUM_LIT:1><EOL>s = self.getSectionByOffset(offset)<EOL>if s:<EOL><INDENT>rva = (offset - self.sectionHeaders[s].pointerToRawData.value) + self.sectionHeaders[s].virtualAddress.value<EOL><DEDENT>return rva<EOL> | Converts a RVA to an offset.
@type offset: int
@param offset: The offset value to be converted to RVA.
@rtype: int
@return: The RVA obtained from the given offset. | f11791:c0:m14 |
def getSectionByOffset(self, offset): | index = -<NUM_LIT:1><EOL>for i in range(len(self.sectionHeaders)):<EOL><INDENT>if (offset < self.sectionHeaders[i].pointerToRawData.value + self.sectionHeaders[i].sizeOfRawData.value):<EOL><INDENT>index = i<EOL>break<EOL><DEDENT><DEDENT>return index<EOL> | Given an offset in the file, tries to determine the section this offset belong to.
@type offset: int
@param offset: Offset value.
@rtype: int
@return: An index, starting at 0, that represents the section the given offset belongs to. | f11791:c0:m15 |
def getSectionIndexByName(self, name): | index = -<NUM_LIT:1><EOL>if name:<EOL><INDENT>for i in range(len(self.sectionHeaders)):<EOL><INDENT>if self.sectionHeaders[i].name.value.find(name) >= <NUM_LIT:0>:<EOL><INDENT>index = i<EOL>break<EOL><DEDENT><DEDENT><DEDENT>return index<EOL> | Given a string representing a section name, tries to find the section index.
@type name: str
@param name: A section name.
@rtype: int
@return: The index, starting at 0, of the section. | f11791:c0:m16 |
def getSectionByRva(self, rva): | index = -<NUM_LIT:1><EOL>if rva < self.sectionHeaders[<NUM_LIT:0>].virtualAddress.value:<EOL><INDENT>return index<EOL><DEDENT>for i in range(len(self.sectionHeaders)):<EOL><INDENT>fa = self.ntHeaders.optionalHeader.fileAlignment.value<EOL>prd = self.sectionHeaders[i].pointerToRawData.value<EOL>srd = self.sectionHeaders[i].sizeOfRawData.value<EOL>if len(str(self)) - self._adjustFileAlignment(prd, fa) < srd:<EOL><INDENT>size = self.sectionHeaders[i].misc.value<EOL><DEDENT>else:<EOL><INDENT>size = max(srd, self.sectionHeaders[i].misc.value)<EOL><DEDENT>if (self.sectionHeaders[i].virtualAddress.value <= rva) and rva < (self.sectionHeaders[i].virtualAddress.value + size):<EOL><INDENT>index = i<EOL>break<EOL><DEDENT><DEDENT>return index<EOL> | Given a RVA in the file, tries to determine the section this RVA belongs to.
@type rva: int
@param rva: RVA value.
@rtype: int
@return: An index, starting at 1, that represents the section the given RVA belongs to. | f11791:c0:m17 |
@staticmethod<EOL><INDENT>def getDosStub():<DEDENT> | return "<STR_LIT>".decode("<STR_LIT>")<EOL> | Returns a default DOS stub.
@rtype: str
@return: A defaul DOS stub. | f11791:c0:m18 |
def _getPaddingToSectionOffset(self): | return len(str(self.dosHeader) + str(self.dosStub) + str(self.ntHeaders) + str(self.sectionHeaders))<EOL> | Returns the offset to last section header present in the PE file.
@rtype: int
@return: The offset where the end of the last section header resides in the PE file. | f11791:c0:m19 |
def fullLoad(self): | self._parseDirectories(self.ntHeaders.optionalHeader.dataDirectory, self.PE_TYPE)<EOL> | Parse all the directories in the PE file. | f11791:c0:m20 |
def _internalParse(self, readDataInstance): | self.dosHeader = DosHeader.parse(readDataInstance)<EOL>self.dosStub = readDataInstance.read(self.dosHeader.e_lfanew.value - readDataInstance.offset)<EOL>self.ntHeaders = NtHeaders.parse(readDataInstance)<EOL>if self.ntHeaders.optionalHeader.magic.value == consts.PE32:<EOL><INDENT>self.PE_TYPE = consts.PE32<EOL><DEDENT>elif self.ntHeaders.optionalHeader.magic.value == consts.PE64:<EOL><INDENT>self.PE_TYPE = consts.PE64<EOL>readDataInstance.setOffset(readDataInstance.tell() - OptionalHeader().sizeof())<EOL>self.ntHeaders.optionalHeader = OptionalHeader64.parse(readDataInstance)<EOL><DEDENT>self.sectionHeaders = SectionHeaders.parse(readDataInstance, self.ntHeaders.fileHeader.numberOfSections.value)<EOL>readDataInstance.setOffset(self.sectionHeaders[<NUM_LIT:0>].pointerToRawData.value)<EOL>self.sections = Sections.parse(readDataInstance, self.sectionHeaders)<EOL>self.overlay = self._getOverlay(readDataInstance, self.sectionHeaders)<EOL>self.signature = self._getSignature(readDataInstance, self.ntHeaders.optionalHeader.dataDirectory)<EOL>if not self._fastLoad:<EOL><INDENT>self._parseDirectories(self.ntHeaders.optionalHeader.dataDirectory, self.PE_TYPE)<EOL><DEDENT> | Populates the attributes of the L{PE} object.
@type readDataInstance: L{ReadData}
@param readDataInstance: A L{ReadData} instance with the data of a PE file. | f11791:c0:m21 |
def addSection(self, data, name ="<STR_LIT>", flags = <NUM_LIT>): | fa = self.ntHeaders.optionalHeader.fileAlignment.value<EOL>sa = self.ntHeaders.optionalHeader.sectionAlignment.value<EOL>padding = "<STR_LIT>" * (fa - len(data))<EOL>sh = SectionHeader()<EOL>if len(self.sectionHeaders):<EOL><INDENT>vaLastSection = self.sectionHeaders[-<NUM_LIT:1>].virtualAddress.value<EOL>sizeLastSection = self.sectionHeaders[-<NUM_LIT:1>].misc.value<EOL>pointerToRawDataLastSection = self.sectionHeaders[-<NUM_LIT:1>].pointerToRawData.value<EOL>sizeOfRawDataLastSection = self.sectionHeaders[-<NUM_LIT:1>].sizeOfRawData.value<EOL>sh.virtualAddress.value = self._adjustSectionAlignment(vaLastSection + sizeLastSection, fa, sa)<EOL>sh.pointerToRawData.value = self._adjustFileAlignment(pointerToRawDataLastSection + sizeOfRawDataLastSection, fa)<EOL><DEDENT>sh.misc.value = self._adjustSectionAlignment(len(data), fa, sa) or consts.DEFAULT_PAGE_SIZE<EOL>sh.sizeOfRawData.value = self._adjustFileAlignment(len(data), fa) or consts.DEFAULT_FILE_ALIGNMENT <EOL>sh.characteristics.value = flags<EOL>sh.name.value = name<EOL>self.sectionHeaders.append(sh)<EOL>self.sections.append(data + padding)<EOL>self.ntHeaders.fileHeader.numberOfSections.value += <NUM_LIT:1><EOL> | Adds a new section to the existing L{PE} instance.
@type data: str
@param data: The data to be added in the new section.
@type name: str
@param name: (Optional) The name for the new section.
@type flags: int
@param flags: (Optional) The attributes for the new section. | f11791:c0:m22 |
def extendSection(self, sectionIndex, data): | fa = self.ntHeaders.optionalHeader.fileAlignment.value<EOL>sa = self.ntHeaders.optionalHeader.sectionAlignment.value <EOL>if len(self.sectionHeaders):<EOL><INDENT>if len(self.sectionHeaders) == sectionIndex:<EOL><INDENT>try:<EOL><INDENT>vzLastSection = self.sectionHeaders[-<NUM_LIT:1>].misc.value <EOL>rzLastSection = self.sectionHeaders[-<NUM_LIT:1>].sizeOfRawData.value<EOL>self.sectionHeaders[-<NUM_LIT:1>].misc.value = self._adjustSectionAlignment(vzLastSection + len(data), fa, sa)<EOL>self.sectionHeaders[-<NUM_LIT:1>].sizeOfRawData.value = self._adjustFileAlignment(rzLastSection + len(data), fa)<EOL>vz = self.sectionHeaders[-<NUM_LIT:1>].misc.value <EOL>rz = self.sectionHeaders[-<NUM_LIT:1>].sizeOfRawData.value<EOL><DEDENT>except IndexError:<EOL><INDENT>raise IndexError("<STR_LIT>")<EOL><DEDENT>if vz < rz:<EOL><INDENT>print("<STR_LIT>" % (vz, rz))<EOL><DEDENT>if len(data) % fa == <NUM_LIT:0>:<EOL><INDENT>self.sections[-<NUM_LIT:1>] += data<EOL><DEDENT>else:<EOL><INDENT>self.sections[-<NUM_LIT:1>] += data + "<STR_LIT>" * (fa - len(data) % fa)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>counter = sectionIndex - <NUM_LIT:1><EOL>vzCurrentSection = self.sectionHeaders[counter].misc.value<EOL>rzCurrentSection = self.sectionHeaders[counter].sizeOfRawData.value<EOL>self.sectionHeaders[counter].misc.value = self._adjustSectionAlignment(vzCurrentSection + len(data), fa, sa)<EOL>self.sectionHeaders[counter].sizeOfRawData.value = self._adjustFileAlignment(rzCurrentSection + len(data), fa)<EOL>if len(data) % fa == <NUM_LIT:0>:<EOL><INDENT>self.sections[counter] += data<EOL><DEDENT>else:<EOL><INDENT>self.sections[counter] += data + "<STR_LIT>" * (fa - len(data) % fa)<EOL><DEDENT>counter += <NUM_LIT:1><EOL>while(counter != len(self.sectionHeaders)):<EOL><INDENT>vzPreviousSection = self.sectionHeaders[counter - <NUM_LIT:1>].misc.value<EOL>vaPreviousSection = self.sectionHeaders[counter - <NUM_LIT:1>].virtualAddress.value<EOL>rzPreviousSection = self.sectionHeaders[counter - <NUM_LIT:1>].sizeOfRawData.value<EOL>roPreviousSection = self.sectionHeaders[counter - <NUM_LIT:1>].pointerToRawData.value<EOL>self.sectionHeaders[counter].virtualAddress.value = self._adjustSectionAlignment(vzPreviousSection + vaPreviousSection, fa, sa)<EOL>self.sectionHeaders[counter].pointerToRawData.value = self._adjustFileAlignment(rzPreviousSection + roPreviousSection, fa)<EOL>vz = self.sectionHeaders[counter].virtualAddress.value <EOL>rz = self.sectionHeaders[counter].pointerToRawData.value<EOL>if vz < rz:<EOL><INDENT>print("<STR_LIT>" % (vz, rz))<EOL><DEDENT>counter += <NUM_LIT:1><EOL><DEDENT><DEDENT>except IndexError:<EOL><INDENT>raise IndexError("<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise excep.SectionHeadersException("<STR_LIT>")<EOL><DEDENT> | Extends an existing section in the L{PE} instance.
@type sectionIndex: int
@param sectionIndex: The index for the section to be extended.
@type data: str
@param data: The data to include in the section.
@raise IndexError: If an invalid C{sectionIndex} was specified.
@raise SectionHeadersException: If there is not section to extend. | f11791:c0:m23 |
def _fixPe(self): | sizeOfImage = <NUM_LIT:0><EOL>for sh in self.sectionHeaders:<EOL><INDENT>sizeOfImage += sh.misc<EOL><DEDENT>self.ntHeaders.optionaHeader.sizeoOfImage.value = self._sectionAlignment(sizeOfImage + <NUM_LIT>)<EOL> | Fixes the necessary fields in the PE file instance in order to create a valid PE32. i.e. SizeOfImage. | f11791:c0:m24 |
def _adjustFileAlignment(self, value, fileAlignment): | if fileAlignment > consts.DEFAULT_FILE_ALIGNMENT:<EOL><INDENT>if not utils.powerOfTwo(fileAlignment):<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>if fileAlignment < consts.DEFAULT_FILE_ALIGNMENT:<EOL><INDENT>return value<EOL><DEDENT>if fileAlignment and value % fileAlignment:<EOL><INDENT>return ((value / fileAlignment) + <NUM_LIT:1>) * fileAlignment<EOL><DEDENT>return value<EOL> | Align a value to C{FileAligment}.
@type value: int
@param value: The value to align.
@type fileAlignment: int
@param fileAlignment: The value to be used to align the C{value} parameter.
@rtype: int
@return: The aligned value. | f11791:c0:m25 |
def _adjustSectionAlignment(self, value, fileAlignment, sectionAlignment): | if fileAlignment < consts.DEFAULT_FILE_ALIGNMENT:<EOL><INDENT>if fileAligment != sectionAlignment:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT><DEDENT>if sectionAlignment < consts.DEFAULT_PAGE_SIZE:<EOL><INDENT>sectionAlignment = fileAlignment<EOL><DEDENT>if sectionAlignment and value % sectionAlignment:<EOL><INDENT>return sectionAlignment * ((value / sectionAlignment) + <NUM_LIT:1>)<EOL><DEDENT>return value<EOL> | Align a value to C{SectionAligment}.
@type value: int
@param value: The value to be aligned.
@type fileAlignment: int
@param fileAlignment: The value to be used as C{FileAlignment}.
@type sectionAlignment: int
@param sectionAlignment: The value to be used as C{SectionAlignment}.
@rtype: int
@return: The aligned value. | f11791:c0:m26 |
def getDwordAtRva(self, rva): | return datatypes.DWORD.parse(utils.ReadData(self.getDataAtRva(rva, <NUM_LIT:4>)))<EOL> | Returns a C{DWORD} from a given RVA.
@type rva: int
@param rva: The RVA to get the C{DWORD} from.
@rtype: L{DWORD}
@return: The L{DWORD} obtained at the given RVA. | f11791:c0:m27 |
def getWordAtRva(self, rva): | return datatypes.WORD.parse(utils.ReadData(self.getDataAtRva(rva, <NUM_LIT:2>)))<EOL> | Returns a C{WORD} from a given RVA.
@type rva: int
@param rva: The RVA to get the C{WORD} from.
@rtype: L{WORD}
@return: The L{WORD} obtained at the given RVA. | f11791:c0:m28 |
def getDwordAtOffset(self, offset): | return datatypes.DWORD.parse(utils.ReadData(self.getDataAtOffset(offset, <NUM_LIT:4>)))<EOL> | Returns a C{DWORD} from a given offset.
@type offset: int
@param offset: The offset to get the C{DWORD} from.
@rtype: L{DWORD}
@return: The L{DWORD} obtained at the given offset. | f11791:c0:m29 |
def getWordAtOffset(self, offset): | return datatypes.WORD.parse(utils.ReadData(self.getDataAtOffset(offset, <NUM_LIT:2>)))<EOL> | Returns a C{WORD} from a given offset.
@type offset: int
@param offset: The offset to get the C{WORD} from.
@rtype: L{WORD}
@return: The L{WORD} obtained at the given offset. | f11791:c0:m30 |
def getQwordAtRva(self, rva): | return datatypes.QWORD.parse(utils.ReadData(self.getDataAtRva(rva, <NUM_LIT:8>)))<EOL> | Returns a C{QWORD} from a given RVA.
@type rva: int
@param rva: The RVA to get the C{QWORD} from.
@rtype: L{QWORD}
@return: The L{QWORD} obtained at the given RVA. | f11791:c0:m31 |
def getQwordAtOffset(self, offset): | return datatypes.QWORD.parse(utils.ReadData(self.getDataAtOffset(offset, <NUM_LIT:8>)))<EOL> | Returns a C{QWORD} from a given offset.
@type offset: int
@param offset: The offset to get the C{QWORD} from.
@rtype: L{QWORD}
@return: The L{QWORD} obtained at the given offset. | f11791:c0:m32 |
def getDataAtRva(self, rva, size): | return self.getDataAtOffset(self.getOffsetFromRva(rva), size)<EOL> | Gets binary data at a given RVA.
@type rva: int
@param rva: The RVA to get the data from.
@type size: int
@param size: The size of the data to be obtained.
@rtype: str
@return: The data obtained at the given RVA. | f11791:c0:m33 |
def getDataAtOffset(self, offset, size): | data = str(self)<EOL>return data[offset:offset+size]<EOL> | Gets binary data at a given offset.
@type offset: int
@param offset: The offset to get the data from.
@type size: int
@param size: The size of the data to be obtained.
@rtype: str
@return: The data obtained at the given offset. | f11791:c0:m34 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.