| 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 <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "core/include/fpdfapi/fpdf_module.h" | 11 #include "core/include/fpdfapi/fpdf_module.h" |
| 12 #include "third_party/base/stl_util.h" | 12 #include "third_party/base/stl_util.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 int CountPages(CPDF_Dictionary* pPages, | 16 int CountPages(CPDF_Dictionary* pPages, |
| 17 std::set<CPDF_Dictionary*>* visited_pages) { | 17 std::set<CPDF_Dictionary*>* visited_pages) { |
| 18 int count = pPages->GetInteger("Count"); | 18 int count = pPages->GetIntegerBy("Count"); |
| 19 if (count > 0 && count < FPDF_PAGE_MAX_NUM) { | 19 if (count > 0 && count < FPDF_PAGE_MAX_NUM) { |
| 20 return count; | 20 return count; |
| 21 } | 21 } |
| 22 CPDF_Array* pKidList = pPages->GetArray("Kids"); | 22 CPDF_Array* pKidList = pPages->GetArrayBy("Kids"); |
| 23 if (!pKidList) { | 23 if (!pKidList) { |
| 24 return 0; | 24 return 0; |
| 25 } | 25 } |
| 26 count = 0; | 26 count = 0; |
| 27 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { | 27 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { |
| 28 CPDF_Dictionary* pKid = pKidList->GetDict(i); | 28 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 29 if (!pKid || pdfium::ContainsKey(*visited_pages, pKid)) { | 29 if (!pKid || pdfium::ContainsKey(*visited_pages, pKid)) { |
| 30 continue; | 30 continue; |
| 31 } | 31 } |
| 32 if (pKid->KeyExist("Kids")) { | 32 if (pKid->KeyExist("Kids")) { |
| 33 // Use |visited_pages| to help detect circular references of pages. | 33 // Use |visited_pages| to help detect circular references of pages. |
| 34 ScopedSetInsertion<CPDF_Dictionary*> local_add(visited_pages, pKid); | 34 ScopedSetInsertion<CPDF_Dictionary*> local_add(visited_pages, pKid); |
| 35 count += CountPages(pKid, visited_pages); | 35 count += CountPages(pKid, visited_pages); |
| 36 } else { | 36 } else { |
| 37 // This page is a leaf node. | 37 // This page is a leaf node. |
| 38 count++; | 38 count++; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 m_pRootDict = pRootObj->GetDict(); | 78 m_pRootDict = pRootObj->GetDict(); |
| 79 if (!m_pRootDict) { | 79 if (!m_pRootDict) { |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 CPDF_Object* pInfoObj = GetIndirectObject(m_pParser->GetInfoObjNum()); | 82 CPDF_Object* pInfoObj = GetIndirectObject(m_pParser->GetInfoObjNum()); |
| 83 if (pInfoObj) { | 83 if (pInfoObj) { |
| 84 m_pInfoDict = pInfoObj->GetDict(); | 84 m_pInfoDict = pInfoObj->GetDict(); |
| 85 } | 85 } |
| 86 CPDF_Array* pIDArray = m_pParser->GetIDArray(); | 86 CPDF_Array* pIDArray = m_pParser->GetIDArray(); |
| 87 if (pIDArray) { | 87 if (pIDArray) { |
| 88 m_ID1 = pIDArray->GetString(0); | 88 m_ID1 = pIDArray->GetStringAt(0); |
| 89 m_ID2 = pIDArray->GetString(1); | 89 m_ID2 = pIDArray->GetStringAt(1); |
| 90 } | 90 } |
| 91 m_PageList.SetSize(RetrievePageCount()); | 91 m_PageList.SetSize(RetrievePageCount()); |
| 92 } | 92 } |
| 93 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { | 93 void CPDF_Document::LoadAsynDoc(CPDF_Dictionary* pLinearized) { |
| 94 m_bLinearized = TRUE; | 94 m_bLinearized = TRUE; |
| 95 m_LastObjNum = m_pParser->GetLastObjNum(); | 95 m_LastObjNum = m_pParser->GetLastObjNum(); |
| 96 CPDF_Object* pIndirectObj = GetIndirectObject(m_pParser->GetRootObjNum()); | 96 CPDF_Object* pIndirectObj = GetIndirectObject(m_pParser->GetRootObjNum()); |
| 97 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; | 97 m_pRootDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; |
| 98 if (!m_pRootDict) { | 98 if (!m_pRootDict) { |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 pIndirectObj = GetIndirectObject(m_pParser->GetInfoObjNum()); | 101 pIndirectObj = GetIndirectObject(m_pParser->GetInfoObjNum()); |
| 102 m_pInfoDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; | 102 m_pInfoDict = pIndirectObj ? pIndirectObj->GetDict() : nullptr; |
| 103 CPDF_Array* pIDArray = m_pParser->GetIDArray(); | 103 CPDF_Array* pIDArray = m_pParser->GetIDArray(); |
| 104 if (pIDArray) { | 104 if (pIDArray) { |
| 105 m_ID1 = pIDArray->GetString(0); | 105 m_ID1 = pIDArray->GetStringAt(0); |
| 106 m_ID2 = pIDArray->GetString(1); | 106 m_ID2 = pIDArray->GetStringAt(1); |
| 107 } | 107 } |
| 108 FX_DWORD dwPageCount = 0; | 108 FX_DWORD dwPageCount = 0; |
| 109 CPDF_Object* pCount = pLinearized->GetElement("N"); | 109 CPDF_Object* pCount = pLinearized->GetElement("N"); |
| 110 if (ToNumber(pCount)) | 110 if (ToNumber(pCount)) |
| 111 dwPageCount = pCount->GetInteger(); | 111 dwPageCount = pCount->GetInteger(); |
| 112 | 112 |
| 113 m_PageList.SetSize(dwPageCount); | 113 m_PageList.SetSize(dwPageCount); |
| 114 CPDF_Object* pNo = pLinearized->GetElement("P"); | 114 CPDF_Object* pNo = pLinearized->GetElement("P"); |
| 115 if (ToNumber(pNo)) | 115 if (ToNumber(pNo)) |
| 116 m_dwFirstPageNo = pNo->GetInteger(); | 116 m_dwFirstPageNo = pNo->GetInteger(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 129 } | 129 } |
| 130 if (m_pDocRender) { | 130 if (m_pDocRender) { |
| 131 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender); | 131 CPDF_ModuleMgr::Get()->GetRenderModule()->DestroyDocData(m_pDocRender); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 #define FX_MAX_PAGE_LEVEL 1024 | 134 #define FX_MAX_PAGE_LEVEL 1024 |
| 135 CPDF_Dictionary* CPDF_Document::_FindPDFPage(CPDF_Dictionary* pPages, | 135 CPDF_Dictionary* CPDF_Document::_FindPDFPage(CPDF_Dictionary* pPages, |
| 136 int iPage, | 136 int iPage, |
| 137 int nPagesToGo, | 137 int nPagesToGo, |
| 138 int level) { | 138 int level) { |
| 139 CPDF_Array* pKidList = pPages->GetArray("Kids"); | 139 CPDF_Array* pKidList = pPages->GetArrayBy("Kids"); |
| 140 if (!pKidList) { | 140 if (!pKidList) { |
| 141 if (nPagesToGo == 0) { | 141 if (nPagesToGo == 0) { |
| 142 return pPages; | 142 return pPages; |
| 143 } | 143 } |
| 144 return NULL; | 144 return NULL; |
| 145 } | 145 } |
| 146 if (level >= FX_MAX_PAGE_LEVEL) { | 146 if (level >= FX_MAX_PAGE_LEVEL) { |
| 147 return NULL; | 147 return NULL; |
| 148 } | 148 } |
| 149 int nKids = pKidList->GetCount(); | 149 int nKids = pKidList->GetCount(); |
| 150 for (int i = 0; i < nKids; i++) { | 150 for (int i = 0; i < nKids; i++) { |
| 151 CPDF_Dictionary* pKid = pKidList->GetDict(i); | 151 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 152 if (!pKid) { | 152 if (!pKid) { |
| 153 nPagesToGo--; | 153 nPagesToGo--; |
| 154 continue; | 154 continue; |
| 155 } | 155 } |
| 156 if (pKid == pPages) { | 156 if (pKid == pPages) { |
| 157 continue; | 157 continue; |
| 158 } | 158 } |
| 159 if (!pKid->KeyExist("Kids")) { | 159 if (!pKid->KeyExist("Kids")) { |
| 160 if (nPagesToGo == 0) { | 160 if (nPagesToGo == 0) { |
| 161 return pKid; | 161 return pKid; |
| 162 } | 162 } |
| 163 m_PageList.SetAt(iPage - nPagesToGo, pKid->GetObjNum()); | 163 m_PageList.SetAt(iPage - nPagesToGo, pKid->GetObjNum()); |
| 164 nPagesToGo--; | 164 nPagesToGo--; |
| 165 } else { | 165 } else { |
| 166 int nPages = pKid->GetInteger("Count"); | 166 int nPages = pKid->GetIntegerBy("Count"); |
| 167 if (nPagesToGo < nPages) { | 167 if (nPagesToGo < nPages) { |
| 168 return _FindPDFPage(pKid, iPage, nPagesToGo, level + 1); | 168 return _FindPDFPage(pKid, iPage, nPagesToGo, level + 1); |
| 169 } | 169 } |
| 170 nPagesToGo -= nPages; | 170 nPagesToGo -= nPages; |
| 171 } | 171 } |
| 172 } | 172 } |
| 173 return NULL; | 173 return NULL; |
| 174 } | 174 } |
| 175 | 175 |
| 176 CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { | 176 CPDF_Dictionary* CPDF_Document::GetPage(int iPage) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 187 int objnum = m_PageList.GetAt(iPage); | 187 int objnum = m_PageList.GetAt(iPage); |
| 188 if (objnum) { | 188 if (objnum) { |
| 189 if (CPDF_Dictionary* pDict = ToDictionary(GetIndirectObject(objnum))) | 189 if (CPDF_Dictionary* pDict = ToDictionary(GetIndirectObject(objnum))) |
| 190 return pDict; | 190 return pDict; |
| 191 } | 191 } |
| 192 | 192 |
| 193 CPDF_Dictionary* pRoot = GetRoot(); | 193 CPDF_Dictionary* pRoot = GetRoot(); |
| 194 if (!pRoot) | 194 if (!pRoot) |
| 195 return nullptr; | 195 return nullptr; |
| 196 | 196 |
| 197 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); | 197 CPDF_Dictionary* pPages = pRoot->GetDictBy("Pages"); |
| 198 if (!pPages) | 198 if (!pPages) |
| 199 return nullptr; | 199 return nullptr; |
| 200 | 200 |
| 201 CPDF_Dictionary* pPage = _FindPDFPage(pPages, iPage, iPage, 0); | 201 CPDF_Dictionary* pPage = _FindPDFPage(pPages, iPage, iPage, 0); |
| 202 if (!pPage) | 202 if (!pPage) |
| 203 return nullptr; | 203 return nullptr; |
| 204 | 204 |
| 205 m_PageList.SetAt(iPage, pPage->GetObjNum()); | 205 m_PageList.SetAt(iPage, pPage->GetObjNum()); |
| 206 return pPage; | 206 return pPage; |
| 207 } | 207 } |
| 208 | 208 |
| 209 int CPDF_Document::_FindPageIndex(CPDF_Dictionary* pNode, | 209 int CPDF_Document::_FindPageIndex(CPDF_Dictionary* pNode, |
| 210 FX_DWORD& skip_count, | 210 FX_DWORD& skip_count, |
| 211 FX_DWORD objnum, | 211 FX_DWORD objnum, |
| 212 int& index, | 212 int& index, |
| 213 int level) { | 213 int level) { |
| 214 if (pNode->KeyExist("Kids")) { | 214 if (pNode->KeyExist("Kids")) { |
| 215 CPDF_Array* pKidList = pNode->GetArray("Kids"); | 215 CPDF_Array* pKidList = pNode->GetArrayBy("Kids"); |
| 216 if (!pKidList) { | 216 if (!pKidList) { |
| 217 return -1; | 217 return -1; |
| 218 } | 218 } |
| 219 if (level >= FX_MAX_PAGE_LEVEL) { | 219 if (level >= FX_MAX_PAGE_LEVEL) { |
| 220 return -1; | 220 return -1; |
| 221 } | 221 } |
| 222 FX_DWORD count = pNode->GetInteger("Count"); | 222 FX_DWORD count = pNode->GetIntegerBy("Count"); |
| 223 if (count <= skip_count) { | 223 if (count <= skip_count) { |
| 224 skip_count -= count; | 224 skip_count -= count; |
| 225 index += count; | 225 index += count; |
| 226 return -1; | 226 return -1; |
| 227 } | 227 } |
| 228 if (count && count == pKidList->GetCount()) { | 228 if (count && count == pKidList->GetCount()) { |
| 229 for (FX_DWORD i = 0; i < count; i++) { | 229 for (FX_DWORD i = 0; i < count; i++) { |
| 230 if (CPDF_Reference* pKid = ToReference(pKidList->GetElement(i))) { | 230 if (CPDF_Reference* pKid = ToReference(pKidList->GetElement(i))) { |
| 231 if (pKid->GetRefObjNum() == objnum) { | 231 if (pKid->GetRefObjNum() == objnum) { |
| 232 m_PageList.SetAt(index + i, objnum); | 232 m_PageList.SetAt(index + i, objnum); |
| 233 return index + i; | 233 return index + i; |
| 234 } | 234 } |
| 235 } | 235 } |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { | 238 for (FX_DWORD i = 0; i < pKidList->GetCount(); i++) { |
| 239 CPDF_Dictionary* pKid = pKidList->GetDict(i); | 239 CPDF_Dictionary* pKid = pKidList->GetDictAt(i); |
| 240 if (!pKid) { | 240 if (!pKid) { |
| 241 continue; | 241 continue; |
| 242 } | 242 } |
| 243 if (pKid == pNode) { | 243 if (pKid == pNode) { |
| 244 continue; | 244 continue; |
| 245 } | 245 } |
| 246 int found_index = | 246 int found_index = |
| 247 _FindPageIndex(pKid, skip_count, objnum, index, level + 1); | 247 _FindPageIndex(pKid, skip_count, objnum, index, level + 1); |
| 248 if (found_index >= 0) { | 248 if (found_index >= 0) { |
| 249 return found_index; | 249 return found_index; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 271 } | 271 } |
| 272 if (!bSkipped && objnum1 == 0) { | 272 if (!bSkipped && objnum1 == 0) { |
| 273 skip_count = i; | 273 skip_count = i; |
| 274 bSkipped = TRUE; | 274 bSkipped = TRUE; |
| 275 } | 275 } |
| 276 } | 276 } |
| 277 CPDF_Dictionary* pRoot = GetRoot(); | 277 CPDF_Dictionary* pRoot = GetRoot(); |
| 278 if (!pRoot) { | 278 if (!pRoot) { |
| 279 return -1; | 279 return -1; |
| 280 } | 280 } |
| 281 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); | 281 CPDF_Dictionary* pPages = pRoot->GetDictBy("Pages"); |
| 282 if (!pPages) { | 282 if (!pPages) { |
| 283 return -1; | 283 return -1; |
| 284 } | 284 } |
| 285 int index = 0; | 285 int index = 0; |
| 286 return _FindPageIndex(pPages, skip_count, objnum, index); | 286 return _FindPageIndex(pPages, skip_count, objnum, index); |
| 287 } | 287 } |
| 288 int CPDF_Document::GetPageCount() const { | 288 int CPDF_Document::GetPageCount() const { |
| 289 return m_PageList.GetSize(); | 289 return m_PageList.GetSize(); |
| 290 } | 290 } |
| 291 | 291 |
| 292 int CPDF_Document::RetrievePageCount() const { | 292 int CPDF_Document::RetrievePageCount() const { |
| 293 CPDF_Dictionary* pRoot = GetRoot(); | 293 CPDF_Dictionary* pRoot = GetRoot(); |
| 294 if (!pRoot) { | 294 if (!pRoot) { |
| 295 return 0; | 295 return 0; |
| 296 } | 296 } |
| 297 CPDF_Dictionary* pPages = pRoot->GetDict("Pages"); | 297 CPDF_Dictionary* pPages = pRoot->GetDictBy("Pages"); |
| 298 if (!pPages) { | 298 if (!pPages) { |
| 299 return 0; | 299 return 0; |
| 300 } | 300 } |
| 301 if (!pPages->KeyExist("Kids")) { | 301 if (!pPages->KeyExist("Kids")) { |
| 302 return 1; | 302 return 1; |
| 303 } | 303 } |
| 304 std::set<CPDF_Dictionary*> visited_pages; | 304 std::set<CPDF_Dictionary*> visited_pages; |
| 305 visited_pages.insert(pPages); | 305 visited_pages.insert(pPages); |
| 306 return CountPages(pPages, &visited_pages); | 306 return CountPages(pPages, &visited_pages); |
| 307 } | 307 } |
| 308 | 308 |
| 309 FX_DWORD CPDF_Document::GetUserPermissions(FX_BOOL bCheckRevision) const { | 309 FX_DWORD CPDF_Document::GetUserPermissions(FX_BOOL bCheckRevision) const { |
| 310 if (!m_pParser) { | 310 if (!m_pParser) { |
| 311 return (FX_DWORD)-1; | 311 return (FX_DWORD)-1; |
| 312 } | 312 } |
| 313 return m_pParser->GetPermissions(bCheckRevision); | 313 return m_pParser->GetPermissions(bCheckRevision); |
| 314 } | 314 } |
| 315 | 315 |
| 316 FX_BOOL CPDF_Document::IsOwner() const { | 316 FX_BOOL CPDF_Document::IsOwner() const { |
| 317 return !m_pParser || m_pParser->IsOwner(); | 317 return !m_pParser || m_pParser->IsOwner(); |
| 318 } | 318 } |
| 319 | 319 |
| 320 FX_BOOL CPDF_Document::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const { | 320 FX_BOOL CPDF_Document::IsFormStream(FX_DWORD objnum, FX_BOOL& bForm) const { |
| 321 auto it = m_IndirectObjs.find(objnum); | 321 auto it = m_IndirectObjs.find(objnum); |
| 322 if (it != m_IndirectObjs.end()) { | 322 if (it != m_IndirectObjs.end()) { |
| 323 CPDF_Stream* pStream = it->second->AsStream(); | 323 CPDF_Stream* pStream = it->second->AsStream(); |
| 324 bForm = pStream && pStream->GetDict()->GetString("Subtype") == "Form"; | 324 bForm = pStream && pStream->GetDict()->GetStringBy("Subtype") == "Form"; |
| 325 return TRUE; | 325 return TRUE; |
| 326 } | 326 } |
| 327 if (!m_pParser) { | 327 if (!m_pParser) { |
| 328 bForm = FALSE; | 328 bForm = FALSE; |
| 329 return TRUE; | 329 return TRUE; |
| 330 } | 330 } |
| 331 return m_pParser->IsFormStream(objnum, bForm); | 331 return m_pParser->IsFormStream(objnum, bForm); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void CPDF_Document::ClearPageData() { | 334 void CPDF_Document::ClearPageData() { |
| 335 if (m_pDocPage) | 335 if (m_pDocPage) |
| 336 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this); | 336 CPDF_ModuleMgr::Get()->GetPageModule()->ClearDoc(this); |
| 337 } | 337 } |
| 338 | 338 |
| 339 void CPDF_Document::ClearRenderData() { | 339 void CPDF_Document::ClearRenderData() { |
| 340 if (m_pDocRender) | 340 if (m_pDocRender) |
| 341 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender); | 341 CPDF_ModuleMgr::Get()->GetRenderModule()->ClearDocData(m_pDocRender); |
| 342 } | 342 } |
| OLD | NEW |