7#include "core/fxcrt/cfx_seekablestreamproxy.h"
15#include "build/build_config.h"
16#include "core/fxcrt/data_vector.h"
17#include "core/fxcrt/fx_extension.h"
18#include "core/fxcrt/fx_safe_types.h"
19#include "third_party/base/check.h"
20#include "third_party/base/check_op.h"
26std::pair<size_t, size_t> UTF8Decode(pdfium::span<
const uint8_t> pSrc,
27 pdfium::span<
wchar_t> pDst) {
28 DCHECK(!pDst.empty());
34 for (size_t iIndex = 0; iIndex < pSrc.size() && iDstNum < pDst.size();
37 uint8_t byte = pSrc[iIndex];
40 pDst[iDstNum++] = byte;
41 }
else if (byte < 0xc0) {
46 dwCode |= (byte & 0x3f);
49 pDst[iDstNum++] = dwCode;
50 }
else if (byte < 0xe0) {
52 dwCode = (byte & 0x1f);
53 }
else if (byte < 0xf0) {
55 dwCode = (byte & 0x0f);
56 }
else if (byte < 0xf8) {
58 dwCode = (byte & 0x07);
59 }
else if (byte < 0xfc) {
61 dwCode = (byte & 0x03);
62 }
else if (byte < 0xfe) {
64 dwCode = (byte & 0x01);
67 return {iSrcNum, iDstNum};
70#if defined(WCHAR_T_IS_32_BIT)
71static_assert(
sizeof(
wchar_t) > 2,
"wchar_t is too small");
73void UTF16ToWChar(
void* pBuffer, size_t iLength) {
75 DCHECK_GT(iLength, 0u);
77 uint16_t* pSrc =
static_cast<uint16_t*>(pBuffer);
78 wchar_t* pDst =
static_cast<
wchar_t*>(pBuffer);
81 for (size_t i = iLength; i > 0; --i)
82 pDst[i - 1] =
static_cast<
wchar_t>(pSrc[i - 1]);
86void SwapByteOrder(uint16_t* pStr, size_t iLength) {
87 while (iLength-- > 0) {
89 *pStr++ = (wch >> 8) | (wch << 8);
95#define BOM_UTF8_MASK 0x00FFFFFF
96#define BOM_UTF8 0x00BFBBEF
97#define BOM_UTF16_MASK 0x0000FFFF
98#define BOM_UTF16_BE 0x0000FFFE
99#define BOM_UTF16_LE 0x0000FEFF
101CFX_SeekableStreamProxy::CFX_SeekableStreamProxy(
103 : m_pStream(stream) {
106 Seek(From::Begin, 0);
109 ReadData(
reinterpret_cast<uint8_t*>(&bom), 3);
129 Seek(From::Begin,
static_cast<
FX_FILESIZE>(m_wBOMLength));
135 return m_pStream->GetSize();
138FX_FILESIZE CFX_SeekableStreamProxy::GetPosition() {
142bool CFX_SeekableStreamProxy::
IsEOF() {
146void CFX_SeekableStreamProxy::Seek(From eSeek,
FX_FILESIZE iOffset) {
149 m_iPosition = iOffset;
151 case From::Current: {
152 FX_SAFE_FILESIZE new_pos = m_iPosition;
162 if (m_wBOMLength > 0)
164 m_wCodePage = wCodePage;
167size_t CFX_SeekableStreamProxy::ReadData(uint8_t* pBuffer, size_t iBufferSize) {
169 DCHECK(iBufferSize > 0);
172 std::min(iBufferSize,
static_cast<size_t>(
GetSize() - m_iPosition));
173 if (iBufferSize <= 0)
176 if (!m_pStream->ReadBlockAtOffset({pBuffer, iBufferSize}, m_iPosition))
179 FX_SAFE_FILESIZE new_pos = m_iPosition;
180 new_pos += iBufferSize;
181 m_iPosition = new_pos.ValueOrDefault(m_iPosition);
182 return new_pos.IsValid() ? iBufferSize : 0;
186 if (!pStr || size == 0)
191 size_t iBytes = size * 2;
192 size_t iLen = ReadData(
reinterpret_cast<uint8_t*>(pStr), iBytes);
195 SwapByteOrder(
reinterpret_cast<uint16_t*>(pStr), size);
197#if defined(WCHAR_T_IS_32_BIT)
199 UTF16ToWChar(pStr, size);
205 size_t iBytes = std::min(size,
static_cast<size_t>(GetSize() - pos));
209 DataVector<uint8_t> buf(iBytes);
210 size_t iLen = ReadData(buf.data(), iBytes);
215 std::tie(iSrc, size) = UTF8Decode({buf.data(), iLen}, {pStr, size});
216 Seek(From::Current, iSrc - iLen);
size_t ReadBlock(wchar_t *pStr, size_t size)
void SetCodePage(FX_CodePage wCodePage)
~CFX_SeekableStreamProxy() override