Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / xmlsecurity / source / xmlsec / xmlstreamio.cxx
blobb3ee767120420383dd0172b64e7ec42c68b5e845
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 .
20 #include <sal/config.h>
21 #include <xmlsec-wrapper.h>
24 * Implementation of the I/O interfaces based on stream and URI binding
26 #include <xmlsec/xmlstreamio.hxx>
27 #include <xmlsec/errorcallback.hxx>
28 #include <rtl/ustring.hxx>
29 #include <rtl/uri.hxx>
30 #include <comphelper/scopeguard.hxx>
31 #include <sal/log.hxx>
33 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
35 static bool g_bInputCallbacksEnabled = false;
36 static bool g_bInputCallbacksRegistered = false;
38 static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding ;
40 extern "C" {
42 static int xmlStreamMatch( const char* uri )
44 css::uno::Reference< css::io::XInputStream > xInputStream ;
46 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
48 if( uri == nullptr || !m_xUriBinding.is() )
49 return 0 ;
50 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
51 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
52 //first. If this failed it would try this
53 //again with the original escaped string. However, it does not get this far, because there
54 //is another callback registered by libxml which claims to be able to handle this uri.
55 OUString sUri =
56 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
57 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
58 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
59 if (!xInputStream.is())
61 //Try the passed in uri directly.
62 //For old documents prior OOo 3.0. We did not use URIs then.
63 xInputStream = m_xUriBinding->getUriBinding(
64 OUString::createFromAscii(uri));
67 SAL_INFO("xmlsecurity.xmlsec",
68 "xmlStreamMath: uri is '" << uri << "', returning " << xInputStream.is());
69 if (xInputStream.is())
70 return 1;
71 else
72 return 0 ;
75 static void* xmlStreamOpen( const char* uri )
77 css::uno::Reference< css::io::XInputStream > xInputStream ;
79 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
81 if( uri == nullptr || !m_xUriBinding.is() )
82 return nullptr ;
84 //see xmlStreamMatch
85 OUString sUri =
86 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
87 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
88 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
89 if (!xInputStream.is())
91 //For old documents.
92 //try the passed in uri directly.
93 xInputStream = m_xUriBinding->getUriBinding(
94 OUString::createFromAscii(uri));
97 if( xInputStream.is() ) {
98 css::io::XInputStream* pInputStream ;
99 pInputStream = xInputStream.get() ;
100 pInputStream->acquire() ;
101 SAL_INFO("xmlsecurity.xmlsec",
102 "xmlStreamOpen: uri is '" << uri << "', returning context " << pInputStream);
103 return static_cast<void*>(pInputStream) ;
107 return nullptr ;
110 static int xmlStreamRead( void* context, char* buffer, int len )
112 int numbers ;
113 css::uno::Reference< css::io::XInputStream > xInputStream ;
114 css::uno::Sequence< sal_Int8 > outSeqs( len ) ;
116 numbers = 0 ;
117 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
119 if( context != nullptr ) {
120 xInputStream = static_cast<css::io::XInputStream*>(context);
121 if( !xInputStream.is() )
122 return 0 ;
124 numbers = xInputStream->readBytes( outSeqs, len ) ;
125 const sal_Int8* readBytes = outSeqs.getArray() ;
126 for( int i = 0 ; i < numbers ; i ++ )
127 *( buffer + i ) = *( readBytes + i ) ;
131 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: context is " << context << ", buffer is now '"
132 << OString(buffer, numbers) << "'");
133 return numbers ;
136 static int xmlStreamClose( void * context )
138 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
140 if( context != nullptr ) {
141 css::io::XInputStream* pInputStream ;
142 pInputStream = static_cast<css::io::XInputStream*>(context);
143 pInputStream->release() ;
144 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: closed context " << context);
148 return 0 ;
153 int xmlEnableStreamInputCallbacks()
155 if (!g_bInputCallbacksEnabled)
157 //Register the callbacks into xmlSec
158 //In order to make the xmlsec io finding the callbacks firstly,
159 //I put the callbacks at the very beginning.
161 //Cleanup the older callbacks.
162 //Notes: all none default callbacks will lose.
163 xmlSecIOCleanupCallbacks() ;
165 // Make sure that errors are reported via SAL_WARN().
166 setErrorRecorder();
167 comphelper::ScopeGuard g([] { clearErrorRecorder(); });
169 // Newer xmlsec wants the callback order in the opposite direction.
170 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible))
172 //Register the default callbacks.
173 //Notes: the error will cause xmlsec working problems.
174 int cbs = xmlSecIORegisterDefaultCallbacks() ;
175 if( cbs < 0 ) {
176 return -1 ;
179 //Register my classbacks.
180 cbs = xmlSecIORegisterCallbacks(
181 xmlStreamMatch,
182 xmlStreamOpen,
183 xmlStreamRead,
184 xmlStreamClose ) ;
185 if( cbs < 0 ) {
186 return -1 ;
189 else
191 //Register my classbacks.
192 int cbs = xmlSecIORegisterCallbacks(
193 xmlStreamMatch,
194 xmlStreamOpen,
195 xmlStreamRead,
196 xmlStreamClose ) ;
197 if( cbs < 0 ) {
198 return -1 ;
201 //Register the default callbacks.
202 //Notes: the error will cause xmlsec working problems.
203 cbs = xmlSecIORegisterDefaultCallbacks() ;
204 if( cbs < 0 ) {
205 return -1 ;
209 g_bInputCallbacksEnabled = true;
212 return 0 ;
215 int xmlRegisterStreamInputCallbacks(
216 css::uno::Reference< css::xml::crypto::XUriBinding > const & aUriBinding
218 if (!g_bInputCallbacksEnabled)
220 if( xmlEnableStreamInputCallbacks() < 0 )
221 return -1 ;
224 if (!g_bInputCallbacksRegistered)
225 g_bInputCallbacksRegistered = true;
227 m_xUriBinding = aUriBinding ;
229 return 0 ;
232 int xmlUnregisterStreamInputCallbacks()
234 if (g_bInputCallbacksRegistered)
236 //Clear the uri-stream binding
237 m_xUriBinding.clear() ;
239 //disable the registered flag
240 g_bInputCallbacksRegistered = false;
243 return 0 ;
246 void xmlDisableStreamInputCallbacks() {
247 xmlUnregisterStreamInputCallbacks() ;
248 g_bInputCallbacksEnabled = false;
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */