aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFJob_argv.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2023-12-23 00:14:11 +0100
committerJay Berkenbilt <ejb@ql.org>2023-12-23 02:10:18 +0100
commit7d7e2234a537b6cd2205fb5cf942d5a9e8a866e3 (patch)
treefc4553803a5510c0e2793cc57d055dd5459e2ff6 /libqpdf/QPDFJob_argv.cc
parent1173a0bdfc56a08eedafc06afcd37f0b35ac3ea2 (diff)
downloadqpdf-7d7e2234a537b6cd2205fb5cf942d5a9e8a866e3.tar.zst
Implement new --encrypt args and completion (fixes #784)
Positional arguments are supported in a backward-compatible way, but completion no longer guides users to it.
Diffstat (limited to 'libqpdf/QPDFJob_argv.cc')
-rw-r--r--libqpdf/QPDFJob_argv.cc74
1 files changed, 40 insertions, 34 deletions
diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc
index adf7ba64..56acd7a9 100644
--- a/libqpdf/QPDFJob_argv.cc
+++ b/libqpdf/QPDFJob_argv.cc
@@ -35,6 +35,9 @@ namespace
std::shared_ptr<QPDFJob::EncConfig> c_enc;
std::vector<std::string> accumulated_args;
std::shared_ptr<char> pages_password{nullptr};
+ std::string user_password;
+ std::string owner_password;
+ bool used_enc_password_args{false};
bool gave_input{false};
bool gave_output{false};
};
@@ -161,64 +164,67 @@ void
ArgParser::argEncrypt()
{
this->accumulated_args.clear();
- if (this->ap.isCompleting() && this->ap.argsLeft() == 0) {
- this->ap.insertCompletion("user-password");
- }
this->ap.selectOptionTable(O_ENCRYPTION);
}
void
ArgParser::argEncPositional(std::string const& arg)
{
+ if (used_enc_password_args) {
+ usage("positional and dashed encryption arguments may not be mixed");
+ }
+
this->accumulated_args.push_back(arg);
- size_t n_args = this->accumulated_args.size();
- if (n_args < 3) {
- if (this->ap.isCompleting() && (this->ap.argsLeft() == 0)) {
- if (n_args == 1) {
- this->ap.insertCompletion("owner-password");
- } else if (n_args == 2) {
- this->ap.insertCompletion("40");
- this->ap.insertCompletion("128");
- this->ap.insertCompletion("256");
- }
- }
+ if (this->accumulated_args.size() < 3) {
return;
}
- std::string user_password = this->accumulated_args.at(0);
- std::string owner_password = this->accumulated_args.at(1);
- std::string len_str = this->accumulated_args.at(2);
- int keylen = 0;
- if (len_str == "40") {
- keylen = 40;
- this->ap.selectOptionTable(O_40_BIT_ENCRYPTION);
- } else if (len_str == "128") {
- keylen = 128;
- this->ap.selectOptionTable(O_128_BIT_ENCRYPTION);
- } else if (len_str == "256") {
- keylen = 256;
- this->ap.selectOptionTable(O_256_BIT_ENCRYPTION);
- } else {
- usage("encryption key length must be 40, 128, or 256");
- }
- this->c_enc = c_main->encrypt(keylen, user_password, owner_password);
+ user_password = this->accumulated_args.at(0);
+ owner_password = this->accumulated_args.at(1);
+ auto len_str = this->accumulated_args.at(2);
+ this->accumulated_args.clear();
+ argEncBits(len_str);
}
void
ArgParser::argEncUserPassword(std::string const& arg)
{
- // QXXXQ
+ if (!accumulated_args.empty()) {
+ usage("positional and dashed encryption arguments may not be mixed");
+ }
+ this->used_enc_password_args = true;
+ this->user_password = arg;
}
void
ArgParser::argEncOwnerPassword(std::string const& arg)
{
- // QXXXQ
+ if (!accumulated_args.empty()) {
+ usage("positional and dashed encryption arguments may not be mixed");
+ }
+ this->used_enc_password_args = true;
+ this->owner_password = arg;
}
void
ArgParser::argEncBits(std::string const& arg)
{
- // QXXXQ
+ if (!accumulated_args.empty()) {
+ usage("positional and dashed encryption arguments may not be mixed");
+ }
+ int keylen = 0;
+ if (arg == "40") {
+ keylen = 40;
+ this->ap.selectOptionTable(O_40_BIT_ENCRYPTION);
+ } else if (arg == "128") {
+ keylen = 128;
+ this->ap.selectOptionTable(O_128_BIT_ENCRYPTION);
+ } else if (arg == "256") {
+ keylen = 256;
+ this->ap.selectOptionTable(O_256_BIT_ENCRYPTION);
+ } else {
+ usage("encryption key length must be 40, 128, or 256");
+ }
+ this->c_enc = c_main->encrypt(keylen, user_password, owner_password);
}
void