Spaces:
Sleeping
Sleeping
File size: 6,985 Bytes
5cee033 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
//========================================================================
//
// Dict.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <[email protected]>
// Copyright (C) 2006 Krzysztof Kowalczyk <[email protected]>
// Copyright (C) 2007-2008 Julien Rebetez <[email protected]>
// Copyright (C) 2008, 2010, 2013, 2014, 2017, 2019, 2020, 2022 Albert Astals Cid <[email protected]>
// Copyright (C) 2010 Paweł Wiejacha <[email protected]>
// Copyright (C) 2012 Fabio D'Urso <[email protected]>
// Copyright (C) 2013 Thomas Freitag <[email protected]>
// Copyright (C) 2014 Scott West <[email protected]>
// Copyright (C) 2017 Adrian Johnson <[email protected]>
// Copyright (C) 2018 Adam Reichold <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include <algorithm>
#include "XRef.h"
#include "Dict.h"
//------------------------------------------------------------------------
// Dict
//------------------------------------------------------------------------
#define dictLocker() const std::scoped_lock locker(mutex)
constexpr int SORT_LENGTH_LOWER_LIMIT = 32;
struct Dict::CmpDictEntry
{
bool operator()(const DictEntry &lhs, const DictEntry &rhs) const { return lhs.first < rhs.first; }
bool operator()(const DictEntry &lhs, const char *rhs) const { return lhs.first < rhs; }
bool operator()(const char *lhs, const DictEntry &rhs) const { return lhs < rhs.first; }
};
Dict::Dict(XRef *xrefA)
{
xref = xrefA;
ref = 1;
sorted = false;
}
Dict::Dict(const Dict *dictA)
{
xref = dictA->xref;
ref = 1;
entries.reserve(dictA->entries.size());
for (const auto &entry : dictA->entries) {
entries.emplace_back(entry.first, entry.second.copy());
}
sorted = dictA->sorted.load();
}
Dict *Dict::copy(XRef *xrefA) const
{
dictLocker();
Dict *dictA = new Dict(this);
dictA->xref = xrefA;
for (auto &entry : dictA->entries) {
if (entry.second.getType() == objDict) {
entry.second = Object(entry.second.getDict()->copy(xrefA));
}
}
return dictA;
}
Dict *Dict::deepCopy() const
{
dictLocker();
Dict *dictA = new Dict(xref);
dictA->entries.reserve(entries.size());
for (auto &entry : entries) {
dictA->entries.emplace_back(entry.first, entry.second.deepCopy());
}
return dictA;
}
void Dict::add(const char *key, Object &&val)
{
dictLocker();
entries.emplace_back(key, std::move(val));
sorted = false;
}
inline const Dict::DictEntry *Dict::find(const char *key) const
{
if (entries.size() >= SORT_LENGTH_LOWER_LIMIT) {
if (!sorted) {
dictLocker();
if (!sorted) {
Dict *that = const_cast<Dict *>(this);
std::sort(that->entries.begin(), that->entries.end(), CmpDictEntry {});
that->sorted = true;
}
}
}
if (sorted) {
const auto pos = std::lower_bound(entries.begin(), entries.end(), key, CmpDictEntry {});
if (pos != entries.end() && pos->first == key) {
return &*pos;
}
} else {
const auto pos = std::find_if(entries.rbegin(), entries.rend(), [key](const DictEntry &entry) { return entry.first == key; });
if (pos != entries.rend()) {
return &*pos;
}
}
return nullptr;
}
inline Dict::DictEntry *Dict::find(const char *key)
{
return const_cast<DictEntry *>(const_cast<const Dict *>(this)->find(key));
}
void Dict::remove(const char *key)
{
dictLocker();
if (auto *entry = find(key)) {
if (sorted) {
const auto index = entry - &entries.front();
entries.erase(entries.begin() + index);
} else {
swap(*entry, entries.back());
entries.pop_back();
}
}
}
void Dict::set(const char *key, Object &&val)
{
if (val.isNull()) {
remove(key);
return;
}
dictLocker();
if (auto *entry = find(key)) {
entry->second = std::move(val);
} else {
add(key, std::move(val));
}
}
bool Dict::is(const char *type) const
{
if (const auto *entry = find("Type")) {
return entry->second.isName(type);
}
return false;
}
Object Dict::lookup(const char *key, int recursion) const
{
if (const auto *entry = find(key)) {
return entry->second.fetch(xref, recursion);
}
return Object(objNull);
}
Object Dict::lookup(const char *key, Ref *returnRef, int recursion) const
{
if (const auto *entry = find(key)) {
if (entry->second.getType() == objRef) {
*returnRef = entry->second.getRef();
} else {
*returnRef = Ref::INVALID();
}
return entry->second.fetch(xref, recursion);
}
*returnRef = Ref::INVALID();
return Object(objNull);
}
Object Dict::lookupEnsureEncryptedIfNeeded(const char *key) const
{
const auto *entry = find(key);
if (!entry) {
return Object(objNull);
}
if (entry->second.getType() == objRef && xref->isEncrypted() && !xref->isRefEncrypted(entry->second.getRef())) {
error(errSyntaxError, -1, "{0:s} is not encrypted and the document is. This may be a hacking attempt", key);
return Object(objNull);
}
return entry->second.fetch(xref);
}
const Object &Dict::lookupNF(const char *key) const
{
if (const auto *entry = find(key)) {
return entry->second;
}
static Object nullObj(objNull);
return nullObj;
}
bool Dict::lookupInt(const char *key, const char *alt_key, int *value) const
{
auto obj1 = lookup(key);
if (obj1.isNull() && alt_key != nullptr) {
obj1 = lookup(alt_key);
}
if (obj1.isInt()) {
*value = obj1.getInt();
return true;
}
return false;
}
Object Dict::getVal(int i, Ref *returnRef) const
{
const DictEntry &entry = entries[i];
if (entry.second.getType() == objRef) {
*returnRef = entry.second.getRef();
} else {
*returnRef = Ref::INVALID();
}
return entry.second.fetch(xref);
}
bool Dict::hasKey(const char *key) const
{
return find(key) != nullptr;
}
std::string Dict::findAvailableKey(const std::string &suggestedKey)
{
int i = 0;
std::string res = suggestedKey;
while (find(res.c_str())) {
res = suggestedKey + std::to_string(i++);
}
return res;
}
|