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

Generated by: LCOV version 1.10