compression.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* compression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. strm.avail_in = p_src_size;
  60. int aout = deflateBound(&strm, p_src_size);
  61. strm.avail_out = aout;
  62. strm.next_in = (Bytef *)p_src;
  63. strm.next_out = p_dst;
  64. deflate(&strm, Z_FINISH);
  65. aout = aout - strm.avail_out;
  66. deflateEnd(&strm);
  67. return aout;
  68. } break;
  69. case MODE_ZSTD: {
  70. ZSTD_CCtx *cctx = ZSTD_createCCtx();
  71. ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, zstd_level);
  72. if (zstd_long_distance_matching) {
  73. ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, 1);
  74. ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, zstd_window_log_size);
  75. }
  76. int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD);
  77. int ret = ZSTD_compressCCtx(cctx, p_dst, max_dst_size, p_src, p_src_size, zstd_level);
  78. ZSTD_freeCCtx(cctx);
  79. return ret;
  80. } break;
  81. }
  82. ERR_FAIL_V(-1);
  83. }
  84. int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
  85. switch (p_mode) {
  86. case MODE_FASTLZ: {
  87. int ss = p_src_size + p_src_size * 6 / 100;
  88. if (ss < 66)
  89. ss = 66;
  90. return ss;
  91. } break;
  92. case MODE_DEFLATE:
  93. case MODE_GZIP: {
  94. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  95. z_stream strm;
  96. strm.zalloc = zipio_alloc;
  97. strm.zfree = zipio_free;
  98. strm.opaque = Z_NULL;
  99. int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  100. if (err != Z_OK)
  101. return -1;
  102. int aout = deflateBound(&strm, p_src_size);
  103. deflateEnd(&strm);
  104. return aout;
  105. } break;
  106. case MODE_ZSTD: {
  107. return ZSTD_compressBound(p_src_size);
  108. } break;
  109. }
  110. ERR_FAIL_V(-1);
  111. }
  112. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  113. switch (p_mode) {
  114. case MODE_FASTLZ: {
  115. int ret_size = 0;
  116. if (p_dst_max_size < 16) {
  117. uint8_t dst[16];
  118. ret_size = fastlz_decompress(p_src, p_src_size, dst, 16);
  119. memcpy(p_dst, dst, p_dst_max_size);
  120. } else {
  121. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  122. }
  123. return ret_size;
  124. } break;
  125. case MODE_DEFLATE:
  126. case MODE_GZIP: {
  127. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  128. z_stream strm;
  129. strm.zalloc = zipio_alloc;
  130. strm.zfree = zipio_free;
  131. strm.opaque = Z_NULL;
  132. strm.avail_in = 0;
  133. strm.next_in = Z_NULL;
  134. int err = inflateInit2(&strm, window_bits);
  135. ERR_FAIL_COND_V(err != Z_OK, -1);
  136. strm.avail_in = p_src_size;
  137. strm.avail_out = p_dst_max_size;
  138. strm.next_in = (Bytef *)p_src;
  139. strm.next_out = p_dst;
  140. err = inflate(&strm, Z_FINISH);
  141. int total = strm.total_out;
  142. inflateEnd(&strm);
  143. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  144. return total;
  145. } break;
  146. case MODE_ZSTD: {
  147. ZSTD_DCtx *dctx = ZSTD_createDCtx();
  148. if (zstd_long_distance_matching) {
  149. ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, zstd_window_log_size);
  150. }
  151. int ret = ZSTD_decompressDCtx(dctx, p_dst, p_dst_max_size, p_src, p_src_size);
  152. ZSTD_freeDCtx(dctx);
  153. return ret;
  154. } break;
  155. }
  156. ERR_FAIL_V(-1);
  157. }
  158. int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
  159. int Compression::gzip_level = Z_DEFAULT_COMPRESSION;
  160. int Compression::zstd_level = 3;
  161. bool Compression::zstd_long_distance_matching = false;
  162. int Compression::zstd_window_log_size = 27; // ZSTD_WINDOWLOG_LIMIT_DEFAULT