1 from struct import Struct as Packer
3 from .lib.py3compat import BytesIO, advance_iterator, bchr
4 from .lib import Container, ListContainer, LazyContainer
7 #===============================================================================
9 #===============================================================================
10 class ConstructError(Exception):
12 class FieldError(ConstructError):
14 class SizeofError(ConstructError):
16 class AdaptationError(ConstructError):
18 class ArrayError(ConstructError):
20 class RangeError(ConstructError):
22 class SwitchError(ConstructError):
24 class SelectError(ConstructError):
26 class TerminatorError(ConstructError):
29 #===============================================================================
31 #===============================================================================
32 class Construct(object):
34 The mother of all constructs.
36 This object is generally not directly instantiated, and it does not
37 directly implement parsing and building, so it is largely only of interest
38 to subclass implementors.
40 The external user API:
48 Subclass authors should not override the external methods. Instead,
49 another API is available:
55 There is also a flag API:
67 Attributes and Inheritance
68 ==========================
70 All constructs have a name and flags. The name is used for naming struct
71 members and context dictionaries. Note that the name can either be a
72 string, or None if the name is not needed. A single underscore ("_") is a
73 reserved name, and so are names starting with a less-than character ("<").
74 The name should be descriptive, short, and valid as a Python identifier,
75 although these rules are not enforced.
77 The flags specify additional behavioral information about this construct.
78 Flags are used by enclosing constructs to determine a proper course of
79 action. Flags are inherited by default, from inner subconstructs to outer
80 constructs. The enclosing construct may set new flags or clear existing
83 For example, if FLAG_COPY_CONTEXT is set, repeaters will pass a copy of
84 the context for each iteration, which is necessary for OnDemand parsing.
87 FLAG_COPY_CONTEXT = 0x0001
92 __slots__ = ["name", "conflags"]
93 def __init__(self, name, flags = 0):
95 if type(name) is not str:
96 raise TypeError("name must be a string or None", name)
97 if name == "_" or name.startswith("<"):
98 raise ValueError("reserved name", name)
100 self.conflags = flags
103 return "%s(%r)" % (self.__class__.__name__, self.name)
105 def _set_flag(self, flag):
107 Set the given flag or flags.
109 :param int flag: flag to set; may be OR'd combination of flags
112 self.conflags |= flag
114 def _clear_flag(self, flag):
116 Clear the given flag or flags.
118 :param int flag: flag to clear; may be OR'd combination of flags
121 self.conflags &= ~flag
123 def _inherit_flags(self, *subcons):
125 Pull flags from subconstructs.
129 self._set_flag(sc.conflags)
131 def _is_flag(self, flag):
133 Check whether a given flag is set.
135 :param int flag: flag to check
138 return bool(self.conflags & flag)
140 def __getstate__(self):
142 Obtain a dictionary representing this construct's state.
146 if hasattr(self, "__dict__"):
147 attrs.update(self.__dict__)
151 if hasattr(c, "__slots__"):
152 slots.extend(c.__slots__)
155 if hasattr(self, name):
156 attrs[name] = getattr(self, name)
159 def __setstate__(self, attrs):
161 Set this construct's state to a given state.
163 for name, value in attrs.items():
164 setattr(self, name, value)
167 """returns a copy of this construct"""
168 self2 = object.__new__(self.__class__)
169 self2.__setstate__(self.__getstate__())
172 def parse(self, data):
174 Parse an in-memory buffer.
176 Strings, buffers, memoryviews, and other complete buffers can be
177 parsed with this method.
180 return self.parse_stream(BytesIO(data))
182 def parse_stream(self, stream):
186 Files, pipes, sockets, and other streaming sources of data are handled
190 return self._parse(stream, Container())
192 def _parse(self, stream, context):
194 Override me in your subclass.
197 raise NotImplementedError()
199 def build(self, obj):
201 Build an object in memory.
204 self.build_stream(obj, stream)
205 return stream.getvalue()
207 def build_stream(self, obj, stream):
209 Build an object directly into a stream.
211 self._build(obj, stream, Container())
213 def _build(self, obj, stream, context):
215 Override me in your subclass.
218 raise NotImplementedError()
220 def sizeof(self, context=None):
222 Calculate the size of this object, optionally using a context.
224 Some constructs have no fixed size and can only know their size for a
225 given hunk of data; these constructs will raise an error if they are
226 not passed a context.
228 :param ``Container`` context: contextual data
230 :returns: int of the length of this construct
231 :raises SizeofError: the size could not be determined
235 context = Container()
237 return self._sizeof(context)
238 except Exception as e:
241 def _sizeof(self, context):
243 Override me in your subclass.
246 raise SizeofError("Raw Constructs have no size!")
248 class Subconstruct(Construct):
250 Abstract subconstruct (wraps an inner construct, inheriting its
254 * subcon - the construct to wrap
256 __slots__ = ["subcon"]
257 def __init__(self, subcon):
258 Construct.__init__(self, subcon.name, subcon.conflags)
260 def _parse(self, stream, context):
261 return self.subcon._parse(stream, context)
262 def _build(self, obj, stream, context):
263 self.subcon._build(obj, stream, context)
264 def _sizeof(self, context):
265 return self.subcon._sizeof(context)
267 class Adapter(Subconstruct):
269 Abstract adapter: calls _decode for parsing and _encode for building.
272 * subcon - the construct to wrap
275 def _parse(self, stream, context):
276 return self._decode(self.subcon._parse(stream, context), context)
277 def _build(self, obj, stream, context):
278 self.subcon._build(self._encode(obj, context), stream, context)
279 def _decode(self, obj, context):
280 raise NotImplementedError()
281 def _encode(self, obj, context):
282 raise NotImplementedError()
285 #===============================================================================
287 #===============================================================================
288 def _read_stream(stream, length):
290 raise ValueError("length must be >= 0", length)
291 data = stream.read(length)
292 if len(data) != length:
293 raise FieldError("expected %d, found %d" % (length, len(data)))
296 def _write_stream(stream, length, data):
298 raise ValueError("length must be >= 0", length)
299 if len(data) != length:
300 raise FieldError("expected %d, found %d" % (length, len(data)))
303 class StaticField(Construct):
305 A fixed-size byte field.
307 :param str name: field name
308 :param int length: number of bytes in the field
311 __slots__ = ["length"]
312 def __init__(self, name, length):
313 Construct.__init__(self, name)
315 def _parse(self, stream, context):
316 return _read_stream(stream, self.length)
317 def _build(self, obj, stream, context):
318 _write_stream(stream, self.length, obj)
319 def _sizeof(self, context):
322 class FormatField(StaticField):
324 A field that uses ``struct`` to pack and unpack data.
326 See ``struct`` documentation for instructions on crafting format strings.
328 :param str name: name of the field
329 :param str endianness: format endianness string; one of "<", ">", or "="
330 :param str format: a single format character
333 __slots__ = ["packer"]
334 def __init__(self, name, endianity, format):
335 if endianity not in (">", "<", "="):
336 raise ValueError("endianity must be be '=', '<', or '>'",
339 raise ValueError("must specify one and only one format char")
340 self.packer = Packer(endianity + format)
341 StaticField.__init__(self, name, self.packer.size)
342 def __getstate__(self):
343 attrs = StaticField.__getstate__(self)
344 attrs["packer"] = attrs["packer"].format
346 def __setstate__(self, attrs):
347 attrs["packer"] = Packer(attrs["packer"])
348 return StaticField.__setstate__(attrs)
349 def _parse(self, stream, context):
351 return self.packer.unpack(_read_stream(stream, self.length))[0]
352 except Exception as ex:
354 def _build(self, obj, stream, context):
356 _write_stream(stream, self.length, self.packer.pack(obj))
357 except Exception as ex:
360 class MetaField(Construct):
362 A variable-length field. The length is obtained at runtime from a
365 :param str name: name of the field
366 :param callable lengthfunc: callable that takes a context and returns
369 >>> foo = Struct("foo",
371 ... MetaField("data", lambda ctx: ctx["length"])
373 >>> foo.parse("\\x03ABC")
374 Container(data = 'ABC', length = 3)
375 >>> foo.parse("\\x04ABCD")
376 Container(data = 'ABCD', length = 4)
379 __slots__ = ["lengthfunc"]
380 def __init__(self, name, lengthfunc):
381 Construct.__init__(self, name)
382 self.lengthfunc = lengthfunc
383 self._set_flag(self.FLAG_DYNAMIC)
384 def _parse(self, stream, context):
385 return _read_stream(stream, self.lengthfunc(context))
386 def _build(self, obj, stream, context):
387 _write_stream(stream, self.lengthfunc(context), obj)
388 def _sizeof(self, context):
389 return self.lengthfunc(context)
392 #===============================================================================
393 # arrays and repeaters
394 #===============================================================================
395 class MetaArray(Subconstruct):
397 An array (repeater) of a meta-count. The array will iterate exactly
398 `countfunc()` times. Will raise ArrayError if less elements are found.
399 See also Array, Range and RepeatUntil.
402 * countfunc - a function that takes the context as a parameter and returns
403 the number of elements of the array (count)
404 * subcon - the subcon to repeat `countfunc()` times
407 MetaArray(lambda ctx: 5, UBInt8("foo"))
409 __slots__ = ["countfunc"]
410 def __init__(self, countfunc, subcon):
411 Subconstruct.__init__(self, subcon)
412 self.countfunc = countfunc
413 self._clear_flag(self.FLAG_COPY_CONTEXT)
414 self._set_flag(self.FLAG_DYNAMIC)
415 def _parse(self, stream, context):
416 obj = ListContainer()
418 count = self.countfunc(context)
420 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
422 obj.append(self.subcon._parse(stream, context.__copy__()))
426 obj.append(self.subcon._parse(stream, context))
428 except ConstructError as ex:
429 raise ArrayError("expected %d, found %d" % (count, c), ex)
431 def _build(self, obj, stream, context):
432 count = self.countfunc(context)
433 if len(obj) != count:
434 raise ArrayError("expected %d, found %d" % (count, len(obj)))
435 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
437 self.subcon._build(subobj, stream, context.__copy__())
440 self.subcon._build(subobj, stream, context)
441 def _sizeof(self, context):
442 return self.subcon._sizeof(context) * self.countfunc(context)
444 class Range(Subconstruct):
446 A range-array. The subcon will iterate between `mincount` to `maxcount`
447 times. If less than `mincount` elements are found, raises RangeError.
448 See also GreedyRange and OptionalGreedyRange.
450 The general-case repeater. Repeats the given unit for at least mincount
451 times, and up to maxcount times. If an exception occurs (EOF, validation
452 error), the repeater exits. If less than mincount units have been
453 successfully parsed, a RangeError is raised.
456 This object requires a seekable stream for parsing.
458 :param int mincount: the minimal count
459 :param int maxcount: the maximal count
460 :param Construct subcon: the subcon to repeat
462 >>> c = Range(3, 7, UBInt8("foo"))
463 >>> c.parse("\\x01\\x02")
464 Traceback (most recent call last):
466 construct.core.RangeError: expected 3..7, found 2
467 >>> c.parse("\\x01\\x02\\x03")
469 >>> c.parse("\\x01\\x02\\x03\\x04\\x05\\x06")
471 >>> c.parse("\\x01\\x02\\x03\\x04\\x05\\x06\\x07")
472 [1, 2, 3, 4, 5, 6, 7]
473 >>> c.parse("\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09")
474 [1, 2, 3, 4, 5, 6, 7]
476 Traceback (most recent call last):
478 construct.core.RangeError: expected 3..7, found 2
479 >>> c.build([1,2,3,4])
480 '\\x01\\x02\\x03\\x04'
481 >>> c.build([1,2,3,4,5,6,7,8])
482 Traceback (most recent call last):
484 construct.core.RangeError: expected 3..7, found 8
487 __slots__ = ["mincount", "maxcout"]
488 def __init__(self, mincount, maxcout, subcon):
489 Subconstruct.__init__(self, subcon)
490 self.mincount = mincount
491 self.maxcout = maxcout
492 self._clear_flag(self.FLAG_COPY_CONTEXT)
493 self._set_flag(self.FLAG_DYNAMIC)
494 def _parse(self, stream, context):
495 obj = ListContainer()
498 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
499 while c < self.maxcout:
501 obj.append(self.subcon._parse(stream, context.__copy__()))
504 while c < self.maxcout:
506 obj.append(self.subcon._parse(stream, context))
508 except ConstructError as ex:
509 if c < self.mincount:
510 raise RangeError("expected %d to %d, found %d" %
511 (self.mincount, self.maxcout, c), ex)
514 def _build(self, obj, stream, context):
515 if len(obj) < self.mincount or len(obj) > self.maxcout:
516 raise RangeError("expected %d to %d, found %d" %
517 (self.mincount, self.maxcout, len(obj)))
520 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
522 if isinstance(obj, bytes):
523 subobj = bchr(subobj)
524 self.subcon._build(subobj, stream, context.__copy__())
528 if isinstance(obj, bytes):
529 subobj = bchr(subobj)
530 self.subcon._build(subobj, stream, context)
532 except ConstructError as ex:
533 if cnt < self.mincount:
534 raise RangeError("expected %d to %d, found %d" %
535 (self.mincount, self.maxcout, len(obj)), ex)
536 def _sizeof(self, context):
537 raise SizeofError("can't calculate size")
539 class RepeatUntil(Subconstruct):
541 An array that repeats until the predicate indicates it to stop. Note that
542 the last element (which caused the repeat to exit) is included in the
546 * predicate - a predicate function that takes (obj, context) and returns
547 True if the stop-condition is met, or False to continue.
548 * subcon - the subcon to repeat.
551 # will read chars until b\x00 (inclusive)
552 RepeatUntil(lambda obj, ctx: obj == b"\x00",
556 __slots__ = ["predicate"]
557 def __init__(self, predicate, subcon):
558 Subconstruct.__init__(self, subcon)
559 self.predicate = predicate
560 self._clear_flag(self.FLAG_COPY_CONTEXT)
561 self._set_flag(self.FLAG_DYNAMIC)
562 def _parse(self, stream, context):
565 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
567 subobj = self.subcon._parse(stream, context.__copy__())
569 if self.predicate(subobj, context):
573 subobj = self.subcon._parse(stream, context)
575 if self.predicate(subobj, context):
577 except ConstructError as ex:
578 raise ArrayError("missing terminator", ex)
580 def _build(self, obj, stream, context):
582 if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
584 self.subcon._build(subobj, stream, context.__copy__())
585 if self.predicate(subobj, context):
590 subobj = bchr(subobj)
591 self.subcon._build(subobj, stream, context.__copy__())
592 if self.predicate(subobj, context):
596 raise ArrayError("missing terminator")
597 def _sizeof(self, context):
598 raise SizeofError("can't calculate size")
601 #===============================================================================
602 # structures and sequences
603 #===============================================================================
604 class Struct(Construct):
606 A sequence of named constructs, similar to structs in C. The elements are
607 parsed and built in the order they are defined.
611 * name - the name of the structure
612 * subcons - a sequence of subconstructs that make up this structure.
613 * nested - a keyword-only argument that indicates whether this struct
614 creates a nested context. The default is True. This parameter is
615 considered "advanced usage", and may be removed in the future.
619 UBInt8("first_element"),
620 UBInt16("second_element"),
622 UBInt8("third_element"),
625 __slots__ = ["subcons", "nested"]
626 def __init__(self, name, *subcons, **kw):
627 self.nested = kw.pop("nested", True)
629 raise TypeError("the only keyword argument accepted is 'nested'", kw)
630 Construct.__init__(self, name)
631 self.subcons = subcons
632 self._inherit_flags(*subcons)
633 self._clear_flag(self.FLAG_EMBED)
634 def _parse(self, stream, context):
635 if "<obj>" in context:
636 obj = context["<obj>"]
641 context = Container(_ = context)
642 for sc in self.subcons:
643 if sc.conflags & self.FLAG_EMBED:
644 context["<obj>"] = obj
645 sc._parse(stream, context)
647 subobj = sc._parse(stream, context)
648 if sc.name is not None:
649 obj[sc.name] = subobj
650 context[sc.name] = subobj
652 def _build(self, obj, stream, context):
653 if "<unnested>" in context:
654 del context["<unnested>"]
656 context = Container(_ = context)
657 for sc in self.subcons:
658 if sc.conflags & self.FLAG_EMBED:
659 context["<unnested>"] = True
661 elif sc.name is None:
664 subobj = getattr(obj, sc.name)
665 context[sc.name] = subobj
666 sc._build(subobj, stream, context)
667 def _sizeof(self, context):
669 context = Container(_ = context)
670 return sum(sc._sizeof(context) for sc in self.subcons)
672 class Sequence(Struct):
674 A sequence of unnamed constructs. The elements are parsed and built in the
675 order they are defined.
679 * name - the name of the structure
680 * subcons - a sequence of subconstructs that make up this structure.
681 * nested - a keyword-only argument that indicates whether this struct
682 creates a nested context. The default is True. This parameter is
683 considered "advanced usage", and may be removed in the future.
687 UBInt8("first_element"),
688 UBInt16("second_element"),
690 UBInt8("third_element"),
694 def _parse(self, stream, context):
695 if "<obj>" in context:
696 obj = context["<obj>"]
699 obj = ListContainer()
701 context = Container(_ = context)
702 for sc in self.subcons:
703 if sc.conflags & self.FLAG_EMBED:
704 context["<obj>"] = obj
705 sc._parse(stream, context)
707 subobj = sc._parse(stream, context)
708 if sc.name is not None:
710 context[sc.name] = subobj
712 def _build(self, obj, stream, context):
713 if "<unnested>" in context:
714 del context["<unnested>"]
716 context = Container(_ = context)
718 for sc in self.subcons:
719 if sc.conflags & self.FLAG_EMBED:
720 context["<unnested>"] = True
722 elif sc.name is None:
725 subobj = advance_iterator(objiter)
726 context[sc.name] = subobj
727 sc._build(subobj, stream, context)
729 class Union(Construct):
731 a set of overlapping fields (like unions in C). when parsing,
732 all fields read the same data; when building, only the first subcon
733 (called "master") is used.
736 * name - the name of the union
737 * master - the master subcon, i.e., the subcon used for building and
738 calculating the total size
739 * subcons - additional subcons
742 Union("what_are_four_bytes",
743 UBInt32("one_dword"),
744 Struct("two_words", UBInt16("first"), UBInt16("second")),
753 __slots__ = ["parser", "builder"]
754 def __init__(self, name, master, *subcons, **kw):
755 Construct.__init__(self, name)
756 args = [Peek(sc) for sc in subcons]
757 args.append(MetaField(None, lambda ctx: master._sizeof(ctx)))
758 self.parser = Struct(name, Peek(master, perform_build = True), *args)
759 self.builder = Struct(name, master)
760 def _parse(self, stream, context):
761 return self.parser._parse(stream, context)
762 def _build(self, obj, stream, context):
763 return self.builder._build(obj, stream, context)
764 def _sizeof(self, context):
765 return self.builder._sizeof(context)
767 #===============================================================================
769 #===============================================================================
770 class Switch(Construct):
772 A conditional branch. Switch will choose the case to follow based on
773 the return value of keyfunc. If no case is matched, and no default value
774 is given, SwitchError will be raised.
778 * name - the name of the construct
779 * keyfunc - a function that takes the context and returns a key, which
780 will ne used to choose the relevant case.
781 * cases - a dictionary mapping keys to constructs. the keys can be any
782 values that may be returned by keyfunc.
783 * default - a default value to use when the key is not found in the cases.
784 if not supplied, an exception will be raised when the key is not found.
785 You can use the builtin construct Pass for 'do-nothing'.
786 * include_key - whether or not to include the key in the return value
787 of parsing. defualt is False.
792 Switch("value", lambda ctx: ctx.type, {
802 class NoDefault(Construct):
803 def _parse(self, stream, context):
804 raise SwitchError("no default case defined")
805 def _build(self, obj, stream, context):
806 raise SwitchError("no default case defined")
807 def _sizeof(self, context):
808 raise SwitchError("no default case defined")
809 NoDefault = NoDefault("No default value specified")
811 __slots__ = ["subcons", "keyfunc", "cases", "default", "include_key"]
813 def __init__(self, name, keyfunc, cases, default = NoDefault,
814 include_key = False):
815 Construct.__init__(self, name)
816 self._inherit_flags(*cases.values())
817 self.keyfunc = keyfunc
819 self.default = default
820 self.include_key = include_key
821 self._inherit_flags(*cases.values())
822 self._set_flag(self.FLAG_DYNAMIC)
823 def _parse(self, stream, context):
824 key = self.keyfunc(context)
825 obj = self.cases.get(key, self.default)._parse(stream, context)
830 def _build(self, obj, stream, context):
834 key = self.keyfunc(context)
835 case = self.cases.get(key, self.default)
836 case._build(obj, stream, context)
837 def _sizeof(self, context):
838 case = self.cases.get(self.keyfunc(context), self.default)
839 return case._sizeof(context)
841 class Select(Construct):
843 Selects the first matching subconstruct. It will literally try each of
844 the subconstructs, until one matches.
847 * requires a seekable stream.
850 * name - the name of the construct
851 * subcons - the subcons to try (order-sensitive)
852 * include_name - a keyword only argument, indicating whether to include
853 the name of the selected subcon in the return value of parsing. default
864 __slots__ = ["subcons", "include_name"]
865 def __init__(self, name, *subcons, **kw):
866 include_name = kw.pop("include_name", False)
868 raise TypeError("the only keyword argument accepted "
869 "is 'include_name'", kw)
870 Construct.__init__(self, name)
871 self.subcons = subcons
872 self.include_name = include_name
873 self._inherit_flags(*subcons)
874 self._set_flag(self.FLAG_DYNAMIC)
875 def _parse(self, stream, context):
876 for sc in self.subcons:
878 context2 = context.__copy__()
880 obj = sc._parse(stream, context2)
881 except ConstructError:
884 context.__update__(context2)
885 if self.include_name:
889 raise SelectError("no subconstruct matched")
890 def _build(self, obj, stream, context):
891 if self.include_name:
893 for sc in self.subcons:
895 sc._build(obj, stream, context)
898 for sc in self.subcons:
900 context2 = context.__copy__()
902 sc._build(obj, stream2, context2)
906 context.__update__(context2)
907 stream.write(stream2.getvalue())
909 raise SelectError("no subconstruct matched", obj)
910 def _sizeof(self, context):
911 raise SizeofError("can't calculate size")
914 #===============================================================================
915 # stream manipulation
916 #===============================================================================
917 class Pointer(Subconstruct):
919 Changes the stream position to a given offset, where the construction
920 should take place, and restores the stream position when finished.
921 See also Anchor, OnDemand and OnDemandPointer.
924 * requires a seekable stream.
927 * offsetfunc: a function that takes the context and returns an absolute
928 stream position, where the construction would take place
929 * subcon - the subcon to use at `offsetfunc()`
933 UBInt32("spam_pointer"),
934 Pointer(lambda ctx: ctx.spam_pointer,
935 Array(5, UBInt8("spam"))
939 __slots__ = ["offsetfunc"]
940 def __init__(self, offsetfunc, subcon):
941 Subconstruct.__init__(self, subcon)
942 self.offsetfunc = offsetfunc
943 def _parse(self, stream, context):
944 newpos = self.offsetfunc(context)
945 origpos = stream.tell()
947 obj = self.subcon._parse(stream, context)
950 def _build(self, obj, stream, context):
951 newpos = self.offsetfunc(context)
952 origpos = stream.tell()
954 self.subcon._build(obj, stream, context)
956 def _sizeof(self, context):
959 class Peek(Subconstruct):
961 Peeks at the stream: parses without changing the stream position.
962 See also Union. If the end of the stream is reached when peeking,
966 * requires a seekable stream.
969 * subcon - the subcon to peek at
970 * perform_build - whether or not to perform building. by default this
971 parameter is set to False, meaning building is a no-op.
976 __slots__ = ["perform_build"]
977 def __init__(self, subcon, perform_build = False):
978 Subconstruct.__init__(self, subcon)
979 self.perform_build = perform_build
980 def _parse(self, stream, context):
983 return self.subcon._parse(stream, context)
988 def _build(self, obj, stream, context):
989 if self.perform_build:
990 self.subcon._build(obj, stream, context)
991 def _sizeof(self, context):
994 class OnDemand(Subconstruct):
996 Allows for on-demand (lazy) parsing. When parsing, it will return a
997 LazyContainer that represents a pointer to the data, but does not actually
998 parses it from stream until it's "demanded".
999 By accessing the 'value' property of LazyContainers, you will demand the
1000 data from the stream. The data will be parsed and cached for later use.
1001 You can use the 'has_value' property to know whether the data has already
1003 See also OnDemandPointer.
1006 * requires a seekable stream.
1010 * advance_stream - whether or not to advance the stream position. by
1011 default this is True, but if subcon is a pointer, this should be False.
1012 * force_build - whether or not to force build. If set to False, and the
1013 LazyContainer has not been demaned, building is a no-op.
1016 OnDemand(Array(10000, UBInt8("foo"))
1018 __slots__ = ["advance_stream", "force_build"]
1019 def __init__(self, subcon, advance_stream = True, force_build = True):
1020 Subconstruct.__init__(self, subcon)
1021 self.advance_stream = advance_stream
1022 self.force_build = force_build
1023 def _parse(self, stream, context):
1024 obj = LazyContainer(self.subcon, stream, stream.tell(), context)
1025 if self.advance_stream:
1026 stream.seek(self.subcon._sizeof(context), 1)
1028 def _build(self, obj, stream, context):
1029 if not isinstance(obj, LazyContainer):
1030 self.subcon._build(obj, stream, context)
1031 elif self.force_build or obj.has_value:
1032 self.subcon._build(obj.value, stream, context)
1033 elif self.advance_stream:
1034 stream.seek(self.subcon._sizeof(context), 1)
1036 class Buffered(Subconstruct):
1038 Creates an in-memory buffered stream, which can undergo encoding and
1039 decoding prior to being passed on to the subconstruct.
1043 * Do not use pointers inside Buffered
1046 * subcon - the subcon which will operate on the buffer
1047 * encoder - a function that takes a string and returns an encoded
1048 string (used after building)
1049 * decoder - a function that takes a string and returns a decoded
1050 string (used before parsing)
1051 * resizer - a function that takes the size of the subcon and "adjusts"
1052 or "resizes" it according to the encoding/decoding process.
1055 Buffered(BitField("foo", 16),
1056 encoder = decode_bin,
1057 decoder = encode_bin,
1058 resizer = lambda size: size / 8,
1061 __slots__ = ["encoder", "decoder", "resizer"]
1062 def __init__(self, subcon, decoder, encoder, resizer):
1063 Subconstruct.__init__(self, subcon)
1064 self.encoder = encoder
1065 self.decoder = decoder
1066 self.resizer = resizer
1067 def _parse(self, stream, context):
1068 data = _read_stream(stream, self._sizeof(context))
1069 stream2 = BytesIO(self.decoder(data))
1070 return self.subcon._parse(stream2, context)
1071 def _build(self, obj, stream, context):
1072 size = self._sizeof(context)
1074 self.subcon._build(obj, stream2, context)
1075 data = self.encoder(stream2.getvalue())
1076 assert len(data) == size
1077 _write_stream(stream, self._sizeof(context), data)
1078 def _sizeof(self, context):
1079 return self.resizer(self.subcon._sizeof(context))
1081 class Restream(Subconstruct):
1083 Wraps the stream with a read-wrapper (for parsing) or a
1084 write-wrapper (for building). The stream wrapper can buffer the data
1085 internally, reading it from- or writing it to the underlying stream
1086 as needed. For example, BitStreamReader reads whole bytes from the
1087 underlying stream, but returns them as individual bits.
1090 When the parsing or building is done, the stream's close method
1091 will be invoked. It can perform any finalization needed for the stream
1092 wrapper, but it must not close the underlying stream.
1095 * Do not use pointers inside Restream
1098 * subcon - the subcon
1099 * stream_reader - the read-wrapper
1100 * stream_writer - the write wrapper
1101 * resizer - a function that takes the size of the subcon and "adjusts"
1102 or "resizes" it according to the encoding/decoding process.
1105 Restream(BitField("foo", 16),
1106 stream_reader = BitStreamReader,
1107 stream_writer = BitStreamWriter,
1108 resizer = lambda size: size / 8,
1111 __slots__ = ["stream_reader", "stream_writer", "resizer"]
1112 def __init__(self, subcon, stream_reader, stream_writer, resizer):
1113 Subconstruct.__init__(self, subcon)
1114 self.stream_reader = stream_reader
1115 self.stream_writer = stream_writer
1116 self.resizer = resizer
1117 def _parse(self, stream, context):
1118 stream2 = self.stream_reader(stream)
1119 obj = self.subcon._parse(stream2, context)
1122 def _build(self, obj, stream, context):
1123 stream2 = self.stream_writer(stream)
1124 self.subcon._build(obj, stream2, context)
1126 def _sizeof(self, context):
1127 return self.resizer(self.subcon._sizeof(context))
1130 #===============================================================================
1132 #===============================================================================
1133 class Reconfig(Subconstruct):
1135 Reconfigures a subconstruct. Reconfig can be used to change the name and
1136 set and clear flags of the inner subcon.
1139 * name - the new name
1140 * subcon - the subcon to reconfigure
1141 * setflags - the flags to set (default is 0)
1142 * clearflags - the flags to clear (default is 0)
1145 Reconfig("foo", UBInt8("bar"))
1148 def __init__(self, name, subcon, setflags = 0, clearflags = 0):
1149 Construct.__init__(self, name, subcon.conflags)
1150 self.subcon = subcon
1151 self._set_flag(setflags)
1152 self._clear_flag(clearflags)
1154 class Anchor(Construct):
1156 Returns the "anchor" (stream position) at the point where it's inserted.
1157 Useful for adjusting relative offsets to absolute positions, or to measure
1158 sizes of constructs.
1159 absolute pointer = anchor + relative offset
1160 size = anchor_after - anchor_before
1164 * requires a seekable stream.
1167 * name - the name of the anchor
1172 UBInt8("relative_offset"),
1173 Pointer(lambda ctx: ctx.relative_offset + ctx.base,
1179 def _parse(self, stream, context):
1180 return stream.tell()
1181 def _build(self, obj, stream, context):
1182 context[self.name] = stream.tell()
1183 def _sizeof(self, context):
1186 class Value(Construct):
1191 * name - the name of the value
1192 * func - a function that takes the context and return the computed value
1198 Value("total_pixels", lambda ctx: ctx.width * ctx.height),
1201 __slots__ = ["func"]
1202 def __init__(self, name, func):
1203 Construct.__init__(self, name)
1205 self._set_flag(self.FLAG_DYNAMIC)
1206 def _parse(self, stream, context):
1207 return self.func(context)
1208 def _build(self, obj, stream, context):
1209 context[self.name] = self.func(context)
1210 def _sizeof(self, context):
1213 #class Dynamic(Construct):
1215 # Dynamically creates a construct and uses it for parsing and building.
1216 # This allows you to create change the construction tree on the fly.
1220 # * name - the name of the construct
1221 # * factoryfunc - a function that takes the context and returns a new
1222 # construct object which will be used for parsing and building.
1227 # return UBInt8("spam")
1229 # return String("spam", 9)
1233 # Dynamic("spam", factory),
1236 # __slots__ = ["factoryfunc"]
1237 # def __init__(self, name, factoryfunc):
1238 # Construct.__init__(self, name, self.FLAG_COPY_CONTEXT)
1239 # self.factoryfunc = factoryfunc
1240 # self._set_flag(self.FLAG_DYNAMIC)
1241 # def _parse(self, stream, context):
1242 # return self.factoryfunc(context)._parse(stream, context)
1243 # def _build(self, obj, stream, context):
1244 # return self.factoryfunc(context)._build(obj, stream, context)
1245 # def _sizeof(self, context):
1246 # return self.factoryfunc(context)._sizeof(context)
1248 class LazyBound(Construct):
1250 Lazily bound construct, useful for constructs that need to make cyclic
1251 references (linked-lists, expression trees, etc.).
1259 LazyBound("next", lambda: foo),
1262 __slots__ = ["bindfunc", "bound"]
1263 def __init__(self, name, bindfunc):
1264 Construct.__init__(self, name)
1266 self.bindfunc = bindfunc
1267 def _parse(self, stream, context):
1268 if self.bound is None:
1269 self.bound = self.bindfunc()
1270 return self.bound._parse(stream, context)
1271 def _build(self, obj, stream, context):
1272 if self.bound is None:
1273 self.bound = self.bindfunc()
1274 self.bound._build(obj, stream, context)
1275 def _sizeof(self, context):
1276 if self.bound is None:
1277 self.bound = self.bindfunc()
1278 return self.bound._sizeof(context)
1280 class Pass(Construct):
1282 A do-nothing construct, useful as the default case for Switch, or
1284 See also Switch and Enum.
1287 * this construct is a singleton. do not try to instatiate it, as it
1294 def _parse(self, stream, context):
1296 def _build(self, obj, stream, context):
1298 def _sizeof(self, context):
1302 class Terminator(Construct):
1304 Asserts the end of the stream has been reached at the point it's placed.
1305 You can use this to ensure no more unparsed data follows.
1308 * this construct is only meaningful for parsing. for building, it's
1310 * this construct is a singleton. do not try to instatiate it, as it
1317 def _parse(self, stream, context):
1319 raise TerminatorError("expected end of stream")
1320 def _build(self, obj, stream, context):
1322 def _sizeof(self, context):
1324 Terminator = Terminator(None)