| 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/fpdfapi/fpdf_parser/include/cpdf_array.h" | 7 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 8 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 8 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 9 #include "core/fpdfdoc/doc_utils.h" | 9 #include "core/fpdfdoc/doc_utils.h" |
| 10 #include "core/fpdfdoc/include/fpdf_doc.h" | 10 #include "core/fpdfdoc/include/fpdf_doc.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 CFX_ByteString CPDF_Dest::GetRemoteName() { | 71 CFX_ByteString CPDF_Dest::GetRemoteName() { |
| 72 return m_pObj ? m_pObj->GetString() : CFX_ByteString(); | 72 return m_pObj ? m_pObj->GetString() : CFX_ByteString(); |
| 73 } | 73 } |
| 74 CPDF_NameTree::CPDF_NameTree(CPDF_Document* pDoc, | 74 CPDF_NameTree::CPDF_NameTree(CPDF_Document* pDoc, |
| 75 const CFX_ByteStringC& category) { | 75 const CFX_ByteStringC& category) { |
| 76 if (pDoc->GetRoot() && pDoc->GetRoot()->GetDictBy("Names")) | 76 if (pDoc->GetRoot() && pDoc->GetRoot()->GetDictBy("Names")) |
| 77 m_pRoot = pDoc->GetRoot()->GetDictBy("Names")->GetDictBy(category); | 77 m_pRoot = pDoc->GetRoot()->GetDictBy("Names")->GetDictBy(category); |
| 78 else | 78 else |
| 79 m_pRoot = NULL; | 79 m_pRoot = NULL; |
| 80 } | 80 } |
| 81 |
| 81 static CPDF_Object* SearchNameNode(CPDF_Dictionary* pNode, | 82 static CPDF_Object* SearchNameNode(CPDF_Dictionary* pNode, |
| 82 const CFX_ByteString& csName, | 83 const CFX_ByteString& csName, |
| 83 int& nIndex, | 84 size_t& nIndex, |
| 84 CPDF_Array** ppFind, | 85 CPDF_Array** ppFind, |
| 85 int nLevel = 0) { | 86 int nLevel = 0) { |
| 86 if (nLevel > nMaxRecursion) { | 87 if (nLevel > nMaxRecursion) { |
| 87 return NULL; | 88 return NULL; |
| 88 } | 89 } |
| 89 CPDF_Array* pLimits = pNode->GetArrayBy("Limits"); | 90 CPDF_Array* pLimits = pNode->GetArrayBy("Limits"); |
| 90 if (pLimits) { | 91 if (pLimits) { |
| 91 CFX_ByteString csLeft = pLimits->GetStringAt(0); | 92 CFX_ByteString csLeft = pLimits->GetStringAt(0); |
| 92 CFX_ByteString csRight = pLimits->GetStringAt(1); | 93 CFX_ByteString csRight = pLimits->GetStringAt(1); |
| 93 if (csLeft.Compare(csRight.AsByteStringC()) > 0) { | 94 if (csLeft.Compare(csRight.AsByteStringC()) > 0) { |
| 94 CFX_ByteString csTmp = csRight; | 95 CFX_ByteString csTmp = csRight; |
| 95 csRight = csLeft; | 96 csRight = csLeft; |
| 96 csLeft = csTmp; | 97 csLeft = csTmp; |
| 97 } | 98 } |
| 98 if (csName.Compare(csLeft.AsByteStringC()) < 0 || | 99 if (csName.Compare(csLeft.AsByteStringC()) < 0 || |
| 99 csName.Compare(csRight.AsByteStringC()) > 0) { | 100 csName.Compare(csRight.AsByteStringC()) > 0) { |
| 100 return NULL; | 101 return NULL; |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 CPDF_Array* pNames = pNode->GetArrayBy("Names"); | 104 CPDF_Array* pNames = pNode->GetArrayBy("Names"); |
| 104 if (pNames) { | 105 if (pNames) { |
| 105 uint32_t dwCount = pNames->GetCount() / 2; | 106 size_t dwCount = pNames->GetCount() / 2; |
| 106 for (uint32_t i = 0; i < dwCount; i++) { | 107 for (size_t i = 0; i < dwCount; i++) { |
| 107 CFX_ByteString csValue = pNames->GetStringAt(i * 2); | 108 CFX_ByteString csValue = pNames->GetStringAt(i * 2); |
| 108 int32_t iCompare = csValue.Compare(csName.AsByteStringC()); | 109 int32_t iCompare = csValue.Compare(csName.AsByteStringC()); |
| 109 if (iCompare <= 0) { | 110 if (iCompare <= 0) { |
| 110 if (ppFind) { | 111 if (ppFind) { |
| 111 *ppFind = pNames; | 112 *ppFind = pNames; |
| 112 } | 113 } |
| 113 if (iCompare < 0) { | 114 if (iCompare < 0) { |
| 114 continue; | 115 continue; |
| 115 } | 116 } |
| 116 } else { | 117 } else { |
| 117 break; | 118 break; |
| 118 } | 119 } |
| 119 nIndex += i; | 120 nIndex += i; |
| 120 return pNames->GetDirectObjectAt(i * 2 + 1); | 121 return pNames->GetDirectObjectAt(i * 2 + 1); |
| 121 } | 122 } |
| 122 nIndex += dwCount; | 123 nIndex += dwCount; |
| 123 return NULL; | 124 return NULL; |
| 124 } | 125 } |
| 125 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); | 126 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); |
| 126 if (!pKids) { | 127 if (!pKids) { |
| 127 return NULL; | 128 return NULL; |
| 128 } | 129 } |
| 129 for (uint32_t i = 0; i < pKids->GetCount(); i++) { | 130 for (size_t i = 0; i < pKids->GetCount(); i++) { |
| 130 CPDF_Dictionary* pKid = pKids->GetDictAt(i); | 131 CPDF_Dictionary* pKid = pKids->GetDictAt(i); |
| 131 if (!pKid) { | 132 if (!pKid) { |
| 132 continue; | 133 continue; |
| 133 } | 134 } |
| 134 CPDF_Object* pFound = | 135 CPDF_Object* pFound = |
| 135 SearchNameNode(pKid, csName, nIndex, ppFind, nLevel + 1); | 136 SearchNameNode(pKid, csName, nIndex, ppFind, nLevel + 1); |
| 136 if (pFound) { | 137 if (pFound) { |
| 137 return pFound; | 138 return pFound; |
| 138 } | 139 } |
| 139 } | 140 } |
| 140 return NULL; | 141 return NULL; |
| 141 } | 142 } |
| 143 |
| 142 static CPDF_Object* SearchNameNode(CPDF_Dictionary* pNode, | 144 static CPDF_Object* SearchNameNode(CPDF_Dictionary* pNode, |
| 143 int nIndex, | 145 size_t nIndex, |
| 144 int& nCurIndex, | 146 size_t& nCurIndex, |
| 145 CFX_ByteString& csName, | 147 CFX_ByteString& csName, |
| 146 CPDF_Array** ppFind, | 148 CPDF_Array** ppFind, |
| 147 int nLevel = 0) { | 149 int nLevel = 0) { |
| 148 if (nLevel > nMaxRecursion) { | 150 if (nLevel > nMaxRecursion) |
| 149 return NULL; | 151 return NULL; |
| 150 } | 152 |
| 151 CPDF_Array* pNames = pNode->GetArrayBy("Names"); | 153 CPDF_Array* pNames = pNode->GetArrayBy("Names"); |
| 152 if (pNames) { | 154 if (pNames) { |
| 153 int nCount = pNames->GetCount() / 2; | 155 size_t nCount = pNames->GetCount() / 2; |
| 154 if (nIndex >= nCurIndex + nCount) { | 156 if (nIndex >= nCurIndex + nCount) { |
| 155 nCurIndex += nCount; | 157 nCurIndex += nCount; |
| 156 return NULL; | 158 return NULL; |
| 157 } | 159 } |
| 158 if (ppFind) { | 160 if (ppFind) |
| 159 *ppFind = pNames; | 161 *ppFind = pNames; |
| 160 } | |
| 161 csName = pNames->GetStringAt((nIndex - nCurIndex) * 2); | 162 csName = pNames->GetStringAt((nIndex - nCurIndex) * 2); |
| 162 return pNames->GetDirectObjectAt((nIndex - nCurIndex) * 2 + 1); | 163 return pNames->GetDirectObjectAt((nIndex - nCurIndex) * 2 + 1); |
| 163 } | 164 } |
| 164 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); | 165 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); |
| 165 if (!pKids) { | 166 if (!pKids) |
| 166 return NULL; | 167 return NULL; |
| 167 } | 168 for (size_t i = 0; i < pKids->GetCount(); i++) { |
| 168 for (uint32_t i = 0; i < pKids->GetCount(); i++) { | |
| 169 CPDF_Dictionary* pKid = pKids->GetDictAt(i); | 169 CPDF_Dictionary* pKid = pKids->GetDictAt(i); |
| 170 if (!pKid) { | 170 if (!pKid) |
| 171 continue; | 171 continue; |
| 172 } | |
| 173 CPDF_Object* pFound = | 172 CPDF_Object* pFound = |
| 174 SearchNameNode(pKid, nIndex, nCurIndex, csName, ppFind, nLevel + 1); | 173 SearchNameNode(pKid, nIndex, nCurIndex, csName, ppFind, nLevel + 1); |
| 175 if (pFound) { | 174 if (pFound) |
| 176 return pFound; | 175 return pFound; |
| 177 } | |
| 178 } | 176 } |
| 179 return NULL; | 177 return NULL; |
| 180 } | 178 } |
| 181 static int CountNames(CPDF_Dictionary* pNode, int nLevel = 0) { | 179 |
| 180 static size_t CountNames(CPDF_Dictionary* pNode, int nLevel = 0) { |
| 182 if (nLevel > nMaxRecursion) { | 181 if (nLevel > nMaxRecursion) { |
| 183 return 0; | 182 return 0; |
| 184 } | 183 } |
| 185 CPDF_Array* pNames = pNode->GetArrayBy("Names"); | 184 CPDF_Array* pNames = pNode->GetArrayBy("Names"); |
| 186 if (pNames) { | 185 if (pNames) { |
| 187 return pNames->GetCount() / 2; | 186 return pNames->GetCount() / 2; |
| 188 } | 187 } |
| 189 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); | 188 CPDF_Array* pKids = pNode->GetArrayBy("Kids"); |
| 190 if (!pKids) { | 189 if (!pKids) { |
| 191 return 0; | 190 return 0; |
| 192 } | 191 } |
| 193 int nCount = 0; | 192 size_t nCount = 0; |
| 194 for (uint32_t i = 0; i < pKids->GetCount(); i++) { | 193 for (size_t i = 0; i < pKids->GetCount(); i++) { |
| 195 CPDF_Dictionary* pKid = pKids->GetDictAt(i); | 194 CPDF_Dictionary* pKid = pKids->GetDictAt(i); |
| 196 if (!pKid) { | 195 if (!pKid) { |
| 197 continue; | 196 continue; |
| 198 } | 197 } |
| 199 nCount += CountNames(pKid, nLevel + 1); | 198 nCount += CountNames(pKid, nLevel + 1); |
| 200 } | 199 } |
| 201 return nCount; | 200 return nCount; |
| 202 } | 201 } |
| 203 int CPDF_NameTree::GetCount() const { | 202 |
| 203 size_t CPDF_NameTree::GetCount() const { |
| 204 if (!m_pRoot) { | 204 if (!m_pRoot) { |
| 205 return 0; | 205 return 0; |
| 206 } | 206 } |
| 207 return ::CountNames(m_pRoot); | 207 return ::CountNames(m_pRoot); |
| 208 } | 208 } |
| 209 |
| 209 int CPDF_NameTree::GetIndex(const CFX_ByteString& csName) const { | 210 int CPDF_NameTree::GetIndex(const CFX_ByteString& csName) const { |
| 210 if (!m_pRoot) { | 211 if (!m_pRoot) { |
| 211 return -1; | 212 return -1; |
| 212 } | 213 } |
| 213 int nIndex = 0; | 214 size_t nIndex = 0; |
| 214 if (!SearchNameNode(m_pRoot, csName, nIndex, NULL)) { | 215 if (!SearchNameNode(m_pRoot, csName, nIndex, NULL)) { |
| 215 return -1; | 216 return -1; |
| 216 } | 217 } |
| 217 return nIndex; | 218 return nIndex; |
| 218 } | 219 } |
| 219 CPDF_Object* CPDF_NameTree::LookupValue(int nIndex, | 220 CPDF_Object* CPDF_NameTree::LookupValue(int nIndex, |
| 220 CFX_ByteString& csName) const { | 221 CFX_ByteString& csName) const { |
| 221 if (!m_pRoot) { | 222 if (!m_pRoot) { |
| 222 return NULL; | 223 return NULL; |
| 223 } | 224 } |
| 224 int nCurIndex = 0; | 225 size_t nCurIndex = 0; |
| 225 return SearchNameNode(m_pRoot, nIndex, nCurIndex, csName, NULL); | 226 return SearchNameNode(m_pRoot, nIndex, nCurIndex, csName, NULL); |
| 226 } | 227 } |
| 227 CPDF_Object* CPDF_NameTree::LookupValue(const CFX_ByteString& csName) const { | 228 CPDF_Object* CPDF_NameTree::LookupValue(const CFX_ByteString& csName) const { |
| 228 if (!m_pRoot) { | 229 if (!m_pRoot) { |
| 229 return NULL; | 230 return NULL; |
| 230 } | 231 } |
| 231 int nIndex = 0; | 232 size_t nIndex = 0; |
| 232 return SearchNameNode(m_pRoot, csName, nIndex, NULL); | 233 return SearchNameNode(m_pRoot, csName, nIndex, NULL); |
| 233 } | 234 } |
| 234 CPDF_Array* CPDF_NameTree::LookupNamedDest(CPDF_Document* pDoc, | 235 CPDF_Array* CPDF_NameTree::LookupNamedDest(CPDF_Document* pDoc, |
| 235 const CFX_ByteStringC& sName) { | 236 const CFX_ByteStringC& sName) { |
| 236 CPDF_Object* pValue = LookupValue(sName); | 237 CPDF_Object* pValue = LookupValue(sName); |
| 237 if (!pValue) { | 238 if (!pValue) { |
| 238 CPDF_Dictionary* pDests = pDoc->GetRoot()->GetDictBy("Dests"); | 239 CPDF_Dictionary* pDests = pDoc->GetRoot()->GetDictBy("Dests"); |
| 239 if (!pDests) | 240 if (!pDests) |
| 240 return nullptr; | 241 return nullptr; |
| 241 pValue = pDests->GetDirectObjectBy(sName); | 242 pValue = pDests->GetDirectObjectBy(sName); |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 bsLbl = bsOrig; | 509 bsLbl = bsOrig; |
| 509 int nPage = FXSYS_atoi(bsLbl); | 510 int nPage = FXSYS_atoi(bsLbl); |
| 510 if (nPage > 0 && nPage <= nPages) { | 511 if (nPage > 0 && nPage <= nPages) { |
| 511 return nPage; | 512 return nPage; |
| 512 } | 513 } |
| 513 return -1; | 514 return -1; |
| 514 } | 515 } |
| 515 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const { | 516 int32_t CPDF_PageLabel::GetPageByLabel(const CFX_WideStringC& wsLabel) const { |
| 516 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsByteStringC()); | 517 return GetPageByLabel(PDF_EncodeText(wsLabel.c_str()).AsByteStringC()); |
| 517 } | 518 } |
| OLD | NEW |