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