| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "xfa/fgas/crt/fgas_stream.h" | 7 #include "xfa/fgas/crt/fgas_stream.h" |
| 8 | 8 |
| 9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ | 9 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN32_MOBILE_ || \ |
| 10 _FX_OS_ == _FX_WIN64_ | 10 _FX_OS_ == _FX_WIN64_ |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 bool LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); | 201 bool LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); |
| 202 bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, | 202 bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 203 uint32_t dwAccess); | 203 uint32_t dwAccess); |
| 204 bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, | 204 bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 205 uint32_t dwAccess); | 205 uint32_t dwAccess); |
| 206 bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, | 206 bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, |
| 207 int32_t iFileSize, | 207 int32_t iFileSize, |
| 208 uint32_t dwAccess); | 208 uint32_t dwAccess); |
| 209 | 209 |
| 210 // IFGAS_Stream | 210 // IFGAS_Stream |
| 211 void Release() override; | |
| 212 IFGAS_Stream* Retain() override; | |
| 213 uint32_t GetAccessModes() const override; | 211 uint32_t GetAccessModes() const override; |
| 214 int32_t GetLength() const override; | 212 int32_t GetLength() const override; |
| 215 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 213 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
| 216 int32_t GetPosition() override; | 214 int32_t GetPosition() override; |
| 217 bool IsEOF() const override; | 215 bool IsEOF() const override; |
| 218 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 216 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
| 219 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; | 217 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; |
| 220 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; | 218 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; |
| 221 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; | 219 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; |
| 222 void Flush() override; | 220 void Flush() override; |
| 223 bool SetLength(int32_t iLength) override; | 221 bool SetLength(int32_t iLength) override; |
| 224 int32_t GetBOM(uint8_t bom[4]) const override; | 222 int32_t GetBOM(uint8_t bom[4]) const override; |
| 225 uint16_t GetCodePage() const override; | 223 uint16_t GetCodePage() const override; |
| 226 uint16_t SetCodePage(uint16_t wCodePage) override; | 224 uint16_t SetCodePage(uint16_t wCodePage) override; |
| 227 IFGAS_Stream* CreateSharedStream(uint32_t dwAccess, | 225 CFX_RetainPtr<IFGAS_Stream> CreateSharedStream(uint32_t dwAccess, |
| 228 int32_t iOffset, | 226 int32_t iOffset, |
| 229 int32_t iLength) override; | 227 int32_t iLength) override; |
| 230 | 228 |
| 231 protected: | 229 protected: |
| 232 FX_STREAMTYPE m_eStreamType; | 230 FX_STREAMTYPE m_eStreamType; |
| 233 IFGAS_StreamImp* m_pStreamImp; | 231 IFGAS_StreamImp* m_pStreamImp; |
| 234 uint32_t m_dwAccess; | 232 uint32_t m_dwAccess; |
| 235 int32_t m_iTotalSize; | 233 int32_t m_iTotalSize; |
| 236 int32_t m_iPosition; | 234 int32_t m_iPosition; |
| 237 int32_t m_iStart; | 235 int32_t m_iStart; |
| 238 int32_t m_iLength; | 236 int32_t m_iLength; |
| 239 int32_t m_iRefCount; | 237 int32_t m_iRefCount; |
| 240 }; | 238 }; |
| 241 | 239 |
| 242 class CFGAS_TextStream : public IFGAS_Stream { | 240 class CFGAS_TextStream : public IFGAS_Stream { |
| 243 public: | 241 public: |
| 244 CFGAS_TextStream(IFGAS_Stream* pStream, bool bDelStream); | 242 explicit CFGAS_TextStream(const CFX_RetainPtr<IFGAS_Stream>& pStream); |
| 245 ~CFGAS_TextStream() override; | 243 ~CFGAS_TextStream() override; |
| 246 | 244 |
| 247 // IFGAS_Stream | 245 // IFGAS_Stream |
| 248 void Release() override; | |
| 249 IFGAS_Stream* Retain() override; | |
| 250 uint32_t GetAccessModes() const override; | 246 uint32_t GetAccessModes() const override; |
| 251 int32_t GetLength() const override; | 247 int32_t GetLength() const override; |
| 252 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 248 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
| 253 int32_t GetPosition() override; | 249 int32_t GetPosition() override; |
| 254 bool IsEOF() const override; | 250 bool IsEOF() const override; |
| 255 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 251 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
| 256 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; | 252 int32_t ReadString(FX_WCHAR* pStr, int32_t iMaxLength, bool& bEOS) override; |
| 257 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; | 253 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; |
| 258 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; | 254 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; |
| 259 void Flush() override; | 255 void Flush() override; |
| 260 bool SetLength(int32_t iLength) override; | 256 bool SetLength(int32_t iLength) override; |
| 261 int32_t GetBOM(uint8_t bom[4]) const override; | 257 int32_t GetBOM(uint8_t bom[4]) const override; |
| 262 uint16_t GetCodePage() const override; | 258 uint16_t GetCodePage() const override; |
| 263 uint16_t SetCodePage(uint16_t wCodePage) override; | 259 uint16_t SetCodePage(uint16_t wCodePage) override; |
| 264 IFGAS_Stream* CreateSharedStream(uint32_t dwAccess, | 260 CFX_RetainPtr<IFGAS_Stream> CreateSharedStream(uint32_t dwAccess, |
| 265 int32_t iOffset, | 261 int32_t iOffset, |
| 266 int32_t iLength) override; | 262 int32_t iLength) override; |
| 267 | 263 |
| 268 protected: | 264 protected: |
| 265 void InitStream(); |
| 266 |
| 269 uint16_t m_wCodePage; | 267 uint16_t m_wCodePage; |
| 270 int32_t m_wBOMLength; | 268 int32_t m_wBOMLength; |
| 271 uint32_t m_dwBOM; | 269 uint32_t m_dwBOM; |
| 272 uint8_t* m_pBuf; | 270 uint8_t* m_pBuf; |
| 273 int32_t m_iBufSize; | 271 int32_t m_iBufSize; |
| 274 bool m_bDelStream; | 272 CFX_RetainPtr<IFGAS_Stream> m_pStreamImp; |
| 275 IFGAS_Stream* m_pStreamImp; | |
| 276 int32_t m_iRefCount; | |
| 277 void InitStream(); | |
| 278 }; | 273 }; |
| 279 | 274 |
| 280 class CFGAS_FileRead : public IFX_SeekableReadStream { | 275 class CFGAS_FileRead : public IFX_SeekableReadStream { |
| 281 public: | 276 public: |
| 282 static CFX_RetainPtr<CFGAS_FileRead> Create(IFGAS_Stream* pStream, | 277 static CFX_RetainPtr<CFGAS_FileRead> Create( |
| 283 bool bReleaseStream); | 278 const CFX_RetainPtr<IFGAS_Stream>& pStream); |
| 279 |
| 280 explicit CFGAS_FileRead(const CFX_RetainPtr<IFGAS_Stream>& pStream); |
| 284 ~CFGAS_FileRead() override; | 281 ~CFGAS_FileRead() override; |
| 285 | 282 |
| 286 // IFX_SeekableReadStream | 283 // IFX_SeekableReadStream |
| 287 FX_FILESIZE GetSize() override; | 284 FX_FILESIZE GetSize() override; |
| 288 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; | 285 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; |
| 289 | 286 |
| 290 protected: | 287 protected: |
| 291 CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream); | 288 CFX_RetainPtr<IFGAS_Stream> m_pStream; |
| 292 | |
| 293 bool m_bReleaseStream; | |
| 294 IFGAS_Stream* m_pStream; | |
| 295 }; | 289 }; |
| 296 | 290 |
| 297 int32_t FileLength(FXSYS_FILE* file) { | 291 int32_t FileLength(FXSYS_FILE* file) { |
| 298 ASSERT(file); | 292 ASSERT(file); |
| 299 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ | 293 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_ |
| 300 return _filelength(_fileno(file)); | 294 return _filelength(_fileno(file)); |
| 301 #else | 295 #else |
| 302 int32_t iPos = FXSYS_ftell(file); | 296 int32_t iPos = FXSYS_ftell(file); |
| 303 FXSYS_fseek(file, 0, FXSYS_SEEK_END); | 297 FXSYS_fseek(file, 0, FXSYS_SEEK_END); |
| 304 int32_t iLen = FXSYS_ftell(file); | 298 int32_t iLen = FXSYS_ftell(file); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 319 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); | 313 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); |
| 320 return bRet; | 314 return bRet; |
| 321 #else | 315 #else |
| 322 return false; | 316 return false; |
| 323 #endif | 317 #endif |
| 324 } | 318 } |
| 325 | 319 |
| 326 } // namespace | 320 } // namespace |
| 327 | 321 |
| 328 // static | 322 // static |
| 329 IFGAS_Stream* IFGAS_Stream::CreateStream( | 323 CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateStream( |
| 330 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, | 324 const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, |
| 331 uint32_t dwAccess) { | 325 uint32_t dwAccess) { |
| 332 CFGAS_Stream* pSR = new CFGAS_Stream; | 326 auto pSR = pdfium::MakeRetain<CFGAS_Stream>(); |
| 333 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { | 327 if (!pSR->LoadFileRead(pFileRead, dwAccess)) |
| 334 pSR->Release(); | |
| 335 return nullptr; | 328 return nullptr; |
| 336 } | 329 |
| 337 if (dwAccess & FX_STREAMACCESS_Text) | 330 if (dwAccess & FX_STREAMACCESS_Text) |
| 338 return new CFGAS_TextStream(pSR, true); | 331 return pdfium::MakeRetain<CFGAS_TextStream>(pSR); |
| 339 | 332 |
| 340 return pSR; | 333 return pSR; |
| 341 } | 334 } |
| 342 | 335 |
| 343 // static | 336 // static |
| 344 IFGAS_Stream* IFGAS_Stream::CreateStream( | 337 CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateStream( |
| 345 const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, | 338 const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, |
| 346 uint32_t dwAccess) { | 339 uint32_t dwAccess) { |
| 347 CFGAS_Stream* pSR = new CFGAS_Stream; | 340 auto pSR = pdfium::MakeRetain<CFGAS_Stream>(); |
| 348 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { | 341 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) |
| 349 pSR->Release(); | |
| 350 return nullptr; | 342 return nullptr; |
| 351 } | 343 |
| 352 if (dwAccess & FX_STREAMACCESS_Text) | 344 if (dwAccess & FX_STREAMACCESS_Text) |
| 353 return new CFGAS_TextStream(pSR, true); | 345 return pdfium::MakeRetain<CFGAS_TextStream>(pSR); |
| 354 | 346 |
| 355 return pSR; | 347 return pSR; |
| 356 } | 348 } |
| 357 | 349 |
| 358 // static | 350 // static |
| 359 IFGAS_Stream* IFGAS_Stream::CreateStream(uint8_t* pData, | 351 CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateStream(uint8_t* pData, |
| 360 int32_t length, | 352 int32_t length, |
| 361 uint32_t dwAccess) { | 353 uint32_t dwAccess) { |
| 362 CFGAS_Stream* pSR = new CFGAS_Stream; | 354 auto pSR = pdfium::MakeRetain<CFGAS_Stream>(); |
| 363 if (!pSR->LoadBuffer(pData, length, dwAccess)) { | 355 if (!pSR->LoadBuffer(pData, length, dwAccess)) |
| 364 pSR->Release(); | |
| 365 return nullptr; | 356 return nullptr; |
| 366 } | 357 |
| 367 if (dwAccess & FX_STREAMACCESS_Text) | 358 if (dwAccess & FX_STREAMACCESS_Text) |
| 368 return new CFGAS_TextStream(pSR, true); | 359 return pdfium::MakeRetain<CFGAS_TextStream>(pSR); |
| 369 | 360 |
| 370 return pSR; | 361 return pSR; |
| 371 } | 362 } |
| 372 | 363 |
| 373 IFGAS_StreamImp::IFGAS_StreamImp() : m_dwAccess(0) {} | 364 IFGAS_StreamImp::IFGAS_StreamImp() : m_dwAccess(0) {} |
| 374 | 365 |
| 375 CFGAS_FileStreamImp::CFGAS_FileStreamImp() : m_hFile(nullptr), m_iLength(0) {} | 366 CFGAS_FileStreamImp::CFGAS_FileStreamImp() : m_hFile(nullptr), m_iLength(0) {} |
| 376 | 367 |
| 377 CFGAS_FileStreamImp::~CFGAS_FileStreamImp() { | 368 CFGAS_FileStreamImp::~CFGAS_FileStreamImp() { |
| 378 if (m_hFile) | 369 if (m_hFile) |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 } | 889 } |
| 899 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2); | 890 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2); |
| 900 m_iPosition += iLen * 2; | 891 m_iPosition += iLen * 2; |
| 901 if (m_iPosition > m_iLength) { | 892 if (m_iPosition > m_iLength) { |
| 902 m_iLength = m_iPosition; | 893 m_iLength = m_iPosition; |
| 903 } | 894 } |
| 904 return iLen; | 895 return iLen; |
| 905 } | 896 } |
| 906 | 897 |
| 907 // static | 898 // static |
| 908 IFGAS_Stream* IFGAS_Stream::CreateTextStream(IFGAS_Stream* pBaseStream, | 899 CFX_RetainPtr<IFGAS_Stream> IFGAS_Stream::CreateTextStream( |
| 909 bool bDeleteOnRelease) { | 900 const CFX_RetainPtr<IFGAS_Stream>& pBaseStream) { |
| 910 ASSERT(pBaseStream); | 901 ASSERT(pBaseStream); |
| 911 return new CFGAS_TextStream(pBaseStream, bDeleteOnRelease); | 902 return pdfium::MakeRetain<CFGAS_TextStream>(pBaseStream); |
| 912 } | 903 } |
| 913 | 904 |
| 914 CFGAS_TextStream::CFGAS_TextStream(IFGAS_Stream* pStream, bool bDelStream) | 905 CFGAS_TextStream::CFGAS_TextStream(const CFX_RetainPtr<IFGAS_Stream>& pStream) |
| 915 : m_wCodePage(FX_CODEPAGE_DefANSI), | 906 : m_wCodePage(FX_CODEPAGE_DefANSI), |
| 916 m_wBOMLength(0), | 907 m_wBOMLength(0), |
| 917 m_dwBOM(0), | 908 m_dwBOM(0), |
| 918 m_pBuf(nullptr), | 909 m_pBuf(nullptr), |
| 919 m_iBufSize(0), | 910 m_iBufSize(0), |
| 920 m_bDelStream(bDelStream), | 911 m_pStreamImp(pStream) { |
| 921 m_pStreamImp(pStream), | |
| 922 m_iRefCount(1) { | |
| 923 ASSERT(m_pStreamImp); | 912 ASSERT(m_pStreamImp); |
| 924 m_pStreamImp->Retain(); | |
| 925 InitStream(); | 913 InitStream(); |
| 926 } | 914 } |
| 927 | 915 |
| 928 CFGAS_TextStream::~CFGAS_TextStream() { | 916 CFGAS_TextStream::~CFGAS_TextStream() { |
| 929 m_pStreamImp->Release(); | |
| 930 if (m_bDelStream) | |
| 931 m_pStreamImp->Release(); | |
| 932 if (m_pBuf) | 917 if (m_pBuf) |
| 933 FX_Free(m_pBuf); | 918 FX_Free(m_pBuf); |
| 934 } | 919 } |
| 935 | 920 |
| 936 void CFGAS_TextStream::InitStream() { | 921 void CFGAS_TextStream::InitStream() { |
| 937 int32_t iPosition = m_pStreamImp->GetPosition(); | 922 int32_t iPosition = m_pStreamImp->GetPosition(); |
| 938 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0); | 923 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, 0); |
| 939 m_pStreamImp->ReadData((uint8_t*)&m_dwBOM, 3); | 924 m_pStreamImp->ReadData((uint8_t*)&m_dwBOM, 3); |
| 940 #if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ | 925 #if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ |
| 941 m_dwBOM &= 0x00FFFFFF; | 926 m_dwBOM &= 0x00FFFFFF; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 972 } else { | 957 } else { |
| 973 m_wBOMLength = 0; | 958 m_wBOMLength = 0; |
| 974 m_dwBOM = 0; | 959 m_dwBOM = 0; |
| 975 m_wCodePage = FXSYS_GetACP(); | 960 m_wCodePage = FXSYS_GetACP(); |
| 976 } | 961 } |
| 977 } | 962 } |
| 978 #endif | 963 #endif |
| 979 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition)); | 964 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, std::max(m_wBOMLength, iPosition)); |
| 980 } | 965 } |
| 981 | 966 |
| 982 void CFGAS_TextStream::Release() { | |
| 983 if (--m_iRefCount < 1) | |
| 984 delete this; | |
| 985 } | |
| 986 | |
| 987 IFGAS_Stream* CFGAS_TextStream::Retain() { | |
| 988 m_iRefCount++; | |
| 989 return this; | |
| 990 } | |
| 991 | |
| 992 uint32_t CFGAS_TextStream::GetAccessModes() const { | 967 uint32_t CFGAS_TextStream::GetAccessModes() const { |
| 993 return m_pStreamImp->GetAccessModes() | FX_STREAMACCESS_Text; | 968 return m_pStreamImp->GetAccessModes() | FX_STREAMACCESS_Text; |
| 994 } | 969 } |
| 995 | 970 |
| 996 int32_t CFGAS_TextStream::GetLength() const { | 971 int32_t CFGAS_TextStream::GetLength() const { |
| 997 return m_pStreamImp->GetLength(); | 972 return m_pStreamImp->GetLength(); |
| 998 } | 973 } |
| 999 | 974 |
| 1000 int32_t CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { | 975 int32_t CFGAS_TextStream::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { |
| 1001 return m_pStreamImp->Seek(eSeek, iOffset); | 976 return m_pStreamImp->Seek(eSeek, iOffset); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1023 } | 998 } |
| 1024 | 999 |
| 1025 bool CFGAS_TextStream::SetLength(int32_t iLength) { | 1000 bool CFGAS_TextStream::SetLength(int32_t iLength) { |
| 1026 return m_pStreamImp->SetLength(iLength); | 1001 return m_pStreamImp->SetLength(iLength); |
| 1027 } | 1002 } |
| 1028 | 1003 |
| 1029 uint16_t CFGAS_TextStream::GetCodePage() const { | 1004 uint16_t CFGAS_TextStream::GetCodePage() const { |
| 1030 return m_wCodePage; | 1005 return m_wCodePage; |
| 1031 } | 1006 } |
| 1032 | 1007 |
| 1033 IFGAS_Stream* CFGAS_TextStream::CreateSharedStream(uint32_t dwAccess, | 1008 CFX_RetainPtr<IFGAS_Stream> CFGAS_TextStream::CreateSharedStream( |
| 1034 int32_t iOffset, | 1009 uint32_t dwAccess, |
| 1035 int32_t iLength) { | 1010 int32_t iOffset, |
| 1036 IFGAS_Stream* pSR = | 1011 int32_t iLength) { |
| 1012 CFX_RetainPtr<IFGAS_Stream> pSR = |
| 1037 m_pStreamImp->CreateSharedStream(dwAccess, iOffset, iLength); | 1013 m_pStreamImp->CreateSharedStream(dwAccess, iOffset, iLength); |
| 1038 if (!pSR) | 1014 if (!pSR) |
| 1039 return nullptr; | 1015 return nullptr; |
| 1040 | 1016 |
| 1041 if (dwAccess & FX_STREAMACCESS_Text) | 1017 if (dwAccess & FX_STREAMACCESS_Text) |
| 1042 return new CFGAS_TextStream(pSR, true); | 1018 return pdfium::MakeRetain<CFGAS_TextStream>(pSR); |
| 1043 | 1019 |
| 1044 return pSR; | 1020 return pSR; |
| 1045 } | 1021 } |
| 1046 | 1022 |
| 1047 int32_t CFGAS_TextStream::GetBOM(uint8_t bom[4]) const { | 1023 int32_t CFGAS_TextStream::GetBOM(uint8_t bom[4]) const { |
| 1048 if (m_wBOMLength < 1) | 1024 if (m_wBOMLength < 1) |
| 1049 return 0; | 1025 return 0; |
| 1050 | 1026 |
| 1051 *(uint32_t*)bom = m_dwBOM; | 1027 *(uint32_t*)bom = m_dwBOM; |
| 1052 return m_wBOMLength; | 1028 return m_wBOMLength; |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1239 if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess)) | 1215 if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess)) |
| 1240 return false; | 1216 return false; |
| 1241 | 1217 |
| 1242 m_pStreamImp = pImp.release(); | 1218 m_pStreamImp = pImp.release(); |
| 1243 m_eStreamType = FX_STREAMTYPE_BufferRead; | 1219 m_eStreamType = FX_STREAMTYPE_BufferRead; |
| 1244 m_dwAccess = dwAccess; | 1220 m_dwAccess = dwAccess; |
| 1245 m_iLength = m_pStreamImp->GetLength(); | 1221 m_iLength = m_pStreamImp->GetLength(); |
| 1246 return true; | 1222 return true; |
| 1247 } | 1223 } |
| 1248 | 1224 |
| 1249 void CFGAS_Stream::Release() { | |
| 1250 if (--m_iRefCount < 1) { | |
| 1251 delete this; | |
| 1252 } | |
| 1253 } | |
| 1254 IFGAS_Stream* CFGAS_Stream::Retain() { | |
| 1255 m_iRefCount++; | |
| 1256 return this; | |
| 1257 } | |
| 1258 | |
| 1259 uint32_t CFGAS_Stream::GetAccessModes() const { | 1225 uint32_t CFGAS_Stream::GetAccessModes() const { |
| 1260 return m_dwAccess; | 1226 return m_dwAccess; |
| 1261 } | 1227 } |
| 1262 | 1228 |
| 1263 int32_t CFGAS_Stream::GetLength() const { | 1229 int32_t CFGAS_Stream::GetLength() const { |
| 1264 if (!m_pStreamImp) { | 1230 if (!m_pStreamImp) { |
| 1265 return -1; | 1231 return -1; |
| 1266 } | 1232 } |
| 1267 if (m_eStreamType == FX_STREAMTYPE_File || | 1233 if (m_eStreamType == FX_STREAMTYPE_File || |
| 1268 m_eStreamType == FX_STREAMTYPE_Buffer) { | 1234 m_eStreamType == FX_STREAMTYPE_Buffer) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1438 return FX_CODEPAGE_UTF16BE; | 1404 return FX_CODEPAGE_UTF16BE; |
| 1439 #endif | 1405 #endif |
| 1440 } | 1406 } |
| 1441 uint16_t CFGAS_Stream::SetCodePage(uint16_t wCodePage) { | 1407 uint16_t CFGAS_Stream::SetCodePage(uint16_t wCodePage) { |
| 1442 #if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ | 1408 #if _FX_ENDIAN_ == _FX_LITTLE_ENDIAN_ |
| 1443 return FX_CODEPAGE_UTF16LE; | 1409 return FX_CODEPAGE_UTF16LE; |
| 1444 #else | 1410 #else |
| 1445 return FX_CODEPAGE_UTF16BE; | 1411 return FX_CODEPAGE_UTF16BE; |
| 1446 #endif | 1412 #endif |
| 1447 } | 1413 } |
| 1448 IFGAS_Stream* CFGAS_Stream::CreateSharedStream(uint32_t dwAccess, | 1414 |
| 1449 int32_t iOffset, | 1415 CFX_RetainPtr<IFGAS_Stream> CFGAS_Stream::CreateSharedStream(uint32_t dwAccess, |
| 1450 int32_t iLength) { | 1416 int32_t iOffset, |
| 1417 int32_t iLength) { |
| 1451 ASSERT(iLength > 0); | 1418 ASSERT(iLength > 0); |
| 1452 if (!m_pStreamImp) | 1419 if (!m_pStreamImp) |
| 1453 return nullptr; | 1420 return nullptr; |
| 1454 | 1421 |
| 1455 if ((m_dwAccess & FX_STREAMACCESS_Text) != 0 && | 1422 if ((m_dwAccess & FX_STREAMACCESS_Text) != 0 && |
| 1456 (dwAccess & FX_STREAMACCESS_Text) == 0) { | 1423 (dwAccess & FX_STREAMACCESS_Text) == 0) { |
| 1457 return nullptr; | 1424 return nullptr; |
| 1458 } | 1425 } |
| 1459 if ((m_dwAccess & FX_STREAMACCESS_Write) == 0 && | 1426 if ((m_dwAccess & FX_STREAMACCESS_Write) == 0 && |
| 1460 (dwAccess & FX_STREAMACCESS_Write) != 0) { | 1427 (dwAccess & FX_STREAMACCESS_Write) != 0) { |
| 1461 return nullptr; | 1428 return nullptr; |
| 1462 } | 1429 } |
| 1463 int32_t iStart = m_iStart + iOffset; | 1430 int32_t iStart = m_iStart + iOffset; |
| 1464 int32_t iTotal = m_iStart + m_iLength; | 1431 int32_t iTotal = m_iStart + m_iLength; |
| 1465 if (iStart < m_iStart || iStart >= iTotal) | 1432 if (iStart < m_iStart || iStart >= iTotal) |
| 1466 return nullptr; | 1433 return nullptr; |
| 1467 | 1434 |
| 1468 int32_t iEnd = iStart + iLength; | 1435 int32_t iEnd = iStart + iLength; |
| 1469 if (iEnd < iStart || iEnd > iTotal) | 1436 if (iEnd < iStart || iEnd > iTotal) |
| 1470 return nullptr; | 1437 return nullptr; |
| 1471 | 1438 |
| 1472 CFGAS_Stream* pShared = new CFGAS_Stream; | 1439 auto pShared = pdfium::MakeRetain<CFGAS_Stream>(); |
| 1473 pShared->m_eStreamType = FX_STREAMTYPE_Stream; | 1440 pShared->m_eStreamType = FX_STREAMTYPE_Stream; |
| 1474 pShared->m_pStreamImp = m_pStreamImp; | 1441 pShared->m_pStreamImp = m_pStreamImp; |
| 1475 pShared->m_dwAccess = dwAccess; | 1442 pShared->m_dwAccess = dwAccess; |
| 1476 pShared->m_iTotalSize = iLength; | 1443 pShared->m_iTotalSize = iLength; |
| 1477 pShared->m_iPosition = iStart; | 1444 pShared->m_iPosition = iStart; |
| 1478 pShared->m_iStart = iStart; | 1445 pShared->m_iStart = iStart; |
| 1479 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; | 1446 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; |
| 1480 if (dwAccess & FX_STREAMACCESS_Text) | 1447 if (dwAccess & FX_STREAMACCESS_Text) |
| 1481 return IFGAS_Stream::CreateTextStream(pShared, true); | 1448 return IFGAS_Stream::CreateTextStream(pShared); |
| 1482 | 1449 |
| 1483 return pShared; | 1450 return pShared; |
| 1484 } | 1451 } |
| 1485 | 1452 |
| 1486 CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() { | 1453 CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() { |
| 1487 return CFGAS_FileRead::Create(this, false); | 1454 return CFGAS_FileRead::Create(CFX_RetainPtr<IFGAS_Stream>(this)); |
| 1488 } | 1455 } |
| 1489 | 1456 |
| 1490 CFX_RetainPtr<CFGAS_FileRead> CFGAS_FileRead::Create(IFGAS_Stream* pStream, | 1457 CFX_RetainPtr<CFGAS_FileRead> CFGAS_FileRead::Create( |
| 1491 bool bReleaseStream) { | 1458 const CFX_RetainPtr<IFGAS_Stream>& pStream) { |
| 1492 return CFX_RetainPtr<CFGAS_FileRead>( | 1459 return pdfium::MakeRetain<CFGAS_FileRead>(pStream); |
| 1493 new CFGAS_FileRead(pStream, bReleaseStream)); | |
| 1494 } | 1460 } |
| 1495 | 1461 |
| 1496 CFGAS_FileRead::CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream) | 1462 CFGAS_FileRead::CFGAS_FileRead(const CFX_RetainPtr<IFGAS_Stream>& pStream) |
| 1497 : m_bReleaseStream(bReleaseStream), m_pStream(pStream) { | 1463 : m_pStream(pStream) { |
| 1498 ASSERT(m_pStream); | 1464 ASSERT(m_pStream); |
| 1499 } | 1465 } |
| 1500 | 1466 |
| 1501 CFGAS_FileRead::~CFGAS_FileRead() { | 1467 CFGAS_FileRead::~CFGAS_FileRead() {} |
| 1502 if (m_bReleaseStream) { | 1468 |
| 1503 m_pStream->Release(); | |
| 1504 } | |
| 1505 } | |
| 1506 FX_FILESIZE CFGAS_FileRead::GetSize() { | 1469 FX_FILESIZE CFGAS_FileRead::GetSize() { |
| 1507 return (FX_FILESIZE)m_pStream->GetLength(); | 1470 return (FX_FILESIZE)m_pStream->GetLength(); |
| 1508 } | 1471 } |
| 1509 | 1472 |
| 1510 bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { | 1473 bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { |
| 1511 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); | 1474 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); |
| 1512 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); | 1475 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); |
| 1513 return iLen == (int32_t)size; | 1476 return iLen == (int32_t)size; |
| 1514 } | 1477 } |
| OLD | NEW |