Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # elftools: common/utils.py |
| 3 | # |
| 4 | # Miscellaneous utilities for elftools |
| 5 | # |
| 6 | # Eli Bendersky (eliben@gmail.com) |
| 7 | # This code is in the public domain |
| 8 | #------------------------------------------------------------------------------- |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 9 | from .exceptions import ELFParseError, ELFError, DWARFError |
eliben | a7c2547 | 2011-09-18 17:31:10 +0300 | [diff] [blame] | 10 | from ..construct import ConstructError |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 11 | |
| 12 | |
Eli Bendersky | ebe5116 | 2011-10-27 17:34:02 +0200 | [diff] [blame] | 13 | def bytelist2string(bytelist): |
| 14 | """ Convert a list of byte values (e.g. [0x10 0x20 0x00]) to a string |
| 15 | (e.g. '\x10\x20\x00'). |
| 16 | """ |
| 17 | return ''.join(chr(b) for b in bytelist) |
| 18 | |
| 19 | |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 20 | def struct_parse(struct, stream, stream_pos=None): |
Eli Bendersky | 3f4de3e | 2011-09-14 05:58:06 +0300 | [diff] [blame] | 21 | """ Convenience function for using the given struct to parse a stream. |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 22 | If stream_pos is provided, the stream is seeked to this position before |
Eli Bendersky | 3f4de3e | 2011-09-14 05:58:06 +0300 | [diff] [blame] | 23 | the parsing is done. Otherwise, the current position of the stream is |
| 24 | used. |
| 25 | Wraps the error thrown by construct with ELFParseError. |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 26 | """ |
| 27 | try: |
| 28 | if stream_pos is not None: |
| 29 | stream.seek(stream_pos) |
| 30 | return struct.parse_stream(stream) |
| 31 | except ConstructError as e: |
| 32 | raise ELFParseError(e.message) |
| 33 | |
| 34 | |
Eli Bendersky | 4a48975 | 2011-11-27 06:34:47 +0200 | [diff] [blame] | 35 | def parse_cstring_from_stream(stream, stream_pos=None): |
| 36 | """ Parse a C-string from the given stream. The string is returned without |
Eli Bendersky | a1d6140 | 2011-11-28 06:25:52 +0200 | [diff] [blame^] | 37 | the terminating \x00 byte. If the terminating byte wasn't found, None |
| 38 | is returned (the stream is exhausted). |
Eli Bendersky | 4a48975 | 2011-11-27 06:34:47 +0200 | [diff] [blame] | 39 | If stream_pos is provided, the stream is seeked to this position before |
| 40 | the parsing is done. Otherwise, the current position of the stream is |
| 41 | used. |
| 42 | """ |
Eli Bendersky | 4a48975 | 2011-11-27 06:34:47 +0200 | [diff] [blame] | 43 | if stream_pos is not None: |
| 44 | stream.seek(stream_pos) |
Eli Bendersky | a1d6140 | 2011-11-28 06:25:52 +0200 | [diff] [blame^] | 45 | CHUNKSIZE = 64 |
| 46 | chunks = [] |
| 47 | found = False |
| 48 | while True: |
| 49 | chunk = stream.read(CHUNKSIZE) |
| 50 | end_index = chunk.find('\x00') |
| 51 | if end_index >= 0: |
| 52 | chunks.append(chunk[:end_index]) |
| 53 | found = True |
| 54 | break |
| 55 | else: |
| 56 | chunks.append(chunk) |
| 57 | if len(chunk) < CHUNKSIZE: |
| 58 | break |
| 59 | return ''.join(chunks) if found else None |
Eli Bendersky | 4a48975 | 2011-11-27 06:34:47 +0200 | [diff] [blame] | 60 | |
| 61 | |
eliben | 116899e | 2011-09-08 17:15:53 +0300 | [diff] [blame] | 62 | def elf_assert(cond, msg=''): |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 63 | """ Assert that cond is True, otherwise raise ELFError(msg) |
| 64 | """ |
eliben | 4455651 | 2011-09-19 12:54:32 +0300 | [diff] [blame] | 65 | _assert_with_exception(cond, msg, ELFError) |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 66 | |
eliben | 4455651 | 2011-09-19 12:54:32 +0300 | [diff] [blame] | 67 | |
| 68 | def dwarf_assert(cond, msg=''): |
| 69 | """ Assert that cond is True, otherwise raise DWARFError(msg) |
| 70 | """ |
| 71 | _assert_with_exception(cond, msg, DWARFError) |
| 72 | |
| 73 | |
| 74 | def _assert_with_exception(cond, msg, exception_type): |
| 75 | if not cond: |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 76 | raise exception_type(msg) |
| 77 | |
eliben | 3b9ad82 | 2011-09-22 11:46:26 +0300 | [diff] [blame] | 78 | |
| 79 | from contextlib import contextmanager |
| 80 | |
| 81 | @contextmanager |
| 82 | def preserve_stream_pos(stream): |
| 83 | """ Usage: |
| 84 | |
| 85 | # stream has some position FOO (return value of stream.tell()) |
| 86 | with preserve_stream_pos(stream): |
| 87 | # do stuff that manipulates the stream |
| 88 | # stream still has position FOO |
| 89 | """ |
| 90 | saved_pos = stream.tell() |
| 91 | yield |
| 92 | stream.seek(saved_pos) |
Eli Bendersky | a1d6140 | 2011-11-28 06:25:52 +0200 | [diff] [blame^] | 93 | |