Pass views and spans to FX_MultiByteToWideString()
-- same for FX_WideStringToMultiByte()
-- convert to size_t for sizes as possible.
-- remove always-zero dwFlags argument.
Bug: pdfium:1706
Change-Id: Iadd3d57f6ac11ad47035a35289ac108bc31195bd
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/83796
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp
index 4e2454f..754db74 100644
--- a/core/fpdfapi/font/cpdf_cidfont.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont.cpp
@@ -329,9 +329,10 @@
charcode = (charcode % 256) * 256 + (charcode / 256);
charsize = 2;
}
- int ret = FX_MultiByteToWideChar(kCharsetCodePages[m_pCMap->GetCoding()], 0,
- reinterpret_cast<const char*>(&charcode),
- charsize, &unicode, 1);
+ size_t ret = FX_MultiByteToWideChar(
+ kCharsetCodePages[m_pCMap->GetCoding()],
+ ByteStringView(reinterpret_cast<const char*>(&charcode), charsize),
+ pdfium::make_span(&unicode, 1));
return ret == 1 ? unicode : 0;
#else
if (!m_pCMap->GetEmbedMap())
@@ -372,9 +373,9 @@
return 0;
#if defined(OS_WIN)
uint8_t buffer[32];
- int ret =
- FX_WideCharToMultiByte(kCharsetCodePages[m_pCMap->GetCoding()], &unicode,
- 1, reinterpret_cast<char*>(buffer), 4);
+ size_t ret = FX_WideCharToMultiByte(
+ kCharsetCodePages[m_pCMap->GetCoding()], WideStringView(&unicode, 1),
+ pdfium::make_span(reinterpret_cast<char*>(buffer), 4));
if (ret == 1)
return buffer[0];
if (ret == 2)
diff --git a/core/fxcrt/fx_codepage.cpp b/core/fxcrt/fx_codepage.cpp
index b15e16f..22a1ede 100644
--- a/core/fxcrt/fx_codepage.cpp
+++ b/core/fxcrt/fx_codepage.cpp
@@ -11,6 +11,7 @@
#include <utility>
#include "build/build_config.h"
+#include "third_party/base/numerics/safe_math.h"
#if defined(OS_WIN)
#include <windows.h>
@@ -289,19 +290,20 @@
(uCharset == FX_Charset::kShiftJIS);
}
-int FX_WideCharToMultiByte(FX_CodePage codepage,
- const wchar_t* wstr,
- int wlen,
- char* buf,
- int buflen) {
+size_t FX_WideCharToMultiByte(FX_CodePage codepage,
+ WideStringView wstr,
+ pdfium::span<char> buf) {
#if defined(OS_WIN)
- return WideCharToMultiByte(static_cast<UINT>(codepage), 0, wstr, wlen, buf,
- buflen, nullptr, nullptr);
+ int input_len = pdfium::base::checked_cast<int>(wstr.GetLength());
+ int output_len = pdfium::base::checked_cast<int>(buf.size());
+ return WideCharToMultiByte(static_cast<UINT>(codepage), 0,
+ wstr.unterminated_c_str(), input_len, buf.data(),
+ output_len, nullptr, nullptr);
#else
- int len = 0;
- for (int i = 0; i < wlen; i++) {
+ size_t len = 0;
+ for (size_t i = 0; i < wstr.GetLength(); i++) {
if (wstr[i] < 0x100) {
- if (buf && len < buflen)
+ if (len < buf.size())
buf[len] = static_cast<char>(wstr[i]);
len++;
}
@@ -310,20 +312,20 @@
#endif
}
-int FX_MultiByteToWideChar(FX_CodePage codepage,
- uint32_t dwFlags,
- const char* bstr,
- int blen,
- wchar_t* buf,
- int buflen) {
+size_t FX_MultiByteToWideChar(FX_CodePage codepage,
+ ByteStringView bstr,
+ pdfium::span<wchar_t> buf) {
#if defined(OS_WIN)
- return MultiByteToWideChar(static_cast<UINT>(codepage), dwFlags, bstr, blen,
- buf, buflen);
+ const int input_len = pdfium::base::checked_cast<int>(bstr.GetLength());
+ const int output_len = pdfium::base::checked_cast<int>(buf.size());
+ return MultiByteToWideChar(static_cast<UINT>(codepage), 0,
+ bstr.unterminated_c_str(), input_len, buf.data(),
+ output_len);
#else
- int wlen = 0;
- for (int i = 0; i < blen; i++) {
- if (buf && wlen < buflen)
- buf[wlen] = reinterpret_cast<const uint8_t*>(bstr)[i];
+ size_t wlen = 0;
+ for (size_t i = 0; i < bstr.GetLength(); i++) {
+ if (wlen < buf.size())
+ buf[wlen] = reinterpret_cast<uint8_t>(bstr[i]);
wlen++;
}
return wlen;
diff --git a/core/fxcrt/fx_codepage.h b/core/fxcrt/fx_codepage.h
index 56b5591..751a2f4 100644
--- a/core/fxcrt/fx_codepage.h
+++ b/core/fxcrt/fx_codepage.h
@@ -11,6 +11,8 @@
// Prove consistency with incomplete forward definitions.
#include "core/fxcrt/fx_codepage_forward.h"
+#include "core/fxcrt/fx_string.h"
+#include "third_party/base/span.h"
enum class FX_CodePage : uint16_t {
kDefANSI = 0,
@@ -111,16 +113,11 @@
FX_Charset FX_GetCharsetFromCodePage(FX_CodePage codepage);
FX_Charset FX_GetCharsetFromInt(int value);
bool FX_CharSetIsCJK(FX_Charset uCharset);
-int FX_WideCharToMultiByte(FX_CodePage codepage,
- const wchar_t* wstr,
- int wlen,
- char* buf,
- int buflen);
-int FX_MultiByteToWideChar(FX_CodePage codepage,
- uint32_t dwFlags,
- const char* bstr,
- int blen,
- wchar_t* buf,
- int buflen);
+size_t FX_WideCharToMultiByte(FX_CodePage codepage,
+ WideStringView wstr,
+ pdfium::span<char> buf);
+size_t FX_MultiByteToWideChar(FX_CodePage codepage,
+ ByteStringView bstr,
+ pdfium::span<wchar_t> buf);
#endif // CORE_FXCRT_FX_CODEPAGE_H_
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index f8de5c2..b7ac000 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -657,9 +657,8 @@
}
ByteString WideString::ToDefANSI() const {
- int src_len = GetLength();
- int dest_len = FX_WideCharToMultiByte(FX_CodePage::kDefANSI, c_str(), src_len,
- nullptr, 0);
+ size_t dest_len =
+ FX_WideCharToMultiByte(FX_CodePage::kDefANSI, AsStringView(), {});
if (!dest_len)
return ByteString();
@@ -667,8 +666,7 @@
{
// Span's lifetime must end before ReleaseBuffer() below.
pdfium::span<char> dest_buf = bstr.GetBuffer(dest_len);
- FX_WideCharToMultiByte(FX_CodePage::kDefANSI, c_str(), src_len,
- dest_buf.data(), dest_len);
+ FX_WideCharToMultiByte(FX_CodePage::kDefANSI, AsStringView(), dest_buf);
}
bstr.ReleaseBuffer(dest_len);
return bstr;
@@ -920,9 +918,7 @@
// static
WideString WideString::FromDefANSI(ByteStringView bstr) {
- int src_len = bstr.GetLength();
- int dest_len = FX_MultiByteToWideChar(
- FX_CodePage::kDefANSI, 0, bstr.unterminated_c_str(), src_len, nullptr, 0);
+ size_t dest_len = FX_MultiByteToWideChar(FX_CodePage::kDefANSI, bstr, {});
if (!dest_len)
return WideString();
@@ -930,8 +926,7 @@
{
// Span's lifetime must end before ReleaseBuffer() below.
pdfium::span<wchar_t> dest_buf = wstr.GetBuffer(dest_len);
- FX_MultiByteToWideChar(FX_CodePage::kDefANSI, 0, bstr.unterminated_c_str(),
- src_len, dest_buf.data(), dest_len);
+ FX_MultiByteToWideChar(FX_CodePage::kDefANSI, bstr, dest_buf);
}
wstr.ReleaseBuffer(dest_len);
return wstr;