# 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

                #  process links to raster image files
                for m in url_img_index_pattern.finditer(tag):
                    imageNumber = fromBase32(m.group(1))
                    imageName = self.rscnames[imageNumber-1]
                    osep = m.group()[0:1]
                    csep = m.group()[-1:]
                    if imageName is not None:
                        replacement = osep +  b'../Images/' + utf8_str(imageName) +  csep
                        self.used[imageName] = 'used'
                        tag = url_img_index_pattern.sub(replacement, tag, 1)
                    else:
                        print("Error: Referenced image %s was not recognized as a valid image in %s" % (imageNumber, tag))

                # process links to fonts
                for m in font_index_pattern.finditer(tag):
                    fontNumber = fromBase32(m.group(1))
                    fontName = self.rscnames[fontNumber-1]
                    osep = m.group()[0:1]
                    csep = m.group()[-1:]
                    if fontName is None:
                        print("Error: Referenced font %s was not recognized as a valid font in %s" % (fontNumber, tag))
                    else:
                        replacement = osep +  b'../Fonts/' + utf8_str(fontName) +  csep
                        tag = font_index_pattern.sub(replacement, tag, 1)
                        self.used[fontName] = 'used'

                # process links to other css pieces
                for m in url_css_index_pattern.finditer(tag):
                    num = fromBase32(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


                #  process links to raster image files
                for m in url_img_index_pattern.finditer(tag):
                    imageNumber = fromBase32(m.group(1))
                    imageName = self.rscnames[imageNumber-1]
                    osep = m.group()[0:1]
                    csep = m.group()[-1:]
                    if imageName is not None:
                        replacement = osep +  b'../Images/' + utf8_str(imageName) +  csep
                        self.used[imageName] = 'used'
                        tag = url_img_index_pattern.sub(replacement, tag, 1)
                    else:
                        print("Error: Referenced image %s was not recognized as a valid image in %s" % (imageNumber, tag))

                # process links to fonts
                for m in font_index_pattern.finditer(tag):
                    fontNumber = fromBase32(m.group(1))
                    fontName = self.rscnames[fontNumber-1]
                    osep = m.group()[0:1]
                    csep = m.group()[-1:]
                    if fontName is None:
                        print("Error: Referenced font %s was not recognized as a valid font in %s" % (fontNumber, tag))
                    else:
                        replacement = osep +  b'../Fonts/' + utf8_str(fontName) +  csep
                        tag = font_index_pattern.sub(replacement, tag, 1)
                        self.used[fontName] = 'used'

                # process links to other css pieces
                for m in url_css_index_pattern.finditer(tag):
                    num = fromBase32(m.group(1))

                    [typ, fmt, pdir, fnm] = self.k8proc.getFlowInfo(num)