Avoid potential negative array index access to cached text.
[LibreOffice.git] / embedserv / source / embed / ed_idataobj.cxx
blob84886e63cb4a38e18a58c2ca9f3e39ce80d9644f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <embeddoc.hxx>
22 #include <com/sun/star/uno/Any.h>
23 #include <com/sun/star/uno/Exception.hpp>
24 #include <com/sun/star/datatransfer/XTransferable.hpp>
27 #include <osl/thread.h>
29 using namespace ::com::sun::star;
32 // EmbedDocument_Impl
35 sal_uInt64 EmbedDocument_Impl::getMetaFileHandle_Impl( bool isEnhMeta )
37 sal_uInt64 pResult = 0;
39 uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetDocument(), uno::UNO_QUERY );
40 if ( xTransferable.is() )
42 datatransfer::DataFlavor aFlavor;
44 if ( isEnhMeta )
46 aFlavor.MimeType = "application/x-openoffice-emf;windows_formatname=\"Image EMF\"";
47 aFlavor.HumanPresentableName = "Enhanced Windows MetaFile";
49 else
51 aFlavor.MimeType = "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";
52 aFlavor.HumanPresentableName = "Windows GDIMetaFile";
55 aFlavor.DataType = cppu::UnoType<sal_uInt64>::get();
57 uno::Any aAny = xTransferable->getTransferData( aFlavor );
58 aAny >>= pResult;
61 return pResult;
65 // IDataObject
67 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
69 if ( !pFormatetc )
70 return DV_E_FORMATETC;
72 if ( !pMedium )
73 return STG_E_MEDIUMFULL;
75 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
76 || pFormatetc->dwAspect == DVASPECT_ICON
77 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
78 return DV_E_DVASPECT;
80 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
82 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
83 return DV_E_TYMED;
85 HENHMETAFILE hMeta = reinterpret_cast<HENHMETAFILE>( getMetaFileHandle_Impl( true ) );
87 if ( hMeta )
89 pMedium->tymed = TYMED_ENHMF;
90 pMedium->hEnhMetaFile = hMeta;
91 pMedium->pUnkForRelease = nullptr;
93 return S_OK;
96 return STG_E_MEDIUMFULL;
98 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
100 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
101 return DV_E_TYMED;
103 HGLOBAL hMeta = reinterpret_cast<HGLOBAL>( getMetaFileHandle_Impl( false ) );
105 if ( hMeta )
107 pMedium->tymed = TYMED_MFPICT;
108 pMedium->hMetaFilePict = hMeta;
109 pMedium->pUnkForRelease = nullptr;
111 return S_OK;
114 return STG_E_MEDIUMFULL;
116 else
118 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
119 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
120 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
122 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
123 return DV_E_TYMED;
125 CComPtr< IStorage > pNewStg;
126 HRESULT hr = StgCreateDocfile( nullptr, STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE, 0, &pNewStg );
127 if ( FAILED( hr ) || !pNewStg ) return STG_E_MEDIUMFULL;
129 hr = SaveTo_Impl( pNewStg );
130 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
132 pMedium->tymed = TYMED_ISTORAGE;
133 pMedium->pstg = pNewStg;
134 pMedium->pstg->AddRef();
135 pMedium->pUnkForRelease = pNewStg;
137 return S_OK;
141 return DV_E_FORMATETC;
144 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
146 if ( !pFormatetc )
147 return DV_E_FORMATETC;
149 if ( !pMedium )
150 return STG_E_MEDIUMFULL;
152 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
153 || pFormatetc->dwAspect == DVASPECT_ICON
154 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
155 return DV_E_DVASPECT;
157 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
158 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
160 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
162 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
163 return DV_E_TYMED;
165 if ( !pMedium->pstg ) return STG_E_MEDIUMFULL;
167 HRESULT hr = SaveTo_Impl( pMedium->pstg );
168 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
170 pMedium->tymed = TYMED_ISTORAGE;
171 pMedium->pUnkForRelease = nullptr;
173 return S_OK;
176 return DV_E_FORMATETC;
179 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
181 if ( pFormatetc )
183 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
184 || pFormatetc->dwAspect == DVASPECT_ICON
185 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
186 return DV_E_DVASPECT;
188 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
190 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
191 return DV_E_TYMED;
193 return S_OK;
195 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
197 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
198 return DV_E_TYMED;
200 return S_OK;
202 else
204 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
205 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
206 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
208 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
209 return DV_E_TYMED;
211 return S_OK;
216 return DV_E_FORMATETC;
220 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
222 if ( !pFormatetcIn || !pFormatetcOut )
223 return DV_E_FORMATETC;
225 pFormatetcOut->ptd = nullptr;
226 pFormatetcOut->cfFormat = pFormatetcIn->cfFormat;
227 pFormatetcOut->dwAspect = DVASPECT_CONTENT;
229 if ( pFormatetcIn->cfFormat == CF_ENHMETAFILE )
231 pFormatetcOut->tymed = TYMED_ENHMF;
232 return S_OK;
234 else if ( pFormatetcIn->cfFormat == CF_METAFILEPICT )
236 pFormatetcOut->tymed = TYMED_MFPICT;
237 return S_OK;
239 else
241 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
242 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
243 if ( pFormatetcIn->cfFormat == cf_embSource || pFormatetcIn->cfFormat == cf_embObj )
245 pFormatetcOut->tymed = TYMED_ISTORAGE;
246 return S_OK;
250 return DV_E_FORMATETC;
253 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::SetData( FORMATETC * /*pFormatetc*/, STGMEDIUM * /*pMedium*/, BOOL /*fRelease*/ )
255 return E_NOTIMPL;
258 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** /*ppFormatetc*/ )
260 if ( dwDirection == DATADIR_GET )
261 return OLE_S_USEREG;
263 return E_NOTIMPL;
266 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
268 if ( !m_pDAdviseHolder )
269 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
270 return E_OUTOFMEMORY;
272 return m_pDAdviseHolder->Advise( static_cast<IDataObject*>(this), pFormatetc, advf, pAdvSink, pdwConnection );
275 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::DUnadvise( DWORD dwConnection )
277 if ( !m_pDAdviseHolder )
278 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
279 return E_OUTOFMEMORY;
281 return m_pDAdviseHolder->Unadvise( dwConnection );
284 COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
286 if ( !m_pDAdviseHolder )
287 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
288 return E_OUTOFMEMORY;
290 return m_pDAdviseHolder->EnumAdvise( ppenumAdvise );
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */