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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/copymem.h"
  33. #include "core/project_settings.h"
  34. #include "thirdparty/misc/fastlz.h"
  35. #include <zlib.h>
  36. #include <zstd.h>
  37. int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  38. switch (p_mode) {
  39. case MODE_FASTLZ: {
  40. if (p_src_size < 16) {
  41. uint8_t src[16];
  42. zeromem(&src[p_src_size], 16 - p_src_size);
  43. copymem(src, p_src, p_src_size);
  44. return fastlz_compress(src, 16, p_dst);
  45. } else {
  46. return fastlz_compress(p_src, p_src_size, p_dst);
  47. }
  48. } break;
  49. case MODE_DEFLATE:
  50. case MODE_GZIP: {
  51. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  52. z_stream strm;
  53. strm.zalloc = zipio_alloc;
  54. strm.zfree = zipio_free;
  55. strm.opaque = Z_NULL;
  56. int level = p_mode == MODE_DEFLATE ? zlib_level : gzip_level;
  57. int err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  58. if (err != Z_OK)
  59. return -1;
  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. return ss;
  92. } break;
  93. case MODE_DEFLATE:
  94. case MODE_GZIP: {
  95. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  96. z_stream strm;
  97. strm.zalloc = zipio_alloc;
  98. strm.zfree = zipio_free;
  99. strm.opaque = Z_NULL;
  100. int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  101. if (err != Z_OK)
  102. return -1;
  103. int aout = deflateBound(&strm, p_src_size);
  104. deflateEnd(&strm);
  105. return aout;
  106. } break;
  107. case MODE_ZSTD: {
  108. return ZSTD_compressBound(p_src_size);
  109. } break;
  110. }
  111. ERR_FAIL_V(-1);
  112. }
  113. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  114. switch (p_mode) {
  115. case MODE_FASTLZ: {
  116. int ret_size = 0;
  117. if (p_dst_max_size < 16) {
  118. uint8_t dst[16];
  119. ret_size = fastlz_decompress(p_src, p_src_size, dst, 16);
  120. copymem(p_dst, dst, p_dst_max_size);
  121. } else {
  122. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  123. }
  124. return ret_size;
  125. } break;
  126. case MODE_DEFLATE:
  127. case MODE_GZIP: {
  128. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  129. z_stream strm;
  130. strm.zalloc = zipio_alloc;
  131. strm.zfree = zipio_free;
  132. strm.opaque = Z_NULL;
  133. strm.avail_in = 0;
  134. strm.next_in = Z_NULL;
  135. int err = inflateInit2(&strm, window_bits);
  136. ERR_FAIL_COND_V(err != Z_OK, -1);
  137. strm.avail_in = p_src_size;
  138. strm.avail_out = p_dst_max_size;
  139. strm.next_in = (Bytef *)p_src;
  140. strm.next_out = p_dst;
  141. err = inflate(&strm, Z_FINISH);
  142. int total = strm.total_out;
  143. inflateEnd(&strm);
  144. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  145. return total;
  146. } break;
  147. case MODE_ZSTD: {
  148. ZSTD_DCtx *dctx = ZSTD_createDCtx();
  149. if (zstd_long_distance_matching) ZSTD_DCtx_setMaxWindowSize(dctx, (size_t)1 << zstd_window_log_size);
  150. int ret = ZSTD_decompressDCtx(dctx, p_dst, p_dst_max_size, p_src, p_src_size);
  151. ZSTD_freeDCtx(dctx);
  152. return ret;
  153. } break;
  154. }
  155. ERR_FAIL_V(-1);
  156. }
  157. int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
  158. int Compression::gzip_level = Z_DEFAULT_COMPRESSION;
  159. int Compression::zstd_level = 3;
  160. bool Compression::zstd_long_distance_matching = false;
  161. int Compression::zstd_window_log_size = 27;