blob: 230ba96c387248016835091fc425825ebb8a1037 [file] [log] [blame]
eliben75b5fde2011-09-08 16:24:49 +03001#-------------------------------------------------------------------------------
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#-------------------------------------------------------------------------------
eliben116899e2011-09-08 17:15:53 +03009from .exceptions import ELFParseError, ELFError
eliben75b5fde2011-09-08 16:24:49 +030010
11
12def 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
eliben116899e2011-09-08 17:15:53 +030026
27def elf_assert(cond, msg=''):
28 """ Assert that cond is True, otherwise raise ELFError(msg)
29 """
30 if not cond:
31 raise ELFError(msg)
32