1 // Copyright 2014 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 #ifndef CORE_FXCRT_FX_STREAM_H_ 8 #define CORE_FXCRT_FX_STREAM_H_ 9 10 #include "core/fxcrt/cfx_retain_ptr.h" 11 #include "core/fxcrt/fx_string.h" 12 #include "core/fxcrt/fx_system.h" 13 14 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 15 #include <direct.h> 16 17 class CFindFileDataA; 18 typedef CFindFileDataA FX_FileHandle; 19 #define FX_FILESIZE int32_t 20 21 #else 22 23 #include <dirent.h> 24 #include <fcntl.h> 25 #include <sys/stat.h> 26 #include <sys/types.h> 27 #include <unistd.h> 28 29 #ifndef O_BINARY 30 #define O_BINARY 0 31 #endif // O_BINARY 32 33 #ifndef O_LARGEFILE 34 #define O_LARGEFILE 0 35 #endif // O_LARGEFILE 36 37 typedef DIR FX_FileHandle; 38 #define FX_FILESIZE off_t 39 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 40 41 FX_FileHandle* FX_OpenFolder(const FX_CHAR* path); 42 bool FX_GetNextFile(FX_FileHandle* handle, 43 CFX_ByteString* filename, 44 bool* bFolder); 45 void FX_CloseFolder(FX_FileHandle* handle); 46 FX_WCHAR FX_GetFolderSeparator(); 47 48 #define FX_FILEMODE_Write 0 49 #define FX_FILEMODE_ReadOnly 1 50 #define FX_FILEMODE_Truncate 2 51 52 class IFX_WriteStream : virtual public CFX_Retainable { 53 public: 54 virtual bool WriteBlock(const void* pData, size_t size) = 0; 55 }; 56 57 class IFX_ReadStream : virtual public CFX_Retainable { 58 public: 59 virtual bool IsEOF() = 0; 60 virtual FX_FILESIZE GetPosition() = 0; 61 virtual size_t ReadBlock(void* buffer, size_t size) = 0; 62 }; 63 64 class IFX_SeekableWriteStream : public IFX_WriteStream { 65 public: 66 // IFX_WriteStream: 67 bool WriteBlock(const void* pData, size_t size) override; 68 69 virtual FX_FILESIZE GetSize() = 0; 70 virtual bool Flush() = 0; 71 virtual bool WriteBlock(const void* pData, 72 FX_FILESIZE offset, 73 size_t size) = 0; 74 }; 75 76 class IFX_SeekableReadStream : public IFX_ReadStream { 77 public: 78 static CFX_RetainPtr<IFX_SeekableReadStream> CreateFromFilename( 79 const FX_CHAR* filename); 80 81 // IFX_ReadStream: 82 bool IsEOF() override; 83 FX_FILESIZE GetPosition() override; 84 size_t ReadBlock(void* buffer, size_t size) override; 85 86 virtual bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0; 87 virtual FX_FILESIZE GetSize() = 0; 88 }; 89 90 class IFX_SeekableStream : public IFX_SeekableReadStream, 91 public IFX_SeekableWriteStream { 92 public: 93 static CFX_RetainPtr<IFX_SeekableStream> CreateFromFilename( 94 const FX_CHAR* filename, 95 uint32_t dwModes); 96 97 static CFX_RetainPtr<IFX_SeekableStream> CreateFromFilename( 98 const FX_WCHAR* filename, 99 uint32_t dwModes); 100 101 // IFX_SeekableReadStream: 102 bool IsEOF() override = 0; 103 FX_FILESIZE GetPosition() override = 0; 104 size_t ReadBlock(void* buffer, size_t size) override = 0; 105 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override = 0; 106 FX_FILESIZE GetSize() override = 0; 107 108 // IFX_SeekableWriteStream: 109 bool WriteBlock(const void* buffer, 110 FX_FILESIZE offset, 111 size_t size) override = 0; 112 bool WriteBlock(const void* buffer, size_t size) override; 113 bool Flush() override = 0; 114 }; 115 116 class IFX_MemoryStream : public IFX_SeekableStream { 117 public: 118 static CFX_RetainPtr<IFX_MemoryStream> Create(uint8_t* pBuffer, 119 size_t nSize, 120 bool bTakeOver = false); 121 static CFX_RetainPtr<IFX_MemoryStream> Create(bool bConsecutive = false); 122 123 virtual bool IsConsecutive() const = 0; 124 virtual void EstimateSize(size_t nInitSize, size_t nGrowSize) = 0; 125 virtual uint8_t* GetBuffer() const = 0; 126 virtual void AttachBuffer(uint8_t* pBuffer, 127 size_t nSize, 128 bool bTakeOver = false) = 0; 129 virtual void DetachBuffer() = 0; 130 }; 131 132 class IFX_BufferedReadStream : public IFX_ReadStream { 133 public: 134 // IFX_ReadStream: 135 bool IsEOF() override = 0; 136 FX_FILESIZE GetPosition() override = 0; 137 size_t ReadBlock(void* buffer, size_t size) override = 0; 138 139 virtual bool ReadNextBlock(bool bRestart = false) = 0; 140 virtual const uint8_t* GetBlockBuffer() = 0; 141 virtual size_t GetBlockSize() = 0; 142 virtual FX_FILESIZE GetBlockOffset() = 0; 143 }; 144 145 #ifdef PDF_ENABLE_XFA 146 class IFX_FileAccess : public CFX_Retainable { 147 public: 148 static CFX_RetainPtr<IFX_FileAccess> CreateDefault( 149 const CFX_WideStringC& wsPath); 150 151 virtual void GetPath(CFX_WideString& wsPath) = 0; 152 virtual CFX_RetainPtr<IFX_SeekableStream> CreateFileStream( 153 uint32_t dwModes) = 0; 154 }; 155 #endif // PDF_ENABLE_XFA 156 157 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 158 class CFindFileData { 159 public: ~CFindFileData()160 virtual ~CFindFileData() {} 161 HANDLE m_Handle; 162 bool m_bEnd; 163 }; 164 165 class CFindFileDataA : public CFindFileData { 166 public: ~CFindFileDataA()167 ~CFindFileDataA() override {} 168 WIN32_FIND_DATAA m_FindData; 169 }; 170 171 class CFindFileDataW : public CFindFileData { 172 public: ~CFindFileDataW()173 ~CFindFileDataW() override {} 174 WIN32_FIND_DATAW m_FindData; 175 }; 176 #endif 177 178 #endif // CORE_FXCRT_FX_STREAM_H_ 179