1dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5dde7d3faSAndrew Rist  * distributed with this work for additional information
6dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10dde7d3faSAndrew Rist  *
11dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12dde7d3faSAndrew Rist  *
13dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18dde7d3faSAndrew Rist  * under the License.
19dde7d3faSAndrew Rist  *
20dde7d3faSAndrew Rist  *************************************************************/
21dde7d3faSAndrew Rist 
22dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
27cdf0e10cSrcweir #include <com/sun/star/embed/XEncryptionProtectedSource2.hpp>
28cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/beans/IllegalTypeException.hpp>
33cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContext.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XDigestContextSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/xml/crypto/DigestID.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <rtl/digest.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <ucbhelper/content.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <comphelper/fileformat.h>
42cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43cdf0e10cSrcweir #include <comphelper/documentconstants.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::com::sun::star;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace comphelper {
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageFactory(const uno::Reference<lang::XMultiServiceFactory> & xSF)53cdf0e10cSrcweir uno::Reference< lang::XSingleServiceFactory > OStorageHelper::GetStorageFactory(
54cdf0e10cSrcweir 							const uno::Reference< lang::XMultiServiceFactory >& xSF )
55cdf0e10cSrcweir 		throw ( uno::Exception )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
58cdf0e10cSrcweir 	if ( !xFactory.is() )
59cdf0e10cSrcweir 		throw uno::RuntimeException();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
62cdf0e10cSrcweir 					xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
63cdf0e10cSrcweir 					uno::UNO_QUERY );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	if ( !xStorageFactory.is() )
66cdf0e10cSrcweir 		throw uno::RuntimeException();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	return xStorageFactory;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // ----------------------------------------------------------------------
GetFileSystemStorageFactory(const uno::Reference<lang::XMultiServiceFactory> & xSF)72cdf0e10cSrcweir uno::Reference< lang::XSingleServiceFactory > OStorageHelper::GetFileSystemStorageFactory(
73cdf0e10cSrcweir 							const uno::Reference< lang::XMultiServiceFactory >& xSF )
74cdf0e10cSrcweir 		throw ( uno::Exception )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
77cdf0e10cSrcweir 	if ( !xFactory.is() )
78cdf0e10cSrcweir 		throw uno::RuntimeException();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
81cdf0e10cSrcweir 					xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.FileSystemStorageFactory" ) ),
82cdf0e10cSrcweir 					uno::UNO_QUERY );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	if ( !xStorageFactory.is() )
85cdf0e10cSrcweir 		throw uno::RuntimeException();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	return xStorageFactory;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // ----------------------------------------------------------------------
GetTemporaryStorage(const uno::Reference<lang::XMultiServiceFactory> & xFactory)91cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetTemporaryStorage(
92cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
93cdf0e10cSrcweir 	throw ( uno::Exception )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstance(),
96cdf0e10cSrcweir 													uno::UNO_QUERY );
97cdf0e10cSrcweir 	if ( !xTempStorage.is() )
98cdf0e10cSrcweir 		throw uno::RuntimeException();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	return xTempStorage;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageFromURL(const::rtl::OUString & aURL,sal_Int32 nStorageMode,const uno::Reference<lang::XMultiServiceFactory> & xFactory)104cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromURL(
105cdf0e10cSrcweir 			const ::rtl::OUString& aURL,
106cdf0e10cSrcweir 			sal_Int32 nStorageMode,
107cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
108cdf0e10cSrcweir 	throw ( uno::Exception )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
111cdf0e10cSrcweir 	aArgs[0] <<= aURL;
112cdf0e10cSrcweir 	aArgs[1] <<= nStorageMode;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
115cdf0e10cSrcweir 													uno::UNO_QUERY );
116cdf0e10cSrcweir 	if ( !xTempStorage.is() )
117cdf0e10cSrcweir 		throw uno::RuntimeException();
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	return xTempStorage;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageFromURL2(const::rtl::OUString & aURL,sal_Int32 nStorageMode,const uno::Reference<lang::XMultiServiceFactory> & xFactory)123cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromURL2(
124cdf0e10cSrcweir 			const ::rtl::OUString& aURL,
125cdf0e10cSrcweir 			sal_Int32 nStorageMode,
126cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
127cdf0e10cSrcweir 	throw ( uno::Exception )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
130cdf0e10cSrcweir 	aArgs[0] <<= aURL;
131cdf0e10cSrcweir 	aArgs[1] <<= nStorageMode;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     uno::Reference< lang::XSingleServiceFactory > xFact;
134cdf0e10cSrcweir     try {
135cdf0e10cSrcweir         ::ucbhelper::Content aCntnt( aURL,
136cdf0e10cSrcweir             uno::Reference< ::com::sun::star::ucb::XCommandEnvironment > () );
137cdf0e10cSrcweir         if (aCntnt.isDocument()) {
138cdf0e10cSrcweir             xFact = GetStorageFactory( xFactory );
139cdf0e10cSrcweir         } else {
140cdf0e10cSrcweir             xFact = GetFileSystemStorageFactory( xFactory );
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir     } catch (uno::Exception &) { }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     if (!xFact.is()) throw uno::RuntimeException();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage(
147cdf0e10cSrcweir         xFact->createInstanceWithArguments( aArgs ), uno::UNO_QUERY );
148cdf0e10cSrcweir 	if ( !xTempStorage.is() )
149cdf0e10cSrcweir 		throw uno::RuntimeException();
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	return xTempStorage;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageFromInputStream(const uno::Reference<io::XInputStream> & xStream,const uno::Reference<lang::XMultiServiceFactory> & xFactory)155cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromInputStream(
156cdf0e10cSrcweir             const uno::Reference < io::XInputStream >& xStream,
157cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
158cdf0e10cSrcweir 		throw ( uno::Exception )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
161cdf0e10cSrcweir 	aArgs[0] <<= xStream;
162cdf0e10cSrcweir 	aArgs[1] <<= embed::ElementModes::READ;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
165cdf0e10cSrcweir 													uno::UNO_QUERY );
166cdf0e10cSrcweir 	if ( !xTempStorage.is() )
167cdf0e10cSrcweir 		throw uno::RuntimeException();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	return xTempStorage;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageFromStream(const uno::Reference<io::XStream> & xStream,sal_Int32 nStorageMode,const uno::Reference<lang::XMultiServiceFactory> & xFactory)173cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromStream(
174cdf0e10cSrcweir             const uno::Reference < io::XStream >& xStream,
175cdf0e10cSrcweir 			sal_Int32 nStorageMode,
176cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
177cdf0e10cSrcweir 		throw ( uno::Exception )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
180cdf0e10cSrcweir 	aArgs[0] <<= xStream;
181cdf0e10cSrcweir 	aArgs[1] <<= nStorageMode;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
184cdf0e10cSrcweir 													uno::UNO_QUERY );
185cdf0e10cSrcweir 	if ( !xTempStorage.is() )
186cdf0e10cSrcweir 		throw uno::RuntimeException();
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	return xTempStorage;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir // ----------------------------------------------------------------------
CopyInputToOutput(const uno::Reference<io::XInputStream> & xInput,const uno::Reference<io::XOutputStream> & xOutput)192cdf0e10cSrcweir void OStorageHelper::CopyInputToOutput(
193cdf0e10cSrcweir 			const uno::Reference< io::XInputStream >& xInput,
194cdf0e10cSrcweir 			const uno::Reference< io::XOutputStream >& xOutput )
195cdf0e10cSrcweir 	throw ( uno::Exception )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	static const sal_Int32 nConstBufferSize = 32000;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	sal_Int32 nRead;
200cdf0e10cSrcweir 	uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	do
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		nRead = xInput->readBytes ( aSequence, nConstBufferSize );
205cdf0e10cSrcweir 		if ( nRead < nConstBufferSize )
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			uno::Sequence < sal_Int8 > aTempBuf ( aSequence.getConstArray(), nRead );
208cdf0e10cSrcweir 			xOutput->writeBytes ( aTempBuf );
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir 		else
211cdf0e10cSrcweir 			xOutput->writeBytes ( aSequence );
212cdf0e10cSrcweir 	}
213cdf0e10cSrcweir 	while ( nRead == nConstBufferSize );
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir // ----------------------------------------------------------------------
GetInputStreamFromURL(const::rtl::OUString & aURL,const uno::Reference<lang::XMultiServiceFactory> & xSF)217cdf0e10cSrcweir uno::Reference< io::XInputStream > OStorageHelper::GetInputStreamFromURL(
218cdf0e10cSrcweir 			const ::rtl::OUString& aURL,
219cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xSF )
220cdf0e10cSrcweir 	throw ( uno::Exception )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
223cdf0e10cSrcweir 	if ( !xFactory.is() )
224cdf0e10cSrcweir 		throw uno::RuntimeException();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	uno::Reference < ::com::sun::star::ucb::XSimpleFileAccess > xTempAccess(
227cdf0e10cSrcweir 			xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
228cdf0e10cSrcweir 			uno::UNO_QUERY );
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 	if ( !xTempAccess.is() )
231cdf0e10cSrcweir 		throw uno::RuntimeException();
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInputStream = xTempAccess->openFileRead( aURL );
234cdf0e10cSrcweir 	if ( !xInputStream.is() )
235cdf0e10cSrcweir 		throw uno::RuntimeException();
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	return xInputStream;
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir // ----------------------------------------------------------------------
SetCommonStorageEncryptionData(const uno::Reference<embed::XStorage> & xStorage,const uno::Sequence<beans::NamedValue> & aEncryptionData)241cdf0e10cSrcweir void OStorageHelper::SetCommonStorageEncryptionData(
242cdf0e10cSrcweir 			const uno::Reference< embed::XStorage >& xStorage,
243cdf0e10cSrcweir 			const uno::Sequence< beans::NamedValue >& aEncryptionData )
244cdf0e10cSrcweir 	throw ( uno::Exception )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	uno::Reference< embed::XEncryptionProtectedSource2 > xEncrSet( xStorage, uno::UNO_QUERY );
247cdf0e10cSrcweir 	if ( !xEncrSet.is() )
248cdf0e10cSrcweir 		throw io::IOException(); // TODO
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 	xEncrSet->setEncryptionData( aEncryptionData );
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253cdf0e10cSrcweir // ----------------------------------------------------------------------
GetXStorageFormat(const uno::Reference<embed::XStorage> & xStorage)254cdf0e10cSrcweir sal_Int32 OStorageHelper::GetXStorageFormat(
255cdf0e10cSrcweir 			const uno::Reference< embed::XStorage >& xStorage )
256cdf0e10cSrcweir 		throw ( uno::Exception )
257cdf0e10cSrcweir {
258cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xStorProps( xStorage, uno::UNO_QUERY_THROW );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	::rtl::OUString aMediaType;
261cdf0e10cSrcweir 	xStorProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	sal_Int32 nResult = 0;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	// TODO/LATER: the filter configuration could be used to detect it later, or batter a special service
266cdf0e10cSrcweir     if (
267cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_ASCII       ) ||
268cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII   ) ||
269cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII) ||
270cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_DRAW_ASCII         ) ||
271cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_IMPRESS_ASCII      ) ||
272cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CALC_ASCII         ) ||
273cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CHART_ASCII        ) ||
274cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_MATH_ASCII         )
275cdf0e10cSrcweir        )
276cdf0e10cSrcweir 	{
277cdf0e10cSrcweir 		nResult = SOFFICE_FILEFORMAT_60;
278cdf0e10cSrcweir 	}
279cdf0e10cSrcweir     else
280cdf0e10cSrcweir     if (
281cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII        ) ||
282cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII    ) ||
283cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) ||
284cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII     ) ||
285cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII) ||
286cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) ||
287cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII       ) ||
288cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII     ) ||
289cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII    ) ||
290cdf0e10cSrcweir 		aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_ASCII    ) ||
291cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII    ) ||
292cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII        ) ||
293cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII     ) ||
294cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII) ||
295cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) ||
296cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII       ) ||
297cdf0e10cSrcweir         aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII     )
298cdf0e10cSrcweir        )
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 		nResult = SOFFICE_FILEFORMAT_8;
301cdf0e10cSrcweir 	}
302cdf0e10cSrcweir 	else
303cdf0e10cSrcweir 	{
304cdf0e10cSrcweir 		// the mediatype is not known
305cdf0e10cSrcweir 		throw beans::IllegalTypeException();
306cdf0e10cSrcweir 	}
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	return nResult;
309cdf0e10cSrcweir }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir // ----------------------------------------------------------------------
GetTemporaryStorageOfFormat(const::rtl::OUString & aFormat,const uno::Reference<lang::XMultiServiceFactory> & xFactory)312cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetTemporaryStorageOfFormat(
313cdf0e10cSrcweir 			const ::rtl::OUString& aFormat,
314cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory )
315cdf0e10cSrcweir 	throw ( uno::Exception )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > xFactoryToUse = xFactory.is() ? xFactory : ::comphelper::getProcessServiceFactory();
318cdf0e10cSrcweir 	if ( !xFactoryToUse.is() )
319cdf0e10cSrcweir 		throw uno::RuntimeException();
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 	uno::Reference< io::XStream > xTmpStream(
322cdf0e10cSrcweir 		xFactoryToUse->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.TempFile" ) ) ),
323cdf0e10cSrcweir 		uno::UNO_QUERY_THROW );
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 	return GetStorageOfFormatFromStream( aFormat, xTmpStream, embed::ElementModes::READWRITE, xFactoryToUse );
326cdf0e10cSrcweir }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageOfFormatFromURL(const::rtl::OUString & aFormat,const::rtl::OUString & aURL,sal_Int32 nStorageMode,const uno::Reference<lang::XMultiServiceFactory> & xFactory,sal_Bool bRepairStorage)329cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromURL(
330cdf0e10cSrcweir 			const ::rtl::OUString& aFormat,
331cdf0e10cSrcweir 			const ::rtl::OUString& aURL,
332cdf0e10cSrcweir 			sal_Int32 nStorageMode,
333cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory,
334cdf0e10cSrcweir             sal_Bool bRepairStorage )
335cdf0e10cSrcweir 	throw ( uno::Exception )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aProps( 1 );
338cdf0e10cSrcweir 	aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
339cdf0e10cSrcweir 	aProps[0].Value <<= aFormat;
340cdf0e10cSrcweir     if ( bRepairStorage )
341cdf0e10cSrcweir     {
342cdf0e10cSrcweir         aProps.realloc( 2 );
343cdf0e10cSrcweir         aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RepairPackage" ) );
344cdf0e10cSrcweir         aProps[1].Value <<= bRepairStorage;
345cdf0e10cSrcweir     }
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 3 );
348cdf0e10cSrcweir 	aArgs[0] <<= aURL;
349cdf0e10cSrcweir 	aArgs[1] <<= nStorageMode;
350cdf0e10cSrcweir 	aArgs[2] <<= aProps;
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
353cdf0e10cSrcweir 													uno::UNO_QUERY );
354cdf0e10cSrcweir 	if ( !xTempStorage.is() )
355cdf0e10cSrcweir 		throw uno::RuntimeException();
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 	return xTempStorage;
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageOfFormatFromInputStream(const::rtl::OUString & aFormat,const uno::Reference<io::XInputStream> & xStream,const uno::Reference<lang::XMultiServiceFactory> & xFactory,sal_Bool bRepairStorage)361cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromInputStream(
362cdf0e10cSrcweir 			const ::rtl::OUString& aFormat,
363cdf0e10cSrcweir             const uno::Reference < io::XInputStream >& xStream,
364cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory,
365cdf0e10cSrcweir             sal_Bool bRepairStorage )
366cdf0e10cSrcweir 		throw ( uno::Exception )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aProps( 1 );
369cdf0e10cSrcweir 	aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
370cdf0e10cSrcweir 	aProps[0].Value <<= aFormat;
371cdf0e10cSrcweir     if ( bRepairStorage )
372cdf0e10cSrcweir     {
373cdf0e10cSrcweir         aProps.realloc( 2 );
374cdf0e10cSrcweir         aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RepairPackage" ) );
375cdf0e10cSrcweir         aProps[1].Value <<= bRepairStorage;
376cdf0e10cSrcweir     }
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 3 );
379cdf0e10cSrcweir 	aArgs[0] <<= xStream;
380cdf0e10cSrcweir 	aArgs[1] <<= embed::ElementModes::READ;
381cdf0e10cSrcweir 	aArgs[2] <<= aProps;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
384cdf0e10cSrcweir 													uno::UNO_QUERY );
385cdf0e10cSrcweir 	if ( !xTempStorage.is() )
386cdf0e10cSrcweir 		throw uno::RuntimeException();
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	return xTempStorage;
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir // ----------------------------------------------------------------------
GetStorageOfFormatFromStream(const::rtl::OUString & aFormat,const uno::Reference<io::XStream> & xStream,sal_Int32 nStorageMode,const uno::Reference<lang::XMultiServiceFactory> & xFactory,sal_Bool bRepairStorage)392cdf0e10cSrcweir uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromStream(
393cdf0e10cSrcweir 			const ::rtl::OUString& aFormat,
394cdf0e10cSrcweir             const uno::Reference < io::XStream >& xStream,
395cdf0e10cSrcweir 			sal_Int32 nStorageMode,
396cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory,
397cdf0e10cSrcweir             sal_Bool bRepairStorage )
398cdf0e10cSrcweir 		throw ( uno::Exception )
399cdf0e10cSrcweir {
400cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aProps( 1 );
401cdf0e10cSrcweir 	aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
402cdf0e10cSrcweir 	aProps[0].Value <<= aFormat;
403cdf0e10cSrcweir     if ( bRepairStorage )
404cdf0e10cSrcweir     {
405cdf0e10cSrcweir         aProps.realloc( 2 );
406cdf0e10cSrcweir         aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "RepairPackage" ) );
407cdf0e10cSrcweir         aProps[1].Value <<= bRepairStorage;
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 3 );
411cdf0e10cSrcweir 	aArgs[0] <<= xStream;
412cdf0e10cSrcweir 	aArgs[1] <<= nStorageMode;
413cdf0e10cSrcweir 	aArgs[2] <<= aProps;
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
416cdf0e10cSrcweir 													uno::UNO_QUERY );
417cdf0e10cSrcweir 	if ( !xTempStorage.is() )
418cdf0e10cSrcweir 		throw uno::RuntimeException();
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 	return xTempStorage;
421cdf0e10cSrcweir }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir // ----------------------------------------------------------------------
CreatePackageEncryptionData(const::rtl::OUString & aPassword,const uno::Reference<lang::XMultiServiceFactory> & xSF)424cdf0e10cSrcweir uno::Sequence< beans::NamedValue > OStorageHelper::CreatePackageEncryptionData( const ::rtl::OUString& aPassword, const uno::Reference< lang::XMultiServiceFactory >& xSF )
425cdf0e10cSrcweir {
426cdf0e10cSrcweir     // TODO/LATER: Should not the method be part of DocPasswordHelper?
427cdf0e10cSrcweir     uno::Sequence< beans::NamedValue > aEncryptionData;
428cdf0e10cSrcweir     sal_Int32 nSha1Ind = 0;
429*49b34792SHerbert Dürr     if ( !aPassword.isEmpty() )
430cdf0e10cSrcweir     {
431cdf0e10cSrcweir         // generate SHA256 start key
432cdf0e10cSrcweir         try
433cdf0e10cSrcweir         {
434cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
435cdf0e10cSrcweir             if ( !xFactory.is() )
436cdf0e10cSrcweir                 throw uno::RuntimeException();
437cdf0e10cSrcweir 
438cdf0e10cSrcweir             uno::Reference< xml::crypto::XDigestContextSupplier > xDigestContextSupplier( xFactory->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.crypto.NSSInitializer" ) ) ), uno::UNO_QUERY_THROW );
439cdf0e10cSrcweir             uno::Reference< xml::crypto::XDigestContext > xDigestContext( xDigestContextSupplier->getDigestContext( xml::crypto::DigestID::SHA256, uno::Sequence< beans::NamedValue >() ), uno::UNO_SET_THROW );
440cdf0e10cSrcweir 
441cdf0e10cSrcweir             ::rtl::OString aUTF8Password( ::rtl::OUStringToOString( aPassword, RTL_TEXTENCODING_UTF8 ) );
442cdf0e10cSrcweir             xDigestContext->updateDigest( uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( aUTF8Password.getStr() ), aUTF8Password.getLength() ) );
443cdf0e10cSrcweir             uno::Sequence< sal_Int8 > aDigest = xDigestContext->finalizeDigestAndDispose();
444cdf0e10cSrcweir 
445cdf0e10cSrcweir             aEncryptionData.realloc( ++nSha1Ind );
446cdf0e10cSrcweir             aEncryptionData[0].Name = PACKAGE_ENCRYPTIONDATA_SHA256UTF8;
447cdf0e10cSrcweir             aEncryptionData[0].Value <<= aDigest;
448cdf0e10cSrcweir         }
449cdf0e10cSrcweir         catch ( uno::Exception& )
450cdf0e10cSrcweir         {
451cdf0e10cSrcweir             OSL_ENSURE( false, "Can not create SHA256 digest!" );
452cdf0e10cSrcweir         }
453cdf0e10cSrcweir 
454cdf0e10cSrcweir         // MS_1252 encoding was used for SO60 document format password encoding,
455cdf0e10cSrcweir         // this encoding supports only a minor subset of nonascii characters,
456cdf0e10cSrcweir         // but for compatibility reasons it has to be used for old document formats
457cdf0e10cSrcweir         aEncryptionData.realloc( nSha1Ind + 2 );
458cdf0e10cSrcweir         aEncryptionData[nSha1Ind].Name = PACKAGE_ENCRYPTIONDATA_SHA1UTF8;
459cdf0e10cSrcweir         aEncryptionData[nSha1Ind + 1].Name = PACKAGE_ENCRYPTIONDATA_SHA1MS1252;
460cdf0e10cSrcweir 
461cdf0e10cSrcweir         rtl_TextEncoding pEncoding[2] = { RTL_TEXTENCODING_UTF8, RTL_TEXTENCODING_MS_1252 };
462cdf0e10cSrcweir 
463cdf0e10cSrcweir         for ( sal_Int32 nInd = 0; nInd < 2; nInd++ )
464cdf0e10cSrcweir         {
465cdf0e10cSrcweir             ::rtl::OString aByteStrPass = ::rtl::OUStringToOString( aPassword, pEncoding[nInd] );
466cdf0e10cSrcweir 
467cdf0e10cSrcweir             sal_uInt8 pBuffer[RTL_DIGEST_LENGTH_SHA1];
468cdf0e10cSrcweir             rtlDigestError nError = rtl_digest_SHA1( aByteStrPass.getStr(),
469cdf0e10cSrcweir                                                     aByteStrPass.getLength(),
470cdf0e10cSrcweir                                                     pBuffer,
471cdf0e10cSrcweir                                                     RTL_DIGEST_LENGTH_SHA1 );
472cdf0e10cSrcweir 
473cdf0e10cSrcweir             if ( nError != rtl_Digest_E_None )
474cdf0e10cSrcweir             {
475cdf0e10cSrcweir                 aEncryptionData.realloc( nSha1Ind );
476cdf0e10cSrcweir                 break;
477cdf0e10cSrcweir             }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir             aEncryptionData[nSha1Ind+nInd].Value <<= uno::Sequence< sal_Int8 >( (sal_Int8*)pBuffer, RTL_DIGEST_LENGTH_SHA1 );
480cdf0e10cSrcweir         }
481cdf0e10cSrcweir     }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir     return aEncryptionData;
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
486cdf0e10cSrcweir // ----------------------------------------------------------------------
IsValidZipEntryFileName(const::rtl::OUString & aName,sal_Bool bSlashAllowed)487cdf0e10cSrcweir sal_Bool OStorageHelper::IsValidZipEntryFileName( const ::rtl::OUString& aName, sal_Bool bSlashAllowed )
488cdf0e10cSrcweir {
489cdf0e10cSrcweir     return IsValidZipEntryFileName( aName.getStr(), aName.getLength(), bSlashAllowed );
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir // ----------------------------------------------------------------------
IsValidZipEntryFileName(const sal_Unicode * pChar,sal_Int32 nLength,sal_Bool bSlashAllowed)493cdf0e10cSrcweir sal_Bool OStorageHelper::IsValidZipEntryFileName(
494cdf0e10cSrcweir     const sal_Unicode *pChar, sal_Int32 nLength, sal_Bool bSlashAllowed )
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nLength; i++ )
497cdf0e10cSrcweir     {
498cdf0e10cSrcweir         switch ( pChar[i] )
499cdf0e10cSrcweir         {
500cdf0e10cSrcweir             case '\\':
501cdf0e10cSrcweir             case '?':
502cdf0e10cSrcweir             case '<':
503cdf0e10cSrcweir             case '>':
504cdf0e10cSrcweir             case '\"':
505cdf0e10cSrcweir             case '|':
506cdf0e10cSrcweir             case ':':
507cdf0e10cSrcweir                 return sal_False;
508cdf0e10cSrcweir             case '/':
509cdf0e10cSrcweir                 if ( !bSlashAllowed )
510cdf0e10cSrcweir                     return sal_False;
511cdf0e10cSrcweir                 break;
512cdf0e10cSrcweir             default:
513cdf0e10cSrcweir                 if ( pChar[i] < 32  || (pChar[i] >= 0xD800 && pChar[i] <= 0xDFFF) )
514cdf0e10cSrcweir                     return sal_False;
515cdf0e10cSrcweir         }
516cdf0e10cSrcweir     }
517cdf0e10cSrcweir     return sal_True;
518cdf0e10cSrcweir }
519cdf0e10cSrcweir 
520cdf0e10cSrcweir // ----------------------------------------------------------------------
PathHasSegment(const::rtl::OUString & aPath,const::rtl::OUString & aSegment)521cdf0e10cSrcweir sal_Bool OStorageHelper::PathHasSegment( const ::rtl::OUString& aPath, const ::rtl::OUString& aSegment )
522cdf0e10cSrcweir {
523cdf0e10cSrcweir     sal_Bool bResult = sal_False;
524cdf0e10cSrcweir     const sal_Int32 nPathLen = aPath.getLength();
525cdf0e10cSrcweir     const sal_Int32 nSegLen = aSegment.getLength();
526cdf0e10cSrcweir 
527cdf0e10cSrcweir     if ( nSegLen && nPathLen >= nSegLen )
528cdf0e10cSrcweir     {
529cdf0e10cSrcweir         ::rtl::OUString aEndSegment( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
530cdf0e10cSrcweir         aEndSegment += aSegment;
531cdf0e10cSrcweir 
532cdf0e10cSrcweir         ::rtl::OUString aInternalSegment( aEndSegment );
533cdf0e10cSrcweir         aInternalSegment += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
534cdf0e10cSrcweir 
535cdf0e10cSrcweir         if ( aPath.indexOf( aInternalSegment ) >= 0 )
536cdf0e10cSrcweir             bResult = sal_True;
537cdf0e10cSrcweir 
538cdf0e10cSrcweir         if ( !bResult && !aPath.compareTo( aSegment, nSegLen ) )
539cdf0e10cSrcweir         {
540cdf0e10cSrcweir             if ( nPathLen == nSegLen || aPath.getStr()[nSegLen] == (sal_Unicode)'/' )
541cdf0e10cSrcweir                 bResult = sal_True;
542cdf0e10cSrcweir         }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir         if ( !bResult && nPathLen > nSegLen && aPath.copy( nPathLen - nSegLen - 1, nSegLen + 1 ).equals( aEndSegment ) )
545cdf0e10cSrcweir             bResult = sal_True;
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     return bResult;
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553