# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def utf8_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p.encode('utf-8')
#     if enc != 'utf-8':
#         return p.decode(enc).encode('utf-8')
#     return p
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# 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


from .compatibility_utils import PY2, utf8_str
from .mobi_utils import fromBase32
import re
                            [typ, fmt, pdir, fnm] = self.k8proc.getFlowInfo(num)
                            flowpart = flows[num]
                            if fmt == b'inline':
                                tag = flowpart
                            else:
                                replacement = b'"../' + utf8_str(pdir) + b'/' + utf8_str(fnm) + b'"'
                                tag = flow_pattern.sub(replacement, tag, 1)
                                self.used[fnm] = 'used'
                        else:
                            print("warning: ignoring non-existent flow link", tag, " value 0x%x" % num)
                    srcpieces[j] = tag
            part = b''.join(srcpieces)

            # store away modified version
            parts[i] = part

        # Handle any embedded raster images links in style= attributes urls
        style_pattern = re.compile(br'''(<[a-zA-Z0-9]+\s[^>]*style\s*=\s*[^>]*>)''', re.IGNORECASE)
        img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)[^'"]*['")]''', re.IGNORECASE)

        for i in range(len(parts)):
            part = parts[i]
            [partnum, dir, filename, beg, end, aidtext] = self.k8proc.partinfo[i]

            # replace urls in style attributes
            srcpieces = style_pattern.split(part)
            for j in range(1, len(srcpieces),2):
                tag = srcpieces[j]
                if b'kindle:embed' in tag:
                    for m in img_index_pattern.finditer(tag):

# Path: resources/lib/kindleunpack/compatibility_utils.py
# PY2 = sys.version_info[0] == 2
# 
# def utf8_str(p, enc='utf-8'):
#     if p is None:
#         return None
#     if isinstance(p, text_type):
#         return p.encode('utf-8')
#     if enc != 'utf-8':
#         return p.decode(enc).encode('utf-8')
#     return p
# 
# Path: resources/lib/kindleunpack/mobi_utils.py
# 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


# Path: resources/lib/kindleunpack/mobi_html.py
from .compatibility_utils import PY2, utf8_str
from .mobi_utils import fromBase32
import re

                            [typ, fmt, pdir, fnm] = self.k8proc.getFlowInfo(num)
                            flowpart = flows[num]
                            if fmt == b'inline':
                                tag = flowpart
                            else:
                                replacement = b'"../' + utf8_str(pdir) + b'/' + utf8_str(fnm) + b'"'
                                tag = flow_pattern.sub(replacement, tag, 1)
                                self.used[fnm] = 'used'
                        else:
                            print("warning: ignoring non-existent flow link", tag, " value 0x%x" % num)
                    srcpieces[j] = tag
            part = b''.join(srcpieces)

            # store away modified version
            parts[i] = part

        # Handle any embedded raster images links in style= attributes urls
        style_pattern = re.compile(br'''(<[a-zA-Z0-9]+\s[^>]*style\s*=\s*[^>]*>)''', re.IGNORECASE)
        img_index_pattern = re.compile(br'''[('"]kindle:embed:([0-9|A-V]+)[^'"]*['")]''', re.IGNORECASE)

        for i in range(len(parts)):
            part = parts[i]
            [partnum, dir, filename, beg, end, aidtext] = self.k8proc.partinfo[i]

            # replace urls in style attributes
            srcpieces = style_pattern.split(part)
            for j in range(1, len(srcpieces),2):
                tag = srcpieces[j]
                if b'kindle:embed' in tag:
                    for m in img_index_pattern.finditer(tag):

                        imageNumber = fromBase32(m.group(1))