| 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 "core/include/fpdfapi/fpdf_parser.h" | 7 #include "core/include/fpdfapi/fpdf_parser.h" |
| 8 | 8 |
| 9 #include "core/include/fxcrt/fx_string.h" | 9 #include "core/include/fxcrt/fx_string.h" |
| 10 | 10 |
| 11 namespace { |
| 12 |
| 13 const FX_DWORD kBlockSize = 1024; |
| 14 |
| 15 } // namespace |
| 16 |
| 11 // static | 17 // static |
| 12 int CPDF_Object::s_nCurRefDepth = 0; | 18 int CPDF_Object::s_nCurRefDepth = 0; |
| 13 | 19 |
| 14 void CPDF_Object::Release() { | 20 void CPDF_Object::Release() { |
| 15 if (m_ObjNum) { | 21 if (m_ObjNum) { |
| 16 return; | 22 return; |
| 17 } | 23 } |
| 18 Destroy(); | 24 Destroy(); |
| 19 } | 25 } |
| 20 void CPDF_Object::Destroy() { | 26 void CPDF_Object::Destroy() { |
| (...skipping 788 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 CPDF_Array* pArray = new CPDF_Array; | 815 CPDF_Array* pArray = new CPDF_Array; |
| 810 pArray->AddNumber16(matrix.a); | 816 pArray->AddNumber16(matrix.a); |
| 811 pArray->AddNumber16(matrix.b); | 817 pArray->AddNumber16(matrix.b); |
| 812 pArray->AddNumber16(matrix.c); | 818 pArray->AddNumber16(matrix.c); |
| 813 pArray->AddNumber16(matrix.d); | 819 pArray->AddNumber16(matrix.d); |
| 814 pArray->AddNumber(matrix.e); | 820 pArray->AddNumber(matrix.e); |
| 815 pArray->AddNumber(matrix.f); | 821 pArray->AddNumber(matrix.f); |
| 816 SetAt(key, pArray); | 822 SetAt(key, pArray); |
| 817 } | 823 } |
| 818 CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) | 824 CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) |
| 819 : CPDF_Object(PDFOBJ_STREAM) { | 825 : CPDF_Object(PDFOBJ_STREAM), |
| 820 m_pDict = pDict; | 826 m_pDict(pDict), |
| 821 m_dwSize = size; | 827 m_dwSize(size), |
| 822 m_GenNum = (FX_DWORD)-1; | 828 m_GenNum(kMemoryBasedGenNum), |
| 823 m_pDataBuf = pData; | 829 m_pDataBuf(pData) {} |
| 824 m_pCryptoHandler = NULL; | 830 |
| 831 CPDF_Stream::~CPDF_Stream() { |
| 832 if (IsMemoryBased()) |
| 833 FX_Free(m_pDataBuf); |
| 834 |
| 835 if (m_pDict) |
| 836 m_pDict->Release(); |
| 825 } | 837 } |
| 826 CPDF_Stream::~CPDF_Stream() { | 838 |
| 827 if (m_GenNum == (FX_DWORD)-1) { | 839 void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) { |
| 828 FX_Free(m_pDataBuf); | |
| 829 } | |
| 830 if (m_pDict) { | |
| 831 m_pDict->Release(); | |
| 832 } | |
| 833 } | |
| 834 void CPDF_Stream::InitStream(CPDF_Dictionary* pDict) { | |
| 835 if (pDict) { | 840 if (pDict) { |
| 836 if (m_pDict) { | 841 if (m_pDict) |
| 837 m_pDict->Release(); | 842 m_pDict->Release(); |
| 838 } | |
| 839 m_pDict = pDict; | 843 m_pDict = pDict; |
| 840 } | 844 } |
| 841 if (m_GenNum == (FX_DWORD)-1) { | 845 if (IsMemoryBased()) |
| 842 FX_Free(m_pDataBuf); | 846 FX_Free(m_pDataBuf); |
| 843 } | 847 |
| 844 m_GenNum = 0; | 848 m_GenNum = 0; |
| 845 m_pFile = NULL; | 849 m_pFile = nullptr; |
| 846 m_pCryptoHandler = NULL; | |
| 847 m_FileOffset = 0; | |
| 848 } | 850 } |
| 851 |
| 849 void CPDF_Stream::InitStream(uint8_t* pData, | 852 void CPDF_Stream::InitStream(uint8_t* pData, |
| 850 FX_DWORD size, | 853 FX_DWORD size, |
| 851 CPDF_Dictionary* pDict) { | 854 CPDF_Dictionary* pDict) { |
| 852 InitStream(pDict); | 855 InitStreamInternal(pDict); |
| 853 m_GenNum = (FX_DWORD)-1; | 856 m_GenNum = kMemoryBasedGenNum; |
| 854 m_pDataBuf = FX_Alloc(uint8_t, size); | 857 m_pDataBuf = FX_Alloc(uint8_t, size); |
| 855 if (pData) { | 858 if (pData) { |
| 856 FXSYS_memcpy(m_pDataBuf, pData, size); | 859 FXSYS_memcpy(m_pDataBuf, pData, size); |
| 857 } | 860 } |
| 858 m_dwSize = size; | 861 m_dwSize = size; |
| 859 if (m_pDict) { | 862 if (m_pDict) { |
| 860 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); | 863 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); |
| 861 } | 864 } |
| 862 } | 865 } |
| 863 void CPDF_Stream::SetData(const uint8_t* pData, | 866 void CPDF_Stream::SetData(const uint8_t* pData, |
| 864 FX_DWORD size, | 867 FX_DWORD size, |
| 865 FX_BOOL bCompressed, | 868 FX_BOOL bCompressed, |
| 866 FX_BOOL bKeepBuf) { | 869 FX_BOOL bKeepBuf) { |
| 867 if (m_GenNum == (FX_DWORD)-1) { | 870 if (IsMemoryBased()) |
| 868 FX_Free(m_pDataBuf); | 871 FX_Free(m_pDataBuf); |
| 869 } else { | 872 m_GenNum = kMemoryBasedGenNum; |
| 870 m_GenNum = (FX_DWORD)-1; | 873 |
| 871 m_pCryptoHandler = NULL; | |
| 872 } | |
| 873 if (bKeepBuf) { | 874 if (bKeepBuf) { |
| 874 m_pDataBuf = (uint8_t*)pData; | 875 m_pDataBuf = (uint8_t*)pData; |
| 875 } else { | 876 } else { |
| 876 m_pDataBuf = FX_Alloc(uint8_t, size); | 877 m_pDataBuf = FX_Alloc(uint8_t, size); |
| 877 if (pData) { | 878 if (pData) { |
| 878 FXSYS_memcpy(m_pDataBuf, pData, size); | 879 FXSYS_memcpy(m_pDataBuf, pData, size); |
| 879 } | 880 } |
| 880 } | 881 } |
| 881 m_dwSize = size; | 882 m_dwSize = size; |
| 882 if (m_pDict == NULL) { | 883 if (!m_pDict) |
| 883 m_pDict = new CPDF_Dictionary; | 884 m_pDict = new CPDF_Dictionary; |
| 884 } | |
| 885 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); | 885 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); |
| 886 if (!bCompressed) { | 886 if (!bCompressed) { |
| 887 m_pDict->RemoveAt(FX_BSTRC("Filter")); | 887 m_pDict->RemoveAt(FX_BSTRC("Filter")); |
| 888 m_pDict->RemoveAt(FX_BSTRC("DecodeParms")); | 888 m_pDict->RemoveAt(FX_BSTRC("DecodeParms")); |
| 889 } | 889 } |
| 890 } | 890 } |
| 891 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, | 891 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, |
| 892 uint8_t* buf, | 892 uint8_t* buf, |
| 893 FX_DWORD size) const { | 893 FX_DWORD size) const { |
| 894 if ((m_GenNum != (FX_DWORD)-1) && m_pFile) { | 894 if (!IsMemoryBased() && m_pFile) |
| 895 return m_pFile->ReadBlock(buf, m_FileOffset + offset, size); | 895 return m_pFile->ReadBlock(buf, offset, size); |
| 896 } | 896 |
| 897 if (m_pDataBuf) { | 897 if (m_pDataBuf) |
| 898 FXSYS_memcpy(buf, m_pDataBuf + offset, size); | 898 FXSYS_memcpy(buf, m_pDataBuf + offset, size); |
| 899 } | |
| 900 return TRUE; | 899 return TRUE; |
| 901 } | 900 } |
| 902 void CPDF_Stream::InitStream(IFX_FileRead* pFile, CPDF_Dictionary* pDict) { | 901 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile, |
| 903 InitStream(pDict); | 902 CPDF_Dictionary* pDict) { |
| 903 InitStreamInternal(pDict); |
| 904 m_pFile = pFile; | 904 m_pFile = pFile; |
| 905 m_dwSize = (FX_DWORD)pFile->GetSize(); | 905 m_dwSize = (FX_DWORD)pFile->GetSize(); |
| 906 if (m_pDict) { | 906 if (m_pDict) { |
| 907 m_pDict->SetAtInteger(FX_BSTRC("Length"), m_dwSize); | 907 m_pDict->SetAtInteger(FX_BSTRC("Length"), m_dwSize); |
| 908 } | 908 } |
| 909 } | 909 } |
| 910 | 910 |
| 911 FX_BOOL CPDF_Stream::Identical(CPDF_Stream* pOther) const { | 911 FX_BOOL CPDF_Stream::Identical(CPDF_Stream* pOther) const { |
| 912 if (!m_pDict) | 912 if (!m_pDict) |
| 913 return pOther->m_pDict ? FALSE : TRUE; | 913 return !pOther->m_pDict; |
| 914 | 914 |
| 915 if (!m_pDict->Identical(pOther->m_pDict)) { | 915 if (!m_pDict->Identical(pOther->m_pDict)) |
| 916 return FALSE; | 916 return FALSE; |
| 917 } | 917 |
| 918 if (m_dwSize != pOther->m_dwSize) { | 918 if (m_dwSize != pOther->m_dwSize) |
| 919 return FALSE; | 919 return FALSE; |
| 920 } | 920 |
| 921 if (m_GenNum != (FX_DWORD)-1 && pOther->m_GenNum != (FX_DWORD)-1) { | 921 if (!IsMemoryBased() && !pOther->IsMemoryBased()) { |
| 922 if (m_pFile == pOther->m_pFile && m_pFile == NULL) { | 922 if (m_pFile == pOther->m_pFile && !m_pFile) |
| 923 return TRUE; | 923 return TRUE; |
| 924 } | 924 |
| 925 if (!m_pFile || !pOther->m_pFile) { | 925 if (!m_pFile || !pOther->m_pFile) |
| 926 return FALSE; | 926 return FALSE; |
| 927 } | 927 |
| 928 uint8_t srcBuf[1024]; | 928 uint8_t srcBuf[kBlockSize]; |
| 929 uint8_t destBuf[1024]; | 929 uint8_t destBuf[kBlockSize]; |
| 930 FX_DWORD size = m_dwSize; | 930 FX_DWORD size = m_dwSize; |
| 931 FX_DWORD srcOffset = m_FileOffset; | 931 if (m_pFile == pOther->m_pFile) |
| 932 FX_DWORD destOffset = pOther->m_FileOffset; | |
| 933 if (m_pFile == pOther->m_pFile && srcOffset == destOffset) { | |
| 934 return TRUE; | 932 return TRUE; |
| 935 } | 933 |
| 934 FX_DWORD offset = 0; |
| 936 while (size > 0) { | 935 while (size > 0) { |
| 937 FX_DWORD actualSize = size > 1024 ? 1024 : size; | 936 FX_DWORD actualSize = std::min(size, kBlockSize); |
| 938 m_pFile->ReadBlock(srcBuf, srcOffset, actualSize); | 937 m_pFile->ReadBlock(srcBuf, offset, actualSize); |
| 939 pOther->m_pFile->ReadBlock(destBuf, destOffset, actualSize); | 938 pOther->m_pFile->ReadBlock(destBuf, offset, actualSize); |
| 940 if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0) { | 939 if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0) |
| 941 return FALSE; | 940 return FALSE; |
| 942 } | 941 |
| 943 size -= actualSize; | 942 size -= actualSize; |
| 944 srcOffset += actualSize; | 943 offset += actualSize; |
| 945 destOffset += actualSize; | |
| 946 } | 944 } |
| 947 return TRUE; | 945 return TRUE; |
| 948 } | 946 } |
| 949 if (m_GenNum != (FX_DWORD)-1 || pOther->m_GenNum != (FX_DWORD)-1) { | 947 |
| 950 IFX_FileRead* pFile = NULL; | 948 if (!IsMemoryBased() || !pOther->IsMemoryBased()) { |
| 951 uint8_t* pBuf = NULL; | 949 IFX_FileRead* pFile = nullptr; |
| 952 FX_DWORD offset = 0; | 950 uint8_t* pBuf = nullptr; |
| 953 if (pOther->m_GenNum != (FX_DWORD)-1) { | 951 if (!pOther->IsMemoryBased()) { |
| 954 pFile = pOther->m_pFile; | 952 pFile = pOther->m_pFile; |
| 955 pBuf = m_pDataBuf; | 953 pBuf = m_pDataBuf; |
| 956 offset = pOther->m_FileOffset; | 954 } else if (!IsMemoryBased()) { |
| 957 } else if (m_GenNum != (FX_DWORD)-1) { | |
| 958 pFile = m_pFile; | 955 pFile = m_pFile; |
| 959 pBuf = pOther->m_pDataBuf; | 956 pBuf = pOther->m_pDataBuf; |
| 960 offset = m_FileOffset; | |
| 961 } | 957 } |
| 962 if (NULL == pBuf) { | 958 if (!pBuf) |
| 963 return FALSE; | 959 return FALSE; |
| 964 } | 960 |
| 965 uint8_t srcBuf[1024]; | 961 uint8_t srcBuf[kBlockSize]; |
| 966 FX_DWORD size = m_dwSize; | 962 FX_DWORD size = m_dwSize; |
| 963 FX_DWORD offset = 0; |
| 967 while (size > 0) { | 964 while (size > 0) { |
| 968 FX_DWORD actualSize = std::min(size, 1024U); | 965 FX_DWORD actualSize = std::min(size, kBlockSize); |
| 969 pFile->ReadBlock(srcBuf, offset, actualSize); | 966 pFile->ReadBlock(srcBuf, offset, actualSize); |
| 970 if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0) { | 967 if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0) |
| 971 return FALSE; | 968 return FALSE; |
| 972 } | 969 |
| 973 pBuf += actualSize; | 970 pBuf += actualSize; |
| 974 size -= actualSize; | 971 size -= actualSize; |
| 975 offset += actualSize; | 972 offset += actualSize; |
| 976 } | 973 } |
| 977 return TRUE; | 974 return TRUE; |
| 978 } | 975 } |
| 979 return FXSYS_memcmp(m_pDataBuf, pOther->m_pDataBuf, m_dwSize) == 0; | 976 return FXSYS_memcmp(m_pDataBuf, pOther->m_pDataBuf, m_dwSize) == 0; |
| 980 } | 977 } |
| 981 | 978 |
| 982 CPDF_StreamAcc::CPDF_StreamAcc() { | 979 CPDF_StreamAcc::CPDF_StreamAcc() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1006 if (dwSrcSize == 0) | 1003 if (dwSrcSize == 0) |
| 1007 return; | 1004 return; |
| 1008 | 1005 |
| 1009 if (!pStream->IsMemoryBased()) { | 1006 if (!pStream->IsMemoryBased()) { |
| 1010 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); | 1007 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); |
| 1011 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) | 1008 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) |
| 1012 return; | 1009 return; |
| 1013 } else { | 1010 } else { |
| 1014 pSrcData = pStream->m_pDataBuf; | 1011 pSrcData = pStream->m_pDataBuf; |
| 1015 } | 1012 } |
| 1016 uint8_t* pDecryptedData; | 1013 uint8_t* pDecryptedData = pSrcData; |
| 1017 FX_DWORD dwDecryptedSize; | 1014 FX_DWORD dwDecryptedSize = dwSrcSize; |
| 1018 if (pStream->m_pCryptoHandler) { | |
| 1019 CFX_BinaryBuf dest_buf; | |
| 1020 dest_buf.EstimateSize(pStream->m_pCryptoHandler->DecryptGetSize(dwSrcSize)); | |
| 1021 void* context = pStream->m_pCryptoHandler->DecryptStart( | |
| 1022 pStream->GetObjNum(), pStream->m_GenNum); | |
| 1023 pStream->m_pCryptoHandler->DecryptStream(context, pSrcData, dwSrcSize, | |
| 1024 dest_buf); | |
| 1025 pStream->m_pCryptoHandler->DecryptFinish(context, dest_buf); | |
| 1026 pDecryptedData = dest_buf.GetBuffer(); | |
| 1027 dwDecryptedSize = dest_buf.GetSize(); | |
| 1028 dest_buf.DetachBuffer(); | |
| 1029 } else { | |
| 1030 pDecryptedData = pSrcData; | |
| 1031 dwDecryptedSize = dwSrcSize; | |
| 1032 } | |
| 1033 if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) { | 1015 if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) { |
| 1034 m_pData = pDecryptedData; | 1016 m_pData = pDecryptedData; |
| 1035 m_dwSize = dwDecryptedSize; | 1017 m_dwSize = dwDecryptedSize; |
| 1036 } else { | 1018 } else { |
| 1037 FX_BOOL bRet = PDF_DataDecode( | 1019 FX_BOOL bRet = PDF_DataDecode( |
| 1038 pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, | 1020 pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, |
| 1039 m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); | 1021 m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); |
| 1040 if (!bRet) { | 1022 if (!bRet) { |
| 1041 m_pData = pDecryptedData; | 1023 m_pData = pDecryptedData; |
| 1042 m_dwSize = dwDecryptedSize; | 1024 m_dwSize = dwDecryptedSize; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1187 } | 1169 } |
| 1188 pObj->m_ObjNum = objnum; | 1170 pObj->m_ObjNum = objnum; |
| 1189 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); | 1171 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); |
| 1190 if (m_LastObjNum < objnum) { | 1172 if (m_LastObjNum < objnum) { |
| 1191 m_LastObjNum = objnum; | 1173 m_LastObjNum = objnum; |
| 1192 } | 1174 } |
| 1193 } | 1175 } |
| 1194 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { | 1176 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { |
| 1195 return m_LastObjNum; | 1177 return m_LastObjNum; |
| 1196 } | 1178 } |
| OLD | NEW |