blob: 2daed04c51cde36a9f0185312d75c5b602f72b4c [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#-------------------------------------------------------------------------------
Eli Benderskyc2074312011-12-17 14:48:42 +02009from contextlib import contextmanager
eliben033b44f2011-09-19 15:48:39 +030010from .exceptions import ELFParseError, ELFError, DWARFError
Eli Bendersky79271e92012-01-27 10:25:47 +020011from .py3compat import int2byte
elibena7c25472011-09-18 17:31:10 +030012from ..construct import ConstructError
Eli Benderskye0735d52011-09-08 20:12:44 +030013
14
Eli Benderskyebe51162011-10-27 17:34:02 +020015def bytelist2string(bytelist):
Eli Bendersky79271e92012-01-27 10:25:47 +020016 """ Convert a list of byte values (e.g. [0x10 0x20 0x00]) to a bytes object
17 (e.g. b'\x10\x20\x00').
Eli Benderskyebe51162011-10-27 17:34:02 +020018 """
Eli Bendersky79271e92012-01-27 10:25:47 +020019 return b''.join(int2byte(b) for b in bytelist)
Eli Benderskyebe51162011-10-27 17:34:02 +020020
21
Eli Benderskye0735d52011-09-08 20:12:44 +030022def struct_parse(struct, stream, stream_pos=None):
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030023 """ Convenience function for using the given struct to parse a stream.
Eli Benderskye0735d52011-09-08 20:12:44 +030024 If stream_pos is provided, the stream is seeked to this position before
Eli Bendersky3f4de3e2011-09-14 05:58:06 +030025 the parsing is done. Otherwise, the current position of the stream is
26 used.
27 Wraps the error thrown by construct with ELFParseError.
Eli Benderskye0735d52011-09-08 20:12:44 +030028 """
29 try:
30 if stream_pos is not None:
31 stream.seek(stream_pos)
32 return struct.parse_stream(stream)
33 except ConstructError as e:
34 raise ELFParseError(e.message)
35
36
Eli Bendersky4a489752011-11-27 06:34:47 +020037def parse_cstring_from_stream(stream, stream_pos=None):
38 """ Parse a C-string from the given stream. The string is returned without
Eli Benderskya1d61402011-11-28 06:25:52 +020039 the terminating \x00 byte. If the terminating byte wasn't found, None
40 is returned (the stream is exhausted).
Eli Bendersky4a489752011-11-27 06:34:47 +020041 If stream_pos is provided, the stream is seeked to this position before
42 the parsing is done. Otherwise, the current position of the stream is
43 used.
44 """
Eli Bendersky4a489752011-11-27 06:34:47 +020045 if stream_pos is not None:
46 stream.seek(stream_pos)
Eli Benderskya1d61402011-11-28 06:25:52 +020047 CHUNKSIZE = 64
48 chunks = []
49 found = False
50 while True:
51 chunk = stream.read(CHUNKSIZE)
Eli Bendersky79271e92012-01-27 10:25:47 +020052 end_index = chunk.find(b'\x00')
Eli Benderskya1d61402011-11-28 06:25:52 +020053 if end_index >= 0:
54 chunks.append(chunk[:end_index])
55 found = True
56 break
57 else:
58 chunks.append(chunk)
59 if len(chunk) < CHUNKSIZE:
60 break
Eli Bendersky79271e92012-01-27 10:25:47 +020061 return b''.join(chunks) if found else None
Eli Bendersky4a489752011-11-27 06:34:47 +020062
63
eliben116899e2011-09-08 17:15:53 +030064def elf_assert(cond, msg=''):
Eli Benderskye0735d52011-09-08 20:12:44 +030065 """ Assert that cond is True, otherwise raise ELFError(msg)
66 """
eliben44556512011-09-19 12:54:32 +030067 _assert_with_exception(cond, msg, ELFError)
Eli Benderskye0735d52011-09-08 20:12:44 +030068
eliben44556512011-09-19 12:54:32 +030069
70def dwarf_assert(cond, msg=''):
71 """ Assert that cond is True, otherwise raise DWARFError(msg)
72 """
73 _assert_with_exception(cond, msg, DWARFError)
74
75
eliben3b9ad822011-09-22 11:46:26 +030076@contextmanager
77def preserve_stream_pos(stream):
78 """ Usage:
79
80 # stream has some position FOO (return value of stream.tell())
81 with preserve_stream_pos(stream):
82 # do stuff that manipulates the stream
83 # stream still has position FOO
84 """
85 saved_pos = stream.tell()
86 yield
87 stream.seek(saved_pos)
Eli Benderskya1d61402011-11-28 06:25:52 +020088
Eli Benderskyc2074312011-12-17 14:48:42 +020089
90#------------------------- PRIVATE -------------------------
91
92def _assert_with_exception(cond, msg, exception_type):
93 if not cond:
94 raise exception_type(msg)
95