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 typedef sal_Bool ( *ExportPPTPointer
)( const std::vector
< css::beans::PropertyValue
>&, tools::SvRef
<SotStorage
> const&,
44 Reference
< XModel
> const &,
45 Reference
< XStatusIndicator
> const &,
46 SvMemoryStream
*, sal_uInt32 nCnvrtFlags
);
48 typedef sal_Bool ( *ImportPPTPointer
)( SdDrawDocument
*, SvStream
&, SotStorage
&, SfxMedium
& );
50 typedef sal_Bool ( *SaveVBAPointer
)( SfxObjectShell
&, SvMemoryStream
*& );
52 #ifdef DISABLE_DYNLOADING
54 extern "C" sal_Bool
ExportPPT( const std::vector
< css::beans::PropertyValue
>&, tools::SvRef
<SotStorage
> const&,
55 Reference
< XModel
> const &,
56 Reference
< XStatusIndicator
> const &,
57 SvMemoryStream
*, sal_uInt32 nCnvrtFlags
);
59 extern "C" sal_Bool
ImportPPT( SdDrawDocument
*, SvStream
&, SotStorage
&, SfxMedium
& );
61 extern "C" sal_Bool
SaveVBA( SfxObjectShell
&, SvMemoryStream
*& );
66 SdPPTFilter::SdPPTFilter( SfxMedium
& rMedium
, ::sd::DrawDocShell
& rDocShell
) :
67 SdFilter( rMedium
, rDocShell
),
72 SdPPTFilter::~SdPPTFilter()
74 delete pBas
; // deleting the compressed basic storage
77 static void lcl_getListOfStreams(SotStorage
* pStorage
, comphelper::SequenceAsHashMap
& aStreamsData
, const OUString
& sPrefix
)
79 SvStorageInfoList aElements
;
80 pStorage
->FillInfoList(&aElements
);
81 for (const auto & aElement
: aElements
)
83 OUString sStreamFullName
= sPrefix
.getLength() ? sPrefix
+ "/" + aElement
.GetName() : aElement
.GetName();
84 if (aElement
.IsStorage())
86 tools::SvRef
<SotStorage
> xSubStorage
= pStorage
->OpenSotStorage(aElement
.GetName(), StreamMode::STD_READ
| StreamMode::SHARE_DENYALL
);
87 lcl_getListOfStreams(xSubStorage
.get(), aStreamsData
, sStreamFullName
);
92 tools::SvRef
<SotStorageStream
> rStream
= pStorage
->OpenSotStream(aElement
.GetName(), StreamMode::READ
| StreamMode::SHARE_DENYALL
);
95 sal_Int32 nStreamSize
= rStream
->GetSize();
96 Sequence
< sal_Int8
> oData
;
97 oData
.realloc(nStreamSize
);
98 sal_Int32 nReadBytes
= rStream
->ReadBytes(oData
.getArray(), nStreamSize
);
99 if (nStreamSize
== nReadBytes
)
100 aStreamsData
[sStreamFullName
] <<= oData
;
106 static tools::SvRef
<SotStorage
> lcl_DRMDecrypt(const SfxMedium
& rMedium
, const tools::SvRef
<SotStorage
>& rStorage
, std::shared_ptr
<SvStream
>& rNewStorageStrm
)
108 tools::SvRef
<SotStorage
> aNewStorage
;
110 // We have DRM encrypted storage. We should try to decrypt it first, if we can
111 Sequence
< Any
> aArguments
;
112 Reference
<XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
113 Reference
< css::packages::XPackageEncryption
> xPackageEncryption(
114 xComponentContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
115 "com.sun.star.comp.oox.crypto.DRMDataSpace", aArguments
, xComponentContext
), UNO_QUERY
);
117 if (!xPackageEncryption
.is())
119 // We do not know how to decrypt this
123 comphelper::SequenceAsHashMap aStreamsData
;
124 lcl_getListOfStreams(rStorage
.get(), aStreamsData
, "");
127 Sequence
<NamedValue
> aStreams
= aStreamsData
.getAsConstNamedValueList();
128 if (!xPackageEncryption
->readEncryptionInfo(aStreams
))
130 // We failed with decryption
134 tools::SvRef
<SotStorageStream
> rContentStream
= rStorage
->OpenSotStream("\011DRMContent", StreamMode::READ
| StreamMode::SHARE_DENYALL
);
135 if (!rContentStream
.is())
140 rNewStorageStrm
= std::make_shared
<SvMemoryStream
>();
142 Reference
<css::io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(rContentStream
.get(), false));
143 Reference
<css::io::XOutputStream
> xDecryptedStream(new utl::OSeekableOutputStreamWrapper(*rNewStorageStrm
));
145 if (!xPackageEncryption
->decrypt(xInputStream
, xDecryptedStream
))
147 // We failed with decryption
151 rNewStorageStrm
->Seek(0);
153 // Further reading is done from new document
154 aNewStorage
= new SotStorage(*rNewStorageStrm
);
156 // Set the media descriptor data
157 Sequence
<NamedValue
> aEncryptionData
= xPackageEncryption
->createEncryptionData("");
158 rMedium
.GetItemSet()->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA
, makeAny(aEncryptionData
)));
160 catch (const std::exception
&)
168 bool SdPPTFilter::Import()
171 std::shared_ptr
<SvStream
> aDecryptedStorageStrm
;
172 tools::SvRef
<SotStorage
> pStorage
= new SotStorage( mrMedium
.GetInStream(), false );
173 if( !pStorage
->GetError() )
175 /* check if there is a dualstorage, then the
176 document is probably a PPT95 containing PPT97 */
177 tools::SvRef
<SotStorage
> xDualStorage
;
178 OUString
sDualStorage( "PP97_DUALSTORAGE" );
179 if ( pStorage
->IsContained( sDualStorage
) )
181 xDualStorage
= pStorage
->OpenSotStorage( sDualStorage
, StreamMode::STD_READ
);
182 pStorage
= xDualStorage
;
184 if (pStorage
->IsContained("\011DRMContent"))
186 // Document is DRM encrypted
187 pStorage
= lcl_DRMDecrypt(mrMedium
, pStorage
, aDecryptedStorageStrm
);
189 tools::SvRef
<SotStorageStream
> pDocStream(pStorage
->OpenSotStream( "PowerPoint Document" , StreamMode::STD_READ
));
192 pDocStream
->SetVersion( pStorage
->GetVersion() );
193 pDocStream
->SetCryptMaskKey(pStorage
->GetKey());
195 if ( pStorage
->IsStream( "EncryptedSummary" ) )
196 mrMedium
.SetError(ERRCODE_SVX_READ_FILTER_PPOINT
);
199 #ifdef DISABLE_DYNLOADING
200 ImportPPTPointer pPPTImport
= ImportPPT
;
202 ImportPPTPointer pPPTImport
= reinterpret_cast< ImportPPTPointer
>(
203 SdFilter::GetLibrarySymbol(mrMedium
.GetFilter()->GetUserData(), "ImportPPT"));
207 bRet
= pPPTImport( &mrDocument
, *pDocStream
, *pStorage
, mrMedium
);
210 mrMedium
.SetError(SVSTREAM_WRONGVERSION
);
218 bool SdPPTFilter::Export()
224 #ifdef DISABLE_DYNLOADING
225 ExportPPTPointer PPTExport
= ExportPPT
;
227 ExportPPTPointer PPTExport
= reinterpret_cast< ExportPPTPointer
>(
228 SdFilter::GetLibrarySymbol(mrMedium
.GetFilter()->GetUserData(), "ExportPPT"));
233 sal_uInt32 nCnvrtFlags
= 0;
234 const SvtFilterOptions
& rFilterOptions
= SvtFilterOptions::Get();
235 if ( rFilterOptions
.IsMath2MathType() )
236 nCnvrtFlags
|= OLE_STARMATH_2_MATHTYPE
;
237 if ( rFilterOptions
.IsWriter2WinWord() )
238 nCnvrtFlags
|= OLE_STARWRITER_2_WINWORD
;
239 if ( rFilterOptions
.IsCalc2Excel() )
240 nCnvrtFlags
|= OLE_STARCALC_2_EXCEL
;
241 if ( rFilterOptions
.IsImpress2PowerPoint() )
242 nCnvrtFlags
|= OLE_STARIMPRESS_2_POWERPOINT
;
243 if ( rFilterOptions
.IsEnablePPTPreview() )
244 nCnvrtFlags
|= 0x8000;
246 CreateStatusIndicator();
248 //OUString sBaseURI( "BaseURI");
249 std::vector
< PropertyValue
> aProperties
;
250 PropertyValue aProperty
;
251 aProperty
.Name
= "BaseURI";
252 aProperty
.Value
<<= mrMedium
.GetBaseURL( true );
253 aProperties
.push_back( aProperty
);
255 SvStream
* pOutputStrm
= mrMedium
.GetOutStream();
257 Sequence
< NamedValue
> aEncryptionData
;
258 Reference
< css::packages::XPackageEncryption
> xPackageEncryption
;
259 const SfxUnoAnyItem
* pEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(mrMedium
.GetItemSet(), SID_ENCRYPTIONDATA
, false);
260 std::shared_ptr
<SvStream
> pMediaStrm
;
261 if (pEncryptionDataItem
&& (pEncryptionDataItem
->GetValue() >>= aEncryptionData
))
263 ::comphelper::SequenceAsHashMap
aHashData(aEncryptionData
);
264 OUString sCryptoType
= aHashData
.getUnpackedValueOrDefault("CryptoType", OUString());
266 if (sCryptoType
.getLength())
268 Reference
<XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
269 Sequence
<Any
> aArguments
{
270 makeAny(NamedValue("Binary", makeAny(true))) };
271 xPackageEncryption
.set(
272 xComponentContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
273 "com.sun.star.comp.oox.crypto." + sCryptoType
, aArguments
, xComponentContext
), UNO_QUERY
);
275 if (xPackageEncryption
.is())
277 // We have an encryptor. Export document into memory stream and encrypt it later
278 pMediaStrm
= std::make_shared
<SvMemoryStream
>();
279 pOutputStrm
= pMediaStrm
.get();
281 // Temp removal of EncryptionData to avoid password protection triggering
282 mrMedium
.GetItemSet()->ClearItem(SID_ENCRYPTIONDATA
);
287 tools::SvRef
<SotStorage
> xStorRef
= new SotStorage(pOutputStrm
, false);
291 bRet
= PPTExport(aProperties
, xStorRef
, mxModel
, mxStatusIndicator
, pBas
, nCnvrtFlags
);
294 if (xPackageEncryption
.is())
296 // Perform DRM encryption
297 pOutputStrm
->Seek(0);
299 xPackageEncryption
->setupEncryption(aEncryptionData
);
301 Reference
<css::io::XInputStream
> xInputStream(new utl::OSeekableInputStreamWrapper(pOutputStrm
, false));
302 Sequence
<NamedValue
> aStreams
= xPackageEncryption
->encrypt(xInputStream
);
304 tools::SvRef
<SotStorage
> xEncryptedRootStrg
= new SotStorage(mrMedium
.GetOutStream(), false);
305 for (const NamedValue
& aStreamData
: std::as_const(aStreams
))
307 // To avoid long paths split and open substorages recursively
308 // Splitting paths manually, since comphelper::string::split is trimming special characters like \0x01, \0x09
309 tools::SvRef
<SotStorage
> pStorage
= xEncryptedRootStrg
.get();
314 OUString sPathElem
= aStreamData
.Name
.getToken(0, L
'/', idx
);
315 if (!sPathElem
.isEmpty())
319 sFileName
= sPathElem
;
323 pStorage
= pStorage
->OpenSotStorage(sPathElem
);
326 } while (pStorage
&& idx
>= 0);
334 tools::SvRef
<SotStorageStream
> pStream
= pStorage
->OpenSotStream(sFileName
);
340 Sequence
<sal_Int8
> aStreamContent
;
341 aStreamData
.Value
>>= aStreamContent
;
342 size_t nBytesWritten
= pStream
->WriteBytes(aStreamContent
.getConstArray(), aStreamContent
.getLength());
343 if (nBytesWritten
!= static_cast<size_t>(aStreamContent
.getLength()))
349 xEncryptedRootStrg
->Commit();
351 // Restore encryption data
352 mrMedium
.GetItemSet()->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA
, makeAny(aEncryptionData
)));
361 void SdPPTFilter::PreSaveBasic()
363 const SvtFilterOptions
& rFilterOptions
= SvtFilterOptions::Get();
364 if( rFilterOptions
.IsLoadPPointBasicStorage() )
366 #ifdef DISABLE_DYNLOADING
367 SaveVBAPointer pSaveVBA
= SaveVBA
;
369 SaveVBAPointer pSaveVBA
= reinterpret_cast< SaveVBAPointer
>(
370 SdFilter::GetLibrarySymbol(mrMedium
.GetFilter()->GetUserData(), "SaveVBA"));
373 pSaveVBA( static_cast<SfxObjectShell
&>(mrDocShell
), pBas
);
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */