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