1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <stdio.h>
25 /*#include <libxml/xmlstring.h>
26 */
27 
28 #include <rtl/ustring.hxx>
29 #include <pk11func.h>
30 
31 #include <cppuhelper/bootstrap.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <cppuhelper/servicefactory.hxx>
34 #include <ucbhelper/contentbroker.hxx>
35 #include <ucbhelper/configurationkeys.hxx>
36 
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
39 #include <com/sun/star/registry/XImplementationRegistration.hpp>
40 #include <com/sun/star/document/XFilter.hpp>
41 #include <com/sun/star/document/XExporter.hpp>
42 #include <com/sun/star/document/XImporter.hpp>
43 #include <com/sun/star/io/XInputStream.hpp>
44 #include <com/sun/star/io/XOutputStream.hpp>
45 #include <com/sun/star/io/XActiveDataSource.hpp>
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #include <com/sun/star/beans/XPropertySet.hpp>
48 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
49 
50 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
51 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
52 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
53 
54 #include <com/sun/star/xml/sax/XParser.hpp>
55 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
56 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
57 #include <com/sun/star/xml/sax/InputSource.hpp>
58 #include <com/sun/star/io/XInputStream.hpp>
59 #include <com/sun/star/io/XOutputStream.hpp>
60 #include <com/sun/star/uno/XNamingService.hpp>
61 
62 using namespace ::rtl ;
63 using namespace ::cppu ;
64 using namespace ::com::sun::star ;
65 using namespace ::com::sun::star::uno ;
66 using namespace ::com::sun::star::io ;
67 using namespace ::com::sun::star::ucb ;
68 using namespace ::com::sun::star::beans ;
69 using namespace ::com::sun::star::document ;
70 using namespace ::com::sun::star::lang ;
71 using namespace ::com::sun::star::bridge ;
72 using namespace ::com::sun::star::registry ;
73 using namespace ::com::sun::star::task ;
74 using namespace ::com::sun::star::xml ;
75 using namespace ::com::sun::star::xml::wrapper ;
76 using namespace ::com::sun::star::xml::sax ;
77 
78 
79 /**
80  * Helper: Implementation of XInputStream
81  */
82 class OInputStream : public WeakImplHelper1 < XInputStream >
83 {
84 	public:
OInputStream(const Sequence<sal_Int8> & seq)85 		OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {}
86 
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)87 		virtual sal_Int32 SAL_CALL readBytes(
88 			Sequence< sal_Int8 >& aData ,
89 			sal_Int32 nBytesToRead
90 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
91 		{
92 			nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ?
93 				m_seq.getLength() - nPos :
94 				nBytesToRead ;
95 			aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ;
96 			nPos += nBytesToRead ;
97 			return nBytesToRead ;
98 		}
99 
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)100 		virtual sal_Int32 SAL_CALL readSomeBytes(
101 			::com::sun::star::uno::Sequence< sal_Int8 >& aData ,
102 			sal_Int32 nMaxBytesToRead
103 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
104 		{
105 			return readBytes( aData, nMaxBytesToRead ) ;
106 		}
107 
skipBytes(sal_Int32 nBytesToSkip)108 		virtual void SAL_CALL skipBytes(
109 			sal_Int32 nBytesToSkip
110 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
111 		{
112 			// not implemented
113 		}
114 
available(void)115 		virtual sal_Int32 SAL_CALL available(
116 			void
117 		) throw( NotConnectedException, IOException, RuntimeException )
118 		{
119 			return m_seq.getLength() - nPos ;
120 		}
121 
closeInput(void)122 		virtual void SAL_CALL closeInput(
123 			void
124 		) throw( NotConnectedException, IOException, RuntimeException )
125 		{
126 			// not needed
127 		}
128 
129 	private:
130 		sal_Int32 nPos;
131 		Sequence< sal_Int8> m_seq;
132 } ;
133 
134 /**
135  * Helper : create a input stream from a file
136  */
137 Reference< XInputStream > createStreamFromFile( const OUString sFile ) ;
138 
139 /**
140  * Helper: Implementation of XOutputStream
141  */
142 class OOutputStream : public WeakImplHelper1 < XOutputStream >
143 {
144 	public:
OOutputStream(const char * pcFile)145 		OOutputStream( const char *pcFile ) {
146 			strcpy( m_pcFile , pcFile ) ;
147 			m_f = 0 ;
148 		}
149 
writeBytes(const Sequence<sal_Int8> & aData)150 		virtual void SAL_CALL writeBytes(
151 			const Sequence< sal_Int8 >& aData
152 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
153 			if( !m_f ) {
154 				m_f = fopen( m_pcFile , "w" ) ;
155 			}
156 
157 			fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ;
158 		}
159 
flush(void)160 		virtual void SAL_CALL flush(
161 			void
162 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
163 			fflush( m_f ) ;
164 		}
165 
closeOutput(void)166 		virtual void SAL_CALL closeOutput(
167 			void
168 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
169 			fclose( m_f ) ;
170 			m_f = 0 ;
171 		}
172 
173 	private:
174 		char m_pcFile[256];
175 		FILE *m_f;
176 } ;
177 
178 /**
179  * Helper: Implementation of XUriBinding
180  */
181 class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding >
182 {
183 	public:
OUriBinding()184 		OUriBinding() {
185 			//Do nothing
186 		}
187 
OUriBinding(::rtl::OUString & aUri,::com::sun::star::uno::Reference<com::sun::star::io::XInputStream> & aInputStream)188 		OUriBinding(
189 			::rtl::OUString& aUri,
190 			::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) {
191 			m_vUris.push_back( aUri ) ;
192 			m_vStreams.push_back( aInputStream ) ;
193 		}
194 
setUriBinding(const::rtl::OUString & aUri,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & aInputStream)195 		virtual void SAL_CALL setUriBinding(
196 			const ::rtl::OUString& aUri ,
197 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream
198 		) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
199 			m_vUris.push_back( aUri ) ;
200 			m_vStreams.push_back( aInputStream ) ;
201 		}
202 
getUriBinding(const::rtl::OUString & uri)203 		virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getUriBinding( const ::rtl::OUString& uri ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
204 			::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ;
205 
206 			int size = m_vUris.size() ;
207 			for( int i = 0 ; i<size ; ++i ) {
208 				if( uri == m_vUris[i] ) {
209 					xInputStream = m_vStreams[i];
210 					break;
211 				}
212 			}
213 
214 			return xInputStream;
215 		}
216 
217 	private:
218 		std::vector< ::rtl::OUString > m_vUris ;
219 		std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ;
220 } ;
221 
222 /**
223  * Helper : set a output stream to a file
224  */
225 Reference< XOutputStream > createStreamToFile( const OUString sFile ) ;
226 
227 /**
228  * Helper : get service manager and context
229  */
230 Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl  ) throw( RuntimeException , Exception ) ;
231 
232 /**
233  * Helper : Get password function for PKCS11 slot
234  */
235 char* PriPK11PasswordFunc( PK11SlotInfo *slot , PRBool retry , void* arg ) ;
236 
237 
238