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 <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 return PDFPARSE_ERROR_SUCCESS; | 270 return PDFPARSE_ERROR_SUCCESS; |
271 } | 271 } |
272 FX_DWORD CPDF_Parser::SetEncryptHandler() { | 272 FX_DWORD CPDF_Parser::SetEncryptHandler() { |
273 ReleaseEncryptHandler(); | 273 ReleaseEncryptHandler(); |
274 SetEncryptDictionary(NULL); | 274 SetEncryptDictionary(NULL); |
275 if (m_pTrailer == NULL) { | 275 if (m_pTrailer == NULL) { |
276 return PDFPARSE_ERROR_FORMAT; | 276 return PDFPARSE_ERROR_FORMAT; |
277 } | 277 } |
278 CPDF_Object* pEncryptObj = m_pTrailer->GetElement(FX_BSTRC("Encrypt")); | 278 CPDF_Object* pEncryptObj = m_pTrailer->GetElement(FX_BSTRC("Encrypt")); |
279 if (pEncryptObj) { | 279 if (pEncryptObj) { |
280 if (pEncryptObj->GetType() == PDFOBJ_DICTIONARY) { | 280 if (CPDF_Dictionary* pEncryptDict = pEncryptObj->AsDictionary()) { |
281 SetEncryptDictionary((CPDF_Dictionary*)pEncryptObj); | 281 SetEncryptDictionary(pEncryptDict); |
282 } else if (pEncryptObj->GetType() == PDFOBJ_REFERENCE) { | 282 } else if (pEncryptObj->GetType() == PDFOBJ_REFERENCE) { |
283 pEncryptObj = m_pDocument->GetIndirectObject( | 283 pEncryptObj = m_pDocument->GetIndirectObject( |
284 ((CPDF_Reference*)pEncryptObj)->GetRefObjNum()); | 284 ((CPDF_Reference*)pEncryptObj)->GetRefObjNum()); |
285 if (pEncryptObj) { | 285 if (pEncryptObj) { |
286 SetEncryptDictionary(pEncryptObj->GetDict()); | 286 SetEncryptDictionary(pEncryptObj->GetDict()); |
287 } | 287 } |
288 } | 288 } |
289 } | 289 } |
290 if (m_bForceUseSecurityHandler) { | 290 if (m_bForceUseSecurityHandler) { |
291 FX_DWORD err = PDFPARSE_ERROR_HANDLER; | 291 FX_DWORD err = PDFPARSE_ERROR_HANDLER; |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 pDict->GetString(FX_BSTRC("Type")); | 797 pDict->GetString(FX_BSTRC("Type")); |
798 if (bsValue == FX_BSTRC("XRef") && | 798 if (bsValue == FX_BSTRC("XRef") && |
799 pDict->KeyExist(FX_BSTRC("Size"))) { | 799 pDict->KeyExist(FX_BSTRC("Size"))) { |
800 CPDF_Object* pRoot = | 800 CPDF_Object* pRoot = |
801 pDict->GetElement(FX_BSTRC("Root")); | 801 pDict->GetElement(FX_BSTRC("Root")); |
802 if (pRoot && pRoot->GetDict() && | 802 if (pRoot && pRoot->GetDict() && |
803 pRoot->GetDict()->GetElement(FX_BSTRC("Pages"))) { | 803 pRoot->GetDict()->GetElement(FX_BSTRC("Pages"))) { |
804 if (m_pTrailer) { | 804 if (m_pTrailer) { |
805 m_pTrailer->Release(); | 805 m_pTrailer->Release(); |
806 } | 806 } |
807 m_pTrailer = (CPDF_Dictionary*)pDict->Clone(); | 807 m_pTrailer = ToDictionary(pDict->Clone()); |
808 } | 808 } |
809 } | 809 } |
810 } | 810 } |
811 } | 811 } |
812 } | 812 } |
813 } | 813 } |
814 FX_FILESIZE offset = 0; | 814 FX_FILESIZE offset = 0; |
815 m_Syntax.RestorePos(obj_pos); | 815 m_Syntax.RestorePos(obj_pos); |
816 offset = m_Syntax.FindTag(FX_BSTRC("obj"), 0); | 816 offset = m_Syntax.FindTag(FX_BSTRC("obj"), 0); |
817 if (offset == -1) { | 817 if (offset == -1) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 break; | 850 break; |
851 } | 851 } |
852 break; | 852 break; |
853 case 7: | 853 case 7: |
854 if (inside_index == 7) { | 854 if (inside_index == 7) { |
855 if (PDF_CharType[byte] == 'W' || PDF_CharType[byte] == 'D') { | 855 if (PDF_CharType[byte] == 'W' || PDF_CharType[byte] == 'D') { |
856 last_trailer = pos + i - 7; | 856 last_trailer = pos + i - 7; |
857 m_Syntax.RestorePos(pos + i - m_Syntax.m_HeaderOffset); | 857 m_Syntax.RestorePos(pos + i - m_Syntax.m_HeaderOffset); |
858 CPDF_Object* pObj = m_Syntax.GetObject(m_pDocument, 0, 0, 0); | 858 CPDF_Object* pObj = m_Syntax.GetObject(m_pDocument, 0, 0, 0); |
859 if (pObj) { | 859 if (pObj) { |
860 if (pObj->GetType() != PDFOBJ_DICTIONARY && | 860 if (!pObj->IsDictionary() && pObj->GetType() != PDFOBJ_STREAM) { |
861 pObj->GetType() != PDFOBJ_STREAM) { | |
862 pObj->Release(); | 861 pObj->Release(); |
863 } else { | 862 } else { |
864 CPDF_Dictionary* pTrailer = NULL; | 863 CPDF_Dictionary* pTrailer = NULL; |
865 if (pObj->GetType() == PDFOBJ_STREAM) { | 864 if (pObj->GetType() == PDFOBJ_STREAM) { |
866 pTrailer = ((CPDF_Stream*)pObj)->GetDict(); | 865 pTrailer = ((CPDF_Stream*)pObj)->GetDict(); |
867 } else { | 866 } else { |
868 pTrailer = (CPDF_Dictionary*)pObj; | 867 pTrailer = pObj->AsDictionary(); |
869 } | 868 } |
870 if (pTrailer) { | 869 if (pTrailer) { |
871 if (m_pTrailer) { | 870 if (m_pTrailer) { |
872 CPDF_Object* pRoot = | 871 CPDF_Object* pRoot = |
873 pTrailer->GetElement(FX_BSTRC("Root")); | 872 pTrailer->GetElement(FX_BSTRC("Root")); |
874 if (pRoot == NULL || | 873 if (pRoot == NULL || |
875 (pRoot->GetType() == PDFOBJ_REFERENCE && | 874 (pRoot->GetType() == PDFOBJ_REFERENCE && |
876 (FX_DWORD)m_CrossRef.GetSize() > | 875 (FX_DWORD)m_CrossRef.GetSize() > |
877 ((CPDF_Reference*)pRoot)->GetRefObjNum() && | 876 ((CPDF_Reference*)pRoot)->GetRefObjNum() && |
878 m_CrossRef.GetAt(((CPDF_Reference*)pRoot) | 877 m_CrossRef.GetAt(((CPDF_Reference*)pRoot) |
879 ->GetRefObjNum()) != 0)) { | 878 ->GetRefObjNum()) != 0)) { |
880 FX_POSITION pos = pTrailer->GetStartPos(); | 879 FX_POSITION pos = pTrailer->GetStartPos(); |
881 while (pos) { | 880 while (pos) { |
882 CFX_ByteString key; | 881 CFX_ByteString key; |
883 CPDF_Object* pObj = | 882 CPDF_Object* pObj = |
884 pTrailer->GetNextElement(pos, key); | 883 pTrailer->GetNextElement(pos, key); |
885 m_pTrailer->SetAt(key, pObj->Clone(), m_pDocument); | 884 m_pTrailer->SetAt(key, pObj->Clone(), m_pDocument); |
886 } | 885 } |
887 pObj->Release(); | 886 pObj->Release(); |
888 } else { | 887 } else { |
889 pObj->Release(); | 888 pObj->Release(); |
890 } | 889 } |
891 } else { | 890 } else { |
892 if (pObj->GetType() == PDFOBJ_STREAM) { | 891 if (pObj->GetType() == PDFOBJ_STREAM) { |
893 m_pTrailer = (CPDF_Dictionary*)pTrailer->Clone(); | 892 m_pTrailer = ToDictionary(pTrailer->Clone()); |
894 pObj->Release(); | 893 pObj->Release(); |
895 } else { | 894 } else { |
896 m_pTrailer = pTrailer; | 895 m_pTrailer = pTrailer; |
897 } | 896 } |
898 FX_FILESIZE dwSavePos = m_Syntax.SavePos(); | 897 FX_FILESIZE dwSavePos = m_Syntax.SavePos(); |
899 CFX_ByteString strWord = m_Syntax.GetKeyword(); | 898 CFX_ByteString strWord = m_Syntax.GetKeyword(); |
900 if (!strWord.Compare(FX_BSTRC("startxref"))) { | 899 if (!strWord.Compare(FX_BSTRC("startxref"))) { |
901 FX_BOOL bNumber = FALSE; | 900 FX_BOOL bNumber = FALSE; |
902 CFX_ByteString bsOffset = m_Syntax.GetNextWord(bNumber); | 901 CFX_ByteString bsOffset = m_Syntax.GetNextWord(bNumber); |
903 if (bNumber) { | 902 if (bNumber) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1027 if (pStream->GetType() != PDFOBJ_STREAM) { | 1026 if (pStream->GetType() != PDFOBJ_STREAM) { |
1028 return FALSE; | 1027 return FALSE; |
1029 } | 1028 } |
1030 prev = pStream->GetDict()->GetInteger(FX_BSTRC("Prev")); | 1029 prev = pStream->GetDict()->GetInteger(FX_BSTRC("Prev")); |
1031 int32_t size = pStream->GetDict()->GetInteger(FX_BSTRC("Size")); | 1030 int32_t size = pStream->GetDict()->GetInteger(FX_BSTRC("Size")); |
1032 if (size < 0) { | 1031 if (size < 0) { |
1033 pStream->Release(); | 1032 pStream->Release(); |
1034 return FALSE; | 1033 return FALSE; |
1035 } | 1034 } |
1036 if (bMainXRef) { | 1035 if (bMainXRef) { |
1037 m_pTrailer = (CPDF_Dictionary*)pStream->GetDict()->Clone(); | 1036 m_pTrailer = ToDictionary(pStream->GetDict()->Clone()); |
1038 m_CrossRef.SetSize(size); | 1037 m_CrossRef.SetSize(size); |
1039 if (m_V5Type.SetSize(size)) { | 1038 if (m_V5Type.SetSize(size)) { |
1040 FXSYS_memset(m_V5Type.GetData(), 0, size); | 1039 FXSYS_memset(m_V5Type.GetData(), 0, size); |
1041 } | 1040 } |
1042 } else { | 1041 } else { |
1043 m_Trailers.Add((CPDF_Dictionary*)pStream->GetDict()->Clone()); | 1042 m_Trailers.Add(ToDictionary(pStream->GetDict()->Clone())); |
1044 } | 1043 } |
1045 std::vector<std::pair<int32_t, int32_t> > arrIndex; | 1044 std::vector<std::pair<int32_t, int32_t> > arrIndex; |
1046 CPDF_Array* pArray = pStream->GetDict()->GetArray(FX_BSTRC("Index")); | 1045 CPDF_Array* pArray = pStream->GetDict()->GetArray(FX_BSTRC("Index")); |
1047 if (pArray) { | 1046 if (pArray) { |
1048 FX_DWORD nPairSize = pArray->GetCount() / 2; | 1047 FX_DWORD nPairSize = pArray->GetCount() / 2; |
1049 for (FX_DWORD i = 0; i < nPairSize; i++) { | 1048 for (FX_DWORD i = 0; i < nPairSize; i++) { |
1050 CPDF_Object* pStartNumObj = pArray->GetElement(i * 2); | 1049 CPDF_Object* pStartNumObj = pArray->GetElement(i * 2); |
1051 CPDF_Object* pCountObj = pArray->GetElement(i * 2 + 1); | 1050 CPDF_Object* pCountObj = pArray->GetElement(i * 2 + 1); |
1052 if (pStartNumObj && pStartNumObj->GetType() == PDFOBJ_NUMBER && | 1051 if (pStartNumObj && pStartNumObj->GetType() == PDFOBJ_NUMBER && |
1053 pCountObj && pCountObj->GetType() == PDFOBJ_NUMBER) { | 1052 pCountObj && pCountObj->GetType() == PDFOBJ_NUMBER) { |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1483 m_Syntax.RestorePos(SavedPos); | 1482 m_Syntax.RestorePos(SavedPos); |
1484 return pObj; | 1483 return pObj; |
1485 } | 1484 } |
1486 | 1485 |
1487 CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { | 1486 CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { |
1488 if (m_Syntax.GetKeyword() != FX_BSTRC("trailer")) | 1487 if (m_Syntax.GetKeyword() != FX_BSTRC("trailer")) |
1489 return nullptr; | 1488 return nullptr; |
1490 | 1489 |
1491 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pObj( | 1490 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pObj( |
1492 m_Syntax.GetObject(m_pDocument, 0, 0, 0)); | 1491 m_Syntax.GetObject(m_pDocument, 0, 0, 0)); |
1493 if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY) | 1492 if (!ToDictionary(pObj.get())) |
1494 return nullptr; | 1493 return nullptr; |
1495 return static_cast<CPDF_Dictionary*>(pObj.release()); | 1494 return pObj.release()->AsDictionary(); |
1496 } | 1495 } |
1497 | 1496 |
1498 FX_DWORD CPDF_Parser::GetPermissions(FX_BOOL bCheckRevision) { | 1497 FX_DWORD CPDF_Parser::GetPermissions(FX_BOOL bCheckRevision) { |
1499 if (m_pSecurityHandler == NULL) { | 1498 if (m_pSecurityHandler == NULL) { |
1500 return (FX_DWORD)-1; | 1499 return (FX_DWORD)-1; |
1501 } | 1500 } |
1502 FX_DWORD dwPermission = m_pSecurityHandler->GetPermissions(); | 1501 FX_DWORD dwPermission = m_pSecurityHandler->GetPermissions(); |
1503 if (m_pEncryptDict && | 1502 if (m_pEncryptDict && |
1504 m_pEncryptDict->GetString(FX_BSTRC("Filter")) == FX_BSTRC("Standard")) { | 1503 m_pEncryptDict->GetString(FX_BSTRC("Filter")) == FX_BSTRC("Standard")) { |
1505 dwPermission &= 0xFFFFFFFC; | 1504 dwPermission &= 0xFFFFFFFC; |
(...skipping 1983 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3489 int32_t iSize = pArray->GetCount(); | 3488 int32_t iSize = pArray->GetCount(); |
3490 CPDF_Object* pItem = NULL; | 3489 CPDF_Object* pItem = NULL; |
3491 for (int32_t j = 0; j < iSize; ++j) { | 3490 for (int32_t j = 0; j < iSize; ++j) { |
3492 pItem = pArray->GetElement(j); | 3491 pItem = pArray->GetElement(j); |
3493 if (pItem && pItem->GetType() == PDFOBJ_REFERENCE) { | 3492 if (pItem && pItem->GetType() == PDFOBJ_REFERENCE) { |
3494 UnavailObjList.Add(((CPDF_Reference*)pItem)->GetRefObjNum()); | 3493 UnavailObjList.Add(((CPDF_Reference*)pItem)->GetRefObjNum()); |
3495 } | 3494 } |
3496 } | 3495 } |
3497 } | 3496 } |
3498 } | 3497 } |
3499 if (pObj->GetType() != PDFOBJ_DICTIONARY) { | 3498 if (!pObj->IsDictionary()) { |
3500 pObj->Release(); | 3499 pObj->Release(); |
3501 continue; | 3500 continue; |
3502 } | 3501 } |
3503 CFX_ByteString type = pObj->GetDict()->GetString(FX_BSTRC("Type")); | 3502 CFX_ByteString type = pObj->GetDict()->GetString(FX_BSTRC("Type")); |
3504 if (type == FX_BSTRC("Pages")) { | 3503 if (type == FX_BSTRC("Pages")) { |
3505 m_PagesArray.Add(pObj); | 3504 m_PagesArray.Add(pObj); |
3506 continue; | 3505 continue; |
3507 } | 3506 } |
3508 pObj->Release(); | 3507 pObj->Release(); |
3509 } | 3508 } |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4042 } | 4041 } |
4043 ScopedFileStream file(FX_CreateMemoryStream(pBuf, (size_t)iSize, FALSE)); | 4042 ScopedFileStream file(FX_CreateMemoryStream(pBuf, (size_t)iSize, FALSE)); |
4044 m_syntaxParser.InitParser(file.get(), 0); | 4043 m_syntaxParser.InitParser(file.get(), 0); |
4045 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pTrailer( | 4044 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pTrailer( |
4046 m_syntaxParser.GetObject(nullptr, 0, 0)); | 4045 m_syntaxParser.GetObject(nullptr, 0, 0)); |
4047 if (!pTrailer) { | 4046 if (!pTrailer) { |
4048 m_Pos += m_syntaxParser.SavePos(); | 4047 m_Pos += m_syntaxParser.SavePos(); |
4049 pHints->AddSegment(m_Pos, iTrailerSize); | 4048 pHints->AddSegment(m_Pos, iTrailerSize); |
4050 return FALSE; | 4049 return FALSE; |
4051 } | 4050 } |
4052 if (pTrailer->GetType() != PDFOBJ_DICTIONARY) | 4051 if (!pTrailer->IsDictionary()) |
4053 return FALSE; | 4052 return FALSE; |
4054 | 4053 |
4055 CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); | 4054 CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); |
4056 CPDF_Object* pEncrypt = pTrailerDict->GetElement("Encrypt"); | 4055 CPDF_Object* pEncrypt = pTrailerDict->GetElement("Encrypt"); |
4057 if (pEncrypt && pEncrypt->GetType() == PDFOBJ_REFERENCE) { | 4056 if (pEncrypt && pEncrypt->GetType() == PDFOBJ_REFERENCE) { |
4058 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; | 4057 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; |
4059 return TRUE; | 4058 return TRUE; |
4060 } | 4059 } |
4061 | 4060 |
4062 FX_DWORD xrefpos = GetDirectInteger(pTrailerDict, FX_BSTRC("Prev")); | 4061 FX_DWORD xrefpos = GetDirectInteger(pTrailerDict, FX_BSTRC("Prev")); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4157 return FALSE; | 4156 return FALSE; |
4158 } | 4157 } |
4159 return FALSE; | 4158 return FALSE; |
4160 } | 4159 } |
4161 if (pPage->GetType() == PDFOBJ_ARRAY) { | 4160 if (pPage->GetType() == PDFOBJ_ARRAY) { |
4162 pPageNode->m_dwPageNo = dwPageNo; | 4161 pPageNode->m_dwPageNo = dwPageNo; |
4163 pPageNode->m_type = PDF_PAGENODE_ARRAY; | 4162 pPageNode->m_type = PDF_PAGENODE_ARRAY; |
4164 pPage->Release(); | 4163 pPage->Release(); |
4165 return TRUE; | 4164 return TRUE; |
4166 } | 4165 } |
4167 if (pPage->GetType() != PDFOBJ_DICTIONARY) { | 4166 if (!pPage->IsDictionary()) { |
4168 pPage->Release(); | 4167 pPage->Release(); |
4169 m_docStatus = PDF_DATAAVAIL_ERROR; | 4168 m_docStatus = PDF_DATAAVAIL_ERROR; |
4170 return FALSE; | 4169 return FALSE; |
4171 } | 4170 } |
4172 pPageNode->m_dwPageNo = dwPageNo; | 4171 pPageNode->m_dwPageNo = dwPageNo; |
4173 CPDF_Dictionary* pDict = pPage->GetDict(); | 4172 CPDF_Dictionary* pDict = pPage->GetDict(); |
4174 CFX_ByteString type = pDict->GetString(FX_BSTRC("Type")); | 4173 CFX_ByteString type = pDict->GetString(FX_BSTRC("Type")); |
4175 if (type == FX_BSTRC("Pages")) { | 4174 if (type == FX_BSTRC("Pages")) { |
4176 pPageNode->m_type = PDF_PAGENODE_PAGES; | 4175 pPageNode->m_type = PDF_PAGENODE_PAGES; |
4177 CPDF_Object* pKids = pDict->GetElement(FX_BSTRC("Kids")); | 4176 CPDF_Object* pKids = pDict->GetElement(FX_BSTRC("Kids")); |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4623 return FALSE; | 4622 return FALSE; |
4624 } | 4623 } |
4625 CPDF_PageNode::~CPDF_PageNode() { | 4624 CPDF_PageNode::~CPDF_PageNode() { |
4626 int32_t iSize = m_childNode.GetSize(); | 4625 int32_t iSize = m_childNode.GetSize(); |
4627 for (int32_t i = 0; i < iSize; ++i) { | 4626 for (int32_t i = 0; i < iSize; ++i) { |
4628 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; | 4627 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; |
4629 delete pNode; | 4628 delete pNode; |
4630 } | 4629 } |
4631 m_childNode.RemoveAll(); | 4630 m_childNode.RemoveAll(); |
4632 } | 4631 } |
OLD | NEW |