诸暨麻将添加redis
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1360 rader
53 KiB

  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/wire_format.h>
  34. #include <stack>
  35. #include <string>
  36. #include <vector>
  37. #include <google/protobuf/stubs/logging.h>
  38. #include <google/protobuf/stubs/common.h>
  39. #include <google/protobuf/stubs/stringprintf.h>
  40. #include <google/protobuf/descriptor.pb.h>
  41. #include <google/protobuf/io/coded_stream.h>
  42. #include <google/protobuf/io/zero_copy_stream.h>
  43. #include <google/protobuf/io/zero_copy_stream_impl.h>
  44. #include <google/protobuf/descriptor.h>
  45. #include <google/protobuf/dynamic_message.h>
  46. #include <google/protobuf/map_field.h>
  47. #include <google/protobuf/map_field_inl.h>
  48. #include <google/protobuf/message_lite.h>
  49. #include <google/protobuf/unknown_field_set.h>
  50. #include <google/protobuf/port_def.inc>
  51. const size_t kMapEntryTagByteSize = 2;
  52. namespace google {
  53. namespace protobuf {
  54. namespace internal {
  55. // Forward declare static functions
  56. static size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field,
  57. const MapKey& value);
  58. static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field,
  59. const MapValueRef& value);
  60. // ===================================================================
  61. bool UnknownFieldSetFieldSkipper::SkipField(io::CodedInputStream* input,
  62. uint32 tag) {
  63. return WireFormat::SkipField(input, tag, unknown_fields_);
  64. }
  65. bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) {
  66. return WireFormat::SkipMessage(input, unknown_fields_);
  67. }
  68. void UnknownFieldSetFieldSkipper::SkipUnknownEnum(int field_number, int value) {
  69. unknown_fields_->AddVarint(field_number, value);
  70. }
  71. bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag,
  72. UnknownFieldSet* unknown_fields) {
  73. int number = WireFormatLite::GetTagFieldNumber(tag);
  74. // Field number 0 is illegal.
  75. if (number == 0) return false;
  76. switch (WireFormatLite::GetTagWireType(tag)) {
  77. case WireFormatLite::WIRETYPE_VARINT: {
  78. uint64 value;
  79. if (!input->ReadVarint64(&value)) return false;
  80. if (unknown_fields != NULL) unknown_fields->AddVarint(number, value);
  81. return true;
  82. }
  83. case WireFormatLite::WIRETYPE_FIXED64: {
  84. uint64 value;
  85. if (!input->ReadLittleEndian64(&value)) return false;
  86. if (unknown_fields != NULL) unknown_fields->AddFixed64(number, value);
  87. return true;
  88. }
  89. case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
  90. uint32 length;
  91. if (!input->ReadVarint32(&length)) return false;
  92. if (unknown_fields == NULL) {
  93. if (!input->Skip(length)) return false;
  94. } else {
  95. if (!input->ReadString(unknown_fields->AddLengthDelimited(number),
  96. length)) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. case WireFormatLite::WIRETYPE_START_GROUP: {
  103. if (!input->IncrementRecursionDepth()) return false;
  104. if (!SkipMessage(input, (unknown_fields == NULL)
  105. ? NULL
  106. : unknown_fields->AddGroup(number))) {
  107. return false;
  108. }
  109. input->DecrementRecursionDepth();
  110. // Check that the ending tag matched the starting tag.
  111. if (!input->LastTagWas(
  112. WireFormatLite::MakeTag(WireFormatLite::GetTagFieldNumber(tag),
  113. WireFormatLite::WIRETYPE_END_GROUP))) {
  114. return false;
  115. }
  116. return true;
  117. }
  118. case WireFormatLite::WIRETYPE_END_GROUP: {
  119. return false;
  120. }
  121. case WireFormatLite::WIRETYPE_FIXED32: {
  122. uint32 value;
  123. if (!input->ReadLittleEndian32(&value)) return false;
  124. if (unknown_fields != NULL) unknown_fields->AddFixed32(number, value);
  125. return true;
  126. }
  127. default: {
  128. return false;
  129. }
  130. }
  131. }
  132. bool WireFormat::SkipMessage(io::CodedInputStream* input,
  133. UnknownFieldSet* unknown_fields) {
  134. while (true) {
  135. uint32 tag = input->ReadTag();
  136. if (tag == 0) {
  137. // End of input. This is a valid place to end, so return true.
  138. return true;
  139. }
  140. WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
  141. if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
  142. // Must be the end of the message.
  143. return true;
  144. }
  145. if (!SkipField(input, tag, unknown_fields)) return false;
  146. }
  147. }
  148. bool WireFormat::ReadPackedEnumPreserveUnknowns(io::CodedInputStream* input,
  149. uint32 field_number,
  150. bool (*is_valid)(int),
  151. UnknownFieldSet* unknown_fields,
  152. RepeatedField<int>* values) {
  153. uint32 length;
  154. if (!input->ReadVarint32(&length)) return false;
  155. io::CodedInputStream::Limit limit = input->PushLimit(length);
  156. while (input->BytesUntilLimit() > 0) {
  157. int value;
  158. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  159. input, &value)) {
  160. return false;
  161. }
  162. if (is_valid == NULL || is_valid(value)) {
  163. values->Add(value);
  164. } else {
  165. unknown_fields->AddVarint(field_number, value);
  166. }
  167. }
  168. input->PopLimit(limit);
  169. return true;
  170. }
  171. uint8* WireFormat::InternalSerializeUnknownFieldsToArray(
  172. const UnknownFieldSet& unknown_fields, uint8* target,
  173. io::EpsCopyOutputStream* stream) {
  174. for (int i = 0; i < unknown_fields.field_count(); i++) {
  175. const UnknownField& field = unknown_fields.field(i);
  176. target = stream->EnsureSpace(target);
  177. switch (field.type()) {
  178. case UnknownField::TYPE_VARINT:
  179. target = WireFormatLite::WriteUInt64ToArray(field.number(),
  180. field.varint(), target);
  181. break;
  182. case UnknownField::TYPE_FIXED32:
  183. target = WireFormatLite::WriteFixed32ToArray(field.number(),
  184. field.fixed32(), target);
  185. break;
  186. case UnknownField::TYPE_FIXED64:
  187. target = WireFormatLite::WriteFixed64ToArray(field.number(),
  188. field.fixed64(), target);
  189. break;
  190. case UnknownField::TYPE_LENGTH_DELIMITED:
  191. target = stream->WriteString(field.number(), field.length_delimited(),
  192. target);
  193. break;
  194. case UnknownField::TYPE_GROUP:
  195. target = WireFormatLite::WriteTagToArray(
  196. field.number(), WireFormatLite::WIRETYPE_START_GROUP, target);
  197. target = InternalSerializeUnknownFieldsToArray(field.group(), target,
  198. stream);
  199. target = stream->EnsureSpace(target);
  200. target = WireFormatLite::WriteTagToArray(
  201. field.number(), WireFormatLite::WIRETYPE_END_GROUP, target);
  202. break;
  203. }
  204. }
  205. return target;
  206. }
  207. uint8* WireFormat::InternalSerializeUnknownMessageSetItemsToArray(
  208. const UnknownFieldSet& unknown_fields, uint8* target,
  209. io::EpsCopyOutputStream* stream) {
  210. for (int i = 0; i < unknown_fields.field_count(); i++) {
  211. const UnknownField& field = unknown_fields.field(i);
  212. // The only unknown fields that are allowed to exist in a MessageSet are
  213. // messages, which are length-delimited.
  214. if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
  215. target = stream->EnsureSpace(target);
  216. // Start group.
  217. target = io::CodedOutputStream::WriteTagToArray(
  218. WireFormatLite::kMessageSetItemStartTag, target);
  219. // Write type ID.
  220. target = io::CodedOutputStream::WriteTagToArray(
  221. WireFormatLite::kMessageSetTypeIdTag, target);
  222. target =
  223. io::CodedOutputStream::WriteVarint32ToArray(field.number(), target);
  224. // Write message.
  225. target = io::CodedOutputStream::WriteTagToArray(
  226. WireFormatLite::kMessageSetMessageTag, target);
  227. target = field.InternalSerializeLengthDelimitedNoTag(target, stream);
  228. target = stream->EnsureSpace(target);
  229. // End group.
  230. target = io::CodedOutputStream::WriteTagToArray(
  231. WireFormatLite::kMessageSetItemEndTag, target);
  232. }
  233. }
  234. return target;
  235. }
  236. size_t WireFormat::ComputeUnknownFieldsSize(
  237. const UnknownFieldSet& unknown_fields) {
  238. size_t size = 0;
  239. for (int i = 0; i < unknown_fields.field_count(); i++) {
  240. const UnknownField& field = unknown_fields.field(i);
  241. switch (field.type()) {
  242. case UnknownField::TYPE_VARINT:
  243. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  244. field.number(), WireFormatLite::WIRETYPE_VARINT));
  245. size += io::CodedOutputStream::VarintSize64(field.varint());
  246. break;
  247. case UnknownField::TYPE_FIXED32:
  248. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  249. field.number(), WireFormatLite::WIRETYPE_FIXED32));
  250. size += sizeof(int32);
  251. break;
  252. case UnknownField::TYPE_FIXED64:
  253. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  254. field.number(), WireFormatLite::WIRETYPE_FIXED64));
  255. size += sizeof(int64);
  256. break;
  257. case UnknownField::TYPE_LENGTH_DELIMITED:
  258. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  259. field.number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
  260. size += io::CodedOutputStream::VarintSize32(
  261. field.length_delimited().size());
  262. size += field.length_delimited().size();
  263. break;
  264. case UnknownField::TYPE_GROUP:
  265. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  266. field.number(), WireFormatLite::WIRETYPE_START_GROUP));
  267. size += ComputeUnknownFieldsSize(field.group());
  268. size += io::CodedOutputStream::VarintSize32(WireFormatLite::MakeTag(
  269. field.number(), WireFormatLite::WIRETYPE_END_GROUP));
  270. break;
  271. }
  272. }
  273. return size;
  274. }
  275. size_t WireFormat::ComputeUnknownMessageSetItemsSize(
  276. const UnknownFieldSet& unknown_fields) {
  277. size_t size = 0;
  278. for (int i = 0; i < unknown_fields.field_count(); i++) {
  279. const UnknownField& field = unknown_fields.field(i);
  280. // The only unknown fields that are allowed to exist in a MessageSet are
  281. // messages, which are length-delimited.
  282. if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
  283. size += WireFormatLite::kMessageSetItemTagsSize;
  284. size += io::CodedOutputStream::VarintSize32(field.number());
  285. int field_size = field.GetLengthDelimitedSize();
  286. size += io::CodedOutputStream::VarintSize32(field_size);
  287. size += field_size;
  288. }
  289. }
  290. return size;
  291. }
  292. // ===================================================================
  293. bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input,
  294. Message* message) {
  295. const Descriptor* descriptor = message->GetDescriptor();
  296. const Reflection* message_reflection = message->GetReflection();
  297. while (true) {
  298. uint32 tag = input->ReadTag();
  299. if (tag == 0) {
  300. // End of input. This is a valid place to end, so return true.
  301. return true;
  302. }
  303. if (WireFormatLite::GetTagWireType(tag) ==
  304. WireFormatLite::WIRETYPE_END_GROUP) {
  305. // Must be the end of the message.
  306. return true;
  307. }
  308. const FieldDescriptor* field = NULL;
  309. if (descriptor != NULL) {
  310. int field_number = WireFormatLite::GetTagFieldNumber(tag);
  311. field = descriptor->FindFieldByNumber(field_number);
  312. // If that failed, check if the field is an extension.
  313. if (field == NULL && descriptor->IsExtensionNumber(field_number)) {
  314. if (input->GetExtensionPool() == NULL) {
  315. field = message_reflection->FindKnownExtensionByNumber(field_number);
  316. } else {
  317. field = input->GetExtensionPool()->FindExtensionByNumber(
  318. descriptor, field_number);
  319. }
  320. }
  321. // If that failed, but we're a MessageSet, and this is the tag for a
  322. // MessageSet item, then parse that.
  323. if (field == NULL && descriptor->options().message_set_wire_format() &&
  324. tag == WireFormatLite::kMessageSetItemStartTag) {
  325. if (!ParseAndMergeMessageSetItem(input, message)) {
  326. return false;
  327. }
  328. continue; // Skip ParseAndMergeField(); already taken care of.
  329. }
  330. }
  331. if (!ParseAndMergeField(tag, field, message, input)) {
  332. return false;
  333. }
  334. }
  335. }
  336. bool WireFormat::SkipMessageSetField(io::CodedInputStream* input,
  337. uint32 field_number,
  338. UnknownFieldSet* unknown_fields) {
  339. uint32 length;
  340. if (!input->ReadVarint32(&length)) return false;
  341. return input->ReadString(unknown_fields->AddLengthDelimited(field_number),
  342. length);
  343. }
  344. bool WireFormat::ParseAndMergeMessageSetField(uint32 field_number,
  345. const FieldDescriptor* field,
  346. Message* message,
  347. io::CodedInputStream* input) {
  348. const Reflection* message_reflection = message->GetReflection();
  349. if (field == NULL) {
  350. // We store unknown MessageSet extensions as groups.
  351. return SkipMessageSetField(
  352. input, field_number, message_reflection->MutableUnknownFields(message));
  353. } else if (field->is_repeated() ||
  354. field->type() != FieldDescriptor::TYPE_MESSAGE) {
  355. // This shouldn't happen as we only allow optional message extensions to
  356. // MessageSet.
  357. GOOGLE_LOG(ERROR) << "Extensions of MessageSets must be optional messages.";
  358. return false;
  359. } else {
  360. Message* sub_message = message_reflection->MutableMessage(
  361. message, field, input->GetExtensionFactory());
  362. return WireFormatLite::ReadMessage(input, sub_message);
  363. }
  364. }
  365. static bool StrictUtf8Check(const FieldDescriptor* field) {
  366. return field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
  367. }
  368. bool WireFormat::ParseAndMergeField(
  369. uint32 tag,
  370. const FieldDescriptor* field, // May be NULL for unknown
  371. Message* message, io::CodedInputStream* input) {
  372. const Reflection* message_reflection = message->GetReflection();
  373. enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format;
  374. if (field == NULL) {
  375. value_format = UNKNOWN;
  376. } else if (WireFormatLite::GetTagWireType(tag) ==
  377. WireTypeForFieldType(field->type())) {
  378. value_format = NORMAL_FORMAT;
  379. } else if (field->is_packable() &&
  380. WireFormatLite::GetTagWireType(tag) ==
  381. WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
  382. value_format = PACKED_FORMAT;
  383. } else {
  384. // We don't recognize this field. Either the field number is unknown
  385. // or the wire type doesn't match. Put it in our unknown field set.
  386. value_format = UNKNOWN;
  387. }
  388. if (value_format == UNKNOWN) {
  389. return SkipField(input, tag,
  390. message_reflection->MutableUnknownFields(message));
  391. } else if (value_format == PACKED_FORMAT) {
  392. uint32 length;
  393. if (!input->ReadVarint32(&length)) return false;
  394. io::CodedInputStream::Limit limit = input->PushLimit(length);
  395. switch (field->type()) {
  396. #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
  397. case FieldDescriptor::TYPE_##TYPE: { \
  398. while (input->BytesUntilLimit() > 0) { \
  399. CPPTYPE value; \
  400. if (!WireFormatLite::ReadPrimitive<CPPTYPE, \
  401. WireFormatLite::TYPE_##TYPE>(input, \
  402. &value)) \
  403. return false; \
  404. message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
  405. } \
  406. break; \
  407. }
  408. HANDLE_PACKED_TYPE(INT32, int32, Int32)
  409. HANDLE_PACKED_TYPE(INT64, int64, Int64)
  410. HANDLE_PACKED_TYPE(SINT32, int32, Int32)
  411. HANDLE_PACKED_TYPE(SINT64, int64, Int64)
  412. HANDLE_PACKED_TYPE(UINT32, uint32, UInt32)
  413. HANDLE_PACKED_TYPE(UINT64, uint64, UInt64)
  414. HANDLE_PACKED_TYPE(FIXED32, uint32, UInt32)
  415. HANDLE_PACKED_TYPE(FIXED64, uint64, UInt64)
  416. HANDLE_PACKED_TYPE(SFIXED32, int32, Int32)
  417. HANDLE_PACKED_TYPE(SFIXED64, int64, Int64)
  418. HANDLE_PACKED_TYPE(FLOAT, float, Float)
  419. HANDLE_PACKED_TYPE(DOUBLE, double, Double)
  420. HANDLE_PACKED_TYPE(BOOL, bool, Bool)
  421. #undef HANDLE_PACKED_TYPE
  422. case FieldDescriptor::TYPE_ENUM: {
  423. while (input->BytesUntilLimit() > 0) {
  424. int value;
  425. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  426. input, &value))
  427. return false;
  428. if (message->GetDescriptor()->file()->syntax() ==
  429. FileDescriptor::SYNTAX_PROTO3) {
  430. message_reflection->AddEnumValue(message, field, value);
  431. } else {
  432. const EnumValueDescriptor* enum_value =
  433. field->enum_type()->FindValueByNumber(value);
  434. if (enum_value != NULL) {
  435. message_reflection->AddEnum(message, field, enum_value);
  436. } else {
  437. // The enum value is not one of the known values. Add it to the
  438. // UnknownFieldSet.
  439. int64 sign_extended_value = static_cast<int64>(value);
  440. message_reflection->MutableUnknownFields(message)->AddVarint(
  441. WireFormatLite::GetTagFieldNumber(tag), sign_extended_value);
  442. }
  443. }
  444. }
  445. break;
  446. }
  447. case FieldDescriptor::TYPE_STRING:
  448. case FieldDescriptor::TYPE_GROUP:
  449. case FieldDescriptor::TYPE_MESSAGE:
  450. case FieldDescriptor::TYPE_BYTES:
  451. // Can't have packed fields of these types: these should be caught by
  452. // the protocol compiler.
  453. return false;
  454. break;
  455. }
  456. input->PopLimit(limit);
  457. } else {
  458. // Non-packed value (value_format == NORMAL_FORMAT)
  459. switch (field->type()) {
  460. #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
  461. case FieldDescriptor::TYPE_##TYPE: { \
  462. CPPTYPE value; \
  463. if (!WireFormatLite::ReadPrimitive<CPPTYPE, WireFormatLite::TYPE_##TYPE>( \
  464. input, &value)) \
  465. return false; \
  466. if (field->is_repeated()) { \
  467. message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
  468. } else { \
  469. message_reflection->Set##CPPTYPE_METHOD(message, field, value); \
  470. } \
  471. break; \
  472. }
  473. HANDLE_TYPE(INT32, int32, Int32)
  474. HANDLE_TYPE(INT64, int64, Int64)
  475. HANDLE_TYPE(SINT32, int32, Int32)
  476. HANDLE_TYPE(SINT64, int64, Int64)
  477. HANDLE_TYPE(UINT32, uint32, UInt32)
  478. HANDLE_TYPE(UINT64, uint64, UInt64)
  479. HANDLE_TYPE(FIXED32, uint32, UInt32)
  480. HANDLE_TYPE(FIXED64, uint64, UInt64)
  481. HANDLE_TYPE(SFIXED32, int32, Int32)
  482. HANDLE_TYPE(SFIXED64, int64, Int64)
  483. HANDLE_TYPE(FLOAT, float, Float)
  484. HANDLE_TYPE(DOUBLE, double, Double)
  485. HANDLE_TYPE(BOOL, bool, Bool)
  486. #undef HANDLE_TYPE
  487. case FieldDescriptor::TYPE_ENUM: {
  488. int value;
  489. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  490. input, &value))
  491. return false;
  492. if (field->is_repeated()) {
  493. message_reflection->AddEnumValue(message, field, value);
  494. } else {
  495. message_reflection->SetEnumValue(message, field, value);
  496. }
  497. break;
  498. }
  499. // Handle strings separately so that we can optimize the ctype=CORD case.
  500. case FieldDescriptor::TYPE_STRING: {
  501. bool strict_utf8_check = StrictUtf8Check(field);
  502. std::string value;
  503. if (!WireFormatLite::ReadString(input, &value)) return false;
  504. if (strict_utf8_check) {
  505. if (!WireFormatLite::VerifyUtf8String(value.data(), value.length(),
  506. WireFormatLite::PARSE,
  507. field->full_name().c_str())) {
  508. return false;
  509. }
  510. } else {
  511. VerifyUTF8StringNamedField(value.data(), value.length(), PARSE,
  512. field->full_name().c_str());
  513. }
  514. if (field->is_repeated()) {
  515. message_reflection->AddString(message, field, value);
  516. } else {
  517. message_reflection->SetString(message, field, value);
  518. }
  519. break;
  520. }
  521. case FieldDescriptor::TYPE_BYTES: {
  522. std::string value;
  523. if (!WireFormatLite::ReadBytes(input, &value)) return false;
  524. if (field->is_repeated()) {
  525. message_reflection->AddString(message, field, value);
  526. } else {
  527. message_reflection->SetString(message, field, value);
  528. }
  529. break;
  530. }
  531. case FieldDescriptor::TYPE_GROUP: {
  532. Message* sub_message;
  533. if (field->is_repeated()) {
  534. sub_message = message_reflection->AddMessage(
  535. message, field, input->GetExtensionFactory());
  536. } else {
  537. sub_message = message_reflection->MutableMessage(
  538. message, field, input->GetExtensionFactory());
  539. }
  540. if (!WireFormatLite::ReadGroup(WireFormatLite::GetTagFieldNumber(tag),
  541. input, sub_message))
  542. return false;
  543. break;
  544. }
  545. case FieldDescriptor::TYPE_MESSAGE: {
  546. Message* sub_message;
  547. if (field->is_repeated()) {
  548. sub_message = message_reflection->AddMessage(
  549. message, field, input->GetExtensionFactory());
  550. } else {
  551. sub_message = message_reflection->MutableMessage(
  552. message, field, input->GetExtensionFactory());
  553. }
  554. if (!WireFormatLite::ReadMessage(input, sub_message)) return false;
  555. break;
  556. }
  557. }
  558. }
  559. return true;
  560. }
  561. bool WireFormat::ParseAndMergeMessageSetItem(io::CodedInputStream* input,
  562. Message* message) {
  563. struct MSReflective {
  564. bool ParseField(int type_id, io::CodedInputStream* input) {
  565. const FieldDescriptor* field =
  566. message_reflection->FindKnownExtensionByNumber(type_id);
  567. return ParseAndMergeMessageSetField(type_id, field, message, input);
  568. }
  569. bool SkipField(uint32 tag, io::CodedInputStream* input) {
  570. return WireFormat::SkipField(input, tag, NULL);
  571. }
  572. const Reflection* message_reflection;
  573. Message* message;
  574. };
  575. return ParseMessageSetItemImpl(
  576. input, MSReflective{message->GetReflection(), message});
  577. }
  578. // ===================================================================
  579. uint8* WireFormat::InternalSerializeWithCachedSizesToArray(
  580. const Message& message, uint8* target, io::EpsCopyOutputStream* stream) {
  581. const Descriptor* descriptor = message.GetDescriptor();
  582. const Reflection* message_reflection = message.GetReflection();
  583. std::vector<const FieldDescriptor*> fields;
  584. // Fields of map entry should always be serialized.
  585. if (descriptor->options().map_entry()) {
  586. for (int i = 0; i < descriptor->field_count(); i++) {
  587. fields.push_back(descriptor->field(i));
  588. }
  589. } else {
  590. message_reflection->ListFields(message, &fields);
  591. }
  592. for (auto field : fields) {
  593. target = InternalSerializeField(field, message, target, stream);
  594. }
  595. if (descriptor->options().message_set_wire_format()) {
  596. return InternalSerializeUnknownMessageSetItemsToArray(
  597. message_reflection->GetUnknownFields(message), target, stream);
  598. } else {
  599. return InternalSerializeUnknownFieldsToArray(
  600. message_reflection->GetUnknownFields(message), target, stream);
  601. }
  602. }
  603. static uint8* SerializeMapKeyWithCachedSizes(const FieldDescriptor* field,
  604. const MapKey& value, uint8* target,
  605. io::EpsCopyOutputStream* stream) {
  606. target = stream->EnsureSpace(target);
  607. switch (field->type()) {
  608. case FieldDescriptor::TYPE_DOUBLE:
  609. case FieldDescriptor::TYPE_FLOAT:
  610. case FieldDescriptor::TYPE_GROUP:
  611. case FieldDescriptor::TYPE_MESSAGE:
  612. case FieldDescriptor::TYPE_BYTES:
  613. case FieldDescriptor::TYPE_ENUM:
  614. GOOGLE_LOG(FATAL) << "Unsupported";
  615. break;
  616. #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
  617. case FieldDescriptor::TYPE_##FieldType: \
  618. target = WireFormatLite::Write##CamelFieldType##ToArray( \
  619. 1, value.Get##CamelCppType##Value(), target); \
  620. break;
  621. CASE_TYPE(INT64, Int64, Int64)
  622. CASE_TYPE(UINT64, UInt64, UInt64)
  623. CASE_TYPE(INT32, Int32, Int32)
  624. CASE_TYPE(FIXED64, Fixed64, UInt64)
  625. CASE_TYPE(FIXED32, Fixed32, UInt32)
  626. CASE_TYPE(BOOL, Bool, Bool)
  627. CASE_TYPE(UINT32, UInt32, UInt32)
  628. CASE_TYPE(SFIXED32, SFixed32, Int32)
  629. CASE_TYPE(SFIXED64, SFixed64, Int64)
  630. CASE_TYPE(SINT32, SInt32, Int32)
  631. CASE_TYPE(SINT64, SInt64, Int64)
  632. #undef CASE_TYPE
  633. case FieldDescriptor::TYPE_STRING:
  634. target = stream->WriteString(1, value.GetStringValue(), target);
  635. break;
  636. }
  637. return target;
  638. }
  639. static uint8* SerializeMapValueRefWithCachedSizes(
  640. const FieldDescriptor* field, const MapValueRef& value, uint8* target,
  641. io::EpsCopyOutputStream* stream) {
  642. target = stream->EnsureSpace(target);
  643. switch (field->type()) {
  644. #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
  645. case FieldDescriptor::TYPE_##FieldType: \
  646. target = WireFormatLite::Write##CamelFieldType##ToArray( \
  647. 2, value.Get##CamelCppType##Value(), target); \
  648. break;
  649. CASE_TYPE(INT64, Int64, Int64)
  650. CASE_TYPE(UINT64, UInt64, UInt64)
  651. CASE_TYPE(INT32, Int32, Int32)
  652. CASE_TYPE(FIXED64, Fixed64, UInt64)
  653. CASE_TYPE(FIXED32, Fixed32, UInt32)
  654. CASE_TYPE(BOOL, Bool, Bool)
  655. CASE_TYPE(UINT32, UInt32, UInt32)
  656. CASE_TYPE(SFIXED32, SFixed32, Int32)
  657. CASE_TYPE(SFIXED64, SFixed64, Int64)
  658. CASE_TYPE(SINT32, SInt32, Int32)
  659. CASE_TYPE(SINT64, SInt64, Int64)
  660. CASE_TYPE(ENUM, Enum, Enum)
  661. CASE_TYPE(DOUBLE, Double, Double)
  662. CASE_TYPE(FLOAT, Float, Float)
  663. #undef CASE_TYPE
  664. case FieldDescriptor::TYPE_STRING:
  665. case FieldDescriptor::TYPE_BYTES:
  666. target = stream->WriteString(2, value.GetStringValue(), target);
  667. break;
  668. case FieldDescriptor::TYPE_MESSAGE:
  669. target = WireFormatLite::InternalWriteMessageToArray(
  670. 2, value.GetMessageValue(), target, stream);
  671. break;
  672. case FieldDescriptor::TYPE_GROUP:
  673. target = WireFormatLite::InternalWriteGroupToArray(
  674. 2, value.GetMessageValue(), target, stream);
  675. break;
  676. }
  677. return target;
  678. }
  679. class MapKeySorter {
  680. public:
  681. static std::vector<MapKey> SortKey(const Message& message,
  682. const Reflection* reflection,
  683. const FieldDescriptor* field) {
  684. std::vector<MapKey> sorted_key_list;
  685. for (MapIterator it =
  686. reflection->MapBegin(const_cast<Message*>(&message), field);
  687. it != reflection->MapEnd(const_cast<Message*>(&message), field);
  688. ++it) {
  689. sorted_key_list.push_back(it.GetKey());
  690. }
  691. MapKeyComparator comparator;
  692. std::sort(sorted_key_list.begin(), sorted_key_list.end(), comparator);
  693. return sorted_key_list;
  694. }
  695. private:
  696. class MapKeyComparator {
  697. public:
  698. bool operator()(const MapKey& a, const MapKey& b) const {
  699. GOOGLE_DCHECK(a.type() == b.type());
  700. switch (a.type()) {
  701. #define CASE_TYPE(CppType, CamelCppType) \
  702. case FieldDescriptor::CPPTYPE_##CppType: { \
  703. return a.Get##CamelCppType##Value() < b.Get##CamelCppType##Value(); \
  704. }
  705. CASE_TYPE(STRING, String)
  706. CASE_TYPE(INT64, Int64)
  707. CASE_TYPE(INT32, Int32)
  708. CASE_TYPE(UINT64, UInt64)
  709. CASE_TYPE(UINT32, UInt32)
  710. CASE_TYPE(BOOL, Bool)
  711. #undef CASE_TYPE
  712. default:
  713. GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
  714. return true;
  715. }
  716. }
  717. };
  718. };
  719. static uint8* InternalSerializeMapEntry(const FieldDescriptor* field,
  720. const MapKey& key,
  721. const MapValueRef& value, uint8* target,
  722. io::EpsCopyOutputStream* stream) {
  723. const FieldDescriptor* key_field = field->message_type()->field(0);
  724. const FieldDescriptor* value_field = field->message_type()->field(1);
  725. size_t size = kMapEntryTagByteSize;
  726. size += MapKeyDataOnlyByteSize(key_field, key);
  727. size += MapValueRefDataOnlyByteSize(value_field, value);
  728. target = stream->EnsureSpace(target);
  729. target = WireFormatLite::WriteTagToArray(
  730. field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED, target);
  731. target = io::CodedOutputStream::WriteVarint32ToArray(size, target);
  732. target = SerializeMapKeyWithCachedSizes(key_field, key, target, stream);
  733. target =
  734. SerializeMapValueRefWithCachedSizes(value_field, value, target, stream);
  735. return target;
  736. }
  737. uint8* WireFormat::InternalSerializeField(const FieldDescriptor* field,
  738. const Message& message, uint8* target,
  739. io::EpsCopyOutputStream* stream) {
  740. const Reflection* message_reflection = message.GetReflection();
  741. if (field->is_extension() &&
  742. field->containing_type()->options().message_set_wire_format() &&
  743. field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  744. !field->is_repeated()) {
  745. return InternalSerializeMessageSetItem(field, message, target, stream);
  746. }
  747. // For map fields, we can use either repeated field reflection or map
  748. // reflection. Our choice has some subtle effects. If we use repeated field
  749. // reflection here, then the repeated field representation becomes
  750. // authoritative for this field: any existing references that came from map
  751. // reflection remain valid for reading, but mutations to them are lost and
  752. // will be overwritten next time we call map reflection!
  753. //
  754. // So far this mainly affects Python, which keeps long-term references to map
  755. // values around, and always uses map reflection. See: b/35918691
  756. //
  757. // Here we choose to use map reflection API as long as the internal
  758. // map is valid. In this way, the serialization doesn't change map field's
  759. // internal state and existing references that came from map reflection remain
  760. // valid for both reading and writing.
  761. if (field->is_map()) {
  762. const MapFieldBase* map_field =
  763. message_reflection->GetMapData(message, field);
  764. if (map_field->IsMapValid()) {
  765. if (stream->IsSerializationDeterministic()) {
  766. std::vector<MapKey> sorted_key_list =
  767. MapKeySorter::SortKey(message, message_reflection, field);
  768. for (std::vector<MapKey>::iterator it = sorted_key_list.begin();
  769. it != sorted_key_list.end(); ++it) {
  770. MapValueRef map_value;
  771. message_reflection->InsertOrLookupMapValue(
  772. const_cast<Message*>(&message), field, *it, &map_value);
  773. target =
  774. InternalSerializeMapEntry(field, *it, map_value, target, stream);
  775. }
  776. } else {
  777. for (MapIterator it = message_reflection->MapBegin(
  778. const_cast<Message*>(&message), field);
  779. it !=
  780. message_reflection->MapEnd(const_cast<Message*>(&message), field);
  781. ++it) {
  782. target = InternalSerializeMapEntry(field, it.GetKey(),
  783. it.GetValueRef(), target, stream);
  784. }
  785. }
  786. return target;
  787. }
  788. }
  789. int count = 0;
  790. if (field->is_repeated()) {
  791. count = message_reflection->FieldSize(message, field);
  792. } else if (field->containing_type()->options().map_entry()) {
  793. // Map entry fields always need to be serialized.
  794. count = 1;
  795. } else if (message_reflection->HasField(message, field)) {
  796. count = 1;
  797. }
  798. // map_entries is for maps that'll be deterministically serialized.
  799. std::vector<const Message*> map_entries;
  800. if (count > 1 && field->is_map() && stream->IsSerializationDeterministic()) {
  801. map_entries =
  802. DynamicMapSorter::Sort(message, count, message_reflection, field);
  803. }
  804. if (field->is_packed()) {
  805. if (count == 0) return target;
  806. target = stream->EnsureSpace(target);
  807. switch (field->type()) {
  808. #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  809. case FieldDescriptor::TYPE_##TYPE: { \
  810. auto r = message_reflection->GetRepeatedField<CPPTYPE>(message, field); \
  811. target = stream->Write##TYPE_METHOD##Packed( \
  812. field->number(), r, FieldDataOnlyByteSize(field, message), target); \
  813. break; \
  814. }
  815. HANDLE_PRIMITIVE_TYPE(INT32, int32, Int32, Int32)
  816. HANDLE_PRIMITIVE_TYPE(INT64, int64, Int64, Int64)
  817. HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32)
  818. HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64)
  819. HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32)
  820. HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64)
  821. HANDLE_PRIMITIVE_TYPE(ENUM, int, Enum, Enum)
  822. #undef HANDLE_PRIMITIVE_TYPE
  823. #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  824. case FieldDescriptor::TYPE_##TYPE: { \
  825. auto r = message_reflection->GetRepeatedField<CPPTYPE>(message, field); \
  826. target = stream->WriteFixedPacked(field->number(), r, target); \
  827. break; \
  828. }
  829. HANDLE_PRIMITIVE_TYPE(FIXED32, uint32, Fixed32, UInt32)
  830. HANDLE_PRIMITIVE_TYPE(FIXED64, uint64, Fixed64, UInt64)
  831. HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32)
  832. HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64)
  833. HANDLE_PRIMITIVE_TYPE(FLOAT, float, Float, Float)
  834. HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double)
  835. HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool)
  836. #undef HANDLE_PRIMITIVE_TYPE
  837. default:
  838. GOOGLE_LOG(FATAL) << "Invalid descriptor";
  839. }
  840. return target;
  841. }
  842. for (int j = 0; j < count; j++) {
  843. target = stream->EnsureSpace(target);
  844. switch (field->type()) {
  845. #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  846. case FieldDescriptor::TYPE_##TYPE: { \
  847. const CPPTYPE value = \
  848. field->is_repeated() \
  849. ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \
  850. j) \
  851. : message_reflection->Get##CPPTYPE_METHOD(message, field); \
  852. target = WireFormatLite::Write##TYPE_METHOD##ToArray(field->number(), \
  853. value, target); \
  854. break; \
  855. }
  856. HANDLE_PRIMITIVE_TYPE(INT32, int32, Int32, Int32)
  857. HANDLE_PRIMITIVE_TYPE(INT64, int64, Int64, Int64)
  858. HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32)
  859. HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64)
  860. HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32)
  861. HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64)
  862. HANDLE_PRIMITIVE_TYPE(FIXED32, uint32, Fixed32, UInt32)
  863. HANDLE_PRIMITIVE_TYPE(FIXED64, uint64, Fixed64, UInt64)
  864. HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32)
  865. HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64)
  866. HANDLE_PRIMITIVE_TYPE(FLOAT, float, Float, Float)
  867. HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double)
  868. HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool)
  869. #undef HANDLE_PRIMITIVE_TYPE
  870. #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  871. case FieldDescriptor::TYPE_##TYPE: \
  872. target = WireFormatLite::InternalWrite##TYPE_METHOD##ToArray( \
  873. field->number(), \
  874. field->is_repeated() \
  875. ? (map_entries.empty() \
  876. ? message_reflection->GetRepeated##CPPTYPE_METHOD(message, \
  877. field, j) \
  878. : *map_entries[j]) \
  879. : message_reflection->Get##CPPTYPE_METHOD(message, field), \
  880. target, stream); \
  881. break;
  882. HANDLE_TYPE(GROUP, Group, Message)
  883. HANDLE_TYPE(MESSAGE, Message, Message)
  884. #undef HANDLE_TYPE
  885. case FieldDescriptor::TYPE_ENUM: {
  886. const EnumValueDescriptor* value =
  887. field->is_repeated()
  888. ? message_reflection->GetRepeatedEnum(message, field, j)
  889. : message_reflection->GetEnum(message, field);
  890. target = WireFormatLite::WriteEnumToArray(field->number(),
  891. value->number(), target);
  892. break;
  893. }
  894. // Handle strings separately so that we can get string references
  895. // instead of copying.
  896. case FieldDescriptor::TYPE_STRING: {
  897. bool strict_utf8_check = StrictUtf8Check(field);
  898. std::string scratch;
  899. const std::string& value =
  900. field->is_repeated()
  901. ? message_reflection->GetRepeatedStringReference(message, field,
  902. j, &scratch)
  903. : message_reflection->GetStringReference(message, field,
  904. &scratch);
  905. if (strict_utf8_check) {
  906. WireFormatLite::VerifyUtf8String(value.data(), value.length(),
  907. WireFormatLite::SERIALIZE,
  908. field->full_name().c_str());
  909. } else {
  910. VerifyUTF8StringNamedField(value.data(), value.length(), SERIALIZE,
  911. field->full_name().c_str());
  912. }
  913. target = stream->WriteString(field->number(), value, target);
  914. break;
  915. }
  916. case FieldDescriptor::TYPE_BYTES: {
  917. std::string scratch;
  918. const std::string& value =
  919. field->is_repeated()
  920. ? message_reflection->GetRepeatedStringReference(message, field,
  921. j, &scratch)
  922. : message_reflection->GetStringReference(message, field,
  923. &scratch);
  924. target = stream->WriteString(field->number(), value, target);
  925. break;
  926. }
  927. }
  928. }
  929. return target;
  930. }
  931. uint8* WireFormat::InternalSerializeMessageSetItem(
  932. const FieldDescriptor* field, const Message& message, uint8* target,
  933. io::EpsCopyOutputStream* stream) {
  934. const Reflection* message_reflection = message.GetReflection();
  935. target = stream->EnsureSpace(target);
  936. // Start group.
  937. target = io::CodedOutputStream::WriteTagToArray(
  938. WireFormatLite::kMessageSetItemStartTag, target);
  939. // Write type ID.
  940. target = WireFormatLite::WriteUInt32ToArray(
  941. WireFormatLite::kMessageSetTypeIdNumber, field->number(), target);
  942. // Write message.
  943. target = WireFormatLite::InternalWriteMessageToArray(
  944. WireFormatLite::kMessageSetMessageNumber,
  945. message_reflection->GetMessage(message, field), target, stream);
  946. // End group.
  947. target = stream->EnsureSpace(target);
  948. target = io::CodedOutputStream::WriteTagToArray(
  949. WireFormatLite::kMessageSetItemEndTag, target);
  950. return target;
  951. }
  952. // ===================================================================
  953. size_t WireFormat::ByteSize(const Message& message) {
  954. const Descriptor* descriptor = message.GetDescriptor();
  955. const Reflection* message_reflection = message.GetReflection();
  956. size_t our_size = 0;
  957. std::vector<const FieldDescriptor*> fields;
  958. // Fields of map entry should always be serialized.
  959. if (descriptor->options().map_entry()) {
  960. for (int i = 0; i < descriptor->field_count(); i++) {
  961. fields.push_back(descriptor->field(i));
  962. }
  963. } else {
  964. message_reflection->ListFields(message, &fields);
  965. }
  966. for (int i = 0; i < fields.size(); i++) {
  967. our_size += FieldByteSize(fields[i], message);
  968. }
  969. if (descriptor->options().message_set_wire_format()) {
  970. our_size += ComputeUnknownMessageSetItemsSize(
  971. message_reflection->GetUnknownFields(message));
  972. } else {
  973. our_size +=
  974. ComputeUnknownFieldsSize(message_reflection->GetUnknownFields(message));
  975. }
  976. return our_size;
  977. }
  978. size_t WireFormat::FieldByteSize(const FieldDescriptor* field,
  979. const Message& message) {
  980. const Reflection* message_reflection = message.GetReflection();
  981. if (field->is_extension() &&
  982. field->containing_type()->options().message_set_wire_format() &&
  983. field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  984. !field->is_repeated()) {
  985. return MessageSetItemByteSize(field, message);
  986. }
  987. size_t count = 0;
  988. if (field->is_repeated()) {
  989. count = FromIntSize(message_reflection->FieldSize(message, field));
  990. } else if (field->containing_type()->options().map_entry()) {
  991. // Map entry fields always need to be serialized.
  992. count = 1;
  993. } else if (message_reflection->HasField(message, field)) {
  994. count = 1;
  995. }
  996. const size_t data_size = FieldDataOnlyByteSize(field, message);
  997. size_t our_size = data_size;
  998. if (field->is_packed()) {
  999. if (data_size > 0) {
  1000. // Packed fields get serialized like a string, not their native type.
  1001. // Technically this doesn't really matter; the size only changes if it's
  1002. // a GROUP
  1003. our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING);
  1004. our_size += io::CodedOutputStream::VarintSize32(data_size);
  1005. }
  1006. } else {
  1007. our_size += count * TagSize(field->number(), field->type());
  1008. }
  1009. return our_size;
  1010. }
  1011. static size_t MapKeyDataOnlyByteSize(const FieldDescriptor* field,
  1012. const MapKey& value) {
  1013. GOOGLE_DCHECK_EQ(FieldDescriptor::TypeToCppType(field->type()), value.type());
  1014. switch (field->type()) {
  1015. case FieldDescriptor::TYPE_DOUBLE:
  1016. case FieldDescriptor::TYPE_FLOAT:
  1017. case FieldDescriptor::TYPE_GROUP:
  1018. case FieldDescriptor::TYPE_MESSAGE:
  1019. case FieldDescriptor::TYPE_BYTES:
  1020. case FieldDescriptor::TYPE_ENUM:
  1021. GOOGLE_LOG(FATAL) << "Unsupported";
  1022. return 0;
  1023. #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
  1024. case FieldDescriptor::TYPE_##FieldType: \
  1025. return WireFormatLite::CamelFieldType##Size( \
  1026. value.Get##CamelCppType##Value());
  1027. #define FIXED_CASE_TYPE(FieldType, CamelFieldType) \
  1028. case FieldDescriptor::TYPE_##FieldType: \
  1029. return WireFormatLite::k##CamelFieldType##Size;
  1030. CASE_TYPE(INT32, Int32, Int32);
  1031. CASE_TYPE(INT64, Int64, Int64);
  1032. CASE_TYPE(UINT32, UInt32, UInt32);
  1033. CASE_TYPE(UINT64, UInt64, UInt64);
  1034. CASE_TYPE(SINT32, SInt32, Int32);
  1035. CASE_TYPE(SINT64, SInt64, Int64);
  1036. CASE_TYPE(STRING, String, String);
  1037. FIXED_CASE_TYPE(FIXED32, Fixed32);
  1038. FIXED_CASE_TYPE(FIXED64, Fixed64);
  1039. FIXED_CASE_TYPE(SFIXED32, SFixed32);
  1040. FIXED_CASE_TYPE(SFIXED64, SFixed64);
  1041. FIXED_CASE_TYPE(BOOL, Bool);
  1042. #undef CASE_TYPE
  1043. #undef FIXED_CASE_TYPE
  1044. }
  1045. GOOGLE_LOG(FATAL) << "Cannot get here";
  1046. return 0;
  1047. }
  1048. static size_t MapValueRefDataOnlyByteSize(const FieldDescriptor* field,
  1049. const MapValueRef& value) {
  1050. switch (field->type()) {
  1051. case FieldDescriptor::TYPE_GROUP:
  1052. GOOGLE_LOG(FATAL) << "Unsupported";
  1053. return 0;
  1054. #define CASE_TYPE(FieldType, CamelFieldType, CamelCppType) \
  1055. case FieldDescriptor::TYPE_##FieldType: \
  1056. return WireFormatLite::CamelFieldType##Size( \
  1057. value.Get##CamelCppType##Value());
  1058. #define FIXED_CASE_TYPE(FieldType, CamelFieldType) \
  1059. case FieldDescriptor::TYPE_##FieldType: \
  1060. return WireFormatLite::k##CamelFieldType##Size;
  1061. CASE_TYPE(INT32, Int32, Int32);
  1062. CASE_TYPE(INT64, Int64, Int64);
  1063. CASE_TYPE(UINT32, UInt32, UInt32);
  1064. CASE_TYPE(UINT64, UInt64, UInt64);
  1065. CASE_TYPE(SINT32, SInt32, Int32);
  1066. CASE_TYPE(SINT64, SInt64, Int64);
  1067. CASE_TYPE(STRING, String, String);
  1068. CASE_TYPE(BYTES, Bytes, String);
  1069. CASE_TYPE(ENUM, Enum, Enum);
  1070. CASE_TYPE(MESSAGE, Message, Message);
  1071. FIXED_CASE_TYPE(FIXED32, Fixed32);
  1072. FIXED_CASE_TYPE(FIXED64, Fixed64);
  1073. FIXED_CASE_TYPE(SFIXED32, SFixed32);
  1074. FIXED_CASE_TYPE(SFIXED64, SFixed64);
  1075. FIXED_CASE_TYPE(DOUBLE, Double);
  1076. FIXED_CASE_TYPE(FLOAT, Float);
  1077. FIXED_CASE_TYPE(BOOL, Bool);
  1078. #undef CASE_TYPE
  1079. #undef FIXED_CASE_TYPE
  1080. }
  1081. GOOGLE_LOG(FATAL) << "Cannot get here";
  1082. return 0;
  1083. }
  1084. size_t WireFormat::FieldDataOnlyByteSize(const FieldDescriptor* field,
  1085. const Message& message) {
  1086. const Reflection* message_reflection = message.GetReflection();
  1087. size_t data_size = 0;
  1088. if (field->is_map()) {
  1089. const MapFieldBase* map_field =
  1090. message_reflection->GetMapData(message, field);
  1091. if (map_field->IsMapValid()) {
  1092. MapIterator iter(const_cast<Message*>(&message), field);
  1093. MapIterator end(const_cast<Message*>(&message), field);
  1094. const FieldDescriptor* key_field = field->message_type()->field(0);
  1095. const FieldDescriptor* value_field = field->message_type()->field(1);
  1096. for (map_field->MapBegin(&iter), map_field->MapEnd(&end); iter != end;
  1097. ++iter) {
  1098. size_t size = kMapEntryTagByteSize;
  1099. size += MapKeyDataOnlyByteSize(key_field, iter.GetKey());
  1100. size += MapValueRefDataOnlyByteSize(value_field, iter.GetValueRef());
  1101. data_size += WireFormatLite::LengthDelimitedSize(size);
  1102. }
  1103. return data_size;
  1104. }
  1105. }
  1106. size_t count = 0;
  1107. if (field->is_repeated()) {
  1108. count =
  1109. internal::FromIntSize(message_reflection->FieldSize(message, field));
  1110. } else if (field->containing_type()->options().map_entry()) {
  1111. // Map entry fields always need to be serialized.
  1112. count = 1;
  1113. } else if (message_reflection->HasField(message, field)) {
  1114. count = 1;
  1115. }
  1116. switch (field->type()) {
  1117. #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  1118. case FieldDescriptor::TYPE_##TYPE: \
  1119. if (field->is_repeated()) { \
  1120. for (int j = 0; j < count; j++) { \
  1121. data_size += WireFormatLite::TYPE_METHOD##Size( \
  1122. message_reflection->GetRepeated##CPPTYPE_METHOD(message, field, \
  1123. j)); \
  1124. } \
  1125. } else { \
  1126. data_size += WireFormatLite::TYPE_METHOD##Size( \
  1127. message_reflection->Get##CPPTYPE_METHOD(message, field)); \
  1128. } \
  1129. break;
  1130. #define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \
  1131. case FieldDescriptor::TYPE_##TYPE: \
  1132. data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \
  1133. break;
  1134. HANDLE_TYPE(INT32, Int32, Int32)
  1135. HANDLE_TYPE(INT64, Int64, Int64)
  1136. HANDLE_TYPE(SINT32, SInt32, Int32)
  1137. HANDLE_TYPE(SINT64, SInt64, Int64)
  1138. HANDLE_TYPE(UINT32, UInt32, UInt32)
  1139. HANDLE_TYPE(UINT64, UInt64, UInt64)
  1140. HANDLE_FIXED_TYPE(FIXED32, Fixed32)
  1141. HANDLE_FIXED_TYPE(FIXED64, Fixed64)
  1142. HANDLE_FIXED_TYPE(SFIXED32, SFixed32)
  1143. HANDLE_FIXED_TYPE(SFIXED64, SFixed64)
  1144. HANDLE_FIXED_TYPE(FLOAT, Float)
  1145. HANDLE_FIXED_TYPE(DOUBLE, Double)
  1146. HANDLE_FIXED_TYPE(BOOL, Bool)
  1147. HANDLE_TYPE(GROUP, Group, Message)
  1148. HANDLE_TYPE(MESSAGE, Message, Message)
  1149. #undef HANDLE_TYPE
  1150. #undef HANDLE_FIXED_TYPE
  1151. case FieldDescriptor::TYPE_ENUM: {
  1152. if (field->is_repeated()) {
  1153. for (int j = 0; j < count; j++) {
  1154. data_size += WireFormatLite::EnumSize(
  1155. message_reflection->GetRepeatedEnum(message, field, j)->number());
  1156. }
  1157. } else {
  1158. data_size += WireFormatLite::EnumSize(
  1159. message_reflection->GetEnum(message, field)->number());
  1160. }
  1161. break;
  1162. }
  1163. // Handle strings separately so that we can get string references
  1164. // instead of copying.
  1165. case FieldDescriptor::TYPE_STRING:
  1166. case FieldDescriptor::TYPE_BYTES: {
  1167. for (int j = 0; j < count; j++) {
  1168. std::string scratch;
  1169. const std::string& value =
  1170. field->is_repeated()
  1171. ? message_reflection->GetRepeatedStringReference(message, field,
  1172. j, &scratch)
  1173. : message_reflection->GetStringReference(message, field,
  1174. &scratch);
  1175. data_size += WireFormatLite::StringSize(value);
  1176. }
  1177. break;
  1178. }
  1179. }
  1180. return data_size;
  1181. }
  1182. size_t WireFormat::MessageSetItemByteSize(const FieldDescriptor* field,
  1183. const Message& message) {
  1184. const Reflection* message_reflection = message.GetReflection();
  1185. size_t our_size = WireFormatLite::kMessageSetItemTagsSize;
  1186. // type_id
  1187. our_size += io::CodedOutputStream::VarintSize32(field->number());
  1188. // message
  1189. const Message& sub_message = message_reflection->GetMessage(message, field);
  1190. size_t message_size = sub_message.ByteSizeLong();
  1191. our_size += io::CodedOutputStream::VarintSize32(message_size);
  1192. our_size += message_size;
  1193. return our_size;
  1194. }
  1195. // Compute the size of the UnknownFieldSet on the wire.
  1196. size_t ComputeUnknownFieldsSize(const InternalMetadataWithArena& metadata,
  1197. size_t total_size, CachedSize* cached_size) {
  1198. total_size += WireFormat::ComputeUnknownFieldsSize(metadata.unknown_fields());
  1199. cached_size->Set(ToCachedSize(total_size));
  1200. return total_size;
  1201. }
  1202. } // namespace internal
  1203. } // namespace protobuf
  1204. } // namespace google