generated_message_table_driven_lite.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__
  31. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__
  32. #include <google/protobuf/generated_message_table_driven.h>
  33. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  34. #include <google/protobuf/extension_set.h>
  35. #include <google/protobuf/implicit_weak_message.h>
  36. #include <google/protobuf/inlined_string_field.h>
  37. #include <google/protobuf/metadata_lite.h>
  38. #include <google/protobuf/repeated_field.h>
  39. #include <google/protobuf/wire_format_lite.h>
  40. #include <google/protobuf/wire_format_lite_inl.h>
  41. #include <type_traits>
  42. namespace google {
  43. namespace protobuf {
  44. namespace internal {
  45. enum StringType {
  46. StringType_STRING = 0,
  47. StringType_INLINED = 3
  48. };
  49. // Logically a superset of StringType, consisting of all field types that
  50. // require special initialization.
  51. enum ProcessingType {
  52. ProcessingType_STRING = 0,
  53. ProcessingType_CORD = 1,
  54. ProcessingType_STRING_PIECE = 2,
  55. ProcessingType_INLINED = 3,
  56. ProcessingType_MESSAGE = 4,
  57. };
  58. enum Cardinality {
  59. Cardinality_SINGULAR = 0,
  60. Cardinality_REPEATED = 1,
  61. Cardinality_ONEOF = 3
  62. };
  63. template <typename Type>
  64. inline Type* Raw(MessageLite* msg, int64 offset) {
  65. return reinterpret_cast<Type*>(reinterpret_cast<uint8*>(msg) + offset);
  66. }
  67. template <typename Type>
  68. inline const Type* Raw(const MessageLite* msg, int64 offset) {
  69. return reinterpret_cast<const Type*>(reinterpret_cast<const uint8*>(msg) +
  70. offset);
  71. }
  72. template <typename InternalMetadata>
  73. inline Arena* GetArena(MessageLite* msg, int64 arena_offset) {
  74. if (GOOGLE_PREDICT_FALSE(arena_offset == -1)) {
  75. return NULL;
  76. }
  77. return Raw<InternalMetadata>(msg, arena_offset)->arena();
  78. }
  79. inline ExtensionSet* GetExtensionSet(MessageLite* msg, int64 extension_offset) {
  80. if (extension_offset == -1) {
  81. return NULL;
  82. }
  83. return Raw<ExtensionSet>(msg, extension_offset);
  84. }
  85. template <typename Type>
  86. inline Type* AddField(MessageLite* msg, int64 offset) {
  87. static_assert(std::is_pod<Type>::value ||
  88. std::is_same<Type, InlinedStringField>::value,
  89. "Do not assign");
  90. google::protobuf::RepeatedField<Type>* repeated =
  91. Raw<google::protobuf::RepeatedField<Type> >(msg, offset);
  92. return repeated->Add();
  93. }
  94. template <>
  95. inline string* AddField<string>(MessageLite* msg, int64 offset) {
  96. google::protobuf::RepeatedPtrField<string>* repeated =
  97. Raw<google::protobuf::RepeatedPtrField<string> >(msg, offset);
  98. return repeated->Add();
  99. }
  100. template <typename Type>
  101. inline void AddField(MessageLite* msg, int64 offset, Type value) {
  102. static_assert(std::is_pod<Type>::value,
  103. "Do not assign");
  104. *AddField<Type>(msg, offset) = value;
  105. }
  106. inline void SetBit(uint32* has_bits, uint32 has_bit_index) {
  107. GOOGLE_DCHECK(has_bits != nullptr);
  108. uint32 mask = static_cast<uint32>(1u) << (has_bit_index % 32);
  109. has_bits[has_bit_index / 32u] |= mask;
  110. }
  111. template <typename Type>
  112. inline Type* MutableField(MessageLite* msg, uint32* has_bits,
  113. uint32 has_bit_index, int64 offset) {
  114. SetBit(has_bits, has_bit_index);
  115. return Raw<Type>(msg, offset);
  116. }
  117. template <typename Type>
  118. inline void SetField(MessageLite* msg, uint32* has_bits, uint32 has_bit_index,
  119. int64 offset, Type value) {
  120. static_assert(std::is_pod<Type>::value,
  121. "Do not assign");
  122. *MutableField<Type>(msg, has_bits, has_bit_index, offset) = value;
  123. }
  124. template <typename Type>
  125. inline void SetOneofField(MessageLite* msg, uint32* oneof_case,
  126. uint32 oneof_case_index, int64 offset,
  127. int field_number, Type value) {
  128. oneof_case[oneof_case_index] = field_number;
  129. *Raw<Type>(msg, offset) = value;
  130. }
  131. // Clears a oneof field. The field argument should correspond to the particular
  132. // field that is currently set in the oneof.
  133. inline void ClearOneofField(const ParseTableField& field, Arena* arena,
  134. MessageLite* msg) {
  135. switch (field.processing_type & kTypeMask) {
  136. case WireFormatLite::TYPE_MESSAGE:
  137. if (arena == NULL) {
  138. delete *Raw<MessageLite*>(msg, field.offset);
  139. }
  140. break;
  141. case WireFormatLite::TYPE_STRING:
  142. case WireFormatLite::TYPE_BYTES:
  143. Raw<ArenaStringPtr>(msg, field.offset)
  144. ->Destroy(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), arena);
  145. break;
  146. case TYPE_STRING_INLINED:
  147. case TYPE_BYTES_INLINED:
  148. Raw<InlinedStringField>(msg, field.offset)->DestroyNoArena(NULL);
  149. break;
  150. default:
  151. // No cleanup needed.
  152. break;
  153. }
  154. }
  155. // Clears and reinitializes a oneof field as necessary, in preparation for
  156. // parsing a new value with type field_type and field number field_number.
  157. //
  158. // Note: the oneof_case argument should point directly to the _oneof_case_
  159. // element corresponding to this particular oneof, not to the beginning of the
  160. // _oneof_case_ array.
  161. template <ProcessingType field_type>
  162. inline void ResetOneofField(const ParseTable& table, int field_number,
  163. Arena* arena, MessageLite* msg, uint32* oneof_case,
  164. int64 offset, const void* default_ptr) {
  165. if (*oneof_case == field_number) {
  166. // The oneof is already set to the right type, so there is no need to clear
  167. // it.
  168. return;
  169. }
  170. if (*oneof_case != 0) {
  171. ClearOneofField(table.fields[*oneof_case], arena, msg);
  172. }
  173. *oneof_case = field_number;
  174. switch (field_type) {
  175. case ProcessingType_STRING:
  176. Raw<ArenaStringPtr>(msg, offset)
  177. ->UnsafeSetDefault(static_cast<const string*>(default_ptr));
  178. break;
  179. case ProcessingType_INLINED:
  180. new (Raw<InlinedStringField>(msg, offset))
  181. InlinedStringField(*static_cast<const string*>(default_ptr));
  182. break;
  183. case ProcessingType_MESSAGE:
  184. MessageLite** submessage = Raw<MessageLite*>(msg, offset);
  185. const MessageLite* prototype =
  186. table.aux[field_number].messages.default_message();
  187. *submessage = prototype->New(arena);
  188. break;
  189. }
  190. }
  191. template <Cardinality cardinality, bool validate, StringType ctype>
  192. static inline bool HandleString(io::CodedInputStream* input, MessageLite* msg,
  193. Arena* arena, uint32* has_bits,
  194. uint32 has_bit_index, int64 offset,
  195. const void* default_ptr,
  196. const char* field_name) {
  197. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  198. const char* sdata;
  199. size_t size;
  200. #endif
  201. switch (ctype) {
  202. case StringType_INLINED: {
  203. InlinedStringField* s;
  204. switch (cardinality) {
  205. case Cardinality_SINGULAR:
  206. // TODO(ckennelly): Is this optimal?
  207. s = MutableField<InlinedStringField>(
  208. msg, has_bits, has_bit_index, offset);
  209. break;
  210. case Cardinality_REPEATED:
  211. s = AddField<InlinedStringField>(msg, offset);
  212. break;
  213. case Cardinality_ONEOF:
  214. s = Raw<InlinedStringField>(msg, offset);
  215. break;
  216. }
  217. GOOGLE_DCHECK(s != nullptr);
  218. ::std::string* value = s->MutableNoArena(NULL);
  219. if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadString(input, value))) {
  220. return false;
  221. }
  222. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  223. sdata = value->data();
  224. size = value->size();
  225. #endif
  226. break;
  227. }
  228. case StringType_STRING: {
  229. string* value;
  230. switch (cardinality) {
  231. case Cardinality_SINGULAR:
  232. // TODO(ckennelly): Is this optimal?
  233. value =
  234. MutableField<ArenaStringPtr>(msg, has_bits, has_bit_index, offset)
  235. ->Mutable(static_cast<const string*>(default_ptr), arena);
  236. break;
  237. case Cardinality_REPEATED:
  238. value = AddField<string>(msg, offset);
  239. break;
  240. case Cardinality_ONEOF:
  241. value = Raw<ArenaStringPtr>(msg, offset)
  242. ->Mutable(static_cast<const string*>(default_ptr), arena);
  243. break;
  244. }
  245. GOOGLE_DCHECK(value != nullptr);
  246. if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadString(input, value))) {
  247. return false;
  248. }
  249. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  250. sdata = value->data();
  251. size = value->size();
  252. #endif
  253. break;
  254. }
  255. }
  256. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  257. if (validate) {
  258. WireFormatLite::VerifyUtf8String(sdata, size, WireFormatLite::PARSE,
  259. field_name);
  260. }
  261. #endif
  262. return true;
  263. }
  264. template <typename UnknownFieldHandler, typename InternalMetadata,
  265. Cardinality cardinality>
  266. inline bool HandleEnum(const ParseTable& table, io::CodedInputStream* input,
  267. MessageLite* msg, uint32* presence,
  268. uint32 presence_index, int64 offset, uint32 tag,
  269. int field_number) {
  270. int value;
  271. if (GOOGLE_PREDICT_FALSE(
  272. (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  273. input, &value)))) {
  274. return false;
  275. }
  276. AuxillaryParseTableField::EnumValidator validator =
  277. table.aux[field_number].enums.validator;
  278. if (validator(value)) {
  279. switch (cardinality) {
  280. case Cardinality_SINGULAR:
  281. SetField(msg, presence, presence_index, offset, value);
  282. break;
  283. case Cardinality_REPEATED:
  284. AddField(msg, offset, value);
  285. break;
  286. case Cardinality_ONEOF:
  287. ClearOneofField(table.fields[presence[presence_index]],
  288. GetArena<InternalMetadata>(msg, table.arena_offset),
  289. msg);
  290. SetOneofField(msg, presence, presence_index, offset, field_number,
  291. value);
  292. break;
  293. }
  294. } else {
  295. UnknownFieldHandler::Varint(msg, table, tag, value);
  296. }
  297. return true;
  298. }
  299. // RepeatedMessageTypeHandler allows us to operate on RepeatedPtrField fields
  300. // without instantiating the specific template.
  301. class RepeatedMessageTypeHandler {
  302. public:
  303. typedef MessageLite Type;
  304. typedef MessageLite WeakType;
  305. static Arena* GetArena(Type* t) { return t->GetArena(); }
  306. static void* GetMaybeArenaPointer(Type* t) {
  307. return t->GetMaybeArenaPointer();
  308. }
  309. static inline Type* NewFromPrototype(const Type* prototype,
  310. Arena* arena = NULL) {
  311. return prototype->New(arena);
  312. }
  313. static void Delete(Type* t, Arena* arena = NULL) {
  314. if (arena == NULL) {
  315. delete t;
  316. }
  317. }
  318. };
  319. class MergePartialFromCodedStreamHelper {
  320. public:
  321. static MessageLite* Add(RepeatedPtrFieldBase* field,
  322. const MessageLite* prototype) {
  323. return field->Add<RepeatedMessageTypeHandler>(
  324. const_cast<MessageLite*>(prototype));
  325. }
  326. };
  327. template <typename UnknownFieldHandler, typename InternalMetadata>
  328. bool MergePartialFromCodedStreamImpl(MessageLite* msg, const ParseTable& table,
  329. io::CodedInputStream* input) {
  330. // We require that has_bits are present, as to avoid having to check for them
  331. // for every field.
  332. //
  333. // TODO(ckennelly): Make this a compile-time parameter with templates.
  334. GOOGLE_DCHECK_GE(table.has_bits_offset, 0);
  335. uint32* has_bits = Raw<uint32>(msg, table.has_bits_offset);
  336. GOOGLE_DCHECK(has_bits != NULL);
  337. while (true) {
  338. uint32 tag = input->ReadTag();
  339. const WireFormatLite::WireType wire_type =
  340. WireFormatLite::GetTagWireType(tag);
  341. const int field_number = WireFormatLite::GetTagFieldNumber(tag);
  342. if (field_number > table.max_field_number) {
  343. // check for possible extensions
  344. if (UnknownFieldHandler::ParseExtension(msg, table, input, tag)) {
  345. // successfully parsed
  346. continue;
  347. }
  348. if (GOOGLE_PREDICT_FALSE(
  349. !UnknownFieldHandler::Skip(msg, table, input, tag))) {
  350. return false;
  351. }
  352. continue;
  353. }
  354. // We implicitly verify that data points to a valid field as we check the
  355. // wire types. Entries in table.fields[i] that do not correspond to valid
  356. // field numbers have their normal_wiretype and packed_wiretype fields set
  357. // with the kInvalidMask value. As wire_type cannot take on that value, we
  358. // will never match.
  359. const ParseTableField* data = table.fields + field_number;
  360. // TODO(ckennelly): Avoid sign extension
  361. const int64 presence_index = data->presence_index;
  362. const int64 offset = data->offset;
  363. const unsigned char processing_type = data->processing_type;
  364. if (data->normal_wiretype == static_cast<unsigned char>(wire_type)) {
  365. // TODO(ckennelly): Use a computed goto on GCC/LLVM or otherwise eliminate
  366. // the bounds check on processing_type.
  367. switch (processing_type) {
  368. #define HANDLE_TYPE(TYPE, CPPTYPE) \
  369. case (WireFormatLite::TYPE_##TYPE): { \
  370. CPPTYPE value; \
  371. if (GOOGLE_PREDICT_FALSE( \
  372. (!WireFormatLite::ReadPrimitive< \
  373. CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \
  374. return false; \
  375. } \
  376. SetField(msg, has_bits, presence_index, offset, value); \
  377. break; \
  378. } \
  379. case (WireFormatLite::TYPE_##TYPE) | kRepeatedMask: { \
  380. google::protobuf::RepeatedField<CPPTYPE>* values = \
  381. Raw<google::protobuf::RepeatedField<CPPTYPE> >(msg, offset); \
  382. if (GOOGLE_PREDICT_FALSE((!WireFormatLite::ReadRepeatedPrimitive< \
  383. CPPTYPE, WireFormatLite::TYPE_##TYPE>( \
  384. data->tag_size, tag, input, values)))) { \
  385. return false; \
  386. } \
  387. break; \
  388. } \
  389. case (WireFormatLite::TYPE_##TYPE) | kOneofMask: { \
  390. uint32* oneof_case = Raw<uint32>(msg, table.oneof_case_offset); \
  391. CPPTYPE value; \
  392. if (GOOGLE_PREDICT_FALSE( \
  393. (!WireFormatLite::ReadPrimitive< \
  394. CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)))) { \
  395. return false; \
  396. } \
  397. ClearOneofField(table.fields[oneof_case[presence_index]], \
  398. GetArena<InternalMetadata>(msg, table.arena_offset), msg); \
  399. SetOneofField(msg, oneof_case, presence_index, offset, field_number, \
  400. value); \
  401. break; \
  402. }
  403. HANDLE_TYPE(INT32, int32)
  404. HANDLE_TYPE(INT64, int64)
  405. HANDLE_TYPE(SINT32, int32)
  406. HANDLE_TYPE(SINT64, int64)
  407. HANDLE_TYPE(UINT32, uint32)
  408. HANDLE_TYPE(UINT64, uint64)
  409. HANDLE_TYPE(FIXED32, uint32)
  410. HANDLE_TYPE(FIXED64, uint64)
  411. HANDLE_TYPE(SFIXED32, int32)
  412. HANDLE_TYPE(SFIXED64, int64)
  413. HANDLE_TYPE(FLOAT, float)
  414. HANDLE_TYPE(DOUBLE, double)
  415. HANDLE_TYPE(BOOL, bool)
  416. #undef HANDLE_TYPE
  417. case WireFormatLite::TYPE_BYTES:
  418. #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  419. case WireFormatLite::TYPE_STRING:
  420. #endif
  421. {
  422. Arena* const arena =
  423. GetArena<InternalMetadata>(msg, table.arena_offset);
  424. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  425. if (GOOGLE_PREDICT_FALSE((
  426. !HandleString<Cardinality_SINGULAR, false, StringType_STRING>(
  427. input, msg, arena, has_bits, presence_index, offset,
  428. default_ptr, NULL)))) {
  429. return false;
  430. }
  431. break;
  432. }
  433. case TYPE_BYTES_INLINED:
  434. #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  435. case TYPE_STRING_INLINED:
  436. #endif
  437. {
  438. Arena* const arena =
  439. GetArena<InternalMetadata>(msg, table.arena_offset);
  440. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  441. if (GOOGLE_PREDICT_FALSE((!HandleString<Cardinality_SINGULAR, false,
  442. StringType_INLINED>(
  443. input, msg, arena, has_bits, presence_index, offset,
  444. default_ptr, NULL)))) {
  445. return false;
  446. }
  447. break;
  448. }
  449. case WireFormatLite::TYPE_BYTES | kOneofMask:
  450. #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  451. case WireFormatLite::TYPE_STRING | kOneofMask:
  452. #endif
  453. {
  454. Arena* const arena =
  455. GetArena<InternalMetadata>(msg, table.arena_offset);
  456. uint32* oneof_case = Raw<uint32>(msg, table.oneof_case_offset);
  457. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  458. ResetOneofField<ProcessingType_STRING>(
  459. table, field_number, arena, msg, oneof_case + presence_index,
  460. offset, default_ptr);
  461. if (GOOGLE_PREDICT_FALSE(
  462. (!HandleString<Cardinality_ONEOF, false, StringType_STRING>(
  463. input, msg, arena, has_bits, presence_index, offset,
  464. default_ptr, NULL)))) {
  465. return false;
  466. }
  467. break;
  468. }
  469. case (WireFormatLite::TYPE_BYTES) | kRepeatedMask:
  470. case TYPE_BYTES_INLINED | kRepeatedMask:
  471. #ifndef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  472. case (WireFormatLite::TYPE_STRING) | kRepeatedMask:
  473. case TYPE_STRING_INLINED | kRepeatedMask:
  474. #endif
  475. {
  476. Arena* const arena =
  477. GetArena<InternalMetadata>(msg, table.arena_offset);
  478. const void* default_ptr =
  479. table.aux[field_number].strings.default_ptr;
  480. if (GOOGLE_PREDICT_FALSE((
  481. !HandleString<Cardinality_REPEATED, false, StringType_STRING>(
  482. input, msg, arena, has_bits, presence_index, offset,
  483. default_ptr, NULL)))) {
  484. return false;
  485. }
  486. break;
  487. }
  488. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  489. case (WireFormatLite::TYPE_STRING): {
  490. Arena* const arena =
  491. GetArena<InternalMetadata>(msg, table.arena_offset);
  492. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  493. const char* field_name = table.aux[field_number].strings.field_name;
  494. if (GOOGLE_PREDICT_FALSE(
  495. (!HandleString<Cardinality_SINGULAR, true, StringType_STRING>(
  496. input, msg, arena, has_bits, presence_index, offset,
  497. default_ptr, field_name)))) {
  498. return false;
  499. }
  500. break;
  501. }
  502. case TYPE_STRING_INLINED | kRepeatedMask:
  503. case (WireFormatLite::TYPE_STRING) | kRepeatedMask: {
  504. Arena* const arena =
  505. GetArena<InternalMetadata>(msg, table.arena_offset);
  506. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  507. const char* field_name = table.aux[field_number].strings.field_name;
  508. if (GOOGLE_PREDICT_FALSE(
  509. (!HandleString<Cardinality_REPEATED, true, StringType_STRING>(
  510. input, msg, arena, has_bits, presence_index, offset,
  511. default_ptr, field_name)))) {
  512. return false;
  513. }
  514. break;
  515. }
  516. case (WireFormatLite::TYPE_STRING) | kOneofMask: {
  517. Arena* const arena =
  518. GetArena<InternalMetadata>(msg, table.arena_offset);
  519. uint32* oneof_case = Raw<uint32>(msg, table.oneof_case_offset);
  520. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  521. const char* field_name = table.aux[field_number].strings.field_name;
  522. ResetOneofField<ProcessingType_STRING>(
  523. table, field_number, arena, msg, oneof_case + presence_index,
  524. offset, default_ptr);
  525. if (GOOGLE_PREDICT_FALSE(
  526. (!HandleString<Cardinality_ONEOF, true, StringType_STRING>(
  527. input, msg, arena, has_bits, presence_index, offset,
  528. default_ptr, field_name)))) {
  529. return false;
  530. }
  531. break;
  532. }
  533. #endif
  534. case WireFormatLite::TYPE_ENUM: {
  535. if (GOOGLE_PREDICT_FALSE(
  536. (!HandleEnum<UnknownFieldHandler, InternalMetadata,
  537. Cardinality_SINGULAR>(
  538. table, input, msg, has_bits, presence_index, offset, tag,
  539. field_number)))) {
  540. return false;
  541. }
  542. break;
  543. }
  544. case WireFormatLite::TYPE_ENUM | kRepeatedMask: {
  545. if (GOOGLE_PREDICT_FALSE(
  546. (!HandleEnum<UnknownFieldHandler, InternalMetadata,
  547. Cardinality_REPEATED>(
  548. table, input, msg, has_bits, presence_index, offset, tag,
  549. field_number)))) {
  550. return false;
  551. }
  552. break;
  553. }
  554. case WireFormatLite::TYPE_ENUM | kOneofMask: {
  555. uint32* oneof_case = Raw<uint32>(msg, table.oneof_case_offset);
  556. if (GOOGLE_PREDICT_FALSE(
  557. (!HandleEnum<UnknownFieldHandler, InternalMetadata,
  558. Cardinality_ONEOF>(table, input, msg, oneof_case,
  559. presence_index, offset, tag,
  560. field_number)))) {
  561. return false;
  562. }
  563. break;
  564. }
  565. case WireFormatLite::TYPE_GROUP: {
  566. MessageLite** submsg_holder =
  567. MutableField<MessageLite*>(msg, has_bits, presence_index, offset);
  568. MessageLite* submsg = *submsg_holder;
  569. if (submsg == NULL) {
  570. Arena* const arena =
  571. GetArena<InternalMetadata>(msg, table.arena_offset);
  572. const MessageLite* prototype =
  573. table.aux[field_number].messages.default_message();
  574. submsg = prototype->New(arena);
  575. *submsg_holder = submsg;
  576. }
  577. if (GOOGLE_PREDICT_FALSE(
  578. !WireFormatLite::ReadGroup(field_number, input, submsg))) {
  579. return false;
  580. }
  581. break;
  582. }
  583. case WireFormatLite::TYPE_GROUP | kRepeatedMask: {
  584. RepeatedPtrFieldBase* field = Raw<RepeatedPtrFieldBase>(msg, offset);
  585. const MessageLite* prototype =
  586. table.aux[field_number].messages.default_message();
  587. GOOGLE_DCHECK(prototype != NULL);
  588. MessageLite* submsg =
  589. MergePartialFromCodedStreamHelper::Add(field, prototype);
  590. if (GOOGLE_PREDICT_FALSE(
  591. !WireFormatLite::ReadGroup(field_number, input, submsg))) {
  592. return false;
  593. }
  594. break;
  595. }
  596. case WireFormatLite::TYPE_MESSAGE: {
  597. MessageLite** submsg_holder =
  598. MutableField<MessageLite*>(msg, has_bits, presence_index, offset);
  599. MessageLite* submsg = *submsg_holder;
  600. if (submsg == NULL) {
  601. Arena* const arena =
  602. GetArena<InternalMetadata>(msg, table.arena_offset);
  603. const MessageLite* prototype =
  604. table.aux[field_number].messages.default_message();
  605. if (prototype == NULL) {
  606. prototype =
  607. ::google::protobuf::internal::ImplicitWeakMessage::default_instance();
  608. }
  609. submsg = prototype->New(arena);
  610. *submsg_holder = submsg;
  611. }
  612. if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) {
  613. return false;
  614. }
  615. break;
  616. }
  617. // TODO(ckennelly): Adapt ReadMessageNoVirtualNoRecursionDepth and
  618. // manage input->IncrementRecursionDepth() here.
  619. case WireFormatLite::TYPE_MESSAGE | kRepeatedMask: {
  620. RepeatedPtrFieldBase* field = Raw<RepeatedPtrFieldBase>(msg, offset);
  621. const MessageLite* prototype =
  622. table.aux[field_number].messages.default_message();
  623. if (prototype == NULL) {
  624. prototype =
  625. ::google::protobuf::internal::ImplicitWeakMessage::default_instance();
  626. }
  627. MessageLite* submsg =
  628. MergePartialFromCodedStreamHelper::Add(field, prototype);
  629. if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) {
  630. return false;
  631. }
  632. break;
  633. }
  634. case WireFormatLite::TYPE_MESSAGE | kOneofMask: {
  635. Arena* const arena =
  636. GetArena<InternalMetadata>(msg, table.arena_offset);
  637. uint32* oneof_case = Raw<uint32>(msg, table.oneof_case_offset);
  638. MessageLite** submsg_holder = Raw<MessageLite*>(msg, offset);
  639. ResetOneofField<ProcessingType_MESSAGE>(
  640. table, field_number, arena, msg, oneof_case + presence_index,
  641. offset, NULL);
  642. MessageLite* submsg = *submsg_holder;
  643. if (GOOGLE_PREDICT_FALSE(!WireFormatLite::ReadMessage(input, submsg))) {
  644. return false;
  645. }
  646. break;
  647. }
  648. #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  649. case TYPE_STRING_INLINED: {
  650. Arena* const arena =
  651. GetArena<InternalMetadata>(msg, table.arena_offset);
  652. const void* default_ptr = table.aux[field_number].strings.default_ptr;
  653. const char* field_name = table.aux[field_number].strings.field_name;
  654. if (GOOGLE_PREDICT_FALSE((
  655. !HandleString<Cardinality_SINGULAR, true, StringType_INLINED>(
  656. input, msg, arena, has_bits, presence_index, offset,
  657. default_ptr, field_name)))) {
  658. return false;
  659. }
  660. break;
  661. }
  662. #endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED
  663. case TYPE_MAP: {
  664. if (GOOGLE_PREDICT_FALSE(!(*table.aux[field_number].maps.parse_map)(
  665. input, Raw<void>(msg, offset)))) {
  666. return false;
  667. }
  668. break;
  669. }
  670. case 0: {
  671. // Done.
  672. return true;
  673. }
  674. default:
  675. break;
  676. }
  677. } else if (data->packed_wiretype == static_cast<unsigned char>(wire_type)) {
  678. // Non-packable fields have their packed_wiretype masked with
  679. // kNotPackedMask, which is impossible to match here.
  680. GOOGLE_DCHECK(processing_type & kRepeatedMask);
  681. GOOGLE_DCHECK_NE(processing_type, kRepeatedMask);
  682. GOOGLE_DCHECK_EQ(0, processing_type & kOneofMask);
  683. GOOGLE_DCHECK_NE(TYPE_BYTES_INLINED | kRepeatedMask, processing_type);
  684. GOOGLE_DCHECK_NE(TYPE_STRING_INLINED | kRepeatedMask, processing_type);
  685. // TODO(ckennelly): Use a computed goto on GCC/LLVM.
  686. //
  687. // Mask out kRepeatedMask bit, allowing the jump table to be smaller.
  688. switch (static_cast<WireFormatLite::FieldType>(
  689. processing_type ^ kRepeatedMask)) {
  690. #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
  691. case WireFormatLite::TYPE_##TYPE: { \
  692. google::protobuf::RepeatedField<CPPTYPE>* values = \
  693. Raw<google::protobuf::RepeatedField<CPPTYPE> >(msg, offset); \
  694. if (GOOGLE_PREDICT_FALSE( \
  695. (!WireFormatLite::ReadPackedPrimitive< \
  696. CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, values)))) { \
  697. return false; \
  698. } \
  699. break; \
  700. }
  701. HANDLE_PACKED_TYPE(INT32, int32, Int32)
  702. HANDLE_PACKED_TYPE(INT64, int64, Int64)
  703. HANDLE_PACKED_TYPE(SINT32, int32, Int32)
  704. HANDLE_PACKED_TYPE(SINT64, int64, Int64)
  705. HANDLE_PACKED_TYPE(UINT32, uint32, UInt32)
  706. HANDLE_PACKED_TYPE(UINT64, uint64, UInt64)
  707. HANDLE_PACKED_TYPE(FIXED32, uint32, UInt32)
  708. HANDLE_PACKED_TYPE(FIXED64, uint64, UInt64)
  709. HANDLE_PACKED_TYPE(SFIXED32, int32, Int32)
  710. HANDLE_PACKED_TYPE(SFIXED64, int64, Int64)
  711. HANDLE_PACKED_TYPE(FLOAT, float, Float)
  712. HANDLE_PACKED_TYPE(DOUBLE, double, Double)
  713. HANDLE_PACKED_TYPE(BOOL, bool, Bool)
  714. #undef HANDLE_PACKED_TYPE
  715. case WireFormatLite::TYPE_ENUM: {
  716. // To avoid unnecessarily calling MutableUnknownFields (which mutates
  717. // InternalMetadataWithArena) when all inputs in the repeated series
  718. // are valid, we implement our own parser rather than call
  719. // WireFormat::ReadPackedEnumPreserveUnknowns.
  720. uint32 length;
  721. if (GOOGLE_PREDICT_FALSE(!input->ReadVarint32(&length))) {
  722. return false;
  723. }
  724. AuxillaryParseTableField::EnumValidator validator =
  725. table.aux[field_number].enums.validator;
  726. google::protobuf::RepeatedField<int>* values =
  727. Raw<google::protobuf::RepeatedField<int> >(msg, offset);
  728. io::CodedInputStream::Limit limit = input->PushLimit(length);
  729. while (input->BytesUntilLimit() > 0) {
  730. int value;
  731. if (GOOGLE_PREDICT_FALSE(
  732. (!google::protobuf::internal::WireFormatLite::ReadPrimitive<
  733. int, WireFormatLite::TYPE_ENUM>(input, &value)))) {
  734. return false;
  735. }
  736. if (validator(value)) {
  737. values->Add(value);
  738. } else {
  739. // TODO(ckennelly): Consider caching here.
  740. UnknownFieldHandler::Varint(msg, table, tag, value);
  741. }
  742. }
  743. input->PopLimit(limit);
  744. break;
  745. }
  746. case WireFormatLite::TYPE_STRING:
  747. case WireFormatLite::TYPE_GROUP:
  748. case WireFormatLite::TYPE_MESSAGE:
  749. case WireFormatLite::TYPE_BYTES:
  750. GOOGLE_DCHECK(false);
  751. return false;
  752. default:
  753. break;
  754. }
  755. } else {
  756. if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
  757. // Must be the end of the message.
  758. return true;
  759. }
  760. // check for possible extensions
  761. if (UnknownFieldHandler::ParseExtension(msg, table, input, tag)) {
  762. // successfully parsed
  763. continue;
  764. }
  765. // process unknown field.
  766. if (GOOGLE_PREDICT_FALSE(
  767. !UnknownFieldHandler::Skip(msg, table, input, tag))) {
  768. return false;
  769. }
  770. }
  771. }
  772. }
  773. } // namespace internal
  774. } // namespace protobuf
  775. } // namespace google
  776. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TABLE_DRIVEN_LITE_H__