Fix some 64 to 32 conversions for Windows.

One inconvenience is that it turns out the tellp() doesn't
actually return a numeric value, rather something that is
explicitly convertible to a numeric value. So we can't directly
apply the checked conversion macros to such results.

Change-Id: I2108536b30d5de22065e791156fa1796ce1ac44f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/88531
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/win32/cfx_psrenderer.cpp b/core/fxge/win32/cfx_psrenderer.cpp
index 69d2add..9cca4c7 100644
--- a/core/fxge/win32/cfx_psrenderer.cpp
+++ b/core/fxge/win32/cfx_psrenderer.cpp
@@ -35,6 +35,7 @@
 #include "core/fxge/text_char_pos.h"
 #include "core/fxge/win32/cfx_psfonttracker.h"
 #include "third_party/base/check_op.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 namespace {
 
@@ -252,14 +253,17 @@
   m_bInited = false;
 
   // Flush `m_PreambleOutput` if it is not empty.
-  std::streampos preamble_pos = m_PreambleOutput.tellp();
+  std::streamoff preamble_pos = m_PreambleOutput.tellp();
   if (preamble_pos > 0) {
-    m_pStream->WriteBlock(m_PreambleOutput.str().c_str(), preamble_pos);
+    m_pStream->WriteBlock(m_PreambleOutput.str().c_str(),
+                          pdfium::base::checked_cast<size_t>(preamble_pos));
     m_PreambleOutput.str(std::string());
   }
 
   // Flush `m_Output`. It's never empty because of the WriteString() call above.
-  m_pStream->WriteBlock(m_Output.str().c_str(), m_Output.tellp());
+  m_pStream->WriteBlock(
+      m_Output.str().c_str(),
+      pdfium::base::checked_cast<size_t>(std::streamoff(m_Output.tellp())));
   m_Output.str(std::string());
 }
 
@@ -588,8 +592,9 @@
       }
       uint8_t* compressed_buf;
       uint32_t compressed_size;
-      PSCompressData(output_buf, output_size, &compressed_buf, &compressed_size,
-                     &filter);
+      PSCompressData(output_buf,
+                     pdfium::base::checked_cast<uint32_t>(output_size),
+                     &compressed_buf, &compressed_size, &filter);
       if (output_buf != compressed_buf)
         FX_Free(output_buf);
 
@@ -646,7 +651,7 @@
         found = true;
       }
       if (found) {
-        *ps_fontnum = i / 256;
+        *ps_fontnum = pdfium::base::checked_cast<int>(i / 256);
         *ps_glyphindex = i % 256;
         return;
       }
@@ -654,7 +659,8 @@
   }
 
   m_PSFontList.push_back(std::make_unique<Glyph>(pFont, charpos.m_GlyphIndex));
-  *ps_fontnum = (m_PSFontList.size() - 1) / 256;
+  *ps_fontnum =
+      pdfium::base::checked_cast<int>((m_PSFontList.size() - 1) / 256);
   *ps_glyphindex = (m_PSFontList.size() - 1) % 256;
   if (*ps_glyphindex == 0) {
     std::ostringstream buf;
@@ -886,8 +892,11 @@
 }
 
 void CFX_PSRenderer::WriteStream(std::ostringstream& stream) {
-  if (stream.tellp() > 0)
-    m_Output.write(stream.str().c_str(), stream.tellp());
+  std::streamoff output_pos = stream.tellp();
+  if (output_pos > 0) {
+    m_Output.write(stream.str().c_str(),
+                   pdfium::base::checked_cast<size_t>(output_pos));
+  }
 }
 
 void CFX_PSRenderer::WriteString(ByteStringView str) {
diff --git a/core/fxge/win32/cgdi_device_driver.cpp b/core/fxge/win32/cgdi_device_driver.cpp
index 1989eda..7cae0b3 100644
--- a/core/fxge/win32/cgdi_device_driver.cpp
+++ b/core/fxge/win32/cgdi_device_driver.cpp
@@ -22,6 +22,7 @@
 #include "third_party/base/check.h"
 #include "third_party/base/check_op.h"
 #include "third_party/base/notreached.h"
+#include "third_party/base/numerics/safe_conversions.h"
 
 #if !defined(_SKIA_SUPPORT_)
 #include "core/fxge/agg/fx_agg_driver.h"
@@ -98,9 +99,10 @@
       dashes[i] = std::max(dashes[i], 1U);
     }
   }
-  return ExtCreatePen(PenStyle, (DWORD)ceil(width), &lb,
-                      pGraphState->m_DashArray.size(),
-                      reinterpret_cast<const DWORD*>(dashes.data()));
+  return ExtCreatePen(
+      PenStyle, (DWORD)ceil(width), &lb,
+      pdfium::base::checked_cast<DWORD>(pGraphState->m_DashArray.size()),
+      reinterpret_cast<const DWORD*>(dashes.data()));
 }
 
 HBRUSH CreateBrush(uint32_t argb) {
diff --git a/core/fxge/win32/cgdi_plus_ext.cpp b/core/fxge/win32/cgdi_plus_ext.cpp
index 1618dfb..4db571b 100644
--- a/core/fxge/win32/cgdi_plus_ext.cpp
+++ b/core/fxge/win32/cgdi_plus_ext.cpp
@@ -433,7 +433,8 @@
     if (m_ReadPos >= m_InterStream.tellp())
       return HRESULT_FROM_WIN32(ERROR_END_OF_MEDIA);
 
-    size_t bytes_left = m_InterStream.tellp() - m_ReadPos;
+    size_t bytes_left = pdfium::base::checked_cast<size_t>(
+        std::streamoff(m_InterStream.tellp()) - m_ReadPos);
     size_t bytes_out =
         std::min(pdfium::base::checked_cast<size_t>(cb), bytes_left);
     memcpy(output, m_InterStream.str().c_str() + m_ReadPos, bytes_out);
@@ -624,9 +625,9 @@
   std::vector<BYTE> gp_types(points.size());
   int nSubPathes = 0;
   bool bSubClose = false;
-  int pos_subclose = 0;
   bool bSmooth = false;
-  int startpoint = 0;
+  size_t pos_subclose = 0;
+  size_t startpoint = 0;
   for (size_t i = 0; i < points.size(); ++i) {
     gp_points[i].X = points[i].m_Point.x;
     gp_points[i].Y = points[i].m_Point.y;
@@ -714,7 +715,8 @@
   Gdiplus::GpPath* pGpPath = nullptr;
   const Gdiplus::GpFillMode gp_fill_mode =
       FillType2Gdip(fill_options.fill_type);
-  CallFunc(GdipCreatePath2)(gp_points.data(), gp_types.data(), points.size(),
+  CallFunc(GdipCreatePath2)(gp_points.data(), gp_types.data(),
+                            pdfium::base::checked_cast<int>(points.size()),
                             gp_fill_mode, &pGpPath);
   if (!pGpPath) {
     if (pMatrix)
@@ -736,13 +738,15 @@
     if (nSubPathes == 1) {
       CallFunc(GdipDrawPath)(pGraphics, pPen, pGpPath);
     } else {
-      int iStart = 0;
+      size_t iStart = 0;
       for (size_t i = 0; i < points.size(); ++i) {
         if (i == points.size() - 1 ||
             gp_types[i + 1] == Gdiplus::PathPointTypeStart) {
           Gdiplus::GpPath* pSubPath;
-          CallFunc(GdipCreatePath2)(&gp_points[iStart], &gp_types[iStart],
-                                    i - iStart + 1, gp_fill_mode, &pSubPath);
+          CallFunc(GdipCreatePath2)(
+              &gp_points[iStart], &gp_types[iStart],
+              pdfium::base::checked_cast<int>(i - iStart + 1), gp_fill_mode,
+              &pSubPath);
           iStart = i + 1;
           CallFunc(GdipDrawPath)(pGraphics, pPen, pSubPath);
           CallFunc(GdipDeletePath)(pSubPath);
diff --git a/core/fxge/win32/cpsoutput.cpp b/core/fxge/win32/cpsoutput.cpp
index 5b39f10..3dfa861 100644
--- a/core/fxge/win32/cpsoutput.cpp
+++ b/core/fxge/win32/cpsoutput.cpp
@@ -25,11 +25,11 @@
 
     switch (m_mode) {
       case OutputMode::kExtEscape:
-        ExtEscape(m_hDC, PASSTHROUGH, send_len + 2,
+        ExtEscape(m_hDC, PASSTHROUGH, static_cast<int>(send_len + 2),
                   reinterpret_cast<const char*>(buffer), 0, nullptr);
         break;
       case OutputMode::kGdiComment:
-        GdiComment(m_hDC, send_len + 2, buffer);
+        GdiComment(m_hDC, static_cast<UINT>(send_len + 2), buffer);
         break;
     }
     input = input.subspan(send_len);
diff --git a/core/fxge/win32/ctext_only_printer_driver.cpp b/core/fxge/win32/ctext_only_printer_driver.cpp
index 4950f17..eb847fb 100644
--- a/core/fxge/win32/ctext_only_printer_driver.cpp
+++ b/core/fxge/win32/ctext_only_printer_driver.cpp
@@ -176,7 +176,7 @@
     size_t send_len = std::min<size_t>(text_span.size(), 1024);
     *(reinterpret_cast<uint16_t*>(buffer)) = static_cast<uint16_t>(send_len);
     memcpy(buffer + 2, text_span.data(), send_len);
-    ::GdiComment(m_hDC, send_len + 2, buffer);
+    ::GdiComment(m_hDC, static_cast<UINT>(send_len + 2), buffer);
     text_span = text_span.subspan(send_len);
   }
   return true;
diff --git a/core/fxge/win32/cwin32_platform.cpp b/core/fxge/win32/cwin32_platform.cpp
index 1c36a53..5e9dc83 100644
--- a/core/fxge/win32/cwin32_platform.cpp
+++ b/core/fxge/win32/cwin32_platform.cpp
@@ -7,11 +7,13 @@
 #include "core/fxge/win32/cwin32_platform.h"
 
 #include <memory>
+#include <utility>
 
 #include "core/fxcrt/fx_codepage.h"
 #include "core/fxge/cfx_folderfontinfo.h"
 #include "core/fxge/cfx_gemodule.h"
 #include "third_party/base/cxx17_backports.h"
+#include "third_party/base/numerics/safe_conversions.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/win/win_util.h"
 
@@ -399,7 +401,9 @@
                                         pdfium::span<uint8_t> buffer) {
   HFONT hOldFont = (HFONT)::SelectObject(m_hDC, (HFONT)hFont);
   table = FXSYS_UINT32_GET_MSBFIRST(reinterpret_cast<uint8_t*>(&table));
-  uint32_t size = ::GetFontData(m_hDC, table, 0, buffer.data(), buffer.size());
+  uint32_t size =
+      ::GetFontData(m_hDC, table, 0, buffer.data(),
+                    pdfium::base::checked_cast<DWORD>(buffer.size()));
   ::SelectObject(m_hDC, hOldFont);
   if (size == GDI_ERROR) {
     return 0;