1*a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a3872823SAndrew Rist  * distributed with this work for additional information
6*a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9*a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
13*a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a3872823SAndrew Rist  * software distributed under the License is distributed on an
15*a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17*a3872823SAndrew Rist  * specific language governing permissions and limitations
18*a3872823SAndrew Rist  * under the License.
19*a3872823SAndrew Rist  *
20*a3872823SAndrew Rist  *************************************************************/
21*a3872823SAndrew Rist 
22*a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <ZipPackageFolder.hxx>
27cdf0e10cSrcweir #include <ZipFile.hxx>
28cdf0e10cSrcweir #include <ZipOutputStream.hxx>
29cdf0e10cSrcweir #include <ZipPackageStream.hxx>
30cdf0e10cSrcweir #include <PackageConstants.hxx>
31cdf0e10cSrcweir #include <ZipPackageFolderEnumeration.hxx>
32cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipConstants.hpp>
33cdf0e10cSrcweir #include <com/sun/star/embed/StorageFormats.hpp>
34cdf0e10cSrcweir #include <vos/diagnose.hxx>
35cdf0e10cSrcweir #include <osl/time.h>
36cdf0e10cSrcweir #include <rtl/digest.h>
37cdf0e10cSrcweir #include <ContentInfo.hxx>
38cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
39cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
40cdf0e10cSrcweir #include <EncryptedDataHeader.hxx>
41cdf0e10cSrcweir #include <rtl/random.h>
42cdf0e10cSrcweir #include <rtl/instance.hxx>
43cdf0e10cSrcweir #include <memory>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace com::sun::star;
46cdf0e10cSrcweir using namespace com::sun::star::packages::zip::ZipConstants;
47cdf0e10cSrcweir using namespace com::sun::star::packages::zip;
48cdf0e10cSrcweir using namespace com::sun::star::packages;
49cdf0e10cSrcweir using namespace com::sun::star::container;
50cdf0e10cSrcweir using namespace com::sun::star::beans;
51cdf0e10cSrcweir using namespace com::sun::star::lang;
52cdf0e10cSrcweir using namespace com::sun::star::io;
53cdf0e10cSrcweir using namespace cppu;
54cdf0e10cSrcweir using namespace std;
55cdf0e10cSrcweir using namespace ::com::sun::star;
56cdf0e10cSrcweir using vos::ORef;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace { struct lcl_CachedImplId : public rtl::Static< uno::Sequence < sal_Int8 >, lcl_CachedImplId > {}; }
59cdf0e10cSrcweir 
ZipPackageFolder(const uno::Reference<XMultiServiceFactory> & xFactory,sal_Int32 nFormat,sal_Bool bAllowRemoveOnInsert)60cdf0e10cSrcweir ZipPackageFolder::ZipPackageFolder ( const uno::Reference< XMultiServiceFactory >& xFactory,
61cdf0e10cSrcweir 									 sal_Int32 nFormat,
62cdf0e10cSrcweir 									 sal_Bool bAllowRemoveOnInsert )
63cdf0e10cSrcweir : m_xFactory( xFactory )
64cdf0e10cSrcweir , m_nFormat( nFormat )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 	OSL_ENSURE( m_xFactory.is(), "No factory is provided to the package folder!" );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	this->mbAllowRemoveOnInsert = bAllowRemoveOnInsert;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	SetFolder ( sal_True );
71cdf0e10cSrcweir 	aEntry.nVersion		= -1;
72cdf0e10cSrcweir 	aEntry.nFlag		= 0;
73cdf0e10cSrcweir 	aEntry.nMethod		= STORED;
74cdf0e10cSrcweir 	aEntry.nTime		= -1;
75cdf0e10cSrcweir 	aEntry.nCrc			= 0;
76cdf0e10cSrcweir 	aEntry.nCompressedSize	= 0;
77cdf0e10cSrcweir 	aEntry.nSize		= 0;
78cdf0e10cSrcweir 	aEntry.nOffset		= -1;
79cdf0e10cSrcweir 	uno::Sequence < sal_Int8 > &rCachedImplId = lcl_CachedImplId::get();
80cdf0e10cSrcweir 	if ( !rCachedImplId.getLength() )
81cdf0e10cSrcweir 	    rCachedImplId = getImplementationId();
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
~ZipPackageFolder()85cdf0e10cSrcweir ZipPackageFolder::~ZipPackageFolder()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
LookForUnexpectedODF12Streams(const::rtl::OUString & aPath)89cdf0e10cSrcweir sal_Bool ZipPackageFolder::LookForUnexpectedODF12Streams( const ::rtl::OUString& aPath )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir     sal_Bool bHasUnexpected = sal_False;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
94cdf0e10cSrcweir 	      !bHasUnexpected && aCI != aEnd;
95cdf0e10cSrcweir 		  aCI++)
96cdf0e10cSrcweir 	{
97cdf0e10cSrcweir 		const ::rtl::OUString &rShortName = (*aCI).first;
98cdf0e10cSrcweir 		const ContentInfo &rInfo = *(*aCI).second;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		if ( rInfo.bFolder )
101cdf0e10cSrcweir 		{
102cdf0e10cSrcweir             if ( aPath.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "META-INF/" ) ) ) )
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 // META-INF is not allowed to contain subfolders
105cdf0e10cSrcweir                 bHasUnexpected = sal_True;
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir             else
108cdf0e10cSrcweir             {
109cdf0e10cSrcweir                 ::rtl::OUString sOwnPath = aPath + rShortName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
110cdf0e10cSrcweir                 bHasUnexpected = rInfo.pFolder->LookForUnexpectedODF12Streams( sOwnPath );
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir 		}
113cdf0e10cSrcweir 		else
114cdf0e10cSrcweir 		{
115cdf0e10cSrcweir             if ( aPath.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "META-INF/" ) ) ) )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 if ( !rShortName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "manifest.xml" ) ) )
118cdf0e10cSrcweir                   && rShortName.indexOf( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "signatures" ) ) ) == -1 )
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     // a stream from META-INF with unexpected name
121cdf0e10cSrcweir                     bHasUnexpected = sal_True;
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir                 // streams from META-INF with expected names are allowed not to be registered in manifest.xml
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir             else if ( !rInfo.pStream->IsFromManifest() )
127cdf0e10cSrcweir             {
128cdf0e10cSrcweir                 // the stream is not in META-INF and ist notregistered in manifest.xml,
129cdf0e10cSrcweir                 // check whether it is an internal part of the package format
130cdf0e10cSrcweir                 if ( aPath.getLength()
131cdf0e10cSrcweir                   || !rShortName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "mimetype" ) ) ) )
132cdf0e10cSrcweir                 {
133cdf0e10cSrcweir                     // if it is not "mimetype" from the root it is not a part of the package
134cdf0e10cSrcweir                     bHasUnexpected = sal_True;
135cdf0e10cSrcweir                 }
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     return bHasUnexpected;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
setChildStreamsTypeByExtension(const beans::StringPair & aPair)143cdf0e10cSrcweir void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair& aPair )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	::rtl::OUString aExt;
146cdf0e10cSrcweir 	if ( aPair.First.toChar() == (sal_Unicode)'.' )
147cdf0e10cSrcweir 		aExt = aPair.First;
148cdf0e10cSrcweir 	else
149cdf0e10cSrcweir 		aExt = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) + aPair.First;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
152cdf0e10cSrcweir 	      aCI != aEnd;
153cdf0e10cSrcweir 		  aCI++)
154cdf0e10cSrcweir 	{
155cdf0e10cSrcweir 		const ::rtl::OUString &rShortName = (*aCI).first;
156cdf0e10cSrcweir 		const ContentInfo &rInfo = *(*aCI).second;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		if ( rInfo.bFolder )
159cdf0e10cSrcweir 			rInfo.pFolder->setChildStreamsTypeByExtension( aPair );
160cdf0e10cSrcweir 		else
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			sal_Int32 nPathLength = rShortName.getLength();
163cdf0e10cSrcweir 			sal_Int32 nExtLength = aExt.getLength();
164cdf0e10cSrcweir 			if ( nPathLength >= nExtLength && rShortName.match( aExt, nPathLength - nExtLength ) )
165cdf0e10cSrcweir 				rInfo.pStream->SetMediaType( aPair.Second );
166cdf0e10cSrcweir 		}
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
copyZipEntry(ZipEntry & rDest,const ZipEntry & rSource)170cdf0e10cSrcweir void ZipPackageFolder::copyZipEntry( ZipEntry &rDest, const ZipEntry &rSource)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir   	rDest.nVersion			= rSource.nVersion;
173cdf0e10cSrcweir     rDest.nFlag				= rSource.nFlag;
174cdf0e10cSrcweir     rDest.nMethod			= rSource.nMethod;
175cdf0e10cSrcweir     rDest.nTime				= rSource.nTime;
176cdf0e10cSrcweir     rDest.nCrc				= rSource.nCrc;
177cdf0e10cSrcweir     rDest.nCompressedSize	= rSource.nCompressedSize;
178cdf0e10cSrcweir     rDest.nSize				= rSource.nSize;
179cdf0e10cSrcweir     rDest.nOffset			= rSource.nOffset;
180cdf0e10cSrcweir     rDest.sPath				= rSource.sPath;
181cdf0e10cSrcweir     rDest.nPathLen			= rSource.nPathLen;
182cdf0e10cSrcweir     rDest.nExtraLen			= rSource.nExtraLen;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
static_getImplementationId()185cdf0e10cSrcweir const ::com::sun::star::uno::Sequence < sal_Int8 >& ZipPackageFolder::static_getImplementationId()
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     return lcl_CachedImplId::get();
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	// XNameContainer
insertByName(const::rtl::OUString & aName,const uno::Any & aElement)191cdf0e10cSrcweir void SAL_CALL ZipPackageFolder::insertByName( const ::rtl::OUString& aName, const uno::Any& aElement )
192cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	if (hasByName(aName))
195cdf0e10cSrcweir 		throw ElementExistException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
196cdf0e10cSrcweir 	else
197cdf0e10cSrcweir 	{
198cdf0e10cSrcweir 		uno::Reference < XUnoTunnel > xRef;
199cdf0e10cSrcweir 		aElement >>= xRef;
200cdf0e10cSrcweir 		if ( (  aElement >>= xRef ) )
201cdf0e10cSrcweir 		{
202cdf0e10cSrcweir 			sal_Int64 nTest;
203cdf0e10cSrcweir 			ZipPackageEntry *pEntry;
204cdf0e10cSrcweir 			if ( ( nTest = xRef->getSomething ( ZipPackageFolder::static_getImplementationId() ) ) != 0 )
205cdf0e10cSrcweir 			{
206cdf0e10cSrcweir 				ZipPackageFolder *pFolder = reinterpret_cast < ZipPackageFolder * > ( nTest );
207cdf0e10cSrcweir 				pEntry = static_cast < ZipPackageEntry * > ( pFolder );
208cdf0e10cSrcweir 			}
209cdf0e10cSrcweir 			else if ( ( nTest = xRef->getSomething ( ZipPackageStream::static_getImplementationId() ) ) != 0 )
210cdf0e10cSrcweir 			{
211cdf0e10cSrcweir 				ZipPackageStream *pStream = reinterpret_cast < ZipPackageStream * > ( nTest );
212cdf0e10cSrcweir 				pEntry = static_cast < ZipPackageEntry * > ( pStream );
213cdf0e10cSrcweir 			}
214cdf0e10cSrcweir 			else
215cdf0e10cSrcweir 				throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 			if (pEntry->getName() != aName )
218cdf0e10cSrcweir 				pEntry->setName (aName);
219cdf0e10cSrcweir 			doInsertByName ( pEntry, sal_True );
220cdf0e10cSrcweir 		}
221cdf0e10cSrcweir 		else
222cdf0e10cSrcweir 			throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir }
removeByName(const::rtl::OUString & Name)225cdf0e10cSrcweir void SAL_CALL ZipPackageFolder::removeByName( const ::rtl::OUString& Name )
226cdf0e10cSrcweir 		throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException)
227cdf0e10cSrcweir {
228cdf0e10cSrcweir 	ContentHash::iterator aIter = maContents.find ( Name );
229cdf0e10cSrcweir 	if ( aIter == maContents.end() )
230cdf0e10cSrcweir 		throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
231cdf0e10cSrcweir 	maContents.erase( aIter );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 	// XEnumerationAccess
createEnumeration()234cdf0e10cSrcweir uno::Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration(  )
235cdf0e10cSrcweir 		throw(uno::RuntimeException)
236cdf0e10cSrcweir {
237cdf0e10cSrcweir 	return uno::Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents));
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 	// XElementAccess
getElementType()240cdf0e10cSrcweir uno::Type SAL_CALL ZipPackageFolder::getElementType(  )
241cdf0e10cSrcweir 		throw(uno::RuntimeException)
242cdf0e10cSrcweir {
243cdf0e10cSrcweir 	return ::getCppuType ((const uno::Reference< XUnoTunnel > *) 0);
244cdf0e10cSrcweir }
hasElements()245cdf0e10cSrcweir sal_Bool SAL_CALL ZipPackageFolder::hasElements(  )
246cdf0e10cSrcweir 		throw(uno::RuntimeException)
247cdf0e10cSrcweir {
248cdf0e10cSrcweir 	return maContents.size() > 0;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 	// XNameAccess
doGetByName(const::rtl::OUString & aName)251cdf0e10cSrcweir ContentInfo& ZipPackageFolder::doGetByName( const ::rtl::OUString& aName )
252cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException)
253cdf0e10cSrcweir {
254cdf0e10cSrcweir 	ContentHash::iterator aIter = maContents.find ( aName );
255cdf0e10cSrcweir 	if ( aIter == maContents.end())
256cdf0e10cSrcweir 		throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
257cdf0e10cSrcweir 	return *(*aIter).second;
258cdf0e10cSrcweir }
getByName(const::rtl::OUString & aName)259cdf0e10cSrcweir uno::Any SAL_CALL ZipPackageFolder::getByName( const ::rtl::OUString& aName )
260cdf0e10cSrcweir 	throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException)
261cdf0e10cSrcweir {
262cdf0e10cSrcweir 	return uno::makeAny ( doGetByName ( aName ).xTunnel );
263cdf0e10cSrcweir }
getElementNames()264cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL ZipPackageFolder::getElementNames(  )
265cdf0e10cSrcweir 		throw(uno::RuntimeException)
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	sal_uInt32 i=0, nSize = maContents.size();
268cdf0e10cSrcweir 	uno::Sequence < ::rtl::OUString > aSequence ( nSize );
269cdf0e10cSrcweir 	for ( ContentHash::const_iterator aIterator = maContents.begin(), aEnd = maContents.end();
270cdf0e10cSrcweir 		  aIterator != aEnd;
271cdf0e10cSrcweir 		  ++i, ++aIterator)
272cdf0e10cSrcweir 		aSequence[i] = (*aIterator).first;
273cdf0e10cSrcweir 	return aSequence;
274cdf0e10cSrcweir }
hasByName(const::rtl::OUString & aName)275cdf0e10cSrcweir sal_Bool SAL_CALL ZipPackageFolder::hasByName( const ::rtl::OUString& aName )
276cdf0e10cSrcweir 	throw(uno::RuntimeException)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir 	return maContents.find ( aName ) != maContents.end ();
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 	// XNameReplace
replaceByName(const::rtl::OUString & aName,const uno::Any & aElement)281cdf0e10cSrcweir void SAL_CALL ZipPackageFolder::replaceByName( const ::rtl::OUString& aName, const uno::Any& aElement )
282cdf0e10cSrcweir 		throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, uno::RuntimeException)
283cdf0e10cSrcweir {
284cdf0e10cSrcweir 	if ( hasByName( aName ) )
285cdf0e10cSrcweir 		removeByName( aName );
286cdf0e10cSrcweir 	else
287cdf0e10cSrcweir 		throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
288cdf0e10cSrcweir 	insertByName(aName, aElement);
289cdf0e10cSrcweir }
290cdf0e10cSrcweir 
ImplSetStoredData(ZipEntry & rEntry,uno::Reference<XInputStream> & rStream)291cdf0e10cSrcweir static void ImplSetStoredData( ZipEntry & rEntry, uno::Reference< XInputStream> & rStream )
292cdf0e10cSrcweir {
293cdf0e10cSrcweir 	// It's very annoying that we have to do this, but lots of zip packages
294cdf0e10cSrcweir 	// don't allow data descriptors for STORED streams, meaning we have to
295cdf0e10cSrcweir 	// know the size and CRC32 of uncompressed streams before we actually
296cdf0e10cSrcweir 	// write them !
297cdf0e10cSrcweir 	CRC32 aCRC32;
298cdf0e10cSrcweir 	rEntry.nMethod = STORED;
299cdf0e10cSrcweir 	rEntry.nCompressedSize = rEntry.nSize = aCRC32.updateStream ( rStream );
300cdf0e10cSrcweir 	rEntry.nCrc = aCRC32.getValue();
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
saveChild(const::rtl::OUString & rShortName,const ContentInfo & rInfo,::rtl::OUString & rPath,std::vector<uno::Sequence<PropertyValue>> & rManList,ZipOutputStream & rZipOut,const uno::Sequence<sal_Int8> & rEncryptionKey,rtlRandomPool & rRandomPool)303cdf0e10cSrcweir bool ZipPackageFolder::saveChild( const ::rtl::OUString &rShortName, const ContentInfo &rInfo, ::rtl::OUString &rPath, std::vector < uno::Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, const uno::Sequence < sal_Int8 >& rEncryptionKey, rtlRandomPool &rRandomPool)
304cdf0e10cSrcweir {
305cdf0e10cSrcweir     bool bSuccess = true;
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	const ::rtl::OUString sMediaTypeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) );
308cdf0e10cSrcweir 	const ::rtl::OUString sVersionProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Version" ) );
309cdf0e10cSrcweir 	const ::rtl::OUString sFullPathProperty ( RTL_CONSTASCII_USTRINGPARAM ( "FullPath" ) );
310cdf0e10cSrcweir 	const ::rtl::OUString sInitialisationVectorProperty ( RTL_CONSTASCII_USTRINGPARAM ( "InitialisationVector" ) );
311cdf0e10cSrcweir 	const ::rtl::OUString sSaltProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Salt" ) );
312cdf0e10cSrcweir 	const ::rtl::OUString sIterationCountProperty ( RTL_CONSTASCII_USTRINGPARAM ( "IterationCount" ) );
313cdf0e10cSrcweir 	const ::rtl::OUString sSizeProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Size" ) );
314cdf0e10cSrcweir 	const ::rtl::OUString sDigestProperty ( RTL_CONSTASCII_USTRINGPARAM ( "Digest" ) );
315cdf0e10cSrcweir     const ::rtl::OUString sEncryptionAlgProperty	( RTL_CONSTASCII_USTRINGPARAM ( "EncryptionAlgorithm" ) );
316cdf0e10cSrcweir     const ::rtl::OUString sStartKeyAlgProperty	( RTL_CONSTASCII_USTRINGPARAM ( "StartKeyAlgorithm" ) );
317cdf0e10cSrcweir     const ::rtl::OUString sDigestAlgProperty 	( RTL_CONSTASCII_USTRINGPARAM ( "DigestAlgorithm" ) );
318cdf0e10cSrcweir     const ::rtl::OUString  sDerivedKeySizeProperty	( RTL_CONSTASCII_USTRINGPARAM ( "DerivedKeySize" ) );
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     uno::Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST);
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     OSL_ENSURE( ( rInfo.bFolder && rInfo.pFolder ) || ( !rInfo.bFolder && rInfo.pStream ), "A valid child object is expected!" );
323cdf0e10cSrcweir     if ( rInfo.bFolder )
324cdf0e10cSrcweir     {
325cdf0e10cSrcweir         ::rtl::OUString sTempName = rPath + rShortName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         if ( rInfo.pFolder->GetMediaType().getLength() )
328cdf0e10cSrcweir         {
329cdf0e10cSrcweir             aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
330cdf0e10cSrcweir             aPropSet[PKG_MNFST_MEDIATYPE].Value <<= rInfo.pFolder->GetMediaType();
331cdf0e10cSrcweir             aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
332cdf0e10cSrcweir             aPropSet[PKG_MNFST_VERSION].Value <<= rInfo.pFolder->GetVersion();
333cdf0e10cSrcweir             aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
334cdf0e10cSrcweir             aPropSet[PKG_MNFST_FULLPATH].Value <<= sTempName;
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir         else
337cdf0e10cSrcweir             aPropSet.realloc( 0 );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         rInfo.pFolder->saveContents( sTempName, rManList, rZipOut, rEncryptionKey, rRandomPool);
340cdf0e10cSrcweir     }
341cdf0e10cSrcweir     else
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         // if pTempEntry is necessary, it will be released and passed to the ZipOutputStream
344cdf0e10cSrcweir         // and be deleted in the ZipOutputStream destructor
345cdf0e10cSrcweir         auto_ptr < ZipEntry > pAutoTempEntry ( new ZipEntry );
346cdf0e10cSrcweir         ZipEntry* pTempEntry = pAutoTempEntry.get();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir         // In case the entry we are reading is also the entry we are writing, we will
349cdf0e10cSrcweir         // store the ZipEntry data in pTempEntry
350cdf0e10cSrcweir 
351cdf0e10cSrcweir         ZipPackageFolder::copyZipEntry ( *pTempEntry, rInfo.pStream->aEntry );
352cdf0e10cSrcweir         pTempEntry->sPath = rPath + rShortName;
353cdf0e10cSrcweir         pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( pTempEntry->sPath, RTL_TEXTENCODING_UTF8 ).getLength() );
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         sal_Bool bToBeEncrypted = rInfo.pStream->IsToBeEncrypted() && (rEncryptionKey.getLength() || rInfo.pStream->HasOwnKey());
356cdf0e10cSrcweir         sal_Bool bToBeCompressed = bToBeEncrypted ? sal_True : rInfo.pStream->IsToBeCompressed();
357cdf0e10cSrcweir 
358cdf0e10cSrcweir         aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
359cdf0e10cSrcweir         aPropSet[PKG_MNFST_MEDIATYPE].Value <<= rInfo.pStream->GetMediaType( );
360cdf0e10cSrcweir         aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
361cdf0e10cSrcweir         aPropSet[PKG_MNFST_VERSION].Value <<= ::rtl::OUString(); // no version is stored for streams currently
362cdf0e10cSrcweir         aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
363cdf0e10cSrcweir         aPropSet[PKG_MNFST_FULLPATH].Value <<= pTempEntry->sPath;
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 
366cdf0e10cSrcweir         OSL_ENSURE( rInfo.pStream->GetStreamMode() != PACKAGE_STREAM_NOTSET, "Unacceptable ZipPackageStream mode!" );
367cdf0e10cSrcweir 
368cdf0e10cSrcweir         sal_Bool bRawStream = sal_False;
369cdf0e10cSrcweir         if ( rInfo.pStream->GetStreamMode() == PACKAGE_STREAM_DETECT )
370cdf0e10cSrcweir             bRawStream = rInfo.pStream->ParsePackageRawStream();
371cdf0e10cSrcweir         else if ( rInfo.pStream->GetStreamMode() == PACKAGE_STREAM_RAW )
372cdf0e10cSrcweir             bRawStream = sal_True;
373cdf0e10cSrcweir 
374cdf0e10cSrcweir         sal_Bool bTransportOwnEncrStreamAsRaw = sal_False;
375cdf0e10cSrcweir         // During the storing the original size of the stream can be changed
376cdf0e10cSrcweir         // TODO/LATER: get rid of this hack
377cdf0e10cSrcweir         sal_Int32 nOwnStreamOrigSize = bRawStream ? rInfo.pStream->GetMagicalHackSize() : rInfo.pStream->getSize();
378cdf0e10cSrcweir 
379cdf0e10cSrcweir         sal_Bool bUseNonSeekableAccess = sal_False;
380cdf0e10cSrcweir         uno::Reference < XInputStream > xStream;
381cdf0e10cSrcweir         if ( !rInfo.pStream->IsPackageMember() && !bRawStream && !bToBeEncrypted && bToBeCompressed )
382cdf0e10cSrcweir         {
383cdf0e10cSrcweir             // the stream is not a package member, not a raw stream,
384cdf0e10cSrcweir             // it should not be encrypted and it should be compressed,
385cdf0e10cSrcweir             // in this case nonseekable access can be used
386cdf0e10cSrcweir 
387cdf0e10cSrcweir             xStream = rInfo.pStream->GetOwnStreamNoWrap();
388cdf0e10cSrcweir             uno::Reference < XSeekable > xSeek ( xStream, uno::UNO_QUERY );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir             bUseNonSeekableAccess = ( xStream.is() && !xSeek.is() );
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir 
393cdf0e10cSrcweir         if ( !bUseNonSeekableAccess )
394cdf0e10cSrcweir         {
395cdf0e10cSrcweir             xStream = rInfo.pStream->getRawData();
396cdf0e10cSrcweir 
397cdf0e10cSrcweir             if ( !xStream.is() )
398cdf0e10cSrcweir             {
399cdf0e10cSrcweir                 VOS_ENSURE( 0, "ZipPackageStream didn't have a stream associated with it, skipping!" );
400cdf0e10cSrcweir                 bSuccess = false;
401cdf0e10cSrcweir                 return bSuccess;
402cdf0e10cSrcweir             }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir             uno::Reference < XSeekable > xSeek ( xStream, uno::UNO_QUERY );
405cdf0e10cSrcweir             try
406cdf0e10cSrcweir             {
407cdf0e10cSrcweir                 if ( xSeek.is() )
408cdf0e10cSrcweir                 {
409cdf0e10cSrcweir                     // If the stream is a raw one, then we should be positioned
410cdf0e10cSrcweir                     // at the beginning of the actual data
411cdf0e10cSrcweir                     if ( !bToBeCompressed || bRawStream )
412cdf0e10cSrcweir                     {
413cdf0e10cSrcweir                         // The raw stream can neither be encrypted nor connected
414cdf0e10cSrcweir                         OSL_ENSURE( !bRawStream || !bToBeCompressed && !bToBeEncrypted, "The stream is already encrypted!\n" );
415cdf0e10cSrcweir                         xSeek->seek ( bRawStream ? rInfo.pStream->GetMagicalHackPos() : 0 );
416cdf0e10cSrcweir                         ImplSetStoredData ( *pTempEntry, xStream );
417cdf0e10cSrcweir 
418cdf0e10cSrcweir                         // TODO/LATER: Get rid of hacks related to switching of Flag Method and Size properties!
419cdf0e10cSrcweir                     }
420cdf0e10cSrcweir                     else if ( bToBeEncrypted )
421cdf0e10cSrcweir                     {
422cdf0e10cSrcweir                         // this is the correct original size
423cdf0e10cSrcweir                         pTempEntry->nSize = static_cast < sal_Int32 > ( xSeek->getLength() );
424cdf0e10cSrcweir                         nOwnStreamOrigSize = pTempEntry->nSize;
425cdf0e10cSrcweir                     }
426cdf0e10cSrcweir 
427cdf0e10cSrcweir                     xSeek->seek ( 0 );
428cdf0e10cSrcweir                 }
429cdf0e10cSrcweir                 else
430cdf0e10cSrcweir                 {
431cdf0e10cSrcweir                     // Okay, we don't have an xSeekable stream. This is possibly bad.
432cdf0e10cSrcweir                     // check if it's one of our own streams, if it is then we know that
433cdf0e10cSrcweir                     // each time we ask for it we'll get a new stream that will be
434cdf0e10cSrcweir                     // at position zero...otherwise, assert and skip this stream...
435cdf0e10cSrcweir                     if ( rInfo.pStream->IsPackageMember() )
436cdf0e10cSrcweir                     {
437cdf0e10cSrcweir                         // if the password has been changed than the stream should not be package member any more
438cdf0e10cSrcweir                         if ( rInfo.pStream->IsEncrypted() && rInfo.pStream->IsToBeEncrypted() )
439cdf0e10cSrcweir                         {
440cdf0e10cSrcweir                             // Should be handled close to the raw stream handling
441cdf0e10cSrcweir                             bTransportOwnEncrStreamAsRaw = sal_True;
442cdf0e10cSrcweir                             pTempEntry->nMethod = STORED;
443cdf0e10cSrcweir 
444cdf0e10cSrcweir                             // TODO/LATER: get rid of this situation
445cdf0e10cSrcweir                             // this size should be different from the one that will be stored in manifest.xml
446cdf0e10cSrcweir                             // it is used in storing algorithms and after storing the correct size will be set
447cdf0e10cSrcweir                             pTempEntry->nSize = pTempEntry->nCompressedSize;
448cdf0e10cSrcweir                         }
449cdf0e10cSrcweir                     }
450cdf0e10cSrcweir                     else
451cdf0e10cSrcweir                     {
452cdf0e10cSrcweir                         bSuccess = false;
453cdf0e10cSrcweir                         return bSuccess;
454cdf0e10cSrcweir                     }
455cdf0e10cSrcweir                 }
456cdf0e10cSrcweir             }
457cdf0e10cSrcweir             catch ( uno::Exception& )
458cdf0e10cSrcweir             {
459cdf0e10cSrcweir                 bSuccess = false;
460cdf0e10cSrcweir                 return bSuccess;
461cdf0e10cSrcweir             }
462cdf0e10cSrcweir 
463cdf0e10cSrcweir             if ( bToBeEncrypted || bRawStream || bTransportOwnEncrStreamAsRaw )
464cdf0e10cSrcweir             {
465cdf0e10cSrcweir                 if ( bToBeEncrypted && !bTransportOwnEncrStreamAsRaw )
466cdf0e10cSrcweir                 {
467cdf0e10cSrcweir                     uno::Sequence < sal_Int8 > aSalt( 16 ), aVector( rInfo.pStream->GetBlockSize() );
468cdf0e10cSrcweir                     rtl_random_getBytes ( rRandomPool, aSalt.getArray(), 16 );
469cdf0e10cSrcweir                     rtl_random_getBytes ( rRandomPool, aVector.getArray(), aVector.getLength() );
470cdf0e10cSrcweir                     sal_Int32 nIterationCount = 1024;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir                     if ( !rInfo.pStream->HasOwnKey() )
473cdf0e10cSrcweir                         rInfo.pStream->setKey ( rEncryptionKey );
474cdf0e10cSrcweir 
475cdf0e10cSrcweir                     rInfo.pStream->setInitialisationVector ( aVector );
476cdf0e10cSrcweir                     rInfo.pStream->setSalt ( aSalt );
477cdf0e10cSrcweir                     rInfo.pStream->setIterationCount ( nIterationCount );
478cdf0e10cSrcweir                 }
479cdf0e10cSrcweir 
480cdf0e10cSrcweir                 // last property is digest, which is inserted later if we didn't have
481cdf0e10cSrcweir                 // a magic header
482cdf0e10cSrcweir                 aPropSet.realloc(PKG_SIZE_ENCR_MNFST);
483cdf0e10cSrcweir 
484cdf0e10cSrcweir                 aPropSet[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
485cdf0e10cSrcweir                 aPropSet[PKG_MNFST_INIVECTOR].Value <<= rInfo.pStream->getInitialisationVector();
486cdf0e10cSrcweir                 aPropSet[PKG_MNFST_SALT].Name = sSaltProperty;
487cdf0e10cSrcweir                 aPropSet[PKG_MNFST_SALT].Value <<= rInfo.pStream->getSalt();
488cdf0e10cSrcweir                 aPropSet[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
489cdf0e10cSrcweir                 aPropSet[PKG_MNFST_ITERATION].Value <<= rInfo.pStream->getIterationCount ();
490cdf0e10cSrcweir 
491cdf0e10cSrcweir                 // Need to store the uncompressed size in the manifest
492cdf0e10cSrcweir                 OSL_ENSURE( nOwnStreamOrigSize >= 0, "The stream size was not correctly initialized!\n" );
493cdf0e10cSrcweir                 aPropSet[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
494cdf0e10cSrcweir                 aPropSet[PKG_MNFST_UCOMPSIZE].Value <<= nOwnStreamOrigSize;
495cdf0e10cSrcweir 
496cdf0e10cSrcweir                 if ( bRawStream || bTransportOwnEncrStreamAsRaw )
497cdf0e10cSrcweir                 {
498cdf0e10cSrcweir                     ::rtl::Reference< EncryptionData > xEncData = rInfo.pStream->GetEncryptionData();
499cdf0e10cSrcweir                     if ( !xEncData.is() )
500cdf0e10cSrcweir                         throw uno::RuntimeException();
501cdf0e10cSrcweir 
502cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty;
503cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest();
504cdf0e10cSrcweir                     aPropSet[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
505cdf0e10cSrcweir                     aPropSet[PKG_MNFST_ENCALG].Value <<= xEncData->m_nEncAlg;
506cdf0e10cSrcweir                     aPropSet[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
507cdf0e10cSrcweir                     aPropSet[PKG_MNFST_STARTALG].Value <<= xEncData->m_nStartKeyGenID;
508cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
509cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DIGESTALG].Value <<= xEncData->m_nCheckAlg;
510cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty;
511cdf0e10cSrcweir                     aPropSet[PKG_MNFST_DERKEYSIZE].Value <<= xEncData->m_nDerivedKeySize;
512cdf0e10cSrcweir                 }
513cdf0e10cSrcweir             }
514cdf0e10cSrcweir         }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir         // If the entry is already stored in the zip file in the format we
517cdf0e10cSrcweir         // want for this write...copy it raw
518cdf0e10cSrcweir         if ( !bUseNonSeekableAccess
519cdf0e10cSrcweir           && ( bRawStream || bTransportOwnEncrStreamAsRaw
520cdf0e10cSrcweir             || ( rInfo.pStream->IsPackageMember() && !bToBeEncrypted
521cdf0e10cSrcweir               && ( ( rInfo.pStream->aEntry.nMethod == DEFLATED && bToBeCompressed )
522cdf0e10cSrcweir                 || ( rInfo.pStream->aEntry.nMethod == STORED && !bToBeCompressed ) ) ) ) )
523cdf0e10cSrcweir         {
524cdf0e10cSrcweir             // If it's a PackageMember, then it's an unbuffered stream and we need
525cdf0e10cSrcweir             // to get a new version of it as we can't seek backwards.
526cdf0e10cSrcweir             if ( rInfo.pStream->IsPackageMember() )
527cdf0e10cSrcweir             {
528cdf0e10cSrcweir                 xStream = rInfo.pStream->getRawData();
529cdf0e10cSrcweir                 if ( !xStream.is() )
530cdf0e10cSrcweir                 {
531cdf0e10cSrcweir                     // Make sure that we actually _got_ a new one !
532cdf0e10cSrcweir                     bSuccess = false;
533cdf0e10cSrcweir                     return bSuccess;
534cdf0e10cSrcweir                 }
535cdf0e10cSrcweir             }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir             try
538cdf0e10cSrcweir             {
539cdf0e10cSrcweir                 if ( bRawStream )
540cdf0e10cSrcweir                     xStream->skipBytes( rInfo.pStream->GetMagicalHackPos() );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir                 rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream, sal_False );
543cdf0e10cSrcweir                 // the entry is provided to the ZipOutputStream that will delete it
544cdf0e10cSrcweir                 pAutoTempEntry.release();
545cdf0e10cSrcweir 
546cdf0e10cSrcweir                 uno::Sequence < sal_Int8 > aSeq ( n_ConstBufferSize );
547cdf0e10cSrcweir                 sal_Int32 nLength;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir                 do
550cdf0e10cSrcweir                 {
551cdf0e10cSrcweir                     nLength = xStream->readBytes( aSeq, n_ConstBufferSize );
552cdf0e10cSrcweir                     rZipOut.rawWrite(aSeq, 0, nLength);
553cdf0e10cSrcweir                 }
554cdf0e10cSrcweir                 while ( nLength == n_ConstBufferSize );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir                 rZipOut.rawCloseEntry();
557cdf0e10cSrcweir             }
558cdf0e10cSrcweir             catch ( ZipException& )
559cdf0e10cSrcweir             {
560cdf0e10cSrcweir                 bSuccess = false;
561cdf0e10cSrcweir             }
562cdf0e10cSrcweir             catch ( IOException& )
563cdf0e10cSrcweir             {
564cdf0e10cSrcweir                 bSuccess = false;
565cdf0e10cSrcweir             }
566cdf0e10cSrcweir         }
567cdf0e10cSrcweir         else
568cdf0e10cSrcweir         {
569cdf0e10cSrcweir             // This stream is defenitly not a raw stream
570cdf0e10cSrcweir 
571cdf0e10cSrcweir             // If nonseekable access is used the stream should be at the beginning and
572cdf0e10cSrcweir             // is useless after the storing. Thus if the storing fails the package should
573cdf0e10cSrcweir             // be thrown away ( as actually it is done currently )!
574cdf0e10cSrcweir             // To allow to reuse the package after the error, the optimization must be removed!
575cdf0e10cSrcweir 
576cdf0e10cSrcweir             // If it's a PackageMember, then our previous reference held a 'raw' stream
577cdf0e10cSrcweir             // so we need to re-get it, unencrypted, uncompressed and positioned at the
578cdf0e10cSrcweir             // beginning of the stream
579cdf0e10cSrcweir             if ( rInfo.pStream->IsPackageMember() )
580cdf0e10cSrcweir             {
581cdf0e10cSrcweir                 xStream = rInfo.pStream->getInputStream();
582cdf0e10cSrcweir                 if ( !xStream.is() )
583cdf0e10cSrcweir                 {
584cdf0e10cSrcweir                     // Make sure that we actually _got_ a new one !
585cdf0e10cSrcweir                     bSuccess = false;
586cdf0e10cSrcweir                     return bSuccess;
587cdf0e10cSrcweir                 }
588cdf0e10cSrcweir             }
589cdf0e10cSrcweir 
590cdf0e10cSrcweir             if ( bToBeCompressed )
591cdf0e10cSrcweir             {
592cdf0e10cSrcweir                 pTempEntry->nMethod = DEFLATED;
593cdf0e10cSrcweir                 pTempEntry->nCrc = pTempEntry->nCompressedSize = pTempEntry->nSize = -1;
594cdf0e10cSrcweir             }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir             try
597cdf0e10cSrcweir             {
598cdf0e10cSrcweir                 rZipOut.putNextEntry ( *pTempEntry, rInfo.pStream, bToBeEncrypted);
599cdf0e10cSrcweir                 // the entry is provided to the ZipOutputStream that will delete it
600cdf0e10cSrcweir                 pAutoTempEntry.release();
601cdf0e10cSrcweir 
602cdf0e10cSrcweir                 sal_Int32 nLength;
603cdf0e10cSrcweir                 uno::Sequence < sal_Int8 > aSeq (n_ConstBufferSize);
604cdf0e10cSrcweir                 do
605cdf0e10cSrcweir                 {
606cdf0e10cSrcweir                     nLength = xStream->readBytes(aSeq, n_ConstBufferSize);
607cdf0e10cSrcweir                     rZipOut.write(aSeq, 0, nLength);
608cdf0e10cSrcweir                 }
609cdf0e10cSrcweir                 while ( nLength == n_ConstBufferSize );
610cdf0e10cSrcweir 
611cdf0e10cSrcweir                 rZipOut.closeEntry();
612cdf0e10cSrcweir             }
613cdf0e10cSrcweir             catch ( ZipException& )
614cdf0e10cSrcweir             {
615cdf0e10cSrcweir                 bSuccess = false;
616cdf0e10cSrcweir             }
617cdf0e10cSrcweir             catch ( IOException& )
618cdf0e10cSrcweir             {
619cdf0e10cSrcweir                 bSuccess = false;
620cdf0e10cSrcweir             }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir             if ( bToBeEncrypted )
623cdf0e10cSrcweir             {
624cdf0e10cSrcweir                 ::rtl::Reference< EncryptionData > xEncData = rInfo.pStream->GetEncryptionData();
625cdf0e10cSrcweir                 if ( !xEncData.is() )
626cdf0e10cSrcweir                     throw uno::RuntimeException();
627cdf0e10cSrcweir 
628cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DIGEST].Name = sDigestProperty;
629cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DIGEST].Value <<= rInfo.pStream->getDigest();
630cdf0e10cSrcweir                 aPropSet[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
631cdf0e10cSrcweir                 aPropSet[PKG_MNFST_ENCALG].Value <<= xEncData->m_nEncAlg;
632cdf0e10cSrcweir                 aPropSet[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
633cdf0e10cSrcweir                 aPropSet[PKG_MNFST_STARTALG].Value <<= xEncData->m_nStartKeyGenID;
634cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
635cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DIGESTALG].Value <<= xEncData->m_nCheckAlg;
636cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty;
637cdf0e10cSrcweir                 aPropSet[PKG_MNFST_DERKEYSIZE].Value <<= xEncData->m_nDerivedKeySize;
638cdf0e10cSrcweir 
639cdf0e10cSrcweir                 rInfo.pStream->SetIsEncrypted ( sal_True );
640cdf0e10cSrcweir             }
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir 
643cdf0e10cSrcweir         if( bSuccess )
644cdf0e10cSrcweir         {
645cdf0e10cSrcweir             if ( !rInfo.pStream->IsPackageMember() )
646cdf0e10cSrcweir             {
647cdf0e10cSrcweir                 rInfo.pStream->CloseOwnStreamIfAny();
648cdf0e10cSrcweir                 rInfo.pStream->SetPackageMember ( sal_True );
649cdf0e10cSrcweir             }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir             if ( bRawStream )
652cdf0e10cSrcweir             {
653cdf0e10cSrcweir                 // the raw stream was integrated and now behaves
654cdf0e10cSrcweir                 // as usual encrypted stream
655cdf0e10cSrcweir                 rInfo.pStream->SetToBeEncrypted( sal_True );
656cdf0e10cSrcweir             }
657cdf0e10cSrcweir 
658cdf0e10cSrcweir             // Remove hacky bit from entry flags
659cdf0e10cSrcweir             if ( pTempEntry->nFlag & ( 1 << 4 ) )
660cdf0e10cSrcweir             {
661cdf0e10cSrcweir                 pTempEntry->nFlag &= ~( 1 << 4 );
662cdf0e10cSrcweir                 pTempEntry->nMethod = STORED;
663cdf0e10cSrcweir             }
664cdf0e10cSrcweir 
665cdf0e10cSrcweir             // Then copy it back afterwards...
666cdf0e10cSrcweir             ZipPackageFolder::copyZipEntry ( rInfo.pStream->aEntry, *pTempEntry );
667cdf0e10cSrcweir 
668cdf0e10cSrcweir             // TODO/LATER: get rid of this hack ( the encrypted stream size property is changed during saving )
669cdf0e10cSrcweir             if ( rInfo.pStream->IsEncrypted() )
670cdf0e10cSrcweir                 rInfo.pStream->setSize( nOwnStreamOrigSize );
671cdf0e10cSrcweir 
672cdf0e10cSrcweir             rInfo.pStream->aEntry.nOffset *= -1;
673cdf0e10cSrcweir         }
674cdf0e10cSrcweir     }
675cdf0e10cSrcweir 
676cdf0e10cSrcweir     // folder can have a mediatype only in package format
677cdf0e10cSrcweir     if ( aPropSet.getLength()
678cdf0e10cSrcweir       && ( m_nFormat == embed::StorageFormats::PACKAGE || ( m_nFormat == embed::StorageFormats::OFOPXML && !rInfo.bFolder ) ) )
679cdf0e10cSrcweir         rManList.push_back( aPropSet );
680cdf0e10cSrcweir 
681cdf0e10cSrcweir     return bSuccess;
682cdf0e10cSrcweir }
683cdf0e10cSrcweir 
saveContents(::rtl::OUString & rPath,std::vector<uno::Sequence<PropertyValue>> & rManList,ZipOutputStream & rZipOut,const uno::Sequence<sal_Int8> & rEncryptionKey,rtlRandomPool & rRandomPool)684cdf0e10cSrcweir void ZipPackageFolder::saveContents( ::rtl::OUString &rPath, std::vector < uno::Sequence < PropertyValue > > &rManList, ZipOutputStream & rZipOut, const uno::Sequence < sal_Int8 >& rEncryptionKey, rtlRandomPool &rRandomPool )
685cdf0e10cSrcweir 	throw( uno::RuntimeException )
686cdf0e10cSrcweir {
687cdf0e10cSrcweir 	bool bWritingFailed = false;
688cdf0e10cSrcweir 
689cdf0e10cSrcweir 	if ( maContents.begin() == maContents.end() && rPath.getLength() && m_nFormat != embed::StorageFormats::OFOPXML )
690cdf0e10cSrcweir 	{
691cdf0e10cSrcweir 		// it is an empty subfolder, use workaround to store it
692cdf0e10cSrcweir 		ZipEntry* pTempEntry = new ZipEntry();
693cdf0e10cSrcweir 		ZipPackageFolder::copyZipEntry ( *pTempEntry, aEntry );
694cdf0e10cSrcweir 		pTempEntry->nPathLen = (sal_Int16)( ::rtl::OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
695cdf0e10cSrcweir 		pTempEntry->nExtraLen = -1;
696cdf0e10cSrcweir 		pTempEntry->sPath = rPath;
697cdf0e10cSrcweir 
698cdf0e10cSrcweir 		try
699cdf0e10cSrcweir 		{
700cdf0e10cSrcweir 			rZipOut.putNextEntry( *pTempEntry, NULL, sal_False );
701cdf0e10cSrcweir 			rZipOut.rawCloseEntry();
702cdf0e10cSrcweir 		}
703cdf0e10cSrcweir 		catch ( ZipException& )
704cdf0e10cSrcweir 		{
705cdf0e10cSrcweir 			bWritingFailed = true;
706cdf0e10cSrcweir 		}
707cdf0e10cSrcweir 		catch ( IOException& )
708cdf0e10cSrcweir 		{
709cdf0e10cSrcweir 			bWritingFailed = true;
710cdf0e10cSrcweir 		}
711cdf0e10cSrcweir 	}
712cdf0e10cSrcweir 
713cdf0e10cSrcweir     bool bMimeTypeStreamStored = false;
714cdf0e10cSrcweir     ::rtl::OUString aMimeTypeStreamName( RTL_CONSTASCII_USTRINGPARAM( "mimetype" ) );
715cdf0e10cSrcweir     if ( m_nFormat == embed::StorageFormats::ZIP && !rPath.getLength() )
716cdf0e10cSrcweir     {
717cdf0e10cSrcweir         // let the "mimtype" stream in root folder be stored as the first stream if it is zip format
718cdf0e10cSrcweir         ContentHash::iterator aIter = maContents.find ( aMimeTypeStreamName );
719cdf0e10cSrcweir         if ( aIter != maContents.end() && !(*aIter).second->bFolder )
720cdf0e10cSrcweir         {
721cdf0e10cSrcweir             bMimeTypeStreamStored = true;
722cdf0e10cSrcweir             bWritingFailed = !saveChild( (*aIter).first, *(*aIter).second, rPath, rManList, rZipOut, rEncryptionKey, rRandomPool );
723cdf0e10cSrcweir         }
724cdf0e10cSrcweir     }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir 	for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
727cdf0e10cSrcweir 	      aCI != aEnd;
728cdf0e10cSrcweir 		  aCI++)
729cdf0e10cSrcweir 	{
730cdf0e10cSrcweir 		const ::rtl::OUString &rShortName = (*aCI).first;
731cdf0e10cSrcweir 		const ContentInfo &rInfo = *(*aCI).second;
732cdf0e10cSrcweir 
733cdf0e10cSrcweir         if ( !bMimeTypeStreamStored || !rShortName.equals( aMimeTypeStreamName ) )
734cdf0e10cSrcweir             bWritingFailed = !saveChild( rShortName, rInfo, rPath, rManList, rZipOut, rEncryptionKey, rRandomPool );
735cdf0e10cSrcweir 	}
736cdf0e10cSrcweir 
737cdf0e10cSrcweir 	if( bWritingFailed )
738cdf0e10cSrcweir 		throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
739cdf0e10cSrcweir }
740cdf0e10cSrcweir 
releaseUpwardRef(void)741cdf0e10cSrcweir void ZipPackageFolder::releaseUpwardRef( void )
742cdf0e10cSrcweir {
743cdf0e10cSrcweir 	// Now it is possible that a package folder is disconnected from the package before removing of the folder.
744cdf0e10cSrcweir 	// Such a scenario is used in storage implementation. When a new version of a folder is provided the old
745cdf0e10cSrcweir 	// one is retrieved, removed from the package but preserved for the error handling.
746cdf0e10cSrcweir 	// In this scenario the referencing to the parent is not really useful, since it requires disposing.
747cdf0e10cSrcweir 
748cdf0e10cSrcweir 	// Actually there is no need in having a reference to the parent, it even make things more complicated and
749cdf0e10cSrcweir 	// requires disposing mechanics. Using of a simple pointer seems to be easier solution and also a safe enough.
750cdf0e10cSrcweir 
751cdf0e10cSrcweir 	clearParent();
752cdf0e10cSrcweir 
753cdf0e10cSrcweir #if 0
754cdf0e10cSrcweir 	for ( ContentHash::const_iterator aCI = maContents.begin();
755cdf0e10cSrcweir 		  aCI!=maContents.end();
756cdf0e10cSrcweir 		  aCI++)
757cdf0e10cSrcweir 	{
758cdf0e10cSrcweir 		ContentInfo &rInfo = * (*aCI).second;
759cdf0e10cSrcweir 		if ( rInfo.bFolder )// && ! rInfo.pFolder->HasReleased () )
760cdf0e10cSrcweir 			rInfo.pFolder->releaseUpwardRef();
761cdf0e10cSrcweir 		else //if ( !rInfo.bFolder && !rInfo.pStream->HasReleased() )
762cdf0e10cSrcweir 			rInfo.pStream->clearParent();
763cdf0e10cSrcweir 	}
764cdf0e10cSrcweir 	clearParent();
765cdf0e10cSrcweir 
766cdf0e10cSrcweir 	VOS_ENSURE ( m_refCount == 1, "Ref-count is not 1!" );
767cdf0e10cSrcweir #endif
768cdf0e10cSrcweir }
769cdf0e10cSrcweir 
getSomething(const uno::Sequence<sal_Int8> & aIdentifier)770cdf0e10cSrcweir sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const uno::Sequence< sal_Int8 >& aIdentifier )
771cdf0e10cSrcweir 	throw(uno::RuntimeException)
772cdf0e10cSrcweir {
773cdf0e10cSrcweir 	sal_Int64 nMe = 0;
774cdf0e10cSrcweir 	if ( aIdentifier.getLength() == 16 &&
775cdf0e10cSrcweir 		 0 == rtl_compareMemory(static_getImplementationId().getConstArray(),  aIdentifier.getConstArray(), 16 ) )
776cdf0e10cSrcweir 		nMe = reinterpret_cast < sal_Int64 > ( this );
777cdf0e10cSrcweir 	return nMe;
778cdf0e10cSrcweir }
setPropertyValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)779cdf0e10cSrcweir void SAL_CALL ZipPackageFolder::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue )
780cdf0e10cSrcweir 		throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, uno::RuntimeException)
781cdf0e10cSrcweir {
782cdf0e10cSrcweir 	if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("MediaType")))
783cdf0e10cSrcweir 	{
784cdf0e10cSrcweir 		// TODO/LATER: activate when zip ucp is ready
785cdf0e10cSrcweir 		// if ( m_nFormat != embed::StorageFormats::PACKAGE )
786cdf0e10cSrcweir 		// 	throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
787cdf0e10cSrcweir 
788cdf0e10cSrcweir 		aValue >>= sMediaType;
789cdf0e10cSrcweir 	}
790cdf0e10cSrcweir 	else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Version")))
791cdf0e10cSrcweir         aValue >>= m_sVersion;
792cdf0e10cSrcweir 	else if (aPropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Size") ) )
793cdf0e10cSrcweir 		aValue >>= aEntry.nSize;
794cdf0e10cSrcweir 	else
795cdf0e10cSrcweir 		throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
796cdf0e10cSrcweir }
getPropertyValue(const::rtl::OUString & PropertyName)797cdf0e10cSrcweir uno::Any SAL_CALL ZipPackageFolder::getPropertyValue( const ::rtl::OUString& PropertyName )
798cdf0e10cSrcweir 		throw(UnknownPropertyException, WrappedTargetException, uno::RuntimeException)
799cdf0e10cSrcweir {
800cdf0e10cSrcweir 	if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "MediaType" ) ) )
801cdf0e10cSrcweir 	{
802cdf0e10cSrcweir 		// TODO/LATER: activate when zip ucp is ready
803cdf0e10cSrcweir 		// if ( m_nFormat != embed::StorageFormats::PACKAGE )
804cdf0e10cSrcweir 		//	throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
805cdf0e10cSrcweir 
806cdf0e10cSrcweir 		return uno::makeAny ( sMediaType );
807cdf0e10cSrcweir 	}
808cdf0e10cSrcweir 	else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Version" ) ) )
809cdf0e10cSrcweir         return uno::makeAny( m_sVersion );
810cdf0e10cSrcweir 	else if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( "Size" ) ) )
811cdf0e10cSrcweir 		return uno::makeAny ( aEntry.nSize );
812cdf0e10cSrcweir 	else
813cdf0e10cSrcweir 		throw UnknownPropertyException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
814cdf0e10cSrcweir }
815cdf0e10cSrcweir 
doInsertByName(ZipPackageEntry * pEntry,sal_Bool bSetParent)816cdf0e10cSrcweir void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, sal_Bool bSetParent )
817cdf0e10cSrcweir 		throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException)
818cdf0e10cSrcweir {
819cdf0e10cSrcweir 	try
820cdf0e10cSrcweir     {
821cdf0e10cSrcweir         if ( pEntry->IsFolder() )
822cdf0e10cSrcweir 		    maContents[pEntry->getName()] = new ContentInfo ( static_cast < ZipPackageFolder *> ( pEntry ) );
823cdf0e10cSrcweir 	    else
824cdf0e10cSrcweir 		    maContents[pEntry->getName()] = new ContentInfo ( static_cast < ZipPackageStream *> ( pEntry ) );
825cdf0e10cSrcweir     }
826cdf0e10cSrcweir     catch(const uno::Exception& rEx)
827cdf0e10cSrcweir     {
828cdf0e10cSrcweir         (void)rEx;
829cdf0e10cSrcweir         throw;
830cdf0e10cSrcweir     }
831cdf0e10cSrcweir 	if ( bSetParent )
832cdf0e10cSrcweir 		pEntry->setParent ( *this );
833cdf0e10cSrcweir }
getImplementationName()834cdf0e10cSrcweir ::rtl::OUString ZipPackageFolder::getImplementationName()
835cdf0e10cSrcweir 	throw (uno::RuntimeException)
836cdf0e10cSrcweir {
837cdf0e10cSrcweir 	return ::rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM ( "ZipPackageFolder" ) );
838cdf0e10cSrcweir }
839cdf0e10cSrcweir 
getSupportedServiceNames()840cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > ZipPackageFolder::getSupportedServiceNames()
841cdf0e10cSrcweir 	throw (uno::RuntimeException)
842cdf0e10cSrcweir {
843cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aNames(1);
844cdf0e10cSrcweir 	aNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.packages.PackageFolder" ) );
845cdf0e10cSrcweir 	return aNames;
846cdf0e10cSrcweir }
supportsService(::rtl::OUString const & rServiceName)847cdf0e10cSrcweir sal_Bool SAL_CALL ZipPackageFolder::supportsService( ::rtl::OUString const & rServiceName )
848cdf0e10cSrcweir 	throw (uno::RuntimeException)
849cdf0e10cSrcweir {
850cdf0e10cSrcweir 	return rServiceName == getSupportedServiceNames()[0];
851cdf0e10cSrcweir }
852