Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 1 | // 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 Liu | 605fb60 | 2017-07-25 14:44:11 -0400 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 10 | #include "core/fdrm/crypto/fx_crypt.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_array.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_document.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 14 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 15 | #include "core/fpdfapi/parser/cpdf_reference.h" |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_string.h" |
| 17 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_filespec.h" |
| 19 | #include "core/fpdfdoc/cpdf_nametree.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 20 | #include "core/fxcrt/cfx_datetime.h" |
| 21 | #include "core/fxcrt/fx_extension.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 22 | #include "fpdfsdk/fsdk_define.h" |
| 23 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | CFX_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 | |
| 35 | CFX_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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 48 | FPDF_EXPORT int FPDF_CALLCONV |
| 49 | FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) { |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 50 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 51 | if (!pDoc) |
| 52 | return 0; |
| 53 | |
| 54 | return CPDF_NameTree(pDoc, "EmbeddedFiles").GetCount(); |
| 55 | } |
| 56 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 57 | FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV |
| 58 | FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 59 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 101 | FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV |
| 102 | FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) { |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 103 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 104 | if (!pDoc || index < 0) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 105 | return nullptr; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 106 | |
| 107 | CPDF_NameTree nameTree(pDoc, "EmbeddedFiles"); |
| 108 | if (static_cast<size_t>(index) >= nameTree.GetCount()) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 109 | return nullptr; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 110 | |
Jane Liu | 67ccef7 | 2017-07-19 13:10:50 -0400 | [diff] [blame] | 111 | CFX_WideString csName; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 112 | return nameTree.LookupValueAndName(index, &csName); |
| 113 | } |
| 114 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 115 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 116 | FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) { |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 117 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 128 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 129 | FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, |
| 130 | void* buffer, |
| 131 | unsigned long buflen) { |
| 132 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 133 | if (!pFile) |
| 134 | return 0; |
| 135 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 136 | return Utf16EncodeMaybeCopyAndReturnLength(CPDF_FileSpec(pFile).GetFileName(), |
| 137 | buffer, buflen); |
| 138 | } |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 139 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 140 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 141 | FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 142 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 153 | FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 154 | FPDFAttachment_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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 167 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 168 | FPDFAttachment_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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 189 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 190 | FPDFAttachment_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 Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 204 | if (bsKey == "CheckSum" && !value.IsEmpty()) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 205 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 217 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 218 | FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment, |
| 219 | FPDF_DOCUMENT document, |
| 220 | const void* contents, |
| 221 | const unsigned long len) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 222 | 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 265 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 266 | FPDFAttachment_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 Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 277 | return DecodeStreamMaybeCopyAndReturnLength(pFileStream, buffer, buflen); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 278 | } |