Add IFX_WriteableStream::WriteFilesize().

Consolidate some similar code into fx_stream.cpp. Make the 32 bit method
consistent with this one:

-- null initialize, just because someday.
-- smaller buffer size is ok.

Change-Id: Ic4af306d37068e683a9a994aea67f06325c55cfd
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/82973
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp
index 3dc2de2..cc7a444 100644
--- a/core/fpdfapi/edit/cpdf_creator.cpp
+++ b/core/fpdfapi/edit/cpdf_creator.cpp
@@ -479,13 +479,7 @@
   if (m_IsIncremental) {
     FX_FILESIZE prev = m_pParser->GetLastXRefOffset();
     if (prev) {
-      if (!m_Archive->WriteString("/Prev "))
-        return Stage::kInvalid;
-
-      char offset_buf[20];
-      memset(offset_buf, 0, sizeof(offset_buf));
-      FXSYS_i64toa(prev, offset_buf, 10);
-      if (!m_Archive->WriteBlock(offset_buf, strlen(offset_buf)))
+      if (!m_Archive->WriteString("/Prev ") || !m_Archive->WriteFilesize(prev))
         return Stage::kInvalid;
     }
   }
@@ -544,13 +538,8 @@
       return Stage::kInvalid;
   }
 
-  if (!m_Archive->WriteString("\r\nstartxref\r\n"))
-    return Stage::kInvalid;
-
-  char offset_buf[20];
-  memset(offset_buf, 0, sizeof(offset_buf));
-  FXSYS_i64toa(m_XrefStart, offset_buf, 10);
-  if (!m_Archive->WriteBlock(offset_buf, strlen(offset_buf)) ||
+  if (!m_Archive->WriteString("\r\nstartxref\r\n") ||
+      !m_Archive->WriteFilesize(m_XrefStart) ||
       !m_Archive->WriteString("\r\n%%EOF\r\n")) {
     return Stage::kInvalid;
   }
diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp
index c2c5cc4..7eadefd 100644
--- a/core/fxcrt/fx_stream.cpp
+++ b/core/fxcrt/fx_stream.cpp
@@ -78,11 +78,17 @@
 }
 
 bool IFX_WriteStream::WriteDWord(uint32_t i) {
-  char buf[32];
+  char buf[20] = {};
   FXSYS_itoa(i, buf, 10);
   return WriteBlock(buf, strlen(buf));
 }
 
+bool IFX_WriteStream::WriteFilesize(FX_FILESIZE size) {
+  char buf[20] = {};
+  FXSYS_i64toa(size, buf, 10);
+  return WriteBlock(buf, strlen(buf));
+}
+
 // static
 RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename(
     const char* filename,
diff --git a/core/fxcrt/fx_stream.h b/core/fxcrt/fx_stream.h
index 576efb6..1e947b8 100644
--- a/core/fxcrt/fx_stream.h
+++ b/core/fxcrt/fx_stream.h
@@ -38,6 +38,7 @@
   bool WriteString(ByteStringView str);
   bool WriteByte(uint8_t byte);
   bool WriteDWord(uint32_t i);
+  bool WriteFilesize(FX_FILESIZE size);
 
  protected:
   virtual ~IFX_WriteStream() = default;