2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Flate.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2021-12-20 22:25:18 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <iostream>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:46:50 +00:00
|
|
|
static char const* whoami = nullptr;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
usage()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
std::cerr << "Usage: " << whoami << " { -uncompress | -compress[=n] }" << std::endl
|
2023-12-23 02:45:10 +00:00
|
|
|
<< "If n is specified with -compress, it is a zlib compression level from"
|
|
|
|
<< std::endl
|
|
|
|
<< "1 to 9 where lower numbers are faster and less compressed and higher" << std::endl
|
2022-04-02 21:14:10 +00:00
|
|
|
<< "numbers are slower and more compressed" << std::endl;
|
2008-04-29 12:55:25 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2023-05-20 11:46:50 +00:00
|
|
|
if ((whoami = strrchr(argv[0], '/')) == nullptr) {
|
2022-02-08 14:18:08 +00:00
|
|
|
whoami = argv[0];
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
++whoami;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
std::cout << whoami << " from qpdf version " << QPDF::QPDFVersion() << std::endl;
|
2022-02-08 14:18:08 +00:00
|
|
|
exit(0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
usage();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pl_Flate::action_e action = Pl_Flate::a_inflate;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((strcmp(argv[1], "-uncompress") == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// okay
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((strcmp(argv[1], "-compress") == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
action = Pl_Flate::a_deflate;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((strncmp(argv[1], "-compress=", 10) == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
action = Pl_Flate::a_deflate;
|
2019-08-23 23:54:08 +00:00
|
|
|
int level = QUtil::string_to_int(argv[1] + 10);
|
|
|
|
Pl_Flate::setCompressionLevel(level);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
usage();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 03:47:44 +00:00
|
|
|
QUtil::binary_stdout();
|
2009-07-15 04:26:32 +00:00
|
|
|
QUtil::binary_stdin();
|
2022-04-09 18:49:10 +00:00
|
|
|
auto out = std::make_shared<Pl_StdioFile>("stdout", stdout);
|
|
|
|
auto flate = std::make_shared<Pl_Flate>("flate", out.get(), action);
|
2021-11-02 21:54:10 +00:00
|
|
|
bool warn = false;
|
|
|
|
flate->setWarnCallback([&warn](char const* msg, int code) {
|
|
|
|
warn = true;
|
2023-05-21 17:35:09 +00:00
|
|
|
std::cerr << whoami << ": WARNING: zlib code " << code << ", msg = " << msg << std::endl;
|
2021-11-02 21:54:10 +00:00
|
|
|
});
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned char buf[10000];
|
|
|
|
bool done = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!done) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), stdin);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len <= 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
done = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
flate->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flate->finish();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << whoami << ": " << e.what() << std::endl;
|
|
|
|
exit(2);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (warn) {
|
2021-11-02 21:54:10 +00:00
|
|
|
exit(3);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|