Coverage Report

Created: 2026-07-16 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/Dict.cc
Line
Count
Source
1
//========================================================================
2
//
3
// Dict.cc
4
//
5
// Copyright 1996-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stddef.h>
12
#include <string.h>
13
#include "gmem.h"
14
#include "gmempp.h"
15
#include "Object.h"
16
#include "XRef.h"
17
#include "Dict.h"
18
19
//------------------------------------------------------------------------
20
21
struct DictEntry {
22
  char *key;
23
  Object val;
24
  DictEntry *next;
25
};
26
27
//------------------------------------------------------------------------
28
// Dict
29
//------------------------------------------------------------------------
30
31
827k
Dict::Dict(XRef *xrefA) {
32
827k
  xref = xrefA;
33
827k
  size = 8;
34
827k
  length = 0;
35
827k
  entries = (DictEntry *)gmallocn(size, sizeof(DictEntry));
36
827k
  hashTab = (DictEntry **)gmallocn(2 * size - 1, sizeof(DictEntry *));
37
827k
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
38
827k
  ref = 1;
39
827k
}
40
41
826k
Dict::~Dict() {
42
826k
  int i;
43
44
3.89M
  for (i = 0; i < length; ++i) {
45
3.07M
    gfree(entries[i].key);
46
3.07M
    entries[i].val.free();
47
3.07M
  }
48
826k
  gfree(entries);
49
826k
  gfree(hashTab);
50
826k
}
51
52
3.84M
void Dict::add(char *key, Object *val) {
53
3.84M
  DictEntry *e;
54
3.84M
  int h;
55
56
3.84M
  if ((e = find(key))) {
57
768k
    e->val.free();
58
768k
    e->val = *val;
59
768k
    gfree(key);
60
3.07M
  } else {
61
3.07M
    if (length == size) {
62
121k
      expand();
63
121k
    }
64
3.07M
    h = hash(key);
65
3.07M
    entries[length].key = key;
66
3.07M
    entries[length].val = *val;
67
3.07M
    entries[length].next = hashTab[h];
68
3.07M
    hashTab[h] = &entries[length];
69
3.07M
    ++length;
70
3.07M
  }
71
3.84M
}
72
73
121k
void Dict::expand() {
74
121k
  int h, i;
75
76
121k
  size *= 2;
77
121k
  entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry));
78
121k
  hashTab = (DictEntry **)greallocn(hashTab, 2 * size - 1,
79
121k
            sizeof(DictEntry *));
80
121k
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
81
1.15M
  for (i = 0; i < length; ++i) {
82
1.03M
    h = hash(entries[i].key);
83
1.03M
    entries[i].next = hashTab[h];
84
1.03M
    hashTab[h] = &entries[i];
85
1.03M
  }
86
121k
}
87
88
9.41M
inline DictEntry *Dict::find(const char *key) {
89
9.41M
  DictEntry *e;
90
9.41M
  int h;
91
92
9.41M
  h = hash(key);
93
11.9M
  for (e = hashTab[h]; e; e = e->next) {
94
4.61M
    if (!strcmp(key, e->key)) {
95
2.11M
      return e;
96
2.11M
    }
97
4.61M
  }
98
7.29M
  return NULL;
99
9.41M
}
100
101
13.5M
int Dict::hash(const char *key) {
102
13.5M
  const char *p;
103
13.5M
  unsigned int h;
104
105
13.5M
  h = 0;
106
86.0M
  for (p = key; *p; ++p) {
107
72.5M
    h = 17 * h + (int)(*p & 0xff);
108
72.5M
  }
109
13.5M
  return (int)(h % (2 * size - 1));
110
13.5M
}
111
112
0
GBool Dict::is(const char *type) {
113
0
  DictEntry *e;
114
115
0
  return (e = find("Type")) && e->val.isName(type);
116
0
}
117
118
4.87M
Object *Dict::lookup(const char *key, Object *obj, int recursion) {
119
4.87M
  DictEntry *e;
120
121
4.87M
  return (e = find(key)) ? e->val.fetch(xref, obj, recursion)
122
4.87M
                         : obj->initNull();
123
4.87M
}
124
125
700k
Object *Dict::lookupNF(const char *key, Object *obj) {
126
700k
  DictEntry *e;
127
128
700k
  return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
129
700k
}
130
131
194k
char *Dict::getKey(int i) {
132
194k
  return entries[i].key;
133
194k
}
134
135
175k
Object *Dict::getVal(int i, Object *obj) {
136
175k
  return entries[i].val.fetch(xref, obj);
137
175k
}
138
139
19.5k
Object *Dict::getValNF(int i, Object *obj) {
140
19.5k
  return entries[i].val.copy(obj);
141
19.5k
}