From a7fe1af609e905f6e5fbf291ec43261a41d9541f Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sun, 15 Mar 2020 18:45:43 +0100 Subject: Remove QRegExp usage from the XBM handler Change-Id: Ie7141c2a04c233bc1298195aebfc0437e43df0d1 Reviewed-by: Eirik Aavitsland --- src/gui/image/qxbmhandler.cpp | 51 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index f06561690c..c422826035 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -44,7 +44,6 @@ #include #include -#include #include #include @@ -67,8 +66,6 @@ static bool read_xbm_header(QIODevice *device, int& w, int& h) const int buflen = 300; const int maxlen = 4096; char buf[buflen + 1]; - QRegExp r1(QLatin1String("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+")); - QRegExp r2(QLatin1String("[0-9]+")); qint64 readBytes = 0; qint64 totalReadBytes = 0; @@ -90,30 +87,38 @@ static bool read_xbm_header(QIODevice *device, int& w, int& h) return false; } - buf[readBytes - 1] = '\0'; - QString sbuf; - sbuf = QString::fromLatin1(buf); + auto parseDefine = [] (const char *buf, int len) -> int { + auto isAsciiLetterOrNumber = [] (char ch) -> bool { + return (ch >= '0' && ch <= '9') || + (ch >= 'A' && ch <= 'Z') || + (ch >= 'a' && ch <= 'z') || + ch == '_' || ch == '.'; + }; + auto isAsciiSpace = [] (char ch) -> bool { + return ch == ' ' || ch == '\t'; + }; + const char define[] = "#define"; + constexpr size_t defineLen = sizeof(define) - 1; + if (strncmp(buf, define, defineLen) != 0) + return 0; + int index = defineLen; + while (buf[index] && isAsciiSpace(buf[index])) + ++index; + while (buf[index] && isAsciiLetterOrNumber(buf[index])) + ++index; + while (buf[index] && isAsciiSpace(buf[index])) + ++index; + + return QByteArray(buf + index, len - index).toInt(); + }; + // "#define .._width " - if (r1.indexIn(sbuf) == 0 && - r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength()) - w = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt(); - else - return false; + w = parseDefine(buf, readBytes - 1); - // "#define .._height " readBytes = device->readLine(buf, buflen); - if (readBytes <= 0) - return false; - buf[readBytes - 1] = '\0'; - - sbuf = QString::fromLatin1(buf); - - if (r1.indexIn(sbuf) == 0 && - r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength()) - h = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt(); - else - return false; + // "#define .._height " + h = parseDefine(buf, readBytes - 1); // format error if (w <= 0 || w > 32767 || h <= 0 || h > 32767) -- cgit v1.2.3