1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-08-06 11:40:25 +00:00
QB64-PE/internal/c/parts/compression/compression.cpp

56 lines
2.1 KiB
C++
Raw Normal View History

//----------------------------------------------------------------------------------------------------
// QB64-PE Compression Library
// Powered by miniz (https://github.com/richgel999/miniz)
//-----------------------------------------------------------------------------------------------------
#include "compression.h"
#include "../../libqb.h"
#include "miniz.h"
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) {
uLongf filesize = (uint32_t)text->len; // length of the text
2022-05-06 04:02:21 +00:00
uLongf compsize = compressBound(filesize);
unsigned char *dest = (unsigned char *)malloc(compsize);
int32_t result = compress(dest, &compsize, text->chr, filesize);
2022-05-06 04:02:21 +00:00
qbs *ret = qbs_new(compsize, 1);
memcpy(ret->chr, dest, compsize);
free(dest);
return ret;
}
qbs *func__inflate(qbs *text, int64_t originalsize, int32_t passed) {
int32_t result = 0;
2022-05-06 04:02:21 +00:00
if (passed == 1) {
uLongf uncompsize = originalsize;
unsigned char *dest = (unsigned char *)malloc(originalsize);
int32_t result = uncompress(dest, &uncompsize, text->chr, text->len);
2022-05-06 04:02:21 +00:00
qbs *ret = qbs_new(uncompsize, 1);
memcpy(ret->chr, dest, uncompsize);
free(dest);
return ret;
2022-05-06 04:02:21 +00:00
} else {
uLongf uncompsize = 0;
unsigned char *dest;
do {
uncompsize = uncompsize + 10000000; // 10 mb original buffer, resized by 10 mb each pass until it's large enough to hold the uncompressed data.
dest = (unsigned char *)malloc(uncompsize);
result = uncompress(dest, &uncompsize, text->chr, text->len);
if (result == Z_BUF_ERROR)
free(dest); // if the buffer is too small, free the old buffer
} while (result == Z_BUF_ERROR); // and try again with a larger buffer
qbs *ret = qbs_new(uncompsize, 1);
memcpy(ret->chr, dest, uncompsize);
free(dest);
return ret;
}
}