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 | |
| 13 | def struct_parse(struct, stream, stream_pos=None): |
Eli Bendersky | 3f4de3e | 2011-09-14 05:58:06 +0300 | [diff] [blame] | 14 | """ Convenience function for using the given struct to parse a stream. |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 15 | 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] | 16 | the parsing is done. Otherwise, the current position of the stream is |
| 17 | used. |
| 18 | Wraps the error thrown by construct with ELFParseError. |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 19 | """ |
| 20 | try: |
| 21 | if stream_pos is not None: |
| 22 | stream.seek(stream_pos) |
| 23 | return struct.parse_stream(stream) |
| 24 | except ConstructError as e: |
| 25 | raise ELFParseError(e.message) |
| 26 | |
| 27 | |
eliben | 116899e | 2011-09-08 17:15:53 +0300 | [diff] [blame] | 28 | def elf_assert(cond, msg=''): |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 29 | """ Assert that cond is True, otherwise raise ELFError(msg) |
| 30 | """ |
eliben | 4455651 | 2011-09-19 12:54:32 +0300 | [diff] [blame] | 31 | _assert_with_exception(cond, msg, ELFError) |
Eli Bendersky | e0735d5 | 2011-09-08 20:12:44 +0300 | [diff] [blame] | 32 | |
eliben | 4455651 | 2011-09-19 12:54:32 +0300 | [diff] [blame] | 33 | |
| 34 | def dwarf_assert(cond, msg=''): |
| 35 | """ Assert that cond is True, otherwise raise DWARFError(msg) |
| 36 | """ |
| 37 | _assert_with_exception(cond, msg, DWARFError) |
| 38 | |
| 39 | |
| 40 | def _assert_with_exception(cond, msg, exception_type): |
| 41 | if not cond: |
eliben | 033b44f | 2011-09-19 15:48:39 +0300 | [diff] [blame] | 42 | raise exception_type(msg) |
| 43 | |
eliben | 3b9ad82 | 2011-09-22 11:46:26 +0300 | [diff] [blame] | 44 | |
| 45 | from contextlib import contextmanager |
| 46 | |
| 47 | @contextmanager |
| 48 | def preserve_stream_pos(stream): |
| 49 | """ Usage: |
| 50 | |
| 51 | # stream has some position FOO (return value of stream.tell()) |
| 52 | with preserve_stream_pos(stream): |
| 53 | # do stuff that manipulates the stream |
| 54 | # stream still has position FOO |
| 55 | """ |
| 56 | saved_pos = stream.tell() |
| 57 | yield |
| 58 | stream.seek(saved_pos) |