# 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
            for j in range(1, len(srcpieces),2):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    for m in posfid_index_pattern.finditer(tag):
                        posfid = m.group(1)
                        offset = m.group(2)
                        filename, idtag = self.k8proc.getIDTagByPosFid(posfid, offset)
                        if idtag == b'':
                            replacement= b'"' + utf8_str(filename) + b'"'
                        else:
                            replacement = b'"' + utf8_str(filename) + b'#' + idtag + b'"'
                        tag = posfid_index_pattern.sub(replacement, tag, 1)
                    srcpieces[j] = tag
            part = b"".join(srcpieces)
            parts.append(part)

        # we are free to cut and paste as we see fit
        # we can safely remove all of the Kindlegen generated aid tags
        # change aid ids that are in k8proc.linked_aids to xhtml ids
        find_tag_with_aid_pattern = re.compile(br'''(<[^>]*\said\s*=[^>]*>)''', re.IGNORECASE)
        within_tag_aid_position_pattern = re.compile(br'''\said\s*=['"]([^'"]*)['"]''')
        for i in range(len(parts)):
            part = parts[i]
            srcpieces = find_tag_with_aid_pattern.split(part)
            for j in range(len(srcpieces)):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    for m in within_tag_aid_position_pattern.finditer(tag):
                        try:
                            aid = m.group(1)

# 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

            for j in range(1, len(srcpieces),2):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    for m in posfid_index_pattern.finditer(tag):
                        posfid = m.group(1)
                        offset = m.group(2)
                        filename, idtag = self.k8proc.getIDTagByPosFid(posfid, offset)
                        if idtag == b'':
                            replacement= b'"' + utf8_str(filename) + b'"'
                        else:
                            replacement = b'"' + utf8_str(filename) + b'#' + idtag + b'"'
                        tag = posfid_index_pattern.sub(replacement, tag, 1)
                    srcpieces[j] = tag
            part = b"".join(srcpieces)
            parts.append(part)

        # we are free to cut and paste as we see fit
        # we can safely remove all of the Kindlegen generated aid tags
        # change aid ids that are in k8proc.linked_aids to xhtml ids
        find_tag_with_aid_pattern = re.compile(br'''(<[^>]*\said\s*=[^>]*>)''', re.IGNORECASE)
        within_tag_aid_position_pattern = re.compile(br'''\said\s*=['"]([^'"]*)['"]''')
        for i in range(len(parts)):
            part = parts[i]
            srcpieces = find_tag_with_aid_pattern.split(part)
            for j in range(len(srcpieces)):
                tag = srcpieces[j]
                if tag.startswith(b'<'):
                    for m in within_tag_aid_position_pattern.finditer(tag):
                        try:
                            aid = m.group(1)

                        except IndexError: