Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/cppuhelper/source/unourl.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
21
#include <cppuhelper/unourl.hxx>
22
23
#include <rtl/malformeduriexception.hxx>
24
#include <rtl/string.h>
25
#include <rtl/textenc.h>
26
#include <rtl/uri.h>
27
#include <rtl/uri.hxx>
28
#include <rtl/ustring.hxx>
29
#include <rtl/character.hxx>
30
#include <sal/types.h>
31
32
#include <map>
33
#include <memory>
34
#include <utility>
35
36
using cppu::UnoUrl;
37
using cppu::UnoUrlDescriptor;
38
39
class UnoUrlDescriptor::Impl
40
{
41
public:
42
    typedef std::map< OUString, OUString > Parameters;
43
44
    OUString m_aDescriptor;
45
    OUString m_aName;
46
    Parameters m_aParameters;
47
48
    /** @exception rtl::MalformedUriException
49
     */
50
    explicit inline Impl(OUString const & m_aDescriptor);
51
52
0
    Impl * clone() const { return new Impl(*this); }
53
};
54
55
inline UnoUrlDescriptor::Impl::Impl(OUString const & rDescriptor)
56
0
{
57
0
    m_aDescriptor = rDescriptor;
58
0
    enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
59
0
    State eState = STATE_NAME0;
60
0
    sal_Int32 nStart = 0;
61
0
    OUString aKey;
62
0
    for (sal_Int32 i = 0;; ++i)
63
0
    {
64
0
        bool bEnd = i == rDescriptor.getLength();
65
0
        sal_Unicode c = bEnd ? 0 : rDescriptor[i];
66
0
        switch (eState)
67
0
        {
68
0
        case STATE_NAME0:
69
0
            if (bEnd || !rtl::isAsciiAlphanumeric(c))
70
0
                throw rtl::MalformedUriException(
71
0
                    u"UNO URL contains bad descriptor name"_ustr);
72
0
            nStart = i;
73
0
            eState = STATE_NAME;
74
0
            break;
75
76
0
        case STATE_NAME:
77
0
            if (bEnd || c == 0x2C) // ','
78
0
            {
79
0
                m_aName
80
0
                    = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
81
0
                eState = STATE_KEY0;
82
0
            }
83
0
            else if (!rtl::isAsciiAlphanumeric(c))
84
0
                throw rtl::MalformedUriException(
85
0
                    u"UNO URL contains bad descriptor name"_ustr);
86
0
            break;
87
88
0
        case STATE_KEY0:
89
0
            if (bEnd || !rtl::isAsciiAlphanumeric(c))
90
0
                throw rtl::MalformedUriException(
91
0
                    u"UNO URL contains bad parameter key"_ustr);
92
0
            nStart = i;
93
0
            eState = STATE_KEY;
94
0
            break;
95
96
0
        case STATE_KEY:
97
0
            if (c == 0x3D) // '='
98
0
            {
99
0
                aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
100
0
                nStart = i + 1;
101
0
                eState = STATE_VALUE;
102
0
            }
103
0
            else if (bEnd || !rtl::isAsciiAlphanumeric(c))
104
0
                throw rtl::MalformedUriException(
105
0
                    u"UNO URL contains bad parameter key"_ustr);
106
0
            break;
107
108
0
        case STATE_VALUE:
109
0
            if (bEnd || c == 0x2C) // ','
110
0
            {
111
0
                if (!m_aParameters.emplace(
112
0
                            aKey,
113
0
                            rtl::Uri::decode(rDescriptor.copy(nStart,
114
0
                                                              i - nStart),
115
0
                                             rtl_UriDecodeWithCharset,
116
0
                                             RTL_TEXTENCODING_UTF8)).second)
117
0
                    throw rtl::MalformedUriException(
118
0
                        u"UNO URL contains duplicated parameter"_ustr);
119
0
                eState = STATE_KEY0;
120
0
            }
121
0
            break;
122
0
        }
123
0
        if (bEnd)
124
0
            break;
125
0
    }
126
0
}
127
128
UnoUrlDescriptor::UnoUrlDescriptor(OUString const & rDescriptor):
129
0
    m_pImpl(new Impl(rDescriptor))
130
0
{}
131
132
UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
133
0
    m_pImpl(rOther.m_pImpl->clone())
134
0
{}
135
136
UnoUrlDescriptor::~UnoUrlDescriptor()
137
0
{
138
0
    delete m_pImpl;
139
0
}
140
141
UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
142
0
{
143
0
    if (this != &rOther)
144
0
    {
145
0
        std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
146
0
        delete m_pImpl;
147
0
        m_pImpl = newImpl.release();
148
0
    }
149
0
    return *this;
150
0
}
151
152
OUString const & UnoUrlDescriptor::getDescriptor() const
153
0
{
154
0
    return m_pImpl->m_aDescriptor;
155
0
}
156
157
OUString const & UnoUrlDescriptor::getName() const
158
0
{
159
0
    return m_pImpl->m_aName;
160
0
}
161
162
bool UnoUrlDescriptor::hasParameter(OUString const & rKey) const
163
0
{
164
0
    return m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase())
165
0
        != m_pImpl->m_aParameters.end();
166
0
}
167
168
OUString UnoUrlDescriptor::getParameter(OUString const & rKey) const
169
0
{
170
0
    Impl::Parameters::const_iterator
171
0
        aIt(m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
172
0
    return aIt == m_pImpl->m_aParameters.end() ? OUString() : aIt->second;
173
0
}
174
175
class UnoUrl::Impl
176
{
177
public:
178
    UnoUrlDescriptor m_aConnection;
179
    UnoUrlDescriptor m_aProtocol;
180
    OUString m_aObjectName;
181
182
0
    Impl * clone() const { return new Impl(*this); }
183
184
    /** @exception rtl::MalformedUriException
185
     */
186
    static inline Impl * create(OUString const & rUrl);
187
188
private:
189
    Impl(OUString const & rConnectionDescriptor,
190
         OUString const & rProtocolDescriptor,
191
         OUString aObjectName):
192
0
        m_aConnection(rConnectionDescriptor),
193
0
        m_aProtocol(rProtocolDescriptor),
194
0
        m_aObjectName(std::move(aObjectName))
195
0
    {}
196
};
197
198
inline UnoUrl::Impl * UnoUrl::Impl::create(OUString const & rUrl)
199
0
{
200
0
    if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
201
0
        throw rtl::MalformedUriException(u"UNO URL does not start with \"uno:\""_ustr);
202
0
    sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
203
0
    sal_Int32 j = rUrl.indexOf(';', i);
204
0
    if (j < 0)
205
0
        throw rtl::MalformedUriException(u"UNO URL has too few semicolons"_ustr);
206
0
    OUString aConnection(rUrl.copy(i, j - i));
207
0
    i = j + 1;
208
0
    j = rUrl.indexOf(0x3B, i); // ';'
209
0
    if (j < 0)
210
0
        throw rtl::MalformedUriException(u"UNO URL has too few semicolons"_ustr);
211
0
    OUString aProtocol(rUrl.copy(i, j - i));
212
0
    i = j + 1;
213
0
    if (i == rUrl.getLength())
214
0
        throw rtl::MalformedUriException(u"UNO URL contains empty ObjectName"_ustr);
215
0
    for (j = i; j < rUrl.getLength(); ++j)
216
0
    {
217
0
        sal_Unicode c = rUrl[j];
218
0
        if (!rtl::isAsciiAlphanumeric(c) && c != 0x21 && c != 0x24 // '!', '$'
219
0
            && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
220
0
            && c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
221
0
            && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
222
0
            && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
223
0
            && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
224
0
            && c != 0x7E) // '~'
225
0
            throw rtl::MalformedUriException(u"UNO URL contains invalid ObjectName"_ustr);
226
0
    }
227
0
    return new Impl(aConnection, aProtocol, rUrl.copy(i));
228
0
}
229
230
0
UnoUrl::UnoUrl(OUString const & rUrl): m_pImpl(Impl::create(rUrl))
231
0
{}
232
233
0
UnoUrl::UnoUrl(UnoUrl const & rOther): m_pImpl(rOther.m_pImpl->clone())
234
0
{}
235
236
UnoUrl::~UnoUrl()
237
0
{
238
0
    delete m_pImpl;
239
0
}
240
241
UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
242
0
{
243
0
    if (this != &rOther)
244
0
    {
245
0
        std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
246
0
        delete m_pImpl;
247
0
        m_pImpl = newImpl.release();
248
0
    }
249
0
    return *this;
250
0
}
251
252
UnoUrlDescriptor const & UnoUrl::getConnection() const
253
0
{
254
0
    return m_pImpl->m_aConnection;
255
0
}
256
257
UnoUrlDescriptor const & UnoUrl::getProtocol() const
258
0
{
259
0
    return m_pImpl->m_aProtocol;
260
0
}
261
262
OUString const & UnoUrl::getObjectName() const
263
0
{
264
0
    return m_pImpl->m_aObjectName;
265
0
}
266
267
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */