From 15cab201d3c98dc6580c8cf592d94ab226f96db5 Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Sat, 13 Aug 2016 17:45:14 +0800 Subject: Make fz_archive a generic archive type. Previously it was inherently tied to zip archives and directories. Now these are separated out into distinct subclasses. This prepares for support for further archive formats. --- source/cbz/mucbz.c | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'source/cbz/mucbz.c') diff --git a/source/cbz/mucbz.c b/source/cbz/mucbz.c index 0b5de93e..17a881cc 100644 --- a/source/cbz/mucbz.c +++ b/source/cbz/mucbz.c @@ -19,7 +19,7 @@ struct cbz_page_s struct cbz_document_s { fz_document super; - fz_archive *zip; + fz_archive *arch; int page_count; const char **page; }; @@ -75,10 +75,10 @@ cbz_compare_page_names(const void *a, const void *b) static void cbz_create_page_list(fz_context *ctx, cbz_document *doc) { - fz_archive *zip = doc->zip; + fz_archive *arch = doc->arch; int i, k, count; - count = fz_count_archive_entries(ctx, zip); + count = fz_count_archive_entries(ctx, arch); doc->page_count = 0; doc->page = fz_malloc_array(ctx, count, sizeof *doc->page); @@ -87,8 +87,13 @@ cbz_create_page_list(fz_context *ctx, cbz_document *doc) { for (k = 0; cbz_ext_list[k]; k++) { - const char *name = fz_list_archive_entry(ctx, zip, i); - const char *ext = name ? strrchr(name, '.') : NULL; + const char *name; + const char *ext; + + if (arch) + name = fz_list_archive_entry(ctx, arch, i); + + ext = name ? strrchr(name, '.') : NULL; if (ext && !fz_strcasecmp(ext, cbz_ext_list[k])) { doc->page[doc->page_count++] = name; @@ -103,7 +108,7 @@ cbz_create_page_list(fz_context *ctx, cbz_document *doc) static void cbz_drop_document(fz_context *ctx, cbz_document *doc) { - fz_drop_archive(ctx, doc->zip); + fz_drop_archive(ctx, doc->arch); fz_free(ctx, (char **)doc->page); fz_free(ctx, doc); } @@ -155,7 +160,7 @@ cbz_load_page(fz_context *ctx, cbz_document *doc, int number) { unsigned char *data = NULL; cbz_page *page = NULL; - fz_buffer *buf; + fz_buffer *buf = NULL; if (number < 0 || number >= doc->page_count) return NULL; @@ -163,7 +168,11 @@ cbz_load_page(fz_context *ctx, cbz_document *doc, int number) fz_var(data); fz_var(page); - buf = fz_read_archive_entry(ctx, doc->zip, doc->page[number]); + if (doc->arch) + buf = fz_read_archive_entry(ctx, doc->arch, doc->page[number]); + if (!buf) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load cbz page"); + fz_try(ctx) { page = fz_new_page(ctx, sizeof *page); @@ -190,14 +199,16 @@ static int cbz_lookup_metadata(fz_context *ctx, cbz_document *doc, const char *key, char *buf, int size) { if (!strcmp(key, "format")) - return (int)fz_strlcpy(buf, "CBZ", size); + return (int) fz_strlcpy(buf, fz_archive_format(ctx, doc->arch), size); return -1; } static cbz_document * cbz_open_document_with_stream(fz_context *ctx, fz_stream *file) { - cbz_document *doc = fz_new_document(ctx, cbz_document); + cbz_document *doc; + + doc = fz_new_document(ctx, cbz_document); doc->super.drop_document = (fz_document_drop_fn *)cbz_drop_document; doc->super.count_pages = (fz_document_count_pages_fn *)cbz_count_pages; @@ -206,7 +217,7 @@ cbz_open_document_with_stream(fz_context *ctx, fz_stream *file) fz_try(ctx) { - doc->zip = fz_open_archive_with_stream(ctx, file); + doc->arch = fz_open_archive_with_stream(ctx, file); cbz_create_page_list(ctx, doc); } fz_catch(ctx) @@ -236,18 +247,15 @@ cbz_open_document(fz_context *ctx, const char *filename) } static int -cbz_recognize(fz_context *doc, const char *magic) +cbz_recognize(fz_context *ctx, const char *magic) { char *ext = strrchr(magic, '.'); - - if (ext) - { - if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip")) - return 100; - } - if (!strcmp(magic, "cbz") || !strcmp(magic, "application/x-cbz")) + if ((ext && !fz_strcasecmp(ext, ".cbz")) || !strcmp(magic, "cbz") || + !strcmp(magic, "application/x-cbz")) + return 100; + if ((ext && !fz_strcasecmp(ext, ".zip")) || !strcmp(magic, "zip") || + !strcmp(magic, "application/zip")) return 100; - return 0; } -- cgit v1.2.3