Use WideStringFromFPDFWideString() in more places.

Also rename CFXByteStringFromFPDFWideString() to
ByteStringFromFPDFWideString(), and implement it using
WideStringFromFPDFWideString().

Change-Id: I323bf69f90cdec78bbee562b9aed6513599c9e33
Reviewed-on: https://pdfium-review.googlesource.com/c/47692
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 2b5b3ee..7e39154 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -175,10 +175,8 @@
   return page ? IPDFPageFromFPDFPage(page)->AsPDFPage() : nullptr;
 }
 
-ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
-  return WideString::FromUTF16LE(wide_string,
-                                 WideString::WStringLength(wide_string))
-      .ToUTF8();
+ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
+  return WideStringFromFPDFWideString(wide_string).ToUTF8();
 }
 
 WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string) {
diff --git a/fpdfsdk/cpdfsdk_helpers.h b/fpdfsdk/cpdfsdk_helpers.h
index 30687bf..31ffc9d 100644
--- a/fpdfsdk/cpdfsdk_helpers.h
+++ b/fpdfsdk/cpdfsdk_helpers.h
@@ -206,7 +206,7 @@
   return reinterpret_cast<CPDFSDK_FormFillEnvironment*>(handle);
 }
 
-ByteString CFXByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
+ByteString ByteStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
 
 WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string);
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 2660ccc..fa13c2c 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -785,7 +785,7 @@
     if (!pApDict)
       pApDict = pAnnotDict->SetNewFor<CPDF_Dictionary>("AP");
 
-    ByteString newValue = CFXByteStringFromFPDFWideString(value);
+    ByteString newValue = ByteStringFromFPDFWideString(value);
     auto pNewApStream = pdfium::MakeUnique<CPDF_Stream>();
     pNewApStream->SetData(newValue.AsRawSpan());
     pApDict->SetFor(modeKey, std::move(pNewApStream));
diff --git a/fpdfsdk/fpdf_attachment.cpp b/fpdfsdk/fpdf_attachment.cpp
index de7389d..3db89a8 100644
--- a/fpdfsdk/fpdf_attachment.cpp
+++ b/fpdfsdk/fpdf_attachment.cpp
@@ -59,15 +59,17 @@
 FPDF_EXPORT FPDF_ATTACHMENT FPDF_CALLCONV
 FPDFDoc_AddAttachment(FPDF_DOCUMENT document, FPDF_WIDESTRING name) {
   CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
-  WideString wsName =
-      WideString::FromUTF16LE(name, WideString::WStringLength(name));
-  if (!pDoc || wsName.IsEmpty())
+  if (!pDoc)
     return nullptr;
 
   CPDF_Dictionary* pRoot = pDoc->GetRoot();
   if (!pRoot)
     return nullptr;
 
+  WideString wsName = WideStringFromFPDFWideString(name);
+  if (wsName.IsEmpty())
+    return nullptr;
+
   // Retrieve the document's Names dictionary; create it if missing.
   CPDF_Dictionary* pNames = pRoot->GetDictFor("Names");
   if (!pNames) {
@@ -170,7 +172,7 @@
     return false;
 
   ByteString bsKey = key;
-  ByteString bsValue = CFXByteStringFromFPDFWideString(value);
+  ByteString bsValue = ByteStringFromFPDFWideString(value);
   bool bEncodedAsHex = bsKey == kChecksumKey;
   if (bEncodedAsHex)
     bsValue = CFXByteStringHexDecode(bsValue);
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index e9224d1..52e1e6b 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -101,12 +101,11 @@
   if (!pDoc)
     return nullptr;
 
-  if (!title || title[0] == 0)
+  WideString encodedTitle = WideStringFromFPDFWideString(title);
+  if (encodedTitle.IsEmpty())
     return nullptr;
 
   CPDF_BookmarkTree tree(pDoc);
-  size_t len = WideString::WStringLength(title);
-  WideString encodedTitle = WideString::FromUTF16LE(title, len);
   std::set<const CPDF_Dictionary*> visited;
   return FPDFBookmarkFromCPDFDictionary(
       FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict());
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 2478c23..bcab5eb 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -452,8 +452,7 @@
   if (!pTextObj)
     return false;
 
-  size_t len = WideString::WStringLength(text);
-  WideString encodedText = WideString::FromUTF16LE(text, len);
+  WideString encodedText = WideStringFromFPDFWideString(text);
   ByteString byteText;
   for (wchar_t wc : encodedText) {
     pTextObj->GetFont()->AppendChar(
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 5e2fab6..4bbc392 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -532,10 +532,7 @@
   if (!pPageView)
     return;
 
-  size_t len = WideString::WStringLength(wsText);
-  WideString wide_str_text = WideString::FromUTF16LE(wsText, len);
-
-  pPageView->ReplaceSelection(wide_str_text);
+  pPageView->ReplaceSelection(WideStringFromFPDFWideString(wsText));
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_CanUndo(FPDF_FORMHANDLE hHandle,
diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp
index 465c18e..044624b 100644
--- a/fpdfsdk/fpdf_text.cpp
+++ b/fpdfsdk/fpdf_text.cpp
@@ -281,9 +281,8 @@
 
   CPDF_TextPageFind* textpageFind =
       new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page));
-  size_t len = WideString::WStringLength(findwhat);
   textpageFind->FindFirst(
-      WideString::FromUTF16LE(findwhat, len), flags,
+      WideStringFromFPDFWideString(findwhat), flags,
       start_index >= 0 ? Optional<size_t>(start_index) : Optional<size_t>());
   return FPDFSchHandleFromCPDFTextPageFind(textpageFind);
 }