Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_attachment.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "public/fpdf_attachment.h"
6
7#include <limits.h>
8
9#include <memory>
10#include <utility>
11
12#include "constants/stream_dict_common.h"
13#include "core/fdrm/fx_crypt.h"
14#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_dictionary.h"
16#include "core/fpdfapi/parser/cpdf_document.h"
17#include "core/fpdfapi/parser/cpdf_name.h"
18#include "core/fpdfapi/parser/cpdf_number.h"
19#include "core/fpdfapi/parser/cpdf_reference.h"
20#include "core/fpdfapi/parser/cpdf_stream.h"
21#include "core/fpdfapi/parser/cpdf_string.h"
22#include "core/fpdfapi/parser/fpdf_parser_decode.h"
23#include "core/fpdfdoc/cpdf_filespec.h"
24#include "core/fpdfdoc/cpdf_nametree.h"
25#include "core/fxcrt/cfx_datetime.h"
26#include "core/fxcrt/data_vector.h"
27#include "core/fxcrt/fx_extension.h"
28#include "core/fxcrt/fx_memory_wrappers.h"
29#include "fpdfsdk/cpdfsdk_helpers.h"
30#include "third_party/base/numerics/safe_conversions.h"
31
32namespace {
33
34constexpr char kChecksumKey[] = "CheckSum";
35
36ByteString CFXByteStringHexDecode(const ByteString& bsHex) {
37 std::unique_ptr<uint8_t, FxFreeDeleter> result;
38 uint32_t size = 0;
39 HexDecode(bsHex.raw_span(), &result, &size);
40 return ByteString(result.get(), size);
41}
42
43ByteString GenerateMD5Base16(const void* contents, const unsigned long len) {
44 uint8_t digest[16];
45 CRYPT_MD5Generate({static_cast<const uint8_t*>(contents), len}, digest);
46 char buf[32];
47 for (int i = 0; i < 16; ++i)
48 FXSYS_IntToTwoHexChars(digest[i], &buf[i * 2]);
49
50 return ByteString(buf, 32);
51}
52
53} // namespace
54
56FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) {
58 if (!pDoc)
59 return 0;
60
61 auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles");
62 return name_tree ? pdfium::base::checked_cast<int>(name_tree->GetCount()) : 0;
63}
64
65FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
66FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) {
68 if (!pDoc)
69 return nullptr;
70
71 WideString wsName = WideStringFromFPDFWideString(name);
72 if (wsName.IsEmpty())
73 return nullptr;
74
75 auto name_tree =
76 CPDF_NameTree::CreateWithRootNameArray(pDoc, "EmbeddedFiles");
77 if (!name_tree)
78 return nullptr;
79
80 // Set up the basic entries in the filespec dictionary.
81 auto pFile = pDoc->NewIndirect<CPDF_Dictionary>();
82 pFile->SetNewFor<CPDF_Name>("Type", "Filespec");
83 pFile->SetNewFor<CPDF_String>("UF", wsName.AsStringView());
84 pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName.AsStringView());
85
86 // Add the new attachment name and filespec into the document's EmbeddedFiles.
87 if (!name_tree->AddValueAndName(pFile->MakeReference(pDoc), wsName))
88 return nullptr;
89
90 // Unretained reference in public API. NOLINTNEXTLINE
91 return FPDFAttachmentFromCPDFObject(pFile);
92}
93
94FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
95FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) {
97 if (!pDoc || index < 0)
98 return nullptr;
99
100 auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles");
101 if (!name_tree || static_cast<size_t>(index) >= name_tree->GetCount())
102 return nullptr;
103
104 WideString csName;
105
106 // Unretained reference in public API. NOLINTNEXTLINE
107 return FPDFAttachmentFromCPDFObject(
108 name_tree->LookupValueAndName(index, &csName));
109}
110
112FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) {
114 if (!pDoc || index < 0)
115 return false;
116
117 auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles");
118 if (!name_tree || static_cast<size_t>(index) >= name_tree->GetCount())
119 return false;
120
121 return name_tree->DeleteValueAndName(index);
122}
123
124FPDF_EXPORT unsigned long FPDF_CALLCONV
125FPDFAttachment_GetName(FPDF_ATTACHMENT attachment,
126 FPDF_WCHAR* buffer,
127 unsigned long buflen) {
129 if (!pFile)
130 return 0;
131
132 CPDF_FileSpec spec(pdfium::WrapRetain(pFile));
134 buflen);
135}
136
138FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
140 if (!pFile)
141 return 0;
142
143 CPDF_FileSpec spec(pdfium::WrapRetain(pFile));
144 RetainPtr<const CPDF_Dictionary> pParamsDict = spec.GetParamsDict();
145 return pParamsDict ? pParamsDict->KeyExist(key) : 0;
146}
147
148FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
149FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
150 if (!FPDFAttachment_HasKey(attachment, key))
151 return FPDF_OBJECT_UNKNOWN;
152
153 CPDF_FileSpec spec(
154 pdfium::WrapRetain(CPDFObjectFromFPDFAttachment(attachment)));
155 RetainPtr<const CPDF_Object> pObj = spec.GetParamsDict()->GetObjectFor(key);
156 return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN;
157}
158
160FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment,
161 FPDF_BYTESTRING key,
162 FPDF_WIDESTRING value) {
164 if (!pFile)
165 return false;
166
167 CPDF_FileSpec spec(pdfium::WrapRetain(pFile));
168 RetainPtr<CPDF_Dictionary> pParamsDict = spec.GetMutableParamsDict();
169 if (!pParamsDict)
170 return false;
171
172 ByteString bsKey = key;
173 ByteString bsValue = ByteStringFromFPDFWideString(value);
174 bool bEncodedAsHex = bsKey == kChecksumKey;
175 if (bEncodedAsHex)
176 bsValue = CFXByteStringHexDecode(bsValue);
177
178 pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue, bEncodedAsHex);
179 return true;
180}
181
182FPDF_EXPORT unsigned long FPDF_CALLCONV
183FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment,
184 FPDF_BYTESTRING key,
185 FPDF_WCHAR* buffer,
186 unsigned long buflen) {
188 if (!pFile)
189 return 0;
190
191 CPDF_FileSpec spec(pdfium::WrapRetain(pFile));
192 RetainPtr<const CPDF_Dictionary> pParamsDict = spec.GetParamsDict();
193 if (!pParamsDict)
194 return 0;
195
196 ByteString bsKey = key;
197 WideString value = pParamsDict->GetUnicodeTextFor(bsKey);
198 if (bsKey == kChecksumKey && !value.IsEmpty()) {
199 const CPDF_String* stringValue =
200 pParamsDict->GetObjectFor(bsKey)->AsString();
201 if (stringValue->IsHex()) {
202 ByteString encoded =
203 PDF_HexEncodeString(stringValue->GetString().AsStringView());
204 value = pdfium::MakeRetain<CPDF_String>(nullptr, encoded, false)
205 ->GetUnicodeText();
206 }
207 }
208
209 return Utf16EncodeMaybeCopyAndReturnLength(value, buffer, buflen);
210}
211
213FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment,
214 FPDF_DOCUMENT document,
215 const void* contents,
216 unsigned long len) {
219 if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX)
220 return false;
221
222 // An empty content must have a zero length.
223 if (!contents && len != 0)
224 return false;
225
226 // Create a dictionary for the new embedded file stream.
227 auto pFileStreamDict = pdfium::MakeRetain<CPDF_Dictionary>();
228 auto pParamsDict = pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
229
230 // Set the size of the new file in the dictionary.
231 pFileStreamDict->SetNewFor<CPDF_Number>(pdfium::stream::kDL,
232 static_cast<int>(len));
233 pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len));
234
235 // Set the creation date of the new attachment in the dictionary.
237 pParamsDict->SetNewFor<CPDF_String>(
238 "CreationDate",
239 ByteString::Format("D:%d%02d%02d%02d%02d%02d", dateTime.GetYear(),
240 dateTime.GetMonth(), dateTime.GetDay(),
241 dateTime.GetHour(), dateTime.GetMinute(),
242 dateTime.GetSecond()),
243 false);
244
245 // Set the checksum of the new attachment in the dictionary.
246 pParamsDict->SetNewFor<CPDF_String>(
247 kChecksumKey, CFXByteStringHexDecode(GenerateMD5Base16(contents, len)),
248 true);
249
250 // Create the file stream and have the filespec dictionary link to it.
251 const uint8_t* contents_as_bytes = static_cast<const uint8_t*>(contents);
252 auto pFileStream = pDoc->NewIndirect<CPDF_Stream>(
253 DataVector<uint8_t>(contents_as_bytes, contents_as_bytes + len),
254 std::move(pFileStreamDict));
255 auto pEFDict = pFile->AsMutableDictionary()->SetNewFor<CPDF_Dictionary>("EF");
256 pEFDict->SetNewFor<CPDF_Reference>("F", pDoc, pFileStream->GetObjNum());
257 return true;
258}
259
261FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment,
262 void* buffer,
263 unsigned long buflen,
264 unsigned long* out_buflen) {
265 if (!out_buflen)
266 return false;
267
269 if (!pFile)
270 return false;
271
272 CPDF_FileSpec spec(pdfium::WrapRetain(pFile));
273 RetainPtr<const CPDF_Stream> pFileStream = spec.GetFileStream();
274 if (!pFileStream)
275 return false;
276
277 *out_buflen = DecodeStreamMaybeCopyAndReturnLength(
278 std::move(pFileStream),
279 {static_cast<uint8_t*>(buffer), static_cast<size_t>(buflen)});
280 return true;
281}
int32_t GetYear() const
static CFX_DateTime Now()
uint8_t GetSecond() const
uint8_t GetHour() const
uint8_t GetDay() const
uint8_t GetMinute() const
uint8_t GetMonth() const
WideString GetFileName() const
virtual CPDF_Dictionary * AsMutableDictionary()
bool IsHex() const
Definition cpdf_string.h:30
ByteString GetString() const override
static ByteString Format(const char *pFormat,...)
bool operator==(const char *ptr) const
ByteString & operator=(ByteString &&that) noexcept
bool IsEmpty() const
Definition widestring.h:118
WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString &text, void *buffer, unsigned long buflen)
CPDF_Object * CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment)
CPDF_Document * CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc)
FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment, FPDF_DOCUMENT document, const void *contents, unsigned long len)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT int FPDF_CALLCONV FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key, FPDF_WIDESTRING value)
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment, void *buffer, unsigned long buflen, unsigned long *out_buflen)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key)
#define FPDF_CALLCONV
Definition fpdfview.h:227
#define FPDF_EXPORT
Definition fpdfview.h:221
#define FPDF_OBJECT_UNKNOWN
Definition fpdfview.h:36
void FXSYS_IntToTwoHexChars(uint8_t n, char *buf)