1/**************************************************************************/
2/* marshalls.h */
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#ifndef MARSHALLS_H
32#define MARSHALLS_H
33
34#include "core/math/math_defs.h"
35#include "core/object/ref_counted.h"
36#include "core/typedefs.h"
37#include "core/variant/variant.h"
38
39// uintr_t is only for pairing with real_t, and we only need it in here.
40#ifdef REAL_T_IS_DOUBLE
41typedef uint64_t uintr_t;
42#else
43typedef uint32_t uintr_t;
44#endif
45
46/**
47 * Miscellaneous helpers for marshaling data types, and encoding
48 * in an endian independent way
49 */
50
51union MarshallFloat {
52 uint32_t i; ///< int
53 float f; ///< float
54};
55
56union MarshallDouble {
57 uint64_t l; ///< long long
58 double d; ///< double
59};
60
61// Behaves like one of the above, depending on compilation setting.
62union MarshallReal {
63 uintr_t i;
64 real_t r;
65};
66
67static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
68 for (int i = 0; i < 2; i++) {
69 *p_arr = p_uint & 0xFF;
70 p_arr++;
71 p_uint >>= 8;
72 }
73
74 return sizeof(uint16_t);
75}
76
77static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
78 for (int i = 0; i < 4; i++) {
79 *p_arr = p_uint & 0xFF;
80 p_arr++;
81 p_uint >>= 8;
82 }
83
84 return sizeof(uint32_t);
85}
86
87static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
88 MarshallFloat mf;
89 mf.f = p_float;
90 encode_uint32(mf.i, p_arr);
91
92 return sizeof(uint32_t);
93}
94
95static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
96 for (int i = 0; i < 8; i++) {
97 *p_arr = p_uint & 0xFF;
98 p_arr++;
99 p_uint >>= 8;
100 }
101
102 return sizeof(uint64_t);
103}
104
105static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
106 MarshallDouble md;
107 md.d = p_double;
108 encode_uint64(md.l, p_arr);
109
110 return sizeof(uint64_t);
111}
112
113static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
114 for (size_t i = 0; i < sizeof(uintr_t); i++) {
115 *p_arr = p_uint & 0xFF;
116 p_arr++;
117 p_uint >>= 8;
118 }
119
120 return sizeof(uintr_t);
121}
122
123static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
124 MarshallReal mr;
125 mr.r = p_real;
126 encode_uintr(mr.i, p_arr);
127
128 return sizeof(uintr_t);
129}
130
131static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
132 int len = 0;
133
134 while (*p_string) {
135 if (p_data) {
136 *p_data = (uint8_t)*p_string;
137 p_data++;
138 }
139 p_string++;
140 len++;
141 }
142
143 if (p_data) {
144 *p_data = 0;
145 }
146 return len + 1;
147}
148
149static inline uint16_t decode_uint16(const uint8_t *p_arr) {
150 uint16_t u = 0;
151
152 for (int i = 0; i < 2; i++) {
153 uint16_t b = *p_arr;
154 b <<= (i * 8);
155 u |= b;
156 p_arr++;
157 }
158
159 return u;
160}
161
162static inline uint32_t decode_uint32(const uint8_t *p_arr) {
163 uint32_t u = 0;
164
165 for (int i = 0; i < 4; i++) {
166 uint32_t b = *p_arr;
167 b <<= (i * 8);
168 u |= b;
169 p_arr++;
170 }
171
172 return u;
173}
174
175static inline float decode_float(const uint8_t *p_arr) {
176 MarshallFloat mf;
177 mf.i = decode_uint32(p_arr);
178 return mf.f;
179}
180
181static inline uint64_t decode_uint64(const uint8_t *p_arr) {
182 uint64_t u = 0;
183
184 for (int i = 0; i < 8; i++) {
185 uint64_t b = (*p_arr) & 0xFF;
186 b <<= (i * 8);
187 u |= b;
188 p_arr++;
189 }
190
191 return u;
192}
193
194static inline double decode_double(const uint8_t *p_arr) {
195 MarshallDouble md;
196 md.l = decode_uint64(p_arr);
197 return md.d;
198}
199
200class EncodedObjectAsID : public RefCounted {
201 GDCLASS(EncodedObjectAsID, RefCounted);
202
203 ObjectID id;
204
205protected:
206 static void _bind_methods();
207
208public:
209 void set_object_id(ObjectID p_id);
210 ObjectID get_object_id() const;
211
212 EncodedObjectAsID() {}
213};
214
215Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0);
216Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
217
218Vector<float> vector3_to_float32_array(const Vector3 *vecs, size_t count);
219
220#endif // MARSHALLS_H
221