summaryrefslogtreecommitdiff
path: root/fitz
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2011-12-17 00:07:09 +0000
committerRobin Watts <robin.watts@artifex.com>2011-12-17 00:07:09 +0000
commitc53b6af33c996a7ae6815ac15254297d43f43a9c (patch)
treefb3a9d65ec564cacc60d8a59b2310867ae0926cb /fitz
parent6888c5757779610c9da201e34b70c1800b898616 (diff)
downloadmupdf-c53b6af33c996a7ae6815ac15254297d43f43a9c.tar.xz
Change stream 'close' functions to facilitate error cleanup.
Rather than passing a stream to a close function, just pass context and state - that's all that is required. This enables us to call close to cleanup neatly if the stream fails to allocate.
Diffstat (limited to 'fitz')
-rw-r--r--fitz/filt_basic.c48
-rw-r--r--fitz/filt_dctd.c16
-rw-r--r--fitz/filt_faxd.c16
-rw-r--r--fitz/filt_flate.c12
-rw-r--r--fitz/filt_jbig2d.c6
-rw-r--r--fitz/filt_lzwd.c6
-rw-r--r--fitz/filt_predict.c12
-rw-r--r--fitz/fitz.h4
-rw-r--r--fitz/stm_open.c39
9 files changed, 74 insertions, 85 deletions
diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c
index 1096f266..3e86514f 100644
--- a/fitz/filt_basic.c
+++ b/fitz/filt_basic.c
@@ -27,11 +27,11 @@ read_null(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_null(fz_stream *stm)
+close_null(fz_context *ctx, void *state_)
{
- struct null_filter *state = stm->state;
+ struct null_filter *state = (struct null_filter *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
@@ -39,7 +39,6 @@ fz_stream *
fz_open_null(fz_stream *chain, int len)
{
struct null_filter *state;
- fz_stream *stream;
fz_context *ctx = chain->ctx;
assert(chain);
@@ -47,16 +46,7 @@ fz_open_null(fz_stream *chain, int len)
state->chain = chain;
state->remain = len;
- fz_try(ctx)
- {
- stream = fz_new_stream(ctx, state, read_null, close_null);
- }
- fz_catch(ctx)
- {
- fz_free(ctx, state);
- fz_rethrow(ctx);
- }
- return stream;
+ return fz_new_stream(ctx, state, read_null, close_null);
}
/* ASCII Hex Decode */
@@ -143,11 +133,11 @@ read_ahxd(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_ahxd(fz_stream *stm)
+close_ahxd(fz_context *ctx, void *state_)
{
- fz_ahxd *state = stm->state;
+ fz_ahxd *state = (fz_ahxd *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
@@ -279,12 +269,12 @@ read_a85d(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_a85d(fz_stream *stm)
+close_a85d(fz_context *ctx, void *state_)
{
- fz_a85d *state = stm->state;
+ fz_a85d *state = (fz_a85d *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
@@ -367,12 +357,12 @@ read_rld(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_rld(fz_stream *stm)
+close_rld(fz_context *ctx, void *state_)
{
- fz_rld *state = stm->state;
+ fz_rld *state = (void *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
@@ -411,12 +401,12 @@ read_arc4(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_arc4(fz_stream *stm)
+close_arc4(fz_context *ctx, void *state_)
{
- fz_arc4c *state = stm->state;
+ fz_arc4c *state = (fz_arc4c *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
@@ -493,12 +483,12 @@ read_aesd(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_aesd(fz_stream *stm)
+close_aesd(fz_context *ctx, void *state_)
{
- fz_aesd *state = stm->state;
+ fz_aesd *state = (fz_aesd *)state_;
fz_stream *chain = state->chain;
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
fz_close(chain);
}
diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c
index 5fb1a933..bae91195 100644
--- a/fitz/filt_dctd.c
+++ b/fitz/filt_dctd.c
@@ -180,14 +180,15 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_dctd(fz_stream *stm)
+close_dctd(fz_context *ctx, void *state_)
{
- fz_dctd *state = stm->state;
+ fz_dctd *state = (fz_dctd *)state_;
if (setjmp(state->jb))
{
- state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
- fz_warn(state->ctx, "jpeg error: %s", state->msg);
+ if (state->cinfo.src)
+ state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
+ fz_warn(ctx, "jpeg error: %s", state->msg);
goto skip;
}
@@ -195,11 +196,12 @@ close_dctd(fz_stream *stm)
jpeg_finish_decompress(&state->cinfo);
skip:
- state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
+ if (state->cinfo.src)
+ state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
jpeg_destroy_decompress(&state->cinfo);
- fz_free(stm->ctx, state->scanline);
+ fz_free(ctx, state->scanline);
fz_close(state->chain);
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
}
fz_stream *
diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c
index be88eda1..309844bf 100644
--- a/fitz/filt_faxd.c
+++ b/fitz/filt_faxd.c
@@ -643,9 +643,9 @@ rtc:
}
static void
-close_faxd(fz_stream *stm)
+close_faxd(fz_context *ctx, void *state_)
{
- fz_faxd *fax = stm->state;
+ fz_faxd *fax = (fz_faxd *)state_;
int i;
/* if we read any extra bytes, try to put them back */
@@ -654,9 +654,9 @@ close_faxd(fz_stream *stm)
fz_unread_byte(fax->chain);
fz_close(fax->chain);
- fz_free(stm->ctx, fax->ref);
- fz_free(stm->ctx, fax->dst);
- fz_free(stm->ctx, fax);
+ fz_free(ctx, fax->ref);
+ fz_free(ctx, fax->dst);
+ fz_free(ctx, fax);
}
fz_stream *
@@ -665,7 +665,6 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
fz_faxd *fax;
fz_obj *obj;
fz_context *ctx;
- fz_stream *stream;
assert(chain);
ctx = chain->ctx;
@@ -724,8 +723,6 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
memset(fax->ref, 0, fax->stride);
memset(fax->dst, 0, fax->stride);
-
- stream = fz_new_stream(ctx, fax, read_faxd, close_faxd);
}
fz_catch(ctx)
{
@@ -734,5 +731,6 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
fz_free(ctx, fax);
fz_rethrow(ctx);
}
- return stream;
+
+ return fz_new_stream(ctx, fax, read_faxd, close_faxd);
}
diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c
index aafb0fc7..d83228a1 100644
--- a/fitz/filt_flate.c
+++ b/fitz/filt_flate.c
@@ -67,17 +67,17 @@ read_flated(fz_stream *stm, unsigned char *outbuf, int outlen)
}
static void
-close_flated(fz_stream *stm)
+close_flated(fz_context *ctx, void *state_)
{
- fz_flate *state = stm->state;
+ fz_flate *state = (fz_flate *)state_;
int code;
code = inflateEnd(&state->z);
if (code != Z_OK)
- fz_warn(stm->ctx, "zlib error: inflateEnd: %s", state->z.msg);
+ fz_warn(ctx, "zlib error: inflateEnd: %s", state->z.msg);
fz_close(state->chain);
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
}
fz_stream *
@@ -86,7 +86,6 @@ fz_open_flated(fz_stream *chain)
fz_flate *state;
int code = Z_OK;
fz_context *ctx = chain->ctx;
- fz_stream *stream;
fz_var(code);
@@ -104,7 +103,6 @@ fz_open_flated(fz_stream *chain)
code = inflateInit(&state->z);
if (code != Z_OK)
fz_throw(ctx, "zlib error: inflateInit: %s", state->z.msg);
- stream = fz_new_stream(chain->ctx, state, read_flated, close_flated);
}
fz_catch(ctx)
{
@@ -113,5 +111,5 @@ fz_open_flated(fz_stream *chain)
fz_free(ctx, state);
fz_rethrow(ctx);
}
- return stream;
+ return fz_new_stream(chain->ctx, state, read_flated, close_flated);
}
diff --git a/fitz/filt_jbig2d.c b/fitz/filt_jbig2d.c
index e90be9f8..69e3df50 100644
--- a/fitz/filt_jbig2d.c
+++ b/fitz/filt_jbig2d.c
@@ -29,16 +29,16 @@ struct fz_jbig2d_s
};
static void
-close_jbig2d(fz_stream *stm)
+close_jbig2d(fz_context *ctx, void *state_)
{
- fz_jbig2d *state = stm->state;
+ fz_jbig2d *state = (fz_jbig2d *)state_;
if (state->page)
jbig2_release_page(state->ctx, state->page);
if (state->gctx)
jbig2_global_ctx_free(state->gctx);
jbig2_ctx_free(state->ctx);
fz_close(state->chain);
- fz_free(stm->ctx, state);
+ fz_free(ctx, state);
}
static int
diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c
index eda52669..1ac588c4 100644
--- a/fitz/filt_lzwd.c
+++ b/fitz/filt_lzwd.c
@@ -156,11 +156,11 @@ read_lzwd(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_lzwd(fz_stream *stm)
+close_lzwd(fz_context *ctx, void *state_)
{
- fz_lzwd *lzw = stm->state;
+ fz_lzwd *lzw = (fz_lzwd *)state_;
fz_close(lzw->chain);
- fz_free(stm->ctx, lzw);
+ fz_free(ctx, lzw);
}
fz_stream *
diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c
index 72b91439..ab4a2eaa 100644
--- a/fitz/filt_predict.c
+++ b/fitz/filt_predict.c
@@ -177,14 +177,14 @@ read_predict(fz_stream *stm, unsigned char *buf, int len)
}
static void
-close_predict(fz_stream *stm)
+close_predict(fz_context *ctx, void *state_)
{
- fz_predict *state = stm->state;
+ fz_predict *state = (fz_predict *)state_;
fz_close(state->chain);
- fz_free(stm->ctx, state->in);
- fz_free(stm->ctx, state->out);
- fz_free(stm->ctx, state->ref);
- fz_free(stm->ctx, state);
+ fz_free(ctx, state->in);
+ fz_free(ctx, state->out);
+ fz_free(ctx, state->ref);
+ fz_free(ctx, state);
}
fz_stream *
diff --git a/fitz/fitz.h b/fitz/fitz.h
index b4c3cd15..589f22ee 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -550,7 +550,7 @@ struct fz_stream_s
unsigned char *bp, *rp, *wp, *ep;
void *state;
int (*read)(fz_stream *stm, unsigned char *buf, int len);
- void (*close)(fz_stream *stm);
+ void (*close)(fz_context *ctx, void *state);
void (*seek)(fz_stream *stm, int offset, int whence);
unsigned char buf[4096];
};
@@ -562,7 +562,7 @@ fz_stream *fz_open_buffer(fz_context *ctx, fz_buffer *buf);
fz_stream *fz_open_memory(fz_context *ctx, unsigned char *data, int len);
void fz_close(fz_stream *stm);
-fz_stream *fz_new_stream(fz_context *ctx, void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *));
+fz_stream *fz_new_stream(fz_context *ctx, void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_context *, void *));
fz_stream *fz_keep_stream(fz_stream *stm);
void fz_fill_buffer(fz_stream *stm);
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index ae918667..fde4f964 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -3,11 +3,19 @@
fz_stream *
fz_new_stream(fz_context *ctx, void *state,
int(*read)(fz_stream *stm, unsigned char *buf, int len),
- void(*close)(fz_stream *stm))
+ void(*close)(fz_context *ctx, void *state))
{
fz_stream *stm;
- stm = fz_malloc_struct(ctx, fz_stream);
+ fz_try(ctx)
+ {
+ stm = fz_malloc_struct(ctx, fz_stream);
+ }
+ fz_catch(ctx)
+ {
+ close(ctx, state);
+ fz_rethrow(ctx);
+ }
stm->refs = 1;
stm->error = 0;
@@ -47,7 +55,7 @@ fz_close(fz_stream *stm)
if (stm->refs == 0)
{
if (stm->close)
- stm->close(stm);
+ stm->close(stm->ctx, stm->state);
fz_free(stm->ctx, stm);
}
}
@@ -72,12 +80,12 @@ static void seek_file(fz_stream *stm, int offset, int whence)
stm->wp = stm->bp;
}
-static void close_file(fz_stream *stm)
+static void close_file(fz_context *ctx, void *state)
{
- int n = close(*(int*)stm->state);
+ int n = close(*(int*)state);
if (n < 0)
- fz_warn(stm->ctx, "close error: %s", strerror(errno));
- fz_free(stm->ctx, stm->state);
+ fz_warn(ctx, "close error: %s", strerror(errno));
+ fz_free(ctx, state);
}
fz_stream *
@@ -89,15 +97,7 @@ fz_open_fd(fz_context *ctx, int fd)
state = fz_malloc_struct(ctx, int);
*state = fd;
- fz_try(ctx)
- {
- stm = fz_new_stream(ctx, state, read_file, close_file);
- }
- fz_catch(ctx)
- {
- fz_free(ctx, state);
- fz_rethrow(ctx);
- }
+ stm = fz_new_stream(ctx, state, read_file, close_file);
stm->seek = seek_file;
return stm;
@@ -142,10 +142,11 @@ static void seek_buffer(fz_stream *stm, int offset, int whence)
stm->wp = stm->ep;
}
-static void close_buffer(fz_stream *stm)
+static void close_buffer(fz_context *ctx, void *state_)
{
- if (stm->state)
- fz_drop_buffer(stm->ctx, stm->state);
+ fz_buffer *state = (fz_buffer *)state_;
+ if (state)
+ fz_drop_buffer(ctx, state);
}
fz_stream *