Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp

Issue 1415163009: Cleanup CPDF_Stream: (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: self review Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp ('k') | core/src/fpdfdoc/doc_ap.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "../../../include/fpdfapi/fpdf_parser.h" 7 #include "../../../include/fpdfapi/fpdf_parser.h"
8 #include "../../../include/fxcrt/fx_string.h" 8 #include "../../../include/fxcrt/fx_string.h"
9 9
10 // static 10 // static
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 CPDF_Array* pArray = new CPDF_Array; 808 CPDF_Array* pArray = new CPDF_Array;
809 pArray->AddNumber16(matrix.a); 809 pArray->AddNumber16(matrix.a);
810 pArray->AddNumber16(matrix.b); 810 pArray->AddNumber16(matrix.b);
811 pArray->AddNumber16(matrix.c); 811 pArray->AddNumber16(matrix.c);
812 pArray->AddNumber16(matrix.d); 812 pArray->AddNumber16(matrix.d);
813 pArray->AddNumber(matrix.e); 813 pArray->AddNumber(matrix.e);
814 pArray->AddNumber(matrix.f); 814 pArray->AddNumber(matrix.f);
815 SetAt(key, pArray); 815 SetAt(key, pArray);
816 } 816 }
817 CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict) 817 CPDF_Stream::CPDF_Stream(uint8_t* pData, FX_DWORD size, CPDF_Dictionary* pDict)
818 : CPDF_Object(PDFOBJ_STREAM) { 818 : CPDF_Object(PDFOBJ_STREAM),
819 m_pDict = pDict; 819 m_pDict(pDict),
820 m_dwSize = size; 820 m_dwSize(size),
821 m_GenNum = (FX_DWORD)-1; 821 m_GenNum(kMemoryBasedGenNum),
822 m_pDataBuf = pData; 822 m_pDataBuf(pData) {}
823 m_pCryptoHandler = NULL; 823
824 CPDF_Stream::~CPDF_Stream() {
825 if (IsMemoryBased())
826 FX_Free(m_pDataBuf);
827
828 if (m_pDict)
829 m_pDict->Release();
824 } 830 }
825 CPDF_Stream::~CPDF_Stream() { 831
826 if (m_GenNum == (FX_DWORD)-1) { 832 void CPDF_Stream::InitStreamInternal(CPDF_Dictionary* pDict) {
827 FX_Free(m_pDataBuf);
828 }
829 if (m_pDict) {
830 m_pDict->Release();
831 }
832 }
833 void CPDF_Stream::InitStream(CPDF_Dictionary* pDict) {
834 if (pDict) { 833 if (pDict) {
835 if (m_pDict) { 834 if (m_pDict)
836 m_pDict->Release(); 835 m_pDict->Release();
837 }
838 m_pDict = pDict; 836 m_pDict = pDict;
839 } 837 }
840 if (m_GenNum == (FX_DWORD)-1) { 838 if (IsMemoryBased())
841 FX_Free(m_pDataBuf); 839 FX_Free(m_pDataBuf);
842 } 840
843 m_GenNum = 0; 841 m_GenNum = 0;
844 m_pFile = NULL; 842 m_pFile = nullptr;
845 m_pCryptoHandler = NULL;
846 m_FileOffset = 0;
847 } 843 }
844
848 void CPDF_Stream::InitStream(uint8_t* pData, 845 void CPDF_Stream::InitStream(uint8_t* pData,
849 FX_DWORD size, 846 FX_DWORD size,
850 CPDF_Dictionary* pDict) { 847 CPDF_Dictionary* pDict) {
851 InitStream(pDict); 848 InitStreamInternal(pDict);
852 m_GenNum = (FX_DWORD)-1; 849 m_GenNum = kMemoryBasedGenNum;
853 m_pDataBuf = FX_Alloc(uint8_t, size); 850 m_pDataBuf = FX_Alloc(uint8_t, size);
854 if (pData) { 851 if (pData) {
855 FXSYS_memcpy(m_pDataBuf, pData, size); 852 FXSYS_memcpy(m_pDataBuf, pData, size);
856 } 853 }
857 m_dwSize = size; 854 m_dwSize = size;
858 if (m_pDict) { 855 if (m_pDict) {
859 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); 856 m_pDict->SetAtInteger(FX_BSTRC("Length"), size);
860 } 857 }
861 } 858 }
862 void CPDF_Stream::SetData(const uint8_t* pData, 859 void CPDF_Stream::SetData(const uint8_t* pData,
863 FX_DWORD size, 860 FX_DWORD size,
864 FX_BOOL bCompressed, 861 FX_BOOL bCompressed,
865 FX_BOOL bKeepBuf) { 862 FX_BOOL bKeepBuf) {
866 if (m_GenNum == (FX_DWORD)-1) { 863 if (IsMemoryBased())
867 FX_Free(m_pDataBuf); 864 FX_Free(m_pDataBuf);
868 } else { 865 else
869 m_GenNum = (FX_DWORD)-1; 866 m_GenNum = kMemoryBasedGenNum;
dsinclair 2015/11/10 17:32:36 It would be clearer to always do this assignment.
Lei Zhang 2015/11/10 18:18:06 Done.
870 m_pCryptoHandler = NULL; 867
871 }
872 if (bKeepBuf) { 868 if (bKeepBuf) {
873 m_pDataBuf = (uint8_t*)pData; 869 m_pDataBuf = (uint8_t*)pData;
874 } else { 870 } else {
875 m_pDataBuf = FX_Alloc(uint8_t, size); 871 m_pDataBuf = FX_Alloc(uint8_t, size);
876 if (pData) { 872 if (pData) {
877 FXSYS_memcpy(m_pDataBuf, pData, size); 873 FXSYS_memcpy(m_pDataBuf, pData, size);
878 } 874 }
879 } 875 }
880 m_dwSize = size; 876 m_dwSize = size;
881 if (m_pDict == NULL) { 877 if (!m_pDict)
882 m_pDict = new CPDF_Dictionary; 878 m_pDict = new CPDF_Dictionary;
883 }
884 m_pDict->SetAtInteger(FX_BSTRC("Length"), size); 879 m_pDict->SetAtInteger(FX_BSTRC("Length"), size);
885 if (!bCompressed) { 880 if (!bCompressed) {
886 m_pDict->RemoveAt(FX_BSTRC("Filter")); 881 m_pDict->RemoveAt(FX_BSTRC("Filter"));
887 m_pDict->RemoveAt(FX_BSTRC("DecodeParms")); 882 m_pDict->RemoveAt(FX_BSTRC("DecodeParms"));
888 } 883 }
889 } 884 }
890 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset, 885 FX_BOOL CPDF_Stream::ReadRawData(FX_FILESIZE offset,
891 uint8_t* buf, 886 uint8_t* buf,
892 FX_DWORD size) const { 887 FX_DWORD size) const {
893 if ((m_GenNum != (FX_DWORD)-1) && m_pFile) { 888 if (!IsMemoryBased() && m_pFile)
894 return m_pFile->ReadBlock(buf, m_FileOffset + offset, size); 889 return m_pFile->ReadBlock(buf, offset, size);
895 } 890
896 if (m_pDataBuf) { 891 if (m_pDataBuf)
897 FXSYS_memcpy(buf, m_pDataBuf + offset, size); 892 FXSYS_memcpy(buf, m_pDataBuf + offset, size);
898 }
899 return TRUE; 893 return TRUE;
900 } 894 }
901 void CPDF_Stream::InitStream(IFX_FileRead* pFile, CPDF_Dictionary* pDict) { 895 void CPDF_Stream::InitStreamFromFile(IFX_FileRead* pFile,
902 InitStream(pDict); 896 CPDF_Dictionary* pDict) {
897 InitStreamInternal(pDict);
903 m_pFile = pFile; 898 m_pFile = pFile;
904 m_dwSize = (FX_DWORD)pFile->GetSize(); 899 m_dwSize = (FX_DWORD)pFile->GetSize();
905 if (m_pDict) { 900 if (m_pDict) {
906 m_pDict->SetAtInteger(FX_BSTRC("Length"), m_dwSize); 901 m_pDict->SetAtInteger(FX_BSTRC("Length"), m_dwSize);
907 } 902 }
908 } 903 }
909 904
910 FX_BOOL CPDF_Stream::Identical(CPDF_Stream* pOther) const { 905 FX_BOOL CPDF_Stream::Identical(CPDF_Stream* pOther) const {
911 if (!m_pDict) 906 if (!m_pDict)
912 return pOther->m_pDict ? FALSE : TRUE; 907 return !pOther->m_pDict;
913 908
914 if (!m_pDict->Identical(pOther->m_pDict)) { 909 if (!m_pDict->Identical(pOther->m_pDict))
915 return FALSE; 910 return FALSE;
916 } 911
917 if (m_dwSize != pOther->m_dwSize) { 912 if (m_dwSize != pOther->m_dwSize)
918 return FALSE; 913 return FALSE;
919 } 914
920 if (m_GenNum != (FX_DWORD)-1 && pOther->m_GenNum != (FX_DWORD)-1) { 915 if (!IsMemoryBased() && !pOther->IsMemoryBased()) {
921 if (m_pFile == pOther->m_pFile && m_pFile == NULL) { 916 if (m_pFile == pOther->m_pFile && !m_pFile)
922 return TRUE; 917 return TRUE;
923 } 918
924 if (!m_pFile || !pOther->m_pFile) { 919 if (!m_pFile || !pOther->m_pFile)
925 return FALSE; 920 return FALSE;
926 } 921
927 uint8_t srcBuf[1024]; 922 const FX_DWORD kBlockSize = 1024;
928 uint8_t destBuf[1024]; 923 uint8_t srcBuf[kBlockSize];
924 uint8_t destBuf[kBlockSize];
929 FX_DWORD size = m_dwSize; 925 FX_DWORD size = m_dwSize;
930 FX_DWORD srcOffset = m_FileOffset; 926 if (m_pFile == pOther->m_pFile)
931 FX_DWORD destOffset = pOther->m_FileOffset;
932 if (m_pFile == pOther->m_pFile && srcOffset == destOffset) {
933 return TRUE; 927 return TRUE;
934 } 928
929 FX_DWORD offset = 0;
935 while (size > 0) { 930 while (size > 0) {
936 FX_DWORD actualSize = size > 1024 ? 1024 : size; 931 FX_DWORD actualSize = std::min(size, kBlockSize);
937 m_pFile->ReadBlock(srcBuf, srcOffset, actualSize); 932 m_pFile->ReadBlock(srcBuf, offset, actualSize);
938 pOther->m_pFile->ReadBlock(destBuf, destOffset, actualSize); 933 pOther->m_pFile->ReadBlock(destBuf, offset, actualSize);
939 if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0) { 934 if (FXSYS_memcmp(srcBuf, destBuf, actualSize) != 0)
940 return FALSE; 935 return FALSE;
941 } 936
942 size -= actualSize; 937 size -= actualSize;
943 srcOffset += actualSize; 938 offset += actualSize;
944 destOffset += actualSize;
945 } 939 }
946 return TRUE; 940 return TRUE;
947 } 941 }
948 if (m_GenNum != (FX_DWORD)-1 || pOther->m_GenNum != (FX_DWORD)-1) { 942
949 IFX_FileRead* pFile = NULL; 943 if (!IsMemoryBased() || !pOther->IsMemoryBased()) {
950 uint8_t* pBuf = NULL; 944 IFX_FileRead* pFile = nullptr;
951 FX_DWORD offset = 0; 945 uint8_t* pBuf = nullptr;
952 if (pOther->m_GenNum != (FX_DWORD)-1) { 946 if (!pOther->IsMemoryBased()) {
953 pFile = pOther->m_pFile; 947 pFile = pOther->m_pFile;
954 pBuf = m_pDataBuf; 948 pBuf = m_pDataBuf;
955 offset = pOther->m_FileOffset; 949 } else if (!IsMemoryBased()) {
956 } else if (m_GenNum != (FX_DWORD)-1) {
957 pFile = m_pFile; 950 pFile = m_pFile;
958 pBuf = pOther->m_pDataBuf; 951 pBuf = pOther->m_pDataBuf;
959 offset = m_FileOffset;
960 } 952 }
961 if (NULL == pBuf) { 953 if (!pBuf)
962 return FALSE; 954 return FALSE;
963 } 955
964 uint8_t srcBuf[1024]; 956 const FX_DWORD kBlockSize = 1024;
dsinclair 2015/11/10 17:32:36 This is defined twice, here are 922, can we move i
Lei Zhang 2015/11/10 18:18:06 Done.
957 uint8_t srcBuf[kBlockSize];
965 FX_DWORD size = m_dwSize; 958 FX_DWORD size = m_dwSize;
959 FX_DWORD offset = 0;
966 while (size > 0) { 960 while (size > 0) {
967 FX_DWORD actualSize = std::min(size, 1024U); 961 FX_DWORD actualSize = std::min(size, kBlockSize);
968 pFile->ReadBlock(srcBuf, offset, actualSize); 962 pFile->ReadBlock(srcBuf, offset, actualSize);
969 if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0) { 963 if (FXSYS_memcmp(srcBuf, pBuf, actualSize) != 0)
970 return FALSE; 964 return FALSE;
971 } 965
972 pBuf += actualSize; 966 pBuf += actualSize;
973 size -= actualSize; 967 size -= actualSize;
974 offset += actualSize; 968 offset += actualSize;
975 } 969 }
976 return TRUE; 970 return TRUE;
977 } 971 }
978 return FXSYS_memcmp(m_pDataBuf, pOther->m_pDataBuf, m_dwSize) == 0; 972 return FXSYS_memcmp(m_pDataBuf, pOther->m_pDataBuf, m_dwSize) == 0;
979 } 973 }
980 974
981 CPDF_StreamAcc::CPDF_StreamAcc() { 975 CPDF_StreamAcc::CPDF_StreamAcc() {
(...skipping 23 matching lines...) Expand all
1005 if (dwSrcSize == 0) 999 if (dwSrcSize == 0)
1006 return; 1000 return;
1007 1001
1008 if (!pStream->IsMemoryBased()) { 1002 if (!pStream->IsMemoryBased()) {
1009 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize); 1003 pSrcData = m_pSrcData = FX_Alloc(uint8_t, dwSrcSize);
1010 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize)) 1004 if (!pStream->ReadRawData(0, pSrcData, dwSrcSize))
1011 return; 1005 return;
1012 } else { 1006 } else {
1013 pSrcData = pStream->m_pDataBuf; 1007 pSrcData = pStream->m_pDataBuf;
1014 } 1008 }
1015 uint8_t* pDecryptedData; 1009 uint8_t* pDecryptedData = pSrcData;
1016 FX_DWORD dwDecryptedSize; 1010 FX_DWORD dwDecryptedSize = dwSrcSize;
1017 if (pStream->m_pCryptoHandler) {
1018 CFX_BinaryBuf dest_buf;
1019 dest_buf.EstimateSize(pStream->m_pCryptoHandler->DecryptGetSize(dwSrcSize));
1020 void* context = pStream->m_pCryptoHandler->DecryptStart(
1021 pStream->GetObjNum(), pStream->m_GenNum);
1022 pStream->m_pCryptoHandler->DecryptStream(context, pSrcData, dwSrcSize,
1023 dest_buf);
1024 pStream->m_pCryptoHandler->DecryptFinish(context, dest_buf);
1025 pDecryptedData = dest_buf.GetBuffer();
1026 dwDecryptedSize = dest_buf.GetSize();
1027 dest_buf.DetachBuffer();
1028 } else {
1029 pDecryptedData = pSrcData;
1030 dwDecryptedSize = dwSrcSize;
1031 }
1032 if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) { 1011 if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) {
1033 m_pData = pDecryptedData; 1012 m_pData = pDecryptedData;
1034 m_dwSize = dwDecryptedSize; 1013 m_dwSize = dwDecryptedSize;
1035 } else { 1014 } else {
1036 FX_BOOL bRet = PDF_DataDecode( 1015 FX_BOOL bRet = PDF_DataDecode(
1037 pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, 1016 pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData,
1038 m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); 1017 m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc);
1039 if (!bRet) { 1018 if (!bRet) {
1040 m_pData = pDecryptedData; 1019 m_pData = pDecryptedData;
1041 m_dwSize = dwDecryptedSize; 1020 m_dwSize = dwDecryptedSize;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 } 1165 }
1187 pObj->m_ObjNum = objnum; 1166 pObj->m_ObjNum = objnum;
1188 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); 1167 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj);
1189 if (m_LastObjNum < objnum) { 1168 if (m_LastObjNum < objnum) {
1190 m_LastObjNum = objnum; 1169 m_LastObjNum = objnum;
1191 } 1170 }
1192 } 1171 }
1193 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { 1172 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const {
1194 return m_LastObjNum; 1173 return m_LastObjNum;
1195 } 1174 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp ('k') | core/src/fpdfdoc/doc_ap.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698