Pass spans to UTF8Decode() in cfx_seekablestreamproxy.cpp
-- prefer early return over else.
-- remove empty input special case since code handles it properly.
Change-Id: Ic44e4501e32986cc49e99bc0857090cd221e0969
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/66631
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/cfx_seekablestreamproxy.cpp b/core/fxcrt/cfx_seekablestreamproxy.cpp
index 67b304f..6c34b03 100644
--- a/core/fxcrt/cfx_seekablestreamproxy.cpp
+++ b/core/fxcrt/cfx_seekablestreamproxy.cpp
@@ -27,27 +27,21 @@
// Returns {src bytes consumed, dst chars produced}.
// Invalid sequences are silently not output.
-std::pair<size_t, size_t> UTF8Decode(const char* pSrc,
- size_t srcLen,
- wchar_t* pDst,
- size_t dstLen) {
- ASSERT(pDst);
- ASSERT(dstLen > 0);
-
- if (srcLen < 1)
- return {0, 0};
+std::pair<size_t, size_t> UTF8Decode(pdfium::span<const uint8_t> pSrc,
+ pdfium::span<wchar_t> pDst) {
+ ASSERT(!pDst.empty());
uint32_t dwCode = 0;
int32_t iPending = 0;
size_t iSrcNum = 0;
size_t iDstNum = 0;
- for (size_t iIndex = 0; iIndex < srcLen && iDstNum < dstLen; ++iIndex) {
+ for (size_t iIndex = 0; iIndex < pSrc.size() && iDstNum < pDst.size();
+ ++iIndex) {
++iSrcNum;
- uint8_t byte = static_cast<uint8_t>(*(pSrc + iIndex));
+ uint8_t byte = pSrc[iIndex];
if (byte < 0x80) {
iPending = 0;
- ++iDstNum;
- *pDst++ = byte;
+ pDst[iDstNum++] = byte;
} else if (byte < 0xc0) {
if (iPending < 1)
continue;
@@ -55,10 +49,8 @@
dwCode = dwCode << 6;
dwCode |= (byte & 0x3f);
--iPending;
- if (iPending == 0) {
- ++iDstNum;
- *pDst++ = dwCode;
- }
+ if (iPending == 0)
+ pDst[iDstNum++] = dwCode;
} else if (byte < 0xe0) {
iPending = 1;
dwCode = (byte & 0x1f);
@@ -214,26 +206,21 @@
if (size > 0)
UTF16ToWChar(pStr, size);
#endif
- } else {
- FX_FILESIZE pos = GetPosition();
- size_t iBytes = std::min(size, static_cast<size_t>(GetSize() - pos));
-
- if (iBytes > 0) {
- std::vector<uint8_t, FxAllocAllocator<uint8_t>> buf(iBytes);
-
- size_t iLen = ReadData(buf.data(), iBytes);
- if (m_wCodePage != FX_CODEPAGE_UTF8)
- return 0;
-
- size_t iSrc = 0;
- std::tie(iSrc, size) =
- UTF8Decode(reinterpret_cast<const char*>(buf.data()), iLen,
- static_cast<wchar_t*>(pStr), size);
- Seek(From::Current, iSrc - iLen);
- } else {
- size = 0;
- }
+ return size;
}
+ FX_FILESIZE pos = GetPosition();
+ size_t iBytes = std::min(size, static_cast<size_t>(GetSize() - pos));
+ if (iBytes == 0)
+ return 0;
+
+ std::vector<uint8_t, FxAllocAllocator<uint8_t>> buf(iBytes);
+ size_t iLen = ReadData(buf.data(), iBytes);
+ if (m_wCodePage != FX_CODEPAGE_UTF8)
+ return 0;
+
+ size_t iSrc;
+ std::tie(iSrc, size) = UTF8Decode({buf.data(), iLen}, {pStr, size});
+ Seek(From::Current, iSrc - iLen);
return size;
}