raise ELFParseError(e.message)
+def parse_cstring_from_stream(stream, stream_pos=None):
+ """ Parse a C-string from the given stream. The string is returned without
+ the terminating \x00 byte.
+ If stream_pos is provided, the stream is seeked to this position before
+ the parsing is done. Otherwise, the current position of the stream is
+ used.
+ """
+ # I could've just used construct.CString, but this function is 4x faster.
+ # Since it's needed a lot, I created it as an optimization.
+ if stream_pos is not None:
+ stream.seek(stream_pos)
+ return ''.join(iter(lambda: stream.read(1), '\x00'))
+
+
def elf_assert(cond, msg=''):
""" Assert that cond is True, otherwise raise ELFError(msg)
"""
from ..construct import CString
from ..common.exceptions import DWARFError
-from ..common.utils import struct_parse, dwarf_assert
+from ..common.utils import (struct_parse, dwarf_assert,
+ parse_cstring_from_stream)
from .structs import DWARFStructs
from .compileunit import CompileUnit
from .abbrevtable import AbbrevTable
""" Obtain a string from the string table section, given an offset
relative to the section.
"""
- return struct_parse(
- CString(''),
- self.debug_str_sec.stream,
- stream_pos=offset)
+ return parse_cstring_from_stream(self.debug_str_sec.stream, offset)
#------ PRIVATE ------#