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>
|
2009-07-15 03:47:44 +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>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
2023-05-20 11:46:50 +00:00
|
|
|
static char const* whoami = nullptr;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
2019-08-23 23:54:08 +00:00
|
|
|
std::cerr << "Usage: " << whoami << " { -uncompress | -compress[=n] }" << std::endl
|
|
|
|
<< "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
|
2019-08-31 12:43:18 +00:00
|
|
|
<< "numbers are slower and more compressed" << std::endl;
|
2008-04-29 12:55:25 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
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];
|
2008-04-29 12:55:25 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
++whoami;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) {
|
2021-12-20 22:25:18 +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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ((strcmp(argv[1], "-uncompress") == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// okay
|
2008-04-29 12:55:25 +00:00
|
|
|
} else if ((strcmp(argv[1], "-compress") == 0)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
action = Pl_Flate::a_deflate;
|
2019-08-23 23:54:08 +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);
|
2008-04-29 12:55:25 +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;
|
|
|
|
std::cerr << whoami << ": WARNING: zlib code " << code << ", msg = " << msg << std::endl;
|
|
|
|
});
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
try {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned char buf[10000];
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
|
|
|
size_t len = fread(buf, 1, sizeof(buf), stdin);
|
|
|
|
if (len <= 0) {
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
flate->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flate->finish();
|
2008-04-29 12:55:25 +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
|
|
|
}
|
|
|
|
|
2021-11-02 21:54:10 +00:00
|
|
|
if (warn) {
|
|
|
|
exit(3);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|