30#include <com/sun/star/beans/PropertyValue.hpp>
31#include <com/sun/star/packages/zip/ZipConstants.hpp>
32#include <com/sun/star/packages/zip/ZipException.hpp>
33#include <com/sun/star/packages/zip/ZipIOException.hpp>
34#include <com/sun/star/packages/manifest/ManifestReader.hpp>
35#include <com/sun/star/packages/manifest/ManifestWriter.hpp>
36#include <com/sun/star/io/TempFile.hpp>
37#include <com/sun/star/io/XStream.hpp>
38#include <com/sun/star/io/XInputStream.hpp>
39#include <com/sun/star/io/XOutputStream.hpp>
40#include <com/sun/star/io/XTruncate.hpp>
41#include <com/sun/star/io/XSeekable.hpp>
42#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
43#include <com/sun/star/container/XNameContainer.hpp>
48#include <com/sun/star/ucb/ContentCreationException.hpp>
49#include <com/sun/star/ucb/TransferInfo.hpp>
50#include <com/sun/star/ucb/NameClash.hpp>
51#include <com/sun/star/ucb/OpenCommandArgument2.hpp>
52#include <com/sun/star/ucb/OpenMode.hpp>
53#include <com/sun/star/ucb/SimpleFileAccess.hpp>
54#include <com/sun/star/io/XActiveDataStreamer.hpp>
55#include <com/sun/star/embed/UseBackupException.hpp>
56#include <com/sun/star/embed/StorageFormats.hpp>
57#include <com/sun/star/beans/NamedValue.hpp>
58#include <com/sun/star/xml/crypto/DigestID.hpp>
61#include <rtl/random.h>
63#include <osl/diagnose.h>
67#include <com/sun/star/io/XAsyncOutputMonitor.hpp>
94using namespace com::sun::star::packages::zip;
95using namespace com::sun::star::packages::manifest;
96using namespace com::sun::star::packages::zip::ZipConstants;
98#if OSL_DEBUG_LEVEL > 0
99#define THROW_WHERE SAL_WHERE
101#define THROW_WHERE ""
108 uno::Reference< XStream > mStream;
111 virtual uno::Reference< XStream > SAL_CALL getStream()
override
114 virtual void SAL_CALL setStream(
const uno::Reference< XStream >&
stream )
override
118class DummyInputStream :
public ::cppu::WeakImplHelper< XInputStream >
120 virtual sal_Int32 SAL_CALL readBytes( uno::Sequence< sal_Int8 >&, sal_Int32 )
override
123 virtual sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< sal_Int8 >&, sal_Int32 )
override
126 virtual void SAL_CALL skipBytes( sal_Int32 )
override
129 virtual sal_Int32 SAL_CALL available()
override
132 virtual void SAL_CALL closeInput()
override
139: m_aMutexHolder( new
comphelper::RefCountedMutex )
140, m_nStartKeyGenerationID( xml::crypto::DigestID::
SHA1 )
141, m_nChecksumDigestID( xml::crypto::DigestID::SHA1_1K )
142, m_nCommonEncryptionID( xml::crypto::CipherID::BLOWFISH_CFB_8 )
143, m_bHasEncryptedEntries ( false )
144, m_bHasNonEncryptedEntries ( false )
145, m_bInconsistent ( false )
146, m_bForceRecovery ( false )
147, m_bMediaTypeFallbackUsed ( false )
149, m_bAllowRemoveOnInsert( true )
167 if (
m_nFormat != embed::StorageFormats::PACKAGE )
170 bool bManifestParsed =
false;
171 static constexpr OUStringLiteral sMeta (
u"META-INF");
175 static constexpr OUStringLiteral sManifest (
u"manifest.xml");
177 uno::Reference< XNameContainer > xMetaInfFolder;
178 aAny >>= xMetaInfFolder;
179 if ( xMetaInfFolder.is() && xMetaInfFolder->hasByName( sManifest ) )
181 uno::Reference < XActiveDataSink >
xSink;
182 aAny = xMetaInfFolder->getByName( sManifest );
186 uno::Reference < XManifestReader > xReader = ManifestReader::create(
m_xContext );
188 static constexpr OUStringLiteral sPropFullPath (
u"FullPath");
189 static constexpr OUStringLiteral sPropVersion (
u"Version");
190 static constexpr OUStringLiteral sPropMediaType (
u"MediaType");
191 static constexpr OUStringLiteral sPropInitialisationVector (
u"InitialisationVector");
192 static constexpr OUStringLiteral sPropSalt (
u"Salt");
193 static constexpr OUStringLiteral sPropIterationCount (
u"IterationCount");
194 static constexpr OUStringLiteral sPropSize (
u"Size");
195 static constexpr OUStringLiteral sPropDigest (
u"Digest");
196 static constexpr OUStringLiteral sPropDerivedKeySize (
u"DerivedKeySize");
197 static constexpr OUStringLiteral sPropDigestAlgorithm (
u"DigestAlgorithm");
198 static constexpr OUStringLiteral sPropEncryptionAlgorithm (
u"EncryptionAlgorithm");
199 static constexpr OUStringLiteral sPropStartKeyAlgorithm (
u"StartKeyAlgorithm");
200 static constexpr OUStringLiteral sKeyInfo (
u"KeyInfo");
202 const uno::Sequence < uno::Sequence < PropertyValue > > aManifestSequence = xReader->readManifestSequence (
xSink->getInputStream() );
203 const Any *pKeyInfo =
nullptr;
205 for (
const uno::Sequence<PropertyValue>& rSequence : aManifestSequence )
208 const Any *pSalt =
nullptr, *pVector =
nullptr, *pCount =
nullptr, *pSize =
nullptr, *pDigest =
nullptr, *pDigestAlg =
nullptr, *pEncryptionAlg =
nullptr, *pStartKeyAlg =
nullptr, *pDerivedKeySize =
nullptr;
209 for (
const PropertyValue& rValue : rSequence )
211 if ( rValue.Name == sPropFullPath )
212 rValue.Value >>= sPath;
213 else if ( rValue.Name == sPropVersion )
214 rValue.Value >>= sVersion;
215 else if ( rValue.Name == sPropMediaType )
217 else if ( rValue.Name == sPropSalt )
218 pSalt = &( rValue.Value );
219 else if ( rValue.Name == sPropInitialisationVector )
220 pVector = &( rValue.Value );
221 else if ( rValue.Name == sPropIterationCount )
222 pCount = &( rValue.Value );
223 else if ( rValue.Name == sPropSize )
224 pSize = &( rValue.Value );
225 else if ( rValue.Name == sPropDigest )
226 pDigest = &( rValue.Value );
227 else if ( rValue.Name == sPropDigestAlgorithm )
228 pDigestAlg = &( rValue.Value );
229 else if ( rValue.Name == sPropEncryptionAlgorithm )
230 pEncryptionAlg = &( rValue.Value );
231 else if ( rValue.Name == sPropStartKeyAlgorithm )
232 pStartKeyAlg = &( rValue.Value );
233 else if ( rValue.Name == sPropDerivedKeySize )
234 pDerivedKeySize = &( rValue.Value );
235 else if ( rValue.Name == sKeyInfo )
236 pKeyInfo = &( rValue.Value );
242 uno::Reference < XInterface > xTmp;
247 pFolder->SetVersion ( sVersion );
252 pStream->SetFromManifest(
true );
254 if ( pKeyInfo && pVector && pSize && pDigest && pDigestAlg && pEncryptionAlg )
256 uno::Sequence < sal_Int8 > aSequence;
258 sal_Int32 nDigestAlg = 0, nEncryptionAlg = 0;
260 pStream->SetToBeEncrypted (
true );
262 *pVector >>= aSequence;
263 pStream->setInitialisationVector ( aSequence );
266 pStream->setSize ( nSize );
268 *pDigest >>= aSequence;
269 pStream->setDigest ( aSequence );
271 *pDigestAlg >>= nDigestAlg;
272 pStream->SetImportedChecksumAlgorithm( nDigestAlg );
274 *pEncryptionAlg >>= nEncryptionAlg;
275 pStream->SetImportedEncryptionAlgorithm( nEncryptionAlg );
279 pStream->SetToBeCompressed (
true );
280 pStream->SetToBeEncrypted (
true );
281 pStream->SetIsEncrypted (
true );
282 pStream->setIterationCount(0);
287 const sal_Int32 nStartKeyAlg = xml::crypto::DigestID::SHA256;
288 pStream->SetImportedStartKeyAlgorithm( nStartKeyAlg );
298 else if ( pSalt && pVector && pCount && pSize && pDigest && pDigestAlg && pEncryptionAlg )
300 uno::Sequence < sal_Int8 > aSequence;
302 sal_Int32
nCount = 0, nDigestAlg = 0, nEncryptionAlg = 0;
303 sal_Int32 nDerivedKeySize = 16, nStartKeyAlg = xml::crypto::DigestID::SHA1;
305 pStream->SetToBeEncrypted (
true );
307 *pSalt >>= aSequence;
308 pStream->setSalt ( aSequence );
310 *pVector >>= aSequence;
311 pStream->setInitialisationVector ( aSequence );
314 pStream->setIterationCount (
nCount );
317 pStream->setSize ( nSize );
319 *pDigest >>= aSequence;
320 pStream->setDigest ( aSequence );
322 *pDigestAlg >>= nDigestAlg;
323 pStream->SetImportedChecksumAlgorithm( nDigestAlg );
325 *pEncryptionAlg >>= nEncryptionAlg;
326 pStream->SetImportedEncryptionAlgorithm( nEncryptionAlg );
328 if ( pDerivedKeySize )
329 *pDerivedKeySize >>= nDerivedKeySize;
330 pStream->SetImportedDerivedKeySize( nDerivedKeySize );
333 *pStartKeyAlg >>= nStartKeyAlg;
334 pStream->SetImportedStartKeyAlgorithm( nStartKeyAlg );
336 pStream->SetToBeCompressed (
true );
337 pStream->SetToBeEncrypted (
true );
338 pStream->SetIsEncrypted (
true );
355 bManifestParsed =
true;
359 xMetaInfFolder->removeByName( sManifest );
370 throw ZipIOException(
373 static constexpr OUStringLiteral sMimetype (
u"mimetype");
377 OUString aPackageMediatype;
378 uno::Reference < io::XActiveDataSink > xMimeSink;
380 if ( xMimeSink.is() )
382 uno::Reference< io::XInputStream > xMimeInStream = xMimeSink->getInputStream();
383 if ( xMimeInStream.is() )
386 uno::Sequence< sal_Int8 >
aData( 1024 );
387 sal_Int32 nRead = xMimeInStream->readBytes(
aData, 1024 );
388 if ( nRead >
aData.getLength() )
389 nRead =
aData.getLength();
392 aPackageMediatype = OUString(
reinterpret_cast<char const *
>(
aData.getConstArray()), nRead, RTL_TEXTENCODING_ASCII_US );
396 if ( !bManifestParsed )
399 if ( aPackageMediatype.startsWith(
"application/vnd.") )
410 throw ZipIOException(
412 "mimetype conflicts with manifest.xml, \""
414 + aPackageMediatype +
"\"" );
430 throw ZipIOException(
431 THROW_WHERE "there are streams not referred in manifest.xml" );
440 if ( bODF12AndNewer )
446 if (
m_nFormat != embed::StorageFormats::OFOPXML )
450 static constexpr OUStringLiteral aContentTypes(
u"[Content_Types].xml");
453 throw io::IOException(
THROW_WHERE "Wrong format!" );
455 uno::Reference < io::XActiveDataSink >
xSink;
460 uno::Reference< io::XInputStream > xInStream =
xSink->getInputStream();
461 if ( xInStream.is() )
464 const uno::Sequence< uno::Sequence< beans::StringPair > > aContentTypeInfo =
465 ::comphelper::OFOPXMLHelper::ReadContentTypeSequence( xInStream,
m_xContext );
467 if ( aContentTypeInfo.getLength() != 2 )
471 for (
const auto& rPair : aContentTypeInfo[0] )
475 for (
const auto& rPair : aContentTypeInfo[1] )
478 if ( rPair.First.toChar() ==
'/' )
479 aPath = rPair.First.copy( 1 );
486 uno::Reference < XInterface > xIterTmp;
487 aIterAny >>= xIterTmp;
491 pStream->SetMediaType( rPair.Second );
500 catch( uno::Exception& )
510 OUString sTemp, sDirName;
511 sal_Int32 nOldIndex, nStreamIndex;
512 FolderHash::iterator aIter;
519 OUString rName = rEntry.
sPath;
525 rName = rName.replace(
'\\',
'/' );
528 nStreamIndex = rName.lastIndexOf (
'/' );
529 if ( nStreamIndex != -1 )
531 sDirName = rName.copy ( 0, nStreamIndex );
534 pCurrent = ( *aIter ).second;
540 while ( (
nIndex = rName.indexOf(
'/', nOldIndex ) ) != -1 )
542 sTemp = rName.copy ( nOldIndex,
nIndex - nOldIndex );
543 if (
nIndex == nOldIndex )
548 pPkgFolder->setName( sTemp );
549 pPkgFolder->doSetParent( pCurrent );
550 pCurrent = pPkgFolder.get();
556 throw css::packages::zip::ZipIOException(
"Bad Zip File, stream as folder");
561 if ( nStreamIndex != -1 && !sDirName.isEmpty() )
564 if ( rName.getLength() -1 != nStreamIndex )
567 sTemp = rName.copy( nStreamIndex );
572 pPkgStream->SetPackageMember(
true);
573 pPkgStream->setZipEntryOnLoading(rEntry);
574 pPkgStream->setName(sTemp);
575 pPkgStream->doSetParent(pCurrent);
580 if (
m_nFormat == embed::StorageFormats::PACKAGE )
582 else if (
m_nFormat == embed::StorageFormats::OFOPXML )
588 beans::NamedValue aNamedValue;
593 bool bHaveZipFile =
true;
598 if ( rArgument >>= aParamUrl )
603 sal_Int32 nParam = aParamUrl.indexOf(
'?' );
606 m_aURL = aParamUrl.copy( 0, nParam );
607 std::u16string_view aParam = aParamUrl.subView( nParam + 1 );
626 m_nFormat = embed::StorageFormats::OFOPXML;
637 m_aURL, uno::Reference< XCommandEnvironment >(),
640 sal_uInt64 aSize = 0;
643 bool bHasSizeProperty = aAny >>= aSize;
644 if( !bHasSizeProperty || aSize )
651 bHaveZipFile =
false;
653 catch ( css::uno::Exception& )
658 bHaveZipFile =
false;
671 else if ( rArgument >>= aNamedValue )
673 if ( aNamedValue.Name ==
"RepairPackage" )
675 else if ( aNamedValue.Name ==
"PackageFormat" )
680 bool bPackFormat =
true;
681 aNamedValue.Value >>= bPackFormat;
687 else if ( aNamedValue.Name ==
"StorageFormat" )
689 OUString aFormatName;
690 sal_Int32 nFormatID = 0;
691 if ( aNamedValue.Value >>= aFormatName )
694 m_nFormat = embed::StorageFormats::PACKAGE;
698 m_nFormat = embed::StorageFormats::OFOPXML;
700 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
702 else if ( aNamedValue.Value >>= nFormatID )
704 if (nFormatID != embed::StorageFormats::PACKAGE
705 && nFormatID != embed::StorageFormats::ZIP
706 && nFormatID != embed::StorageFormats::OFOPXML)
707 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
712 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
716 else if ( aNamedValue.Name ==
"AllowRemoveOnInsert" )
721 else if (aNamedValue.Name ==
"NoFileSync")
730 throw css::uno::Exception (
THROW_WHERE "Bad arguments.",
743 bHaveZipFile =
false;
746 bHaveZipFile =
false;
748 catch ( css::uno::Exception& )
753 bHaveZipFile =
false;
758 bool bBadZipFile =
false;
768 message =
"IOException: " + e.Message;
773 message =
"ZipException: " + e.Message;
786 throw css::packages::zip::ZipIOException (
794 OUString sTemp, sDirName;
795 sal_Int32 nOldIndex, nStreamIndex;
796 FolderHash::iterator aIter;
804 nStreamIndex =
aName.lastIndexOf (
'/' );
805 bool bFolder = nStreamIndex ==
nIndex-1;
807 if ( nStreamIndex != -1 )
810 sDirName =
aName.copy ( 0, nStreamIndex );
821 sal_Int32 nDirIndex =
aName.lastIndexOf (
'/', nStreamIndex );
822 sTemp =
aName.copy ( nDirIndex == -1 ? 0 : nDirIndex+1, nStreamIndex-nDirIndex-1 );
824 if (pFolder && sTemp == pFolder->getName())
825 return Any(uno::Reference(cppu::getXWeak(pFolder)));
830 sTemp =
aName.copy ( nStreamIndex + 1 );
832 if (pFolder && pFolder->
hasByName(sTemp))
851 while ( (
nIndex =
aName.indexOf(
'/', nOldIndex )) != -1 )
853 sTemp =
aName.copy ( nOldIndex,
nIndex - nOldIndex );
854 if (
nIndex == nOldIndex )
859 pPrevious = pCurrent;
862 throw css::packages::zip::ZipIOException(
"Bad Zip File, stream as folder");
869 if ( nStreamIndex != -1 )
871 return Any ( uno::Reference( cppu::getXWeak(pCurrent) ) );
874 sTemp =
aName.copy( nOldIndex );
878 if ( nStreamIndex != -1 )
890 FolderHash::iterator aIter;
901 sal_Int32 nStreamIndex;
902 nStreamIndex =
aName.lastIndexOf (
'/' );
903 bool bFolder = nStreamIndex ==
nIndex-1;
904 if ( nStreamIndex != -1 )
906 sDirName =
aName.copy ( 0, nStreamIndex );
912 sal_Int32 nDirIndex =
aName.lastIndexOf (
'/', nStreamIndex );
913 sTemp =
aName.copy ( nDirIndex == -1 ? 0 : nDirIndex+1, nStreamIndex-nDirIndex-1 );
914 if ( sTemp == ( *aIter ).second->getName() )
921 sTemp =
aName.copy ( nStreamIndex + 1 );
922 if ( ( *aIter ).second->hasByName( sTemp ) )
937 while ( (
nIndex =
aName.indexOf(
'/', nOldIndex )) != -1 )
939 sTemp =
aName.copy ( nOldIndex,
nIndex - nOldIndex );
940 if (
nIndex == nOldIndex )
944 pPrevious = pCurrent;
947 throw css::packages::zip::ZipIOException(
"Bad Zip File, stream as folder");
961 sTemp =
aName.copy( nOldIndex );
970 catch (
const uno::RuntimeException &)
974 catch (
const uno::Exception&)
976 uno::Any e(::cppu::getCaughtException());
977 throw lang::WrappedTargetRuntimeException(
"ZipPackage::hasByHierarchicalName",
nullptr, e);
991 uno::Reference < XInterface > xRef;
1004 static constexpr OUStringLiteral sMime (
u"mimetype");
1009 sal_Int32 nBufferLength =
m_xRootFolder->GetMediaType().getLength();
1011 const uno::Sequence< sal_Int8 > aType(
reinterpret_cast<sal_Int8 const *
>(
sMediaType.getStr()),
1014 pEntry->
sPath = sMime;
1030 catch (
const css::io::IOException & )
1033 throw WrappedTargetException(
1034 THROW_WHERE "Error adding mimetype to the ZipOutputStream!",
1043 uno::Reference < XManifestWriter > xWriter = ManifestWriter::create(
m_xContext );
1047 pEntry->
sPath =
"META-INF/manifest.xml";
1055 sal_Int32 nBufferLength =
static_cast < sal_Int32
> ( pBuffer->getPosition() );
1056 pBuffer->realloc( nBufferLength );
1062 aZipEntry.
write(pBuffer->getSequence());
1072 pEntry->
sPath =
"[Content_Types].xml";
1080 uno::Sequence< beans::StringPair > aDefaultsSequence
1082 {
"xml",
"application/xml" },
1083 {
"rels",
"application/vnd.openxmlformats-package.relationships+xml" },
1084 {
"png",
"image/png" },
1085 {
"jpeg",
"image/jpeg" }
1088 uno::Sequence< beans::StringPair > aOverridesSequence(aManList.size());
1089 auto aOverridesSequenceRange = asNonConstRange(aOverridesSequence);
1090 sal_Int32 nOverSeqLength = 0;
1091 for (
const auto& rMan : aManList)
1095 "The mediatype sequence format is wrong!" );
1097 if ( !aType.isEmpty() )
1103 aOverridesSequenceRange[nOverSeqLength].First =
"/" + aPath;
1104 aOverridesSequenceRange[nOverSeqLength].Second = aType;
1108 aOverridesSequence.realloc(nOverSeqLength);
1110 ::comphelper::OFOPXMLHelper::WriteContentSequence(
1111 pBuffer, aDefaultsSequence, aOverridesSequence,
m_xContext );
1113 sal_Int32 nBufferLength =
static_cast < sal_Int32
> ( pBuffer->getPosition() );
1114 pBuffer->realloc( nBufferLength );
1120 aZipEntry.
write(pBuffer->getSequence());
1145 RandomPool() : m_aRandomPool(rtl_random_createPool ())
1150 return m_aRandomPool;
1155 rtl_random_destroyPool(m_aRandomPool);
1167 bool bUseTemp =
true;
1168 uno::Reference < io::XInputStream > xResult;
1169 uno::Reference < io::XInputStream > xTempIn;
1171 uno::Reference < io::XOutputStream > xTempOut;
1172 uno::Reference< io::XActiveDataStreamer >
xSink;
1179 uno::Reference< io::XStream > xStr =
xSink->getStream();
1182 xTempOut = xStr->getOutputStream();
1191 xTempOut =
m_xStream->getOutputStream();
1200 xTempOut.set( xTempFile );
1201 xTempIn.set( xTempFile );
1208 if (
m_nFormat == embed::StorageFormats::PACKAGE )
1213 static constexpr OUStringLiteral sMeta (
u"META-INF");
1217 static constexpr OUStringLiteral sManifest (
u"manifest.xml");
1219 uno::Reference< XNameContainer > xMetaInfFolder;
1221 aAny >>= xMetaInfFolder;
1222 if ( xMetaInfFolder.is() && xMetaInfFolder->hasByName( sManifest ) )
1223 xMetaInfFolder->removeByName( sManifest );
1229 else if (
m_nFormat == embed::StorageFormats::OFOPXML )
1234 static constexpr OUStringLiteral aContentTypes(
u"[Content_Types].xml");
1241 std::vector < uno::Sequence < PropertyValue > > aManList;
1243 static constexpr OUStringLiteral
sMediaType(
u"MediaType");
1244 static constexpr OUStringLiteral sVersion(
u"Version");
1245 static constexpr OUStringLiteral sFullPath(
u"FullPath");
1246 const bool bIsGpgEncrypt =
m_aGpgProps.hasElements();
1248 if (
m_nFormat == embed::StorageFormats::PACKAGE )
1250 uno::Sequence < PropertyValue > aPropSeq(
1252 auto pPropSeq = aPropSeq.getArray();
1265 aManList.push_back( aPropSeq );
1271 RandomPool aRandomPool;
1273 sal_Int32
const nPBKDF2IterationCount = 100000;
1279 if(
m_nFormat == embed::StorageFormats::PACKAGE )
1283 else if(
m_nFormat == embed::StorageFormats::OFOPXML )
1301 uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor( xTempOut, uno::UNO_QUERY );
1303 asyncOutputMonitor->waitForCompletion();
1306 uno::Reference< io::XInputStream > xNewStream;
1308 xNewStream =
xSink->getStream()->getInputStream();
1310 xNewStream =
m_xStream->getInputStream();
1312 if ( xNewStream.is() )
1316 catch ( uno::Exception& )
1321 uno::Any aCaught( ::cppu::getCaughtException() );
1324 WrappedTargetException aException;
1325 if ( aCaught >>= aException )
1328 throw WrappedTargetException(
1329 THROW_WHERE "Problem writing the original content!",
1337 OUString aErrTxt(
THROW_WHERE "This package is unusable!");
1338 embed::UseBackupException aException( aErrTxt, uno::Reference< uno::XInterface >(), OUString() );
1339 throw WrappedTargetException( aErrTxt,
1341 Any ( aException ) );
1352 m_aURL, uno::Reference< XCommandEnvironment >(),
1360 bool bTruncSuccess =
false;
1366 if( !( aAny >>= aDetect ) )
1367 bTruncSuccess =
true;
1373 if( !bTruncSuccess )
1378 uno::Reference< XInputStream > xTempIn =
new DummyInputStream;
1382 OpenCommandArgument2 aArg;
1383 aArg.Mode = OpenMode::DOCUMENT;
1386 aArg.Properties = uno::Sequence< Property >( 0 );
1408 throw WrappedTargetException(
THROW_WHERE "This package is read only!",
1409 getXWeak(),
Any ( aException ) );
1413 uno::Reference< io::XInputStream > xTempInStream;
1418 catch (
const ucb::ContentCreationException&)
1421 throw WrappedTargetException(
THROW_WHERE "Temporary file should be creatable!",
1422 getXWeak(), anyEx );
1424 if ( xTempInStream.is() )
1426 uno::Reference< io::XSeekable > xTempSeek( xTempInStream, uno::UNO_QUERY_THROW );
1430 xTempSeek->seek( 0 );
1432 catch(
const uno::Exception& )
1435 throw WrappedTargetException(
THROW_WHERE "Temporary file should be seekable!",
1436 getXWeak(), anyEx );
1444 catch(
const io::IOException& )
1447 throw WrappedTargetException(
THROW_WHERE "Temporary file should be connectable!",
1448 getXWeak(), anyEx );
1454 uno::Reference < XOutputStream > xOutputStream;
1459 xOutputStream =
m_xStream->getOutputStream();
1464 uno::Reference<io::XSeekable> xSeekable(xOutputStream, uno::UNO_QUERY);
1468 uno::Reference < XTruncate > xTruncate ( xOutputStream, UNO_QUERY_THROW );
1471 xTruncate->truncate();
1473 catch(
const uno::Exception& )
1476 throw WrappedTargetException(
THROW_WHERE "This package is read only!",
1477 getXWeak(), anyEx );
1484 xOutputStream->flush();
1485 uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor(
1486 xOutputStream, uno::UNO_QUERY );
1487 if ( asyncOutputMonitor.is() ) {
1488 asyncOutputMonitor->waitForCompletion();
1491 catch( uno::Exception& )
1501 uno::Reference< XOutputStream > aOrigFileStream;
1502 bool bCanBeCorrupted =
false;
1507 uno::Reference< css::ucb::XSimpleFileAccess3 > xSimpleAccess(
1509 OSL_ENSURE( xSimpleAccess.is(),
"Can't instantiate SimpleFileAccess service!" );
1510 uno::Reference< io::XTruncate > xOrigTruncate;
1511 if ( xSimpleAccess.is() )
1515 aOrigFileStream = xSimpleAccess->openFileWrite(
m_aURL );
1516 xOrigTruncate.set( aOrigFileStream, uno::UNO_QUERY_THROW );
1518 xOrigTruncate->truncate();
1520 catch( uno::Exception& )
1524 if( xOrigTruncate.is() )
1529 aOrigFileStream->closeOutput();
1531 catch( uno::Exception& )
1534 aOrigFileStream->closeOutput();
1535 }
catch ( uno::Exception& ) {}
1537 aOrigFileStream.clear();
1539 bCanBeCorrupted =
true;
1544 if( !aOrigFileStream.is() )
1548 uno::Reference < XPropertySet > xPropSet ( xTempInStream, UNO_QUERY_THROW );
1550 OUString sTargetFolder =
m_aURL.copy ( 0,
m_aURL.lastIndexOf (
u'/' ) );
1552 sTargetFolder, uno::Reference< XCommandEnvironment >(),
1556 Any aAny = xPropSet->getPropertyValue (
"Uri");
1560 aInfo.NameClash = NameClash::OVERWRITE;
1561 aInfo.MoveData =
false;
1562 aInfo.SourceURL = sTempURL;
1563 aInfo.NewTitle = rtl::Uri::decode (
m_aURL.copy ( 1 +
m_aURL.lastIndexOf (
u'/' ) ),
1564 rtl_UriDecodeWithCharset,
1565 RTL_TEXTENCODING_UTF8 );
1569 catch (
const css::uno::Exception& )
1571 if ( bCanBeCorrupted )
1575 throw WrappedTargetException(
1590 m_xStream.set( xTempStream, uno::UNO_QUERY );
1598 uno::Reference< beans::XPropertySet > xTempFile( xTempStream, uno::UNO_QUERY_THROW );
1599 uno::Any aUrl = xTempFile->getPropertyValue(
"Uri");
1601 xTempFile->setPropertyValue(
"RemoveFile",
1604 catch ( uno::Exception& )
1606 OSL_FAIL(
"These calls are pretty simple, they should not fail!" );
1609 OUString aErrTxt(
THROW_WHERE "This package is read only!");
1610 embed::UseBackupException aException( aErrTxt, uno::Reference< uno::XInterface >(), aTempURL );
1611 throw WrappedTargetException( aErrTxt,
1613 Any ( aException ) );
1618 uno::Sequence< sal_Int8 > aResult;
1622 OUString aNameToFind;
1628 throw uno::RuntimeException(
THROW_WHERE "No expected key is provided!" );
1631 if ( rKey.Name == aNameToFind )
1632 rKey.Value >>= aResult;
1646 return uno::Sequence < ElementChange > ();
1652 return "com.sun.star.packages.comp.ZipPackage";
1657 return {
"com.sun.star.packages.Package" };
1667 return uno::Reference < XPropertySetInfo > ();
1672 if (
m_nFormat != embed::StorageFormats::PACKAGE )
1673 throw UnknownPropertyException(aPropertyName);
1683 throw IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 2 );
1693 throw IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 2 );
1699 uno::Sequence< beans::NamedValue > aAlgorithms;
1700 if (
m_pZipFile || !( aValue >>= aAlgorithms ) || !aAlgorithms.hasElements() )
1703 throw IllegalArgumentException(
THROW_WHERE "unexpected algorithms list is provided.", uno::Reference< uno::XInterface >(), 2 );
1706 for (
const auto& rAlgorithm : std::as_const(aAlgorithms) )
1708 if ( rAlgorithm.Name ==
"StartKeyGenerationAlgorithm" )
1711 if ( !( rAlgorithm.Value >>= nID )
1712 || ( nID != xml::crypto::DigestID::SHA256 && nID != xml::crypto::DigestID::SHA1 ) )
1713 throw IllegalArgumentException(
THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 );
1717 else if ( rAlgorithm.Name ==
"EncryptionAlgorithm" )
1720 if ( !( rAlgorithm.Value >>= nID )
1721 || ( nID != xml::crypto::CipherID::AES_CBC_W3C_PADDING && nID != xml::crypto::CipherID::BLOWFISH_CFB_8 ) )
1722 throw IllegalArgumentException(
THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 );
1726 else if ( rAlgorithm.Name ==
"ChecksumAlgorithm" )
1729 if ( !( rAlgorithm.Value >>= nID )
1730 || ( nID != xml::crypto::DigestID::SHA1_1K && nID != xml::crypto::DigestID::SHA256_1K ) )
1731 throw IllegalArgumentException(
THROW_WHERE "Unexpected start key generation algorithm is provided!", uno::Reference< uno::XInterface >(), 2 );
1737 OSL_ENSURE(
false,
"Unexpected encryption algorithm is provided!" );
1738 throw IllegalArgumentException(
THROW_WHERE "unexpected algorithms list is provided.", uno::Reference< uno::XInterface >(), 2 );
1744 uno::Sequence< uno::Sequence< beans::NamedValue > > aGpgProps;
1745 if ( !( aValue >>= aGpgProps ) || !aGpgProps.hasElements() )
1747 throw IllegalArgumentException(
THROW_WHERE "unexpected Gpg properties are provided.", uno::Reference< uno::XInterface >(), 2 );
1759 throw UnknownPropertyException(aPropertyName);
1804 throw UnknownPropertyException(PropertyName);
1819extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1821 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any>
const&)
1823 return cppu::acquire(
new ZipPackage(context));
1831 css::uno::Sequence<Any> aArgs{
Any(
xStream),
Any(NamedValue(
"RepairPackage",
Any(
true))) };
1832 xPackage->initialize(aArgs);
Reference< XComponentContext > m_xContext
constexpr OUStringLiteral ENCRYPTION_ALGORITHMS_PROPERTY
#define IS_INCONSISTENT_PROPERTY
#define HAS_NONENCRYPTED_ENTRIES_PROPERTY
constexpr OUStringLiteral MEDIATYPE_FALLBACK_USED_PROPERTY
#define PKG_SIZE_NOENCR_MNFST
#define PKG_MNFST_MEDIATYPE
constexpr OUStringLiteral STORAGE_ENCRYPTION_KEYS_PROPERTY
#define PKG_MNFST_VERSION
#define HAS_ENCRYPTED_ENTRIES_PROPERTY
#define PKG_MNFST_FULLPATH
constexpr OUStringLiteral ENCRYPTION_GPG_PROPERTIES
constexpr OUStringLiteral ENCRYPTION_KEY_PROPERTY
bool TestImportZip(SvStream &rStream)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * package_ZipPackage_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
constexpr OUStringLiteral sMediaType
sal_Int32 getValue() const
void update(const css::uno::Sequence< sal_Int8 > &b)
Update CRC32 with specified sequence of bytes.
const ZipEntry * nextElement()
void write(const css::uno::Sequence< sal_Int8 > &rBuffer)
void writeLOC(ZipEntry *pEntry, bool bEncrypt=false)
void rawCloseEntry(bool bEncrypt=false)
const css::uno::Reference< css::io::XOutputStream > & getStream() const
void rawWrite(const css::uno::Sequence< sal_Int8 > &rBuffer)
static void setEntry(ZipEntry *pEntry)
static sal_uInt32 getCurrentDosTime()
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
ZipContentInfo & doGetByName(const OUString &aName)
bool m_bMediaTypeFallbackUsed
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL addVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
std::optional< ZipFile > m_pZipFile
virtual void SAL_CALL commitChanges() override
sal_Int32 m_nStartKeyGenerationID
virtual ~ZipPackage() override
void DisconnectFromTargetAndThrowException_Impl(const css::uno::Reference< css::io::XInputStream > &xTempStream)
css::uno::Reference< css::io::XActiveDataStreamer > openOriginalForOutput()
css::uno::Sequence< sal_Int8 > m_aEncryptionKey
const css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Sequence< css::uno::Sequence< css::beans::NamedValue > > m_aGpgProps
bool m_bAllowRemoveOnInsert
virtual void SAL_CALL addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener) override
void WriteMimetypeMagicFile(ZipOutputStream &aZipOut)
virtual css::uno::Any SAL_CALL getByHierarchicalName(const OUString &aName) override
bool m_bHasEncryptedEntries
InitialisationMode m_eMode
rtl::Reference< comphelper::RefCountedMutex > m_aMutexHolder
void getZipFileContents()
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &PropertyName) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
void ConnectTo(const css::uno::Reference< css::io::XInputStream > &xInStream)
sal_Int32 m_nCommonEncryptionID
bool m_bHasNonEncryptedEntries
virtual void SAL_CALL removeVetoableChangeListener(const OUString &PropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &aListener) override
void WriteContentTypes(ZipOutputStream &aZipOut, const ::std::vector< css::uno::Sequence< css::beans::PropertyValue > > &aManList)
virtual sal_Bool SAL_CALL hasPendingChanges() override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::io::XStream > m_xStream
virtual css::uno::Sequence< css::util::ElementChange > SAL_CALL getPendingChanges() override
rtl::Reference< ZipPackageFolder > m_xRootFolder
virtual void SAL_CALL removePropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &aListener) override
void WriteManifest(ZipOutputStream &aZipOut, const ::std::vector< css::uno::Sequence< css::beans::PropertyValue > > &aManList)
sal_Int32 m_nChecksumDigestID
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::io::XInputStream > writeTempFile()
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
css::uno::Reference< css::io::XSeekable > m_xContentSeek
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override
virtual void SAL_CALL setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
ZipPackage(css::uno::Reference< css::uno::XComponentContext > xContext)
virtual sal_Bool SAL_CALL hasByHierarchicalName(const OUString &aName) override
css::uno::Sequence< css::beans::NamedValue > m_aStorageEncryptionKeys
css::uno::Sequence< sal_Int8 > GetEncryptionKey()
css::uno::Reference< css::io::XInputStream > m_xContentStream
static void CopyInputToOutput(const css::uno::Reference< css::io::XInputStream > &xInput, const css::uno::Reference< css::io::XOutputStream > &xOutput)
css::uno::Sequence< css::beans::NamedValue > getAsConstNamedValueList() const
css::uno::Any setPropertyValue(const OUString &rPropertyName, const css::uno::Any &rValue)
css::uno::Any getPropertyValue(const OUString &rPropertyName)
css::uno::Any executeCommand(const OUString &rCommandName, const css::uno::Any &rCommandArgument)
css::uno::Reference< css::io::XInputStream > openStream()
void writeStream(const css::uno::Reference< css::io::XInputStream > &rStream, bool bReplaceExisting)
Reference< XOutputStream > stream
constexpr OUStringLiteral ODFVER_012_TEXT
Sequence< PropertyValue > aArguments
tools::SvRef< SvBaseLink > xSink
constexpr OUStringLiteral aData
COMPHELPER_DLLPUBLIC bool isFileUrl(std::u16string_view url)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XComponentContext > getProcessComponentContext()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
sal_uInt16 const m_nFormat
constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING
constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA1CORRECT
constexpr OUStringLiteral OFOPXML_STORAGE_FORMAT_STRING
constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA256UTF8
constexpr OUStringLiteral PACKAGE_STORAGE_FORMAT_STRING
ZipPackageFolder * pFolder
sal_Int64 nCompressedSize