# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# PY3 = sys.version_info[0] == 3
# ASCII_CHARS   = set(chr(x) for x in range(128))
# URL_SAFE      = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#                     'abcdefghijklmnopqrstuvwxyz'
#                     '0123456789' '#' '_.-/~')
# IRI_UNSAFE = ASCII_CHARS - URL_SAFE
#     def bchr(s):
#     def bstr(s):
#     def bord(s):
#     def bchar(s):
#     def bchr(s):
#     def bstr(s):
#     def bord(s):
#     def bchar(s):
#     def lrange(*args, **kwargs):
#     def lzip(*args, **kwargs):
#     def lmap(*args, **kwargs):
#     def lfilter(*args, **kwargs):
# def hexlify(bdata):
# def utf8_str(p, enc='utf-8'):
# def unicode_str(p, enc='utf-8'):
# def quoteurl(href):
# def unquoteurl(href):
# def unescapeit(sval):
# def unicode_argv():
# def add_cp65001_codec():


from .compatibility_utils import PY2, text_type, bchr, bord
from itertools import cycle
import binascii
# converts base32 string to value
def fromBase32(str_num):
    if isinstance(str_num, text_type):
        str_num = str_num.encode('latin-1')
    scalelst = [1,32,1024,32768,1048576,33554432,1073741824,34359738368]
    value = 0
    j = 0
    n = len(str_num)
    scale = 0
    for i in range(n):
        c = str_num[n-i-1:n-i]
        if c in b'0123456789':
            v = ord(c) - ord(b'0')
        else:
            v = ord(c) - ord(b'A') + 10
        if j < len(scalelst):
            scale = scalelst[j]
        else:
            scale = scale * 32
        j += 1
        if v != 0:
            value = value + (v * scale)
    return value


# note: if decode a bytestring using 'latin-1' (or any other 0-255 encoding)
# in place of ascii you will get a byte to half-word or integer
# one to one mapping of values from 0 - 255

def mangle_fonts(encryption_key, data):

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# PY3 = sys.version_info[0] == 3
# ASCII_CHARS   = set(chr(x) for x in range(128))
# URL_SAFE      = set('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#                     'abcdefghijklmnopqrstuvwxyz'
#                     '0123456789' '#' '_.-/~')
# IRI_UNSAFE = ASCII_CHARS - URL_SAFE
#     def bchr(s):
#     def bstr(s):
#     def bord(s):
#     def bchar(s):
#     def bchr(s):
#     def bstr(s):
#     def bord(s):
#     def bchar(s):
#     def lrange(*args, **kwargs):
#     def lzip(*args, **kwargs):
#     def lmap(*args, **kwargs):
#     def lfilter(*args, **kwargs):
# def hexlify(bdata):
# def utf8_str(p, enc='utf-8'):
# def unicode_str(p, enc='utf-8'):
# def quoteurl(href):
# def unquoteurl(href):
# def unescapeit(sval):
# def unicode_argv():
# def add_cp65001_codec():


# Path: resources/lib/kindleunpack/mobi_utils.py
from .compatibility_utils import PY2, text_type, bchr, bord
from itertools import cycle
import binascii

# converts base32 string to value
def fromBase32(str_num):
    if isinstance(str_num, text_type):
        str_num = str_num.encode('latin-1')
    scalelst = [1,32,1024,32768,1048576,33554432,1073741824,34359738368]
    value = 0
    j = 0
    n = len(str_num)
    scale = 0
    for i in range(n):
        c = str_num[n-i-1:n-i]
        if c in b'0123456789':
            v = ord(c) - ord(b'0')
        else:
            v = ord(c) - ord(b'A') + 10
        if j < len(scalelst):
            scale = scalelst[j]
        else:
            scale = scale * 32
        j += 1
        if v != 0:
            value = value + (v * scale)
    return value


# note: if decode a bytestring using 'latin-1' (or any other 0-255 encoding)
# in place of ascii you will get a byte to half-word or integer
# one to one mapping of values from 0 - 255

def mangle_fonts(encryption_key, data):

    if isinstance(encryption_key, text_type):