compression.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* compression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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 "global_config.h"
  32. #include "os/copymem.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. z_stream strm;
  51. strm.zalloc = zipio_alloc;
  52. strm.zfree = zipio_free;
  53. strm.opaque = Z_NULL;
  54. int level = GLOBAL_GET("compression/zlib/compression_level");
  55. int err = deflateInit(&strm, level);
  56. if (err != Z_OK)
  57. return -1;
  58. strm.avail_in = p_src_size;
  59. int aout = deflateBound(&strm, p_src_size);
  60. /*if (aout>p_src_size) {
  61. deflateEnd(&strm);
  62. return -1;
  63. }*/
  64. strm.avail_out = aout;
  65. strm.next_in = (Bytef *)p_src;
  66. strm.next_out = p_dst;
  67. deflate(&strm, Z_FINISH);
  68. aout = aout - strm.avail_out;
  69. deflateEnd(&strm);
  70. return aout;
  71. } break;
  72. case MODE_ZSTD: {
  73. int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD);
  74. int level = GLOBAL_GET("compression/zstd/compression_level");
  75. return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, level);
  76. } break;
  77. }
  78. ERR_FAIL_V(-1);
  79. }
  80. int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
  81. switch (p_mode) {
  82. case MODE_FASTLZ: {
  83. int ss = p_src_size + p_src_size * 6 / 100;
  84. if (ss < 66)
  85. ss = 66;
  86. return ss;
  87. } break;
  88. case MODE_DEFLATE: {
  89. z_stream strm;
  90. strm.zalloc = zipio_alloc;
  91. strm.zfree = zipio_free;
  92. strm.opaque = Z_NULL;
  93. int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
  94. if (err != Z_OK)
  95. return -1;
  96. int aout = deflateBound(&strm, p_src_size);
  97. deflateEnd(&strm);
  98. return aout;
  99. } break;
  100. case MODE_ZSTD: {
  101. return ZSTD_compressBound(p_src_size);
  102. } break;
  103. }
  104. ERR_FAIL_V(-1);
  105. }
  106. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  107. switch (p_mode) {
  108. case MODE_FASTLZ: {
  109. int ret_size = 0;
  110. if (p_dst_max_size < 16) {
  111. uint8_t dst[16];
  112. ret_size = fastlz_decompress(p_src, p_src_size, dst, 16);
  113. copymem(p_dst, dst, p_dst_max_size);
  114. } else {
  115. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  116. }
  117. return ret_size;
  118. } break;
  119. case MODE_DEFLATE: {
  120. z_stream strm;
  121. strm.zalloc = zipio_alloc;
  122. strm.zfree = zipio_free;
  123. strm.opaque = Z_NULL;
  124. strm.avail_in = 0;
  125. strm.next_in = Z_NULL;
  126. int err = inflateInit(&strm);
  127. ERR_FAIL_COND_V(err != Z_OK, -1);
  128. strm.avail_in = p_src_size;
  129. strm.avail_out = p_dst_max_size;
  130. strm.next_in = (Bytef *)p_src;
  131. strm.next_out = p_dst;
  132. err = inflate(&strm, Z_FINISH);
  133. int total = strm.total_out;
  134. inflateEnd(&strm);
  135. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  136. return total;
  137. } break;
  138. case MODE_ZSTD: {
  139. return ZSTD_decompress(p_dst, p_dst_max_size, p_src, p_src_size);
  140. } break;
  141. }
  142. ERR_FAIL_V(-1);
  143. }