2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/QPDFEFStreamObjectHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2021-02-08 23:07:21 +00:00
|
|
|
#include <qpdf/Pl_Count.hh>
|
|
|
|
#include <qpdf/Pl_Discard.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_MD5.hh>
|
|
|
|
#include <qpdf/QIntC.hh>
|
2022-09-26 19:11:27 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2021-02-08 23:07:21 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFEFStreamObjectHelper::QPDFEFStreamObjectHelper(QPDFObjectHandle oh) :
|
2021-02-08 23:07:21 +00:00
|
|
|
QPDFObjectHelper(oh),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFEFStreamObjectHelper::getParam(std::string const& pkey)
|
|
|
|
{
|
|
|
|
auto params = this->oh.getDict().getKey("/Params");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (params.isDictionary()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return params.getKey(pkey);
|
|
|
|
}
|
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFEFStreamObjectHelper::setParam(std::string const& pkey, QPDFObjectHandle const& pval)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
|
|
|
auto params = this->oh.getDict().getKey("/Params");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!params.isDictionary()) {
|
2023-05-21 17:35:09 +00:00
|
|
|
params =
|
|
|
|
this->oh.getDict().replaceKeyAndGetNew("/Params", QPDFObjectHandle::newDictionary());
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
params.replaceKey(pkey, pval);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFEFStreamObjectHelper::getCreationDate()
|
|
|
|
{
|
|
|
|
auto val = getParam("/CreationDate");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return val.getUTF8Value();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFEFStreamObjectHelper::getModDate()
|
|
|
|
{
|
|
|
|
auto val = getParam("/ModDate");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return val.getUTF8Value();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
QPDFEFStreamObjectHelper::getSize()
|
|
|
|
{
|
|
|
|
auto val = getParam("/Size");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.isInteger()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return QIntC::to_size(val.getUIntValueAsUInt());
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFEFStreamObjectHelper::getSubtype()
|
|
|
|
{
|
2021-09-09 22:05:48 +00:00
|
|
|
auto val = this->oh.getDict().getKey("/Subtype");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.isName()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
auto n = val.getName();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (n.length() > 1) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return n.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFEFStreamObjectHelper::getChecksum()
|
|
|
|
{
|
|
|
|
auto val = getParam("/CheckSum");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.isString()) {
|
2021-02-08 23:07:21 +00:00
|
|
|
return val.getStringValue();
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFEFStreamObjectHelper::createEFStream(QPDF& qpdf, std::shared_ptr<Buffer> data)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
2022-09-26 19:11:27 +00:00
|
|
|
return newFromStream(qpdf.newStream(data));
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFEFStreamObjectHelper::createEFStream(QPDF& qpdf, std::string const& data)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
2022-09-26 19:11:27 +00:00
|
|
|
return newFromStream(qpdf.newStream(data));
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper
|
2023-05-21 17:35:09 +00:00
|
|
|
QPDFEFStreamObjectHelper::createEFStream(QPDF& qpdf, std::function<void(Pipeline*)> provider)
|
2021-02-08 23:07:21 +00:00
|
|
|
{
|
2022-09-26 19:11:27 +00:00
|
|
|
auto stream = qpdf.newStream();
|
2023-05-21 17:35:09 +00:00
|
|
|
stream.replaceStreamData(provider, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
|
2021-02-08 23:07:21 +00:00
|
|
|
return newFromStream(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper&
|
|
|
|
QPDFEFStreamObjectHelper::setCreationDate(std::string const& date)
|
|
|
|
{
|
|
|
|
setParam("/CreationDate", QPDFObjectHandle::newString(date));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper&
|
|
|
|
QPDFEFStreamObjectHelper::setModDate(std::string const& date)
|
|
|
|
{
|
|
|
|
setParam("/ModDate", QPDFObjectHandle::newString(date));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper&
|
|
|
|
QPDFEFStreamObjectHelper::setSubtype(std::string const& subtype)
|
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
this->oh.getDict().replaceKey("/Subtype", QPDFObjectHandle::newName("/" + subtype));
|
2021-02-08 23:07:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFEFStreamObjectHelper
|
|
|
|
QPDFEFStreamObjectHelper::newFromStream(QPDFObjectHandle stream)
|
|
|
|
{
|
|
|
|
QPDFEFStreamObjectHelper result(stream);
|
2023-05-21 17:35:09 +00:00
|
|
|
stream.getDict().replaceKey("/Type", QPDFObjectHandle::newName("/EmbeddedFile"));
|
2021-02-08 23:07:21 +00:00
|
|
|
Pl_Discard discard;
|
2023-05-27 17:19:52 +00:00
|
|
|
// The PDF spec specifies use of MD5 here and notes that it is not to be used for security. MD5
|
|
|
|
// is known to be insecure.
|
2021-02-08 23:07:21 +00:00
|
|
|
Pl_MD5 md5("EF md5", &discard);
|
|
|
|
Pl_Count count("EF size", &md5);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!stream.pipeStreamData(&count, nullptr, 0, qpdf_dl_all)) {
|
2023-05-21 17:35:09 +00:00
|
|
|
stream.warnIfPossible("unable to get stream data for new embedded file stream");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
result.setParam("/Size", QPDFObjectHandle::newInteger(count.getCount()));
|
2021-02-08 23:07:21 +00:00
|
|
|
result.setParam(
|
2023-05-21 17:35:09 +00:00
|
|
|
"/CheckSum", QPDFObjectHandle::newString(QUtil::hex_decode(md5.getHexDigest())));
|
2021-02-08 23:07:21 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|