From 7697b90bb2cc2351f5c920b4d21fc8c05cd6e9e1 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sat, 18 May 2024 05:13:02 +0300 Subject: [PATCH] kutils: implement KArchive::data() argument to limit the data size to be used (for example) in MIME type determination, i.e. read only a small chunk of the data Signed-off-by: Ivailo Monev --- kutils/karchive/karchive.cpp | 14 ++++++++++---- kutils/karchive/karchive.h | 8 ++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/kutils/karchive/karchive.cpp b/kutils/karchive/karchive.cpp index 492c5ef9..c861b6c0 100644 --- a/kutils/karchive/karchive.cpp +++ b/kutils/karchive/karchive.cpp @@ -201,7 +201,7 @@ public: bool copyData(struct archive* readarchive, struct archive* writearchive); bool writeFile(struct archive* writearchive, QFile *file); - bool readData(struct archive* readarchive, QByteArray *buffer); + bool readData(struct archive* readarchive, QByteArray *buffer, const int maxsize); QString tempFilePath() const; @@ -413,7 +413,7 @@ bool KArchivePrivate::writeFile(struct archive* writearchive, QFile *file) return (readsize >= 0); } -bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer) +bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer, const int maxsize) { char readbuffer[KARCHIVE_BUFFSIZE]; ssize_t readsize = archive_read_data(readarchive, readbuffer, sizeof(readbuffer)); @@ -424,11 +424,17 @@ bool KArchivePrivate::readData(struct archive* readarchive, QByteArray *buffer) if (result != ARCHIVE_OK) { m_error = archive_error_string(readarchive); kDebug() << "archive_read_data" << m_error; + buffer->clear(); return false; } buffer->append(readbuffer, readsize); + if (maxsize > 0 && buffer->size() >= maxsize) { + buffer->resize(maxsize); + return true; + } + readsize = archive_read_data(readarchive, readbuffer, sizeof(readbuffer)); } @@ -1151,7 +1157,7 @@ KArchiveEntry KArchive::entry(const QString &path) const } -QByteArray KArchive::data(const QString &path) const +QByteArray KArchive::data(const QString &path, const int maxsize) const { QByteArray result; @@ -1185,7 +1191,7 @@ QByteArray KArchive::data(const QString &path) const const QByteArray pathname = archive_entry_pathname(entry); const QString pathnamestring = QFile::decodeName(pathname); if (pathnamestring == path) { - d->readData(readarchive, &result); + d->readData(readarchive, &result, maxsize); found = true; break; diff --git a/kutils/karchive/karchive.h b/kutils/karchive/karchive.h index 0c2d2443..f9812559 100644 --- a/kutils/karchive/karchive.h +++ b/kutils/karchive/karchive.h @@ -135,8 +135,12 @@ public: QList list(const QString &path = QString()) const; //! @brief Get entry information for path in archive KArchiveEntry entry(const QString &path) const; - //! @brief Get data for path in archive - QByteArray data(const QString &path) const; + /*! + @brief Get data for path in archive + @param path path to get data for + @param maxsize the limit for data, if zero then it is as if there is no limit + */ + QByteArray data(const QString &path, const int maxsize = 0) const; //! @brief Returns if path is readable archive bool isReadable() const;