marshalls.h 4.5 KB

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