compression.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* compression.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 "compression.h"
  30. #include "fastlz.h"
  31. #include "zlib.h"
  32. #include "zip_io.h"
  33. #include "os/copymem.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. strm.avail_out=aout;
  57. strm.next_in=(Bytef*)p_src;
  58. strm.next_out=p_dst;
  59. deflate(&strm,Z_FINISH);
  60. aout = aout - strm.avail_out;
  61. deflateEnd(&strm);
  62. return aout;
  63. } break;
  64. }
  65. ERR_FAIL_V(-1);
  66. }
  67. int Compression::get_max_compressed_buffer_size(int p_src_size,Mode p_mode){
  68. switch(p_mode) {
  69. case MODE_FASTLZ: {
  70. int ss = p_src_size+p_src_size*6/100;
  71. if (ss<66)
  72. ss=66;
  73. return ss;
  74. } break;
  75. case MODE_DEFLATE: {
  76. z_stream strm;
  77. strm.zalloc = zipio_alloc;
  78. strm.zfree = zipio_free;
  79. strm.opaque = Z_NULL;
  80. int err = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
  81. if (err!=Z_OK)
  82. return -1;
  83. int aout = deflateBound(&strm,p_src_size);
  84. deflateEnd(&strm);
  85. return aout;
  86. } break;
  87. }
  88. ERR_FAIL_V(-1);
  89. }
  90. void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){
  91. switch(p_mode) {
  92. case MODE_FASTLZ: {
  93. if (p_dst_max_size<16) {
  94. uint8_t dst[16];
  95. fastlz_decompress(p_src,p_src_size,dst,16);
  96. copymem(p_dst,dst,p_dst_max_size);
  97. } else {
  98. fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size);
  99. }
  100. return;
  101. } break;
  102. case MODE_DEFLATE: {
  103. z_stream strm;
  104. strm.zalloc = zipio_alloc;
  105. strm.zfree = zipio_free;
  106. strm.opaque = Z_NULL;
  107. strm.avail_in= 0;
  108. strm.next_in=Z_NULL;
  109. int err = inflateInit(&strm);
  110. ERR_FAIL_COND(err!=Z_OK);
  111. strm.avail_in=p_src_size;
  112. strm.avail_out=p_dst_max_size;
  113. strm.next_in=(Bytef*)p_src;
  114. strm.next_out=p_dst;
  115. err = inflate(&strm,Z_FINISH);
  116. inflateEnd(&strm);
  117. ERR_FAIL_COND(err!=Z_STREAM_END);
  118. return;
  119. } break;
  120. }
  121. ERR_FAIL();
  122. }