stream_peer_gzip.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. clear();
  70. compressing = p_compress;
  71. rb.resize(nearest_shift(buffer_size - 1));
  72. buffer.resize(1024);
  73. // Create ctx.
  74. ctx = memalloc(sizeof(z_stream));
  75. z_stream &strm = *(z_stream *)ctx;
  76. strm.next_in = Z_NULL;
  77. strm.avail_in = 0;
  78. strm.zalloc = zipio_alloc;
  79. strm.zfree = zipio_free;
  80. strm.opaque = Z_NULL;
  81. int window_bits = p_is_deflate ? 15 : (15 + 16);
  82. int err = Z_OK;
  83. int level = Z_DEFAULT_COMPRESSION;
  84. if (compressing) {
  85. err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY);
  86. } else {
  87. err = inflateInit2(&strm, window_bits);
  88. }
  89. ERR_FAIL_COND_V(err != Z_OK, FAILED);
  90. return OK;
  91. }
  92. 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) {
  93. ERR_FAIL_COND_V(!ctx, ERR_UNCONFIGURED);
  94. z_stream &strm = *(z_stream *)ctx;
  95. strm.avail_in = p_src_size;
  96. strm.avail_out = p_dst_size;
  97. strm.next_in = (Bytef *)p_src;
  98. strm.next_out = (Bytef *)p_dst;
  99. int flush = p_close ? Z_FINISH : Z_NO_FLUSH;
  100. if (compressing) {
  101. int err = deflate(&strm, flush);
  102. ERR_FAIL_COND_V(err != (p_close ? Z_STREAM_END : Z_OK), FAILED);
  103. } else {
  104. int err = inflate(&strm, flush);
  105. ERR_FAIL_COND_V(err != Z_OK && err != Z_STREAM_END, FAILED);
  106. }
  107. r_out = p_dst_size - strm.avail_out;
  108. r_consumed = p_src_size - strm.avail_in;
  109. return OK;
  110. }
  111. Error StreamPeerGZIP::put_data(const uint8_t *p_data, int p_bytes) {
  112. int wrote = 0;
  113. Error err = put_partial_data(p_data, p_bytes, wrote);
  114. if (err != OK) {
  115. return err;
  116. }
  117. ERR_FAIL_COND_V(p_bytes != wrote, ERR_OUT_OF_MEMORY);
  118. return OK;
  119. }
  120. Error StreamPeerGZIP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  121. ERR_FAIL_COND_V(!ctx, ERR_UNCONFIGURED);
  122. ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER);
  123. // Ensure we have enough space in temporary buffer.
  124. if (buffer.size() < p_bytes) {
  125. buffer.resize(p_bytes);
  126. }
  127. r_sent = 0;
  128. while (r_sent < p_bytes && rb.space_left() > 1024) { // Keep the ring buffer size meaningful.
  129. int sent = 0;
  130. int to_write = 0;
  131. // Compress or decompress
  132. Error err = _process(buffer.ptrw(), MIN(buffer.size(), rb.space_left()), p_data + r_sent, p_bytes - r_sent, sent, to_write);
  133. if (err != OK) {
  134. return err;
  135. }
  136. // When decompressing, we might need to do another round.
  137. r_sent += sent;
  138. // We can't write more than this buffer is full.
  139. if (sent == 0 && to_write == 0) {
  140. return OK;
  141. }
  142. if (to_write) {
  143. // Copy to ring buffer.
  144. int wrote = rb.write(buffer.ptr(), to_write);
  145. ERR_FAIL_COND_V(wrote != to_write, ERR_BUG);
  146. }
  147. }
  148. return OK;
  149. }
  150. Error StreamPeerGZIP::get_data(uint8_t *p_buffer, int p_bytes) {
  151. int received = 0;
  152. Error err = get_partial_data(p_buffer, p_bytes, received);
  153. if (err != OK) {
  154. return err;
  155. }
  156. ERR_FAIL_COND_V(p_bytes != received, ERR_UNAVAILABLE);
  157. return OK;
  158. }
  159. Error StreamPeerGZIP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  160. ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER);
  161. r_received = MIN(p_bytes, rb.data_left());
  162. if (r_received == 0) {
  163. return OK;
  164. }
  165. int received = rb.read(p_buffer, r_received);
  166. ERR_FAIL_COND_V(received != r_received, ERR_BUG);
  167. return OK;
  168. }
  169. int StreamPeerGZIP::get_available_bytes() const {
  170. return rb.data_left();
  171. }
  172. Error StreamPeerGZIP::finish() {
  173. ERR_FAIL_COND_V(!ctx || !compressing, ERR_UNAVAILABLE);
  174. // Ensure we have enough space in temporary buffer.
  175. if (buffer.size() < 1024) {
  176. buffer.resize(1024); // 1024 should be more than enough.
  177. }
  178. int consumed = 0;
  179. int to_write = 0;
  180. Error err = _process(buffer.ptrw(), 1024, nullptr, 0, consumed, to_write, true); // compress
  181. if (err != OK) {
  182. return err;
  183. }
  184. int wrote = rb.write(buffer.ptr(), to_write);
  185. ERR_FAIL_COND_V(wrote != to_write, ERR_OUT_OF_MEMORY);
  186. return OK;
  187. }