blob: b759ae2655f173376834f120040aa1a1ec120a68 [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
26CFX_ByteString CFXByteStringHexDecode(const CFX_ByteString& bsHex) {
27 uint8_t* result = nullptr;
28 uint32_t size = 0;
29 HexDecode(bsHex.raw_str(), bsHex.GetLength(), &result, &size);
30 CFX_ByteString bsDecoded(result, size);
31 FX_Free(result);
32 return bsDecoded;
33}
34
35CFX_ByteString GenerateMD5Base16(const void* contents,
36 const unsigned long len) {
37 uint8_t digest[16];
38 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(contents), len, digest);
39 char buf[32];
40 for (int i = 0; i < 16; ++i)
41 FXSYS_IntToTwoHexChars(digest[i], &buf[i * 2]);
42
43 return CFX_ByteString(buf, 32);
44}
45
46} // namespace
47
Dan Sinclair00d2ad12017-08-10 14:13:02 -040048FPDF_EXPORT int FPDF_CALLCONV
49FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) {
Jane Liu53aafa92017-07-12 19:55:02 -040050 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
51 if (!pDoc)
52 return 0;
53
54 return CPDF_NameTree(pDoc, "EmbeddedFiles").GetCount();
55}
56
Dan Sinclair00d2ad12017-08-10 14:13:02 -040057FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
58FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) {
Jane Liu54a42142017-07-24 16:40:54 -040059 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
60 CFX_WideString wsName =
61 CFX_WideString::FromUTF16LE(name, CFX_WideString::WStringLength(name));
62 if (!pDoc || wsName.IsEmpty())
63 return nullptr;
64
65 CPDF_Dictionary* pRoot = pDoc->GetRoot();
66 if (!pRoot)
67 return nullptr;
68
69 // Retrieve the document's Names dictionary; create it if missing.
70 CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
71 if (!pNames) {
72 pNames = pDoc->NewIndirect<CPDF_Dictionary>();
73 pRoot->SetNewFor<CPDF_Reference>("Names", pDoc, pNames->GetObjNum());
74 }
75
76 // Create the EmbeddedFiles dictionary if missing.
77 if (!pNames->GetDictFor("EmbeddedFiles")) {
78 CPDF_Dictionary* pFiles = pDoc->NewIndirect<CPDF_Dictionary>();
79 pFiles->SetNewFor<CPDF_Array>("Names");
80 pNames->SetNewFor<CPDF_Reference>("EmbeddedFiles", pDoc,
81 pFiles->GetObjNum());
82 }
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);
88 pFile->SetNewFor<CPDF_String>("F", wsName);
89
90 // Add the new attachment name and filespec into the document's EmbeddedFiles.
91 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
92 if (!nameTree.AddValueAndName(
93 pdfium::MakeUnique<CPDF_Reference>(pDoc, pFile->GetObjNum()),
94 wsName)) {
95 return nullptr;
96 }
97
98 return pFile;
99}
100
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400101FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
102FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) {
Jane Liu53aafa92017-07-12 19:55:02 -0400103 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
104 if (!pDoc || index < 0)
Jane Liu18ae06d2017-07-18 10:15:16 -0400105 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400106
107 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
108 if (static_cast<size_t>(index) >= nameTree.GetCount())
Jane Liu18ae06d2017-07-18 10:15:16 -0400109 return nullptr;
Jane Liu53aafa92017-07-12 19:55:02 -0400110
Jane Liu67ccef72017-07-19 13:10:50 -0400111 CFX_WideString csName;
Jane Liu18ae06d2017-07-18 10:15:16 -0400112 return nameTree.LookupValueAndName(index, &csName);
113}
114
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400115FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
116FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) {
Jane Liuf63e8132017-07-25 18:11:27 -0400117 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
118 if (!pDoc || index < 0)
119 return false;
120
121 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
122 if (static_cast<size_t>(index) >= nameTree.GetCount())
123 return false;
124
125 return nameTree.DeleteValueAndName(index);
126}
127
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400128FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400129FPDFAttachment_GetName(FPDF_ATTACHMENT attachment,
130 void* buffer,
131 unsigned long buflen) {
132 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
Jane Liu53aafa92017-07-12 19:55:02 -0400133 if (!pFile)
134 return 0;
135
Jane Liu18ae06d2017-07-18 10:15:16 -0400136 return Utf16EncodeMaybeCopyAndReturnLength(CPDF_FileSpec(pFile).GetFileName(),
137 buffer, buflen);
138}
Jane Liu53aafa92017-07-12 19:55:02 -0400139
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400140FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
141FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400142 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
143 if (!pFile)
144 return 0;
145
146 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
147 if (!pParamsDict)
148 return 0;
149
150 return pParamsDict->KeyExist(CFXByteStringFromFPDFWideString(key));
151}
152
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400153FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400154FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key) {
155 if (!FPDFAttachment_HasKey(attachment, key))
156 return FPDF_OBJECT_UNKNOWN;
157
158 CPDF_Object* pObj = CPDF_FileSpec(CPDFObjectFromFPDFAttachment(attachment))
159 .GetParamsDict()
160 ->GetObjectFor(CFXByteStringFromFPDFWideString(key));
161 if (!pObj)
162 return FPDF_OBJECT_UNKNOWN;
163
164 return pObj->GetType();
165}
166
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400167FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Jane Liu54a42142017-07-24 16:40:54 -0400168FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment,
169 FPDF_WIDESTRING key,
170 FPDF_WIDESTRING value) {
171 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
172 if (!pFile)
173 return false;
174
175 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
176 if (!pParamsDict)
177 return false;
178
179 CFX_ByteString bsKey = CFXByteStringFromFPDFWideString(key);
180 CFX_ByteString bsValue = CFXByteStringFromFPDFWideString(value);
181 bool bEncodedAsHex = bsKey == "CheckSum";
182 if (bEncodedAsHex)
183 bsValue = CFXByteStringHexDecode(bsValue);
184
185 pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue, bEncodedAsHex);
186 return true;
187}
188
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400189FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400190FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment,
191 FPDF_WIDESTRING key,
192 void* buffer,
193 unsigned long buflen) {
194 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
195 if (!pFile)
196 return 0;
197
198 CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict();
199 if (!pParamsDict)
200 return 0;
201
202 CFX_ByteString bsKey = CFXByteStringFromFPDFWideString(key);
203 CFX_WideString value = pParamsDict->GetUnicodeTextFor(bsKey);
Jane Liu54a42142017-07-24 16:40:54 -0400204 if (bsKey == "CheckSum" && !value.IsEmpty()) {
Jane Liu18ae06d2017-07-18 10:15:16 -0400205 CPDF_String* stringValue = pParamsDict->GetObjectFor(bsKey)->AsString();
206 if (stringValue->IsHex()) {
207 value =
208 CPDF_String(nullptr, PDF_EncodeString(stringValue->GetString(), true),
209 false)
210 .GetUnicodeText();
211 }
212 }
213
214 return Utf16EncodeMaybeCopyAndReturnLength(value, buffer, buflen);
215}
216
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400217FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
218FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment,
219 FPDF_DOCUMENT document,
220 const void* contents,
221 const unsigned long len) {
Jane Liu54a42142017-07-24 16:40:54 -0400222 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
223 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
224 if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX)
225 return false;
226
227 // An empty content must have a zero length.
228 if (!contents && len != 0)
229 return false;
230
231 // Create a dictionary for the new embedded file stream.
232 auto pFileStreamDict = pdfium::MakeUnique<CPDF_Dictionary>();
233 CPDF_Dictionary* pParamsDict =
234 pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params");
235
236 // Set the size of the new file in the dictionary.
237 pFileStreamDict->SetNewFor<CPDF_Number>("DL", static_cast<int>(len));
238 pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len));
239
240 // Set the creation date of the new attachment in the dictionary.
241 CFX_DateTime dateTime;
242 dateTime.Now();
243 CFX_ByteString bsDateTime;
244 bsDateTime.Format("D:%d%02d%02d%02d%02d%02d", dateTime.GetYear(),
245 dateTime.GetMonth(), dateTime.GetDay(), dateTime.GetHour(),
246 dateTime.GetMinute(), dateTime.GetSecond());
247 pParamsDict->SetNewFor<CPDF_String>("CreationDate", bsDateTime, false);
248
249 // Set the checksum of the new attachment in the dictionary.
250 pParamsDict->SetNewFor<CPDF_String>(
251 "CheckSum", CFXByteStringHexDecode(GenerateMD5Base16(contents, len)),
252 true);
253
254 // Create the file stream and have the filespec dictionary link to it.
255 std::unique_ptr<uint8_t, FxFreeDeleter> stream(FX_Alloc(uint8_t, len));
256 memcpy(stream.get(), contents, len);
257 CPDF_Stream* pFileStream = pDoc->NewIndirect<CPDF_Stream>(
258 std::move(stream), len, std::move(pFileStreamDict));
259 CPDF_Dictionary* pEFDict =
260 pFile->AsDictionary()->SetNewFor<CPDF_Dictionary>("EF");
261 pEFDict->SetNewFor<CPDF_Reference>("F", pDoc, pFileStream->GetObjNum());
262 return true;
263}
264
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400265FPDF_EXPORT unsigned long FPDF_CALLCONV
Jane Liu18ae06d2017-07-18 10:15:16 -0400266FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment,
267 void* buffer,
268 unsigned long buflen) {
269 CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment);
270 if (!pFile)
271 return 0;
272
273 CPDF_Stream* pFileStream = CPDF_FileSpec(pFile).GetFileStream();
274 if (!pFileStream)
275 return 0;
276
Jane Liu548334e2017-08-03 16:33:40 -0400277 return DecodeStreamMaybeCopyAndReturnLength(pFileStream, buffer, buflen);
Jane Liu53aafa92017-07-12 19:55:02 -0400278}