summaryrefslogtreecommitdiff
path: root/stream
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2008-03-21 13:28:39 +0100
committerTor Andersson <tor@ghostscript.com>2008-03-21 13:28:39 +0100
commit4b9181cdb56a9d2015f8d79e90015038f4792334 (patch)
tree507876fcec4f91aa591f3cbf2196f4dcc4deeaad /stream
parent65e3374a2415399ded4624326a01bb9dfa77fa9e (diff)
downloadmupdf-4b9181cdb56a9d2015f8d79e90015038f4792334.tar.xz
Backport of chained error messages from experimental branch.
Diffstat (limited to 'stream')
-rw-r--r--stream/crypt_arc4.c12
-rw-r--r--stream/crypt_md5.c12
-rw-r--r--stream/filt_a85d.c8
-rw-r--r--stream/filt_a85e.c2
-rw-r--r--stream/filt_ahxd.c4
-rw-r--r--stream/filt_ahxe.c2
-rw-r--r--stream/filt_arc4.c2
-rw-r--r--stream/filt_dctc.h2
-rw-r--r--stream/filt_dctd.c11
-rw-r--r--stream/filt_dcte.c24
-rw-r--r--stream/filt_faxd.c43
-rw-r--r--stream/filt_faxe.c21
-rw-r--r--stream/filt_flate.c12
-rw-r--r--stream/filt_jbig2d.c4
-rw-r--r--stream/filt_jpxd.c17
-rw-r--r--stream/filt_lzwd.c4
-rw-r--r--stream/filt_lzwe.c2
-rw-r--r--stream/filt_null.c7
-rw-r--r--stream/filt_pipeline.c20
-rw-r--r--stream/filt_predict.c41
-rw-r--r--stream/filt_rld.c2
-rw-r--r--stream/filt_rle.c2
-rw-r--r--stream/obj_array.c107
-rw-r--r--stream/obj_dict.c104
-rw-r--r--stream/obj_parse.c180
-rw-r--r--stream/obj_simple.c54
-rw-r--r--stream/stm_buffer.c31
-rw-r--r--stream/stm_filter.c10
-rw-r--r--stream/stm_misc.c41
-rw-r--r--stream/stm_open.c86
-rw-r--r--stream/stm_read.c209
-rw-r--r--stream/stm_write.c169
32 files changed, 806 insertions, 439 deletions
diff --git a/stream/crypt_arc4.c b/stream/crypt_arc4.c
index 0a5c942f..a1eef199 100644
--- a/stream/crypt_arc4.c
+++ b/stream/crypt_arc4.c
@@ -38,14 +38,16 @@ fz_arc4init(fz_arc4 *arc4, unsigned char *key, unsigned keylen)
arc4->x = 0;
arc4->y = 0;
- for (counter = 0; counter < 256; counter++) {
+ for (counter = 0; counter < 256; counter++)
+ {
state[counter] = counter;
}
keyindex = 0;
stateindex = 0;
- for (counter = 0; counter < 256; counter++) {
+ for (counter = 0; counter < 256; counter++)
+ {
t = state[counter];
stateindex = (stateindex + key[keyindex] + t) & 0xff;
u = state[stateindex];
@@ -53,7 +55,8 @@ fz_arc4init(fz_arc4 *arc4, unsigned char *key, unsigned keylen)
state[stateindex] = t;
state[counter] = u;
- if (++keyindex >= keylen) {
+ if (++keyindex >= keylen)
+ {
keyindex = 0;
}
}
@@ -87,7 +90,8 @@ void
fz_arc4encrypt(fz_arc4 *arc4, unsigned char *dest, unsigned char *src, unsigned len)
{
unsigned int i;
- for (i = 0; i < len; i++) {
+ for (i = 0; i < len; i++)
+ {
unsigned char x;
x = fz_arc4next(arc4);
dest[i] = src[i] ^ x;
diff --git a/stream/crypt_md5.c b/stream/crypt_md5.c
index cf20024b..7a600612 100644
--- a/stream/crypt_md5.c
+++ b/stream/crypt_md5.c
@@ -83,7 +83,8 @@ static void encode(unsigned char *output, unsigned long *input, unsigned len)
{
unsigned i, j;
- for (i = 0, j = 0; j < len; i++, j += 4) {
+ for (i = 0, j = 0; j < len; i++, j += 4)
+ {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
@@ -95,7 +96,8 @@ static void decode(unsigned long *output, unsigned char *input, unsigned len)
{
unsigned i, j;
- for (i = 0, j = 0; j < len; i++, j += 4) {
+ for (i = 0, j = 0; j < len; i++, j += 4)
+ {
output[i] = ((unsigned long)input[j]) |
(((unsigned long)input[j+1]) << 8) |
(((unsigned long)input[j+2]) << 16) |
@@ -225,7 +227,8 @@ void fz_md5update(fz_md5 *context, unsigned char *input, unsigned inlen)
partlen = 64 - index;
/* Transform as many times as possible. */
- if (inlen >= partlen) {
+ if (inlen >= partlen)
+ {
memcpy(context->buffer + index, input, partlen);
transform(context->state, context->buffer);
@@ -234,7 +237,8 @@ void fz_md5update(fz_md5 *context, unsigned char *input, unsigned inlen)
index = 0;
}
- else {
+ else
+ {
i = 0;
}
diff --git a/stream/filt_a85d.c b/stream/filt_a85d.c
index 3db4951a..5bbe68cb 100644
--- a/stream/filt_a85d.c
+++ b/stream/filt_a85d.c
@@ -26,7 +26,7 @@ fz_newa85d(fz_filter **fp, fz_obj *params)
FZ_NEWFILTER(fz_a85d, f, a85d);
f->word = 0;
f->count = 0;
- return nil;
+ return fz_okay;
}
void
@@ -90,7 +90,7 @@ fz_processa85d(fz_filter *filter, fz_buffer *in, fz_buffer *out)
c = *in->rp++;
if (c != '>') {
- return fz_throw("ioerror: bad eod marker in a85d");
+ return fz_throw("bad eod marker in a85d");
}
if (out->wp + f->count - 1 > out->ep) {
@@ -102,7 +102,7 @@ fz_processa85d(fz_filter *filter, fz_buffer *in, fz_buffer *out)
case 0:
break;
case 1:
- return fz_throw("ioerror: partial final byte in a85d");
+ return fz_throw("partial final byte in a85d");
case 2:
f->word = f->word * (85L * 85 * 85) + 0xffffffL;
goto o1;
@@ -122,7 +122,7 @@ o1: *(out->wp+0) = f->word >> 24;
}
else if (!iswhite(c)) {
- return fz_throw("ioerror: bad data in a85d: '%c'", c);
+ return fz_throw("bad data in a85d: '%c'", c);
}
}
}
diff --git a/stream/filt_a85e.c b/stream/filt_a85e.c
index f7b5912d..a33ee7c5 100644
--- a/stream/filt_a85e.c
+++ b/stream/filt_a85e.c
@@ -14,7 +14,7 @@ fz_newa85e(fz_filter **fp, fz_obj *params)
{
FZ_NEWFILTER(fz_a85e, f, a85e);
f->c = 0;
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/filt_ahxd.c b/stream/filt_ahxd.c
index f54670b2..d670a8d9 100644
--- a/stream/filt_ahxd.c
+++ b/stream/filt_ahxd.c
@@ -44,7 +44,7 @@ fz_newahxd(fz_filter **fp, fz_obj *params)
FZ_NEWFILTER(fz_ahxd, f, ahxd);
f->odd = 0;
f->a = 0;
- return nil;
+ return fz_okay;
}
void
@@ -88,7 +88,7 @@ fz_processahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out)
}
else if (!iswhite(c)) {
- return fz_throw("ioerror: bad data in ahxd: '%c'", c);
+ return fz_throw("bad data in ahxd: '%c'", c);
}
}
}
diff --git a/stream/filt_ahxe.c b/stream/filt_ahxe.c
index 12c8aad0..8a2e271b 100644
--- a/stream/filt_ahxe.c
+++ b/stream/filt_ahxe.c
@@ -16,7 +16,7 @@ fz_newahxe(fz_filter **fp, fz_obj *params)
{
FZ_NEWFILTER(fz_ahxe, f, ahxe);
f->c = 0;
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/filt_arc4.c b/stream/filt_arc4.c
index 3b3c5957..af038599 100644
--- a/stream/filt_arc4.c
+++ b/stream/filt_arc4.c
@@ -14,7 +14,7 @@ fz_newarc4filter(fz_filter **fp, unsigned char *key, unsigned keylen)
{
FZ_NEWFILTER(fz_arc4c, f, arc4filter);
fz_arc4init(&f->arc4, key, keylen);
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/filt_dctc.h b/stream/filt_dctc.h
index 8aa6aeb7..72f61ef2 100644
--- a/stream/filt_dctc.h
+++ b/stream/filt_dctc.h
@@ -27,7 +27,7 @@ static void myoutmess(j_common_ptr cinfo)
struct myerrmgr *err = (struct myerrmgr *)cinfo->err;
char msgbuf[JMSG_LENGTH_MAX];
err->super.format_message(cinfo, msgbuf);
- fprintf(stderr, "ioerror: dct: %s", msgbuf);
+ fz_warn("jpeg error: %s", msgbuf);
}
static void myiniterr(struct myerrmgr *err)
diff --git a/stream/filt_dctd.c b/stream/filt_dctd.c
index 38ab3fbf..d8fd552f 100644
--- a/stream/filt_dctd.c
+++ b/stream/filt_dctd.c
@@ -72,7 +72,7 @@ fz_newdctd(fz_filter **fp, fz_obj *params)
d->cinfo.err = (struct jpeg_error_mgr*) &d->err;
if (setjmp(d->err.jb)) {
- err = fz_throw("ioerror in dctd: %s", d->err.msg);
+ err = fz_throw("cannot decode jpeg: %s", d->err.msg);
fz_free(d);
return err;
}
@@ -96,7 +96,7 @@ fz_newdctd(fz_filter **fp, fz_obj *params)
d->cinfo.dct_method = JDCT_FASTEST;
d->cinfo.do_fancy_upsampling = FALSE;
- return nil;
+ return fz_okay;
}
void
@@ -104,7 +104,7 @@ fz_dropdctd(fz_filter *filter)
{
fz_dctd *d = (fz_dctd*)filter;
if (setjmp(d->err.jb)) {
- fprintf(stderr, "ioerror in dct: jpeg_destroy_decompress: %s", d->err.msg);
+ fz_warn("jpeg error: jpeg_destroy_decompress: %s", d->err.msg);
return;
}
jpeg_destroy_decompress(&d->cinfo);
@@ -137,8 +137,9 @@ fz_processdctd(fz_filter *filter, fz_buffer *in, fz_buffer *out)
d->src.super.bytes_in_buffer = in->wp - in->rp;
d->src.super.next_input_byte = in->rp;
- if (setjmp(d->err.jb)) {
- return fz_throw("ioerror in dctd: %s", d->err.msg);
+ if (setjmp(d->err.jb))
+ {
+ return fz_throw("cannot decode jpeg: %s", d->err.msg);
}
switch (d->stage)
diff --git a/stream/filt_dcte.c b/stream/filt_dcte.c
index 06f3fcbe..3ce0f21d 100644
--- a/stream/filt_dcte.c
+++ b/stream/filt_dcte.c
@@ -41,23 +41,24 @@ fz_newdcte(fz_filter **fp, fz_obj *params)
e->stage = 0;
obj = fz_dictgets(params, "Columns");
- if (!obj) { fz_free(e); return fz_throw("ioerror in dcte: missing Columns parameter"); }
+ if (!obj) { fz_free(e); return fz_throw("missing Columns parameter"); }
e->columns = fz_toint(obj);
obj = fz_dictgets(params, "Rows");
- if (!obj) { fz_free(e); return fz_throw("ioerror in dcte: missing Rows parameter"); }
+ if (!obj) { fz_free(e); return fz_throw("missing Rows parameter"); }
e->rows = fz_toint(obj);
obj = fz_dictgets(params, "Colors");
- if (!obj) { fz_free(e); return fz_throw("ioerror in dcte: missing Colors parameter"); }
+ if (!obj) { fz_free(e); return fz_throw("missing Colors parameter"); }
e->colors = fz_toint(obj);
/* setup error callback first thing */
myiniterr(&e->err);
e->cinfo.err = (struct jpeg_error_mgr*) &e->err;
- if (setjmp(e->err.jb)) {
- err = fz_throw("ioerror in dcte: %s", e->err.msg);
+ if (setjmp(e->err.jb))
+ {
+ err = fz_throw("cannot encode jpeg: %s", e->err.msg);
fz_free(e);
return err;
}
@@ -119,7 +120,7 @@ fz_newdcte(fz_filter **fp, fz_obj *params)
/* TODO: quant-tables and huffman-tables */
- return nil;
+ return fz_okay;
}
void
@@ -128,7 +129,7 @@ fz_dropdcte(fz_filter *filter)
fz_dcte *e = (fz_dcte*)filter;
if (setjmp(e->err.jb)) {
- fprintf(stderr, "ioerror in dcte: jpeg_destroy_compress: %s", e->err.msg);
+ fz_warn("jpeg error: jpeg_destroy_compress: %s", e->err.msg);
return;
}
@@ -161,7 +162,7 @@ fz_setquanttables(fz_dcte *e, unsigned int **qtables, int qfactor)
unsigned int table[64];
if (setjmp(e->err.jb)) {
- return fz_throw("ioerror in dcte: %s", e->err.msg);
+ return fz_throw("jpeg error: jpeg_add_quant_table: %s", e->err.msg);
}
/* TODO: check for duplicate tables */
@@ -174,7 +175,7 @@ fz_setquanttables(fz_dcte *e, unsigned int **qtables, int qfactor)
e->cinfo.comp_info[i].quant_tbl_no = i;
}
- return nil;
+ return fz_okay;
}
fz_error *
@@ -189,8 +190,9 @@ fz_processdcte(fz_filter *filter, fz_buffer *in, fz_buffer *out)
e->dst.super.free_in_buffer = out->ep - out->wp;
e->dst.super.next_output_byte = out->wp;
- if (setjmp(e->err.jb)) {
- return fz_throw("ioerror in dcte: %s", e->err.msg);
+ if (setjmp(e->err.jb))
+ {
+ return fz_throw("cannot encode jpeg: %s", e->err.msg);
}
switch (e->stage)
diff --git a/stream/filt_faxd.c b/stream/filt_faxd.c
index a0449078..4de4d226 100644
--- a/stream/filt_faxd.c
+++ b/stream/filt_faxd.c
@@ -90,15 +90,24 @@ fz_newfaxd(fz_filter **fp, fz_obj *params)
fax->eolc = 0;
fax->ref = fz_malloc(fax->stride);
- if (!fax->ref) { fz_free(fax); return fz_outofmem; }
+ if (!fax->ref)
+ {
+ fz_free(fax);
+ return fz_throw("outofmem: scanline buffer one");
+ }
fax->dst = fz_malloc(fax->stride);
- if (!fax->dst) { fz_free(fax); fz_free(fax->ref); return fz_outofmem; }
+ if (!fax->dst)
+ {
+ fz_free(fax);
+ fz_free(fax->ref);
+ return fz_throw("outofmem: scanline buffer two");
+ }
memset(fax->ref, 0, fax->stride);
memset(fax->dst, 0, fax->stride);
- return nil;
+ return fz_okay;
}
void
@@ -125,7 +134,7 @@ static inline fz_error * fillbits(fz_faxd *fax, fz_buffer *in)
fax->word |= *in->rp << fax->bidx;
in->rp ++;
}
- return nil;
+ return fz_okay;
}
static int
@@ -164,13 +173,13 @@ dec1d(fz_faxd *fax)
code = getcode(fax, cf_white_decode, cfd_white_initial_bits);
if (code == UNCOMPRESSED)
- return fz_throw("ioerror: uncompressed data in faxd");
+ return fz_throw("uncompressed data in faxd");
if (code < 0)
- return fz_throw("ioerror: negative code in 1d faxd");
+ return fz_throw("negative code in 1d faxd");
if (fax->a + code > fax->columns)
- return fz_throw("ioerror: overflow in 1d faxd");
+ return fz_throw("overflow in 1d faxd");
if (fax->c)
setbits(fax->dst, fax->a, fax->a + code);
@@ -185,7 +194,7 @@ dec1d(fz_faxd *fax)
else
fax->stage = SMAKEUP;
- return nil;
+ return fz_okay;
}
/* decode one 2d code */
@@ -205,13 +214,13 @@ dec2d(fz_faxd *fax)
code = getcode(fax, cf_white_decode, cfd_white_initial_bits);
if (code == UNCOMPRESSED)
- return fz_throw("ioerror: uncompressed data in faxd");
+ return fz_throw("uncompressed data in faxd");
if (code < 0)
- return fz_throw("ioerror: negative code in 2d faxd");
+ return fz_throw("negative code in 2d faxd");
if (fax->a + code > fax->columns)
- return fz_throw("ioerror: overflow in 2d faxd");
+ return fz_throw("overflow in 2d faxd");
if (fax->c)
setbits(fax->dst, fax->a, fax->a + code);
@@ -227,7 +236,7 @@ dec2d(fz_faxd *fax)
fax->stage = SNORMAL;
}
- return nil;
+ return fz_okay;
}
code = getcode(fax, cf_2d_decode, cfd_2d_initial_bits);
@@ -295,13 +304,13 @@ dec2d(fz_faxd *fax)
break;
case UNCOMPRESSED:
- return fz_throw("ioerror: uncompressed data in faxd");
+ return fz_throw("uncompressed data in faxd");
case ERROR:
- return fz_throw("ioerror: invalid code in 2d faxd");
+ return fz_throw("invalid code in 2d faxd");
default:
- return fz_throw("ioerror: invalid code in 2d faxd (%d)", code);
+ return fz_throw("invalid code in 2d faxd (%d)", code);
}
return 0;
@@ -361,14 +370,14 @@ loop:
{
fax->eolc = 0;
error = dec1d(fax);
- if (error) return error;
+ if (error) return error; /* can be fz_io* or real error */
}
else if (fax->dim == 2)
{
fax->eolc = 0;
error = dec2d(fax);
- if (error) return error;
+ if (error) return error; /* can be fz_io* or real error */
}
/* no eol check after makeup codes nor in the middle of an H code */
diff --git a/stream/filt_faxe.c b/stream/filt_faxe.c
index 811b5a29..39d46a8c 100644
--- a/stream/filt_faxe.c
+++ b/stream/filt_faxe.c
@@ -75,15 +75,24 @@ fz_newfaxe(fz_filter **fp, fz_obj *params)
fax->c = 0;
fax->ref = fz_malloc(fax->stride);
- if (!fax->ref) { fz_free(fax); return fz_outofmem; }
+ if (!fax->ref)
+ {
+ fz_free(fax);
+ return fz_throw("outofmemory: scanline buffer one");
+ }
fax->src = fz_malloc(fax->stride);
- if (!fax->src) { fz_free(fax); fz_free(fax->ref); return fz_outofmem; }
+ if (!fax->src)
+ {
+ fz_free(fax);
+ fz_free(fax->ref);
+ return fz_throw("outofmemory: scanline buffer two");
+ }
memset(fax->ref, 0, fax->stride);
memset(fax->src, 0, fax->stride);
- return nil;
+ return fz_okay;
}
void
@@ -188,7 +197,7 @@ enc1d(fz_faxe *fax, unsigned char *line, fz_buffer *out)
fax->c = !fax->c;
}
- return 0;
+ return fz_okay;
}
static fz_error *
@@ -245,7 +254,7 @@ enc2d(fz_faxe *fax, unsigned char *ref, unsigned char *src, fz_buffer *out)
}
}
- return 0;
+ return fz_okay;
}
static fz_error *
@@ -349,7 +358,7 @@ process(fz_faxe *fax, fz_buffer *in, fz_buffer *out)
}
if (error)
- return error;
+ return error; /* one of fz_io* */
fax->ridx ++;
diff --git a/stream/filt_flate.c b/stream/filt_flate.c
index 1e048c17..cd079cd2 100644
--- a/stream/filt_flate.c
+++ b/stream/filt_flate.c
@@ -52,12 +52,12 @@ fz_newflated(fz_filter **fp, fz_obj *params)
if (ei != Z_OK)
{
- eo = fz_throw("ioerror: inflateInit: %s", f->z.msg);
+ eo = fz_throw("zlib error: inflateInit: %s", f->z.msg);
fz_free(f);
return eo;
}
- return nil;
+ return fz_okay;
}
void
@@ -108,7 +108,7 @@ fz_processflated(fz_filter *f, fz_buffer *in, fz_buffer *out)
}
else
{
- return fz_throw("ioerror: inflate: %s", zp->msg);
+ return fz_throw("zlib error: inflate: %s", zp->msg);
}
}
@@ -147,12 +147,12 @@ fz_newflatee(fz_filter **fp, fz_obj *params)
if (ei != Z_OK)
{
- eo = fz_throw("ioerror: deflateInit: %s", f->z.msg);
+ eo = fz_throw("zlib error: deflateInit: %s", f->z.msg);
fz_free(f);
return eo;
}
- return nil;
+ return fz_okay;
}
void
@@ -205,7 +205,7 @@ fz_processflatee(fz_filter *f, fz_buffer *in, fz_buffer *out)
}
else
{
- return fz_throw("ioerror: deflate: %s", zp->msg);
+ return fz_throw("zlib error: deflate: %s", zp->msg);
}
}
diff --git a/stream/filt_jbig2d.c b/stream/filt_jbig2d.c
index ce73ee53..17abecd4 100644
--- a/stream/filt_jbig2d.c
+++ b/stream/filt_jbig2d.c
@@ -48,7 +48,7 @@ fz_newjbig2d(fz_filter **fp, fz_obj *params)
d->ctx = jbig2_ctx_new(nil, JBIG2_OPTIONS_EMBEDDED, nil, nil, nil);
d->page = nil;
d->idx = 0;
- return nil;
+ return fz_okay;
}
void
@@ -65,7 +65,7 @@ fz_setjbig2dglobalstream(fz_filter *filter, unsigned char *buf, int len)
jbig2_data_in(d->ctx, buf, len);
d->gctx = jbig2_make_global_ctx(d->ctx);
d->ctx = jbig2_ctx_new(nil, JBIG2_OPTIONS_EMBEDDED, d->gctx, nil, nil);
- return nil;
+ return fz_okay;
}
fz_error *
diff --git a/stream/filt_jpxd.c b/stream/filt_jpxd.c
index 1b93f477..a8e14d0d 100644
--- a/stream/filt_jpxd.c
+++ b/stream/filt_jpxd.c
@@ -42,22 +42,24 @@ fz_newjpxd(fz_filter **fp, fz_obj *params)
FZ_NEWFILTER(fz_jpxd, d, jpxd);
err = jas_init();
- if (err) {
+ if (err)
+ {
fz_free(d);
- return fz_throw("ioerror in jpxd: jas_init()");
+ return fz_throw("jasper error: jas_init()");
}
d->stream = jas_stream_memopen(nil, 0);
- if (!d->stream) {
+ if (!d->stream)
+ {
fz_free(d);
- return fz_throw("ioerror in jpxd: jas_stream_memopen()");
+ return fz_throw("jasper error: jas_stream_memopen()");
}
d->image = nil;
d->offset = 0;
d->stage = 0;
- return nil;
+ return fz_okay;
}
void
@@ -83,7 +85,8 @@ fz_processjpxd(fz_filter *filter, fz_buffer *in, fz_buffer *out)
}
input:
- while (in->rp < in->wp) {
+ while (in->rp < in->wp)
+ {
n = jas_stream_write(d->stream, in->rp, in->wp - in->rp);
in->rp += n;
}
@@ -98,7 +101,7 @@ decode:
d->image = jas_image_decode(d->stream, -1, 0);
if (!d->image)
- return fz_throw("ioerror in jpxd: unable to decode image data");
+ return fz_throw("jasper error: jas_image_decode()");
fprintf(stderr, "P%c\n# JPX %d x %d n=%d bpc=%d colorspace=%04x %s\n%d %d\n%d\n",
jas_image_numcmpts(d->image) == 1 ? '5' : '6',
diff --git a/stream/filt_lzwd.c b/stream/filt_lzwd.c
index bbdb4984..127ebde8 100644
--- a/stream/filt_lzwd.c
+++ b/stream/filt_lzwd.c
@@ -83,7 +83,7 @@ fz_newlzwd(fz_filter **fp, fz_obj *params)
lzw->oldcode = -1;
lzw->resume = 0;
- return nil;
+ return fz_okay;
}
void
@@ -107,7 +107,7 @@ static inline fz_error * fillbits(fz_lzwd *lzw, fz_buffer *in)
lzw->word |= *in->rp << lzw->bidx;
in->rp ++;
}
- return nil;
+ return fz_okay;
}
static inline void unstuff(fz_lzwd *lzw, fz_buffer *in)
diff --git a/stream/filt_lzwe.c b/stream/filt_lzwe.c
index 978b6ef2..ec6f5f7a 100644
--- a/stream/filt_lzwe.c
+++ b/stream/filt_lzwe.c
@@ -83,7 +83,7 @@ fz_newlzwe(fz_filter **fp, fz_obj *params)
clearhash(lzw);
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/filt_null.c b/stream/filt_null.c
index f51039b4..601ed815 100644
--- a/stream/filt_null.c
+++ b/stream/filt_null.c
@@ -16,7 +16,7 @@ fz_newnullfilter(fz_filter **fp, int len)
FZ_NEWFILTER(fz_nullfilter, f, nullfilter);
f->len = len;
f->cur = 0;
- return nil;
+ return fz_okay;
}
void
@@ -34,7 +34,8 @@ fz_processnullfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out)
if (f->len >= 0)
n = MIN(n, f->len - f->cur);
- if (n) {
+ if (n)
+ {
memcpy(out->wp, in->rp, n);
in->rp += n;
out->wp += n;
@@ -48,6 +49,6 @@ fz_processnullfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out)
if (out->wp == out->ep)
return fz_ioneedout;
- return fz_throw("braindead programmer in nullfilter");
+ return fz_throw("braindead programmer trapped in nullfilter");
}
diff --git a/stream/filt_pipeline.c b/stream/filt_pipeline.c
index 65e1c38d..ea0b9885 100644
--- a/stream/filt_pipeline.c
+++ b/stream/filt_pipeline.c
@@ -24,7 +24,7 @@ fz_chainpipeline(fz_filter **fp, fz_filter *head, fz_filter *tail, fz_buffer *bu
p->tail = fz_keepfilter(tail);
p->tailneedsin = 1;
p->buffer = fz_keepbuffer(buf);
- return nil;
+ return fz_okay;
}
void
@@ -49,9 +49,13 @@ fz_newpipeline(fz_filter **fp, fz_filter *head, fz_filter *tail)
p->tailneedsin = 1;
error = fz_newbuffer(&p->buffer, FZ_BUFSIZE);
- if (error) { fz_free(p); return error; }
+ if (error)
+ {
+ fz_free(p);
+ return fz_rethrow(error, "cannot create buffer");
+ }
- return nil;
+ return fz_okay;
}
void
@@ -100,8 +104,11 @@ head:
else if (e == fz_iodone)
goto tail;
+ else if (e)
+ return fz_rethrow(e, "cannot process head filter");
+
else
- return e;
+ return fz_okay;
tail:
e = fz_process(p->tail, p->buffer, out);
@@ -123,7 +130,10 @@ tail:
else if (e == fz_iodone)
return fz_iodone;
+ else if (e)
+ return fz_rethrow(e, "cannot process tail filter");
+
else
- return e;
+ return fz_okay;
}
diff --git a/stream/filt_predict.c b/stream/filt_predict.c
index bf4680e7..4654e792 100644
--- a/stream/filt_predict.c
+++ b/stream/filt_predict.c
@@ -52,16 +52,22 @@ fz_newpredict(fz_filter **fp, fz_obj *params, int encode)
p->stride = (p->bpc * p->colors * p->columns + 7) / 8;
p->bpp = (p->bpc * p->colors + 7) / 8;
- if (p->predictor >= 10) {
+ if (p->predictor >= 10)
+ {
p->ref = fz_malloc(p->stride);
- if (!p->ref) { fz_free(p); return fz_outofmem; }
+ if (!p->ref)
+ {
+ fz_free(p);
+ return fz_throw("outofmem: scanline buffer");
+ }
memset(p->ref, 0, p->stride);
}
- else {
+ else
+ {
p->ref = nil;
}
- return nil;
+ return fz_okay;
}
void
@@ -122,8 +128,10 @@ tiff(fz_predict *p, unsigned char *in, unsigned char *out)
for (k = 0; k < p->colors; k++)
left[k] = 0;
- for (i = 0; i < p->columns; i++) {
- for (k = 0; k < p->colors; k++) {
+ for (i = 0; i < p->columns; i++)
+ {
+ for (k = 0; k < p->colors; k++)
+ {
int a = getcomponent(in, i * p->colors + k, p->bpc);
int b = p->encode ? a - left[k] : a + left[k];
int c = b % (1 << p->bpc);
@@ -138,7 +146,8 @@ png(fz_predict *p, unsigned char *in, unsigned char *out, int predictor)
{
int upleft[MAXC], left[MAXC], i, k;
- for (k = 0; k < p->bpp; k++) {
+ for (k = 0; k < p->bpp; k++)
+ {
left[k] = 0;
upleft[k] = 0;
}
@@ -187,7 +196,8 @@ fz_processpredict(fz_filter *filter, fz_buffer *in, fz_buffer *out)
while (1)
{
- if (in->rp + dec->stride + (!dec->encode && ispng) > in->wp) {
+ if (in->rp + dec->stride + (!dec->encode && ispng) > in->wp)
+ {
if (in->eof)
return fz_iodone;
return fz_ioneedin;
@@ -196,22 +206,27 @@ fz_processpredict(fz_filter *filter, fz_buffer *in, fz_buffer *out)
if (out->wp + dec->stride + (dec->encode && ispng) > out->ep)
return fz_ioneedout;
- if (dec->predictor == 1) {
+ if (dec->predictor == 1)
+ {
none(dec, in->rp, out->wp);
}
- else if (dec->predictor == 2) {
+ else if (dec->predictor == 2)
+ {
if (dec->bpc != 8)
memset(out->wp, 0, dec->stride);
tiff(dec, in->rp, out->wp);
}
- else {
- if (dec->encode) {
+ else
+ {
+ if (dec->encode)
+ {
predictor = dec->predictor - 10;
if (predictor < 0 || predictor > 4)
predictor = 1;
*out->wp ++ = predictor;
}
- else {
+ else
+ {
predictor = *in->rp++;
}
png(dec, in->rp, out->wp, predictor);
diff --git a/stream/filt_rld.c b/stream/filt_rld.c
index dc720fbd..74499a57 100644
--- a/stream/filt_rld.c
+++ b/stream/filt_rld.c
@@ -5,7 +5,7 @@ fz_error *
fz_newrld(fz_filter **fp, fz_obj *params)
{
FZ_NEWFILTER(fz_filter, f, rld);
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/filt_rle.c b/stream/filt_rle.c
index 6160e148..458bf3ba 100644
--- a/stream/filt_rle.c
+++ b/stream/filt_rle.c
@@ -42,7 +42,7 @@ fz_newrle(fz_filter **fp, fz_obj *params)
enc->state = ZERO;
enc->run = 0;
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/obj_array.c b/stream/obj_array.c
index cfcd3fb1..3953470a 100644
--- a/stream/obj_array.c
+++ b/stream/obj_array.c
@@ -10,7 +10,8 @@ fz_newarray(fz_obj **op, int initialcap)
int i;
obj = *op = fz_malloc(sizeof (fz_obj));
- if (!obj) return fz_outofmem;
+ if (!obj)
+ return fz_throw("outofmem: array struct");
obj->refs = 1;
obj->kind = FZ_ARRAY;
@@ -19,12 +20,16 @@ fz_newarray(fz_obj **op, int initialcap)
obj->u.a.cap = initialcap > 0 ? initialcap : 6;
obj->u.a.items = fz_malloc(sizeof (fz_obj*) * obj->u.a.cap);
- if (!obj->u.a.items) { fz_free(obj); return fz_outofmem; }
+ if (!obj->u.a.items)
+ {
+ fz_free(obj);
+ return fz_throw("outofmem: array item buffer");
+ }
for (i = 0; i < obj->u.a.cap; i++)
obj->u.a.items[i] = nil;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -35,18 +40,25 @@ fz_copyarray(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isarray(obj))
- return fz_throw("typecheck in copyarray");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
error = fz_newarray(&new, fz_arraylen(obj));
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new array");
- for (i = 0; i < fz_arraylen(obj); i++) {
+ for (i = 0; i < fz_arraylen(obj); i++)
+ {
error = fz_arraypush(new, fz_arrayget(obj, i));
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add item to array");
+ }
}
- return nil;
+ *op = new;
+
+ return fz_okay;
}
fz_error *
@@ -58,39 +70,69 @@ fz_deepcopyarray(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isarray(obj))
- return fz_throw("typecheck in deepcopyarray");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
error = fz_newarray(&new, fz_arraylen(obj));
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new array");
for (i = 0; i < fz_arraylen(obj); i++)
{
val = fz_arrayget(obj, i);
- if (fz_isarray(val)) {
+ if (fz_isarray(val))
+ {
error = fz_deepcopyarray(&val, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
+
error = fz_arraypush(new, val);
- if (error) { fz_dropobj(val); fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
+
fz_dropobj(val);
}
- else if (fz_isdict(val)) {
+ else if (fz_isdict(val))
+ {
error = fz_deepcopydict(&val, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
+
error = fz_arraypush(new, val);
- if (error) { fz_dropobj(val); fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
fz_dropobj(val);
}
- else {
+ else
+ {
error = fz_arraypush(new, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
}
}
- return nil;
+ *op = new;
+
+ return fz_okay;
}
int
@@ -117,17 +159,17 @@ fz_error *
fz_arrayput(fz_obj *obj, int i, fz_obj *item)
{
if (!fz_isarray(obj))
- return fz_throw("typecheck in arrayput");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
if (i < 0)
- return fz_throw("rangecheck in arrayput: %d < 0", i);
+ return fz_throw("assert: index %d < 0", i);
if (i >= obj->u.a.len)
- return fz_throw("rangecheck in arrayput: %d > %d", i, obj->u.a.len);
+ return fz_throw("assert: index %d > length %d", i, obj->u.a.len);
if (obj->u.a.items[i])
fz_dropobj(obj->u.a.items[i]);
obj->u.a.items[i] = fz_keepobj(item);
- return nil;
+ return fz_okay;
}
static fz_error *
@@ -139,14 +181,15 @@ growarray(fz_obj *obj)
newcap = obj->u.a.cap * 2;
newitems = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * newcap);
- if (!newitems) return fz_outofmem;
+ if (!newitems)
+ return fz_throw("outofmem: resize item buffer");
obj->u.a.items = newitems;
for (i = obj->u.a.cap ; i < newcap; i++)
obj->u.a.items[i] = nil;
obj->u.a.cap = newcap;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -155,17 +198,19 @@ fz_arraypush(fz_obj *obj, fz_obj *item)
fz_error *error;
if (!fz_isarray(obj))
- return fz_throw("typecheck in arraypush");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
- if (obj->u.a.len + 1 > obj->u.a.cap) {
+ if (obj->u.a.len + 1 > obj->u.a.cap)
+ {
error = growarray(obj);
- if (error) return error;
+ if (error)
+ return fz_rethrow(error, "cannot grow item buffer");
}
obj->u.a.items[obj->u.a.len] = fz_keepobj(item);
obj->u.a.len++;
- return nil;
+ return fz_okay;
}
void
diff --git a/stream/obj_dict.c b/stream/obj_dict.c
index 0981e8a3..a93ae1bd 100644
--- a/stream/obj_dict.c
+++ b/stream/obj_dict.c
@@ -30,7 +30,8 @@ fz_newdict(fz_obj **op, int initialcap)
int i;
obj = *op = fz_malloc(sizeof (fz_obj));
- if (!obj) return fz_outofmem;
+ if (!obj)
+ return fz_throw("outofmem: dict struct");
obj->refs = 1;
obj->kind = FZ_DICT;
@@ -40,9 +41,14 @@ fz_newdict(fz_obj **op, int initialcap)
obj->u.d.cap = initialcap > 0 ? initialcap : 10;
obj->u.d.items = fz_malloc(sizeof(fz_keyval) * obj->u.d.cap);
- if (!obj->u.d.items) { fz_free(obj); return fz_outofmem; }
+ if (!obj->u.d.items)
+ {
+ fz_free(obj);
+ return fz_throw("outofmem: dict item buffer");
+ }
- for (i = 0; i < obj->u.d.cap; i++) {
+ for (i = 0; i < obj->u.d.cap; i++)
+ {
obj->u.d.items[i].k = nil;
obj->u.d.items[i].v = nil;
}
@@ -58,18 +64,24 @@ fz_copydict(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isdict(obj))
- return fz_throw("typecheck in copydict");
+ return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
error = fz_newdict(&new, obj->u.d.cap);
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new dict");
- for (i = 0; i < fz_dictlen(obj); i++) {
+ for (i = 0; i < fz_dictlen(obj); i++)
+ {
error = fz_dictput(new, fz_dictgetkey(obj, i), fz_dictgetval(obj, i));
- if (error) { fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot copy dict entry");
+ }
}
- return nil;
+ *op = new;
+ return fz_okay;
}
fz_error *
@@ -81,39 +93,65 @@ fz_deepcopydict(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isdict(obj))
- return fz_throw("typecheck in deepcopydict");
+ return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
error = fz_newdict(&new, obj->u.d.cap);
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new dict");
for (i = 0; i < fz_dictlen(obj); i++)
{
val = fz_dictgetval(obj, i);
- if (fz_isarray(val)) {
+ if (fz_isarray(val))
+ {
error = fz_deepcopyarray(&val, val);
- if (error) { fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error) { fz_dropobj(val); fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot add copied dict entry");
+ }
fz_dropobj(val);
}
- else if (fz_isdict(val)) {
+ else if (fz_isdict(val))
+ {
error = fz_deepcopydict(&val, val);
- if (error) { fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error) { fz_dropobj(val); fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot add copied dict entry");
+ }
fz_dropobj(val);
}
- else {
+ else
+ {
error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error) { fz_dropobj(new); return error; }
+ if (error)
+ {
+ fz_dropobj(new);
+ return fz_rethrow(error, "cannot copy dict entry");
+ }
}
}
- return nil;
+ *op = new;
+ return fz_okay;
}
static fz_error *
@@ -126,16 +164,18 @@ growdict(fz_obj *obj)
newcap = obj->u.d.cap * 2;
newitems = fz_realloc(obj->u.d.items, sizeof(fz_keyval) * newcap);
- if (!newitems) return fz_outofmem;
+ if (!newitems)
+ return fz_throw("outofmem: resize item buffer");
obj->u.d.items = newitems;
- for (i = obj->u.d.cap; i < newcap; i++) {
+ for (i = obj->u.d.cap; i < newcap; i++)
+ {
obj->u.d.items[i].k = nil;
obj->u.d.items[i].v = nil;
}
obj->u.d.cap = newcap;
- return nil;
+ return fz_okay;
}
int
@@ -243,28 +283,28 @@ fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val)
int i;
if (!fz_isdict(obj))
- return fz_throw("typecheck in dictput");
+ return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
if (fz_isname(key))
s = fz_toname(key);
else if (fz_isstring(key))
s = fz_tostrbuf(key);
else
- return fz_throw("typecheck in dictput");
+ return fz_throw("assert: key is not string or name (%s)", fz_objkindstr(obj));
i = dictfinds(obj, s);
if (i >= 0)
{
fz_dropobj(obj->u.d.items[i].v);
obj->u.d.items[i].v = fz_keepobj(val);
- return nil;
+ return fz_okay;
}
if (obj->u.d.len + 1 > obj->u.d.cap)
{
error = growdict(obj);
if (error)
- return error;
+ return fz_rethrow(error, "cannot grow dict item buffer");
}
/* borked! */
@@ -276,7 +316,7 @@ fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val)
obj->u.d.items[obj->u.d.len].v = fz_keepobj(val);
obj->u.d.len ++;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -297,7 +337,7 @@ fz_dictdels(fz_obj *obj, char *key)
int i;
if (!fz_isdict(obj))
- return fz_throw("typecheck in dictdel");
+ return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
i = dictfinds(obj, key);
if (i >= 0)
@@ -309,7 +349,7 @@ fz_dictdels(fz_obj *obj, char *key)
obj->u.d.len --;
}
- return nil;
+ return fz_okay;
}
fz_error *
@@ -320,7 +360,7 @@ fz_dictdel(fz_obj *obj, fz_obj *key)
else if (fz_isstring(key))
return fz_dictdels(obj, fz_tostrbuf(key));
else
- return fz_throw("typecheck in dictdel");
+ return fz_throw("assert: key is not string or name (%s)", fz_objkindstr(obj));
}
void
diff --git a/stream/obj_parse.c b/stream/obj_parse.c
index 73b761cb..316e8ad4 100644
--- a/stream/obj_parse.c
+++ b/stream/obj_parse.c
@@ -62,6 +62,7 @@ static void parsekeyword(char **sp, char *b, char *eb)
static fz_error *parsename(fz_obj **obj, char **sp)
{
+ fz_error *error;
char buf[64];
char *s = *sp;
char *p = buf;
@@ -72,11 +73,15 @@ static fz_error *parsename(fz_obj **obj, char **sp)
*p++ = 0;
*sp = s;
- return fz_newname(obj, buf);
+ error = fz_newname(obj, buf);
+ if (error)
+ return fz_rethrow(error, "cannot create name");
+ return fz_okay;
}
static fz_error *parsenumber(fz_obj **obj, char **sp)
{
+ fz_error *error;
char buf[32];
char *s = *sp;
char *p = buf;
@@ -92,8 +97,13 @@ static fz_error *parsenumber(fz_obj **obj, char **sp)
*sp = s;
if (strchr(buf, '.'))
- return fz_newreal(obj, atof(buf));
- return fz_newint(obj, atoi(buf));
+ error = fz_newreal(obj, atof(buf));
+ else
+ error = fz_newint(obj, atoi(buf));
+
+ if (error)
+ return fz_rethrow(error, "cannot parse number");
+ return fz_okay;
}
static fz_error *parsedict(fz_obj **obj, char **sp, struct vap *v)
@@ -105,8 +115,8 @@ static fz_error *parsedict(fz_obj **obj, char **sp, struct vap *v)
char *s = *sp;
error = fz_newdict(&dict, 8);
- if (error) return error;
- *obj = dict;
+ if (error)
+ return fz_rethrow(error, "cannot create dict");
s += 2; /* skip "<<" */
@@ -115,39 +125,55 @@ static fz_error *parsedict(fz_obj **obj, char **sp, struct vap *v)
skipwhite(&s);
/* end-of-dict marker >> */
- if (*s == '>') {
+ if (*s == '>')
+ {
s ++;
- if (*s == '>') {
+ if (*s == '>')
+ {
s ++;
break;
}
- error = fz_throw("syntaxerror in parsedict");
+ error = fz_throw("malformed >> marker");
goto cleanup;
}
/* non-name as key, bail */
- if (*s != '/') {
- error = fz_throw("syntaxerror in parsedict");
+ if (*s != '/')
+ {
+ error = fz_throw("key is not a name");
goto cleanup;
}
error = parsename(&key, &s);
- if (error) goto cleanup;
+ if (error)
+ {
+ error = fz_rethrow(error, "cannot parse key");
+ goto cleanup;
+ }
skipwhite(&s);
error = parseobj(&val, &s, v);
- if (error) goto cleanup;
+ if (error)
+ {
+ error = fz_rethrow(error, "cannot parse value");
+ goto cleanup;
+ }
error = fz_dictput(dict, key, val);
- if (error) goto cleanup;
+ if (error)
+ {
+ error = fz_rethrow(error, "cannot insert dict entry");
+ goto cleanup;
+ }
fz_dropobj(val); val = nil;
fz_dropobj(key); key = nil;
}
+ *obj = dict;
*sp = s;
- return nil;
+ return fz_okay;
cleanup:
if (val) fz_dropobj(val);
@@ -166,8 +192,8 @@ static fz_error *parsearray(fz_obj **obj, char **sp, struct vap *v)
char *s = *sp;
error = fz_newarray(&a, 8);
- if (error) return error;
- *obj = a;
+ if (error)
+ return fz_rethrow(error, "cannot create array");
s ++; /* skip '[' */
@@ -175,26 +201,38 @@ static fz_error *parsearray(fz_obj **obj, char **sp, struct vap *v)
{
skipwhite(&s);
- if (*s == ']') {
+ if (*s == ']')
+ {
s ++;
break;
}
error = parseobj(&o, &s, v);
- if (error) { *obj = nil; fz_dropobj(a); return error; }
+ if (error)
+ {
+ fz_dropobj(a);
+ return fz_rethrow(error, "cannot parse item");
+ }
error = fz_arraypush(a, o);
- if (error) { fz_dropobj(o); *obj = nil; fz_dropobj(a); return error; }
+ if (error)
+ {
+ fz_dropobj(o);
+ fz_dropobj(a);
+ return fz_rethrow(error, "cannot add item to array");
+ }
fz_dropobj(o);
}
+ *obj = a;
*sp = s;
- return nil;
+ return fz_okay;
}
static fz_error *parsestring(fz_obj **obj, char **sp)
{
+ fz_error *error;
char buf[512];
char *s = *sp;
char *p = buf;
@@ -236,12 +274,12 @@ static fz_error *parsestring(fz_obj **obj, char **sp)
}
else switch (*s)
{
- case 'n': *p++ = '\n'; s++; break;
- case 'r': *p++ = '\r'; s++; break;
- case 't': *p++ = '\t'; s++; break;
- case 'b': *p++ = '\b'; s++; break;
- case 'f': *p++ = '\f'; s++; break;
- default: *p++ = *s++; break;
+ case 'n': *p++ = '\n'; s++; break;
+ case 'r': *p++ = '\r'; s++; break;
+ case 't': *p++ = '\t'; s++; break;
+ case 'b': *p++ = '\b'; s++; break;
+ case 'f': *p++ = '\f'; s++; break;
+ default: *p++ = *s++; break;
}
}
else
@@ -252,11 +290,16 @@ static fz_error *parsestring(fz_obj **obj, char **sp)
}
*sp = s;
- return fz_newstring(obj, buf, p - buf - 1);
+
+ error = fz_newstring(obj, buf, p - buf - 1);
+ if (error)
+ return fz_rethrow(error, "cannot create string");
+ return fz_okay;
}
static fz_error *parsehexstring(fz_obj **obj, char **sp)
{
+ fz_error *error;
char buf[512];
char *s = *sp;
char *p = buf;
@@ -287,7 +330,10 @@ static fz_error *parsehexstring(fz_obj **obj, char **sp)
}
*sp = s;
- return fz_newstring(obj, buf, p - buf);
+ error = fz_newstring(obj, buf, p - buf);
+ if (error)
+ return fz_rethrow(error, "cannot create string");
+ return fz_okay;
}
static fz_error *parseobj(fz_obj **obj, char **sp, struct vap *v)
@@ -299,7 +345,7 @@ static fz_error *parseobj(fz_obj **obj, char **sp, struct vap *v)
char *s = *sp;
if (*s == '\0')
- return fz_throw("syntaxerror in parseobj: end-of-string");
+ return fz_throw("end of data");
skipwhite(&s);
@@ -308,6 +354,7 @@ static fz_error *parseobj(fz_obj **obj, char **sp, struct vap *v)
if (v != nil && *s == '%')
{
s ++;
+
switch (*s)
{
case 'p': error = fz_newpointer(obj, va_arg(v->ap, void*)); break;
@@ -317,61 +364,102 @@ static fz_error *parseobj(fz_obj **obj, char **sp, struct vap *v)
case 'f': error = fz_newreal(obj, (float)va_arg(v->ap, double)); break;
case 'n': error = fz_newname(obj, va_arg(v->ap, char*)); break;
case 'r':
- oid = va_arg(v->ap, int);
- gid = va_arg(v->ap, int);
- error = fz_newindirect(obj, oid, gid);
- break;
+ oid = va_arg(v->ap, int);
+ gid = va_arg(v->ap, int);
+ error = fz_newindirect(obj, oid, gid);
+ break;
case 's':
- tmp = va_arg(v->ap, char*);
- error = fz_newstring(obj, tmp, strlen(tmp));
- break;
+ tmp = va_arg(v->ap, char*);
+ error = fz_newstring(obj, tmp, strlen(tmp));
+ break;
case '#':
- tmp = va_arg(v->ap, char*);
- len = va_arg(v->ap, int);
- error = fz_newstring(obj, tmp, len);
- break;
+ tmp = va_arg(v->ap, char*);
+ len = va_arg(v->ap, int);
+ error = fz_newstring(obj, tmp, len);
+ break;
default:
- error = fz_throw("unknown format specifier in packobj: '%c'", *s);
- break;
+ error = fz_throw("unknown format specifier in packobj: '%c'", *s);
+ break;
}
+
+ if (error)
+ error = fz_rethrow(error, "cannot create object for %% format");
+
s ++;
}
else if (*s == '/')
+ {
error = parsename(obj, &s);
+ if (error)
+ error = fz_rethrow(error, "cannot parse name");
+ }
else if (*s == '(')
+ {
error = parsestring(obj, &s);
+ if (error)
+ error = fz_rethrow(error, "cannot parse string");
+ }
- else if (*s == '<') {
+ else if (*s == '<')
+ {
if (s[1] == '<')
+ {
error = parsedict(obj, &s, v);
+ if (error)
+ error = fz_rethrow(error, "cannot parse dict");
+ }
else
+ {
error = parsehexstring(obj, &s);
+ if (error)
+ error = fz_rethrow(error, "cannot parse hex string");
+ }
}
else if (*s == '[')
+ {
error = parsearray(obj, &s, v);
+ if (error)
+ error = fz_rethrow(error, "cannot parse array");
+ }
else if (*s == '-' || *s == '.' || (*s >= '0' && *s <= '9'))
+ {
error = parsenumber(obj, &s);
+ if (error)
+ error = fz_rethrow(error, "cannot parse number");
+ }
else if (isregular(*s))
{
parsekeyword(&s, buf, buf + sizeof buf);
if (strcmp("true", buf) == 0)
+ {
error = fz_newbool(obj, 1);
+ if (error)
+ error = fz_rethrow(error, "cannot create bool (true)");
+ }
else if (strcmp("false", buf) == 0)
+ {
error = fz_newbool(obj, 0);
+ if (error)
+ error = fz_rethrow(error, "cannot create bool (false)");
+ }
else if (strcmp("null", buf) == 0)
+ {
error = fz_newnull(obj);
+ if (error)
+ error = fz_rethrow(error, "cannot create null object");
+ }
else
- error = fz_throw("syntaxerror in parseobj: undefined keyword %s", buf);
+ error = fz_throw("undefined keyword %s", buf);
}
else
- error = fz_throw("syntaxerror in parseobj");
+ error = fz_throw("syntax error: unknown byte 0x%d", *s);
*sp = s;
return error;
@@ -388,6 +476,8 @@ fz_packobj(fz_obj **op, char *fmt, ...)
va_copy(v.ap, ap);
error = parseobj(op, &fmt, &v);
+ if (error)
+ error = fz_rethrow(error, "cannot parse object");
va_end(ap);
diff --git a/stream/obj_simple.c b/stream/obj_simple.c
index 77a7bca8..d8eb77b7 100644
--- a/stream/obj_simple.c
+++ b/stream/obj_simple.c
@@ -4,18 +4,18 @@
extern void fz_droparray(fz_obj *array);
extern void fz_dropdict(fz_obj *dict);
-#define NEWOBJ(KIND,SIZE) \
- fz_obj *o; \
- o = *op = fz_malloc(SIZE); \
- if (!o) return fz_outofmem; \
- o->refs = 1; \
- o->kind = KIND; \
+#define NEWOBJ(KIND,SIZE) \
+ fz_obj *o; \
+ o = *op = fz_malloc(SIZE); \
+ if (!o) return fz_throw("outofmem: dynamic object"); \
+ o->refs = 1; \
+ o->kind = KIND;
fz_error *
fz_newnull(fz_obj **op)
{
NEWOBJ(FZ_NULL, sizeof (fz_obj));
- return nil;
+ return fz_okay;
}
fz_error *
@@ -23,7 +23,7 @@ fz_newbool(fz_obj **op, int b)
{
NEWOBJ(FZ_BOOL, sizeof (fz_obj));
o->u.b = b;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -31,7 +31,7 @@ fz_newint(fz_obj **op, int i)
{
NEWOBJ(FZ_INT, sizeof (fz_obj));
o->u.i = i;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -39,7 +39,7 @@ fz_newreal(fz_obj **op, float f)
{
NEWOBJ(FZ_REAL, sizeof (fz_obj));
o->u.f = f;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -49,7 +49,7 @@ fz_newstring(fz_obj **op, char *str, int len)
o->u.s.len = len;
memcpy(o->u.s.buf, str, len);
o->u.s.buf[len] = '\0';
- return nil;
+ return fz_okay;
}
fz_error *
@@ -57,7 +57,7 @@ fz_newname(fz_obj **op, char *str)
{
NEWOBJ(FZ_NAME, offsetof(fz_obj, u.n) + strlen(str) + 1);
strcpy(o->u.n, str);
- return nil;
+ return fz_okay;
}
fz_error *
@@ -66,7 +66,7 @@ fz_newindirect(fz_obj **op, int objid, int genid)
NEWOBJ(FZ_INDIRECT, sizeof (fz_obj));
o->u.r.oid = objid;
o->u.r.gid = genid;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -74,7 +74,7 @@ fz_newpointer(fz_obj **op, void *p)
{
NEWOBJ(FZ_POINTER, sizeof (fz_obj));
o->u.p = p;
- return nil;
+ return fz_okay;
}
fz_obj *
@@ -233,7 +233,7 @@ fz_topointer(fz_obj *obj)
{
if (fz_ispointer(obj))
return obj->u.p;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -242,7 +242,7 @@ fz_newnamefromstring(fz_obj **op, fz_obj *str)
NEWOBJ(FZ_NAME, offsetof(fz_obj, u.n) + fz_tostrlen(str) + 1);
memcpy(o->u.n, fz_tostrbuf(str), fz_tostrlen(str));
o->u.n[fz_tostrlen(str)] = '\0';
- return nil;
+ return fz_okay;
}
int
@@ -261,10 +261,12 @@ fz_objcmp(fz_obj *a, fz_obj *b)
case FZ_BOOL: return a->u.b - b->u.b;
case FZ_INT: return a->u.i - b->u.i;
case FZ_REAL: return a->u.f - b->u.f;
+
case FZ_STRING:
if (a->u.s.len != b->u.s.len)
return a->u.s.len - b->u.s.len;
return memcmp(a->u.s.buf, b->u.s.buf, a->u.s.len);
+
case FZ_NAME:
return strcmp(a->u.n, b->u.n);
@@ -299,3 +301,23 @@ fz_objcmp(fz_obj *a, fz_obj *b)
return 1;
}
+char *fz_objkindstr(fz_obj *obj)
+{
+ if (obj == nil)
+ return "<nil>";
+ switch (obj->kind)
+ {
+ case FZ_NULL: return "null";
+ case FZ_BOOL: return "boolean";
+ case FZ_INT: return "integer";
+ case FZ_REAL: return "real";
+ case FZ_STRING: return "string";
+ case FZ_NAME: return "name";
+ case FZ_ARRAY: return "array";
+ case FZ_DICT: return "dictionary";
+ case FZ_INDIRECT: return "reference";
+ case FZ_POINTER: return "pointer";
+ }
+ return "<unknown>";
+}
+
diff --git a/stream/stm_buffer.c b/stream/stm_buffer.c
index 4fc5636c..8a1e0850 100644
--- a/stream/stm_buffer.c
+++ b/stream/stm_buffer.c
@@ -7,19 +7,24 @@ fz_newbuffer(fz_buffer **bp, int size)
fz_buffer *b;
b = *bp = fz_malloc(sizeof(fz_buffer));
- if (!b) return fz_outofmem;
+ if (!b)
+ return fz_throw("outofmem: buffer struct");
b->refs = 1;
b->ownsdata = 1;
b->bp = fz_malloc(size);
- if (!b->bp) { fz_free(b); return fz_outofmem; }
+ if (!b->bp)
+ {
+ fz_free(b);
+ return fz_throw("outofmem: buffer memory");
+ }
b->rp = b->bp;
b->wp = b->bp;
b->ep = b->bp + size;
b->eof = 0;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -28,7 +33,8 @@ fz_newbufferwithmemory(fz_buffer **bp, unsigned char *data, int size)
fz_buffer *b;
b = *bp = fz_malloc(sizeof(fz_buffer));
- if (!b) return fz_outofmem;
+ if (!b)
+ return fz_throw("outofmem: buffer struct");
b->refs = 1;
b->ownsdata = 0;
@@ -39,7 +45,7 @@ fz_newbufferwithmemory(fz_buffer **bp, unsigned char *data, int size)
b->ep = b->bp + size;
b->eof = 0;
- return nil;
+ return fz_okay;
}
fz_buffer *
@@ -69,26 +75,31 @@ fz_growbuffer(fz_buffer *buf)
int wp = buf->wp - buf->bp;
int ep = buf->ep - buf->bp;
- assert(buf->ownsdata);
+ if (!buf->ownsdata)
+ return fz_throw("assert: grow borrowed memory");
newbp = fz_realloc(buf->bp, ep * 2);
- if (!newbp) return fz_outofmem;
+ if (!newbp)
+ return fz_throw("outofmem: resize buffer memory");
buf->bp = newbp;
buf->rp = buf->bp + rp;
buf->wp = buf->bp + wp;
buf->ep = buf->bp + ep * 2;
- return nil;
+ return fz_okay;
}
fz_error *
fz_rewindbuffer(fz_buffer *buf)
{
- assert(buf->ownsdata);
+ if (!buf->ownsdata)
+ return fz_throw("assert: rewind borrowed memory");
+
memmove(buf->bp, buf->rp, buf->wp - buf->rp);
buf->wp = buf->bp + (buf->wp - buf->rp);
buf->rp = buf->bp;
- return nil;
+
+ return fz_okay;
}
diff --git a/stream/stm_filter.c b/stream/stm_filter.c
index c8091136..65b917d5 100644
--- a/stream/stm_filter.c
+++ b/stream/stm_filter.c
@@ -1,9 +1,9 @@
#include "fitz-base.h"
#include "fitz-stream.h"
-fz_error fz_kioneedin = { -1, "<ioneedin>", "<process>", "filter.c", 0 };
-fz_error fz_kioneedout = { -1, "<ioneedout>", "<process>", "filter.c", 0 };
-fz_error fz_kiodone = { -1, "<iodone>", "<process>", "filter.c", 0 };
+fz_error fz_kioneedin = { -1, "<ioneedin>", "<internal>", "<internal>", 0, 0 };
+fz_error fz_kioneedout = { -1, "<ioneedout>", "<internal>", "<internal>", 0, 0 };
+fz_error fz_kiodone = { -1, "<iodone>", "<internal>", "<internal>", 0, 0 };
fz_error *
fz_process(fz_filter *f, fz_buffer *in, fz_buffer *out)
@@ -27,7 +27,11 @@ fz_process(fz_filter *f, fz_buffer *in, fz_buffer *out)
f->count += out->wp - oldwp;
if (reason != fz_ioneedin && reason != fz_ioneedout)
+ {
+ if (reason != fz_iodone)
+ reason = fz_rethrow(reason, "cannot process filter");
out->eof = 1;
+ }
return reason;
}
diff --git a/stream/stm_misc.c b/stream/stm_misc.c
index bc739055..22d993d1 100644
--- a/stream/stm_misc.c
+++ b/stream/stm_misc.c
@@ -5,14 +5,16 @@
#include "fitz-base.h"
#include "fitz-stream.h"
-int fz_tell(fz_stream *stm)
+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)
+fz_error *
+fz_seek(fz_stream *stm, int offset, int whence)
{
if (stm->mode == FZ_SREAD)
return fz_rseek(stm, offset, whence);
@@ -23,8 +25,11 @@ int fz_seek(fz_stream *stm, int offset, int whence)
* Read a line terminated by LF or CR or CRLF.
*/
-int fz_readline(fz_stream *stm, char *mem, int n)
+fz_error *
+fz_readline(fz_stream *stm, char *mem, int n)
{
+ fz_error *error;
+
char *s = mem;
int c = EOF;
while (n > 1)
@@ -45,7 +50,11 @@ int fz_readline(fz_stream *stm, char *mem, int n)
}
if (n)
*s = '\0';
- return s - mem;
+
+ error = fz_readerror(stm);
+ if (error)
+ return fz_rethrow(error, "cannot read line");
+ return fz_okay;
}
/*
@@ -55,8 +64,10 @@ int fz_readline(fz_stream *stm, char *mem, int n)
enum { CHUNKSIZE = 1024 * 4 };
-int fz_readall(fz_buffer **bufp, fz_stream *stm)
+fz_error *
+fz_readall(fz_buffer **bufp, fz_stream *stm)
{
+ fz_error *error;
fz_buffer *real;
unsigned char *newbuf;
unsigned char *buf;
@@ -64,8 +75,6 @@ int fz_readall(fz_buffer **bufp, fz_stream *stm)
int pos;
int n;
- *bufp = nil;
-
len = 0;
pos = 0;
buf = nil;
@@ -79,17 +88,16 @@ int fz_readall(fz_buffer **bufp, fz_stream *stm)
if (!newbuf)
{
fz_free(buf);
- return -1;
+ return fz_throw("outofmem: scratch buffer");
}
buf = newbuf;
}
- n = fz_read(stm, buf + pos, len - pos);
-
- if (n < 0)
+ error = fz_read(&n, stm, buf + pos, len - pos);
+ if (error)
{
fz_free(buf);
- return -1;
+ return fz_rethrow(error, "cannot read data");
}
pos += n;
@@ -102,16 +110,16 @@ int fz_readall(fz_buffer **bufp, fz_stream *stm)
if (!newbuf)
{
fz_free(buf);
- return -1;
+ return fz_throw("outofmem: scratch buffer");
}
}
else newbuf = buf;
- real = *bufp = fz_malloc(sizeof(fz_buffer));
+ real = fz_malloc(sizeof(fz_buffer));
if (!real)
{
fz_free(newbuf);
- return -1;
+ return fz_throw("outofmem: buffer struct");
}
real->refs = 1;
@@ -122,7 +130,8 @@ int fz_readall(fz_buffer **bufp, fz_stream *stm)
real->ep = buf + pos;
real->eof = 1;
- return real->wp - real->rp;
+ *bufp = real;
+ return fz_okay;
}
}
}
diff --git a/stream/stm_open.c b/stream/stm_open.c
index 43d2a60b..c10d1509 100644
--- a/stream/stm_open.c
+++ b/stream/stm_open.c
@@ -28,19 +28,6 @@ newstm(int kind, int mode)
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)
{
@@ -56,7 +43,7 @@ fz_dropstream(fz_stream *stm)
{
if (stm->error)
{
- fz_warn("unhandled %s", stm->error->msg);
+ fz_warn("dropping unhandled ioerror");
fz_droperror(stm->error);
}
@@ -92,13 +79,13 @@ openfile(fz_stream **stmp, char *path, int mode, int realmode)
stm = newstm(FZ_SFILE, mode);
if (!stm)
- return fz_outofmem;
+ return fz_throw("outofmem: stream struct");
error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE);
if (error)
{
fz_free(stm);
- return error;
+ return fz_rethrow(error, "cannot create buffer");
}
stm->file = open(path, realmode, 0666);
@@ -106,11 +93,11 @@ openfile(fz_stream **stmp, char *path, int mode, int realmode)
{
fz_dropbuffer(stm->buffer);
fz_free(stm);
- return fz_throw("ioerror: open '%s' failed: %s", path, strerror(errno));
+ return fz_throw("syserr: open '%s': %s", path, strerror(errno));
}
*stmp = stm;
- return nil;
+ return fz_okay;
}
static fz_error *
@@ -121,20 +108,20 @@ openfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src, int mode)
stm = newstm(FZ_SFILTER, mode);
if (!stm)
- return fz_outofmem;
+ return fz_throw("outofmem: stream struct");
error = fz_newbuffer(&stm->buffer, FZ_BUFSIZE);
if (error)
{
fz_free(stm);
- return error;
+ return fz_rethrow(error, "cannot create buffer");
}
stm->chain = fz_keepstream(src);
stm->filter = fz_keepfilter(flt);
*stmp = stm;
- return nil;
+ return fz_okay;
}
static fz_error *
@@ -144,7 +131,7 @@ openbuffer(fz_stream **stmp, fz_buffer *buf, int mode)
stm = newstm(FZ_SBUFFER, mode);
if (!stm)
- return fz_outofmem;
+ return fz_throw("outofmem: stream struct");
stm->buffer = fz_keepbuffer(buf);
@@ -152,18 +139,26 @@ openbuffer(fz_stream **stmp, fz_buffer *buf, int mode)
stm->buffer->eof = 1;
*stmp = stm;
- return nil;
+ return fz_okay;
}
fz_error * fz_openrfile(fz_stream **stmp, char *path)
{
- return openfile(stmp, path, FZ_SREAD, O_BINARY | O_RDONLY);
+ fz_error *error;
+ error = openfile(stmp, path, FZ_SREAD, O_BINARY | O_RDONLY);
+ if (error)
+ return fz_rethrow(error, "cannot open file for reading: '%s'", path);
+ return fz_okay;
}
fz_error * fz_openwfile(fz_stream **stmp, char *path)
{
- return openfile(stmp, path, FZ_SWRITE,
+ fz_error *error;
+ error = openfile(stmp, path, FZ_SWRITE,
O_BINARY | O_WRONLY | O_CREAT | O_TRUNC);
+ if (error)
+ return fz_rethrow(error, "cannot open file for writing: '%s'", path);
+ return fz_okay;
}
fz_error * fz_openafile(fz_stream **stmp, char *path)
@@ -173,51 +168,72 @@ fz_error * fz_openafile(fz_stream **stmp, char *path)
error = openfile(stmp, path, FZ_SWRITE, O_BINARY | O_WRONLY);
if (error)
- return error;
+ return fz_rethrow(error, "cannot open file for writing: '%s'", path);
t = lseek((*stmp)->file, 0, 2);
if (t < 0)
{
(*stmp)->dead = 1;
- return fz_throw("ioerror: lseek: %s", strerror(errno));
+ return fz_throw("syserr: lseek '%s': %s", path, strerror(errno));
}
- return nil;
+ return fz_okay;
}
fz_error * fz_openrfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src)
{
- return openfilter(stmp, flt, src, FZ_SREAD);
+ fz_error *error;
+ error = openfilter(stmp, flt, src, FZ_SREAD);
+ if (error)
+ return fz_rethrow(error, "cannot create reading filter stream");
+ return fz_okay;
}
fz_error * fz_openwfilter(fz_stream **stmp, fz_filter *flt, fz_stream *src)
{
- return openfilter(stmp, flt, src, FZ_SWRITE);
+ fz_error *error;
+ error = openfilter(stmp, flt, src, FZ_SWRITE);
+ if (error)
+ return fz_rethrow(error, "cannot create writing filter stream");
+ return fz_okay;
}
fz_error * fz_openrbuffer(fz_stream **stmp, fz_buffer *buf)
{
- return openbuffer(stmp, buf, FZ_SREAD);
+ fz_error *error;
+ error = openbuffer(stmp, buf, FZ_SREAD);
+ if (error)
+ return fz_rethrow(error, "cannot create reading buffer stream");
+ return fz_okay;
}
fz_error * fz_openwbuffer(fz_stream **stmp, fz_buffer *buf)
{
- return openbuffer(stmp, buf, FZ_SWRITE);
+ fz_error *error;
+ error = openbuffer(stmp, buf, FZ_SWRITE);
+ if (error)
+ return fz_rethrow(error, "cannot create writing buffer stream");
+ return fz_okay;
}
-fz_error * fz_openrmemory(fz_stream **stmp, unsigned char *mem, int len)
+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;
+ return fz_rethrow(error, "cannot create memory buffer");
error = fz_openrbuffer(stmp, buf);
+ if (error)
+ {
+ fz_dropbuffer(buf);
+ return fz_rethrow(error, "cannot open memory buffer stream");
+ }
fz_dropbuffer(buf);
- return error;
+ return fz_okay;
}
diff --git a/stream/stm_read.c b/stream/stm_read.c
index 0ab7e076..43bdb192 100644
--- a/stream/stm_read.c
+++ b/stream/stm_read.c
@@ -5,8 +5,8 @@
#include "fitz-base.h"
#include "fitz-stream.h"
-int
-fz_makedata(fz_stream *stm)
+fz_error *
+fz_readimp(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
fz_error *error;
@@ -15,23 +15,29 @@ fz_makedata(fz_stream *stm)
int n;
if (stm->dead)
- return -1;
+ return fz_throw("assert: read from dead stream");
if (stm->mode != FZ_SREAD)
- return -1;
+ return fz_throw("assert: read from writing stream");
if (buf->eof)
- return 0;
+ return fz_okay;
error = fz_rewindbuffer(buf);
if (error)
- goto cleanup;
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot rewind output buffer");
+ }
if (buf->ep - buf->wp == 0)
{
error = fz_growbuffer(buf);
if (error)
- goto cleanup;
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot grow output buffer");
+ }
}
switch (stm->kind)
@@ -41,14 +47,15 @@ fz_makedata(fz_stream *stm)
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;
+ return fz_throw("syserr: read: %s", strerror(errno));
}
+
if (n == 0)
buf->eof = 1;
buf->wp += n;
- return n;
+
+ return fz_okay;
case FZ_SFILTER:
produced = 0;
@@ -62,10 +69,11 @@ fz_makedata(fz_stream *stm)
if (reason == fz_ioneedin)
{
- if (fz_makedata(stm->chain) < 0)
+ error = fz_readimp(stm->chain);
+ if (error)
{
stm->dead = 1;
- return -1;
+ return fz_rethrow(error, "cannot read from input stream");
}
}
@@ -78,50 +86,52 @@ fz_makedata(fz_stream *stm)
{
error = fz_rewindbuffer(buf);
if (error)
- goto cleanup;
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot rewind buffer");
+ }
}
else
{
error = fz_growbuffer(buf);
if (error)
- goto cleanup;
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot grow buffer");
+ }
}
}
else if (reason == fz_iodone)
{
- return 0;
+ return fz_okay;
}
else
{
- error = reason;
- goto cleanup;
+ stm->dead = 1;
+ return fz_rethrow(reason, "cannot process filter");
}
}
case FZ_SBUFFER:
- return 0;
- }
+ return fz_okay;
- return -1;
-
-cleanup:
- stm->error = error;
- stm->dead = 1;
- return -1;
+ default:
+ return fz_throw("assert: unknown stream type");
+ }
}
-int fz_rtell(fz_stream *stm)
+int
+fz_rtell(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
int t;
if (stm->dead)
- return -1;
-
+ return EOF;
if (stm->mode != FZ_SREAD)
- return -1;
+ return EOF;
switch (stm->kind)
{
@@ -129,8 +139,9 @@ int fz_rtell(fz_stream *stm)
t = lseek(stm->file, 0, 1);
if (t < 0)
{
+ fz_warn("syserr: lseek: %s", strerror(errno));
stm->dead = 1;
- return -1;
+ return EOF;
}
return t - (buf->wp - buf->rp);
@@ -139,27 +150,30 @@ int fz_rtell(fz_stream *stm)
case FZ_SBUFFER:
return buf->rp - buf->bp;
- }
- return -1;
+ default:
+ return EOF;
+ }
}
-int fz_rseek(fz_stream *stm, int offset, int whence)
+fz_error *
+fz_rseek(fz_stream *stm, int offset, int whence)
{
+ fz_error *error;
fz_buffer *buf = stm->buffer;
int t, c;
if (stm->dead)
- return -1;
+ return fz_throw("assert: seek in dead stream");
if (stm->mode != FZ_SREAD)
- return -1;
+ return fz_throw("assert: read operation on writing stream");
if (whence == 1)
{
int cur = fz_rtell(stm);
if (cur < 0)
- return -1;
+ return fz_throw("cannot tell current position");
offset = cur + offset;
whence = 0;
}
@@ -172,103 +186,128 @@ int fz_rseek(fz_stream *stm, int offset, int whence)
t = lseek(stm->file, offset, whence);
if (t < 0)
{
- stm->error = fz_throw("ioerror: lseek: %s", strerror(errno));
stm->dead = 1;
- return -1;
+ return fz_throw("syserr: lseek: %s", strerror(errno));
}
buf->rp = buf->bp;
buf->wp = buf->bp;
- return t;
+ return fz_okay;
case FZ_SFILTER:
if (whence == 0)
{
- if (offset < fz_tell(stm))
+ if (offset < fz_rtell(stm))
{
- stm->error = fz_throw("ioerror: cannot seek back in filter");
stm->dead = 1;
- return -1;
+ return fz_throw("assert: seek backwards in filter");
}
- while (fz_tell(stm) < offset)
+ while (fz_rtell(stm) < offset)
{
c = fz_readbyte(stm);
if (c == EOF)
+ {
+ error = fz_readerror(stm);
+ if (error)
+ return fz_rethrow(error, "cannot seek forward in filter");
break;
+ }
}
- return fz_tell(stm);
- }
- else
- {
- stm->dead = 1;
- return -1;
+ return fz_okay;
}
+ stm->dead = 1;
+ return fz_throw("assert: relative seek in filter");
+
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 fz_okay;
- return -1;
+ default:
+ return fz_throw("unknown stream type");
+ }
}
-int fz_readbytex(fz_stream *stm)
+fz_error *
+fz_read(int *np, fz_stream *stm, unsigned char *mem, int n)
{
+ fz_error *error;
fz_buffer *buf = stm->buffer;
- if (buf->rp == buf->wp)
+ int i = 0;
+
+ while (i < n)
{
- if (buf->eof) return EOF;
- if (fz_makedata(stm) < 0) return EOF;
+ while (buf->rp < buf->wp && i < n)
+ mem[i++] = *buf->rp++;
+
+ if (buf->rp == buf->wp)
+ {
+ if (buf->eof)
+ {
+ *np = i;
+ return fz_okay;
+ }
+
+ error = fz_readimp(stm);
+ if (error)
+ return fz_rethrow(error, "cannot produce data");
+ }
}
- if (buf->rp < buf->wp)
- return *buf->rp++;
- return EOF;
+
+ *np = i;
+ return fz_okay;
}
-int fz_peekbytex(fz_stream *stm)
+fz_error *
+fz_readerror(fz_stream *stm)
+{
+ fz_error *error;
+ if (stm->error)
+ {
+ error = stm->error;
+ stm->error = nil;
+ return fz_rethrow(error, "delayed read error");
+ }
+ return fz_okay;
+}
+
+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->eof && !stm->error)
+ {
+ fz_error *error = fz_readimp(stm);
+ if (error)
+ stm->error = fz_rethrow(error, "cannot read data");
+ }
}
- if (buf->rp < buf->wp)
- return *buf->rp;
- return EOF;
+
+ return buf->rp < buf->wp ? *buf->rp++ : EOF ;
}
-int fz_read(fz_stream *stm, unsigned char * restrict mem, int n)
+int
+fz_peekbytex(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
- int i = 0;
- while (i < n)
+ if (buf->rp == buf->wp)
{
-#if 0
- while (buf->rp < buf->wp && i < n)
- mem[i++] = *buf->rp++;
-#else
- int l = buf->wp - buf->rp;
- int ln = n;
- unsigned char * restrict src = buf->rp;
- ln = MIN(n - i, l) + i;
- while (i < ln) {
- mem[i++] = *src++;
- }
- buf->rp = src;
-#endif
- if (buf->rp == buf->wp)
+ if (!buf->eof && !stm->error)
{
- if (buf->eof) return i;
- if (fz_makedata(stm) < 0) return -1;
+ fz_error *error = fz_readimp(stm);
+ if (error)
+ stm->error = fz_rethrow(error, "cannot read data");
}
}
- return i;
+ return buf->rp < buf->wp ? *buf->rp : EOF ;
}
diff --git a/stream/stm_write.c b/stream/stm_write.c
index f619da9c..23b268d9 100644
--- a/stream/stm_write.c
+++ b/stream/stm_write.c
@@ -5,16 +5,17 @@
#include "fitz-base.h"
#include "fitz-stream.h"
-int fz_wtell(fz_stream *stm)
+int
+fz_wtell(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
int t;
if (stm->dead)
- return -1;
+ return EOF;
if (stm->mode != FZ_SWRITE)
- return -1;
+ return EOF;
switch (stm->kind)
{
@@ -22,9 +23,9 @@ int fz_wtell(fz_stream *stm)
t = lseek(stm->file, 0, 1);
if (t < 0)
{
- stm->error = fz_throw("ioerror: lseek: %s", strerror(errno));
+ fz_warn("syserr: lseek: %s", strerror(errno));
stm->dead = 1;
- return -1;
+ return EOF;
}
return t + (buf->wp - buf->rp);
@@ -33,46 +34,47 @@ int fz_wtell(fz_stream *stm)
case FZ_SBUFFER:
return buf->wp - buf->bp;
- }
- return -1;
+ default:
+ return EOF;
+ }
}
-int fz_wseek(fz_stream *stm, int offset, int whence)
+fz_error *
+fz_wseek(fz_stream *stm, int offset, int whence)
{
fz_buffer *buf = stm->buffer;
int t;
if (stm->dead)
- return -1;
+ return fz_throw("assert: seek in dead stream");
if (stm->mode != FZ_SWRITE)
- return -1;
+ return fz_throw("assert: write operation on reading stream");
if (stm->kind != FZ_SFILE)
- return -1;
+ return fz_throw("assert: write seek on non-file stream");
t = lseek(stm->file, offset, whence);
if (t < 0)
{
- stm->error = fz_throw("ioerror: lseek: %s", strerror(errno));
stm->dead = 1;
- return -1;
+ return fz_throw("syserr: lseek: %s", strerror(errno));
}
buf->rp = buf->bp;
buf->wp = buf->bp;
buf->eof = 0;
- return t;
+ return fz_okay;
}
-static int flushfilter(fz_stream *stm)
+static fz_error *
+fz_flushfilterimp(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
fz_error *error;
fz_error *reason;
- int t;
loop:
@@ -81,20 +83,30 @@ loop:
if (reason == fz_ioneedin)
{
if (buf->rp > buf->ep)
- fz_rewindbuffer(buf);
+ {
+ error = fz_rewindbuffer(buf);
+ if (error)
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot rewind buffer");
+ }
+ }
else
{
error = fz_growbuffer(buf);
if (error)
- goto cleanup;
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot grow buffer");
+ }
}
}
else if (reason == fz_ioneedout)
{
- t = fz_flush(stm->chain);
- if (t < 0)
- return -1;
+ error = fz_flush(stm->chain);
+ if (error)
+ return fz_rethrow(error, "cannot flush chain buffer");
}
else if (reason == fz_iodone)
@@ -104,20 +116,15 @@ loop:
else
{
- error = reason;
- goto cleanup;
+ stm->dead = 1;
+ return fz_rethrow(reason, "cannot process filter");
}
/* 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;
+ return fz_okay;
}
/*
@@ -126,20 +133,21 @@ cleanup:
* Called by fz_write and fz_dropstream.
* If buffer is eof, then all data must be flushed.
*/
-int fz_flush(fz_stream *stm)
+fz_error *
+fz_flush(fz_stream *stm)
{
fz_buffer *buf = stm->buffer;
fz_error *error;
int t;
- if (stm->dead == 2)
- return 0;
+ if (stm->dead == 2) /* eod flag */
+ return fz_okay;
if (stm->dead)
- return -1;
+ return fz_throw("assert: flush on dead stream");
if (stm->mode != FZ_SWRITE)
- return -1;
+ return fz_throw("assert: write operation on reading stream");
switch (stm->kind)
{
@@ -149,21 +157,30 @@ int fz_flush(fz_stream *stm)
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;
+ return fz_throw("syserr: write: %s", strerror(errno));
}
buf->rp += t;
}
if (buf->rp > buf->bp)
- fz_rewindbuffer(buf);
+ {
+ error = fz_rewindbuffer(buf);
+ if (error)
+ {
+ stm->dead = 1;
+ return fz_rethrow(error, "cannot rewind buffer");
+ }
+ }
- return 0;
+ return fz_okay;
case FZ_SFILTER:
- return flushfilter(stm);
+ error = fz_flushfilterimp(stm);
+ if (error)
+ return fz_rethrow(error, "cannot flush through filter");
+ return fz_okay;
case FZ_SBUFFER:
if (!buf->eof && buf->wp == buf->ep)
@@ -171,36 +188,35 @@ int fz_flush(fz_stream *stm)
error = fz_growbuffer(buf);
if (error)
{
- stm->error = error;
stm->dead = 1;
- return -1;
+ return fz_rethrow(error, "cannot grow buffer");
}
}
- return 0;
- }
+ return fz_okay;
- return -1;
+ default:
+ return fz_throw("unknown stream type");
+ }
}
/*
* Write data to stream.
* Buffer until internal buffer is full.
* When full, call fz_flush to make more space available.
+ * Return error if all the data could not be written.
*/
-int fz_write(fz_stream *stm, unsigned char *mem, int n)
+fz_error *
+fz_write(fz_stream *stm, unsigned char *mem, int n)
{
fz_buffer *buf = stm->buffer;
+ fz_error *error;
int i = 0;
- int t;
-
- if (stm->dead == 2)
- return 0;
if (stm->dead)
- return -1;
+ return fz_throw("assert: write on dead stream");
if (stm->mode != FZ_SWRITE)
- return -1;
+ return fz_throw("assert: write on reading stream");
while (i < n)
{
@@ -209,24 +225,27 @@ int fz_write(fz_stream *stm, unsigned char *mem, int n)
if (buf->wp == buf->ep && i < n)
{
- t = fz_flush(stm);
- if (t < 0)
- return -1;
+ error = fz_flush(stm);
+ if (error)
+ return fz_rethrow(error, "cannot flush buffer");
if (stm->dead)
- return i;
+ return fz_throw("assert: write on dead stream");
}
}
- return n;
+ return fz_okay;
}
-int fz_printstr(fz_stream *stm, char *s)
+fz_error *
+fz_printstr(fz_stream *stm, char *s)
{
- return fz_write(stm, (unsigned char *) s, strlen(s));
+ return fz_write(stm, s, strlen(s));
}
-int fz_printobj(fz_stream *file, fz_obj *obj, int tight)
+fz_error *
+fz_printobj(fz_stream *file, fz_obj *obj, int tight)
{
+ fz_error *error;
char buf[1024];
char *ptr;
int n;
@@ -235,22 +254,29 @@ int fz_printobj(fz_stream *file, fz_obj *obj, int tight)
if (n < sizeof buf)
{
fz_sprintobj(buf, sizeof buf, obj, tight);
- return fz_write(file, (unsigned char *) buf, n);
+ error = fz_write(file, buf, n);
+ if (error)
+ return fz_rethrow(error, "cannot write buffer");
+ return fz_okay;
}
else
{
ptr = fz_malloc(n);
if (!ptr)
- return -1;
+ return fz_throw("outofmem: scratch buffer");
fz_sprintobj(ptr, n, obj, tight);
- n = fz_write(file, (unsigned char *) ptr, n);
+ error = fz_write(file, ptr, n);
+ if (error)
+ error = fz_rethrow(error, "cannot write buffer");
fz_free(ptr);
- return n;
+ return error;
}
}
-int fz_print(fz_stream *stm, char *fmt, ...)
+fz_error *
+fz_print(fz_stream *stm, char *fmt, ...)
{
+ fz_error *error;
va_list ap;
char buf[1024];
char *p;
@@ -261,20 +287,27 @@ int fz_print(fz_stream *stm, char *fmt, ...)
va_end(ap);
if (n < sizeof buf)
- return fz_write(stm, (unsigned char *) buf, n);
+ {
+ error = fz_write(stm, buf, n);
+ if (error)
+ return fz_rethrow(error, "cannot write buffer");
+ return fz_okay;
+ }
p = fz_malloc(n);
if (!p)
- return -1;
+ return fz_throw("outofmem: scratch buffer");
va_start(ap, fmt);
vsnprintf(p, n, fmt, ap);
va_end(ap);
- n = fz_write(stm, (unsigned char *) p, n);
+ error = fz_write(stm, p, n);
+ if (error)
+ error = fz_rethrow(error, "cannot write buffer");
fz_free(p);
- return n;
+ return error;
}