File: | home/maarten/src/libreoffice/core/include/rtl/ref.hxx |
Warning: | line 192, column 9 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 | |||||||||
21 | #include <tools/solar.h> | ||||||||
22 | #include <vcl/svapp.hxx> | ||||||||
23 | #include <vcl/gdimtf.hxx> | ||||||||
24 | #include <vcl/graph.hxx> | ||||||||
25 | #include <vcl/metaact.hxx> | ||||||||
26 | #include <vcl/virdev.hxx> | ||||||||
27 | #include <vcl/cvtgrf.hxx> | ||||||||
28 | #include <vcl/BitmapTools.hxx> | ||||||||
29 | #include <unotools/configmgr.hxx> | ||||||||
30 | #include <unotools/tempfile.hxx> | ||||||||
31 | #include <osl/process.h> | ||||||||
32 | #include <osl/file.hxx> | ||||||||
33 | #include <osl/thread.h> | ||||||||
34 | #include <rtl/byteseq.hxx> | ||||||||
35 | #include <sal/log.hxx> | ||||||||
36 | #include <o3tl/char16_t2wchar_t.hxx> | ||||||||
37 | #include <o3tl/safeint.hxx> | ||||||||
38 | #include <memory> | ||||||||
39 | #include <string_view> | ||||||||
40 | |||||||||
41 | class FilterConfigItem; | ||||||||
42 | |||||||||
43 | /************************************************************************* | ||||||||
44 | |* | ||||||||
45 | |* ImpSearchEntry() | ||||||||
46 | |* | ||||||||
47 | |* Description Checks if there is a string(pDest) of length nSize | ||||||||
48 | |* inside the memory area pSource which is nComp bytes long. | ||||||||
49 | |* Check is NON-CASE-SENSITIVE. The return value is the | ||||||||
50 | |* address where the string is found or NULL | ||||||||
51 | |* | ||||||||
52 | *************************************************************************/ | ||||||||
53 | |||||||||
54 | static sal_uInt8* ImplSearchEntry( sal_uInt8* pSource, sal_uInt8 const * pDest, size_t nComp, size_t nSize ) | ||||||||
55 | { | ||||||||
56 | while ( nComp-- >= nSize ) | ||||||||
57 | { | ||||||||
58 | size_t i; | ||||||||
59 | for ( i = 0; i < nSize; i++ ) | ||||||||
60 | { | ||||||||
61 | if ( ( pSource[i]&~0x20 ) != ( pDest[i]&~0x20 ) ) | ||||||||
62 | break; | ||||||||
63 | } | ||||||||
64 | if ( i == nSize ) | ||||||||
65 | return pSource; | ||||||||
66 | pSource++; | ||||||||
67 | } | ||||||||
68 | return nullptr; | ||||||||
69 | } | ||||||||
70 | |||||||||
71 | |||||||||
72 | // SecurityCount is the buffersize of the buffer in which we will parse for a number | ||||||||
73 | static long ImplGetNumber(sal_uInt8* &rBuf, sal_uInt32& nSecurityCount) | ||||||||
74 | { | ||||||||
75 | bool bValid = true; | ||||||||
76 | bool bNegative = false; | ||||||||
77 | long nRetValue = 0; | ||||||||
78 | while (nSecurityCount && (*rBuf == ' ' || *rBuf == 0x9)) | ||||||||
79 | { | ||||||||
80 | ++rBuf; | ||||||||
81 | --nSecurityCount; | ||||||||
82 | } | ||||||||
83 | while ( nSecurityCount && ( *rBuf != ' ' ) && ( *rBuf != 0x9 ) && ( *rBuf != 0xd ) && ( *rBuf != 0xa ) ) | ||||||||
84 | { | ||||||||
85 | switch ( *rBuf ) | ||||||||
86 | { | ||||||||
87 | case '.' : | ||||||||
88 | // we'll only use the integer format | ||||||||
89 | bValid = false; | ||||||||
90 | break; | ||||||||
91 | case '-' : | ||||||||
92 | bNegative = true; | ||||||||
93 | break; | ||||||||
94 | default : | ||||||||
95 | if ( ( *rBuf < '0' ) || ( *rBuf > '9' ) ) | ||||||||
96 | nSecurityCount = 1; // error parsing the bounding box values | ||||||||
97 | else if ( bValid ) | ||||||||
98 | { | ||||||||
99 | const bool bFail = o3tl::checked_multiply<long>(nRetValue, 10, nRetValue) || | ||||||||
100 | o3tl::checked_add<long>(nRetValue, *rBuf - '0', nRetValue); | ||||||||
101 | if (bFail) | ||||||||
102 | return 0; | ||||||||
103 | } | ||||||||
104 | break; | ||||||||
105 | } | ||||||||
106 | nSecurityCount--; | ||||||||
107 | ++rBuf; | ||||||||
108 | } | ||||||||
109 | if ( bNegative ) | ||||||||
110 | nRetValue = -nRetValue; | ||||||||
111 | return nRetValue; | ||||||||
112 | } | ||||||||
113 | |||||||||
114 | |||||||||
115 | static int ImplGetLen( sal_uInt8* pBuf, int nMax ) | ||||||||
116 | { | ||||||||
117 | int nLen = 0; | ||||||||
118 | while( nLen != nMax ) | ||||||||
119 | { | ||||||||
120 | sal_uInt8 nDat = *pBuf++; | ||||||||
121 | if ( nDat == 0x0a || nDat == 0x25 ) | ||||||||
122 | break; | ||||||||
123 | nLen++; | ||||||||
124 | } | ||||||||
125 | return nLen; | ||||||||
126 | } | ||||||||
127 | |||||||||
128 | static void MakeAsMeta(Graphic &rGraphic) | ||||||||
129 | { | ||||||||
130 | ScopedVclPtrInstance< VirtualDevice > pVDev; | ||||||||
131 | GDIMetaFile aMtf; | ||||||||
132 | Size aSize = rGraphic.GetPrefSize(); | ||||||||
133 | |||||||||
134 | if( !aSize.Width() || !aSize.Height() ) | ||||||||
135 | aSize = Application::GetDefaultDevice()->PixelToLogic( | ||||||||
136 | rGraphic.GetSizePixel(), MapMode(MapUnit::Map100thMM)); | ||||||||
137 | else | ||||||||
138 | aSize = OutputDevice::LogicToLogic( aSize, | ||||||||
139 | rGraphic.GetPrefMapMode(), MapMode(MapUnit::Map100thMM)); | ||||||||
140 | |||||||||
141 | pVDev->EnableOutput( false ); | ||||||||
142 | aMtf.Record( pVDev ); | ||||||||
143 | pVDev->DrawBitmapEx( Point(), aSize, rGraphic.GetBitmapEx() ); | ||||||||
144 | aMtf.Stop(); | ||||||||
145 | aMtf.WindStart(); | ||||||||
146 | aMtf.SetPrefMapMode(MapMode(MapUnit::Map100thMM)); | ||||||||
147 | aMtf.SetPrefSize( aSize ); | ||||||||
148 | rGraphic = aMtf; | ||||||||
149 | } | ||||||||
150 | |||||||||
151 | static oslProcessError runProcessWithPathSearch(const OUString &rProgName, | ||||||||
152 | rtl_uString* pArgs[], sal_uInt32 nArgs, oslProcess *pProcess, | ||||||||
153 | oslFileHandle *pIn, oslFileHandle *pOut, oslFileHandle *pErr) | ||||||||
154 | { | ||||||||
155 | oslProcessError result = osl_Process_E_None; | ||||||||
156 | oslSecurity pSecurity = osl_getCurrentSecurity(); | ||||||||
157 | #ifdef _WIN32 | ||||||||
158 | /* | ||||||||
159 | * ooo#72096 | ||||||||
160 | * On Window the underlying SearchPath searches in order of... | ||||||||
161 | * The directory from which the application loaded. | ||||||||
162 | * The current directory. | ||||||||
163 | * The Windows system directory. | ||||||||
164 | * The Windows directory. | ||||||||
165 | * The directories that are listed in the PATH environment variable. | ||||||||
166 | * | ||||||||
167 | * Because one of our programs is called "convert" and there is a convert | ||||||||
168 | * in the windows system directory, we want to explicitly search the PATH | ||||||||
169 | * to avoid picking up on that one if ImageMagick's convert precedes it in | ||||||||
170 | * PATH. | ||||||||
171 | * | ||||||||
172 | */ | ||||||||
173 | OUString url; | ||||||||
174 | OUString path(o3tl::toU(_wgetenv(L"PATH"))); | ||||||||
175 | |||||||||
176 | oslFileError err = osl_searchFileURL(rProgName.pData, path.pData, &url.pData); | ||||||||
177 | if (err != osl_File_E_None) | ||||||||
178 | result = osl_Process_E_NotFound; | ||||||||
179 | else | ||||||||
180 | result = osl_executeProcess_WithRedirectedIO(url.pData, | ||||||||
181 | pArgs, nArgs, osl_Process_HIDDEN0x0010, | ||||||||
182 | pSecurity, nullptr, nullptr, 0, pProcess, pIn, pOut, pErr); | ||||||||
183 | #else | ||||||||
184 | result = osl_executeProcess_WithRedirectedIO(rProgName.pData, | ||||||||
185 | pArgs, nArgs, osl_Process_SEARCHPATH0x0002 | osl_Process_HIDDEN0x0010, | ||||||||
186 | pSecurity, nullptr, nullptr, 0, pProcess, pIn, pOut, pErr); | ||||||||
187 | #endif | ||||||||
188 | osl_freeSecurityHandle( pSecurity ); | ||||||||
189 | return result; | ||||||||
190 | } | ||||||||
191 | |||||||||
192 | #if defined(_WIN32) | ||||||||
193 | # define EXESUFFIX"" ".exe" | ||||||||
194 | #else | ||||||||
195 | # define EXESUFFIX"" "" | ||||||||
196 | #endif | ||||||||
197 | |||||||||
198 | static bool RenderAsEMF(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic) | ||||||||
199 | { | ||||||||
200 | utl::TempFile aTempOutput; | ||||||||
201 | utl::TempFile aTempInput; | ||||||||
202 | aTempOutput.EnableKillingFile(); | ||||||||
203 | aTempInput.EnableKillingFile(); | ||||||||
204 | OUString output; | ||||||||
205 | osl::FileBase::getSystemPathFromFileURL(aTempOutput.GetURL(), output); | ||||||||
206 | OUString input; | ||||||||
207 | osl::FileBase::getSystemPathFromFileURL(aTempInput.GetURL(), input); | ||||||||
208 | |||||||||
209 | SvStream* pInputStream = aTempInput.GetStream(StreamMode::WRITE); | ||||||||
210 | sal_uInt64 nCount = pInputStream->WriteBytes(pBuf, nBytesRead); | ||||||||
211 | aTempInput.CloseStream(); | ||||||||
212 | |||||||||
213 | //fdo#64161 pstoedit under non-windows uses libEMF to output the EMF, but | ||||||||
214 | //libEMF cannot calculate the bounding box of text, so the overall bounding | ||||||||
215 | //box is not increased to include that of any text in the eps | ||||||||
216 | // | ||||||||
217 | //-drawbb will force pstoedit to draw a pair of pixels with the bg color to | ||||||||
218 | //the topleft and bottom right of the bounding box as pstoedit sees it, | ||||||||
219 | //which libEMF will then extend its bounding box to fit | ||||||||
220 | // | ||||||||
221 | //-usebbfrominput forces pstoedit to take the original ps bounding box | ||||||||
222 | //as the bounding box as it sees it, instead of calculating its own | ||||||||
223 | //which also doesn't work for this example | ||||||||
224 | // | ||||||||
225 | //Under Linux, positioning of letters within pstoedit is very approximate. | ||||||||
226 | //Using the -nfw option delegates the positioning to the reader, and we | ||||||||
227 | //will do a proper job. The option is ignored on Windows. | ||||||||
228 | OUString arg1("-usebbfrominput"); //-usebbfrominput use the original ps bounding box | ||||||||
229 | OUString arg2("-f"); | ||||||||
230 | OUString arg3("emf:-OO -drawbb -nfw"); //-drawbb mark out the bounding box extent with bg pixels | ||||||||
231 | //-nfw delegate letter placement to us | ||||||||
232 | rtl_uString *args[] = | ||||||||
233 | { | ||||||||
234 | arg1.pData, arg2.pData, arg3.pData, input.pData, output.pData | ||||||||
235 | }; | ||||||||
236 | oslProcess aProcess; | ||||||||
237 | oslFileHandle pIn = nullptr; | ||||||||
238 | oslFileHandle pOut = nullptr; | ||||||||
239 | oslFileHandle pErr = nullptr; | ||||||||
240 | oslProcessError eErr = runProcessWithPathSearch( | ||||||||
241 | "pstoedit" EXESUFFIX"", | ||||||||
242 | args, SAL_N_ELEMENTS(args)(sizeof(sal_n_array_size(args))), | ||||||||
243 | &aProcess, &pIn, &pOut, &pErr); | ||||||||
244 | |||||||||
245 | if (eErr!=osl_Process_E_None) | ||||||||
246 | return false; | ||||||||
247 | |||||||||
248 | bool bRet = false; | ||||||||
249 | if (pIn) osl_closeFile(pIn); | ||||||||
250 | osl_joinProcess(aProcess); | ||||||||
251 | osl_freeProcessHandle(aProcess); | ||||||||
252 | bool bEMFSupported=true; | ||||||||
253 | if (pOut) | ||||||||
254 | { | ||||||||
255 | rtl::ByteSequence seq; | ||||||||
256 | if (osl_File_E_None == osl_readLine(pOut, reinterpret_cast<sal_Sequence **>(&seq))) | ||||||||
257 | { | ||||||||
258 | OString line( reinterpret_cast<const char *>(seq.getConstArray()), seq.getLength() ); | ||||||||
259 | if (line.startsWith("Unsupported output format")) | ||||||||
260 | bEMFSupported=false; | ||||||||
261 | } | ||||||||
262 | osl_closeFile(pOut); | ||||||||
263 | } | ||||||||
264 | if (pErr) osl_closeFile(pErr); | ||||||||
265 | if (nCount == nBytesRead && bEMFSupported) | ||||||||
266 | { | ||||||||
267 | SvFileStream aFile(output, StreamMode::READ); | ||||||||
268 | if (GraphicConverter::Import(aFile, rGraphic, ConvertDataFormat::EMF) == ERRCODE_NONEErrCode(0)) | ||||||||
269 | bRet = true; | ||||||||
270 | } | ||||||||
271 | |||||||||
272 | return bRet; | ||||||||
273 | } | ||||||||
274 | |||||||||
275 | namespace { | ||||||||
276 | |||||||||
277 | struct WriteData | ||||||||
278 | { | ||||||||
279 | oslFileHandle m_pFile; | ||||||||
280 | const sal_uInt8 *m_pBuf; | ||||||||
281 | sal_uInt32 m_nBytesToWrite; | ||||||||
282 | }; | ||||||||
283 | |||||||||
284 | } | ||||||||
285 | |||||||||
286 | extern "C" { | ||||||||
287 | |||||||||
288 | static void WriteFileInThread(void *wData) | ||||||||
289 | { | ||||||||
290 | sal_uInt64 nCount; | ||||||||
291 | WriteData *wdata = static_cast<WriteData *>(wData); | ||||||||
292 | osl_writeFile(wdata->m_pFile, wdata->m_pBuf, wdata->m_nBytesToWrite, &nCount); | ||||||||
293 | // The number of bytes written does not matter. | ||||||||
294 | // The helper process may close its input stream before reading it all. | ||||||||
295 | // (e.g. at "showpage" in EPS) | ||||||||
296 | |||||||||
297 | // File must be closed here. | ||||||||
298 | // Otherwise, the helper process may wait for the next input, | ||||||||
299 | // then its stdout is not closed and osl_readFile() blocks. | ||||||||
300 | if (wdata->m_pFile) osl_closeFile(wdata->m_pFile); | ||||||||
301 | } | ||||||||
302 | |||||||||
303 | } | ||||||||
304 | |||||||||
305 | static bool RenderAsBMPThroughHelper(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, | ||||||||
306 | Graphic& rGraphic, | ||||||||
307 | std::initializer_list<std::u16string_view> aProgNames, | ||||||||
308 | rtl_uString* pArgs[], size_t nArgs) | ||||||||
309 | { | ||||||||
310 | oslProcess aProcess = nullptr; | ||||||||
311 | oslFileHandle pIn = nullptr; | ||||||||
312 | oslFileHandle pOut = nullptr; | ||||||||
313 | oslFileHandle pErr = nullptr; | ||||||||
314 | oslProcessError eErr = osl_Process_E_Unknown; | ||||||||
315 | for (const auto& rProgName : aProgNames) | ||||||||
316 | { | ||||||||
317 | eErr = runProcessWithPathSearch(rProgName, pArgs, nArgs, &aProcess, &pIn, &pOut, &pErr); | ||||||||
318 | if (eErr == osl_Process_E_None) | ||||||||
319 | break; | ||||||||
320 | } | ||||||||
321 | if (eErr!=osl_Process_E_None) | ||||||||
322 | return false; | ||||||||
323 | |||||||||
324 | WriteData Data; | ||||||||
325 | Data.m_pFile = pIn; | ||||||||
326 | Data.m_pBuf = pBuf; | ||||||||
327 | Data.m_nBytesToWrite = nBytesRead; | ||||||||
328 | oslThread hThread = osl_createThread(WriteFileInThread, &Data); | ||||||||
329 | |||||||||
330 | bool bRet = false; | ||||||||
331 | sal_uInt64 nCount; | ||||||||
332 | { | ||||||||
333 | SvMemoryStream aMemStm; | ||||||||
334 | sal_uInt8 aBuf[32000]; | ||||||||
335 | oslFileError eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount); | ||||||||
336 | while (eFileErr == osl_File_E_None && nCount) | ||||||||
337 | { | ||||||||
338 | aMemStm.WriteBytes(aBuf, sal::static_int_cast<std::size_t>(nCount)); | ||||||||
339 | eFileErr = osl_readFile(pOut, aBuf, 32000, &nCount); | ||||||||
340 | } | ||||||||
341 | |||||||||
342 | aMemStm.Seek(0); | ||||||||
343 | if ( | ||||||||
344 | aMemStm.GetEndOfData() && | ||||||||
345 | GraphicConverter::Import(aMemStm, rGraphic, ConvertDataFormat::BMP) == ERRCODE_NONEErrCode(0) | ||||||||
346 | ) | ||||||||
347 | { | ||||||||
348 | MakeAsMeta(rGraphic); | ||||||||
349 | bRet = true; | ||||||||
350 | } | ||||||||
351 | } | ||||||||
352 | if (pOut) osl_closeFile(pOut); | ||||||||
353 | if (pErr) osl_closeFile(pErr); | ||||||||
354 | osl_joinProcess(aProcess); | ||||||||
355 | osl_freeProcessHandle(aProcess); | ||||||||
356 | osl_joinWithThread(hThread); | ||||||||
357 | osl_destroyThread(hThread); | ||||||||
358 | return bRet; | ||||||||
359 | } | ||||||||
360 | |||||||||
361 | static bool RenderAsBMPThroughConvert(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, | ||||||||
362 | Graphic &rGraphic) | ||||||||
363 | { | ||||||||
364 | // density in pixel/inch | ||||||||
365 | OUString arg1("-density"); | ||||||||
366 | // since the preview is also used for PDF-Export & printing on non-PS-printers, | ||||||||
367 | // use some better quality - 300x300 should allow some resizing as well | ||||||||
368 | OUString arg2("300x300"); | ||||||||
369 | // read eps from STDIN | ||||||||
370 | OUString arg3("eps:-"); | ||||||||
371 | // write bmp to STDOUT | ||||||||
372 | OUString arg4("bmp:-"); | ||||||||
373 | rtl_uString *args[] = | ||||||||
374 | { | ||||||||
375 | arg1.pData, arg2.pData, arg3.pData, arg4.pData | ||||||||
376 | }; | ||||||||
377 | return RenderAsBMPThroughHelper(pBuf, nBytesRead, rGraphic, | ||||||||
378 | { u"convert" EXESUFFIX"" }, | ||||||||
379 | args, | ||||||||
380 | SAL_N_ELEMENTS(args)(sizeof(sal_n_array_size(args)))); | ||||||||
381 | } | ||||||||
382 | |||||||||
383 | static bool RenderAsBMPThroughGS(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, | ||||||||
384 | Graphic &rGraphic) | ||||||||
385 | { | ||||||||
386 | OUString arg1("-q"); | ||||||||
387 | OUString arg2("-dBATCH"); | ||||||||
388 | OUString arg3("-dNOPAUSE"); | ||||||||
389 | OUString arg4("-dPARANOIDSAFER"); | ||||||||
390 | OUString arg5("-dEPSCrop"); | ||||||||
391 | OUString arg6("-dTextAlphaBits=4"); | ||||||||
392 | OUString arg7("-dGraphicsAlphaBits=4"); | ||||||||
393 | OUString arg8("-r300x300"); | ||||||||
394 | OUString arg9("-sDEVICE=bmp16m"); | ||||||||
395 | OUString arg10("-sOutputFile=-"); | ||||||||
396 | OUString arg11("-"); | ||||||||
397 | rtl_uString *args[] = | ||||||||
398 | { | ||||||||
399 | arg1.pData, arg2.pData, arg3.pData, arg4.pData, arg5.pData, | ||||||||
400 | arg6.pData, arg7.pData, arg8.pData, arg9.pData, arg10.pData, | ||||||||
401 | arg11.pData | ||||||||
402 | }; | ||||||||
403 | return RenderAsBMPThroughHelper(pBuf, nBytesRead, rGraphic, | ||||||||
404 | #ifdef _WIN32 | ||||||||
405 | // Try both 32-bit and 64-bit ghostscript executable name | ||||||||
406 | { | ||||||||
407 | u"gswin32c" EXESUFFIX"", | ||||||||
408 | u"gswin64c" EXESUFFIX"", | ||||||||
409 | }, | ||||||||
410 | #else | ||||||||
411 | { u"gs" EXESUFFIX"" }, | ||||||||
412 | #endif | ||||||||
413 | args, | ||||||||
414 | SAL_N_ELEMENTS(args)(sizeof(sal_n_array_size(args)))); | ||||||||
415 | } | ||||||||
416 | |||||||||
417 | static bool RenderAsBMP(const sal_uInt8* pBuf, sal_uInt32 nBytesRead, Graphic &rGraphic) | ||||||||
418 | { | ||||||||
419 | if (RenderAsBMPThroughGS(pBuf, nBytesRead, rGraphic)) | ||||||||
420 | return true; | ||||||||
421 | else | ||||||||
422 | return RenderAsBMPThroughConvert(pBuf, nBytesRead, rGraphic); | ||||||||
423 | } | ||||||||
424 | |||||||||
425 | // this method adds a replacement action containing the original wmf or tiff replacement, | ||||||||
426 | // so the original eps can be written when storing to ODF. | ||||||||
427 | static void CreateMtfReplacementAction( GDIMetaFile& rMtf, SvStream& rStrm, sal_uInt32 nOrigPos, sal_uInt32 nPSSize, | ||||||||
428 | sal_uInt32 nPosWMF, sal_uInt32 nSizeWMF, sal_uInt32 nPosTIFF, sal_uInt32 nSizeTIFF ) | ||||||||
429 | { | ||||||||
430 | OString aComment("EPSReplacementGraphic"); | ||||||||
431 | if ( nSizeWMF || nSizeTIFF ) | ||||||||
432 | { | ||||||||
433 | std::vector<sal_uInt8> aWMFBuf; | ||||||||
434 | if (nSizeWMF && checkSeek(rStrm, nOrigPos + nPosWMF) && rStrm.remainingSize() >= nSizeWMF) | ||||||||
435 | { | ||||||||
436 | aWMFBuf.resize(nSizeWMF); | ||||||||
437 | aWMFBuf.resize(rStrm.ReadBytes(aWMFBuf.data(), nSizeWMF)); | ||||||||
438 | } | ||||||||
439 | nSizeWMF = aWMFBuf.size(); | ||||||||
440 | |||||||||
441 | std::vector<sal_uInt8> aTIFFBuf; | ||||||||
442 | if (nSizeTIFF && checkSeek(rStrm, nOrigPos + nPosTIFF) && rStrm.remainingSize() >= nSizeTIFF) | ||||||||
443 | { | ||||||||
444 | aTIFFBuf.resize(nSizeTIFF); | ||||||||
445 | aTIFFBuf.resize(rStrm.ReadBytes(aTIFFBuf.data(), nSizeTIFF)); | ||||||||
446 | } | ||||||||
447 | nSizeTIFF = aTIFFBuf.size(); | ||||||||
448 | |||||||||
449 | SvMemoryStream aReplacement( nSizeWMF + nSizeTIFF + 28 ); | ||||||||
450 | sal_uInt32 const nMagic = 0xc6d3d0c5; | ||||||||
451 | sal_uInt32 nPPos = 28 + nSizeWMF + nSizeTIFF; | ||||||||
452 | sal_uInt32 nWPos = nSizeWMF ? 28 : 0; | ||||||||
453 | sal_uInt32 nTPos = nSizeTIFF ? 28 + nSizeWMF : 0; | ||||||||
454 | |||||||||
455 | aReplacement.WriteUInt32( nMagic ).WriteUInt32( nPPos ).WriteUInt32( nPSSize ) | ||||||||
456 | .WriteUInt32( nWPos ).WriteUInt32( nSizeWMF ) | ||||||||
457 | .WriteUInt32( nTPos ).WriteUInt32( nSizeTIFF ); | ||||||||
458 | |||||||||
459 | aReplacement.WriteBytes(aWMFBuf.data(), nSizeWMF); | ||||||||
460 | aReplacement.WriteBytes(aTIFFBuf.data(), nSizeTIFF); | ||||||||
461 | rMtf.AddAction( static_cast<MetaAction*>( new MetaCommentAction( aComment, 0, static_cast<const sal_uInt8*>(aReplacement.GetData()), aReplacement.Tell() ) ) ); | ||||||||
462 | } | ||||||||
463 | else | ||||||||
464 | rMtf.AddAction( static_cast<MetaAction*>( new MetaCommentAction( aComment, 0, nullptr, 0 ) ) ); | ||||||||
465 | } | ||||||||
466 | |||||||||
467 | //there is no preview -> make a red box | ||||||||
468 | static void MakePreview(sal_uInt8* pBuf, sal_uInt32 nBytesRead, | ||||||||
469 | long nWidth, long nHeight, Graphic &rGraphic) | ||||||||
470 | { | ||||||||
471 | GDIMetaFile aMtf; | ||||||||
472 | ScopedVclPtrInstance< VirtualDevice > pVDev; | ||||||||
| |||||||||
473 | vcl::Font aFont; | ||||||||
474 | |||||||||
475 | pVDev->EnableOutput( false ); | ||||||||
476 | aMtf.Record( pVDev ); | ||||||||
477 | pVDev->SetLineColor( COL_RED ); | ||||||||
478 | pVDev->SetFillColor(); | ||||||||
479 | |||||||||
480 | aFont.SetColor( COL_LIGHTRED ); | ||||||||
481 | |||||||||
482 | pVDev->Push( PushFlags::FONT ); | ||||||||
483 | pVDev->SetFont( aFont ); | ||||||||
484 | |||||||||
485 | tools::Rectangle aRect( Point( 1, 1 ), Size( nWidth - 2, nHeight - 2 ) ); | ||||||||
486 | pVDev->DrawRect( aRect ); | ||||||||
487 | |||||||||
488 | OUString aString; | ||||||||
489 | int nLen; | ||||||||
490 | sal_uInt8* pDest = ImplSearchEntry( pBuf, reinterpret_cast<sal_uInt8 const *>("%%Title:"), nBytesRead - 32, 8 ); | ||||||||
491 | sal_uInt32 nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf)) : 0; | ||||||||
492 | if (nRemainingBytes
| ||||||||
493 | { | ||||||||
494 | pDest += 8; | ||||||||
495 | nRemainingBytes -= 8; | ||||||||
496 | if (nRemainingBytes && *pDest == ' ') | ||||||||
497 | { | ||||||||
498 | ++pDest; | ||||||||
499 | --nRemainingBytes; | ||||||||
500 | } | ||||||||
501 | nLen = ImplGetLen(pDest, std::min<sal_uInt32>(nRemainingBytes, 32)); | ||||||||
502 | if (o3tl::make_unsigned(nLen) < nRemainingBytes) | ||||||||
503 | { | ||||||||
504 | sal_uInt8 aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0; | ||||||||
505 | if ( strcmp( reinterpret_cast<char*>(pDest), "none" ) != 0 ) | ||||||||
506 | { | ||||||||
507 | const char* pStr = reinterpret_cast<char*>(pDest); | ||||||||
508 | aString += " Title:" + OUString(pStr, strlen(pStr), RTL_TEXTENCODING_ASCII_US(((rtl_TextEncoding) 11))) + "\n"; | ||||||||
509 | } | ||||||||
510 | pDest[ nLen ] = aOldValue; | ||||||||
511 | } | ||||||||
512 | } | ||||||||
513 | pDest = ImplSearchEntry( pBuf, reinterpret_cast<sal_uInt8 const *>("%%Creator:"), nBytesRead - 32, 10 ); | ||||||||
514 | nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf)) : 0; | ||||||||
515 | if (nRemainingBytes
| ||||||||
516 | { | ||||||||
517 | pDest += 10; | ||||||||
518 | nRemainingBytes -= 10; | ||||||||
519 | if (nRemainingBytes && *pDest == ' ') | ||||||||
520 | { | ||||||||
521 | ++pDest; | ||||||||
522 | --nRemainingBytes; | ||||||||
523 | } | ||||||||
524 | nLen = ImplGetLen(pDest, std::min<sal_uInt32>(nRemainingBytes, 32)); | ||||||||
525 | if (o3tl::make_unsigned(nLen) < nRemainingBytes) | ||||||||
526 | { | ||||||||
527 | sal_uInt8 aOldValue(pDest[nLen]); pDest[nLen] = 0; | ||||||||
528 | const char* pStr = reinterpret_cast<char*>(pDest); | ||||||||
529 | aString += " Creator:" + OUString(pStr, strlen(pStr), RTL_TEXTENCODING_ASCII_US(((rtl_TextEncoding) 11))) + "\n"; | ||||||||
530 | pDest[nLen] = aOldValue; | ||||||||
531 | } | ||||||||
532 | } | ||||||||
533 | pDest = ImplSearchEntry( pBuf, reinterpret_cast<sal_uInt8 const *>("%%CreationDate:"), nBytesRead - 32, 15 ); | ||||||||
534 | nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf)) : 0; | ||||||||
535 | if (nRemainingBytes
| ||||||||
536 | { | ||||||||
537 | pDest += 15; | ||||||||
538 | nRemainingBytes -= 15; | ||||||||
539 | if (nRemainingBytes && *pDest == ' ') | ||||||||
540 | { | ||||||||
541 | ++pDest; | ||||||||
542 | --nRemainingBytes; | ||||||||
543 | } | ||||||||
544 | nLen = ImplGetLen(pDest, std::min<sal_uInt32>(nRemainingBytes, 32)); | ||||||||
545 | if (o3tl::make_unsigned(nLen) < nRemainingBytes) | ||||||||
546 | { | ||||||||
547 | sal_uInt8 aOldValue(pDest[ nLen ]); pDest[ nLen ] = 0; | ||||||||
548 | if ( strcmp( reinterpret_cast<char*>(pDest), "none" ) != 0 ) | ||||||||
549 | { | ||||||||
550 | aString += " CreationDate:" + OUString::createFromAscii( reinterpret_cast<char*>(pDest) ) + "\n"; | ||||||||
551 | const char* pStr = reinterpret_cast<char*>(pDest); | ||||||||
552 | aString += " CreationDate:" + OUString(pStr, strlen(pStr), RTL_TEXTENCODING_ASCII_US(((rtl_TextEncoding) 11))) + "\n"; | ||||||||
553 | } | ||||||||
554 | pDest[ nLen ] = aOldValue; | ||||||||
555 | } | ||||||||
556 | } | ||||||||
557 | pDest = ImplSearchEntry( pBuf, reinterpret_cast<sal_uInt8 const *>("%%LanguageLevel:"), nBytesRead - 4, 16 ); | ||||||||
558 | nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf)) : 0; | ||||||||
559 | if (nRemainingBytes
| ||||||||
560 | { | ||||||||
561 | pDest += 16; | ||||||||
562 | nRemainingBytes -= 16; | ||||||||
563 | sal_uInt32 nCount = std::min<sal_uInt32>(nRemainingBytes, 4U); | ||||||||
564 | sal_uInt32 nNumber = ImplGetNumber(pDest, nCount); | ||||||||
565 | if (nCount && nNumber < 10) | ||||||||
566 | { | ||||||||
567 | aString += " LanguageLevel:" + OUString::number( nNumber ); | ||||||||
568 | } | ||||||||
569 | } | ||||||||
570 | pVDev->DrawText( aRect, aString, DrawTextFlags::Clip | DrawTextFlags::MultiLine ); | ||||||||
571 | pVDev->Pop(); | ||||||||
572 | aMtf.Stop(); | ||||||||
573 | aMtf.WindStart(); | ||||||||
574 | aMtf.SetPrefMapMode(MapMode(MapUnit::MapPoint)); | ||||||||
575 | aMtf.SetPrefSize( Size( nWidth, nHeight ) ); | ||||||||
576 | rGraphic = aMtf; | ||||||||
577 | } | ||||||||
578 | |||||||||
579 | //================== GraphicImport - the exported function ================ | ||||||||
580 | |||||||||
581 | |||||||||
582 | extern "C" SAL_DLLPUBLIC_EXPORT__attribute__ ((visibility("default"))) bool | ||||||||
583 | ipsGraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* ) | ||||||||
584 | { | ||||||||
585 | if ( rStream.GetError() ) | ||||||||
586 | return false; | ||||||||
587 | |||||||||
588 | Graphic aGraphic; | ||||||||
589 | bool bRetValue = false; | ||||||||
590 | bool bHasPreview = false; | ||||||||
591 | sal_uInt32 nSignature = 0, nPSStreamPos, nPSSize = 0; | ||||||||
592 | sal_uInt32 nSizeWMF = 0; | ||||||||
593 | sal_uInt32 nPosWMF = 0; | ||||||||
594 | sal_uInt32 nSizeTIFF = 0; | ||||||||
595 | sal_uInt32 nPosTIFF = 0; | ||||||||
596 | sal_uInt32 nOrigPos = nPSStreamPos = rStream.Tell(); | ||||||||
597 | SvStreamEndian nOldFormat = rStream.GetEndian(); | ||||||||
598 | rStream.SetEndian( SvStreamEndian::LITTLE ); | ||||||||
599 | rStream.ReadUInt32( nSignature ); | ||||||||
600 | if ( nSignature == 0xc6d3d0c5 ) | ||||||||
601 | { | ||||||||
602 | rStream.ReadUInt32( nPSStreamPos ).ReadUInt32( nPSSize ).ReadUInt32( nPosWMF ).ReadUInt32( nSizeWMF ); | ||||||||
603 | |||||||||
604 | // first we try to get the metafile grafix | ||||||||
605 | |||||||||
606 | if ( nSizeWMF ) | ||||||||
607 | { | ||||||||
608 | if (nPosWMF && checkSeek(rStream, nOrigPos + nPosWMF)) | ||||||||
609 | { | ||||||||
610 | if (GraphicConverter::Import(rStream, aGraphic, ConvertDataFormat::WMF) == ERRCODE_NONEErrCode(0)) | ||||||||
611 | bHasPreview = bRetValue = true; | ||||||||
612 | } | ||||||||
613 | } | ||||||||
614 | else | ||||||||
615 | { | ||||||||
616 | rStream.ReadUInt32( nPosTIFF ).ReadUInt32( nSizeTIFF ); | ||||||||
617 | |||||||||
618 | // else we have to get the tiff grafix | ||||||||
619 | |||||||||
620 | if (nPosTIFF && nSizeTIFF && checkSeek(rStream, nOrigPos + nPosTIFF)) | ||||||||
621 | { | ||||||||
622 | if ( GraphicConverter::Import( rStream, aGraphic, ConvertDataFormat::TIF ) == ERRCODE_NONEErrCode(0) ) | ||||||||
623 | { | ||||||||
624 | MakeAsMeta(aGraphic); | ||||||||
625 | rStream.Seek( nOrigPos + nPosTIFF ); | ||||||||
626 | bHasPreview = bRetValue = true; | ||||||||
627 | } | ||||||||
628 | } | ||||||||
629 | } | ||||||||
630 | } | ||||||||
631 | else | ||||||||
632 | { | ||||||||
633 | nPSStreamPos = nOrigPos; // no preview available _>so we must get the size manually | ||||||||
634 | nPSSize = rStream.Seek( STREAM_SEEK_TO_END((sal_uInt64) 0xFFFFFFFFFFFFFFFFul) ) - nOrigPos; | ||||||||
635 | } | ||||||||
636 | |||||||||
637 | std::unique_ptr<sal_uInt8[]> pHeader( new sal_uInt8[ 22 ] ); | ||||||||
638 | rStream.Seek( nPSStreamPos ); | ||||||||
639 | rStream.ReadBytes(pHeader.get(), 22); // check PostScript header | ||||||||
640 | bool bOk = ImplSearchEntry(pHeader.get(), reinterpret_cast<sal_uInt8 const *>("%!PS-Adobe"), 10, 10) && | ||||||||
641 | ImplSearchEntry(&pHeader[ 15 ], reinterpret_cast<sal_uInt8 const *>("EPS"), 3, 3); | ||||||||
642 | if (bOk) | ||||||||
643 | { | ||||||||
644 | rStream.Seek(nPSStreamPos); | ||||||||
645 | bOk = rStream.remainingSize() >= nPSSize; | ||||||||
646 | SAL_WARN_IF(!bOk, "filter.eps", "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize() << " remains")do { if (true && (!bOk)) { switch (sal_detail_log_report (::SAL_DETAIL_LOG_LEVEL_WARN, "filter.eps")) { case SAL_DETAIL_LOG_ACTION_IGNORE : break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail ::getResult( ::sal::detail::StreamStart() << "eps claims to be: " << nPSSize << " in size, but only " << rStream .remainingSize() << " remains") == 1) { ::sal_detail_log ( (::SAL_DETAIL_LOG_LEVEL_WARN), ("filter.eps"), ("/home/maarten/src/libreoffice/core/filter/source/graphicfilter/ieps/ieps.cxx" ":" "646" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize() << " remains"), 0); } else { ::std::ostringstream sal_detail_stream ; sal_detail_stream << "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize () << " remains"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN ), ("filter.eps"), ("/home/maarten/src/libreoffice/core/filter/source/graphicfilter/ieps/ieps.cxx" ":" "646" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize() << " remains") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("filter.eps" ), ("/home/maarten/src/libreoffice/core/filter/source/graphicfilter/ieps/ieps.cxx" ":" "646" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize() << " remains"), 0); } else { ::std::ostringstream sal_detail_stream ; sal_detail_stream << "eps claims to be: " << nPSSize << " in size, but only " << rStream.remainingSize () << " remains"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN ), ("filter.eps"), ("/home/maarten/src/libreoffice/core/filter/source/graphicfilter/ieps/ieps.cxx" ":" "646" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | ||||||||
647 | } | ||||||||
648 | if (bOk) | ||||||||
649 | { | ||||||||
650 | std::unique_ptr<sal_uInt8[]> pBuf( new sal_uInt8[ nPSSize ] ); | ||||||||
651 | |||||||||
652 | sal_uInt32 nBufStartPos = rStream.Tell(); | ||||||||
653 | sal_uInt32 nBytesRead = rStream.ReadBytes(pBuf.get(), nPSSize); | ||||||||
654 | if ( nBytesRead == nPSSize ) | ||||||||
655 | { | ||||||||
656 | sal_uInt32 nSecurityCount = 32; | ||||||||
657 | // if there is no tiff/wmf preview, we will parse for a preview in | ||||||||
658 | // the eps prolog | ||||||||
659 | if (!bHasPreview && nBytesRead >= nSecurityCount) | ||||||||
660 | { | ||||||||
661 | sal_uInt8* pDest = ImplSearchEntry( pBuf.get(), reinterpret_cast<sal_uInt8 const *>("%%BeginPreview:"), nBytesRead - nSecurityCount, 15 ); | ||||||||
662 | sal_uInt32 nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf.get())) : 0; | ||||||||
663 | if (nRemainingBytes >= 15) | ||||||||
664 | { | ||||||||
665 | pDest += 15; | ||||||||
666 | nSecurityCount = nRemainingBytes - 15; | ||||||||
667 | long nWidth = ImplGetNumber(pDest, nSecurityCount); | ||||||||
668 | long nHeight = ImplGetNumber(pDest, nSecurityCount); | ||||||||
669 | long nBitDepth = ImplGetNumber(pDest, nSecurityCount); | ||||||||
670 | long nScanLines = ImplGetNumber(pDest, nSecurityCount); | ||||||||
671 | pDest = ImplSearchEntry(pDest, reinterpret_cast<sal_uInt8 const *>("%"), nSecurityCount, 1); // go to the first Scanline | ||||||||
672 | bOk = pDest && nWidth > 0 && nHeight > 0 && ( ( nBitDepth == 1 ) || ( nBitDepth == 8 ) ) && nScanLines; | ||||||||
673 | if (bOk) | ||||||||
674 | { | ||||||||
675 | long nResult; | ||||||||
676 | bOk = !o3tl::checked_multiply(nWidth, nHeight, nResult) && nResult <= SAL_MAX_INT32((sal_Int32) 0x7FFFFFFF)/2/3; | ||||||||
677 | } | ||||||||
678 | if (bOk) | ||||||||
679 | { | ||||||||
680 | rStream.Seek( nBufStartPos + ( pDest - pBuf.get() ) ); | ||||||||
681 | |||||||||
682 | vcl::bitmap::RawBitmap aBitmap( Size( nWidth, nHeight ), 24 ); | ||||||||
683 | { | ||||||||
684 | bool bIsValid = true; | ||||||||
685 | sal_uInt8 nDat = 0; | ||||||||
686 | char nByte; | ||||||||
687 | for (long y = 0; bIsValid && y < nHeight; ++y) | ||||||||
688 | { | ||||||||
689 | int nBitsLeft = 0; | ||||||||
690 | for (long x = 0; x < nWidth; ++x) | ||||||||
691 | { | ||||||||
692 | if ( --nBitsLeft < 0 ) | ||||||||
693 | { | ||||||||
694 | while ( bIsValid && ( nBitsLeft != 7 ) ) | ||||||||
695 | { | ||||||||
696 | rStream.ReadChar(nByte); | ||||||||
697 | bIsValid = rStream.good(); | ||||||||
698 | if (!bIsValid) | ||||||||
699 | break; | ||||||||
700 | switch (nByte) | ||||||||
701 | { | ||||||||
702 | case 0x0a : | ||||||||
703 | if ( --nScanLines < 0 ) | ||||||||
704 | bIsValid = false; | ||||||||
705 | break; | ||||||||
706 | case 0x09 : | ||||||||
707 | case 0x0d : | ||||||||
708 | case 0x20 : | ||||||||
709 | case 0x25 : | ||||||||
710 | break; | ||||||||
711 | default: | ||||||||
712 | { | ||||||||
713 | if ( nByte >= '0' ) | ||||||||
714 | { | ||||||||
715 | if ( nByte > '9' ) | ||||||||
716 | { | ||||||||
717 | nByte &=~0x20; // case none sensitive for hexadecimal values | ||||||||
718 | nByte -= ( 'A' - 10 ); | ||||||||
719 | if ( nByte > 15 ) | ||||||||
720 | bIsValid = false; | ||||||||
721 | } | ||||||||
722 | else | ||||||||
723 | nByte -= '0'; | ||||||||
724 | nBitsLeft += 4; | ||||||||
725 | nDat <<= 4; | ||||||||
726 | nDat |= ( nByte ^ 0xf ); // in epsi a zero bit represents white color | ||||||||
727 | } | ||||||||
728 | else | ||||||||
729 | bIsValid = false; | ||||||||
730 | } | ||||||||
731 | break; | ||||||||
732 | } | ||||||||
733 | } | ||||||||
734 | } | ||||||||
735 | if (!bIsValid) | ||||||||
736 | break; | ||||||||
737 | if ( nBitDepth == 1 ) | ||||||||
738 | aBitmap.SetPixel( y, x, Color(static_cast<sal_uInt8>(nDat >> nBitsLeft) & 1) ); | ||||||||
739 | else | ||||||||
740 | { | ||||||||
741 | aBitmap.SetPixel( y, x, nDat ? COL_WHITE : COL_BLACK ); // nBitDepth == 8 | ||||||||
742 | nBitsLeft = 0; | ||||||||
743 | } | ||||||||
744 | } | ||||||||
745 | } | ||||||||
746 | if (bIsValid) | ||||||||
747 | { | ||||||||
748 | ScopedVclPtrInstance<VirtualDevice> pVDev; | ||||||||
749 | GDIMetaFile aMtf; | ||||||||
750 | Size aSize( nWidth, nHeight ); | ||||||||
751 | pVDev->EnableOutput( false ); | ||||||||
752 | aMtf.Record( pVDev ); | ||||||||
753 | aSize = OutputDevice::LogicToLogic(aSize, MapMode(), MapMode(MapUnit::Map100thMM)); | ||||||||
754 | pVDev->DrawBitmapEx( Point(), aSize, vcl::bitmap::CreateFromData(std::move(aBitmap)) ); | ||||||||
755 | aMtf.Stop(); | ||||||||
756 | aMtf.WindStart(); | ||||||||
757 | aMtf.SetPrefMapMode(MapMode(MapUnit::Map100thMM)); | ||||||||
758 | aMtf.SetPrefSize( aSize ); | ||||||||
759 | aGraphic = aMtf; | ||||||||
760 | bHasPreview = bRetValue = true; | ||||||||
761 | } | ||||||||
762 | } | ||||||||
763 | } | ||||||||
764 | } | ||||||||
765 | } | ||||||||
766 | |||||||||
767 | sal_uInt8* pDest = ImplSearchEntry( pBuf.get(), reinterpret_cast<sal_uInt8 const *>("%%BoundingBox:"), nBytesRead, 14 ); | ||||||||
768 | sal_uInt32 nRemainingBytes = pDest ? (nBytesRead - (pDest - pBuf.get())) : 0; | ||||||||
769 | if (nRemainingBytes >= 14) | ||||||||
770 | { | ||||||||
771 | pDest += 14; | ||||||||
772 | nSecurityCount = std::min<sal_uInt32>(nRemainingBytes - 14, 100); | ||||||||
773 | long nNumb[4]; | ||||||||
774 | nNumb[0] = nNumb[1] = nNumb[2] = nNumb[3] = 0; | ||||||||
775 | for ( int i = 0; ( i < 4 ) && nSecurityCount; i++ ) | ||||||||
776 | { | ||||||||
777 | nNumb[ i ] = ImplGetNumber(pDest, nSecurityCount); | ||||||||
778 | } | ||||||||
779 | bool bFail = nSecurityCount == 0; | ||||||||
780 | long nWidth(0), nHeight(0); | ||||||||
781 | if (!bFail) | ||||||||
782 | bFail = o3tl::checked_sub(nNumb[2], nNumb[0], nWidth) || o3tl::checked_add(nWidth, 1L, nWidth); | ||||||||
783 | if (!bFail) | ||||||||
784 | bFail = o3tl::checked_sub(nNumb[3], nNumb[1], nHeight) || o3tl::checked_add(nHeight, 1L, nHeight); | ||||||||
785 | if (!bFail && nWidth > 0 && nHeight > 0) | ||||||||
786 | { | ||||||||
787 | GDIMetaFile aMtf; | ||||||||
788 | |||||||||
789 | // if there is no preview -> try with gs to make one | ||||||||
790 | if (!bHasPreview && !utl::ConfigManager::IsFuzzing()) | ||||||||
791 | { | ||||||||
792 | bHasPreview = RenderAsEMF(pBuf.get(), nBytesRead, aGraphic); | ||||||||
793 | if (!bHasPreview) | ||||||||
794 | bHasPreview = RenderAsBMP(pBuf.get(), nBytesRead, aGraphic); | ||||||||
795 | } | ||||||||
796 | |||||||||
797 | // if there is no preview -> make a red box | ||||||||
798 | if( !bHasPreview ) | ||||||||
799 | { | ||||||||
800 | MakePreview(pBuf.get(), nBytesRead, nWidth, nHeight, | ||||||||
801 | aGraphic); | ||||||||
802 | } | ||||||||
803 | |||||||||
804 | GfxLink aGfxLink( std::move(pBuf), nPSSize, GfxLinkType::EpsBuffer ) ; | ||||||||
805 | aMtf.AddAction( static_cast<MetaAction*>( new MetaEPSAction( Point(), Size( nWidth, nHeight ), | ||||||||
806 | aGfxLink, aGraphic.GetGDIMetaFile() ) ) ); | ||||||||
807 | CreateMtfReplacementAction( aMtf, rStream, nOrigPos, nPSSize, nPosWMF, nSizeWMF, nPosTIFF, nSizeTIFF ); | ||||||||
808 | aMtf.WindStart(); | ||||||||
809 | aMtf.SetPrefMapMode(MapMode(MapUnit::MapPoint)); | ||||||||
810 | aMtf.SetPrefSize( Size( nWidth, nHeight ) ); | ||||||||
811 | rGraphic = aMtf; | ||||||||
812 | bRetValue = true; | ||||||||
813 | } | ||||||||
814 | } | ||||||||
815 | } | ||||||||
816 | } | ||||||||
817 | rStream.SetEndian(nOldFormat); | ||||||||
818 | rStream.Seek( nOrigPos ); | ||||||||
819 | return bRetValue; | ||||||||
820 | } | ||||||||
821 | |||||||||
822 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_PTR_HXX |
21 | #define INCLUDED_VCL_PTR_HXX |
22 | |
23 | #include <sal/config.h> |
24 | |
25 | #include <rtl/ref.hxx> |
26 | |
27 | #include <utility> |
28 | #include <type_traits> |
29 | |
30 | #ifdef DBG_UTIL |
31 | #ifndef _WIN32 |
32 | #include <vcl/vclmain.hxx> |
33 | #endif |
34 | #endif |
35 | |
36 | class VclReferenceBase; |
37 | |
38 | namespace vcl::detail { |
39 | |
40 | template<typename> |
41 | constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; } |
42 | |
43 | template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase( |
44 | int (*)[sizeof(T)]) |
45 | { return std::is_base_of<VclReferenceBase, T>::value; } |
46 | |
47 | } // namespace vcl::detail |
48 | |
49 | /** |
50 | * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses. |
51 | * |
52 | * For more details on the design please see vcl/README.lifecycle |
53 | * |
54 | * @param reference_type must be a subclass of vcl::Window |
55 | */ |
56 | template <class reference_type> |
57 | class VclPtr |
58 | { |
59 | static_assert( |
60 | vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>( |
61 | nullptr), |
62 | "template argument type must be derived from VclReferenceBase"); |
63 | |
64 | ::rtl::Reference<reference_type> m_rInnerRef; |
65 | |
66 | public: |
67 | /** Constructor... |
68 | */ |
69 | VclPtr() |
70 | : m_rInnerRef() |
71 | {} |
72 | |
73 | /** Constructor... |
74 | */ |
75 | VclPtr (reference_type * pBody) |
76 | : m_rInnerRef(pBody) |
77 | {} |
78 | |
79 | /** Constructor... that doesn't take a ref. |
80 | */ |
81 | VclPtr (reference_type * pBody, __sal_NoAcquire) |
82 | : m_rInnerRef(pBody, SAL_NO_ACQUIRE) |
83 | {} |
84 | |
85 | /** Up-casting conversion constructor: Copies interface reference. |
86 | |
87 | Does not work for up-casts to ambiguous bases. For the special case of |
88 | up-casting to Reference< XInterface >, see the corresponding conversion |
89 | operator. |
90 | |
91 | @param rRef another reference |
92 | */ |
93 | template< class derived_type > |
94 | VclPtr( |
95 | const VclPtr< derived_type > & rRef, |
96 | typename std::enable_if< |
97 | std::is_base_of<reference_type, derived_type>::value, int>::type |
98 | = 0 ) |
99 | : m_rInnerRef( static_cast<reference_type*>(rRef) ) |
100 | { |
101 | } |
102 | |
103 | #if defined(DBG_UTIL) && !defined(_WIN32) |
104 | virtual ~VclPtr() |
105 | { |
106 | assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain ::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 106, __extension__ __PRETTY_FUNCTION__)); |
107 | // We can be one of the intermediate counts, but if we are the last |
108 | // VclPtr keeping this object alive, then something forgot to call dispose(). |
109 | assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)) |
110 | && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)); |
111 | } |
112 | VclPtr(VclPtr const &) = default; |
113 | VclPtr(VclPtr &&) = default; |
114 | VclPtr & operator =(VclPtr const &) = default; |
115 | VclPtr & operator =(VclPtr &&) = default; |
116 | #endif |
117 | |
118 | /** |
119 | * A construction helper for VclPtr. Since VclPtr types are created |
120 | * with a reference-count of one - to help fit into the existing |
121 | * code-flow; this helps us to construct them easily. |
122 | * |
123 | * For more details on the design please see vcl/README.lifecycle |
124 | * |
125 | * @tparam reference_type must be a subclass of vcl::Window |
126 | */ |
127 | template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg) |
128 | { |
129 | return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ); |
130 | } |
131 | |
132 | /** Probably most common used: handle->someBodyOp(). |
133 | */ |
134 | reference_type * operator->() const |
135 | { |
136 | return m_rInnerRef.get(); |
137 | } |
138 | |
139 | /** Get the body. Can be used instead of operator->(). |
140 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() |
141 | are the same. |
142 | */ |
143 | reference_type * get() const |
144 | { |
145 | return m_rInnerRef.get(); |
146 | } |
147 | |
148 | void set(reference_type *pBody) |
149 | { |
150 | m_rInnerRef.set(pBody); |
151 | } |
152 | |
153 | void reset(reference_type *pBody) |
154 | { |
155 | m_rInnerRef.set(pBody); |
156 | } |
157 | |
158 | /** Up-casting copy assignment operator. |
159 | |
160 | Does not work for up-casts to ambiguous bases. |
161 | |
162 | @param rRef another reference |
163 | */ |
164 | template<typename derived_type> |
165 | typename std::enable_if< |
166 | std::is_base_of<reference_type, derived_type>::value, |
167 | VclPtr &>::type |
168 | operator =(VclPtr<derived_type> const & rRef) |
169 | { |
170 | m_rInnerRef.set(rRef.get()); |
171 | return *this; |
172 | } |
173 | |
174 | VclPtr & operator =(reference_type * pBody) |
175 | { |
176 | m_rInnerRef.set(pBody); |
177 | return *this; |
178 | } |
179 | |
180 | operator reference_type * () const |
181 | { |
182 | return m_rInnerRef.get(); |
183 | } |
184 | |
185 | explicit operator bool () const |
186 | { |
187 | return m_rInnerRef.get() != nullptr; |
188 | } |
189 | |
190 | void clear() |
191 | { |
192 | m_rInnerRef.clear(); |
193 | } |
194 | |
195 | void reset() |
196 | { |
197 | m_rInnerRef.clear(); |
198 | } |
199 | |
200 | void disposeAndClear() |
201 | { |
202 | // hold it alive for the lifetime of this method |
203 | ::rtl::Reference<reference_type> aTmp(m_rInnerRef); |
204 | m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-) |
205 | if (aTmp.get()) { |
206 | aTmp->disposeOnce(); |
207 | } |
208 | } |
209 | |
210 | /** Needed to place VclPtr's into STL collection. |
211 | */ |
212 | bool operator< (const VclPtr<reference_type> & handle) const |
213 | { |
214 | return (m_rInnerRef < handle.m_rInnerRef); |
215 | } |
216 | }; // class VclPtr |
217 | |
218 | template<typename T1, typename T2> |
219 | inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
220 | return p1.get() == p2.get(); |
221 | } |
222 | |
223 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2) |
224 | { |
225 | return p1.get() == p2; |
226 | } |
227 | |
228 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) { |
229 | return p1.get() == p2; |
230 | } |
231 | |
232 | template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2) |
233 | { |
234 | return p1 == p2.get(); |
235 | } |
236 | |
237 | template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) { |
238 | return p1 == p2.get(); |
239 | } |
240 | |
241 | template<typename T1, typename T2> |
242 | inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
243 | return !(p1 == p2); |
244 | } |
245 | |
246 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2) |
247 | { |
248 | return !(p1 == p2); |
249 | } |
250 | |
251 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) { |
252 | return !(p1 == p2); |
253 | } |
254 | |
255 | template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2) |
256 | { |
257 | return !(p1 == p2); |
258 | } |
259 | |
260 | template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) { |
261 | return !(p1 == p2); |
262 | } |
263 | |
264 | /** |
265 | * A construction helper for a temporary VclPtr. Since VclPtr types |
266 | * are created with a reference-count of one - to help fit into |
267 | * the existing code-flow; this helps us to construct them easily. |
268 | * see also VclPtr::Create and ScopedVclPtr |
269 | * |
270 | * For more details on the design please see vcl/README.lifecycle |
271 | * |
272 | * @param reference_type must be a subclass of vcl::Window |
273 | */ |
274 | template <class reference_type> |
275 | class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type> |
276 | { |
277 | public: |
278 | template<typename... Arg> VclPtrInstance(Arg &&... arg) |
279 | : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
280 | { |
281 | } |
282 | |
283 | /** |
284 | * Override and disallow this, to prevent people accidentally calling it and actually |
285 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
286 | */ |
287 | template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete; |
288 | }; |
289 | |
290 | template <class reference_type> |
291 | class ScopedVclPtr : public VclPtr<reference_type> |
292 | { |
293 | public: |
294 | /** Constructor... |
295 | */ |
296 | ScopedVclPtr() |
297 | : VclPtr<reference_type>() |
298 | {} |
299 | |
300 | /** Constructor |
301 | */ |
302 | ScopedVclPtr (reference_type * pBody) |
303 | : VclPtr<reference_type>(pBody) |
304 | {} |
305 | |
306 | /** Copy constructor... |
307 | */ |
308 | ScopedVclPtr (const VclPtr<reference_type> & handle) |
309 | : VclPtr<reference_type>(handle) |
310 | {} |
311 | |
312 | /** |
313 | Assignment that releases the last reference. |
314 | */ |
315 | void disposeAndReset(reference_type *pBody) |
316 | { |
317 | if (pBody != this->get()) { |
318 | VclPtr<reference_type>::disposeAndClear(); |
319 | VclPtr<reference_type>::set(pBody); |
320 | } |
321 | } |
322 | |
323 | /** |
324 | Assignment that releases the last reference. |
325 | */ |
326 | ScopedVclPtr<reference_type>& operator = (reference_type * pBody) |
327 | { |
328 | disposeAndReset(pBody); |
329 | return *this; |
330 | } |
331 | |
332 | /** Up-casting conversion constructor: Copies interface reference. |
333 | |
334 | Does not work for up-casts to ambiguous bases. For the special case of |
335 | up-casting to Reference< XInterface >, see the corresponding conversion |
336 | operator. |
337 | |
338 | @param rRef another reference |
339 | */ |
340 | template< class derived_type > |
341 | ScopedVclPtr( |
342 | const VclPtr< derived_type > & rRef, |
343 | typename std::enable_if< |
344 | std::is_base_of<reference_type, derived_type>::value, int>::type |
345 | = 0 ) |
346 | : VclPtr<reference_type>( rRef ) |
347 | { |
348 | } |
349 | |
350 | /** Up-casting assignment operator. |
351 | |
352 | Does not work for up-casts to ambiguous bases. |
353 | |
354 | @param rRef another VclPtr |
355 | */ |
356 | template<typename derived_type> |
357 | typename std::enable_if< |
358 | std::is_base_of<reference_type, derived_type>::value, |
359 | ScopedVclPtr &>::type |
360 | operator =(VclPtr<derived_type> const & rRef) |
361 | { |
362 | disposeAndReset(rRef.get()); |
363 | return *this; |
364 | } |
365 | |
366 | /** |
367 | * Override and disallow this, to prevent people accidentally calling it and actually |
368 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
369 | */ |
370 | template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete; |
371 | |
372 | ~ScopedVclPtr() |
373 | { |
374 | VclPtr<reference_type>::disposeAndClear(); |
375 | assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get( ) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 375, __extension__ __PRETTY_FUNCTION__)); // make sure there are no lingering references |
376 | } |
377 | |
378 | private: |
379 | // Most likely we don't want this default copy-constructor. |
380 | ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete; |
381 | // And certainly we don't want a default assignment operator. |
382 | ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete; |
383 | // And disallow reset as that doesn't call disposeAndClear on the original reference |
384 | void reset() = delete; |
385 | void reset(reference_type *pBody) = delete; |
386 | |
387 | protected: |
388 | ScopedVclPtr (reference_type * pBody, __sal_NoAcquire) |
389 | : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE) |
390 | {} |
391 | }; |
392 | |
393 | /** |
394 | * A construction helper for ScopedVclPtr. Since VclPtr types are created |
395 | * with a reference-count of one - to help fit into the existing |
396 | * code-flow; this helps us to construct them easily. |
397 | * |
398 | * For more details on the design please see vcl/README.lifecycle |
399 | * |
400 | * @param reference_type must be a subclass of vcl::Window |
401 | */ |
402 | #if defined _MSC_VER |
403 | #pragma warning(push) |
404 | #pragma warning(disable: 4521) // " multiple copy constructors specified" |
405 | #endif |
406 | template <class reference_type> |
407 | class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type> |
408 | { |
409 | public: |
410 | template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg) |
411 | : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
412 | { |
413 | } |
414 | |
415 | /** |
416 | * Override and disallow this, to prevent people accidentally calling it and actually |
417 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
418 | */ |
419 | template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete; |
420 | |
421 | private: |
422 | // Prevent the above perfect forwarding ctor from hijacking (accidental) |
423 | // attempts at ScopedVclPtrInstance copy construction (where the hijacking |
424 | // would typically lead to somewhat obscure error messages); both non-const |
425 | // and const variants are needed here, as the ScopedVclPtr base class has a |
426 | // const--variant copy ctor, so the implicitly declared copy ctor for |
427 | // ScopedVclPtrInstance would also be the const variant, so non-const copy |
428 | // construction attempts would be hijacked by the perfect forwarding ctor; |
429 | // but if we only declared a non-const variant here, the const variant would |
430 | // no longer be implicitly declared (as there would already be an explicitly |
431 | // declared copy ctor), so const copy construction attempts would then be |
432 | // hijacked by the perfect forwarding ctor: |
433 | ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete; |
434 | ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete; |
435 | }; |
436 | #if defined _MSC_VER |
437 | #pragma warning(pop) |
438 | #endif |
439 | |
440 | #endif // INCLUDED_VCL_PTR_HXX |
441 | |
442 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_RTL_REF_HXX | ||||||||
21 | #define INCLUDED_RTL_REF_HXX | ||||||||
22 | |||||||||
23 | #include "sal/config.h" | ||||||||
24 | |||||||||
25 | #include <cassert> | ||||||||
26 | #include <cstddef> | ||||||||
27 | #include <functional> | ||||||||
28 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
29 | #include <type_traits> | ||||||||
30 | #endif | ||||||||
31 | |||||||||
32 | #include "sal/types.h" | ||||||||
33 | |||||||||
34 | namespace rtl | ||||||||
35 | { | ||||||||
36 | |||||||||
37 | /** Template reference class for reference type. | ||||||||
38 | */ | ||||||||
39 | template <class reference_type> | ||||||||
40 | class Reference | ||||||||
41 | { | ||||||||
42 | /** The <b>reference_type</b> body pointer. | ||||||||
43 | */ | ||||||||
44 | reference_type * m_pBody; | ||||||||
45 | |||||||||
46 | |||||||||
47 | public: | ||||||||
48 | /** Constructor... | ||||||||
49 | */ | ||||||||
50 | Reference() | ||||||||
51 | : m_pBody (NULL__null) | ||||||||
52 | {} | ||||||||
53 | |||||||||
54 | |||||||||
55 | /** Constructor... | ||||||||
56 | */ | ||||||||
57 | Reference (reference_type * pBody, __sal_NoAcquire) | ||||||||
58 | : m_pBody (pBody) | ||||||||
59 | { | ||||||||
60 | } | ||||||||
61 | |||||||||
62 | /** Constructor... | ||||||||
63 | */ | ||||||||
64 | Reference (reference_type * pBody) | ||||||||
65 | : m_pBody (pBody) | ||||||||
66 | { | ||||||||
67 | if (m_pBody) | ||||||||
68 | m_pBody->acquire(); | ||||||||
69 | } | ||||||||
70 | |||||||||
71 | /** Copy constructor... | ||||||||
72 | */ | ||||||||
73 | Reference (const Reference<reference_type> & handle) | ||||||||
74 | : m_pBody (handle.m_pBody) | ||||||||
75 | { | ||||||||
76 | if (m_pBody) | ||||||||
77 | m_pBody->acquire(); | ||||||||
78 | } | ||||||||
79 | |||||||||
80 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
81 | /** Move constructor... | ||||||||
82 | */ | ||||||||
83 | Reference (Reference<reference_type> && handle) noexcept | ||||||||
84 | : m_pBody (handle.m_pBody) | ||||||||
85 | { | ||||||||
86 | handle.m_pBody = nullptr; | ||||||||
87 | } | ||||||||
88 | #endif | ||||||||
89 | |||||||||
90 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
91 | /** Up-casting conversion constructor: Copies interface reference. | ||||||||
92 | |||||||||
93 | Does not work for up-casts to ambiguous bases. | ||||||||
94 | |||||||||
95 | @param rRef another reference | ||||||||
96 | */ | ||||||||
97 | template< class derived_type > | ||||||||
98 | inline Reference( | ||||||||
99 | const Reference< derived_type > & rRef, | ||||||||
100 | std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 ) | ||||||||
101 | : m_pBody (rRef.get()) | ||||||||
102 | { | ||||||||
103 | if (m_pBody) | ||||||||
104 | m_pBody->acquire(); | ||||||||
105 | } | ||||||||
106 | #endif | ||||||||
107 | |||||||||
108 | /** Destructor... | ||||||||
109 | */ | ||||||||
110 | ~Reference() COVERITY_NOEXCEPT_FALSE | ||||||||
111 | { | ||||||||
112 | if (m_pBody) | ||||||||
113 | m_pBody->release(); | ||||||||
114 | } | ||||||||
115 | |||||||||
116 | /** Set... | ||||||||
117 | Similar to assignment. | ||||||||
118 | */ | ||||||||
119 | Reference<reference_type> & | ||||||||
120 | SAL_CALL set (reference_type * pBody) | ||||||||
121 | { | ||||||||
122 | if (pBody) | ||||||||
123 | pBody->acquire(); | ||||||||
124 | reference_type * const pOld = m_pBody; | ||||||||
125 | m_pBody = pBody; | ||||||||
126 | if (pOld) | ||||||||
127 | pOld->release(); | ||||||||
128 | return *this; | ||||||||
129 | } | ||||||||
130 | |||||||||
131 | /** Assignment. | ||||||||
132 | Unbinds this instance from its body (if bound) and | ||||||||
133 | bind it to the body represented by the handle. | ||||||||
134 | */ | ||||||||
135 | Reference<reference_type> & | ||||||||
136 | SAL_CALL operator= (const Reference<reference_type> & handle) | ||||||||
137 | { | ||||||||
138 | return set( handle.m_pBody ); | ||||||||
139 | } | ||||||||
140 | |||||||||
141 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
142 | /** Assignment. | ||||||||
143 | * Unbinds this instance from its body (if bound), | ||||||||
144 | * bind it to the body represented by the handle, and | ||||||||
145 | * set the body represented by the handle to nullptr. | ||||||||
146 | */ | ||||||||
147 | Reference<reference_type> & | ||||||||
148 | operator= (Reference<reference_type> && handle) | ||||||||
149 | { | ||||||||
150 | // self-movement guts ourself | ||||||||
151 | if (m_pBody) | ||||||||
152 | m_pBody->release(); | ||||||||
153 | m_pBody = handle.m_pBody; | ||||||||
154 | handle.m_pBody = nullptr; | ||||||||
155 | return *this; | ||||||||
156 | } | ||||||||
157 | #endif | ||||||||
158 | |||||||||
159 | /** Assignment... | ||||||||
160 | */ | ||||||||
161 | Reference<reference_type> & | ||||||||
162 | SAL_CALL operator= (reference_type * pBody) | ||||||||
163 | { | ||||||||
164 | return set( pBody ); | ||||||||
165 | } | ||||||||
166 | |||||||||
167 | /** Unbind the body from this handle. | ||||||||
168 | Note that for a handle representing a large body, | ||||||||
169 | "handle.clear().set(new body());" _might_ | ||||||||
170 | perform a little bit better than "handle.set(new body());", | ||||||||
171 | since in the second case two large objects exist in memory | ||||||||
172 | (the old body and the new body). | ||||||||
173 | */ | ||||||||
174 | Reference<reference_type> & SAL_CALL clear() | ||||||||
175 | { | ||||||||
176 | if (m_pBody
| ||||||||
177 | { | ||||||||
178 | reference_type * const pOld = m_pBody; | ||||||||
179 | m_pBody = NULL__null; | ||||||||
180 | pOld->release(); | ||||||||
181 | } | ||||||||
182 | return *this; | ||||||||
183 | } | ||||||||
184 | |||||||||
185 | |||||||||
186 | /** Get the body. Can be used instead of operator->(). | ||||||||
187 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() | ||||||||
188 | are the same. | ||||||||
189 | */ | ||||||||
190 | reference_type * SAL_CALL get() const | ||||||||
191 | { | ||||||||
192 | return m_pBody; | ||||||||
| |||||||||
193 | } | ||||||||
194 | |||||||||
195 | |||||||||
196 | /** Probably most common used: handle->someBodyOp(). | ||||||||
197 | */ | ||||||||
198 | reference_type * SAL_CALL operator->() const | ||||||||
199 | { | ||||||||
200 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 200, __extension__ __PRETTY_FUNCTION__)); | ||||||||
201 | return m_pBody; | ||||||||
202 | } | ||||||||
203 | |||||||||
204 | |||||||||
205 | /** Allows (*handle).someBodyOp(). | ||||||||
206 | */ | ||||||||
207 | reference_type & SAL_CALL operator*() const | ||||||||
208 | { | ||||||||
209 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 209, __extension__ __PRETTY_FUNCTION__)); | ||||||||
210 | return *m_pBody; | ||||||||
211 | } | ||||||||
212 | |||||||||
213 | |||||||||
214 | /** Returns True if the handle does point to a valid body. | ||||||||
215 | */ | ||||||||
216 | bool SAL_CALL is() const | ||||||||
217 | { | ||||||||
218 | return (m_pBody != NULL__null); | ||||||||
219 | } | ||||||||
220 | |||||||||
221 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
222 | /** Returns True if the handle does point to a valid body. | ||||||||
223 | */ | ||||||||
224 | explicit operator bool() const | ||||||||
225 | { | ||||||||
226 | return is(); | ||||||||
227 | } | ||||||||
228 | #endif | ||||||||
229 | |||||||||
230 | /** Returns True if this points to pBody. | ||||||||
231 | */ | ||||||||
232 | bool SAL_CALL operator== (const reference_type * pBody) const | ||||||||
233 | { | ||||||||
234 | return (m_pBody == pBody); | ||||||||
235 | } | ||||||||
236 | |||||||||
237 | |||||||||
238 | /** Returns True if handle points to the same body. | ||||||||
239 | */ | ||||||||
240 | bool | ||||||||
241 | SAL_CALL operator== (const Reference<reference_type> & handle) const | ||||||||
242 | { | ||||||||
243 | return (m_pBody == handle.m_pBody); | ||||||||
244 | } | ||||||||
245 | |||||||||
246 | |||||||||
247 | /** Needed to place References into STL collection. | ||||||||
248 | */ | ||||||||
249 | bool | ||||||||
250 | SAL_CALL operator!= (const Reference<reference_type> & handle) const | ||||||||
251 | { | ||||||||
252 | return (m_pBody != handle.m_pBody); | ||||||||
253 | } | ||||||||
254 | |||||||||
255 | |||||||||
256 | /** Needed to place References into STL collection. | ||||||||
257 | */ | ||||||||
258 | bool | ||||||||
259 | SAL_CALL operator< (const Reference<reference_type> & handle) const | ||||||||
260 | { | ||||||||
261 | return (m_pBody < handle.m_pBody); | ||||||||
262 | } | ||||||||
263 | |||||||||
264 | |||||||||
265 | /** Needed to place References into STL collection. | ||||||||
266 | */ | ||||||||
267 | bool | ||||||||
268 | SAL_CALL operator> (const Reference<reference_type> & handle) const | ||||||||
269 | { | ||||||||
270 | return (m_pBody > handle.m_pBody); | ||||||||
271 | } | ||||||||
272 | }; | ||||||||
273 | |||||||||
274 | } // namespace rtl | ||||||||
275 | |||||||||
276 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
277 | namespace std | ||||||||
278 | { | ||||||||
279 | |||||||||
280 | /// @cond INTERNAL | ||||||||
281 | /** | ||||||||
282 | Make rtl::Reference hashable by default for use in STL containers. | ||||||||
283 | |||||||||
284 | @since LibreOffice 6.3 | ||||||||
285 | */ | ||||||||
286 | template<typename T> | ||||||||
287 | struct hash<::rtl::Reference<T>> | ||||||||
288 | { | ||||||||
289 | std::size_t operator()(::rtl::Reference<T> const & s) const | ||||||||
290 | { return std::size_t(s.get()); } | ||||||||
291 | }; | ||||||||
292 | /// @endcond | ||||||||
293 | |||||||||
294 | } | ||||||||
295 | |||||||||
296 | #endif | ||||||||
297 | |||||||||
298 | #endif /* ! INCLUDED_RTL_REF_HXX */ | ||||||||
299 | |||||||||
300 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_Reference_HXX |
20 | #define INCLUDED_VCL_Reference_HXX |
21 | |
22 | #include <vcl/dllapi.h> |
23 | #include <osl/interlck.h> |
24 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
32 | void acquire() const |
33 | { |
34 | osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1); |
35 | } |
36 | |
37 | void release() const |
38 | { |
39 | if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |