1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlstreamio.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
35 * Implementation of the I/O interfaces based on stream and URI binding
37 #include "xmlstreamio.hxx"
38 #include <rtl/ustring.hxx>
39 #include "rtl/uri.hxx"
41 #include <libxml/uri.h>
42 #include <sal/types.h>
43 //For reasons that escape me, this is what xmlsec does when size_t is not 4
44 #if SAL_TYPES_SIZEOFPOINTER != 4
45 # define XMLSEC_NO_SIZE_T
47 #include <xmlsec/io.h>
49 #define XMLSTREAMIO_INITIALIZED 0x01
50 #define XMLSTREAMIO_REGISTERED 0x02
52 /* Global variables */
54 * Enable stream I/O or not.
56 static char enableXmlStreamIO
= 0x00 ;
58 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XUriBinding
> m_xUriBinding
;
61 int xmlStreamMatch( const char* uri
)
63 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
65 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
66 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
67 if( uri
== NULL
|| !m_xUriBinding
.is() )
69 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
70 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
71 //first. If this failed it would try this
72 //again with the original escaped string. However, it does not get this far, because there
73 //is another callback registered by libxml which claims to be able to handle this uri.
74 ::rtl::OUString sUri
=
75 ::rtl::Uri::encode( ::rtl::OUString::createFromAscii( uri
),
76 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
77 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
78 if (!xInputStream
.is())
80 //Try the the passed in uri directly.
81 //For old documents prior OOo 3.0. We did not use URIs then.
82 xInputStream
= m_xUriBinding
->getUriBinding(
83 ::rtl::OUString::createFromAscii(uri
));
86 if (xInputStream
.is())
93 void* xmlStreamOpen( const char* uri
)
95 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
96 ::com::sun::star::io::XInputStream
* pInputStream
;
98 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
99 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
100 if( uri
== NULL
|| !m_xUriBinding
.is() )
104 ::rtl::OUString sUri
=
105 ::rtl::Uri::encode( ::rtl::OUString::createFromAscii( uri
),
106 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
107 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
108 if (!xInputStream
.is())
111 //try the the passed in uri directly.
112 xInputStream
= m_xUriBinding
->getUriBinding(
113 ::rtl::OUString::createFromAscii(uri
));
116 if( xInputStream
.is() ) {
117 pInputStream
= xInputStream
.get() ;
118 pInputStream
->acquire() ;
119 return ( void* )pInputStream
;
127 int xmlStreamRead( void* context
, char* buffer
, int len
)
130 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
131 ::com::sun::star::uno::Sequence
< sal_Int8
> outSeqs( len
) ;
134 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
135 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
136 if( context
!= NULL
) {
137 xInputStream
= ( com::sun::star::io::XInputStream
* )context
;
138 if( !xInputStream
.is() )
141 numbers
= xInputStream
->readBytes( outSeqs
, len
) ;
142 const sal_Int8
* readBytes
= ( const sal_Int8
* )outSeqs
.getArray() ;
143 for( int i
= 0 ; i
< numbers
; i
++ )
144 *( buffer
+ i
) = *( readBytes
+ i
) ;
152 int xmlStreamClose( void * context
)
154 ::com::sun::star::io::XInputStream
* pInputStream
;
156 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
157 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
158 if( context
!= NULL
) {
159 pInputStream
= ( ::com::sun::star::io::XInputStream
* )context
;
160 pInputStream
->release() ;
167 int xmlEnableStreamInputCallbacks()
171 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
172 //Register the callbacks into libxml2
173 //cbs = xmlRegisterInputCallbacks(
177 // xmlStreamClose ) ;
182 //Register the callbacks into xmlSec
183 //In order to make the xmlsec io finding the callbacks firstly,
184 //I put the callbacks at the very begining.
186 //Cleanup the older callbacks.
187 //Notes: all none default callbacks will lose.
188 xmlSecIOCleanupCallbacks() ;
190 //Register my classbacks.
191 cbs
= xmlSecIORegisterCallbacks(
200 //Register the default callbacks.
201 //Notes: the error will cause xmlsec working problems.
202 cbs
= xmlSecIORegisterDefaultCallbacks() ;
207 enableXmlStreamIO
|= XMLSTREAMIO_INITIALIZED
;
213 int xmlRegisterStreamInputCallbacks(
214 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XUriBinding
>& aUriBinding
216 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
217 if( xmlEnableStreamInputCallbacks() < 0 )
221 if( !( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
222 enableXmlStreamIO
|= XMLSTREAMIO_REGISTERED
;
225 m_xUriBinding
= aUriBinding
;
230 int xmlUnregisterStreamInputCallbacks( void )
232 if( ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
233 //Clear the uir-stream binding
234 m_xUriBinding
.clear() ;
236 //disable the registered flag
237 enableXmlStreamIO
&= ~XMLSTREAMIO_REGISTERED
;
243 void xmlDisableStreamInputCallbacks() {
244 xmlUnregisterStreamInputCallbacks() ;
245 enableXmlStreamIO
&= ~XMLSTREAMIO_INITIALIZED
;