1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fgas/crt/ifgas_stream.h"
#if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \
_FX_OS_ == _FX_WIN64_
#include <io.h>
#endif
#include <algorithm>
#include <memory>
#include <utility>
#include "third_party/base/ptr_util.h"
#include "xfa/fgas/crt/fgas_codepage.h"
namespace {
class IFGAS_StreamImp {
public:
virtual ~IFGAS_StreamImp() {}
virtual int32_t GetLength() const = 0;
virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0;
virtual int32_t GetPosition() = 0;
virtual bool IsEOF() const = 0;
virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0;
virtual int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) = 0;
virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0;
virtual int32_t WriteString(const wchar_t* pStr, int32_t iLength) = 0;
virtual void Flush() = 0;
virtual bool SetLength(int32_t iLength) = 0;
protected:
IFGAS_StreamImp();
uint32_t GetAccessModes() const { return m_dwAccess; }
private:
uint32_t m_dwAccess;
};
class CFGAS_FileReadStreamImp : public IFGAS_StreamImp {
public:
CFGAS_FileReadStreamImp();
~CFGAS_FileReadStreamImp() override {}
bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead);
// IFGAS_StreamImp:
int32_t GetLength() const override;
int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
int32_t GetPosition() override { return m_iPosition; }
bool IsEOF() const override;
int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override;
int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override {
return 0;
}
int32_t WriteString(const wchar_t* pStr, int32_t iLength) override {
return 0;
}
void Flush() override {}
bool SetLength(int32_t iLength) override { return false; }
protected:
CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead;
int32_t m_iPosition;
int32_t m_iLength;
};
class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp {
public:
CFGAS_FileWriteStreamImp();
~CFGAS_FileWriteStreamImp() override {}
bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite,
uint32_t dwAccess);
// IFGAS_StreamImp:
int32_t GetLength() const override;
int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
int32_t GetPosition() override { return m_iPosition; }
bool IsEOF() const override;
int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override { return 0; }
int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override {
return 0;
}
int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t WriteString(const wchar_t* pStr, int32_t iLength) override;
void Flush() override;
bool SetLength(int32_t iLength) override { return false; }
protected:
CFX_RetainPtr<IFX_SeekableWriteStream> m_pFileWrite;
int32_t m_iPosition;
};
class CFGAS_Stream : public IFGAS_Stream {
public:
template <typename T, typename... Args>
friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
// IFGAS_Stream
uint32_t GetAccessModes() const override;
int32_t GetLength() const override;
int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
int32_t GetPosition() override;
bool IsEOF() const override;
int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override;
int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t WriteString(const wchar_t* pStr, int32_t iLength) override;
void Flush() override;
bool SetLength(int32_t iLength) override;
int32_t GetBOM(uint8_t bom[4]) const override;
uint16_t GetCodePage() const override;
uint16_t SetCodePage(uint16_t wCodePage) override;
protected:
CFGAS_Stream(std::unique_ptr<IFGAS_StreamImp> imp, uint32_t dwAccess);
~CFGAS_Stream() override;
std::unique_ptr<IFGAS_StreamImp> m_pStreamImp;
uint32_t m_dwAccess;
int32_t m_iTotalSize;
int32_t m_iPosition;
int32_t m_iStart;
int32_t m_iLength;
int32_t m_iRefCount;
};
class CFGAS_TextStream : public IFGAS_Stream {
public:
template <typename T, typename... Args>
friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
// IFGAS_Stream
uint32_t GetAccessModes() const override;
int32_t GetLength() const override;
int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override;
int32_t GetPosition() override;
bool IsEOF() const override;
int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t ReadString(wchar_t* pStr, int32_t iMaxLength, bool& bEOS) override;
int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override;
int32_t WriteString(const wchar_t* pStr, int32_t iLength) override;
void Flush() override;
bool SetLength(int32_t iLength) override;
int32_t GetBOM(uint8_t bom[4]) const override;
uint16_t GetCodePage() const override;
uint16_t SetCodePage(uint16_t wCodePage) override;
protected:
explicit CFGAS_TextStream(const CFX_RetainPtr<IFGAS_Stream>& pStream);
~CFGAS_TextStream() override;
void InitStream();
uint16_t m_wCodePage;
int32_t m_wBOMLength;
uint32_t m_dwBOM;
uint8_t* m_pBuf;
int32_t m_iBufSize;
CFX_RetainPtr<IFGAS_Stream> m_pStreamImp;
};
class CFGAS_FileRead : public IFX_SeekableReadStream {
public:
explicit CFGAS_FileRead(const CFX_RetainPtr<IFGAS_Stream>& pStream);
~CFGAS_FileRead() override;
// IFX_SeekableReadStream
FX_FILESIZE GetSize() override;
bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
protected:
CFX_RetainPtr<IFGAS_Stream> m_pStream;
};
IFGAS_StreamImp::IFGAS_StreamImp() : m_dwAccess(0) {}
CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp()
: m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {}
bool CFGAS_FileReadStreamImp::LoadFileRead(
const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead) {
ASSERT(!m_pFileRead && pFileRead);
m_pFileRead = pFileRead;
m_iLength = m_pFileRead->GetSize();
return true;
}
int32_t CFGAS_FileReadStreamImp::GetLength() const {
return m_iLength;
}
int32_t CFGAS_FileReadStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
switch (eSeek) {
case FX_STREAMSEEK_Begin:
m_iPosition = iOffset;
break;
case FX_STREAMSEEK_Current:
m_iPosition += iOffset;
break;
case FX_STREAMSEEK_End:
m_iPosition = m_iLength + iOffset;
break;
}
if (m_iPosition < 0) {
m_iPosition = 0;
} else if (m_iPosition >= m_iLength) {
m_iPosition = m_iLength;
}
return m_iPosition;
}
bool CFGAS_FileReadStreamImp::IsEOF() const {
return m_iPosition >= m_iLength;
}
int32_t CFGAS_FileReadStreamImp::ReadData(uint8_t* pBuffer,
int32_t iBufferSize) {
ASSERT(m_pFileRead);
ASSERT(pBuffer && iBufferSize > 0);
if (iBufferSize > m_iLength - m_iPosition) {
iBufferSize = m_iLength - m_iPosition;
}
if (m_pFileRead->ReadBlock(pBuffer, m_iPosition, iBufferSize)) {
m_iPosition += iBufferSize;
return iBufferSize;
}
return 0;
}
int32_t CFGAS_FileReadStreamImp::ReadString(wchar_t* pStr,
int32_t iMaxLength,
bool& bEOS) {
ASSERT(m_pFileRead);
ASSERT(pStr && iMaxLength > 0);
iMaxLength = ReadData((uint8_t*)pStr, iMaxLength * 2) / 2;
if (iMaxLength <= 0) {
return 0;
}
int32_t i = 0;
while (i < iMaxLength && pStr[i] != L'\0') {
++i;
}
bEOS = (m_iPosition >= m_iLength) || pStr[i] == L'\0';
return i;
}
CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp()
: m_pFileWrite(nullptr), m_iPosition(0) {}
bool CFGAS_FileWriteStreamImp::LoadFileWrite(
const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite,
uint32_t dwAccess) {
ASSERT(!m_pFileWrite && pFileWrite);
m_iPosition = pFileWrite->GetSize();
m_pFileWrite = pFileWrite;
return true;
}
int32_t CFGAS_FileWriteStreamImp::GetLength() const {
if (!m_pFileWrite)
return 0;
return (int32_t)m_pFileWrite->GetSize();
}
int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
int32_t iLength = GetLength();
switch (eSeek) {
case FX_STREAMSEEK_Begin:
m_iPosition = iOffset;
break;
case FX_STREAMSEEK_Current:
m_iPosition += iOffset;
break;
case FX_STREAMSEEK_End:
m_iPosition = iLength + iOffset;
break;
}
if (m_iPosition < 0) {
m_iPosition = 0;
} else if (m_iPosition >= iLength) {
m_iPosition = iLength;
}
return m_iPosition;
}
bool CFGAS_FileWriteStreamImp::IsEOF() const {
return m_iPosition >= GetLength();
}
int32_t CFGAS_FileWriteStreamImp::WriteData(const uint8_t* pBuffer,
int32_t iBufferSize) {
if (!m_pFileWrite) {
return 0;
}
if (m_pFileWrite->WriteBlock(pBuffer, m_iPosition, iBufferSize)) {
m_iPosition += iBufferSize;
}
return iBufferSize;
}
int32_t CFGAS_FileWriteStreamImp::WriteString(const wchar_t* pStr,
int32_t iLength) {
return WriteData((const uint8_t*)pStr, iLength * sizeof(wchar_t));
}
void CFGAS_FileWriteStreamImp::Flush() {
if (m_pFileWrite) {
m_pFileWrite->Flush();
}
}
CFGAS_TextStream::CFGAS_TextStream(const CFX_RetainPtr<IFGAS_Stream>& pStream)
: m_wCodePage(FX_CODEPAGE_DefANSI),
m_wBOMLength(0),
m_dwBOM(0),
m_pBuf(nullptr),
m_iBufSize(0),
m_pStreamImp(pStream) {
ASSERT(m_pStreamImp);
InitStream();
}
CFGAS_TextStream::~CFGAS_TextStream() {
if (m_pBuf)
FX_Free(m_pBuf);
}
void CFGAS_TextStream::InitStream() {
int32_t iPosition = m_pStreamImp->GetPosition();
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0);
m_pStreamImp->ReadData((uint8_t*)&m_dwBOM, 3);
#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_
m_dwBOM &= 0x00FFFFFF;
if (m_dwBOM == 0x00BFBBEF) {
m_wBOMLength = 3;
m_wCodePage = FX_CODEPAGE_UTF8;
} else {
m_dwBOM &= 0x0000FFFF;
if (m_dwBOM == 0x0000FFFE) {
m_wBOMLength = 2;
m_wCodePage = FX_CODEPAGE_UTF16BE;
} else if (m_dwBOM == 0x0000FEFF) {
m_wBOMLength = 2;
m_wCodePage = FX_CODEPAGE_UTF16LE;
} else {
m_wBOMLength = 0;
m_dwBOM = 0;
m_wCodePage = FXSYS_GetACP();
}
}
#else
m_dwBOM &= 0xFFFFFF00;
if (m_dwBOM == 0xEFBBBF00) {
m_wBOMLength = 3;
m_wCodePage = FX_CODEPAGE_UTF8;
} else {
m_dwBOM &= 0xFFFF0000;
if (m_dwBOM == 0xFEFF0000) {
m_wBOMLength = 2;
m_wCodePage = FX_CODEPAGE_UTF16BE;
} else if (m_dwBOM == 0xFFFE0000) {
m_wBOMLength = 2;
m_wCodePage = FX_CODEPAGE_UTF16LE;
} else {
m_wBOMLength = 0;
m_dwBOM = 0;
m_wCodePage = FXSYS_GetACP();
}
}
#endif
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition));
}
uint32_t CFGAS_TextStream::GetAccessModes() const {
return m_pStreamImp->GetAccessModes();
}
int32_t CFGAS_TextStream::GetLength() const {
return m_pStreamImp->GetLength();
}
int32_t CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
return m_pStreamImp->Seek(eSeek, iOffset);
}
int32_t CFGAS_TextStream::GetPosition() {
return m_pStreamImp->GetPosition();
}
bool CFGAS_TextStream::IsEOF() const {
return m_pStreamImp->IsEOF();
}
int32_t CFGAS_TextStream::ReadData(uint8_t* pBuffer, int32_t iBufferSize) {
return m_pStreamImp->ReadData(pBuffer, iBufferSize);
}
int32_t CFGAS_TextStream::WriteData(const uint8_t* pBuffer,
int32_t iBufferSize) {
return m_pStreamImp->WriteData(pBuffer, iBufferSize);
}
void CFGAS_TextStream::Flush() {
m_pStreamImp->Flush();
}
bool CFGAS_TextStream::SetLength(int32_t iLength) {
return m_pStreamImp->SetLength(iLength);
}
uint16_t CFGAS_TextStream::GetCodePage() const {
return m_wCodePage;
}
int32_t CFGAS_TextStream::GetBOM(uint8_t bom[4]) const {
if (m_wBOMLength < 1)
return 0;
*(uint32_t*)bom = m_dwBOM;
return m_wBOMLength;
}
uint16_t CFGAS_TextStream::SetCodePage(uint16_t wCodePage) {
if (m_wBOMLength > 0)
return m_wCodePage;
uint16_t v = m_wCodePage;
m_wCodePage = wCodePage;
return v;
}
int32_t CFGAS_TextStream::ReadString(wchar_t* pStr,
int32_t iMaxLength,
bool& bEOS) {
ASSERT(pStr && iMaxLength > 0);
if (!m_pStreamImp) {
return -1;
}
int32_t iLen;
if (m_wCodePage == FX_CODEPAGE_UTF16LE ||
m_wCodePage == FX_CODEPAGE_UTF16BE) {
int32_t iBytes = iMaxLength * 2;
iLen = m_pStreamImp->ReadData((uint8_t*)pStr, iBytes);
iMaxLength = iLen / 2;
if (sizeof(wchar_t) > 2) {
FX_UTF16ToWChar(pStr, iMaxLength);
}
#if _FX_ENDIAN_ == _FX_BIG_ENDIAN_
if (m_wCodePage == FX_CODEPAGE_UTF16LE) {
FX_SwapByteOrder(pStr, iMaxLength);
}
#else
if (m_wCodePage == FX_CODEPAGE_UTF16BE) {
FX_SwapByteOrder(pStr, iMaxLength);
}
#endif
} else {
int32_t pos = m_pStreamImp->GetPosition();
int32_t iBytes = iMaxLength;
iBytes = std::min(iBytes, m_pStreamImp->GetLength() - pos);
if (iBytes > 0) {
if (!m_pBuf) {
m_pBuf = FX_Alloc(uint8_t, iBytes);
m_iBufSize = iBytes;
} else if (iBytes > m_iBufSize) {
m_pBuf = FX_Realloc(uint8_t, m_pBuf, iBytes);
m_iBufSize = iBytes;
}
iLen = m_pStreamImp->ReadData(m_pBuf, iBytes);
int32_t iSrc = iLen;
int32_t iDecode = FX_DecodeString(m_wCodePage, (const char*)m_pBuf, &iSrc,
pStr, &iMaxLength, true);
m_pStreamImp->Seek(FX_STREAMSEEK_Current, iSrc - iLen);
if (iDecode < 1) {
return -1;
}
} else {
iMaxLength = 0;
}
}
bEOS = m_pStreamImp->IsEOF();
return iMaxLength;
}
int32_t CFGAS_TextStream::WriteString(const wchar_t* pStr, int32_t iLength) {
ASSERT(pStr && iLength > 0);
if ((m_pStreamImp->GetAccessModes() & FX_STREAMACCESS_Write) == 0)
return -1;
if (m_wCodePage == FX_CODEPAGE_UTF8) {
int32_t len = iLength;
CFX_UTF8Encoder encoder;
while (len-- > 0) {
encoder.Input(*pStr++);
}
CFX_ByteStringC bsResult = encoder.GetResult();
m_pStreamImp->WriteData((const uint8_t*)bsResult.c_str(),
bsResult.GetLength());
}
return iLength;
}
CFGAS_Stream::CFGAS_Stream(std::unique_ptr<IFGAS_StreamImp> imp,
uint32_t dwAccess)
: m_pStreamImp(std::move(imp)),
m_dwAccess(dwAccess),
m_iTotalSize(0),
m_iPosition(0),
m_iStart(0),
m_iLength(m_pStreamImp->GetLength()),
m_iRefCount(1) {}
CFGAS_Stream::~CFGAS_Stream() {}
uint32_t CFGAS_Stream::GetAccessModes() const {
return m_dwAccess;
}
int32_t CFGAS_Stream::GetLength() const {
return m_pStreamImp ? m_pStreamImp->GetLength() : -1;
}
int32_t CFGAS_Stream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) {
if (!m_pStreamImp)
return -1;
m_iPosition = m_pStreamImp->Seek(eSeek, iOffset);
return m_iPosition;
}
int32_t CFGAS_Stream::GetPosition() {
if (!m_pStreamImp)
return -1;
m_iPosition = m_pStreamImp->GetPosition();
return m_iPosition;
}
bool CFGAS_Stream::IsEOF() const {
return m_pStreamImp ? m_pStreamImp->IsEOF() : true;
}
int32_t CFGAS_Stream::ReadData(uint8_t* pBuffer, int32_t iBufferSize) {
ASSERT(pBuffer && iBufferSize > 0);
if (!m_pStreamImp) {
return -1;
}
int32_t iLen = std::min(m_iStart + m_iLength - m_iPosition, iBufferSize);
if (iLen <= 0) {
return 0;
}
if (m_pStreamImp->GetPosition() != m_iPosition) {
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition);
}
iLen = m_pStreamImp->ReadData(pBuffer, iLen);
m_iPosition = m_pStreamImp->GetPosition();
return iLen;
}
int32_t CFGAS_Stream::ReadString(wchar_t* pStr,
int32_t iMaxLength,
bool& bEOS) {
ASSERT(pStr && iMaxLength > 0);
if (!m_pStreamImp) {
return -1;
}
int32_t iEnd = m_iStart + m_iLength;
int32_t iLen = iEnd - m_iPosition;
iLen = std::min(iEnd / 2, iMaxLength);
if (iLen <= 0) {
return 0;
}
if (m_pStreamImp->GetPosition() != m_iPosition) {
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition);
}
iLen = m_pStreamImp->ReadString(pStr, iLen, bEOS);
m_iPosition = m_pStreamImp->GetPosition();
if (iLen > 0 && m_iPosition >= iEnd) {
bEOS = true;
}
return iLen;
}
int32_t CFGAS_Stream::WriteData(const uint8_t* pBuffer, int32_t iBufferSize) {
ASSERT(pBuffer && iBufferSize > 0);
if (!m_pStreamImp) {
return -1;
}
if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) {
return -1;
}
int32_t iLen = iBufferSize;
int32_t iEnd = m_iStart + m_iLength;
if (m_pStreamImp->GetPosition() != m_iPosition) {
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition);
}
iLen = m_pStreamImp->WriteData(pBuffer, iLen);
m_iPosition = m_pStreamImp->GetPosition();
if (m_iPosition > iEnd) {
m_iLength = m_iPosition - m_iStart;
}
return iLen;
}
int32_t CFGAS_Stream::WriteString(const wchar_t* pStr, int32_t iLength) {
ASSERT(pStr && iLength > 0);
if (!m_pStreamImp) {
return -1;
}
if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) {
return -1;
}
int32_t iLen = iLength;
int32_t iEnd = m_iStart + m_iLength;
if (m_pStreamImp->GetPosition() != m_iPosition) {
m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition);
}
iLen = m_pStreamImp->WriteString(pStr, iLen);
m_iPosition = m_pStreamImp->GetPosition();
if (m_iPosition > iEnd) {
m_iLength = m_iPosition - m_iStart;
}
return iLen;
}
void CFGAS_Stream::Flush() {
if (!m_pStreamImp) {
return;
}
if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) {
return;
}
m_pStreamImp->Flush();
}
bool CFGAS_Stream::SetLength(int32_t iLength) {
if (!m_pStreamImp) {
return false;
}
if ((m_dwAccess & FX_STREAMACCESS_Write) == 0) {
return false;
}
return m_pStreamImp->SetLength(iLength);
}
int32_t CFGAS_Stream::GetBOM(uint8_t bom[4]) const {
if (!m_pStreamImp) {
return -1;
}
return 0;
}
uint16_t CFGAS_Stream::GetCodePage() const {
#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_
return FX_CODEPAGE_UTF16LE;
#else
return FX_CODEPAGE_UTF16BE;
#endif
}
uint16_t CFGAS_Stream::SetCodePage(uint16_t wCodePage) {
#if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_
return FX_CODEPAGE_UTF16LE;
#else
return FX_CODEPAGE_UTF16BE;
#endif
}
CFGAS_FileRead::CFGAS_FileRead(const CFX_RetainPtr<IFGAS_Stream>& pStream)
: m_pStream(pStream) {
ASSERT(m_pStream);
}
CFGAS_FileRead::~CFGAS_FileRead() {}
FX_FILESIZE CFGAS_FileRead::GetSize() {
return (FX_FILESIZE)m_pStream->GetLength();
}
bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) {
m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset);
int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size);
return iLen == (int32_t)size;
}
} // namespace
// static
CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateReadStream(
const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead) {
if (!pFileRead)
return nullptr;
std::unique_ptr<IFGAS_StreamImp> pImp =
pdfium::MakeUnique<CFGAS_FileReadStreamImp>();
if (!static_cast<CFGAS_FileReadStreamImp*>(pImp.get())
->LoadFileRead(pFileRead)) {
return nullptr;
}
return pdfium::MakeRetain<CFGAS_TextStream>(
pdfium::MakeRetain<CFGAS_Stream>(std::move(pImp), 0));
}
// static
CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateWriteStream(
const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite) {
if (!pFileWrite)
return nullptr;
std::unique_ptr<IFGAS_StreamImp> pImp =
pdfium::MakeUnique<CFGAS_FileWriteStreamImp>();
if (!static_cast<CFGAS_FileWriteStreamImp*>(pImp.get())
->LoadFileWrite(pFileWrite, FX_STREAMACCESS_Write)) {
return nullptr;
}
return pdfium::MakeRetain<CFGAS_TextStream>(
pdfium::MakeRetain<CFGAS_Stream>(std::move(pImp), FX_STREAMACCESS_Write));
}
CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() {
return pdfium::MakeRetain<CFGAS_FileRead>(CFX_RetainPtr<IFGAS_Stream>(this));
}
|