1 #-------------------------------------------------------------------------------
2 # elftools: common/construct_utils.py
4 # Some complementary construct utilities
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from ..construct import Subconstruct, ConstructError, ArrayError
12 class RepeatUntilExcluding(Subconstruct):
13 """ A version of construct's RepeatUntil that doesn't include the last
14 element (which casued the repeat to exit) in the return value.
16 Only parsing is currently implemented.
18 P.S. removed some code duplication
20 __slots__ = ["predicate"]
21 def __init__(self, predicate, subcon):
22 Subconstruct.__init__(self, subcon)
23 self.predicate = predicate
24 self._clear_flag(self.FLAG_COPY_CONTEXT)
25 self._set_flag(self.FLAG_DYNAMIC)
26 def _parse(self, stream, context):
29 context_for_subcon = context
30 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
31 context_for_subcon = context.__copy__()
34 subobj = self.subcon._parse(stream, context_for_subcon)
35 if self.predicate(subobj, context):
38 except ConstructError as ex:
39 raise ArrayError("missing terminator", ex)
41 def _build(self, obj, stream, context):
42 raise NotImplementedError('no building')
43 def _sizeof(self, context):
44 raise SizeofError("can't calculate size")