marshalls.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*************************************************************************/
  2. /* marshalls.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef MARSHALLS_H
  31. #define MARSHALLS_H
  32. #include "typedefs.h"
  33. #include "variant.h"
  34. /**
  35. * Miscelaneous helpers for marshalling data types, and encoding
  36. * in an endian independent way
  37. */
  38. union MarshallFloat {
  39. uint32_t i; ///< int
  40. float f; ///< float
  41. };
  42. union MarshallDouble {
  43. uint64_t l; ///< long long
  44. double d; ///< double
  45. };
  46. static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
  47. for (int i = 0; i < 2; i++) {
  48. *p_arr = p_uint & 0xFF;
  49. p_arr++;
  50. p_uint >>= 8;
  51. }
  52. return sizeof(uint16_t);
  53. }
  54. static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) {
  55. for (int i = 0; i < 4; i++) {
  56. *p_arr = p_uint & 0xFF;
  57. p_arr++;
  58. p_uint >>= 8;
  59. }
  60. return sizeof(uint32_t);
  61. }
  62. static inline unsigned int encode_float(float p_float, uint8_t *p_arr) {
  63. MarshallFloat mf;
  64. mf.f = p_float;
  65. encode_uint32(mf.i, p_arr);
  66. return sizeof(uint32_t);
  67. }
  68. static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) {
  69. for (int i = 0; i < 8; i++) {
  70. *p_arr = p_uint & 0xFF;
  71. p_arr++;
  72. p_uint >>= 8;
  73. }
  74. return sizeof(uint64_t);
  75. }
  76. static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
  77. MarshallDouble md;
  78. md.d = p_double;
  79. encode_uint64(md.l, p_arr);
  80. return sizeof(uint64_t);
  81. }
  82. static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
  83. int len = 0;
  84. while (*p_string) {
  85. if (p_data) {
  86. *p_data = (uint8_t)*p_string;
  87. p_data++;
  88. }
  89. p_string++;
  90. len++;
  91. };
  92. if (p_data) *p_data = 0;
  93. return len + 1;
  94. }
  95. static inline uint16_t decode_uint16(const uint8_t *p_arr) {
  96. uint16_t u = 0;
  97. for (int i = 0; i < 2; i++) {
  98. uint16_t b = *p_arr;
  99. b <<= (i * 8);
  100. u |= b;
  101. p_arr++;
  102. }
  103. return u;
  104. }
  105. static inline uint32_t decode_uint32(const uint8_t *p_arr) {
  106. uint32_t u = 0;
  107. for (int i = 0; i < 4; i++) {
  108. uint32_t b = *p_arr;
  109. b <<= (i * 8);
  110. u |= b;
  111. p_arr++;
  112. }
  113. return u;
  114. }
  115. static inline float decode_float(const uint8_t *p_arr) {
  116. MarshallFloat mf;
  117. mf.i = decode_uint32(p_arr);
  118. return mf.f;
  119. }
  120. static inline uint64_t decode_uint64(const uint8_t *p_arr) {
  121. uint64_t u = 0;
  122. for (int i = 0; i < 8; i++) {
  123. uint64_t b = (*p_arr) & 0xFF;
  124. b <<= (i * 8);
  125. u |= b;
  126. p_arr++;
  127. }
  128. return u;
  129. }
  130. static inline double decode_double(const uint8_t *p_arr) {
  131. MarshallDouble md;
  132. md.l = decode_uint64(p_arr);
  133. return md.d;
  134. }
  135. Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = NULL);
  136. Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len);
  137. #endif