1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fpdfdoc/cpdf_numbertree.h"
8 
9 #include "core/fpdfapi/parser/cpdf_array.h"
10 #include "core/fpdfapi/parser/cpdf_dictionary.h"
11 
12 namespace {
13 
SearchNumberNode(const CPDF_Dictionary * pNode,int num)14 CPDF_Object* SearchNumberNode(const CPDF_Dictionary* pNode, int num) {
15   CPDF_Array* pLimits = pNode->GetArrayFor("Limits");
16   if (pLimits &&
17       (num < pLimits->GetIntegerAt(0) || num > pLimits->GetIntegerAt(1))) {
18     return nullptr;
19   }
20   CPDF_Array* pNumbers = pNode->GetArrayFor("Nums");
21   if (pNumbers) {
22     for (size_t i = 0; i < pNumbers->GetCount() / 2; i++) {
23       int index = pNumbers->GetIntegerAt(i * 2);
24       if (num == index)
25         return pNumbers->GetDirectObjectAt(i * 2 + 1);
26       if (index > num)
27         break;
28     }
29     return nullptr;
30   }
31 
32   CPDF_Array* pKids = pNode->GetArrayFor("Kids");
33   if (!pKids)
34     return nullptr;
35 
36   for (size_t i = 0; i < pKids->GetCount(); i++) {
37     CPDF_Dictionary* pKid = pKids->GetDictAt(i);
38     if (!pKid)
39       continue;
40 
41     CPDF_Object* pFound = SearchNumberNode(pKid, num);
42     if (pFound)
43       return pFound;
44   }
45   return nullptr;
46 }
47 
48 }  // namespace
49 
LookupValue(int num) const50 CPDF_Object* CPDF_NumberTree::LookupValue(int num) const {
51   return SearchNumberNode(m_pRoot, num);
52 }
53