| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qktxhandler_p.h" |
| 5 | #include "qtexturefiledata_p.h" |
| 6 | #include <QtEndian> |
| 7 | #include <QSize> |
| 8 | #include <QMap> |
| 9 | #include <QtCore/qiodevice.h> |
| 10 | |
| 11 | //#define KTX_DEBUG |
| 12 | #ifdef KTX_DEBUG |
| 13 | #include <QDebug> |
| 14 | #include <QMetaEnum> |
| 15 | #include <QOpenGLTexture> |
| 16 | #endif |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | using namespace Qt::StringLiterals; |
| 21 | |
| 22 | #define KTX_IDENTIFIER_LENGTH 12 |
| 23 | static const char ktxIdentifier[KTX_IDENTIFIER_LENGTH] = { '\xAB', 'K', 'T', 'X', ' ', '1', '1', '\xBB', '\r', '\n', '\x1A', '\n' }; |
| 24 | static const quint32 platformEndianIdentifier = 0x04030201; |
| 25 | static const quint32 inversePlatformEndianIdentifier = 0x01020304; |
| 26 | |
| 27 | struct { |
| 28 | quint8 [KTX_IDENTIFIER_LENGTH]; // Must match ktxIdentifier |
| 29 | quint32 ; // Either platformEndianIdentifier or inversePlatformEndianIdentifier, other values not allowed. |
| 30 | quint32 ; |
| 31 | quint32 ; |
| 32 | quint32 ; |
| 33 | quint32 ; |
| 34 | quint32 ; |
| 35 | quint32 ; |
| 36 | quint32 ; |
| 37 | quint32 ; |
| 38 | quint32 ; |
| 39 | quint32 ; |
| 40 | quint32 ; |
| 41 | quint32 ; |
| 42 | }; |
| 43 | |
| 44 | static constexpr quint32 = sizeof(KTXHeader); |
| 45 | |
| 46 | // Currently unused, declared for future reference |
| 47 | struct KTXKeyValuePairItem { |
| 48 | quint32 keyAndValueByteSize; |
| 49 | /* |
| 50 | quint8 keyAndValue[keyAndValueByteSize]; |
| 51 | quint8 valuePadding[3 - ((keyAndValueByteSize + 3) % 4)]; |
| 52 | */ |
| 53 | }; |
| 54 | |
| 55 | struct KTXMipmapLevel { |
| 56 | quint32 imageSize; |
| 57 | /* |
| 58 | for each array_element in numberOfArrayElements* |
| 59 | for each face in numberOfFaces |
| 60 | for each z_slice in pixelDepth* |
| 61 | for each row or row_of_blocks in pixelHeight* |
| 62 | for each pixel or block_of_pixels in pixelWidth |
| 63 | Byte data[format-specific-number-of-bytes]** |
| 64 | end |
| 65 | end |
| 66 | end |
| 67 | Byte cubePadding[0-3] |
| 68 | end |
| 69 | end |
| 70 | quint8 mipPadding[3 - ((imageSize + 3) % 4)] |
| 71 | */ |
| 72 | }; |
| 73 | |
| 74 | // Returns the nearest multiple of 4 greater than or equal to 'value' |
| 75 | static const std::optional<quint32> nearestMultipleOf4(quint32 value) |
| 76 | { |
| 77 | constexpr quint32 rounding = 4; |
| 78 | quint32 result = 0; |
| 79 | if (qAddOverflow(v1: value, v2: rounding - 1, r: &result)) |
| 80 | return std::nullopt; |
| 81 | result &= ~(rounding - 1); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | // Returns a view with prechecked bounds |
| 86 | static QByteArrayView safeView(QByteArrayView view, quint32 start, quint32 length) |
| 87 | { |
| 88 | quint32 end = 0; |
| 89 | if (qAddOverflow(v1: start, v2: length, r: &end) || end > quint32(view.length())) |
| 90 | return {}; |
| 91 | return view.sliced(pos: start, n: length); |
| 92 | } |
| 93 | |
| 94 | QKtxHandler::~QKtxHandler() = default; |
| 95 | |
| 96 | bool QKtxHandler::canRead(const QByteArray &suffix, const QByteArray &block) |
| 97 | { |
| 98 | Q_UNUSED(suffix); |
| 99 | return block.startsWith(bv: ktxIdentifier); |
| 100 | } |
| 101 | |
| 102 | QTextureFileData QKtxHandler::read() |
| 103 | { |
| 104 | if (!device()) |
| 105 | return QTextureFileData(); |
| 106 | |
| 107 | const QByteArray buf = device()->readAll(); |
| 108 | if (static_cast<size_t>(buf.size()) > std::numeric_limits<quint32>::max()) { |
| 109 | qCWarning(lcQtGuiTextureIO, "Too big KTX file %s" , logName().constData()); |
| 110 | return QTextureFileData(); |
| 111 | } |
| 112 | |
| 113 | if (!canRead(suffix: QByteArray(), block: buf)) { |
| 114 | qCWarning(lcQtGuiTextureIO, "Invalid KTX file %s" , logName().constData()); |
| 115 | return QTextureFileData(); |
| 116 | } |
| 117 | |
| 118 | if (buf.size() < qsizetype(qktxh_headerSize)) { |
| 119 | qCWarning(lcQtGuiTextureIO, "Invalid KTX header size in %s" , logName().constData()); |
| 120 | return QTextureFileData(); |
| 121 | } |
| 122 | |
| 123 | KTXHeader ; |
| 124 | memcpy(dest: &header, src: buf.data(), n: qktxh_headerSize); |
| 125 | if (!checkHeader(header)) { |
| 126 | qCWarning(lcQtGuiTextureIO, "Unsupported KTX file format in %s" , logName().constData()); |
| 127 | return QTextureFileData(); |
| 128 | } |
| 129 | |
| 130 | QTextureFileData texData; |
| 131 | texData.setData(buf); |
| 132 | |
| 133 | texData.setSize(QSize(decode(val: header.pixelWidth), decode(val: header.pixelHeight))); |
| 134 | texData.setGLFormat(decode(val: header.glFormat)); |
| 135 | texData.setGLInternalFormat(decode(val: header.glInternalFormat)); |
| 136 | texData.setGLBaseInternalFormat(decode(val: header.glBaseInternalFormat)); |
| 137 | |
| 138 | texData.setNumLevels(decode(val: header.numberOfMipmapLevels)); |
| 139 | texData.setNumFaces(decode(val: header.numberOfFaces)); |
| 140 | |
| 141 | const quint32 bytesOfKeyValueData = decode(val: header.bytesOfKeyValueData); |
| 142 | quint32 ; |
| 143 | if (qAddOverflow(v1: qktxh_headerSize, v2: bytesOfKeyValueData, r: &headerKeyValueSize)) { |
| 144 | qCWarning(lcQtGuiTextureIO, "Overflow in size of key value data in header of KTX file %s" , |
| 145 | logName().constData()); |
| 146 | return QTextureFileData(); |
| 147 | } |
| 148 | |
| 149 | if (headerKeyValueSize >= quint32(buf.size())) { |
| 150 | qCWarning(lcQtGuiTextureIO, "OOB request in KTX file %s" , logName().constData()); |
| 151 | return QTextureFileData(); |
| 152 | } |
| 153 | |
| 154 | // File contains key/values |
| 155 | if (bytesOfKeyValueData > 0) { |
| 156 | auto keyValueDataView = safeView(view: buf, start: qktxh_headerSize, length: bytesOfKeyValueData); |
| 157 | if (keyValueDataView.isEmpty()) { |
| 158 | qCWarning(lcQtGuiTextureIO, "Invalid view in KTX file %s" , logName().constData()); |
| 159 | return QTextureFileData(); |
| 160 | } |
| 161 | |
| 162 | auto keyValues = decodeKeyValues(view: keyValueDataView); |
| 163 | if (!keyValues) { |
| 164 | qCWarning(lcQtGuiTextureIO, "Could not parse key values in KTX file %s" , |
| 165 | logName().constData()); |
| 166 | return QTextureFileData(); |
| 167 | } |
| 168 | |
| 169 | texData.setKeyValueMetadata(*keyValues); |
| 170 | } |
| 171 | |
| 172 | // Technically, any number of levels is allowed but if the value is bigger than |
| 173 | // what is possible in KTX V2 (and what makes sense) we return an error. |
| 174 | // maxLevels = log2(max(width, height, depth)) |
| 175 | const int maxLevels = (sizeof(quint32) * 8) |
| 176 | - qCountLeadingZeroBits(v: std::max( |
| 177 | l: { header.pixelWidth, header.pixelHeight, header.pixelDepth })); |
| 178 | |
| 179 | if (texData.numLevels() > maxLevels) { |
| 180 | qCWarning(lcQtGuiTextureIO, "Too many levels in KTX file %s" , logName().constData()); |
| 181 | return QTextureFileData(); |
| 182 | } |
| 183 | |
| 184 | if (texData.numFaces() != 1 && texData.numFaces() != 6) { |
| 185 | qCWarning(lcQtGuiTextureIO, "Invalid number of faces in KTX file %s" , logName().constData()); |
| 186 | return QTextureFileData(); |
| 187 | } |
| 188 | |
| 189 | quint32 offset = headerKeyValueSize; |
| 190 | for (int level = 0; level < texData.numLevels(); level++) { |
| 191 | const auto imageSizeView = safeView(view: buf, start: offset, length: sizeof(quint32)); |
| 192 | if (imageSizeView.isEmpty()) { |
| 193 | qCWarning(lcQtGuiTextureIO, "OOB request in KTX file %s" , logName().constData()); |
| 194 | return QTextureFileData(); |
| 195 | } |
| 196 | |
| 197 | const quint32 imageSize = decode(val: qFromUnaligned<quint32>(src: imageSizeView.data())); |
| 198 | offset += sizeof(quint32); // overflow checked indirectly above |
| 199 | |
| 200 | for (int face = 0; face < texData.numFaces(); face++) { |
| 201 | texData.setDataOffset(offset, level, face); |
| 202 | texData.setDataLength(length: imageSize, level, face); |
| 203 | |
| 204 | // Add image data and padding to offset |
| 205 | const auto padded = nearestMultipleOf4(value: imageSize); |
| 206 | if (!padded) { |
| 207 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX file %s" , logName().constData()); |
| 208 | return QTextureFileData(); |
| 209 | } |
| 210 | |
| 211 | quint32 offsetNext; |
| 212 | if (qAddOverflow(v1: offset, v2: *padded, r: &offsetNext)) { |
| 213 | qCWarning(lcQtGuiTextureIO, "OOB request in KTX file %s" , logName().constData()); |
| 214 | return QTextureFileData(); |
| 215 | } |
| 216 | |
| 217 | offset = offsetNext; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (!texData.isValid()) { |
| 222 | qCWarning(lcQtGuiTextureIO, "Invalid values in header of KTX file %s" , |
| 223 | logName().constData()); |
| 224 | return QTextureFileData(); |
| 225 | } |
| 226 | |
| 227 | texData.setLogName(logName()); |
| 228 | |
| 229 | #ifdef KTX_DEBUG |
| 230 | qDebug() << "KTX file handler read" << texData; |
| 231 | #endif |
| 232 | |
| 233 | return texData; |
| 234 | } |
| 235 | |
| 236 | bool QKtxHandler::checkHeader(const KTXHeader &) |
| 237 | { |
| 238 | if (header.endianness != platformEndianIdentifier && header.endianness != inversePlatformEndianIdentifier) |
| 239 | return false; |
| 240 | inverseEndian = (header.endianness == inversePlatformEndianIdentifier); |
| 241 | #ifdef KTX_DEBUG |
| 242 | QMetaEnum tfme = QMetaEnum::fromType<QOpenGLTexture::TextureFormat>(); |
| 243 | QMetaEnum ptme = QMetaEnum::fromType<QOpenGLTexture::PixelType>(); |
| 244 | qDebug("Header of %s:" , logName().constData()); |
| 245 | qDebug(" glType: 0x%x (%s)" , decode(header.glType), ptme.valueToKey(decode(header.glType))); |
| 246 | qDebug(" glTypeSize: %u" , decode(header.glTypeSize)); |
| 247 | qDebug(" glFormat: 0x%x (%s)" , decode(header.glFormat), |
| 248 | tfme.valueToKey(decode(header.glFormat))); |
| 249 | qDebug(" glInternalFormat: 0x%x (%s)" , decode(header.glInternalFormat), |
| 250 | tfme.valueToKey(decode(header.glInternalFormat))); |
| 251 | qDebug(" glBaseInternalFormat: 0x%x (%s)" , decode(header.glBaseInternalFormat), |
| 252 | tfme.valueToKey(decode(header.glBaseInternalFormat))); |
| 253 | qDebug(" pixelWidth: %u" , decode(header.pixelWidth)); |
| 254 | qDebug(" pixelHeight: %u" , decode(header.pixelHeight)); |
| 255 | qDebug(" pixelDepth: %u" , decode(header.pixelDepth)); |
| 256 | qDebug(" numberOfArrayElements: %u" , decode(header.numberOfArrayElements)); |
| 257 | qDebug(" numberOfFaces: %u" , decode(header.numberOfFaces)); |
| 258 | qDebug(" numberOfMipmapLevels: %u" , decode(header.numberOfMipmapLevels)); |
| 259 | qDebug(" bytesOfKeyValueData: %u" , decode(header.bytesOfKeyValueData)); |
| 260 | #endif |
| 261 | const bool isCompressedImage = decode(val: header.glType) == 0 && decode(val: header.glFormat) == 0 |
| 262 | && decode(val: header.pixelDepth) == 0; |
| 263 | const bool isCubeMap = decode(val: header.numberOfFaces) == 6; |
| 264 | const bool is2D = decode(val: header.pixelDepth) == 0 && decode(val: header.numberOfArrayElements) == 0; |
| 265 | |
| 266 | return is2D && (isCubeMap || isCompressedImage); |
| 267 | } |
| 268 | |
| 269 | std::optional<QMap<QByteArray, QByteArray>> QKtxHandler::decodeKeyValues(QByteArrayView view) const |
| 270 | { |
| 271 | QMap<QByteArray, QByteArray> output; |
| 272 | quint32 offset = 0; |
| 273 | while (offset < quint32(view.size())) { |
| 274 | const auto keyAndValueByteSizeView = safeView(view, start: offset, length: sizeof(quint32)); |
| 275 | if (keyAndValueByteSizeView.isEmpty()) { |
| 276 | qCWarning(lcQtGuiTextureIO, "Invalid view in KTX key-value" ); |
| 277 | return std::nullopt; |
| 278 | } |
| 279 | |
| 280 | const quint32 keyAndValueByteSize = |
| 281 | decode(val: qFromUnaligned<quint32>(src: keyAndValueByteSizeView.data())); |
| 282 | |
| 283 | quint32 offsetKeyAndValueStart; |
| 284 | if (qAddOverflow(v1: offset, v2: quint32(sizeof(quint32)), r: &offsetKeyAndValueStart)) { |
| 285 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX key-value" ); |
| 286 | return std::nullopt; |
| 287 | } |
| 288 | |
| 289 | quint32 offsetKeyAndValueEnd; |
| 290 | if (qAddOverflow(v1: offsetKeyAndValueStart, v2: keyAndValueByteSize, r: &offsetKeyAndValueEnd)) { |
| 291 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX key-value" ); |
| 292 | return std::nullopt; |
| 293 | } |
| 294 | |
| 295 | const auto keyValueView = safeView(view, start: offsetKeyAndValueStart, length: keyAndValueByteSize); |
| 296 | if (keyValueView.isEmpty()) { |
| 297 | qCWarning(lcQtGuiTextureIO, "Invalid view in KTX key-value" ); |
| 298 | return std::nullopt; |
| 299 | } |
| 300 | |
| 301 | // 'key' is a UTF-8 string ending with a null terminator, 'value' is the rest. |
| 302 | // To separate the key and value we convert the complete data to utf-8 and find the first |
| 303 | // null terminator from the left, here we split the data into two. |
| 304 | |
| 305 | const int idx = keyValueView.indexOf(ch: '\0'); |
| 306 | if (idx == -1) { |
| 307 | qCWarning(lcQtGuiTextureIO, "Invalid key in KTX key-value" ); |
| 308 | return std::nullopt; |
| 309 | } |
| 310 | |
| 311 | const QByteArrayView keyView = safeView(view, start: offsetKeyAndValueStart, length: idx); |
| 312 | if (keyView.isEmpty()) { |
| 313 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX key-value" ); |
| 314 | return std::nullopt; |
| 315 | } |
| 316 | |
| 317 | const quint32 keySize = idx + 1; // Actual data size |
| 318 | |
| 319 | quint32 offsetValueStart; |
| 320 | if (qAddOverflow(v1: offsetKeyAndValueStart, v2: keySize, r: &offsetValueStart)) { |
| 321 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX key-value" ); |
| 322 | return std::nullopt; |
| 323 | } |
| 324 | |
| 325 | quint32 valueSize; |
| 326 | if (qSubOverflow(v1: keyAndValueByteSize, v2: keySize, r: &valueSize)) { |
| 327 | qCWarning(lcQtGuiTextureIO, "Underflow in KTX key-value" ); |
| 328 | return std::nullopt; |
| 329 | } |
| 330 | |
| 331 | const QByteArrayView valueView = safeView(view, start: offsetValueStart, length: valueSize); |
| 332 | if (valueView.isEmpty()) { |
| 333 | qCWarning(lcQtGuiTextureIO, "Invalid view in KTX key-value" ); |
| 334 | return std::nullopt; |
| 335 | } |
| 336 | |
| 337 | output.insert(key: keyView.toByteArray(), value: valueView.toByteArray()); |
| 338 | |
| 339 | const auto offsetNext = nearestMultipleOf4(value: offsetKeyAndValueEnd); |
| 340 | if (!offsetNext) { |
| 341 | qCWarning(lcQtGuiTextureIO, "Overflow in KTX key-value" ); |
| 342 | return std::nullopt; |
| 343 | } |
| 344 | |
| 345 | offset = *offsetNext; |
| 346 | } |
| 347 | |
| 348 | return output; |
| 349 | } |
| 350 | |
| 351 | quint32 QKtxHandler::decode(quint32 val) const |
| 352 | { |
| 353 | return inverseEndian ? qbswap<quint32>(source: val) : val; |
| 354 | } |
| 355 | |
| 356 | QT_END_NAMESPACE |
| 357 | |