marshalls.h
1 /*************************************************************************/
2 /* marshalls.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef MARSHALLS_H
30 #define MARSHALLS_H
31 
32 #include "typedefs.h"
33 
34 #include "variant.h"
35 
43 
44  uint32_t i;
45  float f;
46 };
47 
49 
50  uint64_t l;
51  double d;
52 };
53 
54 static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
55 
56  for (int i=0;i<2;i++) {
57 
58  *p_arr=p_uint&0xFF;
59  p_arr++; p_uint>>=8;
60  }
61 
62  return sizeof( uint16_t );
63 }
64 
65 static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
66 
67  for (int i=0;i<4;i++) {
68 
69  *p_arr=p_uint&0xFF;
70  p_arr++; p_uint>>=8;
71  }
72 
73  return sizeof( uint32_t );
74 }
75 
76 static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
77 
78  MarshallFloat mf;
79  mf.f=p_float;
80  encode_uint32( mf.i, p_arr );
81 
82  return sizeof( uint32_t );
83 }
84 
85 static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
86 
87  for (int i=0;i<8;i++) {
88 
89  *p_arr=p_uint&0xFF;
90  p_arr++; p_uint>>=8;
91  }
92 
93  return sizeof(uint64_t);
94 }
95 
96 static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
97 
98  MarshallDouble md;
99  md.d=p_double;
100  encode_uint64( md.l, p_arr );
101 
102  return sizeof(uint64_t);
103 
104 }
105 
106 
107 static inline int encode_cstring(const char *p_string, uint8_t * p_data) {
108 
109  int len=0;
110 
111  while (*p_string) {
112 
113  if (p_data) {
114 
115  *p_data=(uint8_t)*p_string;
116  p_data++;
117  }
118  p_string++;
119  len++;
120  };
121 
122  if (p_data) *p_data = 0;
123  return len+1;
124 }
125 
126 static inline uint16_t decode_uint16(const uint8_t *p_arr) {
127 
128  uint16_t u=0;
129 
130  for (int i=0;i<2;i++) {
131 
132  uint16_t b = *p_arr;
133  b<<=(i*8);
134  u|=b;
135  p_arr++;
136  }
137 
138  return u;
139 }
140 
141 static inline uint32_t decode_uint32(const uint8_t *p_arr) {
142 
143  uint32_t u=0;
144 
145  for (int i=0;i<4;i++) {
146 
147  uint32_t b = *p_arr;
148  b<<=(i*8);
149  u|=b;
150  p_arr++;
151  }
152 
153  return u;
154 }
155 
156 static inline float decode_float(const uint8_t *p_arr) {
157 
158  MarshallFloat mf;
159  mf.i = decode_uint32(p_arr);
160  return mf.f;
161 }
162 
163 static inline uint64_t decode_uint64(const uint8_t *p_arr) {
164 
165  uint64_t u=0;
166 
167  for (int i=0;i<8;i++) {
168 
169  uint64_t b = (*p_arr)&0xFF;
170  b<<=(i*8);
171  u|=b;
172  p_arr++;
173  }
174 
175  return u;
176 }
177 
178 static inline double decode_double(const uint8_t *p_arr) {
179 
180  MarshallDouble md;
181  md.l = decode_uint64( p_arr );
182  return md.d;
183 
184 }
185 
186 
187 Error decode_variant(Variant& r_variant,const uint8_t *p_buffer, int p_len,int *r_len=NULL);
188 Error encode_variant(const Variant& p_variant, uint8_t *r_buffer, int &r_len);
189 
190 #endif
float f
float
Definition: marshalls.h:45
Definition: variant.h:74
double d
double
Definition: marshalls.h:51
uint32_t i
int
Definition: marshalls.h:44
uint64_t l
long long
Definition: marshalls.h:50
Definition: marshalls.h:48
Definition: marshalls.h:42