Coverage Report

Created: 2026-10-17 07:11

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
1.12M
Dict::Dict(XRef *xrefA) {
32
1.12M
  xref = xrefA;
33
1.12M
  size = 8;
34
1.12M
  length = 0;
35
1.12M
  entries = (DictEntry *)gmallocn(size, sizeof(DictEntry));
36
1.12M
  hashTab = (DictEntry **)gmallocn(2 * size - 1, sizeof(DictEntry *));
37
1.12M
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
38
1.12M
  ref = 1;
39
1.12M
}
40
41
1.12M
Dict::~Dict() {
42
1.12M
  int i;
43
44
5.33M
  for (i = 0; i < length; ++i) {
45
4.20M
    gfree(entries[i].key);
46
4.20M
    entries[i].val.free();
47
4.20M
  }
48
1.12M
  gfree(entries);
49
1.12M
  gfree(hashTab);
50
1.12M
}
51
52
4.86M
void Dict::add(char *key, Object *val) {
53
4.86M
  DictEntry *e;
54
4.86M
  int h;
55
56
4.86M
  if ((e = find(key))) {
57
654k
    e->val.free();
58
654k
    e->val = *val;
59
654k
    gfree(key);
60
4.21M
  } else {
61
4.21M
    if (length == size) {
62
111k
      expand();
63
111k
    }
64
4.21M
    h = hash(key);
65
4.21M
    entries[length].key = key;
66
4.21M
    entries[length].val = *val;
67
4.21M
    entries[length].next = hashTab[h];
68
4.21M
    hashTab[h] = &entries[length];
69
4.21M
    ++length;
70
4.21M
  }
71
4.86M
}
72
73
111k
void Dict::expand() {
74
111k
  int h, i;
75
76
111k
  size *= 2;
77
111k
  entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry));
78
111k
  hashTab = (DictEntry **)greallocn(hashTab, 2 * size - 1,
79
111k
            sizeof(DictEntry *));
80
111k
  memset(hashTab, 0, (2 * size - 1) * sizeof(DictEntry *));
81
1.26M
  for (i = 0; i < length; ++i) {
82
1.15M
    h = hash(entries[i].key);
83
1.15M
    entries[i].next = hashTab[h];
84
1.15M
    hashTab[h] = &entries[i];
85
1.15M
  }
86
111k
}
87
88
22.1M
inline DictEntry *Dict::find(const char *key) {
89
22.1M
  DictEntry *e;
90
22.1M
  int h;
91
92
22.1M
  h = hash(key);
93
28.8M
  for (e = hashTab[h]; e; e = e->next) {
94
10.0M
    if (!strcmp(key, e->key)) {
95
3.29M
      return e;
96
3.29M
    }
97
10.0M
  }
98
18.8M
  return NULL;
99
22.1M
}
100
101
27.5M
int Dict::hash(const char *key) {
102
27.5M
  const char *p;
103
27.5M
  unsigned int h;
104
105
27.5M
  h = 0;
106
195M
  for (p = key; *p; ++p) {
107
168M
    h = 17 * h + (int)(*p & 0xff);
108
168M
  }
109
27.5M
  return (int)(h % (2 * size - 1));
110
27.5M
}
111
112
5.63k
GBool Dict::is(const char *type) {
113
5.63k
  DictEntry *e;
114
115
5.63k
  return (e = find("Type")) && e->val.isName(type);
116
5.63k
}
117
118
14.3M
Object *Dict::lookup(const char *key, Object *obj, int recursion) {
119
14.3M
  DictEntry *e;
120
121
14.3M
  return (e = find(key)) ? e->val.fetch(xref, obj, recursion)
122
14.3M
                         : obj->initNull();
123
14.3M
}
124
125
2.91M
Object *Dict::lookupNF(const char *key, Object *obj) {
126
2.91M
  DictEntry *e;
127
128
2.91M
  return (e = find(key)) ? e->val.copy(obj) : obj->initNull();
129
2.91M
}
130
131
163k
char *Dict::getKey(int i) {
132
163k
  return entries[i].key;
133
163k
}
134
135
36.9k
Object *Dict::getVal(int i, Object *obj) {
136
36.9k
  return entries[i].val.fetch(xref, obj);
137
36.9k
}
138
139
129k
Object *Dict::getValNF(int i, Object *obj) {
140
129k
  return entries[i].val.copy(obj);
141
129k
}