Consolidate QuadPoints reading code in fpdfsdk.

Also fix nits in QuadPoints code in cpdf_annot.cpp.

Change-Id: I7852b673d3dca906e6d250cb3cfa305f8ea7e742
Reviewed-on: https://pdfium-review.googlesource.com/28893
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 91cdb12..42bda6e 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -205,9 +205,12 @@
   return pResult;
 }
 
-// Static.
+// static
 CFX_FloatRect CPDF_Annot::RectFromQuadPointsArray(const CPDF_Array* pArray,
                                                   size_t nIndex) {
+  ASSERT(pArray);
+  ASSERT(nIndex < pArray->GetCount() / 8);
+
   // QuadPoints are defined with 4 pairs of numbers
   // ([ pair0, pair1, pair2, pair3 ]), where
   // pair0 = top_left
@@ -224,7 +227,7 @@
       pArray->GetNumberAt(2 + nIndex * 8), pArray->GetNumberAt(3 + nIndex * 8));
 }
 
-// Static.
+// static
 CFX_FloatRect CPDF_Annot::BoundingRectFromQuadPoints(
     CPDF_Dictionary* pAnnotDict) {
   CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
@@ -240,23 +243,21 @@
   return ret;
 }
 
-// Static.
+// static
 CFX_FloatRect CPDF_Annot::RectFromQuadPoints(CPDF_Dictionary* pAnnotDict,
                                              size_t nIndex) {
   CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
-
   if (!pArray)
     return CFX_FloatRect();
-
   return RectFromQuadPointsArray(pArray, nIndex);
 }
 
-// Static.
+// static
 bool CPDF_Annot::IsAnnotationHidden(CPDF_Dictionary* pAnnotDict) {
   return !!(pAnnotDict->GetIntegerFor("F") & ANNOTFLAG_HIDDEN);
 }
 
-// Static.
+// static
 CPDF_Annot::Subtype CPDF_Annot::StringToAnnotSubtype(
     const ByteString& sSubtype) {
   if (sSubtype == "Text")
@@ -316,7 +317,7 @@
   return CPDF_Annot::Subtype::UNKNOWN;
 }
 
-// Static.
+// static
 ByteString CPDF_Annot::AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype) {
   if (nSubtype == CPDF_Annot::Subtype::TEXT)
     return "Text";
@@ -375,7 +376,7 @@
   return "";
 }
 
-// Static.
+// static
 size_t CPDF_Annot::QuadPointCount(const CPDF_Array* pArray) {
   return pArray->GetCount() / 8;
 }
diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp
index d2a15ce..b5dccaa 100644
--- a/fpdfsdk/fpdfannot.cpp
+++ b/fpdfsdk/fpdfannot.cpp
@@ -627,24 +627,9 @@
   if (!annot || !quad_points || !FPDFAnnot_HasAttachmentPoints(annot))
     return false;
 
-  CPDF_Dictionary* pAnnotDict =
-      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict();
-  if (!pAnnotDict)
-    return false;
-
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
-  if (!pArray)
-    return false;
-
-  quad_points->x1 = pArray->GetNumberAt(0);
-  quad_points->y1 = pArray->GetNumberAt(1);
-  quad_points->x2 = pArray->GetNumberAt(2);
-  quad_points->y2 = pArray->GetNumberAt(3);
-  quad_points->x3 = pArray->GetNumberAt(4);
-  quad_points->y3 = pArray->GetNumberAt(5);
-  quad_points->x4 = pArray->GetNumberAt(6);
-  quad_points->y4 = pArray->GetNumberAt(7);
-  return true;
+  return GetQuadPointsFromDictionary(
+      CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(), 0,
+      quad_points);
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFAnnot_SetRect(FPDF_ANNOTATION annot,
diff --git a/fpdfsdk/fpdfdoc.cpp b/fpdfsdk/fpdfdoc.cpp
index befdc4d..774dc28 100644
--- a/fpdfsdk/fpdfdoc.cpp
+++ b/fpdfsdk/fpdfdoc.cpp
@@ -78,6 +78,31 @@
   return ToDictionary(static_cast<CPDF_Object*>(link));
 }
 
+const CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict) {
+  return dict ? dict->GetArrayFor("QuadPoints") : nullptr;
+}
+
+bool GetQuadPointsFromDictionary(CPDF_Dictionary* dict,
+                                 size_t quad_index,
+                                 FS_QUADPOINTSF* quad_points) {
+  ASSERT(quad_points);
+
+  const CPDF_Array* pArray = GetQuadPointsArrayFromDictionary(dict);
+  if (!pArray || quad_index >= pArray->GetCount() / 8)
+    return false;
+
+  quad_index *= 8;
+  quad_points->x1 = pArray->GetNumberAt(quad_index);
+  quad_points->y1 = pArray->GetNumberAt(quad_index + 1);
+  quad_points->x2 = pArray->GetNumberAt(quad_index + 2);
+  quad_points->y2 = pArray->GetNumberAt(quad_index + 3);
+  quad_points->x3 = pArray->GetNumberAt(quad_index + 4);
+  quad_points->y3 = pArray->GetNumberAt(quad_index + 5);
+  quad_points->x4 = pArray->GetNumberAt(quad_index + 6);
+  quad_points->y4 = pArray->GetNumberAt(quad_index + 7);
+  return true;
+}
+
 FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV
 FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
   CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
@@ -379,41 +404,20 @@
 }
 
 FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountQuadPoints(FPDF_LINK link_annot) {
-  if (!link_annot)
-    return 0;
-  CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFLink(link_annot);
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
-  if (!pArray)
-    return 0;
-  return static_cast<int>(pArray->GetCount() / 8);
+  const CPDF_Array* pArray =
+      GetQuadPointsArrayFromDictionary(CPDFDictionaryFromFPDFLink(link_annot));
+  return pArray ? static_cast<int>(pArray->GetCount() / 8) : 0;
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
 FPDFLink_GetQuadPoints(FPDF_LINK link_annot,
                        int quad_index,
                        FS_QUADPOINTSF* quad_points) {
-  if (!link_annot || !quad_points)
+  if (!quad_points || quad_index < 0)
     return false;
-  CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFLink(link_annot);
-  CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
-  if (!pArray)
-    return false;
-
-  if (quad_index < 0 ||
-      static_cast<size_t>(quad_index) >= pArray->GetCount() / 8 ||
-      (static_cast<size_t>(quad_index * 8 + 7) >= pArray->GetCount())) {
-    return false;
-  }
-
-  quad_points->x1 = pArray->GetNumberAt(quad_index * 8);
-  quad_points->y1 = pArray->GetNumberAt(quad_index * 8 + 1);
-  quad_points->x2 = pArray->GetNumberAt(quad_index * 8 + 2);
-  quad_points->y2 = pArray->GetNumberAt(quad_index * 8 + 3);
-  quad_points->x3 = pArray->GetNumberAt(quad_index * 8 + 4);
-  quad_points->y3 = pArray->GetNumberAt(quad_index * 8 + 5);
-  quad_points->x4 = pArray->GetNumberAt(quad_index * 8 + 6);
-  quad_points->y4 = pArray->GetNumberAt(quad_index * 8 + 7);
-  return true;
+  return GetQuadPointsFromDictionary(CPDFDictionaryFromFPDFLink(link_annot),
+                                     static_cast<size_t>(quad_index),
+                                     quad_points);
 }
 
 FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document,
diff --git a/fpdfsdk/fsdk_define.h b/fpdfsdk/fsdk_define.h
index d503ba5..acd8437 100644
--- a/fpdfsdk/fsdk_define.h
+++ b/fpdfsdk/fsdk_define.h
@@ -10,6 +10,7 @@
 #include "core/fpdfapi/parser/cpdf_parser.h"
 #include "core/fxge/dib/cfx_dibitmap.h"
 #include "core/fxge/fx_dib.h"
+#include "public/fpdf_doc.h"
 #include "public/fpdfview.h"
 
 #ifdef PDF_ENABLE_XFA
@@ -83,6 +84,11 @@
 
 CPDF_Dictionary* CPDFDictionaryFromFPDFLink(FPDF_LINK link);
 
+const CPDF_Array* GetQuadPointsArrayFromDictionary(CPDF_Dictionary* dict);
+bool GetQuadPointsFromDictionary(CPDF_Dictionary* dict,
+                                 size_t quad_index,
+                                 FS_QUADPOINTSF* quad_points);
+
 CFX_FloatRect CFXFloatRectFromFSRECTF(const FS_RECTF& rect);
 void FSRECTFFromCFXFloatRect(const CFX_FloatRect& rect, FS_RECTF* out_rect);