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 <sfx2/docfile.hxx>
21 #include <sfx2/docfilt.hxx>
22 #include <sfx2/sfxsids.hrc>
23 #include <filter/msfilter/msoleexp.hxx>
24 #include <svx/svxerr.hxx>
25 #include <unotools/fltrcfg.hxx>
26 #include <unotools/streamwrap.hxx>
27 #include <sot/storage.hxx>
28 #include <comphelper/sequenceashashmap.hxx>
29 #include <comphelper/processfactory.hxx>
31 #include <com/sun/star/packages/XPackageEncryption.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <sdpptwrp.hxx>
35 #include <DrawDocShell.hxx>
36 #include <sfx2/frame.hxx>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::task
;
41 using namespace ::com::sun::star::frame
;
43 SdPPTFilter::SdPPTFilter( SfxMedium
& rMedium
, ::sd::DrawDocShell
& rDocShell
) :
44 SdFilter( rMedium
, rDocShell
),
49 SdPPTFilter::~SdPPTFilter()
51 delete pBas
; // deleting the compressed basic storage
54 static void lcl_getListOfStreams(SotStorage
* pStorage
, comphelper::SequenceAsHashMap
& aStreamsData
, std::u16string_view sPrefix
)
56 SvStorageInfoList aElements
;
57 pStorage
->FillInfoList(&aElements
);
58 for (const auto & aElement
: aElements
)
60 OUString sStreamFullName
= sPrefix
.size() ? OUString::Concat(sPrefix
) + "/" + aElement
.GetName() : aElement
.GetName();
61 if (aElement
.IsStorage())
63 tools::SvRef
<SotStorage
> xSubStorage
= pStorage
->OpenSotStorage(aElement
.GetName(), StreamMode::STD_READ
| StreamMode::SHARE_DENYALL
);
64 lcl_getListOfStreams(xSubStorage
.get(), aStreamsData
, sStreamFullName
);
69 tools::SvRef
<SotStorageStream
> rStream
= pStorage
->OpenSotStream(aElement
.GetName(), StreamMode::READ
| StreamMode::SHARE_DENYALL
);
72 sal_Int32 nStreamSize
= rStream
->GetSize();
73 Sequence
< sal_Int8
> oData
;
74 oData
.realloc(nStreamSize
);
75 sal_Int32 nReadBytes
= rStream
->ReadBytes(oData
.getArray(), nStreamSize
);
76 if (nStreamSize
== nReadBytes
)
77 aStreamsData
[sStreamFullName
] <<= oData
;
83 static tools::SvRef
<SotStorage
> lcl_DRMDecrypt(const SfxMedium
& rMedium
, const tools::SvRef
<SotStorage
>& rStorage
, std::shared_ptr
<SvStream
>& rNewStorageStrm
)
85 tools::SvRef
<SotStorage
> aNewStorage
;
87 // We have DRM encrypted storage. We should try to decrypt it first, if we can
88 Sequence
< Any
> aArguments
;
89 Reference
<XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
90 Reference
< css::packages::XPackageEncryption
> xPackageEncryption(
91 xComponentContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
92 "com.sun.star.comp.oox.crypto.DRMDataSpace", aArguments
, xComponentContext
), UNO_QUERY
);
94 if (!xPackageEncryption
.is())
96 // We do not know how to decrypt this
100 comphelper::SequenceAsHashMap aStreamsData
;
101 lcl_getListOfStreams(rStorage
.get(), aStreamsData
, u
"");
104 Sequence
<NamedValue
> aStreams
= aStreamsData
.getAsConstNamedValueList();
105 if (!xPackageEncryption
->readEncryptionInfo(aStreams
))
107 // We failed with decryption
111 tools::SvRef
<SotStorageStream
> rContentStream
= rStorage
->OpenSotStream("\011DRMContent", StreamMode::READ
| StreamMode::SHARE_DENYALL
);
112 if (!rContentStream
.is())
117 rNewStorageStrm
= std::make_shared
<SvMemoryStream
>();
119 Reference
<css::io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(rContentStream
.get(), false));
120 Reference
<css::io::XOutputStream
> xDecryptedStream(new utl::OSeekableOutputStreamWrapper(*rNewStorageStrm
));
122 if (!xPackageEncryption
->decrypt(xInputStream
, xDecryptedStream
))
124 // We failed with decryption
128 rNewStorageStrm
->Seek(0);
130 // Further reading is done from new document
131 aNewStorage
= new SotStorage(*rNewStorageStrm
);
133 // Set the media descriptor data
134 Sequence
<NamedValue
> aEncryptionData
= xPackageEncryption
->createEncryptionData("");
135 rMedium
.GetItemSet()->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA
, Any(aEncryptionData
)));
137 catch (const std::exception
&)
145 bool SdPPTFilter::Import()
148 std::shared_ptr
<SvStream
> aDecryptedStorageStrm
;
149 tools::SvRef
<SotStorage
> pStorage
= new SotStorage( mrMedium
.GetInStream(), false );
150 if( !pStorage
->GetError() )
152 /* check if there is a dualstorage, then the
153 document is probably a PPT95 containing PPT97 */
154 tools::SvRef
<SotStorage
> xDualStorage
;
155 OUString
sDualStorage( "PP97_DUALSTORAGE" );
156 if ( pStorage
->IsContained( sDualStorage
) )
158 xDualStorage
= pStorage
->OpenSotStorage( sDualStorage
, StreamMode::STD_READ
);
159 pStorage
= xDualStorage
;
161 if (pStorage
->IsContained("\011DRMContent"))
163 // Document is DRM encrypted
164 pStorage
= lcl_DRMDecrypt(mrMedium
, pStorage
, aDecryptedStorageStrm
);
166 tools::SvRef
<SotStorageStream
> pDocStream(pStorage
->OpenSotStream( "PowerPoint Document" , StreamMode::STD_READ
));
169 pDocStream
->SetVersion( pStorage
->GetVersion() );
170 pDocStream
->SetCryptMaskKey(pStorage
->GetKey());
172 if ( pStorage
->IsStream( "EncryptedSummary" ) )
173 mrMedium
.SetError(ERRCODE_SVX_READ_FILTER_PPOINT
);
176 bRet
= ImportPPT( &mrDocument
, *pDocStream
, *pStorage
, mrMedium
);
179 mrMedium
.SetError(SVSTREAM_WRONGVERSION
);
187 bool SdPPTFilter::Export()
193 sal_uInt32 nCnvrtFlags
= 0;
194 const SvtFilterOptions
& rFilterOptions
= SvtFilterOptions::Get();
195 if ( rFilterOptions
.IsMath2MathType() )
196 nCnvrtFlags
|= OLE_STARMATH_2_MATHTYPE
;
197 if ( rFilterOptions
.IsWriter2WinWord() )
198 nCnvrtFlags
|= OLE_STARWRITER_2_WINWORD
;
199 if ( rFilterOptions
.IsCalc2Excel() )
200 nCnvrtFlags
|= OLE_STARCALC_2_EXCEL
;
201 if ( rFilterOptions
.IsImpress2PowerPoint() )
202 nCnvrtFlags
|= OLE_STARIMPRESS_2_POWERPOINT
;
203 if ( rFilterOptions
.IsEnablePPTPreview() )
204 nCnvrtFlags
|= 0x8000;
206 CreateStatusIndicator();
208 //OUString sBaseURI( "BaseURI");
209 std::vector
< PropertyValue
> aProperties
;
210 PropertyValue aProperty
;
211 aProperty
.Name
= "BaseURI";
212 aProperty
.Value
<<= mrMedium
.GetBaseURL( true );
213 aProperties
.push_back( aProperty
);
215 SvStream
* pOutputStrm
= mrMedium
.GetOutStream();
217 Sequence
< NamedValue
> aEncryptionData
;
218 Reference
< css::packages::XPackageEncryption
> xPackageEncryption
;
219 const SfxUnoAnyItem
* pEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(mrMedium
.GetItemSet(), SID_ENCRYPTIONDATA
, false);
220 std::shared_ptr
<SvStream
> pMediaStrm
;
221 if (pEncryptionDataItem
&& (pEncryptionDataItem
->GetValue() >>= aEncryptionData
))
223 ::comphelper::SequenceAsHashMap
aHashData(aEncryptionData
);
224 OUString sCryptoType
= aHashData
.getUnpackedValueOrDefault("CryptoType", OUString());
226 if (sCryptoType
.getLength())
228 Reference
<XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
229 Sequence
<Any
> aArguments
{
230 Any(NamedValue("Binary", Any(true))) };
231 xPackageEncryption
.set(
232 xComponentContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
233 "com.sun.star.comp.oox.crypto." + sCryptoType
, aArguments
, xComponentContext
), UNO_QUERY
);
235 if (xPackageEncryption
.is())
237 // We have an encryptor. Export document into memory stream and encrypt it later
238 pMediaStrm
= std::make_shared
<SvMemoryStream
>();
239 pOutputStrm
= pMediaStrm
.get();
241 // Temp removal of EncryptionData to avoid password protection triggering
242 mrMedium
.GetItemSet()->ClearItem(SID_ENCRYPTIONDATA
);
247 tools::SvRef
<SotStorage
> xStorRef
= new SotStorage(pOutputStrm
, false);
251 bRet
= ExportPPT(aProperties
, xStorRef
, mxModel
, mxStatusIndicator
, pBas
, nCnvrtFlags
);
254 if (xPackageEncryption
.is())
256 // Perform DRM encryption
257 pOutputStrm
->Seek(0);
259 xPackageEncryption
->setupEncryption(aEncryptionData
);
261 Reference
<css::io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(pOutputStrm
, false));
262 Sequence
<NamedValue
> aStreams
= xPackageEncryption
->encrypt(xInputStream
);
264 tools::SvRef
<SotStorage
> xEncryptedRootStrg
= new SotStorage(mrMedium
.GetOutStream(), false);
265 for (const NamedValue
& aStreamData
: std::as_const(aStreams
))
267 // To avoid long paths split and open substorages recursively
268 // Splitting paths manually, since comphelper::string::split is trimming special characters like \0x01, \0x09
269 tools::SvRef
<SotStorage
> pStorage
= xEncryptedRootStrg
.get();
274 OUString sPathElem
= aStreamData
.Name
.getToken(0, L
'/', idx
);
275 if (!sPathElem
.isEmpty())
279 sFileName
= sPathElem
;
283 pStorage
= pStorage
->OpenSotStorage(sPathElem
);
286 } while (pStorage
&& idx
>= 0);
294 tools::SvRef
<SotStorageStream
> pStream
= pStorage
->OpenSotStream(sFileName
);
300 Sequence
<sal_Int8
> aStreamContent
;
301 aStreamData
.Value
>>= aStreamContent
;
302 size_t nBytesWritten
= pStream
->WriteBytes(aStreamContent
.getConstArray(), aStreamContent
.getLength());
303 if (nBytesWritten
!= static_cast<size_t>(aStreamContent
.getLength()))
309 xEncryptedRootStrg
->Commit();
311 // Restore encryption data
312 mrMedium
.GetItemSet()->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA
, Any(aEncryptionData
)));
320 void SdPPTFilter::PreSaveBasic()
322 const SvtFilterOptions
& rFilterOptions
= SvtFilterOptions::Get();
323 if( rFilterOptions
.IsLoadPPointBasicStorage() )
325 SaveVBA( static_cast<SfxObjectShell
&>(mrDocShell
), pBas
);
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */