From bb89382f936fa373eb7ec6708d83e5713cada644 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 21 Dec 2018 14:30:22 -0500 Subject: [PATCH] Allow --show-object=trailer --- ChangeLog | 2 ++ qpdf/qpdf.cc | 38 +++++++++++++++++++++++--------- qpdf/qtest/qpdf.test | 8 ++++++- qpdf/qtest/qpdf/show-trailer.out | 1 + 4 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 qpdf/qtest/qpdf/show-trailer.out diff --git a/ChangeLog b/ChangeLog index 7818c98a..40ecd446 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2018-12-21 Jay Berkenbilt + * Allow --show-object=trailer for showing the document trailer. + * You can now use eval $(qpdf --completion-bash) to enable bash completion for qpdf. It's not perfect, but it works pretty well. diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index b1028e0e..c86cb2a4 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -112,6 +112,7 @@ struct Options check_linearization(false), show_linearization(false), show_xref(false), + show_trailer(false), show_obj(0), show_gen(0), show_raw_stream_data(false), @@ -185,6 +186,7 @@ struct Options bool check_linearization; bool show_linearization; bool show_xref; + bool show_trailer; int show_obj; int show_gen; bool show_raw_stream_data; @@ -511,7 +513,7 @@ ArgParser::initOptionTable() (*t)["show-linearization"] = oe_bare(&ArgParser::argShowLinearization); (*t)["show-xref"] = oe_bare(&ArgParser::argShowXref); (*t)["show-object"] = oe_requiredParameter( - &ArgParser::argShowObject, "obj[,gen]"); + &ArgParser::argShowObject, "trailer|obj[,gen]"); (*t)["raw-stream-data"] = oe_bare(&ArgParser::argRawStreamData); (*t)["filtered-stream-data"] = oe_bare(&ArgParser::argFilteredStreamData); (*t)["show-npages"] = oe_bare(&ArgParser::argShowNpages); @@ -929,14 +931,21 @@ ArgParser::argShowXref() void ArgParser::argShowObject(char* parameter) { - char* obj = parameter; - char* gen = obj; - if ((gen = strchr(obj, ',')) != 0) + if (strcmp(parameter, "trailer") == 0) { - *gen++ = 0; - o.show_gen = QUtil::string_to_int(gen); + o.show_trailer = true; + } + else + { + char* obj = parameter; + char* gen = obj; + if ((gen = strchr(obj, ',')) != 0) + { + *gen++ = 0; + o.show_gen = QUtil::string_to_int(gen); + } + o.show_obj = QUtil::string_to_int(obj); } - o.show_obj = QUtil::string_to_int(obj); o.require_outfile = false; } @@ -1453,7 +1462,8 @@ automated test suites for software that uses the qpdf library.\n\ --check-linearization check file integrity and linearization status\n\ --show-linearization check and show all linearization data\n\ --show-xref show the contents of the cross-reference table\n\ ---show-object=obj[,gen] show the contents of the given object\n\ +--show-object=trailer|obj[,gen]\n\ + show the contents of the given object\n\ --raw-stream-data show raw stream data instead of object contents\n\ --filtered-stream-data show filtered stream data instead of object contents\n\ --show-npages print the number of pages in the file\n\ @@ -2374,7 +2384,15 @@ static void do_check(QPDF& pdf, Options& o, int& exit_code) static void do_show_obj(QPDF& pdf, Options& o, int& exit_code) { - QPDFObjectHandle obj = pdf.getObjectByID(o.show_obj, o.show_gen); + QPDFObjectHandle obj; + if (o.show_trailer) + { + obj = pdf.getTrailer(); + } + else + { + obj = pdf.getObjectByID(o.show_obj, o.show_gen); + } if (obj.isStream()) { if (o.show_raw_stream_data || o.show_filtered_stream_data) @@ -2676,7 +2694,7 @@ static void do_inspection(QPDF& pdf, Options& o) { pdf.showXRefTable(); } - if (o.show_obj > 0) + if ((o.show_obj > 0) || o.show_trailer) { do_show_obj(pdf, o, exit_code); } diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test index 8a5d2099..8c81efc8 100644 --- a/qpdf/qtest/qpdf.test +++ b/qpdf/qtest/qpdf.test @@ -2048,7 +2048,7 @@ $td->runtest("stream detected", show_ntests(); # ---------- $td->notify("--- Extraction Tests ---"); -$n_tests += 11; +$n_tests += 12; $td->runtest("show xref", {$td->COMMAND => "qpdf encrypted-with-images.pdf" . @@ -2122,6 +2122,12 @@ $td->runtest("show-xref-by-id-filtered", {$td->FILE => "show-xref-by-id-filtered.out", $td->EXIT_STATUS => 0}); +$td->runtest("show trailer", + {$td->COMMAND => "qpdf minimal.pdf --show-object=trailer"}, + {$td->FILE => "show-trailer.out", + $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); + show_ntests(); # ---------- $td->notify("--- Clear-text Metadata Tests ---"); diff --git a/qpdf/qtest/qpdf/show-trailer.out b/qpdf/qtest/qpdf/show-trailer.out new file mode 100644 index 00000000..3ec34467 --- /dev/null +++ b/qpdf/qtest/qpdf/show-trailer.out @@ -0,0 +1 @@ +<< /Root 1 0 R /Size 7 >>