LCOV - code coverage report
Current view: top level - cppuhelper/source - unourl.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 105 119 88.2 %
Date: 2014-11-03 Functions: 18 26 69.2 %
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             : 
      35             : using cppu::UnoUrl;
      36             : using cppu::UnoUrlDescriptor;
      37             : 
      38             : namespace {
      39             : 
      40        3256 : inline bool isAlphanum(sal_Unicode c)
      41             : {
      42        3216 :     return (c >= 0x30 && c <= 0x39) // '0'--'9'
      43        3188 :         || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
      44        6304 :         || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
      45             : }
      46             : 
      47             : }
      48             : 
      49         468 : class UnoUrlDescriptor::Impl
      50             : {
      51             : public:
      52             :     typedef std::map< rtl::OUString, rtl::OUString > Parameters;
      53             : 
      54             :     rtl::OUString m_aDescriptor;
      55             :     rtl::OUString m_aName;
      56             :     Parameters m_aParameters;
      57             : 
      58             :     /** @exception rtl::MalformedUriException
      59             :      */
      60             :     explicit inline Impl(rtl::OUString const & m_aDescriptor);
      61             : 
      62           0 :     inline Impl * clone() const { return new Impl(*this); }
      63             : };
      64             : 
      65         512 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
      66             : {
      67         490 :     m_aDescriptor = rDescriptor;
      68             :     enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
      69         490 :     State eState = STATE_NAME0;
      70         490 :     sal_Int32 nStart = 0;
      71         490 :     rtl::OUString aKey;
      72        9454 :     for (sal_Int32 i = 0;; ++i)
      73             :     {
      74        9454 :         bool bEnd = i == rDescriptor.getLength();
      75        9454 :         sal_Unicode c = bEnd ? 0 : rDescriptor[i];
      76        9454 :         switch (eState)
      77             :         {
      78             :         case STATE_NAME0:
      79         490 :             if (bEnd || !isAlphanum(c))
      80             :                 throw rtl::MalformedUriException(
      81           2 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      82         488 :             nStart = i;
      83         488 :             eState = STATE_NAME;
      84         488 :             break;
      85             : 
      86             :         case STATE_NAME:
      87        1586 :             if (bEnd || c == 0x2C) // ','
      88             :             {
      89             :                 m_aName
      90         484 :                     = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
      91         484 :                 eState = STATE_KEY0;
      92             :             }
      93        1102 :             else if (!isAlphanum(c))
      94             :                 throw rtl::MalformedUriException(
      95           4 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      96        1582 :             break;
      97             : 
      98             :         case STATE_KEY0:
      99         404 :             if (bEnd || !isAlphanum(c))
     100             :                 throw rtl::MalformedUriException(
     101          10 :                     rtl::OUString("UNO URL contains bad parameter key"));
     102         394 :             nStart = i;
     103         394 :             eState = STATE_KEY;
     104         394 :             break;
     105             : 
     106             :         case STATE_KEY:
     107        1302 :             if (c == 0x3D) // '='
     108             :             {
     109         390 :                 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
     110         390 :                 nStart = i + 1;
     111         390 :                 eState = STATE_VALUE;
     112             :             }
     113         912 :             else if (bEnd || !isAlphanum(c))
     114             :                 throw rtl::MalformedUriException(
     115           4 :                     rtl::OUString("UNO URL contains bad parameter key"));
     116        1298 :             break;
     117             : 
     118             :         case STATE_VALUE:
     119        5672 :             if (bEnd || c == 0x2C) // ','
     120             :             {
     121         780 :                 if (!m_aParameters.insert(
     122             :                         Parameters::value_type(
     123             :                             aKey,
     124             :                             rtl::Uri::decode(rDescriptor.copy(nStart,
     125             :                                                               i - nStart),
     126             :                                              rtl_UriDecodeWithCharset,
     127         780 :                                              RTL_TEXTENCODING_UTF8))).second)
     128             :                     throw rtl::MalformedUriException(
     129           2 :                         rtl::OUString("UNO URL contains duplicated parameter"));
     130         388 :                 eState = STATE_KEY0;
     131             :             }
     132        5670 :             break;
     133             :         }
     134        9432 :         if (bEnd)
     135         468 :             break;
     136        8986 :     }
     137         468 : }
     138             : 
     139         350 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
     140         370 :     m_xImpl(new Impl(rDescriptor))
     141         330 : {}
     142             : 
     143             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     144         128 : UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
     145         128 :     m_xImpl(rImpl)
     146         128 : {}
     147             : SAL_WNODEPRECATED_DECLARATIONS_POP
     148             : 
     149           0 : UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
     150           0 :     m_xImpl(rOther.m_xImpl->clone())
     151           0 : {}
     152             : 
     153         458 : UnoUrlDescriptor::~UnoUrlDescriptor()
     154         458 : {}
     155             : 
     156           0 : UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
     157             : {
     158           0 :     m_xImpl.reset(rOther.m_xImpl->clone());
     159           0 :     return *this;
     160             : }
     161             : 
     162          72 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
     163             : {
     164          72 :     return m_xImpl->m_aDescriptor;
     165             : }
     166             : 
     167         142 : rtl::OUString const & UnoUrlDescriptor::getName() const
     168             : {
     169         142 :     return m_xImpl->m_aName;
     170             : }
     171             : 
     172          34 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
     173             : {
     174          68 :     return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
     175         102 :         != m_xImpl->m_aParameters.end();
     176             : }
     177             : 
     178         188 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
     179             : {
     180             :     Impl::Parameters::const_iterator
     181         188 :         aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
     182         188 :     return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
     183             : }
     184             : 
     185          64 : class UnoUrl::Impl
     186             : {
     187             : public:
     188             :     UnoUrlDescriptor m_aConnection;
     189             :     UnoUrlDescriptor m_aProtocol;
     190             :     rtl::OUString m_aObjectName;
     191             : 
     192           0 :     inline Impl * clone() const { return new Impl(*this); }
     193             : 
     194             :     /** @exception rtl::MalformedUriException
     195             :      */
     196             :     static inline Impl * create(rtl::OUString const & rUrl);
     197             : 
     198             : private:
     199             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     200          64 :     Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection,
     201             :                               std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol,
     202             :                               rtl::OUString const & rObjectName):
     203             :         m_aConnection(rConnection),
     204             :         m_aProtocol(rProtocol),
     205          64 :         m_aObjectName(rObjectName)
     206          64 :     {}
     207             : SAL_WNODEPRECATED_DECLARATIONS_POP
     208             : };
     209             : 
     210          78 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
     211             : {
     212          78 :     if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
     213             :         throw rtl::MalformedUriException(
     214           6 :             rtl::OUString("UNO URL does not start with \"uno:\""));
     215          72 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
     216          72 :     sal_Int32 j = rUrl.indexOf(';', i);
     217          72 :     if (j < 0)
     218             :         throw rtl::MalformedUriException(
     219           2 :             rtl::OUString("UNO URL has too few semicolons"));
     220             :     SAL_WNODEPRECATED_DECLARATIONS_PUSH
     221             :     std::auto_ptr< UnoUrlDescriptor::Impl >
     222          70 :         xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     223             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     224          70 :     i = j + 1;
     225          70 :     j = rUrl.indexOf(0x3B, i); // ';'
     226          70 :     if (j < 0)
     227             :         throw rtl::MalformedUriException(
     228           0 :             rtl::OUString("UNO URL has too few semicolons"));
     229             :     SAL_WNODEPRECATED_DECLARATIONS_PUSH
     230             :     std::auto_ptr< UnoUrlDescriptor::Impl >
     231         140 :         xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     232             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     233          68 :     i = j + 1;
     234          68 :     if (i == rUrl.getLength())
     235             :         throw rtl::MalformedUriException(
     236           2 :             rtl::OUString("UNO URL contains empty ObjectName"));
     237         422 :     for (j = i; j < rUrl.getLength(); ++j)
     238             :     {
     239         358 :         sal_Unicode c = rUrl[j];
     240         762 :         if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
     241          42 :             && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
     242          38 :             && c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
     243          34 :             && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
     244          22 :             && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
     245          10 :             && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
     246         360 :             && c != 0x7E) // '~'
     247             :             throw rtl::MalformedUriException(
     248           2 :                 rtl::OUString("UNO URL contains invalid ObjectName"));
     249             :     }
     250         134 :     return new Impl(xConnection, xProtocol, rUrl.copy(i));
     251             : }
     252             : 
     253          78 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
     254          64 : {}
     255             : 
     256           0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
     257           0 : {}
     258             : 
     259          64 : UnoUrl::~UnoUrl()
     260          64 : {}
     261             : 
     262           0 : UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
     263             : {
     264           0 :     m_xImpl.reset(rOther.m_xImpl->clone());
     265           0 :     return *this;
     266             : }
     267             : 
     268          18 : UnoUrlDescriptor const & UnoUrl::getConnection() const
     269             : {
     270          18 :     return m_xImpl->m_aConnection;
     271             : }
     272             : 
     273          18 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
     274             : {
     275          18 :     return m_xImpl->m_aProtocol;
     276             : }
     277             : 
     278          26 : rtl::OUString const & UnoUrl::getObjectName() const
     279             : {
     280          26 :     return m_xImpl->m_aObjectName;
     281             : }
     282             : 
     283             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10