eliben | 75b5fde | 2011-09-08 16:24:49 +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 | 116899e | 2011-09-08 17:15:53 +0300 | [diff] [blame^] | 9 | from .exceptions import ELFParseError, ELFError
|
eliben | 75b5fde | 2011-09-08 16:24:49 +0300 | [diff] [blame] | 10 |
|
| 11 |
|
| 12 | def struct_parse(struct, stream, stream_pos=None):
|
| 13 | """ Convenience function for using the given struct to parse a stream (at
|
| 14 | its current location).
|
| 15 | If stream_pos is provided, the stream is seeked to this position before
|
| 16 | the parsing is done.
|
| 17 | Wraps the error thrown by construct with our own error.
|
| 18 | """
|
| 19 | try:
|
| 20 | if stream_pos is not None:
|
| 21 | stream.seek(stream_pos)
|
| 22 | return struct.parse_stream(stream)
|
| 23 | except ConstructError as e:
|
| 24 | raise ELFParseError(e.message)
|
| 25 |
|
eliben | 116899e | 2011-09-08 17:15:53 +0300 | [diff] [blame^] | 26 |
|
| 27 | def elf_assert(cond, msg=''): |
| 28 | """ Assert that cond is True, otherwise raise ELFError(msg)
|
| 29 | """
|
| 30 | if not cond:
|
| 31 | raise ELFError(msg)
|
| 32 |
|