2020-01-18 11:38:54 +01:00
|
|
|
// Copyright (C) 2019-2020 Jakub Melka
|
2019-11-17 17:41:07 +01:00
|
|
|
//
|
|
|
|
// This file is part of PdfForQt.
|
|
|
|
//
|
|
|
|
// PdfForQt is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// PdfForQt is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "pdffile.h"
|
|
|
|
#include "pdfdocument.h"
|
|
|
|
#include "pdfencoding.h"
|
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
QString PDFFileSpecification::getPlatformFileName() const
|
|
|
|
{
|
|
|
|
// UF has maximal precedence, because it is unicode string
|
|
|
|
if (!m_UF.isEmpty())
|
|
|
|
{
|
|
|
|
return m_UF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_F.isEmpty())
|
|
|
|
{
|
|
|
|
return QString::fromLatin1(m_F);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
for (const QByteArray& platformName : { m_DOS, m_Mac, m_Unix })
|
|
|
|
{
|
|
|
|
if (!platformName.isEmpty())
|
|
|
|
{
|
|
|
|
return QString::fromLatin1(platformName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
for (const QByteArray& platformName : { m_Unix, m_Mac, m_DOS })
|
|
|
|
{
|
|
|
|
if (!platformName.isEmpty())
|
|
|
|
{
|
|
|
|
return QString::fromLatin1(platformName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
for (const QByteArray& platformName : { m_Mac, m_Unix, m_DOS })
|
|
|
|
{
|
|
|
|
if (!platformName.isEmpty())
|
|
|
|
{
|
|
|
|
return QString::fromLatin1(platformName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const PDFEmbeddedFile* PDFFileSpecification::getPlatformFile() const
|
|
|
|
{
|
|
|
|
if (m_embeddedFiles.count("UF"))
|
|
|
|
{
|
|
|
|
return &m_embeddedFiles.at("UF");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_embeddedFiles.count("F"))
|
|
|
|
{
|
|
|
|
return &m_embeddedFiles.at("F");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (m_embeddedFiles.count("DOS"))
|
|
|
|
{
|
|
|
|
return &m_embeddedFiles.at("DOS");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
if (m_embeddedFiles.count("Unix"))
|
|
|
|
{
|
|
|
|
return &m_embeddedFiles.at("Unix");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
if (m_embeddedFiles.count("Mac"))
|
|
|
|
{
|
|
|
|
return &m_embeddedFiles.at("Mac");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-29 18:53:04 +02:00
|
|
|
PDFFileSpecification PDFFileSpecification::parse(const PDFObjectStorage* storage, PDFObject object)
|
2019-11-17 17:41:07 +01:00
|
|
|
{
|
|
|
|
PDFFileSpecification result;
|
2020-08-07 19:29:22 +02:00
|
|
|
|
|
|
|
if (object.isReference())
|
|
|
|
{
|
|
|
|
result.m_selfReference = object.getReference();
|
|
|
|
}
|
|
|
|
|
2020-03-29 18:53:04 +02:00
|
|
|
object = storage->getObject(object);
|
2019-11-17 17:41:07 +01:00
|
|
|
|
|
|
|
if (object.isString())
|
|
|
|
{
|
|
|
|
result.m_UF = PDFEncoding::convertTextString(object.getString());
|
|
|
|
}
|
|
|
|
else if (object.isDictionary())
|
|
|
|
{
|
2020-03-29 18:53:04 +02:00
|
|
|
PDFDocumentDataLoaderDecorator loader(storage);
|
2019-11-17 17:41:07 +01:00
|
|
|
const PDFDictionary* dictionary = object.getDictionary();
|
|
|
|
|
|
|
|
result.m_fileSystem = loader.readNameFromDictionary(dictionary, "FS");
|
|
|
|
result.m_F = loader.readStringFromDictionary(dictionary, "F");
|
|
|
|
result.m_UF = loader.readTextStringFromDictionary(dictionary, "UF", QString());
|
|
|
|
result.m_DOS = loader.readStringFromDictionary(dictionary, "DOS");
|
|
|
|
result.m_Mac = loader.readStringFromDictionary(dictionary, "Mac");
|
|
|
|
result.m_Unix = loader.readStringFromDictionary(dictionary, "Unix");
|
2020-08-02 17:28:08 +02:00
|
|
|
result.m_id = PDFFileIdentifier::parse(storage, dictionary->get("ID"));
|
2019-11-17 17:41:07 +01:00
|
|
|
result.m_volatile = loader.readBooleanFromDictionary(dictionary, "V", false);
|
|
|
|
result.m_description = loader.readTextStringFromDictionary(dictionary, "Desc", QString());
|
2020-08-07 19:29:22 +02:00
|
|
|
result.m_collection = loader.readReferenceFromDictionary(dictionary, "CI");
|
|
|
|
result.m_thumbnailReference = loader.readReferenceFromDictionary(dictionary, "Thumb");
|
|
|
|
result.m_encryptedPayload = dictionary->get("EP");
|
|
|
|
|
|
|
|
constexpr const std::array relationships = {
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Unspecified", AssociatedFileRelationship::Unspecified },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Source", AssociatedFileRelationship::Source },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Data", AssociatedFileRelationship::Data },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Alternative", AssociatedFileRelationship::Alternative },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Supplement", AssociatedFileRelationship::Supplement },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "EncryptedPayload", AssociatedFileRelationship::EncryptedPayload },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "FormData", AssociatedFileRelationship::FormData },
|
|
|
|
std::pair<const char*, AssociatedFileRelationship>{ "Schema", AssociatedFileRelationship::Schema },
|
|
|
|
};
|
|
|
|
|
|
|
|
result.m_associatedFileRelationship = loader.readEnumByName(dictionary->get("AFRelationship"), relationships.begin(), relationships.end(), AssociatedFileRelationship::Unspecified);
|
2019-11-17 17:41:07 +01:00
|
|
|
|
2020-03-29 18:53:04 +02:00
|
|
|
PDFObject embeddedFiles = storage->getObject(dictionary->get("EF"));
|
2020-08-07 19:29:22 +02:00
|
|
|
PDFObject relatedFiles = storage->getObject(dictionary->get("RF"));
|
2019-11-17 17:41:07 +01:00
|
|
|
if (embeddedFiles.isDictionary())
|
|
|
|
{
|
|
|
|
const PDFDictionary* embeddedFilesDictionary = embeddedFiles.getDictionary();
|
2020-08-07 19:29:22 +02:00
|
|
|
const PDFDictionary* relatedFilesDictionary = relatedFiles.isDictionary() ? relatedFiles.getDictionary() : nullptr;
|
2019-11-17 17:41:07 +01:00
|
|
|
for (size_t i = 0; i < embeddedFilesDictionary->getCount(); ++i)
|
|
|
|
{
|
2020-08-07 19:29:22 +02:00
|
|
|
QByteArray key = embeddedFilesDictionary->getKey(i).getString();
|
|
|
|
result.m_embeddedFiles[key] = PDFEmbeddedFile::parse(storage, embeddedFilesDictionary->getValue(i));
|
|
|
|
|
|
|
|
if (relatedFilesDictionary)
|
|
|
|
{
|
|
|
|
PDFObject relatedFileArrayObject = storage->getObject(relatedFilesDictionary->get(key));
|
|
|
|
if (relatedFileArrayObject.isArray())
|
|
|
|
{
|
|
|
|
const PDFArray* relatedFileArray = relatedFileArrayObject.getArray();
|
|
|
|
const size_t relatedFilesCount = relatedFileArray->getCount() / 2;
|
|
|
|
|
|
|
|
RelatedFiles& relatedFiles = result.m_relatedFiles[key];
|
|
|
|
relatedFiles.reserve(relatedFilesCount);
|
|
|
|
for (size_t i = 0; i < relatedFilesCount; ++i)
|
|
|
|
{
|
|
|
|
RelatedFile relatedFile;
|
|
|
|
relatedFile.name = loader.readString(relatedFileArray->getItem(2 * i));
|
|
|
|
relatedFile.fileReference = loader.readReference(relatedFileArray->getItem(2 * i + 1));
|
|
|
|
relatedFiles.emplace_back(qMove(relatedFile));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 17:41:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-29 18:53:04 +02:00
|
|
|
PDFEmbeddedFile PDFEmbeddedFile::parse(const PDFObjectStorage* storage, PDFObject object)
|
2019-11-17 17:41:07 +01:00
|
|
|
{
|
|
|
|
PDFEmbeddedFile result;
|
2020-03-29 18:53:04 +02:00
|
|
|
object = storage->getObject(object);
|
2019-11-17 17:41:07 +01:00
|
|
|
|
|
|
|
if (object.isStream())
|
|
|
|
{
|
|
|
|
const PDFStream* stream = object.getStream();
|
|
|
|
const PDFDictionary* dictionary = stream->getDictionary();
|
2020-03-29 18:53:04 +02:00
|
|
|
PDFDocumentDataLoaderDecorator loader(storage);
|
2019-11-17 17:41:07 +01:00
|
|
|
result.m_stream = object;
|
|
|
|
result.m_subtype = loader.readNameFromDictionary(dictionary, "Subtype");
|
|
|
|
|
2020-03-29 18:53:04 +02:00
|
|
|
const PDFObject& paramsObject = storage->getObject(dictionary->get("Params"));
|
2019-11-17 17:41:07 +01:00
|
|
|
if (paramsObject.isDictionary())
|
|
|
|
{
|
|
|
|
const PDFDictionary* paramsDictionary = paramsObject.getDictionary();
|
|
|
|
auto getDateTime = [&loader, paramsDictionary](const char* name)
|
|
|
|
{
|
|
|
|
QByteArray ba = loader.readStringFromDictionary(paramsDictionary, name);
|
|
|
|
if (!ba.isEmpty())
|
|
|
|
{
|
|
|
|
return PDFEncoding::convertToDateTime(ba);
|
|
|
|
}
|
|
|
|
return QDateTime();
|
|
|
|
};
|
|
|
|
|
|
|
|
result.m_size = loader.readIntegerFromDictionary(paramsDictionary, "Size", -1);
|
|
|
|
result.m_creationDate = getDateTime("CreationDate");
|
|
|
|
result.m_modifiedDate = getDateTime("ModDate");
|
|
|
|
result.m_checksum = loader.readStringFromDictionary(paramsDictionary, "CheckSum");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-02 17:28:08 +02:00
|
|
|
PDFFileIdentifier PDFFileIdentifier::parse(const PDFObjectStorage* storage, PDFObject object)
|
|
|
|
{
|
|
|
|
PDFFileIdentifier result;
|
|
|
|
PDFDocumentDataLoaderDecorator loader(storage);
|
|
|
|
std::vector<QByteArray> identifiers = loader.readStringArray(object);
|
|
|
|
|
|
|
|
if (identifiers.size() >= 1)
|
|
|
|
{
|
|
|
|
result.m_permanentIdentifier = qMove(identifiers[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identifiers.size() >= 2)
|
|
|
|
{
|
|
|
|
result.m_changingIdentifier = qMove(identifiers[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-17 17:41:07 +01:00
|
|
|
} // namespace pdf
|