diff options
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDFJob_argv.cc | 177 | ||||
-rw-r--r-- | libqpdf/QPDFJob_config.cc | 209 | ||||
-rw-r--r-- | libqpdf/qpdf/auto_job_decl.hh | 8 | ||||
-rw-r--r-- | libqpdf/qpdf/auto_job_init.hh | 16 |
4 files changed, 206 insertions, 204 deletions
diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc index 063f77d8..a11eca9f 100644 --- a/libqpdf/QPDFJob_argv.cc +++ b/libqpdf/QPDFJob_argv.cc @@ -188,33 +188,6 @@ ArgParser::argShowCrypto() } void -ArgParser::argPasswordFile(char* parameter) -{ - std::list<std::string> lines; - if (strcmp(parameter, "-") == 0) - { - QTC::TC("qpdf", "qpdf password stdin"); - lines = QUtil::read_lines_from_file(std::cin); - } - else - { - QTC::TC("qpdf", "qpdf 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->ap.getProgname() - << ": WARNING: all but the first line of" - << " the password file are ignored" << std::endl; - } - } -} - -void ArgParser::argEncrypt() { this->accumulated_args.clear(); @@ -273,31 +246,6 @@ ArgParser::argEncPositional(char* arg) } void -ArgParser::argPasswordMode(char* 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"); - } -} - -void ArgParser::argEnc256AllowInsecure() { o.allow_insecure = true; @@ -454,112 +402,6 @@ ArgParser::argCopyAttachmentsFrom() } void -ArgParser::argStreamData(char* 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"); - } -} - -void -ArgParser::argDecodeLevel(char* 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"); - } -} - -void -ArgParser::argObjectStreams(char* 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"); - } -} - -void -ArgParser::argRemoveUnreferencedResources(char* 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"); - } -} - -void -ArgParser::argShowObject(char* parameter) -{ - QPDFJob::parse_object_id(parameter, o.show_trailer, o.show_obj, o.show_gen); - o.require_outfile = false; -} - -void ArgParser::argEnc40Print(char* parameter) { o.r2_print = (strcmp(parameter, "y") == 0); @@ -817,25 +659,6 @@ ArgParser::argEndCopyAttachment() } void -ArgParser::argJobJsonFile(char* 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->ap.getProgname() + - "--job-json-help for information on the file format."); - } -} - -void ArgParser::argJobJsonHelp() { std::cout << JOB_SCHEMA_DATA << std::endl; 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; } diff --git a/libqpdf/qpdf/auto_job_decl.hh b/libqpdf/qpdf/auto_job_decl.hh index 98e1d6e3..d65df1d9 100644 --- a/libqpdf/qpdf/auto_job_decl.hh +++ b/libqpdf/qpdf/auto_job_decl.hh @@ -24,15 +24,7 @@ void argEncrypt(); void argOverlay(); void argPages(); void argUnderlay(); -void argJobJsonFile(char *); -void argPasswordFile(char *); void argRotate(char *); -void argShowObject(char *); -void argDecodeLevel(char *); -void argObjectStreams(char *); -void argPasswordMode(char *); -void argRemoveUnreferencedResources(char *); -void argStreamData(char *); void argPagesPositional(char*); void argPagesPassword(char *); void argEndPages(); diff --git a/libqpdf/qpdf/auto_job_init.hh b/libqpdf/qpdf/auto_job_init.hh index d6bb63f3..bdf9cb0e 100644 --- a/libqpdf/qpdf/auto_job_init.hh +++ b/libqpdf/qpdf/auto_job_init.hh @@ -86,7 +86,7 @@ this->ap.addRequiredParameter("copy-encryption", [this](char *x){c_main->copyEnc this->ap.addRequiredParameter("encryption-file-password", [this](char *x){c_main->encryptionFilePassword(x);}, "password"); this->ap.addRequiredParameter("force-version", [this](char *x){c_main->forceVersion(x);}, "version"); this->ap.addRequiredParameter("ii-min-bytes", [this](char *x){c_main->iiMinBytes(x);}, "minimum"); -this->ap.addRequiredParameter("job-json-file", p(&ArgParser::argJobJsonFile), "file"); +this->ap.addRequiredParameter("job-json-file", [this](char *x){c_main->jobJsonFile(x);}, "file"); this->ap.addRequiredParameter("json-object", [this](char *x){c_main->jsonObject(x);}, "trailer"); this->ap.addRequiredParameter("keep-files-open-threshold", [this](char *x){c_main->keepFilesOpenThreshold(x);}, "count"); this->ap.addRequiredParameter("linearize-pass1", [this](char *x){c_main->linearizePass1(x);}, "filename"); @@ -95,21 +95,21 @@ this->ap.addRequiredParameter("oi-min-area", [this](char *x){c_main->oiMinArea(x this->ap.addRequiredParameter("oi-min-height", [this](char *x){c_main->oiMinHeight(x);}, "minimum"); this->ap.addRequiredParameter("oi-min-width", [this](char *x){c_main->oiMinWidth(x);}, "minimum"); this->ap.addRequiredParameter("password", [this](char *x){c_main->password(x);}, "password"); -this->ap.addRequiredParameter("password-file", p(&ArgParser::argPasswordFile), "password"); +this->ap.addRequiredParameter("password-file", [this](char *x){c_main->passwordFile(x);}, "password"); this->ap.addRequiredParameter("remove-attachment", [this](char *x){c_main->removeAttachment(x);}, "attachment"); this->ap.addRequiredParameter("rotate", p(&ArgParser::argRotate), "[+|-]angle"); this->ap.addRequiredParameter("show-attachment", [this](char *x){c_main->showAttachment(x);}, "attachment"); -this->ap.addRequiredParameter("show-object", p(&ArgParser::argShowObject), "trailer"); +this->ap.addRequiredParameter("show-object", [this](char *x){c_main->showObject(x);}, "trailer"); this->ap.addChoices("compress-streams", [this](char *x){c_main->compressStreams(x);}, true, yn_choices); -this->ap.addChoices("decode-level", p(&ArgParser::argDecodeLevel), true, decode_level_choices); +this->ap.addChoices("decode-level", [this](char *x){c_main->decodeLevel(x);}, true, decode_level_choices); this->ap.addChoices("flatten-annotations", [this](char *x){c_main->flattenAnnotations(x);}, true, flatten_choices); this->ap.addChoices("json-key", [this](char *x){c_main->jsonKey(x);}, true, json_key_choices); this->ap.addChoices("keep-files-open", [this](char *x){c_main->keepFilesOpen(x);}, true, yn_choices); this->ap.addChoices("normalize-content", [this](char *x){c_main->normalizeContent(x);}, true, yn_choices); -this->ap.addChoices("object-streams", p(&ArgParser::argObjectStreams), true, object_streams_choices); -this->ap.addChoices("password-mode", p(&ArgParser::argPasswordMode), true, password_mode_choices); -this->ap.addChoices("remove-unreferenced-resources", p(&ArgParser::argRemoveUnreferencedResources), true, remove_unref_choices); -this->ap.addChoices("stream-data", p(&ArgParser::argStreamData), true, stream_data_choices); +this->ap.addChoices("object-streams", [this](char *x){c_main->objectStreams(x);}, true, object_streams_choices); +this->ap.addChoices("password-mode", [this](char *x){c_main->passwordMode(x);}, true, password_mode_choices); +this->ap.addChoices("remove-unreferenced-resources", [this](char *x){c_main->removeUnreferencedResources(x);}, true, remove_unref_choices); +this->ap.addChoices("stream-data", [this](char *x){c_main->streamData(x);}, true, stream_data_choices); this->ap.registerOptionTable("pages", b(&ArgParser::argEndPages)); this->ap.addPositional(p(&ArgParser::argPagesPositional)); this->ap.addRequiredParameter("password", p(&ArgParser::argPagesPassword), "password"); |