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_memory.h"
11 #include "fx_string.h"
12 
13 void* FX_OpenFolder(FX_LPCSTR path);
14 void* FX_OpenFolder(FX_LPCWSTR path);
15 FX_BOOL FX_GetNextFile(void* handle, CFX_ByteString& filename, FX_BOOL& bFolder);
16 FX_BOOL FX_GetNextFile(void* handle, CFX_WideString& filename, FX_BOOL& bFolder);
17 void FX_CloseFolder(void* handle);
18 FX_WCHAR FX_GetFolderSeparator();
19 typedef struct FX_HFILE_ {
20     FX_LPVOID pData;
21 }* FX_HFILE;
22 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
23 #define FX_FILESIZE			FX_INT32
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)  ((FX_BYTE)(a>>24))
41 #define FX_GETBYTEOFFSET16(a)  ((FX_BYTE)(a>>16))
42 #define FX_GETBYTEOFFSET8(a)   ((FX_BYTE)(a>>8))
43 #define FX_GETBYTEOFFSET0(a)   ((FX_BYTE)(a))
44 #define FX_FILEMODE_Write		0
45 #define FX_FILEMODE_ReadOnly	1
46 #define FX_FILEMODE_Truncate	2
47 FX_HFILE	FX_File_Open(FX_BSTR fileName, FX_DWORD dwMode);
48 FX_HFILE	FX_File_Open(FX_WSTR fileName, FX_DWORD dwMode);
49 void		FX_File_Close(FX_HFILE hFile);
50 FX_FILESIZE	FX_File_GetSize(FX_HFILE hFile);
51 FX_FILESIZE	FX_File_GetPosition(FX_HFILE hFile);
52 FX_FILESIZE	FX_File_SetPosition(FX_HFILE hFile, FX_FILESIZE pos);
53 size_t		FX_File_Read(FX_HFILE hFile, void* pBuffer, size_t szBuffer);
54 size_t		FX_File_ReadPos(FX_HFILE hFile, void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
55 size_t		FX_File_Write(FX_HFILE hFile, const void* pBuffer, size_t szBuffer);
56 size_t		FX_File_WritePos(FX_HFILE hFile, const void* pBuffer, size_t szBuffer, FX_FILESIZE pos);
57 FX_BOOL		FX_File_Flush(FX_HFILE hFile);
58 FX_BOOL		FX_File_Truncate(FX_HFILE hFile, FX_FILESIZE szFile);
59 FX_BOOL		FX_File_Exist(FX_BSTR fileName);
60 FX_BOOL		FX_File_Exist(FX_WSTR fileName);
61 FX_BOOL		FX_File_Delete(FX_BSTR fileName);
62 FX_BOOL		FX_File_Delete(FX_WSTR fileName);
63 FX_BOOL		FX_File_Copy(FX_BSTR fileNameSrc, FX_BSTR fileNameDst);
64 FX_BOOL		FX_File_Copy(FX_WSTR fileNameSrc, FX_WSTR fileNameDst);
65 FX_BOOL		FX_File_Move(FX_BSTR fileNameSrc, FX_BSTR fileNameDst);
66 FX_BOOL		FX_File_Move(FX_WSTR fileNameSrc, FX_WSTR fileNameDst);
67 class IFX_StreamWrite
68 {
69 public:
~IFX_StreamWrite()70     virtual ~IFX_StreamWrite() { }
71     virtual void		Release() = 0;
72 
73     virtual	FX_BOOL		WriteBlock(const void* pData, size_t size) = 0;
74 };
75 class IFX_FileWrite : public IFX_StreamWrite
76 {
77 public:
78 
79     virtual void			Release() = 0;
80 
81     virtual FX_FILESIZE		GetSize() = 0;
82 
83     virtual FX_BOOL			Flush() = 0;
84 
85     virtual	FX_BOOL			WriteBlock(const void* pData, FX_FILESIZE offset, size_t size) = 0;
WriteBlock(const void * pData,size_t size)86     virtual	FX_BOOL			WriteBlock(const void* pData, size_t size)
87     {
88         return WriteBlock(pData, GetSize(), size);
89     }
90 };
91 IFX_FileWrite* FX_CreateFileWrite(FX_LPCSTR filename);
92 IFX_FileWrite* FX_CreateFileWrite(FX_LPCWSTR filename);
93 class IFX_StreamRead
94 {
95 public:
~IFX_StreamRead()96     virtual ~IFX_StreamRead() { }
97 
98     virtual void			Release() = 0;
99 
100     virtual FX_BOOL			IsEOF() = 0;
101 
102     virtual FX_FILESIZE		GetPosition() = 0;
103 
104     virtual size_t			ReadBlock(void* buffer, size_t size) = 0;
105 };
106 class IFX_FileRead : IFX_StreamRead
107 {
108 public:
109     virtual void			Release() = 0;
110 
111     virtual FX_FILESIZE		GetSize() = 0;
112 
IsEOF()113     virtual FX_BOOL			IsEOF()
114     {
115         return FALSE;
116     }
117 
GetPosition()118     virtual FX_FILESIZE		GetPosition()
119     {
120         return 0;
121     }
122 
SetRange(FX_FILESIZE offset,FX_FILESIZE size)123     virtual FX_BOOL			SetRange(FX_FILESIZE offset, FX_FILESIZE size)
124     {
125         return FALSE;
126     }
127 
ClearRange()128     virtual void			ClearRange() {}
129 
130     virtual FX_BOOL			ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0;
131 
ReadBlock(void * buffer,size_t size)132     virtual size_t			ReadBlock(void* buffer, size_t size)
133     {
134         return 0;
135     }
136 };
137 IFX_FileRead* FX_CreateFileRead(FX_LPCSTR filename);
138 IFX_FileRead* FX_CreateFileRead(FX_LPCWSTR filename);
139 class IFX_FileStream : public IFX_FileRead, public IFX_FileWrite
140 {
141 public:
142 
143     virtual IFX_FileStream*		Retain() = 0;
144 
145     virtual void				Release() = 0;
146 
147     virtual FX_FILESIZE			GetSize() = 0;
148 
149     virtual FX_BOOL				IsEOF() = 0;
150 
151     virtual FX_FILESIZE			GetPosition() = 0;
152 
153     virtual FX_BOOL				ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0;
154 
155     virtual size_t				ReadBlock(void* buffer, size_t size) = 0;
156 
157     virtual	FX_BOOL				WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) = 0;
WriteBlock(const void * buffer,size_t size)158     virtual	FX_BOOL				WriteBlock(const void* buffer, size_t size)
159     {
160         return WriteBlock(buffer, GetSize(), size);
161     }
162 
163     virtual FX_BOOL				Flush() = 0;
164 };
165 IFX_FileStream*		FX_CreateFileStream(FX_LPCSTR filename, FX_DWORD dwModes);
166 IFX_FileStream*		FX_CreateFileStream(FX_LPCWSTR filename, FX_DWORD dwModes);
167 class IFX_MemoryStream : public IFX_FileStream
168 {
169 public:
170 
171     virtual FX_BOOL			IsConsecutive() const = 0;
172 
173     virtual void			EstimateSize(size_t nInitSize, size_t nGrowSize) = 0;
174 
175     virtual FX_LPBYTE		GetBuffer() const = 0;
176 
177     virtual void			AttachBuffer(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE) = 0;
178 
179     virtual void			DetachBuffer() = 0;
180 };
181 IFX_MemoryStream*	FX_CreateMemoryStream(FX_LPBYTE pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE);
182 IFX_MemoryStream*	FX_CreateMemoryStream(FX_BOOL bConsecutive = FALSE);
183 class IFX_BufferRead : public IFX_StreamRead
184 {
185 public:
186 
187     virtual void			Release() = 0;
188 
189     virtual FX_BOOL			IsEOF() = 0;
190 
191     virtual FX_FILESIZE		GetPosition() = 0;
192 
193     virtual size_t			ReadBlock(void* buffer, size_t size) = 0;
194 
195     virtual FX_BOOL			ReadNextBlock(FX_BOOL bRestart = FALSE) = 0;
196 
197     virtual FX_LPCBYTE		GetBlockBuffer() = 0;
198 
199     virtual size_t			GetBlockSize() = 0;
200 
201     virtual FX_FILESIZE		GetBlockOffset() = 0;
202 };
203 
204 #endif  // CORE_INCLUDE_FXCRT_FX_STREAM_H_
205