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 
CXFA_WideTextRead(const CFX_WideString & wsBuffer)14 CXFA_WideTextRead::CXFA_WideTextRead(const CFX_WideString& wsBuffer)
15     : m_wsBuffer(wsBuffer), m_iPosition(0) {}
16 
~CXFA_WideTextRead()17 CXFA_WideTextRead::~CXFA_WideTextRead() {}
18 
GetAccessModes() const19 uint32_t CXFA_WideTextRead::GetAccessModes() const {
20   return FX_STREAMACCESS_Read | FX_STREAMACCESS_Text;
21 }
22 
GetLength() const23 int32_t CXFA_WideTextRead::GetLength() const {
24   return m_wsBuffer.GetLength() * sizeof(FX_WCHAR);
25 }
26 
Seek(FX_STREAMSEEK eSeek,int32_t iOffset)27 int32_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 
GetPosition()43 int32_t CXFA_WideTextRead::GetPosition() {
44   return m_iPosition * sizeof(FX_WCHAR);
45 }
46 
IsEOF() const47 bool CXFA_WideTextRead::IsEOF() const {
48   return m_iPosition >= m_wsBuffer.GetLength();
49 }
50 
ReadData(uint8_t * pBuffer,int32_t iBufferSize)51 int32_t CXFA_WideTextRead::ReadData(uint8_t* pBuffer, int32_t iBufferSize) {
52   return 0;
53 }
54 
ReadString(FX_WCHAR * pStr,int32_t iMaxLength,bool & bEOS)55 int32_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 
WriteData(const uint8_t * pBuffer,int32_t iBufferSize)68 int32_t CXFA_WideTextRead::WriteData(const uint8_t* pBuffer,
69                                      int32_t iBufferSize) {
70   return 0;
71 }
72 
WriteString(const FX_WCHAR * pStr,int32_t iLength)73 int32_t CXFA_WideTextRead::WriteString(const FX_WCHAR* pStr, int32_t iLength) {
74   return 0;
75 }
76 
SetLength(int32_t iLength)77 bool CXFA_WideTextRead::SetLength(int32_t iLength) {
78   return false;
79 }
80 
GetBOM(uint8_t bom[4]) const81 int32_t CXFA_WideTextRead::GetBOM(uint8_t bom[4]) const {
82   return 0;
83 }
84 
GetCodePage() const85 uint16_t CXFA_WideTextRead::GetCodePage() const {
86   return (sizeof(FX_WCHAR) == 2) ? FX_CODEPAGE_UTF16LE : FX_CODEPAGE_UTF32LE;
87 }
88 
SetCodePage(uint16_t wCodePage)89 uint16_t CXFA_WideTextRead::SetCodePage(uint16_t wCodePage) {
90   return GetCodePage();
91 }
92 
CreateSharedStream(uint32_t dwAccess,int32_t iOffset,int32_t iLength)93 CFX_RetainPtr<IFGAS_Stream> CXFA_WideTextRead::CreateSharedStream(
94     uint32_t dwAccess,
95     int32_t iOffset,
96     int32_t iLength) {
97   return nullptr;
98 }
99 
GetSrcText() const100 CFX_WideString CXFA_WideTextRead::GetSrcText() const {
101   return m_wsBuffer;
102 }
103