5#include "public/fpdf_attachment.h"
12#include "constants/stream_dict_common.h"
13#include "core/fdrm/fx_crypt.h"
14#include "core/fpdfapi/parser/cpdf_array.h"
15#include "core/fpdfapi/parser/cpdf_dictionary.h"
16#include "core/fpdfapi/parser/cpdf_document.h"
17#include "core/fpdfapi/parser/cpdf_name.h"
18#include "core/fpdfapi/parser/cpdf_number.h"
19#include "core/fpdfapi/parser/cpdf_reference.h"
20#include "core/fpdfapi/parser/cpdf_stream.h"
21#include "core/fpdfapi/parser/cpdf_string.h"
22#include "core/fpdfapi/parser/fpdf_parser_decode.h"
23#include "core/fpdfdoc/cpdf_filespec.h"
24#include "core/fpdfdoc/cpdf_nametree.h"
25#include "core/fxcrt/cfx_datetime.h"
26#include "core/fxcrt/data_vector.h"
27#include "core/fxcrt/fx_extension.h"
28#include "core/fxcrt/fx_memory_wrappers.h"
29#include "fpdfsdk/cpdfsdk_helpers.h"
30#include "third_party/base/numerics/safe_conversions.h"
34constexpr char kChecksumKey[] =
"CheckSum";
36ByteString CFXByteStringHexDecode(
const ByteString& bsHex) {
39 HexDecode(bsHex.raw_span(), &result, &size);
40 return ByteString(result.get(), size);
43ByteString GenerateMD5Base16(
const void* contents,
const unsigned long len) {
45 CRYPT_MD5Generate({
static_cast<
const uint8_t*>(contents), len}, digest);
47 for (
int i = 0; i < 16; ++i)
50 return ByteString(buf, 32);
61 auto name_tree = CPDF_NameTree::Create(pDoc,
"EmbeddedFiles");
62 return name_tree ? pdfium::base::checked_cast<
int>(name_tree->GetCount()) : 0;
76 CPDF_NameTree::CreateWithRootNameArray(pDoc,
"EmbeddedFiles");
81 auto pFile = pDoc->NewIndirect<CPDF_Dictionary>();
82 pFile->SetNewFor<CPDF_Name>(
"Type",
"Filespec");
83 pFile->SetNewFor<CPDF_String>(
"UF", wsName.AsStringView());
84 pFile->SetNewFor<CPDF_String>(pdfium
::stream::kF, wsName.AsStringView());
87 if (!name_tree->AddValueAndName(pFile->MakeReference(pDoc), wsName))
91 return FPDFAttachmentFromCPDFObject(pFile);
97 if (!pDoc || index < 0)
100 auto name_tree = CPDF_NameTree::Create(pDoc,
"EmbeddedFiles");
101 if (!name_tree ||
static_cast<size_t>(index) >= name_tree->GetCount())
107 return FPDFAttachmentFromCPDFObject(
108 name_tree->LookupValueAndName(index, &csName));
114 if (!pDoc || index < 0)
117 auto name_tree = CPDF_NameTree::Create(pDoc,
"EmbeddedFiles");
118 if (!name_tree ||
static_cast<size_t>(index) >= name_tree->GetCount())
121 return name_tree->DeleteValueAndName(index);
127 unsigned long buflen) {
144 RetainPtr<
const CPDF_Dictionary> pParamsDict = spec.GetParamsDict();
145 return pParamsDict ? pParamsDict->KeyExist(key) : 0;
162 FPDF_WIDESTRING value) {
168 RetainPtr<CPDF_Dictionary> pParamsDict = spec.GetMutableParamsDict();
172 ByteString bsKey = key;
174 bool bEncodedAsHex = bsKey
== kChecksumKey;
176 bsValue
= CFXByteStringHexDecode(bsValue);
178 pParamsDict->SetNewFor<CPDF_String>(bsKey, bsValue, bEncodedAsHex);
186 unsigned long buflen) {
192 RetainPtr<
const CPDF_Dictionary> pParamsDict = spec.GetParamsDict();
196 ByteString bsKey = key;
197 WideString value = pParamsDict->GetUnicodeTextFor(bsKey);
199 const CPDF_String* stringValue =
200 pParamsDict->GetObjectFor(bsKey)->AsString();
203 PDF_HexEncodeString(stringValue
->GetString().AsStringView());
204 value = pdfium::MakeRetain<CPDF_String>(
nullptr, encoded,
false)
214 FPDF_DOCUMENT document,
215 const void* contents,
219 if (!pFile || !pFile->IsDictionary() || !pDoc || len > INT_MAX)
223 if (!contents && len != 0)
227 auto pFileStreamDict = pdfium::MakeRetain<CPDF_Dictionary>();
228 auto pParamsDict = pFileStreamDict->SetNewFor<CPDF_Dictionary>(
"Params");
232 static_cast<
int>(len));
233 pParamsDict->SetNewFor<CPDF_Number>(
"Size",
static_cast<
int>(len));
237 pParamsDict->SetNewFor<CPDF_String>(
246 pParamsDict->SetNewFor<CPDF_String>(
247 kChecksumKey, CFXByteStringHexDecode(GenerateMD5Base16(contents, len)),
251 const uint8_t* contents_as_bytes =
static_cast<
const uint8_t*>(contents);
252 auto pFileStream = pDoc->NewIndirect<CPDF_Stream>(
253 DataVector<uint8_t>(contents_as_bytes, contents_as_bytes + len),
254 std::move(pFileStreamDict));
256 pEFDict->SetNewFor<CPDF_Reference>(
"F", pDoc, pFileStream->GetObjNum());
263 unsigned long buflen,
264 unsigned long* out_buflen) {
273 RetainPtr<
const CPDF_Stream> pFileStream = spec.GetFileStream();
277 *out_buflen = DecodeStreamMaybeCopyAndReturnLength(
278 std::move(pFileStream),
279 {
static_cast<uint8_t*>(buffer),
static_cast<size_t>(buflen)});
static CFX_DateTime Now()
uint8_t GetSecond() const
uint8_t GetMinute() const
WideString GetFileName() const
virtual CPDF_Dictionary * AsMutableDictionary()
ByteString GetString() const override
static ByteString Format(const char *pFormat,...)
bool operator==(const char *ptr) const
ByteString & operator=(ByteString &&that) noexcept
WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString &text, void *buffer, unsigned long buflen)
CPDF_Object * CPDFObjectFromFPDFAttachment(FPDF_ATTACHMENT attachment)
CPDF_Document * CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc)
FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment, FPDF_DOCUMENT document, const void *contents, unsigned long len)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT int FPDF_CALLCONV FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, FPDF_WCHAR *buffer, unsigned long buflen)
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFDoc_DeleteAttachment(FPDF_DOCUMENT document, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key, FPDF_WIDESTRING value)
FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV FPDFDoc_GetAttachment(FPDF_DOCUMENT document, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_GetFile(FPDF_ATTACHMENT attachment, void *buffer, unsigned long buflen, unsigned long *out_buflen)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key)
#define FPDF_OBJECT_UNKNOWN
void FXSYS_IntToTwoHexChars(uint8_t n, char *buf)