Avoid writing const/non-const versions of the same function.

Use const_cast for the non-const version to call the const version.

Change-Id: Ibdf5fe53255ee6e983555080336f5d63e683afd1
Reviewed-on: https://pdfium-review.googlesource.com/37490
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp
index 071461b..78227ae 100644
--- a/core/fpdfapi/parser/cpdf_dictionary.cpp
+++ b/core/fpdfapi/parser/cpdf_dictionary.cpp
@@ -87,8 +87,8 @@
 }
 
 CPDF_Object* CPDF_Dictionary::GetObjectFor(const ByteString& key) {
-  auto it = m_Map.find(key);
-  return it != m_Map.end() ? it->second.get() : nullptr;
+  return const_cast<CPDF_Object*>(
+      static_cast<const CPDF_Dictionary*>(this)->GetObjectFor(key));
 }
 
 const CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(
@@ -98,8 +98,8 @@
 }
 
 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(const ByteString& key) {
-  CPDF_Object* p = GetObjectFor(key);
-  return p ? p->GetDirect() : nullptr;
+  return const_cast<CPDF_Object*>(
+      static_cast<const CPDF_Dictionary*>(this)->GetDirectObjectFor(key));
 }
 
 ByteString CPDF_Dictionary::GetStringFor(const ByteString& key) const {
@@ -154,14 +154,8 @@
 }
 
 CPDF_Dictionary* CPDF_Dictionary::GetDictFor(const ByteString& key) {
-  CPDF_Object* p = GetDirectObjectFor(key);
-  if (!p)
-    return nullptr;
-  if (CPDF_Dictionary* pDict = p->AsDictionary())
-    return pDict;
-  if (CPDF_Stream* pStream = p->AsStream())
-    return pStream->GetDict();
-  return nullptr;
+  return const_cast<CPDF_Dictionary*>(
+      static_cast<const CPDF_Dictionary*>(this)->GetDictFor(key));
 }
 
 const CPDF_Array* CPDF_Dictionary::GetArrayFor(const ByteString& key) const {
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index 2a825d7..7641dbd 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -329,8 +329,8 @@
 }
 
 CPDF_Dictionary* CPDF_Document::GetPagesDict() {
-  CPDF_Dictionary* pRoot = GetRoot();
-  return pRoot ? pRoot->GetDictFor("Pages") : nullptr;
+  return const_cast<CPDF_Dictionary*>(
+      static_cast<const CPDF_Document*>(this)->GetPagesDict());
 }
 
 bool CPDF_Document::IsPageLoaded(int iPage) const {
diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp
index 95254b7..2cc51e3 100644
--- a/core/fpdfdoc/cpdf_filespec.cpp
+++ b/core/fpdfdoc/cpdf_filespec.cpp
@@ -19,10 +19,6 @@
 
 namespace {
 
-// List of keys to check for the file specification string.
-// Follows the same precedence order as GetFileName().
-constexpr const char* kKeys[] = {"UF", "F", "DOS", "Mac", "Unix"};
-
 #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ || \
     _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
 WideString ChangeSlashToPlatform(const wchar_t* str) {
@@ -136,6 +132,9 @@
   if (!pFiles)
     return nullptr;
 
+  // List of keys to check for the file specification string.
+  // Follows the same precedence order as GetFileName().
+  constexpr const char* kKeys[] = {"UF", "F", "DOS", "Mac", "Unix"};
   size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys);
   for (size_t i = 0; i < end; ++i) {
     ByteString key = kKeys[i];
@@ -149,25 +148,8 @@
 }
 
 CPDF_Stream* CPDF_FileSpec::GetFileStream() {
-  CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary();
-  if (!pDict)
-    return nullptr;
-
-  // Get the embedded files dictionary.
-  CPDF_Dictionary* pFiles = pDict->GetDictFor("EF");
-  if (!pFiles)
-    return nullptr;
-
-  size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys);
-  for (size_t i = 0; i < end; ++i) {
-    ByteString key = kKeys[i];
-    if (!pDict->GetUnicodeTextFor(key).IsEmpty()) {
-      CPDF_Stream* pStream = pFiles->GetStreamFor(key);
-      if (pStream)
-        return pStream;
-    }
-  }
-  return nullptr;
+  return const_cast<CPDF_Stream*>(
+      static_cast<const CPDF_FileSpec*>(this)->GetFileStream());
 }
 
 const CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() const {
@@ -180,12 +162,8 @@
 }
 
 CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() {
-  CPDF_Stream* pStream = GetFileStream();
-  if (!pStream)
-    return nullptr;
-
-  CPDF_Dictionary* pDict = pStream->GetDict();
-  return pDict ? pDict->GetDictFor("Params") : nullptr;
+  return const_cast<CPDF_Dictionary*>(
+      static_cast<const CPDF_FileSpec*>(this)->GetParamsDict());
 }
 
 WideString CPDF_FileSpec::EncodeFileName(const WideString& filepath) {
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 2f05ad8..c88513d 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -37,8 +37,6 @@
 const int kFormTextNoScroll = 0x400;
 const int kFormTextComb = 0x800;
 
-constexpr int kGetFieldMaxRecursion = 32;
-
 bool IsUnison(CPDF_FormField* pField) {
   if (pField->GetType() == CPDF_FormField::CheckBox)
     return true;
@@ -58,6 +56,7 @@
 const CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict,
                                      const char* name,
                                      int nLevel) {
+  static constexpr int kGetFieldMaxRecursion = 32;
   if (!pFieldDict || nLevel > kGetFieldMaxRecursion)
     return nullptr;
 
@@ -72,15 +71,8 @@
 CPDF_Object* FPDF_GetFieldAttr(CPDF_Dictionary* pFieldDict,
                                const char* name,
                                int nLevel) {
-  if (!pFieldDict || nLevel > kGetFieldMaxRecursion)
-    return nullptr;
-
-  CPDF_Object* pAttr = pFieldDict->GetDirectObjectFor(name);
-  if (pAttr)
-    return pAttr;
-
-  CPDF_Dictionary* pParent = pFieldDict->GetDictFor("Parent");
-  return pParent ? FPDF_GetFieldAttr(pParent, name, nLevel + 1) : nullptr;
+  return const_cast<CPDF_Object*>(FPDF_GetFieldAttr(
+      static_cast<const CPDF_Dictionary*>(pFieldDict), name, nLevel));
 }
 
 WideString FPDF_GetFullName(CPDF_Dictionary* pFieldDict) {