tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / xmlsecurity / source / xmlsec / xmlstreamio.cxx
blob2fa32955d6a80c518d52876a4e3e6f191a59e650
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>
22 #include <xmlsec/io.h>
25 * Implementation of the I/O interfaces based on stream and URI binding
27 #include <xmlsec/xmlstreamio.hxx>
28 #include <xmlsec/errorcallback.hxx>
29 #include <rtl/ustring.hxx>
30 #include <rtl/uri.hxx>
31 #include <comphelper/scopeguard.hxx>
32 #include <sal/log.hxx>
34 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
36 static bool g_bInputCallbacksEnabled = false;
37 static bool g_bInputCallbacksRegistered = false;
39 static css::uno::Reference< css::xml::crypto::XUriBinding > m_xUriBinding ;
41 extern "C" {
43 static int xmlStreamMatch( const char* uri )
45 css::uno::Reference< css::io::XInputStream > xInputStream ;
47 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
49 if( uri == nullptr || !m_xUriBinding.is() )
50 return 0 ;
51 //XMLSec first unescapes the uri and calls this function. For example, we pass the Uri
52 //ObjectReplacements/Object%201 then XMLSec passes ObjectReplacements/Object 1
53 //first. If this failed it would try this
54 //again with the original escaped string. However, it does not get this far, because there
55 //is another callback registered by libxml which claims to be able to handle this uri.
56 OUString sUri =
57 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
58 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
59 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
60 if (!xInputStream.is())
62 //Try the passed in uri directly.
63 //For old documents prior OOo 3.0. We did not use URIs then.
64 xInputStream = m_xUriBinding->getUriBinding(
65 OUString::createFromAscii(uri));
68 SAL_INFO("xmlsecurity.xmlsec",
69 "xmlStreamMath: uri is '" << uri << "', returning " << xInputStream.is());
70 if (xInputStream.is())
71 return 1;
72 else
73 return 0 ;
76 static void* xmlStreamOpen( const char* uri )
78 css::uno::Reference< css::io::XInputStream > xInputStream ;
80 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
82 if( uri == nullptr || !m_xUriBinding.is() )
83 return nullptr ;
85 //see xmlStreamMatch
86 OUString sUri =
87 ::rtl::Uri::encode( OUString::createFromAscii( uri ),
88 rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
89 xInputStream = m_xUriBinding->getUriBinding( sUri ) ;
90 if (!xInputStream.is())
92 //For old documents.
93 //try the passed in uri directly.
94 xInputStream = m_xUriBinding->getUriBinding(
95 OUString::createFromAscii(uri));
98 if( xInputStream.is() ) {
99 css::io::XInputStream* pInputStream ;
100 pInputStream = xInputStream.get() ;
101 pInputStream->acquire() ;
102 SAL_INFO("xmlsecurity.xmlsec",
103 "xmlStreamOpen: uri is '" << uri << "', returning context " << pInputStream);
104 return static_cast<void*>(pInputStream) ;
108 return nullptr ;
111 static 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 (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
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 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: context is " << context << ", buffer is now '"
133 << OString(buffer, numbers) << "'");
134 return numbers ;
137 static int xmlStreamClose( void * context )
139 if (g_bInputCallbacksEnabled && g_bInputCallbacksRegistered)
141 if( context != nullptr ) {
142 css::io::XInputStream* pInputStream ;
143 pInputStream = static_cast<css::io::XInputStream*>(context);
144 pInputStream->release() ;
145 SAL_INFO("xmlsecurity.xmlsec", "xmlStreamRead: closed context " << context);
149 return 0 ;
154 int xmlEnableStreamInputCallbacks()
156 if (!g_bInputCallbacksEnabled)
158 //Register the callbacks into xmlSec
159 //In order to make the xmlsec io finding the callbacks firstly,
160 //I put the callbacks at the very beginning.
162 //Cleanup the older callbacks.
163 //Notes: all none default callbacks will lose.
164 xmlSecIOCleanupCallbacks() ;
166 // Make sure that errors are reported via SAL_WARN().
167 setErrorRecorder();
168 comphelper::ScopeGuard g([] { clearErrorRecorder(); });
170 // Newer xmlsec wants the callback order in the opposite direction.
171 if (xmlSecCheckVersionExt(1, 2, 26, xmlSecCheckVersionABICompatible))
173 //Register the default callbacks.
174 //Notes: the error will cause xmlsec working problems.
175 int cbs = xmlSecIORegisterDefaultCallbacks() ;
176 if( cbs < 0 ) {
177 return -1 ;
180 //Register my classbacks.
181 cbs = xmlSecIORegisterCallbacks(
182 xmlStreamMatch,
183 xmlStreamOpen,
184 xmlStreamRead,
185 xmlStreamClose ) ;
186 if( cbs < 0 ) {
187 return -1 ;
190 else
192 //Register my classbacks.
193 int cbs = xmlSecIORegisterCallbacks(
194 xmlStreamMatch,
195 xmlStreamOpen,
196 xmlStreamRead,
197 xmlStreamClose ) ;
198 if( cbs < 0 ) {
199 return -1 ;
202 //Register the default callbacks.
203 //Notes: the error will cause xmlsec working problems.
204 cbs = xmlSecIORegisterDefaultCallbacks() ;
205 if( cbs < 0 ) {
206 return -1 ;
210 g_bInputCallbacksEnabled = true;
213 return 0 ;
216 int xmlRegisterStreamInputCallbacks(
217 css::uno::Reference< css::xml::crypto::XUriBinding > const & aUriBinding
219 if (!g_bInputCallbacksEnabled)
221 if( xmlEnableStreamInputCallbacks() < 0 )
222 return -1 ;
225 if (!g_bInputCallbacksRegistered)
226 g_bInputCallbacksRegistered = true;
228 m_xUriBinding = aUriBinding ;
230 return 0 ;
233 int xmlUnregisterStreamInputCallbacks()
235 if (g_bInputCallbacksRegistered)
237 //Clear the uri-stream binding
238 m_xUriBinding.clear() ;
240 //disable the registered flag
241 g_bInputCallbacksRegistered = false;
244 return 0 ;
247 void xmlDisableStreamInputCallbacks() {
248 xmlUnregisterStreamInputCallbacks() ;
249 g_bInputCallbacksEnabled = false;
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */