diff options
| author | Andrew Lamoureux <andrew@vector35.com> | 2019-03-19 17:32:44 -0400 |
|---|---|---|
| committer | Rusty Wagner <rusty@vector35.com> | 2019-03-20 13:00:17 -0400 |
| commit | 24ead55316ac8497df5842caf4e1d51f2b799787 (patch) | |
| tree | 3dfdfae8a076445c9d24bf6d1b40e57f36ebf94a /python/examples/kaitai/kaitai_struct_formats/serialization | |
| parent | 411acbaa4a1d217e731d3df0b189445117be3b26 (diff) | |
kaitai: adjustments and rearrangements for python2/3 compat
Diffstat (limited to 'python/examples/kaitai/kaitai_struct_formats/serialization')
9 files changed, 0 insertions, 2069 deletions
diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/__init__.py b/python/examples/kaitai/kaitai_struct_formats/serialization/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/__init__.py +++ /dev/null diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/__init__.py b/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/__init__.py +++ /dev/null diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/asn1_der.py b/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/asn1_der.py deleted file mode 100644 index 1809b2db..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/asn1/asn1_der.py +++ /dev/null @@ -1,235 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ....kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -from enum import Enum -import collections - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class Asn1Der(KaitaiStruct): - """ASN.1 (Abstract Syntax Notation One) DER (Distinguished Encoding - Rules) is a standard-backed serialization scheme used in many - different use-cases. Particularly popular usage scenarios are X.509 - certificates and some telecommunication / networking protocols. - - DER is self-describing encoding scheme which allows representation - of simple, atomic data elements, such as strings and numbers, and - complex objects, such as sequences of other elements. - - DER is a subset of BER (Basic Encoding Rules), with an emphasis on - being non-ambiguous: there's always exactly one canonical way to - encode a data structure defined in terms of ASN.1 using DER. - - This spec allows full parsing of format syntax, but to understand - the semantics, one would typically require a dictionary of Object - Identifiers (OIDs), to match OID bodies against some human-readable - list of constants. OIDs are covered by many different standards, - so typically it's simpler to use a pre-compiled list of them, such - as: - - * https://www.cs.auckland.ac.nz/~pgut001/dumpasn1.cfg - * http://oid-info.com/ - * https://www.alvestrand.no/objectid/top.html - - .. seealso:: - Source - https://www.itu.int/rec/T-REC-X.690-201508-I/en - """ - - class TypeTag(Enum): - end_of_content = 0 - boolean = 1 - integer = 2 - bit_string = 3 - octet_string = 4 - null_value = 5 - object_id = 6 - object_descriptor = 7 - external = 8 - real = 9 - enumerated = 10 - embedded_pdv = 11 - utf8string = 12 - relative_oid = 13 - sequence_10 = 16 - printable_string = 19 - ia5string = 22 - sequence_30 = 48 - set = 49 - SEQ_FIELDS = ["type_tag", "len", "body"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['type_tag']['start'] = self._io.pos() - self.type_tag = KaitaiStream.resolve_enum(self._root.TypeTag, self._io.read_u1()) - self._debug['type_tag']['end'] = self._io.pos() - self._debug['len']['start'] = self._io.pos() - self.len = self._root.LenEncoded(self._io, self, self._root) - self.len._read() - self._debug['len']['end'] = self._io.pos() - self._debug['body']['start'] = self._io.pos() - _on = self.type_tag - if _on == self._root.TypeTag.printable_string: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodyPrintableString(io, self, self._root) - self.body._read() - elif _on == self._root.TypeTag.sequence_10: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodySequence(io, self, self._root) - self.body._read() - elif _on == self._root.TypeTag.set: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodySequence(io, self, self._root) - self.body._read() - elif _on == self._root.TypeTag.sequence_30: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodySequence(io, self, self._root) - self.body._read() - elif _on == self._root.TypeTag.utf8string: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodyUtf8string(io, self, self._root) - self.body._read() - elif _on == self._root.TypeTag.object_id: - self._raw_body = self._io.read_bytes(self.len.result) - io = KaitaiStream(BytesIO(self._raw_body)) - self.body = self._root.BodyObjectId(io, self, self._root) - self.body._read() - else: - self.body = self._io.read_bytes(self.len.result) - self._debug['body']['end'] = self._io.pos() - - class BodySequence(KaitaiStruct): - SEQ_FIELDS = ["entries"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['entries']['start'] = self._io.pos() - self.entries = [] - i = 0 - while not self._io.is_eof(): - if not 'arr' in self._debug['entries']: - self._debug['entries']['arr'] = [] - self._debug['entries']['arr'].append({'start': self._io.pos()}) - _t_entries = Asn1Der(self._io) - _t_entries._read() - self.entries.append(_t_entries) - self._debug['entries']['arr'][len(self.entries) - 1]['end'] = self._io.pos() - i += 1 - - self._debug['entries']['end'] = self._io.pos() - - - class BodyUtf8string(KaitaiStruct): - SEQ_FIELDS = ["str"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['str']['start'] = self._io.pos() - self.str = (self._io.read_bytes_full()).decode(u"UTF-8") - self._debug['str']['end'] = self._io.pos() - - - class BodyObjectId(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.microsoft.com/en-us/windows/desktop/SecCertEnroll/about-object-identifier - """ - SEQ_FIELDS = ["first_and_second", "rest"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['first_and_second']['start'] = self._io.pos() - self.first_and_second = self._io.read_u1() - self._debug['first_and_second']['end'] = self._io.pos() - self._debug['rest']['start'] = self._io.pos() - self.rest = self._io.read_bytes_full() - self._debug['rest']['end'] = self._io.pos() - - @property - def first(self): - if hasattr(self, '_m_first'): - return self._m_first if hasattr(self, '_m_first') else None - - self._m_first = self.first_and_second // 40 - return self._m_first if hasattr(self, '_m_first') else None - - @property - def second(self): - if hasattr(self, '_m_second'): - return self._m_second if hasattr(self, '_m_second') else None - - self._m_second = (self.first_and_second % 40) - return self._m_second if hasattr(self, '_m_second') else None - - - class LenEncoded(KaitaiStruct): - SEQ_FIELDS = ["b1", "int2", "int1"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['b1']['start'] = self._io.pos() - self.b1 = self._io.read_u1() - self._debug['b1']['end'] = self._io.pos() - if self.b1 == 130: - self._debug['int2']['start'] = self._io.pos() - self.int2 = self._io.read_u2be() - self._debug['int2']['end'] = self._io.pos() - - if self.b1 == 129: - self._debug['int1']['start'] = self._io.pos() - self.int1 = self._io.read_u1() - self._debug['int1']['end'] = self._io.pos() - - - @property - def result(self): - if hasattr(self, '_m_result'): - return self._m_result if hasattr(self, '_m_result') else None - - self._m_result = (self.int1 if self.b1 == 129 else (self.int2 if self.b1 == 130 else self.b1)) - return self._m_result if hasattr(self, '_m_result') else None - - - class BodyPrintableString(KaitaiStruct): - SEQ_FIELDS = ["str"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['str']['start'] = self._io.pos() - self.str = (self._io.read_bytes_full()).decode(u"ASCII") - self._debug['str']['end'] = self._io.pos() - - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/bson.py b/python/examples/kaitai/kaitai_struct_formats/serialization/bson.py deleted file mode 100644 index f3b0f295..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/bson.py +++ /dev/null @@ -1,404 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ...kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -import collections -from enum import Enum - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class Bson(KaitaiStruct): - """BSON, short for Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec. For example, BSON has a Date type and a BinData type. BSON can be compared to binary interchange formats, like Protocol Buffers. BSON is more "schemaless" than Protocol Buffers, which can give it an advantage in flexibility but also a slight disadvantage in space efficiency (BSON has overhead for field names within the serialized data). BSON was designed to have the following three characteristics: - * Lightweight. Keeping spatial overhead to a minimum is important for any data representation format, especially when used over the network. - * Traversable. BSON is designed to be traversed easily. This is a vital property in its role as the primary data representation for MongoDB. - * Efficient. Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types. - """ - SEQ_FIELDS = ["len", "fields", "terminator"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._io.read_s4le() - self._debug['len']['end'] = self._io.pos() - self._debug['fields']['start'] = self._io.pos() - self._raw_fields = self._io.read_bytes((self.len - 5)) - io = KaitaiStream(BytesIO(self._raw_fields)) - self.fields = self._root.ElementsList(io, self, self._root) - self.fields._read() - self._debug['fields']['end'] = self._io.pos() - self._debug['terminator']['start'] = self._io.pos() - self.terminator = self._io.ensure_fixed_contents(b"\x00") - self._debug['terminator']['end'] = self._io.pos() - - class Timestamp(KaitaiStruct): - """Special internal type used by MongoDB replication and sharding. First 4 bytes are an increment, second 4 are a timestamp.""" - SEQ_FIELDS = ["increment", "timestamp"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['increment']['start'] = self._io.pos() - self.increment = self._io.read_u4le() - self._debug['increment']['end'] = self._io.pos() - self._debug['timestamp']['start'] = self._io.pos() - self.timestamp = self._io.read_u4le() - self._debug['timestamp']['end'] = self._io.pos() - - - class BinData(KaitaiStruct): - """The BSON "binary" or "BinData" datatype is used to represent arrays of bytes. It is somewhat analogous to the Java notion of a ByteArray. BSON binary values have a subtype. This is used to indicate what kind of data is in the byte array. Subtypes from zero to 127 are predefined or reserved. Subtypes from 128-255 are user-defined.""" - - class Subtype(Enum): - generic = 0 - function = 1 - byte_array_deprecated = 2 - uuid_deprecated = 3 - uuid = 4 - md5 = 5 - custom = 128 - SEQ_FIELDS = ["len", "subtype", "content"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._io.read_s4le() - self._debug['len']['end'] = self._io.pos() - self._debug['subtype']['start'] = self._io.pos() - self.subtype = KaitaiStream.resolve_enum(self._root.BinData.Subtype, self._io.read_u1()) - self._debug['subtype']['end'] = self._io.pos() - self._debug['content']['start'] = self._io.pos() - _on = self.subtype - if _on == self._root.BinData.Subtype.byte_array_deprecated: - self._raw_content = self._io.read_bytes(self.len) - io = KaitaiStream(BytesIO(self._raw_content)) - self.content = self._root.BinData.ByteArrayDeprecated(io, self, self._root) - self.content._read() - else: - self.content = self._io.read_bytes(self.len) - self._debug['content']['end'] = self._io.pos() - - class ByteArrayDeprecated(KaitaiStruct): - """The BSON "binary" or "BinData" datatype is used to represent arrays of bytes. It is somewhat analogous to the Java notion of a ByteArray. BSON binary values have a subtype. This is used to indicate what kind of data is in the byte array. Subtypes from zero to 127 are predefined or reserved. Subtypes from 128-255 are user-defined.""" - SEQ_FIELDS = ["len", "content"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._io.read_s4le() - self._debug['len']['end'] = self._io.pos() - self._debug['content']['start'] = self._io.pos() - self.content = self._io.read_bytes(self.len) - self._debug['content']['end'] = self._io.pos() - - - - class ElementsList(KaitaiStruct): - SEQ_FIELDS = ["elements"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['elements']['start'] = self._io.pos() - self.elements = [] - i = 0 - while not self._io.is_eof(): - if not 'arr' in self._debug['elements']: - self._debug['elements']['arr'] = [] - self._debug['elements']['arr'].append({'start': self._io.pos()}) - _t_elements = self._root.Element(self._io, self, self._root) - _t_elements._read() - self.elements.append(_t_elements) - self._debug['elements']['arr'][len(self.elements) - 1]['end'] = self._io.pos() - i += 1 - - self._debug['elements']['end'] = self._io.pos() - - - class Cstring(KaitaiStruct): - SEQ_FIELDS = ["str"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['str']['start'] = self._io.pos() - self.str = (self._io.read_bytes_term(0, False, True, True)).decode(u"UTF-8") - self._debug['str']['end'] = self._io.pos() - - - class String(KaitaiStruct): - SEQ_FIELDS = ["len", "str", "terminator"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._io.read_s4le() - self._debug['len']['end'] = self._io.pos() - self._debug['str']['start'] = self._io.pos() - self.str = (self._io.read_bytes((self.len - 1))).decode(u"UTF-8") - self._debug['str']['end'] = self._io.pos() - self._debug['terminator']['start'] = self._io.pos() - self.terminator = self._io.ensure_fixed_contents(b"\x00") - self._debug['terminator']['end'] = self._io.pos() - - - class Element(KaitaiStruct): - - class BsonType(Enum): - min_key = -1 - end_of_object = 0 - number_double = 1 - string = 2 - document = 3 - array = 4 - bin_data = 5 - undefined = 6 - object_id = 7 - boolean = 8 - utc_datetime = 9 - jst_null = 10 - reg_ex = 11 - db_pointer = 12 - javascript = 13 - symbol = 14 - code_with_scope = 15 - number_int = 16 - timestamp = 17 - number_long = 18 - number_decimal = 19 - max_key = 127 - SEQ_FIELDS = ["type_byte", "name", "content"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['type_byte']['start'] = self._io.pos() - self.type_byte = KaitaiStream.resolve_enum(self._root.Element.BsonType, self._io.read_u1()) - self._debug['type_byte']['end'] = self._io.pos() - self._debug['name']['start'] = self._io.pos() - self.name = self._root.Cstring(self._io, self, self._root) - self.name._read() - self._debug['name']['end'] = self._io.pos() - self._debug['content']['start'] = self._io.pos() - _on = self.type_byte - if _on == self._root.Element.BsonType.code_with_scope: - self.content = self._root.CodeWithScope(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.reg_ex: - self.content = self._root.RegEx(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.number_double: - self.content = self._io.read_f8le() - elif _on == self._root.Element.BsonType.symbol: - self.content = self._root.String(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.timestamp: - self.content = self._root.Timestamp(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.number_int: - self.content = self._io.read_s4le() - elif _on == self._root.Element.BsonType.document: - self.content = Bson(self._io) - self.content._read() - elif _on == self._root.Element.BsonType.object_id: - self.content = self._root.ObjectId(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.javascript: - self.content = self._root.String(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.utc_datetime: - self.content = self._io.read_s8le() - elif _on == self._root.Element.BsonType.boolean: - self.content = self._io.read_u1() - elif _on == self._root.Element.BsonType.number_long: - self.content = self._io.read_s8le() - elif _on == self._root.Element.BsonType.bin_data: - self.content = self._root.BinData(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.string: - self.content = self._root.String(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.db_pointer: - self.content = self._root.DbPointer(self._io, self, self._root) - self.content._read() - elif _on == self._root.Element.BsonType.array: - self.content = Bson(self._io) - self.content._read() - elif _on == self._root.Element.BsonType.number_decimal: - self.content = self._root.F16(self._io, self, self._root) - self.content._read() - self._debug['content']['end'] = self._io.pos() - - - class DbPointer(KaitaiStruct): - SEQ_FIELDS = ["namespace", "id"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['namespace']['start'] = self._io.pos() - self.namespace = self._root.String(self._io, self, self._root) - self.namespace._read() - self._debug['namespace']['end'] = self._io.pos() - self._debug['id']['start'] = self._io.pos() - self.id = self._root.ObjectId(self._io, self, self._root) - self.id._read() - self._debug['id']['end'] = self._io.pos() - - - class U3(KaitaiStruct): - """Implements unsigned 24-bit (3 byte) integer. - """ - SEQ_FIELDS = ["b1", "b2", "b3"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['b1']['start'] = self._io.pos() - self.b1 = self._io.read_u1() - self._debug['b1']['end'] = self._io.pos() - self._debug['b2']['start'] = self._io.pos() - self.b2 = self._io.read_u1() - self._debug['b2']['end'] = self._io.pos() - self._debug['b3']['start'] = self._io.pos() - self.b3 = self._io.read_u1() - self._debug['b3']['end'] = self._io.pos() - - @property - def value(self): - if hasattr(self, '_m_value'): - return self._m_value if hasattr(self, '_m_value') else None - - self._m_value = ((self.b1 | (self.b2 << 8)) | (self.b3 << 16)) - return self._m_value if hasattr(self, '_m_value') else None - - - class CodeWithScope(KaitaiStruct): - SEQ_FIELDS = ["id", "source", "scope"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['id']['start'] = self._io.pos() - self.id = self._io.read_s4le() - self._debug['id']['end'] = self._io.pos() - self._debug['source']['start'] = self._io.pos() - self.source = self._root.String(self._io, self, self._root) - self.source._read() - self._debug['source']['end'] = self._io.pos() - self._debug['scope']['start'] = self._io.pos() - self.scope = Bson(self._io) - self.scope._read() - self._debug['scope']['end'] = self._io.pos() - - - class F16(KaitaiStruct): - """128-bit IEEE 754-2008 decimal floating point.""" - SEQ_FIELDS = ["str", "exponent", "significand_hi", "significand_lo"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['str']['start'] = self._io.pos() - self.str = self._io.read_bits_int(1) != 0 - self._debug['str']['end'] = self._io.pos() - self._debug['exponent']['start'] = self._io.pos() - self.exponent = self._io.read_bits_int(15) - self._debug['exponent']['end'] = self._io.pos() - self._debug['significand_hi']['start'] = self._io.pos() - self.significand_hi = self._io.read_bits_int(49) - self._debug['significand_hi']['end'] = self._io.pos() - self._io.align_to_byte() - self._debug['significand_lo']['start'] = self._io.pos() - self.significand_lo = self._io.read_u8le() - self._debug['significand_lo']['end'] = self._io.pos() - - - class ObjectId(KaitaiStruct): - """https://docs.mongodb.com/manual/reference/method/ObjectId/.""" - SEQ_FIELDS = ["epoch_time", "machine_id", "process_id", "counter"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['epoch_time']['start'] = self._io.pos() - self.epoch_time = self._io.read_u4le() - self._debug['epoch_time']['end'] = self._io.pos() - self._debug['machine_id']['start'] = self._io.pos() - self.machine_id = self._root.U3(self._io, self, self._root) - self.machine_id._read() - self._debug['machine_id']['end'] = self._io.pos() - self._debug['process_id']['start'] = self._io.pos() - self.process_id = self._io.read_u2le() - self._debug['process_id']['end'] = self._io.pos() - self._debug['counter']['start'] = self._io.pos() - self.counter = self._root.U3(self._io, self, self._root) - self.counter._read() - self._debug['counter']['end'] = self._io.pos() - - - class RegEx(KaitaiStruct): - SEQ_FIELDS = ["pattern", "options"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['pattern']['start'] = self._io.pos() - self.pattern = self._root.Cstring(self._io, self, self._root) - self.pattern._read() - self._debug['pattern']['end'] = self._io.pos() - self._debug['options']['start'] = self._io.pos() - self.options = self._root.Cstring(self._io, self, self._root) - self.options._read() - self._debug['options']['end'] = self._io.pos() - - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/google_protobuf.py b/python/examples/kaitai/kaitai_struct_formats/serialization/google_protobuf.py deleted file mode 100644 index 0bb3cac6..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/google_protobuf.py +++ /dev/null @@ -1,151 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ...kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -import collections -from enum import Enum - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -from vlq_base128_le import VlqBase128Le -class GoogleProtobuf(KaitaiStruct): - """Google Protocol Buffers (AKA protobuf) is a popular data - serialization scheme used for communication protocols, data storage, - etc. There are implementations are available for almost every - popular language. The focus points of this scheme are brevity (data - is encoded in a very size-efficient manner) and extensibility (one - can add keys to the structure, while keeping it readable in previous - version of software). - - Protobuf uses semi-self-describing encoding scheme for its - messages. It means that it is possible to parse overall structure of - the message (skipping over fields one can't understand), but to - fully understand the message, one needs a protocol definition file - (`.proto`). To be specific: - - * "Keys" in key-value pairs provided in the message are identified - only with an integer "field tag". `.proto` file provides info on - which symbolic field names these field tags map to. - * "Keys" also provide something called "wire type". It's not a data - type in its common sense (i.e. you can't, for example, distinguish - `sint32` vs `uint32` vs some enum, or `string` from `bytes`), but - it's enough information to determine how many bytes to - parse. Interpretation of the value should be done according to the - type specified in `.proto` file. - * There's no direct information on which fields are optional / - required, which fields may be repeated or constitute a map, what - restrictions are placed on fields usage in a single message, what - are the fields' default values, etc, etc. - - .. seealso:: - Source - https://developers.google.com/protocol-buffers/docs/encoding - """ - SEQ_FIELDS = ["pairs"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['pairs']['start'] = self._io.pos() - self.pairs = [] - i = 0 - while not self._io.is_eof(): - if not 'arr' in self._debug['pairs']: - self._debug['pairs']['arr'] = [] - self._debug['pairs']['arr'].append({'start': self._io.pos()}) - _t_pairs = self._root.Pair(self._io, self, self._root) - _t_pairs._read() - self.pairs.append(_t_pairs) - self._debug['pairs']['arr'][len(self.pairs) - 1]['end'] = self._io.pos() - i += 1 - - self._debug['pairs']['end'] = self._io.pos() - - class Pair(KaitaiStruct): - """Key-value pair.""" - - class WireTypes(Enum): - varint = 0 - bit_64 = 1 - len_delimited = 2 - group_start = 3 - group_end = 4 - bit_32 = 5 - SEQ_FIELDS = ["key", "value"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['key']['start'] = self._io.pos() - self.key = VlqBase128Le(self._io) - self.key._read() - self._debug['key']['end'] = self._io.pos() - self._debug['value']['start'] = self._io.pos() - _on = self.wire_type - if _on == self._root.Pair.WireTypes.varint: - self.value = VlqBase128Le(self._io) - self.value._read() - elif _on == self._root.Pair.WireTypes.len_delimited: - self.value = self._root.DelimitedBytes(self._io, self, self._root) - self.value._read() - elif _on == self._root.Pair.WireTypes.bit_64: - self.value = self._io.read_u8le() - elif _on == self._root.Pair.WireTypes.bit_32: - self.value = self._io.read_u4le() - self._debug['value']['end'] = self._io.pos() - - @property - def wire_type(self): - """"Wire type" is a part of the "key" that carries enough - information to parse value from the wire, i.e. read correct - amount of bytes, but there's not enough informaton to - interprete in unambiguously. For example, one can't clearly - distinguish 64-bit fixed-sized integers from 64-bit floats, - signed zigzag-encoded varints from regular unsigned varints, - arbitrary bytes from UTF-8 encoded strings, etc. - """ - if hasattr(self, '_m_wire_type'): - return self._m_wire_type if hasattr(self, '_m_wire_type') else None - - self._m_wire_type = KaitaiStream.resolve_enum(self._root.Pair.WireTypes, (self.key.value & 7)) - return self._m_wire_type if hasattr(self, '_m_wire_type') else None - - @property - def field_tag(self): - """Identifies a field of protocol. One can look up symbolic - field name in a `.proto` file by this field tag. - """ - if hasattr(self, '_m_field_tag'): - return self._m_field_tag if hasattr(self, '_m_field_tag') else None - - self._m_field_tag = (self.key.value >> 3) - return self._m_field_tag if hasattr(self, '_m_field_tag') else None - - - class DelimitedBytes(KaitaiStruct): - SEQ_FIELDS = ["len", "body"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = VlqBase128Le(self._io) - self.len._read() - self._debug['len']['end'] = self._io.pos() - self._debug['body']['start'] = self._io.pos() - self.body = self._io.read_bytes(self.len.value) - self._debug['body']['end'] = self._io.pos() - - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/microsoft_cfb.py b/python/examples/kaitai/kaitai_struct_formats/serialization/microsoft_cfb.py deleted file mode 100644 index d8ecb714..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/microsoft_cfb.py +++ /dev/null @@ -1,287 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ...kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -import collections -from enum import Enum - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class MicrosoftCfb(KaitaiStruct): - SEQ_FIELDS = ["header"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['header']['start'] = self._io.pos() - self.header = self._root.CfbHeader(self._io, self, self._root) - self.header._read() - self._debug['header']['end'] = self._io.pos() - - class CfbHeader(KaitaiStruct): - SEQ_FIELDS = ["signature", "clsid", "version_minor", "version_major", "byte_order", "sector_shift", "mini_sector_shift", "reserved1", "size_dir", "size_fat", "ofs_dir", "transaction_seq", "mini_stream_cutoff_size", "ofs_mini_fat", "size_mini_fat", "ofs_difat", "size_difat", "difat"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['signature']['start'] = self._io.pos() - self.signature = self._io.ensure_fixed_contents(b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1") - self._debug['signature']['end'] = self._io.pos() - self._debug['clsid']['start'] = self._io.pos() - self.clsid = self._io.ensure_fixed_contents(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00") - self._debug['clsid']['end'] = self._io.pos() - self._debug['version_minor']['start'] = self._io.pos() - self.version_minor = self._io.read_u2le() - self._debug['version_minor']['end'] = self._io.pos() - self._debug['version_major']['start'] = self._io.pos() - self.version_major = self._io.read_u2le() - self._debug['version_major']['end'] = self._io.pos() - self._debug['byte_order']['start'] = self._io.pos() - self.byte_order = self._io.ensure_fixed_contents(b"\xFE\xFF") - self._debug['byte_order']['end'] = self._io.pos() - self._debug['sector_shift']['start'] = self._io.pos() - self.sector_shift = self._io.read_u2le() - self._debug['sector_shift']['end'] = self._io.pos() - self._debug['mini_sector_shift']['start'] = self._io.pos() - self.mini_sector_shift = self._io.read_u2le() - self._debug['mini_sector_shift']['end'] = self._io.pos() - self._debug['reserved1']['start'] = self._io.pos() - self.reserved1 = self._io.read_bytes(6) - self._debug['reserved1']['end'] = self._io.pos() - self._debug['size_dir']['start'] = self._io.pos() - self.size_dir = self._io.read_s4le() - self._debug['size_dir']['end'] = self._io.pos() - self._debug['size_fat']['start'] = self._io.pos() - self.size_fat = self._io.read_s4le() - self._debug['size_fat']['end'] = self._io.pos() - self._debug['ofs_dir']['start'] = self._io.pos() - self.ofs_dir = self._io.read_s4le() - self._debug['ofs_dir']['end'] = self._io.pos() - self._debug['transaction_seq']['start'] = self._io.pos() - self.transaction_seq = self._io.read_s4le() - self._debug['transaction_seq']['end'] = self._io.pos() - self._debug['mini_stream_cutoff_size']['start'] = self._io.pos() - self.mini_stream_cutoff_size = self._io.read_s4le() - self._debug['mini_stream_cutoff_size']['end'] = self._io.pos() - self._debug['ofs_mini_fat']['start'] = self._io.pos() - self.ofs_mini_fat = self._io.read_s4le() - self._debug['ofs_mini_fat']['end'] = self._io.pos() - self._debug['size_mini_fat']['start'] = self._io.pos() - self.size_mini_fat = self._io.read_s4le() - self._debug['size_mini_fat']['end'] = self._io.pos() - self._debug['ofs_difat']['start'] = self._io.pos() - self.ofs_difat = self._io.read_s4le() - self._debug['ofs_difat']['end'] = self._io.pos() - self._debug['size_difat']['start'] = self._io.pos() - self.size_difat = self._io.read_s4le() - self._debug['size_difat']['end'] = self._io.pos() - self._debug['difat']['start'] = self._io.pos() - self.difat = [None] * (109) - for i in range(109): - if not 'arr' in self._debug['difat']: - self._debug['difat']['arr'] = [] - self._debug['difat']['arr'].append({'start': self._io.pos()}) - self.difat[i] = self._io.read_s4le() - self._debug['difat']['arr'][i]['end'] = self._io.pos() - - self._debug['difat']['end'] = self._io.pos() - - - class FatEntries(KaitaiStruct): - SEQ_FIELDS = ["entries"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['entries']['start'] = self._io.pos() - self.entries = [] - i = 0 - while not self._io.is_eof(): - if not 'arr' in self._debug['entries']: - self._debug['entries']['arr'] = [] - self._debug['entries']['arr'].append({'start': self._io.pos()}) - self.entries.append(self._io.read_s4le()) - self._debug['entries']['arr'][len(self.entries) - 1]['end'] = self._io.pos() - i += 1 - - self._debug['entries']['end'] = self._io.pos() - - - class DirEntry(KaitaiStruct): - - class ObjType(Enum): - unknown = 0 - storage = 1 - stream = 2 - root_storage = 5 - - class RbColor(Enum): - red = 0 - black = 1 - SEQ_FIELDS = ["name", "name_len", "object_type", "color_flag", "left_sibling_id", "right_sibling_id", "child_id", "clsid", "state", "time_create", "time_mod", "ofs", "size"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['name']['start'] = self._io.pos() - self.name = (self._io.read_bytes(64)).decode(u"UTF-16LE") - self._debug['name']['end'] = self._io.pos() - self._debug['name_len']['start'] = self._io.pos() - self.name_len = self._io.read_u2le() - self._debug['name_len']['end'] = self._io.pos() - self._debug['object_type']['start'] = self._io.pos() - self.object_type = KaitaiStream.resolve_enum(self._root.DirEntry.ObjType, self._io.read_u1()) - self._debug['object_type']['end'] = self._io.pos() - self._debug['color_flag']['start'] = self._io.pos() - self.color_flag = KaitaiStream.resolve_enum(self._root.DirEntry.RbColor, self._io.read_u1()) - self._debug['color_flag']['end'] = self._io.pos() - self._debug['left_sibling_id']['start'] = self._io.pos() - self.left_sibling_id = self._io.read_s4le() - self._debug['left_sibling_id']['end'] = self._io.pos() - self._debug['right_sibling_id']['start'] = self._io.pos() - self.right_sibling_id = self._io.read_s4le() - self._debug['right_sibling_id']['end'] = self._io.pos() - self._debug['child_id']['start'] = self._io.pos() - self.child_id = self._io.read_s4le() - self._debug['child_id']['end'] = self._io.pos() - self._debug['clsid']['start'] = self._io.pos() - self.clsid = self._io.read_bytes(16) - self._debug['clsid']['end'] = self._io.pos() - self._debug['state']['start'] = self._io.pos() - self.state = self._io.read_u4le() - self._debug['state']['end'] = self._io.pos() - self._debug['time_create']['start'] = self._io.pos() - self.time_create = self._io.read_u8le() - self._debug['time_create']['end'] = self._io.pos() - self._debug['time_mod']['start'] = self._io.pos() - self.time_mod = self._io.read_u8le() - self._debug['time_mod']['end'] = self._io.pos() - self._debug['ofs']['start'] = self._io.pos() - self.ofs = self._io.read_s4le() - self._debug['ofs']['end'] = self._io.pos() - self._debug['size']['start'] = self._io.pos() - self.size = self._io.read_u8le() - self._debug['size']['end'] = self._io.pos() - - @property - def mini_stream(self): - if hasattr(self, '_m_mini_stream'): - return self._m_mini_stream if hasattr(self, '_m_mini_stream') else None - - if self.object_type == self._root.DirEntry.ObjType.root_storage: - io = self._root._io - _pos = io.pos() - io.seek(((self.ofs + 1) * self._root.sector_size)) - self._debug['_m_mini_stream']['start'] = io.pos() - self._m_mini_stream = io.read_bytes(self.size) - self._debug['_m_mini_stream']['end'] = io.pos() - io.seek(_pos) - - return self._m_mini_stream if hasattr(self, '_m_mini_stream') else None - - @property - def child(self): - if hasattr(self, '_m_child'): - return self._m_child if hasattr(self, '_m_child') else None - - if self.child_id != -1: - io = self._root._io - _pos = io.pos() - io.seek((((self._root.header.ofs_dir + 1) * self._root.sector_size) + (self.child_id * 128))) - self._debug['_m_child']['start'] = io.pos() - self._m_child = self._root.DirEntry(io, self, self._root) - self._m_child._read() - self._debug['_m_child']['end'] = io.pos() - io.seek(_pos) - - return self._m_child if hasattr(self, '_m_child') else None - - @property - def left_sibling(self): - if hasattr(self, '_m_left_sibling'): - return self._m_left_sibling if hasattr(self, '_m_left_sibling') else None - - if self.left_sibling_id != -1: - io = self._root._io - _pos = io.pos() - io.seek((((self._root.header.ofs_dir + 1) * self._root.sector_size) + (self.left_sibling_id * 128))) - self._debug['_m_left_sibling']['start'] = io.pos() - self._m_left_sibling = self._root.DirEntry(io, self, self._root) - self._m_left_sibling._read() - self._debug['_m_left_sibling']['end'] = io.pos() - io.seek(_pos) - - return self._m_left_sibling if hasattr(self, '_m_left_sibling') else None - - @property - def right_sibling(self): - if hasattr(self, '_m_right_sibling'): - return self._m_right_sibling if hasattr(self, '_m_right_sibling') else None - - if self.right_sibling_id != -1: - io = self._root._io - _pos = io.pos() - io.seek((((self._root.header.ofs_dir + 1) * self._root.sector_size) + (self.right_sibling_id * 128))) - self._debug['_m_right_sibling']['start'] = io.pos() - self._m_right_sibling = self._root.DirEntry(io, self, self._root) - self._m_right_sibling._read() - self._debug['_m_right_sibling']['end'] = io.pos() - io.seek(_pos) - - return self._m_right_sibling if hasattr(self, '_m_right_sibling') else None - - - @property - def sector_size(self): - if hasattr(self, '_m_sector_size'): - return self._m_sector_size if hasattr(self, '_m_sector_size') else None - - self._m_sector_size = (1 << self.header.sector_shift) - return self._m_sector_size if hasattr(self, '_m_sector_size') else None - - @property - def fat(self): - if hasattr(self, '_m_fat'): - return self._m_fat if hasattr(self, '_m_fat') else None - - _pos = self._io.pos() - self._io.seek(self.sector_size) - self._debug['_m_fat']['start'] = self._io.pos() - self._raw__m_fat = self._io.read_bytes((self.header.size_fat * self.sector_size)) - io = KaitaiStream(BytesIO(self._raw__m_fat)) - self._m_fat = self._root.FatEntries(io, self, self._root) - self._m_fat._read() - self._debug['_m_fat']['end'] = self._io.pos() - self._io.seek(_pos) - return self._m_fat if hasattr(self, '_m_fat') else None - - @property - def dir(self): - if hasattr(self, '_m_dir'): - return self._m_dir if hasattr(self, '_m_dir') else None - - _pos = self._io.pos() - self._io.seek(((self.header.ofs_dir + 1) * self.sector_size)) - self._debug['_m_dir']['start'] = self._io.pos() - self._m_dir = self._root.DirEntry(self._io, self, self._root) - self._m_dir._read() - self._debug['_m_dir']['end'] = self._io.pos() - self._io.seek(_pos) - return self._m_dir if hasattr(self, '_m_dir') else None - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/msgpack.py b/python/examples/kaitai/kaitai_struct_formats/serialization/msgpack.py deleted file mode 100644 index 4b8b2036..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/msgpack.py +++ /dev/null @@ -1,467 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ...kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -import collections - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class Msgpack(KaitaiStruct): - """MessagePack (msgpack) is a system to serialize arbitrary structured - data into a compact binary stream. - - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md - """ - SEQ_FIELDS = ["b1", "int_extra", "float_32_value", "float_64_value", "str_len_8", "str_len_16", "str_len_32", "str_value", "num_array_elements_16", "num_array_elements_32", "array_elements", "num_map_elements_16", "num_map_elements_32", "map_elements"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['b1']['start'] = self._io.pos() - self.b1 = self._io.read_u1() - self._debug['b1']['end'] = self._io.pos() - self._debug['int_extra']['start'] = self._io.pos() - _on = self.b1 - if _on == 211: - self.int_extra = self._io.read_s8be() - elif _on == 209: - self.int_extra = self._io.read_s2be() - elif _on == 210: - self.int_extra = self._io.read_s4be() - elif _on == 208: - self.int_extra = self._io.read_s1() - elif _on == 205: - self.int_extra = self._io.read_u2be() - elif _on == 207: - self.int_extra = self._io.read_u8be() - elif _on == 204: - self.int_extra = self._io.read_u1() - elif _on == 206: - self.int_extra = self._io.read_u4be() - self._debug['int_extra']['end'] = self._io.pos() - if self.is_float_32: - self._debug['float_32_value']['start'] = self._io.pos() - self.float_32_value = self._io.read_f4be() - self._debug['float_32_value']['end'] = self._io.pos() - - if self.is_float_64: - self._debug['float_64_value']['start'] = self._io.pos() - self.float_64_value = self._io.read_f8be() - self._debug['float_64_value']['end'] = self._io.pos() - - if self.is_str_8: - self._debug['str_len_8']['start'] = self._io.pos() - self.str_len_8 = self._io.read_u1() - self._debug['str_len_8']['end'] = self._io.pos() - - if self.is_str_16: - self._debug['str_len_16']['start'] = self._io.pos() - self.str_len_16 = self._io.read_u2be() - self._debug['str_len_16']['end'] = self._io.pos() - - if self.is_str_32: - self._debug['str_len_32']['start'] = self._io.pos() - self.str_len_32 = self._io.read_u4be() - self._debug['str_len_32']['end'] = self._io.pos() - - if self.is_str: - self._debug['str_value']['start'] = self._io.pos() - self.str_value = (self._io.read_bytes(self.str_len)).decode(u"UTF-8") - self._debug['str_value']['end'] = self._io.pos() - - if self.is_array_16: - self._debug['num_array_elements_16']['start'] = self._io.pos() - self.num_array_elements_16 = self._io.read_u2be() - self._debug['num_array_elements_16']['end'] = self._io.pos() - - if self.is_array_32: - self._debug['num_array_elements_32']['start'] = self._io.pos() - self.num_array_elements_32 = self._io.read_u4be() - self._debug['num_array_elements_32']['end'] = self._io.pos() - - if self.is_array: - self._debug['array_elements']['start'] = self._io.pos() - self.array_elements = [None] * (self.num_array_elements) - for i in range(self.num_array_elements): - if not 'arr' in self._debug['array_elements']: - self._debug['array_elements']['arr'] = [] - self._debug['array_elements']['arr'].append({'start': self._io.pos()}) - _t_array_elements = Msgpack(self._io) - _t_array_elements._read() - self.array_elements[i] = _t_array_elements - self._debug['array_elements']['arr'][i]['end'] = self._io.pos() - - self._debug['array_elements']['end'] = self._io.pos() - - if self.is_map_16: - self._debug['num_map_elements_16']['start'] = self._io.pos() - self.num_map_elements_16 = self._io.read_u2be() - self._debug['num_map_elements_16']['end'] = self._io.pos() - - if self.is_map_32: - self._debug['num_map_elements_32']['start'] = self._io.pos() - self.num_map_elements_32 = self._io.read_u4be() - self._debug['num_map_elements_32']['end'] = self._io.pos() - - if self.is_map: - self._debug['map_elements']['start'] = self._io.pos() - self.map_elements = [None] * (self.num_map_elements) - for i in range(self.num_map_elements): - if not 'arr' in self._debug['map_elements']: - self._debug['map_elements']['arr'] = [] - self._debug['map_elements']['arr'].append({'start': self._io.pos()}) - _t_map_elements = self._root.MapTuple(self._io, self, self._root) - _t_map_elements._read() - self.map_elements[i] = _t_map_elements - self._debug['map_elements']['arr'][i]['end'] = self._io.pos() - - self._debug['map_elements']['end'] = self._io.pos() - - - class MapTuple(KaitaiStruct): - SEQ_FIELDS = ["key", "value"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['key']['start'] = self._io.pos() - self.key = Msgpack(self._io) - self.key._read() - self._debug['key']['end'] = self._io.pos() - self._debug['value']['start'] = self._io.pos() - self.value = Msgpack(self._io) - self.value._read() - self._debug['value']['end'] = self._io.pos() - - - @property - def is_array_32(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array - """ - if hasattr(self, '_m_is_array_32'): - return self._m_is_array_32 if hasattr(self, '_m_is_array_32') else None - - self._m_is_array_32 = self.b1 == 221 - return self._m_is_array_32 if hasattr(self, '_m_is_array_32') else None - - @property - def int_value(self): - if hasattr(self, '_m_int_value'): - return self._m_int_value if hasattr(self, '_m_int_value') else None - - if self.is_int: - self._m_int_value = (self.pos_int7_value if self.is_pos_int7 else (self.neg_int5_value if self.is_neg_int5 else 4919)) - - return self._m_int_value if hasattr(self, '_m_int_value') else None - - @property - def str_len(self): - if hasattr(self, '_m_str_len'): - return self._m_str_len if hasattr(self, '_m_str_len') else None - - if self.is_str: - self._m_str_len = ((self.b1 & 31) if self.is_fix_str else (self.str_len_8 if self.is_str_8 else (self.str_len_16 if self.is_str_16 else self.str_len_32))) - - return self._m_str_len if hasattr(self, '_m_str_len') else None - - @property - def is_fix_array(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array - """ - if hasattr(self, '_m_is_fix_array'): - return self._m_is_fix_array if hasattr(self, '_m_is_fix_array') else None - - self._m_is_fix_array = (self.b1 & 240) == 144 - return self._m_is_fix_array if hasattr(self, '_m_is_fix_array') else None - - @property - def is_map(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map - """ - if hasattr(self, '_m_is_map'): - return self._m_is_map if hasattr(self, '_m_is_map') else None - - self._m_is_map = ((self.is_fix_map) or (self.is_map_16) or (self.is_map_32)) - return self._m_is_map if hasattr(self, '_m_is_map') else None - - @property - def is_array(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array - """ - if hasattr(self, '_m_is_array'): - return self._m_is_array if hasattr(self, '_m_is_array') else None - - self._m_is_array = ((self.is_fix_array) or (self.is_array_16) or (self.is_array_32)) - return self._m_is_array if hasattr(self, '_m_is_array') else None - - @property - def is_float(self): - if hasattr(self, '_m_is_float'): - return self._m_is_float if hasattr(self, '_m_is_float') else None - - self._m_is_float = ((self.is_float_32) or (self.is_float_64)) - return self._m_is_float if hasattr(self, '_m_is_float') else None - - @property - def is_str_8(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - """ - if hasattr(self, '_m_is_str_8'): - return self._m_is_str_8 if hasattr(self, '_m_is_str_8') else None - - self._m_is_str_8 = self.b1 == 217 - return self._m_is_str_8 if hasattr(self, '_m_is_str_8') else None - - @property - def is_fix_map(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map - """ - if hasattr(self, '_m_is_fix_map'): - return self._m_is_fix_map if hasattr(self, '_m_is_fix_map') else None - - self._m_is_fix_map = (self.b1 & 240) == 128 - return self._m_is_fix_map if hasattr(self, '_m_is_fix_map') else None - - @property - def is_int(self): - if hasattr(self, '_m_is_int'): - return self._m_is_int if hasattr(self, '_m_is_int') else None - - self._m_is_int = ((self.is_pos_int7) or (self.is_neg_int5)) - return self._m_is_int if hasattr(self, '_m_is_int') else None - - @property - def is_bool(self): - if hasattr(self, '_m_is_bool'): - return self._m_is_bool if hasattr(self, '_m_is_bool') else None - - self._m_is_bool = ((self.b1 == 194) or (self.b1 == 195)) - return self._m_is_bool if hasattr(self, '_m_is_bool') else None - - @property - def is_str_16(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - """ - if hasattr(self, '_m_is_str_16'): - return self._m_is_str_16 if hasattr(self, '_m_is_str_16') else None - - self._m_is_str_16 = self.b1 == 218 - return self._m_is_str_16 if hasattr(self, '_m_is_str_16') else None - - @property - def is_float_64(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float - """ - if hasattr(self, '_m_is_float_64'): - return self._m_is_float_64 if hasattr(self, '_m_is_float_64') else None - - self._m_is_float_64 = self.b1 == 203 - return self._m_is_float_64 if hasattr(self, '_m_is_float_64') else None - - @property - def is_map_16(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map - """ - if hasattr(self, '_m_is_map_16'): - return self._m_is_map_16 if hasattr(self, '_m_is_map_16') else None - - self._m_is_map_16 = self.b1 == 222 - return self._m_is_map_16 if hasattr(self, '_m_is_map_16') else None - - @property - def is_neg_int5(self): - if hasattr(self, '_m_is_neg_int5'): - return self._m_is_neg_int5 if hasattr(self, '_m_is_neg_int5') else None - - self._m_is_neg_int5 = (self.b1 & 224) == 224 - return self._m_is_neg_int5 if hasattr(self, '_m_is_neg_int5') else None - - @property - def pos_int7_value(self): - if hasattr(self, '_m_pos_int7_value'): - return self._m_pos_int7_value if hasattr(self, '_m_pos_int7_value') else None - - if self.is_pos_int7: - self._m_pos_int7_value = self.b1 - - return self._m_pos_int7_value if hasattr(self, '_m_pos_int7_value') else None - - @property - def is_nil(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-nil - """ - if hasattr(self, '_m_is_nil'): - return self._m_is_nil if hasattr(self, '_m_is_nil') else None - - self._m_is_nil = self.b1 == 192 - return self._m_is_nil if hasattr(self, '_m_is_nil') else None - - @property - def float_value(self): - if hasattr(self, '_m_float_value'): - return self._m_float_value if hasattr(self, '_m_float_value') else None - - if self.is_float: - self._m_float_value = (self.float_32_value if self.is_float_32 else self.float_64_value) - - return self._m_float_value if hasattr(self, '_m_float_value') else None - - @property - def num_array_elements(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array - """ - if hasattr(self, '_m_num_array_elements'): - return self._m_num_array_elements if hasattr(self, '_m_num_array_elements') else None - - if self.is_array: - self._m_num_array_elements = ((self.b1 & 15) if self.is_fix_array else (self.num_array_elements_16 if self.is_array_16 else self.num_array_elements_32)) - - return self._m_num_array_elements if hasattr(self, '_m_num_array_elements') else None - - @property - def neg_int5_value(self): - if hasattr(self, '_m_neg_int5_value'): - return self._m_neg_int5_value if hasattr(self, '_m_neg_int5_value') else None - - if self.is_neg_int5: - self._m_neg_int5_value = -((self.b1 & 31)) - - return self._m_neg_int5_value if hasattr(self, '_m_neg_int5_value') else None - - @property - def bool_value(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-bool - """ - if hasattr(self, '_m_bool_value'): - return self._m_bool_value if hasattr(self, '_m_bool_value') else None - - if self.is_bool: - self._m_bool_value = self.b1 == 195 - - return self._m_bool_value if hasattr(self, '_m_bool_value') else None - - @property - def is_pos_int7(self): - if hasattr(self, '_m_is_pos_int7'): - return self._m_is_pos_int7 if hasattr(self, '_m_is_pos_int7') else None - - self._m_is_pos_int7 = (self.b1 & 128) == 0 - return self._m_is_pos_int7 if hasattr(self, '_m_is_pos_int7') else None - - @property - def is_array_16(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-array - """ - if hasattr(self, '_m_is_array_16'): - return self._m_is_array_16 if hasattr(self, '_m_is_array_16') else None - - self._m_is_array_16 = self.b1 == 220 - return self._m_is_array_16 if hasattr(self, '_m_is_array_16') else None - - @property - def is_str(self): - if hasattr(self, '_m_is_str'): - return self._m_is_str if hasattr(self, '_m_is_str') else None - - self._m_is_str = ((self.is_fix_str) or (self.is_str_8) or (self.is_str_16) or (self.is_str_32)) - return self._m_is_str if hasattr(self, '_m_is_str') else None - - @property - def is_fix_str(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - """ - if hasattr(self, '_m_is_fix_str'): - return self._m_is_fix_str if hasattr(self, '_m_is_fix_str') else None - - self._m_is_fix_str = (self.b1 & 224) == 160 - return self._m_is_fix_str if hasattr(self, '_m_is_fix_str') else None - - @property - def is_str_32(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-str - """ - if hasattr(self, '_m_is_str_32'): - return self._m_is_str_32 if hasattr(self, '_m_is_str_32') else None - - self._m_is_str_32 = self.b1 == 219 - return self._m_is_str_32 if hasattr(self, '_m_is_str_32') else None - - @property - def num_map_elements(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map - """ - if hasattr(self, '_m_num_map_elements'): - return self._m_num_map_elements if hasattr(self, '_m_num_map_elements') else None - - if self.is_map: - self._m_num_map_elements = ((self.b1 & 15) if self.is_fix_map else (self.num_map_elements_16 if self.is_map_16 else self.num_map_elements_32)) - - return self._m_num_map_elements if hasattr(self, '_m_num_map_elements') else None - - @property - def is_float_32(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-float - """ - if hasattr(self, '_m_is_float_32'): - return self._m_is_float_32 if hasattr(self, '_m_is_float_32') else None - - self._m_is_float_32 = self.b1 == 202 - return self._m_is_float_32 if hasattr(self, '_m_is_float_32') else None - - @property - def is_map_32(self): - """ - .. seealso:: - Source - https://github.com/msgpack/msgpack/blob/master/spec.md#formats-map - """ - if hasattr(self, '_m_is_map_32'): - return self._m_is_map_32 if hasattr(self, '_m_is_map_32') else None - - self._m_is_map_32 = self.b1 == 223 - return self._m_is_map_32 if hasattr(self, '_m_is_map_32') else None - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/ruby_marshal.py b/python/examples/kaitai/kaitai_struct_formats/serialization/ruby_marshal.py deleted file mode 100644 index 08b35625..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/ruby_marshal.py +++ /dev/null @@ -1,417 +0,0 @@ -from __future__ import absolute_import -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from ...kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -from enum import Enum -import collections - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class RubyMarshal(KaitaiStruct): - """Ruby's Marshal module allows serialization and deserialization of - many standard and arbitrary Ruby objects in a compact binary - format. It is relatively fast, available in stdlibs standard and - allows conservation of language-specific properties (such as symbols - or encoding-aware strings). - - Feature-wise, it is comparable to other language-specific - implementations, such as: - - * Java's - [Serializable](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html) - * .NET - [BinaryFormatter](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter) - * Python's - [marshal](https://docs.python.org/3/library/marshal.html), - [pickle](https://docs.python.org/3/library/pickle.html) and - [shelve](https://docs.python.org/3/library/shelve.html) - - From internal perspective, serialized stream consists of a simple - magic header and a record. - - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Stream+Format - """ - - class Codes(Enum): - ruby_string = 34 - const_nil = 48 - ruby_symbol = 58 - ruby_symbol_link = 59 - const_false = 70 - instance_var = 73 - ruby_struct = 83 - const_true = 84 - ruby_array = 91 - packed_int = 105 - bignum = 108 - ruby_hash = 123 - SEQ_FIELDS = ["version", "records"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['version']['start'] = self._io.pos() - self.version = self._io.ensure_fixed_contents(b"\x04\x08") - self._debug['version']['end'] = self._io.pos() - self._debug['records']['start'] = self._io.pos() - self.records = self._root.Record(self._io, self, self._root) - self.records._read() - self._debug['records']['end'] = self._io.pos() - - class RubyArray(KaitaiStruct): - SEQ_FIELDS = ["num_elements", "elements"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['num_elements']['start'] = self._io.pos() - self.num_elements = self._root.PackedInt(self._io, self, self._root) - self.num_elements._read() - self._debug['num_elements']['end'] = self._io.pos() - self._debug['elements']['start'] = self._io.pos() - self.elements = [None] * (self.num_elements.value) - for i in range(self.num_elements.value): - if not 'arr' in self._debug['elements']: - self._debug['elements']['arr'] = [] - self._debug['elements']['arr'].append({'start': self._io.pos()}) - _t_elements = self._root.Record(self._io, self, self._root) - _t_elements._read() - self.elements[i] = _t_elements - self._debug['elements']['arr'][i]['end'] = self._io.pos() - - self._debug['elements']['end'] = self._io.pos() - - - class Bignum(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Bignum - """ - SEQ_FIELDS = ["sign", "len_div_2", "body"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['sign']['start'] = self._io.pos() - self.sign = self._io.read_u1() - self._debug['sign']['end'] = self._io.pos() - self._debug['len_div_2']['start'] = self._io.pos() - self.len_div_2 = self._root.PackedInt(self._io, self, self._root) - self.len_div_2._read() - self._debug['len_div_2']['end'] = self._io.pos() - self._debug['body']['start'] = self._io.pos() - self.body = self._io.read_bytes((self.len_div_2.value * 2)) - self._debug['body']['end'] = self._io.pos() - - - class RubyStruct(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Struct - """ - SEQ_FIELDS = ["name", "num_members", "members"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['name']['start'] = self._io.pos() - self.name = self._root.Record(self._io, self, self._root) - self.name._read() - self._debug['name']['end'] = self._io.pos() - self._debug['num_members']['start'] = self._io.pos() - self.num_members = self._root.PackedInt(self._io, self, self._root) - self.num_members._read() - self._debug['num_members']['end'] = self._io.pos() - self._debug['members']['start'] = self._io.pos() - self.members = [None] * (self.num_members.value) - for i in range(self.num_members.value): - if not 'arr' in self._debug['members']: - self._debug['members']['arr'] = [] - self._debug['members']['arr'].append({'start': self._io.pos()}) - _t_members = self._root.Pair(self._io, self, self._root) - _t_members._read() - self.members[i] = _t_members - self._debug['members']['arr'][i]['end'] = self._io.pos() - - self._debug['members']['end'] = self._io.pos() - - - class RubySymbol(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Symbols+and+Byte+Sequence - """ - SEQ_FIELDS = ["len", "name"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._root.PackedInt(self._io, self, self._root) - self.len._read() - self._debug['len']['end'] = self._io.pos() - self._debug['name']['start'] = self._io.pos() - self.name = (self._io.read_bytes(self.len.value)).decode(u"UTF-8") - self._debug['name']['end'] = self._io.pos() - - - class PackedInt(KaitaiStruct): - """Ruby uses sophisticated system to pack integers: first `code` - byte either determines packing scheme or carries encoded - immediate value (thus allowing smaller values from -123 to 122 - (inclusive) to take only one byte. There are 11 encoding schemes - in total: - - * 0 is encoded specially (as 0) - * 1..122 are encoded as immediate value with a shift - * 123..255 are encoded with code of 0x01 and 1 extra byte - * 0x100..0xffff are encoded with code of 0x02 and 2 extra bytes - * 0x10000..0xffffff are encoded with code of 0x03 and 3 extra - bytes - * 0x1000000..0xffffffff are encoded with code of 0x04 and 4 - extra bytes - * -123..-1 are encoded as immediate value with another shift - * -256..-124 are encoded with code of 0xff and 1 extra byte - * -0x10000..-257 are encoded with code of 0xfe and 2 extra bytes - * -0x1000000..0x10001 are encoded with code of 0xfd and 3 extra - bytes - * -0x40000000..-0x1000001 are encoded with code of 0xfc and 4 - extra bytes - - Values beyond that are serialized as bignum (even if they - technically might be not Bignum class in Ruby implementation, - i.e. if they fit into 64 bits on a 64-bit platform). - - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Fixnum+and+long - """ - SEQ_FIELDS = ["code", "encoded", "encoded2"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['code']['start'] = self._io.pos() - self.code = self._io.read_u1() - self._debug['code']['end'] = self._io.pos() - self._debug['encoded']['start'] = self._io.pos() - _on = self.code - if _on == 4: - self.encoded = self._io.read_u4le() - elif _on == 1: - self.encoded = self._io.read_u1() - elif _on == 252: - self.encoded = self._io.read_u4le() - elif _on == 253: - self.encoded = self._io.read_u2le() - elif _on == 3: - self.encoded = self._io.read_u2le() - elif _on == 2: - self.encoded = self._io.read_u2le() - elif _on == 255: - self.encoded = self._io.read_u1() - elif _on == 254: - self.encoded = self._io.read_u2le() - self._debug['encoded']['end'] = self._io.pos() - self._debug['encoded2']['start'] = self._io.pos() - _on = self.code - if _on == 3: - self.encoded2 = self._io.read_u1() - elif _on == 253: - self.encoded2 = self._io.read_u1() - self._debug['encoded2']['end'] = self._io.pos() - - @property - def is_immediate(self): - if hasattr(self, '_m_is_immediate'): - return self._m_is_immediate if hasattr(self, '_m_is_immediate') else None - - self._m_is_immediate = ((self.code > 4) and (self.code < 252)) - return self._m_is_immediate if hasattr(self, '_m_is_immediate') else None - - @property - def value(self): - if hasattr(self, '_m_value'): - return self._m_value if hasattr(self, '_m_value') else None - - self._m_value = (((self.code - 5) if self.code < 128 else (4 - (~(self.code) & 127))) if self.is_immediate else (0 if self.code == 0 else ((self.encoded - 256) if self.code == 255 else ((self.encoded - 65536) if self.code == 254 else ((((self.encoded2 << 16) | self.encoded) - 16777216) if self.code == 253 else (((self.encoded2 << 16) | self.encoded) if self.code == 3 else self.encoded)))))) - return self._m_value if hasattr(self, '_m_value') else None - - - class Pair(KaitaiStruct): - SEQ_FIELDS = ["key", "value"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['key']['start'] = self._io.pos() - self.key = self._root.Record(self._io, self, self._root) - self.key._read() - self._debug['key']['end'] = self._io.pos() - self._debug['value']['start'] = self._io.pos() - self.value = self._root.Record(self._io, self, self._root) - self.value._read() - self._debug['value']['end'] = self._io.pos() - - - class InstanceVar(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Instance+Variables - """ - SEQ_FIELDS = ["obj", "num_vars", "vars"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['obj']['start'] = self._io.pos() - self.obj = self._root.Record(self._io, self, self._root) - self.obj._read() - self._debug['obj']['end'] = self._io.pos() - self._debug['num_vars']['start'] = self._io.pos() - self.num_vars = self._root.PackedInt(self._io, self, self._root) - self.num_vars._read() - self._debug['num_vars']['end'] = self._io.pos() - self._debug['vars']['start'] = self._io.pos() - self.vars = [None] * (self.num_vars.value) - for i in range(self.num_vars.value): - if not 'arr' in self._debug['vars']: - self._debug['vars']['arr'] = [] - self._debug['vars']['arr'].append({'start': self._io.pos()}) - _t_vars = self._root.Pair(self._io, self, self._root) - _t_vars._read() - self.vars[i] = _t_vars - self._debug['vars']['arr'][i]['end'] = self._io.pos() - - self._debug['vars']['end'] = self._io.pos() - - - class Record(KaitaiStruct): - """Each record starts with a single byte that determines its type - (`code`) and contents. If necessary, additional info as parsed - as `body`, to be determined by `code`. - """ - SEQ_FIELDS = ["code", "body"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['code']['start'] = self._io.pos() - self.code = KaitaiStream.resolve_enum(self._root.Codes, self._io.read_u1()) - self._debug['code']['end'] = self._io.pos() - self._debug['body']['start'] = self._io.pos() - _on = self.code - if _on == self._root.Codes.packed_int: - self.body = self._root.PackedInt(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.bignum: - self.body = self._root.Bignum(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_array: - self.body = self._root.RubyArray(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_symbol_link: - self.body = self._root.PackedInt(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_struct: - self.body = self._root.RubyStruct(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_string: - self.body = self._root.RubyString(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.instance_var: - self.body = self._root.InstanceVar(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_hash: - self.body = self._root.RubyHash(self._io, self, self._root) - self.body._read() - elif _on == self._root.Codes.ruby_symbol: - self.body = self._root.RubySymbol(self._io, self, self._root) - self.body._read() - self._debug['body']['end'] = self._io.pos() - - - class RubyHash(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-Hash+and+Hash+with+Default+Value - """ - SEQ_FIELDS = ["num_pairs", "pairs"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['num_pairs']['start'] = self._io.pos() - self.num_pairs = self._root.PackedInt(self._io, self, self._root) - self.num_pairs._read() - self._debug['num_pairs']['end'] = self._io.pos() - self._debug['pairs']['start'] = self._io.pos() - self.pairs = [None] * (self.num_pairs.value) - for i in range(self.num_pairs.value): - if not 'arr' in self._debug['pairs']: - self._debug['pairs']['arr'] = [] - self._debug['pairs']['arr'].append({'start': self._io.pos()}) - _t_pairs = self._root.Pair(self._io, self, self._root) - _t_pairs._read() - self.pairs[i] = _t_pairs - self._debug['pairs']['arr'][i]['end'] = self._io.pos() - - self._debug['pairs']['end'] = self._io.pos() - - - class RubyString(KaitaiStruct): - """ - .. seealso:: - Source - https://docs.ruby-lang.org/en/2.4.0/marshal_rdoc.html#label-String - """ - SEQ_FIELDS = ["len", "body"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['len']['start'] = self._io.pos() - self.len = self._root.PackedInt(self._io, self, self._root) - self.len._read() - self._debug['len']['end'] = self._io.pos() - self._debug['body']['start'] = self._io.pos() - self.body = self._io.read_bytes(self.len.value) - self._debug['body']['end'] = self._io.pos() - - - diff --git a/python/examples/kaitai/kaitai_struct_formats/serialization/vlq_base128_le.py b/python/examples/kaitai/kaitai_struct_formats/serialization/vlq_base128_le.py deleted file mode 100644 index 407fdac7..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/serialization/vlq_base128_le.py +++ /dev/null @@ -1,108 +0,0 @@ -# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild - -from pkg_resources import parse_version -from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO -import collections - - -if parse_version(ks_version) < parse_version('0.7'): - raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version)) - -class VlqBase128Le(KaitaiStruct): - """A variable-length unsigned integer using base128 encoding. 1-byte groups - consist of 1-bit flag of continuation and 7-bit value chunk, and are ordered - "least significant group first", i.e. in "little-endian" manner. - - This particular encoding is specified and used in: - - * DWARF debug file format, where it's dubbed "unsigned LEB128" or "ULEB128". - http://dwarfstd.org/doc/dwarf-2.0.0.pdf - page 139 - * Google Protocol Buffers, where it's called "Base 128 Varints". - https://developers.google.com/protocol-buffers/docs/encoding?csw=1#varints - * Apache Lucene, where it's called "VInt" - http://lucene.apache.org/core/3_5_0/fileformats.html#VInt - * Apache Avro uses this as a basis for integer encoding, adding ZigZag on - top of it for signed ints - http://avro.apache.org/docs/current/spec.html#binary_encode_primitive - - More information on this encoding is available at https://en.wikipedia.org/wiki/LEB128 - - This particular implementation supports serialized values to up 8 bytes long. - """ - SEQ_FIELDS = ["groups"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['groups']['start'] = self._io.pos() - self.groups = [] - i = 0 - while True: - if not 'arr' in self._debug['groups']: - self._debug['groups']['arr'] = [] - self._debug['groups']['arr'].append({'start': self._io.pos()}) - _t_groups = self._root.Group(self._io, self, self._root) - _t_groups._read() - _ = _t_groups - self.groups.append(_) - self._debug['groups']['arr'][len(self.groups) - 1]['end'] = self._io.pos() - if not (_.has_next): - break - i += 1 - self._debug['groups']['end'] = self._io.pos() - - class Group(KaitaiStruct): - """One byte group, clearly divided into 7-bit "value" chunk and 1-bit "continuation" flag. - """ - SEQ_FIELDS = ["b"] - def __init__(self, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['b']['start'] = self._io.pos() - self.b = self._io.read_u1() - self._debug['b']['end'] = self._io.pos() - - @property - def has_next(self): - """If true, then we have more bytes to read.""" - if hasattr(self, '_m_has_next'): - return self._m_has_next if hasattr(self, '_m_has_next') else None - - self._m_has_next = (self.b & 128) != 0 - return self._m_has_next if hasattr(self, '_m_has_next') else None - - @property - def value(self): - """The 7-bit (base128) numeric value chunk of this group.""" - if hasattr(self, '_m_value'): - return self._m_value if hasattr(self, '_m_value') else None - - self._m_value = (self.b & 127) - return self._m_value if hasattr(self, '_m_value') else None - - - @property - def len(self): - if hasattr(self, '_m_len'): - return self._m_len if hasattr(self, '_m_len') else None - - self._m_len = len(self.groups) - return self._m_len if hasattr(self, '_m_len') else None - - @property - def value(self): - """Resulting value as normal integer.""" - if hasattr(self, '_m_value'): - return self._m_value if hasattr(self, '_m_value') else None - - self._m_value = (((((((self.groups[0].value + ((self.groups[1].value << 7) if self.len >= 2 else 0)) + ((self.groups[2].value << 14) if self.len >= 3 else 0)) + ((self.groups[3].value << 21) if self.len >= 4 else 0)) + ((self.groups[4].value << 28) if self.len >= 5 else 0)) + ((self.groups[5].value << 35) if self.len >= 6 else 0)) + ((self.groups[6].value << 42) if self.len >= 7 else 0)) + ((self.groups[7].value << 49) if self.len >= 8 else 0)) - return self._m_value if hasattr(self, '_m_value') else None - - |
