summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2011-04-07 14:39:17 +0200
committerTor Andersson <tor.andersson@artifex.com>2011-04-07 14:39:17 +0200
commit567ad2330ec8be4f23014a0a3e8dce11bcd1eada (patch)
tree0ca643fd1350da7355e7152fe460f2b9d855e285
parent6294585ac2dfe73654a9ac5965517219e4ea7ede (diff)
downloadmupdf-567ad2330ec8be4f23014a0a3e8dce11bcd1eada.tar.xz
Use fz_stream instead of FILE* to open XPS files.
-rw-r--r--apps/pdfapp.c2
-rw-r--r--pdf/pdf_xref.c5
-rw-r--r--xps/muxps.h2
-rw-r--r--xps/xps_zip.c130
4 files changed, 86 insertions, 53 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c
index 46459497..fc2cfdfb 100644
--- a/apps/pdfapp.c
+++ b/apps/pdfapp.c
@@ -179,7 +179,7 @@ static void pdfapp_open_xps(pdfapp_t *app, char *filename, int fd)
void pdfapp_open(pdfapp_t *app, char *filename, int fd)
{
- if (strstr(filename, ".xps") || strstr(filename, ".XPS"))
+ if (strstr(filename, ".xps") || strstr(filename, ".XPS") || strstr(filename, ".rels"))
pdfapp_open_xps(app, filename, fd);
else
pdfapp_open_pdf(app, filename, fd);
diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c
index f93f7489..c5943d64 100644
--- a/pdf/pdf_xref.c
+++ b/pdf/pdf_xref.c
@@ -921,19 +921,16 @@ fz_error
pdf_open_xref(pdf_xref **xrefp, char *filename, char *password)
{
fz_error error;
- pdf_xref *xref;
fz_stream *file;
file = fz_open_file(filename);
if (!file)
return fz_throw("cannot open file '%s': %s", filename, strerror(errno));
- error = pdf_open_xref_with_stream(&xref, file, password);
+ error = pdf_open_xref_with_stream(xrefp, file, password);
if (error)
return fz_rethrow(error, "cannot load document '%s'", filename);
fz_close(file);
-
- *xrefp = xref;
return fz_okay;
}
diff --git a/xps/muxps.h b/xps/muxps.h
index a6faec67..bbdbfea5 100644
--- a/xps/muxps.h
+++ b/xps/muxps.h
@@ -202,7 +202,7 @@ struct xps_entry_s
struct xps_context_s
{
char *directory;
- FILE *file;
+ fz_stream *file;
int zip_count;
xps_entry *zip_table;
diff --git a/xps/xps_zip.c b/xps/xps_zip.c
index ccf82027..ab36954b 100644
--- a/xps/xps_zip.c
+++ b/xps/xps_zip.c
@@ -25,20 +25,20 @@ xps_free_part(xps_context *ctx, xps_part *part)
fz_free(part);
}
-static inline int getshort(FILE *file)
+static inline int getshort(fz_stream *file)
{
- int a = getc(file);
- int b = getc(file);
- return a | (b << 8);
+ int a = fz_read_byte(file);
+ int b = fz_read_byte(file);
+ return a | b << 8;
}
-static inline int getlong(FILE *file)
+static inline int getlong(fz_stream *file)
{
- int a = getc(file);
- int b = getc(file);
- int c = getc(file);
- int d = getc(file);
- return a | (b << 8) | (c << 16) | (d << 24);
+ int a = fz_read_byte(file);
+ int b = fz_read_byte(file);
+ int c = fz_read_byte(file);
+ int d = fz_read_byte(file);
+ return a | b << 8 | c << 16 | d << 24;
}
static void *
@@ -90,7 +90,7 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf)
int namelength, extralength;
int code;
- fseek(ctx->file, ent->offset, 0);
+ fz_seek(ctx->file, ent->offset, 0);
sig = getlong(ctx->file);
if (sig != ZIP_LOCAL_FILE_SIG)
@@ -107,17 +107,17 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf)
namelength = getshort(ctx->file);
extralength = getshort(ctx->file);
- fseek(ctx->file, namelength + extralength, 1);
+ fz_seek(ctx->file, namelength + extralength, 1);
if (method == 0)
{
- fread(outbuf, 1, ent->usize, ctx->file);
+ fz_read(ctx->file, outbuf, ent->usize);
}
else if (method == 8)
{
inbuf = fz_malloc(ent->csize);
- fread(inbuf, 1, ent->csize, ctx->file);
+ fz_read(ctx->file, inbuf, ent->csize);
memset(&stream, 0, sizeof(z_stream));
stream.zalloc = (alloc_func) xps_zip_alloc_items;
@@ -163,7 +163,7 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
int namesize, metasize, commentsize;
int i;
- fseek(ctx->file, start_offset, 0);
+ fz_seek(ctx->file, start_offset, 0);
sig = getlong(ctx->file);
if (sig != ZIP_END_OF_CENTRAL_DIRECTORY_SIG)
@@ -180,7 +180,7 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
ctx->zip_table = fz_calloc(count, sizeof(xps_entry));
memset(ctx->zip_table, 0, sizeof(xps_entry) * count);
- fseek(ctx->file, offset, 0);
+ fz_seek(ctx->file, offset, 0);
for (i = 0; i < count; i++)
{
@@ -206,11 +206,11 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
ctx->zip_table[i].offset = getlong(ctx->file);
ctx->zip_table[i].name = fz_malloc(namesize + 1);
- fread(ctx->zip_table[i].name, 1, namesize, ctx->file);
+ fz_read(ctx->file, (unsigned char*)ctx->zip_table[i].name, namesize);
ctx->zip_table[i].name[namesize] = 0;
- fseek(ctx->file, metasize, 1);
- fseek(ctx->file, commentsize, 1);
+ fz_seek(ctx->file, metasize, 1);
+ fz_seek(ctx->file, commentsize, 1);
}
qsort(ctx->zip_table, count, sizeof(xps_entry), xps_compare_entries);
@@ -221,21 +221,21 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
static int
xps_find_and_read_zip_dir(xps_context *ctx)
{
+ unsigned char buf[512];
int file_size, back, maxback;
int i, n;
- char buf[512];
- fseek(ctx->file, 0, SEEK_END);
- file_size = ftell(ctx->file);
+ fz_seek(ctx->file, 0, SEEK_END);
+ file_size = fz_tell(ctx->file);
maxback = MIN(file_size, 0xFFFF + sizeof buf);
back = MIN(maxback, sizeof buf);
while (back < maxback)
{
- fseek(ctx->file, file_size - back, 0);
+ fz_seek(ctx->file, file_size - back, 0);
- n = fread(buf, 1, sizeof buf, ctx->file);
+ n = fz_read(ctx->file, buf, sizeof buf);
if (n < 0)
return fz_throw("cannot read end of central directory");
@@ -391,12 +391,10 @@ xps_read_part(xps_context *ctx, char *partname)
}
int
-xps_open_file(xps_context **ctxp, char *filename)
+xps_open_directory(xps_context **ctxp, char *directory)
{
xps_context *ctx;
- char buf[2048];
int code;
- char *p;
ctx = fz_malloc(sizeof(xps_context));
memset(ctx, 0, sizeof(xps_context));
@@ -404,30 +402,38 @@ xps_open_file(xps_context **ctxp, char *filename)
ctx->colorspace_table = xps_hash_new();
ctx->start_part = NULL;
- ctx->file = fopen(filename, "rb");
- if (!ctx->file)
+ ctx->directory = fz_strdup(directory);
+
+ code = xps_read_page_list(ctx);
+ if (code)
{
xps_free_context(ctx);
- return fz_throw("cannot open file: '%s'", filename);
+ return fz_rethrow(code, "cannot read page list");
}
- if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels"))
- {
- fz_strlcpy(buf, filename, sizeof buf);
- p = strstr(buf, "/_rels/.rels");
- if (!p)
- p = strstr(buf, "\\_rels\\.rels");
- *p = 0;
- ctx->directory = fz_strdup(buf);
- }
- else
+ *ctxp = ctx;
+ return fz_okay;
+}
+
+int
+xps_open_stream(xps_context **ctxp, fz_stream *file)
+{
+ xps_context *ctx;
+ int code;
+
+ ctx = fz_malloc(sizeof(xps_context));
+ memset(ctx, 0, sizeof(xps_context));
+ ctx->font_table = xps_hash_new();
+ ctx->colorspace_table = xps_hash_new();
+ ctx->start_part = NULL;
+
+ ctx->file = fz_keep_stream(file);
+
+ code = xps_find_and_read_zip_dir(ctx);
+ if (code < 0)
{
- code = xps_find_and_read_zip_dir(ctx);
- if (code < 0)
- {
- xps_free_context(ctx);
- return fz_rethrow(code, "cannot read zip central directory");
- }
+ xps_free_context(ctx);
+ return fz_rethrow(code, "cannot read zip central directory");
}
code = xps_read_page_list(ctx);
@@ -441,6 +447,36 @@ xps_open_file(xps_context **ctxp, char *filename)
return fz_okay;
}
+int
+xps_open_file(xps_context **ctxp, char *filename)
+{
+ char buf[2048];
+ fz_stream *file;
+ char *p;
+ int code;
+
+ if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels"))
+ {
+ fz_strlcpy(buf, filename, sizeof buf);
+ p = strstr(buf, "/_rels/.rels");
+ if (!p)
+ p = strstr(buf, "\\_rels\\.rels");
+ *p = 0;
+ return xps_open_directory(ctxp, buf);
+ }
+
+ file = fz_open_file(filename);
+ if (!file)
+ return fz_throw("cannot open file '%s': %s", filename, strerror(errno));
+
+ code = xps_open_stream(ctxp, file);
+ if (code)
+ return fz_rethrow(code, "cannot load document '%s'", filename);
+
+ fz_close(file);
+ return fz_okay;
+}
+
static void xps_free_key_func(void *ptr)
{
fz_free(ptr);
@@ -462,7 +498,7 @@ xps_free_context(xps_context *ctx)
int i;
if (ctx->file)
- fclose(ctx->file);
+ fz_close(ctx->file);
for (i = 0; i < ctx->zip_count; i++)
fz_free(ctx->zip_table[i].name);