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