From 796be86b078c59bed0d67c4322072317a9fafb06 Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Mon, 5 Sep 2016 15:37:42 +0100 Subject: mutool clean: Fixes seen as part of bug 697092 investigation. Firstly, we avoid compressing streams if they get bigger. Secondly, we ensure that we always update the Length field. Seen as part of the investigation into bug 697092, though not the actual cause. Thanks to Tor for the latter part of the fix. --- source/pdf/pdf-write.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c index 84e64450..2dead828 100644 --- a/source/pdf/pdf-write.c +++ b/source/pdf/pdf-write.c @@ -1637,11 +1637,18 @@ static void copystream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts if (do_deflate && !pdf_dict_get(ctx, obj, PDF_NAME_Filter)) { - pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); - tmp = deflatebuf(ctx, buf->data, buf->len); - fz_drop_buffer(ctx, buf); - buf = tmp; + if (tmp->len >= buf->len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); + fz_drop_buffer(ctx, buf); + buf = tmp; + } } if (opts->do_ascii && isbinarystream(buf)) @@ -1651,12 +1658,12 @@ static void copystream(fz_context *ctx, pdf_document *doc, pdf_write_state *opts buf = tmp; addhexfilter(ctx, doc, obj); - - newlen = pdf_new_int(ctx, doc, (int)buf->len); - pdf_dict_put(ctx, obj, PDF_NAME_Length, newlen); - pdf_drop_obj(ctx, newlen); } + newlen = pdf_new_int(ctx, doc, (int)buf->len); + pdf_dict_put(ctx, obj, PDF_NAME_Length, newlen); + pdf_drop_obj(ctx, newlen); + fz_printf(ctx, opts->out, "%d %d obj\n", num, gen); pdf_print_obj(ctx, opts->out, obj, opts->do_tight); fz_puts(ctx, opts->out, "\nstream\n"); @@ -1686,11 +1693,18 @@ static void expandstream(fz_context *ctx, pdf_document *doc, pdf_write_state *op if (do_deflate) { - pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); - tmp = deflatebuf(ctx, buf->data, buf->len); - fz_drop_buffer(ctx, buf); - buf = tmp; + if (tmp->len >= buf->len) + { + /* Don't bother compressing, as we gain nothing. */ + fz_drop_buffer(ctx, tmp); + } + else + { + pdf_dict_put(ctx, obj, PDF_NAME_Filter, PDF_NAME_FlateDecode); + fz_drop_buffer(ctx, buf); + buf = tmp; + } } if (opts->do_ascii && isbinarystream(buf)) -- cgit v1.2.3