diff options
author | Robin Watts <Robin.Watts@artifex.com> | 2011-10-04 18:44:19 +0100 |
---|---|---|
committer | Robin Watts <Robin.Watts@artifex.com> | 2011-10-04 18:44:19 +0100 |
commit | d208be26537db558edb70236ae517cea31b7ebab (patch) | |
tree | 57da95b97e354a53bd4517a42010e90968f007d9 /pdf/pdf_stream.c | |
parent | ba46cad4b09bb957085900a203206c8fa5868cd4 (diff) | |
download | mupdf-d208be26537db558edb70236ae517cea31b7ebab.tar.xz |
Move to exception handling rather than error passing throughout.
This frees us from passing errors back everywhere, and hence enables us
to pass results back as return values.
Rather than having to explicitly check for errors everywhere and bubble
them, we now allow exception handling to do the work for us; the
downside to this is that we no longer emit as much debugging information
as we did before (though this could be put back in). For now, the
debugging information we have lost has been retained in comments
with 'RJW:' at the start.
This code needs fuller testing, but is being committed as a work in
progress.
Diffstat (limited to 'pdf/pdf_stream.c')
-rw-r--r-- | pdf/pdf_stream.c | 141 |
1 files changed, 62 insertions, 79 deletions
diff --git a/pdf/pdf_stream.c b/pdf/pdf_stream.c index c7259bc3..614f076a 100644 --- a/pdf/pdf_stream.c +++ b/pdf/pdf_stream.c @@ -7,17 +7,11 @@ int pdf_is_stream(pdf_xref *xref, int num, int gen) { - fz_error error; - if (num < 0 || num >= xref->len) return 0; - error = pdf_cache_object(xref, num, gen); - if (error) - { - fz_error_handle(error, "cannot load object, ignoring error"); - return 0; - } + pdf_cache_object(xref, num, gen); + /* RJW: "cannot load object, ignoring error" */ return xref->table[num].stm_ofs > 0; } @@ -57,7 +51,6 @@ pdf_stream_has_crypt(fz_context *ctx, fz_obj *stm) static fz_stream * build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num, int gen) { - fz_error error; char *s; fz_context *ctx = chain->ctx; @@ -100,9 +93,8 @@ build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num, if (obj) { fz_buffer *globals; - error = pdf_load_stream(&globals, xref, fz_to_num(obj), fz_to_gen(obj)); - if (error) - fz_error_handle(error, "cannot load jbig2 global segments"); + globals = pdf_load_stream(xref, fz_to_num(obj), fz_to_gen(obj)); + /* RJW: "cannot load jbig2 global segments" */ chain = fz_open_jbig2d(chain, globals); fz_drop_buffer(ctx, globals); return chain; @@ -233,29 +225,26 @@ pdf_open_inline_stream(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int len * Open a stream for reading the raw (compressed but decrypted) data. * Using xref->file while this is open is a bad idea. */ -fz_error -pdf_open_raw_stream(fz_stream **stmp, pdf_xref *xref, int num, int gen) +fz_stream * +pdf_open_raw_stream(pdf_xref *xref, int num, int gen) { - pdf_xref_entry *x; - fz_error error; + pdf_xref_entry * volatile x; + fz_stream *stm; if (num < 0 || num >= xref->len) - return fz_error_make("object id out of range (%d %d R)", num, gen); + fz_throw(xref->ctx, "object id out of range (%d %d R)", num, gen); x = xref->table + num; - error = pdf_cache_object(xref, num, gen); - if (error) - return fz_error_note(error, "cannot load stream object (%d %d R)", num, gen); + pdf_cache_object(xref, num, gen); + /* RJW: "cannot load stream object (%d %d R)", num, gen */ - if (x->stm_ofs) - { - *stmp = pdf_open_raw_filter(xref->file, xref, x->obj, num, gen); - fz_seek(xref->file, x->stm_ofs, 0); - return fz_okay; - } + if (x->stm_ofs == 0) + fz_throw(xref->ctx, "object is not a stream"); - return fz_error_make("object is not a stream"); + stm = pdf_open_raw_filter(xref->file, xref, x->obj, num, gen); + fz_seek(xref->file, x->stm_ofs, 0); + return stm; } /* @@ -263,75 +252,67 @@ pdf_open_raw_stream(fz_stream **stmp, pdf_xref *xref, int num, int gen) * Put the opened file in xref->stream. * Using xref->file while a stream is open is a Bad idea. */ -fz_error -pdf_open_stream(fz_stream **stmp, pdf_xref *xref, int num, int gen) +fz_stream * +pdf_open_stream(pdf_xref *xref, int num, int gen) { pdf_xref_entry *x; - fz_error error; + fz_stream *stm; if (num < 0 || num >= xref->len) - return fz_error_make("object id out of range (%d %d R)", num, gen); + fz_throw(xref->ctx, "object id out of range (%d %d R)", num, gen); x = xref->table + num; - error = pdf_cache_object(xref, num, gen); - if (error) - return fz_error_note(error, "cannot load stream object (%d %d R)", num, gen); + pdf_cache_object(xref, num, gen); + /* RJW: "cannot load stream object (%d %d R)", num, gen */ - if (x->stm_ofs) - { - *stmp = pdf_open_filter(xref->file, xref, x->obj, num, gen); - fz_seek(xref->file, x->stm_ofs, 0); - return fz_okay; - } + if (x->stm_ofs == 0) + fz_throw(xref->ctx, "object is not a stream"); - return fz_error_make("object is not a stream"); + stm = pdf_open_filter(xref->file, xref, x->obj, num, gen); + fz_seek(xref->file, x->stm_ofs, 0); + return stm; } -fz_error -pdf_open_stream_at(fz_stream **stmp, pdf_xref *xref, int num, int gen, fz_obj *dict, int stm_ofs) +fz_stream * +pdf_open_stream_at(pdf_xref *xref, int num, int gen, fz_obj *dict, int stm_ofs) { - if (stm_ofs) - { - *stmp = pdf_open_filter(xref->file, xref, dict, num, gen); - fz_seek(xref->file, stm_ofs, 0); - return fz_okay; - } - return fz_error_make("object is not a stream"); + fz_stream *stm; + + if (stm_ofs == 0) + fz_throw(xref->ctx, "object is not a stream"); + + stm = pdf_open_filter(xref->file, xref, dict, num, gen); + fz_seek(xref->file, stm_ofs, 0); + return stm; } /* * Load raw (compressed but decrypted) contents of a stream into buf. */ -fz_error -pdf_load_raw_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen) +fz_buffer * +pdf_load_raw_stream(pdf_xref *xref, int num, int gen) { - fz_error error; fz_stream *stm; fz_obj *dict; int len; + fz_buffer *buf; - error = pdf_load_object(&dict, xref, num, gen); - if (error) - return fz_error_note(error, "cannot load stream dictionary (%d %d R)", num, gen); + dict = pdf_load_object(xref, num, gen); + /* RJW: "cannot load stream dictionary (%d %d R)", num, gen */ len = fz_to_int(fz_dict_gets(dict, "Length")); fz_drop_obj(dict); - error = pdf_open_raw_stream(&stm, xref, num, gen); - if (error) - return fz_error_note(error, "cannot open raw stream (%d %d R)", num, gen); + stm = pdf_open_raw_stream(xref, num, gen); + /* RJW: "cannot open raw stream (%d %d R)", num, gen */ - error = fz_read_all(bufp, stm, len); - if (error) - { - fz_close(stm); - return fz_error_note(error, "cannot read raw stream (%d %d R)", num, gen); - } + buf = fz_read_all(stm, len); + /* RJW: "cannot read raw stream (%d %d R)", num, gen */ fz_close(stm); - return fz_okay; + return buf; } static int @@ -353,21 +334,20 @@ pdf_guess_filter_length(int len, char *filter) /* * Load uncompressed contents of a stream into buf. */ -fz_error -pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen) +fz_buffer * +pdf_load_stream(pdf_xref *xref, int num, int gen) { - fz_error error; + fz_context *ctx = xref->ctx; fz_stream *stm; fz_obj *dict, *obj; int i, len, n; + fz_buffer * volatile buf; - error = pdf_open_stream(&stm, xref, num, gen); - if (error) - return fz_error_note(error, "cannot open stream (%d %d R)", num, gen); + stm = pdf_open_stream(xref, num, gen); + /* RJW: "cannot open stream (%d %d R)", num, gen */ - error = pdf_load_object(&dict, xref, num, gen); - if (error) - return fz_error_note(error, "cannot load stream dictionary (%d %d R)", num, gen); + dict = pdf_load_object(xref, num, gen); + /* RJW: "cannot load stream dictionary (%d %d R)", num, gen */ len = fz_to_int(fz_dict_gets(dict, "Length")); obj = fz_dict_gets(dict, "Filter"); @@ -378,13 +358,16 @@ pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen) fz_drop_obj(dict); - error = fz_read_all(bufp, stm, len); - if (error) + fz_try(ctx) + { + buf = fz_read_all(stm, len); + } + fz_catch(ctx) { fz_close(stm); - return fz_error_note(error, "cannot read raw stream (%d %d R)", num, gen); + fz_throw(ctx, "cannot read raw stream (%d %d R)", num, gen); } fz_close(stm); - return fz_okay; + return buf; } |