Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <string.h>
21 :
22 : #include <ZipPackageFolder.hxx>
23 : #include <ZipFile.hxx>
24 : #include <ZipOutputEntry.hxx>
25 : #include <ZipOutputStream.hxx>
26 : #include <ZipPackageStream.hxx>
27 : #include <PackageConstants.hxx>
28 : #include <ZipPackageFolderEnumeration.hxx>
29 : #include <com/sun/star/packages/zip/ZipConstants.hpp>
30 : #include <com/sun/star/embed/StorageFormats.hpp>
31 : #include <cppuhelper/supportsservice.hxx>
32 : #include <cppuhelper/typeprovider.hxx>
33 : #include <osl/diagnose.h>
34 : #include <osl/time.h>
35 : #include <rtl/digest.h>
36 : #include <ContentInfo.hxx>
37 : #include <com/sun/star/beans/PropertyValue.hpp>
38 : #include <EncryptedDataHeader.hxx>
39 : #include <rtl/instance.hxx>
40 : #include <boost/scoped_ptr.hpp>
41 :
42 : using namespace com::sun::star;
43 : using namespace com::sun::star::packages::zip::ZipConstants;
44 : using namespace com::sun::star::packages::zip;
45 : using namespace com::sun::star::packages;
46 : using namespace com::sun::star::container;
47 : using namespace com::sun::star::beans;
48 : using namespace com::sun::star::lang;
49 : using namespace com::sun::star::io;
50 : using namespace cppu;
51 :
52 : #if OSL_DEBUG_LEVEL > 0
53 : #define THROW_WHERE SAL_WHERE
54 : #else
55 : #define THROW_WHERE ""
56 : #endif
57 :
58 : namespace { struct lcl_CachedImplId : public rtl::Static< cppu::OImplementationId, lcl_CachedImplId > {}; }
59 :
60 245662 : ZipPackageFolder::ZipPackageFolder( const css::uno::Reference < css::uno::XComponentContext >& xContext,
61 : sal_Int32 nFormat,
62 245662 : bool bAllowRemoveOnInsert )
63 : {
64 245662 : m_xContext = xContext;
65 245662 : m_nFormat = nFormat;
66 245662 : mbAllowRemoveOnInsert = bAllowRemoveOnInsert;
67 245662 : SetFolder ( true );
68 245662 : aEntry.nVersion = -1;
69 245662 : aEntry.nFlag = 0;
70 245662 : aEntry.nMethod = STORED;
71 245662 : aEntry.nTime = -1;
72 245662 : aEntry.nCrc = 0;
73 245662 : aEntry.nCompressedSize = 0;
74 245662 : aEntry.nSize = 0;
75 245662 : aEntry.nOffset = -1;
76 245662 : }
77 :
78 491202 : ZipPackageFolder::~ZipPackageFolder()
79 : {
80 491202 : }
81 :
82 15816 : bool ZipPackageFolder::LookForUnexpectedODF12Streams( const OUString& aPath )
83 : {
84 15816 : bool bHasUnexpected = false;
85 :
86 79704 : for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
87 39852 : !bHasUnexpected && aCI != aEnd;
88 : ++aCI)
89 : {
90 24036 : const OUString &rShortName = (*aCI).first;
91 24036 : const ContentInfo &rInfo = *(*aCI).second;
92 :
93 24036 : if ( rInfo.bFolder )
94 : {
95 14430 : if ( aPath == "META-INF/" )
96 : {
97 : // META-INF is not allowed to contain subfolders
98 0 : bHasUnexpected = true;
99 : }
100 : else
101 : {
102 14430 : OUString sOwnPath = aPath + rShortName + "/";
103 14430 : bHasUnexpected = rInfo.pFolder->LookForUnexpectedODF12Streams( sOwnPath );
104 : }
105 : }
106 : else
107 : {
108 9606 : if ( aPath == "META-INF/" )
109 : {
110 0 : if ( rShortName != "manifest.xml"
111 0 : && rShortName.indexOf( "signatures" ) == -1 )
112 : {
113 : // a stream from META-INF with unexpected name
114 0 : bHasUnexpected = true;
115 : }
116 :
117 : // streams from META-INF with expected names are allowed not to be registered in manifest.xml
118 : }
119 9606 : else if ( !rInfo.pStream->IsFromManifest() )
120 : {
121 : // the stream is not in META-INF and is not registered in manifest.xml,
122 : // check whether it is an internal part of the package format
123 0 : if ( !aPath.isEmpty() || rShortName != "mimetype" )
124 : {
125 : // if it is not "mimetype" from the root it is not a part of the package
126 0 : bHasUnexpected = true;
127 : }
128 : }
129 : }
130 : }
131 :
132 15816 : return bHasUnexpected;
133 : }
134 :
135 95882 : void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair& aPair )
136 : {
137 95882 : OUString aExt;
138 95882 : if ( aPair.First.toChar() == (sal_Unicode)'.' )
139 0 : aExt = aPair.First;
140 : else
141 95882 : aExt = "." + aPair.First;
142 :
143 484554 : for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
144 : aCI != aEnd;
145 : ++aCI)
146 : {
147 388672 : const OUString &rShortName = (*aCI).first;
148 388672 : const ContentInfo &rInfo = *(*aCI).second;
149 :
150 388672 : if ( rInfo.bFolder )
151 82822 : rInfo.pFolder->setChildStreamsTypeByExtension( aPair );
152 : else
153 : {
154 305850 : sal_Int32 nPathLength = rShortName.getLength();
155 305850 : sal_Int32 nExtLength = aExt.getLength();
156 305850 : if ( nPathLength >= nExtLength && rShortName.match( aExt, nPathLength - nExtLength ) )
157 112166 : rInfo.pStream->SetMediaType( aPair.Second );
158 : }
159 95882 : }
160 95882 : }
161 :
162 33908 : void ZipPackageFolder::copyZipEntry( ZipEntry &rDest, const ZipEntry &rSource)
163 : {
164 33908 : rDest.nVersion = rSource.nVersion;
165 33908 : rDest.nFlag = rSource.nFlag;
166 33908 : rDest.nMethod = rSource.nMethod;
167 33908 : rDest.nTime = rSource.nTime;
168 33908 : rDest.nCrc = rSource.nCrc;
169 33908 : rDest.nCompressedSize = rSource.nCompressedSize;
170 33908 : rDest.nSize = rSource.nSize;
171 33908 : rDest.nOffset = rSource.nOffset;
172 33908 : rDest.sPath = rSource.sPath;
173 33908 : rDest.nPathLen = rSource.nPathLen;
174 33908 : rDest.nExtraLen = rSource.nExtraLen;
175 33908 : }
176 :
177 107774 : ::com::sun::star::uno::Sequence < sal_Int8 > ZipPackageFolder::static_getImplementationId()
178 : {
179 107774 : return lcl_CachedImplId::get().getImplementationId();
180 : }
181 :
182 : // XNameContainer
183 28010 : void SAL_CALL ZipPackageFolder::insertByName( const OUString& aName, const uno::Any& aElement )
184 : throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException, std::exception)
185 : {
186 28010 : if (hasByName(aName))
187 0 : throw ElementExistException(THROW_WHERE );
188 : else
189 : {
190 28010 : uno::Reference < XUnoTunnel > xRef;
191 28010 : aElement >>= xRef;
192 28010 : if ( ( aElement >>= xRef ) )
193 : {
194 : sal_Int64 nTest;
195 : ZipPackageEntry *pEntry;
196 28010 : if ( ( nTest = xRef->getSomething ( ZipPackageFolder::static_getImplementationId() ) ) != 0 )
197 : {
198 7010 : ZipPackageFolder *pFolder = reinterpret_cast < ZipPackageFolder * > ( nTest );
199 7010 : pEntry = static_cast < ZipPackageEntry * > ( pFolder );
200 : }
201 21000 : else if ( ( nTest = xRef->getSomething ( ZipPackageStream::static_getImplementationId() ) ) != 0 )
202 : {
203 21000 : ZipPackageStream *pStream = reinterpret_cast < ZipPackageStream * > ( nTest );
204 21000 : pEntry = static_cast < ZipPackageEntry * > ( pStream );
205 : }
206 : else
207 0 : throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
208 :
209 28010 : if (pEntry->getName() != aName )
210 28006 : pEntry->setName (aName);
211 28010 : doInsertByName ( pEntry, true );
212 : }
213 : else
214 0 : throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
215 : }
216 28010 : }
217 12538 : void SAL_CALL ZipPackageFolder::removeByName( const OUString& Name )
218 : throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException, std::exception)
219 : {
220 12538 : ContentHash::iterator aIter = maContents.find ( Name );
221 12538 : if ( aIter == maContents.end() )
222 0 : throw NoSuchElementException(THROW_WHERE );
223 12538 : maContents.erase( aIter );
224 12538 : }
225 : // XEnumerationAccess
226 108392 : uno::Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( )
227 : throw(uno::RuntimeException, std::exception)
228 : {
229 108392 : return uno::Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents));
230 : }
231 : // XElementAccess
232 0 : uno::Type SAL_CALL ZipPackageFolder::getElementType( )
233 : throw(uno::RuntimeException, std::exception)
234 : {
235 0 : return cppu::UnoType<XUnoTunnel>::get();
236 : }
237 0 : sal_Bool SAL_CALL ZipPackageFolder::hasElements( )
238 : throw(uno::RuntimeException, std::exception)
239 : {
240 0 : return maContents.size() > 0;
241 : }
242 : // XNameAccess
243 316870 : ContentInfo& ZipPackageFolder::doGetByName( const OUString& aName )
244 : throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException)
245 : {
246 316870 : ContentHash::iterator aIter = maContents.find ( aName );
247 316870 : if ( aIter == maContents.end())
248 0 : throw NoSuchElementException(THROW_WHERE );
249 316870 : return *(*aIter).second;
250 : }
251 247554 : uno::Any SAL_CALL ZipPackageFolder::getByName( const OUString& aName )
252 : throw(NoSuchElementException, WrappedTargetException, uno::RuntimeException, std::exception)
253 : {
254 247554 : return uno::makeAny ( doGetByName ( aName ).xTunnel );
255 : }
256 0 : uno::Sequence< OUString > SAL_CALL ZipPackageFolder::getElementNames( )
257 : throw(uno::RuntimeException, std::exception)
258 : {
259 0 : sal_uInt32 i=0, nSize = maContents.size();
260 0 : uno::Sequence < OUString > aSequence ( nSize );
261 0 : for ( ContentHash::const_iterator aIterator = maContents.begin(), aEnd = maContents.end();
262 : aIterator != aEnd;
263 : ++i, ++aIterator)
264 0 : aSequence[i] = (*aIterator).first;
265 0 : return aSequence;
266 : }
267 1044398 : sal_Bool SAL_CALL ZipPackageFolder::hasByName( const OUString& aName )
268 : throw(uno::RuntimeException, std::exception)
269 : {
270 1044398 : return maContents.find ( aName ) != maContents.end ();
271 : }
272 : // XNameReplace
273 0 : void SAL_CALL ZipPackageFolder::replaceByName( const OUString& aName, const uno::Any& aElement )
274 : throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, uno::RuntimeException, std::exception)
275 : {
276 0 : if ( hasByName( aName ) )
277 0 : removeByName( aName );
278 : else
279 0 : throw NoSuchElementException(THROW_WHERE );
280 0 : insertByName(aName, aElement);
281 0 : }
282 :
283 7126 : bool ZipPackageFolder::saveChild(
284 : const OUString &rPath,
285 : std::vector < uno::Sequence < PropertyValue > > &rManList,
286 : ZipOutputStream & rZipOut,
287 : const uno::Sequence < sal_Int8 >& rEncryptionKey,
288 : const rtlRandomPool &rRandomPool)
289 : {
290 7126 : bool bSuccess = true;
291 :
292 7126 : const OUString sMediaTypeProperty ("MediaType");
293 14252 : const OUString sVersionProperty ("Version");
294 14252 : const OUString sFullPathProperty ("FullPath");
295 :
296 14252 : uno::Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST);
297 14252 : OUString sTempName = rPath + "/";
298 :
299 7126 : if ( !GetMediaType().isEmpty() )
300 : {
301 146 : aPropSet[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
302 146 : aPropSet[PKG_MNFST_MEDIATYPE].Value <<= GetMediaType();
303 146 : aPropSet[PKG_MNFST_VERSION].Name = sVersionProperty;
304 146 : aPropSet[PKG_MNFST_VERSION].Value <<= GetVersion();
305 146 : aPropSet[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
306 146 : aPropSet[PKG_MNFST_FULLPATH].Value <<= sTempName;
307 : }
308 : else
309 6980 : aPropSet.realloc( 0 );
310 :
311 7126 : saveContents( sTempName, rManList, rZipOut, rEncryptionKey, rRandomPool);
312 :
313 : // folder can have a mediatype only in package format
314 7126 : if ( aPropSet.getLength() && ( m_nFormat == embed::StorageFormats::PACKAGE ) )
315 146 : rManList.push_back( aPropSet );
316 :
317 14252 : return bSuccess;
318 : }
319 :
320 8460 : void ZipPackageFolder::saveContents(
321 : const OUString &rPath,
322 : std::vector < uno::Sequence < PropertyValue > > &rManList,
323 : ZipOutputStream & rZipOut,
324 : const uno::Sequence < sal_Int8 >& rEncryptionKey,
325 : const rtlRandomPool &rRandomPool ) const
326 : throw( uno::RuntimeException )
327 : {
328 8460 : bool bWritingFailed = false;
329 :
330 8460 : if ( maContents.begin() == maContents.end() && !rPath.isEmpty() && m_nFormat != embed::StorageFormats::OFOPXML )
331 : {
332 : // it is an empty subfolder, use workaround to store it
333 920 : ZipEntry* pTempEntry = new ZipEntry();
334 920 : ZipPackageFolder::copyZipEntry ( *pTempEntry, aEntry );
335 920 : pTempEntry->nPathLen = (sal_Int16)( OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
336 920 : pTempEntry->nExtraLen = -1;
337 920 : pTempEntry->sPath = rPath;
338 :
339 : try
340 : {
341 920 : ZipOutputEntry aZipEntry(m_xContext, rZipOut.getChucker(), *pTempEntry, NULL, false);
342 920 : aZipEntry.rawCloseEntry();
343 920 : rZipOut.addEntry(pTempEntry);
344 : }
345 0 : catch ( ZipException& )
346 : {
347 0 : bWritingFailed = true;
348 : }
349 0 : catch ( IOException& )
350 : {
351 0 : bWritingFailed = true;
352 : }
353 : }
354 :
355 8460 : bool bMimeTypeStreamStored = false;
356 8460 : OUString aMimeTypeStreamName("mimetype");
357 8460 : if ( m_nFormat == embed::StorageFormats::ZIP && rPath.isEmpty() )
358 : {
359 : // let the "mimetype" stream in root folder be stored as the first stream if it is zip format
360 0 : ContentHash::const_iterator aIter = maContents.find ( aMimeTypeStreamName );
361 0 : if ( aIter != maContents.end() && !(*aIter).second->bFolder )
362 : {
363 0 : bMimeTypeStreamStored = true;
364 0 : bWritingFailed = !aIter->second->pStream->saveChild(
365 0 : rPath + aIter->first, rManList, rZipOut, rEncryptionKey, rRandomPool );
366 : }
367 : }
368 :
369 32080 : for ( ContentHash::const_iterator aCI = maContents.begin(), aEnd = maContents.end();
370 : aCI != aEnd;
371 : ++aCI)
372 : {
373 23620 : const OUString &rShortName = (*aCI).first;
374 23620 : const ContentInfo &rInfo = *(*aCI).second;
375 :
376 23620 : if ( !bMimeTypeStreamStored || !rShortName.equals( aMimeTypeStreamName ) )
377 : {
378 23620 : if (rInfo.bFolder)
379 : {
380 : bWritingFailed = !rInfo.pFolder->saveChild(
381 7126 : rPath + rShortName, rManList, rZipOut, rEncryptionKey, rRandomPool );
382 : }
383 : else
384 : {
385 : bWritingFailed = !rInfo.pStream->saveChild(
386 16494 : rPath + rShortName, rManList, rZipOut, rEncryptionKey, rRandomPool );
387 : }
388 : }
389 : }
390 :
391 8460 : if( bWritingFailed )
392 0 : throw uno::RuntimeException(THROW_WHERE );
393 8460 : }
394 :
395 136404 : void ZipPackageFolder::releaseUpwardRef( void )
396 : {
397 : // Now it is possible that a package folder is disconnected from the package before removing of the folder.
398 : // Such a scenario is used in storage implementation. When a new version of a folder is provided the old
399 : // one is retrieved, removed from the package but preserved for the error handling.
400 : // In this scenario the referencing to the parent is not really useful, since it requires disposing.
401 :
402 : // Actually there is no need in having a reference to the parent, it even make things more complicated and
403 : // requires disposing mechanics. Using of a simple pointer seems to be easier solution and also a safe enough.
404 :
405 136404 : clearParent();
406 :
407 : #if 0
408 : for ( ContentHash::const_iterator aCI = maContents.begin();
409 : aCI!=maContents.end();
410 : aCI++)
411 : {
412 : ContentInfo &rInfo = * (*aCI).second;
413 : if ( rInfo.bFolder )// && ! rInfo.pFolder->HasReleased () )
414 : rInfo.pFolder->releaseUpwardRef();
415 : else //if ( !rInfo.bFolder && !rInfo.pStream->HasReleased() )
416 : rInfo.pStream->clearParent();
417 : }
418 : clearParent();
419 :
420 : OSL_ENSURE ( m_refCount == 1, "Ref-count is not 1!" );
421 : #endif
422 136404 : }
423 :
424 38584 : sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const uno::Sequence< sal_Int8 >& aIdentifier )
425 : throw(uno::RuntimeException, std::exception)
426 : {
427 38584 : sal_Int64 nMe = 0;
428 154336 : if ( aIdentifier.getLength() == 16 &&
429 154336 : 0 == memcmp(static_getImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) )
430 38584 : nMe = reinterpret_cast < sal_Int64 > ( this );
431 38584 : return nMe;
432 : }
433 8144 : void SAL_CALL ZipPackageFolder::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
434 : throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, uno::RuntimeException, std::exception)
435 : {
436 8144 : if ( aPropertyName == "MediaType" )
437 : {
438 : // TODO/LATER: activate when zip ucp is ready
439 : // if ( m_nFormat != embed::StorageFormats::PACKAGE )
440 : // throw UnknownPropertyException(THROW_WHERE );
441 :
442 4072 : aValue >>= msMediaType;
443 : }
444 4072 : else if ( aPropertyName == "Version" )
445 4072 : aValue >>= m_sVersion;
446 0 : else if ( aPropertyName == "Size" )
447 0 : aValue >>= aEntry.nSize;
448 : else
449 0 : throw UnknownPropertyException(THROW_WHERE );
450 8144 : }
451 130428 : uno::Any SAL_CALL ZipPackageFolder::getPropertyValue( const OUString& PropertyName )
452 : throw(UnknownPropertyException, WrappedTargetException, uno::RuntimeException, std::exception)
453 : {
454 130428 : if ( PropertyName == "MediaType" )
455 : {
456 : // TODO/LATER: activate when zip ucp is ready
457 : // if ( m_nFormat != embed::StorageFormats::PACKAGE )
458 : // throw UnknownPropertyException(THROW_WHERE );
459 :
460 65152 : return uno::makeAny ( msMediaType );
461 : }
462 65276 : else if ( PropertyName == "Version" )
463 65276 : return uno::makeAny( m_sVersion );
464 0 : else if ( PropertyName == "Size" )
465 0 : return uno::makeAny ( aEntry.nSize );
466 : else
467 0 : throw UnknownPropertyException(THROW_WHERE );
468 : }
469 :
470 538510 : void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, bool bSetParent )
471 : throw(IllegalArgumentException, ElementExistException, WrappedTargetException, uno::RuntimeException)
472 : {
473 : try
474 : {
475 538510 : if ( pEntry->IsFolder() )
476 136432 : maContents[pEntry->getName()] = new ContentInfo ( static_cast < ZipPackageFolder *> ( pEntry ) );
477 : else
478 402078 : maContents[pEntry->getName()] = new ContentInfo ( static_cast < ZipPackageStream *> ( pEntry ) );
479 : }
480 0 : catch(const uno::Exception& rEx)
481 : {
482 : (void)rEx;
483 0 : throw;
484 : }
485 538510 : if ( bSetParent )
486 28010 : pEntry->setParent ( *this );
487 538510 : }
488 0 : OUString ZipPackageFolder::getImplementationName()
489 : throw (uno::RuntimeException, std::exception)
490 : {
491 0 : return OUString("ZipPackageFolder");
492 : }
493 :
494 0 : uno::Sequence< OUString > ZipPackageFolder::getSupportedServiceNames()
495 : throw (uno::RuntimeException, std::exception)
496 : {
497 0 : uno::Sequence< OUString > aNames(1);
498 0 : aNames[0] = "com.sun.star.packages.PackageFolder";
499 0 : return aNames;
500 : }
501 :
502 0 : sal_Bool SAL_CALL ZipPackageFolder::supportsService( OUString const & rServiceName )
503 : throw (uno::RuntimeException, std::exception)
504 : {
505 0 : return cppu::supportsService(this, rServiceName);
506 : }
507 :
508 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|