summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2017-08-31 02:33:27 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-08-31 16:41:19 +0000
commitdf064df7a08e008b3c8e4d56bb0b75da9f014147 (patch)
tree17f7244a29424c5e61460ed672b2f25df1278b14 /fpdfsdk
parent63b012652da22f75172895a975ea7932e63dcc81 (diff)
downloadpdfium-df064df7a08e008b3c8e4d56bb0b75da9f014147.tar.xz
Change APIs to use FPDF_BYTESTRING for keys.
Change-Id: I865a9eeb197ea2c1f5480cae32d975909495676d Reviewed-on: https://pdfium-review.googlesource.com/12551 Reviewed-by: dsinclair <dsinclair@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdfannot.cpp40
-rw-r--r--fpdfsdk/fpdfannot_embeddertest.cpp112
-rw-r--r--fpdfsdk/fpdfattachment.cpp41
-rw-r--r--fpdfsdk/fpdfattachment_embeddertest.cpp54
4 files changed, 99 insertions, 148 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<CPDF_String>(CFXByteStringFromFPDFWideString(key),
- CFXByteStringFromFPDFWideString(value),
- false);
+ pAnnotDict->SetNewFor<CPDF_String>(
+ 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<unsigned short, pdfium::FreeDeleter> 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<char> 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<unsigned short*>(buf.data()))
.c_str());
// Check that the content is correct.
- std::unique_ptr<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<char> 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<unsigned short*>(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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<char> 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<unsigned short*>(buf.data()))
.c_str());
// Check that the string value of the modified date is correct.
- std::unique_ptr<unsigned short, pdfium::FreeDeleter> 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<unsigned short*>(buf.data()))
.c_str());
@@ -880,7 +863,7 @@ TEST_F(FPDFAnnotEmbeddertest, GetSetStringValue) {
const wchar_t new_date[] = L"D:201706282359Z00'00'";
std::unique_ptr<unsigned short, pdfium::FreeDeleter> 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<unsigned short*>(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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<CPDF_String>(
- "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<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> 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<unsigned short*>(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<unsigned short, pdfium::FreeDeleter> 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<unsigned short*>(buf.data())));
@@ -171,21 +167,17 @@ TEST_F(FPDFAttachmentEmbeddertest, AddAttachmentsWithParams) {
strlen(kContents)));
// Set the date to be an arbitrary value.
- std::unique_ptr<unsigned short, pdfium::FreeDeleter> date_key =
- GetFPDFWideString(L"CreationDate");
constexpr wchar_t kDateW[] = L"D:20170720161527-04'00'";
std::unique_ptr<unsigned short, pdfium::FreeDeleter> 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<unsigned short, pdfium::FreeDeleter> checksum_key =
- GetFPDFWideString(L"CheckSum");
constexpr wchar_t kCheckSumW[] = L"<ABCDEF01234567899876543210FEDCBA>";
std::unique_ptr<unsigned short, pdfium::FreeDeleter> 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<unsigned short*>(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<unsigned short*>(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"<D41D8CD98F00B204E9800998ECF8427E>",
GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())));