From 24ead55316ac8497df5842caf4e1d51f2b799787 Mon Sep 17 00:00:00 2001 From: Andrew Lamoureux Date: Tue, 19 Mar 2019 17:32:44 -0400 Subject: kaitai: adjustments and rearrangements for python2/3 compat --- .../kaitai_struct_formats/common/__init__.py | 0 .../kaitai/kaitai_struct_formats/common/bcd.py | 111 --------------------- .../kaitai_struct_formats/common/vlq_base128_be.py | 103 ------------------- .../kaitai_struct_formats/common/vlq_base128_le.py | 109 -------------------- 4 files changed, 323 deletions(-) delete mode 100644 python/examples/kaitai/kaitai_struct_formats/common/__init__.py delete mode 100644 python/examples/kaitai/kaitai_struct_formats/common/bcd.py delete mode 100644 python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_be.py delete mode 100644 python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_le.py (limited to 'python/examples/kaitai/kaitai_struct_formats/common') diff --git a/python/examples/kaitai/kaitai_struct_formats/common/__init__.py b/python/examples/kaitai/kaitai_struct_formats/common/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/python/examples/kaitai/kaitai_struct_formats/common/bcd.py b/python/examples/kaitai/kaitai_struct_formats/common/bcd.py deleted file mode 100644 index d75829d2..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/common/bcd.py +++ /dev/null @@ -1,111 +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 Bcd(KaitaiStruct): - """BCD (Binary Coded Decimals) is a common way to encode integer - numbers in a way that makes human-readable output somewhat - simpler. In this encoding scheme, every decimal digit is encoded as - either a single byte (8 bits), or a nibble (half of a byte, 4 - bits). This obviously wastes a lot of bits, but it makes translation - into human-readable string much easier than traditional - binary-to-decimal conversion process, which includes lots of - divisions by 10. - - For example, encoding integer 31337 in 8-digit, 8 bits per digit, - big endian order of digits BCD format yields - - ``` - 00 00 00 03 01 03 03 07 - ``` - - Encoding the same integer as 8-digit, 4 bits per digit, little - endian order BCD format would yield: - - ``` - 73 31 30 00 - ``` - - Using this type of encoding in Kaitai Struct is pretty - straightforward: one calls for this type, specifying desired - encoding parameters, and gets result using either `as_int` or - `as_str` attributes. - """ - SEQ_FIELDS = ["digits"] - def __init__(self, num_digits, bits_per_digit, is_le, _io, _parent=None, _root=None): - self._io = _io - self._parent = _parent - self._root = _root if _root else self - self.num_digits = num_digits - self.bits_per_digit = bits_per_digit - self.is_le = is_le - self._debug = collections.defaultdict(dict) - - def _read(self): - self._debug['digits']['start'] = self._io.pos() - self.digits = [None] * (self.num_digits) - for i in range(self.num_digits): - if not 'arr' in self._debug['digits']: - self._debug['digits']['arr'] = [] - self._debug['digits']['arr'].append({'start': self._io.pos()}) - _on = self.bits_per_digit - if _on == 4: - if not 'arr' in self._debug['digits']: - self._debug['digits']['arr'] = [] - self._debug['digits']['arr'].append({'start': self._io.pos()}) - self.digits[i] = self._io.read_bits_int(4) - self._debug['digits']['arr'][i]['end'] = self._io.pos() - elif _on == 8: - if not 'arr' in self._debug['digits']: - self._debug['digits']['arr'] = [] - self._debug['digits']['arr'].append({'start': self._io.pos()}) - self.digits[i] = self._io.read_u1() - self._debug['digits']['arr'][i]['end'] = self._io.pos() - self._debug['digits']['arr'][i]['end'] = self._io.pos() - - self._debug['digits']['end'] = self._io.pos() - - @property - def as_int(self): - """Value of this BCD number as integer. Endianness would be selected based on `is_le` parameter given.""" - if hasattr(self, '_m_as_int'): - return self._m_as_int if hasattr(self, '_m_as_int') else None - - self._m_as_int = (self.as_int_le if self.is_le else self.as_int_be) - return self._m_as_int if hasattr(self, '_m_as_int') else None - - @property - def as_int_le(self): - """Value of this BCD number as integer (treating digit order as little-endian).""" - if hasattr(self, '_m_as_int_le'): - return self._m_as_int_le if hasattr(self, '_m_as_int_le') else None - - self._m_as_int_le = (self.digits[0] + (0 if self.num_digits < 2 else ((self.digits[1] * 10) + (0 if self.num_digits < 3 else ((self.digits[2] * 100) + (0 if self.num_digits < 4 else ((self.digits[3] * 1000) + (0 if self.num_digits < 5 else ((self.digits[4] * 10000) + (0 if self.num_digits < 6 else ((self.digits[5] * 100000) + (0 if self.num_digits < 7 else ((self.digits[6] * 1000000) + (0 if self.num_digits < 8 else (self.digits[7] * 10000000))))))))))))))) - return self._m_as_int_le if hasattr(self, '_m_as_int_le') else None - - @property - def last_idx(self): - """Index of last digit (0-based).""" - if hasattr(self, '_m_last_idx'): - return self._m_last_idx if hasattr(self, '_m_last_idx') else None - - self._m_last_idx = (self.num_digits - 1) - return self._m_last_idx if hasattr(self, '_m_last_idx') else None - - @property - def as_int_be(self): - """Value of this BCD number as integer (treating digit order as big-endian).""" - if hasattr(self, '_m_as_int_be'): - return self._m_as_int_be if hasattr(self, '_m_as_int_be') else None - - self._m_as_int_be = (self.digits[self.last_idx] + (0 if self.num_digits < 2 else ((self.digits[(self.last_idx - 1)] * 10) + (0 if self.num_digits < 3 else ((self.digits[(self.last_idx - 2)] * 100) + (0 if self.num_digits < 4 else ((self.digits[(self.last_idx - 3)] * 1000) + (0 if self.num_digits < 5 else ((self.digits[(self.last_idx - 4)] * 10000) + (0 if self.num_digits < 6 else ((self.digits[(self.last_idx - 5)] * 100000) + (0 if self.num_digits < 7 else ((self.digits[(self.last_idx - 6)] * 1000000) + (0 if self.num_digits < 8 else (self.digits[(self.last_idx - 7)] * 10000000))))))))))))))) - return self._m_as_int_be if hasattr(self, '_m_as_int_be') else None - - diff --git a/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_be.py b/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_be.py deleted file mode 100644 index 097d1e36..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_be.py +++ /dev/null @@ -1,103 +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 VlqBase128Be(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 - "most significant group first", i.e. in "big-endian" manner. - - This particular encoding is specified and used in: - - * Standard MIDI file format - * ASN.1 BER encoding - - More information on this encoding is available at - https://en.wikipedia.org/wiki/Variable-length_quantity - - 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 last(self): - if hasattr(self, '_m_last'): - return self._m_last if hasattr(self, '_m_last') else None - - self._m_last = (len(self.groups) - 1) - return self._m_last if hasattr(self, '_m_last') 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[self.last].value + ((self.groups[(self.last - 1)].value << 7) if self.last >= 1 else 0)) + ((self.groups[(self.last - 2)].value << 14) if self.last >= 2 else 0)) + ((self.groups[(self.last - 3)].value << 21) if self.last >= 3 else 0)) + ((self.groups[(self.last - 4)].value << 28) if self.last >= 4 else 0)) + ((self.groups[(self.last - 5)].value << 35) if self.last >= 5 else 0)) + ((self.groups[(self.last - 6)].value << 42) if self.last >= 6 else 0)) + ((self.groups[(self.last - 7)].value << 49) if self.last >= 7 else 0)) - return self._m_value if hasattr(self, '_m_value') else None - - diff --git a/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_le.py b/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_le.py deleted file mode 100644 index ae49cffc..00000000 --- a/python/examples/kaitai/kaitai_struct_formats/common/vlq_base128_le.py +++ /dev/null @@ -1,109 +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 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 - - -- cgit v1.3.1