LCOV - code coverage report
Current view: top level - cppuhelper/source - unourl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 104 122 85.2 %
Date: 2015-06-13 12:38:46 Functions: 17 25 68.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <osl/diagnose.h>
      24             : #include <rtl/malformeduriexception.hxx>
      25             : #include <rtl/string.h>
      26             : #include <rtl/textenc.h>
      27             : #include <rtl/uri.h>
      28             : #include <rtl/uri.hxx>
      29             : #include <rtl/ustring.h>
      30             : #include <rtl/ustring.hxx>
      31             : #include <sal/types.h>
      32             : 
      33             : #include <map>
      34             : #include <memory>
      35             : 
      36             : using cppu::UnoUrl;
      37             : using cppu::UnoUrlDescriptor;
      38             : 
      39             : namespace {
      40             : 
      41        1962 : inline bool isAlphanum(sal_Unicode c)
      42             : {
      43        1937 :     return (c >= 0x30 && c <= 0x39) // '0'--'9'
      44        1928 :         || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
      45        3802 :         || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
      46             : }
      47             : 
      48             : }
      49             : 
      50         260 : class UnoUrlDescriptor::Impl
      51             : {
      52             : public:
      53             :     typedef std::map< rtl::OUString, rtl::OUString > Parameters;
      54             : 
      55             :     rtl::OUString m_aDescriptor;
      56             :     rtl::OUString m_aName;
      57             :     Parameters m_aParameters;
      58             : 
      59             :     /** @exception rtl::MalformedUriException
      60             :      */
      61             :     explicit inline Impl(rtl::OUString const & m_aDescriptor);
      62             : 
      63           0 :     inline Impl * clone() const { return new Impl(*this); }
      64             : };
      65             : 
      66         282 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
      67             : {
      68         271 :     m_aDescriptor = rDescriptor;
      69             :     enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
      70         271 :     State eState = STATE_NAME0;
      71         271 :     sal_Int32 nStart = 0;
      72         271 :     rtl::OUString aKey;
      73        5849 :     for (sal_Int32 i = 0;; ++i)
      74             :     {
      75        5849 :         bool bEnd = i == rDescriptor.getLength();
      76        5849 :         sal_Unicode c = bEnd ? 0 : rDescriptor[i];
      77        5849 :         switch (eState)
      78             :         {
      79             :         case STATE_NAME0:
      80         271 :             if (bEnd || !isAlphanum(c))
      81             :                 throw rtl::MalformedUriException(
      82           1 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      83         270 :             nStart = i;
      84         270 :             eState = STATE_NAME;
      85         270 :             break;
      86             : 
      87             :         case STATE_NAME:
      88         896 :             if (bEnd || c == 0x2C) // ','
      89             :             {
      90             :                 m_aName
      91         268 :                     = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
      92         268 :                 eState = STATE_KEY0;
      93             :             }
      94         628 :             else if (!isAlphanum(c))
      95             :                 throw rtl::MalformedUriException(
      96           2 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      97         894 :             break;
      98             : 
      99             :         case STATE_KEY0:
     100         227 :             if (bEnd || !isAlphanum(c))
     101             :                 throw rtl::MalformedUriException(
     102           5 :                     rtl::OUString("UNO URL contains bad parameter key"));
     103         222 :             nStart = i;
     104         222 :             eState = STATE_KEY;
     105         222 :             break;
     106             : 
     107             :         case STATE_KEY:
     108         751 :             if (c == 0x3D) // '='
     109             :             {
     110         220 :                 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
     111         220 :                 nStart = i + 1;
     112         220 :                 eState = STATE_VALUE;
     113             :             }
     114         531 :             else if (bEnd || !isAlphanum(c))
     115             :                 throw rtl::MalformedUriException(
     116           2 :                     rtl::OUString("UNO URL contains bad parameter key"));
     117         749 :             break;
     118             : 
     119             :         case STATE_VALUE:
     120        3704 :             if (bEnd || c == 0x2C) // ','
     121             :             {
     122         440 :                 if (!m_aParameters.insert(
     123             :                         Parameters::value_type(
     124             :                             aKey,
     125             :                             rtl::Uri::decode(rDescriptor.copy(nStart,
     126             :                                                               i - nStart),
     127             :                                              rtl_UriDecodeWithCharset,
     128         440 :                                              RTL_TEXTENCODING_UTF8))).second)
     129             :                     throw rtl::MalformedUriException(
     130           1 :                         rtl::OUString("UNO URL contains duplicated parameter"));
     131         219 :                 eState = STATE_KEY0;
     132             :             }
     133        3703 :             break;
     134             :         }
     135        5838 :         if (bEnd)
     136         260 :             break;
     137        5589 :     }
     138         260 : }
     139             : 
     140         271 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
     141         282 :     m_pImpl(new Impl(rDescriptor))
     142         260 : {}
     143             : 
     144           0 : UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
     145           0 :     m_pImpl(rOther.m_pImpl->clone())
     146           0 : {}
     147             : 
     148         260 : UnoUrlDescriptor::~UnoUrlDescriptor()
     149             : {
     150         260 :     delete m_pImpl;
     151         260 : }
     152             : 
     153           0 : UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
     154             : {
     155           0 :     std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
     156           0 :     delete m_pImpl;
     157           0 :     m_pImpl = newImpl.release();
     158           0 :     return *this;
     159             : }
     160             : 
     161          46 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
     162             : {
     163          46 :     return m_pImpl->m_aDescriptor;
     164             : }
     165             : 
     166          91 : rtl::OUString const & UnoUrlDescriptor::getName() const
     167             : {
     168          91 :     return m_pImpl->m_aName;
     169             : }
     170             : 
     171          17 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
     172             : {
     173          34 :     return m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase())
     174          51 :         != m_pImpl->m_aParameters.end();
     175             : }
     176             : 
     177         114 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
     178             : {
     179             :     Impl::Parameters::const_iterator
     180         114 :         aIt(m_pImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
     181         114 :     return aIt == m_pImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
     182             : }
     183             : 
     184          37 : class UnoUrl::Impl
     185             : {
     186             : public:
     187             :     UnoUrlDescriptor m_aConnection;
     188             :     UnoUrlDescriptor m_aProtocol;
     189             :     rtl::OUString m_aObjectName;
     190             : 
     191           0 :     inline Impl * clone() const { return new Impl(*this); }
     192             : 
     193             :     /** @exception rtl::MalformedUriException
     194             :      */
     195             :     static inline Impl * create(rtl::OUString const & rUrl);
     196             : 
     197             : private:
     198          38 :     Impl(rtl::OUString const & rConnectionDescriptor,
     199             :          rtl::OUString const & rProtocolDescriptor,
     200             :          rtl::OUString const & rObjectName):
     201             :         m_aConnection(rConnectionDescriptor),
     202             :         m_aProtocol(rProtocolDescriptor),
     203          39 :         m_aObjectName(rObjectName)
     204          37 :     {}
     205             : };
     206             : 
     207          44 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
     208             : {
     209          44 :     if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
     210             :         throw rtl::MalformedUriException(
     211           3 :             rtl::OUString("UNO URL does not start with \"uno:\""));
     212          41 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
     213          41 :     sal_Int32 j = rUrl.indexOf(';', i);
     214          41 :     if (j < 0)
     215             :         throw rtl::MalformedUriException(
     216           1 :             rtl::OUString("UNO URL has too few semicolons"));
     217          40 :     rtl::OUString aConnection(rUrl.copy(i, j - i));
     218          40 :     i = j + 1;
     219          40 :     j = rUrl.indexOf(0x3B, i); // ';'
     220          40 :     if (j < 0)
     221             :         throw rtl::MalformedUriException(
     222           0 :             rtl::OUString("UNO URL has too few semicolons"));
     223          80 :     rtl::OUString aProtocol(rUrl.copy(i, j - i));
     224          40 :     i = j + 1;
     225          40 :     if (i == rUrl.getLength())
     226             :         throw rtl::MalformedUriException(
     227           1 :             rtl::OUString("UNO URL contains empty ObjectName"));
     228         348 :     for (j = i; j < rUrl.getLength(); ++j)
     229             :     {
     230         310 :         sal_Unicode c = rUrl[j];
     231         648 :         if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
     232          26 :             && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
     233          24 :             && c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
     234          22 :             && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
     235          11 :             && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
     236           5 :             && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
     237         311 :             && c != 0x7E) // '~'
     238             :             throw rtl::MalformedUriException(
     239           1 :                 rtl::OUString("UNO URL contains invalid ObjectName"));
     240             :     }
     241          78 :     return new Impl(aConnection, aProtocol, rUrl.copy(i));
     242             : }
     243             : 
     244          44 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_pImpl(Impl::create(rUrl))
     245          37 : {}
     246             : 
     247           0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_pImpl(rOther.m_pImpl->clone())
     248           0 : {}
     249             : 
     250          37 : UnoUrl::~UnoUrl()
     251             : {
     252          37 :     delete m_pImpl;
     253          37 : }
     254             : 
     255           0 : UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
     256             : {
     257           0 :     std::unique_ptr<Impl> newImpl(rOther.m_pImpl->clone());
     258           0 :     delete m_pImpl;
     259           0 :     m_pImpl = newImpl.release();
     260           0 :     return *this;
     261             : }
     262             : 
     263          14 : UnoUrlDescriptor const & UnoUrl::getConnection() const
     264             : {
     265          14 :     return m_pImpl->m_aConnection;
     266             : }
     267             : 
     268          14 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
     269             : {
     270          14 :     return m_pImpl->m_aProtocol;
     271             : }
     272             : 
     273          18 : rtl::OUString const & UnoUrl::getObjectName() const
     274             : {
     275          18 :     return m_pImpl->m_aObjectName;
     276             : }
     277             : 
     278             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11