[gs-commits] mupdf/master - 0_6-241-g406ac81 - Make an educated guess at the stream length by looking at the filters used in pdf_loadstream.

Tor Andersson tor at ghostscript.com
Tue Aug 3 12:09:46 UTC 2010


commit 406ac81dc471ef30dbe5619d82369f2f43e99882
Author: Tor Andersson <tor at ghostscript.com>
Date:   Fri Jul 30 11:57:31 2010 +0000

    Make an educated guess at the stream length by looking at the filters used in pdf_loadstream.
    
    Ignore-this: 83e7777f18b70c0e693da63b9ece9582
    
    darcs-hash:20100730115731-f546f-5edbbd6b9cf2b47b242b5ffb7d6030bbd4e1b455.gz

 3 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/fitz/fitz.h b/fitz/fitz.h
index 1af70f6..44f1c07 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -501,7 +501,7 @@ void fz_seek(fz_stream *stm, int offset, int whence);
 
 int fz_read(fz_stream *stm, unsigned char *buf, int len);
 void fz_readline(fz_stream *stm, char *buf, int max);
-fz_error fz_readall(fz_buffer **bufp, fz_stream *stm);
+fz_error fz_readall(fz_buffer **bufp, fz_stream *stm, int initial);
 
 static inline int fz_readbyte(fz_stream *stm)
 {
diff --git a/fitz/stm_read.c b/fitz/stm_read.c
index 3bdddad..fb25121 100644
--- a/fitz/stm_read.c
+++ b/fitz/stm_read.c
@@ -55,12 +55,15 @@ fz_fillbuffer(fz_stream *stm)
 }
 
 fz_error
-fz_readall(fz_buffer **bufp, fz_stream *stm)
+fz_readall(fz_buffer **bufp, fz_stream *stm, int initial)
 {
 	fz_buffer *buf;
 	int n;
 
-	buf = fz_newbuffer(16 * 1024);
+	if (initial < 1024)
+		initial = 1024;
+
+	buf = fz_newbuffer(initial);
 
 	while (1)
 	{
diff --git a/mupdf/pdf_stream.c b/mupdf/pdf_stream.c
index c564529..db0ffaa 100644
--- a/mupdf/pdf_stream.c
+++ b/mupdf/pdf_stream.c
@@ -315,12 +315,22 @@ pdf_loadrawstream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
 {
 	fz_error error;
 	fz_stream *stm;
+	fz_obj *dict;
+	int len;
+
+	error = pdf_loadobject(&dict, xref, num, gen);
+	if (error)
+		return fz_rethrow(error, "cannot load stream dictionary (%d %d R)", num, gen);
+
+	len = fz_toint(fz_dictgets(dict, "Length"));
+
+	fz_dropobj(dict);
 
 	error = pdf_openrawstream(&stm, xref, num, gen);
 	if (error)
 		return fz_rethrow(error, "cannot open raw stream (%d %d R)", num, gen);
 
-	error = fz_readall(bufp, stm);
+	error = fz_readall(bufp, stm, len);
 	if (error)
 	{
 		fz_close(stm);
@@ -331,6 +341,18 @@ pdf_loadrawstream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
 	return fz_okay;
 }
 
+static int
+pdf_guessfilterlength(int len, char *filter)
+{
+	if (!strcmp(filter, "ASCIIHexDecode"))
+		return len / 2;
+	if (!strcmp(filter, "FlateDecode"))
+		return len * 3;
+	if (!strcmp(filter, "LZWDecode"))
+		return len * 2;
+	return len;
+}
+
 /*
  * Load uncompressed contents of a stream into buf.
  */
@@ -339,12 +361,26 @@ pdf_loadstream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
 {
 	fz_error error;
 	fz_stream *stm;
+	fz_obj *dict, *obj;
+	int i, len;
 
 	error = pdf_openstream(&stm, xref, num, gen);
 	if (error)
 		return fz_rethrow(error, "cannot open stream (%d %d R)", num, gen);
 
-	error = fz_readall(bufp, stm);
+	error = pdf_loadobject(&dict, xref, num, gen);
+	if (error)
+		return fz_rethrow(error, "cannot load stream dictionary (%d %d R)", num, gen);
+
+	len = fz_toint(fz_dictgets(dict, "Length"));
+	obj = fz_dictgets(dict, "Filter");
+	len = pdf_guessfilterlength(len, fz_toname(obj));
+	for (i = 0; i < fz_arraylen(obj); i++)
+		len = pdf_guessfilterlength(len, fz_toname(fz_arrayget(obj, i)));
+
+	fz_dropobj(dict);
+
+	error = fz_readall(bufp, stm, len);
 	if (error)
 	{
 		fz_close(stm);

--
git/hooks/post-receive


More information about the gs-commits mailing list