summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cbz/mucbz.c5
-rw-r--r--cbz/mucbz.h2
-rw-r--r--fitz/doc_document.c58
-rw-r--r--fitz/filt_lzwd.c8
-rw-r--r--fitz/fitz.h10
-rw-r--r--pdf/mupdf-internal.h2
-rw-r--r--pdf/mupdf.h2
-rw-r--r--pdf/pdf_xref.c9
-rw-r--r--pdf/pdf_xref_aux.c13
-rw-r--r--xps/muxps.h2
-rw-r--r--xps/xps_zip.c5
11 files changed, 81 insertions, 35 deletions
diff --git a/cbz/mucbz.c b/cbz/mucbz.c
index f557236f..83737833 100644
--- a/cbz/mucbz.c
+++ b/cbz/mucbz.c
@@ -285,9 +285,8 @@ cbz_read_zip_dir(cbz_document *doc)
}
cbz_document *
-cbz_open_document_with_stream(fz_stream *file)
+cbz_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
- fz_context *ctx = file->ctx;
cbz_document *doc;
doc = fz_malloc_struct(ctx, cbz_document);
@@ -323,7 +322,7 @@ cbz_open_document(fz_context *ctx, char *filename)
fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno));
fz_try(ctx) {
- doc = cbz_open_document_with_stream(file);
+ doc = cbz_open_document_with_stream(ctx, file);
} fz_always(ctx) {
fz_close(file);
} fz_catch(ctx) {
diff --git a/cbz/mucbz.h b/cbz/mucbz.h
index 080423bf..05288eb3 100644
--- a/cbz/mucbz.h
+++ b/cbz/mucbz.h
@@ -30,7 +30,7 @@ cbz_document *cbz_open_document(fz_context *ctx, char *filename);
fz_open_file_w or fz_open_fd for opening a stream, and
fz_close for closing an open stream.
*/
-cbz_document *cbz_open_document_with_stream(fz_stream *file);
+cbz_document *cbz_open_document_with_stream(fz_context *ctx, fz_stream *file);
/*
cbz_close_document: Closes and frees an opened document.
diff --git a/fitz/doc_document.c b/fitz/doc_document.c
index f60f3468..8b8197e7 100644
--- a/fitz/doc_document.c
+++ b/fitz/doc_document.c
@@ -5,6 +5,11 @@ extern struct pdf_document *pdf_open_document(fz_context *ctx, char *filename);
extern struct xps_document *xps_open_document(fz_context *ctx, char *filename);
extern struct cbz_document *cbz_open_document(fz_context *ctx, char *filename);
+extern struct pdf_document *pdf_open_document_with_stream(fz_context *ctx, fz_stream *file);
+extern struct xps_document *xps_open_document_with_stream(fz_context *ctx, fz_stream *file);
+extern struct cbz_document *cbz_open_document_with_stream(fz_context *ctx, fz_stream *file);
+
+
static inline int fz_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
@@ -24,25 +29,48 @@ static inline int fz_strcasecmp(char *a, char *b)
}
fz_document *
+fz_open_document_with_stream(fz_context *ctx, char *magic, fz_stream *stream)
+{
+ char *ext = strrchr(magic, '.');
+
+ if (ext)
+ {
+ if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels"))
+ return (fz_document*) xps_open_document_with_stream(ctx, stream);
+ if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
+ return (fz_document*) cbz_open_document_with_stream(ctx, stream);
+ if (!fz_strcasecmp(ext, ".pdf"))
+ return (fz_document*) pdf_open_document_with_stream(ctx, stream);
+ }
+
+ if (!strcmp(magic, "cbz") || !strcmp(magic, "application/x-cbz"))
+ return (fz_document*) cbz_open_document_with_stream(ctx, stream);
+ if (!strcmp(magic, "xps") || !strcmp(magic, "application/vnd.ms-xpsdocument"))
+ return (fz_document*) xps_open_document_with_stream(ctx, stream);
+ if (!strcmp(magic, "pdf") || !strcmp(magic, "application/pdf"))
+ return (fz_document*) pdf_open_document_with_stream(ctx, stream);
+
+ /* last guess: pdf */
+ return (fz_document*) pdf_open_document_with_stream(ctx, stream);
+}
+
+fz_document *
fz_open_document(fz_context *ctx, char *filename)
{
char *ext = strrchr(filename, '.');
- if (ext && (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels")))
- return (fz_document*) xps_open_document(ctx, filename);
- if (ext && !fz_strcasecmp(ext, ".cbz"))
- return (fz_document*) cbz_open_document(ctx, filename);
-#if 0
- /* We used to only open pdf files if they ended in .pdf. For now,
- * until we move to detecting filetypes by their content, we disable
- * this code, and assume that any file that hasn't matched an
- * extension already, is a PDF. */
- if (ext && !fz_strcasecmp(ext, ".pdf"))
- return (fz_document*) pdf_open_document(ctx, filename);
- fz_throw(ctx, "unknown document type: '%s'", filename);
- return NULL;
-#else
+
+ if (ext)
+ {
+ if (!fz_strcasecmp(ext, ".xps") || !fz_strcasecmp(ext, ".rels"))
+ return (fz_document*) xps_open_document(ctx, filename);
+ if (!fz_strcasecmp(ext, ".cbz") || !fz_strcasecmp(ext, ".zip"))
+ return (fz_document*) cbz_open_document(ctx, filename);
+ if (!fz_strcasecmp(ext, ".pdf"))
+ return (fz_document*) pdf_open_document(ctx, filename);
+ }
+
+ /* last guess: pdf */
return (fz_document*) pdf_open_document(ctx, filename);
-#endif
}
void
diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c
index 3ee4d34c..b1aa4421 100644
--- a/fitz/filt_lzwd.c
+++ b/fitz/filt_lzwd.c
@@ -80,6 +80,12 @@ read_lzwd(fz_stream *stm, unsigned char *buf, int len)
break;
}
+ if (next_code >= NUM_CODES && code != LZW_CLEAR)
+ {
+ fz_warn(stm->ctx, "missing clear code in lzw decode");
+ code = LZW_CLEAR;
+ }
+
if (code == LZW_CLEAR)
{
code_bits = MIN_BITS;
@@ -112,7 +118,7 @@ read_lzwd(fz_stream *stm, unsigned char *buf, int len)
{
code_bits ++;
if (code_bits > MAX_BITS)
- code_bits = MAX_BITS; /* FIXME */
+ code_bits = MAX_BITS;
}
old_code = code;
diff --git a/fitz/fitz.h b/fitz/fitz.h
index 6c744aec..3d98bffd 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -2143,6 +2143,16 @@ typedef struct fz_page_s fz_page;
fz_document *fz_open_document(fz_context *ctx, char *filename);
/*
+ fz_open_document_with_stream: Open a PDF, XPS or CBZ document.
+
+ Open a document using the specified stream object rather than
+ opening a file on disk.
+
+ magic: a string used to detect document type; either a file name or mime-type.
+*/
+fz_document *fz_open_document_with_stream(fz_context *ctx, char *magic, fz_stream *stream);
+
+/*
fz_close_document: Close and free an open document.
The resource store in the context associated with fz_document
diff --git a/pdf/mupdf-internal.h b/pdf/mupdf-internal.h
index 4d3de41a..eca0b67f 100644
--- a/pdf/mupdf-internal.h
+++ b/pdf/mupdf-internal.h
@@ -230,7 +230,7 @@ struct pdf_document_s
};
pdf_document *pdf_open_document_no_run(fz_context *ctx, const char *filename);
-pdf_document *pdf_open_document_no_run_with_stream(fz_stream *file);
+pdf_document *pdf_open_document_no_run_with_stream(fz_context *ctx, fz_stream *file);
void pdf_cache_object(pdf_document *doc, int num, int gen);
diff --git a/pdf/mupdf.h b/pdf/mupdf.h
index 16b0f58b..a2fe90f7 100644
--- a/pdf/mupdf.h
+++ b/pdf/mupdf.h
@@ -167,7 +167,7 @@ pdf_document *pdf_open_document(fz_context *ctx, const char *filename);
fz_open_file_w or fz_open_fd for opening a stream, and
fz_close for closing an open stream.
*/
-pdf_document *pdf_open_document_with_stream(fz_stream *file);
+pdf_document *pdf_open_document_with_stream(fz_context *ctx, fz_stream *file);
/*
pdf_close_document: Closes and frees an opened PDF document.
diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c
index a8b8ab43..b8e1ce40 100644
--- a/pdf/pdf_xref.c
+++ b/pdf/pdf_xref.c
@@ -1269,9 +1269,8 @@ pdf_interact(pdf_document *doc)
*/
static pdf_document *
-pdf_new_document(fz_stream *file)
+pdf_new_document(fz_context *ctx, fz_stream *file)
{
- fz_context *ctx = file->ctx;
pdf_document *doc = fz_malloc_struct(ctx, pdf_document);
doc->super.close = (void*)pdf_close_document;
@@ -1299,9 +1298,9 @@ pdf_new_document(fz_stream *file)
}
pdf_document *
-pdf_open_document_no_run_with_stream(fz_stream *file)
+pdf_open_document_no_run_with_stream(fz_context *ctx, fz_stream *file)
{
- pdf_document *doc = pdf_new_document(file);
+ pdf_document *doc = pdf_new_document(ctx, file);
pdf_init_document(doc);
return doc;
}
@@ -1317,7 +1316,7 @@ pdf_open_document_no_run(fz_context *ctx, const char *filename)
fz_try(ctx)
{
file = fz_open_file(ctx, filename);
- doc = pdf_new_document(file);
+ doc = pdf_new_document(ctx, file);
pdf_init_document(doc);
}
fz_always(ctx)
diff --git a/pdf/pdf_xref_aux.c b/pdf/pdf_xref_aux.c
index 531a7e75..bf2fe19b 100644
--- a/pdf/pdf_xref_aux.c
+++ b/pdf/pdf_xref_aux.c
@@ -9,11 +9,16 @@
resulting executables.
*/
+static void pdf_run_page_shim(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie)
+{
+ pdf_run_page((pdf_document*)doc, (pdf_page*)page, dev, transform, cookie);
+}
+
pdf_document *
-pdf_open_document_with_stream(fz_stream *file)
+pdf_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
- pdf_document *doc = pdf_open_document_no_run_with_stream(file);
- doc->super.run_page = (void*)pdf_run_page;
+ pdf_document *doc = pdf_open_document_no_run_with_stream(ctx, file);
+ doc->super.run_page = pdf_run_page_shim;
return doc;
}
@@ -21,6 +26,6 @@ pdf_document *
pdf_open_document(fz_context *ctx, const char *filename)
{
pdf_document *doc = pdf_open_document_no_run(ctx, filename);
- doc->super.run_page = (void*)pdf_run_page;
+ doc->super.run_page = pdf_run_page_shim;
return doc;
}
diff --git a/xps/muxps.h b/xps/muxps.h
index 128ed35f..8ec577af 100644
--- a/xps/muxps.h
+++ b/xps/muxps.h
@@ -45,7 +45,7 @@ xps_document *xps_open_document(fz_context *ctx, char *filename);
fz_open_file_w or fz_open_fd for opening a stream, and
fz_close for closing an open stream.
*/
-xps_document *xps_open_document_with_stream(fz_stream *file);
+xps_document *xps_open_document_with_stream(fz_context *ctx, fz_stream *file);
/*
xps_close_document: Closes and frees an opened document.
diff --git a/xps/xps_zip.c b/xps/xps_zip.c
index e36f9c0f..762fe2ba 100644
--- a/xps/xps_zip.c
+++ b/xps/xps_zip.c
@@ -576,9 +576,8 @@ xps_open_document_with_directory(fz_context *ctx, char *directory)
}
xps_document *
-xps_open_document_with_stream(fz_stream *file)
+xps_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
- fz_context *ctx = file->ctx;
xps_document *doc;
doc = fz_malloc_struct(ctx, xps_document);
@@ -624,7 +623,7 @@ xps_open_document(fz_context *ctx, char *filename)
fz_try(ctx)
{
- doc = xps_open_document_with_stream(file);
+ doc = xps_open_document_with_stream(ctx, file);
}
fz_always(ctx)
{