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 1977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3483 int32_t iSize = pArray->GetCount(); | 3482 int32_t iSize = pArray->GetCount(); |
3484 CPDF_Object* pItem = NULL; | 3483 CPDF_Object* pItem = NULL; |
3485 for (int32_t j = 0; j < iSize; ++j) { | 3484 for (int32_t j = 0; j < iSize; ++j) { |
3486 pItem = pArray->GetElement(j); | 3485 pItem = pArray->GetElement(j); |
3487 if (pItem && pItem->GetType() == PDFOBJ_REFERENCE) { | 3486 if (pItem && pItem->GetType() == PDFOBJ_REFERENCE) { |
3488 UnavailObjList.Add(((CPDF_Reference*)pItem)->GetRefObjNum()); | 3487 UnavailObjList.Add(((CPDF_Reference*)pItem)->GetRefObjNum()); |
3489 } | 3488 } |
3490 } | 3489 } |
3491 } | 3490 } |
3492 } | 3491 } |
3493 if (pObj->GetType() != PDFOBJ_DICTIONARY) { | 3492 if (!pObj->IsDictionary()) { |
3494 pObj->Release(); | 3493 pObj->Release(); |
3495 continue; | 3494 continue; |
3496 } | 3495 } |
3497 CFX_ByteString type = pObj->GetDict()->GetString(FX_BSTRC("Type")); | 3496 CFX_ByteString type = pObj->GetDict()->GetString(FX_BSTRC("Type")); |
3498 if (type == FX_BSTRC("Pages")) { | 3497 if (type == FX_BSTRC("Pages")) { |
3499 m_PagesArray.Add(pObj); | 3498 m_PagesArray.Add(pObj); |
3500 continue; | 3499 continue; |
3501 } | 3500 } |
3502 pObj->Release(); | 3501 pObj->Release(); |
3503 } | 3502 } |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4036 } | 4035 } |
4037 ScopedFileStream file(FX_CreateMemoryStream(pBuf, (size_t)iSize, FALSE)); | 4036 ScopedFileStream file(FX_CreateMemoryStream(pBuf, (size_t)iSize, FALSE)); |
4038 m_syntaxParser.InitParser(file.get(), 0); | 4037 m_syntaxParser.InitParser(file.get(), 0); |
4039 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pTrailer( | 4038 nonstd::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>> pTrailer( |
4040 m_syntaxParser.GetObject(nullptr, 0, 0)); | 4039 m_syntaxParser.GetObject(nullptr, 0, 0)); |
4041 if (!pTrailer) { | 4040 if (!pTrailer) { |
4042 m_Pos += m_syntaxParser.SavePos(); | 4041 m_Pos += m_syntaxParser.SavePos(); |
4043 pHints->AddSegment(m_Pos, iTrailerSize); | 4042 pHints->AddSegment(m_Pos, iTrailerSize); |
4044 return FALSE; | 4043 return FALSE; |
4045 } | 4044 } |
4046 if (pTrailer->GetType() != PDFOBJ_DICTIONARY) | 4045 if (!pTrailer->IsDictionary()) |
4047 return FALSE; | 4046 return FALSE; |
4048 | 4047 |
4049 CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); | 4048 CPDF_Dictionary* pTrailerDict = pTrailer->GetDict(); |
4050 CPDF_Object* pEncrypt = pTrailerDict->GetElement("Encrypt"); | 4049 CPDF_Object* pEncrypt = pTrailerDict->GetElement("Encrypt"); |
4051 if (pEncrypt && pEncrypt->GetType() == PDFOBJ_REFERENCE) { | 4050 if (pEncrypt && pEncrypt->GetType() == PDFOBJ_REFERENCE) { |
4052 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; | 4051 m_docStatus = PDF_DATAAVAIL_LOADALLFILE; |
4053 return TRUE; | 4052 return TRUE; |
4054 } | 4053 } |
4055 | 4054 |
4056 FX_DWORD xrefpos = GetDirectInteger(pTrailerDict, FX_BSTRC("Prev")); | 4055 FX_DWORD xrefpos = GetDirectInteger(pTrailerDict, FX_BSTRC("Prev")); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4151 return FALSE; | 4150 return FALSE; |
4152 } | 4151 } |
4153 return FALSE; | 4152 return FALSE; |
4154 } | 4153 } |
4155 if (pPage->GetType() == PDFOBJ_ARRAY) { | 4154 if (pPage->GetType() == PDFOBJ_ARRAY) { |
4156 pPageNode->m_dwPageNo = dwPageNo; | 4155 pPageNode->m_dwPageNo = dwPageNo; |
4157 pPageNode->m_type = PDF_PAGENODE_ARRAY; | 4156 pPageNode->m_type = PDF_PAGENODE_ARRAY; |
4158 pPage->Release(); | 4157 pPage->Release(); |
4159 return TRUE; | 4158 return TRUE; |
4160 } | 4159 } |
4161 if (pPage->GetType() != PDFOBJ_DICTIONARY) { | 4160 if (!pPage->IsDictionary()) { |
4162 pPage->Release(); | 4161 pPage->Release(); |
4163 m_docStatus = PDF_DATAAVAIL_ERROR; | 4162 m_docStatus = PDF_DATAAVAIL_ERROR; |
4164 return FALSE; | 4163 return FALSE; |
4165 } | 4164 } |
4166 pPageNode->m_dwPageNo = dwPageNo; | 4165 pPageNode->m_dwPageNo = dwPageNo; |
4167 CPDF_Dictionary* pDict = pPage->GetDict(); | 4166 CPDF_Dictionary* pDict = pPage->GetDict(); |
4168 CFX_ByteString type = pDict->GetString(FX_BSTRC("Type")); | 4167 CFX_ByteString type = pDict->GetString(FX_BSTRC("Type")); |
4169 if (type == FX_BSTRC("Pages")) { | 4168 if (type == FX_BSTRC("Pages")) { |
4170 pPageNode->m_type = PDF_PAGENODE_PAGES; | 4169 pPageNode->m_type = PDF_PAGENODE_PAGES; |
4171 CPDF_Object* pKids = pDict->GetElement(FX_BSTRC("Kids")); | 4170 CPDF_Object* pKids = pDict->GetElement(FX_BSTRC("Kids")); |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4617 return FALSE; | 4616 return FALSE; |
4618 } | 4617 } |
4619 CPDF_PageNode::~CPDF_PageNode() { | 4618 CPDF_PageNode::~CPDF_PageNode() { |
4620 int32_t iSize = m_childNode.GetSize(); | 4619 int32_t iSize = m_childNode.GetSize(); |
4621 for (int32_t i = 0; i < iSize; ++i) { | 4620 for (int32_t i = 0; i < iSize; ++i) { |
4622 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; | 4621 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; |
4623 delete pNode; | 4622 delete pNode; |
4624 } | 4623 } |
4625 m_childNode.RemoveAll(); | 4624 m_childNode.RemoveAll(); |
4626 } | 4625 } |
OLD | NEW |