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