blob: 7402114756dda638e94fc745dca4bd83032320e9 [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
Jane Liu54a42142017-07-24 16:40:54 -040010#include "core/fdrm/crypto/fx_crypt.h"
Jane Liu54a42142017-07-24 16:40:54 -040011#include "core/fpdfapi/parser/cpdf_array.h"
Jane Liu53aafa92017-07-12 19:55:02 -040012#include "core/fpdfapi/parser/cpdf_document.h"
Jane Liu54a42142017-07-24 16:40:54 -040013#include "core/fpdfapi/parser/cpdf_name.h"
14#include "core/fpdfapi/parser/cpdf_number.h"
15#include "core/fpdfapi/parser/cpdf_reference.h"
Jane Liu18ae06d2017-07-18 10:15:16 -040016#include "core/fpdfapi/parser/cpdf_string.h"
17#include "core/fpdfapi/parser/fpdf_parser_decode.h"
Jane Liu53aafa92017-07-12 19:55:02 -040018#include "core/fpdfdoc/cpdf_filespec.h"
19#include "core/fpdfdoc/cpdf_nametree.h"
Jane Liu54a42142017-07-24 16:40:54 -040020#include "core/fxcrt/cfx_datetime.h"
21#include "core/fxcrt/fx_extension.h"
Jane Liu53aafa92017-07-12 19:55:02 -040022#include "fpdfsdk/fsdk_define.h"
23
Jane Liu54a42142017-07-24 16:40:54 -040024namespace {
25
Lei Zhangdf064df2017-08-31 02:33:27 -070026constexpr char kChecksumKey[] = "CheckSum";
27
Ryan Harrison275e2602017-09-18 14:23:18 -040028ByteString CFXByteStringHexDecode(const ByteString& bsHex) {
Jane Liu54a42142017-07-24 16:40:54 -040029 uint8_t* result = nullptr;
30 uint32_t size = 0;
31 HexDecode(bsHex.raw_str(), bsHex.GetLength(), &result, &size);
Ryan Harrison275e2602017-09-18 14:23:18 -040032 ByteString bsDecoded(result, size);
Jane Liu54a42142017-07-24 16:40:54 -040033 FX_Free(result);
34 return bsDecoded;
35}
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>();
74 pRoot->SetNewFor<CPDF_Reference>("Names", pDoc, pNames->GetObjNum());
75 }
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");
81 pNames->SetNewFor<CPDF_Reference>("EmbeddedFiles", pDoc,
82 pFiles->GetObjNum());
83 }
84
85 // Set up the basic entries in the filespec dictionary.
86 CPDF_Dictionary* pFile = pDoc->NewIndirect<CPDF_Dictionary>();
87 pFile->SetNewFor<CPDF_Name>("Type", "Filespec");
88 pFile->SetNewFor<CPDF_String>("UF", wsName);
89 pFile->SetNewFor<CPDF_String>("F", wsName);
90
91 // Add the new attachment name and filespec into the document's EmbeddedFiles.
92 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
93 if (!nameTree.AddValueAndName(
94 pdfium::MakeUnique<CPDF_Reference>(pDoc, pFile->GetObjNum()),
95 wsName)) {
96 return nullptr;
97 }
98
99 return pFile;
100}
101
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400102FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
103FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) {
Jane Liu53aafa92017-07-12 19:55:02 -0400104 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
105 if (!pDoc || index < 0)
Jane Liu18ae06d2017-07-18 10:15:16 -0400106 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400107
108 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
109 if (static_cast<size_t>(index) >= nameTree.GetCount())
Jane Liu18ae06d2017-07-18 10:15:16 -0400110 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400111
Ryan Harrison275e2602017-09-18 14:23:18 -0400112 WideString csName;
Jane Liu18ae06d2017-07-18 10:15:16 -0400113 return nameTree.LookupValueAndName(index, &csName);
114}
115
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400116FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
117FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) {
Jane Liuf63e8132017-07-25 18:11:27 -0400118 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
119 if (!pDoc || index < 0)
120 return false;
121
122 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
123 if (static_cast<size_t>(index) >= nameTree.GetCount())
124 return false;
125
126 return nameTree.DeleteValueAndName(index);
127}
128
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400129FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400130FPDFAttachment_GetName(FPDF_ATTACHMENT attachment,
131 void* buffer,
132 unsigned long buflen) {
133 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
Jane Liu53aafa92017-07-12 19:55:02 -0400134 if (!pFile)
135 return 0;
136
Jane Liu18ae06d2017-07-18 10:15:16 -0400137 return Utf16EncodeMaybeCopyAndReturnLength(CPDF_FileSpec(pFile).GetFileName(),
138 buffer, buflen);
139}
Jane Liu53aafa92017-07-12 19:55:02 -0400140
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400141FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700142FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400143 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
144 if (!pFile)
145 return 0;
146
147 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
Lei Zhangdf064df2017-08-31 02:33:27 -0700148 return pParamsDict ? pParamsDict->KeyExist(key) : 0;
Jane Liu18ae06d2017-07-18 10:15:16 -0400149}
150
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400151FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
Lei Zhangdf064df2017-08-31 02:33:27 -0700152FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400153 if (!FPDFAttachment_HasKey(attachment, key))
154 return FPDF_OBJECT_UNKNOWN;
155
Lei Zhangdf064df2017-08-31 02:33:27 -0700156 CPDF_FileSpec spec(CPDFObjectFromFPDFAttachment(attachment));
157 CPDF_Object* pObj = spec.GetParamsDict()->GetObjectFor(key);
158 return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN;
Jane Liu18ae06d2017-07-18 10:15:16 -0400159}
160
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400161FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu54a42142017-07-24 16:40:54 -0400162FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment,
Lei Zhangdf064df2017-08-31 02:33:27 -0700163 FPDF_BYTESTRING key,
Jane Liu54a42142017-07-24 16:40:54 -0400164 FPDF_WIDESTRING value) {
165 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
166 if (!pFile)
167 return false;
168
169 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
170 if (!pParamsDict)
171 return false;
172
Ryan Harrison275e2602017-09-18 14:23:18 -0400173 ByteString bsKey = key;
174 ByteString bsValue = CFXByteStringFromFPDFWideString(value);
Lei Zhangdf064df2017-08-31 02:33:27 -0700175 bool bEncodedAsHex = bsKey == kChecksumKey;
Jane Liu54a42142017-07-24 16:40:54 -0400176 if (bEncodedAsHex)
177 bsValue = CFXByteStringHexDecode(bsValue);
178
179 pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue, bEncodedAsHex);
180 return true;
181}
182
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400183FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400184FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment,
Lei Zhangdf064df2017-08-31 02:33:27 -0700185 FPDF_BYTESTRING key,
Jane Liu18ae06d2017-07-18 10:15:16 -0400186 void* buffer,
187 unsigned long buflen) {
188 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
189 if (!pFile)
190 return 0;
191
192 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
193 if (!pParamsDict)
194 return 0;
195
Ryan Harrison275e2602017-09-18 14:23:18 -0400196 ByteString bsKey = key;
197 WideString value = pParamsDict->GetUnicodeTextFor(bsKey);
Lei Zhangdf064df2017-08-31 02:33:27 -0700198 if (bsKey == kChecksumKey && !value.IsEmpty()) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400199 CPDF_String* stringValue = pParamsDict->GetObjectFor(bsKey)->AsString();
200 if (stringValue->IsHex()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400201 ByteString encoded = PDF_EncodeString(stringValue->GetString(), true);
Lei Zhangdf064df2017-08-31 02:33:27 -0700202 value = CPDF_String(nullptr, encoded, false).GetUnicodeText();
Jane Liu18ae06d2017-07-18 10:15:16 -0400203 }
204 }
205
206 return Utf16EncodeMaybeCopyAndReturnLength(value, buffer, buflen);
207}
208
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400209FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
210FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment,
211 FPDF_DOCUMENT document,
212 const void* contents,
213 const unsigned long len) {
Jane Liu54a42142017-07-24 16:40:54 -0400214 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
215 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
216 if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX)
217 return false;
218
219 // An empty content must have a zero length.
220 if (!contents && len != 0)
221 return false;
222
223 // Create a dictionary for the new embedded file stream.
224 auto pFileStreamDict = pdfium::MakeUnique<CPDF_Dictionary>();
225 CPDF_Dictionary* pParamsDict =
226 pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
227
228 // Set the size of the new file in the dictionary.
229 pFileStreamDict->SetNewFor<CPDF_Number>("DL", static_cast<int>(len));
230 pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len));
231
232 // Set the creation date of the new attachment in the dictionary.
233 CFX_DateTime dateTime;
234 dateTime.Now();
Ryan Harrison275e2602017-09-18 14:23:18 -0400235 ByteString bsDateTime;
Jane Liu54a42142017-07-24 16:40:54 -0400236 bsDateTime.Format("D:%d%02d%02d%02d%02d%02d", dateTime.GetYear(),
237 dateTime.GetMonth(), dateTime.GetDay(), dateTime.GetHour(),
238 dateTime.GetMinute(), dateTime.GetSecond());
239 pParamsDict->SetNewFor<CPDF_String>("CreationDate", bsDateTime, false);
240
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");
253 pEFDict->SetNewFor<CPDF_Reference>("F", pDoc, pFileStream->GetObjNum());
254 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}