Home | History | Annotate | Download | only in crt
      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_IMP
      8 #define _FX_STREAM_IMP
      9 class CFX_StreamImp;
     10 class CFX_FileStreamImp;
     11 class CFX_BufferStreamImp;
     12 class CFX_FileReadStreamImp;
     13 class CFX_BufferReadStreamImp;
     14 class CFX_FileWriteStreamImp;
     15 class CFX_Stream;
     16 class CFX_TextStream;
     17 class CFX_FileRead;
     18 class CFX_FileWrite;
     19 class CFX_BufferAccImp;
     20 class CFX_StreamImp : public CFX_ThreadLock {
     21  public:
     22   virtual void Release() { delete this; }
     23   virtual FX_DWORD GetAccessModes() const { return m_dwAccess; }
     24   virtual int32_t GetLength() const = 0;
     25   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) = 0;
     26   virtual int32_t GetPosition() = 0;
     27   virtual FX_BOOL IsEOF() const = 0;
     28   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) = 0;
     29   virtual int32_t ReadString(FX_WCHAR* pStr,
     30                              int32_t iMaxLength,
     31                              FX_BOOL& bEOS) = 0;
     32   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) = 0;
     33   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) = 0;
     34   virtual void Flush() = 0;
     35   virtual FX_BOOL SetLength(int32_t iLength) = 0;
     36 
     37  protected:
     38   CFX_StreamImp();
     39   virtual ~CFX_StreamImp() {}
     40   FX_DWORD m_dwAccess;
     41 };
     42 class CFX_FileStreamImp : public CFX_StreamImp {
     43  public:
     44   CFX_FileStreamImp();
     45   virtual ~CFX_FileStreamImp();
     46   FX_BOOL LoadFile(const FX_WCHAR* pszSrcFileName, FX_DWORD dwAccess);
     47   virtual int32_t GetLength() const;
     48   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
     49   virtual int32_t GetPosition();
     50   virtual FX_BOOL IsEOF() const;
     51   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
     52   virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS);
     53   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize);
     54   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength);
     55   virtual void Flush();
     56   virtual FX_BOOL SetLength(int32_t iLength);
     57 
     58  protected:
     59   FXSYS_FILE* m_hFile;
     60   int32_t m_iLength;
     61 };
     62 class CFX_BufferStreamImp : public CFX_StreamImp {
     63  public:
     64   CFX_BufferStreamImp();
     65   virtual ~CFX_BufferStreamImp() {}
     66   FX_BOOL LoadBuffer(uint8_t* pData, int32_t iTotalSize, FX_DWORD dwAccess);
     67   virtual int32_t GetLength() const;
     68   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
     69   virtual int32_t GetPosition();
     70   virtual FX_BOOL IsEOF() const;
     71   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
     72   virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS);
     73   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize);
     74   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength);
     75   virtual void Flush() {}
     76   virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; }
     77 
     78  protected:
     79   uint8_t* m_pData;
     80   int32_t m_iTotalSize;
     81   int32_t m_iPosition;
     82   int32_t m_iLength;
     83 };
     84 class CFX_FileReadStreamImp : public CFX_StreamImp {
     85  public:
     86   CFX_FileReadStreamImp();
     87   virtual ~CFX_FileReadStreamImp() {}
     88   FX_BOOL LoadFileRead(IFX_FileRead* pFileRead, FX_DWORD dwAccess);
     89   virtual int32_t GetLength() const;
     90   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
     91   virtual int32_t GetPosition() { return m_iPosition; }
     92   virtual FX_BOOL IsEOF() const;
     93 
     94   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
     95   virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS);
     96   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) {
     97     return 0;
     98   }
     99   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) {
    100     return 0;
    101   }
    102   virtual void Flush() {}
    103   virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; }
    104 
    105  protected:
    106   IFX_FileRead* m_pFileRead;
    107   int32_t m_iPosition;
    108   int32_t m_iLength;
    109 };
    110 class CFX_BufferReadStreamImp : public CFX_StreamImp {
    111  public:
    112   CFX_BufferReadStreamImp();
    113   ~CFX_BufferReadStreamImp();
    114   FX_BOOL LoadBufferRead(IFX_BufferRead* pBufferRead,
    115                          int32_t iFileSize,
    116                          FX_DWORD dwAccess,
    117                          FX_BOOL bReleaseBufferRead);
    118 
    119   virtual int32_t GetLength() const;
    120   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
    121   virtual int32_t GetPosition() { return m_iPosition; }
    122   virtual FX_BOOL IsEOF() const;
    123 
    124   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
    125   virtual int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, FX_BOOL& bEOS);
    126   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) {
    127     return 0;
    128   }
    129   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) {
    130     return 0;
    131   }
    132   virtual void Flush() {}
    133   virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; }
    134 
    135  private:
    136   IFX_BufferRead* m_pBufferRead;
    137   FX_BOOL m_bReleaseBufferRead;
    138   int32_t m_iPosition;
    139   int32_t m_iBufferSize;
    140 };
    141 class CFX_FileWriteStreamImp : public CFX_StreamImp {
    142  public:
    143   CFX_FileWriteStreamImp();
    144   virtual ~CFX_FileWriteStreamImp() {}
    145   FX_BOOL LoadFileWrite(IFX_FileWrite* pFileWrite, FX_DWORD dwAccess);
    146   virtual int32_t GetLength() const;
    147   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
    148   virtual int32_t GetPosition() { return m_iPosition; }
    149   virtual FX_BOOL IsEOF() const;
    150   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) { return 0; }
    151   virtual int32_t ReadString(FX_WCHAR* pStr,
    152                              int32_t iMaxLength,
    153                              FX_BOOL& bEOS) {
    154     return 0;
    155   }
    156   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize);
    157   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength);
    158   virtual void Flush();
    159   virtual FX_BOOL SetLength(int32_t iLength) { return FALSE; }
    160 
    161  protected:
    162   IFX_FileWrite* m_pFileWrite;
    163   int32_t m_iPosition;
    164 };
    165 enum FX_STREAMTYPE {
    166   FX_SREAMTYPE_Unknown = 0,
    167   FX_STREAMTYPE_File,
    168   FX_STREAMTYPE_Buffer,
    169   FX_STREAMTYPE_Stream,
    170   FX_STREAMTYPE_BufferRead,
    171 };
    172 class CFX_Stream : public IFX_Stream, public CFX_ThreadLock {
    173  public:
    174   CFX_Stream();
    175   ~CFX_Stream();
    176   FX_BOOL LoadFile(const FX_WCHAR* pszSrcFileName, FX_DWORD dwAccess);
    177   FX_BOOL LoadBuffer(uint8_t* pData, int32_t iTotalSize, FX_DWORD dwAccess);
    178   FX_BOOL LoadFileRead(IFX_FileRead* pFileRead, FX_DWORD dwAccess);
    179   FX_BOOL LoadFileWrite(IFX_FileWrite* pFileWrite, FX_DWORD dwAccess);
    180   FX_BOOL LoadBufferRead(IFX_BufferRead* pBufferRead,
    181                          int32_t iFileSize,
    182                          FX_DWORD dwAccess,
    183                          FX_BOOL bReleaseBufferRead);
    184   virtual void Release();
    185   virtual IFX_Stream* Retain();
    186   virtual FX_DWORD GetAccessModes() const { return m_dwAccess; }
    187   virtual int32_t GetLength() const;
    188   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
    189   virtual int32_t GetPosition();
    190   virtual FX_BOOL IsEOF() const;
    191   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
    192   virtual int32_t ReadString(FX_WCHAR* pStr,
    193                              int32_t iMaxLength,
    194                              FX_BOOL& bEOS,
    195                              int32_t const* pByteSize = NULL);
    196   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize);
    197   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength);
    198   virtual void Flush();
    199   virtual FX_BOOL SetLength(int32_t iLength);
    200   virtual int32_t GetBOM(uint8_t bom[4]) const;
    201   virtual FX_WORD GetCodePage() const;
    202   virtual FX_WORD SetCodePage(FX_WORD wCodePage);
    203   virtual void Lock() { CFX_ThreadLock::Lock(); }
    204   virtual void Unlock() { CFX_ThreadLock::Unlock(); }
    205   virtual IFX_Stream* CreateSharedStream(FX_DWORD dwAccess,
    206                                          int32_t iOffset,
    207                                          int32_t iLength);
    208 
    209  protected:
    210   FX_STREAMTYPE m_eStreamType;
    211   CFX_StreamImp* m_pStreamImp;
    212   FX_DWORD m_dwAccess;
    213   int32_t m_iTotalSize;
    214   int32_t m_iPosition;
    215   int32_t m_iStart;
    216   int32_t m_iLength;
    217   int32_t m_iRefCount;
    218 };
    219 class CFX_TextStream : public IFX_Stream, public CFX_ThreadLock {
    220  public:
    221   CFX_TextStream(IFX_Stream* pStream, FX_BOOL bDelStream);
    222   ~CFX_TextStream();
    223   virtual void Release();
    224   virtual IFX_Stream* Retain();
    225 
    226   virtual FX_DWORD GetAccessModes() const;
    227   virtual int32_t GetLength() const;
    228   virtual int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset);
    229   virtual int32_t GetPosition();
    230   virtual FX_BOOL IsEOF() const;
    231 
    232   virtual int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize);
    233   virtual int32_t ReadString(FX_WCHAR* pStr,
    234                              int32_t iMaxLength,
    235                              FX_BOOL& bEOS,
    236                              int32_t const* pByteSize = NULL);
    237   virtual int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize);
    238   virtual int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength);
    239   virtual void Flush();
    240   virtual FX_BOOL SetLength(int32_t iLength);
    241 
    242   virtual int32_t GetBOM(uint8_t bom[4]) const;
    243   virtual FX_WORD GetCodePage() const;
    244   virtual FX_WORD SetCodePage(FX_WORD wCodePage);
    245 
    246   virtual void Lock() { CFX_ThreadLock::Lock(); }
    247   virtual void Unlock() { CFX_ThreadLock::Unlock(); }
    248 
    249   virtual IFX_Stream* CreateSharedStream(FX_DWORD dwAccess,
    250                                          int32_t iOffset,
    251                                          int32_t iLength);
    252 
    253  protected:
    254   FX_WORD m_wCodePage;
    255   int32_t m_wBOMLength;
    256   FX_DWORD m_dwBOM;
    257   uint8_t* m_pBuf;
    258   int32_t m_iBufSize;
    259   FX_BOOL m_bDelStream;
    260   IFX_Stream* m_pStreamImp;
    261   int32_t m_iRefCount;
    262   void InitStream();
    263 };
    264 
    265 class CFGAS_FileRead : public IFX_FileRead {
    266  public:
    267   CFGAS_FileRead(IFX_Stream* pStream, FX_BOOL bReleaseStream);
    268   virtual ~CFGAS_FileRead();
    269   virtual void Release() { delete this; }
    270   virtual FX_FILESIZE GetSize();
    271   virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size);
    272 
    273  protected:
    274   FX_BOOL m_bReleaseStream;
    275   IFX_Stream* m_pStream;
    276 };
    277 
    278 class CFX_BufferAccImp : public IFX_FileRead {
    279  public:
    280   CFX_BufferAccImp(IFX_BufferRead* pBufferRead,
    281                    FX_FILESIZE iFileSize,
    282                    FX_BOOL bReleaseStream);
    283   virtual ~CFX_BufferAccImp();
    284   virtual void Release() { delete this; }
    285   virtual FX_FILESIZE GetSize();
    286   virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size);
    287 
    288  protected:
    289   IFX_BufferRead* m_pBufferRead;
    290   FX_BOOL m_bReleaseStream;
    291   FX_FILESIZE m_iBufSize;
    292 };
    293 
    294 class CFGAS_FileWrite : public IFX_FileWrite {
    295  public:
    296   CFGAS_FileWrite(IFX_Stream* pStream, FX_BOOL bReleaseStream);
    297   virtual ~CFGAS_FileWrite();
    298   virtual void Release() { delete this; }
    299   virtual FX_FILESIZE GetSize();
    300   virtual FX_BOOL Flush();
    301   virtual FX_BOOL WriteBlock(const void* pData, size_t size);
    302   virtual FX_BOOL WriteBlock(const void* pData,
    303                              FX_FILESIZE offset,
    304                              size_t size);
    305 
    306  protected:
    307   IFX_Stream* m_pStream;
    308   FX_BOOL m_bReleaseStream;
    309 };
    310 
    311 #endif
    312