Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Remove unused KoLZF code
Halla Rempt66c8d985cc4a
diff --git a/libs/store/CMakeLists.txt b/libs/store/CMakeLists.txtindex d381b07030..fd7bdbb82f 100644--- a/libs/store/CMakeLists.txt+++ b/libs/store/CMakeLists.txt@@ -5,7 +5,6 @@ add_subdirectory(tests)set(kritastore_LIB_SRCSKoDirectoryStore.cppKoStoreDevice.cpp- KoLZF.cppKoStore.cppKoXmlNS.cppKoXmlWriter.cppdiff --git a/libs/store/KoLZF.cpp b/libs/store/KoLZF.cppdeleted file mode 100644index 2dcefb7fe0..0000000000--- a/libs/store/KoLZF.cpp+++ /dev/null@@ -1,354 +0,0 @@-/* This file is part of the KDE project- SPDX-FileCopyrightText: 2005-2006 Ariya Hidayat <[email protected]>- SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>-- SPDX-License-Identifier: LGPL-2.0-or-later-*/--#include "KoLZF.h"--#include <QByteArray>-#include <QDebug>---namespace KoLZF {--#define HASH_LOG 12-#define HASH_SIZE (1<< HASH_LOG)-#define HASH_MASK (HASH_SIZE-1)--#ifdef __GNUC__-#pragma GCC diagnostic ignored "-Wcast-align"-#endif-#define UPDATE_HASH(v,p) { v = *((quint16*)p); v ^= *((quint16*)(p+1))^(v>>(16-HASH_LOG)); }--#define MAX_COPY 32-#define MAX_LEN 264 /* 256 + 8 */-#define MAX_DISTANCE 8192--// Lossless compression using LZF algorithm, this is faster on modern CPU than-// the original implementation in http://liblzf.plan9.de/-int compress(const void* input, int length, void* output, int maxout)-{- if (input == 0 || length < 1 || output == 0 || maxout < 2) {- return 0;- }-- const quint8* ip = (const quint8*) input;- const quint8* ip_limit = ip + length - MAX_COPY - 4;- quint8* op = (quint8*) output;- const quint8* last_op = (quint8*) output + maxout - 1;-- const quint8* htab[HASH_SIZE];- const quint8** hslot;- quint32 hval;-- quint8* ref;- qint32 copy;- qint32 len;- qint32 distance;- quint8* anchor;-- /* initializes hash table */- for (hslot = htab; hslot < htab + HASH_SIZE; ++hslot) {- *hslot = ip;- }-- /* we start with literal copy */- copy = 0;- *op++ = MAX_COPY - 1;-- /* main loop */- while (ip < ip_limit) {- /* find potential match */- UPDATE_HASH(hval, ip);- hslot = htab + (hval & HASH_MASK);- ref = (quint8*) * hslot;-- /* update hash table */- *hslot = ip;-- /* find itself? then it's no match */- if (ip == ref)- goto literal;-- /* is this a match? check the first 2 bytes */- if (*((quint16*)ref) != *((quint16*)ip))- goto literal;-- /* now check the 3rd byte */- if (ref[2] != ip[2])- goto literal;-- /* calculate distance to the match */- distance = ip - ref;-- /* skip if too far away */- if (distance >= MAX_DISTANCE)- goto literal;-- /* here we have 3-byte matches */- anchor = (quint8*)ip;- len = 3;- ref += 3;- ip += 3;-- /* now we have to check how long the match is */- if (ip < ip_limit - MAX_LEN) {- while (len < MAX_LEN - 8) {- /* unroll 8 times */- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- if (*ref++ != *ip++) break;- len += 8;- }- --ip;- }- len = ip - anchor;-- /* just before the last non-matching byte */- ip = anchor + len;-- /* if we have copied something, adjust the copy count */- if (copy) {- /* copy is biased, '0' means 1 byte copy */- anchor = anchor - copy - 1;- *(op - copy - 1) = copy - 1;- copy = 0;- } else {- /* back, to overwrite the copy count */- --op;- }-- /* length is biased, '1' means a match of 3 bytes */- len -= 2;-- /* distance is also biased */- --distance;-- /* encode the match */- if (len < 7) {- if (op + 2 > last_op) {- return 0;- }- *op++ = (len << 5) + (distance >> 8);- } else {- if (op + 3 > last_op) {- return 0;- }- *op++ = (7 << 5) + (distance >> 8);- *op++ = len - 7;- }- *op++ = (distance & 255);-- /* assuming next will be literal copy */- *op++ = MAX_COPY - 1;-- /* update the hash at match boundary */- --ip;- UPDATE_HASH(hval, ip);- htab[hval & HASH_MASK] = ip;- ++ip;-- continue;-- literal:- if (op + 1 > last_op) {- return 0;- }- *op++ = *ip++;- ++copy;- if (copy >= MAX_COPY) {- // start next literal copy item- copy = 0;- *op++ = MAX_COPY - 1;- }- }-- /* left-over as literal copy */- ip_limit = (const quint8*)input + length;-- // TODO: smart calculation to see here if enough output is left-- while (ip < ip_limit) {- if (op == last_op) {- return 0;- }- *op++ = *ip++;- ++copy;- if (copy == MAX_COPY) {- // start next literal copy item- copy = 0;- if (ip < ip_limit) {- if (op == last_op) {- return 0;- }- *op++ = MAX_COPY - 1;- } else {- // do not write possibly out of bounds- // just pretend we moved one more, for the final treatment- ++op;- }- }- }-- /* if we have copied something, adjust the copy length */- if (copy) {- *(op - copy - 1) = copy - 1;- } else {- --op;- }-- return op - (quint8*)output;-}--int decompress(const void* input, int length, void* output, int maxout)-{- if (input == 0 || length < 1) {- return 0;- }- if (output == 0 || maxout < 1) {- return 0;- }-- const quint8* ip = (const quint8*) input;- const quint8* ip_limit = ip + length - 1;- quint8* op = (quint8*) output;- quint8* op_limit = op + maxout;- quint8* ref;-- while (ip < ip_limit) {- quint32 ctrl = (*ip) + 1;- quint32 ofs = ((*ip) & 31) << 8;- quint32 len = (*ip++) >> 5;-- if (ctrl < 33) {- /* literal copy */- if (op + ctrl > op_limit)- return 0;-- /* crazy unrolling */- if (ctrl) {- *op++ = *ip++;- --ctrl;-- if (ctrl) {- *op++ = *ip++;- --ctrl;-- if (ctrl) {- *op++ = *ip++;- --ctrl;-- for (;ctrl; --ctrl)- *op++ = *ip++;- }- }- }- } else {- /* back reference */- --len;- ref = op - ofs;- --ref;-- if (len == 7 - 1)- len += *ip++;-- ref -= *ip++;-- if (op + len + 3 > op_limit)- return 0;-- if (ref < (quint8 *)output)- return 0;-- *op++ = *ref++;- *op++ = *ref++;- *op++ = *ref++;- if (len)- for (; len; --len)- *op++ = *ref++;- }- }-- return op - (quint8*)output;-}---QByteArray compress(const QByteArray& input)-{- const void* const in_data = (const void*) input.constData();- unsigned int in_len = (unsigned int)input.size();-- QByteArray output;- output.resize(in_len + 4 + 1);-- // we use 4 bytes to store uncompressed length- // and 1 extra byte as flag (0=uncompressed, 1=compressed)- output[0] = in_len & 255;- output[1] = (in_len >> 8) & 255;- output[2] = (in_len >> 16) & 255;- output[3] = (in_len >> 24) & 255;- output[4] = 1;-- unsigned int out_len = in_len - 1;- unsigned char* out_data = (unsigned char*) output.data() + 5;-- unsigned int len = compress(in_data, in_len, out_data, out_len);-- if ((len > out_len) || (len == 0)) {- // output buffer is too small, likely because the data can't- // be compressed. so here just copy without compression- output.replace(5, output.size()-5, input);-- // flag must indicate "uncompressed block"- output[4] = 0;- } else {- output.resize(len + 4 + 1);- }-- // minimize memory- output.squeeze();-- return output;-}--// will not squeeze output-void decompress(const QByteArray& input, QByteArray& output)-{- Q_ASSERT(input.size() >= 5);-- // read out first how big is the uncompressed size- unsigned int unpack_size = 0;- unpack_size |= ((quint8)input[0]);- unpack_size |= ((quint8)input[1]) << 8;- unpack_size |= ((quint8)input[2]) << 16;- unpack_size |= ((quint8)input[3]) << 24;-- // prepare the output- output.resize(unpack_size);-- // compression flag- quint8 flag = (quint8)input[4];-- // prepare for lzf- const void* const in_data = (const void*)(input.constData() + 5);- unsigned int in_len = (unsigned int)input.size() - 5;- unsigned char* out_data = (unsigned char*) output.data();- unsigned int out_len = (unsigned int)unpack_size;-- if (flag == 0) {- memcpy(output.data(), in_data, in_len);- } else {- unsigned int len = decompress(in_data, in_len, out_data, out_len);- Q_ASSERT(len == out_len);- Q_UNUSED(len);- }-}--}diff --git a/libs/store/KoLZF.h b/libs/store/KoLZF.hdeleted file mode 100644index a7c3310077..0000000000--- a/libs/store/KoLZF.h+++ /dev/null@@ -1,50 +0,0 @@-/* This file is part of the KDE project- SPDX-FileCopyrightText: 2005-2006 Ariya Hidayat <[email protected]>- SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>-- SPDX-License-Identifier: LGPL-2.0-or-later-*/--#ifndef KO_LZF_H-#define KO_LZF_H--class QByteArray;--namespace KoLZF-{--/**- * @param input where to read the data to compress from- * @param length length of the input- * @param output where to write the compressed data to- * @param maxout maximal usable length of output, needs to be at least 2 bytes- * @return the length of data written to output, or, on failure, 0- */-int compress(const void* input, int length, void* output, int maxout);--/**- * @param input where to read the data to decompress from- * @param length length of the input- * @param output where to write the decompressed data to- * @param maxout maximal usable length of output- * @return the length of data written to output, or, on failure, 0- */-int decompress(const void* input, int length, void* output, int maxout);--/**- * @param data the data to compress- * @return the compressed data (with KoLZF header)- */-QByteArray compress(const QByteArray& data);--/**- * @param data the data to decompress (with KoLZF header)- * @param output where to write the decompressed data to- * Existing content will be lost.- * On failure will be an empty QByteArray.- */-void decompress(const QByteArray &data, QByteArray &output);--}--#endifdiff --git a/libs/store/tests/CMakeLists.txt b/libs/store/tests/CMakeLists.txtindex 6028b7a040..8bfa790f92 100644--- a/libs/store/tests/CMakeLists.txt+++ b/libs/store/tests/CMakeLists.txt@@ -1,14 +1,7 @@include(KritaAddBrokenUnitTest)-kis_add_test(- ../KoLZF.cpp TestKoLZF.cpp- TEST_NAME TestKoLZF- LINK_LIBRARIES kritastore kritatestsdk kritaglobal- NAME_PREFIX "libs-odf"- )--########### manual test for file contents ###############+add_executable(storedroptest storedroptest.cpp)target_link_libraries(storedroptest kritastore Qt${QT_MAJOR_VERSION}::Widgets)ecm_mark_as_test(storedroptest)diff --git a/libs/store/tests/TestKoLZF.cpp b/libs/store/tests/TestKoLZF.cppdeleted file mode 100644index dadc33bb14..0000000000--- a/libs/store/tests/TestKoLZF.cpp+++ /dev/null@@ -1,235 +0,0 @@-/* This file is part of the KDE project- * SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>- *- * SPDX-License-Identifier: LGPL-2.0-or-later- */--#include "TestKoLZF.h"--#include <KoLZF.h>--#include <simpletest.h>--void TestKoLZF::testArrayCompressionEmpty_data()-{- QTest::addColumn<char>("canary");- QTest::newRow("00") << '\0';- QTest::newRow("FF") << '\xFF';-}--void TestKoLZF::testArrayCompressionEmpty()-{- QFETCH(char, canary);-- const char inputData[] = "test";- // use one more byte and see if it stays untouched- char outputdata[1];- outputdata[0] = canary;-- const int compressedDataLength = KoLZF::compress(inputData, 0, outputdata, 0);-- QCOMPARE(compressedDataLength, 0);- QCOMPARE(outputdata[0], canary);-}--void TestKoLZF::testArrayCompressionNullPointerInput()-{- char outputdata[4];-- const int compressedDataLength = KoLZF::compress(0, 4, outputdata, 4);-- QCOMPARE(compressedDataLength, 0);-}--void TestKoLZF::testArrayCompressionNullPointerOutput()-{- const char inputData[] = "test";-- const int compressedDataLength = KoLZF::compress(inputData, 4, 0, 4);-- QCOMPARE(compressedDataLength, 0);-}--void TestKoLZF::testArrayDecompressionEmpty_data()-{- QTest::addColumn<char>("canary");- QTest::newRow("00") << '\0';- QTest::newRow("FF") << '\xFF';-}--void TestKoLZF::testArrayDecompressionEmpty()-{- QFETCH(char, canary);-- const char inputData[] = "test";- char outputdata[1];- outputdata[0] = canary;-- const int uncompressedDataLength = KoLZF::decompress(inputData, 0, outputdata, 0);-- QCOMPARE(uncompressedDataLength, 0);- QCOMPARE(outputdata[0], canary);-}--void TestKoLZF::testArrayDecompressionNullPointerInput()-{- char outputdata[4];-- const int uncompressedDataLength = KoLZF::decompress(0, 0, outputdata, 4);-- QCOMPARE(uncompressedDataLength, 0);-}--void TestKoLZF::testArrayDecompressionNullPointerOutput()-{- const char inputData[] = "test";-- const int uncompressedDataLength = KoLZF::decompress(inputData, 4, 0, 4);-- QCOMPARE(uncompressedDataLength, 0);-}--Q_DECLARE_METATYPE(char*)--void TestKoLZF::testArrayRoundtripDifferentSizes_data()-{- QTest::addColumn<char*>("data");- QTest::addColumn<int>("size");- QTest::addColumn<char>("canary");-- static const char canary[] = {'\0', '\xFF'};- static const int canaryCount = sizeof( canary ) / sizeof( canary[0] );- static const int fillMethodCount = 2;- static const char * const fillMethodName[fillMethodCount] = {"uni", "series"};-- for(int c = 0; c < canaryCount; ++c) {- for(int i = 1; i < 512; ++i) {- for (int f = 0; f < 2; ++f) {- char *data = new char[i];- if (f == 0) {- memset(data, 't', i);- } else {- for (int b = 0; b < i; ++b) {- data[b] = b % 256;- }- }-- QByteArray ba = QByteArray::number(i)+'-'+QByteArray(1, canary[c]).toHex()+'-'+QByteArray(fillMethodName[f]);- QTest::newRow(ba.constData())- << data << i << canary[c];- }- }- }-}--void TestKoLZF::testArrayRoundtripDifferentSizes()-{- QFETCH(char*, data);- QFETCH(int, size);- QFETCH(char, canary);-- char * compressedData = new char[size+1];- compressedData[size] = canary;-- // try- const int compressedDataLength = KoLZF::compress(data, size, compressedData, size);-- QCOMPARE(compressedData[size], canary);-- const bool compressed = (compressedDataLength != 0);- // done with testing if not compressed- if (!compressed) {- delete [] data;- delete [] compressedData;- return;- }-- // now try uncompressing- char * uncompressedData = new char[size+1];- uncompressedData[size] = canary;-- const int uncompressedDataLength =- KoLZF::decompress(compressedData, compressedDataLength, uncompressedData, size);-- QVERIFY(uncompressedDataLength != 0);- QCOMPARE(uncompressedDataLength, size);- QCOMPARE(memcmp(uncompressedData, data, size), 0 );- QCOMPARE(uncompressedData[size], canary);-- delete [] data;- delete [] compressedData;- delete [] uncompressedData;-}---void TestKoLZF::testByteArrayCompressionEmpty()-{- const QByteArray empty;-- // try- const QByteArray compressed = KoLZF::compress(empty);-- // only metadata- QCOMPARE(compressed.size(), 5);- // size is 0- QCOMPARE(compressed.at(0), static_cast<char>(0));- QCOMPARE(compressed.at(1), static_cast<char>(0));- QCOMPARE(compressed.at(2), static_cast<char>(0));- QCOMPARE(compressed.at(3), static_cast<char>(0));- // uncompressed- QCOMPARE(compressed.at(4), static_cast<char>(0));-}--void TestKoLZF::testByteArrayDecompressionEmpty()-{- const char emptyCompressedRaw[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };- const QByteArray emptyCompressed = QByteArray::fromRawData(emptyCompressedRaw, 5);-- QByteArray uncompressed;-- // try- KoLZF::decompress(emptyCompressed, uncompressed);-- // empty- QCOMPARE(uncompressed.size(), 0);-}--void TestKoLZF::testByteArrayRoundtripDifferentSizes_data()-{- QTest::addColumn<QByteArray>("data");-- static const int fillMethodCount = 2;- static const char * const fillMethodName[fillMethodCount] = {"uni", "series"};-- for(int i = 0; i < 512; ++i) {- for (int f = 0; f < 2; ++f) {- QByteArray data;-- if (f == 0) {- data = QByteArray(i, 't');- } else {- data.resize(i);- for (int b = 0; b < i; ++b) {- data[b] = b % 256;- }- }- QByteArray ba = QByteArray::number(i)+'-'+fillMethodName[f];- QTest::newRow(ba.constData()) << data;- }- }-}--void TestKoLZF::testByteArrayRoundtripDifferentSizes()-{- QFETCH(QByteArray, data);-- // try- const QByteArray compressed = KoLZF::compress(data);- QByteArray uncompressed;- KoLZF::decompress(compressed, uncompressed);-- QCOMPARE(uncompressed, data);-}---QTEST_GUILESS_MAIN(TestKoLZF)diff --git a/libs/store/tests/TestKoLZF.h b/libs/store/tests/TestKoLZF.hdeleted file mode 100644index 6139ac8126..0000000000--- a/libs/store/tests/TestKoLZF.h+++ /dev/null@@ -1,35 +0,0 @@-/* This file is part of the KDE project- * SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>- *- * SPDX-License-Identifier: LGPL-2.0-or-later- */--#ifndef TESTKOLZF_H-#define TESTKOLZF_H--// Qt-#include <QObject>--class TestKoLZF : public QObject-{- Q_OBJECT--private Q_SLOTS:- void testArrayCompressionEmpty_data();- void testArrayCompressionEmpty();- void testArrayCompressionNullPointerInput();- void testArrayCompressionNullPointerOutput();- void testArrayDecompressionEmpty_data();- void testArrayDecompressionEmpty();- void testArrayDecompressionNullPointerInput();- void testArrayDecompressionNullPointerOutput();- void testArrayRoundtripDifferentSizes_data();- void testArrayRoundtripDifferentSizes();-- void testByteArrayCompressionEmpty();- void testByteArrayDecompressionEmpty();- void testByteArrayRoundtripDifferentSizes_data();- void testByteArrayRoundtripDifferentSizes();-};--#endif