stream_peer_gzip.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************/
  2. /* stream_peer_gzip.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/io/stream_peer_gzip.h"
  31. #include "core/io/zip_io.h"
  32. #include <zlib.h>
  33. void StreamPeerGZIP::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("start_compression", "use_deflate", "buffer_size"), &StreamPeerGZIP::start_compression, DEFVAL(false), DEFVAL(65535));
  35. ClassDB::bind_method(D_METHOD("start_decompression", "use_deflate", "buffer_size"), &StreamPeerGZIP::start_decompression, DEFVAL(false), DEFVAL(65535));
  36. ClassDB::bind_method(D_METHOD("finish"), &StreamPeerGZIP::finish);
  37. ClassDB::bind_method(D_METHOD("clear"), &StreamPeerGZIP::clear);
  38. }
  39. StreamPeerGZIP::StreamPeerGZIP() {
  40. }
  41. StreamPeerGZIP::~StreamPeerGZIP() {
  42. _close();
  43. }
  44. void StreamPeerGZIP::_close() {
  45. if (ctx) {
  46. z_stream *strm = (z_stream *)ctx;
  47. if (compressing) {
  48. deflateEnd(strm);
  49. } else {
  50. inflateEnd(strm);
  51. }
  52. memfree(strm);
  53. ctx = nullptr;
  54. }
  55. }
  56. void StreamPeerGZIP::clear() {
  57. _close();
  58. rb.clear();
  59. buffer.clear();
  60. }
  61. Error StreamPeerGZIP::start_compression(bool p_is_deflate, int buffer_size) {
  62. return _start(true, p_is_deflate, buffer_size);
  63. }
  64. Error StreamPeerGZIP::start_decompression(bool p_is_deflate, int buffer_size) {
  65. return _start(false, p_is_deflate, buffer_size);
  66. }
  67. Error StreamPeerGZIP::_start(bool p_compress, bool p_is_deflate, int buffer_size) {
  68. ERR_FAIL_COND_V(ctx != nullptr, ERR_ALREADY_IN_USE);
  69. ERR_FAIL_COND_V_MSG(buffer_size <= 0, ERR_INVALID_PARAMETER, "Invalid buffer size. It should be a positive integer.");
  70. clear();
  71. compressing = p_compress;
  72. rb.resize(nearest_shift(buffer_size - 1));
  73. buffer.resize(1024);
  74. // Create ctx.
  75. ctx = memalloc(sizeof(z_stream));
  76. z_stream &strm = *(z_stream *)ctx;
  77. strm.next_in = Z_NULL;
  78. strm.avail_in = 0;
  79. strm.zalloc = zipio_alloc;
  80. strm.zfree = zipio_free;
  81. strm.opaque = Z_NULL;
  82. int window_bits = p_is_deflate ? 15 : (15 + 16);
  83. int err = Z_OK;
  84. int level = Z_DEFAULT_COMPRESSION;
  85. if (compressing) {
  86. err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  87. } else {
  88. err = inflateInit2(&strm, window_bits);
  89. }
  90. ERR_FAIL_COND_V(err != Z_OK, FAILED);
  91. return OK;
  92. }
  93. Error StreamPeerGZIP::_process(uint8_t *p_dst, int p_dst_size, const uint8_t *p_src, int p_src_size, int &r_consumed, int &r_out, bool p_close) {
  94. ERR_FAIL_NULL_V(ctx, ERR_UNCONFIGURED);
  95. z_stream &strm = *(z_stream *)ctx;
  96. strm.avail_in = p_src_size;
  97. strm.avail_out = p_dst_size;
  98. strm.next_in = (Bytef *)p_src;
  99. strm.next_out = (Bytef *)p_dst;
  100. int flush = p_close ? Z_FINISH : Z_NO_FLUSH;
  101. if (compressing) {
  102. int err = deflate(&strm, flush);
  103. ERR_FAIL_COND_V(err != (p_close ? Z_STREAM_END : Z_OK), FAILED);
  104. } else {
  105. int err = inflate(&strm, flush);
  106. ERR_FAIL_COND_V(err != Z_OK && err != Z_STREAM_END, FAILED);
  107. }
  108. r_out = p_dst_size - strm.avail_out;
  109. r_consumed = p_src_size - strm.avail_in;
  110. return OK;
  111. }
  112. Error StreamPeerGZIP::put_data(const uint8_t *p_data, int p_bytes) {
  113. int wrote = 0;
  114. Error err = put_partial_data(p_data, p_bytes, wrote);
  115. if (err != OK) {
  116. return err;
  117. }
  118. ERR_FAIL_COND_V(p_bytes != wrote, ERR_OUT_OF_MEMORY);
  119. return OK;
  120. }
  121. Error StreamPeerGZIP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  122. ERR_FAIL_NULL_V(ctx, ERR_UNCONFIGURED);
  123. ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER);
  124. // Ensure we have enough space in temporary buffer.
  125. if (buffer.size() < p_bytes) {
  126. buffer.resize(p_bytes);
  127. }
  128. r_sent = 0;
  129. while (r_sent < p_bytes && rb.space_left() > 1024) { // Keep the ring buffer size meaningful.
  130. int sent = 0;
  131. int to_write = 0;
  132. // Compress or decompress
  133. Error err = _process(buffer.ptrw(), MIN(buffer.size(), rb.space_left()), p_data + r_sent, p_bytes - r_sent, sent, to_write);
  134. if (err != OK) {
  135. return err;
  136. }
  137. // When decompressing, we might need to do another round.
  138. r_sent += sent;
  139. // We can't write more than this buffer is full.
  140. if (sent == 0 && to_write == 0) {
  141. return OK;
  142. }
  143. if (to_write) {
  144. // Copy to ring buffer.
  145. int wrote = rb.write(buffer.ptr(), to_write);
  146. ERR_FAIL_COND_V(wrote != to_write, ERR_BUG);
  147. }
  148. }
  149. return OK;
  150. }
  151. Error StreamPeerGZIP::get_data(uint8_t *p_buffer, int p_bytes) {
  152. int received = 0;
  153. Error err = get_partial_data(p_buffer, p_bytes, received);
  154. if (err != OK) {
  155. return err;
  156. }
  157. ERR_FAIL_COND_V(p_bytes != received, ERR_UNAVAILABLE);
  158. return OK;
  159. }
  160. Error StreamPeerGZIP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  161. ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER);
  162. r_received = MIN(p_bytes, rb.data_left());
  163. if (r_received == 0) {
  164. return OK;
  165. }
  166. int received = rb.read(p_buffer, r_received);
  167. ERR_FAIL_COND_V(received != r_received, ERR_BUG);
  168. return OK;
  169. }
  170. int StreamPeerGZIP::get_available_bytes() const {
  171. return rb.data_left();
  172. }
  173. Error StreamPeerGZIP::finish() {
  174. ERR_FAIL_COND_V(!ctx || !compressing, ERR_UNAVAILABLE);
  175. // Ensure we have enough space in temporary buffer.
  176. if (buffer.size() < 1024) {
  177. buffer.resize(1024); // 1024 should be more than enough.
  178. }
  179. int consumed = 0;
  180. int to_write = 0;
  181. Error err = _process(buffer.ptrw(), 1024, nullptr, 0, consumed, to_write, true); // compress
  182. if (err != OK) {
  183. return err;
  184. }
  185. int wrote = rb.write(buffer.ptr(), to_write);
  186. ERR_FAIL_COND_V(wrote != to_write, ERR_OUT_OF_MEMORY);
  187. return OK;
  188. }