Clean up CPDF_PageOrganizer::Init()

- Move the majority of Init() code into InitDestDoc(). Just in case
  there will be other init work unrelated to the destination document.
- Rename the local variables inside InitDestDoc() to be more concise.

Change-Id: I5c635f2a3e4b0396e2fc3ee1243ee46fcc3b12a8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/113650
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index d923354..ddfbb2a 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -232,6 +232,8 @@
   void ClearObjectNumberMap() { m_ObjectNumberMap.clear(); }
 
  private:
+  bool InitDestDoc();
+
   uint32_t GetNewObjId(CPDF_Reference* pRef);
 
   UnownedPtr<CPDF_Document> const m_pDestDoc;
@@ -251,36 +253,41 @@
   DCHECK(m_pDestDoc);
   DCHECK(m_pSrcDoc);
 
-  RetainPtr<CPDF_Dictionary> pNewRoot = dest()->GetMutableRoot();
-  if (!pNewRoot)
+  return InitDestDoc();
+}
+
+bool CPDF_PageOrganizer::InitDestDoc() {
+  RetainPtr<CPDF_Dictionary> root = dest()->GetMutableRoot();
+  if (!root) {
     return false;
-
-  RetainPtr<CPDF_Dictionary> pDocInfoDict = dest()->GetInfo();
-  if (pDocInfoDict) {
-    pDocInfoDict->SetNewFor<CPDF_String>("Producer", "PDFium", false);
   }
 
-  ByteString cbRootType = pNewRoot->GetByteStringFor("Type", ByteString());
-  if (cbRootType.IsEmpty())
-    pNewRoot->SetNewFor<CPDF_Name>("Type", "Catalog");
-
-  RetainPtr<CPDF_Object> pElement = pNewRoot->GetMutableObjectFor("Pages");
-  RetainPtr<CPDF_Dictionary> pNewPages =
-      pElement ? ToDictionary(pElement->GetMutableDirect()) : nullptr;
-  if (!pNewPages) {
-    pNewPages = dest()->NewIndirect<CPDF_Dictionary>();
-    pNewRoot->SetNewFor<CPDF_Reference>("Pages", dest(),
-                                        pNewPages->GetObjNum());
+  RetainPtr<CPDF_Dictionary> info = dest()->GetInfo();
+  if (info) {
+    info->SetNewFor<CPDF_String>("Producer", "PDFium", false);
   }
-  ByteString cbPageType = pNewPages->GetByteStringFor("Type", ByteString());
-  if (cbPageType.IsEmpty())
-    pNewPages->SetNewFor<CPDF_Name>("Type", "Pages");
 
-  if (!pNewPages->GetArrayFor("Kids")) {
-    auto pNewArray = dest()->NewIndirect<CPDF_Array>();
-    pNewPages->SetNewFor<CPDF_Number>("Count", 0);
-    pNewPages->SetNewFor<CPDF_Reference>("Kids", dest(),
-                                         pNewArray->GetObjNum());
+  if (root->GetByteStringFor("Type", ByteString()).IsEmpty()) {
+    root->SetNewFor<CPDF_Name>("Type", "Catalog");
+  }
+
+  RetainPtr<CPDF_Dictionary> pages;
+  if (RetainPtr<CPDF_Object> current_pages = root->GetMutableObjectFor("Pages");
+      current_pages) {
+    pages = ToDictionary(current_pages->GetMutableDirect());
+  }
+  if (!pages) {
+    pages = dest()->NewIndirect<CPDF_Dictionary>();
+    root->SetNewFor<CPDF_Reference>("Pages", dest(), pages->GetObjNum());
+  }
+  if (pages->GetByteStringFor("Type", ByteString()).IsEmpty()) {
+    pages->SetNewFor<CPDF_Name>("Type", "Pages");
+  }
+
+  if (!pages->GetArrayFor("Kids")) {
+    auto kids_array = dest()->NewIndirect<CPDF_Array>();
+    pages->SetNewFor<CPDF_Number>("Count", 0);
+    pages->SetNewFor<CPDF_Reference>("Kids", dest(), kids_array->GetObjNum());
   }
   return true;
 }