blob: 477d7ee52905f7640f8bf734d503412ac81b29dd [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):
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030013 """ Convenience function for using the given struct to parse a stream.
Eli Benderskye0735d52011-09-08 20:12:44 +030014 If stream_pos is provided, the stream is seeked to this position before
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030015 the parsing is done. Otherwise, the current position of the stream is
16 used.
17 Wraps the error thrown by construct with ELFParseError.
Eli Benderskye0735d52011-09-08 20:12:44 +030018 """
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