# 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
        "read INDX header"
        if not data[:4] == b'INDX':
            print("Warning: index section is not INDX")
            return False
        words = (
                'len', 'nul1', 'type', 'gen', 'start', 'count', 'code',
                'lng', 'total', 'ordt', 'ligt', 'nligt', 'nctoc'
        )
        num = len(words)
        values = struct.unpack(bstr('>%dL' % num), data[4:4*(num+1)])
        header = {}
        for n in range(num):
            header[words[n]] = values[n]

        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)

# 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

        "read INDX header"
        if not data[:4] == b'INDX':
            print("Warning: index section is not INDX")
            return False
        words = (
                'len', 'nul1', 'type', 'gen', 'start', 'count', 'code',
                'lng', 'total', 'ordt', 'ligt', 'nligt', 'nctoc'
        )
        num = len(words)
        values = struct.unpack(bstr('>%dL' % num), data[4:4*(num+1)])
        header = {}
        for n in range(num):
            header[words[n]] = values[n]

        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)