mirror of
https://github.com/QB64-Phoenix-Edition/QB64pe.git
synced 2024-09-20 04:24:48 +00:00
Merge pull request #510 from a740g/compression-fixes
Improve func__deflate() & func__inflate()
This commit is contained in:
commit
de49e25475
3 changed files with 42 additions and 36 deletions
2
Makefile
2
Makefile
|
@ -324,8 +324,6 @@ else
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq ($(filter y,$(DEP_ZLIB)),)
|
ifneq ($(filter y,$(DEP_ZLIB)),)
|
||||||
EXE_LIBS += $(MINIZ_OBJS) $(COMPRESSION_OBJS)
|
|
||||||
|
|
||||||
LICENSE_IN_USE += miniz
|
LICENSE_IN_USE += miniz
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
@ -15,4 +15,11 @@ $(PATH_INTERNAL_C)/parts/compression/%.o: $(PATH_INTERNAL_C)/parts/compression/%
|
||||||
$(PATH_INTERNAL_C)/parts/compression/%.o: $(PATH_INTERNAL_C)/parts/compression/%.cpp
|
$(PATH_INTERNAL_C)/parts/compression/%.o: $(PATH_INTERNAL_C)/parts/compression/%.cpp
|
||||||
$(CXX) -O2 $(CXXFLAGS) -DDEPENDENCY_CONSOLE_ONLY -Wall $< -c -o $@
|
$(CXX) -O2 $(CXXFLAGS) -DDEPENDENCY_CONSOLE_ONLY -Wall $< -c -o $@
|
||||||
|
|
||||||
CLEAN_LIST += $(MINIZ_OBJS) $(COMPRESSION_OBJS)
|
COMPRESSION_LIB := $(PATH_INTERNAL_C)/parts/compression/compression.a
|
||||||
|
|
||||||
|
$(COMPRESSION_LIB): $(MINIZ_OBJS) $(COMPRESSION_OBJS)
|
||||||
|
$(AR) rcs $@ $(MINIZ_OBJS) $(COMPRESSION_OBJS)
|
||||||
|
|
||||||
|
EXE_LIBS += $(COMPRESSION_LIB)
|
||||||
|
|
||||||
|
CLEAN_LIST += $(COMPRESSION_LIB) $(MINIZ_OBJS) $(COMPRESSION_OBJS)
|
||||||
|
|
|
@ -7,52 +7,53 @@
|
||||||
|
|
||||||
#include "compression.h"
|
#include "compression.h"
|
||||||
#include "qbs.h"
|
#include "qbs.h"
|
||||||
|
|
||||||
#include "miniz.h"
|
#include "miniz.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
uint32_t func__adler32(qbs *text) {
|
uint32_t func__adler32(qbs *text) {
|
||||||
if (!text->len) return 1;
|
if (!text->len)
|
||||||
return (uint32_t) adler32(1, text->chr, text->len);
|
return 1;
|
||||||
|
return (uint32_t)adler32(1, text->chr, text->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t func__crc32(qbs *text) {
|
uint32_t func__crc32(qbs *text) {
|
||||||
if (!text->len) return 0;
|
if (!text->len)
|
||||||
return (uint32_t) crc32(0, text->chr, text->len);
|
return 0;
|
||||||
|
return (uint32_t)crc32(0, text->chr, text->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
qbs *func__deflate(qbs *text) {
|
qbs *func__deflate(qbs *text) {
|
||||||
uLongf filesize = (uint32_t)text->len; // length of the text
|
auto fileSize = uLongf(text->len);
|
||||||
uLongf compsize = compressBound(filesize);
|
auto compSize = compressBound(fileSize);
|
||||||
unsigned char *dest = (unsigned char *)malloc(compsize);
|
auto dest = qbs_new(compSize, 1); // compressing directly to the qbs gives us a performance boost
|
||||||
int32_t result = compress(dest, &compsize, text->chr, filesize);
|
compress(dest->chr, &compSize, text->chr, fileSize); // discard result because we do not do any error checking
|
||||||
qbs *ret = qbs_new(compsize, 1);
|
return qbs_left(dest, compSize);
|
||||||
memcpy(ret->chr, dest, compsize);
|
|
||||||
free(dest);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qbs *func__inflate(qbs *text, int64_t originalsize, int32_t passed) {
|
qbs *func__inflate(qbs *text, int64_t originalSize, int32_t passed) {
|
||||||
int32_t result = 0;
|
if (passed) {
|
||||||
if (passed == 1) {
|
// Passing negative values can do bad things to qbs
|
||||||
uLongf uncompsize = originalsize;
|
if (originalSize > 0) {
|
||||||
unsigned char *dest = (unsigned char *)malloc(originalsize);
|
auto uncompSize = uLongf(originalSize);
|
||||||
int32_t result = uncompress(dest, &uncompsize, text->chr, text->len);
|
auto dest = qbs_new(uncompSize, 1); // decompressing directly to the qbs gives us a performance boost
|
||||||
qbs *ret = qbs_new(uncompsize, 1);
|
uncompress(dest->chr, &uncompSize, text->chr, uLongf(text->len)); // discard result because we do not do any error checking
|
||||||
memcpy(ret->chr, dest, uncompsize);
|
return dest; // no size adjustment is done assuming the exact original size was passed
|
||||||
free(dest);
|
} else {
|
||||||
return ret;
|
return qbs_new(0, 1); // simply return an empty qbs if originalSize is zero or negative
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
uLongf uncompsize = 0;
|
static const uLongf InflateChunkSize = 10 * 1024 * 1024;
|
||||||
unsigned char *dest;
|
std::vector<Byte> dest; // to get rid of malloc() and free()
|
||||||
|
auto compSize = uLongf(text->len);
|
||||||
|
uLongf uncompSize = 0;
|
||||||
do {
|
do {
|
||||||
uncompsize = uncompsize + 10000000; // 10 mb original buffer, resized by 10 mb each pass until it's large enough to hold the uncompressed data.
|
uncompSize += InflateChunkSize; // 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);
|
dest.resize(uncompSize);
|
||||||
result = uncompress(dest, &uncompsize, text->chr, text->len);
|
} while (uncompress(&dest[0], &uncompSize, text->chr, compSize) == Z_BUF_ERROR); // and try again with a larger buffer
|
||||||
if (result == Z_BUF_ERROR)
|
auto ret = qbs_new(uncompSize, 1);
|
||||||
free(dest); // if the buffer is too small, free the old buffer
|
memcpy(ret->chr, &dest[0], uncompSize);
|
||||||
} 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;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue