Merge to XFA: Remove the m_V5Type member variable from CPDF_Parser.

Use the type field in the m_ObjectInfo map instead.

BUG=pdfium:111
TBR=weili@chromium.org

Review URL: https://codereview.chromium.org/1650793002 .

(cherry picked from commit 93c196d868535f684007a7d3ed1f7146851a0ab9)

Review URL: https://codereview.chromium.org/1652083004 .
diff --git a/core/include/fpdfapi/fpdf_parser.h b/core/include/fpdfapi/fpdf_parser.h
index 62c2a8b..57120ec 100644
--- a/core/include/fpdfapi/fpdf_parser.h
+++ b/core/include/fpdfapi/fpdf_parser.h
@@ -430,18 +430,17 @@
   CFX_ByteString m_Password;
 
   struct ObjectInfo {
-    ObjectInfo() : pos(0) {}
+    ObjectInfo() : pos(0), type(0) {}
 
     FX_FILESIZE pos;
-// TODO(thestig): Use fields below in place of |m_V5Type| and |m_ObjVersion|
-#if 0
     uint8_t type;
+// TODO(thestig): Use field below in place of |m_ObjVersion|
+#if 0
     uint16_t gennum;
 #endif
   };
   std::map<FX_DWORD, ObjectInfo> m_ObjectInfo;
 
-  CFX_ByteArray m_V5Type;
   CFX_WordArray m_ObjVersion;
   CFX_FileSizeArray m_SortedOffset;
   CFX_ArrayTemplate<CPDF_Dictionary*> m_Trailers;
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
index 653ce28..5cc1177 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp
@@ -125,7 +125,9 @@
 }
 
 uint8_t CPDF_Parser::GetObjectType(FX_DWORD objnum) const {
-  return m_V5Type[objnum];
+  ASSERT(IsValidObjectNumber(objnum));
+  auto it = m_ObjectInfo.find(objnum);
+  return it != m_ObjectInfo.end() ? it->second.type : 0;
 }
 
 uint16_t CPDF_Parser::GetObjectGenNum(FX_DWORD objnum) const {
@@ -177,7 +179,6 @@
 
   m_SortedOffset.RemoveAll();
   m_ObjectInfo.clear();
-  m_V5Type.RemoveAll();
   m_ObjVersion.RemoveAll();
   int32_t iLen = m_Trailers.GetSize();
   for (int32_t i = 0; i < iLen; ++i) {
@@ -341,10 +342,10 @@
   if (!IsValidObjectNumber(objnum))
     return 0;
 
-  if (m_V5Type[objnum] == 1)
+  if (GetObjectType(objnum) == 1)
     return GetObjectPositionOrZero(objnum);
 
-  if (m_V5Type[objnum] == 2) {
+  if (GetObjectType(objnum) == 2) {
     FX_FILESIZE pos = GetObjectPositionOrZero(objnum);
     return GetObjectPositionOrZero(pos);
   }
@@ -361,10 +362,8 @@
   }
 
   int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size");
-  if (xrefsize > 0 && xrefsize <= kMaxXRefSize) {
+  if (xrefsize > 0 && xrefsize <= kMaxXRefSize)
     ShrinkObjectMap(xrefsize);
-    m_V5Type.SetSize(xrefsize);
-  }
 
   CFX_FileSizeArray CrossRefList;
   CFX_FileSizeArray XRefStreamList;
@@ -473,7 +472,7 @@
       char* pEntry = &buf[i * recordsize];
       if (pEntry[17] == 'f') {
         m_ObjectInfo[objnum].pos = 0;
-        m_V5Type.SetAtGrow(objnum, 0);
+        m_ObjectInfo[objnum].type = 0;
       } else {
         int32_t offset = FXSYS_atoi(pEntry);
         if (offset == 0) {
@@ -496,7 +495,7 @@
             m_SortedOffset.Add(m_ObjectInfo[objnum].pos);
           }
         }
-        m_V5Type.SetAtGrow(objnum, 1);
+        m_ObjectInfo[objnum].type = 1;
       }
     }
   }
@@ -555,7 +554,7 @@
           char* pEntry = &buf[i * recordsize];
           if (pEntry[17] == 'f') {
             m_ObjectInfo[objnum].pos = 0;
-            m_V5Type.SetAtGrow(objnum, 0);
+            m_ObjectInfo[objnum].type = 0;
           } else {
             FX_FILESIZE offset = (FX_FILESIZE)FXSYS_atoi64(pEntry);
             if (offset == 0) {
@@ -574,7 +573,7 @@
                 !FindPosInOffsets(m_ObjectInfo[objnum].pos)) {
               m_SortedOffset.Add(m_ObjectInfo[objnum].pos);
             }
-            m_V5Type.SetAtGrow(objnum, 1);
+            m_ObjectInfo[objnum].type = 1;
           }
         }
       }
@@ -606,7 +605,6 @@
 
 FX_BOOL CPDF_Parser::RebuildCrossRef() {
   m_ObjectInfo.clear();
-  m_V5Type.RemoveAll();
   m_SortedOffset.RemoveAll();
   m_ObjVersion.RemoveAll();
   if (m_pTrailer) {
@@ -822,7 +820,7 @@
                   }
                 } else {
                   m_ObjectInfo[objnum].pos = obj_pos;
-                  m_V5Type.SetAtGrow(objnum, 1);
+                  m_ObjectInfo[objnum].type = 1;
                   m_ObjVersion.SetAtGrow(objnum, (int16_t)gennum);
                 }
                 if (pObject) {
@@ -1016,9 +1014,8 @@
   if (bMainXRef) {
     m_pTrailer = ToDictionary(pStream->GetDict()->Clone());
     ShrinkObjectMap(size);
-    if (m_V5Type.SetSize(size)) {
-      FXSYS_memset(m_V5Type.GetData(), 0, size);
-    }
+    for (auto it : m_ObjectInfo)
+      it.second.type = 0;
   } else {
     m_Trailers.Add(ToDictionary(pStream->GetDict()->Clone()));
   }
@@ -1081,8 +1078,7 @@
     const uint8_t* segstart = pData + segindex * totalWidth;
     FX_SAFE_DWORD dwMaxObjNum = startnum;
     dwMaxObjNum += count;
-    FX_DWORD dwV5Size =
-        pdfium::base::checked_cast<FX_DWORD, int32_t>(m_V5Type.GetSize());
+    FX_DWORD dwV5Size = m_ObjectInfo.empty() ? 0 : GetLastObjNum() + 1;
     if (!dwMaxObjNum.IsValid() || dwMaxObjNum.ValueOrDie() > dwV5Size) {
       continue;
     }
@@ -1092,7 +1088,7 @@
       if (WidthArray[0]) {
         type = GetVarInt(entrystart, WidthArray[0]);
       }
-      if (m_V5Type[startnum + j] == 255) {
+      if (GetObjectType(startnum + j) == 255) {
         FX_FILESIZE offset =
             GetVarInt(entrystart + WidthArray[0], WidthArray[1]);
         m_ObjectInfo[startnum + j].pos = offset;
@@ -1104,10 +1100,10 @@
         }
         continue;
       }
-      if (m_V5Type[startnum + j]) {
+      if (GetObjectType(startnum + j)) {
         continue;
       }
-      m_V5Type[startnum + j] = type;
+      m_ObjectInfo[startnum + j].type = type;
       if (type == 0) {
         m_ObjectInfo[startnum + j].pos = 0;
       } else {
@@ -1122,11 +1118,11 @@
             m_SortedOffset.Add(offset);
           }
         } else {
-          if (offset < 0 || offset >= m_V5Type.GetSize()) {
+          if (offset < 0 || !IsValidObjectNumber(offset)) {
             pStream->Release();
             return FALSE;
           }
-          m_V5Type[offset] = 255;
+          m_ObjectInfo[offset].type = 255;
         }
       }
     }
@@ -1160,9 +1156,9 @@
   bForm = FALSE;
   if (!IsValidObjectNumber(objnum))
     return TRUE;
-  if (m_V5Type[objnum] == 0)
+  if (GetObjectType(objnum) == 0)
     return TRUE;
-  if (m_V5Type[objnum] == 2)
+  if (GetObjectType(objnum) == 2)
     return TRUE;
   FX_FILESIZE pos = m_ObjectInfo[objnum].pos;
   void* pResult =
@@ -1196,13 +1192,13 @@
     return nullptr;
   ScopedSetInsertion<FX_DWORD> local_insert(&m_ParsingObjNums, objnum);
 
-  if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) {
+  if (GetObjectType(objnum) == 1 || GetObjectType(objnum) == 255) {
     FX_FILESIZE pos = m_ObjectInfo[objnum].pos;
     if (pos <= 0)
       return nullptr;
     return ParseIndirectObjectAt(pObjList, pos, objnum);
   }
-  if (m_V5Type[objnum] != 2)
+  if (GetObjectType(objnum) != 2)
     return nullptr;
 
   CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos);
@@ -1254,10 +1250,10 @@
   if (!IsValidObjectNumber(objnum))
     return 0;
 
-  if (m_V5Type[objnum] == 2)
+  if (GetObjectType(objnum) == 2)
     objnum = GetObjectPositionOrZero(objnum);
 
-  if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) {
+  if (GetObjectType(objnum) == 1 || GetObjectType(objnum) == 255) {
     FX_FILESIZE offset = GetObjectPositionOrZero(objnum);
     if (offset == 0)
       return 0;
@@ -1284,7 +1280,7 @@
   if (!IsValidObjectNumber(objnum))
     return;
 
-  if (m_V5Type[objnum] == 2) {
+  if (GetObjectType(objnum) == 2) {
     CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos);
     if (!pObjStream)
       return;
@@ -1316,7 +1312,7 @@
     return;
   }
 
-  if (m_V5Type[objnum] != 1)
+  if (GetObjectType(objnum) != 1)
     return;
 
   FX_FILESIZE pos = m_ObjectInfo[objnum].pos;
@@ -1573,10 +1569,8 @@
     }
 
     int32_t xrefsize = GetDirectInteger(m_pTrailer, "Size");
-    if (xrefsize > 0) {
+    if (xrefsize > 0)
       ShrinkObjectMap(xrefsize);
-      m_V5Type.SetSize(xrefsize);
-    }
   }
   Error eRet = SetEncryptHandler();
   if (eRet != SUCCESS) {
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp
index e091f14..b39eb96 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser_unittest.cpp
@@ -290,9 +290,10 @@
     ASSERT_TRUE(parser.LoadCrossRefV4(0, 0, FALSE));
     const FX_FILESIZE offsets[] = {0, 17, 81, 0, 331, 409};
     const uint8_t types[] = {0, 1, 1, 0, 1, 1};
-    for (size_t i = 0; i < FX_ArraySize(offsets); ++i)
+    for (size_t i = 0; i < FX_ArraySize(offsets); ++i) {
       EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos);
-    ASSERT_TRUE(CompareArray(parser.m_V5Type, types, FX_ArraySize(types)));
+      EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type);
+    }
   }
   {
     const unsigned char xref_table[] =
@@ -315,9 +316,10 @@
     const FX_FILESIZE offsets[] = {0, 0,     0,     25325, 0, 0,    0,
                                    0, 25518, 25635, 0,     0, 25777};
     const uint8_t types[] = {0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1};
-    for (size_t i = 0; i < FX_ArraySize(offsets); ++i)
+    for (size_t i = 0; i < FX_ArraySize(offsets); ++i) {
       EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos);
-    ASSERT_TRUE(CompareArray(parser.m_V5Type, types, FX_ArraySize(types)));
+      EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type);
+    }
   }
   {
     const unsigned char xref_table[] =
@@ -340,9 +342,10 @@
     const FX_FILESIZE offsets[] = {0, 0, 0,     25325, 0, 0,    0,
                                    0, 0, 25635, 0,     0, 25777};
     const uint8_t types[] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1};
-    for (size_t i = 0; i < FX_ArraySize(offsets); ++i)
+    for (size_t i = 0; i < FX_ArraySize(offsets); ++i) {
       EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos);
-    ASSERT_TRUE(CompareArray(parser.m_V5Type, types, FX_ArraySize(types)));
+      EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type);
+    }
   }
   {
     const unsigned char xref_table[] =
@@ -363,8 +366,9 @@
     ASSERT_TRUE(parser.LoadCrossRefV4(0, 0, FALSE));
     const FX_FILESIZE offsets[] = {0, 23, 0, 0, 0, 45, 179};
     const uint8_t types[] = {0, 1, 0, 0, 0, 1, 1};
-    for (size_t i = 0; i < FX_ArraySize(offsets); ++i)
+    for (size_t i = 0; i < FX_ArraySize(offsets); ++i) {
       EXPECT_EQ(offsets[i], parser.m_ObjectInfo[i].pos);
-    ASSERT_TRUE(CompareArray(parser.m_V5Type, types, FX_ArraySize(types)));
+      EXPECT_EQ(types[i], parser.m_ObjectInfo[i].type);
+    }
   }
 }