fix toolbar import
[ooovba.git] / package / source / zippackage / ZipPackageStream.cxx
blob2b87580fdf037fcc68c3aeb0903840e7e5e98aff
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ZipPackageStream.cxx,v $
10 * $Revision: 1.51 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include <com/sun/star/packages/zip/ZipConstants.hpp>
34 #include <com/sun/star/packages/zip/ZipIOException.hpp>
35 #include <com/sun/star/io/XInputStream.hpp>
36 #include <com/sun/star/io/XOutputStream.hpp>
37 #include <com/sun/star/io/XStream.hpp>
38 #include <com/sun/star/io/XSeekable.hpp>
41 #include <ZipPackageStream.hxx>
42 #include <ZipPackage.hxx>
43 #include <ZipFile.hxx>
44 #include <EncryptedDataHeader.hxx>
45 #include <vos/diagnose.hxx>
46 #include "wrapstreamforshare.hxx"
48 #include <comphelper/seekableinput.hxx>
49 #include <comphelper/storagehelper.hxx>
51 #include <PackageConstants.hxx>
53 using namespace com::sun::star::packages::zip::ZipConstants;
54 using namespace com::sun::star::packages::zip;
55 using namespace com::sun::star::uno;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star;
58 using namespace cppu;
59 using namespace rtl;
61 Sequence < sal_Int8 > ZipPackageStream::aImplementationId = Sequence < sal_Int8 > ();
64 ZipPackageStream::ZipPackageStream ( ZipPackage & rNewPackage,
65 const Reference< XMultiServiceFactory >& xFactory,
66 sal_Bool bAllowRemoveOnInsert )
67 : m_xFactory( xFactory )
68 , rZipPackage(rNewPackage)
69 , bToBeCompressed ( sal_True )
70 , bToBeEncrypted ( sal_False )
71 , bHaveOwnKey ( sal_False )
72 , bIsEncrypted ( sal_False )
73 , xEncryptionData ( )
74 , m_nStreamMode( PACKAGE_STREAM_NOTSET )
75 , m_nMagicalHackPos( 0 )
76 , m_nMagicalHackSize( 0 )
77 , m_bHasSeekable( sal_False )
78 , m_bCompressedIsSetFromOutside( sal_False )
80 OSL_ENSURE( m_xFactory.is(), "No factory is provided to ZipPackageStream!\n" );
82 this->mbAllowRemoveOnInsert = bAllowRemoveOnInsert;
84 SetFolder ( sal_False );
85 aEntry.nVersion = -1;
86 aEntry.nFlag = 0;
87 aEntry.nMethod = -1;
88 aEntry.nTime = -1;
89 aEntry.nCrc = -1;
90 aEntry.nCompressedSize = -1;
91 aEntry.nSize = -1;
92 aEntry.nOffset = -1;
93 aEntry.nNameLen = -1;
94 aEntry.nExtraLen = -1;
96 if ( !aImplementationId.getLength() )
98 aImplementationId = getImplementationId();
102 ZipPackageStream::~ZipPackageStream( void )
106 void ZipPackageStream::setZipEntryOnLoading( const ZipEntry &rInEntry)
108 aEntry.nVersion = rInEntry.nVersion;
109 aEntry.nFlag = rInEntry.nFlag;
110 aEntry.nMethod = rInEntry.nMethod;
111 aEntry.nTime = rInEntry.nTime;
112 aEntry.nCrc = rInEntry.nCrc;
113 aEntry.nCompressedSize = rInEntry.nCompressedSize;
114 aEntry.nSize = rInEntry.nSize;
115 aEntry.nOffset = rInEntry.nOffset;
116 aEntry.sName = rInEntry.sName;
117 aEntry.nNameLen = rInEntry.nNameLen;
118 aEntry.nExtraLen = rInEntry.nExtraLen;
120 if ( aEntry.nMethod == STORED )
121 bToBeCompressed = sal_False;
124 //--------------------------------------------------------------------------
125 void ZipPackageStream::CloseOwnStreamIfAny()
127 if ( xStream.is() )
129 xStream->closeInput();
130 xStream = uno::Reference< io::XInputStream >();
131 m_bHasSeekable = sal_False;
135 //--------------------------------------------------------------------------
136 uno::Reference< io::XInputStream >& ZipPackageStream::GetOwnSeekStream()
138 if ( !m_bHasSeekable && xStream.is() )
140 // The package component requires that every stream either be FROM a package or it must support XSeekable!
141 // The only exception is a nonseekable stream that is provided only for storing, if such a stream
142 // is accessed before commit it MUST be wrapped.
143 // Wrap the stream in case it is not seekable
144 xStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( xStream, m_xFactory );
145 Reference< io::XSeekable > xSeek( xStream, UNO_QUERY );
146 if ( !xSeek.is() )
147 throw RuntimeException( OUString::createFromAscii( "The stream must support XSeekable!" ),
148 Reference< XInterface >() );
150 m_bHasSeekable = sal_True;
153 return xStream;
156 //--------------------------------------------------------------------------
157 uno::Reference< io::XInputStream > ZipPackageStream::GetRawEncrStreamNoHeaderCopy()
159 if ( m_nStreamMode != PACKAGE_STREAM_RAW || !GetOwnSeekStream().is() )
160 throw io::IOException(); // TODO
162 if ( xEncryptionData.isEmpty() )
163 throw ZipIOException( OUString::createFromAscii( "Encrypted stream without encryption data!\n" ),
164 Reference< XInterface >() );
166 uno::Reference< io::XSeekable > xSeek( GetOwnSeekStream(), UNO_QUERY );
167 if ( !xSeek.is() )
168 throw ZipIOException( OUString::createFromAscii( "The stream must be seekable!\n" ),
169 Reference< XInterface >() );
171 // skip header
172 xSeek->seek( n_ConstHeaderSize + xEncryptionData->aInitVector.getLength() +
173 xEncryptionData->aSalt.getLength() + xEncryptionData->aDigest.getLength() );
175 // create temporary stream
176 uno::Reference < io::XOutputStream > xTempOut(
177 m_xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
178 uno::UNO_QUERY );
179 uno::Reference < io::XInputStream > xTempIn( xTempOut, UNO_QUERY );
180 uno::Reference < io::XSeekable > xTempSeek( xTempOut, UNO_QUERY );
181 if ( !xTempOut.is() || !xTempIn.is() || !xTempSeek.is() )
182 throw io::IOException(); // TODO:
184 // copy the raw stream to the temporary file starting from the current position
185 ::comphelper::OStorageHelper::CopyInputToOutput( GetOwnSeekStream(), xTempOut );
186 xTempOut->closeOutput();
187 xTempSeek->seek( 0 );
189 return xTempIn;
192 //--------------------------------------------------------------------------
193 Reference< io::XInputStream > ZipPackageStream::TryToGetRawFromDataStream( sal_Bool bAddHeaderForEncr )
195 if ( m_nStreamMode != PACKAGE_STREAM_DATA || !GetOwnSeekStream().is() || bAddHeaderForEncr && !bToBeEncrypted )
196 throw packages::NoEncryptionException(); // TODO
198 Sequence< sal_Int8 > aKey;
200 if ( bToBeEncrypted )
202 aKey = ( xEncryptionData.isEmpty() || !bHaveOwnKey ) ? rZipPackage.getEncryptionKey() :
203 xEncryptionData->aKey;
204 if ( !aKey.getLength() )
205 throw packages::NoEncryptionException(); // TODO
210 // create temporary file
211 uno::Reference < io::XStream > xTempStream(
212 m_xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
213 uno::UNO_QUERY );
214 if ( !xTempStream.is() )
215 throw io::IOException(); // TODO:
217 // create a package based on it
218 ZipPackage* pPackage = new ZipPackage( m_xFactory );
219 Reference< XSingleServiceFactory > xPackageAsFactory( static_cast< XSingleServiceFactory* >( pPackage ) );
220 if ( !xPackageAsFactory.is() )
221 throw RuntimeException(); // TODO
223 Sequence< Any > aArgs( 1 );
224 aArgs[0] <<= xTempStream;
225 pPackage->initialize( aArgs );
227 // create a new package stream
228 Reference< XDataSinkEncrSupport > xNewPackStream( xPackageAsFactory->createInstance(), UNO_QUERY );
229 if ( !xNewPackStream.is() )
230 throw RuntimeException(); // TODO
232 if ( !m_aSharedMutexRef.Is() )
233 m_aSharedMutexRef = new SotMutexHolder();
234 xNewPackStream->setDataStream( static_cast< io::XInputStream* >(
235 new WrapStreamForShare( GetOwnSeekStream(), m_aSharedMutexRef ) ) );
237 Reference< XPropertySet > xNewPSProps( xNewPackStream, UNO_QUERY );
238 if ( !xNewPSProps.is() )
239 throw RuntimeException(); // TODO
241 // copy all the properties of this stream to the new stream
242 xNewPSProps->setPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ), makeAny( sMediaType ) );
243 xNewPSProps->setPropertyValue( ::rtl::OUString::createFromAscii( "Compressed" ), makeAny( bToBeCompressed ) );
244 if ( bToBeEncrypted )
246 xNewPSProps->setPropertyValue( ::rtl::OUString::createFromAscii( "EncryptionKey" ), makeAny( aKey ) );
247 xNewPSProps->setPropertyValue( ::rtl::OUString::createFromAscii( "Encrypted" ), makeAny( sal_True ) );
250 // insert a new stream in the package
251 Reference< XUnoTunnel > xTunnel;
252 Any aRoot = pPackage->getByHierarchicalName( ::rtl::OUString::createFromAscii( "/" ) );
253 aRoot >>= xTunnel;
254 Reference< container::XNameContainer > xRootNameContainer( xTunnel, UNO_QUERY );
255 if ( !xRootNameContainer.is() )
256 throw RuntimeException(); // TODO
258 Reference< XUnoTunnel > xNPSTunnel( xNewPackStream, UNO_QUERY );
259 xRootNameContainer->insertByName( ::rtl::OUString::createFromAscii( "dummy" ), makeAny( xNPSTunnel ) );
261 // commit the temporary package
262 pPackage->commitChanges();
264 // get raw stream from the temporary package
265 Reference< io::XInputStream > xInRaw;
266 if ( bAddHeaderForEncr )
267 xInRaw = xNewPackStream->getRawStream();
268 else
269 xInRaw = xNewPackStream->getPlainRawStream();
271 // create another temporary file
272 uno::Reference < io::XOutputStream > xTempOut(
273 m_xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
274 uno::UNO_QUERY );
275 uno::Reference < io::XInputStream > xTempIn( xTempOut, UNO_QUERY );
276 uno::Reference < io::XSeekable > xTempSeek( xTempOut, UNO_QUERY );
277 if ( !xTempOut.is() || !xTempIn.is() || !xTempSeek.is() )
278 throw io::IOException(); // TODO:
280 // copy the raw stream to the temporary file
281 ::comphelper::OStorageHelper::CopyInputToOutput( xInRaw, xTempOut );
282 xTempOut->closeOutput();
283 xTempSeek->seek( 0 );
285 // close raw stream, package stream and folder
286 xInRaw = Reference< io::XInputStream >();
287 xNewPSProps = Reference< XPropertySet >();
288 xNPSTunnel = Reference< XUnoTunnel >();
289 xNewPackStream = Reference< XDataSinkEncrSupport >();
290 xTunnel = Reference< XUnoTunnel >();
291 xRootNameContainer = Reference< container::XNameContainer >();
293 // return the stream representing the first temporary file
294 return xTempIn;
296 catch ( RuntimeException& )
298 throw;
300 catch ( Exception& )
304 throw io::IOException(); // TODO
307 //--------------------------------------------------------------------------
308 sal_Bool ZipPackageStream::ParsePackageRawStream()
310 OSL_ENSURE( GetOwnSeekStream().is(), "A stream must be provided!\n" );
312 if ( !GetOwnSeekStream().is() )
313 return sal_False;
315 sal_Bool bOk = sal_False;
317 vos::ORef < EncryptionData > xTempEncrData;
318 sal_Int32 nMagHackSize = 0;
319 Sequence < sal_Int8 > aHeader ( 4 );
321 try
323 if ( GetOwnSeekStream()->readBytes ( aHeader, 4 ) == 4 )
325 const sal_Int8 *pHeader = aHeader.getConstArray();
326 sal_uInt32 nHeader = ( pHeader [0] & 0xFF ) |
327 ( pHeader [1] & 0xFF ) << 8 |
328 ( pHeader [2] & 0xFF ) << 16 |
329 ( pHeader [3] & 0xFF ) << 24;
330 if ( nHeader == n_ConstHeader )
332 // this is one of our god-awful, but extremely devious hacks, everyone cheer
333 xTempEncrData = new EncryptionData;
335 ::rtl::OUString aMediaType;
336 if ( ZipFile::StaticFillData ( xTempEncrData, nMagHackSize, aMediaType, GetOwnSeekStream() ) )
338 // We'll want to skip the data we've just read, so calculate how much we just read
339 // and remember it
340 m_nMagicalHackPos = n_ConstHeaderSize + xTempEncrData->aSalt.getLength()
341 + xTempEncrData->aInitVector.getLength()
342 + xTempEncrData->aDigest.getLength()
343 + aMediaType.getLength() * sizeof( sal_Unicode );
344 m_nMagicalHackSize = nMagHackSize;
345 sMediaType = aMediaType;
347 bOk = sal_True;
352 catch( Exception& )
356 if ( !bOk )
358 // the provided stream is not a raw stream
359 return sal_False;
362 xEncryptionData = xTempEncrData;
363 SetIsEncrypted ( sal_True );
364 // it's already compressed and encrypted
365 bToBeEncrypted = bToBeCompressed = sal_False;
367 return sal_True;
370 void ZipPackageStream::SetPackageMember( sal_Bool bNewValue )
372 if ( bNewValue )
374 m_nStreamMode = PACKAGE_STREAM_PACKAGEMEMBER;
375 m_nMagicalHackPos = 0;
376 m_nMagicalHackSize = 0;
378 else if ( m_nStreamMode == PACKAGE_STREAM_PACKAGEMEMBER )
379 m_nStreamMode = PACKAGE_STREAM_NOTSET; // must be reset
382 // XActiveDataSink
383 //--------------------------------------------------------------------------
384 void SAL_CALL ZipPackageStream::setInputStream( const Reference< io::XInputStream >& aStream )
385 throw(RuntimeException)
387 // if seekable access is required the wrapping will be done on demand
388 m_aSharedMutexRef = new SotMutexHolder();
389 xStream = aStream;
390 m_bHasSeekable = sal_False;
391 SetPackageMember ( sal_False );
392 aEntry.nTime = -1;
393 m_nStreamMode = PACKAGE_STREAM_DETECT;
396 //--------------------------------------------------------------------------
397 Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawData()
398 throw(RuntimeException)
402 if (IsPackageMember())
404 if ( !xEncryptionData.isEmpty() && !bHaveOwnKey )
405 xEncryptionData->aKey = rZipPackage.getEncryptionKey();
406 return rZipPackage.getZipFile().getRawData( aEntry, xEncryptionData, bIsEncrypted );
408 else if ( GetOwnSeekStream().is() )
410 if ( !m_aSharedMutexRef.Is() )
411 m_aSharedMutexRef = new SotMutexHolder();
412 return new WrapStreamForShare( GetOwnSeekStream(), m_aSharedMutexRef );
414 else
415 return Reference < io::XInputStream > ();
417 catch (ZipException &)//rException)
419 VOS_ENSURE( 0, "ZipException thrown");//rException.Message);
420 return Reference < io::XInputStream > ();
422 catch (Exception &)
424 VOS_ENSURE( 0, "Exception is thrown during stream wrapping!\n");
425 return Reference < io::XInputStream > ();
429 //--------------------------------------------------------------------------
430 Reference< io::XInputStream > SAL_CALL ZipPackageStream::getInputStream( )
431 throw(RuntimeException)
435 if (IsPackageMember())
437 if ( !xEncryptionData.isEmpty() && !bHaveOwnKey )
438 xEncryptionData->aKey = rZipPackage.getEncryptionKey();
439 return rZipPackage.getZipFile().getInputStream( aEntry, xEncryptionData, bIsEncrypted );
441 else if ( GetOwnSeekStream().is() )
443 if ( !m_aSharedMutexRef.Is() )
444 m_aSharedMutexRef = new SotMutexHolder();
445 return new WrapStreamForShare( GetOwnSeekStream(), m_aSharedMutexRef );
447 else
448 return Reference < io::XInputStream > ();
450 catch (ZipException &)//rException)
452 VOS_ENSURE( 0,"ZipException thrown");//rException.Message);
453 return Reference < io::XInputStream > ();
455 catch (Exception &)
457 VOS_ENSURE( 0, "Exception is thrown during stream wrapping!\n");
458 return Reference < io::XInputStream > ();
462 // XDataSinkEncrSupport
463 //--------------------------------------------------------------------------
464 Reference< io::XInputStream > SAL_CALL ZipPackageStream::getDataStream()
465 throw ( packages::WrongPasswordException,
466 io::IOException,
467 RuntimeException )
469 // There is no stream attached to this object
470 if ( m_nStreamMode == PACKAGE_STREAM_NOTSET )
471 return Reference< io::XInputStream >();
473 // this method can not be used together with old approach
474 if ( m_nStreamMode == PACKAGE_STREAM_DETECT )
475 throw packages::zip::ZipIOException(); // TODO
477 if ( !xEncryptionData.isEmpty() && !bHaveOwnKey )
478 xEncryptionData->aKey = rZipPackage.getEncryptionKey();
480 if (IsPackageMember())
482 if ( !xEncryptionData.isEmpty() && !bHaveOwnKey )
483 xEncryptionData->aKey = rZipPackage.getEncryptionKey();
485 return rZipPackage.getZipFile().getDataStream( aEntry, xEncryptionData, bIsEncrypted );
487 else if ( m_nStreamMode == PACKAGE_STREAM_RAW )
488 return ZipFile::StaticGetDataFromRawStream( GetOwnSeekStream(), xEncryptionData );
489 else if ( GetOwnSeekStream().is() )
491 if ( !m_aSharedMutexRef.Is() )
492 m_aSharedMutexRef = new SotMutexHolder();
493 return new WrapStreamForShare( GetOwnSeekStream(), m_aSharedMutexRef );
495 else
496 return uno::Reference< io::XInputStream >();
499 //--------------------------------------------------------------------------
500 Reference< io::XInputStream > SAL_CALL ZipPackageStream::getRawStream()
501 throw ( packages::NoEncryptionException,
502 io::IOException,
503 uno::RuntimeException )
505 // There is no stream attached to this object
506 if ( m_nStreamMode == PACKAGE_STREAM_NOTSET )
507 return Reference< io::XInputStream >();
509 // this method can not be used together with old approach
510 if ( m_nStreamMode == PACKAGE_STREAM_DETECT )
511 throw packages::zip::ZipIOException(); // TODO
513 if (IsPackageMember())
515 if ( !bIsEncrypted || xEncryptionData.isEmpty() )
516 throw packages::NoEncryptionException(); // TODO
518 return rZipPackage.getZipFile().getWrappedRawStream( aEntry, xEncryptionData, sMediaType );
520 else if ( GetOwnSeekStream().is() )
522 if ( m_nStreamMode == PACKAGE_STREAM_RAW )
524 if ( !m_aSharedMutexRef.Is() )
525 m_aSharedMutexRef = new SotMutexHolder();
526 return new WrapStreamForShare( GetOwnSeekStream(), m_aSharedMutexRef );
528 else if ( m_nStreamMode == PACKAGE_STREAM_DATA && bToBeEncrypted )
529 return TryToGetRawFromDataStream( sal_True );
532 throw packages::NoEncryptionException(); // TODO
536 //--------------------------------------------------------------------------
537 void SAL_CALL ZipPackageStream::setDataStream( const Reference< io::XInputStream >& aStream )
538 throw ( io::IOException,
539 RuntimeException )
541 setInputStream( aStream );
542 m_nStreamMode = PACKAGE_STREAM_DATA;
545 //--------------------------------------------------------------------------
546 void SAL_CALL ZipPackageStream::setRawStream( const Reference< io::XInputStream >& aStream )
547 throw ( packages::EncryptionNotAllowedException,
548 packages::NoRawFormatException,
549 io::IOException,
550 RuntimeException)
552 // wrap the stream in case it is not seekable
553 Reference< io::XInputStream > xNewStream = ::comphelper::OSeekableInputWrapper::CheckSeekableCanWrap( aStream, m_xFactory );
554 Reference< io::XSeekable > xSeek( xNewStream, UNO_QUERY );
555 if ( !xSeek.is() )
556 throw RuntimeException( OUString::createFromAscii( "The stream must support XSeekable!" ),
557 Reference< XInterface >() );
559 xSeek->seek( 0 );
560 Reference< io::XInputStream > xOldStream = xStream;
561 xStream = xNewStream;
562 if ( !ParsePackageRawStream() )
564 xStream = xOldStream;
565 throw packages::NoRawFormatException();
568 // the raw stream MUST have seekable access
569 m_bHasSeekable = sal_True;
571 m_aSharedMutexRef = new SotMutexHolder();
572 SetPackageMember ( sal_False );
573 aEntry.nTime = -1;
574 m_nStreamMode = PACKAGE_STREAM_RAW;
577 //--------------------------------------------------------------------------
578 uno::Reference< io::XInputStream > SAL_CALL ZipPackageStream::getPlainRawStream()
579 throw ( io::IOException,
580 uno::RuntimeException )
582 // There is no stream attached to this object
583 if ( m_nStreamMode == PACKAGE_STREAM_NOTSET )
584 return Reference< io::XInputStream >();
586 // this method can not be used together with old approach
587 if ( m_nStreamMode == PACKAGE_STREAM_DETECT )
588 throw packages::zip::ZipIOException(); // TODO
590 if (IsPackageMember())
592 return rZipPackage.getZipFile().getRawData( aEntry, xEncryptionData, bIsEncrypted );
594 else if ( GetOwnSeekStream().is() )
596 if ( m_nStreamMode == PACKAGE_STREAM_RAW )
598 // the header should not be returned here
599 return GetRawEncrStreamNoHeaderCopy();
601 else if ( m_nStreamMode == PACKAGE_STREAM_DATA )
602 return TryToGetRawFromDataStream( sal_False );
605 return Reference< io::XInputStream >();
608 // XUnoTunnel
610 //--------------------------------------------------------------------------
611 sal_Int64 SAL_CALL ZipPackageStream::getSomething( const Sequence< sal_Int8 >& aIdentifier )
612 throw(RuntimeException)
614 sal_Int64 nMe = 0;
615 if ( aIdentifier.getLength() == 16 &&
616 0 == rtl_compareMemory( static_getImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) )
617 nMe = reinterpret_cast < sal_Int64 > ( this );
618 return nMe;
621 // XPropertySet
622 //--------------------------------------------------------------------------
623 void SAL_CALL ZipPackageStream::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
624 throw(beans::UnknownPropertyException, beans::PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
626 if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType")))
628 if ( rZipPackage.getFormat() != PACKAGE_FORMAT && rZipPackage.getFormat() != OFOPXML_FORMAT )
629 throw beans::PropertyVetoException();
631 if ( aValue >>= sMediaType )
633 if (sMediaType.getLength() > 0)
635 if ( sMediaType.indexOf (OUString( RTL_CONSTASCII_USTRINGPARAM ( "text" ) ) ) != -1
636 || sMediaType.equals( OUString( RTL_CONSTASCII_USTRINGPARAM ( "application/vnd.sun.star.oleobject" ) ) ) )
637 bToBeCompressed = sal_True;
638 else if ( !m_bCompressedIsSetFromOutside )
639 bToBeCompressed = sal_False;
642 else
643 throw IllegalArgumentException( OUString::createFromAscii( "MediaType must be a string!\n" ),
644 Reference< XInterface >(),
645 2 );
648 else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Size") ) )
650 if ( !( aValue >>= aEntry.nSize ) )
651 throw IllegalArgumentException( OUString::createFromAscii( "Wrong type for Size property!\n" ),
652 Reference< XInterface >(),
653 2 );
655 else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Encrypted") ) )
657 if ( rZipPackage.getFormat() != PACKAGE_FORMAT )
658 throw beans::PropertyVetoException();
660 sal_Bool bEnc = sal_False;
661 if ( aValue >>= bEnc )
663 // In case of new raw stream, the stream must not be encrypted on storing
664 if ( bEnc && m_nStreamMode == PACKAGE_STREAM_RAW )
665 throw IllegalArgumentException( OUString::createFromAscii( "Raw stream can not be encrypted on storing" ),
666 Reference< XInterface >(),
667 2 );
669 bToBeEncrypted = bEnc;
670 if ( bToBeEncrypted && xEncryptionData.isEmpty())
671 xEncryptionData = new EncryptionData;
673 else
674 throw IllegalArgumentException( OUString::createFromAscii( "Wrong type for Encrypted property!\n" ),
675 Reference< XInterface >(),
676 2 );
679 else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("EncryptionKey") ) )
681 if ( rZipPackage.getFormat() != PACKAGE_FORMAT )
682 throw beans::PropertyVetoException();
684 Sequence < sal_Int8 > aNewKey;
686 if ( !( aValue >>= aNewKey ) )
688 OUString sTempString;
689 if ( ( aValue >>= sTempString ) )
691 sal_Int32 nNameLength = sTempString.getLength();
692 Sequence < sal_Int8 > aSequence ( nNameLength );
693 sal_Int8 *pArray = aSequence.getArray();
694 const sal_Unicode *pChar = sTempString.getStr();
695 for ( sal_Int16 i = 0; i < nNameLength; i++)
696 pArray[i] = static_cast < const sal_Int8 > (pChar[i]);
697 aNewKey = aSequence;
699 else
700 throw IllegalArgumentException( OUString::createFromAscii( "Wrong type for EncryptionKey property!\n" ),
701 Reference< XInterface >(),
702 2 );
705 if ( aNewKey.getLength() )
707 if ( xEncryptionData.isEmpty())
708 xEncryptionData = new EncryptionData;
710 xEncryptionData->aKey = aNewKey;
711 // In case of new raw stream, the stream must not be encrypted on storing
712 bHaveOwnKey = sal_True;
713 if ( m_nStreamMode != PACKAGE_STREAM_RAW )
714 bToBeEncrypted = sal_True;
716 else
717 bHaveOwnKey = sal_False;
719 else if (aPropertyName.equalsAsciiL ( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) )
721 sal_Bool bCompr = sal_False;
723 if ( aValue >>= bCompr )
725 // In case of new raw stream, the stream must not be encrypted on storing
726 if ( bCompr && m_nStreamMode == PACKAGE_STREAM_RAW )
727 throw IllegalArgumentException( OUString::createFromAscii( "Raw stream can not be encrypted on storing" ),
728 Reference< XInterface >(),
729 2 );
731 bToBeCompressed = bCompr;
732 m_bCompressedIsSetFromOutside = sal_True;
734 else
735 throw IllegalArgumentException( OUString::createFromAscii( "Wrong type for Compressed property!\n" ),
736 Reference< XInterface >(),
737 2 );
739 else
740 throw beans::UnknownPropertyException();
743 //--------------------------------------------------------------------------
744 Any SAL_CALL ZipPackageStream::getPropertyValue( const OUString& PropertyName )
745 throw(beans::UnknownPropertyException, WrappedTargetException, RuntimeException)
747 Any aAny;
748 if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) )
750 aAny <<= sMediaType;
751 return aAny;
753 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) )
755 aAny <<= aEntry.nSize;
756 return aAny;
758 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Encrypted" ) ) )
760 aAny <<= ( m_nStreamMode == PACKAGE_STREAM_RAW ) ? sal_True : bToBeEncrypted;
761 return aAny;
763 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "WasEncrypted" ) ) )
765 aAny <<= bIsEncrypted;
766 return aAny;
768 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Compressed" ) ) )
770 aAny <<= bToBeCompressed;
771 return aAny;
773 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "EncryptionKey" ) ) )
775 aAny <<= xEncryptionData.isEmpty () ? Sequence < sal_Int8 > () : xEncryptionData->aKey;
776 return aAny;
778 else
779 throw beans::UnknownPropertyException();
782 //--------------------------------------------------------------------------
783 void ZipPackageStream::setSize (const sal_Int32 nNewSize)
785 if (aEntry.nCompressedSize != nNewSize )
786 aEntry.nMethod = DEFLATED;
787 aEntry.nSize = nNewSize;
789 //--------------------------------------------------------------------------
790 OUString ZipPackageStream::getImplementationName()
791 throw (RuntimeException)
793 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageStream" ) );
796 //--------------------------------------------------------------------------
797 Sequence< OUString > ZipPackageStream::getSupportedServiceNames()
798 throw (RuntimeException)
800 Sequence< OUString > aNames(1);
801 aNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageStream" ) );
802 return aNames;
804 //--------------------------------------------------------------------------
805 sal_Bool SAL_CALL ZipPackageStream::supportsService( OUString const & rServiceName )
806 throw (RuntimeException)
808 return rServiceName == getSupportedServiceNames()[0];