diff --git a/src/imageformats/pcx.cpp b/src/imageformats/pcx.cpp index 4efd19a..c707e3b 100644 --- a/src/imageformats/pcx.cpp +++ b/src/imageformats/pcx.cpp @@ -266,29 +266,30 @@ PCXHEADER::PCXHEADER() bool peekHeader(QIODevice *d, PCXHEADER& h) { - qint64 pos = 0; - if (!d->isSequential()) { - pos = d->pos(); + qint64 oldPos = d->pos(); + QByteArray head = d->read(sizeof(PCXHEADER)); + int readBytes = head.size(); + + if (d->isSequential()) { + for (int pos = readBytes -1; pos >= 0; --pos) { + d->ungetChar(head[pos]); + } + } else { + d->seek(oldPos); + } + + if (readBytes < sizeof(PCXHEADER)) { + return false; } auto ok = false; { // datastream is destroyed before working on device - QDataStream ds(d); + QDataStream ds(head); ds.setByteOrder(QDataStream::LittleEndian); ds >> h; ok = ds.status() == QDataStream::Ok && h.isValid(); } - if (!d->isSequential()) { - return d->seek(pos) && ok; - } - - // sequential device undo - auto head = reinterpret_cast(&h); - auto readBytes = sizeof(h); - while (readBytes > 0) { - d->ungetChar(head[readBytes-- - 1]); - } return ok; }