merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / xmlsec / xmlstreamio.cxx
blobd5ba439467fe5dc52f2be147132519852f4a8c3b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
32 * Implementation of the I/O interfaces based on stream and URI binding
34 #include "xmlstreamio.hxx"
35 #include <rtl/ustring.hxx>
36 #include "rtl/uri.hxx"
38 #include <libxml/uri.h>
39 #include <sal/types.h>
40 //For reasons that escape me, this is what xmlsec does when size_t is not 4
41 #if SAL_TYPES_SIZEOFPOINTER != 4
42 # define XMLSEC_NO_SIZE_T
43 #endif
44 #include <xmlsec/io.h>
46 #define XMLSTREAMIO_INITIALIZED 0x01
47 #define XMLSTREAMIO_REGISTERED 0x02
49 /* Global variables */
50 /*-
51 * Enable stream I/O or not.
53 static char enableXmlStreamIO = 0x00 ;
55 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XUriBinding > m_xUriBinding ;
57 extern "C"
58 int xmlStreamMatch( const char* uri )
60 ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
62 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
63 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
64 if( uri == NULL || !m_xUriBinding.is() )
65 return 0 ;
66 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
67 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
68 //first. If this failed it would try this
69 //again with the original escaped string. However, it does not get this far, because there
70 //is another callback registered by libxml which claims to be able to handle this uri.
71 ::rtl::OUString sUri =
72 ::rtl::Uri::encode( ::rtl::OUString::createFromAscii( uri ),
73 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
74 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
75 if (!xInputStream.is())
77 //Try the the passed in uri directly.
78 //For old documents prior OOo 3.0. We did not use URIs then.
79 xInputStream = m_xUriBinding->getUriBinding(
80 ::rtl::OUString::createFromAscii(uri));
83 if (xInputStream.is())
84 return 1;
85 else
86 return 0 ;
89 extern "C"
90 void* xmlStreamOpen( const char* uri )
92 ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
93 ::com::sun::star::io::XInputStream* pInputStream ;
95 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
96 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
97 if( uri == NULL || !m_xUriBinding.is() )
98 return NULL ;
100 //see xmlStreamMatch
101 ::rtl::OUString sUri =
102 ::rtl::Uri::encode( ::rtl::OUString::createFromAscii( uri ),
103 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
104 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
105 if (!xInputStream.is())
107 //For old documents.
108 //try the the passed in uri directly.
109 xInputStream = m_xUriBinding->getUriBinding(
110 ::rtl::OUString::createFromAscii(uri));
113 if( xInputStream.is() ) {
114 pInputStream = xInputStream.get() ;
115 pInputStream->acquire() ;
116 return ( void* )pInputStream ;
120 return NULL ;
123 extern "C"
124 int xmlStreamRead( void* context, char* buffer, int len )
126 int numbers ;
127 ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
128 ::com::sun::star::uno::Sequence< sal_Int8 > outSeqs( len ) ;
130 numbers = 0 ;
131 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
132 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
133 if( context != NULL ) {
134 xInputStream = ( com::sun::star::io::XInputStream* )context ;
135 if( !xInputStream.is() )
136 return 0 ;
138 numbers = xInputStream->readBytes( outSeqs, len ) ;
139 const sal_Int8* readBytes = ( const sal_Int8* )outSeqs.getArray() ;
140 for( int i = 0 ; i < numbers ; i ++ )
141 *( buffer + i ) = *( readBytes + i ) ;
145 return numbers ;
148 extern "C"
149 int xmlStreamClose( void * context )
151 ::com::sun::star::io::XInputStream* pInputStream ;
153 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
154 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
155 if( context != NULL ) {
156 pInputStream = ( ::com::sun::star::io::XInputStream* )context ;
157 pInputStream->release() ;
161 return 0 ;
164 int xmlEnableStreamInputCallbacks()
166 int cbs = 0 ;
168 if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
169 //Register the callbacks into libxml2
170 //cbs = xmlRegisterInputCallbacks(
171 // xmlStreamMatch,
172 // xmlStreamOpen,
173 // xmlStreamRead,
174 // xmlStreamClose ) ;
175 //if( cbs < 0 ) {
176 // return -1 ;
179 //Register the callbacks into xmlSec
180 //In order to make the xmlsec io finding the callbacks firstly,
181 //I put the callbacks at the very begining.
183 //Cleanup the older callbacks.
184 //Notes: all none default callbacks will lose.
185 xmlSecIOCleanupCallbacks() ;
187 //Register my classbacks.
188 cbs = xmlSecIORegisterCallbacks(
189 xmlStreamMatch,
190 xmlStreamOpen,
191 xmlStreamRead,
192 xmlStreamClose ) ;
193 if( cbs < 0 ) {
194 return -1 ;
197 //Register the default callbacks.
198 //Notes: the error will cause xmlsec working problems.
199 cbs = xmlSecIORegisterDefaultCallbacks() ;
200 if( cbs < 0 ) {
201 return -1 ;
204 enableXmlStreamIO |= XMLSTREAMIO_INITIALIZED ;
207 return 0 ;
210 int xmlRegisterStreamInputCallbacks(
211 ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XUriBinding >& aUriBinding
213 if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
214 if( xmlEnableStreamInputCallbacks() < 0 )
215 return -1 ;
218 if( !( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
219 enableXmlStreamIO |= XMLSTREAMIO_REGISTERED ;
222 m_xUriBinding = aUriBinding ;
224 return 0 ;
227 int xmlUnregisterStreamInputCallbacks( void )
229 if( ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
230 //Clear the uir-stream binding
231 m_xUriBinding.clear() ;
233 //disable the registered flag
234 enableXmlStreamIO &= ~XMLSTREAMIO_REGISTERED ;
237 return 0 ;
240 void xmlDisableStreamInputCallbacks() {
241 xmlUnregisterStreamInputCallbacks() ;
242 enableXmlStreamIO &= ~XMLSTREAMIO_INITIALIZED ;