merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / source / xmlsec / xmlstreamio.cxx
blobcf391abc8547c44759b5d4a06a1efd684a5f780a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlstreamio.cxx,v $
10 * $Revision: 1.7 $
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
46 #endif
47 #include <xmlsec/io.h>
49 #define XMLSTREAMIO_INITIALIZED 0x01
50 #define XMLSTREAMIO_REGISTERED 0x02
52 /* Global variables */
53 /*-
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 ;
60 extern "C"
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() )
68 return 0 ;
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())
87 return 1;
88 else
89 return 0 ;
92 extern "C"
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() )
101 return NULL ;
103 //see xmlStreamMatch
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())
110 //For old documents.
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 ;
123 return NULL ;
126 extern "C"
127 int xmlStreamRead( void* context, char* buffer, int len )
129 int numbers ;
130 ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream > xInputStream ;
131 ::com::sun::star::uno::Sequence< sal_Int8 > outSeqs( len ) ;
133 numbers = 0 ;
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() )
139 return 0 ;
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 ) ;
148 return numbers ;
151 extern "C"
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() ;
164 return 0 ;
167 int xmlEnableStreamInputCallbacks()
169 int cbs = 0 ;
171 if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
172 //Register the callbacks into libxml2
173 //cbs = xmlRegisterInputCallbacks(
174 // xmlStreamMatch,
175 // xmlStreamOpen,
176 // xmlStreamRead,
177 // xmlStreamClose ) ;
178 //if( cbs < 0 ) {
179 // return -1 ;
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(
192 xmlStreamMatch,
193 xmlStreamOpen,
194 xmlStreamRead,
195 xmlStreamClose ) ;
196 if( cbs < 0 ) {
197 return -1 ;
200 //Register the default callbacks.
201 //Notes: the error will cause xmlsec working problems.
202 cbs = xmlSecIORegisterDefaultCallbacks() ;
203 if( cbs < 0 ) {
204 return -1 ;
207 enableXmlStreamIO |= XMLSTREAMIO_INITIALIZED ;
210 return 0 ;
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 )
218 return -1 ;
221 if( !( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
222 enableXmlStreamIO |= XMLSTREAMIO_REGISTERED ;
225 m_xUriBinding = aUriBinding ;
227 return 0 ;
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 ;
240 return 0 ;
243 void xmlDisableStreamInputCallbacks() {
244 xmlUnregisterStreamInputCallbacks() ;
245 enableXmlStreamIO &= ~XMLSTREAMIO_INITIALIZED ;