blob: 716bebd19bbd7c1d9e4aab94f3ceb15d99d773c7 [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#-------------------------------------------------------------------------------
9from .exceptions import ELFParseError
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