From 37779f92eda4a7adf4b0f0212f89680beca77c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20B=C3=BCnzli?= Date: Tue, 28 Oct 2014 15:04:01 +0100 Subject: more liberally accept page trees with unexpected content pdf_lookup_page_loc_imp currently throws if any object in the page tree is neither a /Pages node nor a /Page leaf. This unnecessarily rejects slightly broken documents such as the ones from https://code.google.com/p/sumatrapdf/issues/detail?id=2582 and https://code.google.com/p/sumatrapdf/issues/detail?id=2608 . pdf_count_pages_before_kid currently wrongly throws if a /Pages node doesn't contain any kids and correctly states so (which even seems to be permitted by the PDF specification). --- source/pdf/pdf-page.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'source/pdf/pdf-page.c') diff --git a/source/pdf/pdf-page.c b/source/pdf/pdf-page.c index a6a5699a..6543b7dc 100644 --- a/source/pdf/pdf-page.c +++ b/source/pdf/pdf-page.c @@ -64,37 +64,35 @@ pdf_lookup_page_loc_imp(pdf_document *doc, pdf_obj *node, int *skip, pdf_obj **p { pdf_obj *kid = pdf_array_get(kids, i); char *type = pdf_to_name(pdf_dict_gets(kid, "Type")); - if (!strcmp(type, "Page") || (!*type && pdf_dict_gets(kid, "MediaBox"))) + if (*type ? !strcmp(type, "Pages") : pdf_dict_gets(kid, "Kids") && !pdf_dict_gets(kid, "MediaBox")) { - if (*skip == 0) + int count = pdf_to_int(pdf_dict_gets(kid, "Count")); + if (*skip < count) { - if (parentp) *parentp = node; - if (indexp) *indexp = i; - hit = kid; + node = kid; break; } else { - (*skip)--; + *skip -= count; } } - else if (!strcmp(type, "Pages") || (!*type && pdf_dict_gets(kid, "Kids"))) + else { - int count = pdf_to_int(pdf_dict_gets(kid, "Count")); - if (*skip < count) + if (*type ? strcmp(type, "Page") != 0 : !pdf_dict_gets(kid, "MediaBox")) + fz_warn(ctx, "non-page object in page tree (%s)", type); + if (*skip == 0) { - node = kid; + if (parentp) *parentp = node; + if (indexp) *indexp = i; + hit = kid; break; } else { - *skip -= count; + (*skip)--; } } - else - { - fz_throw(ctx, FZ_ERROR_GENERIC, "non-page object in page tree (%s)", type); - } } } while (hit == NULL); @@ -151,7 +149,7 @@ pdf_count_pages_before_kid(pdf_document *doc, pdf_obj *parent, int kid_num) { pdf_obj *count = pdf_dict_gets(kid, "Count"); int n = pdf_to_int(count); - if (count == NULL || n <= 0) + if (!pdf_is_int(count) || n < 0) fz_throw(doc->ctx, FZ_ERROR_GENERIC, "illegal or missing count in pages tree"); total += n; } -- cgit v1.2.3