Improve ValidateDictType().
Then use it in several places to de-duplicate some code.
-- avoid constructing temporary strings.
-- handle nullptr args on behalf of callers.
-- remove superfluous Get() call.
Change-Id: I0b847c7bca1cbb843456a2d1ae58a388ad958e1c
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/87592
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index 1396166..143551c 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -162,20 +162,20 @@
return ret;
}
-bool ValidateDictType(const CPDF_Dictionary* dict, const ByteString& type) {
+bool ValidateDictType(const CPDF_Dictionary* dict, ByteStringView type) {
DCHECK(!type.IsEmpty());
- return dict->GetNameFor("Type") == type;
+ return dict && dict->GetNameFor("Type") == type;
}
bool ValidateDictAllResourcesOfType(const CPDF_Dictionary* dict,
- const ByteString& type) {
+ ByteStringView type) {
if (!dict)
return false;
CPDF_DictionaryLocker locker(dict);
for (const auto& it : locker) {
- const CPDF_Dictionary* entry = ToDictionary(it.second.Get()->GetDirect());
- if (!entry || !ValidateDictType(entry, type))
+ const CPDF_Dictionary* entry = ToDictionary(it.second->GetDirect());
+ if (!ValidateDictType(entry, type))
return false;
}
return true;
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.h b/core/fpdfapi/parser/fpdf_parser_utility.h
index 3ef96c8..7f20a9d 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.h
+++ b/core/fpdfapi/parser/fpdf_parser_utility.h
@@ -55,13 +55,14 @@
std::vector<float> ReadArrayElementsToVector(const CPDF_Array* pArray,
size_t nCount);
-// Returns true if |dict| has a /Type name entry that matches |type|.
-bool ValidateDictType(const CPDF_Dictionary* dict, const ByteString& type);
+// Returns true if |dict| is non-null and has a /Type name entry that matches
+// |type|.
+bool ValidateDictType(const CPDF_Dictionary* dict, ByteStringView type);
// Returns true if |dict| is non-null and all entries in |dict| are dictionaries
// of |type|.
bool ValidateDictAllResourcesOfType(const CPDF_Dictionary* dict,
- const ByteString& type);
+ ByteStringView type);
// Shorthand for ValidateDictAllResourcesOfType(dict, "Font").
bool ValidateFontResourceDict(const CPDF_Dictionary* dict);
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index 9ffba8a..db2f793 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -200,7 +200,7 @@
for (const auto& it : locker) {
const ByteString& csKey = it.first;
CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
- if (!pElement || pElement->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pElement, "Font"))
continue;
auto* pData = CPDF_DocPageData::FromDocument(m_pDocument.Get());
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index df437c2..357b623 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -185,7 +185,7 @@
for (const auto& it : locker) {
const ByteString& csKey = it.first;
const CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
- if (!pElement || pElement->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pElement, "Font"))
continue;
if (pFont->GetFontDict() == pElement) {
*csNameTag = csKey;
@@ -216,7 +216,7 @@
for (const auto& it : locker) {
const ByteString& csKey = it.first;
CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
- if (!pElement || pElement->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pElement, "Font"))
continue;
pFont = CPDF_DocPageData::FromDocument(pDocument)->GetFont(pElement);
@@ -321,7 +321,7 @@
for (const auto& it : locker) {
const ByteString& csKey = it.first;
CPDF_Dictionary* pElement = ToDictionary(it.second->GetDirect());
- if (!pElement || pElement->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pElement, "Font"))
continue;
auto* pData = CPDF_DocPageData::FromDocument(pDocument);
@@ -719,7 +719,7 @@
return nullptr;
CPDF_Dictionary* pElement = pFonts->GetDictFor(csAlias);
- if (!pElement || pElement->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pElement, "Font"))
return nullptr;
return CPDF_DocPageData::FromDocument(m_pDocument)->GetFont(pElement);
diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp
index 7020121..a34bc98 100644
--- a/xfa/fgas/font/cfgas_pdffontmgr.cpp
+++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp
@@ -12,6 +12,7 @@
#include "core/fpdfapi/page/cpdf_docpagedata.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/fpdf_parser_utility.h"
#include "core/fxge/fx_font.h"
#include "third_party/base/check.h"
#include "third_party/base/cxx17_backports.h"
@@ -61,7 +62,7 @@
continue;
}
CPDF_Dictionary* pFontDict = ToDictionary(pObj->GetDirect());
- if (!pFontDict || pFontDict->GetNameFor("Type") != "Font")
+ if (!ValidateDictType(pFontDict, "Font"))
return nullptr;
RetainPtr<CPDF_Font> pPDFFont = pData->GetFont(pFontDict);