summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2008-08-15 16:57:09 +0200
committerTor Andersson <tor@ghostscript.com>2008-08-15 16:57:09 +0200
commita27aa33c254a0054af72147a5ce05d106df15251 (patch)
tree42b0a2756bd13b9fac97915346c122550ff74427
parentbdcfc98006f17dbe748665d8e98006ab79250d4f (diff)
downloadmupdf-a27aa33c254a0054af72147a5ce05d106df15251.tar.xz
Simplify (and possibly reduce performance) of fz_readall().
-rw-r--r--include/fitz/stm_stream.h2
-rw-r--r--mupdf/pdf_image.c2
-rw-r--r--mupdf/pdf_stream.c4
-rw-r--r--stream/stm_misc.c88
4 files changed, 26 insertions, 70 deletions
diff --git a/include/fitz/stm_stream.h b/include/fitz/stm_stream.h
index 62eb8515..4f7463bb 100644
--- a/include/fitz/stm_stream.h
+++ b/include/fitz/stm_stream.h
@@ -58,7 +58,7 @@ fz_error * fz_rseek(fz_stream *stm, int offset, int whence);
fz_error * fz_readimp(fz_stream *stm);
fz_error * fz_read(int *np, fz_stream *stm, unsigned char *buf, int len);
-fz_error * fz_readall(fz_buffer **bufp, fz_stream *stm);
+fz_error * fz_readall(fz_buffer **bufp, fz_stream *stm, int sizehint);
fz_error * fz_readline(fz_stream *stm, char *buf, int max);
/*
diff --git a/mupdf/pdf_image.c b/mupdf/pdf_image.c
index 7265f95c..1b1ab254 100644
--- a/mupdf/pdf_image.c
+++ b/mupdf/pdf_image.c
@@ -143,7 +143,7 @@ pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref,
if (error)
return error;
- error = fz_readall(&img->samples, tempfile);
+ error = fz_readall(&img->samples, tempfile, img->stride * img->super.h);
if (error)
return error;
diff --git a/mupdf/pdf_stream.c b/mupdf/pdf_stream.c
index dfa946aa..c024dd8b 100644
--- a/mupdf/pdf_stream.c
+++ b/mupdf/pdf_stream.c
@@ -484,7 +484,7 @@ pdf_loadrawstream(fz_buffer **bufp, pdf_xref *xref, int oid, int gen)
if (error)
return fz_rethrow(error, "cannot open raw stream (%d)", oid);
- error = fz_readall(bufp, stm);
+ error = fz_readall(bufp, stm, 0);
fz_dropstream(stm);
if (error)
return fz_rethrow(error, "cannot load stream into buffer (%d)", oid);
@@ -504,7 +504,7 @@ pdf_loadstream(fz_buffer **bufp, pdf_xref *xref, int oid, int gen)
if (error)
return fz_rethrow(error, "cannot open stream (%d)", oid);
- error = fz_readall(bufp, stm);
+ error = fz_readall(bufp, stm, 0);
fz_dropstream(stm);
if (error)
return fz_rethrow(error, "cannot load stream into buffer (%d)", oid);
diff --git a/stream/stm_misc.c b/stream/stm_misc.c
index b91b6c7e..70c1c315 100644
--- a/stream/stm_misc.c
+++ b/stream/stm_misc.c
@@ -59,83 +59,39 @@ fz_readline(fz_stream *stm, char *mem, int n)
/*
* Utility function to consume all the contents of an input stream into
- * a freshly allocated buffer; realloced and trimmed to size.
+ * a freshly allocated buffer.
*/
-enum { CHUNKSIZE = 1024 * 4 };
-
fz_error *
-fz_readall(fz_buffer **bufp, fz_stream *stm)
+fz_readall(fz_buffer **bufp, fz_stream *stm, int sizehint)
{
fz_error *error;
- fz_buffer *real;
- unsigned char *newbuf;
- unsigned char *buf;
- int len;
- int pos;
- int n;
+ fz_buffer *buf;
+ int c;
- len = 0;
- pos = 0;
- buf = nil;
+ if (sizehint == 0)
+ sizehint = 4 * 1024;
- while (1)
- {
- if (len - pos == 0)
- {
- if (len == 0)
- len = CHUNKSIZE;
- else
- len = len * 2;
- newbuf = fz_realloc(buf, len);
- if (!newbuf)
- {
- fz_free(buf);
- return fz_throw("outofmem: scratch buffer");
- }
- buf = newbuf;
- }
+ error = fz_newbuffer(&buf, sizehint);
+ if (error)
+ return fz_rethrow(error, "cannot create scratch buffer");
- error = fz_read(&n, stm, buf + pos, len - pos);
- if (error)
+ for (c = fz_readbyte(stm); c != EOF; c = fz_readbyte(stm))
+ {
+ if (buf->wp == buf->ep)
{
- fz_free(buf);
- return fz_rethrow(error, "cannot read data");
+ error = fz_growbuffer(buf);
+ if (error)
+ {
+ fz_dropbuffer(buf);
+ return fz_rethrow(error, "cannot resize scratch buffer");
+ }
}
- pos += n;
-
- if (n < CHUNKSIZE)
- {
- if (pos > 0)
- {
- newbuf = fz_realloc(buf, pos);
- if (!newbuf)
- {
- fz_free(buf);
- return fz_throw("outofmem: scratch buffer");
- }
- }
- else newbuf = buf;
-
- real = fz_malloc(sizeof(fz_buffer));
- if (!real)
- {
- fz_free(newbuf);
- return fz_throw("outofmem: buffer struct");
- }
-
- real->refs = 1;
- real->ownsdata = 1;
- real->bp = newbuf;
- real->rp = newbuf;
- real->wp = newbuf + pos;
- real->ep = newbuf + pos;
- real->eof = 1;
-
- *bufp = real;
- return fz_okay;
- }
+ *buf->wp++ = c;
}
+
+ *bufp = buf;
+ return fz_okay;
}