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