update dev300-m58
[ooovba.git] / package / source / zippackage / ZipPackageFolder.cxx
blob0ca894bef831f7c971f6fc0d54f87e43677b8051
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: ZipPackageFolder.cxx,v $
10 * $Revision: 1.85 $
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 <ZipPackageFolder.hxx>
34 #include <ZipFile.hxx>
35 #include <ZipOutputStream.hxx>
36 #include <ZipPackageStream.hxx>
37 #include <PackageConstants.hxx>
38 #include <ZipPackageFolderEnumeration.hxx>
39 #include <com/sun/star/packages/zip/ZipConstants.hpp>
40 #include <vos/diagnose.hxx>
41 #include <osl/time.h>
42 #include <rtl/digest.h>
43 #include <ContentInfo.hxx>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/io/XSeekable.hpp>
46 #include <EncryptedDataHeader.hxx>
47 #include <rtl/random.h>
48 #include <memory>
50 using namespace com::sun::star::packages::zip::ZipConstants;
51 using namespace com::sun::star::packages::zip;
52 using namespace com::sun::star::container;
53 using namespace com::sun::star::packages;
54 using namespace com::sun::star::beans;
55 using namespace com::sun::star::lang;
56 using namespace com::sun::star::uno;
57 using namespace com::sun::star::io;
58 using namespace cppu;
59 using namespace rtl;
60 using namespace std;
61 using namespace ::com::sun::star;
62 using vos::ORef;
64 Sequence < sal_Int8 > ZipPackageFolder::aImplementationId = Sequence < sal_Int8 > ();
66 ZipPackageFolder::ZipPackageFolder ( const Reference< XMultiServiceFactory >& xFactory,
67 sal_Int16 nFormat,
68 sal_Bool bAllowRemoveOnInsert )
69 : m_xFactory( xFactory )
70 , m_nFormat( nFormat )
72 OSL_ENSURE( m_xFactory.is(), "No factory is provided to the package folder!" );
74 this->mbAllowRemoveOnInsert = bAllowRemoveOnInsert;
76 SetFolder ( sal_True );
77 aEntry.nVersion = -1;
78 aEntry.nFlag = 0;
79 aEntry.nMethod = STORED;
80 aEntry.nTime = -1;
81 aEntry.nCrc = 0;
82 aEntry.nCompressedSize = 0;
83 aEntry.nSize = 0;
84 aEntry.nOffset = -1;
85 if ( !aImplementationId.getLength() )
87 aImplementationId = getImplementationId();
92 ZipPackageFolder::~ZipPackageFolder()
96 void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair& aPair )
98 ::rtl::OUString aExt;
99 if ( aPair.First.toChar() == (sal_Unicode)'.' )
100 aExt = aPair.First;
101 else
102 aExt = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) + aPair.First;
104 for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
105 aCI != aEnd;
106 aCI++)
108 const OUString &rShortName = (*aCI).first;
109 const ContentInfo &rInfo = *(*aCI).second;
111 if ( rInfo.bFolder )
112 rInfo.pFolder->setChildStreamsTypeByExtension( aPair );
113 else
115 sal_Int32 nNameLength = rShortName.getLength();
116 sal_Int32 nExtLength = aExt.getLength();
117 if ( nNameLength >= nExtLength && rShortName.match( aExt, nNameLength - nExtLength ) )
118 rInfo.pStream->SetMediaType( aPair.Second );
123 void ZipPackageFolder::copyZipEntry( ZipEntry &rDest, const ZipEntry &rSource)
125 rDest.nVersion = rSource.nVersion;
126 rDest.nFlag = rSource.nFlag;
127 rDest.nMethod = rSource.nMethod;
128 rDest.nTime = rSource.nTime;
129 rDest.nCrc = rSource.nCrc;
130 rDest.nCompressedSize = rSource.nCompressedSize;
131 rDest.nSize = rSource.nSize;
132 rDest.nOffset = rSource.nOffset;
133 rDest.sName = rSource.sName;
134 rDest.nNameLen = rSource.nNameLen;
135 rDest.nExtraLen = rSource.nExtraLen;
138 // XNameContainer
139 void SAL_CALL ZipPackageFolder::insertByName( const OUString& aName, const Any& aElement )
140 throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
142 if (hasByName(aName))
143 throw ElementExistException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
144 else
146 Reference < XUnoTunnel > xRef;
147 aElement >>= xRef;
148 if ( ( aElement >>= xRef ) )
150 sal_Int64 nTest;
151 ZipPackageEntry *pEntry;
152 if ( ( nTest = xRef->getSomething ( ZipPackageFolder::static_getImplementationId() ) ) != 0 )
154 ZipPackageFolder *pFolder = reinterpret_cast < ZipPackageFolder * > ( nTest );
155 pEntry = static_cast < ZipPackageEntry * > ( pFolder );
157 else if ( ( nTest = xRef->getSomething ( ZipPackageStream::static_getImplementationId() ) ) != 0 )
159 ZipPackageStream *pStream = reinterpret_cast < ZipPackageStream * > ( nTest );
160 pEntry = static_cast < ZipPackageEntry * > ( pStream );
162 else
163 throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
165 if (pEntry->getName() != aName )
166 pEntry->setName (aName);
167 doInsertByName ( pEntry, sal_True );
169 else
170 throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
173 void SAL_CALL ZipPackageFolder::removeByName( const OUString& Name )
174 throw(NoSuchElementException, WrappedTargetException, RuntimeException)
176 ContentHash::iterator aIter = maContents.find ( Name );
177 if ( aIter == maContents.end() )
178 throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
179 maContents.erase( aIter );
181 // XEnumerationAccess
182 Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( )
183 throw(RuntimeException)
185 return Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents));
187 // XElementAccess
188 Type SAL_CALL ZipPackageFolder::getElementType( )
189 throw(RuntimeException)
191 return ::getCppuType ((const Reference< XUnoTunnel > *) 0);
193 sal_Bool SAL_CALL ZipPackageFolder::hasElements( )
194 throw(RuntimeException)
196 return maContents.size() > 0;
198 // XNameAccess
199 ContentInfo& ZipPackageFolder::doGetByName( const OUString& aName )
200 throw(NoSuchElementException, WrappedTargetException, RuntimeException)
202 ContentHash::iterator aIter = maContents.find ( aName );
203 if ( aIter == maContents.end())
204 throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
205 return *(*aIter).second;
207 Any SAL_CALL ZipPackageFolder::getByName( const OUString& aName )
208 throw(NoSuchElementException, WrappedTargetException, RuntimeException)
210 return makeAny ( doGetByName ( aName ).xTunnel );
212 Sequence< OUString > SAL_CALL ZipPackageFolder::getElementNames( )
213 throw(RuntimeException)
215 sal_uInt32 i=0, nSize = maContents.size();
216 Sequence < OUString > aSequence ( nSize );
217 OUString *pNames = aSequence.getArray();
218 for ( ContentHash::const_iterator aIterator = maContents.begin(), aEnd = maContents.end();
219 aIterator != aEnd;
220 ++i, ++aIterator)
221 pNames[i] = (*aIterator).first;
222 return aSequence;
224 sal_Bool SAL_CALL ZipPackageFolder::hasByName( const OUString& aName )
225 throw(RuntimeException)
227 return maContents.find ( aName ) != maContents.end ();
229 // XNameReplace
230 void SAL_CALL ZipPackageFolder::replaceByName( const OUString& aName, const Any& aElement )
231 throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
233 if ( hasByName( aName ) )
234 removeByName( aName );
235 else
236 throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
237 insertByName(aName, aElement);
240 static void ImplSetStoredData( ZipEntry & rEntry, Reference < XInputStream> & rStream )
242 // It's very annoying that we have to do this, but lots of zip packages
243 // don't allow data descriptors for STORED streams, meaning we have to
244 // know the size and CRC32 of uncompressed streams before we actually
245 // write them !
246 CRC32 aCRC32;
247 rEntry.nMethod = STORED;
248 rEntry.nCompressedSize = rEntry.nSize = aCRC32.updateStream ( rStream );
249 rEntry.nCrc = aCRC32.getValue();
252 void ZipPackageFolder::saveContents(OUString &rPath, std::vector < Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, Sequence < sal_Int8 > &rEncryptionKey, rtlRandomPool &rRandomPool)
253 throw(RuntimeException)
255 sal_Bool bWritingFailed = sal_False;
256 ZipPackageFolder *pFolder = NULL;
257 ZipPackageStream *pStream = NULL;
258 const OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) );
259 const OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) );
260 const OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) );
261 const OUString sInitialisationVectorProperty ( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) );
262 const OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) );
263 const OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) );
264 const OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) );
265 const OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) );
267 sal_Bool bHaveEncryptionKey = rEncryptionKey.getLength() ? sal_True : sal_False;
269 if ( maContents.begin() == maContents.end() && rPath.getLength() && m_nFormat != OFOPXML_FORMAT )
271 // it is an empty subfolder, use workaround to store it
272 ZipEntry* pTempEntry = new ZipEntry();
273 ZipPackageFolder::copyZipEntry ( *pTempEntry, aEntry );
274 pTempEntry->nNameLen = (sal_Int16)( ::rtl::OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
275 pTempEntry->nExtraLen = -1;
276 pTempEntry->sName = rPath;
280 vos::ORef < EncryptionData > aEmptyEncr;
281 rZipOut.putNextEntry ( *pTempEntry, aEmptyEncr, sal_False );
282 rZipOut.rawCloseEntry();
284 catch ( ZipException& )
286 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
287 bWritingFailed = sal_True;
289 catch ( IOException& )
291 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
292 bWritingFailed = sal_True;
296 for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
297 aCI != aEnd;
298 aCI++)
300 const OUString &rShortName = (*aCI).first;
301 const ContentInfo &rInfo = *(*aCI).second;
303 Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST);
304 PropertyValue *pValue = aPropSet.getArray();
306 if ( rInfo.bFolder )
307 pFolder = rInfo.pFolder;
308 else
309 pStream = rInfo.pStream;
311 if ( rInfo.bFolder )
313 OUString sTempName = rPath + rShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
315 pValue[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
316 pValue[PKG_MNFST_MEDIATYPE].Value <<= pFolder->GetMediaType();
317 pValue[PKG_MNFST_VERSION].Name = sVersionProperty;
318 pValue[PKG_MNFST_VERSION].Value <<= pFolder->GetVersion();
319 pValue[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
320 pValue[PKG_MNFST_FULLPATH].Value <<= sTempName;
322 pFolder->saveContents( sTempName, rManList, rZipOut, rEncryptionKey, rRandomPool);
324 else
326 // if pTempEntry is necessary, it will be released and passed to the ZipOutputStream
327 // and be deleted in the ZipOutputStream destructor
328 auto_ptr < ZipEntry > pAutoTempEntry ( new ZipEntry );
329 ZipEntry* pTempEntry = pAutoTempEntry.get();
331 // In case the entry we are reading is also the entry we are writing, we will
332 // store the ZipEntry data in pTempEntry
334 ZipPackageFolder::copyZipEntry ( *pTempEntry, pStream->aEntry );
335 pTempEntry->sName = rPath + rShortName;
336 pTempEntry->nNameLen = (sal_Int16)( ::rtl::OUStringToOString( pTempEntry->sName, RTL_TEXTENCODING_UTF8 ).getLength() );
338 sal_Bool bToBeEncrypted = pStream->IsToBeEncrypted() && (bHaveEncryptionKey || pStream->HasOwnKey());
339 sal_Bool bToBeCompressed = bToBeEncrypted ? sal_True : pStream->IsToBeCompressed();
341 pValue[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
342 pValue[PKG_MNFST_MEDIATYPE].Value <<= pStream->GetMediaType( );
343 pValue[PKG_MNFST_VERSION].Name = sVersionProperty;
344 pValue[PKG_MNFST_VERSION].Value <<= ::rtl::OUString(); // no version is stored for streams currently
345 pValue[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
346 pValue[PKG_MNFST_FULLPATH].Value <<= pTempEntry->sName;
349 OSL_ENSURE( pStream->GetStreamMode() != PACKAGE_STREAM_NOTSET, "Unacceptable ZipPackageStream mode!" );
351 sal_Bool bRawStream = sal_False;
352 if ( pStream->GetStreamMode() == PACKAGE_STREAM_DETECT )
353 bRawStream = pStream->ParsePackageRawStream();
354 else if ( pStream->GetStreamMode() == PACKAGE_STREAM_RAW )
355 bRawStream = sal_True;
357 sal_Bool bTransportOwnEncrStreamAsRaw = sal_False;
358 // During the storing the original size of the stream can be changed
359 // TODO/LATER: get rid of this hack
360 sal_Int32 nOwnStreamOrigSize = bRawStream ? pStream->GetMagicalHackSize() : pStream->getSize();
362 sal_Bool bUseNonSeekableAccess = sal_False;
363 Reference < XInputStream > xStream;
364 if ( !pStream->IsPackageMember() && !bRawStream && !bToBeEncrypted && bToBeCompressed )
366 // the stream is not a package member, not a raw stream,
367 // it should not be encrypted and it should be compressed,
368 // in this case nonseekable access can be used
370 xStream = pStream->GetOwnStreamNoWrap();
371 Reference < XSeekable > xSeek ( xStream, UNO_QUERY );
373 bUseNonSeekableAccess = ( xStream.is() && !xSeek.is() );
376 if ( !bUseNonSeekableAccess )
378 xStream = pStream->getRawData();
380 if ( !xStream.is() )
382 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
383 bWritingFailed = sal_True;
384 continue;
387 Reference < XSeekable > xSeek ( xStream, UNO_QUERY );
390 if ( xSeek.is() )
392 // If the stream is a raw one, then we should be positioned
393 // at the beginning of the actual data
394 if ( !bToBeCompressed || bRawStream )
396 // The raw stream can neither be encrypted nor connected
397 OSL_ENSURE( !bRawStream || !bToBeCompressed && !bToBeEncrypted, "The stream is already encrypted!\n" );
398 xSeek->seek ( bRawStream ? pStream->GetMagicalHackPos() : 0 );
399 ImplSetStoredData ( *pTempEntry, xStream );
401 // TODO/LATER: Get rid of hacks related to switching of Flag Method and Size properties!
403 else if ( bToBeEncrypted )
405 // this is the correct original size
406 pTempEntry->nSize = static_cast < sal_Int32 > ( xSeek->getLength() );
407 nOwnStreamOrigSize = pTempEntry->nSize;
410 xSeek->seek ( 0 );
412 else
414 // Okay, we don't have an xSeekable stream. This is possibly bad.
415 // check if it's one of our own streams, if it is then we know that
416 // each time we ask for it we'll get a new stream that will be
417 // at position zero...otherwise, assert and skip this stream...
418 if ( pStream->IsPackageMember() )
420 // if the password has been changed than the stream should not be package member any more
421 if ( pStream->IsEncrypted() && pStream->IsToBeEncrypted() )
423 // Should be handled close to the raw stream handling
424 bTransportOwnEncrStreamAsRaw = sal_True;
425 pTempEntry->nMethod = STORED;
427 // TODO/LATER: get rid of this situation
428 // this size should be different from the one that will be stored in manifest.xml
429 // it is used in storing algorithms and after storing the correct size will be set
430 pTempEntry->nSize = pTempEntry->nCompressedSize;
433 else
435 VOS_ENSURE( 0, "The package component requires that every stream either be FROM a package or it must support XSeekable!" );
436 continue;
440 catch ( Exception& )
442 VOS_ENSURE( 0, "The stream provided to the package component has problems!" );
443 bWritingFailed = sal_True;
444 continue;
447 if ( bToBeEncrypted || bRawStream || bTransportOwnEncrStreamAsRaw )
449 if ( bToBeEncrypted && !bTransportOwnEncrStreamAsRaw )
451 Sequence < sal_uInt8 > aSalt ( 16 ), aVector ( 8 );
452 rtl_random_getBytes ( rRandomPool, aSalt.getArray(), 16 );
453 rtl_random_getBytes ( rRandomPool, aVector.getArray(), 8 );
454 sal_Int32 nIterationCount = 1024;
456 if ( !pStream->HasOwnKey() )
457 pStream->setKey ( rEncryptionKey );
459 pStream->setInitialisationVector ( aVector );
460 pStream->setSalt ( aSalt );
461 pStream->setIterationCount ( nIterationCount );
464 // last property is digest, which is inserted later if we didn't have
465 // a magic header
466 aPropSet.realloc(PKG_SIZE_ENCR_MNFST);
468 pValue = aPropSet.getArray();
469 pValue[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
470 pValue[PKG_MNFST_INIVECTOR].Value <<= pStream->getInitialisationVector();
471 pValue[PKG_MNFST_SALT].Name = sSaltProperty;
472 pValue[PKG_MNFST_SALT].Value <<= pStream->getSalt();
473 pValue[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
474 pValue[PKG_MNFST_ITERATION].Value <<= pStream->getIterationCount ();
476 // Need to store the uncompressed size in the manifest
477 OSL_ENSURE( nOwnStreamOrigSize >= 0, "The stream size was not correctly initialized!\n" );
478 pValue[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
479 pValue[PKG_MNFST_UCOMPSIZE].Value <<= nOwnStreamOrigSize;
481 if ( bRawStream || bTransportOwnEncrStreamAsRaw )
483 pValue[PKG_MNFST_DIGEST].Name = sDigestProperty;
484 pValue[PKG_MNFST_DIGEST].Value <<= pStream->getDigest();
489 // If the entry is already stored in the zip file in the format we
490 // want for this write...copy it raw
491 if ( !bUseNonSeekableAccess &&
492 ( bRawStream || bTransportOwnEncrStreamAsRaw ||
493 ( pStream->IsPackageMember() && !bToBeEncrypted &&
494 ( pStream->aEntry.nMethod == DEFLATED && bToBeCompressed ) ||
495 ( pStream->aEntry.nMethod == STORED && !bToBeCompressed ) ) ) )
497 // If it's a PackageMember, then it's an unbuffered stream and we need
498 // to get a new version of it as we can't seek backwards.
499 if ( pStream->IsPackageMember() )
501 xStream = pStream->getRawData();
502 if ( !xStream.is() )
504 // Make sure that we actually _got_ a new one !
505 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
506 continue;
512 if ( bRawStream )
513 xStream->skipBytes( pStream->GetMagicalHackPos() );
515 rZipOut.putNextEntry ( *pTempEntry, pStream->getEncryptionData(), sal_False );
516 // the entry is provided to the ZipOutputStream that will delete it
517 pAutoTempEntry.release();
519 Sequence < sal_Int8 > aSeq ( n_ConstBufferSize );
520 sal_Int32 nLength;
524 nLength = xStream->readBytes( aSeq, n_ConstBufferSize );
525 rZipOut.rawWrite(aSeq, 0, nLength);
527 while ( nLength == n_ConstBufferSize );
529 rZipOut.rawCloseEntry();
531 catch ( ZipException& )
533 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
534 bWritingFailed = sal_True;
536 catch ( IOException& )
538 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
539 bWritingFailed = sal_True;
542 else
544 // This stream is defenitly not a raw stream
546 // If nonseekable access is used the stream should be at the beginning and
547 // is useless after the storing. Thus if the storing fails the package should
548 // be thrown away ( as actually it is done currently )!
549 // To allow to reuse the package after the error, the optimization must be removed!
551 // If it's a PackageMember, then our previous reference held a 'raw' stream
552 // so we need to re-get it, unencrypted, uncompressed and positioned at the
553 // beginning of the stream
554 if ( pStream->IsPackageMember() )
556 xStream = pStream->getInputStream();
557 if ( !xStream.is() )
559 // Make sure that we actually _got_ a new one !
560 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
561 continue;
565 if ( bToBeCompressed )
567 pTempEntry->nMethod = DEFLATED;
568 pTempEntry->nCrc = pTempEntry->nCompressedSize = pTempEntry->nSize = -1;
573 rZipOut.putNextEntry ( *pTempEntry, pStream->getEncryptionData(), bToBeEncrypted);
574 // the entry is provided to the ZipOutputStream that will delete it
575 pAutoTempEntry.release();
577 sal_Int32 nLength;
578 Sequence < sal_Int8 > aSeq (n_ConstBufferSize);
581 nLength = xStream->readBytes(aSeq, n_ConstBufferSize);
582 rZipOut.write(aSeq, 0, nLength);
584 while ( nLength == n_ConstBufferSize );
586 rZipOut.closeEntry();
588 catch ( ZipException& )
590 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
591 bWritingFailed = sal_True;
593 catch ( IOException& )
595 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
596 bWritingFailed = sal_True;
599 if ( bToBeEncrypted )
601 pValue[PKG_MNFST_DIGEST].Name = sDigestProperty;
602 pValue[PKG_MNFST_DIGEST].Value <<= pStream->getDigest();
603 pStream->SetIsEncrypted ( sal_True );
607 if( !bWritingFailed )
609 if ( !pStream->IsPackageMember() )
611 pStream->CloseOwnStreamIfAny();
612 pStream->SetPackageMember ( sal_True );
615 if ( bRawStream )
617 // the raw stream was integrated and now behaves
618 // as usual encrypted stream
619 pStream->SetToBeEncrypted( sal_True );
622 // Remove hacky bit from entry flags
623 if ( pTempEntry->nFlag & ( 1 << 4 ) )
625 pTempEntry->nFlag &= ~( 1 << 4 );
626 pTempEntry->nMethod = STORED;
629 // Then copy it back afterwards...
630 ZipPackageFolder::copyZipEntry ( pStream->aEntry, *pTempEntry );
632 // TODO/LATER: get rid of this hack ( the encrypted stream size property is changed during saving )
633 if ( pStream->IsEncrypted() )
634 pStream->setSize( nOwnStreamOrigSize );
636 pStream->aEntry.sName = rShortName;
637 pStream->aEntry.nOffset *= -1;
641 // folder can have a mediatype only in package format
642 if ( m_nFormat == PACKAGE_FORMAT || ( m_nFormat == OFOPXML_FORMAT && !rInfo.bFolder ) )
643 rManList.push_back( aPropSet );
646 if( bWritingFailed )
647 throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
650 void ZipPackageFolder::releaseUpwardRef( void )
652 // Now it is possible that a package folder is disconnected from the package before removing of the folder.
653 // Such a scenario is used in storage implementation. When a new version of a folder is provided the old
654 // one is retrieved, removed from the package but preserved for the error handling.
655 // In this scenario the referencing to the parent is not really useful, since it requires disposing.
657 // Actually there is no need in having a reference to the parent, it even make things more complicated and
658 // requires disposing mechanics. Using of a simple pointer seems to be easier solution and also a safe enough.
660 clearParent();
662 #if 0
663 for ( ContentHash::const_iterator aCI = maContents.begin();
664 aCI!=maContents.end();
665 aCI++)
667 ContentInfo &rInfo = * (*aCI).second;
668 if ( rInfo.bFolder )// && ! rInfo.pFolder->HasReleased () )
669 rInfo.pFolder->releaseUpwardRef();
670 else //if ( !rInfo.bFolder && !rInfo.pStream->HasReleased() )
671 rInfo.pStream->clearParent();
673 clearParent();
675 VOS_ENSURE ( m_refCount == 1, "Ref-count is not 1!" );
676 #endif
679 sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const Sequence< sal_Int8 >& aIdentifier )
680 throw(RuntimeException)
682 sal_Int64 nMe = 0;
683 if ( aIdentifier.getLength() == 16 &&
684 0 == rtl_compareMemory(static_getImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) )
685 nMe = reinterpret_cast < sal_Int64 > ( this );
686 return nMe;
688 void SAL_CALL ZipPackageFolder::setPropertyValue( const OUString& aPropertyName, const Any& aValue )
689 throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
691 if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType")))
693 // TODO/LATER: activate when zip ucp is ready
694 // if ( m_nFormat != PACKAGE_FORMAT )
695 // throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
697 aValue >>= sMediaType;
699 else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Version")))
700 aValue >>= m_sVersion;
701 else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Size") ) )
702 aValue >>= aEntry.nSize;
703 else
704 throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
706 Any SAL_CALL ZipPackageFolder::getPropertyValue( const OUString& PropertyName )
707 throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
709 if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) )
711 // TODO/LATER: activate when zip ucp is ready
712 // if ( m_nFormat != PACKAGE_FORMAT )
713 // throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
715 return makeAny ( sMediaType );
717 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Version" ) ) )
718 return makeAny( m_sVersion );
719 else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) )
720 return makeAny ( aEntry.nSize );
721 else
722 throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
725 void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, sal_Bool bSetParent )
726 throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
728 if ( pEntry->IsFolder() )
729 maContents[pEntry->aEntry.sName] = new ContentInfo ( static_cast < ZipPackageFolder *> ( pEntry ) );
730 else
731 maContents[pEntry->aEntry.sName] = new ContentInfo ( static_cast < ZipPackageStream *> ( pEntry ) );
733 if ( bSetParent )
734 pEntry->setParent ( *this );
736 OUString ZipPackageFolder::getImplementationName()
737 throw (RuntimeException)
739 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageFolder" ) );
742 Sequence< OUString > ZipPackageFolder::getSupportedServiceNames()
743 throw (RuntimeException)
745 Sequence< OUString > aNames(1);
746 aNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageFolder" ) );
747 return aNames;
749 sal_Bool SAL_CALL ZipPackageFolder::supportsService( OUString const & rServiceName )
750 throw (RuntimeException)
752 return rServiceName == getSupportedServiceNames()[0];