1 // Copyright 2017 The PDFium Authors
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 "core/fxcrt/fx_stream.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "core/fxcrt/fileaccess_iface.h"
13
14 namespace {
15
16 class CFX_CRTFileStream final : public IFX_SeekableStream {
17 public:
18 CONSTRUCT_VIA_MAKE_RETAIN;
19
20 // IFX_SeekableStream:
GetSize()21 FX_FILESIZE GetSize() override { return m_pFile->GetSize(); }
IsEOF()22 bool IsEOF() override { return GetPosition() >= GetSize(); }
GetPosition()23 FX_FILESIZE GetPosition() override { return m_pFile->GetPosition(); }
ReadBlockAtOffset(pdfium::span<uint8_t> buffer,FX_FILESIZE offset)24 bool ReadBlockAtOffset(pdfium::span<uint8_t> buffer,
25 FX_FILESIZE offset) override {
26 return m_pFile->ReadPos(buffer.data(), buffer.size(), offset) > 0;
27 }
ReadBlock(pdfium::span<uint8_t> buffer)28 size_t ReadBlock(pdfium::span<uint8_t> buffer) override {
29 return m_pFile->Read(buffer.data(), buffer.size());
30 }
WriteBlockAtOffset(pdfium::span<const uint8_t> buffer,FX_FILESIZE offset)31 bool WriteBlockAtOffset(pdfium::span<const uint8_t> buffer,
32 FX_FILESIZE offset) override {
33 return !!m_pFile->WritePos(buffer.data(), buffer.size(), offset);
34 }
Flush()35 bool Flush() override { return m_pFile->Flush(); }
36
37 private:
CFX_CRTFileStream(std::unique_ptr<FileAccessIface> pFA)38 explicit CFX_CRTFileStream(std::unique_ptr<FileAccessIface> pFA)
39 : m_pFile(std::move(pFA)) {}
40 ~CFX_CRTFileStream() override = default;
41
42 std::unique_ptr<FileAccessIface> m_pFile;
43 };
44
45 } // namespace
46
WriteString(ByteStringView str)47 bool IFX_WriteStream::WriteString(ByteStringView str) {
48 return WriteBlock(str.raw_span());
49 }
50
WriteByte(uint8_t byte)51 bool IFX_WriteStream::WriteByte(uint8_t byte) {
52 return WriteBlock({&byte, 1});
53 }
54
WriteDWord(uint32_t i)55 bool IFX_WriteStream::WriteDWord(uint32_t i) {
56 char buf[20] = {};
57 FXSYS_itoa(i, buf, 10);
58 return WriteBlock({reinterpret_cast<uint8_t*>(buf), strlen(buf)});
59 }
60
WriteFilesize(FX_FILESIZE size)61 bool IFX_WriteStream::WriteFilesize(FX_FILESIZE size) {
62 char buf[20] = {};
63 FXSYS_i64toa(size, buf, 10);
64 return WriteBlock({reinterpret_cast<uint8_t*>(buf), strlen(buf)});
65 }
66
67 // static
CreateFromFilename(const char * filename)68 RetainPtr<IFX_SeekableReadStream> IFX_SeekableReadStream::CreateFromFilename(
69 const char* filename) {
70 std::unique_ptr<FileAccessIface> pFA = FileAccessIface::Create();
71 if (!pFA->Open(filename))
72 return nullptr;
73 return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
74 }
75
WriteBlock(pdfium::span<const uint8_t> buffer)76 bool IFX_SeekableWriteStream::WriteBlock(pdfium::span<const uint8_t> buffer) {
77 return WriteBlockAtOffset(buffer, GetSize());
78 }
79
IsEOF()80 bool IFX_SeekableReadStream::IsEOF() {
81 return false;
82 }
83
GetPosition()84 FX_FILESIZE IFX_SeekableReadStream::GetPosition() {
85 return 0;
86 }
87
ReadBlock(pdfium::span<uint8_t> buffer)88 size_t IFX_SeekableReadStream::ReadBlock(pdfium::span<uint8_t> buffer) {
89 return 0;
90 }
91
WriteBlock(pdfium::span<const uint8_t> buffer)92 bool IFX_SeekableStream::WriteBlock(pdfium::span<const uint8_t> buffer) {
93 return WriteBlockAtOffset(buffer, GetSize());
94 }
95