From cbf56a63adfaaa172ac7f9956e4452ccda9b3a60 Mon Sep 17 00:00:00 2001 From: Michal Lazo Date: Fri, 29 Jul 2022 15:12:12 +0200 Subject: [PATCH] option to disable qtextcodec usage --- common.pri | 4 ++++ quazip/quazip.cpp | 53 +++++++++++++++++++++++++++++++++++++++---- quazip/quazip.h | 4 ++++ quazip/quazip.pri | 4 +++- quazip/quazipfile.cpp | 8 +++++++ 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/common.pri b/common.pri index 498e654..bcc667a 100644 --- a/common.pri +++ b/common.pri @@ -1,4 +1,8 @@ exists(config.pri):infile(config.pri, SOLUTIONS_LIBRARY, yes): CONFIG += quazip-uselib +exists(config.pri):infile(config.pri, USE_TEXTCODEC, yes): { + DEFINES += USE_TEXTCODEC + CONFIG += use-textcodec +} TEMPLATE += fakelib QUAZIP_LIBNAME = $$qtLibraryTarget(quazip-head) TEMPLATE -= fakelib diff --git a/quazip/quazip.cpp b/quazip/quazip.cpp index b6831c9..64db93a 100644 --- a/quazip/quazip.cpp +++ b/quazip/quazip.cpp @@ -45,9 +45,9 @@ class QuaZipPrivate { /// The pointer to the corresponding QuaZip instance. QuaZip *q; /// The codec for file names (used when UTF-8 is not enabled). - QTextCodec *fileNameCodec; + QTextCodec *fileNameCodec = NULL; /// The codec for comments (used when UTF-8 is not enabled). - QTextCodec *commentCodec; + QTextCodec *commentCodec = NULL; /// The archive file name. QString zipName; /// The device to access the archive. @@ -79,7 +79,11 @@ class QuaZipPrivate { inline QTextCodec *getDefaultFileNameCodec() { if (defaultFileNameCodec == NULL) { +#if defined( USE_TEXTCODEC ) return QTextCodec::codecForLocale(); +#else + return NULL; +#endif } else { return defaultFileNameCodec; } @@ -87,8 +91,10 @@ class QuaZipPrivate { /// The constructor for the corresponding QuaZip constructor. inline QuaZipPrivate(QuaZip *q): q(q), +#if defined( USE_TEXTCODEC ) fileNameCodec(getDefaultFileNameCodec()), commentCodec(QTextCodec::codecForLocale()), +#endif ioDevice(NULL), mode(QuaZip::mdNotOpen), hasCurrentFile_f(false), @@ -107,8 +113,10 @@ class QuaZipPrivate { /// The constructor for the corresponding QuaZip constructor. inline QuaZipPrivate(QuaZip *q, const QString &zipName): q(q), +#if defined( USE_TEXTCODEC ) fileNameCodec(getDefaultFileNameCodec()), commentCodec(QTextCodec::codecForLocale()), +#endif zipName(zipName), ioDevice(NULL), mode(QuaZip::mdNotOpen), @@ -128,8 +136,10 @@ class QuaZipPrivate { /// The constructor for the corresponding QuaZip constructor. inline QuaZipPrivate(QuaZip *q, QIODevice *ioDevice): q(q), +#if defined( USE_TEXTCODEC ) fileNameCodec(getDefaultFileNameCodec()), commentCodec(QTextCodec::codecForLocale()), +#endif ioDevice(ioDevice), mode(QuaZip::mdNotOpen), hasCurrentFile_f(false), @@ -359,7 +369,11 @@ void QuaZip::close() case mdAdd: p->zipError=zipClose(p->zipFile_f, p->comment.isNull() ? NULL : isUtf8Enabled() ? p->comment.toUtf8().constData() +#if defined( USE_TEXTCODEC ) : p->commentCodec->fromUnicode(p->comment).constData()); +#else + : p->comment.toUtf8().constData()); +#endif break; default: qWarning("QuaZip::close(): unknown mode: %d", (int)p->mode); @@ -425,9 +439,13 @@ QString QuaZip::getComment()const if((fakeThis->p->zipError=unzGetGlobalComment(p->unzFile_f, comment.data(), comment.size())) < 0) return QString(); fakeThis->p->zipError = UNZ_OK; +#if defined( USE_TEXTCODEC ) unsigned flags = 0; return (unzGetFileFlags(p->unzFile_f, &flags) == UNZ_OK) && (flags & UNZ_ENCODING_UTF8) ? QString::fromUtf8(comment) : p->commentCodec->toUnicode(comment); +#else + return QString::fromUtf8(comment); +#endif } bool QuaZip::setCurrentFile(const QString& fileName, CaseSensitivity cs) @@ -562,8 +580,13 @@ bool QuaZip::getCurrentFileInfo(QuaZipFileInfo64 *info)const info->diskNumberStart=info_z.disk_num_start; info->internalAttr=info_z.internal_fa; info->externalAttr=info_z.external_fa; +#if defined( USE_TEXTCODEC ) info->name=(info->flags & UNZ_ENCODING_UTF8) ? QString::fromUtf8(fileName) : p->fileNameCodec->toUnicode(fileName); info->comment=(info->flags & UNZ_ENCODING_UTF8) ? QString::fromUtf8(comment) : p->commentCodec->toUnicode(comment); +#else + info->name=QString::fromUtf8(fileName); + info->comment=QString::fromUtf8(comment); +#endif info->extra=extra; info->dateTime=QDateTime( QDate(info_z.tmu_date.tm_year, info_z.tmu_date.tm_mon+1, info_z.tmu_date.tm_mday), @@ -588,8 +611,16 @@ QString QuaZip::getCurrentFileName()const NULL, 0, NULL, 0))!=UNZ_OK) return QString(); fileName.resize(file_info.size_filename); - QString result = (file_info.flag & UNZ_ENCODING_UTF8) - ? QString::fromUtf8(fileName) : p->fileNameCodec->toUnicode(fileName); + QString result; + if (file_info.flag & UNZ_ENCODING_UTF8) + result = QString::fromUtf8(fileName); + else +#if defined( USE_TEXTCODEC ) + result = p->fileNameCodec->toUnicode(fileName); +#else + result = QString::fromUtf8(fileName); +#endif + if (result.isEmpty()) return result; // Add to directory map @@ -604,7 +635,11 @@ void QuaZip::setFileNameCodec(QTextCodec *fileNameCodec) void QuaZip::setFileNameCodec(const char *fileNameCodecName) { - p->fileNameCodec=QTextCodec::codecForName(fileNameCodecName); +#if defined( USE_TEXTCODEC ) + p->fileNameCodec=QTextCodec::codecForName(fileNameCodecName); +#else + Q_UNUSED(fileNameCodecName); +#endif } void QuaZip::setOsCode(uint osCode) @@ -629,7 +664,11 @@ void QuaZip::setCommentCodec(QTextCodec *commentCodec) void QuaZip::setCommentCodec(const char *commentCodecName) { +#if defined( USE_TEXTCODEC ) p->commentCodec=QTextCodec::codecForName(commentCodecName); +#else + Q_UNUSED(commentCodecName); +#endif } QTextCodec *QuaZip::getCommentCodec()const @@ -802,7 +841,11 @@ void QuaZip::setDefaultFileNameCodec(QTextCodec *codec) void QuaZip::setDefaultFileNameCodec(const char *codecName) { +#if defined( USE_TEXTCODEC ) setDefaultFileNameCodec(QTextCodec::codecForName(codecName)); +#else + Q_UNUSED(codecName); +#endif } void QuaZip::setDefaultOsCode(uint osCode) diff --git a/quazip/quazip.h b/quazip/quazip.h index 9e88b52..10bdf88 100644 --- a/quazip/quazip.h +++ b/quazip/quazip.h @@ -27,11 +27,15 @@ quazip/(un)zip.h files for details, basically it's zlib license. #include #include +#if defined( USE_TEXTCODEC ) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) # include #else # include #endif +#else +class QTextCodec; +#endif #include "zip.h" #include "unzip.h" diff --git a/quazip/quazip.pri b/quazip/quazip.pri index 60a6da4..933059c 100644 --- a/quazip/quazip.pri +++ b/quazip/quazip.pri @@ -11,7 +11,9 @@ win32:INCLUDEPATH += $(MEMSOURCE_LIB)/qt/src/3rdparty/zlib } greaterThan( QT_MAJOR_VERSION, 5 ) { - QT += core5compat + use-textcodec:{ + QT += core5compat + } } unix:LIBS += -lz diff --git a/quazip/quazipfile.cpp b/quazip/quazipfile.cpp index b984de7..ba9de06 100644 --- a/quazip/quazipfile.cpp +++ b/quazip/quazipfile.cpp @@ -344,15 +344,23 @@ bool QuaZipFile::open(OpenMode mode, const QuaZipNewInfo& info, else zipClearFlags(p->zip->getZipFile(), ZIP_WRITE_DATA_DESCRIPTOR); p->setZipError(zipOpenNewFileInZip4_64(p->zip->getZipFile(), +#if defined( USE_TEXTCODEC ) p->zip->isUtf8Enabled() ? info.name.toUtf8().constData() : p->zip->getFileNameCodec()->fromUnicode(info.name).constData(), +#else + info.name.toUtf8().constData(), +#endif &info_z, info.extraLocal.constData(), info.extraLocal.length(), info.extraGlobal.constData(), info.extraGlobal.length(), +#if defined( USE_TEXTCODEC ) p->zip->isUtf8Enabled() ? info.comment.toUtf8().constData() : p->zip->getCommentCodec()->fromUnicode(info.comment).constData(), +#else + info.comment.toUtf8().constData(), +#endif method, level, (int)raw, windowBits, memLevel, strategy, password, (uLong)crc,