Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fgas / src / crt / fx_stream.h
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #ifndef _FX_STREAM_IMP\r
8 #define _FX_STREAM_IMP\r
9 class CFX_StreamImp;\r
10 class CFX_FileStreamImp;\r
11 class CFX_BufferStreamImp;\r
12 class CFX_FileReadStreamImp;\r
13 class CFX_BufferReadStreamImp;\r
14 class CFX_FileWriteStreamImp;\r
15 class CFX_Stream;\r
16 class CFX_TextStream;\r
17 class CFX_FileRead;\r
18 class CFX_FileWrite;\r
19 class CFX_BufferAccImp;\r
20 class CFX_StreamImp : public CFX_ThreadLock, public CFX_Object\r
21 {\r
22 public:\r
23     virtual void                        Release()\r
24     {\r
25         delete this;\r
26     }\r
27     virtual FX_DWORD            GetAccessModes() const\r
28     {\r
29         return m_dwAccess;\r
30     }\r
31     virtual FX_INT32            GetLength() const = 0;\r
32     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset) = 0;\r
33     virtual FX_INT32            GetPosition() = 0;\r
34     virtual FX_BOOL                     IsEOF() const = 0;\r
35     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize) = 0;\r
36     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS) = 0;\r
37     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize) = 0;\r
38     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength) = 0;\r
39     virtual void                        Flush() = 0;\r
40     virtual FX_BOOL                     SetLength(FX_INT32 iLength) = 0;\r
41 protected:\r
42     CFX_StreamImp();\r
43     virtual ~CFX_StreamImp() {}\r
44     FX_DWORD                            m_dwAccess;\r
45 };\r
46 class CFX_FileStreamImp : public CFX_StreamImp\r
47 {\r
48 public:\r
49     CFX_FileStreamImp();\r
50     virtual ~CFX_FileStreamImp();\r
51     FX_BOOL                     LoadFile(FX_LPCWSTR pszSrcFileName, FX_DWORD dwAccess);\r
52     virtual FX_INT32            GetLength() const;\r
53     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
54     virtual FX_INT32            GetPosition();\r
55     virtual FX_BOOL                     IsEOF() const;\r
56     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
57     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS);\r
58     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize);\r
59     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength);\r
60     virtual void                        Flush();\r
61     virtual FX_BOOL                     SetLength(FX_INT32 iLength);\r
62 protected:\r
63     FXSYS_FILE  *m_hFile;\r
64     FX_INT32    m_iLength;\r
65 };\r
66 class CFX_BufferStreamImp : public CFX_StreamImp\r
67 {\r
68 public:\r
69     CFX_BufferStreamImp();\r
70     virtual ~CFX_BufferStreamImp() {}\r
71     FX_BOOL                     LoadBuffer(FX_LPBYTE pData, FX_INT32 iTotalSize, FX_DWORD dwAccess);\r
72     virtual FX_INT32            GetLength() const;\r
73     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
74     virtual FX_INT32            GetPosition();\r
75     virtual FX_BOOL                     IsEOF() const;\r
76     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
77     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS);\r
78     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize);\r
79     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength);\r
80     virtual void                        Flush() {}\r
81     virtual FX_BOOL                     SetLength(FX_INT32 iLength)\r
82     {\r
83         return FALSE;\r
84     }\r
85 protected:\r
86     FX_LPBYTE   m_pData;\r
87     FX_INT32    m_iTotalSize;\r
88     FX_INT32    m_iPosition;\r
89     FX_INT32    m_iLength;\r
90 };\r
91 class CFX_FileReadStreamImp : public CFX_StreamImp\r
92 {\r
93 public:\r
94     CFX_FileReadStreamImp();\r
95     virtual ~CFX_FileReadStreamImp() {}\r
96     FX_BOOL                     LoadFileRead(IFX_FileRead *pFileRead, FX_DWORD dwAccess);\r
97     virtual FX_INT32            GetLength() const;\r
98     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
99     virtual FX_INT32            GetPosition()\r
100     {\r
101         return m_iPosition;\r
102     }\r
103     virtual FX_BOOL                     IsEOF() const;\r
104 \r
105     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
106     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS);\r
107     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize)\r
108     {\r
109         return 0;\r
110     }\r
111     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength)\r
112     {\r
113         return 0;\r
114     }\r
115     virtual void                        Flush() {}\r
116     virtual FX_BOOL                     SetLength(FX_INT32 iLength)\r
117     {\r
118         return FALSE;\r
119     }\r
120 protected:\r
121     IFX_FileRead                        *m_pFileRead;\r
122     FX_INT32                            m_iPosition;\r
123     FX_INT32                            m_iLength;\r
124 };\r
125 class CFX_BufferReadStreamImp : public CFX_StreamImp\r
126 {\r
127 public:\r
128     CFX_BufferReadStreamImp();\r
129     ~CFX_BufferReadStreamImp();\r
130     FX_BOOL                     LoadBufferRead(IFX_BufferRead *pBufferRead, FX_INT32 iFileSize, FX_DWORD dwAccess, FX_BOOL bReleaseBufferRead);\r
131 \r
132     virtual FX_INT32            GetLength() const;\r
133     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
134     virtual FX_INT32            GetPosition()\r
135     {\r
136         return m_iPosition;\r
137     }\r
138     virtual FX_BOOL                     IsEOF() const;\r
139 \r
140     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
141     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS);\r
142     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize)\r
143     {\r
144         return 0;\r
145     }\r
146     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength)\r
147     {\r
148         return 0;\r
149     }\r
150     virtual void                        Flush() {}\r
151     virtual FX_BOOL                     SetLength(FX_INT32 iLength)\r
152     {\r
153         return FALSE;\r
154     }\r
155 private:\r
156     IFX_BufferRead                      *m_pBufferRead;\r
157     FX_BOOL                                     m_bReleaseBufferRead;\r
158     FX_INT32                            m_iPosition;\r
159     FX_INT32                            m_iBufferSize;\r
160 };\r
161 class CFX_FileWriteStreamImp : public CFX_StreamImp\r
162 {\r
163 public:\r
164     CFX_FileWriteStreamImp();\r
165     virtual ~CFX_FileWriteStreamImp() {}\r
166     FX_BOOL                     LoadFileWrite(IFX_FileWrite *pFileWrite, FX_DWORD dwAccess);\r
167     virtual FX_INT32            GetLength() const;\r
168     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
169     virtual FX_INT32            GetPosition()\r
170     {\r
171         return m_iPosition;\r
172     }\r
173     virtual FX_BOOL                     IsEOF() const;\r
174     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize)\r
175     {\r
176         return 0;\r
177     }\r
178     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS)\r
179     {\r
180         return 0;\r
181     }\r
182     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize);\r
183     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength);\r
184     virtual void                        Flush();\r
185     virtual FX_BOOL                     SetLength(FX_INT32 iLength)\r
186     {\r
187         return FALSE;\r
188     }\r
189 protected:\r
190     IFX_FileWrite                       *m_pFileWrite;\r
191     FX_INT32                            m_iPosition;\r
192 };\r
193 enum FX_STREAMTYPE {\r
194     FX_SREAMTYPE_Unknown        =  0,\r
195     FX_STREAMTYPE_File                  ,\r
196     FX_STREAMTYPE_Buffer                ,\r
197     FX_STREAMTYPE_Stream                ,\r
198     FX_STREAMTYPE_BufferRead    ,\r
199 };\r
200 class CFX_Stream : public IFX_Stream, public CFX_ThreadLock, public CFX_Object\r
201 {\r
202 public:\r
203     CFX_Stream();\r
204     ~CFX_Stream();\r
205     FX_BOOL                     LoadFile(FX_LPCWSTR pszSrcFileName, FX_DWORD dwAccess);\r
206     FX_BOOL                     LoadBuffer(FX_LPBYTE pData, FX_INT32 iTotalSize, FX_DWORD dwAccess);\r
207     FX_BOOL                     LoadFileRead(IFX_FileRead *pFileRead, FX_DWORD dwAccess);\r
208     FX_BOOL                     LoadFileWrite(IFX_FileWrite *pFileWrite, FX_DWORD dwAccess);\r
209     FX_BOOL                     LoadBufferRead(IFX_BufferRead *pBufferRead, FX_INT32 iFileSize, FX_DWORD dwAccess, FX_BOOL bReleaseBufferRead);\r
210     virtual void                        Release();\r
211     virtual IFX_Stream*         Retain();\r
212     virtual FX_DWORD            GetAccessModes() const\r
213     {\r
214         return m_dwAccess;\r
215     }\r
216     virtual FX_INT32            GetLength() const;\r
217     virtual FX_INT32            Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
218     virtual FX_INT32            GetPosition();\r
219     virtual FX_BOOL                     IsEOF() const;\r
220     virtual FX_INT32            ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
221     virtual FX_INT32            ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS, FX_INT32 const *pByteSize = NULL);\r
222     virtual FX_INT32            WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize);\r
223     virtual FX_INT32            WriteString(FX_LPCWSTR pStr, FX_INT32 iLength);\r
224     virtual void                        Flush();\r
225     virtual FX_BOOL                     SetLength(FX_INT32 iLength);\r
226     virtual FX_INT32            GetBOM(FX_BYTE bom[4]) const;\r
227     virtual FX_WORD                     GetCodePage() const;\r
228     virtual FX_WORD                     SetCodePage(FX_WORD wCodePage);\r
229     virtual void                        Lock()\r
230     {\r
231         CFX_ThreadLock::Lock();\r
232     }\r
233     virtual void                        Unlock()\r
234     {\r
235         CFX_ThreadLock::Unlock();\r
236     }\r
237     virtual IFX_Stream*         CreateSharedStream(FX_DWORD dwAccess, FX_INT32 iOffset, FX_INT32 iLength);\r
238 protected:\r
239     FX_STREAMTYPE                       m_eStreamType;\r
240     CFX_StreamImp                       *m_pStreamImp;\r
241     FX_DWORD                            m_dwAccess;\r
242     FX_INT32                            m_iTotalSize;\r
243     FX_INT32                            m_iPosition;\r
244     FX_INT32                            m_iStart;\r
245     FX_INT32                            m_iLength;\r
246     FX_INT32                            m_iRefCount;\r
247 };\r
248 class CFX_TextStream : public IFX_Stream, public CFX_ThreadLock, public CFX_Object\r
249 {\r
250 public:\r
251     CFX_TextStream(IFX_Stream *pStream, FX_BOOL bDelStream);\r
252     ~CFX_TextStream();\r
253     virtual void                                Release();\r
254     virtual IFX_Stream*                 Retain();\r
255 \r
256     virtual FX_DWORD                    GetAccessModes() const;\r
257     virtual FX_INT32                    GetLength() const;\r
258     virtual FX_INT32                    Seek(FX_STREAMSEEK eSeek, FX_INT32 iOffset);\r
259     virtual FX_INT32                    GetPosition();\r
260     virtual FX_BOOL                             IsEOF() const;\r
261 \r
262     virtual FX_INT32                    ReadData(FX_LPBYTE pBuffer, FX_INT32 iBufferSize);\r
263     virtual FX_INT32                    ReadString(FX_LPWSTR pStr, FX_INT32 iMaxLength, FX_BOOL &bEOS, FX_INT32 const *pByteSize = NULL);\r
264     virtual FX_INT32                    WriteData(FX_LPCBYTE pBuffer, FX_INT32 iBufferSize);\r
265     virtual FX_INT32                    WriteString(FX_LPCWSTR pStr, FX_INT32 iLength);\r
266     virtual void                                Flush();\r
267     virtual FX_BOOL                             SetLength(FX_INT32 iLength);\r
268 \r
269     virtual FX_INT32                    GetBOM(FX_BYTE bom[4]) const;\r
270     virtual FX_WORD                             GetCodePage() const;\r
271     virtual FX_WORD                             SetCodePage(FX_WORD wCodePage);\r
272 \r
273     virtual void                                Lock()\r
274     {\r
275         CFX_ThreadLock::Lock();\r
276     }\r
277     virtual void                                Unlock()\r
278     {\r
279         CFX_ThreadLock::Unlock();\r
280     }\r
281 \r
282     virtual IFX_Stream*                 CreateSharedStream(FX_DWORD dwAccess, FX_INT32 iOffset, FX_INT32 iLength);\r
283 protected:\r
284     FX_WORD             m_wCodePage;\r
285     FX_WORD             m_wBOMLength;\r
286     FX_DWORD    m_dwBOM;\r
287     FX_LPBYTE   m_pBuf;\r
288     FX_INT32    m_iBufSize;\r
289     FX_BOOL             m_bDelStream;\r
290     IFX_Stream  *m_pStreamImp;\r
291     FX_INT32    m_iRefCount;\r
292     void                InitStream();\r
293 };\r
294 #ifdef FX_FILESIZE\r
295 class CFGAS_FileRead : public IFX_FileRead, public CFX_Object\r
296 {\r
297 public:\r
298     CFGAS_FileRead(IFX_Stream *pStream, FX_BOOL bReleaseStream);\r
299     virtual ~CFGAS_FileRead();\r
300     virtual void                        Release()\r
301     {\r
302         delete this;\r
303     }\r
304     virtual FX_FILESIZE         GetSize();\r
305     virtual FX_BOOL                     ReadBlock(void* buffer, FX_FILESIZE offset, size_t size);\r
306 protected:\r
307     FX_BOOL                                     m_bReleaseStream;\r
308     IFX_Stream                          *m_pStream;\r
309 };\r
310 #else\r
311 class CFGAS_FileRead : public IFX_FileRead, public CFX_Object\r
312 {\r
313 public:\r
314     CFGAS_FileRead(IFX_Stream *pStream, FX_BOOL bReleaseStream);\r
315     virtual ~CFGAS_FileRead();\r
316 \r
317     virtual void                        Release()\r
318     {\r
319         delete this;\r
320     }\r
321     virtual FX_DWORD            GetSize();\r
322     virtual FX_BOOL                     ReadBlock(void* buffer, FX_DWORD offset, FX_DWORD size);\r
323 \r
324 protected:\r
325     FX_BOOL                                     m_bReleaseStream;\r
326     IFX_Stream                          *m_pStream;\r
327 };\r
328 #endif\r
329 #ifdef FX_FILESIZE\r
330 class CFX_BufferAccImp : public IFX_FileRead, public CFX_Object\r
331 {\r
332 public:\r
333     CFX_BufferAccImp(IFX_BufferRead *pBufferRead, FX_FILESIZE iFileSize, FX_BOOL bReleaseStream);\r
334     virtual ~CFX_BufferAccImp();\r
335     virtual void                        Release()\r
336     {\r
337         delete this;\r
338     }\r
339     virtual FX_FILESIZE         GetSize();\r
340     virtual FX_BOOL                     ReadBlock(void* buffer, FX_FILESIZE offset, size_t size);\r
341 protected:\r
342     IFX_BufferRead                      *m_pBufferRead;\r
343     FX_BOOL                                     m_bReleaseStream;\r
344     FX_FILESIZE                         m_iBufSize;\r
345 };\r
346 #else\r
347 class CFX_BufferAccImp : public IFX_FileRead, public CFX_Object\r
348 {\r
349 public:\r
350     CFX_BufferAccImp(IFX_BufferRead *pBufferRead, FX_INT32 iFileSize, FX_BOOL bReleaseStream);\r
351     virtual ~CFX_BufferAccImp();\r
352 \r
353     virtual void                        Release()\r
354     {\r
355         delete this;\r
356     }\r
357     virtual FX_DWORD            GetSize();\r
358     virtual FX_BOOL                     ReadBlock(void* buffer, FX_DWORD offset, FX_DWORD size);\r
359 \r
360 protected:\r
361     IFX_BufferRead                      *m_pBufferRead;\r
362     FX_BOOL                                     m_bReleaseStream;\r
363     FX_INT32                            m_iBufSize;\r
364 };\r
365 #endif\r
366 #ifdef FX_FILESIZE\r
367 class CFGAS_FileWrite : public IFX_FileWrite, public CFX_Object\r
368 {\r
369 public:\r
370     CFGAS_FileWrite(IFX_Stream *pStream, FX_BOOL bReleaseStream);\r
371     virtual ~CFGAS_FileWrite();\r
372     virtual void                        Release()\r
373     {\r
374         delete this;\r
375     }\r
376     virtual FX_FILESIZE         GetSize();\r
377     virtual FX_BOOL                     Flush();\r
378     virtual     FX_BOOL                 WriteBlock(const void* pData, size_t size);\r
379     virtual     FX_BOOL                 WriteBlock(const void* pData, FX_FILESIZE offset, size_t size);\r
380 protected:\r
381     IFX_Stream                          *m_pStream;\r
382     FX_BOOL                                     m_bReleaseStream;\r
383 };\r
384 #else\r
385 class CFGAS_FileWrite : public IFX_FileWrite, public CFX_Object\r
386 {\r
387 public:\r
388     CFGAS_FileWrite(IFX_Stream *pStream, FX_BOOL bReleaseStream);\r
389     virtual ~CFGAS_FileWrite();\r
390 \r
391     virtual void                        Release()\r
392     {\r
393         delete this;\r
394     }\r
395     virtual FX_DWORD            GetSize();\r
396     virtual FX_DWORD            Flush();\r
397     virtual     FX_BOOL                 WriteBlock(const void* pData, FX_DWORD size);\r
398     virtual     FX_BOOL                 WriteBlock(const void* pData, FX_DWORD offset, FX_DWORD size);\r
399 \r
400 protected:\r
401     IFX_Stream                          *m_pStream;\r
402     FX_BOOL                                     m_bReleaseStream;\r
403 };\r
404 #endif\r
405 #endif\r