Replace hardcoded 256 with CPDF_SimpleFont::kInternalTableSize

Ensures tables are consistently sized. Then use spancpy() in one place
to ensure we get an exception should one of these change inappropriately
down the road.

-- add an in-header bool initializer, just because.
-- change one constant array from `g_`-naming to `k`-naming since
   `g_`-naming should imply mutability.

Change-Id: I80881eaa7329103281d66d9be5883d2a34a4a132
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82791
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_simplefont.cpp b/core/fpdfapi/font/cpdf_simplefont.cpp
index 822cf28..698e35b 100644
--- a/core/fpdfapi/font/cpdf_simplefont.cpp
+++ b/core/fpdfapi/font/cpdf_simplefont.cpp
@@ -6,6 +6,9 @@
 
 #include "core/fpdfapi/font/cpdf_simplefont.h"
 
+#include <algorithm>
+#include <iterator>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_name.h"
@@ -146,7 +149,7 @@
   if (!pDiffs)
     return;
 
-  m_CharNames.resize(256);
+  m_CharNames.resize(kInternalTableSize);
   uint32_t cur_code = 0;
   for (uint32_t i = 0; i < pDiffs->size(); i++) {
     const CPDF_Object* pElement = pDiffs->GetDirectObjectAt(i);
@@ -155,7 +158,7 @@
 
     const CPDF_Name* pName = pElement->AsName();
     if (pName) {
-      if (cur_code < 256)
+      if (cur_code < m_CharNames.size())
         m_CharNames[cur_code] = pName->GetString();
       cur_code++;
     } else {
@@ -197,9 +200,7 @@
   if (pWidthArray) {
     if (pFontDesc && pFontDesc->KeyExist("MissingWidth")) {
       int MissingWidth = pFontDesc->GetIntegerFor("MissingWidth");
-      for (int i = 0; i < 256; i++) {
-        m_CharWidth[i] = MissingWidth;
-      }
+      std::fill(std::begin(m_CharWidth), std::end(m_CharWidth), MissingWidth);
     }
     size_t width_start = m_pFontDict->GetIntegerFor("FirstChar", 0);
     size_t width_end = m_pFontDict->GetIntegerFor("LastChar", 0);
@@ -251,8 +252,8 @@
 void CPDF_SimpleFont::LoadSubstFont() {
   if (!m_bUseFontWidth && !FontStyleIsFixedPitch(m_Flags)) {
     int width = 0;
-    int i;
-    for (i = 0; i < 256; i++) {
+    size_t i;
+    for (i = 0; i < kInternalTableSize; i++) {
       if (m_CharWidth[i] == 0 || m_CharWidth[i] == 0xffff)
         continue;
 
@@ -261,7 +262,7 @@
       else if (width != m_CharWidth[i])
         break;
     }
-    if (i == 256 && width)
+    if (i == kInternalTableSize && width)
       m_Flags |= FXFONT_FIXED_PITCH;
   }
   m_Font.LoadSubst(m_BaseFontName, IsTrueTypeFont(), m_Flags, GetFontWeight(),
diff --git a/core/fpdfapi/font/cpdf_simplefont.h b/core/fpdfapi/font/cpdf_simplefont.h
index 0e7e9e0..a2b6884 100644
--- a/core/fpdfapi/font/cpdf_simplefont.h
+++ b/core/fpdfapi/font/cpdf_simplefont.h
@@ -31,6 +31,8 @@
   bool HasFontWidths() const override;
 
  protected:
+  static constexpr size_t kInternalTableSize = 256;
+
   CPDF_SimpleFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
 
   virtual void LoadGlyphMap() = 0;
@@ -42,11 +44,11 @@
 
   CPDF_FontEncoding m_Encoding{PDFFONT_ENCODING_BUILTIN};
   int m_BaseEncoding = PDFFONT_ENCODING_BUILTIN;
-  bool m_bUseFontWidth;
+  bool m_bUseFontWidth = false;
   std::vector<ByteString> m_CharNames;
-  uint16_t m_GlyphIndex[256];
-  uint16_t m_CharWidth[256];
-  FX_RECT m_CharBBox[256];
+  uint16_t m_GlyphIndex[kInternalTableSize];
+  uint16_t m_CharWidth[kInternalTableSize];
+  FX_RECT m_CharBBox[kInternalTableSize];
 };
 
 #endif  // CORE_FPDFAPI_FONT_CPDF_SIMPLEFONT_H_
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp
index 85be683..90dd4dc 100644
--- a/core/fpdfapi/font/cpdf_type1font.cpp
+++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -6,8 +6,12 @@
 
 #include "core/fpdfapi/font/cpdf_type1font.h"
 
+#include <algorithm>
+#include <iterator>
+
 #include "build/build_config.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
+#include "core/fxcrt/span_util.h"
 #include "core/fxge/cfx_fontmapper.h"
 #include "core/fxge/cfx_gemodule.h"
 #include "core/fxge/fx_font.h"
@@ -25,14 +29,14 @@
   const char* m_pStrUnicode;  // Raw, POD struct.
 };
 
-const GlyphNameMap g_GlyphNameSubsts[] = {{"ff", "uniFB00"},
-                                          {"ffi", "uniFB03"},
-                                          {"ffl", "uniFB04"},
-                                          {"fi", "uniFB01"},
-                                          {"fl", "uniFB02"}};
+const GlyphNameMap kGlyphNameSubsts[] = {{"ff", "uniFB00"},
+                                         {"ffi", "uniFB03"},
+                                         {"ffl", "uniFB04"},
+                                         {"fi", "uniFB01"},
+                                         {"fl", "uniFB02"}};
 
 const char* GlyphNameRemap(const char* pStrAdobe) {
-  for (const auto& element : g_GlyphNameSubsts) {
+  for (const auto& element : kGlyphNameSubsts) {
     if (!FXSYS_stricmp(element.m_pStrAdobe, pStrAdobe))
       return element.m_pStrUnicode;
   }
@@ -97,8 +101,7 @@
     m_Flags = FXFONT_NONSYMBOLIC;
   }
   if (IsFixedFont()) {
-    for (int i = 0; i < 256; i++)
-      m_CharWidth[i] = 600;
+    std::fill(std::begin(m_CharWidth), std::end(m_CharWidth), 600);
   }
   if (m_Base14Font == CFX_FontMapper::kSymbol)
     m_BaseEncoding = PDFFONT_ENCODING_ADOBE_SYMBOL;
@@ -139,7 +142,7 @@
   if (!IsEmbedded() && !IsSymbolicFont() && m_Font.IsTTFont()) {
     if (FT_UseTTCharmap(m_Font.GetFaceRec(), 3, 0)) {
       bool bGotOne = false;
-      for (uint32_t charcode = 0; charcode < 256; charcode++) {
+      for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
         const uint8_t prefix[4] = {0x00, 0xf0, 0xf1, 0xf2};
         for (int j = 0; j < 4; j++) {
           uint16_t unicode = prefix[j] * 256 + charcode;
@@ -166,7 +169,7 @@
     if (m_BaseEncoding == 0)
       m_BaseEncoding = PDFFONT_ENCODING_STANDARD;
 
-    for (uint32_t charcode = 0; charcode < 256; charcode++) {
+    for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
       const char* name =
           GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
       if (!name)
@@ -187,8 +190,10 @@
       }
     }
 #if defined(OS_APPLE)
-    if (!bCoreText)
-      memcpy(m_ExtGID, m_GlyphIndex, sizeof(m_ExtGID));
+    if (!bCoreText) {
+      fxcrt::spancpy(pdfium::make_span(m_ExtGID),
+                     pdfium::make_span(m_GlyphIndex));
+    }
 #endif
     return;
   }
@@ -196,7 +201,7 @@
 #if defined(OS_APPLE)
   if (bCoreText) {
     if (FontStyleIsSymbolic(m_Flags)) {
-      for (uint32_t charcode = 0; charcode < 256; charcode++) {
+      for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
         const char* name =
             GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
         if (name) {
@@ -212,11 +217,10 @@
             unicode =
                 FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode);
           }
-          char name_glyph[256];
-          memset(name_glyph, 0, sizeof(name_glyph));
+          char name_glyph[kInternalTableSize] = {};
           FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
-                            name_glyph, 256);
-          name_glyph[255] = 0;
+                            name_glyph, sizeof(name_glyph));
+          name_glyph[kInternalTableSize - 1] = 0;
           if (unicode == 0 && name_glyph[0] != 0)
             unicode = PDF_UnicodeFromAdobeName(name_glyph);
 
@@ -229,7 +233,7 @@
 
     bool bUnicode =
         FXFT_Select_Charmap(m_Font.GetFaceRec(), FT_ENCODING_UNICODE) == 0;
-    for (uint32_t charcode = 0; charcode < 256; charcode++) {
+    for (uint32_t charcode = 0; charcode < kInternalTableSize; charcode++) {
       const char* name =
           GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
       if (!name)
@@ -261,7 +265,7 @@
   }
 #endif  // defined(OS_APPLE)
   if (FontStyleIsSymbolic(m_Flags)) {
-    for (int charcode = 0; charcode < 256; charcode++) {
+    for (size_t charcode = 0; charcode < kInternalTableSize; charcode++) {
       const char* name =
           GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
       if (name) {
@@ -274,11 +278,10 @@
           wchar_t unicode =
               FT_UnicodeFromCharCode(PDFFONT_ENCODING_STANDARD, charcode);
           if (unicode == 0) {
-            char name_glyph[256];
-            memset(name_glyph, 0, sizeof(name_glyph));
+            char name_glyph[kInternalTableSize] = {};
             FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode],
-                              name_glyph, 256);
-            name_glyph[255] = 0;
+                              name_glyph, sizeof(name_glyph));
+            name_glyph[kInternalTableSize - 1] = 0;
             if (name_glyph[0] != 0)
               unicode = PDF_UnicodeFromAdobeName(name_glyph);
           }
@@ -295,7 +298,7 @@
 
   bool bUnicode =
       FXFT_Select_Charmap(m_Font.GetFaceRec(), FT_ENCODING_UNICODE) == 0;
-  for (int charcode = 0; charcode < 256; charcode++) {
+  for (size_t charcode = 0; charcode < kInternalTableSize; charcode++) {
     const char* name = GetAdobeCharName(m_BaseEncoding, m_CharNames, charcode);
     if (!name)
       continue;
@@ -341,10 +344,10 @@
 }
 
 void CPDF_Type1Font::CalcExtGID(uint32_t charcode) {
-  char name_glyph[256];
+  char name_glyph[kInternalTableSize] = {};
   FT_Get_Glyph_Name(m_Font.GetFaceRec(), m_GlyphIndex[charcode], name_glyph,
-                    256);
-  name_glyph[255] = 0;
+                    sizeof(name_glyph));
+  name_glyph[kInternalTableSize - 1] = 0;
   SetExtGID(name_glyph, charcode);
 }
 #endif  // defined(OS_APPLE)
diff --git a/core/fpdfapi/font/cpdf_type1font.h b/core/fpdfapi/font/cpdf_type1font.h
index f590a5b..96140b9 100644
--- a/core/fpdfapi/font/cpdf_type1font.h
+++ b/core/fpdfapi/font/cpdf_type1font.h
@@ -44,7 +44,7 @@
   void SetExtGID(const char* name, uint32_t charcode);
   void CalcExtGID(uint32_t charcode);
 
-  uint16_t m_ExtGID[256];
+  uint16_t m_ExtGID[kInternalTableSize];
 #endif
 
   Optional<CFX_FontMapper::StandardFont> m_Base14Font;