Bump version to 6.0-36
[LibreOffice.git] / xmlsecurity / source / xmlsec / xmlstreamio.cxx
blob6edd946da4fa9523fcdd4fdb376e81900831532b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 * Implementation of the I/O interfaces based on stream and URI binding
24 #include <xmlsec/xmlstreamio.hxx>
25 #include <xmlsec/errorcallback.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/uri.hxx>
28 #include <comphelper/scopeguard.hxx>
30 #include <libxml/uri.h>
31 #include <xmlsec-wrapper.h>
33 #define XMLSTREAMIO_INITIALIZED 0x01
34 #define XMLSTREAMIO_REGISTERED 0x02
36 /* Global variables */
37 /*-
38 * Enable stream I/O or not.
40 static char enableXmlStreamIO = 0x00 ;
42 static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding ;
44 extern "C"
45 int xmlStreamMatch( const char* uri )
47 css::uno::Reference< css::io::XInputStream > xInputStream ;
49 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
50 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
51 if( uri == nullptr || !m_xUriBinding.is() )
52 return 0 ;
53 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
54 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
55 //first. If this failed it would try this
56 //again with the original escaped string. However, it does not get this far, because there
57 //is another callback registered by libxml which claims to be able to handle this uri.
58 OUString sUri =
59 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
60 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
61 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
62 if (!xInputStream.is())
64 //Try the passed in uri directly.
65 //For old documents prior OOo 3.0. We did not use URIs then.
66 xInputStream = m_xUriBinding->getUriBinding(
67 OUString::createFromAscii(uri));
70 if (xInputStream.is())
71 return 1;
72 else
73 return 0 ;
76 extern "C"
77 void* xmlStreamOpen( const char* uri )
79 css::uno::Reference< css::io::XInputStream > xInputStream ;
81 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
82 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
83 if( uri == nullptr || !m_xUriBinding.is() )
84 return nullptr ;
86 //see xmlStreamMatch
87 OUString sUri =
88 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
89 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
90 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
91 if (!xInputStream.is())
93 //For old documents.
94 //try the passed in uri directly.
95 xInputStream = m_xUriBinding->getUriBinding(
96 OUString::createFromAscii(uri));
99 if( xInputStream.is() ) {
100 css::io::XInputStream* pInputStream ;
101 pInputStream = xInputStream.get() ;
102 pInputStream->acquire() ;
103 return static_cast<void*>(pInputStream) ;
107 return nullptr ;
110 extern "C"
111 int xmlStreamRead( void* context, char* buffer, int len )
113 int numbers ;
114 css::uno::Reference< css::io::XInputStream > xInputStream ;
115 css::uno::Sequence< sal_Int8 > outSeqs( len ) ;
117 numbers = 0 ;
118 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
119 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
120 if( context != nullptr ) {
121 xInputStream = static_cast<css::io::XInputStream*>(context);
122 if( !xInputStream.is() )
123 return 0 ;
125 numbers = xInputStream->readBytes( outSeqs, len ) ;
126 const sal_Int8* readBytes = outSeqs.getArray() ;
127 for( int i = 0 ; i < numbers ; i ++ )
128 *( buffer + i ) = *( readBytes + i ) ;
132 return numbers ;
135 extern "C"
136 int xmlStreamClose( void * context )
138 if( ( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) &&
139 ( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
140 if( context != nullptr ) {
141 css::io::XInputStream* pInputStream ;
142 pInputStream = static_cast<css::io::XInputStream*>(context);
143 pInputStream->release() ;
147 return 0 ;
150 XSECXMLSEC_DLLPUBLIC int xmlEnableStreamInputCallbacks()
153 if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
154 //Register the callbacks into xmlSec
155 //In order to make the xmlsec io finding the callbacks firstly,
156 //I put the callbacks at the very beginning.
158 //Cleanup the older callbacks.
159 //Notes: all none default callbacks will lose.
160 xmlSecIOCleanupCallbacks() ;
162 // Make sure that errors are reported via SAL_WARN().
163 setErrorRecorder();
164 comphelper::ScopeGuard g([] { clearErrorRecorder(); });
166 // Newer xmlsec wants the callback order in the opposite direction.
167 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible))
169 //Register the default callbacks.
170 //Notes: the error will cause xmlsec working problems.
171 int cbs = xmlSecIORegisterDefaultCallbacks() ;
172 if( cbs < 0 ) {
173 return -1 ;
176 //Register my classbacks.
177 cbs = xmlSecIORegisterCallbacks(
178 xmlStreamMatch,
179 xmlStreamOpen,
180 xmlStreamRead,
181 xmlStreamClose ) ;
182 if( cbs < 0 ) {
183 return -1 ;
186 else
188 //Register my classbacks.
189 int cbs = xmlSecIORegisterCallbacks(
190 xmlStreamMatch,
191 xmlStreamOpen,
192 xmlStreamRead,
193 xmlStreamClose ) ;
194 if( cbs < 0 ) {
195 return -1 ;
198 //Register the default callbacks.
199 //Notes: the error will cause xmlsec working problems.
200 cbs = xmlSecIORegisterDefaultCallbacks() ;
201 if( cbs < 0 ) {
202 return -1 ;
206 enableXmlStreamIO |= XMLSTREAMIO_INITIALIZED ;
209 return 0 ;
212 XSECXMLSEC_DLLPUBLIC int xmlRegisterStreamInputCallbacks(
213 css::uno::Reference< css::xml::crypto::XUriBinding > const & aUriBinding
215 if( !( enableXmlStreamIO & XMLSTREAMIO_INITIALIZED ) ) {
216 if( xmlEnableStreamInputCallbacks() < 0 )
217 return -1 ;
220 if( !( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) ) {
221 enableXmlStreamIO |= XMLSTREAMIO_REGISTERED ;
224 m_xUriBinding = aUriBinding ;
226 return 0 ;
229 XSECXMLSEC_DLLPUBLIC int xmlUnregisterStreamInputCallbacks()
231 if( enableXmlStreamIO & XMLSTREAMIO_REGISTERED ) {
232 //Clear the uri-stream binding
233 m_xUriBinding.clear() ;
235 //disable the registered flag
236 enableXmlStreamIO &= ~XMLSTREAMIO_REGISTERED ;
239 return 0 ;
242 XSECXMLSEC_DLLPUBLIC void xmlDisableStreamInputCallbacks() {
243 xmlUnregisterStreamInputCallbacks() ;
244 enableXmlStreamIO &= ~XMLSTREAMIO_INITIALIZED ;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */