blob: 666176f6b8c71d13c6b7eccb89f4b6535f065d5e [file] [log] [blame]
Eli Benderskye0735d52011-09-08 20:12:44 +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#-------------------------------------------------------------------------------
9from .exceptions import ELFParseError, ELFError
10
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
26
eliben116899e2011-09-08 17:15:53 +030027def elf_assert(cond, msg=''):
Eli Benderskye0735d52011-09-08 20:12:44 +030028 """ Assert that cond is True, otherwise raise ELFError(msg)
29 """
30 if not cond:
31 raise ELFError(msg)
32