compression.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*************************************************************************/
  2. /* compression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/copymem.h"
  32. #include "project_settings.h"
  33. #include "zip_io.h"
  34. #include "thirdparty/misc/fastlz.h"
  35. #include "thirdparty/zstd/zstd.h"
  36. #include <zlib.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. int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD);
  72. return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, zstd_level);
  73. } break;
  74. }
  75. ERR_FAIL_V(-1);
  76. }
  77. int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
  78. switch (p_mode) {
  79. case MODE_FASTLZ: {
  80. int ss = p_src_size + p_src_size * 6 / 100;
  81. if (ss < 66)
  82. ss = 66;
  83. return ss;
  84. } break;
  85. case MODE_DEFLATE:
  86. case MODE_GZIP: {
  87. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  88. z_stream strm;
  89. strm.zalloc = zipio_alloc;
  90. strm.zfree = zipio_free;
  91. strm.opaque = Z_NULL;
  92. int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  93. if (err != Z_OK)
  94. return -1;
  95. int aout = deflateBound(&strm, p_src_size);
  96. deflateEnd(&strm);
  97. return aout;
  98. } break;
  99. case MODE_ZSTD: {
  100. return ZSTD_compressBound(p_src_size);
  101. } break;
  102. }
  103. ERR_FAIL_V(-1);
  104. }
  105. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  106. switch (p_mode) {
  107. case MODE_FASTLZ: {
  108. int ret_size = 0;
  109. if (p_dst_max_size < 16) {
  110. uint8_t dst[16];
  111. ret_size = fastlz_decompress(p_src, p_src_size, dst, 16);
  112. copymem(p_dst, dst, p_dst_max_size);
  113. } else {
  114. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  115. }
  116. return ret_size;
  117. } break;
  118. case MODE_DEFLATE:
  119. case MODE_GZIP: {
  120. int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
  121. z_stream strm;
  122. strm.zalloc = zipio_alloc;
  123. strm.zfree = zipio_free;
  124. strm.opaque = Z_NULL;
  125. strm.avail_in = 0;
  126. strm.next_in = Z_NULL;
  127. int err = inflateInit2(&strm, window_bits);
  128. ERR_FAIL_COND_V(err != Z_OK, -1);
  129. strm.avail_in = p_src_size;
  130. strm.avail_out = p_dst_max_size;
  131. strm.next_in = (Bytef *)p_src;
  132. strm.next_out = p_dst;
  133. err = inflate(&strm, Z_FINISH);
  134. int total = strm.total_out;
  135. inflateEnd(&strm);
  136. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  137. return total;
  138. } break;
  139. case MODE_ZSTD: {
  140. return ZSTD_decompress(p_dst, p_dst_max_size, p_src, p_src_size);
  141. } break;
  142. }
  143. ERR_FAIL_V(-1);
  144. }
  145. int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
  146. int Compression::gzip_level = Z_DEFAULT_COMPRESSION;
  147. int Compression::zstd_level = 3;