Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / headless / svpbmp.cxx
blob178ea129c65595b2dbf02453f9a9e159e6405dbb
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <cstring>
25 #include <headless/svpbmp.hxx>
26 #include <headless/svpgdi.hxx>
27 #include <headless/svpinst.hxx>
29 #include <basegfx/vector/b2ivector.hxx>
30 #include <basegfx/range/b2ibox.hxx>
31 #include <o3tl/safeint.hxx>
32 #include <tools/helpers.hxx>
33 #include <vcl/bitmap.hxx>
35 using namespace basegfx;
37 SvpSalBitmap::SvpSalBitmap()
41 SvpSalBitmap::~SvpSalBitmap()
43 Destroy();
46 static std::optional<BitmapBuffer> ImplCreateDIB(
47 const Size& rSize,
48 vcl::PixelFormat ePixelFormat,
49 const BitmapPalette& rPal,
50 bool bClear)
52 if (!rSize.Width() || !rSize.Height())
53 return std::nullopt;
55 std::optional<BitmapBuffer> pDIB(std::in_place);
57 switch (ePixelFormat)
59 case vcl::PixelFormat::N8_BPP:
60 pDIB->mnFormat = ScanlineFormat::N8BitPal;
61 break;
62 case vcl::PixelFormat::N24_BPP:
63 pDIB->mnFormat = SVP_24BIT_FORMAT;
64 break;
65 case vcl::PixelFormat::N32_BPP:
66 pDIB->mnFormat = SVP_CAIRO_FORMAT;
67 break;
68 case vcl::PixelFormat::INVALID:
69 assert(false);
70 pDIB->mnFormat = SVP_CAIRO_FORMAT;
71 break;
74 sal_uInt16 nColors = 0;
75 if (ePixelFormat <= vcl::PixelFormat::N8_BPP)
76 nColors = vcl::numberOfColors(ePixelFormat);
78 pDIB->mnFormat |= ScanlineFormat::TopDown;
79 pDIB->mnWidth = rSize.Width();
80 pDIB->mnHeight = rSize.Height();
81 tools::Long nScanlineBase;
82 bool bFail = o3tl::checked_multiply<tools::Long>(pDIB->mnWidth, vcl::pixelFormatBitCount(ePixelFormat), nScanlineBase);
83 if (bFail)
85 SAL_WARN("vcl.gdi", "checked multiply failed");
86 return std::nullopt;
88 pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
89 if (pDIB->mnScanlineSize < nScanlineBase/8)
91 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
92 return std::nullopt;
94 pDIB->mnBitCount = vcl::pixelFormatBitCount(ePixelFormat);
96 if (nColors)
98 pDIB->maPalette = rPal;
99 pDIB->maPalette.SetEntryCount( nColors );
102 size_t size;
103 bFail = o3tl::checked_multiply<size_t>(pDIB->mnHeight, pDIB->mnScanlineSize, size);
104 SAL_WARN_IF(bFail, "vcl.gdi", "checked multiply failed");
105 if (bFail || size > SAL_MAX_INT32/2)
107 return std::nullopt;
112 pDIB->mpBits = new sal_uInt8[size];
113 #ifdef __SANITIZE_ADDRESS__
114 if (!pDIB->mpBits)
115 { // can only happen with ASAN allocator_may_return_null=1
116 pDIB.reset();
118 else
119 #endif
120 if (bClear)
122 std::memset(pDIB->mpBits, 0, size);
125 catch (const std::bad_alloc&)
127 pDIB.reset();
130 return pDIB;
133 void SvpSalBitmap::Create(const std::optional<BitmapBuffer>& pBuf)
135 Destroy();
136 moDIB = pBuf;
139 bool SvpSalBitmap::ImplCreate(const Size& rSize, vcl::PixelFormat ePixelFormat,
140 const BitmapPalette& rPal, bool bClear)
142 Destroy();
143 moDIB = ImplCreateDIB(rSize, ePixelFormat, rPal, bClear);
144 return moDIB.has_value();
147 bool SvpSalBitmap::Create(const Size& rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette& rPal)
149 return ImplCreate(rSize, ePixelFormat, rPal, true);
152 bool SvpSalBitmap::Create(const SalBitmap& rBmp)
154 Destroy();
156 const SvpSalBitmap& rSalBmp = static_cast<const SvpSalBitmap&>(rBmp);
158 if (rSalBmp.moDIB)
160 // TODO: reference counting...
161 moDIB.emplace( *rSalBmp.moDIB );
163 const size_t size = moDIB->mnScanlineSize * moDIB->mnHeight;
164 if (size > SAL_MAX_INT32/2)
166 moDIB.reset();
167 return false;
170 // TODO: get rid of this when BitmapBuffer gets copy constructor
173 moDIB->mpBits = new sal_uInt8[size];
174 std::memcpy(moDIB->mpBits, rSalBmp.moDIB->mpBits, size);
176 catch (const std::bad_alloc&)
178 moDIB.reset();
182 return !rSalBmp.moDIB.has_value() || moDIB.has_value();
185 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
186 SalGraphics* /*pGraphics*/ )
188 return false;
191 bool SvpSalBitmap::Create(const SalBitmap& /*rSalBmp*/,
192 vcl::PixelFormat /*eNewPixelFormat*/)
194 return false;
197 bool SvpSalBitmap::Create( const css::uno::Reference< css::rendering::XBitmapCanvas >& /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
199 return false;
202 void SvpSalBitmap::Destroy()
204 if (moDIB.has_value())
206 delete[] moDIB->mpBits;
207 moDIB.reset();
211 Size SvpSalBitmap::GetSize() const
213 Size aSize;
215 if (moDIB.has_value())
217 aSize.setWidth( moDIB->mnWidth );
218 aSize.setHeight( moDIB->mnHeight );
221 return aSize;
224 sal_uInt16 SvpSalBitmap::GetBitCount() const
226 sal_uInt16 nBitCount;
228 if (moDIB.has_value())
229 nBitCount = moDIB->mnBitCount;
230 else
231 nBitCount = 0;
233 return nBitCount;
236 BitmapBuffer* SvpSalBitmap::AcquireBuffer(BitmapAccessMode)
238 return moDIB ? &*moDIB : nullptr;
241 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer*, BitmapAccessMode nMode)
243 if( nMode == BitmapAccessMode::Write )
244 InvalidateChecksum();
247 bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
249 return false;
252 bool SvpSalBitmap::ScalingSupported() const
254 return false;
257 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
259 return false;
262 bool SvpSalBitmap::Replace( const ::Color& /*rSearchColor*/, const ::Color& /*rReplaceColor*/, sal_uInt8 /*nTol*/ )
264 return false;
267 const basegfx::SystemDependentDataHolder* SvpSalBitmap::accessSystemDependentDataHolder() const
269 return this;
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */