compression.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**************************************************************************/
  2. /* compression.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "compression.h"
  31. #include "core/io/zip_io.h"
  32. #include "core/project_settings.h"
  33. #include "thirdparty/misc/fastlz.h"
  34. #include <zlib.h>
  35. #include <zstd.h>
  36. int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  37. switch (p_mode) {
  38. case MODE_FASTLZ: {
  39. if (p_src_size < 16) {
  40. uint8_t src[16];
  41. memset(&src[p_src_size], 0, 16 - p_src_size);
  42. memcpy(src, p_src, p_src_size);
  43. return fastlz_compress(src, 16, p_dst);
  44. } else {
  45. return fastlz_compress(p_src, p_src_size, p_dst);
  46. }
  47. } break;
  48. case MODE_DEFLATE:
  49. case MODE_GZIP: {
  50. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  51. z_stream strm;
  52. strm.zalloc = zipio_alloc;
  53. strm.zfree = zipio_free;
  54. strm.opaque = Z_NULL;
  55. int level = p_mode == MODE_DEFLATE ? zlib_level : gzip_level;
  56. int err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  57. if (err != Z_OK) {
  58. return -1;
  59. }
  60. strm.avail_in = p_src_size;
  61. int aout = deflateBound(&strm, p_src_size);
  62. strm.avail_out = aout;
  63. strm.next_in = (Bytef *)p_src;
  64. strm.next_out = p_dst;
  65. deflate(&strm, Z_FINISH);
  66. aout = aout - strm.avail_out;
  67. deflateEnd(&strm);
  68. return aout;
  69. } break;
  70. case MODE_ZSTD: {
  71. ZSTD_CCtx *cctx = ZSTD_createCCtx();
  72. ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, zstd_level);
  73. if (zstd_long_distance_matching) {
  74. ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1);
  75. ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, zstd_window_log_size);
  76. }
  77. int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD);
  78. int ret = ZSTD_compressCCtx(cctx, p_dst, max_dst_size, p_src, p_src_size, zstd_level);
  79. ZSTD_freeCCtx(cctx);
  80. return ret;
  81. } break;
  82. }
  83. ERR_FAIL_V(-1);
  84. }
  85. int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
  86. switch (p_mode) {
  87. case MODE_FASTLZ: {
  88. int ss = p_src_size + p_src_size * 6 / 100;
  89. if (ss < 66) {
  90. ss = 66;
  91. }
  92. return ss;
  93. } break;
  94. case MODE_DEFLATE:
  95. case MODE_GZIP: {
  96. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  97. z_stream strm;
  98. strm.zalloc = zipio_alloc;
  99. strm.zfree = zipio_free;
  100. strm.opaque = Z_NULL;
  101. int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  102. if (err != Z_OK) {
  103. return -1;
  104. }
  105. int aout = deflateBound(&strm, p_src_size);
  106. deflateEnd(&strm);
  107. return aout;
  108. } break;
  109. case MODE_ZSTD: {
  110. return ZSTD_compressBound(p_src_size);
  111. } break;
  112. }
  113. ERR_FAIL_V(-1);
  114. }
  115. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  116. switch (p_mode) {
  117. case MODE_FASTLZ: {
  118. int ret_size = 0;
  119. if (p_dst_max_size < 16) {
  120. uint8_t dst[16];
  121. fastlz_decompress(p_src, p_src_size, dst, 16);
  122. memcpy(p_dst, dst, p_dst_max_size);
  123. ret_size = p_dst_max_size;
  124. } else {
  125. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  126. }
  127. return ret_size;
  128. } break;
  129. case MODE_DEFLATE:
  130. case MODE_GZIP: {
  131. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  132. z_stream strm;
  133. strm.zalloc = zipio_alloc;
  134. strm.zfree = zipio_free;
  135. strm.opaque = Z_NULL;
  136. strm.avail_in = 0;
  137. strm.next_in = Z_NULL;
  138. int err = inflateInit2(&strm, window_bits);
  139. ERR_FAIL_COND_V(err != Z_OK, -1);
  140. strm.avail_in = p_src_size;
  141. strm.avail_out = p_dst_max_size;
  142. strm.next_in = (Bytef *)p_src;
  143. strm.next_out = p_dst;
  144. err = inflate(&strm, Z_FINISH);
  145. int total = strm.total_out;
  146. inflateEnd(&strm);
  147. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  148. return total;
  149. } break;
  150. case MODE_ZSTD: {
  151. ZSTD_DCtx *dctx = ZSTD_createDCtx();
  152. if (zstd_long_distance_matching) {
  153. ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, zstd_window_log_size);
  154. }
  155. int ret = ZSTD_decompressDCtx(dctx, p_dst, p_dst_max_size, p_src, p_src_size);
  156. ZSTD_freeDCtx(dctx);
  157. return ret;
  158. } break;
  159. }
  160. ERR_FAIL_V(-1);
  161. }
  162. /**
  163. This will handle both Gzip and Deflat streams. It will automatically allocate the output buffer into the provided p_dst_vect Vector.
  164. This is required for compressed data who's final uncompressed size is unknown, as is the case for HTTP response bodies.
  165. This is much slower however than using Compression::decompress because it may result in multiple full copies of the output buffer.
  166. */
  167. int Compression::decompress_dynamic(PoolVector<uint8_t> *p_dst, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  168. int ret;
  169. uint8_t *dst = nullptr;
  170. int out_mark = 0;
  171. z_stream strm;
  172. ERR_FAIL_COND_V(p_src_size <= 0, Z_DATA_ERROR);
  173. // This function only supports GZip and Deflate
  174. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  175. ERR_FAIL_COND_V(p_mode != MODE_DEFLATE && p_mode != MODE_GZIP, Z_ERRNO);
  176. // Initialize the stream
  177. strm.zalloc = Z_NULL;
  178. strm.zfree = Z_NULL;
  179. strm.opaque = Z_NULL;
  180. strm.avail_in = 0;
  181. strm.next_in = Z_NULL;
  182. int err = inflateInit2(&strm, window_bits);
  183. ERR_FAIL_COND_V(err != Z_OK, -1);
  184. // Setup the stream inputs
  185. strm.next_in = (Bytef *)p_src;
  186. strm.avail_in = p_src_size;
  187. // Ensure the destination buffer is empty
  188. p_dst->resize(0);
  189. // decompress until deflate stream ends or end of file
  190. do {
  191. // Add another chunk size to the output buffer
  192. // This forces a copy of the whole buffer
  193. p_dst->resize(p_dst->size() + gzip_chunk);
  194. // Get pointer to the actual output buffer
  195. dst = p_dst->write().ptr();
  196. // Set the stream to the new output stream
  197. // Since it was copied, we need to reset the stream to the new buffer
  198. strm.next_out = &(dst[out_mark]);
  199. strm.avail_out = gzip_chunk;
  200. // run inflate() on input until output buffer is full and needs to be resized
  201. // or input runs out
  202. do {
  203. ret = inflate(&strm, Z_SYNC_FLUSH);
  204. switch (ret) {
  205. case Z_NEED_DICT:
  206. ret = Z_DATA_ERROR;
  207. FALLTHROUGH;
  208. case Z_DATA_ERROR:
  209. case Z_MEM_ERROR:
  210. case Z_STREAM_ERROR:
  211. case Z_BUF_ERROR:
  212. if (strm.msg) {
  213. WARN_PRINT(strm.msg);
  214. }
  215. (void)inflateEnd(&strm);
  216. p_dst->resize(0);
  217. return ret;
  218. }
  219. } while (strm.avail_out > 0 && strm.avail_in > 0);
  220. out_mark += gzip_chunk;
  221. // Encorce max output size
  222. if (p_max_dst_size > -1 && strm.total_out > (uint64_t)p_max_dst_size) {
  223. (void)inflateEnd(&strm);
  224. p_dst->resize(0);
  225. return Z_BUF_ERROR;
  226. }
  227. } while (ret != Z_STREAM_END);
  228. // If all done successfully, resize the output if it's larger than the actual output
  229. if (ret == Z_STREAM_END && (unsigned long)p_dst->size() > strm.total_out) {
  230. p_dst->resize(strm.total_out);
  231. }
  232. // clean up and return
  233. (void)inflateEnd(&strm);
  234. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  235. }
  236. int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
  237. int Compression::gzip_level = Z_DEFAULT_COMPRESSION;
  238. int Compression::zstd_level = 3;
  239. bool Compression::zstd_long_distance_matching = false;
  240. int Compression::zstd_window_log_size = 27; // ZSTD_WINDOWLOG_LIMIT_DEFAULT
  241. int Compression::gzip_chunk = 16384;