summaryrefslogtreecommitdiff
path: root/core/fpdfdoc/cpdf_numbertree.cpp
blob: 36d18c2d19c42cc50feb260111f3de1c1613ee13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fpdfdoc/cpdf_numbertree.h"

#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"

namespace {

CPDF_Object* SearchNumberNode(const CPDF_Dictionary* pNode, int num) {
  CPDF_Array* pLimits = pNode->GetArrayFor("Limits");
  if (pLimits &&
      (num < pLimits->GetIntegerAt(0) || num > pLimits->GetIntegerAt(1))) {
    return nullptr;
  }
  CPDF_Array* pNumbers = pNode->GetArrayFor("Nums");
  if (pNumbers) {
    for (size_t i = 0; i < pNumbers->GetCount() / 2; i++) {
      int index = pNumbers->GetIntegerAt(i * 2);
      if (num == index)
        return pNumbers->GetDirectObjectAt(i * 2 + 1);
      if (index > num)
        break;
    }
    return nullptr;
  }

  CPDF_Array* pKids = pNode->GetArrayFor("Kids");
  if (!pKids)
    return nullptr;

  for (size_t i = 0; i < pKids->GetCount(); i++) {
    CPDF_Dictionary* pKid = pKids->GetDictAt(i);
    if (!pKid)
      continue;

    CPDF_Object* pFound = SearchNumberNode(pKid, num);
    if (pFound)
      return pFound;
  }
  return nullptr;
}

}  // namespace

CPDF_Object* CPDF_NumberTree::LookupValue(int num) const {
  return SearchNumberNode(m_pRoot, num);
}