xref: /AOO41X/main/xmlsecurity/tools/standalone/csfit/helper.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <stdio.h>
29*cdf0e10cSrcweir /*#include <libxml/xmlstring.h>
30*cdf0e10cSrcweir */
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <rtl/ustring.hxx>
33*cdf0e10cSrcweir #include <pk11func.h>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
36*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
38*cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
39*cdf0e10cSrcweir #include <ucbhelper/configurationkeys.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/document/XExporter.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/document/XImporter.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XUriBinding.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
64*cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir using namespace ::rtl ;
67*cdf0e10cSrcweir using namespace ::cppu ;
68*cdf0e10cSrcweir using namespace ::com::sun::star ;
69*cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
70*cdf0e10cSrcweir using namespace ::com::sun::star::io ;
71*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
72*cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
73*cdf0e10cSrcweir using namespace ::com::sun::star::document ;
74*cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
75*cdf0e10cSrcweir using namespace ::com::sun::star::bridge ;
76*cdf0e10cSrcweir using namespace ::com::sun::star::registry ;
77*cdf0e10cSrcweir using namespace ::com::sun::star::task ;
78*cdf0e10cSrcweir using namespace ::com::sun::star::xml ;
79*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
80*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax ;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir /**
84*cdf0e10cSrcweir  * Helper: Implementation of XInputStream
85*cdf0e10cSrcweir  */
86*cdf0e10cSrcweir class OInputStream : public WeakImplHelper1 < XInputStream >
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	public:
89*cdf0e10cSrcweir 		OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 		virtual sal_Int32 SAL_CALL readBytes(
92*cdf0e10cSrcweir 			Sequence< sal_Int8 >& aData ,
93*cdf0e10cSrcweir 			sal_Int32 nBytesToRead
94*cdf0e10cSrcweir 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
95*cdf0e10cSrcweir 		{
96*cdf0e10cSrcweir 			nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ?
97*cdf0e10cSrcweir 				m_seq.getLength() - nPos :
98*cdf0e10cSrcweir 				nBytesToRead ;
99*cdf0e10cSrcweir 			aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ;
100*cdf0e10cSrcweir 			nPos += nBytesToRead ;
101*cdf0e10cSrcweir 			return nBytesToRead ;
102*cdf0e10cSrcweir 		}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 		virtual sal_Int32 SAL_CALL readSomeBytes(
105*cdf0e10cSrcweir 			::com::sun::star::uno::Sequence< sal_Int8 >& aData ,
106*cdf0e10cSrcweir 			sal_Int32 nMaxBytesToRead
107*cdf0e10cSrcweir 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
108*cdf0e10cSrcweir 		{
109*cdf0e10cSrcweir 			return readBytes( aData, nMaxBytesToRead ) ;
110*cdf0e10cSrcweir 		}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		virtual void SAL_CALL skipBytes(
113*cdf0e10cSrcweir 			sal_Int32 nBytesToSkip
114*cdf0e10cSrcweir 		) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
115*cdf0e10cSrcweir 		{
116*cdf0e10cSrcweir 			// not implemented
117*cdf0e10cSrcweir 		}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		virtual sal_Int32 SAL_CALL available(
120*cdf0e10cSrcweir 			void
121*cdf0e10cSrcweir 		) throw( NotConnectedException, IOException, RuntimeException )
122*cdf0e10cSrcweir 		{
123*cdf0e10cSrcweir 			return m_seq.getLength() - nPos ;
124*cdf0e10cSrcweir 		}
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		virtual void SAL_CALL closeInput(
127*cdf0e10cSrcweir 			void
128*cdf0e10cSrcweir 		) throw( NotConnectedException, IOException, RuntimeException )
129*cdf0e10cSrcweir 		{
130*cdf0e10cSrcweir 			// not needed
131*cdf0e10cSrcweir 		}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	private:
134*cdf0e10cSrcweir 		sal_Int32 nPos;
135*cdf0e10cSrcweir 		Sequence< sal_Int8> m_seq;
136*cdf0e10cSrcweir } ;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir /**
139*cdf0e10cSrcweir  * Helper : create a input stream from a file
140*cdf0e10cSrcweir  */
141*cdf0e10cSrcweir Reference< XInputStream > createStreamFromFile( const OUString sFile ) ;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir /**
144*cdf0e10cSrcweir  * Helper: Implementation of XOutputStream
145*cdf0e10cSrcweir  */
146*cdf0e10cSrcweir class OOutputStream : public WeakImplHelper1 < XOutputStream >
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	public:
149*cdf0e10cSrcweir 		OOutputStream( const char *pcFile ) {
150*cdf0e10cSrcweir 			strcpy( m_pcFile , pcFile ) ;
151*cdf0e10cSrcweir 			m_f = 0 ;
152*cdf0e10cSrcweir 		}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		virtual void SAL_CALL writeBytes(
155*cdf0e10cSrcweir 			const Sequence< sal_Int8 >& aData
156*cdf0e10cSrcweir 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
157*cdf0e10cSrcweir 			if( !m_f ) {
158*cdf0e10cSrcweir 				m_f = fopen( m_pcFile , "w" ) ;
159*cdf0e10cSrcweir 			}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 			fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ;
162*cdf0e10cSrcweir 		}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 		virtual void SAL_CALL flush(
165*cdf0e10cSrcweir 			void
166*cdf0e10cSrcweir 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
167*cdf0e10cSrcweir 			fflush( m_f ) ;
168*cdf0e10cSrcweir 		}
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 		virtual void SAL_CALL closeOutput(
171*cdf0e10cSrcweir 			void
172*cdf0e10cSrcweir 		) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
173*cdf0e10cSrcweir 			fclose( m_f ) ;
174*cdf0e10cSrcweir 			m_f = 0 ;
175*cdf0e10cSrcweir 		}
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	private:
178*cdf0e10cSrcweir 		char m_pcFile[256];
179*cdf0e10cSrcweir 		FILE *m_f;
180*cdf0e10cSrcweir } ;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir /**
183*cdf0e10cSrcweir  * Helper: Implementation of XUriBinding
184*cdf0e10cSrcweir  */
185*cdf0e10cSrcweir class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding >
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	public:
188*cdf0e10cSrcweir 		OUriBinding() {
189*cdf0e10cSrcweir 			//Do nothing
190*cdf0e10cSrcweir 		}
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		OUriBinding(
193*cdf0e10cSrcweir 			::rtl::OUString& aUri,
194*cdf0e10cSrcweir 			::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) {
195*cdf0e10cSrcweir 			m_vUris.push_back( aUri ) ;
196*cdf0e10cSrcweir 			m_vStreams.push_back( aInputStream ) ;
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		virtual void SAL_CALL setUriBinding(
200*cdf0e10cSrcweir 			const ::rtl::OUString& aUri ,
201*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream
202*cdf0e10cSrcweir 		) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
203*cdf0e10cSrcweir 			m_vUris.push_back( aUri ) ;
204*cdf0e10cSrcweir 			m_vStreams.push_back( aInputStream ) ;
205*cdf0e10cSrcweir 		}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		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 ) {
208*cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 			int size = m_vUris.size() ;
211*cdf0e10cSrcweir 			for( int i = 0 ; i<size ; ++i ) {
212*cdf0e10cSrcweir 				if( uri == m_vUris[i] ) {
213*cdf0e10cSrcweir 					xInputStream = m_vStreams[i];
214*cdf0e10cSrcweir 					break;
215*cdf0e10cSrcweir 				}
216*cdf0e10cSrcweir 			}
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 			return xInputStream;
219*cdf0e10cSrcweir 		}
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	private:
222*cdf0e10cSrcweir 		std::vector< ::rtl::OUString > m_vUris ;
223*cdf0e10cSrcweir 		std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ;
224*cdf0e10cSrcweir } ;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir /**
227*cdf0e10cSrcweir  * Helper : set a output stream to a file
228*cdf0e10cSrcweir  */
229*cdf0e10cSrcweir Reference< XOutputStream > createStreamToFile( const OUString sFile ) ;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir /**
232*cdf0e10cSrcweir  * Helper : get service manager and context
233*cdf0e10cSrcweir  */
234*cdf0e10cSrcweir Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl  ) throw( RuntimeException , Exception ) ;
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir /**
237*cdf0e10cSrcweir  * Helper : Get password function for PKCS11 slot
238*cdf0e10cSrcweir  */
239*cdf0e10cSrcweir char* PriPK11PasswordFunc( PK11SlotInfo *slot , PRBool retry , void* arg ) ;
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 
242