K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2017 The PDFium Authors |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 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 | |
Lei Zhang | bfd8a9b | 2020-04-23 16:49:08 +0000 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | |
Lei Zhang | 295454b | 2024-05-22 02:10:33 +0000 | [diff] [blame] | 9 | #include <array> |
Jane Liu | 605fb60 | 2017-07-25 14:44:11 -0400 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <utility> |
| 12 | |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame] | 13 | #include "constants/stream_dict_common.h" |
Lei Zhang | d145e4b | 2018-10-12 18:54:31 +0000 | [diff] [blame] | 14 | #include "core/fdrm/fx_crypt.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_array.h" |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_document.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 18 | #include "core/fpdfapi/parser/cpdf_name.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_reference.h" |
Lei Zhang | ed975f9 | 2019-02-20 18:49:01 +0000 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 22 | #include "core/fpdfapi/parser/cpdf_string.h" |
| 23 | #include "core/fpdfapi/parser/fpdf_parser_decode.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 24 | #include "core/fpdfdoc/cpdf_filespec.h" |
| 25 | #include "core/fpdfdoc/cpdf_nametree.h" |
Lei Zhang | fc0397e | 2024-05-16 21:07:59 +0000 | [diff] [blame] | 26 | #include "core/fxcodec/data_and_bytes_consumed.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 27 | #include "core/fxcrt/cfx_datetime.h" |
Lei Zhang | 6dc8a14 | 2022-10-20 23:08:15 +0000 | [diff] [blame] | 28 | #include "core/fxcrt/data_vector.h" |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 29 | #include "core/fxcrt/fx_extension.h" |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 30 | #include "core/fxcrt/numerics/safe_conversions.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 31 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 32 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 33 | namespace { |
| 34 | |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 35 | constexpr char kChecksumKey[] = "CheckSum"; |
| 36 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 37 | } // namespace |
| 38 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 39 | FPDF_EXPORT int FPDF_CALLCONV |
| 40 | FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) { |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 41 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 42 | if (!pDoc) |
| 43 | return 0; |
| 44 | |
Lei Zhang | 1575b47 | 2020-04-02 23:09:15 +0000 | [diff] [blame] | 45 | auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles"); |
Lei Zhang | f36006c | 2024-02-17 00:56:24 +0000 | [diff] [blame] | 46 | return name_tree ? pdfium::checked_cast<int>(name_tree->GetCount()) : 0; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 49 | FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV |
| 50 | FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 51 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Lei Zhang | b46a763 | 2019-01-09 02:56:16 +0000 | [diff] [blame] | 52 | if (!pDoc) |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 53 | return nullptr; |
| 54 | |
Tom Sepez | 6967ba9 | 2024-05-10 00:40:04 +0000 | [diff] [blame] | 55 | // SAFETY: required from caller. |
| 56 | WideString wsName = UNSAFE_BUFFERS(WideStringFromFPDFWideString(name)); |
Lei Zhang | b46a763 | 2019-01-09 02:56:16 +0000 | [diff] [blame] | 57 | if (wsName.IsEmpty()) |
| 58 | return nullptr; |
| 59 | |
Lei Zhang | 74694c1 | 2020-04-02 21:25:13 +0000 | [diff] [blame] | 60 | auto name_tree = |
| 61 | CPDF_NameTree::CreateWithRootNameArray(pDoc, "EmbeddedFiles"); |
| 62 | if (!name_tree) |
| 63 | return nullptr; |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 64 | |
| 65 | // Set up the basic entries in the filespec dictionary. |
Tom Sepez | 2c1b81c | 2022-09-20 23:57:45 +0000 | [diff] [blame] | 66 | auto pFile = pDoc->NewIndirect<CPDF_Dictionary>(); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 67 | pFile->SetNewFor<CPDF_Name>("Type", "Filespec"); |
Tom Sepez | 9e80262 | 2022-03-18 20:44:15 +0000 | [diff] [blame] | 68 | pFile->SetNewFor<CPDF_String>("UF", wsName.AsStringView()); |
| 69 | pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName.AsStringView()); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 70 | |
| 71 | // Add the new attachment name and filespec into the document's EmbeddedFiles. |
Lei Zhang | 74694c1 | 2020-04-02 21:25:13 +0000 | [diff] [blame] | 72 | if (!name_tree->AddValueAndName(pFile->MakeReference(pDoc), wsName)) |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 73 | return nullptr; |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 74 | |
Tom Sepez | 490d681 | 2022-11-04 23:25:37 +0000 | [diff] [blame] | 75 | // Unretained reference in public API. NOLINTNEXTLINE |
| 76 | return FPDFAttachmentFromCPDFObject(pFile); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 79 | FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV |
| 80 | FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index) { |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 81 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 82 | if (!pDoc || index < 0) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 83 | return nullptr; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 84 | |
Lei Zhang | 1575b47 | 2020-04-02 23:09:15 +0000 | [diff] [blame] | 85 | auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles"); |
| 86 | if (!name_tree || static_cast<size_t>(index) >= name_tree->GetCount()) |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 87 | return nullptr; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 88 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 89 | WideString csName; |
Tom Sepez | 490d681 | 2022-11-04 23:25:37 +0000 | [diff] [blame] | 90 | |
| 91 | // Unretained reference in public API. NOLINTNEXTLINE |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 92 | return FPDFAttachmentFromCPDFObject( |
Tom Sepez | 490d681 | 2022-11-04 23:25:37 +0000 | [diff] [blame] | 93 | name_tree->LookupValueAndName(index, &csName)); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 96 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 97 | FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index) { |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 98 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 99 | if (!pDoc || index < 0) |
| 100 | return false; |
| 101 | |
Lei Zhang | 1575b47 | 2020-04-02 23:09:15 +0000 | [diff] [blame] | 102 | auto name_tree = CPDF_NameTree::Create(pDoc, "EmbeddedFiles"); |
| 103 | if (!name_tree || static_cast<size_t>(index) >= name_tree->GetCount()) |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 104 | return false; |
| 105 | |
Lei Zhang | 1575b47 | 2020-04-02 23:09:15 +0000 | [diff] [blame] | 106 | return name_tree->DeleteValueAndName(index); |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 109 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 110 | FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, |
Lei Zhang | 960e295 | 2019-05-15 20:58:36 +0000 | [diff] [blame] | 111 | FPDF_WCHAR* buffer, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 112 | unsigned long buflen) { |
| 113 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 114 | if (!pFile) |
| 115 | return 0; |
| 116 | |
Tom Sepez | 3df809c | 2022-09-27 23:03:54 +0000 | [diff] [blame] | 117 | CPDF_FileSpec spec(pdfium::WrapRetain(pFile)); |
Tom Sepez | 946c0d3 | 2024-05-09 20:47:23 +0000 | [diff] [blame] | 118 | // SAFETY: required from caller. |
Tom Sepez | 6967ba9 | 2024-05-10 00:40:04 +0000 | [diff] [blame] | 119 | return Utf16EncodeMaybeCopyAndReturnLength( |
| 120 | spec.GetFileName(), UNSAFE_BUFFERS(SpanFromFPDFApiArgs(buffer, buflen))); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 121 | } |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 122 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 123 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 124 | FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 125 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
| 126 | if (!pFile) |
| 127 | return 0; |
| 128 | |
Tom Sepez | 3df809c | 2022-09-27 23:03:54 +0000 | [diff] [blame] | 129 | CPDF_FileSpec spec(pdfium::WrapRetain(pFile)); |
| 130 | RetainPtr<const CPDF_Dictionary> pParamsDict = spec.GetParamsDict(); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 131 | return pParamsDict ? pParamsDict->KeyExist(key) : 0; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 134 | FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 135 | FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 136 | if (!FPDFAttachment_HasKey(attachment, key)) |
| 137 | return FPDF_OBJECT_UNKNOWN; |
| 138 | |
Tom Sepez | 3df809c | 2022-09-27 23:03:54 +0000 | [diff] [blame] | 139 | CPDF_FileSpec spec( |
| 140 | pdfium::WrapRetain(CPDFObjectFromFPDFAttachment(attachment))); |
Tom Sepez | 3f90d43 | 2022-09-19 20:53:13 +0000 | [diff] [blame] | 141 | RetainPtr<const CPDF_Object> pObj = spec.GetParamsDict()->GetObjectFor(key); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 142 | return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 145 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 146 | FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 147 | FPDF_BYTESTRING key, |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 148 | FPDF_WIDESTRING value) { |
| 149 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
| 150 | if (!pFile) |
| 151 | return false; |
| 152 | |
Tom Sepez | 3df809c | 2022-09-27 23:03:54 +0000 | [diff] [blame] | 153 | CPDF_FileSpec spec(pdfium::WrapRetain(pFile)); |
| 154 | RetainPtr<CPDF_Dictionary> pParamsDict = spec.GetMutableParamsDict(); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 155 | if (!pParamsDict) |
| 156 | return false; |
| 157 | |
Tom Sepez | 6967ba9 | 2024-05-10 00:40:04 +0000 | [diff] [blame] | 158 | // SAFETY: required from caller. |
| 159 | ByteString bsValue = UNSAFE_BUFFERS(ByteStringFromFPDFWideString(value)); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 160 | ByteString bsKey = key; |
Lei Zhang | 295454b | 2024-05-22 02:10:33 +0000 | [diff] [blame] | 161 | if (bsKey == kChecksumKey) { |
| 162 | pParamsDict->SetNewFor<CPDF_String>(bsKey, |
| 163 | HexDecode(bsValue.unsigned_span()).data, |
| 164 | CPDF_String::DataType::kIsHex); |
| 165 | } else { |
| 166 | pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue); |
Tom Sepez | 6967ba9 | 2024-05-10 00:40:04 +0000 | [diff] [blame] | 167 | } |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 171 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 172 | FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 173 | FPDF_BYTESTRING key, |
Lei Zhang | 960e295 | 2019-05-15 20:58:36 +0000 | [diff] [blame] | 174 | FPDF_WCHAR* buffer, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 175 | unsigned long buflen) { |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 176 | CPDF_Object* file = CPDFObjectFromFPDFAttachment(attachment); |
| 177 | if (!file) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 178 | return 0; |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 179 | } |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 180 | |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 181 | CPDF_FileSpec spec(pdfium::WrapRetain(file)); |
| 182 | RetainPtr<const CPDF_Dictionary> params = spec.GetParamsDict(); |
| 183 | if (!params) { |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 184 | return 0; |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 185 | } |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 186 | |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 187 | // SAFETY: required from caller. |
| 188 | auto buffer_span = UNSAFE_BUFFERS(SpanFromFPDFApiArgs(buffer, buflen)); |
| 189 | |
| 190 | ByteString key_str = key; |
| 191 | RetainPtr<const CPDF_Object> object = params->GetObjectFor(key_str); |
| 192 | if (!object || (!object->IsString() && !object->IsName())) { |
| 193 | // Per API description, return an empty string in these cases. |
| 194 | return Utf16EncodeMaybeCopyAndReturnLength(WideString(), buffer_span); |
| 195 | } |
| 196 | |
| 197 | if (key_str == kChecksumKey) { |
| 198 | RetainPtr<const CPDF_String> string_object = ToString(object); |
| 199 | if (string_object && string_object->IsHex()) { |
Tom Sepez | 07819c4 | 2022-01-06 23:17:00 +0000 | [diff] [blame] | 200 | ByteString encoded = |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 201 | PDF_HexEncodeString(string_object->GetString().AsStringView()); |
| 202 | return Utf16EncodeMaybeCopyAndReturnLength( |
Lei Zhang | d4b014a | 2024-05-22 02:10:46 +0000 | [diff] [blame] | 203 | PDF_DecodeText(encoded.unsigned_span()), buffer_span); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
Lei Zhang | 4ce737f | 2024-05-22 02:10:39 +0000 | [diff] [blame] | 206 | |
| 207 | return Utf16EncodeMaybeCopyAndReturnLength(object->GetUnicodeText(), |
| 208 | buffer_span); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 211 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 212 | FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment, |
| 213 | FPDF_DOCUMENT document, |
| 214 | const void* contents, |
Lei Zhang | ca86e90 | 2020-06-09 22:12:34 +0000 | [diff] [blame] | 215 | unsigned long len) { |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 216 | // An empty content must have a zero length. |
| 217 | if (!contents && len != 0) { |
| 218 | return false; |
| 219 | } |
| 220 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 221 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
| 222 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 223 | if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 224 | return false; |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 225 | } |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 226 | |
| 227 | // Create a dictionary for the new embedded file stream. |
Tom Sepez | a3097da | 2019-05-01 16:42:36 +0000 | [diff] [blame] | 228 | auto pFileStreamDict = pdfium::MakeRetain<CPDF_Dictionary>(); |
Tom Sepez | 8aad22a | 2022-09-22 18:56:16 +0000 | [diff] [blame] | 229 | auto pParamsDict = pFileStreamDict->SetNewFor<CPDF_Dictionary>("Params"); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 230 | |
| 231 | // Set the size of the new file in the dictionary. |
Lei Zhang | 2617056 | 2018-04-17 17:01:52 +0000 | [diff] [blame] | 232 | pFileStreamDict->SetNewFor<CPDF_Number>(pdfium::stream::kDL, |
| 233 | static_cast<int>(len)); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 234 | pParamsDict->SetNewFor<CPDF_Number>("Size", static_cast<int>(len)); |
| 235 | |
| 236 | // Set the creation date of the new attachment in the dictionary. |
Dan Sinclair | ec2209d | 2017-11-16 22:08:27 +0000 | [diff] [blame] | 237 | CFX_DateTime dateTime = CFX_DateTime::Now(); |
Dan Sinclair | 1c4735a | 2017-11-16 22:08:07 +0000 | [diff] [blame] | 238 | pParamsDict->SetNewFor<CPDF_String>( |
| 239 | "CreationDate", |
| 240 | ByteString::Format("D:%d%02d%02d%02d%02d%02d", dateTime.GetYear(), |
| 241 | dateTime.GetMonth(), dateTime.GetDay(), |
| 242 | dateTime.GetHour(), dateTime.GetMinute(), |
Lei Zhang | 7d870c7 | 2024-05-21 16:44:46 +0000 | [diff] [blame] | 243 | dateTime.GetSecond())); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 244 | |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 245 | // SAFETY: required from caller. |
| 246 | pdfium::span<const uint8_t> contents_span = UNSAFE_BUFFERS( |
| 247 | pdfium::make_span(static_cast<const uint8_t*>(contents), len)); |
| 248 | |
Lei Zhang | 295454b | 2024-05-22 02:10:33 +0000 | [diff] [blame] | 249 | std::array<uint8_t, 16> digest; |
| 250 | CRYPT_MD5Generate(contents_span, digest.data()); |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 251 | |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 252 | // Set the checksum of the new attachment in the dictionary. |
Lei Zhang | 295454b | 2024-05-22 02:10:33 +0000 | [diff] [blame] | 253 | pParamsDict->SetNewFor<CPDF_String>(kChecksumKey, digest, |
| 254 | CPDF_String::DataType::kIsHex); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 255 | |
| 256 | // Create the file stream and have the filespec dictionary link to it. |
Lei Zhang | 6dc8a14 | 2022-10-20 23:08:15 +0000 | [diff] [blame] | 257 | auto pFileStream = pDoc->NewIndirect<CPDF_Stream>( |
Lei Zhang | 15e66ce | 2024-05-21 00:52:25 +0000 | [diff] [blame] | 258 | DataVector<uint8_t>(contents_span.begin(), contents_span.end()), |
Lei Zhang | 6dc8a14 | 2022-10-20 23:08:15 +0000 | [diff] [blame] | 259 | std::move(pFileStreamDict)); |
Tom Sepez | 946c0d3 | 2024-05-09 20:47:23 +0000 | [diff] [blame] | 260 | |
Tom Sepez | 8aad22a | 2022-09-22 18:56:16 +0000 | [diff] [blame] | 261 | auto pEFDict = pFile->AsMutableDictionary()->SetNewFor<CPDF_Dictionary>("EF"); |
Tom Sepez | f8c67a2 | 2019-05-06 17:09:15 +0000 | [diff] [blame] | 262 | pEFDict->SetNewFor<CPDF_Reference>("F", pDoc, pFileStream->GetObjNum()); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
Hui Yingst | 4414ee2 | 2020-06-10 20:58:19 +0000 | [diff] [blame] | 266 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 267 | FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment, |
| 268 | void* buffer, |
Hui Yingst | 4414ee2 | 2020-06-10 20:58:19 +0000 | [diff] [blame] | 269 | unsigned long buflen, |
| 270 | unsigned long* out_buflen) { |
| 271 | if (!out_buflen) |
| 272 | return false; |
| 273 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 274 | CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); |
| 275 | if (!pFile) |
Hui Yingst | 4414ee2 | 2020-06-10 20:58:19 +0000 | [diff] [blame] | 276 | return false; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 277 | |
Tom Sepez | 3df809c | 2022-09-27 23:03:54 +0000 | [diff] [blame] | 278 | CPDF_FileSpec spec(pdfium::WrapRetain(pFile)); |
| 279 | RetainPtr<const CPDF_Stream> pFileStream = spec.GetFileStream(); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 280 | if (!pFileStream) |
Hui Yingst | 4414ee2 | 2020-06-10 20:58:19 +0000 | [diff] [blame] | 281 | return false; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 282 | |
Tom Sepez | 3b679fe | 2024-04-09 18:49:32 +0000 | [diff] [blame] | 283 | // SAFETY: required from caller. |
Tom Sepez | 06943a9 | 2022-11-10 20:29:34 +0000 | [diff] [blame] | 284 | *out_buflen = DecodeStreamMaybeCopyAndReturnLength( |
| 285 | std::move(pFileStream), |
Tom Sepez | 3b679fe | 2024-04-09 18:49:32 +0000 | [diff] [blame] | 286 | UNSAFE_BUFFERS(pdfium::make_span(static_cast<uint8_t*>(buffer), |
| 287 | static_cast<size_t>(buflen)))); |
Hui Yingst | 4414ee2 | 2020-06-10 20:58:19 +0000 | [diff] [blame] | 288 | return true; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 289 | } |