summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/kaitai_struct_formats/database
diff options
context:
space:
mode:
authorAndrew Lamoureux <andrew@vector35.com>2019-03-18 17:34:09 -0400
committerRusty Wagner <rusty@vector35.com>2019-03-20 13:00:17 -0400
commit815f5485eb50eff3b483a6bcc0c3d3d11569f8ca (patch)
tree5128136ac86971ed8729e8c19cb00bcf8f220acc /python/examples/kaitai/kaitai_struct_formats/database
parent8e9322ed8b5826dd62bc931d83e0828d42f7969f (diff)
kaitai: all formats compiled and included
Diffstat (limited to 'python/examples/kaitai/kaitai_struct_formats/database')
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/__init__.py0
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/dbf.py220
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/gettext_mo.py545
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/sqlite3.py470
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/tsm.py196
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/database/vlq_base128_be.py102
6 files changed, 1533 insertions, 0 deletions
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/__init__.py b/python/examples/kaitai/kaitai_struct_formats/database/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/__init__.py
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/dbf.py b/python/examples/kaitai/kaitai_struct_formats/database/dbf.py
new file mode 100644
index 00000000..4cfeb9e8
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/dbf.py
@@ -0,0 +1,220 @@
+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 Dbf(KaitaiStruct):
+ SEQ_FIELDS = ["header1", "header2", "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['header1']['start'] = self._io.pos()
+ self.header1 = self._root.Header1(self._io, self, self._root)
+ self.header1._read()
+ self._debug['header1']['end'] = self._io.pos()
+ self._debug['header2']['start'] = self._io.pos()
+ self._raw_header2 = self._io.read_bytes((self.header1.len_header - 12))
+ io = KaitaiStream(BytesIO(self._raw_header2))
+ self.header2 = self._root.Header2(io, self, self._root)
+ self.header2._read()
+ self._debug['header2']['end'] = self._io.pos()
+ self._debug['records']['start'] = self._io.pos()
+ self.records = [None] * (self.header1.num_records)
+ for i in range(self.header1.num_records):
+ if not 'arr' in self._debug['records']:
+ self._debug['records']['arr'] = []
+ self._debug['records']['arr'].append({'start': self._io.pos()})
+ self.records[i] = self._io.read_bytes(self.header1.len_record)
+ self._debug['records']['arr'][i]['end'] = self._io.pos()
+
+ self._debug['records']['end'] = self._io.pos()
+
+ class Header2(KaitaiStruct):
+ SEQ_FIELDS = ["header_dbase_3", "header_dbase_7", "fields"]
+ 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):
+ if self._root.header1.dbase_level == 3:
+ self._debug['header_dbase_3']['start'] = self._io.pos()
+ self.header_dbase_3 = self._root.HeaderDbase3(self._io, self, self._root)
+ self.header_dbase_3._read()
+ self._debug['header_dbase_3']['end'] = self._io.pos()
+
+ if self._root.header1.dbase_level == 7:
+ self._debug['header_dbase_7']['start'] = self._io.pos()
+ self.header_dbase_7 = self._root.HeaderDbase7(self._io, self, self._root)
+ self.header_dbase_7._read()
+ self._debug['header_dbase_7']['end'] = self._io.pos()
+
+ self._debug['fields']['start'] = self._io.pos()
+ self.fields = [None] * (11)
+ for i in range(11):
+ if not 'arr' in self._debug['fields']:
+ self._debug['fields']['arr'] = []
+ self._debug['fields']['arr'].append({'start': self._io.pos()})
+ _t_fields = self._root.Field(self._io, self, self._root)
+ _t_fields._read()
+ self.fields[i] = _t_fields
+ self._debug['fields']['arr'][i]['end'] = self._io.pos()
+
+ self._debug['fields']['end'] = self._io.pos()
+
+
+ class Field(KaitaiStruct):
+ SEQ_FIELDS = ["name", "datatype", "data_address", "length", "decimal_count", "reserved1", "work_area_id", "reserved2", "set_fields_flag", "reserved3"]
+ 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(11)).decode(u"ASCII")
+ self._debug['name']['end'] = self._io.pos()
+ self._debug['datatype']['start'] = self._io.pos()
+ self.datatype = self._io.read_u1()
+ self._debug['datatype']['end'] = self._io.pos()
+ self._debug['data_address']['start'] = self._io.pos()
+ self.data_address = self._io.read_u4le()
+ self._debug['data_address']['end'] = self._io.pos()
+ self._debug['length']['start'] = self._io.pos()
+ self.length = self._io.read_u1()
+ self._debug['length']['end'] = self._io.pos()
+ self._debug['decimal_count']['start'] = self._io.pos()
+ self.decimal_count = self._io.read_u1()
+ self._debug['decimal_count']['end'] = self._io.pos()
+ self._debug['reserved1']['start'] = self._io.pos()
+ self.reserved1 = self._io.read_bytes(2)
+ self._debug['reserved1']['end'] = self._io.pos()
+ self._debug['work_area_id']['start'] = self._io.pos()
+ self.work_area_id = self._io.read_u1()
+ self._debug['work_area_id']['end'] = self._io.pos()
+ self._debug['reserved2']['start'] = self._io.pos()
+ self.reserved2 = self._io.read_bytes(2)
+ self._debug['reserved2']['end'] = self._io.pos()
+ self._debug['set_fields_flag']['start'] = self._io.pos()
+ self.set_fields_flag = self._io.read_u1()
+ self._debug['set_fields_flag']['end'] = self._io.pos()
+ self._debug['reserved3']['start'] = self._io.pos()
+ self.reserved3 = self._io.read_bytes(8)
+ self._debug['reserved3']['end'] = self._io.pos()
+
+
+ class Header1(KaitaiStruct):
+ """
+ .. seealso::
+ - section 1.1 - http://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
+ """
+ SEQ_FIELDS = ["version", "last_update_y", "last_update_m", "last_update_d", "num_records", "len_header", "len_record"]
+ 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.read_u1()
+ self._debug['version']['end'] = self._io.pos()
+ self._debug['last_update_y']['start'] = self._io.pos()
+ self.last_update_y = self._io.read_u1()
+ self._debug['last_update_y']['end'] = self._io.pos()
+ self._debug['last_update_m']['start'] = self._io.pos()
+ self.last_update_m = self._io.read_u1()
+ self._debug['last_update_m']['end'] = self._io.pos()
+ self._debug['last_update_d']['start'] = self._io.pos()
+ self.last_update_d = self._io.read_u1()
+ self._debug['last_update_d']['end'] = self._io.pos()
+ self._debug['num_records']['start'] = self._io.pos()
+ self.num_records = self._io.read_u4le()
+ self._debug['num_records']['end'] = self._io.pos()
+ self._debug['len_header']['start'] = self._io.pos()
+ self.len_header = self._io.read_u2le()
+ self._debug['len_header']['end'] = self._io.pos()
+ self._debug['len_record']['start'] = self._io.pos()
+ self.len_record = self._io.read_u2le()
+ self._debug['len_record']['end'] = self._io.pos()
+
+ @property
+ def dbase_level(self):
+ if hasattr(self, '_m_dbase_level'):
+ return self._m_dbase_level if hasattr(self, '_m_dbase_level') else None
+
+ self._m_dbase_level = (self.version & 7)
+ return self._m_dbase_level if hasattr(self, '_m_dbase_level') else None
+
+
+ class HeaderDbase3(KaitaiStruct):
+ SEQ_FIELDS = ["reserved1", "reserved2", "reserved3"]
+ 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['reserved1']['start'] = self._io.pos()
+ self.reserved1 = self._io.read_bytes(3)
+ self._debug['reserved1']['end'] = self._io.pos()
+ self._debug['reserved2']['start'] = self._io.pos()
+ self.reserved2 = self._io.read_bytes(13)
+ self._debug['reserved2']['end'] = self._io.pos()
+ self._debug['reserved3']['start'] = self._io.pos()
+ self.reserved3 = self._io.read_bytes(4)
+ self._debug['reserved3']['end'] = self._io.pos()
+
+
+ class HeaderDbase7(KaitaiStruct):
+ SEQ_FIELDS = ["reserved1", "has_incomplete_transaction", "dbase_iv_encryption", "reserved2", "production_mdx", "language_driver_id", "reserved3", "language_driver_name", "reserved4"]
+ 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['reserved1']['start'] = self._io.pos()
+ self.reserved1 = self._io.ensure_fixed_contents(b"\x00\x00")
+ self._debug['reserved1']['end'] = self._io.pos()
+ self._debug['has_incomplete_transaction']['start'] = self._io.pos()
+ self.has_incomplete_transaction = self._io.read_u1()
+ self._debug['has_incomplete_transaction']['end'] = self._io.pos()
+ self._debug['dbase_iv_encryption']['start'] = self._io.pos()
+ self.dbase_iv_encryption = self._io.read_u1()
+ self._debug['dbase_iv_encryption']['end'] = self._io.pos()
+ self._debug['reserved2']['start'] = self._io.pos()
+ self.reserved2 = self._io.read_bytes(12)
+ self._debug['reserved2']['end'] = self._io.pos()
+ self._debug['production_mdx']['start'] = self._io.pos()
+ self.production_mdx = self._io.read_u1()
+ self._debug['production_mdx']['end'] = self._io.pos()
+ self._debug['language_driver_id']['start'] = self._io.pos()
+ self.language_driver_id = self._io.read_u1()
+ self._debug['language_driver_id']['end'] = self._io.pos()
+ self._debug['reserved3']['start'] = self._io.pos()
+ self.reserved3 = self._io.ensure_fixed_contents(b"\x00\x00")
+ self._debug['reserved3']['end'] = self._io.pos()
+ self._debug['language_driver_name']['start'] = self._io.pos()
+ self.language_driver_name = self._io.read_bytes(32)
+ self._debug['language_driver_name']['end'] = self._io.pos()
+ self._debug['reserved4']['start'] = self._io.pos()
+ self.reserved4 = self._io.read_bytes(4)
+ self._debug['reserved4']['end'] = self._io.pos()
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/gettext_mo.py b/python/examples/kaitai/kaitai_struct_formats/database/gettext_mo.py
new file mode 100644
index 00000000..2064a039
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/gettext_mo.py
@@ -0,0 +1,545 @@
+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 GettextMo(KaitaiStruct):
+ """[GNU gettext](https://www.gnu.org/software/gettext/) is a popular
+ solution in free/open source software world to do i18n/l10n of
+ software, by providing translated strings that will substitute
+ strings in original language (typically, English).
+
+ gettext .mo is a binary database format which stores these string
+ translation pairs in an efficient binary format, ready to be used by
+ gettext-enabled software. .mo format is a result of compilation of
+ text-based .po files using
+ [msgfmt](https://www.gnu.org/software/gettext/manual/html_node/msgfmt-Invocation.html#msgfmt-Invocation)
+ utility. The reverse conversion (.mo -> .po) is also possible using
+ [msgunfmt](https://www.gnu.org/software/gettext/manual/html_node/msgunfmt-Invocation.html#msgunfmt-Invocation)
+ decompiler utility.
+
+ .. seealso::
+ Source - https://gitlab.com/worr/libintl
+ """
+ SEQ_FIELDS = ["signature", "mo"]
+ 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.read_bytes(4)
+ self._debug['signature']['end'] = self._io.pos()
+ self._debug['mo']['start'] = self._io.pos()
+ self.mo = self._root.Mo(self._io, self, self._root)
+ self.mo._read()
+ self._debug['mo']['end'] = self._io.pos()
+
+ class HashLookupIteration(KaitaiStruct):
+ SEQ_FIELDS = []
+ def __init__(self, idx, collision_step, _io, _parent=None, _root=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self.idx = idx
+ self.collision_step = collision_step
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+ pass
+
+ @property
+ def original(self):
+ if hasattr(self, '_m_original'):
+ return self._m_original if hasattr(self, '_m_original') else None
+
+ self._m_original = self._root.mo.originals[self.idx].str
+ return self._m_original if hasattr(self, '_m_original') else None
+
+ @property
+ def translation(self):
+ if hasattr(self, '_m_translation'):
+ return self._m_translation if hasattr(self, '_m_translation') else None
+
+ self._m_translation = self._root.mo.translations[self.idx].str
+ return self._m_translation if hasattr(self, '_m_translation') else None
+
+ @property
+ def next_idx(self):
+ if hasattr(self, '_m_next_idx'):
+ return self._m_next_idx if hasattr(self, '_m_next_idx') else None
+
+ self._m_next_idx = ((self.idx + self.collision_step) - (self._root.mo.num_hashtable_items if self.idx >= (self._root.mo.num_hashtable_items - self.collision_step) else 0))
+ return self._m_next_idx if hasattr(self, '_m_next_idx') else None
+
+ @property
+ def next(self):
+ if hasattr(self, '_m_next'):
+ return self._m_next if hasattr(self, '_m_next') else None
+
+ _pos = self._io.pos()
+ self._io.seek(0)
+ self._debug['_m_next']['start'] = self._io.pos()
+ self._m_next = self._root.HashLookupIteration(self._root.mo.hashtable_items[self.next_idx].val, self.collision_step, self._io, self, self._root)
+ self._m_next._read()
+ self._debug['_m_next']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_next if hasattr(self, '_m_next') else None
+
+
+ class LookupIteration(KaitaiStruct):
+ SEQ_FIELDS = []
+ def __init__(self, current, query, _io, _parent=None, _root=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self.current = current
+ self.query = query
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+ pass
+
+ @property
+ def found(self):
+ if hasattr(self, '_m_found'):
+ return self._m_found if hasattr(self, '_m_found') else None
+
+ self._m_found = self.query == self.current.original
+ return self._m_found if hasattr(self, '_m_found') else None
+
+ @property
+ def next(self):
+ if hasattr(self, '_m_next'):
+ return self._m_next if hasattr(self, '_m_next') else None
+
+ if not (self.found):
+ _pos = self._io.pos()
+ self._io.seek(0)
+ self._debug['_m_next']['start'] = self._io.pos()
+ self._m_next = self._root.LookupIteration(self.current.next, self.query, self._io, self, self._root)
+ self._m_next._read()
+ self._debug['_m_next']['end'] = self._io.pos()
+ self._io.seek(_pos)
+
+ return self._m_next if hasattr(self, '_m_next') else None
+
+
+ class HashtableLookup(KaitaiStruct):
+ """def lookup(s:str, t:gettext_mo.GettextMo):
+ try:
+ l=gettext_mo.GettextMo.HashtableLookup(s, string_hash(s), t._io, _parent=t, _root=t)
+ e=l.entry
+ while(not e.found):
+ e=e.next
+ return e.current
+ except:
+ raise Exception("Not found "+s+" in the hashtable!")
+
+ lookup(t.mo.originals[145].str, t)
+
+ .. seealso::
+ Source - https://gitlab.com/worr/libintl/raw/master/src/lib/libintl/gettext.c
+ """
+ SEQ_FIELDS = []
+ def __init__(self, query, hash, _io, _parent=None, _root=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self.query = query
+ self.hash = hash
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+ pass
+
+ @property
+ def collision_step(self):
+ if hasattr(self, '_m_collision_step'):
+ return self._m_collision_step if hasattr(self, '_m_collision_step') else None
+
+ self._m_collision_step = ((self.hash % (self._root.mo.num_hashtable_items - 2)) + 1)
+ return self._m_collision_step if hasattr(self, '_m_collision_step') else None
+
+ @property
+ def idx(self):
+ if hasattr(self, '_m_idx'):
+ return self._m_idx if hasattr(self, '_m_idx') else None
+
+ self._m_idx = (self.hash % self._root.mo.num_hashtable_items)
+ return self._m_idx if hasattr(self, '_m_idx') else None
+
+ @property
+ def hash_lookup_iteration(self):
+ if hasattr(self, '_m_hash_lookup_iteration'):
+ return self._m_hash_lookup_iteration if hasattr(self, '_m_hash_lookup_iteration') else None
+
+ _pos = self._io.pos()
+ self._io.seek(0)
+ self._debug['_m_hash_lookup_iteration']['start'] = self._io.pos()
+ self._m_hash_lookup_iteration = self._root.HashLookupIteration(self._root.mo.hashtable_items[self.idx].val, self.collision_step, self._io, self, self._root)
+ self._m_hash_lookup_iteration._read()
+ self._debug['_m_hash_lookup_iteration']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_hash_lookup_iteration if hasattr(self, '_m_hash_lookup_iteration') else None
+
+ @property
+ def entry(self):
+ if hasattr(self, '_m_entry'):
+ return self._m_entry if hasattr(self, '_m_entry') else None
+
+ _pos = self._io.pos()
+ self._io.seek(0)
+ self._debug['_m_entry']['start'] = self._io.pos()
+ self._m_entry = self._root.LookupIteration(self.hash_lookup_iteration, self.query, self._io, self, self._root)
+ self._m_entry._read()
+ self._debug['_m_entry']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_entry if hasattr(self, '_m_entry') else None
+
+
+ class Mo(KaitaiStruct):
+ SEQ_FIELDS = ["version", "num_translations", "ofs_originals", "ofs_translations", "num_hashtable_items", "ofs_hashtable_items"]
+ 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):
+ _on = self._root.signature
+ if _on == b"\xDE\x12\x04\x95":
+ self._is_le = True
+ elif _on == b"\x95\x04\x12\xDE":
+ self._is_le = False
+
+ if self._is_le == True:
+ self._read_le()
+ elif self._is_le == False:
+ self._read_be()
+ else:
+ raise Exception("Unable to decide endianness")
+
+ def _read_le(self):
+ self._debug['version']['start'] = self._io.pos()
+ self.version = self._root.Mo.Version(self._io, self, self._root, self._is_le)
+ self.version._read()
+ self._debug['version']['end'] = self._io.pos()
+ self._debug['num_translations']['start'] = self._io.pos()
+ self.num_translations = self._io.read_u4le()
+ self._debug['num_translations']['end'] = self._io.pos()
+ self._debug['ofs_originals']['start'] = self._io.pos()
+ self.ofs_originals = self._io.read_u4le()
+ self._debug['ofs_originals']['end'] = self._io.pos()
+ self._debug['ofs_translations']['start'] = self._io.pos()
+ self.ofs_translations = self._io.read_u4le()
+ self._debug['ofs_translations']['end'] = self._io.pos()
+ self._debug['num_hashtable_items']['start'] = self._io.pos()
+ self.num_hashtable_items = self._io.read_u4le()
+ self._debug['num_hashtable_items']['end'] = self._io.pos()
+ self._debug['ofs_hashtable_items']['start'] = self._io.pos()
+ self.ofs_hashtable_items = self._io.read_u4le()
+ self._debug['ofs_hashtable_items']['end'] = self._io.pos()
+
+ def _read_be(self):
+ self._debug['version']['start'] = self._io.pos()
+ self.version = self._root.Mo.Version(self._io, self, self._root, self._is_le)
+ self.version._read()
+ self._debug['version']['end'] = self._io.pos()
+ self._debug['num_translations']['start'] = self._io.pos()
+ self.num_translations = self._io.read_u4be()
+ self._debug['num_translations']['end'] = self._io.pos()
+ self._debug['ofs_originals']['start'] = self._io.pos()
+ self.ofs_originals = self._io.read_u4be()
+ self._debug['ofs_originals']['end'] = self._io.pos()
+ self._debug['ofs_translations']['start'] = self._io.pos()
+ self.ofs_translations = self._io.read_u4be()
+ self._debug['ofs_translations']['end'] = self._io.pos()
+ self._debug['num_hashtable_items']['start'] = self._io.pos()
+ self.num_hashtable_items = self._io.read_u4be()
+ self._debug['num_hashtable_items']['end'] = self._io.pos()
+ self._debug['ofs_hashtable_items']['start'] = self._io.pos()
+ self.ofs_hashtable_items = self._io.read_u4be()
+ self._debug['ofs_hashtable_items']['end'] = self._io.pos()
+
+ class Version(KaitaiStruct):
+ SEQ_FIELDS = ["version_raw"]
+ def __init__(self, _io, _parent=None, _root=None, _is_le=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self._is_le = _is_le
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+
+ if self._is_le == True:
+ self._read_le()
+ elif self._is_le == False:
+ self._read_be()
+ else:
+ raise Exception("Unable to decide endianness")
+
+ def _read_le(self):
+ self._debug['version_raw']['start'] = self._io.pos()
+ self.version_raw = self._io.read_u4le()
+ self._debug['version_raw']['end'] = self._io.pos()
+
+ def _read_be(self):
+ self._debug['version_raw']['start'] = self._io.pos()
+ self.version_raw = self._io.read_u4be()
+ self._debug['version_raw']['end'] = self._io.pos()
+
+ @property
+ def major(self):
+ if hasattr(self, '_m_major'):
+ return self._m_major if hasattr(self, '_m_major') else None
+
+ self._m_major = (self.version_raw >> 16)
+ return self._m_major if hasattr(self, '_m_major') else None
+
+ @property
+ def minor(self):
+ if hasattr(self, '_m_minor'):
+ return self._m_minor if hasattr(self, '_m_minor') else None
+
+ self._m_minor = (self.version_raw & 65535)
+ return self._m_minor if hasattr(self, '_m_minor') else None
+
+
+ class HashtableItem(KaitaiStruct):
+ SEQ_FIELDS = ["raw_val"]
+ def __init__(self, _io, _parent=None, _root=None, _is_le=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self._is_le = _is_le
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+
+ if self._is_le == True:
+ self._read_le()
+ elif self._is_le == False:
+ self._read_be()
+ else:
+ raise Exception("Unable to decide endianness")
+
+ def _read_le(self):
+ self._debug['raw_val']['start'] = self._io.pos()
+ self.raw_val = self._io.read_u4le()
+ self._debug['raw_val']['end'] = self._io.pos()
+
+ def _read_be(self):
+ self._debug['raw_val']['start'] = self._io.pos()
+ self.raw_val = self._io.read_u4be()
+ self._debug['raw_val']['end'] = self._io.pos()
+
+ @property
+ def mask(self):
+ if hasattr(self, '_m_mask'):
+ return self._m_mask if hasattr(self, '_m_mask') else None
+
+ self._m_mask = 2147483648
+ return self._m_mask if hasattr(self, '_m_mask') else None
+
+ @property
+ def val_1(self):
+ if hasattr(self, '_m_val_1'):
+ return self._m_val_1 if hasattr(self, '_m_val_1') else None
+
+ if self.raw_val != 0:
+ self._m_val_1 = (self.raw_val - 1)
+
+ return self._m_val_1 if hasattr(self, '_m_val_1') else None
+
+ @property
+ def is_system_dependent(self):
+ if hasattr(self, '_m_is_system_dependent'):
+ return self._m_is_system_dependent if hasattr(self, '_m_is_system_dependent') else None
+
+ if self.raw_val != 0:
+ self._m_is_system_dependent = (self.val_1 & self.mask) == 1
+
+ return self._m_is_system_dependent if hasattr(self, '_m_is_system_dependent') else None
+
+ @property
+ def val(self):
+ if hasattr(self, '_m_val'):
+ return self._m_val if hasattr(self, '_m_val') else None
+
+ if self.raw_val != 0:
+ self._m_val = (self.val_1 & ~(self.mask))
+
+ return self._m_val if hasattr(self, '_m_val') else None
+
+
+ class Descriptor(KaitaiStruct):
+ SEQ_FIELDS = ["len_str", "ofs_str"]
+ def __init__(self, _io, _parent=None, _root=None, _is_le=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self._is_le = _is_le
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+
+ if self._is_le == True:
+ self._read_le()
+ elif self._is_le == False:
+ self._read_be()
+ else:
+ raise Exception("Unable to decide endianness")
+
+ def _read_le(self):
+ self._debug['len_str']['start'] = self._io.pos()
+ self.len_str = self._io.read_u4le()
+ self._debug['len_str']['end'] = self._io.pos()
+ self._debug['ofs_str']['start'] = self._io.pos()
+ self.ofs_str = self._io.read_u4le()
+ self._debug['ofs_str']['end'] = self._io.pos()
+
+ def _read_be(self):
+ self._debug['len_str']['start'] = self._io.pos()
+ self.len_str = self._io.read_u4be()
+ self._debug['len_str']['end'] = self._io.pos()
+ self._debug['ofs_str']['start'] = self._io.pos()
+ self.ofs_str = self._io.read_u4be()
+ self._debug['ofs_str']['end'] = self._io.pos()
+
+ @property
+ def str(self):
+ if hasattr(self, '_m_str'):
+ return self._m_str if hasattr(self, '_m_str') else None
+
+ io = self._root._io
+ _pos = io.pos()
+ io.seek(self.ofs_str)
+ self._debug['_m_str']['start'] = io.pos()
+ if self._is_le:
+ self._m_str = (KaitaiStream.bytes_terminate(io.read_bytes(self.len_str), 0, False)).decode(u"UTF-8")
+ else:
+ self._m_str = (KaitaiStream.bytes_terminate(io.read_bytes(self.len_str), 0, False)).decode(u"UTF-8")
+ self._debug['_m_str']['end'] = io.pos()
+ io.seek(_pos)
+ return self._m_str if hasattr(self, '_m_str') else None
+
+
+ @property
+ def originals(self):
+ if hasattr(self, '_m_originals'):
+ return self._m_originals if hasattr(self, '_m_originals') else None
+
+ io = self._root._io
+ _pos = io.pos()
+ io.seek(self.ofs_originals)
+ self._debug['_m_originals']['start'] = io.pos()
+ if self._is_le:
+ self._m_originals = [None] * (self.num_translations)
+ for i in range(self.num_translations):
+ if not 'arr' in self._debug['_m_originals']:
+ self._debug['_m_originals']['arr'] = []
+ self._debug['_m_originals']['arr'].append({'start': io.pos()})
+ _t__m_originals = self._root.Mo.Descriptor(io, self, self._root, self._is_le)
+ _t__m_originals._read()
+ self._m_originals[i] = _t__m_originals
+ self._debug['_m_originals']['arr'][i]['end'] = io.pos()
+
+ else:
+ self._m_originals = [None] * (self.num_translations)
+ for i in range(self.num_translations):
+ if not 'arr' in self._debug['_m_originals']:
+ self._debug['_m_originals']['arr'] = []
+ self._debug['_m_originals']['arr'].append({'start': io.pos()})
+ _t__m_originals = self._root.Mo.Descriptor(io, self, self._root, self._is_le)
+ _t__m_originals._read()
+ self._m_originals[i] = _t__m_originals
+ self._debug['_m_originals']['arr'][i]['end'] = io.pos()
+
+ self._debug['_m_originals']['end'] = io.pos()
+ io.seek(_pos)
+ return self._m_originals if hasattr(self, '_m_originals') else None
+
+ @property
+ def translations(self):
+ if hasattr(self, '_m_translations'):
+ return self._m_translations if hasattr(self, '_m_translations') else None
+
+ io = self._root._io
+ _pos = io.pos()
+ io.seek(self.ofs_translations)
+ self._debug['_m_translations']['start'] = io.pos()
+ if self._is_le:
+ self._m_translations = [None] * (self.num_translations)
+ for i in range(self.num_translations):
+ if not 'arr' in self._debug['_m_translations']:
+ self._debug['_m_translations']['arr'] = []
+ self._debug['_m_translations']['arr'].append({'start': io.pos()})
+ _t__m_translations = self._root.Mo.Descriptor(io, self, self._root, self._is_le)
+ _t__m_translations._read()
+ self._m_translations[i] = _t__m_translations
+ self._debug['_m_translations']['arr'][i]['end'] = io.pos()
+
+ else:
+ self._m_translations = [None] * (self.num_translations)
+ for i in range(self.num_translations):
+ if not 'arr' in self._debug['_m_translations']:
+ self._debug['_m_translations']['arr'] = []
+ self._debug['_m_translations']['arr'].append({'start': io.pos()})
+ _t__m_translations = self._root.Mo.Descriptor(io, self, self._root, self._is_le)
+ _t__m_translations._read()
+ self._m_translations[i] = _t__m_translations
+ self._debug['_m_translations']['arr'][i]['end'] = io.pos()
+
+ self._debug['_m_translations']['end'] = io.pos()
+ io.seek(_pos)
+ return self._m_translations if hasattr(self, '_m_translations') else None
+
+ @property
+ def hashtable_items(self):
+ if hasattr(self, '_m_hashtable_items'):
+ return self._m_hashtable_items if hasattr(self, '_m_hashtable_items') else None
+
+ if self.ofs_hashtable_items != 0:
+ io = self._root._io
+ _pos = io.pos()
+ io.seek(self.ofs_hashtable_items)
+ self._debug['_m_hashtable_items']['start'] = io.pos()
+ if self._is_le:
+ self._m_hashtable_items = [None] * (self.num_hashtable_items)
+ for i in range(self.num_hashtable_items):
+ if not 'arr' in self._debug['_m_hashtable_items']:
+ self._debug['_m_hashtable_items']['arr'] = []
+ self._debug['_m_hashtable_items']['arr'].append({'start': io.pos()})
+ _t__m_hashtable_items = self._root.Mo.HashtableItem(io, self, self._root, self._is_le)
+ _t__m_hashtable_items._read()
+ self._m_hashtable_items[i] = _t__m_hashtable_items
+ self._debug['_m_hashtable_items']['arr'][i]['end'] = io.pos()
+
+ else:
+ self._m_hashtable_items = [None] * (self.num_hashtable_items)
+ for i in range(self.num_hashtable_items):
+ if not 'arr' in self._debug['_m_hashtable_items']:
+ self._debug['_m_hashtable_items']['arr'] = []
+ self._debug['_m_hashtable_items']['arr'].append({'start': io.pos()})
+ _t__m_hashtable_items = self._root.Mo.HashtableItem(io, self, self._root, self._is_le)
+ _t__m_hashtable_items._read()
+ self._m_hashtable_items[i] = _t__m_hashtable_items
+ self._debug['_m_hashtable_items']['arr'][i]['end'] = io.pos()
+
+ self._debug['_m_hashtable_items']['end'] = io.pos()
+ io.seek(_pos)
+
+ return self._m_hashtable_items if hasattr(self, '_m_hashtable_items') else None
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/sqlite3.py b/python/examples/kaitai/kaitai_struct_formats/database/sqlite3.py
new file mode 100644
index 00000000..94781796
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/sqlite3.py
@@ -0,0 +1,470 @@
+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))
+
+from vlq_base128_be import VlqBase128Be
+class Sqlite3(KaitaiStruct):
+ """SQLite3 is a popular serverless SQL engine, implemented as a library
+ to be used within other applications. It keeps its databases as
+ regular disk files.
+
+ Every database file is segmented into pages. First page (starting at
+ the very beginning) is special: it contains a file-global header
+ which specifies some data relevant to proper parsing (i.e. format
+ versions, size of page, etc). After the header, normal contents of
+ the first page follow.
+
+ Each page would be of some type, and generally, they would be
+ reached via the links starting from the first page. First page type
+ (`root_page`) is always "btree_page".
+
+ .. seealso::
+ Source - https://www.sqlite.org/fileformat.html
+ """
+
+ class Versions(Enum):
+ legacy = 1
+ wal = 2
+
+ class Encodings(Enum):
+ utf_8 = 1
+ utf_16le = 2
+ utf_16be = 3
+ SEQ_FIELDS = ["magic", "len_page_mod", "write_version", "read_version", "reserved_space", "max_payload_frac", "min_payload_frac", "leaf_payload_frac", "file_change_counter", "num_pages", "first_freelist_trunk_page", "num_freelist_pages", "schema_cookie", "schema_format", "def_page_cache_size", "largest_root_page", "text_encoding", "user_version", "is_incremental_vacuum", "application_id", "reserved", "version_valid_for", "sqlite_version_number", "root_page"]
+ 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['magic']['start'] = self._io.pos()
+ self.magic = self._io.ensure_fixed_contents(b"\x53\x51\x4C\x69\x74\x65\x20\x66\x6F\x72\x6D\x61\x74\x20\x33\x00")
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['len_page_mod']['start'] = self._io.pos()
+ self.len_page_mod = self._io.read_u2be()
+ self._debug['len_page_mod']['end'] = self._io.pos()
+ self._debug['write_version']['start'] = self._io.pos()
+ self.write_version = KaitaiStream.resolve_enum(self._root.Versions, self._io.read_u1())
+ self._debug['write_version']['end'] = self._io.pos()
+ self._debug['read_version']['start'] = self._io.pos()
+ self.read_version = KaitaiStream.resolve_enum(self._root.Versions, self._io.read_u1())
+ self._debug['read_version']['end'] = self._io.pos()
+ self._debug['reserved_space']['start'] = self._io.pos()
+ self.reserved_space = self._io.read_u1()
+ self._debug['reserved_space']['end'] = self._io.pos()
+ self._debug['max_payload_frac']['start'] = self._io.pos()
+ self.max_payload_frac = self._io.read_u1()
+ self._debug['max_payload_frac']['end'] = self._io.pos()
+ self._debug['min_payload_frac']['start'] = self._io.pos()
+ self.min_payload_frac = self._io.read_u1()
+ self._debug['min_payload_frac']['end'] = self._io.pos()
+ self._debug['leaf_payload_frac']['start'] = self._io.pos()
+ self.leaf_payload_frac = self._io.read_u1()
+ self._debug['leaf_payload_frac']['end'] = self._io.pos()
+ self._debug['file_change_counter']['start'] = self._io.pos()
+ self.file_change_counter = self._io.read_u4be()
+ self._debug['file_change_counter']['end'] = self._io.pos()
+ self._debug['num_pages']['start'] = self._io.pos()
+ self.num_pages = self._io.read_u4be()
+ self._debug['num_pages']['end'] = self._io.pos()
+ self._debug['first_freelist_trunk_page']['start'] = self._io.pos()
+ self.first_freelist_trunk_page = self._io.read_u4be()
+ self._debug['first_freelist_trunk_page']['end'] = self._io.pos()
+ self._debug['num_freelist_pages']['start'] = self._io.pos()
+ self.num_freelist_pages = self._io.read_u4be()
+ self._debug['num_freelist_pages']['end'] = self._io.pos()
+ self._debug['schema_cookie']['start'] = self._io.pos()
+ self.schema_cookie = self._io.read_u4be()
+ self._debug['schema_cookie']['end'] = self._io.pos()
+ self._debug['schema_format']['start'] = self._io.pos()
+ self.schema_format = self._io.read_u4be()
+ self._debug['schema_format']['end'] = self._io.pos()
+ self._debug['def_page_cache_size']['start'] = self._io.pos()
+ self.def_page_cache_size = self._io.read_u4be()
+ self._debug['def_page_cache_size']['end'] = self._io.pos()
+ self._debug['largest_root_page']['start'] = self._io.pos()
+ self.largest_root_page = self._io.read_u4be()
+ self._debug['largest_root_page']['end'] = self._io.pos()
+ self._debug['text_encoding']['start'] = self._io.pos()
+ self.text_encoding = KaitaiStream.resolve_enum(self._root.Encodings, self._io.read_u4be())
+ self._debug['text_encoding']['end'] = self._io.pos()
+ self._debug['user_version']['start'] = self._io.pos()
+ self.user_version = self._io.read_u4be()
+ self._debug['user_version']['end'] = self._io.pos()
+ self._debug['is_incremental_vacuum']['start'] = self._io.pos()
+ self.is_incremental_vacuum = self._io.read_u4be()
+ self._debug['is_incremental_vacuum']['end'] = self._io.pos()
+ self._debug['application_id']['start'] = self._io.pos()
+ self.application_id = self._io.read_u4be()
+ self._debug['application_id']['end'] = self._io.pos()
+ self._debug['reserved']['start'] = self._io.pos()
+ self.reserved = self._io.read_bytes(20)
+ self._debug['reserved']['end'] = self._io.pos()
+ self._debug['version_valid_for']['start'] = self._io.pos()
+ self.version_valid_for = self._io.read_u4be()
+ self._debug['version_valid_for']['end'] = self._io.pos()
+ self._debug['sqlite_version_number']['start'] = self._io.pos()
+ self.sqlite_version_number = self._io.read_u4be()
+ self._debug['sqlite_version_number']['end'] = self._io.pos()
+ self._debug['root_page']['start'] = self._io.pos()
+ self.root_page = self._root.BtreePage(self._io, self, self._root)
+ self.root_page._read()
+ self._debug['root_page']['end'] = self._io.pos()
+
+ class Serial(KaitaiStruct):
+ SEQ_FIELDS = ["code"]
+ 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 = VlqBase128Be(self._io)
+ self.code._read()
+ self._debug['code']['end'] = self._io.pos()
+
+ @property
+ def is_blob(self):
+ if hasattr(self, '_m_is_blob'):
+ return self._m_is_blob if hasattr(self, '_m_is_blob') else None
+
+ self._m_is_blob = ((self.code.value >= 12) and ((self.code.value % 2) == 0))
+ return self._m_is_blob if hasattr(self, '_m_is_blob') else None
+
+ @property
+ def is_string(self):
+ if hasattr(self, '_m_is_string'):
+ return self._m_is_string if hasattr(self, '_m_is_string') else None
+
+ self._m_is_string = ((self.code.value >= 13) and ((self.code.value % 2) == 1))
+ return self._m_is_string if hasattr(self, '_m_is_string') else None
+
+ @property
+ def len_content(self):
+ if hasattr(self, '_m_len_content'):
+ return self._m_len_content if hasattr(self, '_m_len_content') else None
+
+ if self.code.value >= 12:
+ self._m_len_content = (self.code.value - 12) // 2
+
+ return self._m_len_content if hasattr(self, '_m_len_content') else None
+
+
+ class BtreePage(KaitaiStruct):
+ SEQ_FIELDS = ["page_type", "first_freeblock", "num_cells", "ofs_cells", "num_frag_free_bytes", "right_ptr", "cells"]
+ 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['page_type']['start'] = self._io.pos()
+ self.page_type = self._io.read_u1()
+ self._debug['page_type']['end'] = self._io.pos()
+ self._debug['first_freeblock']['start'] = self._io.pos()
+ self.first_freeblock = self._io.read_u2be()
+ self._debug['first_freeblock']['end'] = self._io.pos()
+ self._debug['num_cells']['start'] = self._io.pos()
+ self.num_cells = self._io.read_u2be()
+ self._debug['num_cells']['end'] = self._io.pos()
+ self._debug['ofs_cells']['start'] = self._io.pos()
+ self.ofs_cells = self._io.read_u2be()
+ self._debug['ofs_cells']['end'] = self._io.pos()
+ self._debug['num_frag_free_bytes']['start'] = self._io.pos()
+ self.num_frag_free_bytes = self._io.read_u1()
+ self._debug['num_frag_free_bytes']['end'] = self._io.pos()
+ if ((self.page_type == 2) or (self.page_type == 5)) :
+ self._debug['right_ptr']['start'] = self._io.pos()
+ self.right_ptr = self._io.read_u4be()
+ self._debug['right_ptr']['end'] = self._io.pos()
+
+ self._debug['cells']['start'] = self._io.pos()
+ self.cells = [None] * (self.num_cells)
+ for i in range(self.num_cells):
+ if not 'arr' in self._debug['cells']:
+ self._debug['cells']['arr'] = []
+ self._debug['cells']['arr'].append({'start': self._io.pos()})
+ _t_cells = self._root.RefCell(self._io, self, self._root)
+ _t_cells._read()
+ self.cells[i] = _t_cells
+ self._debug['cells']['arr'][i]['end'] = self._io.pos()
+
+ self._debug['cells']['end'] = self._io.pos()
+
+
+ class CellIndexLeaf(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://www.sqlite.org/fileformat.html#b_tree_pages
+ """
+ SEQ_FIELDS = ["len_payload", "payload"]
+ 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_payload']['start'] = self._io.pos()
+ self.len_payload = VlqBase128Be(self._io)
+ self.len_payload._read()
+ self._debug['len_payload']['end'] = self._io.pos()
+ self._debug['payload']['start'] = self._io.pos()
+ self._raw_payload = self._io.read_bytes(self.len_payload.value)
+ io = KaitaiStream(BytesIO(self._raw_payload))
+ self.payload = self._root.CellPayload(io, self, self._root)
+ self.payload._read()
+ self._debug['payload']['end'] = self._io.pos()
+
+
+ class Serials(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 = VlqBase128Be(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 CellTableLeaf(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://www.sqlite.org/fileformat.html#b_tree_pages
+ """
+ SEQ_FIELDS = ["len_payload", "row_id", "payload"]
+ 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_payload']['start'] = self._io.pos()
+ self.len_payload = VlqBase128Be(self._io)
+ self.len_payload._read()
+ self._debug['len_payload']['end'] = self._io.pos()
+ self._debug['row_id']['start'] = self._io.pos()
+ self.row_id = VlqBase128Be(self._io)
+ self.row_id._read()
+ self._debug['row_id']['end'] = self._io.pos()
+ self._debug['payload']['start'] = self._io.pos()
+ self._raw_payload = self._io.read_bytes(self.len_payload.value)
+ io = KaitaiStream(BytesIO(self._raw_payload))
+ self.payload = self._root.CellPayload(io, self, self._root)
+ self.payload._read()
+ self._debug['payload']['end'] = self._io.pos()
+
+
+ class CellPayload(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://sqlite.org/fileformat2.html#record_format
+ """
+ SEQ_FIELDS = ["len_header_and_len", "column_serials", "column_contents"]
+ 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_header_and_len']['start'] = self._io.pos()
+ self.len_header_and_len = VlqBase128Be(self._io)
+ self.len_header_and_len._read()
+ self._debug['len_header_and_len']['end'] = self._io.pos()
+ self._debug['column_serials']['start'] = self._io.pos()
+ self._raw_column_serials = self._io.read_bytes((self.len_header_and_len.value - 1))
+ io = KaitaiStream(BytesIO(self._raw_column_serials))
+ self.column_serials = self._root.Serials(io, self, self._root)
+ self.column_serials._read()
+ self._debug['column_serials']['end'] = self._io.pos()
+ self._debug['column_contents']['start'] = self._io.pos()
+ self.column_contents = [None] * (len(self.column_serials.entries))
+ for i in range(len(self.column_serials.entries)):
+ if not 'arr' in self._debug['column_contents']:
+ self._debug['column_contents']['arr'] = []
+ self._debug['column_contents']['arr'].append({'start': self._io.pos()})
+ _t_column_contents = self._root.ColumnContent(self.column_serials.entries[i], self._io, self, self._root)
+ _t_column_contents._read()
+ self.column_contents[i] = _t_column_contents
+ self._debug['column_contents']['arr'][i]['end'] = self._io.pos()
+
+ self._debug['column_contents']['end'] = self._io.pos()
+
+
+ class CellTableInterior(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://www.sqlite.org/fileformat.html#b_tree_pages
+ """
+ SEQ_FIELDS = ["left_child_page", "row_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['left_child_page']['start'] = self._io.pos()
+ self.left_child_page = self._io.read_u4be()
+ self._debug['left_child_page']['end'] = self._io.pos()
+ self._debug['row_id']['start'] = self._io.pos()
+ self.row_id = VlqBase128Be(self._io)
+ self.row_id._read()
+ self._debug['row_id']['end'] = self._io.pos()
+
+
+ class CellIndexInterior(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://www.sqlite.org/fileformat.html#b_tree_pages
+ """
+ SEQ_FIELDS = ["left_child_page", "len_payload", "payload"]
+ 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['left_child_page']['start'] = self._io.pos()
+ self.left_child_page = self._io.read_u4be()
+ self._debug['left_child_page']['end'] = self._io.pos()
+ self._debug['len_payload']['start'] = self._io.pos()
+ self.len_payload = VlqBase128Be(self._io)
+ self.len_payload._read()
+ self._debug['len_payload']['end'] = self._io.pos()
+ self._debug['payload']['start'] = self._io.pos()
+ self._raw_payload = self._io.read_bytes(self.len_payload.value)
+ io = KaitaiStream(BytesIO(self._raw_payload))
+ self.payload = self._root.CellPayload(io, self, self._root)
+ self.payload._read()
+ self._debug['payload']['end'] = self._io.pos()
+
+
+ class ColumnContent(KaitaiStruct):
+ SEQ_FIELDS = ["as_int", "as_float", "as_blob", "as_str"]
+ def __init__(self, ser, _io, _parent=None, _root=None):
+ self._io = _io
+ self._parent = _parent
+ self._root = _root if _root else self
+ self.ser = ser
+ self._debug = collections.defaultdict(dict)
+
+ def _read(self):
+ if ((self.serial_type.code.value >= 1) and (self.serial_type.code.value <= 6)) :
+ self._debug['as_int']['start'] = self._io.pos()
+ _on = self.serial_type.code.value
+ if _on == 4:
+ self.as_int = self._io.read_u4be()
+ elif _on == 6:
+ self.as_int = self._io.read_u8be()
+ elif _on == 1:
+ self.as_int = self._io.read_u1()
+ elif _on == 3:
+ self.as_int = self._io.read_bits_int(24)
+ elif _on == 5:
+ self.as_int = self._io.read_bits_int(48)
+ elif _on == 2:
+ self.as_int = self._io.read_u2be()
+ self._debug['as_int']['end'] = self._io.pos()
+
+ if self.serial_type.code.value == 7:
+ self._debug['as_float']['start'] = self._io.pos()
+ self.as_float = self._io.read_f8be()
+ self._debug['as_float']['end'] = self._io.pos()
+
+ if self.serial_type.is_blob:
+ self._debug['as_blob']['start'] = self._io.pos()
+ self.as_blob = self._io.read_bytes(self.serial_type.len_content)
+ self._debug['as_blob']['end'] = self._io.pos()
+
+ self._debug['as_str']['start'] = self._io.pos()
+ self.as_str = (self._io.read_bytes(self.serial_type.len_content)).decode(u"UTF-8")
+ self._debug['as_str']['end'] = self._io.pos()
+
+ @property
+ def serial_type(self):
+ if hasattr(self, '_m_serial_type'):
+ return self._m_serial_type if hasattr(self, '_m_serial_type') else None
+
+ self._m_serial_type = self.ser
+ return self._m_serial_type if hasattr(self, '_m_serial_type') else None
+
+
+ class RefCell(KaitaiStruct):
+ SEQ_FIELDS = ["ofs_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['ofs_body']['start'] = self._io.pos()
+ self.ofs_body = self._io.read_u2be()
+ self._debug['ofs_body']['end'] = self._io.pos()
+
+ @property
+ def body(self):
+ if hasattr(self, '_m_body'):
+ return self._m_body if hasattr(self, '_m_body') else None
+
+ _pos = self._io.pos()
+ self._io.seek(self.ofs_body)
+ self._debug['_m_body']['start'] = self._io.pos()
+ _on = self._parent.page_type
+ if _on == 13:
+ self._m_body = self._root.CellTableLeaf(self._io, self, self._root)
+ self._m_body._read()
+ elif _on == 5:
+ self._m_body = self._root.CellTableInterior(self._io, self, self._root)
+ self._m_body._read()
+ elif _on == 10:
+ self._m_body = self._root.CellIndexLeaf(self._io, self, self._root)
+ self._m_body._read()
+ elif _on == 2:
+ self._m_body = self._root.CellIndexInterior(self._io, self, self._root)
+ self._m_body._read()
+ self._debug['_m_body']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_body if hasattr(self, '_m_body') else None
+
+
+ @property
+ def len_page(self):
+ if hasattr(self, '_m_len_page'):
+ return self._m_len_page if hasattr(self, '_m_len_page') else None
+
+ self._m_len_page = (65536 if self.len_page_mod == 1 else self.len_page_mod)
+ return self._m_len_page if hasattr(self, '_m_len_page') else None
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/tsm.py b/python/examples/kaitai/kaitai_struct_formats/database/tsm.py
new file mode 100644
index 00000000..48000456
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/tsm.py
@@ -0,0 +1,196 @@
+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 Tsm(KaitaiStruct):
+ """InfluxDB is a scalable database optimized for storage of time
+ series, real-time application metrics, operations monitoring events,
+ etc, written in Go.
+
+ Data is stored in .tsm files, which are kept pretty simple
+ conceptually. Each .tsm file contains a header and footer, which
+ stores offset to an index. Index is used to find a data block for a
+ requested time boundary.
+ """
+ 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.Header(self._io, self, self._root)
+ self.header._read()
+ self._debug['header']['end'] = self._io.pos()
+
+ class Header(KaitaiStruct):
+ SEQ_FIELDS = ["magic", "version"]
+ 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['magic']['start'] = self._io.pos()
+ self.magic = self._io.ensure_fixed_contents(b"\x16\xD1\x16\xD1")
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['version']['start'] = self._io.pos()
+ self.version = self._io.read_u1()
+ self._debug['version']['end'] = self._io.pos()
+
+
+ class Index(KaitaiStruct):
+ SEQ_FIELDS = ["offset"]
+ 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['offset']['start'] = self._io.pos()
+ self.offset = self._io.read_u8be()
+ self._debug['offset']['end'] = self._io.pos()
+
+ class IndexHeader(KaitaiStruct):
+ SEQ_FIELDS = ["key_len", "key", "type", "entry_count", "index_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['key_len']['start'] = self._io.pos()
+ self.key_len = self._io.read_u2be()
+ self._debug['key_len']['end'] = self._io.pos()
+ self._debug['key']['start'] = self._io.pos()
+ self.key = (self._io.read_bytes(self.key_len)).decode(u"UTF-8")
+ self._debug['key']['end'] = self._io.pos()
+ self._debug['type']['start'] = self._io.pos()
+ self.type = self._io.read_u1()
+ self._debug['type']['end'] = self._io.pos()
+ self._debug['entry_count']['start'] = self._io.pos()
+ self.entry_count = self._io.read_u2be()
+ self._debug['entry_count']['end'] = self._io.pos()
+ self._debug['index_entries']['start'] = self._io.pos()
+ self.index_entries = [None] * (self.entry_count)
+ for i in range(self.entry_count):
+ if not 'arr' in self._debug['index_entries']:
+ self._debug['index_entries']['arr'] = []
+ self._debug['index_entries']['arr'].append({'start': self._io.pos()})
+ _t_index_entries = self._root.Index.IndexHeader.IndexEntry(self._io, self, self._root)
+ _t_index_entries._read()
+ self.index_entries[i] = _t_index_entries
+ self._debug['index_entries']['arr'][i]['end'] = self._io.pos()
+
+ self._debug['index_entries']['end'] = self._io.pos()
+
+ class IndexEntry(KaitaiStruct):
+ SEQ_FIELDS = ["min_time", "max_time", "block_offset", "block_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['min_time']['start'] = self._io.pos()
+ self.min_time = self._io.read_u8be()
+ self._debug['min_time']['end'] = self._io.pos()
+ self._debug['max_time']['start'] = self._io.pos()
+ self.max_time = self._io.read_u8be()
+ self._debug['max_time']['end'] = self._io.pos()
+ self._debug['block_offset']['start'] = self._io.pos()
+ self.block_offset = self._io.read_u8be()
+ self._debug['block_offset']['end'] = self._io.pos()
+ self._debug['block_size']['start'] = self._io.pos()
+ self.block_size = self._io.read_u4be()
+ self._debug['block_size']['end'] = self._io.pos()
+
+ class BlockEntry(KaitaiStruct):
+ SEQ_FIELDS = ["crc32", "data"]
+ 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['crc32']['start'] = self._io.pos()
+ self.crc32 = self._io.read_u4be()
+ self._debug['crc32']['end'] = self._io.pos()
+ self._debug['data']['start'] = self._io.pos()
+ self.data = self._io.read_bytes((self._parent.block_size - 4))
+ self._debug['data']['end'] = self._io.pos()
+
+
+ @property
+ def block(self):
+ if hasattr(self, '_m_block'):
+ return self._m_block if hasattr(self, '_m_block') else None
+
+ io = self._root._io
+ _pos = io.pos()
+ io.seek(self.block_offset)
+ self._debug['_m_block']['start'] = io.pos()
+ self._m_block = self._root.Index.IndexHeader.IndexEntry.BlockEntry(io, self, self._root)
+ self._m_block._read()
+ self._debug['_m_block']['end'] = io.pos()
+ io.seek(_pos)
+ return self._m_block if hasattr(self, '_m_block') else None
+
+
+
+ @property
+ def entries(self):
+ if hasattr(self, '_m_entries'):
+ return self._m_entries if hasattr(self, '_m_entries') else None
+
+ _pos = self._io.pos()
+ self._io.seek(self.offset)
+ self._debug['_m_entries']['start'] = self._io.pos()
+ self._m_entries = []
+ i = 0
+ while True:
+ if not 'arr' in self._debug['_m_entries']:
+ self._debug['_m_entries']['arr'] = []
+ self._debug['_m_entries']['arr'].append({'start': self._io.pos()})
+ _t__m_entries = self._root.Index.IndexHeader(self._io, self, self._root)
+ _t__m_entries._read()
+ _ = _t__m_entries
+ self._m_entries.append(_)
+ self._debug['_m_entries']['arr'][len(self._m_entries) - 1]['end'] = self._io.pos()
+ if self._io.pos() == (self._io.size() - 8):
+ break
+ i += 1
+ self._debug['_m_entries']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_entries if hasattr(self, '_m_entries') else None
+
+
+ @property
+ def index(self):
+ if hasattr(self, '_m_index'):
+ return self._m_index if hasattr(self, '_m_index') else None
+
+ _pos = self._io.pos()
+ self._io.seek((self._io.size() - 8))
+ self._debug['_m_index']['start'] = self._io.pos()
+ self._m_index = self._root.Index(self._io, self, self._root)
+ self._m_index._read()
+ self._debug['_m_index']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_index if hasattr(self, '_m_index') else None
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/database/vlq_base128_be.py b/python/examples/kaitai/kaitai_struct_formats/database/vlq_base128_be.py
new file mode 100644
index 00000000..826cdd95
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/database/vlq_base128_be.py
@@ -0,0 +1,102 @@
+# 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
+
+