Revert "Remove CFX_ByteTextBuf from cpdfsdk_interform.cpp and others."

This reverts commit aea80dcc0abc0c310316fa502f91a359bc684758.

Reason for revert: WriteBuf's behavior was altered

Original change's description:
> Remove CFX_ByteTextBuf from cpdfsdk_interform.cpp and others.
> 
> Bug: pdfium:731
> Change-Id: I61d38ab3f2b0ac68b8479ade25bab50f3a73c27b
> Reviewed-on: https://pdfium-review.googlesource.com/9770
> Reviewed-by: Tom Sepez <tsepez@chromium.org>
> Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>

TBR=tsepez@chromium.org,hnakashima@chromium.org,rharrison@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: pdfium:731
Change-Id: Ic66a2641c259173fb3792f7a4206e19ee4707fc9
Reviewed-on: https://pdfium-review.googlesource.com/10150
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp
index b008069..85e2f6e 100644
--- a/core/fpdfapi/parser/cfdf_document.cpp
+++ b/core/fpdfapi/parser/cfdf_document.cpp
@@ -7,7 +7,6 @@
 #include "core/fpdfapi/parser/cfdf_document.h"
 
 #include <memory>
-#include <sstream>
 #include <utility>
 
 #include "core/fpdfapi/edit/cpdf_creator.h"
@@ -89,11 +88,10 @@
   }
 }
 
-CFX_ByteString CFDF_Document::WriteToString() const {
+bool CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const {
   if (!m_pRootDict)
-    return CFX_ByteString();
+    return false;
 
-  std::ostringstream buf;
   buf << "%FDF-1.2\r\n";
   for (const auto& pair : *this)
     buf << pair.first << " 0 obj\r\n"
@@ -101,6 +99,5 @@
 
   buf << "trailer\r\n<</Root " << m_pRootDict->GetObjNum()
       << " 0 R>>\r\n%%EOF\r\n";
-
-  return CFX_ByteString(buf);
+  return true;
 }
diff --git a/core/fpdfapi/parser/cfdf_document.h b/core/fpdfapi/parser/cfdf_document.h
index 45bf83d..9a13877 100644
--- a/core/fpdfapi/parser/cfdf_document.h
+++ b/core/fpdfapi/parser/cfdf_document.h
@@ -27,7 +27,7 @@
   CFDF_Document();
   ~CFDF_Document() override;
 
-  CFX_ByteString WriteToString() const;
+  bool WriteBuf(CFX_ByteTextBuf& buf) const;
   CPDF_Dictionary* GetRoot() const { return m_pRootDict.Get(); }
 
  protected:
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index 27db105..552bb31 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -8,8 +8,6 @@
 
 #include <algorithm>
 #include <memory>
-#include <sstream>
-#include <string>
 #include <vector>
 
 #include "core/fpdfapi/page/cpdf_page.h"
@@ -438,28 +436,21 @@
                                      const std::vector<CPDF_FormField*>& fields,
                                      bool bIncludeOrExclude,
                                      bool bUrlEncoded) {
-  CFX_ByteString textBuf = ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude);
+  CFX_ByteTextBuf textBuf;
+  ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
 
+  uint8_t* pBuffer = textBuf.GetBuffer();
   FX_STRSIZE nBufSize = textBuf.GetLength();
-  if (nBufSize == 0)
-    return false;
 
-  uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize);
-  memcpy(pLocalBuffer, textBuf.c_str(), nBufSize);
-  uint8_t* pBuffer = pLocalBuffer;
-
-  if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) {
-    FX_Free(pLocalBuffer);
+  if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
     return false;
-  }
 
   m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
+  return true;
+}
 
-  if (pBuffer != pLocalBuffer)
-    FX_Free(pBuffer);
-
-  FX_Free(pLocalBuffer);
-
+bool CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
+                                            CFX_WideString csTxtFile) {
   return true;
 }
 
@@ -478,7 +469,7 @@
   if (!pFields)
     return false;
 
-  std::ostringstream fdfEncodedData;
+  CFX_ByteTextBuf fdfEncodedData;
   for (uint32_t i = 0; i < pFields->GetCount(); i++) {
     CPDF_Dictionary* pField = pFields->GetDictAt(i);
     if (!pField)
@@ -499,22 +490,19 @@
       fdfEncodedData << "&";
   }
 
-  nBufSize = fdfEncodedData.tellp();
-  if (nBufSize == 0)
-    return false;
-
+  nBufSize = fdfEncodedData.GetLength();
   pBuf = FX_Alloc(uint8_t, nBufSize);
-  memcpy(pBuf, fdfEncodedData.str().c_str(), nBufSize);
+  memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
   return true;
 }
 
-CFX_ByteString CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
+bool CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
     const std::vector<CPDF_FormField*>& fields,
-    bool bIncludeOrExclude) {
+    bool bIncludeOrExclude,
+    CFX_ByteTextBuf& textBuf) {
   std::unique_ptr<CFDF_Document> pFDF = m_pInterForm->ExportToFDF(
       m_pFormFillEnv->JS_docGetFilePath(), fields, bIncludeOrExclude, false);
-
-  return pFDF ? pFDF->WriteToString() : CFX_ByteString();
+  return pFDF ? pFDF->WriteBuf(textBuf) : false;
 }
 
 CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
@@ -535,36 +523,26 @@
   if (!pFDFDoc)
     return false;
 
-  CFX_ByteString fdfBuffer = pFDFDoc->WriteToString();
-
-  FX_STRSIZE nBufSize = fdfBuffer.GetLength();
-  if (nBufSize == 0)
+  CFX_ByteTextBuf FdfBuffer;
+  if (!pFDFDoc->WriteBuf(FdfBuffer))
     return false;
 
-  uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize);
-  memcpy(pLocalBuffer, fdfBuffer.c_str(), nBufSize);
-  uint8_t* pBuffer = pLocalBuffer;
-
-  if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) {
-    FX_Free(pLocalBuffer);
+  uint8_t* pBuffer = FdfBuffer.GetBuffer();
+  FX_STRSIZE nBufSize = FdfBuffer.GetLength();
+  if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
     return false;
-  }
 
   m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
-
-  if (pBuffer != pLocalBuffer)
+  if (bUrlEncoded)
     FX_Free(pBuffer);
 
-  FX_Free(pLocalBuffer);
-
   return true;
 }
 
-CFX_ByteString CPDFSDK_InterForm::ExportFormToFDFTextBuf() {
+bool CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
   std::unique_ptr<CFDF_Document> pFDF =
       m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false);
-
-  return pFDF ? pFDF->WriteToString() : CFX_ByteString();
+  return pFDF && pFDF->WriteBuf(textBuf);
 }
 
 bool CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
diff --git a/fpdfsdk/cpdfsdk_interform.h b/fpdfsdk/cpdfsdk_interform.h
index c4a1ff1..b613c73 100644
--- a/fpdfsdk/cpdfsdk_interform.h
+++ b/fpdfsdk/cpdfsdk_interform.h
@@ -90,10 +90,10 @@
                     bool bIncludeOrExclude,
                     bool bUrlEncoded);
   bool SubmitForm(const CFX_WideString& sDestination, bool bUrlEncoded);
-  CFX_ByteString ExportFormToFDFTextBuf();
-  CFX_ByteString ExportFieldsToFDFTextBuf(
-      const std::vector<CPDF_FormField*>& fields,
-      bool bIncludeOrExclude);
+  bool ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
+  bool ExportFieldsToFDFTextBuf(const std::vector<CPDF_FormField*>& fields,
+                                bool bIncludeOrExclude,
+                                CFX_ByteTextBuf& textBuf);
   CFX_WideString GetTemporaryFileName(const CFX_WideString& sFileExt);
 
   bool IsNeedHighLight(int nFieldType);
@@ -117,6 +117,7 @@
   int BeforeFormImportData(CPDF_InterForm* pForm) override;
   void AfterFormImportData(CPDF_InterForm* pForm) override;
 
+  bool FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile);
   bool FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize);
   int GetPageIndexByAnnotDict(CPDF_Document* pDocument,
                               CPDF_Dictionary* pAnnotDict) const;
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 77878a5..ba4b2ae 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -7,7 +7,6 @@
 #include "fpdfsdk/javascript/Document.h"
 
 #include <algorithm>
-#include <sstream>
 #include <utility>
 #include <vector>
 
@@ -383,21 +382,16 @@
       iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
   CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
   CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
-  CFX_ByteString sTextBuf = pInterForm->ExportFormToFDFTextBuf();
-  if (sTextBuf.GetLength() == 0)
+  CFX_ByteTextBuf textBuf;
+  if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
     return false;
 
-  FX_STRSIZE nBufSize = sTextBuf.GetLength();
-  char* pMutableBuf = FX_Alloc(char, nBufSize);
-  memcpy(pMutableBuf, sTextBuf.c_str(), nBufSize);
-
   pRuntime->BeginBlock();
   CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
-  pFormFillEnv->JS_docmailForm(pMutableBuf, nBufSize, bUI, cTo.c_str(),
-                               cSubject.c_str(), cCc.c_str(), cBcc.c_str(),
-                               cMsg.c_str());
+  pFormFillEnv->JS_docmailForm(textBuf.GetBuffer(), textBuf.GetLength(), bUI,
+                               cTo.c_str(), cSubject.c_str(), cCc.c_str(),
+                               cBcc.c_str(), cMsg.c_str());
   pRuntime->EndBlock();
-  FX_Free(pMutableBuf);
   return true;
 }