1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Alex Char.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:critical reason:data-parser
5
6#ifndef QICNSHANDLER_P_H
7#define QICNSHANDLER_P_H
8
9#include <QtGui/qimageiohandler.h>
10#include <QtCore/qlist.h>
11
12#ifndef QT_NO_DATASTREAM
13
14#define MAKEOSTYPE(c0,c1,c2,c3) (((quint8)c0 << 24) | ((quint8)c1 << 16) | ((quint8)c2 << 8) | (quint8)c3)
15
16QT_BEGIN_NAMESPACE
17
18struct ICNSBlockHeader
19{
20 enum OS {
21 TypeIcns = MAKEOSTYPE('i', 'c', 'n', 's'), // Icns container magic
22 TypeToc = MAKEOSTYPE('T', 'O', 'C', ' '), // Table of contents
23 TypeIcnv = MAKEOSTYPE('i', 'c', 'n', 'V'), // Icon Composer version
24 // Legacy:
25 TypeClut = MAKEOSTYPE('c', 'l', 'u', 't'), // Color look-up table (pre-OS X resources)
26 TypeTile = MAKEOSTYPE('t', 'i', 'l', 'e'), // Container (icon variants)
27 TypeOver = MAKEOSTYPE('o', 'v', 'e', 'r'), // Container (icon variants)
28 TypeOpen = MAKEOSTYPE('o', 'p', 'e', 'n'), // Container (icon variants)
29 TypeDrop = MAKEOSTYPE('d', 'r', 'o', 'p'), // Container (icon variants)
30 TypeOdrp = MAKEOSTYPE('o', 'd', 'r', 'p'), // Container (icon variants)
31 };
32
33 quint32 ostype;
34 quint32 length;
35};
36
37struct ICNSEntry
38{
39 enum Group {
40 GroupUnknown = 0, // Default for invalid ones
41 GroupMini = 'm', // "mini" (16x12)
42 GroupSmall = 's', // "small" (16x16)
43 GroupLarge = 'l', // "large" (32x32)
44 GroupHuge = 'h', // "huge" (48x48)
45 GroupThumbnail = 't', // "thumbnail" (128x128)
46 GroupPortable = 'p', // "portable"? (Speculation, used for png/jp2)
47 GroupCompressed = 'c', // "compressed"? (Speculation, used for png/jp2)
48 // Legacy icons:
49 GroupICON = 'N', // "ICON" (32x32)
50 };
51 enum Depth {
52 DepthUnknown = 0, // Default for invalid or compressed ones
53 DepthMono = 1,
54 Depth4bit = 4,
55 Depth8bit = 8,
56 Depth32bit = 32
57 };
58 enum Flags {
59 Unknown = 0x0, // Default for invalid ones
60 IsIcon = 0x1, // Contains a raw icon without alpha or compressed icon
61 IsMask = 0x2, // Contains alpha mask
62 IconPlusMask = IsIcon | IsMask // Contains raw icon and mask combined in one entry (double size)
63 };
64 enum Format {
65 FormatUnknown = 0, // Default for invalid or undetermined ones
66 RawIcon, // Raw legacy icon, uncompressed
67 RLE24, // Raw 32bit icon, data is compressed
68 PNG, // Compressed icon in PNG format
69 JP2 // Compressed icon in JPEG2000 format
70 };
71
72 quint32 ostype; // Real OSType
73 quint32 variant; // Virtual OSType: a parent container, zero if parent is icns root
74 Group group; // ASCII character number
75 quint32 width; // For uncompressed icons only, zero for compressed ones for now
76 quint32 height; // For uncompressed icons only, zero for compressed ones fow now
77 Depth depth; // Color depth
78 Flags flags; // Flags to determine the type of entry
79 Format dataFormat; // Format of the image data
80 quint32 dataLength; // Length of the image data in bytes
81 qint64 dataOffset; // Offset from the initial position of the file/device
82
83 ICNSEntry() :
84 ostype(0), variant(0), group(GroupUnknown), width(0), height(0), depth(DepthUnknown),
85 flags(Unknown), dataFormat(FormatUnknown), dataLength(0), dataOffset(0)
86 {
87 }
88};
89Q_DECLARE_TYPEINFO(ICNSEntry, Q_RELOCATABLE_TYPE);
90
91class QICNSHandler : public QImageIOHandler
92{
93public:
94 QICNSHandler();
95
96 bool canRead() const override;
97 bool read(QImage *image) override;
98 bool write(const QImage &image) override;
99
100 bool supportsOption(ImageOption option) const override;
101 QVariant option(ImageOption option) const override;
102
103 int imageCount() const override;
104 bool jumpToImage(int imageNumber) override;
105 bool jumpToNextImage() override;
106
107 static bool canRead(QIODevice *device);
108
109private:
110 bool ensureScanned() const;
111 bool scanDevice();
112 bool addEntry(const ICNSBlockHeader &header, qint64 imgDataOffset, quint32 variant = 0);
113 const ICNSEntry &getIconMask(const ICNSEntry &icon) const;
114
115private:
116 enum ScanState {
117 ScanError = -1,
118 ScanNotScanned = 0,
119 ScanSuccess = 1,
120 };
121
122 int m_currentIconIndex;
123 QList<ICNSEntry> m_icons;
124 QList<ICNSEntry> m_masks;
125 ScanState m_state;
126};
127
128QT_END_NAMESPACE
129
130#endif // QT_NO_DATASTREAM
131
132#endif /* QICNSHANDLER_P_H */
133

source code of qtimageformats/src/plugins/imageformats/icns/qicnshandler_p.h