1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/xml/crypto/XUriBinding.hpp>
32 #include <xmlsec-wrapper.h>
34 static bool g_bInputCallbacksEnabled
= false;
35 static bool g_bInputCallbacksRegistered
= false;
37 static css::uno::Reference
< css::xml::crypto::XUriBinding
> m_xUriBinding
;
41 static int xmlStreamMatch( const char* uri
)
43 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
45 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
47 if( uri
== nullptr || !m_xUriBinding
.is() )
49 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
50 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
51 //first. If this failed it would try this
52 //again with the original escaped string. However, it does not get this far, because there
53 //is another callback registered by libxml which claims to be able to handle this uri.
55 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
56 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
57 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
58 if (!xInputStream
.is())
60 //Try the passed in uri directly.
61 //For old documents prior OOo 3.0. We did not use URIs then.
62 xInputStream
= m_xUriBinding
->getUriBinding(
63 OUString::createFromAscii(uri
));
66 if (xInputStream
.is())
72 static void* xmlStreamOpen( const char* uri
)
74 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
76 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
78 if( uri
== nullptr || !m_xUriBinding
.is() )
83 ::rtl::Uri::encode( OUString::createFromAscii( uri
),
84 rtl_UriCharClassUric
, rtl_UriEncodeKeepEscapes
, RTL_TEXTENCODING_UTF8
);
85 xInputStream
= m_xUriBinding
->getUriBinding( sUri
) ;
86 if (!xInputStream
.is())
89 //try the passed in uri directly.
90 xInputStream
= m_xUriBinding
->getUriBinding(
91 OUString::createFromAscii(uri
));
94 if( xInputStream
.is() ) {
95 css::io::XInputStream
* pInputStream
;
96 pInputStream
= xInputStream
.get() ;
97 pInputStream
->acquire() ;
98 return static_cast<void*>(pInputStream
) ;
105 static int xmlStreamRead( void* context
, char* buffer
, int len
)
108 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
109 css::uno::Sequence
< sal_Int8
> outSeqs( len
) ;
112 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
114 if( context
!= nullptr ) {
115 xInputStream
= static_cast<css::io::XInputStream
*>(context
);
116 if( !xInputStream
.is() )
119 numbers
= xInputStream
->readBytes( outSeqs
, len
) ;
120 const sal_Int8
* readBytes
= outSeqs
.getArray() ;
121 for( int i
= 0 ; i
< numbers
; i
++ )
122 *( buffer
+ i
) = *( readBytes
+ i
) ;
129 static int xmlStreamClose( void * context
)
131 if (g_bInputCallbacksEnabled
&& g_bInputCallbacksRegistered
)
133 if( context
!= nullptr ) {
134 css::io::XInputStream
* pInputStream
;
135 pInputStream
= static_cast<css::io::XInputStream
*>(context
);
136 pInputStream
->release() ;
145 int xmlEnableStreamInputCallbacks()
147 if (!g_bInputCallbacksEnabled
)
149 //Register the callbacks into xmlSec
150 //In order to make the xmlsec io finding the callbacks firstly,
151 //I put the callbacks at the very beginning.
153 //Cleanup the older callbacks.
154 //Notes: all none default callbacks will lose.
155 xmlSecIOCleanupCallbacks() ;
157 // Make sure that errors are reported via SAL_WARN().
159 comphelper::ScopeGuard
g([] { clearErrorRecorder(); });
161 // Newer xmlsec wants the callback order in the opposite direction.
162 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible
))
164 //Register the default callbacks.
165 //Notes: the error will cause xmlsec working problems.
166 int cbs
= xmlSecIORegisterDefaultCallbacks() ;
171 //Register my classbacks.
172 cbs
= xmlSecIORegisterCallbacks(
183 //Register my classbacks.
184 int cbs
= xmlSecIORegisterCallbacks(
193 //Register the default callbacks.
194 //Notes: the error will cause xmlsec working problems.
195 cbs
= xmlSecIORegisterDefaultCallbacks() ;
201 g_bInputCallbacksEnabled
= true;
207 int xmlRegisterStreamInputCallbacks(
208 css::uno::Reference
< css::xml::crypto::XUriBinding
> const & aUriBinding
210 if (!g_bInputCallbacksEnabled
)
212 if( xmlEnableStreamInputCallbacks() < 0 )
216 if (!g_bInputCallbacksRegistered
)
217 g_bInputCallbacksRegistered
= true;
219 m_xUriBinding
= aUriBinding
;
224 int xmlUnregisterStreamInputCallbacks()
226 if (g_bInputCallbacksRegistered
)
228 //Clear the uri-stream binding
229 m_xUriBinding
.clear() ;
231 //disable the registered flag
232 g_bInputCallbacksRegistered
= false;
238 void xmlDisableStreamInputCallbacks() {
239 xmlUnregisterStreamInputCallbacks() ;
240 g_bInputCallbacksEnabled
= false;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */