You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

upb.h 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * This file contains shared definitions that are widely used across upb.
  29. */
  30. #ifndef UPB_H_
  31. #define UPB_H_
  32. #include <assert.h>
  33. #include <stdarg.h>
  34. #include <stdbool.h>
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <string.h>
  38. // TODO(b/232091617): Remove these and fix everything that breaks as a result.
  39. #include "upb/arena.h"
  40. #include "upb/status.h"
  41. // Must be last.
  42. #include "upb/port_def.inc"
  43. #ifdef __cplusplus
  44. extern "C"
  45. {
  46. #endif
  47. /** upb_StringView ************************************************************/
  48. typedef struct
  49. {
  50. const char* data;
  51. size_t size;
  52. } upb_StringView;
  53. UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char* data, size_t size)
  54. {
  55. upb_StringView ret;
  56. ret.data = data;
  57. ret.size = size;
  58. return ret;
  59. }
  60. UPB_INLINE upb_StringView upb_StringView_FromString(const char* data)
  61. {
  62. return upb_StringView_FromDataAndSize(data, strlen(data));
  63. }
  64. UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b)
  65. {
  66. return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
  67. }
  68. #define UPB_STRINGVIEW_INIT(ptr, len) \
  69. { \
  70. ptr, len \
  71. }
  72. #define UPB_STRINGVIEW_FORMAT "%.*s"
  73. #define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data
  74. /* Constants ******************************************************************/
  75. /* A list of types as they are encoded on-the-wire. */
  76. typedef enum
  77. {
  78. kUpb_WireType_Varint = 0,
  79. kUpb_WireType_64Bit = 1,
  80. kUpb_WireType_Delimited = 2,
  81. kUpb_WireType_StartGroup = 3,
  82. kUpb_WireType_EndGroup = 4,
  83. kUpb_WireType_32Bit = 5
  84. } upb_WireType;
  85. /* The types a field can have. Note that this list is not identical to the
  86. * types defined in descriptor.proto, which gives INT32 and SINT32 separate
  87. * types (we distinguish the two with the "integer encoding" enum below). */
  88. typedef enum
  89. {
  90. kUpb_CType_Bool = 1,
  91. kUpb_CType_Float = 2,
  92. kUpb_CType_Int32 = 3,
  93. kUpb_CType_UInt32 = 4,
  94. kUpb_CType_Enum = 5, /* Enum values are int32. */
  95. kUpb_CType_Message = 6,
  96. kUpb_CType_Double = 7,
  97. kUpb_CType_Int64 = 8,
  98. kUpb_CType_UInt64 = 9,
  99. kUpb_CType_String = 10,
  100. kUpb_CType_Bytes = 11
  101. } upb_CType;
  102. /* The repeated-ness of each field; this matches descriptor.proto. */
  103. typedef enum
  104. {
  105. kUpb_Label_Optional = 1,
  106. kUpb_Label_Required = 2,
  107. kUpb_Label_Repeated = 3
  108. } upb_Label;
  109. /* Descriptor types, as defined in descriptor.proto. */
  110. typedef enum
  111. {
  112. kUpb_FieldType_Double = 1,
  113. kUpb_FieldType_Float = 2,
  114. kUpb_FieldType_Int64 = 3,
  115. kUpb_FieldType_UInt64 = 4,
  116. kUpb_FieldType_Int32 = 5,
  117. kUpb_FieldType_Fixed64 = 6,
  118. kUpb_FieldType_Fixed32 = 7,
  119. kUpb_FieldType_Bool = 8,
  120. kUpb_FieldType_String = 9,
  121. kUpb_FieldType_Group = 10,
  122. kUpb_FieldType_Message = 11,
  123. kUpb_FieldType_Bytes = 12,
  124. kUpb_FieldType_UInt32 = 13,
  125. kUpb_FieldType_Enum = 14,
  126. kUpb_FieldType_SFixed32 = 15,
  127. kUpb_FieldType_SFixed64 = 16,
  128. kUpb_FieldType_SInt32 = 17,
  129. kUpb_FieldType_SInt64 = 18
  130. } upb_FieldType;
  131. #define kUpb_Map_Begin ((size_t)-1)
  132. UPB_INLINE bool _upb_IsLittleEndian(void)
  133. {
  134. int x = 1;
  135. return *(char*)&x == 1;
  136. }
  137. UPB_INLINE uint32_t _upb_BigEndian_Swap32(uint32_t val)
  138. {
  139. if (_upb_IsLittleEndian())
  140. {
  141. return val;
  142. }
  143. else
  144. {
  145. return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
  146. ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
  147. }
  148. }
  149. UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val)
  150. {
  151. if (_upb_IsLittleEndian())
  152. {
  153. return val;
  154. }
  155. else
  156. {
  157. return ((uint64_t)_upb_BigEndian_Swap32((uint32_t)val) << 32) |
  158. _upb_BigEndian_Swap32((uint32_t)(val >> 32));
  159. }
  160. }
  161. UPB_INLINE int _upb_Log2Ceiling(int x)
  162. {
  163. if (x <= 1)
  164. return 0;
  165. #ifdef __GNUC__
  166. return 32 - __builtin_clz(x - 1);
  167. #else
  168. int lg2 = 0;
  169. while (1 << lg2 < x)
  170. lg2++;
  171. return lg2;
  172. #endif
  173. }
  174. UPB_INLINE int _upb_Log2CeilingSize(int x)
  175. {
  176. return 1 << _upb_Log2Ceiling(x);
  177. }
  178. #include "upb/port_undef.inc"
  179. #ifdef __cplusplus
  180. } /* extern "C" */
  181. #endif
  182. #endif /* UPB_H_ */