2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-01 03:12:29 +00:00
qpdf/libqpdf/QPDFEFStreamObjectHelper.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
3.8 KiB
C++
Raw Normal View History

2021-02-08 23:07:21 +00:00
#include <qpdf/QPDFEFStreamObjectHelper.hh>
2021-02-08 23:07:21 +00:00
#include <qpdf/Pl_Count.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/Pl_MD5.hh>
#include <qpdf/QIntC.hh>
2022-09-26 19:11:27 +00:00
#include <qpdf/QPDF.hh>
2021-02-08 23:07:21 +00:00
#include <qpdf/QUtil.hh>
QPDFEFStreamObjectHelper::QPDFEFStreamObjectHelper(QPDFObjectHandle oh) :
QPDFObjectHelper(oh),
m(new Members())
{
}
QPDFObjectHandle
QPDFEFStreamObjectHelper::getParam(std::string const& pkey)
{
auto params = this->oh.getDict().getKey("/Params");
if (params.isDictionary()) {
return params.getKey(pkey);
}
return QPDFObjectHandle::newNull();
}
void
QPDFEFStreamObjectHelper::setParam(std::string const& pkey, QPDFObjectHandle const& pval)
{
auto params = this->oh.getDict().getKey("/Params");
if (!params.isDictionary()) {
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");
if (val.isString()) {
return val.getUTF8Value();
}
return "";
}
std::string
QPDFEFStreamObjectHelper::getModDate()
{
auto val = getParam("/ModDate");
if (val.isString()) {
return val.getUTF8Value();
}
return "";
}
size_t
QPDFEFStreamObjectHelper::getSize()
{
auto val = getParam("/Size");
if (val.isInteger()) {
return QIntC::to_size(val.getUIntValueAsUInt());
}
return 0;
}
std::string
QPDFEFStreamObjectHelper::getSubtype()
{
auto val = this->oh.getDict().getKey("/Subtype");
2021-02-08 23:07:21 +00:00
if (val.isName()) {
auto n = val.getName();
if (n.length() > 1) {
return n.substr(1);
}
}
return "";
}
std::string
QPDFEFStreamObjectHelper::getChecksum()
{
auto val = getParam("/CheckSum");
if (val.isString()) {
return val.getStringValue();
}
return "";
}
QPDFEFStreamObjectHelper
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
QPDFEFStreamObjectHelper::createEFStream(QPDF& qpdf, std::string const& data)
{
2022-09-26 19:11:27 +00:00
return newFromStream(qpdf.newStream(data));
2021-02-08 23:07:21 +00:00
}
QPDFEFStreamObjectHelper
QPDFEFStreamObjectHelper::createEFStream(QPDF& qpdf, std::function<void(Pipeline*)> provider)
{
2022-09-26 19:11:27 +00:00
auto stream = qpdf.newStream();
2021-02-08 23:07:21 +00:00
stream.replaceStreamData(provider, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
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)
{
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);
stream.getDict().replaceKey("/Type", QPDFObjectHandle::newName("/EmbeddedFile"));
Pl_Discard discard;
// 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);
if (!stream.pipeStreamData(&count, nullptr, 0, qpdf_dl_all)) {
stream.warnIfPossible("unable to get stream data for new embedded file stream");
} else {
result.setParam("/Size", QPDFObjectHandle::newInteger(count.getCount()));
result.setParam(
"/CheckSum", QPDFObjectHandle::newString(QUtil::hex_decode(md5.getHexDigest())));
}
return result;
}