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_INCLUDE_FXCRT_FX_STREAM_H_
8 #define CORE_INCLUDE_FXCRT_FX_STREAM_H_
9 
10 #include "fx_string.h"
11 
12 void* FX_OpenFolder(const FX_CHAR* path);
13 void* FX_OpenFolder(const FX_WCHAR* path);
14 FX_BOOL FX_GetNextFile(void* handle,
15                        CFX_ByteString& filename,
16                        FX_BOOL& bFolder);
17 FX_BOOL FX_GetNextFile(void* handle,
18                        CFX_WideString& filename,
19                        FX_BOOL& bFolder);
20 void FX_CloseFolder(void* handle);
21 FX_WCHAR FX_GetFolderSeparator();
22 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
23 #define FX_FILESIZE int32_t
24 #else
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #ifndef O_BINARY
29 #define O_BINARY 0
30 #endif
31 #ifndef O_LARGEFILE
32 #define O_LARGEFILE 0
33 #endif
34 #define FX_FILESIZE off_t
35 #endif
36 #define FX_GETBYTEOFFSET32(a) 0
37 #define FX_GETBYTEOFFSET40(a) 0
38 #define FX_GETBYTEOFFSET48(a) 0
39 #define FX_GETBYTEOFFSET56(a) 0
40 #define FX_GETBYTEOFFSET24(a) ((uint8_t)(a >> 24))
41 #define FX_GETBYTEOFFSET16(a) ((uint8_t)(a >> 16))
42 #define FX_GETBYTEOFFSET8(a) ((uint8_t)(a >> 8))
43 #define FX_GETBYTEOFFSET0(a) ((uint8_t)(a))
44 #define FX_FILEMODE_Write 0
45 #define FX_FILEMODE_ReadOnly 1
46 #define FX_FILEMODE_Truncate 2
47 
48 class IFX_StreamWrite {
49  public:
~IFX_StreamWrite()50   virtual ~IFX_StreamWrite() {}
51   virtual void Release() = 0;
52   virtual FX_BOOL WriteBlock(const void* pData, size_t size) = 0;
53 };
54 
55 class IFX_FileWrite : public IFX_StreamWrite {
56  public:
57   // IFX_StreamWrite:
WriteBlock(const void * pData,size_t size)58   FX_BOOL WriteBlock(const void* pData, size_t size) override {
59     return WriteBlock(pData, GetSize(), size);
60   }
61 
62   virtual FX_FILESIZE GetSize() = 0;
63   virtual FX_BOOL Flush() = 0;
64   virtual FX_BOOL WriteBlock(const void* pData,
65                              FX_FILESIZE offset,
66                              size_t size) = 0;
67 };
68 
69 class IFX_StreamRead {
70  public:
~IFX_StreamRead()71   virtual ~IFX_StreamRead() {}
72 
73   virtual void Release() = 0;
74   virtual FX_BOOL IsEOF() = 0;
75   virtual FX_FILESIZE GetPosition() = 0;
76   virtual size_t ReadBlock(void* buffer, size_t size) = 0;
77 };
78 
79 class IFX_FileRead : IFX_StreamRead {
80  public:
81   // IFX_StreamRead:
82   void Release() override = 0;
IsEOF()83   FX_BOOL IsEOF() override { return FALSE; }
GetPosition()84   FX_FILESIZE GetPosition() override { return 0; }
ReadBlock(void * buffer,size_t size)85   size_t ReadBlock(void* buffer, size_t size) override { return 0; }
86 
87   virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0;
88   virtual FX_FILESIZE GetSize() = 0;
89 };
90 
91 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename);
92 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename);
93 
94 class IFX_FileStream : public IFX_FileRead, public IFX_FileWrite {
95  public:
96   virtual IFX_FileStream* Retain() = 0;
97 
98   // IFX_FileRead:
99   void Release() override = 0;
100   FX_BOOL IsEOF() override = 0;
101   FX_FILESIZE GetPosition() override = 0;
102   size_t ReadBlock(void* buffer, size_t size) override = 0;
103   FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override = 0;
104   FX_FILESIZE GetSize() override = 0;
105 
106   // IFX_FileWrite:
107   FX_BOOL WriteBlock(const void* buffer,
108                      FX_FILESIZE offset,
109                      size_t size) override = 0;
WriteBlock(const void * buffer,size_t size)110   FX_BOOL WriteBlock(const void* buffer, size_t size) override {
111     return WriteBlock(buffer, GetSize(), size);
112   }
113   FX_BOOL Flush() override = 0;
114 };
115 
116 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes);
117 IFX_FileStream* FX_CreateFileStream(const FX_WCHAR* filename, FX_DWORD dwModes);
118 
119 #ifdef PDF_ENABLE_XFA
120 class IFX_FileAccess {
121  public:
~IFX_FileAccess()122   virtual ~IFX_FileAccess() {}
123   virtual void Release() = 0;
124   virtual IFX_FileAccess* Retain() = 0;
125   virtual void GetPath(CFX_WideString& wsPath) = 0;
126   virtual IFX_FileStream* CreateFileStream(FX_DWORD dwModes) = 0;
127 };
128 IFX_FileAccess* FX_CreateDefaultFileAccess(const CFX_WideStringC& wsPath);
129 #endif  // PDF_ENABLE_XFA
130 
131 class IFX_MemoryStream : public IFX_FileStream {
132  public:
133   virtual FX_BOOL IsConsecutive() const = 0;
134 
135   virtual void EstimateSize(size_t nInitSize, size_t nGrowSize) = 0;
136 
137   virtual uint8_t* GetBuffer() const = 0;
138 
139   virtual void AttachBuffer(uint8_t* pBuffer,
140                             size_t nSize,
141                             FX_BOOL bTakeOver = FALSE) = 0;
142 
143   virtual void DetachBuffer() = 0;
144 };
145 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer,
146                                         size_t nSize,
147                                         FX_BOOL bTakeOver = FALSE);
148 IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive = FALSE);
149 class IFX_BufferRead : public IFX_StreamRead {
150  public:
151   // IFX_StreamRead:
152   void Release() override = 0;
153   FX_BOOL IsEOF() override = 0;
154   FX_FILESIZE GetPosition() override = 0;
155   size_t ReadBlock(void* buffer, size_t size) override = 0;
156 
157   virtual FX_BOOL ReadNextBlock(FX_BOOL bRestart = FALSE) = 0;
158   virtual const uint8_t* GetBlockBuffer() = 0;
159   virtual size_t GetBlockSize() = 0;
160   virtual FX_FILESIZE GetBlockOffset() = 0;
161 };
162 
163 #endif  // CORE_INCLUDE_FXCRT_FX_STREAM_H_
164