blob: 692e5ddb761c483beab40954b54cdfd9e3be77e1 [file] [log] [blame]
Jane Liu53aafa92017-07-12 19:55:02 -04001// Copyright 2017 PDFium Authors. All rights reserved.
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
Jane Liu605fb602017-07-25 14:44:11 -04007#include <memory>
8#include <utility>
9
Lei Zhang26170562018-04-17 17:01:52 +000010#include "constants/stream_dict_common.h"
Jane Liu54a42142017-07-24 16:40:54 -040011#include "core/fdrm/crypto/fx_crypt.h"
Jane Liu54a42142017-07-24 16:40:54 -040012#include "core/fpdfapi/parser/cpdf_array.h"
Lei Zhang81535612018-10-09 21:15:17 +000013#include "core/fpdfapi/parser/cpdf_dictionary.h"
Jane Liu53aafa92017-07-12 19:55:02 -040014#include "core/fpdfapi/parser/cpdf_document.h"
Jane Liu54a42142017-07-24 16:40:54 -040015#include "core/fpdfapi/parser/cpdf_name.h"
16#include "core/fpdfapi/parser/cpdf_number.h"
17#include "core/fpdfapi/parser/cpdf_reference.h"
Jane Liu18ae06d2017-07-18 10:15:16 -040018#include "core/fpdfapi/parser/cpdf_string.h"
19#include "core/fpdfapi/parser/fpdf_parser_decode.h"
Jane Liu53aafa92017-07-12 19:55:02 -040020#include "core/fpdfdoc/cpdf_filespec.h"
21#include "core/fpdfdoc/cpdf_nametree.h"
Jane Liu54a42142017-07-24 16:40:54 -040022#include "core/fxcrt/cfx_datetime.h"
23#include "core/fxcrt/fx_extension.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000024#include "fpdfsdk/cpdfsdk_helpers.h"
Jane Liu53aafa92017-07-12 19:55:02 -040025
Jane Liu54a42142017-07-24 16:40:54 -040026namespace {
27
Lei Zhangdf064df2017-08-31 02:33:27 -070028constexpr char kChecksumKey[] = "CheckSum";
29
Ryan Harrison275e2602017-09-18 14:23:18 -040030ByteString CFXByteStringHexDecode(const ByteString& bsHex) {
Lei Zhang8365e762018-09-11 07:57:48 +000031 std::unique_ptr<uint8_t, FxFreeDeleter> result;
Jane Liu54a42142017-07-24 16:40:54 -040032 uint32_t size = 0;
Tom Sepez1934a242018-08-29 19:32:47 +000033 HexDecode(bsHex.AsRawSpan(), &result, &size);
Lei Zhang8365e762018-09-11 07:57:48 +000034 return ByteString(result.get(), size);
Jane Liu54a42142017-07-24 16:40:54 -040035}
36
Ryan Harrison275e2602017-09-18 14:23:18 -040037ByteString GenerateMD5Base16(const void* contents, const unsigned long len) {
Jane Liu54a42142017-07-24 16:40:54 -040038 uint8_t digest[16];
39 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(contents), len, digest);
40 char buf[32];
41 for (int i = 0; i < 16; ++i)
42 FXSYS_IntToTwoHexChars(digest[i], &buf[i * 2]);
43
Ryan Harrison275e2602017-09-18 14:23:18 -040044 return ByteString(buf, 32);
Jane Liu54a42142017-07-24 16:40:54 -040045}
46
47} // namespace
48
Dan Sinclair00d2ad12017-08-10 14:13:02 -040049FPDF_EXPORT int FPDF_CALLCONV
50FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) {
Jane Liu53aafa92017-07-12 19:55:02 -040051 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
52 if (!pDoc)
53 return 0;
54
55 return CPDF_NameTree(pDoc, "EmbeddedFiles").GetCount();
56}
57
Dan Sinclair00d2ad12017-08-10 14:13:02 -040058FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
59FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) {
Jane Liu54a42142017-07-24 16:40:54 -040060 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Ryan Harrison275e2602017-09-18 14:23:18 -040061 WideString wsName =
62 WideString::FromUTF16LE(name, WideString::WStringLength(name));
Jane Liu54a42142017-07-24 16:40:54 -040063 if (!pDoc || wsName.IsEmpty())
64 return nullptr;
65
66 CPDF_Dictionary* pRoot = pDoc->GetRoot();
67 if (!pRoot)
68 return nullptr;
69
70 // Retrieve the document's Names dictionary; create it if missing.
71 CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
72 if (!pNames) {
73 pNames = pDoc->NewIndirect<CPDF_Dictionary>();
Artem Stryginfb727262018-06-11 18:19:57 +000074 pRoot->SetFor("Names", pNames->MakeReference(pDoc));
Jane Liu54a42142017-07-24 16:40:54 -040075 }
76
77 // Create the EmbeddedFiles dictionary if missing.
78 if (!pNames->GetDictFor("EmbeddedFiles")) {
79 CPDF_Dictionary* pFiles = pDoc->NewIndirect<CPDF_Dictionary>();
80 pFiles->SetNewFor<CPDF_Array>("Names");
Artem Stryginfb727262018-06-11 18:19:57 +000081 pNames->SetFor("EmbeddedFiles", pFiles->MakeReference(pDoc));
Jane Liu54a42142017-07-24 16:40:54 -040082 }
83
84 // Set up the basic entries in the filespec dictionary.
85 CPDF_Dictionary* pFile = pDoc->NewIndirect<CPDF_Dictionary>();
86 pFile->SetNewFor<CPDF_Name>("Type", "Filespec");
87 pFile->SetNewFor<CPDF_String>("UF", wsName);
Lei Zhang26170562018-04-17 17:01:52 +000088 pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName);
Jane Liu54a42142017-07-24 16:40:54 -040089
90 // Add the new attachment name and filespec into the document's EmbeddedFiles.
91 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
Artem Stryginfb727262018-06-11 18:19:57 +000092 if (!nameTree.AddValueAndName(pFile->MakeReference(pDoc), wsName)) {
Jane Liu54a42142017-07-24 16:40:54 -040093 return nullptr;
94 }
95
Tom Sepez525147a2018-05-03 17:19:53 +000096 return FPDFAttachmentFromCPDFObject(pFile);
Jane Liu54a42142017-07-24 16:40:54 -040097}
98
Dan Sinclair00d2ad12017-08-10 14:13:02 -040099FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
100FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) {
Jane Liu53aafa92017-07-12 19:55:02 -0400101 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
102 if (!pDoc || index < 0)
Jane Liu18ae06d2017-07-18 10:15:16 -0400103 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400104
105 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
106 if (static_cast<size_t>(index) >= nameTree.GetCount())
Jane Liu18ae06d2017-07-18 10:15:16 -0400107 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400108
Ryan Harrison275e2602017-09-18 14:23:18 -0400109 WideString csName;
Tom Sepez525147a2018-05-03 17:19:53 +0000110 return FPDFAttachmentFromCPDFObject(
111 nameTree.LookupValueAndName(index, &csName));
Jane Liu18ae06d2017-07-18 10:15:16 -0400112}
113
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400114FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
115FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) {
Jane Liuf63e8132017-07-25 18:11:27 -0400116 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
117 if (!pDoc || index < 0)
118 return false;
119
120 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
121 if (static_cast<size_t>(index) >= nameTree.GetCount())
122 return false;
123
124 return nameTree.DeleteValueAndName(index);
125}
126
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400127FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400128FPDFAttachment_GetName(FPDF_ATTACHMENT attachment,
129 void* buffer,
130 unsigned long buflen) {
131 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
Jane Liu53aafa92017-07-12 19:55:02 -0400132 if (!pFile)
133 return 0;
134
Jane Liu18ae06d2017-07-18 10:15:16 -0400135 return Utf16EncodeMaybeCopyAndReturnLength(CPDF_FileSpec(pFile).GetFileName(),
136 buffer, buflen);
137}
Jane Liu53aafa92017-07-12 19:55:02 -0400138
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400139FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700140FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400141 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
142 if (!pFile)
143 return 0;
144
145 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
Lei Zhangdf064df2017-08-31 02:33:27 -0700146 return pParamsDict ? pParamsDict->KeyExist(key) : 0;
Jane Liu18ae06d2017-07-18 10:15:16 -0400147}
148
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400149FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700150FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400151 if (!FPDFAttachment_HasKey(attachment, key))
152 return FPDF_OBJECT_UNKNOWN;
153
Lei Zhangdf064df2017-08-31 02:33:27 -0700154 CPDF_FileSpec spec(CPDFObjectFromFPDFAttachment(attachment));
155 CPDF_Object* pObj = spec.GetParamsDict()->GetObjectFor(key);
156 return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN;
Jane Liu18ae06d2017-07-18 10:15:16 -0400157}
158
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400159FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu54a42142017-07-24 16:40:54 -0400160FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment,
Lei Zhangdf064df2017-08-31 02:33:27 -0700161 FPDF_BYTESTRING key,
Jane Liu54a42142017-07-24 16:40:54 -0400162 FPDF_WIDESTRING value) {
163 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
164 if (!pFile)
165 return false;
166
167 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
168 if (!pParamsDict)
169 return false;
170
Ryan Harrison275e2602017-09-18 14:23:18 -0400171 ByteString bsKey = key;
172 ByteString bsValue = CFXByteStringFromFPDFWideString(value);
Lei Zhangdf064df2017-08-31 02:33:27 -0700173 bool bEncodedAsHex = bsKey == kChecksumKey;
Jane Liu54a42142017-07-24 16:40:54 -0400174 if (bEncodedAsHex)
175 bsValue = CFXByteStringHexDecode(bsValue);
176
177 pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue, bEncodedAsHex);
178 return true;
179}
180
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400181FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400182FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment,
Lei Zhangdf064df2017-08-31 02:33:27 -0700183 FPDF_BYTESTRING key,
Jane Liu18ae06d2017-07-18 10:15:16 -0400184 void* buffer,
185 unsigned long buflen) {
186 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
187 if (!pFile)
188 return 0;
189
190 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
191 if (!pParamsDict)
192 return 0;
193
Ryan Harrison275e2602017-09-18 14:23:18 -0400194 ByteString bsKey = key;
195 WideString value = pParamsDict->GetUnicodeTextFor(bsKey);
Lei Zhangdf064df2017-08-31 02:33:27 -0700196 if (bsKey == kChecksumKey && !value.IsEmpty()) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400197 CPDF_String* stringValue = pParamsDict->GetObjectFor(bsKey)->AsString();
198 if (stringValue->IsHex()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400199 ByteString encoded = PDF_EncodeString(stringValue->GetString(), true);
Lei Zhangdf064df2017-08-31 02:33:27 -0700200 value = CPDF_String(nullptr, encoded, false).GetUnicodeText();
Jane Liu18ae06d2017-07-18 10:15:16 -0400201 }
202 }
203
204 return Utf16EncodeMaybeCopyAndReturnLength(value, buffer, buflen);
205}
206
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400207FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
208FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment,
209 FPDF_DOCUMENT document,
210 const void* contents,
211 const unsigned long len) {
Jane Liu54a42142017-07-24 16:40:54 -0400212 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
213 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
214 if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX)
215 return false;
216
217 // An empty content must have a zero length.
218 if (!contents && len != 0)
219 return false;
220
221 // Create a dictionary for the new embedded file stream.
222 auto pFileStreamDict = pdfium::MakeUnique<CPDF_Dictionary>();
223 CPDF_Dictionary* pParamsDict =
224 pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
225
226 // Set the size of the new file in the dictionary.
Lei Zhang26170562018-04-17 17:01:52 +0000227 pFileStreamDict->SetNewFor<CPDF_Number>(pdfium::stream::kDL,
228 static_cast<int>(len));
Jane Liu54a42142017-07-24 16:40:54 -0400229 pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len));
230
231 // Set the creation date of the new attachment in the dictionary.
Dan Sinclairec2209d2017-11-16 22:08:27 +0000232 CFX_DateTime dateTime = CFX_DateTime::Now();
Dan Sinclair1c4735a2017-11-16 22:08:07 +0000233 pParamsDict->SetNewFor<CPDF_String>(
234 "CreationDate",
235 ByteString::Format("D:%d%02d%02d%02d%02d%02d", dateTime.GetYear(),
236 dateTime.GetMonth(), dateTime.GetDay(),
237 dateTime.GetHour(), dateTime.GetMinute(),
238 dateTime.GetSecond()),
239 false);
Jane Liu54a42142017-07-24 16:40:54 -0400240
241 // Set the checksum of the new attachment in the dictionary.
242 pParamsDict->SetNewFor<CPDF_String>(
Lei Zhangdf064df2017-08-31 02:33:27 -0700243 kChecksumKey, CFXByteStringHexDecode(GenerateMD5Base16(contents, len)),
Jane Liu54a42142017-07-24 16:40:54 -0400244 true);
245
246 // Create the file stream and have the filespec dictionary link to it.
247 std::unique_ptr<uint8_t, FxFreeDeleter> stream(FX_Alloc(uint8_t, len));
248 memcpy(stream.get(), contents, len);
249 CPDF_Stream* pFileStream = pDoc->NewIndirect<CPDF_Stream>(
250 std::move(stream), len, std::move(pFileStreamDict));
251 CPDF_Dictionary* pEFDict =
252 pFile->AsDictionary()->SetNewFor<CPDF_Dictionary>("EF");
Artem Stryginfb727262018-06-11 18:19:57 +0000253 pEFDict->SetFor("F", pFileStream->MakeReference(pDoc));
Jane Liu54a42142017-07-24 16:40:54 -0400254 return true;
255}
256
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400257FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400258FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment,
259 void* buffer,
260 unsigned long buflen) {
261 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
262 if (!pFile)
263 return 0;
264
265 CPDF_Stream* pFileStream = CPDF_FileSpec(pFile).GetFileStream();
266 if (!pFileStream)
267 return 0;
268
Jane Liu548334e2017-08-03 16:33:40 -0400269 return DecodeStreamMaybeCopyAndReturnLength(pFileStream, buffer, buflen);
Jane Liu53aafa92017-07-12 19:55:02 -0400270}