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 "../fgas_base.h" | 7 #include "../fgas_base.h" |
8 #include "fx_stream.h" | 8 #include "fx_stream.h" |
9 IFX_Stream* IFX_Stream::CreateStream(IFX_BufferRead* pBufferRead, | 9 IFX_Stream* IFX_Stream::CreateStream(IFX_BufferRead* pBufferRead, |
10 FX_DWORD dwAccess, | 10 FX_DWORD dwAccess, |
11 int32_t iFileSize, | 11 int32_t iFileSize, |
12 FX_BOOL bReleaseBufferRead) { | 12 FX_BOOL bReleaseBufferRead) { |
13 CFX_Stream* pSR = new CFX_Stream; | 13 CFX_Stream* pSR = new CFX_Stream; |
14 if (!pSR) { | |
15 return NULL; | |
16 } | |
17 if (!pSR->LoadBufferRead(pBufferRead, iFileSize, dwAccess, | 14 if (!pSR->LoadBufferRead(pBufferRead, iFileSize, dwAccess, |
18 bReleaseBufferRead)) { | 15 bReleaseBufferRead)) { |
19 pSR->Release(); | 16 pSR->Release(); |
20 return NULL; | 17 return NULL; |
21 } | 18 } |
22 if (dwAccess & FX_STREAMACCESS_Text) { | 19 if (dwAccess & FX_STREAMACCESS_Text) { |
23 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 20 return new CFX_TextStream(pSR, TRUE); |
24 if (!pTR) { | |
25 pTR->Release(); | |
26 pTR = NULL; | |
27 } | |
28 return pTR; | |
29 } | 21 } |
30 return pSR; | 22 return pSR; |
31 } | 23 } |
32 IFX_Stream* IFX_Stream::CreateStream(IFX_FileRead* pFileRead, | 24 IFX_Stream* IFX_Stream::CreateStream(IFX_FileRead* pFileRead, |
33 FX_DWORD dwAccess) { | 25 FX_DWORD dwAccess) { |
34 CFX_Stream* pSR = new CFX_Stream; | 26 CFX_Stream* pSR = new CFX_Stream; |
35 if (pSR == NULL) { | |
36 return NULL; | |
37 } | |
38 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { | 27 if (!pSR->LoadFileRead(pFileRead, dwAccess)) { |
39 pSR->Release(); | 28 pSR->Release(); |
40 return NULL; | 29 return NULL; |
41 } | 30 } |
42 if (dwAccess & FX_STREAMACCESS_Text) { | 31 if (dwAccess & FX_STREAMACCESS_Text) { |
43 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 32 return new CFX_TextStream(pSR, TRUE); |
44 if (pTR == NULL) { | |
45 pSR->Release(); | |
46 return NULL; | |
47 } | |
48 return pTR; | |
49 } | 33 } |
50 return pSR; | 34 return pSR; |
51 } | 35 } |
52 IFX_Stream* IFX_Stream::CreateStream(IFX_FileWrite* pFileWrite, | 36 IFX_Stream* IFX_Stream::CreateStream(IFX_FileWrite* pFileWrite, |
53 FX_DWORD dwAccess) { | 37 FX_DWORD dwAccess) { |
54 CFX_Stream* pSR = new CFX_Stream; | 38 CFX_Stream* pSR = new CFX_Stream; |
55 if (pSR == NULL) { | |
56 return NULL; | |
57 } | |
58 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { | 39 if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { |
59 pSR->Release(); | 40 pSR->Release(); |
60 return NULL; | 41 return NULL; |
61 } | 42 } |
62 if (dwAccess & FX_STREAMACCESS_Text) { | 43 if (dwAccess & FX_STREAMACCESS_Text) { |
63 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 44 return new CFX_TextStream(pSR, TRUE); |
64 if (pTR == NULL) { | |
65 pSR->Release(); | |
66 return NULL; | |
67 } | |
68 return pTR; | |
69 } | 45 } |
70 return pSR; | 46 return pSR; |
71 } | 47 } |
72 IFX_Stream* IFX_Stream::CreateStream(const FX_WCHAR* pszFileName, | 48 IFX_Stream* IFX_Stream::CreateStream(const FX_WCHAR* pszFileName, |
73 FX_DWORD dwAccess) { | 49 FX_DWORD dwAccess) { |
74 CFX_Stream* pSR = new CFX_Stream; | 50 CFX_Stream* pSR = new CFX_Stream; |
75 if (pSR == NULL) { | |
76 return NULL; | |
77 } | |
78 if (!pSR->LoadFile(pszFileName, dwAccess)) { | 51 if (!pSR->LoadFile(pszFileName, dwAccess)) { |
79 pSR->Release(); | 52 pSR->Release(); |
80 return NULL; | 53 return NULL; |
81 } | 54 } |
82 if (dwAccess & FX_STREAMACCESS_Text) { | 55 if (dwAccess & FX_STREAMACCESS_Text) { |
83 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 56 return new CFX_TextStream(pSR, TRUE); |
84 if (pTR == NULL) { | |
85 pSR->Release(); | |
86 return NULL; | |
87 } | |
88 return pTR; | |
89 } | 57 } |
90 return pSR; | 58 return pSR; |
91 } | 59 } |
92 IFX_Stream* IFX_Stream::CreateStream(uint8_t* pData, | 60 IFX_Stream* IFX_Stream::CreateStream(uint8_t* pData, |
93 int32_t length, | 61 int32_t length, |
94 FX_DWORD dwAccess) { | 62 FX_DWORD dwAccess) { |
95 CFX_Stream* pSR = new CFX_Stream; | 63 CFX_Stream* pSR = new CFX_Stream; |
96 if (pSR == NULL) { | |
97 return NULL; | |
98 } | |
99 if (!pSR->LoadBuffer(pData, length, dwAccess)) { | 64 if (!pSR->LoadBuffer(pData, length, dwAccess)) { |
100 pSR->Release(); | 65 pSR->Release(); |
101 return NULL; | 66 return NULL; |
102 } | 67 } |
103 if (dwAccess & FX_STREAMACCESS_Text) { | 68 if (dwAccess & FX_STREAMACCESS_Text) { |
104 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 69 return new CFX_TextStream(pSR, TRUE); |
105 if (pTR == NULL) { | |
106 pSR->Release(); | |
107 return NULL; | |
108 } | |
109 return pTR; | |
110 } | 70 } |
111 return pSR; | 71 return pSR; |
112 } | 72 } |
113 CFX_StreamImp::CFX_StreamImp() : CFX_ThreadLock(), m_dwAccess(0) {} | 73 CFX_StreamImp::CFX_StreamImp() : CFX_ThreadLock(), m_dwAccess(0) {} |
114 CFX_FileStreamImp::CFX_FileStreamImp() | 74 CFX_FileStreamImp::CFX_FileStreamImp() |
115 : CFX_StreamImp(), m_hFile(NULL), m_iLength(0) {} | 75 : CFX_StreamImp(), m_hFile(NULL), m_iLength(0) {} |
116 CFX_FileStreamImp::~CFX_FileStreamImp() { | 76 CFX_FileStreamImp::~CFX_FileStreamImp() { |
117 if (m_hFile != NULL) { | 77 if (m_hFile != NULL) { |
118 FXSYS_fclose(m_hFile); | 78 FXSYS_fclose(m_hFile); |
119 } | 79 } |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 } | 731 } |
772 IFX_Stream* CFX_TextStream::CreateSharedStream(FX_DWORD dwAccess, | 732 IFX_Stream* CFX_TextStream::CreateSharedStream(FX_DWORD dwAccess, |
773 int32_t iOffset, | 733 int32_t iOffset, |
774 int32_t iLength) { | 734 int32_t iLength) { |
775 IFX_Stream* pSR = | 735 IFX_Stream* pSR = |
776 m_pStreamImp->CreateSharedStream(dwAccess, iOffset, iLength); | 736 m_pStreamImp->CreateSharedStream(dwAccess, iOffset, iLength); |
777 if (pSR == NULL) { | 737 if (pSR == NULL) { |
778 return NULL; | 738 return NULL; |
779 } | 739 } |
780 if (dwAccess & FX_STREAMACCESS_Text) { | 740 if (dwAccess & FX_STREAMACCESS_Text) { |
781 IFX_Stream* pTR = new CFX_TextStream(pSR, TRUE); | 741 return new CFX_TextStream(pSR, TRUE); |
782 if (pTR == NULL) { | |
783 pSR->Release(); | |
784 return NULL; | |
785 } | |
786 return pTR; | |
787 } | 742 } |
788 return pSR; | 743 return pSR; |
789 } | 744 } |
790 int32_t CFX_TextStream::GetBOM(uint8_t bom[4]) const { | 745 int32_t CFX_TextStream::GetBOM(uint8_t bom[4]) const { |
791 if (m_wBOMLength < 1) { | 746 if (m_wBOMLength < 1) { |
792 return 0; | 747 return 0; |
793 } | 748 } |
794 *(FX_DWORD*)bom = m_dwBOM; | 749 *(FX_DWORD*)bom = m_dwBOM; |
795 return (int32_t)m_wBOMLength; | 750 return (int32_t)m_wBOMLength; |
796 } | 751 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 } | 849 } |
895 FX_BOOL CFX_Stream::LoadFile(const FX_WCHAR* pszSrcFileName, | 850 FX_BOOL CFX_Stream::LoadFile(const FX_WCHAR* pszSrcFileName, |
896 FX_DWORD dwAccess) { | 851 FX_DWORD dwAccess) { |
897 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { | 852 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { |
898 return FALSE; | 853 return FALSE; |
899 } | 854 } |
900 if (pszSrcFileName == NULL || FXSYS_wcslen(pszSrcFileName) < 1) { | 855 if (pszSrcFileName == NULL || FXSYS_wcslen(pszSrcFileName) < 1) { |
901 return FALSE; | 856 return FALSE; |
902 } | 857 } |
903 m_pStreamImp = new CFX_FileStreamImp(); | 858 m_pStreamImp = new CFX_FileStreamImp(); |
904 if (m_pStreamImp == NULL) { | |
905 return FALSE; | |
906 } | |
907 FX_BOOL bRet = | 859 FX_BOOL bRet = |
908 ((CFX_FileStreamImp*)m_pStreamImp)->LoadFile(pszSrcFileName, dwAccess); | 860 ((CFX_FileStreamImp*)m_pStreamImp)->LoadFile(pszSrcFileName, dwAccess); |
909 if (!bRet) { | 861 if (!bRet) { |
910 m_pStreamImp->Release(); | 862 m_pStreamImp->Release(); |
911 m_pStreamImp = NULL; | 863 m_pStreamImp = NULL; |
912 } else { | 864 } else { |
913 m_eStreamType = FX_STREAMTYPE_File; | 865 m_eStreamType = FX_STREAMTYPE_File; |
914 m_dwAccess = dwAccess; | 866 m_dwAccess = dwAccess; |
915 m_iLength = m_pStreamImp->GetLength(); | 867 m_iLength = m_pStreamImp->GetLength(); |
916 } | 868 } |
917 return bRet; | 869 return bRet; |
918 } | 870 } |
919 FX_BOOL CFX_Stream::LoadFileRead(IFX_FileRead* pFileRead, FX_DWORD dwAccess) { | 871 FX_BOOL CFX_Stream::LoadFileRead(IFX_FileRead* pFileRead, FX_DWORD dwAccess) { |
920 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { | 872 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { |
921 return FALSE; | 873 return FALSE; |
922 } | 874 } |
923 if (pFileRead == NULL) { | 875 if (pFileRead == NULL) { |
924 return FALSE; | 876 return FALSE; |
925 } | 877 } |
926 m_pStreamImp = new CFX_FileReadStreamImp(); | 878 m_pStreamImp = new CFX_FileReadStreamImp(); |
927 if (m_pStreamImp == NULL) { | |
928 return FALSE; | |
929 } | |
930 FX_BOOL bRet = | 879 FX_BOOL bRet = |
931 ((CFX_FileReadStreamImp*)m_pStreamImp)->LoadFileRead(pFileRead, dwAccess); | 880 ((CFX_FileReadStreamImp*)m_pStreamImp)->LoadFileRead(pFileRead, dwAccess); |
932 if (!bRet) { | 881 if (!bRet) { |
933 m_pStreamImp->Release(); | 882 m_pStreamImp->Release(); |
934 m_pStreamImp = NULL; | 883 m_pStreamImp = NULL; |
935 } else { | 884 } else { |
936 m_eStreamType = FX_STREAMTYPE_File; | 885 m_eStreamType = FX_STREAMTYPE_File; |
937 m_dwAccess = dwAccess; | 886 m_dwAccess = dwAccess; |
938 m_iLength = m_pStreamImp->GetLength(); | 887 m_iLength = m_pStreamImp->GetLength(); |
939 } | 888 } |
940 return bRet; | 889 return bRet; |
941 } | 890 } |
942 FX_BOOL CFX_Stream::LoadFileWrite(IFX_FileWrite* pFileWrite, | 891 FX_BOOL CFX_Stream::LoadFileWrite(IFX_FileWrite* pFileWrite, |
943 FX_DWORD dwAccess) { | 892 FX_DWORD dwAccess) { |
944 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { | 893 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { |
945 return FALSE; | 894 return FALSE; |
946 } | 895 } |
947 if (pFileWrite == NULL) { | 896 if (pFileWrite == NULL) { |
948 return FALSE; | 897 return FALSE; |
949 } | 898 } |
950 m_pStreamImp = new CFX_FileWriteStreamImp(); | 899 m_pStreamImp = new CFX_FileWriteStreamImp(); |
951 if (m_pStreamImp == NULL) { | |
952 return FALSE; | |
953 } | |
954 FX_BOOL bRet = ((CFX_FileWriteStreamImp*)m_pStreamImp) | 900 FX_BOOL bRet = ((CFX_FileWriteStreamImp*)m_pStreamImp) |
955 ->LoadFileWrite(pFileWrite, dwAccess); | 901 ->LoadFileWrite(pFileWrite, dwAccess); |
956 if (!bRet) { | 902 if (!bRet) { |
957 m_pStreamImp->Release(); | 903 m_pStreamImp->Release(); |
958 m_pStreamImp = NULL; | 904 m_pStreamImp = NULL; |
959 } else { | 905 } else { |
960 m_eStreamType = FX_STREAMTYPE_File; | 906 m_eStreamType = FX_STREAMTYPE_File; |
961 m_dwAccess = dwAccess; | 907 m_dwAccess = dwAccess; |
962 m_iLength = m_pStreamImp->GetLength(); | 908 m_iLength = m_pStreamImp->GetLength(); |
963 } | 909 } |
964 return bRet; | 910 return bRet; |
965 } | 911 } |
966 FX_BOOL CFX_Stream::LoadBuffer(uint8_t* pData, | 912 FX_BOOL CFX_Stream::LoadBuffer(uint8_t* pData, |
967 int32_t iTotalSize, | 913 int32_t iTotalSize, |
968 FX_DWORD dwAccess) { | 914 FX_DWORD dwAccess) { |
969 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { | 915 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { |
970 return FALSE; | 916 return FALSE; |
971 } | 917 } |
972 if (pData == NULL || iTotalSize < 1) { | 918 if (pData == NULL || iTotalSize < 1) { |
973 return FALSE; | 919 return FALSE; |
974 } | 920 } |
975 m_pStreamImp = new CFX_BufferStreamImp(); | 921 m_pStreamImp = new CFX_BufferStreamImp(); |
976 if (m_pStreamImp == NULL) { | |
977 return FALSE; | |
978 } | |
979 FX_BOOL bRet = ((CFX_BufferStreamImp*)m_pStreamImp) | 922 FX_BOOL bRet = ((CFX_BufferStreamImp*)m_pStreamImp) |
980 ->LoadBuffer(pData, iTotalSize, dwAccess); | 923 ->LoadBuffer(pData, iTotalSize, dwAccess); |
981 if (!bRet) { | 924 if (!bRet) { |
982 m_pStreamImp->Release(); | 925 m_pStreamImp->Release(); |
983 m_pStreamImp = NULL; | 926 m_pStreamImp = NULL; |
984 } else { | 927 } else { |
985 m_eStreamType = FX_STREAMTYPE_Buffer; | 928 m_eStreamType = FX_STREAMTYPE_Buffer; |
986 m_dwAccess = dwAccess; | 929 m_dwAccess = dwAccess; |
987 m_iLength = m_pStreamImp->GetLength(); | 930 m_iLength = m_pStreamImp->GetLength(); |
988 } | 931 } |
989 return bRet; | 932 return bRet; |
990 } | 933 } |
991 FX_BOOL CFX_Stream::LoadBufferRead(IFX_BufferRead* pBufferRead, | 934 FX_BOOL CFX_Stream::LoadBufferRead(IFX_BufferRead* pBufferRead, |
992 int32_t iFileSize, | 935 int32_t iFileSize, |
993 FX_DWORD dwAccess, | 936 FX_DWORD dwAccess, |
994 FX_BOOL bReleaseBufferRead) { | 937 FX_BOOL bReleaseBufferRead) { |
995 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { | 938 if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp != NULL) { |
996 return FALSE; | 939 return FALSE; |
997 } | 940 } |
998 if (!pBufferRead) { | 941 if (!pBufferRead) { |
999 return FALSE; | 942 return FALSE; |
1000 } | 943 } |
1001 m_pStreamImp = new CFX_BufferReadStreamImp; | 944 m_pStreamImp = new CFX_BufferReadStreamImp; |
1002 if (m_pStreamImp == NULL) { | |
1003 return FALSE; | |
1004 } | |
1005 FX_BOOL bRet = ((CFX_BufferReadStreamImp*)m_pStreamImp) | 945 FX_BOOL bRet = ((CFX_BufferReadStreamImp*)m_pStreamImp) |
1006 ->LoadBufferRead(pBufferRead, iFileSize, dwAccess, | 946 ->LoadBufferRead(pBufferRead, iFileSize, dwAccess, |
1007 bReleaseBufferRead); | 947 bReleaseBufferRead); |
1008 if (!bRet) { | 948 if (!bRet) { |
1009 m_pStreamImp->Release(); | 949 m_pStreamImp->Release(); |
1010 m_pStreamImp = NULL; | 950 m_pStreamImp = NULL; |
1011 } else { | 951 } else { |
1012 m_eStreamType = FX_STREAMTYPE_BufferRead; | 952 m_eStreamType = FX_STREAMTYPE_BufferRead; |
1013 m_dwAccess = dwAccess; | 953 m_dwAccess = dwAccess; |
1014 m_iLength = m_pStreamImp->GetLength(); | 954 m_iLength = m_pStreamImp->GetLength(); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1238 register int32_t iStart = m_iStart + iOffset; | 1178 register int32_t iStart = m_iStart + iOffset; |
1239 register int32_t iTotal = m_iStart + m_iLength; | 1179 register int32_t iTotal = m_iStart + m_iLength; |
1240 if (iStart < m_iStart || iStart >= iTotal) { | 1180 if (iStart < m_iStart || iStart >= iTotal) { |
1241 return NULL; | 1181 return NULL; |
1242 } | 1182 } |
1243 register int32_t iEnd = iStart + iLength; | 1183 register int32_t iEnd = iStart + iLength; |
1244 if (iEnd < iStart || iEnd > iTotal) { | 1184 if (iEnd < iStart || iEnd > iTotal) { |
1245 return NULL; | 1185 return NULL; |
1246 } | 1186 } |
1247 CFX_Stream* pShared = new CFX_Stream; | 1187 CFX_Stream* pShared = new CFX_Stream; |
1248 if (pShared == NULL) { | |
1249 return NULL; | |
1250 } | |
1251 pShared->m_eStreamType = FX_STREAMTYPE_Stream; | 1188 pShared->m_eStreamType = FX_STREAMTYPE_Stream; |
1252 pShared->m_pStreamImp = m_pStreamImp; | 1189 pShared->m_pStreamImp = m_pStreamImp; |
1253 pShared->m_dwAccess = dwAccess; | 1190 pShared->m_dwAccess = dwAccess; |
1254 pShared->m_iTotalSize = iLength; | 1191 pShared->m_iTotalSize = iLength; |
1255 pShared->m_iPosition = iStart; | 1192 pShared->m_iPosition = iStart; |
1256 pShared->m_iStart = iStart; | 1193 pShared->m_iStart = iStart; |
1257 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; | 1194 pShared->m_iLength = (dwAccess & FX_STREAMACCESS_Write) != 0 ? 0 : iLength; |
1258 if (dwAccess & FX_STREAMACCESS_Text) { | 1195 if (dwAccess & FX_STREAMACCESS_Text) { |
1259 return IFX_Stream::CreateTextStream(pShared, TRUE); | 1196 return IFX_Stream::CreateTextStream(pShared, TRUE); |
1260 } | 1197 } |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1556 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, | 1493 FX_BOOL CFGAS_FileWrite::WriteBlock(const void* pData, |
1557 FX_DWORD offset, | 1494 FX_DWORD offset, |
1558 FX_DWORD size) { | 1495 FX_DWORD size) { |
1559 m_pStream->Lock(); | 1496 m_pStream->Lock(); |
1560 m_pStream->Seek(FX_STREAMSEEK_Begin, offset); | 1497 m_pStream->Seek(FX_STREAMSEEK_Begin, offset); |
1561 int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size); | 1498 int32_t iLen = m_pStream->WriteData((uint8_t*)pData, (int32_t)size); |
1562 m_pStream->Unlock(); | 1499 m_pStream->Unlock(); |
1563 return iLen == (int32_t)size; | 1500 return iLen == (int32_t)size; |
1564 } | 1501 } |
1565 #endif | 1502 #endif |
OLD | NEW |