From df064df7a08e008b3c8e4d56bb0b75da9f014147 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 31 Aug 2017 02:33:27 -0700 Subject: Change APIs to use FPDF_BYTESTRING for keys. Change-Id: I865a9eeb197ea2c1f5480cae32d975909495676d Reviewed-on: https://pdfium-review.googlesource.com/12551 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- fpdfsdk/fpdfannot.cpp | 40 +++++------- fpdfsdk/fpdfannot_embeddertest.cpp | 112 +++++++++++++------------------- fpdfsdk/fpdfattachment.cpp | 41 +++++------- fpdfsdk/fpdfattachment_embeddertest.cpp | 54 +++++++-------- public/fpdf_annot.h | 10 +-- public/fpdf_attachment.h | 8 +-- samples/pdfium_test.cc | 14 ++-- 7 files changed, 114 insertions(+), 165 deletions(-) diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index d6080fd35f..95f0f44af9 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -689,7 +689,7 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetRect(FPDF_ANNOTATION annot, } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key) { + FPDF_BYTESTRING key) { if (!annot) return false; @@ -698,26 +698,22 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot, if (!pAnnotDict) return false; - return pAnnotDict->KeyExist(CFXByteStringFromFPDFWideString(key)); + return pAnnotDict->KeyExist(key); } FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV -FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_WIDESTRING key) { +FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_BYTESTRING key) { if (!FPDFAnnot_HasKey(annot, key)) return FPDF_OBJECT_UNKNOWN; - CPDF_Object* pObj = - CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict()->GetObjectFor( - CFXByteStringFromFPDFWideString(key)); - if (!pObj) - return FPDF_OBJECT_UNKNOWN; - - return pObj->GetType(); + auto* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot); + CPDF_Object* pObj = pAnnot->GetAnnotDict()->GetObjectFor(key); + return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN; } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, FPDF_WIDESTRING value) { if (!annot) return false; @@ -727,15 +723,14 @@ FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot, if (!pAnnotDict) return false; - pAnnotDict->SetNewFor(CFXByteStringFromFPDFWideString(key), - CFXByteStringFromFPDFWideString(value), - false); + pAnnotDict->SetNewFor( + key, CFXByteStringFromFPDFWideString(value), false); return true; } FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, void* buffer, unsigned long buflen) { if (!annot) @@ -746,19 +741,17 @@ FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, if (!pAnnotDict) return 0; - return Utf16EncodeMaybeCopyAndReturnLength( - pAnnotDict->GetUnicodeTextFor(CFXByteStringFromFPDFWideString(key)), - buffer, buflen); + return Utf16EncodeMaybeCopyAndReturnLength(pAnnotDict->GetUnicodeTextFor(key), + buffer, buflen); } FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV -FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_WIDESTRING key) { +FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_BYTESTRING key) { CPDF_AnnotContext* pAnnot = CPDFAnnotContextFromFPDFAnnotation(annot); if (!pAnnot || !pAnnot->GetAnnotDict()) return nullptr; - CPDF_Dictionary* pLinkedDict = - pAnnot->GetAnnotDict()->GetDictFor(CFXByteStringFromFPDFWideString(key)); + CPDF_Dictionary* pLinkedDict = pAnnot->GetAnnotDict()->GetDictFor(key); if (!pLinkedDict || pLinkedDict->GetStringFor("Type") != "Annot") return nullptr; @@ -773,10 +766,7 @@ FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetFlags(FPDF_ANNOTATION annot) { CPDF_Dictionary* pAnnotDict = CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); - if (!pAnnotDict) - return FPDF_ANNOT_FLAG_NONE; - - return pAnnotDict->GetIntegerFor("F"); + return pAnnotDict ? pAnnotDict->GetIntegerFor("F") : FPDF_ANNOT_FLAG_NONE; } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetFlags(FPDF_ANNOTATION annot, diff --git a/fpdfsdk/fpdfannot_embeddertest.cpp b/fpdfsdk/fpdfannot_embeddertest.cpp index 6c0cdedab5..a2127b299c 100644 --- a/fpdfsdk/fpdfannot_embeddertest.cpp +++ b/fpdfsdk/fpdfannot_embeddertest.cpp @@ -13,6 +13,8 @@ #include "testing/embedder_test.h" #include "testing/gtest/include/gtest/gtest.h" +static constexpr char kContentsKey[] = "Contents"; + class FPDFAnnotEmbeddertest : public EmbedderTest {}; TEST_F(FPDFAnnotEmbeddertest, RenderAnnotWithOnlyRolloverAP) { @@ -59,29 +61,22 @@ TEST_F(FPDFAnnotEmbeddertest, ExtractHighlightLongContent) { EXPECT_EQ(255u, A); // Check that the author is correct. - std::unique_ptr author_key = - GetFPDFWideString(L"T"); - EXPECT_EQ(FPDF_OBJECT_STRING, - FPDFAnnot_GetValueType(annot, author_key.get())); - unsigned long len = - FPDFAnnot_GetStringValue(annot, author_key.get(), nullptr, 0); + static constexpr char kAuthorKey[] = "T"; + EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kAuthorKey)); + unsigned long len = FPDFAnnot_GetStringValue(annot, kAuthorKey, nullptr, 0); std::vector buf(len); - EXPECT_EQ(28u, - FPDFAnnot_GetStringValue(annot, author_key.get(), buf.data(), len)); + EXPECT_EQ(28u, FPDFAnnot_GetStringValue(annot, kAuthorKey, buf.data(), len)); EXPECT_STREQ(L"Jae Hyun Park", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); // Check that the content is correct. - std::unique_ptr contents_key = - GetFPDFWideString(L"Contents"); - EXPECT_EQ(FPDF_OBJECT_STRING, - FPDFAnnot_GetValueType(annot, contents_key.get())); - len = FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0); + EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(annot, kContentsKey)); + len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(2690u, FPDFAnnot_GetStringValue(annot, contents_key.get(), - buf.data(), len)); + EXPECT_EQ(2690u, + FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len)); const wchar_t contents[] = L"This is a note for that highlight annotation. Very long highlight " "annotation. Long long long Long long longLong long longLong long " @@ -146,10 +141,7 @@ TEST_F(FPDFAnnotEmbeddertest, ExtractInkMultiple) { EXPECT_EQ(76u, A); // Check that there is no content. - std::unique_ptr contents_key = - GetFPDFWideString(L"Contents"); - EXPECT_EQ(2u, - FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0)); + EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0)); // Check that the rectange coordinates are correct. // Note that upon rendering, the rectangle coordinates will be adjusted. @@ -242,18 +234,15 @@ TEST_F(FPDFAnnotEmbeddertest, AddFirstTextAnnotation) { EXPECT_EQ(165.f, rect.top); // Set the content of the annotation. - std::unique_ptr contents_key = - GetFPDFWideString(L"Contents"); - const wchar_t contents[] = L"Hello! This is a customized content."; + static constexpr wchar_t contents[] = L"Hello! This is a customized content."; std::unique_ptr text = GetFPDFWideString(contents); - ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, contents_key.get(), text.get())); + ASSERT_TRUE(FPDFAnnot_SetStringValue(annot, kContentsKey, text.get())); // Check that the content has been set correctly. - unsigned long len = - FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0); + unsigned long len = FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0); std::vector buf(len); - EXPECT_EQ(74u, FPDFAnnot_GetStringValue(annot, contents_key.get(), buf.data(), - len)); + EXPECT_EQ(74u, + FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len)); EXPECT_STREQ(contents, GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); @@ -841,37 +830,31 @@ TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) { ASSERT_TRUE(annot); // Check that a non-existent key does not exist. - EXPECT_FALSE(FPDFAnnot_HasKey(annot, GetFPDFWideString(L"none").get())); + EXPECT_FALSE(FPDFAnnot_HasKey(annot, "none")); // Check that the string value of a non-string dictionary entry is empty. - std::unique_ptr ap_key = - GetFPDFWideString(L"AP"); - EXPECT_TRUE(FPDFAnnot_HasKey(annot, ap_key.get())); - EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, ap_key.get())); - EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, ap_key.get(), nullptr, 0)); + static constexpr char kApKey[] = "AP"; + EXPECT_TRUE(FPDFAnnot_HasKey(annot, kApKey)); + EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kApKey)); + EXPECT_EQ(2u, FPDFAnnot_GetStringValue(annot, kApKey, nullptr, 0)); // Check that the string value of the hash is correct. - std::unique_ptr hash_key = - GetFPDFWideString(L"AAPL:Hash"); - EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get())); - unsigned long len = - FPDFAnnot_GetStringValue(annot, hash_key.get(), nullptr, 0); + static constexpr char kHashKey[] = "AAPL:Hash"; + EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey)); + unsigned long len = FPDFAnnot_GetStringValue(annot, kHashKey, nullptr, 0); std::vector buf(len); - EXPECT_EQ(66u, - FPDFAnnot_GetStringValue(annot, hash_key.get(), buf.data(), len)); + EXPECT_EQ(66u, FPDFAnnot_GetStringValue(annot, kHashKey, buf.data(), len)); EXPECT_STREQ(L"395fbcb98d558681742f30683a62a2ad", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); // Check that the string value of the modified date is correct. - std::unique_ptr date_key = - GetFPDFWideString(L"M"); - EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, hash_key.get())); - len = FPDFAnnot_GetStringValue(annot, date_key.get(), nullptr, 0); + static constexpr char kDateKey[] = "M"; + EXPECT_EQ(FPDF_OBJECT_NAME, FPDFAnnot_GetValueType(annot, kHashKey)); + len = FPDFAnnot_GetStringValue(annot, kDateKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(44u, - FPDFAnnot_GetStringValue(annot, date_key.get(), buf.data(), len)); + EXPECT_EQ(44u, FPDFAnnot_GetStringValue(annot, kDateKey, buf.data(), len)); EXPECT_STREQ(L"D:201706071721Z00'00'", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); @@ -880,7 +863,7 @@ TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) { const wchar_t new_date[] = L"D:201706282359Z00'00'"; std::unique_ptr text = GetFPDFWideString(new_date); - EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, date_key.get(), text.get())); + EXPECT_TRUE(FPDFAnnot_SetStringValue(annot, kDateKey, text.get())); // Save the document, closing the page and document. FPDFPage_CloseAnnot(annot); @@ -899,13 +882,12 @@ TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) { FPDF_ANNOTATION new_annot = FPDFPage_GetAnnot(m_SavedPage, 0); // Check that the string value of the modified date is the newly-set value. - EXPECT_EQ(FPDF_OBJECT_STRING, - FPDFAnnot_GetValueType(new_annot, date_key.get())); - len = FPDFAnnot_GetStringValue(new_annot, date_key.get(), nullptr, 0); + EXPECT_EQ(FPDF_OBJECT_STRING, FPDFAnnot_GetValueType(new_annot, kDateKey)); + len = FPDFAnnot_GetStringValue(new_annot, kDateKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(44u, FPDFAnnot_GetStringValue(new_annot, date_key.get(), buf.data(), - len)); + EXPECT_EQ(44u, + FPDFAnnot_GetStringValue(new_annot, kDateKey, buf.data(), len)); EXPECT_STREQ(new_date, GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); @@ -926,14 +908,12 @@ TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) { ASSERT_TRUE(annot); EXPECT_EQ(FPDF_ANNOT_HIGHLIGHT, FPDFAnnot_GetSubtype(annot)); EXPECT_EQ(0, FPDFPage_GetAnnotIndex(page, annot)); - std::unique_ptr popup_key = - GetFPDFWideString(L"Popup"); - ASSERT_TRUE(FPDFAnnot_HasKey(annot, popup_key.get())); - ASSERT_EQ(FPDF_OBJECT_REFERENCE, - FPDFAnnot_GetValueType(annot, popup_key.get())); + static constexpr char kPopupKey[] = "Popup"; + ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPopupKey)); + ASSERT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPopupKey)); // Retrieve and verify the popup of the highlight annotation. - FPDF_ANNOTATION popup = FPDFAnnot_GetLinkedAnnot(annot, popup_key.get()); + FPDF_ANNOTATION popup = FPDFAnnot_GetLinkedAnnot(annot, kPopupKey); ASSERT_TRUE(popup); EXPECT_EQ(FPDF_ANNOT_POPUP, FPDFAnnot_GetSubtype(popup)); EXPECT_EQ(1, FPDFPage_GetAnnotIndex(page, popup)); @@ -944,18 +924,16 @@ TEST_F(FPDFAnnotEmbeddertest, ExtractLinkedAnnotations) { // Attempting to retrieve |annot|'s "IRT"-linked annotation would fail, since // "IRT" is not a key in |annot|'s dictionary. - std::unique_ptr irt_key = - GetFPDFWideString(L"IRT"); - ASSERT_FALSE(FPDFAnnot_HasKey(annot, irt_key.get())); - EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, irt_key.get())); + static constexpr char kIRTKey[] = "IRT"; + ASSERT_FALSE(FPDFAnnot_HasKey(annot, kIRTKey)); + EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kIRTKey)); // Attempting to retrieve |annot|'s parent dictionary as an annotation would // fail, since its parent is not an annotation. - std::unique_ptr p_key = - GetFPDFWideString(L"P"); - ASSERT_TRUE(FPDFAnnot_HasKey(annot, p_key.get())); - EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, p_key.get())); - EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, p_key.get())); + static constexpr char kPKey[] = "P"; + ASSERT_TRUE(FPDFAnnot_HasKey(annot, kPKey)); + EXPECT_EQ(FPDF_OBJECT_REFERENCE, FPDFAnnot_GetValueType(annot, kPKey)); + EXPECT_FALSE(FPDFAnnot_GetLinkedAnnot(annot, kPKey)); FPDFPage_CloseAnnot(popup); FPDFPage_CloseAnnot(annot); diff --git a/fpdfsdk/fpdfattachment.cpp b/fpdfsdk/fpdfattachment.cpp index b759ae2655..d984cf8910 100644 --- a/fpdfsdk/fpdfattachment.cpp +++ b/fpdfsdk/fpdfattachment.cpp @@ -23,6 +23,8 @@ namespace { +constexpr char kChecksumKey[] = "CheckSum"; + CFX_ByteString CFXByteStringHexDecode(const CFX_ByteString& bsHex) { uint8_t* result = nullptr; uint32_t size = 0; @@ -138,35 +140,28 @@ FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV -FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key) { +FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) { CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); if (!pFile) return 0; CPDF_Dictionary* pParamsDict = CPDF_FileSpec(pFile).GetParamsDict(); - if (!pParamsDict) - return 0; - - return pParamsDict->KeyExist(CFXByteStringFromFPDFWideString(key)); + return pParamsDict ? pParamsDict->KeyExist(key) : 0; } FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV -FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key) { +FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key) { if (!FPDFAttachment_HasKey(attachment, key)) return FPDF_OBJECT_UNKNOWN; - CPDF_Object* pObj = CPDF_FileSpec(CPDFObjectFromFPDFAttachment(attachment)) - .GetParamsDict() - ->GetObjectFor(CFXByteStringFromFPDFWideString(key)); - if (!pObj) - return FPDF_OBJECT_UNKNOWN; - - return pObj->GetType(); + CPDF_FileSpec spec(CPDFObjectFromFPDFAttachment(attachment)); + CPDF_Object* pObj = spec.GetParamsDict()->GetObjectFor(key); + return pObj ? pObj->GetType() : FPDF_OBJECT_UNKNOWN; } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, FPDF_WIDESTRING value) { CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); if (!pFile) @@ -176,9 +171,9 @@ FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, if (!pParamsDict) return false; - CFX_ByteString bsKey = CFXByteStringFromFPDFWideString(key); + CFX_ByteString bsKey = key; CFX_ByteString bsValue = CFXByteStringFromFPDFWideString(value); - bool bEncodedAsHex = bsKey == "CheckSum"; + bool bEncodedAsHex = bsKey == kChecksumKey; if (bEncodedAsHex) bsValue = CFXByteStringHexDecode(bsValue); @@ -188,7 +183,7 @@ FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, void* buffer, unsigned long buflen) { CPDF_Object* pFile = CPDFObjectFromFPDFAttachment(attachment); @@ -199,15 +194,13 @@ FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, if (!pParamsDict) return 0; - CFX_ByteString bsKey = CFXByteStringFromFPDFWideString(key); + CFX_ByteString bsKey = key; CFX_WideString value = pParamsDict->GetUnicodeTextFor(bsKey); - if (bsKey == "CheckSum" && !value.IsEmpty()) { + if (bsKey == kChecksumKey && !value.IsEmpty()) { CPDF_String* stringValue = pParamsDict->GetObjectFor(bsKey)->AsString(); if (stringValue->IsHex()) { - value = - CPDF_String(nullptr, PDF_EncodeString(stringValue->GetString(), true), - false) - .GetUnicodeText(); + CFX_ByteString encoded = PDF_EncodeString(stringValue->GetString(), true); + value = CPDF_String(nullptr, encoded, false).GetUnicodeText(); } } @@ -248,7 +241,7 @@ FPDFAttachment_SetFile(FPDF_ATTACHMENT attachment, // Set the checksum of the new attachment in the dictionary. pParamsDict->SetNewFor( - "CheckSum", CFXByteStringHexDecode(GenerateMD5Base16(contents, len)), + kChecksumKey, CFXByteStringHexDecode(GenerateMD5Base16(contents, len)), true); // Create the file stream and have the filespec dictionary link to it. diff --git a/fpdfsdk/fpdfattachment_embeddertest.cpp b/fpdfsdk/fpdfattachment_embeddertest.cpp index 74b3d7b469..dd9b5aebf3 100644 --- a/fpdfsdk/fpdfattachment_embeddertest.cpp +++ b/fpdfsdk/fpdfattachment_embeddertest.cpp @@ -10,6 +10,9 @@ #include "public/fpdfview.h" #include "testing/embedder_test.h" +static constexpr char kDateKey[] = "CreationDate"; +static constexpr char kChecksumKey[] = "CheckSum"; + class FPDFAttachmentEmbeddertest : public EmbedderTest {}; TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) { @@ -37,25 +40,21 @@ TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) { EXPECT_EQ(std::string("test"), std::string(buf.data(), 4)); // Check that a non-existent key does not exist. - EXPECT_FALSE( - FPDFAttachment_HasKey(attachment, GetFPDFWideString(L"none").get())); + EXPECT_FALSE(FPDFAttachment_HasKey(attachment, "none")); // Check that the string value of a non-string dictionary entry is empty. - std::unique_ptr size_key = - GetFPDFWideString(L"Size"); + static constexpr char kSizeKey[] = "Size"; EXPECT_EQ(FPDF_OBJECT_NUMBER, - FPDFAttachment_GetValueType(attachment, size_key.get())); - EXPECT_EQ(2u, FPDFAttachment_GetStringValue(attachment, size_key.get(), - nullptr, 0)); + FPDFAttachment_GetValueType(attachment, kSizeKey)); + EXPECT_EQ(2u, + FPDFAttachment_GetStringValue(attachment, kSizeKey, nullptr, 0)); // Check that the creation date of the first attachment is correct. - std::unique_ptr date_key = - GetFPDFWideString(L"CreationDate"); - len = FPDFAttachment_GetStringValue(attachment, date_key.get(), nullptr, 0); + len = FPDFAttachment_GetStringValue(attachment, kDateKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, date_key.get(), - buf.data(), len)); + EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, kDateKey, buf.data(), + len)); EXPECT_STREQ(L"D:20170712214438-07'00'", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); @@ -78,13 +77,10 @@ TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) { EXPECT_EQ(kCheckSum, generated_checksum); // Check that the stored checksum matches expectation. - std::unique_ptr checksum_key = - GetFPDFWideString(L"CheckSum"); - len = - FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0); + len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(), + EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, buf.data(), len)); EXPECT_EQ(kCheckSumW, GetPlatformWString(reinterpret_cast(buf.data()))); @@ -171,21 +167,17 @@ TEST_F(FPDFAttachmentEmbeddertest, AddAttachmentsWithParams) { strlen(kContents))); // Set the date to be an arbitrary value. - std::unique_ptr date_key = - GetFPDFWideString(L"CreationDate"); constexpr wchar_t kDateW[] = L"D:20170720161527-04'00'"; std::unique_ptr ws_date = GetFPDFWideString(kDateW); EXPECT_TRUE( - FPDFAttachment_SetStringValue(attachment, date_key.get(), ws_date.get())); + FPDFAttachment_SetStringValue(attachment, kDateKey, ws_date.get())); // Set the checksum to be an arbitrary value. - std::unique_ptr checksum_key = - GetFPDFWideString(L"CheckSum"); constexpr wchar_t kCheckSumW[] = L""; std::unique_ptr ws_checksum = GetFPDFWideString(kCheckSumW); - EXPECT_TRUE(FPDFAttachment_SetStringValue(attachment, checksum_key.get(), + EXPECT_TRUE(FPDFAttachment_SetStringValue(attachment, kChecksumKey, ws_checksum.get())); // Verify the name of the new attachment (i.e. the second attachment). @@ -206,21 +198,20 @@ TEST_F(FPDFAttachmentEmbeddertest, AddAttachmentsWithParams) { EXPECT_EQ(std::string(kContents), std::string(buf.data(), 12)); // Verify the creation date of the new attachment. - len = FPDFAttachment_GetStringValue(attachment, date_key.get(), nullptr, 0); + len = FPDFAttachment_GetStringValue(attachment, kDateKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, date_key.get(), - buf.data(), len)); + EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, kDateKey, buf.data(), + len)); EXPECT_STREQ(kDateW, GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); // Verify the checksum of the new attachment. - len = - FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0); + len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(), + EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, buf.data(), len)); EXPECT_STREQ(kCheckSumW, GetPlatformWString(reinterpret_cast(buf.data())) @@ -230,11 +221,10 @@ TEST_F(FPDFAttachmentEmbeddertest, AddAttachmentsWithParams) { // gets updated to the correct value. EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), nullptr, 0)); EXPECT_EQ(0u, FPDFAttachment_GetFile(attachment, nullptr, 0)); - len = - FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0); + len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); buf.clear(); buf.resize(len); - EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(), + EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, buf.data(), len)); EXPECT_EQ(L"", GetPlatformWString(reinterpret_cast(buf.data()))); diff --git a/public/fpdf_annot.h b/public/fpdf_annot.h index 429a3f7541..b92763855b 100644 --- a/public/fpdf_annot.h +++ b/public/fpdf_annot.h @@ -347,7 +347,7 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_GetRect(FPDF_ANNOTATION annot, // // Returns true if |key| exists. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key); + FPDF_BYTESTRING key); // Experimental API. // Get the type of the value corresponding to |key| in |annot|'s dictionary. @@ -357,7 +357,7 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_HasKey(FPDF_ANNOTATION annot, // // Returns the type of the dictionary value. FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV -FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_WIDESTRING key); +FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_BYTESTRING key); // Experimental API. // Set the string value corresponding to |key| in |annot|'s dictionary, @@ -371,7 +371,7 @@ FPDFAnnot_GetValueType(FPDF_ANNOTATION annot, FPDF_WIDESTRING key); // Returns true if successful. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, FPDF_WIDESTRING value); // Experimental API. @@ -391,7 +391,7 @@ FPDFAnnot_SetStringValue(FPDF_ANNOTATION annot, // Returns the length of the string value. FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, void* buffer, unsigned long buflen); @@ -406,7 +406,7 @@ FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, // // Returns a handle to the linked annotation object, or NULL on failure. FPDF_EXPORT FPDF_ANNOTATION FPDF_CALLCONV -FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_WIDESTRING key); +FPDFAnnot_GetLinkedAnnot(FPDF_ANNOTATION annot, FPDF_BYTESTRING key); // Experimental API. // Get the annotation flags of |annot|. diff --git a/public/fpdf_attachment.h b/public/fpdf_attachment.h index 5a7ab6a722..411ee219af 100644 --- a/public/fpdf_attachment.h +++ b/public/fpdf_attachment.h @@ -81,7 +81,7 @@ FPDFAttachment_GetName(FPDF_ATTACHMENT attachment, // // Returns true if |key| exists. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV -FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key); +FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key); // Experimental API. // Get the type of the value corresponding to |key| in the params dictionary of @@ -92,7 +92,7 @@ FPDFAttachment_HasKey(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key); // // Returns the type of the dictionary value. FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV -FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key); +FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_BYTESTRING key); // Experimental API. // Set the string value corresponding to |key| in the params dictionary of the @@ -106,7 +106,7 @@ FPDFAttachment_GetValueType(FPDF_ATTACHMENT attachment, FPDF_WIDESTRING key); // Returns true if successful. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, FPDF_WIDESTRING value); // Experimental API. @@ -127,7 +127,7 @@ FPDFAttachment_SetStringValue(FPDF_ATTACHMENT attachment, // Returns the length of the dictionary value string. FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAttachment_GetStringValue(FPDF_ATTACHMENT attachment, - FPDF_WIDESTRING key, + FPDF_BYTESTRING key, void* buffer, unsigned long buflen); diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index b94b674286..40f12a318b 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -395,21 +395,19 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { } // Retrieve the annotation's contents and author. - std::unique_ptr contents_key = - GetFPDFWideString(L"Contents"); + static constexpr char kContentsKey[] = "Contents"; + static constexpr char kAuthorKey[] = "T"; unsigned long len = - FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0); + FPDFAnnot_GetStringValue(annot, kContentsKey, nullptr, 0); std::vector buf(len); - FPDFAnnot_GetStringValue(annot, contents_key.get(), buf.data(), len); + FPDFAnnot_GetStringValue(annot, kContentsKey, buf.data(), len); fprintf(fp, "Content: %ls\n", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); - std::unique_ptr author_key = - GetFPDFWideString(L"T"); - len = FPDFAnnot_GetStringValue(annot, author_key.get(), nullptr, 0); + len = FPDFAnnot_GetStringValue(annot, kAuthorKey, nullptr, 0); buf.clear(); buf.resize(len); - FPDFAnnot_GetStringValue(annot, author_key.get(), buf.data(), len); + FPDFAnnot_GetStringValue(annot, kAuthorKey, buf.data(), len); fprintf(fp, "Author: %ls\n", GetPlatformWString(reinterpret_cast(buf.data())) .c_str()); -- cgit v1.2.3