LibreOffice Module sd (master) 1
sdpptwrp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sfx2/docfile.hxx>
21#include <sfx2/docfilt.hxx>
22#include <sfx2/sfxsids.hrc>
24#include <svx/svxerr.hxx>
25#include <unotools/fltrcfg.hxx>
27#include <sot/storage.hxx>
30
31#include <com/sun/star/packages/XPackageEncryption.hpp>
32#include <com/sun/star/uno/XComponentContext.hpp>
33
34#include <sdpptwrp.hxx>
35#include <DrawDocShell.hxx>
36#include <sfx2/frame.hxx>
37
38using namespace ::com::sun::star::uno;
39using namespace ::com::sun::star::beans;
40using namespace ::com::sun::star::task;
41using namespace ::com::sun::star::frame;
42
44 SdFilter( rMedium, rDocShell ),
45 pBas ( nullptr )
46{
47}
48
50{
51 delete pBas; // deleting the compressed basic storage
52}
53
54static void lcl_getListOfStreams(SotStorage * pStorage, comphelper::SequenceAsHashMap& aStreamsData, std::u16string_view sPrefix)
55{
56 SvStorageInfoList aElements;
57 pStorage->FillInfoList(&aElements);
58 for (const auto & aElement : aElements)
59 {
60 OUString sStreamFullName = sPrefix.size() ? OUString::Concat(sPrefix) + "/" + aElement.GetName() : aElement.GetName();
61 if (aElement.IsStorage())
62 {
63 tools::SvRef<SotStorage> xSubStorage = pStorage->OpenSotStorage(aElement.GetName(), StreamMode::STD_READ | StreamMode::SHARE_DENYALL);
64 lcl_getListOfStreams(xSubStorage.get(), aStreamsData, sStreamFullName);
65 }
66 else
67 {
68 // Read stream
69 tools::SvRef<SotStorageStream> rStream = pStorage->OpenSotStream(aElement.GetName(), StreamMode::READ | StreamMode::SHARE_DENYALL);
70 if (rStream.is())
71 {
72 sal_Int32 nStreamSize = rStream->GetSize();
74 oData.realloc(nStreamSize);
75 sal_Int32 nReadBytes = rStream->ReadBytes(oData.getArray(), nStreamSize);
76 if (nStreamSize == nReadBytes)
77 aStreamsData[sStreamFullName] <<= oData;
78 }
79 }
80 }
81}
82
83static tools::SvRef<SotStorage> lcl_DRMDecrypt(const SfxMedium& rMedium, const tools::SvRef<SotStorage>& rStorage, std::shared_ptr<SvStream>& rNewStorageStrm)
84{
85 tools::SvRef<SotStorage> aNewStorage;
86
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);
93
94 if (!xPackageEncryption.is())
95 {
96 // We do not know how to decrypt this
97 return aNewStorage;
98 }
99
101 lcl_getListOfStreams(rStorage.get(), aStreamsData, u"");
102
103 try {
104 Sequence<NamedValue> aStreams = aStreamsData.getAsConstNamedValueList();
105 if (!xPackageEncryption->readEncryptionInfo(aStreams))
106 {
107 // We failed with decryption
108 return aNewStorage;
109 }
110
111 tools::SvRef<SotStorageStream> rContentStream = rStorage->OpenSotStream("\011DRMContent", StreamMode::READ | StreamMode::SHARE_DENYALL);
112 if (!rContentStream.is())
113 {
114 return aNewStorage;
115 }
116
117 rNewStorageStrm = std::make_shared<SvMemoryStream>();
118
119 Reference<css::io::XInputStream > xInputStream(new utl::OSeekableInputStreamWrapper(rContentStream.get(), false));
120 Reference<css::io::XOutputStream > xDecryptedStream(new utl::OSeekableOutputStreamWrapper(*rNewStorageStrm));
121
122 if (!xPackageEncryption->decrypt(xInputStream, xDecryptedStream))
123 {
124 // We failed with decryption
125 return aNewStorage;
126 }
127
128 rNewStorageStrm->Seek(0);
129
130 // Further reading is done from new document
131 aNewStorage = new SotStorage(*rNewStorageStrm);
132
133 // Set the media descriptor data
134 Sequence<NamedValue> aEncryptionData = xPackageEncryption->createEncryptionData("");
135 rMedium.GetItemSet().Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, Any(aEncryptionData)));
136 }
137 catch (const std::exception&)
138 {
139 return aNewStorage;
140 }
141
142 return aNewStorage;
143}
144
146{
147 bool bRet = false;
148 std::shared_ptr<SvStream> aDecryptedStorageStrm;
149 tools::SvRef<SotStorage> pStorage = new SotStorage( mrMedium.GetInStream(), false );
150 if( !pStorage->GetError() )
151 {
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 ) )
157 {
158 xDualStorage = pStorage->OpenSotStorage( sDualStorage, StreamMode::STD_READ );
159 pStorage = xDualStorage;
160 }
161 if (pStorage->IsContained("\011DRMContent"))
162 {
163 // Document is DRM encrypted
164 pStorage = lcl_DRMDecrypt(mrMedium, pStorage, aDecryptedStorageStrm);
165 }
166 tools::SvRef<SotStorageStream> pDocStream(pStorage->OpenSotStream( "PowerPoint Document" , StreamMode::STD_READ ));
167 if( pDocStream )
168 {
169 pDocStream->SetVersion( pStorage->GetVersion() );
170 pDocStream->SetCryptMaskKey(pStorage->GetKey());
171
172 if ( pStorage->IsStream( "EncryptedSummary" ) )
174 else
175 {
176 bRet = ImportPPT( &mrDocument, *pDocStream, *pStorage, mrMedium );
177
178 if ( !bRet )
180 }
181 }
182 }
183
184 return bRet;
185}
186
188{
189 bool bRet = false;
190
191 if( mxModel.is() )
192 {
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;
205
207
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 );
214
215 SvStream * pOutputStrm = mrMedium.GetOutStream();
216
217 Sequence< NamedValue > aEncryptionData;
218 Reference< css::packages::XPackageEncryption > xPackageEncryption;
219 const SfxUnoAnyItem* pEncryptionDataItem = mrMedium.GetItemSet().GetItem(SID_ENCRYPTIONDATA, false);
220 std::shared_ptr<SvStream> pMediaStrm;
221 if (pEncryptionDataItem && (pEncryptionDataItem->GetValue() >>= aEncryptionData))
222 {
223 ::comphelper::SequenceAsHashMap aHashData(aEncryptionData);
224 OUString sCryptoType = aHashData.getUnpackedValueOrDefault("CryptoType", OUString());
225
226 if (sCryptoType.getLength())
227 {
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);
234
235 if (xPackageEncryption.is())
236 {
237 // We have an encryptor. Export document into memory stream and encrypt it later
238 pMediaStrm = std::make_shared<SvMemoryStream>();
239 pOutputStrm = pMediaStrm.get();
240
241 // Temp removal of EncryptionData to avoid password protection triggering
242 mrMedium.GetItemSet().ClearItem(SID_ENCRYPTIONDATA);
243 }
244 }
245 }
246
247 tools::SvRef<SotStorage> xStorRef = new SotStorage(pOutputStrm, false);
248
249 if (xStorRef.is())
250 {
251 bRet = ExportPPT(aProperties, xStorRef, mxModel, mxStatusIndicator, pBas, nCnvrtFlags);
252 xStorRef->Commit();
253
254 if (xPackageEncryption.is())
255 {
256 // Perform DRM encryption
257 pOutputStrm->Seek(0);
258
259 xPackageEncryption->setupEncryption(aEncryptionData);
260
261 Reference<css::io::XInputStream > xInputStream(new utl::OSeekableInputStreamWrapper(pOutputStrm, false));
262 Sequence<NamedValue> aStreams = xPackageEncryption->encrypt(xInputStream);
263
264 tools::SvRef<SotStorage> xEncryptedRootStrg = new SotStorage(mrMedium.GetOutStream(), false);
265 for (const NamedValue & aStreamData : std::as_const(aStreams))
266 {
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();
270 OUString sFileName;
271 sal_Int32 idx = 0;
272 do
273 {
274 OUString sPathElem = aStreamData.Name.getToken(0, L'/', idx);
275 if (!sPathElem.isEmpty())
276 {
277 if (idx < 0)
278 {
279 sFileName = sPathElem;
280 }
281 else
282 {
283 pStorage = pStorage->OpenSotStorage(sPathElem);
284 }
285 }
286 } while (pStorage && idx >= 0);
287
288 if (!pStorage)
289 {
290 bRet = false;
291 break;
292 }
293
294 tools::SvRef<SotStorageStream> pStream = pStorage->OpenSotStream(sFileName);
295 if (!pStream)
296 {
297 bRet = false;
298 break;
299 }
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()))
304 {
305 bRet = false;
306 break;
307 }
308 }
309 xEncryptedRootStrg->Commit();
310
311 // Restore encryption data
312 mrMedium.GetItemSet().Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, Any(aEncryptionData)));
313 }
314 }
315 }
316
317 return bRet;
318}
319
321{
322 const SvtFilterOptions& rFilterOptions = SvtFilterOptions::Get();
323 if( rFilterOptions.IsLoadPPointBasicStorage() )
324 {
325 SaveVBA( static_cast<SfxObjectShell&>(mrDocShell), pBas );
326 }
327}
328
329/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
SfxMedium & mrMedium
Definition: sdfilter.hxx:52
::sd::DrawDocShell & mrDocShell
Definition: sdfilter.hxx:53
css::uno::Reference< css::task::XStatusIndicator > mxStatusIndicator
Definition: sdfilter.hxx:50
css::uno::Reference< css::frame::XModel > mxModel
Definition: sdfilter.hxx:49
void CreateStatusIndicator()
Definition: sdfilter.cxx:53
SdDrawDocument & mrDocument
Definition: sdfilter.hxx:54
SvMemoryStream * pBas
Definition: sdpptwrp.hxx:43
bool Export() override
Definition: sdpptwrp.cxx:187
SdPPTFilter(SfxMedium &rMedium, ::sd::DrawDocShell &rDocShell)
Definition: sdpptwrp.cxx:43
bool Import()
these methods are necessary for the export to PowerPoint
Definition: sdpptwrp.cxx:145
virtual ~SdPPTFilter() override
Definition: sdpptwrp.cxx:49
void PreSaveBasic()
restores the original basic storage
Definition: sdpptwrp.cxx:320
sal_uInt16 ClearItem(sal_uInt16 nWhich=0)
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
void SetError(ErrCode nError)
OUString GetBaseURL(bool bForSaving=false)
SfxItemSet & GetItemSet() const
SvStream * GetOutStream()
SvStream * GetInStream()
const css::uno::Any & GetValue() const
void FillInfoList(SvStorageInfoList *) const
tools::SvRef< SotStorageStream > OpenSotStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE)
SotStorage * OpenSotStorage(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE, bool transacted=true)
sal_uInt64 Seek(sal_uInt64 nPos)
bool IsLoadPPointBasicStorage() const
bool IsCalc2Excel() const
static SvtFilterOptions & Get()
bool IsImpress2PowerPoint() const
bool IsWriter2WinWord() const
bool IsEnablePPTPreview() const
bool IsMath2MathType() const
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
css::uno::Sequence< css::beans::NamedValue > getAsConstNamedValueList() const
T * get() const
bool is() const
float u
SAL_DLLPUBLIC_EXPORT bool SaveVBA(SfxObjectShell &rDocShell, SvMemoryStream *&pBas)
Definition: eppt.cxx:1434
SAL_DLLPUBLIC_EXPORT bool ExportPPT(const std::vector< css::beans::PropertyValue > &rMediaData, tools::SvRef< SotStorage > const &rSvStorage, css::uno::Reference< css::frame::XModel > const &rXModel, css::uno::Reference< css::task::XStatusIndicator > const &rXStatInd, SvMemoryStream *pVBA, sal_uInt32 nCnvrtFlags)
Definition: eppt.cxx:1421
#define SVSTREAM_WRONGVERSION
OUString sPrefix
const sal_uInt16 idx[]
Sequence< PropertyValue > aArguments
#define OLE_STARIMPRESS_2_POWERPOINT
#define OLE_STARWRITER_2_WINWORD
#define OLE_STARCALC_2_EXCEL
#define OLE_STARMATH_2_MATHTYPE
Reference< XComponentContext > getProcessComponentContext()
SAL_DLLPUBLIC_EXPORT bool ImportPPT(SdDrawDocument *pDocument, SvStream &rDocStream, SotStorage &rStorage, SfxMedium &rMedium)
Definition: pptin.cxx:2762
static tools::SvRef< SotStorage > lcl_DRMDecrypt(const SfxMedium &rMedium, const tools::SvRef< SotStorage > &rStorage, std::shared_ptr< SvStream > &rNewStorageStrm)
Definition: sdpptwrp.cxx:83
static void lcl_getListOfStreams(SotStorage *pStorage, comphelper::SequenceAsHashMap &aStreamsData, std::u16string_view sPrefix)
Definition: sdpptwrp.cxx:54
std::vector< SvStorageInfo > SvStorageInfoList
#define ERRCODE_SVX_READ_FILTER_PPOINT