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 "xmlstreamio.hxx"
25 #include <rtl/ustring.hxx>
26 #include <rtl/uri.hxx>
28 #include <libxml/uri.h>
29 #include "xmlsecurity/xmlsec-wrapper.h"
31 #define XMLSTREAMIO_INITIALIZED 0x01
32 #define XMLSTREAMIO_REGISTERED 0x02
34 /* Global variables */
36 * Enable stream I/O or not.
38 static char enableXmlStreamIO
= 0x00 ;
40 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XUriBinding
> m_xUriBinding
;
43 int xmlStreamMatch( const char* uri
)
45 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
47 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
48 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
49 if( uri
== NULL
|| !m_xUriBinding
.is() )
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.
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 if (xInputStream
.is())
75 void* xmlStreamOpen( const char* uri
)
77 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
79 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
80 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
81 if( uri
== NULL
|| !m_xUriBinding
.is() )
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())
92 //try the passed in uri directly.
93 xInputStream
= m_xUriBinding
->getUriBinding(
94 OUString::createFromAscii(uri
));
97 if( xInputStream
.is() ) {
98 ::com::sun::star::io::XInputStream
* pInputStream
;
99 pInputStream
= xInputStream
.get() ;
100 pInputStream
->acquire() ;
101 return ( void* )pInputStream
;
109 int xmlStreamRead( void* context
, char* buffer
, int len
)
112 ::com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> xInputStream
;
113 ::com::sun::star::uno::Sequence
< sal_Int8
> outSeqs( len
) ;
116 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
117 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
118 if( context
!= NULL
) {
119 xInputStream
= static_cast<com::sun::star::io::XInputStream
*>(context
);
120 if( !xInputStream
.is() )
123 numbers
= xInputStream
->readBytes( outSeqs
, len
) ;
124 const sal_Int8
* readBytes
= ( const sal_Int8
* )outSeqs
.getArray() ;
125 for( int i
= 0 ; i
< numbers
; i
++ )
126 *( buffer
+ i
) = *( readBytes
+ i
) ;
134 int xmlStreamClose( void * context
)
136 if( ( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) &&
137 ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
138 if( context
!= NULL
) {
139 ::com::sun::star::io::XInputStream
* pInputStream
;
140 pInputStream
= static_cast<css::io::XInputStream
*>(context
);
141 pInputStream
->release() ;
148 int xmlEnableStreamInputCallbacks()
151 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
152 //Register the callbacks into xmlSec
153 //In order to make the xmlsec io finding the callbacks firstly,
154 //I put the callbacks at the very beginning.
156 //Cleanup the older callbacks.
157 //Notes: all none default callbacks will lose.
158 xmlSecIOCleanupCallbacks() ;
160 //Register my classbacks.
161 int cbs
= xmlSecIORegisterCallbacks(
170 //Register the default callbacks.
171 //Notes: the error will cause xmlsec working problems.
172 cbs
= xmlSecIORegisterDefaultCallbacks() ;
177 enableXmlStreamIO
|= XMLSTREAMIO_INITIALIZED
;
183 int xmlRegisterStreamInputCallbacks(
184 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XUriBinding
>& aUriBinding
186 if( !( enableXmlStreamIO
& XMLSTREAMIO_INITIALIZED
) ) {
187 if( xmlEnableStreamInputCallbacks() < 0 )
191 if( !( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
192 enableXmlStreamIO
|= XMLSTREAMIO_REGISTERED
;
195 m_xUriBinding
= aUriBinding
;
200 int xmlUnregisterStreamInputCallbacks()
202 if( ( enableXmlStreamIO
& XMLSTREAMIO_REGISTERED
) ) {
203 //Clear the uir-stream binding
204 m_xUriBinding
.clear() ;
206 //disable the registered flag
207 enableXmlStreamIO
&= ~XMLSTREAMIO_REGISTERED
;
213 void xmlDisableStreamInputCallbacks() {
214 xmlUnregisterStreamInputCallbacks() ;
215 enableXmlStreamIO
&= ~XMLSTREAMIO_INITIALIZED
;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */