# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def bchr(s):
#     return bytes([s])
# 
# def lmap(*args, **kwargs):
#     return list(map(*args, **kwargs))
# 
# def bstr(s):
#     if isinstance(s, str):
#         return bytes(s, 'latin-1')
#     else:
#         return bytes(s)


from .compatibility_utils import PY2, bchr, lmap, bstr
import struct

if PY2:
    range = xrange

# note:  struct pack, unpack, unpack_from all require bytestring format
# data all the way up to at least python 2.7.5, python 3 okay with bytestring


class unpackException(Exception):
    pass

class UncompressedReader:

    def unpack(self, data):
        return data

class PalmdocReader:

    def unpack(self, i):
        o, p = b'', 0
        while p < len(i):
            # for python 3 must use slice since i[p] returns int while slice returns character
            c = ord(i[p:p+1])
            p += 1
            if (c >= 1 and c <= 8):
                o += i[p:p+c]
                p += c
            elif (c < 128):
                o += bchr(c)
            elif (c >= 192):

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def bchr(s):
#     return bytes([s])
# 
# def lmap(*args, **kwargs):
#     return list(map(*args, **kwargs))
# 
# def bstr(s):
#     if isinstance(s, str):
#         return bytes(s, 'latin-1')
#     else:
#         return bytes(s)


# Path: resources/lib/kindleunpack/mobi_uncompress.py
from .compatibility_utils import PY2, bchr, lmap, bstr
import struct


if PY2:
    range = xrange

# note:  struct pack, unpack, unpack_from all require bytestring format
# data all the way up to at least python 2.7.5, python 3 okay with bytestring


class unpackException(Exception):
    pass

class UncompressedReader:

    def unpack(self, data):
        return data

class PalmdocReader:

    def unpack(self, i):
        o, p = b'', 0
        while p < len(i):
            # for python 3 must use slice since i[p] returns int while slice returns character
            c = ord(i[p:p+1])
            p += 1
            if (c >= 1 and c <= 8):
                o += i[p:p+c]
                p += c
            elif (c < 128):
                o += bchr(c)
            elif (c >= 192):

                o += b' ' + bchr(c ^ 128)