summaryrefslogtreecommitdiff
path: root/source/cbz/mucbz.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2014-08-27 13:14:22 +0200
committerTor Andersson <tor.andersson@artifex.com>2014-08-27 13:15:56 +0200
commit95c1d14aff266ef9c544c97c669df2257a978e1c (patch)
treeec2f01530eb0f201d64277ef996030d8d49b1315 /source/cbz/mucbz.c
parentd291833a0e41076ecdca6ef67c4a459da36df746 (diff)
downloadmupdf-95c1d14aff266ef9c544c97c669df2257a978e1c.tar.xz
cbz: Use csize and usize fields from central directory.
Don't rely on the csize and usize fileds being set in the individual entry headers.
Diffstat (limited to 'source/cbz/mucbz.c')
-rw-r--r--source/cbz/mucbz.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/source/cbz/mucbz.c b/source/cbz/mucbz.c
index f018d1c9..2c7bd118 100644
--- a/source/cbz/mucbz.c
+++ b/source/cbz/mucbz.c
@@ -29,6 +29,7 @@ struct cbz_entry_s
{
char *name;
int offset;
+ int csize, usize;
};
struct cbz_document_s
@@ -72,16 +73,15 @@ cbz_zip_free(void *ctx, void *ptr)
}
static unsigned char *
-cbz_read_zip_entry(cbz_document *doc, int offset, int *sizep)
+cbz_read_zip_entry(cbz_document *doc, cbz_entry *entry, int *sizep)
{
fz_context *ctx = doc->ctx;
fz_stream *file = doc->file;
int sig, general, method, namelength, extralength;
- unsigned long csize, usize;
unsigned char *cdata;
int code;
- fz_seek(file, offset, 0);
+ fz_seek(file, entry->offset, 0);
sig = getlong(doc->file);
if (sig != ZIP_LOCAL_FILE_SIG)
@@ -96,17 +96,17 @@ cbz_read_zip_entry(cbz_document *doc, int offset, int *sizep)
(void) getshort(doc->file); /* file time */
(void) getshort(doc->file); /* file date */
(void) getlong(doc->file); /* crc-32 */
- csize = getlong(doc->file); /* csize */
- usize = getlong(doc->file); /* usize */
+ (void) getlong(doc->file); /* csize */
+ (void) getlong(doc->file); /* usize */
namelength = getshort(doc->file);
extralength = getshort(doc->file);
fz_seek(file, namelength + extralength, 1);
- cdata = fz_malloc(ctx, csize);
+ cdata = fz_malloc(ctx, entry->csize);
fz_try(ctx)
{
- fz_read(file, cdata, csize);
+ fz_read(file, cdata, entry->csize);
}
fz_catch(ctx)
{
@@ -116,13 +116,13 @@ cbz_read_zip_entry(cbz_document *doc, int offset, int *sizep)
if (method == 0)
{
- *sizep = usize;
+ *sizep = entry->usize;
return cdata;
}
if (method == 8)
{
- unsigned char *udata = fz_malloc(ctx, usize);
+ unsigned char *udata = fz_malloc(ctx, entry->usize);
z_stream stream;
memset(&stream, 0, sizeof stream);
@@ -130,9 +130,9 @@ cbz_read_zip_entry(cbz_document *doc, int offset, int *sizep)
stream.zfree = cbz_zip_free;
stream.opaque = ctx;
stream.next_in = cdata;
- stream.avail_in = csize;
+ stream.avail_in = entry->csize;
stream.next_out = udata;
- stream.avail_out = usize;
+ stream.avail_out = entry->usize;
fz_try(ctx)
{
@@ -158,7 +158,7 @@ cbz_read_zip_entry(cbz_document *doc, int offset, int *sizep)
fz_rethrow(ctx);
}
- *sizep = usize;
+ *sizep = entry->usize;
return udata;
}
@@ -257,8 +257,8 @@ cbz_read_zip_dir_imp(cbz_document *doc, int startoffset)
(void) getshort(file); /* last mod file time */
(void) getshort(file); /* last mod file date */
(void) getlong(file); /* crc-32 */
- (void) getlong(file); /* csize */
- (void) getlong(file); /* usize */
+ entry->csize = getlong(file); /* csize */
+ entry->usize = getlong(file); /* usize */
namesize = getshort(file);
metasize = getshort(file);
commentsize = getshort(file);
@@ -401,28 +401,26 @@ cbz_page *
cbz_load_page(cbz_document *doc, int number)
{
fz_context *ctx = doc->ctx;
- unsigned char *data;
+ unsigned char *data = NULL;
cbz_page *page = NULL;
int size;
if (number < 0 || number >= doc->page_count)
return NULL;
- number = doc->page[number];
-
fz_var(data);
fz_var(page);
+
fz_try(ctx)
{
+ data = cbz_read_zip_entry(doc, &doc->entry[doc->page[number]], &size);
page = fz_malloc_struct(ctx, cbz_page);
- page->image = NULL;
-
- data = cbz_read_zip_entry(doc, doc->entry[number].offset, &size);
-
page->image = fz_new_image_from_data(ctx, data, size);
+ data = NULL; /* image owns data now */
}
fz_catch(ctx)
{
+ fz_free(ctx, data);
cbz_free_page(doc, page);
fz_rethrow(ctx);
}