1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
45 #if OSL_DEBUG_LEVEL > 0
46 #define THROW_WHERE SAL_WHERE
48 #define THROW_WHERE ""
51 ZipPackageFolder::ZipPackageFolder( const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
53 bool bAllowRemoveOnInsert
)
55 m_xContext
= xContext
;
57 mbAllowRemoveOnInsert
= bAllowRemoveOnInsert
;
61 aEntry
.nMethod
= STORED
;
64 aEntry
.nCompressedSize
= 0;
69 ZipPackageFolder::~ZipPackageFolder()
73 bool ZipPackageFolder::LookForUnexpectedODF12Streams( std::u16string_view aPath
)
75 bool bHasUnexpected
= false;
77 for (const auto& [rShortName
, rInfo
] : maContents
)
81 if ( aPath
== u
"META-INF/" )
83 // META-INF is not allowed to contain subfolders
84 bHasUnexpected
= true;
88 OUString sOwnPath
= aPath
+ rShortName
+ "/";
89 bHasUnexpected
= rInfo
.pFolder
->LookForUnexpectedODF12Streams( sOwnPath
);
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;
121 return bHasUnexpected
;
124 void ZipPackageFolder::setChildStreamsTypeByExtension( const beans::StringPair
& aPair
)
127 if ( aPair
.First
.toChar() == '.' )
130 aExt
= "." + aPair
.First
;
132 for (const auto& [rShortName
, rInfo
] : maContents
)
135 rInfo
.pFolder
->setChildStreamsTypeByExtension( aPair
);
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
);
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
;
154 if ( !(aElement
>>= xRef
) )
155 throw IllegalArgumentException(THROW_WHERE
, uno::Reference
< uno::XInterface
>(), 0 );
157 ZipPackageEntry
* pEntry
= dynamic_cast<ZipPackageFolder
*>(xRef
.get());
159 pEntry
= dynamic_cast<ZipPackageStream
*>(xRef
.get());
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
));
181 uno::Type SAL_CALL
ZipPackageFolder::getElementType( )
183 return cppu::UnoType
<XInterface
>::get();
185 sal_Bool SAL_CALL
ZipPackageFolder::hasElements( )
187 return !maContents
.empty();
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 ();
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
;
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
);
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
)
308 if( !rInfo
.pFolder
->saveChild(
309 rPath
+ rShortName
, rManList
, rZipOut
, rEncryptionKey
, nPBKDF2IterationCount
, rRandomPool
))
311 throw uno::RuntimeException( THROW_WHERE
);
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
;
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
);
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
)));
366 maContents
.emplace(pEntry
->getName(), ZipContentInfo(static_cast<ZipPackageStream
*>(pEntry
)));
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
)
390 , pStream ( pNewStream
)
394 ZipContentInfo::ZipContentInfo ( ZipPackageFolder
* pNewFolder
)
395 : xPackageEntry ( pNewFolder
)
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()
409 pFolder
->clearParent();
411 pStream
->clearParent();
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */