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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 void Release() override; | 220 void Release() override; |
221 IFX_Stream* Retain() override; | 221 IFX_Stream* Retain() override; |
222 uint32_t GetAccessModes() const override; | 222 uint32_t GetAccessModes() const override; |
223 int32_t GetLength() const override; | 223 int32_t GetLength() const override; |
224 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 224 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
225 int32_t GetPosition() override; | 225 int32_t GetPosition() override; |
226 FX_BOOL IsEOF() const override; | 226 FX_BOOL IsEOF() const override; |
227 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 227 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
228 int32_t ReadString(FX_WCHAR* pStr, | 228 int32_t ReadString(FX_WCHAR* pStr, |
229 int32_t iMaxLength, | 229 int32_t iMaxLength, |
230 FX_BOOL& bEOS, | 230 FX_BOOL& bEOS) override; |
231 int32_t const* pByteSize = nullptr) override; | |
232 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; | 231 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; |
233 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; | 232 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; |
234 void Flush() override; | 233 void Flush() override; |
235 FX_BOOL SetLength(int32_t iLength) override; | 234 FX_BOOL SetLength(int32_t iLength) override; |
236 int32_t GetBOM(uint8_t bom[4]) const override; | 235 int32_t GetBOM(uint8_t bom[4]) const override; |
237 uint16_t GetCodePage() const override; | 236 uint16_t GetCodePage() const override; |
238 uint16_t SetCodePage(uint16_t wCodePage) override; | 237 uint16_t SetCodePage(uint16_t wCodePage) override; |
239 IFX_Stream* CreateSharedStream(uint32_t dwAccess, | 238 IFX_Stream* CreateSharedStream(uint32_t dwAccess, |
240 int32_t iOffset, | 239 int32_t iOffset, |
241 int32_t iLength) override; | 240 int32_t iLength) override; |
(...skipping 18 matching lines...) Expand all Loading... |
260 void Release() override; | 259 void Release() override; |
261 IFX_Stream* Retain() override; | 260 IFX_Stream* Retain() override; |
262 uint32_t GetAccessModes() const override; | 261 uint32_t GetAccessModes() const override; |
263 int32_t GetLength() const override; | 262 int32_t GetLength() const override; |
264 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; | 263 int32_t Seek(FX_STREAMSEEK eSeek, int32_t iOffset) override; |
265 int32_t GetPosition() override; | 264 int32_t GetPosition() override; |
266 FX_BOOL IsEOF() const override; | 265 FX_BOOL IsEOF() const override; |
267 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; | 266 int32_t ReadData(uint8_t* pBuffer, int32_t iBufferSize) override; |
268 int32_t ReadString(FX_WCHAR* pStr, | 267 int32_t ReadString(FX_WCHAR* pStr, |
269 int32_t iMaxLength, | 268 int32_t iMaxLength, |
270 FX_BOOL& bEOS, | 269 FX_BOOL& bEOS) override; |
271 int32_t const* pByteSize = nullptr) override; | |
272 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; | 270 int32_t WriteData(const uint8_t* pBuffer, int32_t iBufferSize) override; |
273 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; | 271 int32_t WriteString(const FX_WCHAR* pStr, int32_t iLength) override; |
274 void Flush() override; | 272 void Flush() override; |
275 FX_BOOL SetLength(int32_t iLength) override; | 273 FX_BOOL SetLength(int32_t iLength) override; |
276 int32_t GetBOM(uint8_t bom[4]) const override; | 274 int32_t GetBOM(uint8_t bom[4]) const override; |
277 uint16_t GetCodePage() const override; | 275 uint16_t GetCodePage() const override; |
278 uint16_t SetCodePage(uint16_t wCodePage) override; | 276 uint16_t SetCodePage(uint16_t wCodePage) override; |
279 IFX_Stream* CreateSharedStream(uint32_t dwAccess, | 277 IFX_Stream* CreateSharedStream(uint32_t dwAccess, |
280 int32_t iOffset, | 278 int32_t iOffset, |
281 int32_t iLength) override; | 279 int32_t iLength) override; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 FX_BOOL bRet = ::SetEndOfFile(hFile); | 329 FX_BOOL bRet = ::SetEndOfFile(hFile); |
332 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); | 330 ::SetFilePointer(hFile, (int32_t)dwPos, 0, FILE_BEGIN); |
333 return bRet; | 331 return bRet; |
334 #else | 332 #else |
335 return FALSE; | 333 return FALSE; |
336 #endif | 334 #endif |
337 } | 335 } |
338 | 336 |
339 } // namespace | 337 } // namespace |
340 | 338 |
341 IFX_Stream* IFX_Stream::CreateStream(IFX_BufferRead* pBufferRead, | 339 // static |
342 uint32_t dwAccess, | |
343 int32_t iFileSize, | |
344 FX_BOOL bReleaseBufferRead) { | |
345 CFX_Stream* pSR = new CFX_Stream; | |
346 if (!pSR->LoadBufferRead(pBufferRead, iFileSize, dwAccess, | |
347 bReleaseBufferRead)) { | |
348 pSR->Release(); | |
349 return nullptr; | |
350 } | |
351 if (dwAccess & FX_STREAMACCESS_Text) { | |
352 return new CFX_TextStream(pSR, TRUE); | |
353 } | |
354 return pSR; | |
355 } | |
356 IFX_Stream* IFX_Stream::CreateStream(IFX_FileRead* pFileRead, | 340 IFX_Stream* IFX_Stream::CreateStream(IFX_FileRead* pFileRead, |
357 uint32_t dwAccess) { | 341 uint32_t dwAccess) { |
358 CFX_Stream* pSR = new CFX_Stream; | 342 CFX_Stream* pSR = new CFX_Stream; |
359 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { | 343 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { |
360 pSR->Release(); | 344 pSR->Release(); |
361 return nullptr; | 345 return nullptr; |
362 } | 346 } |
363 if (dwAccess & FX_STREAMACCESS_Text) { | 347 if (dwAccess & FX_STREAMACCESS_Text) { |
364 return new CFX_TextStream(pSR, TRUE); | 348 return new CFX_TextStream(pSR, TRUE); |
365 } | 349 } |
366 return pSR; | 350 return pSR; |
367 } | 351 } |
| 352 |
| 353 // static |
368 IFX_Stream* IFX_Stream::CreateStream(IFX_FileWrite* pFileWrite, | 354 IFX_Stream* IFX_Stream::CreateStream(IFX_FileWrite* pFileWrite, |
369 uint32_t dwAccess) { | 355 uint32_t dwAccess) { |
370 CFX_Stream* pSR = new CFX_Stream; | 356 CFX_Stream* pSR = new CFX_Stream; |
371 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { | 357 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { |
372 pSR->Release(); | 358 pSR->Release(); |
373 return nullptr; | 359 return nullptr; |
374 } | 360 } |
375 if (dwAccess & FX_STREAMACCESS_Text) { | 361 if (dwAccess & FX_STREAMACCESS_Text) { |
376 return new CFX_TextStream(pSR, TRUE); | 362 return new CFX_TextStream(pSR, TRUE); |
377 } | 363 } |
378 return pSR; | 364 return pSR; |
379 } | 365 } |
380 IFX_Stream* IFX_Stream::CreateStream(const FX_WCHAR* pszFileName, | 366 |
381 uint32_t dwAccess) { | 367 // static |
382 CFX_Stream* pSR = new CFX_Stream; | |
383 if (!pSR->LoadFile(pszFileName, dwAccess)) { | |
384 pSR->Release(); | |
385 return nullptr; | |
386 } | |
387 if (dwAccess & FX_STREAMACCESS_Text) { | |
388 return new CFX_TextStream(pSR, TRUE); | |
389 } | |
390 return pSR; | |
391 } | |
392 IFX_Stream* IFX_Stream::CreateStream(uint8_t* pData, | 368 IFX_Stream* IFX_Stream::CreateStream(uint8_t* pData, |
393 int32_t length, | 369 int32_t length, |
394 uint32_t dwAccess) { | 370 uint32_t dwAccess) { |
395 CFX_Stream* pSR = new CFX_Stream; | 371 CFX_Stream* pSR = new CFX_Stream; |
396 if (!pSR->LoadBuffer(pData, length, dwAccess)) { | 372 if (!pSR->LoadBuffer(pData, length, dwAccess)) { |
397 pSR->Release(); | 373 pSR->Release(); |
398 return nullptr; | 374 return nullptr; |
399 } | 375 } |
400 if (dwAccess & FX_STREAMACCESS_Text) { | 376 if (dwAccess & FX_STREAMACCESS_Text) { |
401 return new CFX_TextStream(pSR, TRUE); | 377 return new CFX_TextStream(pSR, TRUE); |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 if (iLen <= 0) { | 901 if (iLen <= 0) { |
926 return 0; | 902 return 0; |
927 } | 903 } |
928 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2); | 904 FXSYS_memcpy(m_pData + m_iPosition, pStr, iLen * 2); |
929 m_iPosition += iLen * 2; | 905 m_iPosition += iLen * 2; |
930 if (m_iPosition > m_iLength) { | 906 if (m_iPosition > m_iLength) { |
931 m_iLength = m_iPosition; | 907 m_iLength = m_iPosition; |
932 } | 908 } |
933 return iLen; | 909 return iLen; |
934 } | 910 } |
| 911 |
| 912 // static |
935 IFX_Stream* IFX_Stream::CreateTextStream(IFX_Stream* pBaseStream, | 913 IFX_Stream* IFX_Stream::CreateTextStream(IFX_Stream* pBaseStream, |
936 FX_BOOL bDeleteOnRelease) { | 914 FX_BOOL bDeleteOnRelease) { |
937 ASSERT(pBaseStream); | 915 ASSERT(pBaseStream); |
938 return new CFX_TextStream(pBaseStream, bDeleteOnRelease); | 916 return new CFX_TextStream(pBaseStream, bDeleteOnRelease); |
939 } | 917 } |
| 918 |
940 CFX_TextStream::CFX_TextStream(IFX_Stream* pStream, FX_BOOL bDelStream) | 919 CFX_TextStream::CFX_TextStream(IFX_Stream* pStream, FX_BOOL bDelStream) |
941 : m_wCodePage(FX_CODEPAGE_DefANSI), | 920 : m_wCodePage(FX_CODEPAGE_DefANSI), |
942 m_wBOMLength(0), | 921 m_wBOMLength(0), |
943 m_dwBOM(0), | 922 m_dwBOM(0), |
944 m_pBuf(nullptr), | 923 m_pBuf(nullptr), |
945 m_iBufSize(0), | 924 m_iBufSize(0), |
946 m_bDelStream(bDelStream), | 925 m_bDelStream(bDelStream), |
947 m_pStreamImp(pStream), | 926 m_pStreamImp(pStream), |
948 m_iRefCount(1) { | 927 m_iRefCount(1) { |
949 ASSERT(m_pStreamImp); | 928 ASSERT(m_pStreamImp); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1066 uint16_t CFX_TextStream::SetCodePage(uint16_t wCodePage) { | 1045 uint16_t CFX_TextStream::SetCodePage(uint16_t wCodePage) { |
1067 if (m_wBOMLength > 0) { | 1046 if (m_wBOMLength > 0) { |
1068 return m_wCodePage; | 1047 return m_wCodePage; |
1069 } | 1048 } |
1070 uint16_t v = m_wCodePage; | 1049 uint16_t v = m_wCodePage; |
1071 m_wCodePage = wCodePage; | 1050 m_wCodePage = wCodePage; |
1072 return v; | 1051 return v; |
1073 } | 1052 } |
1074 int32_t CFX_TextStream::ReadString(FX_WCHAR* pStr, | 1053 int32_t CFX_TextStream::ReadString(FX_WCHAR* pStr, |
1075 int32_t iMaxLength, | 1054 int32_t iMaxLength, |
1076 FX_BOOL& bEOS, | 1055 FX_BOOL& bEOS) { |
1077 int32_t const* pByteSize) { | |
1078 ASSERT(pStr && iMaxLength > 0); | 1056 ASSERT(pStr && iMaxLength > 0); |
1079 if (!m_pStreamImp) { | 1057 if (!m_pStreamImp) { |
1080 return -1; | 1058 return -1; |
1081 } | 1059 } |
1082 int32_t iLen; | 1060 int32_t iLen; |
1083 if (m_wCodePage == FX_CODEPAGE_UTF16LE || | 1061 if (m_wCodePage == FX_CODEPAGE_UTF16LE || |
1084 m_wCodePage == FX_CODEPAGE_UTF16BE) { | 1062 m_wCodePage == FX_CODEPAGE_UTF16BE) { |
1085 int32_t iBytes = pByteSize ? *pByteSize : iMaxLength * 2; | 1063 int32_t iBytes = iMaxLength * 2; |
1086 iLen = m_pStreamImp->ReadData((uint8_t*)pStr, iBytes); | 1064 iLen = m_pStreamImp->ReadData((uint8_t*)pStr, iBytes); |
1087 iMaxLength = iLen / 2; | 1065 iMaxLength = iLen / 2; |
1088 if (sizeof(FX_WCHAR) > 2) { | 1066 if (sizeof(FX_WCHAR) > 2) { |
1089 FX_UTF16ToWChar(pStr, iMaxLength); | 1067 FX_UTF16ToWChar(pStr, iMaxLength); |
1090 } | 1068 } |
1091 #if _FX_ENDIAN_ == _FX_BIG_ENDIAN_ | 1069 #if _FX_ENDIAN_ == _FX_BIG_ENDIAN_ |
1092 if (m_wCodePage == FX_CODEPAGE_UTF16LE) { | 1070 if (m_wCodePage == FX_CODEPAGE_UTF16LE) { |
1093 FX_SwapByteOrder(pStr, iMaxLength); | 1071 FX_SwapByteOrder(pStr, iMaxLength); |
1094 } | 1072 } |
1095 #else | 1073 #else |
1096 if (m_wCodePage == FX_CODEPAGE_UTF16BE) { | 1074 if (m_wCodePage == FX_CODEPAGE_UTF16BE) { |
1097 FX_SwapByteOrder(pStr, iMaxLength); | 1075 FX_SwapByteOrder(pStr, iMaxLength); |
1098 } | 1076 } |
1099 #endif | 1077 #endif |
1100 } else { | 1078 } else { |
1101 int32_t pos = m_pStreamImp->GetPosition(); | 1079 int32_t pos = m_pStreamImp->GetPosition(); |
1102 int32_t iBytes = pByteSize ? *pByteSize : iMaxLength; | 1080 int32_t iBytes = iMaxLength; |
1103 iBytes = std::min(iBytes, m_pStreamImp->GetLength() - pos); | 1081 iBytes = std::min(iBytes, m_pStreamImp->GetLength() - pos); |
1104 if (iBytes > 0) { | 1082 if (iBytes > 0) { |
1105 if (!m_pBuf) { | 1083 if (!m_pBuf) { |
1106 m_pBuf = FX_Alloc(uint8_t, iBytes); | 1084 m_pBuf = FX_Alloc(uint8_t, iBytes); |
1107 m_iBufSize = iBytes; | 1085 m_iBufSize = iBytes; |
1108 } else if (iBytes > m_iBufSize) { | 1086 } else if (iBytes > m_iBufSize) { |
1109 m_pBuf = FX_Realloc(uint8_t, m_pBuf, iBytes); | 1087 m_pBuf = FX_Realloc(uint8_t, m_pBuf, iBytes); |
1110 m_iBufSize = iBytes; | 1088 m_iBufSize = iBytes; |
1111 } | 1089 } |
1112 iLen = m_pStreamImp->ReadData(m_pBuf, iBytes); | 1090 iLen = m_pStreamImp->ReadData(m_pBuf, iBytes); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1334 } | 1312 } |
1335 if (m_pStreamImp->GetPosition() != m_iPosition) { | 1313 if (m_pStreamImp->GetPosition() != m_iPosition) { |
1336 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition); | 1314 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition); |
1337 } | 1315 } |
1338 iLen = m_pStreamImp->ReadData(pBuffer, iLen); | 1316 iLen = m_pStreamImp->ReadData(pBuffer, iLen); |
1339 m_iPosition = m_pStreamImp->GetPosition(); | 1317 m_iPosition = m_pStreamImp->GetPosition(); |
1340 return iLen; | 1318 return iLen; |
1341 } | 1319 } |
1342 int32_t CFX_Stream::ReadString(FX_WCHAR* pStr, | 1320 int32_t CFX_Stream::ReadString(FX_WCHAR* pStr, |
1343 int32_t iMaxLength, | 1321 int32_t iMaxLength, |
1344 FX_BOOL& bEOS, | 1322 FX_BOOL& bEOS) { |
1345 int32_t const* pByteSize) { | |
1346 ASSERT(pStr && iMaxLength > 0); | 1323 ASSERT(pStr && iMaxLength > 0); |
1347 if (!m_pStreamImp) { | 1324 if (!m_pStreamImp) { |
1348 return -1; | 1325 return -1; |
1349 } | 1326 } |
1350 int32_t iEnd = m_iStart + m_iLength; | 1327 int32_t iEnd = m_iStart + m_iLength; |
1351 int32_t iLen = iEnd - m_iPosition; | 1328 int32_t iLen = iEnd - m_iPosition; |
1352 if (pByteSize) { | |
1353 iLen = std::min(iLen, *pByteSize); | |
1354 } | |
1355 iLen = std::min(iEnd / 2, iMaxLength); | 1329 iLen = std::min(iEnd / 2, iMaxLength); |
1356 if (iLen <= 0) { | 1330 if (iLen <= 0) { |
1357 return 0; | 1331 return 0; |
1358 } | 1332 } |
1359 if (m_pStreamImp->GetPosition() != m_iPosition) { | 1333 if (m_pStreamImp->GetPosition() != m_iPosition) { |
1360 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition); | 1334 m_pStreamImp->Seek(FX_STREAMSEEK_Begin, m_iPosition); |
1361 } | 1335 } |
1362 iLen = m_pStreamImp->ReadString(pStr, iLen, bEOS); | 1336 iLen = m_pStreamImp->ReadString(pStr, iLen, bEOS); |
1363 m_iPosition = m_pStreamImp->GetPosition(); | 1337 m_iPosition = m_pStreamImp->GetPosition(); |
1364 if (iLen > 0 && m_iPosition >= iEnd) { | 1338 if (iLen > 0 && m_iPosition >= iEnd) { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1515 FX_FILESIZE offset, | 1489 FX_FILESIZE offset, |
1516 size_t size) { | 1490 size_t size) { |
1517 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); | 1491 m_pStream->Seek(FX_STREAMSEEK_Begin, (int32_t)offset); |
1518 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); | 1492 int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); |
1519 return iLen == (int32_t)size; | 1493 return iLen == (int32_t)size; |
1520 } | 1494 } |
1521 | 1495 |
1522 void CFGAS_FileRead::Release() { | 1496 void CFGAS_FileRead::Release() { |
1523 delete this; | 1497 delete this; |
1524 } | 1498 } |
OLD | NEW |