Merge pull request #9634 from vnen/gzip-compression
Add GZIP compression support
This commit is contained in:
commit
8691355158
6 changed files with 24 additions and 14 deletions
|
@ -1780,6 +1780,7 @@ void _File::_bind_methods() {
|
||||||
BIND_CONSTANT(COMPRESSION_FASTLZ);
|
BIND_CONSTANT(COMPRESSION_FASTLZ);
|
||||||
BIND_CONSTANT(COMPRESSION_DEFLATE);
|
BIND_CONSTANT(COMPRESSION_DEFLATE);
|
||||||
BIND_CONSTANT(COMPRESSION_ZSTD);
|
BIND_CONSTANT(COMPRESSION_ZSTD);
|
||||||
|
BIND_CONSTANT(COMPRESSION_GZIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
_File::_File() {
|
_File::_File() {
|
||||||
|
|
|
@ -372,7 +372,8 @@ public:
|
||||||
enum CompressionMode {
|
enum CompressionMode {
|
||||||
COMPRESSION_FASTLZ = Compression::MODE_FASTLZ,
|
COMPRESSION_FASTLZ = Compression::MODE_FASTLZ,
|
||||||
COMPRESSION_DEFLATE = Compression::MODE_DEFLATE,
|
COMPRESSION_DEFLATE = Compression::MODE_DEFLATE,
|
||||||
COMPRESSION_ZSTD = Compression::MODE_ZSTD
|
COMPRESSION_ZSTD = Compression::MODE_ZSTD,
|
||||||
|
COMPRESSION_GZIP = Compression::MODE_GZIP
|
||||||
};
|
};
|
||||||
|
|
||||||
Error open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key);
|
Error open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key);
|
||||||
|
|
|
@ -974,6 +974,8 @@ GlobalConfig::GlobalConfig() {
|
||||||
custom_prop_info["compression/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
|
custom_prop_info["compression/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
|
||||||
GLOBAL_DEF("compression/zlib/compression_level", Z_DEFAULT_COMPRESSION);
|
GLOBAL_DEF("compression/zlib/compression_level", Z_DEFAULT_COMPRESSION);
|
||||||
custom_prop_info["compression/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
|
custom_prop_info["compression/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
|
||||||
|
GLOBAL_DEF("compression/gzip/compression_level", Z_DEFAULT_COMPRESSION);
|
||||||
|
custom_prop_info["compression/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
|
||||||
|
|
||||||
using_datapack = false;
|
using_datapack = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,23 +52,22 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,
|
||||||
}
|
}
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case MODE_DEFLATE: {
|
case MODE_DEFLATE:
|
||||||
|
case MODE_GZIP: {
|
||||||
|
|
||||||
|
int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
|
||||||
|
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
strm.zalloc = zipio_alloc;
|
strm.zalloc = zipio_alloc;
|
||||||
strm.zfree = zipio_free;
|
strm.zfree = zipio_free;
|
||||||
strm.opaque = Z_NULL;
|
strm.opaque = Z_NULL;
|
||||||
int level = GLOBAL_GET("compression/zlib/compression_level");
|
int level = p_mode == MODE_DEFLATE ? GLOBAL_GET("compression/zlib/compression_level") : GLOBAL_GET("compression/gzip/compression_level");
|
||||||
int err = deflateInit(&strm, level);
|
int err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
|
||||||
if (err != Z_OK)
|
if (err != Z_OK)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
strm.avail_in = p_src_size;
|
strm.avail_in = p_src_size;
|
||||||
int aout = deflateBound(&strm, p_src_size);
|
int aout = deflateBound(&strm, p_src_size);
|
||||||
/*if (aout>p_src_size) {
|
|
||||||
deflateEnd(&strm);
|
|
||||||
return -1;
|
|
||||||
}*/
|
|
||||||
strm.avail_out = aout;
|
strm.avail_out = aout;
|
||||||
strm.next_in = (Bytef *)p_src;
|
strm.next_in = (Bytef *)p_src;
|
||||||
strm.next_out = p_dst;
|
strm.next_out = p_dst;
|
||||||
|
@ -100,13 +99,16 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
|
||||||
return ss;
|
return ss;
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
case MODE_DEFLATE: {
|
case MODE_DEFLATE:
|
||||||
|
case MODE_GZIP: {
|
||||||
|
|
||||||
|
int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
|
||||||
|
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
strm.zalloc = zipio_alloc;
|
strm.zalloc = zipio_alloc;
|
||||||
strm.zfree = zipio_free;
|
strm.zfree = zipio_free;
|
||||||
strm.opaque = Z_NULL;
|
strm.opaque = Z_NULL;
|
||||||
int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
|
int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
|
||||||
if (err != Z_OK)
|
if (err != Z_OK)
|
||||||
return -1;
|
return -1;
|
||||||
int aout = deflateBound(&strm, p_src_size);
|
int aout = deflateBound(&strm, p_src_size);
|
||||||
|
@ -138,7 +140,10 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p
|
||||||
}
|
}
|
||||||
return ret_size;
|
return ret_size;
|
||||||
} break;
|
} break;
|
||||||
case MODE_DEFLATE: {
|
case MODE_DEFLATE:
|
||||||
|
case MODE_GZIP: {
|
||||||
|
|
||||||
|
int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
|
||||||
|
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
strm.zalloc = zipio_alloc;
|
strm.zalloc = zipio_alloc;
|
||||||
|
@ -146,7 +151,7 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p
|
||||||
strm.opaque = Z_NULL;
|
strm.opaque = Z_NULL;
|
||||||
strm.avail_in = 0;
|
strm.avail_in = 0;
|
||||||
strm.next_in = Z_NULL;
|
strm.next_in = Z_NULL;
|
||||||
int err = inflateInit(&strm);
|
int err = inflateInit2(&strm, window_bits);
|
||||||
ERR_FAIL_COND_V(err != Z_OK, -1);
|
ERR_FAIL_COND_V(err != Z_OK, -1);
|
||||||
|
|
||||||
strm.avail_in = p_src_size;
|
strm.avail_in = p_src_size;
|
||||||
|
|
|
@ -37,7 +37,8 @@ public:
|
||||||
enum Mode {
|
enum Mode {
|
||||||
MODE_FASTLZ,
|
MODE_FASTLZ,
|
||||||
MODE_DEFLATE,
|
MODE_DEFLATE,
|
||||||
MODE_ZSTD
|
MODE_ZSTD,
|
||||||
|
MODE_GZIP
|
||||||
};
|
};
|
||||||
|
|
||||||
static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD);
|
static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD);
|
||||||
|
|
|
@ -516,7 +516,7 @@ struct _VariantCall {
|
||||||
PoolByteArray compressed;
|
PoolByteArray compressed;
|
||||||
Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]);
|
Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]);
|
||||||
|
|
||||||
compressed.resize(Compression::get_max_compressed_buffer_size(ba->size()));
|
compressed.resize(Compression::get_max_compressed_buffer_size(ba->size(), mode));
|
||||||
int result = Compression::compress(compressed.write().ptr(), ba->read().ptr(), ba->size(), mode);
|
int result = Compression::compress(compressed.write().ptr(), ba->read().ptr(), ba->size(), mode);
|
||||||
|
|
||||||
result = result >= 0 ? result : 0;
|
result = result >= 0 ? result : 0;
|
||||||
|
|
Loading…
Reference in a new issue