Add ValidateDictOptionalType() utility function.
This is similar to ValidateDictType(), but considers a dictionary that
omits /Type to be valid.
Change-Id: Ibc17123c26e06db60d90d9071af74d9fd069382a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/90530
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index 143551c..6b01790 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -185,6 +185,12 @@
return ValidateDictAllResourcesOfType(dict, "Font");
}
+bool ValidateDictOptionalType(const CPDF_Dictionary* dict,
+ ByteStringView type) {
+ DCHECK(!type.IsEmpty());
+ return dict && (!dict->KeyExist("Type") || dict->GetNameFor("Type") == type);
+}
+
std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj) {
if (!pObj) {
buf << " null";
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.h b/core/fpdfapi/parser/fpdf_parser_utility.h
index 7f20a9d..fbe87bd 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.h
+++ b/core/fpdfapi/parser/fpdf_parser_utility.h
@@ -67,6 +67,9 @@
// Shorthand for ValidateDictAllResourcesOfType(dict, "Font").
bool ValidateFontResourceDict(const CPDF_Dictionary* dict);
+// Like ValidateDictType(), but /Type can also not exist.
+bool ValidateDictOptionalType(const CPDF_Dictionary* dict, ByteStringView type);
+
std::ostream& operator<<(std::ostream& buf, const CPDF_Object* pObj);
#endif // CORE_FPDFAPI_PARSER_FPDF_PARSER_UTILITY_H_
diff --git a/core/fpdfapi/parser/fpdf_parser_utility_unittest.cpp b/core/fpdfapi/parser/fpdf_parser_utility_unittest.cpp
index 3e4d968..0deb439 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility_unittest.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility_unittest.cpp
@@ -108,3 +108,21 @@
CPDF_PageModule::Destroy();
}
+
+TEST(fpdf_parser_utility, ValidateDictOptionalType) {
+ auto dict = pdfium::MakeRetain<CPDF_Dictionary>();
+
+ // No type is ok.
+ EXPECT_TRUE(ValidateDictOptionalType(dict.Get(), "foo"));
+ EXPECT_TRUE(ValidateDictOptionalType(dict.Get(), "bar"));
+
+ // Add the wrong object type.
+ dict->SetNewFor<CPDF_String>("Type", L"foo");
+ EXPECT_FALSE(ValidateDictOptionalType(dict.Get(), "foo"));
+ EXPECT_FALSE(ValidateDictOptionalType(dict.Get(), "bar"));
+
+ // Add the correct object type.
+ dict->SetNewFor<CPDF_Name>("Type", "foo");
+ EXPECT_TRUE(ValidateDictOptionalType(dict.Get(), "foo"));
+ EXPECT_FALSE(ValidateDictOptionalType(dict.Get(), "bar"));
+}