Write the PE file. This function will process all headers and components of the PE file and include all changes made (by just assigning to attributes in the PE objects) and write the changes back to a file whose name is provided as an argument. The filename i
(self, filename=None)
| 3313 | self.RICH_HEADER = None |
| 3314 | |
| 3315 | def write(self, filename=None): |
| 3316 | """Write the PE file. |
| 3317 | |
| 3318 | This function will process all headers and components |
| 3319 | of the PE file and include all changes made (by just |
| 3320 | assigning to attributes in the PE objects) and write |
| 3321 | the changes back to a file whose name is provided as |
| 3322 | an argument. The filename is optional, if not |
| 3323 | provided the data will be returned as a 'str' object. |
| 3324 | """ |
| 3325 | |
| 3326 | file_data = bytearray(self.__data__) |
| 3327 | |
| 3328 | for structure in self.__structures__: |
| 3329 | struct_data = bytearray(structure.__pack__()) |
| 3330 | offset = structure.get_file_offset() |
| 3331 | file_data[offset : offset + len(struct_data)] = struct_data |
| 3332 | |
| 3333 | if hasattr(self, "VS_VERSIONINFO"): |
| 3334 | if hasattr(self, "FileInfo"): |
| 3335 | for finfo in self.FileInfo: |
| 3336 | for entry in finfo: |
| 3337 | if hasattr(entry, "StringTable"): |
| 3338 | for st_entry in entry.StringTable: |
| 3339 | for key, entry in list(st_entry.entries.items()): |
| 3340 | |
| 3341 | # Offsets and lengths of the keys and values. |
| 3342 | # Each value in the dictionary is a tuple: |
| 3343 | # (key length, value length) |
| 3344 | # The lengths are in characters, not in bytes. |
| 3345 | offsets = st_entry.entries_offsets[key] |
| 3346 | lengths = st_entry.entries_lengths[key] |
| 3347 | |
| 3348 | if len(entry) > lengths[1]: |
| 3349 | l = entry.decode("utf-8").encode("utf-16le") |
| 3350 | file_data[ |
| 3351 | offsets[1] : offsets[1] + lengths[1] * 2 |
| 3352 | ] = l[: lengths[1] * 2] |
| 3353 | else: |
| 3354 | encoded_data = entry.decode("utf-8").encode( |
| 3355 | "utf-16le" |
| 3356 | ) |
| 3357 | file_data[ |
| 3358 | offsets[1] : offsets[1] + len(encoded_data) |
| 3359 | ] = encoded_data |
| 3360 | |
| 3361 | new_file_data = file_data |
| 3362 | if not filename: |
| 3363 | return new_file_data |
| 3364 | |
| 3365 | f = open(filename, "wb+") |
| 3366 | f.write(new_file_data) |
| 3367 | f.close() |
| 3368 | return |
| 3369 | |
| 3370 | def parse_sections(self, offset): |
| 3371 | """Fetch the PE file sections. |
no test coverage detected