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