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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <rtl/ref.hxx>
#include <rtl/string.h>
#include <rtl/ustrbuf.hxx>
#include <rtl/ustring.h>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <sal/types.h>
#include <o3tl/string_view.hxx>
#include "additions.hxx"
#include "data.hxx"
#include "node.hxx"
#include "nodemap.hxx"
#include "rootnode.hxx"
#include "setnode.hxx"
namespace configmgr {
namespace {
bool decode(
std::u16string_view encoded, std::size_t begin, std::size_t end,
OUString * decoded)
{
assert(
begin <= end && end <= encoded.size() &&
decoded != nullptr);
OUStringBuffer buf(end - begin);
while (begin != end) {
sal_Unicode c = encoded[begin++];
if (c == '&') {
if (o3tl::starts_with(encoded.substr(begin), u"amp;")) {
buf.append('&');
begin += RTL_CONSTASCII_LENGTH("amp;");
} else if (o3tl::starts_with(encoded.substr(begin), u"quot;")) {
buf.append('"');
begin += RTL_CONSTASCII_LENGTH("quot;");
} else if (o3tl::starts_with(encoded.substr(begin), u"apos;")) {
buf.append('\'');
begin += RTL_CONSTASCII_LENGTH("apos;");
} else {
return false;
}
assert(begin <= end);
} else {
buf.append(c);
}
}
*decoded = buf.makeStringAndClear();
return true;
}
}
OUString Data::createSegment(
std::u16string_view templateName, OUString const & name)
{
if (templateName.empty()) {
return name;
}
OUStringBuffer buf(128);
//TODO: verify template name contains no bad chars?
buf.append(OUString::Concat(templateName) + "['");
for (sal_Int32 i = 0; i < name.getLength(); ++i) {
sal_Unicode c = name[i];
switch (c) {
case '&':
buf.append("&");
break;
case '"':
buf.append(""");
break;
case '\'':
buf.append("'");
break;
default:
buf.append(c);
break;
}
}
buf.append("']");
return buf.makeStringAndClear();
}
sal_Int32 Data::parseSegment(
OUString const & path, sal_Int32 index, OUString * name,
bool * setElement, OUString * templateName)
{
assert(
index >= 0 && index <= path.getLength() && name != nullptr &&
setElement != nullptr);
sal_Int32 i = index;
while (i < path.getLength() && path[i] != '/' && path[i] != '[') {
++i;
}
if (i == path.getLength() || path[i] == '/') {
*name = path.copy(index, i - index);
*setElement = false;
return i;
}
if (templateName != nullptr) {
if (i - index == 1 && path[index] == '*') {
templateName->clear();
} else {
*templateName = path.copy(index, i - index);
}
}
if (++i == path.getLength()) {
return -1;
}
sal_Unicode del = path[i++];
if (del != '\'' && del != '"') {
return -1;
}
sal_Int32 j = path.indexOf(del, i);
if (j == -1 || j + 1 == path.getLength() || path[j + 1] != ']' ||
!decode(path, i, j, name))
{
return -1;
}
*setElement = true;
return j + 2;
}
OUString Data::fullTemplateName(
std::u16string_view component, std::u16string_view name)
{
if (component.find(':') != std::u16string_view::npos || name.find(':') != std::u16string_view::npos) {
throw css::uno::RuntimeException(
OUString::Concat("bad component/name pair containing colon ") + component + "/" +
name);
}
return OUString::Concat(component) + ":" + name;
}
bool Data::equalTemplateNames(
OUString const & shortName, OUString const & longName)
{
if (shortName.indexOf(':') == -1) {
sal_Int32 i = longName.indexOf(':') + 1;
assert(i > 0);
return
rtl_ustr_compare_WithLength(
shortName.getStr(), shortName.getLength(),
longName.getStr() + i, longName.getLength() - i) ==
0;
} else {
return shortName == longName;
}
}
Data::Data(): root_(new RootNode) {}
rtl::Reference< Node > Data::resolvePathRepresentation(
OUString const & pathRepresentation,
OUString * canonicRepresentation, std::vector<OUString> * path, int * finalizedLayer)
const
{
if (pathRepresentation.isEmpty() || pathRepresentation[0] != '/') {
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
if (path != nullptr) {
path->clear();
}
if (pathRepresentation == "/") {
if (canonicRepresentation != nullptr) {
*canonicRepresentation = pathRepresentation;
}
if (finalizedLayer != nullptr) {
*finalizedLayer = NO_LAYER;
}
return root_;
}
OUString seg;
bool setElement;
OUString templateName;
sal_Int32 n = parseSegment(pathRepresentation, 1, &seg, &setElement, nullptr);
if (n == -1 || setElement)
{
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
NodeMap const & components = getComponents();
NodeMap::const_iterator i(components.find(seg));
OUStringBuffer canonic(128);
rtl::Reference< Node > parent;
int finalized = NO_LAYER;
for (rtl::Reference< Node > p(i == components.end() ? nullptr : i->second);;) {
if (!p.is()) {
return p;
}
if (canonicRepresentation != nullptr) {
canonic.append('/');
canonic.append(createSegment(templateName, seg));
}
if (path != nullptr) {
path->push_back(seg);
}
finalized = std::min(finalized, p->getFinalized());
if (n != pathRepresentation.getLength() &&
pathRepresentation[n++] != '/')
{
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
// for backwards compatibility, ignore a final slash
if (n == pathRepresentation.getLength()) {
if (canonicRepresentation != nullptr) {
*canonicRepresentation = canonic.makeStringAndClear();
}
if (finalizedLayer != nullptr) {
*finalizedLayer = finalized;
}
return p;
}
parent = p;
templateName.clear();
n = parseSegment(
pathRepresentation, n, &seg, &setElement, &templateName);
if (n == -1) {
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
// For backwards compatibility, allow set members to be accessed with
// simple path segments, like group members:
p = p->getMember(seg);
if (setElement) {
switch (parent->kind()) {
case Node::KIND_LOCALIZED_PROPERTY:
if (!templateName.isEmpty()) {
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
break;
case Node::KIND_SET:
if (!templateName.isEmpty() &&
!static_cast< SetNode * >(parent.get())->isValidTemplate(
templateName))
{
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
break;
default:
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
if (!templateName.isEmpty() && p != nullptr) {
assert(!p->getTemplateName().isEmpty());
if (!equalTemplateNames(templateName, p->getTemplateName())) {
throw css::uno::RuntimeException(
"bad path " + pathRepresentation);
}
}
}
}
}
rtl::Reference< Node > Data::getTemplate(
int layer, OUString const & fullName) const
{
return templates.findNode(layer, fullName);
}
NodeMap & Data::getComponents() const {
return root_->getMembers();
}
Additions * Data::addExtensionXcuAdditions(
OUString const & url, int layer)
{
rtl::Reference item(new ExtensionXcu);
ExtensionXcuAdditions::iterator i(
extensionXcuAdditions_.emplace(
url, rtl::Reference< ExtensionXcu >()).first);
if (i->second.is()) {
throw css::uno::RuntimeException(
"already added extension xcu " + url);
}
i->second = item;
item->layer = layer;
return &item->additions;
}
rtl::Reference< Data::ExtensionXcu > Data::removeExtensionXcuAdditions(
OUString const & url)
{
ExtensionXcuAdditions::iterator i(extensionXcuAdditions_.find(url));
if (i == extensionXcuAdditions_.end()) {
// This can happen, as migration of pre OOo 3.3 UserInstallation
// extensions in dp_registry::backend::configuration::BackendImpl::
// PackageImpl::processPackage_ can cause just-in-time creation of
// extension xcu files that are never added via addExtensionXcuAdditions
// (also, there might be url spelling differences between calls to
// addExtensionXcuAdditions and removeExtensionXcuAdditions?):
SAL_INFO(
"configmgr",
"unknown Data::removeExtensionXcuAdditions(" << url << ")");
return rtl::Reference< ExtensionXcu >();
}
rtl::Reference< ExtensionXcu > item(i->second);
extensionXcuAdditions_.erase(i);
return item;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|