LCOV - code coverage report
Current view: top level - xmlsecurity/source/xmlsec - xmlstreamio.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 17 83 20.5 %
Date: 2014-11-03 Functions: 5 10 50.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             : /*
      22             :  * Implementation of the I/O interfaces based on stream and URI binding
      23             :  */
      24             : #include "xmlstreamio.hxx"
      25             : #include <rtl/ustring.hxx>
      26             : #include <rtl/uri.hxx>
      27             : 
      28             : #include <libxml/uri.h>
      29             : #include "xmlsecurity/xmlsec-wrapper.h"
      30             : 
      31             : #define XMLSTREAMIO_INITIALIZED 0x01
      32             : #define XMLSTREAMIO_REGISTERED  0x02
      33             : 
      34             : /* Global variables */
      35             : /*-
      36             :  * Enable stream I/O or not.
      37             :  */
      38             : static char enableXmlStreamIO = 0x00 ;
      39             : 
      40           8 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XUriBinding > m_xUriBinding ;
      41             : 
      42             : extern "C"
      43           0 : int xmlStreamMatch( const char* uri )
      44             : {
      45           0 :     ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
      46             : 
      47           0 :     if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
      48           0 :         ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
      49           0 :         if( uri == NULL || !m_xUriBinding.is() )
      50           0 :             return 0 ;
      51             :         //XMLSec first unescapes the uri and  calls this function. For example, we pass the Uri
      52             :         //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
      53             :         //first. If this failed it would try this
      54             :         //again with the original escaped string. However, it does not get this far, because there
      55             :         //is another callback registered by libxml which claims to be able to handle this uri.
      56             :         OUString sUri =
      57             :             ::rtl::Uri::encode( OUString::createFromAscii( uri ),
      58           0 :             rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
      59           0 :         xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
      60           0 :         if (!xInputStream.is())
      61             :         {
      62             :             //Try the passed in uri directly.
      63             :             //For old documents prior OOo 3.0. We did not use URIs then.
      64           0 :             xInputStream = m_xUriBinding->getUriBinding(
      65           0 :                 OUString::createFromAscii(uri));
      66           0 :         }
      67             :     }
      68           0 :     if (xInputStream.is())
      69           0 :         return 1;
      70             :     else
      71           0 :         return 0 ;
      72             : }
      73             : 
      74             : extern "C"
      75           0 : void* xmlStreamOpen( const char* uri )
      76             : {
      77           0 :     ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
      78             : 
      79           0 :     if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
      80           0 :         ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
      81           0 :         if( uri == NULL || !m_xUriBinding.is() )
      82           0 :             return NULL ;
      83             : 
      84             :         //see xmlStreamMatch
      85             :         OUString sUri =
      86             :             ::rtl::Uri::encode( OUString::createFromAscii( uri ),
      87           0 :             rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
      88           0 :         xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
      89           0 :         if (!xInputStream.is())
      90             :         {
      91             :             //For old documents.
      92             :             //try the passed in uri directly.
      93           0 :             xInputStream = m_xUriBinding->getUriBinding(
      94           0 :                 OUString::createFromAscii(uri));
      95             :         }
      96             : 
      97           0 :         if( xInputStream.is() ) {
      98             :             ::com::sun::star::io::XInputStream* pInputStream ;
      99           0 :             pInputStream = xInputStream.get() ;
     100           0 :             pInputStream->acquire() ;
     101           0 :             return ( void* )pInputStream ;
     102           0 :         }
     103             :     }
     104             : 
     105           0 :     return NULL ;
     106             : }
     107             : 
     108             : extern "C"
     109           0 : int xmlStreamRead( void* context, char* buffer, int len )
     110             : {
     111             :     int numbers ;
     112           0 :     ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
     113           0 :     ::com::sun::star::uno::Sequence< sal_Int8 > outSeqs( len ) ;
     114             : 
     115           0 :     numbers = 0 ;
     116           0 :     if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
     117           0 :         ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
     118           0 :         if( context != NULL ) {
     119           0 :             xInputStream = ( com::sun::star::io::XInputStream* )context ;
     120           0 :             if( !xInputStream.is() )
     121           0 :                 return 0 ;
     122             : 
     123           0 :             numbers = xInputStream->readBytes( outSeqs, len ) ;
     124           0 :             const sal_Int8* readBytes = ( const sal_Int8* )outSeqs.getArray() ;
     125           0 :             for( int i = 0 ; i < numbers ; i ++ )
     126           0 :                 *( buffer + i ) = *( readBytes + i ) ;
     127             :         }
     128             :     }
     129             : 
     130           0 :     return numbers ;
     131             : }
     132             : 
     133             : extern "C"
     134           0 : int xmlStreamClose( void * context )
     135             : {
     136           0 :     if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
     137           0 :         ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
     138           0 :         if( context != NULL ) {
     139             :             ::com::sun::star::io::XInputStream* pInputStream ;
     140           0 :             pInputStream = ( ::com::sun::star::io::XInputStream* )context ;
     141           0 :             pInputStream->release() ;
     142             :         }
     143             :     }
     144             : 
     145           0 :     return 0 ;
     146             : }
     147             : 
     148           2 : int xmlEnableStreamInputCallbacks()
     149             : {
     150             : 
     151           2 :     if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
     152             :         //Register the callbacks into xmlSec
     153             :         //In order to make the xmlsec io finding the callbacks firstly,
     154             :         //I put the callbacks at the very beginning.
     155             : 
     156             :         //Cleanup the older callbacks.
     157             :         //Notes: all none default callbacks will lose.
     158           2 :         xmlSecIOCleanupCallbacks() ;
     159             : 
     160             :         //Register my classbacks.
     161             :         int cbs = xmlSecIORegisterCallbacks(
     162             :                     xmlStreamMatch,
     163             :                     xmlStreamOpen,
     164             :                     xmlStreamRead,
     165           2 :                     xmlStreamClose ) ;
     166           2 :         if( cbs < 0 ) {
     167           0 :             return -1 ;
     168             :         }
     169             : 
     170             :         //Register the default callbacks.
     171             :         //Notes: the error will cause xmlsec working problems.
     172           2 :         cbs = xmlSecIORegisterDefaultCallbacks() ;
     173           2 :         if( cbs < 0 ) {
     174           0 :             return -1 ;
     175             :         }
     176             : 
     177           2 :         enableXmlStreamIO |= XMLSTREAMIO_INITIALIZED ;
     178             :     }
     179             : 
     180           2 :     return 0 ;
     181             : }
     182             : 
     183           0 : int xmlRegisterStreamInputCallbacks(
     184             :     ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XUriBinding >& aUriBinding
     185             : ) {
     186           0 :     if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
     187           0 :         if( xmlEnableStreamInputCallbacks() < 0 )
     188           0 :             return -1 ;
     189             :     }
     190             : 
     191           0 :     if( !( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
     192           0 :         enableXmlStreamIO |= XMLSTREAMIO_REGISTERED ;
     193             :     }
     194             : 
     195           0 :     m_xUriBinding = aUriBinding ;
     196             : 
     197           0 :     return 0 ;
     198             : }
     199             : 
     200           2 : int xmlUnregisterStreamInputCallbacks( void )
     201             : {
     202           2 :     if( ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
     203             :         //Clear the uir-stream binding
     204           0 :         m_xUriBinding.clear() ;
     205             : 
     206             :         //disable the registered flag
     207           0 :         enableXmlStreamIO &= ~XMLSTREAMIO_REGISTERED ;
     208             :     }
     209             : 
     210           2 :     return 0 ;
     211             : }
     212             : 
     213           2 : void xmlDisableStreamInputCallbacks() {
     214           2 :     xmlUnregisterStreamInputCallbacks() ;
     215           2 :     enableXmlStreamIO &= ~XMLSTREAMIO_INITIALIZED ;
     216          26 : }
     217             : 
     218             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10