Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / package / source / zippackage / ZipPackageFolder.cxx
blob175536415a4862efc5d9b6f9dc975097ad7b1435
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <ZipPackageFolder.hxx>
21 #include <ZipOutputStream.hxx>
22 #include <ZipPackageStream.hxx>
23 #include <PackageConstants.hxx>
24 #include "ZipPackageFolderEnumeration.hxx"
25 #include <com/sun/star/io/IOException.hpp>
26 #include <com/sun/star/packages/zip/ZipConstants.hpp>
27 #include <com/sun/star/packages/zip/ZipException.hpp>
28 #include <com/sun/star/embed/StorageFormats.hpp>
29 #include <comphelper/sequence.hxx>
30 #include <comphelper/servicehelper.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <sal/log.hxx>
33 #include <com/sun/star/beans/PropertyValue.hpp>
35 using namespace com::sun::star;
36 using namespace com::sun::star::packages::zip::ZipConstants;
37 using namespace com::sun::star::packages::zip;
38 using namespace com::sun::star::packages;
39 using namespace com::sun::star::container;
40 using namespace com::sun::star::beans;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::io;
43 using namespace cppu;
45 #if OSL_DEBUG_LEVEL > 0
46 #define THROW_WHERE SAL_WHERE
47 #else
48 #define THROW_WHERE ""
49 #endif
51 ZipPackageFolder::ZipPackageFolder( const css::uno::Reference < css::uno::XComponentContext >& xContext,
52 sal_Int32 nFormat,
53 bool bAllowRemoveOnInsert )
55 m_xContext = xContext;
56 m_nFormat = nFormat;
57 mbAllowRemoveOnInsert = bAllowRemoveOnInsert;
58 SetFolder ( true );
59 aEntry.nVersion = -1;
60 aEntry.nFlag = 0;
61 aEntry.nMethod = STORED;
62 aEntry.nTime = -1;
63 aEntry.nCrc = 0;
64 aEntry.nCompressedSize = 0;
65 aEntry.nSize = 0;
66 aEntry.nOffset = -1;
69 ZipPackageFolder::~ZipPackageFolder()
73 bool ZipPackageFolder::LookForUnexpectedODF12Streams( std::u16string_view aPath )
75 bool bHasUnexpected = false;
77 for (const auto& [rShortName, rInfo] : maContents)
79 if ( rInfo.bFolder )
81 if ( aPath == u"META-INF/" )
83 // META-INF is not allowed to contain subfolders
84 bHasUnexpected = true;
86 else
88 OUString sOwnPath = aPath + rShortName + "/";
89 bHasUnexpected = rInfo.pFolder->LookForUnexpectedODF12Streams( sOwnPath );
92 else
94 if ( aPath == u"META-INF/" )
96 if ( rShortName != "manifest.xml"
97 && rShortName.indexOf( "signatures" ) == -1 )
99 // a stream from META-INF with unexpected name
100 bHasUnexpected = true;
103 // streams from META-INF with expected names are allowed not to be registered in manifest.xml
105 else if ( !rInfo.pStream->IsFromManifest() )
107 // the stream is not in META-INF and is not registered in manifest.xml,
108 // check whether it is an internal part of the package format
109 if ( !aPath.empty() || rShortName != "mimetype" )
111 // if it is not "mimetype" from the root it is not a part of the package
112 bHasUnexpected = true;
117 if (bHasUnexpected)
118 break;
121 return bHasUnexpected;
124 void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair& aPair )
126 OUString aExt;
127 if ( aPair.First.toChar() == '.' )
128 aExt = aPair.First;
129 else
130 aExt = "." + aPair.First;
132 for (const auto& [rShortName, rInfo] : maContents)
134 if ( rInfo.bFolder )
135 rInfo.pFolder->setChildStreamsTypeByExtension( aPair );
136 else
138 sal_Int32 nPathLength = rShortName.getLength();
139 sal_Int32 nExtLength = aExt.getLength();
140 if ( nPathLength >= nExtLength && rShortName.match( aExt, nPathLength - nExtLength ) )
141 rInfo.pStream->SetMediaType( aPair.Second );
146 // XNameContainer
147 void SAL_CALL ZipPackageFolder::insertByName( const OUString& aName, const uno::Any& aElement )
149 if (hasByName(aName))
150 throw ElementExistException(THROW_WHERE );
152 uno::Reference < XInterface > xRef;
153 aElement >>= xRef;
154 if ( !(aElement >>= xRef) )
155 throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
157 ZipPackageEntry* pEntry = dynamic_cast<ZipPackageFolder*>(xRef.get());
158 if (!pEntry)
159 pEntry = dynamic_cast<ZipPackageStream*>(xRef.get());
160 if (!pEntry)
161 throw IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 0 );
163 if (pEntry->getName() != aName )
164 pEntry->setName (aName);
165 doInsertByName ( pEntry, true );
168 void SAL_CALL ZipPackageFolder::removeByName( const OUString& Name )
170 ContentHash::iterator aIter = maContents.find ( Name );
171 if ( aIter == maContents.end() )
172 throw NoSuchElementException(THROW_WHERE );
173 maContents.erase( aIter );
175 // XEnumerationAccess
176 uno::Reference< XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( )
178 return uno::Reference < XEnumeration> (new ZipPackageFolderEnumeration(maContents));
180 // XElementAccess
181 uno::Type SAL_CALL ZipPackageFolder::getElementType( )
183 return cppu::UnoType<XInterface>::get();
185 sal_Bool SAL_CALL ZipPackageFolder::hasElements( )
187 return !maContents.empty();
189 // XNameAccess
190 ZipContentInfo& ZipPackageFolder::doGetByName( const OUString& aName )
192 ContentHash::iterator aIter = maContents.find ( aName );
193 if ( aIter == maContents.end())
194 throw NoSuchElementException(THROW_WHERE );
195 return aIter->second;
198 uno::Any SAL_CALL ZipPackageFolder::getByName( const OUString& aName )
200 return uno::Any ( uno::Reference<XInterface>(static_cast<cppu::OWeakObject*>(doGetByName ( aName ).xPackageEntry.get())) );
202 uno::Sequence< OUString > SAL_CALL ZipPackageFolder::getElementNames( )
204 return comphelper::mapKeysToSequence(maContents);
206 sal_Bool SAL_CALL ZipPackageFolder::hasByName( const OUString& aName )
208 return maContents.find ( aName ) != maContents.end ();
210 // XNameReplace
211 void SAL_CALL ZipPackageFolder::replaceByName( const OUString& aName, const uno::Any& aElement )
213 if ( !hasByName( aName ) )
214 throw NoSuchElementException(THROW_WHERE );
216 removeByName( aName );
217 insertByName(aName, aElement);
220 bool ZipPackageFolder::saveChild(
221 const OUString &rPath,
222 std::vector < uno::Sequence < PropertyValue > > &rManList,
223 ZipOutputStream & rZipOut,
224 const uno::Sequence < sal_Int8 >& rEncryptionKey,
225 sal_Int32 nPBKDF2IterationCount,
226 const rtlRandomPool &rRandomPool)
228 uno::Sequence < PropertyValue > aPropSet (PKG_SIZE_NOENCR_MNFST);
229 OUString sTempName = rPath + "/";
231 if ( !GetMediaType().isEmpty() )
233 auto pPropSet = aPropSet.getArray();
234 pPropSet[PKG_MNFST_MEDIATYPE].Name = "MediaType";
235 pPropSet[PKG_MNFST_MEDIATYPE].Value <<= GetMediaType();
236 pPropSet[PKG_MNFST_VERSION].Name = "Version";
237 pPropSet[PKG_MNFST_VERSION].Value <<= GetVersion();
238 pPropSet[PKG_MNFST_FULLPATH].Name = "FullPath";
239 pPropSet[PKG_MNFST_FULLPATH].Value <<= sTempName;
241 else
242 aPropSet.realloc( 0 );
244 saveContents( sTempName, rManList, rZipOut, rEncryptionKey, nPBKDF2IterationCount, rRandomPool);
246 // folder can have a mediatype only in package format
247 if ( aPropSet.hasElements() && ( m_nFormat == embed::StorageFormats::PACKAGE ) )
248 rManList.push_back( aPropSet );
250 return true;
253 void ZipPackageFolder::saveContents(
254 const OUString &rPath,
255 std::vector < uno::Sequence < PropertyValue > > &rManList,
256 ZipOutputStream & rZipOut,
257 const uno::Sequence < sal_Int8 >& rEncryptionKey,
258 sal_Int32 nPBKDF2IterationCount,
259 const rtlRandomPool &rRandomPool ) const
261 if ( maContents.empty() && !rPath.isEmpty() && m_nFormat != embed::StorageFormats::OFOPXML )
263 // it is an empty subfolder, use workaround to store it
264 ZipEntry* pTempEntry = new ZipEntry(aEntry);
265 pTempEntry->nPathLen = static_cast<sal_Int16>( OUStringToOString( rPath, RTL_TEXTENCODING_UTF8 ).getLength() );
266 pTempEntry->nExtraLen = -1;
267 pTempEntry->sPath = rPath;
271 ZipOutputStream::setEntry(pTempEntry);
272 rZipOut.writeLOC(pTempEntry);
273 rZipOut.rawCloseEntry();
275 catch ( ZipException& )
277 throw uno::RuntimeException( THROW_WHERE );
279 catch ( IOException& )
281 throw uno::RuntimeException( THROW_WHERE );
285 bool bMimeTypeStreamStored = false;
286 OUString aMimeTypeStreamName("mimetype");
287 if ( m_nFormat == embed::StorageFormats::ZIP && rPath.isEmpty() )
289 // let the "mimetype" stream in root folder be stored as the first stream if it is zip format
290 ContentHash::const_iterator aIter = maContents.find ( aMimeTypeStreamName );
291 if ( aIter != maContents.end() && !(*aIter).second.bFolder )
293 bMimeTypeStreamStored = true;
294 if( !aIter->second.pStream->saveChild(
295 rPath + aIter->first, rManList, rZipOut, rEncryptionKey, nPBKDF2IterationCount, rRandomPool ))
297 throw uno::RuntimeException( THROW_WHERE );
302 for (const auto& [rShortName, rInfo] : maContents)
304 if ( !bMimeTypeStreamStored || rShortName != aMimeTypeStreamName )
306 if (rInfo.bFolder)
308 if( !rInfo.pFolder->saveChild(
309 rPath + rShortName, rManList, rZipOut, rEncryptionKey, nPBKDF2IterationCount, rRandomPool ))
311 throw uno::RuntimeException( THROW_WHERE );
314 else
316 if( !rInfo.pStream->saveChild(
317 rPath + rShortName, rManList, rZipOut, rEncryptionKey, nPBKDF2IterationCount, rRandomPool ))
319 throw uno::RuntimeException( THROW_WHERE );
326 void SAL_CALL ZipPackageFolder::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
328 if ( aPropertyName == "MediaType" )
330 // TODO/LATER: activate when zip ucp is ready
331 // if ( m_nFormat != embed::StorageFormats::PACKAGE )
332 // throw UnknownPropertyException(THROW_WHERE );
334 aValue >>= msMediaType;
336 else if ( aPropertyName == "Version" )
337 aValue >>= m_sVersion;
338 else if ( aPropertyName == "Size" )
339 aValue >>= aEntry.nSize;
340 else
341 throw UnknownPropertyException(aPropertyName);
343 uno::Any SAL_CALL ZipPackageFolder::getPropertyValue( const OUString& PropertyName )
345 if ( PropertyName == "MediaType" )
347 // TODO/LATER: activate when zip ucp is ready
348 // if ( m_nFormat != embed::StorageFormats::PACKAGE )
349 // throw UnknownPropertyException(THROW_WHERE );
351 return uno::Any ( msMediaType );
353 else if ( PropertyName == "Version" )
354 return uno::Any( m_sVersion );
355 else if ( PropertyName == "Size" )
356 return uno::Any ( aEntry.nSize );
357 else
358 throw UnknownPropertyException(PropertyName);
361 void ZipPackageFolder::doInsertByName ( ZipPackageEntry *pEntry, bool bSetParent )
363 if ( pEntry->IsFolder() )
364 maContents.emplace(pEntry->getName(), ZipContentInfo(static_cast<ZipPackageFolder*>(pEntry)));
365 else
366 maContents.emplace(pEntry->getName(), ZipContentInfo(static_cast<ZipPackageStream*>(pEntry)));
367 if ( bSetParent )
368 pEntry->setParent ( *this );
371 OUString ZipPackageFolder::getImplementationName()
373 return "ZipPackageFolder";
376 uno::Sequence< OUString > ZipPackageFolder::getSupportedServiceNames()
378 return { "com.sun.star.packages.PackageFolder" };
381 sal_Bool SAL_CALL ZipPackageFolder::supportsService( OUString const & rServiceName )
383 return cppu::supportsService(this, rServiceName);
387 ZipContentInfo::ZipContentInfo ( ZipPackageStream * pNewStream )
388 : xPackageEntry ( pNewStream )
389 , bFolder ( false )
390 , pStream ( pNewStream )
394 ZipContentInfo::ZipContentInfo ( ZipPackageFolder * pNewFolder )
395 : xPackageEntry ( pNewFolder )
396 , bFolder ( true )
397 , pFolder ( pNewFolder )
401 ZipContentInfo::ZipContentInfo( const ZipContentInfo& ) = default;
402 ZipContentInfo::ZipContentInfo( ZipContentInfo&& ) = default;
403 ZipContentInfo& ZipContentInfo::operator=( const ZipContentInfo& ) = default;
404 ZipContentInfo& ZipContentInfo::operator=( ZipContentInfo&& ) = default;
406 ZipContentInfo::~ZipContentInfo()
408 if ( bFolder )
409 pFolder->clearParent();
410 else
411 pStream->clearParent();
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */