Debian Codegraph @

core/io/stream_peer_gzip.cpp

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