Prepare for converting FX_STRSIZE int->size_t

When turning on this conversion a number of typing issues and other nits where
found in the code base that can be merged in without actually changing the
underlying type. Landing these changes before the type change CL, since there is
a high likelihood that the type change will need to be rolled back, since it is
high risk.

BUG=pdfium:828

Change-Id: I587443d9090055963446485a1aacb8772eb5ca64
Reviewed-on: https://pdfium-review.googlesource.com/12810
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cmap.cpp b/core/fpdfapi/font/cpdf_cmap.cpp
index 9e8c48b..659139f 100644
--- a/core/fpdfapi/font/cpdf_cmap.cpp
+++ b/core/fpdfapi/font/cpdf_cmap.cpp
@@ -434,46 +434,46 @@
 int CPDF_CMap::AppendChar(char* str, uint32_t charcode) const {
   switch (m_CodingScheme) {
     case OneByte:
-      str[0] = (uint8_t)charcode;
+      str[0] = static_cast<char>(charcode);
       return 1;
     case TwoBytes:
-      str[0] = (uint8_t)(charcode / 256);
-      str[1] = (uint8_t)(charcode % 256);
+      str[0] = static_cast<char>(charcode / 256);
+      str[1] = static_cast<char>(charcode % 256);
       return 2;
     case MixedTwoBytes:
-      if (charcode < 0x100 && !m_MixedTwoByteLeadingBytes[(uint8_t)charcode]) {
-        str[0] = (uint8_t)charcode;
+      if (charcode < 0x100 && !m_MixedTwoByteLeadingBytes[charcode]) {
+        str[0] = static_cast<char>(charcode);
         return 1;
       }
-      str[0] = (uint8_t)(charcode >> 8);
-      str[1] = (uint8_t)charcode;
+      str[0] = static_cast<char>(charcode >> 8);
+      str[1] = static_cast<char>(charcode);
       return 2;
     case MixedFourBytes:
       if (charcode < 0x100) {
-        int iSize =
-            GetFourByteCharSizeImpl(charcode, m_MixedFourByteLeadingRanges);
+        int iSize = static_cast<int>(
+            GetFourByteCharSizeImpl(charcode, m_MixedFourByteLeadingRanges));
         if (iSize == 0)
           iSize = 1;
-        str[iSize - 1] = (uint8_t)charcode;
+        str[iSize - 1] = static_cast<char>(charcode);
         if (iSize > 1)
           memset(str, 0, iSize - 1);
         return iSize;
       }
       if (charcode < 0x10000) {
-        str[0] = (uint8_t)(charcode >> 8);
-        str[1] = (uint8_t)charcode;
+        str[0] = static_cast<char>(charcode >> 8);
+        str[1] = static_cast<char>(charcode);
         return 2;
       }
       if (charcode < 0x1000000) {
-        str[0] = (uint8_t)(charcode >> 16);
-        str[1] = (uint8_t)(charcode >> 8);
-        str[2] = (uint8_t)charcode;
+        str[0] = static_cast<char>(charcode >> 16);
+        str[1] = static_cast<char>(charcode >> 8);
+        str[2] = static_cast<char>(charcode);
         return 3;
       }
-      str[0] = (uint8_t)(charcode >> 24);
-      str[1] = (uint8_t)(charcode >> 16);
-      str[2] = (uint8_t)(charcode >> 8);
-      str[3] = (uint8_t)charcode;
+      str[0] = static_cast<char>(charcode >> 24);
+      str[1] = static_cast<char>(charcode >> 16);
+      str[2] = static_cast<char>(charcode >> 8);
+      str[3] = static_cast<char>(charcode);
       return 4;
   }
   return 0;
diff --git a/core/fxcrt/cfx_binarybuf.cpp b/core/fxcrt/cfx_binarybuf.cpp
index a1388b8..6c67912 100644
--- a/core/fxcrt/cfx_binarybuf.cpp
+++ b/core/fxcrt/cfx_binarybuf.cpp
@@ -51,7 +51,8 @@
   if (m_AllocSize >= new_size.ValueOrDie())
     return;
 
-  int alloc_step = std::max(128, m_AllocStep ? m_AllocStep : m_AllocSize / 4);
+  FX_STRSIZE alloc_step = std::max(static_cast<FX_STRSIZE>(128),
+                                   m_AllocStep ? m_AllocStep : m_AllocSize / 4);
   new_size += alloc_step - 1;  // Quantize, don't combine these lines.
   new_size /= alloc_step;
   new_size *= alloc_step;
diff --git a/core/fxcrt/cfx_seekablestreamproxy.cpp b/core/fxcrt/cfx_seekablestreamproxy.cpp
index da11707..a67ec52 100644
--- a/core/fxcrt/cfx_seekablestreamproxy.cpp
+++ b/core/fxcrt/cfx_seekablestreamproxy.cpp
@@ -230,15 +230,15 @@
 FX_STRSIZE CFX_SeekableStreamProxy::ReadString(wchar_t* pStr,
                                                FX_STRSIZE iMaxLength,
                                                bool* bEOS) {
-  ASSERT(pStr);
-  ASSERT(iMaxLength > 0);
+  if (!pStr || iMaxLength <= 0)
+    return 0;
 
   if (m_IsWriteStream)
     return 0;
 
   if (m_wCodePage == FX_CODEPAGE_UTF16LE ||
       m_wCodePage == FX_CODEPAGE_UTF16BE) {
-    FX_FILESIZE iBytes = iMaxLength * 2;
+    FX_STRSIZE iBytes = iMaxLength * 2;
     FX_STRSIZE iLen = ReadData(reinterpret_cast<uint8_t*>(pStr), iBytes);
     iMaxLength = iLen / 2;
     if (sizeof(wchar_t) > 2 && iMaxLength > 0)
diff --git a/core/fxcrt/cfx_seekablestreamproxy.h b/core/fxcrt/cfx_seekablestreamproxy.h
index 1a8e6f2..0e427fc 100644
--- a/core/fxcrt/cfx_seekablestreamproxy.h
+++ b/core/fxcrt/cfx_seekablestreamproxy.h
@@ -25,7 +25,7 @@
 
   FX_FILESIZE GetLength() const { return m_pStream->GetSize(); }
   FX_FILESIZE GetPosition() { return m_iPosition; }
-  FX_STRSIZE GetBOMLength() const { return std::max(0, m_wBOMLength); }
+  FX_STRSIZE GetBOMLength() const { return m_wBOMLength; }
   bool IsEOF() const { return m_iPosition >= GetLength(); }
 
   void Seek(From eSeek, FX_FILESIZE iOffset);
diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp
index ee5bbba..970bbd2 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -226,12 +226,12 @@
 }
 
 int CJS_PublicMethods::ParseStringInteger(const CFX_WideString& str,
-                                          int nStart,
-                                          int& nSkip,
-                                          int nMaxStep) {
+                                          FX_STRSIZE nStart,
+                                          FX_STRSIZE& nSkip,
+                                          FX_STRSIZE nMaxStep) {
   int nRet = 0;
   nSkip = 0;
-  for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
+  for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
     if (i - nStart > 10)
       break;
 
@@ -249,11 +249,11 @@
 }
 
 CFX_WideString CJS_PublicMethods::ParseStringString(const CFX_WideString& str,
-                                                    int nStart,
-                                                    int& nSkip) {
+                                                    FX_STRSIZE nStart,
+                                                    FX_STRSIZE& nSkip) {
   CFX_WideString swRet;
   nSkip = 0;
-  for (int i = nStart, sz = str.GetLength(); i < sz; i++) {
+  for (FX_STRSIZE i = nStart, sz = str.GetLength(); i < sz; i++) {
     wchar_t c = str[i];
     if (!std::iswdigit(c))
       break;
@@ -278,10 +278,10 @@
 
   int number[3];
 
-  int nSkip = 0;
-  int nLen = value.GetLength();
-  int nIndex = 0;
-  int i = 0;
+  FX_STRSIZE nSkip = 0;
+  FX_STRSIZE nLen = value.GetLength();
+  FX_STRSIZE nIndex = 0;
+  FX_STRSIZE i = 0;
   while (i < nLen) {
     if (nIndex > 2)
       break;
@@ -367,7 +367,7 @@
   bool bBadFormat = false;
 
   FX_STRSIZE i = 0;
-  int j = 0;
+  FX_STRSIZE j = 0;
 
   while (i < format.GetLength()) {
     if (bExit)
@@ -392,8 +392,8 @@
       case 'M':
       case 's':
       case 't': {
-        int oldj = j;
-        int nSkip = 0;
+        FX_STRSIZE oldj = j;
+        FX_STRSIZE nSkip = 0;
         FX_STRSIZE remaining = format.GetLength() - i - 1;
 
         if (remaining == 0 || format[i + 1] != c) {
@@ -806,7 +806,7 @@
   }
 
   // Processing separator style
-  if (iDec2 < strValue.GetLength()) {
+  if (static_cast<FX_STRSIZE>(iDec2) < strValue.GetLength()) {
     if (iSepStyle == 2 || iSepStyle == 3)
       strValue.Replace(".", ",");
 
@@ -983,8 +983,10 @@
 
   CFX_WideString wprefix = wstrValue.Left(pEvent->SelStart());
   CFX_WideString wpostfix;
-  if (pEvent->SelEnd() < wstrValue.GetLength())
-    wpostfix = wstrValue.Right(wstrValue.GetLength() - pEvent->SelEnd());
+  if (pEvent->SelEnd() >= 0 &&
+      static_cast<FX_STRSIZE>(pEvent->SelEnd()) < wstrValue.GetLength())
+    wpostfix = wstrValue.Right(wstrValue.GetLength() -
+                               static_cast<FX_STRSIZE>(pEvent->SelEnd()));
   val = wprefix + wstrChange + wpostfix;
   return true;
 }
@@ -1537,8 +1539,9 @@
     prefix = L"";
 
   if (pEventHandler->SelEnd() >= 0 &&
-      pEventHandler->SelEnd() <= swValue.GetLength())
-    postfix = swValue.Right(swValue.GetLength() - pEventHandler->SelEnd());
+      static_cast<FX_STRSIZE>(pEventHandler->SelEnd()) <= swValue.GetLength())
+    postfix = swValue.Right(swValue.GetLength() -
+                            static_cast<FX_STRSIZE>(pEventHandler->SelEnd()));
   else
     postfix = L"";
 
diff --git a/fpdfsdk/javascript/PublicMethods.h b/fpdfsdk/javascript/PublicMethods.h
index 0f6123c..c373f51 100644
--- a/fpdfsdk/javascript/PublicMethods.h
+++ b/fpdfsdk/javascript/PublicMethods.h
@@ -133,12 +133,12 @@
   JS_STATIC_DECLARE_GLOBAL_FUN();
 
   static int ParseStringInteger(const CFX_WideString& string,
-                                int nStart,
-                                int& nSkip,
-                                int nMaxStep);
+                                FX_STRSIZE nStart,
+                                FX_STRSIZE& nSkip,
+                                FX_STRSIZE nMaxStep);
   static CFX_WideString ParseStringString(const CFX_WideString& string,
-                                          int nStart,
-                                          int& nSkip);
+                                          FX_STRSIZE nStart,
+                                          FX_STRSIZE& nSkip);
   static double MakeRegularDate(const CFX_WideString& value,
                                 const CFX_WideString& format,
                                 bool* bWrongFormat);
diff --git a/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
index 9a48c1e..6d8d51d 100644
--- a/fxbarcode/oned/BC_OnedEAN13Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN13Writer.cpp
@@ -27,6 +27,7 @@
 #include <memory>
 #include <vector>
 
+#include "core/fxcrt/fx_extension.h"
 #include "core/fxge/cfx_defaultrenderdevice.h"
 #include "fxbarcode/BC_Writer.h"
 #include "fxbarcode/oned/BC_OneDimWriter.h"
@@ -81,15 +82,14 @@
   FX_STRSIZE j = 1;
   for (FX_STRSIZE i = 0; i < contents.GetLength(); i++) {
     if (j % 2) {
-      odd += FXSYS_atoi(contents.Mid(i, 1).c_str());
+      odd += FXSYS_DecimalCharToInt(contents[i]);
     } else {
-      even += FXSYS_atoi(contents.Mid(i, 1).c_str());
+      even += FXSYS_DecimalCharToInt(contents[i]);
     }
     j++;
   }
-  int32_t checksum = (odd * 3 + even) % 10;
-  checksum = (10 - checksum) % 10;
-  return (checksum);
+  int32_t checksum = 10 - (odd * 3 + even) % 10;
+  return checksum;
 }
 
 uint8_t* CBC_OnedEAN13Writer::EncodeWithHint(const CFX_ByteString& contents,