Make PDF_DecodeText() take a span.

Retain just one version of PDF_DecodeText() instead of having two.

Change-Id: Ibab6b59f62174e19341a3563dd3e4cf16c5b1c50
Reviewed-on: https://pdfium-review.googlesource.com/c/48190
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_name.cpp b/core/fpdfapi/parser/cpdf_name.cpp
index 721c58a..46c1a1a 100644
--- a/core/fpdfapi/parser/cpdf_name.cpp
+++ b/core/fpdfapi/parser/cpdf_name.cpp
@@ -48,7 +48,7 @@
 }
 
 WideString CPDF_Name::GetUnicodeText() const {
-  return PDF_DecodeText(m_Name);
+  return PDF_DecodeText(m_Name.AsRawSpan());
 }
 
 bool CPDF_Name::WriteTo(IFX_ArchiveStream* archive,
diff --git a/core/fpdfapi/parser/cpdf_stream.cpp b/core/fpdfapi/parser/cpdf_stream.cpp
index 6ebca95..0c6b5c4 100644
--- a/core/fpdfapi/parser/cpdf_stream.cpp
+++ b/core/fpdfapi/parser/cpdf_stream.cpp
@@ -176,7 +176,7 @@
 WideString CPDF_Stream::GetUnicodeText() const {
   auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(this);
   pAcc->LoadAllDataFiltered();
-  return PDF_DecodeText(pAcc->GetData(), pAcc->GetSize());
+  return PDF_DecodeText(pAcc->GetSpan());
 }
 
 bool CPDF_Stream::WriteTo(IFX_ArchiveStream* archive,
diff --git a/core/fpdfapi/parser/cpdf_string.cpp b/core/fpdfapi/parser/cpdf_string.cpp
index 08f3db3..5a6222e 100644
--- a/core/fpdfapi/parser/cpdf_string.cpp
+++ b/core/fpdfapi/parser/cpdf_string.cpp
@@ -64,7 +64,7 @@
 }
 
 WideString CPDF_String::GetUnicodeText() const {
-  return PDF_DecodeText(m_String);
+  return PDF_DecodeText(m_String.AsRawSpan());
 }
 
 bool CPDF_String::WriteTo(IFX_ArchiveStream* archive,
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index 5e496fd..a2529c5 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -454,20 +454,20 @@
   return true;
 }
 
-WideString PDF_DecodeText(const uint8_t* src_data, uint32_t src_len) {
+WideString PDF_DecodeText(pdfium::span<const uint8_t> span) {
   int dest_pos = 0;
   WideString result;
-  if (src_len >= 2 && ((src_data[0] == 0xfe && src_data[1] == 0xff) ||
-                       (src_data[0] == 0xff && src_data[1] == 0xfe))) {
-    uint32_t max_chars = (src_len - 2) / 2;
+  if (span.size() >= 2 && ((span[0] == 0xfe && span[1] == 0xff) ||
+                           (span[0] == 0xff && span[1] == 0xfe))) {
+    uint32_t max_chars = (span.size() - 2) / 2;
     if (!max_chars)
       return result;
 
     pdfium::span<wchar_t> dest_buf = result.GetBuffer(max_chars);
     uint16_t (*GetUnicodeFromBytes)(const uint8_t*) =
-        src_data[0] == 0xfe ? GetUnicodeFromBigEndianBytes
-                            : GetUnicodeFromLittleEndianBytes;
-    const uint8_t* unicode_str = src_data + 2;
+        span[0] == 0xfe ? GetUnicodeFromBigEndianBytes
+                        : GetUnicodeFromLittleEndianBytes;
+    const uint8_t* unicode_str = &span[2];
     for (uint32_t i = 0; i < max_chars * 2; i += 2) {
       uint16_t unicode = GetUnicodeFromBytes(unicode_str + i);
 
@@ -490,20 +490,15 @@
       dest_buf[dest_pos++] = unicode;
     }
   } else {
-    pdfium::span<wchar_t> dest_buf = result.GetBuffer(src_len);
-    for (uint32_t i = 0; i < src_len; ++i)
-      dest_buf[i] = PDFDocEncoding[src_data[i]];
-    dest_pos = src_len;
+    pdfium::span<wchar_t> dest_buf = result.GetBuffer(span.size());
+    for (uint32_t i = 0; i < span.size(); ++i)
+      dest_buf[i] = PDFDocEncoding[span[i]];
+    dest_pos = span.size();
   }
   result.ReleaseBuffer(dest_pos);
   return result;
 }
 
-WideString PDF_DecodeText(const ByteString& bstr) {
-  return PDF_DecodeText(reinterpret_cast<const uint8_t*>(bstr.c_str()),
-                        bstr.GetLength());
-}
-
 ByteString PDF_EncodeText(const WideString& str) {
   size_t i = 0;
   size_t len = str.GetLength();
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h
index 2652630..25213c5 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.h
+++ b/core/fpdfapi/parser/fpdf_parser_decode.h
@@ -24,8 +24,7 @@
 bool ValidateDecoderPipeline(const CPDF_Array* pDecoders);
 
 ByteString PDF_EncodeString(const ByteString& src, bool bHex);
-WideString PDF_DecodeText(const uint8_t* pData, uint32_t size);
-WideString PDF_DecodeText(const ByteString& bstr);
+WideString PDF_DecodeText(pdfium::span<const uint8_t> span);
 ByteString PDF_EncodeText(const WideString& str);
 
 std::unique_ptr<CCodec_ScanlineDecoder> CreateFaxDecoder(
diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp
index 7ff0914..7044606 100644
--- a/core/fpdfdoc/cpdf_formcontrol.cpp
+++ b/core/fpdfdoc/cpdf_formcontrol.cpp
@@ -76,7 +76,7 @@
     csOn = pArray->GetStringAt(m_pField->GetControlIndex(this));
   if (csOn.IsEmpty())
     csOn = "Yes";
-  return PDF_DecodeText(csOn);
+  return PDF_DecodeText(csOn.AsRawSpan());
 }
 
 bool CPDF_FormControl::IsChecked() const {
diff --git a/fpdfsdk/cpdfsdk_interactiveform.cpp b/fpdfsdk/cpdfsdk_interactiveform.cpp
index c48ff70..1e761cf 100644
--- a/fpdfsdk/cpdfsdk_interactiveform.cpp
+++ b/fpdfsdk/cpdfsdk_interactiveform.cpp
@@ -101,7 +101,7 @@
     name = pField->GetUnicodeTextFor("T");
     ByteString name_b = name.ToDefANSI();
     ByteString csBValue = pField->GetStringFor("V");
-    WideString csWValue = PDF_DecodeText(csBValue);
+    WideString csWValue = PDF_DecodeText(csBValue.AsRawSpan());
     ByteString csValue_b = csWValue.ToDefANSI();
     fdfEncodedData << name_b << "=" << csValue_b;
     if (i != pFields->size() - 1)
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 571f6eb..e46c3db 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -1052,8 +1052,9 @@
     return nullptr;
 
   CPDF_NameTree name_tree(pDoc, "Dests");
+  ByteStringView name_view(name);
   return FPDFDestFromCPDFArray(
-      name_tree.LookupNamedDest(pDoc, PDF_DecodeText(ByteString(name))));
+      name_tree.LookupNamedDest(pDoc, PDF_DecodeText(name_view.span())));
 }
 
 #ifdef PDF_ENABLE_V8
@@ -1145,10 +1146,10 @@
 
     index -= count;
     int i = 0;
-    ByteString bsName;
+    ByteStringView bsName;
     CPDF_DictionaryLocker locker(pDest);
     for (const auto& it : locker) {
-      bsName = it.first;
+      bsName = it.first.AsStringView();
       pDestObj = it.second.get();
       if (!pDestObj)
         continue;
@@ -1156,7 +1157,7 @@
         break;
       i++;
     }
-    wsName = PDF_DecodeText(bsName);
+    wsName = PDF_DecodeText(bsName.span());
   } else {
     pDestObj = nameTree.LookupValueAndName(index, &wsName);
   }