kaitaistruct-0.7/0000755000175000017500000000000013064544754015221 5ustar greycatgreycat00000000000000kaitaistruct-0.7/MANIFEST.in0000644000175000017500000000002213006675475016752 0ustar greycatgreycat00000000000000include README.md kaitaistruct-0.7/setup.py0000755000175000017500000000315213051113726016722 0ustar greycatgreycat00000000000000from setuptools import setup from io import open from kaitaistruct import __version__ with open('README.md', encoding='utf-8') as f: long_description = f.read() setup( name='kaitaistruct', version=__version__, description='Kaitai Struct declarative parser generator for binary data: runtime library for Python', long_description=long_description, url='http://kaitai.io', author='Kaitai Project', author_email='greycat@kaitai.io', license='MIT', # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ # How mature is this project? Common values are # 3 - Alpha # 4 - Beta # 5 - Production/Stable 'Development Status :: 4 - Beta', # Indicate who your project is intended for 'Intended Audience :: Developers', 'Topic :: Software Development :: Build Tools', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: MIT License', # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', ], keywords='kaitai,struct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development', py_modules=["kaitaistruct"] ) kaitaistruct-0.7/PKG-INFO0000644000175000017500000000735413064544754016327 0ustar greycatgreycat00000000000000Metadata-Version: 1.1 Name: kaitaistruct Version: 0.7 Summary: Kaitai Struct declarative parser generator for binary data: runtime library for Python Home-page: http://kaitai.io Author: Kaitai Project Author-email: greycat@kaitai.io License: MIT Description: # Kaitai Struct: runtime library for Python This library implements Kaitai Struct API for Python. [Kaitai Struct](http://kaitai.io) is a declarative language used for describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc. It is similar to Python's [construct] and [Construct3], but it is language-agnostic. The format description is done in YAML-based .ksy format, which then can be compiled into a wide range of target languages. [construct]: https://pypi.python.org/pypi/construct [Construct3]: http://tomerfiliba.com/blog/Survey-of-Construct3/ Further reading: * [About Kaitai Struct](http://kaitai.io/) * [About API implemented in this library](http://doc.kaitai.io/stream_api.html) * [Python-specific notes](http://doc.kaitai.io/lang_python.html) ## Installing ### Using `requirements.txt` If you want to use Kaitai Struct runtime in your project and you use `requirements.txt` to manage your dependencies, just add the following line to it: ``` kaitaistruct ``` and then run `pip install -r requirements.txt` to update all your dependencies. ### Using `pip` directly You can use ```shell pip install kaitaistruct ``` to install the package manually using `pip` Python package manager. ## Licensing Copyright 2015-2017 Kaitai Project: MIT license Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Keywords: kaitai,struct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: Topic :: Software Development :: Build Tools Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 kaitaistruct-0.7/kaitaistruct.egg-info/0000755000175000017500000000000013064544754021422 5ustar greycatgreycat00000000000000kaitaistruct-0.7/kaitaistruct.egg-info/dependency_links.txt0000644000175000017500000000000113064544754025470 0ustar greycatgreycat00000000000000 kaitaistruct-0.7/kaitaistruct.egg-info/top_level.txt0000644000175000017500000000001513064544754024150 0ustar greycatgreycat00000000000000kaitaistruct kaitaistruct-0.7/kaitaistruct.egg-info/PKG-INFO0000644000175000017500000000735413064544754022530 0ustar greycatgreycat00000000000000Metadata-Version: 1.1 Name: kaitaistruct Version: 0.7 Summary: Kaitai Struct declarative parser generator for binary data: runtime library for Python Home-page: http://kaitai.io Author: Kaitai Project Author-email: greycat@kaitai.io License: MIT Description: # Kaitai Struct: runtime library for Python This library implements Kaitai Struct API for Python. [Kaitai Struct](http://kaitai.io) is a declarative language used for describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc. It is similar to Python's [construct] and [Construct3], but it is language-agnostic. The format description is done in YAML-based .ksy format, which then can be compiled into a wide range of target languages. [construct]: https://pypi.python.org/pypi/construct [Construct3]: http://tomerfiliba.com/blog/Survey-of-Construct3/ Further reading: * [About Kaitai Struct](http://kaitai.io/) * [About API implemented in this library](http://doc.kaitai.io/stream_api.html) * [Python-specific notes](http://doc.kaitai.io/lang_python.html) ## Installing ### Using `requirements.txt` If you want to use Kaitai Struct runtime in your project and you use `requirements.txt` to manage your dependencies, just add the following line to it: ``` kaitaistruct ``` and then run `pip install -r requirements.txt` to update all your dependencies. ### Using `pip` directly You can use ```shell pip install kaitaistruct ``` to install the package manually using `pip` Python package manager. ## Licensing Copyright 2015-2017 Kaitai Project: MIT license Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Keywords: kaitai,struct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Intended Audience :: Developers Classifier: Topic :: Software Development :: Build Tools Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 kaitaistruct-0.7/kaitaistruct.egg-info/SOURCES.txt0000644000175000017500000000031013064544754023300 0ustar greycatgreycat00000000000000MANIFEST.in README.md kaitaistruct.py setup.cfg setup.py kaitaistruct.egg-info/PKG-INFO kaitaistruct.egg-info/SOURCES.txt kaitaistruct.egg-info/dependency_links.txt kaitaistruct.egg-info/top_level.txtkaitaistruct-0.7/README.md0000644000175000017500000000450513052601565016473 0ustar greycatgreycat00000000000000# Kaitai Struct: runtime library for Python This library implements Kaitai Struct API for Python. [Kaitai Struct](http://kaitai.io) is a declarative language used for describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc. It is similar to Python's [construct] and [Construct3], but it is language-agnostic. The format description is done in YAML-based .ksy format, which then can be compiled into a wide range of target languages. [construct]: https://pypi.python.org/pypi/construct [Construct3]: http://tomerfiliba.com/blog/Survey-of-Construct3/ Further reading: * [About Kaitai Struct](http://kaitai.io/) * [About API implemented in this library](http://doc.kaitai.io/stream_api.html) * [Python-specific notes](http://doc.kaitai.io/lang_python.html) ## Installing ### Using `requirements.txt` If you want to use Kaitai Struct runtime in your project and you use `requirements.txt` to manage your dependencies, just add the following line to it: ``` kaitaistruct ``` and then run `pip install -r requirements.txt` to update all your dependencies. ### Using `pip` directly You can use ```shell pip install kaitaistruct ``` to install the package manually using `pip` Python package manager. ## Licensing Copyright 2015-2017 Kaitai Project: MIT license Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. kaitaistruct-0.7/kaitaistruct.py0000644000175000017500000002413713063072027020275 0ustar greycatgreycat00000000000000import itertools import sys from struct import unpack try: import cStringIO BytesIO = cStringIO.StringIO except ImportError: from io import BytesIO # noqa PY2 = sys.version_info[0] == 2 # Kaitai Struct runtime streaming API version, defined as per PEP-0396 # standard. Used for two purposes: # # * .py files generated by ksc from .ksy check that they import proper # KS runtime library by this version number; # * distribution utils (setup.py) use this when packaging for PyPI # __version__ = '0.7' class KaitaiStruct(object): def __init__(self, stream): self._io = stream def __enter__(self): return self def __exit__(self, *args, **kwargs): self.close() @classmethod def from_file(cls, filename): f = open(filename, 'rb') try: return cls(KaitaiStream(f)) except Exception: # close file descriptor, then reraise the exception f.close() raise def close(self): self._io.close() class KaitaiStream(object): def __init__(self, io): self._io = io self.align_to_byte() def __enter__(self): return self def __exit__(self, *args, **kwargs): self.close() def close(self): self._io.close() # ======================================================================== # Stream positioning # ======================================================================== def is_eof(self): io = self._io t = io.read(1) if t == b'': return True else: io.seek(io.tell() - 1) return False def seek(self, n): self._io.seek(n) def pos(self): return self._io.tell() def size(self): # Python has no internal File object API function to get # current file / StringIO size, thus we use the following # trick. io = self._io # Remember our current position cur_pos = io.tell() # Seek to the end of the File object io.seek(0, 2) # Remember position, which is equal to the full length full_size = io.tell() # Seek back to the current position io.seek(cur_pos) return full_size # ======================================================================== # Integer numbers # ======================================================================== # ------------------------------------------------------------------------ # Signed # ------------------------------------------------------------------------ def read_s1(self): return unpack('b', self.read_bytes(1))[0] # ........................................................................ # Big-endian # ........................................................................ def read_s2be(self): return unpack('>h', self.read_bytes(2))[0] def read_s4be(self): return unpack('>i', self.read_bytes(4))[0] def read_s8be(self): return unpack('>q', self.read_bytes(8))[0] # ........................................................................ # Little-endian # ........................................................................ def read_s2le(self): return unpack('H', self.read_bytes(2))[0] def read_u4be(self): return unpack('>I', self.read_bytes(4))[0] def read_u8be(self): return unpack('>Q', self.read_bytes(8))[0] # ........................................................................ # Little-endian # ........................................................................ def read_u2le(self): return unpack('f', self.read_bytes(4))[0] def read_f8be(self): return unpack('>d', self.read_bytes(8))[0] # ........................................................................ # Little-endian # ........................................................................ def read_f4le(self): return unpack(' 0: # 1 bit => 1 byte # 8 bits => 1 byte # 9 bits => 2 bytes bytes_needed = ((bits_needed - 1) // 8) + 1 buf = self.read_bytes(bytes_needed) for byte in buf: # Python 2 will get "byte" as one-character str, thus # we need to convert it to integer manually; Python 3 # is fine as is. if isinstance(byte, str): byte = ord(byte) self.bits <<= 8 self.bits |= byte self.bits_left += 8 # raw mask with required number of 1s, starting from lowest bit mask = (1 << n) - 1 # shift mask to align with highest bits available in self.bits shift_bits = self.bits_left - n mask <<= shift_bits # derive reading result res = (self.bits & mask) >> shift_bits # clear top bits that we've just read => AND with 1s self.bits_left -= n mask = (1 << self.bits_left) - 1 self.bits &= mask return res # ======================================================================== # Byte arrays # ======================================================================== def read_bytes(self, n): r = self._io.read(n) if len(r) < n: raise EOFError( "requested %d bytes, but got only %d bytes" % (n, len(r)) ) return r def read_bytes_full(self): return self._io.read() def read_bytes_term(self, term, include_term, consume_term, eos_error): r = b'' while True: c = self._io.read(1) if c == b'': if eos_error: raise Exception( "End of stream reached, but no terminator %d found" % (term,) ) else: return r elif ord(c) == term: if include_term: r += c if not consume_term: self._io.seek(self._io.tell() - 1) return r else: r += c def ensure_fixed_contents(self, expected): actual = self._io.read(len(expected)) if actual != expected: raise Exception( "Unexpected fixed contents: got %s, was waiting for %s" % (str(actual), str(expected)) ) return actual @staticmethod def bytes_strip_right(src, pad_byte): new_len = len(src) if PY2: data = bytearray(src) else: data = src while data[new_len - 1] == pad_byte: new_len -= 1 return data[:new_len] @staticmethod def bytes_terminate(src, term, include_term): new_len = 0 max_len = len(src) if PY2: data = bytearray(src) else: data = src while new_len < max_len and data[new_len] != term: new_len += 1 if include_term and new_len < max_len: new_len += 1 return data[:new_len] # ======================================================================== # Byte array processing # ======================================================================== @staticmethod def process_xor_one(data, key): if PY2: r = bytearray(data) for i in range(len(r)): r[i] ^= key return bytes(r) else: return bytes(v ^ key for v in data) @staticmethod def process_xor_many(data, key): if PY2: r = bytearray(data) k = bytearray(key) ki = 0 kl = len(k) for i in range(len(r)): r[i] ^= k[ki] ki += 1 if ki >= kl: ki = 0 return bytes(r) else: return bytes(a ^ b for a, b in zip(data, itertools.cycle(key))) @staticmethod def process_rotate_left(data, amount, group_size): if group_size != 1: raise Exception( "unable to rotate group of %d bytes yet" % (group_size,) ) mask = group_size * 8 - 1 anti_amount = -amount & mask r = bytearray(data) for i in range(len(r)): r[i] = (r[i] << amount) & 0xff | (r[i] >> anti_amount) return bytes(r) kaitaistruct-0.7/setup.cfg0000644000175000017500000000013013064544754017034 0ustar greycatgreycat00000000000000[bdist_wheel] universal = 1 [egg_info] tag_build = tag_date = 0 tag_svn_revision = 0