Debian Codegraph @

src/plugins/imageformats/mng/qmnghandler.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
  • reject broken MNG images
    Origin
    upstream: https://codereview.qt-project.org/c/qt/qtbase/+/303313 For backport to Qt 5.15, add these checks directly to qtimageformats, not qtbase.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the MNG plugins in the Qt ImageFormats module.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qmnghandler_p.h"
41
42 #include "qimage.h"
43 #include "qvariant.h"
44 #include "qcolor.h"
•45 #include <private/qimage_p.h>
46
47 #define MNG_USE_SO
48 #include <libmng.h>
49
50 QT_BEGIN_NAMESPACE
51
52 class QMngHandlerPrivate
53 {
54 Q_DECLARE_PUBLIC(QMngHandler)
55 public:
56 bool haveReadNone;
57 bool haveReadAll;
58 mng_handle hMNG;
59 QImage image;
60 int elapsed;
61 int nextDelay;
62 int iterCount;
63 int frameIndex;
64 int nextIndex;
65 int frameCount;
66 mng_uint32 iStyle;
67 mng_bool readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead);
68 mng_bool writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten);
69 mng_bool processHeader(mng_uint32 iWidth, mng_uint32 iHeight);
70 QMngHandlerPrivate(QMngHandler *q_ptr);
71 ~QMngHandlerPrivate();
72 bool getNextImage(QImage *result);
73 bool writeImage(const QImage &image);
74 int currentImageNumber() const;
75 int imageCount() const;
76 bool jumpToImage(int imageNumber);
77 bool jumpToNextImage();
78 int nextImageDelay() const;
79 bool setBackgroundColor(const QColor &color);
80 QColor backgroundColor() const;
81 QMngHandler *q_ptr;
82 };
83
84 static mng_bool myerror(mng_handle /*hMNG*/,
85 mng_int32 iErrorcode,
86 mng_int8 /*iSeverity*/,
87 mng_chunkid iChunkname,
88 mng_uint32 /*iChunkseq*/,
89 mng_int32 iExtra1,
90 mng_int32 iExtra2,
91 mng_pchar zErrortext)
92 {
93 qWarning("MNG error %d: %s; chunk %c%c%c%c; subcode %d:%d",
94 iErrorcode,zErrortext,
95 (iChunkname>>24)&0xff,
96 (iChunkname>>16)&0xff,
97 (iChunkname>>8)&0xff,
98 (iChunkname>>0)&0xff,
99 iExtra1,iExtra2);
100 return MNG_TRUE;
101 }
102
103 static mng_ptr myalloc(mng_size_t iSize)
104 {
105 return (mng_ptr)calloc(1, iSize);
106 }
107
108 static void myfree(mng_ptr pPtr, mng_size_t /*iSize*/)
109 {
110 free(pPtr);
111 }
112
113 static mng_bool myopenstream(mng_handle)
114 {
115 return MNG_TRUE;
116 }
117
118 static mng_bool myclosestream(mng_handle hMNG)
119 {
120 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
121 pMydata->haveReadAll = true;
122 return MNG_TRUE;
123 }
124
125 static mng_bool myreaddata(mng_handle hMNG,
126 mng_ptr pBuf,
127 mng_uint32 iSize,
128 mng_uint32p pRead)
129 {
130 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
131 return pMydata->readData(pBuf, iSize, pRead);
132 }
133
134 static mng_bool mywritedata(mng_handle hMNG,
135 mng_ptr pBuf,
136 mng_uint32 iSize,
137 mng_uint32p pWritten)
138 {
139 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
140 return pMydata->writeData(pBuf, iSize, pWritten);
141 }
142
143 static mng_bool myprocessheader(mng_handle hMNG,
144 mng_uint32 iWidth,
145 mng_uint32 iHeight)
146 {
147 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
148 return pMydata->processHeader(iWidth, iHeight);
149 }
150
151 static mng_ptr mygetcanvasline(mng_handle hMNG,
152 mng_uint32 iLinenr)
153 {
154 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
155 return (mng_ptr)pMydata->image.scanLine(iLinenr);
156 }
157
158 static mng_bool myrefresh(mng_handle /*hMNG*/,
159 mng_uint32 /*iX*/,
160 mng_uint32 /*iY*/,
161 mng_uint32 /*iWidth*/,
162 mng_uint32 /*iHeight*/)
163 {
164 return MNG_TRUE;
165 }
166
167 static mng_uint32 mygettickcount(mng_handle hMNG)
168 {
169 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
170 return pMydata->elapsed++;
171 }
172
173 static mng_bool mysettimer(mng_handle hMNG,
174 mng_uint32 iMsecs)
175 {
176 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
177 pMydata->elapsed += iMsecs;
178 pMydata->nextDelay = iMsecs;
179 return MNG_TRUE;
180 }
181
182 static mng_bool myprocessterm(mng_handle hMNG,
183 mng_uint8 iTermaction,
184 mng_uint8 /*iIteraction*/,
185 mng_uint32 /*iDelay*/,
186 mng_uint32 iItermax)
187 {
188 QMngHandlerPrivate *pMydata = reinterpret_cast<QMngHandlerPrivate *>(mng_get_userdata(hMNG));
189 if (iTermaction == 3)
190 pMydata->iterCount = iItermax;
191 return MNG_TRUE;
192 }
193
194 static mng_bool mytrace(mng_handle,
195 mng_int32 iFuncnr,
196 mng_int32 iFuncseq,
197 mng_pchar zFuncname)
198 {
199 qDebug("mng trace: iFuncnr: %d iFuncseq: %d zFuncname: %s", iFuncnr, iFuncseq, zFuncname);
200 return MNG_TRUE;
201 }
202
203 QMngHandlerPrivate::QMngHandlerPrivate(QMngHandler *q_ptr)
204 : haveReadNone(true), haveReadAll(false), elapsed(0), nextDelay(0), iterCount(1),
205 frameIndex(-1), nextIndex(0), frameCount(0), q_ptr(q_ptr)
206 {
207 iStyle = (QSysInfo::ByteOrder == QSysInfo::LittleEndian) ? MNG_CANVAS_BGRA8 : MNG_CANVAS_ARGB8;
208 // Initialize libmng
209 hMNG = mng_initialize((mng_ptr)this, myalloc, myfree, mytrace);
210 if (hMNG) {
211 // Set callback functions
212 mng_setcb_errorproc(hMNG, myerror);
213 mng_setcb_openstream(hMNG, myopenstream);
214 mng_setcb_closestream(hMNG, myclosestream);
215 mng_setcb_readdata(hMNG, myreaddata);
216 mng_setcb_writedata(hMNG, mywritedata);
217 mng_setcb_processheader(hMNG, myprocessheader);
218 mng_setcb_getcanvasline(hMNG, mygetcanvasline);
219 mng_setcb_refresh(hMNG, myrefresh);
220 mng_setcb_gettickcount(hMNG, mygettickcount);
221 mng_setcb_settimer(hMNG, mysettimer);
222 mng_setcb_processterm(hMNG, myprocessterm);
223 mng_set_doprogressive(hMNG, MNG_FALSE);
224 mng_set_suspensionmode(hMNG, MNG_TRUE);
225 }
226 }
227
228 QMngHandlerPrivate::~QMngHandlerPrivate()
229 {
230 mng_cleanup(&hMNG);
231 }
232
233 mng_bool QMngHandlerPrivate::readData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pRead)
234 {
235 Q_Q(QMngHandler);
236 *pRead = q->device()->read((char *)pBuf, iSize);
237 return (*pRead > 0) ? MNG_TRUE : MNG_FALSE;
238 }
239
240 mng_bool QMngHandlerPrivate::writeData(mng_ptr pBuf, mng_uint32 iSize, mng_uint32p pWritten)
241 {
242 Q_Q(QMngHandler);
243 *pWritten = q->device()->write((char *)pBuf, iSize);
244 return MNG_TRUE;
245 }
246
247 mng_bool QMngHandlerPrivate::processHeader(mng_uint32 iWidth, mng_uint32 iHeight)
248 {
249 if (mng_set_canvasstyle(hMNG, iStyle) != MNG_NOERROR)
250 return MNG_FALSE;
•251 QImageData::ImageSizeParameters szp =
252 QImageData::calculateImageParameters(iWidth, iHeight, 32);
253 if (!szp.isValid())
254 return MNG_FALSE;
255 // 256 MB is enough for an 8K 64bpp image
256 if (szp.totalSize > 256 << 20) {
257 qWarning("QMngHandler: Rejecting image as it exceeds the current "
258 "allocation limit of 256 megabytes");
259 return MNG_FALSE;
260 }
261 image = QImage(iWidth, iHeight, QImage::Format_ARGB32);
262 image.fill(0);
263 return MNG_TRUE;
264 }
265
266 bool QMngHandlerPrivate::getNextImage(QImage *result)
267 {
268 mng_retcode ret;
269 const bool savedHaveReadAll = haveReadAll;
270 if (haveReadNone) {
271 haveReadNone = false;
272 ret = mng_readdisplay(hMNG);
273 } else {
274 ret = mng_display_resume(hMNG);
275 }
276 if ((MNG_NOERROR == ret) || (MNG_NEEDTIMERWAIT == ret)) {
277 *result = image;
278
279 // QTBUG-28894 -- libmng produces an extra frame at the end
280 // of the animation on the first loop only.
281 if (nextDelay == 1 && (!savedHaveReadAll && haveReadAll)) {
282 ret = mng_display_resume(hMNG);
283 }
284
285 frameIndex = nextIndex++;
286 if (haveReadAll && (frameCount == 0))
287 frameCount = nextIndex;
288 return true;
289 }
290 return false;
291 }
292
293 bool QMngHandlerPrivate::writeImage(const QImage &image)
294 {
295 mng_reset(hMNG);
296 if (mng_create(hMNG) != MNG_NOERROR)
297 return false;
298
299 this->image = image.convertToFormat(QImage::Format_ARGB32);
300 int w = image.width();
301 int h = image.height();
302
303 if (
304 // width, height, ticks, layercount, framecount, playtime, simplicity
305 (mng_putchunk_mhdr(hMNG, w, h, 1000, 0, 0, 0, 7) == MNG_NOERROR) &&
306 // termination_action, action_after_iterations, delay, iteration_max
307 (mng_putchunk_term(hMNG, 3, 0, 1, 0x7FFFFFFF) == MNG_NOERROR) &&
308 // width, height, bitdepth, colortype, compression, filter, interlace
309 (mng_putchunk_ihdr(hMNG, w, h, 8, 6, 0, 0, 0) == MNG_NOERROR) &&
310 // width, height, colortype, bitdepth, compression, filter, interlace, canvasstyle, getcanvasline
311 (mng_putimgdata_ihdr(hMNG, w, h, 6, 8, 0, 0, 0, iStyle, mygetcanvasline) == MNG_NOERROR) &&
312 (mng_putchunk_iend(hMNG) == MNG_NOERROR) &&
313 (mng_putchunk_mend(hMNG) == MNG_NOERROR) &&
314 (mng_write(hMNG) == MNG_NOERROR)
315 )
316 return true;
317 return false;
318 }
319
320 int QMngHandlerPrivate::currentImageNumber() const
321 {
322 // return mng_get_currentframe(hMNG) % imageCount(); not implemented, apparently
323 return frameIndex;
324 }
325
326 int QMngHandlerPrivate::imageCount() const
327 {
328 // return mng_get_totalframes(hMNG); not implemented, apparently
329 if (haveReadAll)
330 return frameCount;
331 return 0; // Don't know
332 }
333
334 bool QMngHandlerPrivate::jumpToImage(int imageNumber)
335 {
336 if (imageNumber == nextIndex)
337 return true;
338
339 if ((imageNumber == 0) && haveReadAll && (nextIndex == frameCount)) {
340 // Loop!
341 nextIndex = 0;
342 return true;
343 }
344 if (mng_display_freeze(hMNG) == MNG_NOERROR) {
345 if (mng_display_goframe(hMNG, imageNumber) == MNG_NOERROR) {
346 nextIndex = imageNumber;
347 return true;
348 }
349 }
350 return false;
351 }
352
353 bool QMngHandlerPrivate::jumpToNextImage()
354 {
355 const int numImages = imageCount();
356 return numImages > 1 && jumpToImage((currentImageNumber() + 1) % numImages);
357 }
358
359 int QMngHandlerPrivate::nextImageDelay() const
360 {
361 return nextDelay;
362 }
363
364 bool QMngHandlerPrivate::setBackgroundColor(const QColor &color)
365 {
366 mng_uint16 iRed = (mng_uint16)(color.red() << 8);
367 mng_uint16 iBlue = (mng_uint16)(color.blue() << 8);
368 mng_uint16 iGreen = (mng_uint16)(color.green() << 8);
369 return (mng_set_bgcolor(hMNG, iRed, iBlue, iGreen) == MNG_NOERROR);
370 }
371
372 QColor QMngHandlerPrivate::backgroundColor() const
373 {
374 mng_uint16 iRed;
375 mng_uint16 iBlue;
376 mng_uint16 iGreen;
377 if (mng_get_bgcolor(hMNG, &iRed, &iBlue, &iGreen) == MNG_NOERROR)
378 return QColor((iRed >> 8) & 0xFF, (iGreen >> 8) & 0xFF, (iBlue >> 8) & 0xFF);
379 return QColor();
380 }
381
382 QMngHandler::QMngHandler()
383 : d_ptr(new QMngHandlerPrivate(this))
384 {
385 }
386
387 QMngHandler::~QMngHandler()
388 {
389 }
390
391 /*! \reimp */
392 bool QMngHandler::canRead() const
393 {
394 Q_D(const QMngHandler);
395 if ((!d->haveReadNone
396 && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))))
397 || canRead(device()))
398 {
399 setFormat("mng");
400 return true;
401 }
402 return false;
403 }
404
405 /*! \internal */
406 bool QMngHandler::canRead(QIODevice *device)
407 {
408 if (!device) {
409 qWarning("QMngHandler::canRead() called with no device");
410 return false;
411 }
412
413 return device->peek(8) == "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A";
414 }
415
416 /*! \reimp */
417 bool QMngHandler::read(QImage *image)
418 {
419 Q_D(QMngHandler);
420 return canRead() ? d->getNextImage(image) : false;
421 }
422
423 /*! \reimp */
424 bool QMngHandler::write(const QImage &image)
425 {
426 Q_D(QMngHandler);
427 return d->writeImage(image);
428 }
429
430 /*! \reimp */
431 int QMngHandler::currentImageNumber() const
432 {
433 Q_D(const QMngHandler);
434 return d->currentImageNumber();
435 }
436
437 /*! \reimp */
438 int QMngHandler::imageCount() const
439 {
440 Q_D(const QMngHandler);
441 return d->imageCount();
442 }
443
444 /*! \reimp */
445 bool QMngHandler::jumpToImage(int imageNumber)
446 {
447 Q_D(QMngHandler);
448 return d->jumpToImage(imageNumber);
449 }
450
451 /*! \reimp */
452 bool QMngHandler::jumpToNextImage()
453 {
454 Q_D(QMngHandler);
455 return d->jumpToNextImage();
456 }
457
458 /*! \reimp */
459 int QMngHandler::loopCount() const
460 {
461 Q_D(const QMngHandler);
462 if (d->iterCount == 0x7FFFFFFF)
463 return -1; // infinite loop
464 return d->iterCount-1;
465 }
466
467 /*! \reimp */
468 int QMngHandler::nextImageDelay() const
469 {
470 Q_D(const QMngHandler);
471 return d->nextImageDelay();
472 }
473
474 /*! \reimp */
475 QVariant QMngHandler::option(ImageOption option) const
476 {
477 Q_D(const QMngHandler);
478 if (option == QImageIOHandler::Animation)
479 return true;
480 else if (option == QImageIOHandler::BackgroundColor)
481 return d->backgroundColor();
482 return QVariant();
483 }
484
485 /*! \reimp */
486 void QMngHandler::setOption(ImageOption option, const QVariant & value)
487 {
488 Q_D(QMngHandler);
489 if (option == QImageIOHandler::BackgroundColor)
490 d->setBackgroundColor(qvariant_cast<QColor>(value));
491 }
492
493 /*! \reimp */
494 bool QMngHandler::supportsOption(ImageOption option) const
495 {
496 if (option == QImageIOHandler::Animation)
497 return true;
498 else if (option == QImageIOHandler::BackgroundColor)
499 return true;
500 return false;
501 }
502
503 QT_END_NAMESPACE