1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-09-20 09:04:43 +00:00
QB64-PE/internal/c/parts/compression/compression.cpp

60 lines
2.5 KiB
C++
Raw Normal View History

//----------------------------------------------------------------------------------------------------
// QB64-PE Compression Library
// Powered by miniz (https://github.com/richgel999/miniz)
//-----------------------------------------------------------------------------------------------------
#include "libqb-common.h"
#include "compression.h"
#include "qbs.h"
#include "miniz.h"
#include <vector>
uint32_t func__adler32(qbs *text) {
if (!text->len)
return 1;
return (uint32_t)adler32(1, text->chr, text->len);
}
uint32_t func__crc32(qbs *text) {
if (!text->len)
return 0;
return (uint32_t)crc32(0, text->chr, text->len);
}
2022-05-06 04:02:21 +00:00
qbs *func__deflate(qbs *text) {
auto fileSize = uLongf(text->len);
auto compSize = compressBound(fileSize);
auto dest = qbs_new(compSize, 1); // compressing directly to the qbs gives us a performance boost
compress(dest->chr, &compSize, text->chr, fileSize); // discard result because we do not do any error checking
2024-06-22 07:04:20 +00:00
return qbs_left(dest, compSize);
2022-05-06 04:02:21 +00:00
}
qbs *func__inflate(qbs *text, int64_t originalSize, int32_t passed) {
if (passed) {
// Passing negative values can do bad things to qbs
if (originalSize > 0) {
auto uncompSize = uLongf(originalSize);
auto dest = qbs_new(uncompSize, 1); // decompressing directly to the qbs gives us a performance boost
uncompress(dest->chr, &uncompSize, text->chr, uLongf(text->len)); // discard result because we do not do any error checking
return dest; // no size adjustment is done assuming the exact original size was passed
} else {
return qbs_new(0, 1); // simply return an empty qbs if originalSize is zero or negative
}
2022-05-06 04:02:21 +00:00
} else {
static const uLongf InflateChunkSize = 10 * 1024 * 1024;
std::vector<Byte> dest; // to get rid of malloc() and free()
auto compSize = uLongf(text->len);
uLongf uncompSize = 0;
2022-05-06 04:02:21 +00:00
do {
uncompSize += InflateChunkSize; // 10 mb original buffer, resized by 10 mb each pass until it's large enough to hold the uncompressed data.
dest.resize(uncompSize);
} while (uncompress(&dest[0], &uncompSize, text->chr, compSize) == Z_BUF_ERROR); // and try again with a larger buffer
auto ret = qbs_new(uncompSize, 1);
memcpy(ret->chr, &dest[0], uncompSize);
2022-05-06 04:02:21 +00:00
return ret;
}
}