2022-01-05 22:20:20 +00:00
|
|
|
#include <qpdf/QPDFJob.hh>
|
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2021-02-09 20:30:56 +00:00
|
|
|
|
2022-01-05 22:20:20 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2021-02-09 20:30:56 +00:00
|
|
|
|
2022-01-05 22:20:20 +00:00
|
|
|
static int constexpr EXIT_ERROR = 2;
|
|
|
|
static int constexpr EXIT_WARNING = 3;
|
2021-02-09 20:30:56 +00:00
|
|
|
|
2022-01-05 22:20:20 +00:00
|
|
|
// For is-encrypted and requires-password
|
|
|
|
static int constexpr EXIT_IS_NOT_ENCRYPTED = 2;
|
|
|
|
static int constexpr EXIT_CORRECT_PASSWORD = 3;
|
2021-02-09 20:30:56 +00:00
|
|
|
|
2022-01-05 22:20:20 +00:00
|
|
|
static char const* whoami = 0;
|
2021-02-09 20:30:56 +00:00
|
|
|
|
2022-01-05 22:20:20 +00:00
|
|
|
static void usageExit(std::string const& msg)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
std::cerr
|
|
|
|
<< std::endl
|
|
|
|
<< whoami << ": " << msg << std::endl
|
|
|
|
<< std::endl
|
|
|
|
<< "For detailed help, run " << whoami << " --help" << std::endl
|
|
|
|
<< std::endl;
|
2022-01-05 22:20:20 +00:00
|
|
|
exit(EXIT_ERROR);
|
2017-08-05 12:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
int realmain(int argc, char* argv[])
|
2017-08-05 12:24:05 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
QUtil::setLineBuf(stdout);
|
|
|
|
|
|
|
|
// Remove prefix added by libtool for consistency during testing.
|
|
|
|
if (strncmp(whoami, "lt-", 3) == 0)
|
2018-08-13 02:20:40 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
whoami += 3;
|
2018-08-13 02:20:40 +00:00
|
|
|
}
|
2017-08-05 12:24:05 +00:00
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
QPDFJob j;
|
|
|
|
|
|
|
|
bool errors = false;
|
2017-08-05 12:24:05 +00:00
|
|
|
try
|
|
|
|
{
|
2022-01-05 22:20:20 +00:00
|
|
|
j.initializeFromArgv(argc, argv);
|
2022-01-03 15:16:28 +00:00
|
|
|
j.run();
|
2017-08-05 12:24:05 +00:00
|
|
|
}
|
2022-01-05 22:20:20 +00:00
|
|
|
catch (QPDFArgParser::Usage& e)
|
|
|
|
{
|
|
|
|
usageExit(e.what());
|
|
|
|
}
|
2017-08-05 12:24:05 +00:00
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
std::cerr << whoami << ": " << e.what() << std::endl;
|
|
|
|
errors = true;
|
2017-08-05 12:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
bool warnings = j.hasWarnings();
|
|
|
|
if (warnings)
|
2021-02-09 20:30:56 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
if (! j.suppressWarnings())
|
2021-02-09 20:30:56 +00:00
|
|
|
{
|
2022-01-03 17:16:16 +00:00
|
|
|
if (j.createsOutput())
|
|
|
|
{
|
|
|
|
std::cerr << whoami << ": operation succeeded with warnings;"
|
|
|
|
<< " resulting file may have some problems"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << whoami << ": operation succeeded with warnings"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2018-12-22 15:52:06 +00:00
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
// Still return with warning code even if warnings were
|
2022-01-05 22:20:20 +00:00
|
|
|
// suppressed, so leave warnings == true unless we've been
|
|
|
|
// specifically instructed to do otherwise.
|
|
|
|
if (j.warningsExitZero())
|
|
|
|
{
|
|
|
|
warnings = false;
|
|
|
|
}
|
2018-12-22 15:52:06 +00:00
|
|
|
}
|
2020-04-04 21:59:53 +00:00
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
unsigned long encryption_status = j.getEncryptionStatus();
|
|
|
|
if (j.checkIsEncrypted())
|
2018-12-22 15:52:06 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
if (encryption_status & qpdf_es_encrypted)
|
2018-12-22 15:52:06 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
QTC::TC("qpdf", "qpdf check encrypted encrypted");
|
|
|
|
return 0;
|
2018-12-22 15:52:06 +00:00
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
else
|
2020-04-04 21:59:53 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
QTC::TC("qpdf", "qpdf check encrypted not encrypted");
|
|
|
|
return EXIT_IS_NOT_ENCRYPTED;
|
2020-04-04 21:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
else if (j.checkRequiresPassword())
|
2018-12-17 22:40:54 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
if (encryption_status & qpdf_es_encrypted)
|
2018-12-17 22:40:54 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
if (encryption_status & qpdf_es_password_incorrect)
|
2018-12-17 22:40:54 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
QTC::TC("qpdf", "qpdf check password password incorrect");
|
|
|
|
return 0;
|
2018-12-17 22:40:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
QTC::TC("qpdf", "qpdf check password password correct");
|
|
|
|
return EXIT_CORRECT_PASSWORD;
|
2018-12-17 22:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 15:16:28 +00:00
|
|
|
else
|
2018-12-17 22:40:54 +00:00
|
|
|
{
|
2022-01-03 15:16:28 +00:00
|
|
|
QTC::TC("qpdf", "qpdf check password not encrypted");
|
|
|
|
return EXIT_IS_NOT_ENCRYPTED;
|
2018-12-17 22:40:54 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 15:16:28 +00:00
|
|
|
return (errors ? EXIT_ERROR :
|
|
|
|
warnings ? EXIT_WARNING :
|
|
|
|
0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-01-14 02:29:12 +00:00
|
|
|
|
|
|
|
#ifdef WINDOWS_WMAIN
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
int wmain(int argc, wchar_t* argv[])
|
|
|
|
{
|
2020-01-14 16:38:48 +00:00
|
|
|
return QUtil::call_main_from_wmain(argc, argv, realmain);
|
2019-01-14 02:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
return realmain(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|