diff options
Diffstat (limited to 'libqpdf/QPDFJob_config.cc')
-rw-r--r-- | libqpdf/QPDFJob_config.cc | 209 |
1 files changed, 198 insertions, 11 deletions
diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index c4961fd3..2110f005 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -1,7 +1,13 @@ #include <qpdf/QPDFJob.hh> #include <qpdf/QUtil.hh> +#include <qpdf/QTC.hh> #include <cstring> +static void usage(std::string const& msg) +{ + throw QPDFJob::ConfigError(msg); +} + QPDFJob::Config& QPDFJob::Config::allowWeakCrypto() { @@ -497,6 +503,191 @@ QPDFJob::Config::withImages() return *this; } +QPDFJob::Config& +QPDFJob::Config::passwordFile(char const* parameter) +{ + std::list<std::string> lines; + if (strcmp(parameter, "-") == 0) + { + QTC::TC("qpdf", "QPDFJob_config password stdin"); + lines = QUtil::read_lines_from_file(std::cin); + } + else + { + QTC::TC("qpdf", "QPDFJob_config password file"); + lines = QUtil::read_lines_from_file(parameter); + } + if (lines.size() >= 1) + { + o.password = QUtil::make_shared_cstr(lines.front()); + + if (lines.size() > 1) + { + std::cerr << this->o.m->message_prefix + << ": WARNING: all but the first line of" + << " the password file are ignored" << std::endl; + } + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::passwordMode(char const* parameter) +{ + if (strcmp(parameter, "bytes") == 0) + { + o.password_mode = QPDFJob::pm_bytes; + } + else if (strcmp(parameter, "hex-bytes") == 0) + { + o.password_mode = QPDFJob::pm_hex_bytes; + } + else if (strcmp(parameter, "unicode") == 0) + { + o.password_mode = QPDFJob::pm_unicode; + } + else if (strcmp(parameter, "auto") == 0) + { + o.password_mode = QPDFJob::pm_auto; + } + else + { + usage("invalid password-mode option"); + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::streamData(char const* parameter) +{ + o.stream_data_set = true; + if (strcmp(parameter, "compress") == 0) + { + o.stream_data_mode = qpdf_s_compress; + } + else if (strcmp(parameter, "preserve") == 0) + { + o.stream_data_mode = qpdf_s_preserve; + } + else if (strcmp(parameter, "uncompress") == 0) + { + o.stream_data_mode = qpdf_s_uncompress; + } + else + { + // If this happens, it means streamDataChoices in + // ArgParser::initOptionTable is wrong. + usage("invalid stream-data option"); + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::decodeLevel(char const* parameter) +{ + o.decode_level_set = true; + if (strcmp(parameter, "none") == 0) + { + o.decode_level = qpdf_dl_none; + } + else if (strcmp(parameter, "generalized") == 0) + { + o.decode_level = qpdf_dl_generalized; + } + else if (strcmp(parameter, "specialized") == 0) + { + o.decode_level = qpdf_dl_specialized; + } + else if (strcmp(parameter, "all") == 0) + { + o.decode_level = qpdf_dl_all; + } + else + { + // If this happens, it means decodeLevelChoices in + // ArgParser::initOptionTable is wrong. + usage("invalid option"); + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::objectStreams(char const* parameter) +{ + o.object_stream_set = true; + if (strcmp(parameter, "disable") == 0) + { + o.object_stream_mode = qpdf_o_disable; + } + else if (strcmp(parameter, "preserve") == 0) + { + o.object_stream_mode = qpdf_o_preserve; + } + else if (strcmp(parameter, "generate") == 0) + { + o.object_stream_mode = qpdf_o_generate; + } + else + { + // If this happens, it means objectStreamsChoices in + // ArgParser::initOptionTable is wrong. + usage("invalid object stream mode"); + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::removeUnreferencedResources(char const* parameter) +{ + if (strcmp(parameter, "auto") == 0) + { + o.remove_unreferenced_page_resources = QPDFJob::re_auto; + } + else if (strcmp(parameter, "yes") == 0) + { + o.remove_unreferenced_page_resources = QPDFJob::re_yes; + } + else if (strcmp(parameter, "no") == 0) + { + o.remove_unreferenced_page_resources = QPDFJob::re_no; + } + else + { + // If this happens, it means remove_unref_choices in + // ArgParser::initOptionTable is wrong. + usage("invalid value for --remove-unreferenced-page-resources"); + } + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::showObject(char const* parameter) +{ + QPDFJob::parse_object_id(parameter, o.show_trailer, o.show_obj, o.show_gen); + o.require_outfile = false; + return *this; +} + +QPDFJob::Config& +QPDFJob::Config::jobJsonFile(char const* parameter) +{ + PointerHolder<char> file_buf; + size_t size; + QUtil::read_file_into_memory(parameter, file_buf, size); + try + { + o.initializeFromJson(std::string(file_buf.getPointer(), size)); + } + catch (std::exception& e) + { + throw std::runtime_error( + "error with job-json file " + std::string(parameter) + " " + + e.what() + "\nRun " + this->o.m->message_prefix + + "--job-json-help for information on the file format."); + } + return *this; +} + std::shared_ptr<QPDFJob::CopyAttConfig> QPDFJob::Config::copyAttachmentsFrom() { @@ -534,7 +725,7 @@ QPDFJob::CopyAttConfig::end() { if (this->caf.path.empty()) { - throw QPDFJob::ConfigError("copy attachments: no path specified"); + usage("copy attachments: no path specified"); } this->config.o.attachments_to_copy.push_back(this->caf); return this->config; @@ -577,8 +768,7 @@ QPDFJob::AttConfig::creationdate(char const* parameter) { if (! QUtil::pdf_time_to_qpdf_time(parameter)) { - throw QPDFJob::ConfigError( - std::string(parameter) + " is not a valid PDF timestamp"); + usage(std::string(parameter) + " is not a valid PDF timestamp"); } this->att.creationdate = parameter; return *this; @@ -589,8 +779,7 @@ QPDFJob::AttConfig::moddate(char const* parameter) { if (! QUtil::pdf_time_to_qpdf_time(parameter)) { - throw QPDFJob::ConfigError( - std::string(parameter) + " is not a valid PDF timestamp"); + usage(std::string(parameter) + " is not a valid PDF timestamp"); } this->att.moddate = parameter; return *this; @@ -601,8 +790,7 @@ QPDFJob::AttConfig::mimetype(char const* parameter) { if (strchr(parameter, '/') == nullptr) { - throw QPDFJob::ConfigError( - "mime type should be specified as type/subtype"); + usage("mime type should be specified as type/subtype"); } this->att.mimetype = parameter; return *this; @@ -629,13 +817,12 @@ QPDFJob::AttConfig::end() QUtil::get_current_qpdf_time()); if (this->att.path.empty()) { - throw QPDFJob::ConfigError("add attachment: no path specified"); + usage("add attachment: no path specified"); } std::string last_element = QUtil::path_basename(this->att.path); if (last_element.empty()) { - throw QPDFJob::ConfigError( - "path for --add-attachment may not be empty"); + usage("path for --add-attachment may not be empty"); } if (this->att.filename.empty()) { @@ -674,7 +861,7 @@ QPDFJob::PagesConfig::end() { if (this->config.o.page_specs.empty()) { - throw QPDFJob::ConfigError("--pages: no page specifications given"); + usage("--pages: no page specifications given"); } return this->config; } |