compression.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*************************************************************************/
  2. /* compression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "zip_io.h"
  33. #include "thirdparty/misc/fastlz.h"
  34. #include <zlib.h>
  35. int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  36. switch (p_mode) {
  37. case MODE_FASTLZ: {
  38. if (p_src_size < 16) {
  39. uint8_t src[16];
  40. zeromem(&src[p_src_size], 16 - p_src_size);
  41. copymem(src, p_src, p_src_size);
  42. return fastlz_compress(src, 16, p_dst);
  43. } else {
  44. return fastlz_compress(p_src, p_src_size, p_dst);
  45. }
  46. } break;
  47. case MODE_DEFLATE: {
  48. z_stream strm;
  49. strm.zalloc = zipio_alloc;
  50. strm.zfree = zipio_free;
  51. strm.opaque = Z_NULL;
  52. int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
  53. if (err != Z_OK)
  54. return -1;
  55. strm.avail_in = p_src_size;
  56. int aout = deflateBound(&strm, p_src_size);
  57. strm.avail_out = aout;
  58. strm.next_in = (Bytef *)p_src;
  59. strm.next_out = p_dst;
  60. deflate(&strm, Z_FINISH);
  61. aout = aout - strm.avail_out;
  62. deflateEnd(&strm);
  63. return aout;
  64. } break;
  65. }
  66. ERR_FAIL_V(-1);
  67. }
  68. int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) {
  69. switch (p_mode) {
  70. case MODE_FASTLZ: {
  71. int ss = p_src_size + p_src_size * 6 / 100;
  72. if (ss < 66)
  73. ss = 66;
  74. return ss;
  75. } break;
  76. case MODE_DEFLATE: {
  77. z_stream strm;
  78. strm.zalloc = zipio_alloc;
  79. strm.zfree = zipio_free;
  80. strm.opaque = Z_NULL;
  81. int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
  82. if (err != Z_OK)
  83. return -1;
  84. int aout = deflateBound(&strm, p_src_size);
  85. deflateEnd(&strm);
  86. return aout;
  87. } break;
  88. }
  89. ERR_FAIL_V(-1);
  90. }
  91. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
  92. switch (p_mode) {
  93. case MODE_FASTLZ: {
  94. int ret_size = 0;
  95. if (p_dst_max_size < 16) {
  96. uint8_t dst[16];
  97. ret_size = fastlz_decompress(p_src, p_src_size, dst, 16);
  98. copymem(p_dst, dst, p_dst_max_size);
  99. } else {
  100. ret_size = fastlz_decompress(p_src, p_src_size, p_dst, p_dst_max_size);
  101. }
  102. return ret_size;
  103. } break;
  104. case MODE_DEFLATE: {
  105. z_stream strm;
  106. strm.zalloc = zipio_alloc;
  107. strm.zfree = zipio_free;
  108. strm.opaque = Z_NULL;
  109. strm.avail_in = 0;
  110. strm.next_in = Z_NULL;
  111. int err = inflateInit(&strm);
  112. ERR_FAIL_COND_V(err != Z_OK, -1);
  113. strm.avail_in = p_src_size;
  114. strm.avail_out = p_dst_max_size;
  115. strm.next_in = (Bytef *)p_src;
  116. strm.next_out = p_dst;
  117. err = inflate(&strm, Z_FINISH);
  118. int total = strm.total_out;
  119. inflateEnd(&strm);
  120. ERR_FAIL_COND_V(err != Z_STREAM_END, -1);
  121. return total;
  122. } break;
  123. }
  124. ERR_FAIL_V(-1);
  125. }