1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ZipPackageFolder.cxx,v $
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>
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>
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
;
61 using namespace ::com::sun::star
;
64 Sequence
< sal_Int8
> ZipPackageFolder::aImplementationId
= Sequence
< sal_Int8
> ();
66 ZipPackageFolder::ZipPackageFolder ( const Reference
< XMultiServiceFactory
>& xFactory
,
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
);
79 aEntry
.nMethod
= STORED
;
82 aEntry
.nCompressedSize
= 0;
85 if ( !aImplementationId
.getLength() )
87 aImplementationId
= getImplementationId();
92 ZipPackageFolder::~ZipPackageFolder()
96 void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair
& aPair
)
99 if ( aPair
.First
.toChar() == (sal_Unicode
)'.' )
102 aExt
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) + aPair
.First
;
104 for ( ContentHash::const_iterator aCI
= maContents
.begin(), aEnd
= maContents
.end();
108 const OUString
&rShortName
= (*aCI
).first
;
109 const ContentInfo
&rInfo
= *(*aCI
).second
;
112 rInfo
.pFolder
->setChildStreamsTypeByExtension( aPair
);
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
;
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();
146 Reference
< XUnoTunnel
> xRef
;
148 if ( ( aElement
>>= xRef
) )
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
);
163 throw IllegalArgumentException();
165 if (pEntry
->getName() != aName
)
166 pEntry
->setName (aName
);
167 doInsertByName ( pEntry
, sal_True
);
170 throw IllegalArgumentException();
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();
179 maContents
.erase( aIter
);
181 // XEnumerationAccess
182 Reference
< XEnumeration
> SAL_CALL
ZipPackageFolder::createEnumeration( )
183 throw(RuntimeException
)
185 return Reference
< XEnumeration
> (new ZipPackageFolderEnumeration(maContents
));
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;
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();
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();
221 pNames
[i
] = (*aIterator
).first
;
224 sal_Bool SAL_CALL
ZipPackageFolder::hasByName( const OUString
& aName
)
225 throw(RuntimeException
)
227 return maContents
.find ( aName
) != maContents
.end ();
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
);
236 throw NoSuchElementException();
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
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();
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();
307 pFolder
= rInfo
.pFolder
;
309 pStream
= rInfo
.pStream
;
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
);
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();
382 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
383 bWritingFailed
= sal_True
;
387 Reference
< XSeekable
> xSeek ( xStream
, UNO_QUERY
);
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
;
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
;
435 VOS_ENSURE( 0, "The package component requires that every stream either be FROM a package or it must support XSeekable!" );
442 VOS_ENSURE( 0, "The stream provided to the package component has problems!" );
443 bWritingFailed
= sal_True
;
447 if ( bToBeEncrypted
|| bRawStream
|| bTransportOwnEncrStreamAsRaw
)
449 if ( bToBeEncrypted
&& !bTransportOwnEncrStreamAsRaw
)
451 Sequence
< sal_uInt8
> aSalt ( 16 ), aVector ( 8 );
452 Sequence
< sal_Int8
> aKey ( 16 );
453 rtl_random_getBytes ( rRandomPool
, aSalt
.getArray(), 16 );
454 rtl_random_getBytes ( rRandomPool
, aVector
.getArray(), 8 );
455 sal_Int32 nIterationCount
= 1024;
457 if ( pStream
->HasOwnKey() )
458 rtl_digest_PBKDF2 ( reinterpret_cast < sal_uInt8
* > (aKey
.getArray()), 16,
459 reinterpret_cast < const sal_uInt8
* > (pStream
->getKey().getConstArray()), pStream
->getKey().getLength(),
460 reinterpret_cast < const sal_uInt8
* > ( aSalt
.getConstArray() ), 16,
463 rtl_digest_PBKDF2 ( reinterpret_cast < sal_uInt8
* > (aKey
.getArray()), 16,
464 reinterpret_cast < const sal_uInt8
* > (rEncryptionKey
.getConstArray()), rEncryptionKey
.getLength(),
465 reinterpret_cast < const sal_uInt8
* > ( aSalt
.getConstArray() ), 16,
467 pStream
->setInitialisationVector ( aVector
);
468 pStream
->setSalt ( aSalt
);
469 pStream
->setIterationCount ( nIterationCount
);
470 pStream
->setKey ( aKey
);
473 // last property is digest, which is inserted later if we didn't have
475 aPropSet
.realloc(PKG_SIZE_ENCR_MNFST
);
477 pValue
= aPropSet
.getArray();
478 pValue
[PKG_MNFST_INIVECTOR
].Name
= sInitialisationVectorProperty
;
479 pValue
[PKG_MNFST_INIVECTOR
].Value
<<= pStream
->getInitialisationVector();
480 pValue
[PKG_MNFST_SALT
].Name
= sSaltProperty
;
481 pValue
[PKG_MNFST_SALT
].Value
<<= pStream
->getSalt();
482 pValue
[PKG_MNFST_ITERATION
].Name
= sIterationCountProperty
;
483 pValue
[PKG_MNFST_ITERATION
].Value
<<= pStream
->getIterationCount ();
485 // Need to store the uncompressed size in the manifest
486 OSL_ENSURE( nOwnStreamOrigSize
>= 0, "The stream size was not correctly initialized!\n" );
487 pValue
[PKG_MNFST_UCOMPSIZE
].Name
= sSizeProperty
;
488 pValue
[PKG_MNFST_UCOMPSIZE
].Value
<<= nOwnStreamOrigSize
;
490 if ( bRawStream
|| bTransportOwnEncrStreamAsRaw
)
492 pValue
[PKG_MNFST_DIGEST
].Name
= sDigestProperty
;
493 pValue
[PKG_MNFST_DIGEST
].Value
<<= pStream
->getDigest();
498 // If the entry is already stored in the zip file in the format we
499 // want for this write...copy it raw
500 if ( !bUseNonSeekableAccess
&&
501 ( bRawStream
|| bTransportOwnEncrStreamAsRaw
||
502 ( pStream
->IsPackageMember() && !bToBeEncrypted
&&
503 ( pStream
->aEntry
.nMethod
== DEFLATED
&& bToBeCompressed
) ||
504 ( pStream
->aEntry
.nMethod
== STORED
&& !bToBeCompressed
) ) ) )
506 // If it's a PackageMember, then it's an unbuffered stream and we need
507 // to get a new version of it as we can't seek backwards.
508 if ( pStream
->IsPackageMember() )
510 xStream
= pStream
->getRawData();
513 // Make sure that we actually _got_ a new one !
514 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
522 xStream
->skipBytes( pStream
->GetMagicalHackPos() );
524 rZipOut
.putNextEntry ( *pTempEntry
, pStream
->getEncryptionData(), sal_False
);
525 // the entry is provided to the ZipOutputStream that will delete it
526 pAutoTempEntry
.release();
528 Sequence
< sal_Int8
> aSeq ( n_ConstBufferSize
);
533 nLength
= xStream
->readBytes( aSeq
, n_ConstBufferSize
);
534 rZipOut
.rawWrite(aSeq
, 0, nLength
);
536 while ( nLength
== n_ConstBufferSize
);
538 rZipOut
.rawCloseEntry();
540 catch ( ZipException
& )
542 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
543 bWritingFailed
= sal_True
;
545 catch ( IOException
& )
547 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
548 bWritingFailed
= sal_True
;
553 // This stream is defenitly not a raw stream
555 // If nonseekable access is used the stream should be at the beginning and
556 // is useless after the storing. Thus if the storing fails the package should
557 // be thrown away ( as actually it is done currently )!
558 // To allow to reuse the package after the error, the optimization must be removed!
560 // If it's a PackageMember, then our previous reference held a 'raw' stream
561 // so we need to re-get it, unencrypted, uncompressed and positioned at the
562 // beginning of the stream
563 if ( pStream
->IsPackageMember() )
565 xStream
= pStream
->getInputStream();
568 // Make sure that we actually _got_ a new one !
569 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
574 if ( bToBeCompressed
)
576 pTempEntry
->nMethod
= DEFLATED
;
577 pTempEntry
->nCrc
= pTempEntry
->nCompressedSize
= pTempEntry
->nSize
= -1;
582 rZipOut
.putNextEntry ( *pTempEntry
, pStream
->getEncryptionData(), bToBeEncrypted
);
583 // the entry is provided to the ZipOutputStream that will delete it
584 pAutoTempEntry
.release();
587 Sequence
< sal_Int8
> aSeq (n_ConstBufferSize
);
590 nLength
= xStream
->readBytes(aSeq
, n_ConstBufferSize
);
591 rZipOut
.write(aSeq
, 0, nLength
);
593 while ( nLength
== n_ConstBufferSize
);
595 rZipOut
.closeEntry();
597 catch ( ZipException
& )
599 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
600 bWritingFailed
= sal_True
;
602 catch ( IOException
& )
604 VOS_ENSURE( 0, "Error writing ZipOutputStream" );
605 bWritingFailed
= sal_True
;
608 if ( bToBeEncrypted
)
610 pValue
[PKG_MNFST_DIGEST
].Name
= sDigestProperty
;
611 pValue
[PKG_MNFST_DIGEST
].Value
<<= pStream
->getDigest();
612 pStream
->SetIsEncrypted ( sal_True
);
616 if( !bWritingFailed
)
618 if ( !pStream
->IsPackageMember() )
620 pStream
->CloseOwnStreamIfAny();
621 pStream
->SetPackageMember ( sal_True
);
626 // the raw stream was integrated and now behaves
627 // as usual encrypted stream
628 pStream
->SetToBeEncrypted( sal_True
);
631 // Remove hacky bit from entry flags
632 if ( pTempEntry
->nFlag
& ( 1 << 4 ) )
634 pTempEntry
->nFlag
&= ~( 1 << 4 );
635 pTempEntry
->nMethod
= STORED
;
638 // Then copy it back afterwards...
639 ZipPackageFolder::copyZipEntry ( pStream
->aEntry
, *pTempEntry
);
641 // TODO/LATER: get rid of this hack ( the encrypted stream size property is changed during saving )
642 if ( pStream
->IsEncrypted() )
643 pStream
->setSize( nOwnStreamOrigSize
);
645 pStream
->aEntry
.sName
= rShortName
;
646 pStream
->aEntry
.nOffset
*= -1;
650 // folder can have a mediatype only in package format
651 if ( m_nFormat
== PACKAGE_FORMAT
|| ( m_nFormat
== OFOPXML_FORMAT
&& !rInfo
.bFolder
) )
652 rManList
.push_back( aPropSet
);
656 throw RuntimeException();
659 void ZipPackageFolder::releaseUpwardRef( void )
661 // Now it is possible that a package folder is disconnected from the package before removing of the folder.
662 // Such a scenario is used in storage implementation. When a new version of a folder is provided the old
663 // one is retrieved, removed from the package but preserved for the error handling.
664 // In this scenario the referencing to the parent is not really useful, since it requires disposing.
666 // Actually there is no need in having a reference to the parent, it even make things more complicated and
667 // requires disposing mechanics. Using of a simple pointer seems to be easier solution and also a safe enough.
672 for ( ContentHash::const_iterator aCI
= maContents
.begin();
673 aCI
!=maContents
.end();
676 ContentInfo
&rInfo
= * (*aCI
).second
;
677 if ( rInfo
.bFolder
)// && ! rInfo.pFolder->HasReleased () )
678 rInfo
.pFolder
->releaseUpwardRef();
679 else //if ( !rInfo.bFolder && !rInfo.pStream->HasReleased() )
680 rInfo
.pStream
->clearParent();
684 VOS_ENSURE ( m_refCount
== 1, "Ref-count is not 1!" );
688 sal_Int64 SAL_CALL
ZipPackageFolder::getSomething( const Sequence
< sal_Int8
>& aIdentifier
)
689 throw(RuntimeException
)
692 if ( aIdentifier
.getLength() == 16 &&
693 0 == rtl_compareMemory(static_getImplementationId().getConstArray(), aIdentifier
.getConstArray(), 16 ) )
694 nMe
= reinterpret_cast < sal_Int64
> ( this );
697 void SAL_CALL
ZipPackageFolder::setPropertyValue( const OUString
& aPropertyName
, const Any
& aValue
)
698 throw(UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
700 if (aPropertyName
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType")))
702 // TODO/LATER: activate when zip ucp is ready
703 // if ( m_nFormat != PACKAGE_FORMAT )
704 // throw UnknownPropertyException();
706 aValue
>>= sMediaType
;
708 else if (aPropertyName
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Version")))
709 aValue
>>= m_sVersion
;
710 else if (aPropertyName
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Size") ) )
711 aValue
>>= aEntry
.nSize
;
713 throw UnknownPropertyException();
715 Any SAL_CALL
ZipPackageFolder::getPropertyValue( const OUString
& PropertyName
)
716 throw(UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
718 if (PropertyName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) )
720 // TODO/LATER: activate when zip ucp is ready
721 // if ( m_nFormat != PACKAGE_FORMAT )
722 // throw UnknownPropertyException();
724 return makeAny ( sMediaType
);
726 else if (PropertyName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Version" ) ) )
727 return makeAny( m_sVersion
);
728 else if (PropertyName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) )
729 return makeAny ( aEntry
.nSize
);
731 throw UnknownPropertyException();
734 void ZipPackageFolder::doInsertByName ( ZipPackageEntry
*pEntry
, sal_Bool bSetParent
)
735 throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
)
737 if ( pEntry
->IsFolder() )
738 maContents
[pEntry
->aEntry
.sName
] = new ContentInfo ( static_cast < ZipPackageFolder
*> ( pEntry
) );
740 maContents
[pEntry
->aEntry
.sName
] = new ContentInfo ( static_cast < ZipPackageStream
*> ( pEntry
) );
743 pEntry
->setParent ( *this );
745 OUString
ZipPackageFolder::getImplementationName()
746 throw (RuntimeException
)
748 return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageFolder" ) );
751 Sequence
< OUString
> ZipPackageFolder::getSupportedServiceNames()
752 throw (RuntimeException
)
754 Sequence
< OUString
> aNames(1);
755 aNames
[0] = OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageFolder" ) );
758 sal_Bool SAL_CALL
ZipPackageFolder::supportsService( OUString
const & rServiceName
)
759 throw (RuntimeException
)
761 return rServiceName
== getSupportedServiceNames()[0];