# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def bchr(s):
#     return bytes([s])
# 
# def bstr(s):
#     if isinstance(s, str):
#         return bytes(s, 'latin-1')
#     else:
#         return bytes(s)
# 
# def bord(s):
#     return s
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# def toHex(byteList):
#     return binascii.hexlify(byteList)


from .compatibility_utils import PY2, bchr, bstr, bord
from .mobi_utils import toHex
import struct
        ordt1 = None
        ordt2 = None

        ocnt, oentries, op1, op2, otagx  = struct.unpack_from(b'>LLLLL',data, 0xa4)
        if header['code'] == 0xfdea or ocnt != 0 or oentries > 0:
            # horribly hacked up ESP (sample) mobi books use two ORDT sections but never specify
            # them in the proper place in the header.  They seem to be codepage 65002 which seems
            # to be some sort of strange EBCDIC utf-8 or 16 encoded strings

            # so we need to look for them and store them away to process leading text
            # ORDT1 has 1 byte long entries, ORDT2 has 2 byte long entries
            # we only ever seem to use the seocnd but ...
            assert(ocnt == 1)
            assert(data[op1:op1+4] == b'ORDT')
            assert(data[op2:op2+4] == b'ORDT')
            ordt1 = struct.unpack_from(bstr('>%dB' % oentries), data, op1+4)
            ordt2 = struct.unpack_from(bstr('>%dH' % oentries), data, op2+4)

        if self.DEBUG:
            print("parsed INDX header:")
            for n in words:
                print(n, "%X" % header[n],)
            print("")
        return header, ordt1, ordt2

    def readCTOC(self, txtdata):
        # read all blocks from CTOC
        ctoc_data = {}
        offset = 0
        while offset<len(txtdata):

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def bchr(s):
#     return bytes([s])
# 
# def bstr(s):
#     if isinstance(s, str):
#         return bytes(s, 'latin-1')
#     else:
#         return bytes(s)
# 
# def bord(s):
#     return s
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# def toHex(byteList):
#     return binascii.hexlify(byteList)


# Path: resources/lib/kindleunpack/mobi_index.py
from .compatibility_utils import PY2, bchr, bstr, bord
from .mobi_utils import toHex
import struct

        ordt1 = None
        ordt2 = None

        ocnt, oentries, op1, op2, otagx  = struct.unpack_from(b'>LLLLL',data, 0xa4)
        if header['code'] == 0xfdea or ocnt != 0 or oentries > 0:
            # horribly hacked up ESP (sample) mobi books use two ORDT sections but never specify
            # them in the proper place in the header.  They seem to be codepage 65002 which seems
            # to be some sort of strange EBCDIC utf-8 or 16 encoded strings

            # so we need to look for them and store them away to process leading text
            # ORDT1 has 1 byte long entries, ORDT2 has 2 byte long entries
            # we only ever seem to use the seocnd but ...
            assert(ocnt == 1)
            assert(data[op1:op1+4] == b'ORDT')
            assert(data[op2:op2+4] == b'ORDT')
            ordt1 = struct.unpack_from(bstr('>%dB' % oentries), data, op1+4)
            ordt2 = struct.unpack_from(bstr('>%dH' % oentries), data, op2+4)

        if self.DEBUG:
            print("parsed INDX header:")
            for n in words:
                print(n, "%X" % header[n],)
            print("")
        return header, ordt1, ordt2

    def readCTOC(self, txtdata):
        # read all blocks from CTOC
        ctoc_data = {}
        offset = 0
        while offset<len(txtdata):

            if PY2: