compression.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "zlib.h"
  30. #include "os/copymem.h"
  31. #include "compression.h"
  32. #include "fastlz.h"
  33. #include "zip_io.h"
  34. int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,Mode p_mode) {
  35. switch(p_mode) {
  36. case MODE_FASTLZ: {
  37. if (p_src_size<16) {
  38. uint8_t src[16];
  39. zeromem(&src[p_src_size],16-p_src_size);
  40. copymem(src,p_src,p_src_size);
  41. return fastlz_compress(src,16,p_dst);
  42. } else {
  43. return fastlz_compress(p_src,p_src_size,p_dst);
  44. }
  45. } break;
  46. case MODE_DEFLATE: {
  47. z_stream strm;
  48. strm.zalloc = zipio_alloc;
  49. strm.zfree = zipio_free;
  50. strm.opaque = Z_NULL;
  51. int err = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
  52. if (err!=Z_OK)
  53. return -1;
  54. strm.avail_in=p_src_size;
  55. int aout = deflateBound(&strm,p_src_size);
  56. /*if (aout>p_src_size) {
  57. deflateEnd(&strm);
  58. return -1;
  59. }*/
  60. strm.avail_out=aout;
  61. strm.next_in=(Bytef*)p_src;
  62. strm.next_out=p_dst;
  63. deflate(&strm,Z_FINISH);
  64. aout = aout - strm.avail_out;
  65. deflateEnd(&strm);
  66. return aout;
  67. } break;
  68. }
  69. ERR_FAIL_V(-1);
  70. }
  71. int Compression::get_max_compressed_buffer_size(int p_src_size,Mode p_mode){
  72. switch(p_mode) {
  73. case MODE_FASTLZ: {
  74. int ss = p_src_size+p_src_size*6/100;
  75. if (ss<66)
  76. ss=66;
  77. return ss;
  78. } break;
  79. case MODE_DEFLATE: {
  80. z_stream strm;
  81. strm.zalloc = zipio_alloc;
  82. strm.zfree = zipio_free;
  83. strm.opaque = Z_NULL;
  84. int err = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
  85. if (err!=Z_OK)
  86. return -1;
  87. int aout = deflateBound(&strm,p_src_size);
  88. deflateEnd(&strm);
  89. return aout;
  90. } break;
  91. }
  92. ERR_FAIL_V(-1);
  93. }
  94. int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){
  95. switch(p_mode) {
  96. case MODE_FASTLZ: {
  97. int ret_size=0;
  98. if (p_dst_max_size<16) {
  99. uint8_t dst[16];
  100. ret_size = fastlz_decompress(p_src,p_src_size,dst,16);
  101. copymem(p_dst,dst,p_dst_max_size);
  102. } else {
  103. ret_size = fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size);
  104. }
  105. return ret_size;
  106. } break;
  107. case MODE_DEFLATE: {
  108. z_stream strm;
  109. strm.zalloc = zipio_alloc;
  110. strm.zfree = zipio_free;
  111. strm.opaque = Z_NULL;
  112. strm.avail_in= 0;
  113. strm.next_in=Z_NULL;
  114. int err = inflateInit(&strm);
  115. ERR_FAIL_COND_V(err!=Z_OK,-1);
  116. strm.avail_in=p_src_size;
  117. strm.avail_out=p_dst_max_size;
  118. strm.next_in=(Bytef*)p_src;
  119. strm.next_out=p_dst;
  120. err = inflate(&strm,Z_FINISH);
  121. int total = strm.total_out;
  122. inflateEnd(&strm);
  123. ERR_FAIL_COND_V(err!=Z_STREAM_END,-1);
  124. return total;
  125. } break;
  126. }
  127. ERR_FAIL_V(-1);
  128. }