7#include "core/fxcrt/cfx_seekablestreamproxy.h"
15#include "build/build_config.h"
16#include "core/fxcrt/check.h"
17#include "core/fxcrt/check_op.h"
18#include "core/fxcrt/data_vector.h"
19#include "core/fxcrt/fx_extension.h"
20#include "core/fxcrt/fx_safe_types.h"
21#include "core/fxcrt/span.h"
22#include "core/fxcrt/span_util.h"
28std::pair<size_t, size_t> UTF8Decode(pdfium::span<
const uint8_t> pSrc,
29 pdfium::span<
wchar_t> pDst) {
36 for (size_t iIndex = 0; iIndex < pSrc.size() && iDstNum < pDst.size();
39 uint8_t byte = pSrc[iIndex];
42 pDst[iDstNum++] = byte;
43 }
else if (byte < 0xc0) {
48 dwCode |= (byte & 0x3f);
51 pDst[iDstNum++] = dwCode;
52 }
else if (byte < 0xe0) {
54 dwCode = (byte & 0x1f);
55 }
else if (byte < 0xf0) {
57 dwCode = (byte & 0x0f);
58 }
else if (byte < 0xf8) {
60 dwCode = (byte & 0x07);
61 }
else if (byte < 0xfc) {
63 dwCode = (byte & 0x03);
64 }
else if (byte < 0xfe) {
66 dwCode = (byte & 0x01);
69 return {iSrcNum, iDstNum};
72void UTF16ToWChar(pdfium::span<
wchar_t> buffer) {
73#if defined(WCHAR_T_IS_32_BIT)
74 auto src = fxcrt::reinterpret_span<uint16_t>(buffer);
76 for (size_t i = buffer.size(); i > 0; --i) {
77 buffer[i - 1] =
static_cast<
wchar_t>(src[i - 1]);
82void SwapByteOrder(pdfium::span<uint16_t> str) {
83 for (
auto& wch : str) {
84 wch = (wch >> 8) | (wch << 8);
90#define BOM_UTF8_MASK 0x00FFFFFF
91#define BOM_UTF8 0x00BFBBEF
92#define BOM_UTF16_MASK 0x0000FFFF
93#define BOM_UTF16_BE 0x0000FFFE
94#define BOM_UTF16_LE 0x0000FEFF
96CFX_SeekableStreamProxy::CFX_SeekableStreamProxy(
101 Seek(From::Begin, 0);
104 ReadData(pdfium::byte_span_from_ref(bom).first<3>());
124 Seek(From::Begin,
static_cast<
FX_FILESIZE>(m_wBOMLength));
130 return m_pStream->GetSize();
133FX_FILESIZE CFX_SeekableStreamProxy::GetPosition()
const {
137bool CFX_SeekableStreamProxy::
IsEOF()
const {
141void CFX_SeekableStreamProxy::Seek(From eSeek,
FX_FILESIZE iOffset) {
144 m_iPosition = iOffset;
146 case From::Current: {
157 if (m_wBOMLength > 0)
159 m_wCodePage = wCodePage;
162size_t CFX_SeekableStreamProxy::ReadData(pdfium::span<uint8_t> buffer) {
164 const size_t remaining =
static_cast<size_t>(
GetSize() - m_iPosition);
165 size_t read_size =
std::min(buffer.size(), remaining);
166 if (read_size == 0) {
169 if (!m_pStream->ReadBlockAtOffset(buffer.first(read_size), m_iPosition)) {
173 new_pos += read_size;
174 m_iPosition = new_pos.ValueOrDefault(m_iPosition);
175 return new_pos.IsValid() ? read_size : 0;
179 if (buffer.empty()) {
184 size_t bytes_to_read = buffer.size() *
sizeof(uint16_t);
186 ReadData(pdfium::as_writable_bytes(buffer).first(bytes_to_read));
187 size_t elements = bytes_read /
sizeof(uint16_t);
189 SwapByteOrder(
fxcrt::reinterpret_span<uint16_t>(buffer).first(elements));
191 UTF16ToWChar(buffer.first(elements));
195 size_t bytes_to_read =
196 std::min(buffer.size(),
static_cast<size_t>(
GetSize() - pos));
197 if (bytes_to_read == 0) {
200 DataVector<uint8_t> byte_buf(bytes_to_read);
201 size_t bytes_read = ReadData(byte_buf);
205 auto [src_bytes_consumed, dest_wchars_produced] =
206 UTF8Decode(pdfium::make_span(byte_buf).first(bytes_read), buffer);
207 Seek(From::Current, src_bytes_consumed - bytes_read);
208 return dest_wchars_produced;
FX_FILESIZE GetSize() const
size_t ReadBlock(pdfium::span< wchar_t > buffer)
void SetCodePage(FX_CodePage wCodePage)
~CFX_SeekableStreamProxy() override
pdfium::CheckedNumeric< FX_FILESIZE > FX_SAFE_FILESIZE