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.

decode.h 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * Internal implementation details of the decoder that are shared between
  29. * decode.c and decode_fast.c.
  30. */
  31. #ifndef UPB_INTERNAL_DECODE_H_
  32. #define UPB_INTERNAL_DECODE_H_
  33. #include <setjmp.h>
  34. #include "upb/decode.h"
  35. #include "upb/internal/upb.h"
  36. #include "upb/msg_internal.h"
  37. #include "third_party/utf8_range/utf8_range.h"
  38. /* Must be last. */
  39. #include "upb/port_def.inc"
  40. #define DECODE_NOGROUP (uint32_t) - 1
  41. typedef struct upb_Decoder {
  42. const char* end; /* Can read up to 16 bytes slop beyond this. */
  43. const char* limit_ptr; /* = end + UPB_MIN(limit, 0) */
  44. upb_Message* unknown_msg; /* If non-NULL, add unknown data at buffer flip. */
  45. const char* unknown; /* Start of unknown data. */
  46. const upb_ExtensionRegistry*
  47. extreg; /* For looking up extensions during the parse. */
  48. int limit; /* Submessage limit relative to end. */
  49. int depth; /* Tracks recursion depth to bound stack usage. */
  50. uint32_t end_group; /* field number of END_GROUP tag, else DECODE_NOGROUP */
  51. uint16_t options;
  52. bool missing_required;
  53. char patch[32];
  54. upb_Arena arena;
  55. jmp_buf err;
  56. #ifndef NDEBUG
  57. const char* debug_tagstart;
  58. const char* debug_valstart;
  59. #endif
  60. } upb_Decoder;
  61. /* Error function that will abort decoding with longjmp(). We can't declare this
  62. * UPB_NORETURN, even though it is appropriate, because if we do then compilers
  63. * will "helpfully" refuse to tailcall to it
  64. * (see: https://stackoverflow.com/a/55657013), which will defeat a major goal
  65. * of our optimizations. That is also why we must declare it in a separate file,
  66. * otherwise the compiler will see that it calls longjmp() and deduce that it is
  67. * noreturn. */
  68. const char* fastdecode_err(upb_Decoder* d, int status);
  69. extern const uint8_t upb_utf8_offsets[];
  70. UPB_INLINE
  71. bool decode_verifyutf8_inl(const char* ptr, int len) {
  72. const char* end = ptr + len;
  73. // Check 8 bytes at a time for any non-ASCII char.
  74. while (end - ptr >= 8) {
  75. uint64_t data;
  76. memcpy(&data, ptr, 8);
  77. if (data & 0x8080808080808080) goto non_ascii;
  78. ptr += 8;
  79. }
  80. // Check one byte at a time for non-ASCII.
  81. while (ptr < end) {
  82. if (*ptr & 0x80) goto non_ascii;
  83. ptr++;
  84. }
  85. return true;
  86. non_ascii:
  87. return utf8_range2((const unsigned char*)ptr, end - ptr) == 0;
  88. }
  89. const char* decode_checkrequired(upb_Decoder* d, const char* ptr,
  90. const upb_Message* msg,
  91. const upb_MiniTable* l);
  92. /* x86-64 pointers always have the high 16 bits matching. So we can shift
  93. * left 8 and right 8 without loss of information. */
  94. UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
  95. return ((intptr_t)tablep << 8) | tablep->table_mask;
  96. }
  97. UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
  98. return (const upb_MiniTable*)(table >> 8);
  99. }
  100. UPB_INLINE
  101. const char* decode_isdonefallback_inl(upb_Decoder* d, const char* ptr,
  102. int overrun, int* status) {
  103. if (overrun < d->limit) {
  104. /* Need to copy remaining data into patch buffer. */
  105. UPB_ASSERT(overrun < 16);
  106. if (d->unknown_msg) {
  107. if (!_upb_Message_AddUnknown(d->unknown_msg, d->unknown, ptr - d->unknown,
  108. &d->arena)) {
  109. *status = kUpb_DecodeStatus_OutOfMemory;
  110. return NULL;
  111. }
  112. d->unknown = &d->patch[0] + overrun;
  113. }
  114. memset(d->patch + 16, 0, 16);
  115. memcpy(d->patch, d->end, 16);
  116. ptr = &d->patch[0] + overrun;
  117. d->end = &d->patch[16];
  118. d->limit -= 16;
  119. d->limit_ptr = d->end + d->limit;
  120. d->options &= ~kUpb_DecodeOption_AliasString;
  121. UPB_ASSERT(ptr < d->limit_ptr);
  122. return ptr;
  123. } else {
  124. *status = kUpb_DecodeStatus_Malformed;
  125. return NULL;
  126. }
  127. }
  128. const char* decode_isdonefallback(upb_Decoder* d, const char* ptr, int overrun);
  129. UPB_INLINE
  130. bool decode_isdone(upb_Decoder* d, const char** ptr) {
  131. int overrun = *ptr - d->end;
  132. if (UPB_LIKELY(*ptr < d->limit_ptr)) {
  133. return false;
  134. } else if (UPB_LIKELY(overrun == d->limit)) {
  135. return true;
  136. } else {
  137. *ptr = decode_isdonefallback(d, *ptr, overrun);
  138. return false;
  139. }
  140. }
  141. #if UPB_FASTTABLE
  142. UPB_INLINE
  143. const char* fastdecode_tagdispatch(upb_Decoder* d, const char* ptr,
  144. upb_Message* msg, intptr_t table,
  145. uint64_t hasbits, uint64_t tag) {
  146. const upb_MiniTable* table_p = decode_totablep(table);
  147. uint8_t mask = table;
  148. uint64_t data;
  149. size_t idx = tag & mask;
  150. UPB_ASSUME((idx & 7) == 0);
  151. idx >>= 3;
  152. data = table_p->fasttable[idx].field_data ^ tag;
  153. UPB_MUSTTAIL return table_p->fasttable[idx].field_parser(d, ptr, msg, table,
  154. hasbits, data);
  155. }
  156. #endif
  157. UPB_INLINE uint32_t fastdecode_loadtag(const char* ptr) {
  158. uint16_t tag;
  159. memcpy(&tag, ptr, 2);
  160. return tag;
  161. }
  162. UPB_INLINE void decode_checklimit(upb_Decoder* d) {
  163. UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
  164. }
  165. UPB_INLINE int decode_pushlimit(upb_Decoder* d, const char* ptr, int size) {
  166. int limit = size + (int)(ptr - d->end);
  167. int delta = d->limit - limit;
  168. decode_checklimit(d);
  169. d->limit = limit;
  170. d->limit_ptr = d->end + UPB_MIN(0, limit);
  171. decode_checklimit(d);
  172. return delta;
  173. }
  174. UPB_INLINE void decode_poplimit(upb_Decoder* d, const char* ptr,
  175. int saved_delta) {
  176. UPB_ASSERT(ptr - d->end == d->limit);
  177. decode_checklimit(d);
  178. d->limit += saved_delta;
  179. d->limit_ptr = d->end + UPB_MIN(0, d->limit);
  180. decode_checklimit(d);
  181. }
  182. #include "upb/port_undef.inc"
  183. #endif /* UPB_INTERNAL_DECODE_H_ */