示例#1
0
文件: mobi8.py 项目: Eksmo/calibre
    def extract_resources(self):
        self.resource_map = []
        known_types = {b'FLIS', b'FCIS', b'SRCS',
                    b'\xe9\x8e\r\n', b'RESC', b'BOUN', b'FDST', b'DATP',
                    b'AUDI', b'VIDE'}

        for i, rec in enumerate(self.resource_records):
            sig = rec.raw[:4]
            payload = rec.raw
            ext = 'dat'
            prefix = 'binary'
            suffix = ''
            if sig in {b'HUFF', b'CDIC', b'INDX'}: continue
            # TODO: Ignore CNCX records as well
            if sig == b'FONT':
                font = read_font_record(rec.raw)
                if font['err']:
                    raise ValueError('Failed to read font record: %s Headers: %s'%(
                        font['err'], font['headers']))
                payload = (font['font_data'] if font['font_data'] else
                        font['raw_data'])
                prefix, ext = 'fonts', font['ext']
            elif sig not in known_types:
                q = imghdr.what(None, rec.raw)
                if q:
                    prefix, ext = 'images', q

            if prefix == 'binary':
                if sig == b'\xe9\x8e\r\n':
                    suffix = '-EOF'
                elif sig in known_types:
                    suffix = '-' + sig.decode('ascii')

            self.resource_map.append(('%s/%06d%s.%s'%(prefix, i, suffix, ext),
                payload))
示例#2
0
    def extract_resources(self, sections):
        from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF
        resource_map = []
        container = None
        for x in ('fonts', 'images'):
            os.mkdir(x)

        for start, end in self.resource_offsets:
            for i, sec in enumerate(sections[start:end]):
                fname_idx = i + 1
                data = sec[0]
                typ = data[:4]
                href = None
                if typ in {
                        b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN',
                        b'FDST', b'DATP', b'AUDI', b'VIDE', b'RESC', b'CMET',
                        b'PAGE'
                }:
                    pass  # Ignore these records
                elif typ == b'FONT':
                    font = read_font_record(data)
                    href = "fonts/%05d.%s" % (fname_idx, font['ext'])
                    if font['err']:
                        self.log.warn('Reading font record %d failed: %s' %
                                      (fname_idx, font['err']))
                        if font['headers']:
                            self.log.debug('Font record headers: %s' %
                                           font['headers'])
                    with open(href.replace('/', os.sep), 'wb') as f:
                        f.write(font['font_data']
                                if font['font_data'] else font['raw_data'])
                    if font['encrypted']:
                        self.encrypted_fonts.append(href)
                elif typ == b'CONT':
                    if data == b'CONTBOUNDARY':
                        container = None
                        continue
                    container = Container(data)
                elif typ == b'CRES':
                    data, imgtype = container.load_image(data)
                    if data is not None:
                        href = 'images/%05d.%s' % (container.resource_index,
                                                   imgtype)
                        with open(href.replace('/', os.sep), 'wb') as f:
                            f.write(data)
                elif typ == b'\xa0\xa0\xa0\xa0' and len(
                        data) == 4 and container is not None:
                    container.resource_index += 1
                elif container is None:
                    if not (len(data) == len(PLACEHOLDER_GIF)
                            and data == PLACEHOLDER_GIF):
                        imgtype = find_imgtype(data)
                        href = 'images/%05d.%s' % (fname_idx, imgtype)
                        with open(href.replace('/', os.sep), 'wb') as f:
                            f.write(data)

                resource_map.append(href)

        return resource_map
示例#3
0
 def __init__(self, idx, record):
     self.raw = record.raw
     name = '%06d' % idx
     self.font = read_font_record(self.raw)
     if self.font['err']:
         raise ValueError('Failed to read font record: %s Headers: %s' %
                          (self.font['err'], self.font['headers']))
     self.payload = (self.font['font_data']
                     if self.font['font_data'] else self.font['raw_data'])
     self.name = '%s.%s' % (name, self.font['ext'])
示例#4
0
文件: mobi6.py 项目: j-howell/calibre
 def __init__(self, idx, record):
     self.raw = record.raw
     name = '%06d'%idx
     self.font = read_font_record(self.raw)
     if self.font['err']:
         raise ValueError('Failed to read font record: %s Headers: %s'%(
             self.font['err'], self.font['headers']))
     self.payload = (self.font['font_data'] if self.font['font_data'] else
             self.font['raw_data'])
     self.name = '%s.%s'%(name, self.font['ext'])
示例#5
0
    def extract_resources(self, sections):
        from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF
        resource_map = []
        container = None
        for x in ('fonts', 'images'):
            os.mkdir(x)

        for start, end in self.resource_offsets:
            for i, sec in enumerate(sections[start:end]):
                fname_idx = i+1
                data = sec[0]
                typ = data[:4]
                href = None
                if typ in {b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN',
                        b'FDST', b'DATP', b'AUDI', b'VIDE', b'RESC', b'CMET', b'PAGE'}:
                    pass  # Ignore these records
                elif typ == b'FONT':
                    font = read_font_record(data)
                    href = "fonts/%05d.%s" % (fname_idx, font['ext'])
                    if font['err']:
                        self.log.warn('Reading font record %d failed: %s'%(
                            fname_idx, font['err']))
                        if font['headers']:
                            self.log.debug('Font record headers: %s'%font['headers'])
                    with open(href.replace('/', os.sep), 'wb') as f:
                        f.write(font['font_data'] if font['font_data'] else
                                font['raw_data'])
                    if font['encrypted']:
                        self.encrypted_fonts.append(href)
                elif typ == b'CONT':
                    if data == b'CONTBOUNDARY':
                        container = None
                        continue
                    container = Container(data)
                elif typ == b'CRES':
                    data, imgtype = container.load_image(data)
                    if data is not None:
                        href = 'images/%05d.%s'%(container.resource_index, imgtype)
                        with open(href.replace('/', os.sep), 'wb') as f:
                            f.write(data)
                elif typ == b'\xa0\xa0\xa0\xa0' and len(data) == 4 and container is not None:
                    container.resource_index += 1
                elif container is None:
                    if not (len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF):
                        imgtype = find_imgtype(data)
                        href = 'images/%05d.%s'%(fname_idx, imgtype)
                        with open(href.replace('/', os.sep), 'wb') as f:
                            f.write(data)

                resource_map.append(href)

        return resource_map
示例#6
0
    def extract_resources(self):
        resource_map = []
        for x in ("fonts", "images"):
            os.mkdir(x)

        for i, sec in enumerate(self.resource_sections):
            fname_idx = i + 1
            data = sec[0]
            typ = data[:4]
            href = None
            if typ in {
                b"FLIS",
                b"FCIS",
                b"SRCS",
                b"\xe9\x8e\r\n",
                b"RESC",
                b"BOUN",
                b"FDST",
                b"DATP",
                b"AUDI",
                b"VIDE",
            }:
                pass  # Ignore these records
            elif typ == b"FONT":
                font = read_font_record(data)
                href = "fonts/%05d.%s" % (fname_idx, font["ext"])
                if font["err"]:
                    self.log.warn("Reading font record %d failed: %s" % (fname_idx, font["err"]))
                    if font["headers"]:
                        self.log.debug("Font record headers: %s" % font["headers"])
                with open(href.replace("/", os.sep), "wb") as f:
                    f.write(font["font_data"] if font["font_data"] else font["raw_data"])
                if font["encrypted"]:
                    self.encrypted_fonts.append(href)
            else:
                imgtype = what(None, data)
                if imgtype is None:
                    from calibre.utils.magick.draw import identify_data

                    try:
                        imgtype = identify_data(data)[2]
                    except Exception:
                        imgtype = "unknown"
                href = "images/%05d.%s" % (fname_idx, imgtype)
                with open(href.replace("/", os.sep), "wb") as f:
                    f.write(data)

            resource_map.append(href)

        return resource_map
示例#7
0
文件: mobi8.py 项目: mrmac123/calibre
    def extract_resources(self):
        from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF
        resource_map = []
        for x in ('fonts', 'images'):
            os.mkdir(x)

        for i, sec in enumerate(self.resource_sections):
            fname_idx = i + 1
            data = sec[0]
            typ = data[:4]
            href = None
            if typ in {
                    b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN',
                    b'FDST', b'DATP', b'AUDI', b'VIDE'
            }:
                pass  # Ignore these records
            elif typ == b'RESC':
                self.resc_data = read_resc_record(data)
            elif typ == b'FONT':
                font = read_font_record(data)
                href = "fonts/%05d.%s" % (fname_idx, font['ext'])
                if font['err']:
                    self.log.warn('Reading font record %d failed: %s' %
                                  (fname_idx, font['err']))
                    if font['headers']:
                        self.log.debug('Font record headers: %s' %
                                       font['headers'])
                with open(href.replace('/', os.sep), 'wb') as f:
                    f.write(font['font_data']
                            if font['font_data'] else font['raw_data'])
                if font['encrypted']:
                    self.encrypted_fonts.append(href)
            else:
                if not (len(data) == len(PLACEHOLDER_GIF)
                        and data == PLACEHOLDER_GIF):
                    imgtype = what(None, data)
                    if imgtype is None:
                        from calibre.utils.magick.draw import identify_data
                        try:
                            imgtype = identify_data(data)[2]
                        except Exception:
                            imgtype = 'unknown'
                    href = 'images/%05d.%s' % (fname_idx, imgtype)
                    with open(href.replace('/', os.sep), 'wb') as f:
                        f.write(data)

            resource_map.append(href)

        return resource_map
示例#8
0
文件: mobi8.py 项目: alip/calibre
    def extract_resources(self):
        from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF
        resource_map = []
        for x in ('fonts', 'images'):
            os.mkdir(x)

        for i, sec in enumerate(self.resource_sections):
            fname_idx = i+1
            data = sec[0]
            typ = data[:4]
            href = None
            if typ in {b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'BOUN',
                       b'FDST', b'DATP', b'AUDI', b'VIDE'}:
                pass  # Ignore these records
            elif typ == b'RESC':
                self.resc_data = read_resc_record(data)
            elif typ == b'FONT':
                font = read_font_record(data)
                href = "fonts/%05d.%s" % (fname_idx, font['ext'])
                if font['err']:
                    self.log.warn('Reading font record %d failed: %s'%(
                        fname_idx, font['err']))
                    if font['headers']:
                        self.log.debug('Font record headers: %s'%font['headers'])
                with open(href.replace('/', os.sep), 'wb') as f:
                    f.write(font['font_data'] if font['font_data'] else
                            font['raw_data'])
                if font['encrypted']:
                    self.encrypted_fonts.append(href)
            else:
                if len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF:
                    continue
                imgtype = what(None, data)
                if imgtype is None:
                    from calibre.utils.magick.draw import identify_data
                    try:
                        imgtype = identify_data(data)[2]
                    except Exception:
                        imgtype = 'unknown'
                href = 'images/%05d.%s'%(fname_idx, imgtype)
                with open(href.replace('/', os.sep), 'wb') as f:
                    f.write(data)

            resource_map.append(href)

        return resource_map
示例#9
0
文件: mobi8.py 项目: sss/calibre
    def extract_resources(self):
        self.resource_map = []
        known_types = {
            b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'RESC', b'BOUN',
            b'FDST', b'DATP', b'AUDI', b'VIDE'
        }

        for i, rec in enumerate(self.resource_records):
            sig = rec.raw[:4]
            payload = rec.raw
            ext = 'dat'
            prefix = 'binary'
            suffix = ''
            if sig in {b'HUFF', b'CDIC', b'INDX'}:
                continue
            # TODO: Ignore CNCX records as well
            if sig == b'FONT':
                font = read_font_record(rec.raw)
                if font['err']:
                    raise ValueError(
                        'Failed to read font record: %s Headers: %s' %
                        (font['err'], font['headers']))
                payload = (font['font_data']
                           if font['font_data'] else font['raw_data'])
                prefix, ext = 'fonts', font['ext']
            elif sig not in known_types:
                q = what(None, rec.raw)
                if q:
                    prefix, ext = 'images', q

            if prefix == 'binary':
                if sig == b'\xe9\x8e\r\n':
                    suffix = '-EOF'
                elif sig in known_types:
                    suffix = '-' + sig.decode('ascii')

            self.resource_map.append(
                ('%s/%06d%s.%s' % (prefix, i, suffix, ext), payload))
示例#10
0
    def extract_resources(self):
        resource_map = []
        for x in ('fonts', 'images'):
            os.mkdir(x)

        for i, sec in enumerate(self.resource_sections):
            fname_idx = i+1
            data = sec[0]
            typ = data[:4]
            href = None
            if typ in {b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n',
                    b'RESC', b'BOUN', b'FDST', b'DATP', b'AUDI', b'VIDE'}:
                pass # Ignore these records
            elif typ == b'FONT':
                font = read_font_record(data)
                href = "fonts/%05d.%s" % (fname_idx, font['ext'])
                if font['err']:
                    self.log.warn('Reading font record %d failed: %s'%(
                        fname_idx, font['err']))
                    if font['headers']:
                        self.log.debug('Font record headers: %s'%font['headers'])
                with open(href.replace('/', os.sep), 'wb') as f:
                    f.write(font['font_data'] if font['font_data'] else
                            font['raw_data'])
                if font['encrypted']:
                    self.encrypted_fonts.append(href)
            else:
                imgtype = imghdr.what(None, data)
                if imgtype is None:
                    imgtype = 'unknown'
                href = 'images/%05d.%s'%(fname_idx, imgtype)
                with open(href.replace('/', os.sep), 'wb') as f:
                    f.write(data)

            resource_map.append(href)

        return resource_map
示例#11
0
文件: mobi8.py 项目: j-howell/calibre
    def extract_resources(self, records):
        self.resource_map = []
        self.containers = []
        known_types = {b'FLIS', b'FCIS', b'SRCS',
                    b'\xe9\x8e\r\n', b'RESC', b'BOUN', b'FDST', b'DATP',
                    b'AUDI', b'VIDE', b'CRES', b'CONT', b'CMET', b'PAGE'}
        container = None

        for i, rec in enumerate(records):
            for (l, r, offset) in self.resource_ranges:
                if l <= i <= r:
                    resource_index = i + 1
                    if offset is not None and resource_index >= offset:
                        resource_index -= offset
                    break
            else:
                continue
            sig = rec.raw[:4]
            payload = rec.raw
            ext = 'dat'
            prefix = 'binary'
            suffix = ''
            if sig in {b'HUFF', b'CDIC', b'INDX'}:
                continue
            # TODO: Ignore CNCX records as well
            if sig == b'FONT':
                font = read_font_record(rec.raw)
                if font['err']:
                    raise ValueError('Failed to read font record: %s Headers: %s'%(
                        font['err'], font['headers']))
                payload = (font['font_data'] if font['font_data'] else
                        font['raw_data'])
                prefix, ext = 'fonts', font['ext']
            elif sig == b'CONT':
                if payload == b'CONTBOUNDARY':
                    self.containers.append(container)
                    container = None
                    continue
                container = ContainerHeader(payload)
            elif sig == b'CRES':
                container.resources.append(payload)
                if container.is_image_container:
                    payload = payload[12:]
                    q = what(None, payload)
                    if q:
                        prefix, ext = 'hd-images', q
                        resource_index = len(container.resources)
            elif sig == b'\xa0\xa0\xa0\xa0' and len(payload) == 4:
                if container is None:
                    print('Found an end of container record with no container, ignoring')
                else:
                    container.resources.append(None)
                continue
            elif sig not in known_types:
                if container is not None and len(container.resources) == container.num_of_resource_records:
                    container.add_hrefs(payload)
                    continue
                q = what(None, rec.raw)
                if q:
                    prefix, ext = 'images', q

            if prefix == 'binary':
                if sig == b'\xe9\x8e\r\n':
                    suffix = '-EOF'
                elif sig in known_types:
                    suffix = '-' + sig.decode('ascii')

            self.resource_map.append(('%s/%06d%s.%s'%(prefix, resource_index, suffix, ext),
                payload))
示例#12
0
    def extract_resources(self, records):
        self.resource_map = []
        self.containers = []
        known_types = {
            b'FLIS', b'FCIS', b'SRCS', b'\xe9\x8e\r\n', b'RESC', b'BOUN',
            b'FDST', b'DATP', b'AUDI', b'VIDE', b'CRES', b'CONT', b'CMET',
            b'PAGE'
        }
        container = None

        for i, rec in enumerate(records):
            for (l, r, offset) in self.resource_ranges:
                if l <= i <= r:
                    resource_index = i + 1
                    if offset is not None and resource_index >= offset:
                        resource_index -= offset
                    break
            else:
                continue
            sig = rec.raw[:4]
            payload = rec.raw
            ext = 'dat'
            prefix = 'binary'
            suffix = ''
            if sig in {b'HUFF', b'CDIC', b'INDX'}:
                continue
            # TODO: Ignore CNCX records as well
            if sig == b'FONT':
                font = read_font_record(rec.raw)
                if font['err']:
                    raise ValueError(
                        'Failed to read font record: %s Headers: %s' %
                        (font['err'], font['headers']))
                payload = (font['font_data']
                           if font['font_data'] else font['raw_data'])
                prefix, ext = 'fonts', font['ext']
            elif sig == b'CONT':
                if payload == b'CONTBOUNDARY':
                    self.containers.append(container)
                    container = None
                    continue
                container = ContainerHeader(payload)
            elif sig == b'CRES':
                container.resources.append(payload)
                if container.is_image_container:
                    payload = payload[12:]
                    q = what(None, payload)
                    if q:
                        prefix, ext = 'hd-images', q
                        resource_index = len(container.resources)
            elif sig == b'\xa0\xa0\xa0\xa0' and len(payload) == 4:
                if container is None:
                    print(
                        'Found an end of container record with no container, ignoring'
                    )
                else:
                    container.resources.append(None)
                continue
            elif sig not in known_types:
                if container is not None and len(
                        container.resources
                ) == container.num_of_resource_records:
                    container.add_hrefs(payload)
                    continue
                q = what(None, rec.raw)
                if q:
                    prefix, ext = 'images', q

            if prefix == 'binary':
                if sig == b'\xe9\x8e\r\n':
                    suffix = '-EOF'
                elif sig in known_types:
                    suffix = '-' + sig.decode('ascii')

            self.resource_map.append(
                ('%s/%06d%s.%s' % (prefix, resource_index, suffix, ext),
                 payload))
示例#13
0
文件: mobi8.py 项目: mirror/calibre
    def extract_resources(self, sections):
        from calibre.ebooks.mobi.writer2.resources import PLACEHOLDER_GIF

        resource_map = []
        container = None
        for x in ("fonts", "images"):
            os.mkdir(x)

        for start, end in self.resource_offsets:
            for i, sec in enumerate(sections[start:end]):
                fname_idx = i + 1
                data = sec[0]
                typ = data[:4]
                href = None
                if typ in {
                    b"FLIS",
                    b"FCIS",
                    b"SRCS",
                    b"\xe9\x8e\r\n",
                    b"BOUN",
                    b"FDST",
                    b"DATP",
                    b"AUDI",
                    b"VIDE",
                    b"RESC",
                    b"CMET",
                    b"PAGE",
                }:
                    pass  # Ignore these records
                elif typ == b"FONT":
                    font = read_font_record(data)
                    href = "fonts/%05d.%s" % (fname_idx, font["ext"])
                    if font["err"]:
                        self.log.warn("Reading font record %d failed: %s" % (fname_idx, font["err"]))
                        if font["headers"]:
                            self.log.debug("Font record headers: %s" % font["headers"])
                    with open(href.replace("/", os.sep), "wb") as f:
                        f.write(font["font_data"] if font["font_data"] else font["raw_data"])
                    if font["encrypted"]:
                        self.encrypted_fonts.append(href)
                elif typ == b"CONT":
                    if data == b"CONTBOUNDARY":
                        container = None
                        continue
                    container = Container(data)
                elif typ == b"CRES":
                    data, imgtype = container.load_image(data)
                    if data is not None:
                        href = "images/%05d.%s" % (container.resource_index, imgtype)
                        with open(href.replace("/", os.sep), "wb") as f:
                            f.write(data)
                elif typ == b"\xa0\xa0\xa0\xa0" and len(data) == 4 and container is not None:
                    container.resource_index += 1
                elif container is None:
                    if not (len(data) == len(PLACEHOLDER_GIF) and data == PLACEHOLDER_GIF):
                        imgtype = find_imgtype(data)
                        href = "images/%05d.%s" % (fname_idx, imgtype)
                        with open(href.replace("/", os.sep), "wb") as f:
                            f.write(data)

                resource_map.append(href)

        return resource_map
示例#14
0
    def extract_resources(self, records):
        self.resource_map = []
        self.containers = []
        known_types = {
            b"FLIS",
            b"FCIS",
            b"SRCS",
            b"\xe9\x8e\r\n",
            b"RESC",
            b"BOUN",
            b"FDST",
            b"DATP",
            b"AUDI",
            b"VIDE",
            b"CRES",
            b"CONT",
            b"CMET",
            b"PAGE",
        }
        container = None

        for i, rec in enumerate(records):
            for (l, r, offset) in self.resource_ranges:
                if l <= i <= r:
                    resource_index = i + 1
                    if offset is not None and resource_index >= offset:
                        resource_index -= offset
                    break
            else:
                continue
            sig = rec.raw[:4]
            payload = rec.raw
            ext = "dat"
            prefix = "binary"
            suffix = ""
            if sig in {b"HUFF", b"CDIC", b"INDX"}:
                continue
            # TODO: Ignore CNCX records as well
            if sig == b"FONT":
                font = read_font_record(rec.raw)
                if font["err"]:
                    raise ValueError("Failed to read font record: %s Headers: %s" % (font["err"], font["headers"]))
                payload = font["font_data"] if font["font_data"] else font["raw_data"]
                prefix, ext = "fonts", font["ext"]
            elif sig == b"CONT":
                if payload == b"CONTBOUNDARY":
                    self.containers.append(container)
                    container = None
                    continue
                container = ContainerHeader(payload)
            elif sig == b"CRES":
                container.resources.append(payload)
                if container.is_image_container:
                    payload = payload[12:]
                    q = what(None, payload)
                    if q:
                        prefix, ext = "hd-images", q
                        resource_index = len(container.resources)
            elif sig == b"\xa0\xa0\xa0\xa0" and len(payload) == 4:
                container.resources.append(None)
                continue
            elif sig not in known_types:
                if container is not None and len(container.resources) == container.num_of_resource_records:
                    container.add_hrefs(payload)
                    continue
                q = what(None, rec.raw)
                if q:
                    prefix, ext = "images", q

            if prefix == "binary":
                if sig == b"\xe9\x8e\r\n":
                    suffix = "-EOF"
                elif sig in known_types:
                    suffix = "-" + sig.decode("ascii")

            self.resource_map.append(("%s/%06d%s.%s" % (prefix, resource_index, suffix, ext), payload))