summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/kaitai_struct_formats/archive
diff options
context:
space:
mode:
Diffstat (limited to 'python/examples/kaitai/kaitai_struct_formats/archive')
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/__init__.py0
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/cpio_old_le.py146
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/gzip.py230
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/lzh.py161
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/rar.py323
-rw-r--r--python/examples/kaitai/kaitai_struct_formats/archive/zip.py503
6 files changed, 1363 insertions, 0 deletions
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/__init__.py b/python/examples/kaitai/kaitai_struct_formats/archive/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/__init__.py
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/cpio_old_le.py b/python/examples/kaitai/kaitai_struct_formats/archive/cpio_old_le.py
new file mode 100644
index 00000000..2b019d86
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/cpio_old_le.py
@@ -0,0 +1,146 @@
+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 CpioOldLe(KaitaiStruct):
+ SEQ_FIELDS = ["files"]
+ 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['files']['start'] = self._io.pos()
+ self.files = []
+ i = 0
+ while not self._io.is_eof():
+ if not 'arr' in self._debug['files']:
+ self._debug['files']['arr'] = []
+ self._debug['files']['arr'].append({'start': self._io.pos()})
+ _t_files = self._root.File(self._io, self, self._root)
+ _t_files._read()
+ self.files.append(_t_files)
+ self._debug['files']['arr'][len(self.files) - 1]['end'] = self._io.pos()
+ i += 1
+
+ self._debug['files']['end'] = self._io.pos()
+
+ class File(KaitaiStruct):
+ SEQ_FIELDS = ["header", "path_name", "string_terminator", "path_name_padding", "file_data", "file_data_padding", "end_of_file_padding"]
+ 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.FileHeader(self._io, self, self._root)
+ self.header._read()
+ self._debug['header']['end'] = self._io.pos()
+ self._debug['path_name']['start'] = self._io.pos()
+ self.path_name = self._io.read_bytes((self.header.path_name_size - 1))
+ self._debug['path_name']['end'] = self._io.pos()
+ self._debug['string_terminator']['start'] = self._io.pos()
+ self.string_terminator = self._io.ensure_fixed_contents(b"\x00")
+ self._debug['string_terminator']['end'] = self._io.pos()
+ if (self.header.path_name_size % 2) == 1:
+ self._debug['path_name_padding']['start'] = self._io.pos()
+ self.path_name_padding = self._io.ensure_fixed_contents(b"\x00")
+ self._debug['path_name_padding']['end'] = self._io.pos()
+
+ self._debug['file_data']['start'] = self._io.pos()
+ self.file_data = self._io.read_bytes(self.header.file_size.value)
+ self._debug['file_data']['end'] = self._io.pos()
+ if (self.header.file_size.value % 2) == 1:
+ self._debug['file_data_padding']['start'] = self._io.pos()
+ self.file_data_padding = self._io.ensure_fixed_contents(b"\x00")
+ self._debug['file_data_padding']['end'] = self._io.pos()
+
+ if ((self.path_name == b"\x54\x52\x41\x49\x4C\x45\x52\x21\x21\x21") and (self.header.file_size.value == 0)) :
+ self._debug['end_of_file_padding']['start'] = self._io.pos()
+ self.end_of_file_padding = self._io.read_bytes_full()
+ self._debug['end_of_file_padding']['end'] = self._io.pos()
+
+
+
+ class FileHeader(KaitaiStruct):
+ SEQ_FIELDS = ["magic", "device_number", "inode_number", "mode", "user_id", "group_id", "number_of_links", "r_device_number", "modification_time", "path_name_size", "file_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['magic']['start'] = self._io.pos()
+ self.magic = self._io.ensure_fixed_contents(b"\xC7\x71")
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['device_number']['start'] = self._io.pos()
+ self.device_number = self._io.read_u2le()
+ self._debug['device_number']['end'] = self._io.pos()
+ self._debug['inode_number']['start'] = self._io.pos()
+ self.inode_number = self._io.read_u2le()
+ self._debug['inode_number']['end'] = self._io.pos()
+ self._debug['mode']['start'] = self._io.pos()
+ self.mode = self._io.read_u2le()
+ self._debug['mode']['end'] = self._io.pos()
+ self._debug['user_id']['start'] = self._io.pos()
+ self.user_id = self._io.read_u2le()
+ self._debug['user_id']['end'] = self._io.pos()
+ self._debug['group_id']['start'] = self._io.pos()
+ self.group_id = self._io.read_u2le()
+ self._debug['group_id']['end'] = self._io.pos()
+ self._debug['number_of_links']['start'] = self._io.pos()
+ self.number_of_links = self._io.read_u2le()
+ self._debug['number_of_links']['end'] = self._io.pos()
+ self._debug['r_device_number']['start'] = self._io.pos()
+ self.r_device_number = self._io.read_u2le()
+ self._debug['r_device_number']['end'] = self._io.pos()
+ self._debug['modification_time']['start'] = self._io.pos()
+ self.modification_time = self._root.FourByteUnsignedInteger(self._io, self, self._root)
+ self.modification_time._read()
+ self._debug['modification_time']['end'] = self._io.pos()
+ self._debug['path_name_size']['start'] = self._io.pos()
+ self.path_name_size = self._io.read_u2le()
+ self._debug['path_name_size']['end'] = self._io.pos()
+ self._debug['file_size']['start'] = self._io.pos()
+ self.file_size = self._root.FourByteUnsignedInteger(self._io, self, self._root)
+ self.file_size._read()
+ self._debug['file_size']['end'] = self._io.pos()
+
+
+ class FourByteUnsignedInteger(KaitaiStruct):
+ SEQ_FIELDS = ["most_significant_bits", "least_significant_bits"]
+ 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['most_significant_bits']['start'] = self._io.pos()
+ self.most_significant_bits = self._io.read_u2le()
+ self._debug['most_significant_bits']['end'] = self._io.pos()
+ self._debug['least_significant_bits']['start'] = self._io.pos()
+ self.least_significant_bits = self._io.read_u2le()
+ self._debug['least_significant_bits']['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.least_significant_bits + (self.most_significant_bits << 16))
+ return self._m_value if hasattr(self, '_m_value') else None
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/gzip.py b/python/examples/kaitai/kaitai_struct_formats/archive/gzip.py
new file mode 100644
index 00000000..b233cff2
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/gzip.py
@@ -0,0 +1,230 @@
+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 Gzip(KaitaiStruct):
+ """Gzip is a popular and standard single-file archiving format. It
+ essentially provides a container that stores original file name,
+ timestamp and a few other things (like optional comment), basic
+ CRCs, etc, and a file compressed by a chosen compression algorithm.
+
+ As of 2019, there is actually only one working solution for
+ compression algorithms, so it's typically raw DEFLATE stream
+ (without zlib header) in all gzipped files.
+
+ .. seealso::
+ Source - https://tools.ietf.org/html/rfc1952
+ """
+
+ class CompressionMethods(Enum):
+ deflate = 8
+
+ class Oses(Enum):
+ fat = 0
+ amiga = 1
+ vms = 2
+ unix = 3
+ vm_cms = 4
+ atari_tos = 5
+ hpfs = 6
+ macintosh = 7
+ z_system = 8
+ cp_m = 9
+ tops_20 = 10
+ ntfs = 11
+ qdos = 12
+ acorn_riscos = 13
+ unknown = 255
+ SEQ_FIELDS = ["magic", "compression_method", "flags", "mod_time", "extra_flags", "os", "extras", "name", "comment", "header_crc16", "body", "body_crc32", "len_uncompressed"]
+ 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"\x1F\x8B")
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['compression_method']['start'] = self._io.pos()
+ self.compression_method = KaitaiStream.resolve_enum(self._root.CompressionMethods, self._io.read_u1())
+ self._debug['compression_method']['end'] = self._io.pos()
+ self._debug['flags']['start'] = self._io.pos()
+ self.flags = self._root.Flags(self._io, self, self._root)
+ self.flags._read()
+ self._debug['flags']['end'] = self._io.pos()
+ self._debug['mod_time']['start'] = self._io.pos()
+ self.mod_time = self._io.read_u4le()
+ self._debug['mod_time']['end'] = self._io.pos()
+ self._debug['extra_flags']['start'] = self._io.pos()
+ _on = self.compression_method
+ if _on == self._root.CompressionMethods.deflate:
+ self.extra_flags = self._root.ExtraFlagsDeflate(self._io, self, self._root)
+ self.extra_flags._read()
+ self._debug['extra_flags']['end'] = self._io.pos()
+ self._debug['os']['start'] = self._io.pos()
+ self.os = KaitaiStream.resolve_enum(self._root.Oses, self._io.read_u1())
+ self._debug['os']['end'] = self._io.pos()
+ if self.flags.has_extra:
+ self._debug['extras']['start'] = self._io.pos()
+ self.extras = self._root.Extras(self._io, self, self._root)
+ self.extras._read()
+ self._debug['extras']['end'] = self._io.pos()
+
+ if self.flags.has_name:
+ self._debug['name']['start'] = self._io.pos()
+ self.name = self._io.read_bytes_term(0, False, True, True)
+ self._debug['name']['end'] = self._io.pos()
+
+ if self.flags.has_comment:
+ self._debug['comment']['start'] = self._io.pos()
+ self.comment = self._io.read_bytes_term(0, False, True, True)
+ self._debug['comment']['end'] = self._io.pos()
+
+ if self.flags.has_header_crc:
+ self._debug['header_crc16']['start'] = self._io.pos()
+ self.header_crc16 = self._io.read_u2le()
+ self._debug['header_crc16']['end'] = self._io.pos()
+
+ self._debug['body']['start'] = self._io.pos()
+ self.body = self._io.read_bytes(((self._io.size() - self._io.pos()) - 8))
+ self._debug['body']['end'] = self._io.pos()
+ self._debug['body_crc32']['start'] = self._io.pos()
+ self.body_crc32 = self._io.read_u4le()
+ self._debug['body_crc32']['end'] = self._io.pos()
+ self._debug['len_uncompressed']['start'] = self._io.pos()
+ self.len_uncompressed = self._io.read_u4le()
+ self._debug['len_uncompressed']['end'] = self._io.pos()
+
+ class Flags(KaitaiStruct):
+ SEQ_FIELDS = ["reserved1", "has_comment", "has_name", "has_extra", "has_header_crc", "is_text"]
+ 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_bits_int(3)
+ self._debug['reserved1']['end'] = self._io.pos()
+ self._debug['has_comment']['start'] = self._io.pos()
+ self.has_comment = self._io.read_bits_int(1) != 0
+ self._debug['has_comment']['end'] = self._io.pos()
+ self._debug['has_name']['start'] = self._io.pos()
+ self.has_name = self._io.read_bits_int(1) != 0
+ self._debug['has_name']['end'] = self._io.pos()
+ self._debug['has_extra']['start'] = self._io.pos()
+ self.has_extra = self._io.read_bits_int(1) != 0
+ self._debug['has_extra']['end'] = self._io.pos()
+ self._debug['has_header_crc']['start'] = self._io.pos()
+ self.has_header_crc = self._io.read_bits_int(1) != 0
+ self._debug['has_header_crc']['end'] = self._io.pos()
+ self._debug['is_text']['start'] = self._io.pos()
+ self.is_text = self._io.read_bits_int(1) != 0
+ self._debug['is_text']['end'] = self._io.pos()
+
+
+ class ExtraFlagsDeflate(KaitaiStruct):
+
+ class CompressionStrengths(Enum):
+ best = 2
+ fast = 4
+ SEQ_FIELDS = ["compression_strength"]
+ 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['compression_strength']['start'] = self._io.pos()
+ self.compression_strength = KaitaiStream.resolve_enum(self._root.ExtraFlagsDeflate.CompressionStrengths, self._io.read_u1())
+ self._debug['compression_strength']['end'] = self._io.pos()
+
+
+ class Subfields(KaitaiStruct):
+ """Container for many subfields, constrained by size of stream.
+ """
+ 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 = self._root.Subfield(self._io, self, self._root)
+ _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 Subfield(KaitaiStruct):
+ """Every subfield follows typical [TLV scheme](https://en.wikipedia.org/wiki/Type-length-value):
+
+ * `id` serves role of "T"ype
+ * `len_data` serves role of "L"ength
+ * `data` serves role of "V"alue
+
+ This way it's possible to for arbitrary parser to skip over
+ subfields it does not support.
+ """
+ SEQ_FIELDS = ["id", "len_data", "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['id']['start'] = self._io.pos()
+ self.id = self._io.read_u2le()
+ self._debug['id']['end'] = self._io.pos()
+ self._debug['len_data']['start'] = self._io.pos()
+ self.len_data = self._io.read_u2le()
+ self._debug['len_data']['end'] = self._io.pos()
+ self._debug['data']['start'] = self._io.pos()
+ self.data = self._io.read_bytes(self.len_data)
+ self._debug['data']['end'] = self._io.pos()
+
+
+ class Extras(KaitaiStruct):
+ SEQ_FIELDS = ["len_subfields", "subfields"]
+ 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_subfields']['start'] = self._io.pos()
+ self.len_subfields = self._io.read_u2le()
+ self._debug['len_subfields']['end'] = self._io.pos()
+ self._debug['subfields']['start'] = self._io.pos()
+ self._raw_subfields = self._io.read_bytes(self.len_subfields)
+ io = KaitaiStream(BytesIO(self._raw_subfields))
+ self.subfields = self._root.Subfields(io, self, self._root)
+ self.subfields._read()
+ self._debug['subfields']['end'] = self._io.pos()
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/lzh.py b/python/examples/kaitai/kaitai_struct_formats/archive/lzh.py
new file mode 100644
index 00000000..8f9b75ea
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/lzh.py
@@ -0,0 +1,161 @@
+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 Lzh(KaitaiStruct):
+ """LHA (LHarc, LZH) is a file format used by a popular freeware
+ eponymous archiver, created in 1988 by Haruyasu Yoshizaki. Over the
+ years, many ports and implementations were developed, sporting many
+ extensions to original 1988 LZH.
+
+ File format is pretty simple and essentially consists of a stream of
+ records.
+ """
+ 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 = self._root.Record(self._io, self, self._root)
+ _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 Record(KaitaiStruct):
+ SEQ_FIELDS = ["header_len", "file_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['header_len']['start'] = self._io.pos()
+ self.header_len = self._io.read_u1()
+ self._debug['header_len']['end'] = self._io.pos()
+ if self.header_len > 0:
+ self._debug['file_record']['start'] = self._io.pos()
+ self.file_record = self._root.FileRecord(self._io, self, self._root)
+ self.file_record._read()
+ self._debug['file_record']['end'] = self._io.pos()
+
+
+
+ class FileRecord(KaitaiStruct):
+ SEQ_FIELDS = ["header", "file_uncompr_crc16", "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['header']['start'] = self._io.pos()
+ self._raw_header = self._io.read_bytes((self._parent.header_len - 1))
+ io = KaitaiStream(BytesIO(self._raw_header))
+ self.header = self._root.Header(io, self, self._root)
+ self.header._read()
+ self._debug['header']['end'] = self._io.pos()
+ if self.header.header1.lha_level == 0:
+ self._debug['file_uncompr_crc16']['start'] = self._io.pos()
+ self.file_uncompr_crc16 = self._io.read_u2le()
+ self._debug['file_uncompr_crc16']['end'] = self._io.pos()
+
+ self._debug['body']['start'] = self._io.pos()
+ self.body = self._io.read_bytes(self.header.header1.file_size_compr)
+ self._debug['body']['end'] = self._io.pos()
+
+
+ class Header(KaitaiStruct):
+ SEQ_FIELDS = ["header1", "filename_len", "filename", "file_uncompr_crc16", "os", "ext_header_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['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()
+ if self.header1.lha_level == 0:
+ self._debug['filename_len']['start'] = self._io.pos()
+ self.filename_len = self._io.read_u1()
+ self._debug['filename_len']['end'] = self._io.pos()
+
+ if self.header1.lha_level == 0:
+ self._debug['filename']['start'] = self._io.pos()
+ self.filename = (self._io.read_bytes(self.filename_len)).decode(u"ASCII")
+ self._debug['filename']['end'] = self._io.pos()
+
+ if self.header1.lha_level == 2:
+ self._debug['file_uncompr_crc16']['start'] = self._io.pos()
+ self.file_uncompr_crc16 = self._io.read_u2le()
+ self._debug['file_uncompr_crc16']['end'] = self._io.pos()
+
+ if self.header1.lha_level == 2:
+ self._debug['os']['start'] = self._io.pos()
+ self.os = self._io.read_u1()
+ self._debug['os']['end'] = self._io.pos()
+
+ if self.header1.lha_level == 2:
+ self._debug['ext_header_size']['start'] = self._io.pos()
+ self.ext_header_size = self._io.read_u2le()
+ self._debug['ext_header_size']['end'] = self._io.pos()
+
+
+
+ class Header1(KaitaiStruct):
+ SEQ_FIELDS = ["header_checksum", "method_id", "file_size_compr", "file_size_uncompr", "file_timestamp", "attr", "lha_level"]
+ 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_checksum']['start'] = self._io.pos()
+ self.header_checksum = self._io.read_u1()
+ self._debug['header_checksum']['end'] = self._io.pos()
+ self._debug['method_id']['start'] = self._io.pos()
+ self.method_id = (self._io.read_bytes(5)).decode(u"ASCII")
+ self._debug['method_id']['end'] = self._io.pos()
+ self._debug['file_size_compr']['start'] = self._io.pos()
+ self.file_size_compr = self._io.read_u4le()
+ self._debug['file_size_compr']['end'] = self._io.pos()
+ self._debug['file_size_uncompr']['start'] = self._io.pos()
+ self.file_size_uncompr = self._io.read_u4le()
+ self._debug['file_size_uncompr']['end'] = self._io.pos()
+ self._debug['file_timestamp']['start'] = self._io.pos()
+ self.file_timestamp = self._io.read_u4le()
+ self._debug['file_timestamp']['end'] = self._io.pos()
+ self._debug['attr']['start'] = self._io.pos()
+ self.attr = self._io.read_u1()
+ self._debug['attr']['end'] = self._io.pos()
+ self._debug['lha_level']['start'] = self._io.pos()
+ self.lha_level = self._io.read_u1()
+ self._debug['lha_level']['end'] = self._io.pos()
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/rar.py b/python/examples/kaitai/kaitai_struct_formats/archive/rar.py
new file mode 100644
index 00000000..e9f4787e
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/rar.py
@@ -0,0 +1,323 @@
+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 Rar(KaitaiStruct):
+ """RAR is a archive format used by popular proprietary RAR archiver,
+ created by Eugene Roshal. There are two major versions of format
+ (v1.5-4.0 and RAR v5+).
+
+ File format essentially consists of a linear sequence of
+ blocks. Each block has fixed header and custom body (that depends on
+ block type), so it's possible to skip block even if one doesn't know
+ how to process a certain block type.
+
+ .. seealso::
+ - http://acritum.com/winrar/rar-format
+ """
+
+ class BlockTypes(Enum):
+ marker = 114
+ archive_header = 115
+ file_header = 116
+ old_style_comment_header = 117
+ old_style_authenticity_info_76 = 118
+ old_style_subblock = 119
+ old_style_recovery_record = 120
+ old_style_authenticity_info_79 = 121
+ subblock = 122
+ terminator = 123
+
+ class Oses(Enum):
+ ms_dos = 0
+ os_2 = 1
+ windows = 2
+ unix = 3
+ mac_os = 4
+ beos = 5
+
+ class Methods(Enum):
+ store = 48
+ fastest = 49
+ fast = 50
+ normal = 51
+ good = 52
+ best = 53
+ SEQ_FIELDS = ["magic", "blocks"]
+ 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._root.MagicSignature(self._io, self, self._root)
+ self.magic._read()
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['blocks']['start'] = self._io.pos()
+ self.blocks = []
+ i = 0
+ while not self._io.is_eof():
+ if not 'arr' in self._debug['blocks']:
+ self._debug['blocks']['arr'] = []
+ self._debug['blocks']['arr'].append({'start': self._io.pos()})
+ _on = self.magic.version
+ if _on == 0:
+ if not 'arr' in self._debug['blocks']:
+ self._debug['blocks']['arr'] = []
+ self._debug['blocks']['arr'].append({'start': self._io.pos()})
+ _t_blocks = self._root.Block(self._io, self, self._root)
+ _t_blocks._read()
+ self.blocks.append(_t_blocks)
+ self._debug['blocks']['arr'][len(self.blocks) - 1]['end'] = self._io.pos()
+ elif _on == 1:
+ if not 'arr' in self._debug['blocks']:
+ self._debug['blocks']['arr'] = []
+ self._debug['blocks']['arr'].append({'start': self._io.pos()})
+ _t_blocks = self._root.BlockV5(self._io, self, self._root)
+ _t_blocks._read()
+ self.blocks.append(_t_blocks)
+ self._debug['blocks']['arr'][len(self.blocks) - 1]['end'] = self._io.pos()
+ self._debug['blocks']['arr'][len(self.blocks) - 1]['end'] = self._io.pos()
+ i += 1
+
+ self._debug['blocks']['end'] = self._io.pos()
+
+ class BlockV5(KaitaiStruct):
+ SEQ_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):
+ pass
+
+
+ class Block(KaitaiStruct):
+ """Basic block that RAR files consist of. There are several block
+ types (see `block_type`), which have different `body` and
+ `add_body`.
+ """
+ SEQ_FIELDS = ["crc16", "block_type", "flags", "block_size", "add_size", "body", "add_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['crc16']['start'] = self._io.pos()
+ self.crc16 = self._io.read_u2le()
+ self._debug['crc16']['end'] = self._io.pos()
+ self._debug['block_type']['start'] = self._io.pos()
+ self.block_type = KaitaiStream.resolve_enum(self._root.BlockTypes, self._io.read_u1())
+ self._debug['block_type']['end'] = self._io.pos()
+ self._debug['flags']['start'] = self._io.pos()
+ self.flags = self._io.read_u2le()
+ self._debug['flags']['end'] = self._io.pos()
+ self._debug['block_size']['start'] = self._io.pos()
+ self.block_size = self._io.read_u2le()
+ self._debug['block_size']['end'] = self._io.pos()
+ if self.has_add:
+ self._debug['add_size']['start'] = self._io.pos()
+ self.add_size = self._io.read_u4le()
+ self._debug['add_size']['end'] = self._io.pos()
+
+ self._debug['body']['start'] = self._io.pos()
+ _on = self.block_type
+ if _on == self._root.BlockTypes.file_header:
+ self._raw_body = self._io.read_bytes(self.body_size)
+ io = KaitaiStream(BytesIO(self._raw_body))
+ self.body = self._root.BlockFileHeader(io, self, self._root)
+ self.body._read()
+ else:
+ self.body = self._io.read_bytes(self.body_size)
+ self._debug['body']['end'] = self._io.pos()
+ if self.has_add:
+ self._debug['add_body']['start'] = self._io.pos()
+ self.add_body = self._io.read_bytes(self.add_size)
+ self._debug['add_body']['end'] = self._io.pos()
+
+
+ @property
+ def has_add(self):
+ """True if block has additional content attached to it."""
+ if hasattr(self, '_m_has_add'):
+ return self._m_has_add if hasattr(self, '_m_has_add') else None
+
+ self._m_has_add = (self.flags & 32768) != 0
+ return self._m_has_add if hasattr(self, '_m_has_add') else None
+
+ @property
+ def header_size(self):
+ if hasattr(self, '_m_header_size'):
+ return self._m_header_size if hasattr(self, '_m_header_size') else None
+
+ self._m_header_size = (11 if self.has_add else 7)
+ return self._m_header_size if hasattr(self, '_m_header_size') else None
+
+ @property
+ def body_size(self):
+ if hasattr(self, '_m_body_size'):
+ return self._m_body_size if hasattr(self, '_m_body_size') else None
+
+ self._m_body_size = (self.block_size - self.header_size)
+ return self._m_body_size if hasattr(self, '_m_body_size') else None
+
+
+ class BlockFileHeader(KaitaiStruct):
+ SEQ_FIELDS = ["low_unp_size", "host_os", "file_crc32", "file_time", "rar_version", "method", "name_size", "attr", "high_pack_size", "file_name", "salt"]
+ 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['low_unp_size']['start'] = self._io.pos()
+ self.low_unp_size = self._io.read_u4le()
+ self._debug['low_unp_size']['end'] = self._io.pos()
+ self._debug['host_os']['start'] = self._io.pos()
+ self.host_os = KaitaiStream.resolve_enum(self._root.Oses, self._io.read_u1())
+ self._debug['host_os']['end'] = self._io.pos()
+ self._debug['file_crc32']['start'] = self._io.pos()
+ self.file_crc32 = self._io.read_u4le()
+ self._debug['file_crc32']['end'] = self._io.pos()
+ self._debug['file_time']['start'] = self._io.pos()
+ self.file_time = self._root.DosTime(self._io, self, self._root)
+ self.file_time._read()
+ self._debug['file_time']['end'] = self._io.pos()
+ self._debug['rar_version']['start'] = self._io.pos()
+ self.rar_version = self._io.read_u1()
+ self._debug['rar_version']['end'] = self._io.pos()
+ self._debug['method']['start'] = self._io.pos()
+ self.method = KaitaiStream.resolve_enum(self._root.Methods, self._io.read_u1())
+ self._debug['method']['end'] = self._io.pos()
+ self._debug['name_size']['start'] = self._io.pos()
+ self.name_size = self._io.read_u2le()
+ self._debug['name_size']['end'] = self._io.pos()
+ self._debug['attr']['start'] = self._io.pos()
+ self.attr = self._io.read_u4le()
+ self._debug['attr']['end'] = self._io.pos()
+ if (self._parent.flags & 256) != 0:
+ self._debug['high_pack_size']['start'] = self._io.pos()
+ self.high_pack_size = self._io.read_u4le()
+ self._debug['high_pack_size']['end'] = self._io.pos()
+
+ self._debug['file_name']['start'] = self._io.pos()
+ self.file_name = self._io.read_bytes(self.name_size)
+ self._debug['file_name']['end'] = self._io.pos()
+ if (self._parent.flags & 1024) != 0:
+ self._debug['salt']['start'] = self._io.pos()
+ self.salt = self._io.read_u8le()
+ self._debug['salt']['end'] = self._io.pos()
+
+
+
+ class MagicSignature(KaitaiStruct):
+ """RAR uses either 7-byte magic for RAR versions 1.5 to 4.0, and
+ 8-byte magic (and pretty different block format) for v5+. This
+ type would parse and validate both versions of signature. Note
+ that actually this signature is a valid RAR "block": in theory,
+ one can omit signature reading at all, and read this normally,
+ as a block, if exact RAR version is known (and thus it's
+ possible to choose correct block format).
+ """
+ SEQ_FIELDS = ["magic1", "version", "magic3"]
+ 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['magic1']['start'] = self._io.pos()
+ self.magic1 = self._io.ensure_fixed_contents(b"\x52\x61\x72\x21\x1A\x07")
+ self._debug['magic1']['end'] = self._io.pos()
+ self._debug['version']['start'] = self._io.pos()
+ self.version = self._io.read_u1()
+ self._debug['version']['end'] = self._io.pos()
+ if self.version == 1:
+ self._debug['magic3']['start'] = self._io.pos()
+ self.magic3 = self._io.ensure_fixed_contents(b"\x00")
+ self._debug['magic3']['end'] = self._io.pos()
+
+
+
+ class DosTime(KaitaiStruct):
+ SEQ_FIELDS = ["time", "date"]
+ 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['time']['start'] = self._io.pos()
+ self.time = self._io.read_u2le()
+ self._debug['time']['end'] = self._io.pos()
+ self._debug['date']['start'] = self._io.pos()
+ self.date = self._io.read_u2le()
+ self._debug['date']['end'] = self._io.pos()
+
+ @property
+ def month(self):
+ if hasattr(self, '_m_month'):
+ return self._m_month if hasattr(self, '_m_month') else None
+
+ self._m_month = ((self.date & 480) >> 5)
+ return self._m_month if hasattr(self, '_m_month') else None
+
+ @property
+ def seconds(self):
+ if hasattr(self, '_m_seconds'):
+ return self._m_seconds if hasattr(self, '_m_seconds') else None
+
+ self._m_seconds = ((self.time & 31) * 2)
+ return self._m_seconds if hasattr(self, '_m_seconds') else None
+
+ @property
+ def year(self):
+ if hasattr(self, '_m_year'):
+ return self._m_year if hasattr(self, '_m_year') else None
+
+ self._m_year = (((self.date & 65024) >> 9) + 1980)
+ return self._m_year if hasattr(self, '_m_year') else None
+
+ @property
+ def minutes(self):
+ if hasattr(self, '_m_minutes'):
+ return self._m_minutes if hasattr(self, '_m_minutes') else None
+
+ self._m_minutes = ((self.time & 2016) >> 5)
+ return self._m_minutes if hasattr(self, '_m_minutes') else None
+
+ @property
+ def day(self):
+ if hasattr(self, '_m_day'):
+ return self._m_day if hasattr(self, '_m_day') else None
+
+ self._m_day = (self.date & 31)
+ return self._m_day if hasattr(self, '_m_day') else None
+
+ @property
+ def hours(self):
+ if hasattr(self, '_m_hours'):
+ return self._m_hours if hasattr(self, '_m_hours') else None
+
+ self._m_hours = ((self.time & 63488) >> 11)
+ return self._m_hours if hasattr(self, '_m_hours') else None
+
+
+
diff --git a/python/examples/kaitai/kaitai_struct_formats/archive/zip.py b/python/examples/kaitai/kaitai_struct_formats/archive/zip.py
new file mode 100644
index 00000000..1f6894dd
--- /dev/null
+++ b/python/examples/kaitai/kaitai_struct_formats/archive/zip.py
@@ -0,0 +1,503 @@
+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 Zip(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
+ """
+
+ class Compression(Enum):
+ none = 0
+ shrunk = 1
+ reduced_1 = 2
+ reduced_2 = 3
+ reduced_3 = 4
+ reduced_4 = 5
+ imploded = 6
+ deflated = 8
+ enhanced_deflated = 9
+ pkware_dcl_imploded = 10
+ bzip2 = 12
+ lzma = 14
+ ibm_terse = 18
+ ibm_lz77_z = 19
+ ppmd = 98
+
+ class ExtraCodes(Enum):
+ zip64 = 1
+ av_info = 7
+ os2 = 9
+ ntfs = 10
+ openvms = 12
+ pkware_unix = 13
+ file_stream_and_fork_descriptors = 14
+ patch_descriptor = 15
+ pkcs7 = 20
+ x509_cert_id_and_signature_for_file = 21
+ x509_cert_id_for_central_dir = 22
+ strong_encryption_header = 23
+ record_management_controls = 24
+ pkcs7_enc_recip_cert_list = 25
+ ibm_s390_uncomp = 101
+ ibm_s390_comp = 102
+ poszip_4690 = 18064
+ extended_timestamp = 21589
+ infozip_unix = 30805
+ infozip_unix_var_size = 30837
+ SEQ_FIELDS = ["sections"]
+ 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['sections']['start'] = self._io.pos()
+ self.sections = []
+ i = 0
+ while not self._io.is_eof():
+ if not 'arr' in self._debug['sections']:
+ self._debug['sections']['arr'] = []
+ self._debug['sections']['arr'].append({'start': self._io.pos()})
+ _t_sections = self._root.PkSection(self._io, self, self._root)
+ _t_sections._read()
+ self.sections.append(_t_sections)
+ self._debug['sections']['arr'][len(self.sections) - 1]['end'] = self._io.pos()
+ i += 1
+
+ self._debug['sections']['end'] = self._io.pos()
+
+ class LocalFile(KaitaiStruct):
+ SEQ_FIELDS = ["header", "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['header']['start'] = self._io.pos()
+ self.header = self._root.LocalFileHeader(self._io, self, self._root)
+ self.header._read()
+ self._debug['header']['end'] = self._io.pos()
+ self._debug['body']['start'] = self._io.pos()
+ self.body = self._io.read_bytes(self.header.compressed_size)
+ self._debug['body']['end'] = self._io.pos()
+
+
+ class ExtraField(KaitaiStruct):
+ SEQ_FIELDS = ["code", "size", "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.ExtraCodes, self._io.read_u2le())
+ self._debug['code']['end'] = self._io.pos()
+ self._debug['size']['start'] = self._io.pos()
+ self.size = self._io.read_u2le()
+ self._debug['size']['end'] = self._io.pos()
+ self._debug['body']['start'] = self._io.pos()
+ _on = self.code
+ if _on == self._root.ExtraCodes.ntfs:
+ self._raw_body = self._io.read_bytes(self.size)
+ io = KaitaiStream(BytesIO(self._raw_body))
+ self.body = self._root.ExtraField.Ntfs(io, self, self._root)
+ self.body._read()
+ elif _on == self._root.ExtraCodes.extended_timestamp:
+ self._raw_body = self._io.read_bytes(self.size)
+ io = KaitaiStream(BytesIO(self._raw_body))
+ self.body = self._root.ExtraField.ExtendedTimestamp(io, self, self._root)
+ self.body._read()
+ elif _on == self._root.ExtraCodes.infozip_unix_var_size:
+ self._raw_body = self._io.read_bytes(self.size)
+ io = KaitaiStream(BytesIO(self._raw_body))
+ self.body = self._root.ExtraField.InfozipUnixVarSize(io, self, self._root)
+ self.body._read()
+ else:
+ self.body = self._io.read_bytes(self.size)
+ self._debug['body']['end'] = self._io.pos()
+
+ class Ntfs(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://github.com/LuaDist/zip/blob/master/proginfo/extrafld.txt#L191
+ """
+ SEQ_FIELDS = ["reserved", "attributes"]
+ 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['reserved']['start'] = self._io.pos()
+ self.reserved = self._io.read_u4le()
+ self._debug['reserved']['end'] = self._io.pos()
+ self._debug['attributes']['start'] = self._io.pos()
+ self.attributes = []
+ i = 0
+ while not self._io.is_eof():
+ if not 'arr' in self._debug['attributes']:
+ self._debug['attributes']['arr'] = []
+ self._debug['attributes']['arr'].append({'start': self._io.pos()})
+ _t_attributes = self._root.ExtraField.Ntfs.Attribute(self._io, self, self._root)
+ _t_attributes._read()
+ self.attributes.append(_t_attributes)
+ self._debug['attributes']['arr'][len(self.attributes) - 1]['end'] = self._io.pos()
+ i += 1
+
+ self._debug['attributes']['end'] = self._io.pos()
+
+ class Attribute(KaitaiStruct):
+ SEQ_FIELDS = ["tag", "size", "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['tag']['start'] = self._io.pos()
+ self.tag = self._io.read_u2le()
+ self._debug['tag']['end'] = self._io.pos()
+ self._debug['size']['start'] = self._io.pos()
+ self.size = self._io.read_u2le()
+ self._debug['size']['end'] = self._io.pos()
+ self._debug['body']['start'] = self._io.pos()
+ _on = self.tag
+ if _on == 1:
+ self._raw_body = self._io.read_bytes(self.size)
+ io = KaitaiStream(BytesIO(self._raw_body))
+ self.body = self._root.ExtraField.Ntfs.Attribute1(io, self, self._root)
+ self.body._read()
+ else:
+ self.body = self._io.read_bytes(self.size)
+ self._debug['body']['end'] = self._io.pos()
+
+
+ class Attribute1(KaitaiStruct):
+ SEQ_FIELDS = ["last_mod_time", "last_access_time", "creation_time"]
+ 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['last_mod_time']['start'] = self._io.pos()
+ self.last_mod_time = self._io.read_u8le()
+ self._debug['last_mod_time']['end'] = self._io.pos()
+ self._debug['last_access_time']['start'] = self._io.pos()
+ self.last_access_time = self._io.read_u8le()
+ self._debug['last_access_time']['end'] = self._io.pos()
+ self._debug['creation_time']['start'] = self._io.pos()
+ self.creation_time = self._io.read_u8le()
+ self._debug['creation_time']['end'] = self._io.pos()
+
+
+
+ class ExtendedTimestamp(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://github.com/LuaDist/zip/blob/master/proginfo/extrafld.txt#L817
+ """
+ SEQ_FIELDS = ["flags", "mod_time", "access_time", "create_time"]
+ 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['flags']['start'] = self._io.pos()
+ self.flags = self._io.read_u1()
+ self._debug['flags']['end'] = self._io.pos()
+ self._debug['mod_time']['start'] = self._io.pos()
+ self.mod_time = self._io.read_u4le()
+ self._debug['mod_time']['end'] = self._io.pos()
+ if not (self._io.is_eof()):
+ self._debug['access_time']['start'] = self._io.pos()
+ self.access_time = self._io.read_u4le()
+ self._debug['access_time']['end'] = self._io.pos()
+
+ if not (self._io.is_eof()):
+ self._debug['create_time']['start'] = self._io.pos()
+ self.create_time = self._io.read_u4le()
+ self._debug['create_time']['end'] = self._io.pos()
+
+
+
+ class InfozipUnixVarSize(KaitaiStruct):
+ """
+ .. seealso::
+ Source - https://github.com/LuaDist/zip/blob/master/proginfo/extrafld.txt#L1339
+ """
+ SEQ_FIELDS = ["version", "uid_size", "uid", "gid_size", "gid"]
+ 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['uid_size']['start'] = self._io.pos()
+ self.uid_size = self._io.read_u1()
+ self._debug['uid_size']['end'] = self._io.pos()
+ self._debug['uid']['start'] = self._io.pos()
+ self.uid = self._io.read_bytes(self.uid_size)
+ self._debug['uid']['end'] = self._io.pos()
+ self._debug['gid_size']['start'] = self._io.pos()
+ self.gid_size = self._io.read_u1()
+ self._debug['gid_size']['end'] = self._io.pos()
+ self._debug['gid']['start'] = self._io.pos()
+ self.gid = self._io.read_bytes(self.gid_size)
+ self._debug['gid']['end'] = self._io.pos()
+
+
+
+ class CentralDirEntry(KaitaiStruct):
+ """
+ .. seealso::
+ - 4.3.12 - https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
+ """
+ SEQ_FIELDS = ["version_made_by", "version_needed_to_extract", "flags", "compression_method", "last_mod_file_time", "last_mod_file_date", "crc32", "compressed_size", "uncompressed_size", "file_name_len", "extra_len", "comment_len", "disk_number_start", "int_file_attr", "ext_file_attr", "local_header_offset", "file_name", "extra", "comment"]
+ 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_made_by']['start'] = self._io.pos()
+ self.version_made_by = self._io.read_u2le()
+ self._debug['version_made_by']['end'] = self._io.pos()
+ self._debug['version_needed_to_extract']['start'] = self._io.pos()
+ self.version_needed_to_extract = self._io.read_u2le()
+ self._debug['version_needed_to_extract']['end'] = self._io.pos()
+ self._debug['flags']['start'] = self._io.pos()
+ self.flags = self._io.read_u2le()
+ self._debug['flags']['end'] = self._io.pos()
+ self._debug['compression_method']['start'] = self._io.pos()
+ self.compression_method = KaitaiStream.resolve_enum(self._root.Compression, self._io.read_u2le())
+ self._debug['compression_method']['end'] = self._io.pos()
+ self._debug['last_mod_file_time']['start'] = self._io.pos()
+ self.last_mod_file_time = self._io.read_u2le()
+ self._debug['last_mod_file_time']['end'] = self._io.pos()
+ self._debug['last_mod_file_date']['start'] = self._io.pos()
+ self.last_mod_file_date = self._io.read_u2le()
+ self._debug['last_mod_file_date']['end'] = self._io.pos()
+ self._debug['crc32']['start'] = self._io.pos()
+ self.crc32 = self._io.read_u4le()
+ self._debug['crc32']['end'] = self._io.pos()
+ self._debug['compressed_size']['start'] = self._io.pos()
+ self.compressed_size = self._io.read_u4le()
+ self._debug['compressed_size']['end'] = self._io.pos()
+ self._debug['uncompressed_size']['start'] = self._io.pos()
+ self.uncompressed_size = self._io.read_u4le()
+ self._debug['uncompressed_size']['end'] = self._io.pos()
+ self._debug['file_name_len']['start'] = self._io.pos()
+ self.file_name_len = self._io.read_u2le()
+ self._debug['file_name_len']['end'] = self._io.pos()
+ self._debug['extra_len']['start'] = self._io.pos()
+ self.extra_len = self._io.read_u2le()
+ self._debug['extra_len']['end'] = self._io.pos()
+ self._debug['comment_len']['start'] = self._io.pos()
+ self.comment_len = self._io.read_u2le()
+ self._debug['comment_len']['end'] = self._io.pos()
+ self._debug['disk_number_start']['start'] = self._io.pos()
+ self.disk_number_start = self._io.read_u2le()
+ self._debug['disk_number_start']['end'] = self._io.pos()
+ self._debug['int_file_attr']['start'] = self._io.pos()
+ self.int_file_attr = self._io.read_u2le()
+ self._debug['int_file_attr']['end'] = self._io.pos()
+ self._debug['ext_file_attr']['start'] = self._io.pos()
+ self.ext_file_attr = self._io.read_u4le()
+ self._debug['ext_file_attr']['end'] = self._io.pos()
+ self._debug['local_header_offset']['start'] = self._io.pos()
+ self.local_header_offset = self._io.read_s4le()
+ self._debug['local_header_offset']['end'] = self._io.pos()
+ self._debug['file_name']['start'] = self._io.pos()
+ self.file_name = (self._io.read_bytes(self.file_name_len)).decode(u"UTF-8")
+ self._debug['file_name']['end'] = self._io.pos()
+ self._debug['extra']['start'] = self._io.pos()
+ self._raw_extra = self._io.read_bytes(self.extra_len)
+ io = KaitaiStream(BytesIO(self._raw_extra))
+ self.extra = self._root.Extras(io, self, self._root)
+ self.extra._read()
+ self._debug['extra']['end'] = self._io.pos()
+ self._debug['comment']['start'] = self._io.pos()
+ self.comment = (self._io.read_bytes(self.comment_len)).decode(u"UTF-8")
+ self._debug['comment']['end'] = self._io.pos()
+
+ @property
+ def local_header(self):
+ if hasattr(self, '_m_local_header'):
+ return self._m_local_header if hasattr(self, '_m_local_header') else None
+
+ _pos = self._io.pos()
+ self._io.seek(self.local_header_offset)
+ self._debug['_m_local_header']['start'] = self._io.pos()
+ self._m_local_header = self._root.PkSection(self._io, self, self._root)
+ self._m_local_header._read()
+ self._debug['_m_local_header']['end'] = self._io.pos()
+ self._io.seek(_pos)
+ return self._m_local_header if hasattr(self, '_m_local_header') else None
+
+
+ class PkSection(KaitaiStruct):
+ SEQ_FIELDS = ["magic", "section_type", "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['magic']['start'] = self._io.pos()
+ self.magic = self._io.ensure_fixed_contents(b"\x50\x4B")
+ self._debug['magic']['end'] = self._io.pos()
+ self._debug['section_type']['start'] = self._io.pos()
+ self.section_type = self._io.read_u2le()
+ self._debug['section_type']['end'] = self._io.pos()
+ self._debug['body']['start'] = self._io.pos()
+ _on = self.section_type
+ if _on == 513:
+ self.body = self._root.CentralDirEntry(self._io, self, self._root)
+ self.body._read()
+ elif _on == 1027:
+ self.body = self._root.LocalFile(self._io, self, self._root)
+ self.body._read()
+ elif _on == 1541:
+ self.body = self._root.EndOfCentralDir(self._io, self, self._root)
+ self.body._read()
+ self._debug['body']['end'] = self._io.pos()
+
+
+ class Extras(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 = self._root.ExtraField(self._io, self, self._root)
+ _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 LocalFileHeader(KaitaiStruct):
+ SEQ_FIELDS = ["version", "flags", "compression_method", "file_mod_time", "file_mod_date", "crc32", "compressed_size", "uncompressed_size", "file_name_len", "extra_len", "file_name", "extra"]
+ 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_u2le()
+ self._debug['version']['end'] = self._io.pos()
+ self._debug['flags']['start'] = self._io.pos()
+ self.flags = self._io.read_u2le()
+ self._debug['flags']['end'] = self._io.pos()
+ self._debug['compression_method']['start'] = self._io.pos()
+ self.compression_method = KaitaiStream.resolve_enum(self._root.Compression, self._io.read_u2le())
+ self._debug['compression_method']['end'] = self._io.pos()
+ self._debug['file_mod_time']['start'] = self._io.pos()
+ self.file_mod_time = self._io.read_u2le()
+ self._debug['file_mod_time']['end'] = self._io.pos()
+ self._debug['file_mod_date']['start'] = self._io.pos()
+ self.file_mod_date = self._io.read_u2le()
+ self._debug['file_mod_date']['end'] = self._io.pos()
+ self._debug['crc32']['start'] = self._io.pos()
+ self.crc32 = self._io.read_u4le()
+ self._debug['crc32']['end'] = self._io.pos()
+ self._debug['compressed_size']['start'] = self._io.pos()
+ self.compressed_size = self._io.read_u4le()
+ self._debug['compressed_size']['end'] = self._io.pos()
+ self._debug['uncompressed_size']['start'] = self._io.pos()
+ self.uncompressed_size = self._io.read_u4le()
+ self._debug['uncompressed_size']['end'] = self._io.pos()
+ self._debug['file_name_len']['start'] = self._io.pos()
+ self.file_name_len = self._io.read_u2le()
+ self._debug['file_name_len']['end'] = self._io.pos()
+ self._debug['extra_len']['start'] = self._io.pos()
+ self.extra_len = self._io.read_u2le()
+ self._debug['extra_len']['end'] = self._io.pos()
+ self._debug['file_name']['start'] = self._io.pos()
+ self.file_name = (self._io.read_bytes(self.file_name_len)).decode(u"UTF-8")
+ self._debug['file_name']['end'] = self._io.pos()
+ self._debug['extra']['start'] = self._io.pos()
+ self._raw_extra = self._io.read_bytes(self.extra_len)
+ io = KaitaiStream(BytesIO(self._raw_extra))
+ self.extra = self._root.Extras(io, self, self._root)
+ self.extra._read()
+ self._debug['extra']['end'] = self._io.pos()
+
+
+ class EndOfCentralDir(KaitaiStruct):
+ SEQ_FIELDS = ["disk_of_end_of_central_dir", "disk_of_central_dir", "qty_central_dir_entries_on_disk", "qty_central_dir_entries_total", "central_dir_size", "central_dir_offset", "comment_len", "comment"]
+ 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['disk_of_end_of_central_dir']['start'] = self._io.pos()
+ self.disk_of_end_of_central_dir = self._io.read_u2le()
+ self._debug['disk_of_end_of_central_dir']['end'] = self._io.pos()
+ self._debug['disk_of_central_dir']['start'] = self._io.pos()
+ self.disk_of_central_dir = self._io.read_u2le()
+ self._debug['disk_of_central_dir']['end'] = self._io.pos()
+ self._debug['qty_central_dir_entries_on_disk']['start'] = self._io.pos()
+ self.qty_central_dir_entries_on_disk = self._io.read_u2le()
+ self._debug['qty_central_dir_entries_on_disk']['end'] = self._io.pos()
+ self._debug['qty_central_dir_entries_total']['start'] = self._io.pos()
+ self.qty_central_dir_entries_total = self._io.read_u2le()
+ self._debug['qty_central_dir_entries_total']['end'] = self._io.pos()
+ self._debug['central_dir_size']['start'] = self._io.pos()
+ self.central_dir_size = self._io.read_u4le()
+ self._debug['central_dir_size']['end'] = self._io.pos()
+ self._debug['central_dir_offset']['start'] = self._io.pos()
+ self.central_dir_offset = self._io.read_u4le()
+ self._debug['central_dir_offset']['end'] = self._io.pos()
+ self._debug['comment_len']['start'] = self._io.pos()
+ self.comment_len = self._io.read_u2le()
+ self._debug['comment_len']['end'] = self._io.pos()
+ self._debug['comment']['start'] = self._io.pos()
+ self.comment = (self._io.read_bytes(self.comment_len)).decode(u"UTF-8")
+ self._debug['comment']['end'] = self._io.pos()
+
+
+