From 4ad40244021880e90d670720ac4c3beeac70aebc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 2 Jun 2005 11:34:46 +0200 Subject: heh. forgot a bunch of files for the new stream stuff. --- apps/pdfdebug.c | 4 +- apps/samshow.c | 2 +- include/fitz/stream.h | 8 +- include/fitz/tree.h | 1 - mupdf/pdf_image.c | 4 +- mupdf/pdf_open.c | 24 ++--- mupdf/pdf_page.c | 2 +- mupdf/pdf_save.c | 4 +- mupdf/pdf_stream.c | 8 +- mupdf/pdf_xref.c | 2 +- samus/sa_xml.c | 2 +- samus/sa_zip.c | 10 +- stream/stm_misc.c | 128 +++++++++++++++++++++++ stream/stm_open.c | 222 +++++++++++++++++++++++++++++++++++++++ stream/stm_read.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++ stream/stm_write.c | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++ 16 files changed, 926 insertions(+), 36 deletions(-) create mode 100644 stream/stm_misc.c create mode 100644 stream/stm_open.c create mode 100644 stream/stm_read.c create mode 100644 stream/stm_write.c diff --git a/apps/pdfdebug.c b/apps/pdfdebug.c index 1ae84d7c..3a3f8321 100644 --- a/apps/pdfdebug.c +++ b/apps/pdfdebug.c @@ -57,7 +57,7 @@ void decodestream(pdf_xref *xref, int oid, int gid) if (n == 0) break; if (n < 0) - fz_abort(fz_throw("ioerror: read failed")); + fz_abort(fz_ioerror(stm)); printsafe(buf, n); } @@ -81,7 +81,7 @@ void copystream(pdf_xref *xref, int oid, int gid) if (n == 0) break; if (n < 0) - fz_abort(fz_throw("ioerror: read failed")); + fz_abort(fz_ioerror(stm)); printsafe(buf, n); } diff --git a/apps/samshow.c b/apps/samshow.c index aa4030cc..d62e8651 100644 --- a/apps/samshow.c +++ b/apps/samshow.c @@ -23,7 +23,7 @@ int runzip(int argc, char **argv) fz_abort(error); n = fz_readall(&buf, stm); if (n < 0) - fz_abort(fz_throw("ioerror: readall failed")); + fz_abort(fz_ioerror(stm)); fz_dropstream(stm); fwrite(buf->rp, 1, buf->wp - buf->rp, stdout); diff --git a/include/fitz/stream.h b/include/fitz/stream.h index 5140fcca..b0778373 100644 --- a/include/fitz/stream.h +++ b/include/fitz/stream.h @@ -17,6 +17,7 @@ struct fz_stream_s fz_buffer *buffer; fz_filter *filter; fz_stream *chain; + fz_error *error; int file; }; @@ -42,10 +43,9 @@ fz_error *fz_openwfilter(fz_stream **stmp, fz_filter *flt, fz_stream *chain); * Functions that are common to both input and output streams. */ -/* behave like dup() */ -fz_stream *fz_keepstream(fz_stream *stm); +fz_error *fz_ioerror(fz_stream *stm); -/* essentially your close() */ +fz_stream *fz_keepstream(fz_stream *stm); void fz_dropstream(fz_stream *stm); int fz_tell(fz_stream *stm); @@ -68,7 +68,7 @@ int fz_readline(fz_stream *stm, char *buf, int max); int fz_readbytex(fz_stream *stm); int fz_peekbytex(fz_stream *stm); -#if 1 +#ifdef DEBUG #define fz_readbyte fz_readbytex #define fz_peekbyte fz_peekbytex #else diff --git a/include/fitz/tree.h b/include/fitz/tree.h index 9f76f167..22665736 100644 --- a/include/fitz/tree.h +++ b/include/fitz/tree.h @@ -2,7 +2,6 @@ * The display tree is at the center of attention in Fitz. * The tree and most of its minor nodes. * Paths and text nodes are found elsewhere. - * Resources used are also found elsewhere. */ typedef struct fz_tree_s fz_tree; diff --git a/mupdf/pdf_image.c b/mupdf/pdf_image.c index cd7457a0..c081d4f3 100644 --- a/mupdf/pdf_image.c +++ b/mupdf/pdf_image.c @@ -137,7 +137,7 @@ pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref, i = fz_readall(&img->samples, tempfile); if (i < 0) - return fz_throw("ioerror: readall failed"); + return fz_ioerror(tempfile); fz_dropfilter(filter); fz_dropstream(tempfile); @@ -150,7 +150,7 @@ pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref, i = fz_read(file, img->samples->bp, img->super.h * img->stride); if (i < 0) - return fz_throw("ioerror: read failed"); + return fz_ioerror(file); img->samples->wp += img->super.h * img->stride; } diff --git a/mupdf/pdf_open.c b/mupdf/pdf_open.c index 67d9b5d9..fa1b214a 100644 --- a/mupdf/pdf_open.c +++ b/mupdf/pdf_open.c @@ -19,7 +19,7 @@ loadversion(pdf_xref *xref) n = fz_seek(xref->file, 0, 0); if (n < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); fz_readline(xref->file, buf, sizeof buf); if (memcmp(buf, "%PDF-", 5) != 0) @@ -41,15 +41,15 @@ readstartxref(pdf_xref *xref) t = fz_seek(xref->file, 0, 2); if (t == -1) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); t = fz_seek(xref->file, MAX(0, t - ((int)sizeof buf)), 0); if (t == -1) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); n = fz_read(xref->file, buf, sizeof buf); if (n == -1) - return fz_throw("ioerror: read failed"); + return fz_ioerror(xref->file); for (i = n - 9; i >= 0; i--) { @@ -93,7 +93,7 @@ readoldtrailer(pdf_xref *xref, char *buf, int cap) n = fz_readline(xref->file, buf, cap); if (n < 0) - return fz_throw("ioerror: read failed"); + return fz_ioerror(xref->file); s = buf; ofs = atoi(strsep(&s, " ")); @@ -105,11 +105,11 @@ readoldtrailer(pdf_xref *xref, char *buf, int cap) t = fz_tell(xref->file); if (t < 0) - return fz_throw("ioerror: tell failed"); + return fz_ioerror(xref->file); n = fz_seek(xref->file, t + 20 * len, 0); if (n < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); } t = pdf_lex(xref->file, buf, cap, &n); @@ -138,7 +138,7 @@ readtrailer(pdf_xref *xref, char *buf, int cap) n = fz_seek(xref->file, xref->startxref, 0); if (n < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); c = fz_peekbyte(xref->file); if (c == 'x') @@ -177,7 +177,7 @@ readoldxref(fz_obj **trailerp, pdf_xref *xref, char *buf, int cap) n = fz_readline(xref->file, buf, cap); if (n < 0) - return fz_throw("ioerror: read failed"); + return fz_ioerror(xref->file); s = buf; ofs = atoi(strsep(&s, " ")); @@ -194,7 +194,7 @@ readoldxref(fz_obj **trailerp, pdf_xref *xref, char *buf, int cap) { n = fz_read(xref->file, buf, 20); if (n < 0) - return fz_throw("ioerror: read failed"); + return fz_ioerror(xref->file); if (n != 20) return fz_throw("syntaxerror: truncated xref table"); if (!xref->table[ofs + i].type) @@ -327,7 +327,7 @@ readxref(fz_obj **trailerp, pdf_xref *xref, int ofs, char *buf, int cap) n = fz_seek(xref->file, ofs, 0); if (n < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); c = fz_peekbyte(xref->file); if (c == 'x') @@ -438,7 +438,7 @@ pdf_loadobjstm(pdf_xref *xref, int oid, int gen, char *buf, int cap) n = fz_seek(stm, first, 0); if (n < 0) { - error = fz_throw("ioerror: seek failed"); + error = fz_ioerror(stm); goto cleanupstm; } diff --git a/mupdf/pdf_page.c b/mupdf/pdf_page.c index 44ac769c..34a60647 100644 --- a/mupdf/pdf_page.c +++ b/mupdf/pdf_page.c @@ -59,7 +59,7 @@ runmany(pdf_csi *csi, pdf_xref *xref, fz_obj *rdb, fz_obj *list) if (n == -1) { - error = fz_throw("ioerror: write failed"); + error = fz_ioerror(file); goto cleanup1; } diff --git a/mupdf/pdf_save.c b/mupdf/pdf_save.c index a23fa419..0bcb8ac1 100644 --- a/mupdf/pdf_save.c +++ b/mupdf/pdf_save.c @@ -42,14 +42,14 @@ writestream(fz_stream *out, pdf_xref *xref, pdf_crypt *encrypt, int oid, int gen break; if (n < 0) { - error = fz_throw("ioerror: read failed"); + error = fz_ioerror(srcstm); goto cleanupsrc; } n = fz_write(dststm, buf, n); if (n < 0) { - error = fz_throw("ioerror: write failed"); + error = fz_ioerror(dststm); goto cleanupsrc; } } diff --git a/mupdf/pdf_stream.c b/mupdf/pdf_stream.c index 0479dfd7..f7a18970 100644 --- a/mupdf/pdf_stream.c +++ b/mupdf/pdf_stream.c @@ -355,7 +355,7 @@ pdf_openrawstream(fz_stream **stmp, pdf_xref *xref, int oid, int gen) if (n == -1) { fz_dropfilter(filter); - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); } error = fz_openrfilter(stmp, filter, xref->file); @@ -423,7 +423,7 @@ pdf_openstream(fz_stream **stmp, pdf_xref *xref, int oid, int gen) if (n == -1) { fz_dropfilter(filter); - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); } error = fz_openrfilter(stmp, filter, xref->file); @@ -456,7 +456,7 @@ pdf_loadrawstream(fz_buffer **bufp, pdf_xref *xref, int oid, int gen) fz_dropstream(stm); if (n < 0) - return fz_throw("ioerror: readall failed"); + return fz_ioerror(stm); return nil; } @@ -479,7 +479,7 @@ pdf_loadstream(fz_buffer **bufp, pdf_xref *xref, int oid, int gen) fz_dropstream(stm); if (n < 0) - return fz_throw("ioerror: readall failed"); + return fz_ioerror(stm); return nil; } diff --git a/mupdf/pdf_xref.c b/mupdf/pdf_xref.c index e4d25832..589d7268 100644 --- a/mupdf/pdf_xref.c +++ b/mupdf/pdf_xref.c @@ -389,7 +389,7 @@ pdf_cacheobject(pdf_xref *xref, int oid, int gen) { n = fz_seek(xref->file, x->ofs, 0); if (n < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(xref->file); error = pdf_parseindobj(&x->obj, xref->file, buf, sizeof buf, &roid, &rgen, &x->stmofs); if (error) diff --git a/samus/sa_xml.c b/samus/sa_xml.c index a7b2c764..4b9ac07a 100644 --- a/samus/sa_xml.c +++ b/samus/sa_xml.c @@ -197,7 +197,7 @@ sa_openxml(sa_xmlparser **spp, fz_stream *file, int ns) len = fz_read(file, buf, XMLBUFLEN); if (len < 0) { - error = fz_throw("ioerror: read failed"); + error = fz_ioerror(file); goto cleanup; } diff --git a/samus/sa_zip.c b/samus/sa_zip.c index eb182c01..865d6129 100644 --- a/samus/sa_zip.c +++ b/samus/sa_zip.c @@ -131,8 +131,8 @@ static fz_error *findzipendofdir(sa_zip *zip) int n, i; filesize = fz_seek(zip->file, 0, 2); - if (filesize == -1) - return fz_throw("ioerror: seek failed"); + if (filesize < 0) + return fz_ioerror(zip->file); maxback = MIN(filesize, 0xFFFF + sizeof buf); back = MIN(maxback, sizeof buf); @@ -142,7 +142,7 @@ static fz_error *findzipendofdir(sa_zip *zip) fz_seek(zip->file, filesize - back, 0); n = fz_read(zip->file, buf, sizeof buf); if (n < 0) - return fz_throw("ioerror: read failed"); + return fz_ioerror(zip->file); for (i = n - 4; i > 0; i--) if (!memcmp(buf + i, "\120\113\5\6", 4)) @@ -241,7 +241,7 @@ static fz_error *reallyopenzipentry(fz_stream **stmp, sa_zip *zip, int idx) t = fz_seek(zip->file, zip->table[idx].offset, 0); if (t < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(zip->file); sign = read4(zip->file); if (sign != 0x04034b50) @@ -266,7 +266,7 @@ static fz_error *reallyopenzipentry(fz_stream **stmp, sa_zip *zip, int idx) t = fz_seek(zip->file, namesize + metasize, 1); if (t < 0) - return fz_throw("ioerror: seek failed"); + return fz_ioerror(zip->file); switch (method) { diff --git a/stream/stm_misc.c b/stream/stm_misc.c new file mode 100644 index 00000000..010676fd --- /dev/null +++ b/stream/stm_misc.c @@ -0,0 +1,128 @@ +/* + * Miscellaneous I/O functions + */ + +#include "fitz.h" + +int fz_tell(fz_stream *stm) +{ + if (stm->mode == FZ_SREAD) + return fz_rtell(stm); + return fz_wtell(stm); +} + +int fz_seek(fz_stream *stm, int offset, int whence) +{ + if (stm->mode == FZ_SREAD) + return fz_rseek(stm, offset, whence); + return fz_wseek(stm, offset, whence); +} + +/* + * Read a line terminated by LF or CR or CRLF. + */ + +int fz_readline(fz_stream *stm, char *mem, int n) +{ + char *s = mem; + int c = EOF; + while (n > 1) + { + c = fz_readbyte(stm); + if (c == EOF) + break; + if (c == '\r') { + c = fz_peekbyte(stm); + if (c == '\n') + c = fz_readbyte(stm); + break; + } + if (c == '\n') + break; + *s++ = c; + n--; + } + if (n) + *s = '\0'; + return s - mem; +} + +/* + * Utility function to consume all the contents of an input stream into + * a freshly allocated buffer; realloced and trimmed to size. + */ + +enum { CHUNKSIZE = 1024 * 4 }; + +int fz_readall(fz_buffer **bufp, fz_stream *stm) +{ + fz_buffer *real; + unsigned char *newbuf; + unsigned char *buf; + int len; + int pos; + int n; + + *bufp = nil; + + len = 0; + pos = 0; + buf = nil; + + while (1) + { + if (len - pos == 0) + { + len += CHUNKSIZE; + newbuf = fz_realloc(buf, len); + if (!newbuf) + { + fz_free(buf); + return -1; + } + buf = newbuf; + } + + n = fz_read(stm, buf + pos, len - pos); + + if (n < 0) + { + fz_free(buf); + return -1; + } + + pos += n; + + if (n < CHUNKSIZE) + { + if (pos > 0) + { + newbuf = fz_realloc(buf, pos); + if (!newbuf) + { + fz_free(buf); + return -1; + } + } + else newbuf = buf; + + real = *bufp = fz_malloc(sizeof(fz_buffer)); + if (!real) + { + fz_free(newbuf); + return -1; + } + + real->refs = 1; + real->ownsdata = 1; + real->bp = buf; + real->rp = buf; + real->wp = buf + pos; + real->ep = buf + pos; + real->eof = 1; + + return real->wp - real->rp; + } + } +} + diff --git a/stream/stm_open.c b/stream/stm_open.c new file mode 100644 index 00000000..d72d9837 --- /dev/null +++ b/stream/stm_open.c @@ -0,0 +1,222 @@ +/* + * Creation and destruction. + */ + +#include "fitz.h" + +static fz_stream * +newstm(int kind, int mode) +{ + fz_stream *stm; + + stm = fz_malloc(sizeof(fz_stream)); + if (!stm) + return nil; + + stm->refs = 1; + stm->kind = kind; + stm->mode = mode; + stm->dead = 0; + stm->error = nil; + stm->buffer = nil; + + stm->chain = nil; + stm->filter = nil; + stm->file = -1; + + return stm; +} + +fz_error * +fz_ioerror(fz_stream *stm) +{ + fz_error *error; + if (stm->error) + { + error = stm->error; + stm->error = nil; + return error; + } + return fz_throw("ioerror: no error"); +} + +fz_stream * +fz_keepstream(fz_stream *stm) +{ + stm->refs ++; + return stm; +} + +void +fz_dropstream(fz_stream *stm) +{ + stm->refs --; + if (stm->refs == 0) + { + if (stm->error) + { + fz_warn("unhandled %s", stm->error->msg); + fz_droperror(stm->error); + } + + if (stm->mode == FZ_SWRITE) + { + stm->buffer->eof = 1; + fz_flush(stm); + } + + switch (stm->kind) + { + case FZ_SFILE: + close(stm->file); + break; + case FZ_SFILTER: + fz_dropfilter(stm->filter); + fz_dropstream(stm->chain); + break; + case FZ_SBUFFER: + break; + } + + fz_dropbuffer(stm->buffer); + fz_free(stm); + } +} + +static fz_error * +openfile(fz_stream **stmp, char *path, int mode, int realmode) +{ + fz_error *error; + fz_stream *stm; + + stm = newstm(FZ_SFILE, mode); + if (!stm) + return fz_outofmem; + + error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE); + if (error) + { + fz_free(stm); + return error; + } + + stm->file = open(path, realmode, 0666); + if (stm->file < 0) + { + fz_dropbuffer(stm->buffer); + fz_free(stm); + return fz_throw("ioerror: open '%s' failed: %s", path, strerror(errno)); + } + + *stmp = stm; + return nil; +} + +static fz_error * +openfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src, int mode) +{ + fz_error *error; + fz_stream *stm; + + stm = newstm(FZ_SFILTER, mode); + if (!stm) + return fz_outofmem; + + error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE); + if (error) + { + fz_free(stm); + return error; + } + + stm->chain = fz_keepstream(src); + stm->filter = fz_keepfilter(flt); + + *stmp = stm; + return nil; +} + +static fz_error * +openbuffer(fz_stream **stmp, fz_buffer *buf, int mode) +{ + fz_stream *stm; + + stm = newstm(FZ_SBUFFER, mode); + if (!stm) + return fz_outofmem; + + stm->buffer = fz_keepbuffer(buf); + + if (mode == FZ_SREAD) + stm->buffer->eof = 1; + + *stmp = stm; + return nil; +} + +fz_error * fz_openrfile(fz_stream **stmp, char *path) +{ + return openfile(stmp, path, FZ_SREAD, O_BINARY | O_RDONLY); +} + +fz_error * fz_openwfile(fz_stream **stmp, char *path) +{ + return openfile(stmp, path, FZ_SWRITE, + O_BINARY | O_WRONLY | O_CREAT | O_TRUNC); +} + +fz_error * fz_openafile(fz_stream **stmp, char *path) +{ + fz_error *error; + int t; + + error = openfile(stmp, path, FZ_SWRITE, O_BINARY | O_WRONLY); + if (error) + return error; + + t = lseek((*stmp)->file, 0, 2); + if (t < 0) + { + (*stmp)->dead = 1; + return fz_throw("ioerror: lseek: %s", strerror(errno)); + } + + return nil; +} + +fz_error * fz_openrfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src) +{ + return openfilter(stmp, flt, src, FZ_SREAD); +} + +fz_error * fz_openwfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src) +{ + return openfilter(stmp, flt, src, FZ_SWRITE); +} + +fz_error * fz_openrbuffer(fz_stream **stmp, fz_buffer *buf) +{ + return openbuffer(stmp, buf, FZ_SREAD); +} + +fz_error * fz_openwbuffer(fz_stream **stmp, fz_buffer *buf) +{ + return openbuffer(stmp, buf, FZ_SWRITE); +} + +fz_error * fz_openrmemory(fz_stream **stmp, char *mem, int len) +{ + fz_error *error; + fz_buffer *buf; + + error = fz_newbufferwithmemory(&buf, mem, len); + if (error) + return error; + + error = fz_openrbuffer(stmp, buf); + + fz_dropbuffer(buf); + + return error; +} + diff --git a/stream/stm_read.c b/stream/stm_read.c new file mode 100644 index 00000000..cb7c5c02 --- /dev/null +++ b/stream/stm_read.c @@ -0,0 +1,262 @@ +/* + * Input streams. + */ + +#include "fitz.h" + +int +fz_makedata(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + fz_error *reason; + int produced; + int n; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + if (buf->eof) + return 0; + + error = fz_rewindbuffer(buf); + if (error) + goto cleanup; + + if (buf->ep - buf->wp == 0) + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + + switch (stm->kind) + { + + case FZ_SFILE: + n = read(stm->file, buf->wp, buf->ep - buf->wp); + if (n == -1) + { + stm->error = fz_throw("ioerror: read: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + if (n == 0) + buf->eof = 1; + buf->wp += n; + return n; + + case FZ_SFILTER: + produced = 0; + + while (1) + { + reason = fz_process(stm->filter, stm->chain->buffer, buf); + + if (stm->filter->produced) + produced = 1; + + if (reason == fz_ioneedin) + { + if (fz_makedata(stm->chain) < 0) + { + stm->dead = 1; + return -1; + } + } + + else if (reason == fz_ioneedout) + { + if (produced) + return 0; + + if (buf->rp > buf->bp) + { + error = fz_rewindbuffer(buf); + if (error) + goto cleanup; + } + else + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + } + + else if (reason == fz_iodone) + { + return 0; + } + + else + { + error = reason; + goto cleanup; + } + } + + case FZ_SBUFFER: + return 0; + } + + return -1; + +cleanup: + stm->error = error; + stm->dead = 1; + return -1; +} + +int fz_rtell(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, 0, 1); + if (t < 0) + { + stm->dead = 1; + return -1; + } + return t - (buf->wp - buf->rp); + + case FZ_SFILTER: + return stm->filter->count - (buf->wp - buf->rp); + + case FZ_SBUFFER: + return buf->rp - buf->bp; + } + + return -1; +} + +int fz_rseek(fz_stream *stm, int offset, int whence) +{ + fz_buffer *buf = stm->buffer; + int t, c; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SREAD) + return -1; + + if (whence == 1) + { + int cur = fz_rtell(stm); + if (cur < 0) + return -1; + offset = cur + offset; + whence = 0; + } + + buf->eof = 0; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, offset, whence); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp = buf->bp; + buf->wp = buf->bp; + + return t; + + case FZ_SFILTER: + if (whence == 0) + { + if (offset < fz_tell(stm)) + { + stm->error = fz_throw("ioerror: cannot seek back in filter"); + stm->dead = 1; + return -1; + } + while (fz_tell(stm) < offset) + { + c = fz_readbyte(stm); + if (c == EOF) + break; + } + return fz_tell(stm); + } + else + { + stm->dead = 1; + return -1; + } + + case FZ_SBUFFER: + if (whence == 0) + buf->rp = CLAMP(buf->bp + offset, buf->bp, buf->ep); + else + buf->rp = CLAMP(buf->ep + offset, buf->bp, buf->ep); + return buf->rp - buf->bp; + } + + return -1; +} + +int fz_readbytex(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + if (buf->rp == buf->wp) + { + if (buf->eof) return EOF; + if (fz_makedata(stm) < 0) return EOF; + } + if (buf->rp < buf->wp) + return *buf->rp++; + return EOF; +} + +int fz_peekbytex(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + if (buf->rp == buf->wp) + { + if (buf->eof) return EOF; + if (fz_makedata(stm) < 0) return EOF; + } + if (buf->rp < buf->wp) + return *buf->rp; + return EOF; +} + +int fz_read(fz_stream *stm, unsigned char *mem, int n) +{ + fz_buffer *buf = stm->buffer; + int i = 0; + + while (i < n) + { + while (buf->rp < buf->wp && i < n) + mem[i++] = *buf->rp++; + if (buf->rp == buf->wp) + { + if (buf->eof) return i; + if (fz_makedata(stm) < 0) return -1; + } + } + + return i; +} + diff --git a/stream/stm_write.c b/stream/stm_write.c new file mode 100644 index 00000000..3b4a8baf --- /dev/null +++ b/stream/stm_write.c @@ -0,0 +1,279 @@ +/* + * Output streams. + */ + +#include "fitz.h" + +int fz_wtell(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + t = lseek(stm->file, 0, 1); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + return t - (buf->wp - buf->rp); + + case FZ_SFILTER: + return stm->filter->count - (buf->wp - buf->rp); + + case FZ_SBUFFER: + return buf->wp - buf->bp; + } + + return -1; +} + +int fz_wseek(fz_stream *stm, int offset, int whence) +{ + fz_buffer *buf = stm->buffer; + int t; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + if (stm->kind != FZ_SFILE) + return -1; + + t = lseek(stm->file, offset, whence); + if (t < 0) + { + stm->error = fz_throw("ioerror: lseek: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp = buf->bp; + buf->wp = buf->bp; + buf->eof = 0; + + return t; +} + +static int flushfilter(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + fz_error *reason; + int t; + +loop: + + reason = fz_process(stm->filter, stm->buffer, stm->chain->buffer); + + if (reason == fz_ioneedin) + { + if (buf->rp > buf->ep) + fz_rewindbuffer(buf); + else + { + error = fz_growbuffer(buf); + if (error) + goto cleanup; + } + } + + else if (reason == fz_ioneedout) + { + t = fz_flush(stm->chain); + if (t < 0) + return -1; + } + + else if (reason == fz_iodone) + { + stm->dead = 2; /* special flag that we are dead because of eod */ + } + + else + { + error = reason; + goto cleanup; + } + + /* if we are at eof, repeat until other filter sets otherside to eof */ + if (buf->eof && !stm->chain->buffer->eof) + goto loop; + + return 0; + +cleanup: + stm->error = error; + stm->dead = 1; + return -1; +} + +/* + * Empty the buffer into the sink. + * Promise to make more space available. + * Called by fz_write and fz_dropstream. + * If buffer is eof, then all data must be flushed. + */ +int fz_flush(fz_stream *stm) +{ + fz_buffer *buf = stm->buffer; + fz_error *error; + int t; + + if (stm->dead == 2) + return 0; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + switch (stm->kind) + { + case FZ_SFILE: + while (buf->rp < buf->wp) + { + t = write(stm->file, buf->rp, buf->wp - buf->rp); + if (t < 0) + { + stm->error = fz_throw("ioerror: write: %s", strerror(errno)); + stm->dead = 1; + return -1; + } + + buf->rp += t; + } + + if (buf->rp > buf->bp) + fz_rewindbuffer(buf); + + return 0; + + case FZ_SFILTER: + return flushfilter(stm); + + case FZ_SBUFFER: + if (!buf->eof && buf->wp == buf->ep) + { + error = fz_growbuffer(buf); + if (error) + { + stm->error = error; + stm->dead = 1; + return -1; + } + } + return 0; + } + + return -1; +} + +/* + * Write data to stream. + * Buffer until internal buffer is full. + * When full, call fz_flush to make more space available. + */ +int fz_write(fz_stream *stm, unsigned char *mem, int n) +{ + fz_buffer *buf = stm->buffer; + int i = 0; + int t; + + if (stm->dead == 2) + return 0; + + if (stm->dead) + return -1; + + if (stm->mode != FZ_SWRITE) + return -1; + + while (i < n) + { + while (buf->wp < buf->ep && i < n) + *buf->wp++ = mem[i++]; + + if (buf->wp == buf->ep && i < n) + { + t = fz_flush(stm); + if (t < 0) + return -1; + if (stm->dead) + return i; + } + } + + return n; +} + +int fz_printstr(fz_stream *stm, char *s) +{ + return fz_write(stm, s, strlen(s)); +} + +int fz_printobj(fz_stream *file, fz_obj *obj, int tight) +{ + char buf[1024]; + char *ptr; + int n; + + n = fz_sprintobj(nil, 0, obj, tight); + if (n < sizeof buf) + { + fz_sprintobj(buf, sizeof buf, obj, tight); + return fz_write(file, buf, n); + } + else + { + ptr = fz_malloc(n); + if (!ptr) + return -1; + fz_sprintobj(ptr, n, obj, tight); + n = fz_write(file, ptr, n); + fz_free(ptr); + return n; + } +} + +int fz_print(fz_stream *stm, char *fmt, ...) +{ + va_list ap; + char buf[1024]; + char *p; + int n; + + va_start(ap, fmt); + n = vsnprintf(buf, sizeof buf, fmt, ap); + va_end(ap); + + if (n < sizeof buf) + return fz_write(stm, buf, n); + + p = fz_malloc(n); + if (!p) + return -1; + + va_start(ap, fmt); + vsnprintf(p, n, fmt, ap); + va_end(ap); + + n = fz_write(stm, p, n); + + fz_free(p); + + return n; +} + -- cgit v1.2.3