1// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "xfa/fxfa/parser/cxfa_widetextread.h"
8
9#include <algorithm>
10
11#include "core/fxcrt/fx_ext.h"
12#include "xfa/fgas/crt/fgas_codepage.h"
13
14CXFA_WideTextRead::CXFA_WideTextRead(const CFX_WideString& wsBuffer)
15    : m_wsBuffer(wsBuffer), m_iPosition(0) {}
16
17CXFA_WideTextRead::~CXFA_WideTextRead() {}
18
19uint32_t CXFA_WideTextRead::GetAccessModes() const {
20  return FX_STREAMACCESS_Read | FX_STREAMACCESS_Text;
21}
22
23int32_t CXFA_WideTextRead::GetLength() const {
24  return m_wsBuffer.GetLength() * sizeof(FX_WCHAR);
25}
26
27int32_t CXFA_WideTextRead::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
28  switch (eSeek) {
29    case FX_STREAMSEEK_Begin:
30      m_iPosition = iOffset;
31      break;
32    case FX_STREAMSEEK_Current:
33      m_iPosition += iOffset;
34      break;
35    case FX_STREAMSEEK_End:
36      m_iPosition = m_wsBuffer.GetLength() + iOffset;
37      break;
38  }
39  m_iPosition = std::min(std::max(0, m_iPosition), m_wsBuffer.GetLength());
40  return GetPosition();
41}
42
43int32_t CXFA_WideTextRead::GetPosition() {
44  return m_iPosition * sizeof(FX_WCHAR);
45}
46
47bool CXFA_WideTextRead::IsEOF() const {
48  return m_iPosition >= m_wsBuffer.GetLength();
49}
50
51int32_t CXFA_WideTextRead::ReadData(uint8_t* pBuffer, int32_t iBufferSize) {
52  return 0;
53}
54
55int32_t CXFA_WideTextRead::ReadString(FX_WCHAR* pStr,
56                                      int32_t iMaxLength,
57                                      bool& bEOS) {
58  iMaxLength = std::min(iMaxLength, m_wsBuffer.GetLength() - m_iPosition);
59  if (iMaxLength == 0)
60    return 0;
61
62  FXSYS_wcsncpy(pStr, m_wsBuffer.c_str() + m_iPosition, iMaxLength);
63  m_iPosition += iMaxLength;
64  bEOS = IsEOF();
65  return iMaxLength;
66}
67
68int32_t CXFA_WideTextRead::WriteData(const uint8_t* pBuffer,
69                                     int32_t iBufferSize) {
70  return 0;
71}
72
73int32_t CXFA_WideTextRead::WriteString(const FX_WCHAR* pStr, int32_t iLength) {
74  return 0;
75}
76
77bool CXFA_WideTextRead::SetLength(int32_t iLength) {
78  return false;
79}
80
81int32_t CXFA_WideTextRead::GetBOM(uint8_t bom[4]) const {
82  return 0;
83}
84
85uint16_t CXFA_WideTextRead::GetCodePage() const {
86  return (sizeof(FX_WCHAR) == 2) ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE;
87}
88
89uint16_t CXFA_WideTextRead::SetCodePage(uint16_t wCodePage) {
90  return GetCodePage();
91}
92
93CFX_RetainPtr<IFGAS_Stream> CXFA_WideTextRead::CreateSharedStream(
94    uint32_t dwAccess,
95    int32_t iOffset,
96    int32_t iLength) {
97  return nullptr;
98}
99
100CFX_WideString CXFA_WideTextRead::GetSrcText() const {
101  return m_wsBuffer;
102}
103