diff options
-rw-r--r-- | fitz/filt_basic.c | 15 | ||||
-rw-r--r-- | fitz/filt_flate.c | 32 | ||||
-rw-r--r-- | fitz/stm_buffer.c | 10 | ||||
-rw-r--r-- | fitz/stm_read.c | 40 | ||||
-rw-r--r-- | pdf/pdf_page.c | 55 | ||||
-rw-r--r-- | pdf/pdf_stream.c | 46 |
6 files changed, 131 insertions, 67 deletions
diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index 3d211645..7e5f2f21 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -39,13 +39,24 @@ fz_stream * fz_open_null(fz_stream *chain, int len) { struct null_filter *state; + fz_stream *stream; + fz_context *ctx = chain->ctx; assert(chain); - state = fz_malloc(chain->ctx, sizeof(struct null_filter)); + state = fz_malloc(ctx, sizeof(struct null_filter)); state->chain = chain; state->remain = len; - return fz_new_stream(chain->ctx, state, read_null, close_null); + 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; } /* ASCII Hex Decode */ diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index d7077bc8..9b3370c4 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -12,7 +12,7 @@ struct fz_flate_s static void *zalloc(void *opaque, unsigned int items, unsigned int size) { - return fz_malloc_array(opaque, items, size); + return fz_malloc_array_no_throw(opaque, items, size); } static void zfree(void *opaque, void *ptr) @@ -84,20 +84,34 @@ fz_stream * fz_open_flated(fz_stream *chain) { fz_flate *state; - int code; + int code = Z_OK; + fz_context *ctx = chain->ctx; + fz_stream *stream; + + fz_var(code); - state = fz_malloc(chain->ctx, sizeof(fz_flate)); + state = fz_malloc(ctx, sizeof(fz_flate)); state->chain = chain; state->z.zalloc = zalloc; state->z.zfree = zfree; - state->z.opaque = chain->ctx; + state->z.opaque = ctx; state->z.next_in = NULL; state->z.avail_in = 0; - code = inflateInit(&state->z); - if (code != Z_OK) - fz_warn(chain->ctx, "zlib error: inflateInit: %s", state->z.msg); - - return fz_new_stream(chain->ctx, state, read_flated, close_flated); + fz_try(ctx) + { + 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) + { + if (code == Z_OK) + inflateEnd(&state->z); + fz_free(ctx, state); + fz_rethrow(ctx); + } + return stream; } diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index 2722be1a..0f846987 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -9,7 +9,15 @@ fz_new_buffer(fz_context *ctx, int size) b = fz_malloc(ctx, sizeof(fz_buffer)); b->refs = 1; - b->data = fz_malloc(ctx, size); + fz_try(ctx) + { + b->data = fz_malloc(ctx, size); + } + fz_catch(ctx) + { + fz_free(ctx, b); + fz_rethrow(ctx); + } b->cap = size; b->len = 0; diff --git a/fitz/stm_read.c b/fitz/stm_read.c index 9f9fc302..3d76d51a 100644 --- a/fitz/stm_read.c +++ b/fitz/stm_read.c @@ -94,27 +94,37 @@ fz_read_all(fz_stream *stm, int initial) int n; fz_context *ctx = stm->ctx; - if (initial < 1024) - initial = 1024; + fz_var(buf); - buf = fz_new_buffer(ctx, initial); - - while (1) + fz_try(ctx) { - if (buf->len == buf->cap) - fz_grow_buffer(ctx, buf); + if (initial < 1024) + initial = 1024; + + buf = fz_new_buffer(ctx, initial); - if (buf->len / 200 > initial) + while (1) { - fz_drop_buffer(ctx, buf); - fz_throw(ctx, "compression bomb detected"); - } + if (buf->len == buf->cap) + fz_grow_buffer(ctx, buf); - n = fz_read(stm, buf->data + buf->len, buf->cap - buf->len); - if (n == 0) - break; + if (buf->len / 200 > initial) + { + fz_drop_buffer(ctx, buf); + fz_throw(ctx, "compression bomb detected"); + } + + n = fz_read(stm, buf->data + buf->len, buf->cap - buf->len); + if (n == 0) + break; - buf->len += n; + buf->len += n; + } + } + fz_catch(ctx) + { + fz_drop_buffer(ctx, buf); + fz_rethrow(ctx); } return buf; diff --git a/pdf/pdf_page.c b/pdf/pdf_page.c index 5a895445..d0bf57e7 100644 --- a/pdf/pdf_page.c +++ b/pdf/pdf_page.c @@ -26,10 +26,28 @@ pdf_find_page_number(pdf_xref *xref, fz_obj *page) } static void +put_marker_bool(fz_context *ctx, fz_obj *rdb, char *marker, int val) +{ + fz_obj *tmp; + + tmp = fz_new_bool(ctx, val); + fz_try(ctx) + { + fz_dict_puts(rdb, marker, tmp); + } + fz_catch(ctx) + { + fz_drop_obj(tmp); + fz_rethrow(ctx); + } + fz_drop_obj(tmp); +} + +static void pdf_load_page_tree_node(pdf_xref *xref, fz_obj *node, struct info info) { fz_obj *dict, *kids, *count; - fz_obj *obj, *tmp; + fz_obj *obj; int i, n; fz_context *ctx = xref->ctx; @@ -55,17 +73,7 @@ pdf_load_page_tree_node(pdf_xref *xref, fz_obj *node, struct info info) if (obj) info.rotate = obj; - tmp = fz_new_null(ctx); - fz_try(ctx) - { - fz_dict_puts(node, ".seen", tmp); - } - fz_catch(ctx) - { - fz_drop_obj(tmp); - fz_rethrow(ctx); - } - fz_drop_obj(tmp); + put_marker_bool(ctx, node, ".seen", 0); n = fz_array_len(kids); for (i = 0; i < n; i++) @@ -165,7 +173,6 @@ static int pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb) { fz_obj *dict; - fz_obj *tmp; int i, n; if (!rdb) @@ -175,9 +182,7 @@ pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb) if (fz_dict_gets(rdb, ".useBM")) return fz_to_bool(fz_dict_gets(rdb, ".useBM")); - tmp = fz_new_bool(ctx, 0); - fz_dict_puts(rdb, ".useBM", tmp); - fz_drop_obj(tmp); + put_marker_bool(ctx, rdb, ".useBM", 0); dict = fz_dict_gets(rdb, "ExtGState"); n = fz_dict_len(dict); @@ -200,9 +205,7 @@ pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb) return 0; found: - tmp = fz_new_bool(ctx, 1); - fz_dict_puts(rdb, ".useBM", tmp); - fz_drop_obj(tmp); + put_marker_bool(ctx, rdb, ".useBM", 1); return 1; } @@ -335,6 +338,13 @@ pdf_load_page(pdf_xref *xref, int number) fz_try(ctx) { page->contents = pdf_load_page_contents(xref, obj); + + if (pdf_resources_use_blending(ctx, page->resources)) + page->transparency = 1; + + for (annot = page->annots; annot && !page->transparency; annot = annot->next) + if (pdf_resources_use_blending(ctx, annot->ap->resources)) + page->transparency = 1; } fz_catch(ctx) { @@ -342,13 +352,6 @@ pdf_load_page(pdf_xref *xref, int number) fz_throw(ctx, "cannot load page %d contents (%d 0 R)", number + 1, fz_to_num(pageref)); } - if (pdf_resources_use_blending(ctx, page->resources)) - page->transparency = 1; - - for (annot = page->annots; annot && !page->transparency; annot = annot->next) - if (pdf_resources_use_blending(ctx, annot->ap->resources)) - page->transparency = 1; - return page; } diff --git a/pdf/pdf_stream.c b/pdf/pdf_stream.c index 0a38d20f..625fbc76 100644 --- a/pdf/pdf_stream.c +++ b/pdf/pdf_stream.c @@ -155,21 +155,30 @@ build_filter_chain(fz_stream *chain, pdf_xref *xref, fz_obj *fs, fz_obj *ps, int * stream length, followed by a decryption filter. */ static fz_stream * -pdf_open_raw_filter(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int num, int gen) +pdf_open_raw_filter(fz_stream *orig, pdf_xref *xref, fz_obj *stmobj, int num, int gen) { int hascrypt; int len; - fz_context *ctx = chain->ctx; - - /* don't close chain when we close this filter */ - fz_keep_stream(chain); + fz_context *ctx = orig->ctx; + fz_stream *chain; len = fz_to_int(fz_dict_gets(stmobj, "Length")); - chain = fz_open_null(chain, len); + chain = fz_open_null(orig, len); + + fz_try(ctx) + { + hascrypt = pdf_stream_has_crypt(ctx, stmobj); + if (xref->crypt && !hascrypt) + chain = pdf_open_crypt(chain, xref->crypt, num, gen); + } + fz_catch(ctx) + { + fz_close(chain); + fz_rethrow(ctx); + } - hascrypt = pdf_stream_has_crypt(ctx, stmobj); - if (xref->crypt && !hascrypt) - chain = pdf_open_crypt(chain, xref->crypt, num, gen); + /* don't close the original stream when we close this filter */ + fz_keep_stream(orig); return chain; } @@ -183,16 +192,25 @@ pdf_open_filter(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int num, int g { fz_obj *filters; fz_obj *params; + fz_context *ctx = chain->ctx; filters = fz_dict_getsa(stmobj, "Filter", "F"); params = fz_dict_getsa(stmobj, "DecodeParms", "DP"); chain = pdf_open_raw_filter(chain, xref, stmobj, num, gen); - if (fz_is_name(filters)) - return build_filter(chain, xref, filters, params, num, gen); - if (fz_array_len(filters) > 0) - return build_filter_chain(chain, xref, filters, params, num, gen); + fz_try(ctx) + { + if (fz_is_name(filters)) + chain = build_filter(chain, xref, filters, params, num, gen); + else if (fz_array_len(filters) > 0) + chain = build_filter_chain(chain, xref, filters, params, num, gen); + } + fz_catch(ctx) + { + fz_close(chain); + fz_rethrow(ctx); + } return chain; } @@ -340,7 +358,7 @@ fz_buffer * pdf_load_stream(pdf_xref *xref, int num, int gen) { fz_context *ctx = xref->ctx; - fz_stream *stm; + fz_stream *stm = NULL; fz_obj *dict, *obj; int i, len, n; fz_buffer *buf; |