summaryrefslogtreecommitdiff
path: root/core/io/stream_peer.cpp
blob: 27f8d4e88faacb5ed4d4463a627c360ac96d7f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*************************************************************************/
/*  stream_peer.cpp                                                      */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "stream_peer.h"

#include "core/io/marshalls.h"

Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) {
	int len = p_data.size();
	if (len == 0) {
		return OK;
	}
	const uint8_t *r = p_data.ptr();
	return put_data(&r[0], len);
}

Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) {
	Array ret;

	int len = p_data.size();
	if (len == 0) {
		ret.push_back(OK);
		ret.push_back(0);
		return ret;
	}

	const uint8_t *r = p_data.ptr();
	int sent;
	Error err = put_partial_data(&r[0], len, sent);

	if (err != OK) {
		sent = 0;
	}
	ret.push_back(err);
	ret.push_back(sent);
	return ret;
}

Array StreamPeer::_get_data(int p_bytes) {
	Array ret;

	Vector<uint8_t> data;
	data.resize(p_bytes);
	if (data.size() != p_bytes) {
		ret.push_back(ERR_OUT_OF_MEMORY);
		ret.push_back(Vector<uint8_t>());
		return ret;
	}

	uint8_t *w = data.ptrw();
	Error err = get_data(&w[0], p_bytes);

	ret.push_back(err);
	ret.push_back(data);
	return ret;
}

Array StreamPeer::_get_partial_data(int p_bytes) {
	Array ret;

	Vector<uint8_t> data;
	data.resize(p_bytes);
	if (data.size() != p_bytes) {
		ret.push_back(ERR_OUT_OF_MEMORY);
		ret.push_back(Vector<uint8_t>());
		return ret;
	}

	uint8_t *w = data.ptrw();
	int received;
	Error err = get_partial_data(&w[0], p_bytes, received);

	if (err != OK) {
		data.resize(0);
	} else if (received != data.size()) {
		data.resize(received);
	}

	ret.push_back(err);
	ret.push_back(data);
	return ret;
}

void StreamPeer::set_big_endian(bool p_big_endian) {
	big_endian = p_big_endian;
}

bool StreamPeer::is_big_endian_enabled() const {
	return big_endian;
}

void StreamPeer::put_u8(uint8_t p_val) {
	put_data((const uint8_t *)&p_val, 1);
}

void StreamPeer::put_8(int8_t p_val) {
	put_data((const uint8_t *)&p_val, 1);
}

void StreamPeer::put_u16(uint16_t p_val) {
	if (big_endian) {
		p_val = BSWAP16(p_val);
	}
	uint8_t buf[2];
	encode_uint16(p_val, buf);
	put_data(buf, 2);
}

void StreamPeer::put_16(int16_t p_val) {
	if (big_endian) {
		p_val = BSWAP16(p_val);
	}
	uint8_t buf[2];
	encode_uint16(p_val, buf);
	put_data(buf, 2);
}

void StreamPeer::put_u32(uint32_t p_val) {
	if (big_endian) {
		p_val = BSWAP32(p_val);
	}
	uint8_t buf[4];
	encode_uint32(p_val, buf);
	put_data(buf, 4);
}

void StreamPeer::put_32(int32_t p_val) {
	if (big_endian) {
		p_val = BSWAP32(p_val);
	}
	uint8_t buf[4];
	encode_uint32(p_val, buf);
	put_data(buf, 4);
}

void StreamPeer::put_u64(uint64_t p_val) {
	if (big_endian) {
		p_val = BSWAP64(p_val);
	}
	uint8_t buf[8];
	encode_uint64(p_val, buf);
	put_data(buf, 8);
}

void StreamPeer::put_64(int64_t p_val) {
	if (big_endian) {
		p_val = BSWAP64(p_val);
	}
	uint8_t buf[8];
	encode_uint64(p_val, buf);
	put_data(buf, 8);
}

void StreamPeer::put_float(float p_val) {
	uint8_t buf[4];

	encode_float(p_val, buf);
	if (big_endian) {
		uint32_t *p32 = (uint32_t *)buf;
		*p32 = BSWAP32(*p32);
	}

	put_data(buf, 4);
}

void StreamPeer::put_double(double p_val) {
	uint8_t buf[8];
	encode_double(p_val, buf);
	if (big_endian) {
		uint64_t *p64 = (uint64_t *)buf;
		*p64 = BSWAP64(*p64);
	}
	put_data(buf, 8);
}

void StreamPeer::put_string(const String &p_string) {
	CharString cs = p_string.ascii();
	put_u32(cs.length());
	put_data((const uint8_t *)cs.get_data(), cs.length());
}

void StreamPeer::put_utf8_string(const String &p_string) {
	CharString cs = p_string.utf8();
	put_u32(cs.length());
	put_data((const uint8_t *)cs.get_data(), cs.length());
}

void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) {
	int len = 0;
	Vector<uint8_t> buf;
	encode_variant(p_variant, nullptr, len, p_full_objects);
	buf.resize(len);
	put_32(len);
	encode_variant(p_variant, buf.ptrw(), len, p_full_objects);
	put_data(buf.ptr(), buf.size());
}

uint8_t StreamPeer::get_u8() {
	uint8_t buf[1];
	get_data(buf, 1);
	return buf[0];
}

int8_t StreamPeer::get_8() {
	uint8_t buf[1];
	get_data(buf, 1);
	return buf[0];
}

uint16_t StreamPeer::get_u16() {
	uint8_t buf[2];
	get_data(buf, 2);
	uint16_t r = decode_uint16(buf);
	if (big_endian) {
		r = BSWAP16(r);
	}
	return r;
}

int16_t StreamPeer::get_16() {
	uint8_t buf[2];
	get_data(buf, 2);
	uint16_t r = decode_uint16(buf);
	if (big_endian) {
		r = BSWAP16(r);
	}
	return r;
}

uint32_t StreamPeer::get_u32() {
	uint8_t buf[4];
	get_data(buf, 4);
	uint32_t r = decode_uint32(buf);
	if (big_endian) {
		r = BSWAP32(r);
	}
	return r;
}

int32_t StreamPeer::get_32() {
	uint8_t buf[4];
	get_data(buf, 4);
	uint32_t r = decode_uint32(buf);
	if (big_endian) {
		r = BSWAP32(r);
	}
	return r;
}

uint64_t StreamPeer::get_u64() {
	uint8_t buf[8];
	get_data(buf, 8);
	uint64_t r = decode_uint64(buf);
	if (big_endian) {
		r = BSWAP64(r);
	}
	return r;
}

int64_t StreamPeer::get_64() {
	uint8_t buf[8];
	get_data(buf, 8);
	uint64_t r = decode_uint64(buf);
	if (big_endian) {
		r = BSWAP64(r);
	}
	return r;
}

float StreamPeer::get_float() {
	uint8_t buf[4];
	get_data(buf, 4);

	if (big_endian) {
		uint32_t *p32 = (uint32_t *)buf;
		*p32 = BSWAP32(*p32);
	}

	return decode_float(buf);
}

double StreamPeer::get_double() {
	uint8_t buf[8];
	get_data(buf, 8);

	if (big_endian) {
		uint64_t *p64 = (uint64_t *)buf;
		*p64 = BSWAP64(*p64);
	}

	return decode_double(buf);
}

String StreamPeer::get_string(int p_bytes) {
	if (p_bytes < 0) {
		p_bytes = get_u32();
	}
	ERR_FAIL_COND_V(p_bytes < 0, String());

	Vector<char> buf;
	Error err = buf.resize(p_bytes + 1);
	ERR_FAIL_COND_V(err != OK, String());
	err = get_data((uint8_t *)&buf[0], p_bytes);
	ERR_FAIL_COND_V(err != OK, String());
	buf.write[p_bytes] = 0;
	return buf.ptr();
}

String StreamPeer::get_utf8_string(int p_bytes) {
	if (p_bytes < 0) {
		p_bytes = get_u32();
	}
	ERR_FAIL_COND_V(p_bytes < 0, String());

	Vector<uint8_t> buf;
	Error err = buf.resize(p_bytes);
	ERR_FAIL_COND_V(err != OK, String());
	err = get_data(buf.ptrw(), p_bytes);
	ERR_FAIL_COND_V(err != OK, String());

	String ret;
	ret.parse_utf8((const char *)buf.ptr(), buf.size());
	return ret;
}

Variant StreamPeer::get_var(bool p_allow_objects) {
	int len = get_32();
	Vector<uint8_t> var;
	Error err = var.resize(len);
	ERR_FAIL_COND_V(err != OK, Variant());
	err = get_data(var.ptrw(), len);
	ERR_FAIL_COND_V(err != OK, Variant());

	Variant ret;
	err = decode_variant(ret, var.ptr(), len, nullptr, p_allow_objects);
	ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");

	return ret;
}

void StreamPeer::_bind_methods() {
	ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data);
	ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data);

	ClassDB::bind_method(D_METHOD("get_data", "bytes"), &StreamPeer::_get_data);
	ClassDB::bind_method(D_METHOD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data);

	ClassDB::bind_method(D_METHOD("get_available_bytes"), &StreamPeer::get_available_bytes);

	ClassDB::bind_method(D_METHOD("set_big_endian", "enable"), &StreamPeer::set_big_endian);
	ClassDB::bind_method(D_METHOD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled);

	ClassDB::bind_method(D_METHOD("put_8", "value"), &StreamPeer::put_8);
	ClassDB::bind_method(D_METHOD("put_u8", "value"), &StreamPeer::put_u8);
	ClassDB::bind_method(D_METHOD("put_16", "value"), &StreamPeer::put_16);
	ClassDB::bind_method(D_METHOD("put_u16", "value"), &StreamPeer::put_u16);
	ClassDB::bind_method(D_METHOD("put_32", "value"), &StreamPeer::put_32);
	ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32);
	ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64);
	ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64);
	ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float);
	ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double);
	ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string);
	ClassDB::bind_method(D_METHOD("put_utf8_string", "value"), &StreamPeer::put_utf8_string);
	ClassDB::bind_method(D_METHOD("put_var", "value", "full_objects"), &StreamPeer::put_var, DEFVAL(false));

	ClassDB::bind_method(D_METHOD("get_8"), &StreamPeer::get_8);
	ClassDB::bind_method(D_METHOD("get_u8"), &StreamPeer::get_u8);
	ClassDB::bind_method(D_METHOD("get_16"), &StreamPeer::get_16);
	ClassDB::bind_method(D_METHOD("get_u16"), &StreamPeer::get_u16);
	ClassDB::bind_method(D_METHOD("get_32"), &StreamPeer::get_32);
	ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32);
	ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64);
	ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64);
	ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float);
	ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double);
	ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1));
	ClassDB::bind_method(D_METHOD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string, DEFVAL(-1));
	ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &StreamPeer::get_var, DEFVAL(false));

	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled");
}

////////////////////////////////

void StreamPeerBuffer::_bind_methods() {
	ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek);
	ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size);
	ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position);
	ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize);
	ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array);
	ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array);
	ClassDB::bind_method(D_METHOD("clear"), &StreamPeerBuffer::clear);
	ClassDB::bind_method(D_METHOD("duplicate"), &StreamPeerBuffer::duplicate);

	ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data_array"), "set_data_array", "get_data_array");
}

Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) {
	if (p_bytes <= 0) {
		return OK;
	}

	if (pointer + p_bytes > data.size()) {
		data.resize(pointer + p_bytes);
	}

	uint8_t *w = data.ptrw();
	memcpy(&w[pointer], p_data, p_bytes);

	pointer += p_bytes;
	return OK;
}

Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
	r_sent = p_bytes;
	return put_data(p_data, p_bytes);
}

Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) {
	int recv;
	get_partial_data(p_buffer, p_bytes, recv);
	if (recv != p_bytes) {
		return ERR_INVALID_PARAMETER;
	}

	return OK;
}

Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
	if (pointer + p_bytes > data.size()) {
		r_received = data.size() - pointer;
		if (r_received <= 0) {
			r_received = 0;
			return OK; //you got 0
		}
	} else {
		r_received = p_bytes;
	}

	const uint8_t *r = data.ptr();
	memcpy(p_buffer, r + pointer, r_received);

	pointer += r_received;
	// FIXME: return what? OK or ERR_*
	// return OK for now so we don't maybe return garbage
	return OK;
}

int StreamPeerBuffer::get_available_bytes() const {
	return data.size() - pointer;
}

void StreamPeerBuffer::seek(int p_pos) {
	ERR_FAIL_COND(p_pos < 0);
	ERR_FAIL_COND(p_pos > data.size());
	pointer = p_pos;
}

int StreamPeerBuffer::get_size() const {
	return data.size();
}

int StreamPeerBuffer::get_position() const {
	return pointer;
}

void StreamPeerBuffer::resize(int p_size) {
	data.resize(p_size);
}

void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) {
	data = p_data;
	pointer = 0;
}

Vector<uint8_t> StreamPeerBuffer::get_data_array() const {
	return data;
}

void StreamPeerBuffer::clear() {
	data.resize(0);
	pointer = 0;
}

Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
	Ref<StreamPeerBuffer> spb;
	spb.instantiate();
	spb->data = data;
	return spb;
}