/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/wire_format.cc

http://github.com/tomahawk-player/tomahawk · C++ · 1069 lines · 827 code · 144 blank · 98 comment · 155 complexity · 21ec333087711b3d30a42abc592ab3b7 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 <stack>
  34. #include <string>
  35. #include <vector>
  36. #include <google/protobuf/wire_format.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/descriptor.h>
  39. #include <google/protobuf/wire_format_lite_inl.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/unknown_field_set.h>
  45. namespace google {
  46. namespace protobuf {
  47. namespace internal {
  48. using internal::WireFormatLite;
  49. namespace {
  50. // This function turns out to be convenient when using some macros later.
  51. inline int GetEnumNumber(const EnumValueDescriptor* descriptor) {
  52. return descriptor->number();
  53. }
  54. } // anonymous namespace
  55. // ===================================================================
  56. bool UnknownFieldSetFieldSkipper::SkipField(
  57. io::CodedInputStream* input, uint32 tag) {
  58. return WireFormat::SkipField(input, tag, unknown_fields_);
  59. }
  60. bool UnknownFieldSetFieldSkipper::SkipMessage(io::CodedInputStream* input) {
  61. return WireFormat::SkipMessage(input, unknown_fields_);
  62. }
  63. void UnknownFieldSetFieldSkipper::SkipUnknownEnum(
  64. int field_number, int value) {
  65. unknown_fields_->AddVarint(field_number, value);
  66. }
  67. bool WireFormat::SkipField(io::CodedInputStream* input, uint32 tag,
  68. UnknownFieldSet* unknown_fields) {
  69. int number = WireFormatLite::GetTagFieldNumber(tag);
  70. switch (WireFormatLite::GetTagWireType(tag)) {
  71. case WireFormatLite::WIRETYPE_VARINT: {
  72. uint64 value;
  73. if (!input->ReadVarint64(&value)) return false;
  74. if (unknown_fields != NULL) unknown_fields->AddVarint(number, value);
  75. return true;
  76. }
  77. case WireFormatLite::WIRETYPE_FIXED64: {
  78. uint64 value;
  79. if (!input->ReadLittleEndian64(&value)) return false;
  80. if (unknown_fields != NULL) unknown_fields->AddFixed64(number, value);
  81. return true;
  82. }
  83. case WireFormatLite::WIRETYPE_LENGTH_DELIMITED: {
  84. uint32 length;
  85. if (!input->ReadVarint32(&length)) return false;
  86. if (unknown_fields == NULL) {
  87. if (!input->Skip(length)) return false;
  88. } else {
  89. if (!input->ReadString(unknown_fields->AddLengthDelimited(number),
  90. length)) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. case WireFormatLite::WIRETYPE_START_GROUP: {
  97. if (!input->IncrementRecursionDepth()) return false;
  98. if (!SkipMessage(input, (unknown_fields == NULL) ?
  99. NULL : unknown_fields->AddGroup(number))) {
  100. return false;
  101. }
  102. input->DecrementRecursionDepth();
  103. // Check that the ending tag matched the starting tag.
  104. if (!input->LastTagWas(WireFormatLite::MakeTag(
  105. WireFormatLite::GetTagFieldNumber(tag),
  106. WireFormatLite::WIRETYPE_END_GROUP))) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. case WireFormatLite::WIRETYPE_END_GROUP: {
  112. return false;
  113. }
  114. case WireFormatLite::WIRETYPE_FIXED32: {
  115. uint32 value;
  116. if (!input->ReadLittleEndian32(&value)) return false;
  117. if (unknown_fields != NULL) unknown_fields->AddFixed32(number, value);
  118. return true;
  119. }
  120. default: {
  121. return false;
  122. }
  123. }
  124. }
  125. bool WireFormat::SkipMessage(io::CodedInputStream* input,
  126. UnknownFieldSet* unknown_fields) {
  127. while(true) {
  128. uint32 tag = input->ReadTag();
  129. if (tag == 0) {
  130. // End of input. This is a valid place to end, so return true.
  131. return true;
  132. }
  133. WireFormatLite::WireType wire_type = WireFormatLite::GetTagWireType(tag);
  134. if (wire_type == WireFormatLite::WIRETYPE_END_GROUP) {
  135. // Must be the end of the message.
  136. return true;
  137. }
  138. if (!SkipField(input, tag, unknown_fields)) return false;
  139. }
  140. }
  141. void WireFormat::SerializeUnknownFields(const UnknownFieldSet& unknown_fields,
  142. io::CodedOutputStream* output) {
  143. for (int i = 0; i < unknown_fields.field_count(); i++) {
  144. const UnknownField& field = unknown_fields.field(i);
  145. switch (field.type()) {
  146. case UnknownField::TYPE_VARINT:
  147. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  148. WireFormatLite::WIRETYPE_VARINT));
  149. output->WriteVarint64(field.varint());
  150. break;
  151. case UnknownField::TYPE_FIXED32:
  152. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  153. WireFormatLite::WIRETYPE_FIXED32));
  154. output->WriteLittleEndian32(field.fixed32());
  155. break;
  156. case UnknownField::TYPE_FIXED64:
  157. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  158. WireFormatLite::WIRETYPE_FIXED64));
  159. output->WriteLittleEndian64(field.fixed64());
  160. break;
  161. case UnknownField::TYPE_LENGTH_DELIMITED:
  162. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  163. WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
  164. output->WriteVarint32(field.length_delimited().size());
  165. output->WriteString(field.length_delimited());
  166. break;
  167. case UnknownField::TYPE_GROUP:
  168. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  169. WireFormatLite::WIRETYPE_START_GROUP));
  170. SerializeUnknownFields(field.group(), output);
  171. output->WriteVarint32(WireFormatLite::MakeTag(field.number(),
  172. WireFormatLite::WIRETYPE_END_GROUP));
  173. break;
  174. }
  175. }
  176. }
  177. uint8* WireFormat::SerializeUnknownFieldsToArray(
  178. const UnknownFieldSet& unknown_fields,
  179. uint8* target) {
  180. for (int i = 0; i < unknown_fields.field_count(); i++) {
  181. const UnknownField& field = unknown_fields.field(i);
  182. switch (field.type()) {
  183. case UnknownField::TYPE_VARINT:
  184. target = WireFormatLite::WriteInt64ToArray(
  185. field.number(), field.varint(), target);
  186. break;
  187. case UnknownField::TYPE_FIXED32:
  188. target = WireFormatLite::WriteFixed32ToArray(
  189. field.number(), field.fixed32(), target);
  190. break;
  191. case UnknownField::TYPE_FIXED64:
  192. target = WireFormatLite::WriteFixed64ToArray(
  193. field.number(), field.fixed64(), target);
  194. break;
  195. case UnknownField::TYPE_LENGTH_DELIMITED:
  196. target = WireFormatLite::WriteBytesToArray(
  197. field.number(), field.length_delimited(), target);
  198. break;
  199. case UnknownField::TYPE_GROUP:
  200. target = WireFormatLite::WriteTagToArray(
  201. field.number(), WireFormatLite::WIRETYPE_START_GROUP, target);
  202. target = SerializeUnknownFieldsToArray(field.group(), target);
  203. target = WireFormatLite::WriteTagToArray(
  204. field.number(), WireFormatLite::WIRETYPE_END_GROUP, target);
  205. break;
  206. }
  207. }
  208. return target;
  209. }
  210. void WireFormat::SerializeUnknownMessageSetItems(
  211. const UnknownFieldSet& unknown_fields,
  212. io::CodedOutputStream* output) {
  213. for (int i = 0; i < unknown_fields.field_count(); i++) {
  214. const UnknownField& field = unknown_fields.field(i);
  215. // The only unknown fields that are allowed to exist in a MessageSet are
  216. // messages, which are length-delimited.
  217. if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
  218. const string& data = field.length_delimited();
  219. // Start group.
  220. output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
  221. // Write type ID.
  222. output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
  223. output->WriteVarint32(field.number());
  224. // Write message.
  225. output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
  226. output->WriteVarint32(data.size());
  227. output->WriteString(data);
  228. // End group.
  229. output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
  230. }
  231. }
  232. }
  233. uint8* WireFormat::SerializeUnknownMessageSetItemsToArray(
  234. const UnknownFieldSet& unknown_fields,
  235. uint8* target) {
  236. for (int i = 0; i < unknown_fields.field_count(); i++) {
  237. const UnknownField& field = unknown_fields.field(i);
  238. // The only unknown fields that are allowed to exist in a MessageSet are
  239. // messages, which are length-delimited.
  240. if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
  241. const string& data = field.length_delimited();
  242. // Start group.
  243. target = io::CodedOutputStream::WriteTagToArray(
  244. WireFormatLite::kMessageSetItemStartTag, target);
  245. // Write type ID.
  246. target = io::CodedOutputStream::WriteTagToArray(
  247. WireFormatLite::kMessageSetTypeIdTag, target);
  248. target = io::CodedOutputStream::WriteVarint32ToArray(
  249. field.number(), target);
  250. // Write message.
  251. target = io::CodedOutputStream::WriteTagToArray(
  252. WireFormatLite::kMessageSetMessageTag, target);
  253. target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target);
  254. target = io::CodedOutputStream::WriteStringToArray(data, target);
  255. // End group.
  256. target = io::CodedOutputStream::WriteTagToArray(
  257. WireFormatLite::kMessageSetItemEndTag, target);
  258. }
  259. }
  260. return target;
  261. }
  262. int WireFormat::ComputeUnknownFieldsSize(
  263. const UnknownFieldSet& unknown_fields) {
  264. int size = 0;
  265. for (int i = 0; i < unknown_fields.field_count(); i++) {
  266. const UnknownField& field = unknown_fields.field(i);
  267. switch (field.type()) {
  268. case UnknownField::TYPE_VARINT:
  269. size += io::CodedOutputStream::VarintSize32(
  270. WireFormatLite::MakeTag(field.number(),
  271. WireFormatLite::WIRETYPE_VARINT));
  272. size += io::CodedOutputStream::VarintSize64(field.varint());
  273. break;
  274. case UnknownField::TYPE_FIXED32:
  275. size += io::CodedOutputStream::VarintSize32(
  276. WireFormatLite::MakeTag(field.number(),
  277. WireFormatLite::WIRETYPE_FIXED32));
  278. size += sizeof(int32);
  279. break;
  280. case UnknownField::TYPE_FIXED64:
  281. size += io::CodedOutputStream::VarintSize32(
  282. WireFormatLite::MakeTag(field.number(),
  283. WireFormatLite::WIRETYPE_FIXED64));
  284. size += sizeof(int64);
  285. break;
  286. case UnknownField::TYPE_LENGTH_DELIMITED:
  287. size += io::CodedOutputStream::VarintSize32(
  288. WireFormatLite::MakeTag(field.number(),
  289. WireFormatLite::WIRETYPE_LENGTH_DELIMITED));
  290. size += io::CodedOutputStream::VarintSize32(
  291. field.length_delimited().size());
  292. size += field.length_delimited().size();
  293. break;
  294. case UnknownField::TYPE_GROUP:
  295. size += io::CodedOutputStream::VarintSize32(
  296. WireFormatLite::MakeTag(field.number(),
  297. WireFormatLite::WIRETYPE_START_GROUP));
  298. size += ComputeUnknownFieldsSize(field.group());
  299. size += io::CodedOutputStream::VarintSize32(
  300. WireFormatLite::MakeTag(field.number(),
  301. WireFormatLite::WIRETYPE_END_GROUP));
  302. break;
  303. }
  304. }
  305. return size;
  306. }
  307. int WireFormat::ComputeUnknownMessageSetItemsSize(
  308. const UnknownFieldSet& unknown_fields) {
  309. int size = 0;
  310. for (int i = 0; i < unknown_fields.field_count(); i++) {
  311. const UnknownField& field = unknown_fields.field(i);
  312. // The only unknown fields that are allowed to exist in a MessageSet are
  313. // messages, which are length-delimited.
  314. if (field.type() == UnknownField::TYPE_LENGTH_DELIMITED) {
  315. size += WireFormatLite::kMessageSetItemTagsSize;
  316. size += io::CodedOutputStream::VarintSize32(field.number());
  317. size += io::CodedOutputStream::VarintSize32(
  318. field.length_delimited().size());
  319. size += field.length_delimited().size();
  320. }
  321. }
  322. return size;
  323. }
  324. // ===================================================================
  325. bool WireFormat::ParseAndMergePartial(io::CodedInputStream* input,
  326. Message* message) {
  327. const Descriptor* descriptor = message->GetDescriptor();
  328. const Reflection* message_reflection = message->GetReflection();
  329. while(true) {
  330. uint32 tag = input->ReadTag();
  331. if (tag == 0) {
  332. // End of input. This is a valid place to end, so return true.
  333. return true;
  334. }
  335. if (WireFormatLite::GetTagWireType(tag) ==
  336. WireFormatLite::WIRETYPE_END_GROUP) {
  337. // Must be the end of the message.
  338. return true;
  339. }
  340. const FieldDescriptor* field = NULL;
  341. if (descriptor != NULL) {
  342. int field_number = WireFormatLite::GetTagFieldNumber(tag);
  343. field = descriptor->FindFieldByNumber(field_number);
  344. // If that failed, check if the field is an extension.
  345. if (field == NULL && descriptor->IsExtensionNumber(field_number)) {
  346. if (input->GetExtensionPool() == NULL) {
  347. field = message_reflection->FindKnownExtensionByNumber(field_number);
  348. } else {
  349. field = input->GetExtensionPool()
  350. ->FindExtensionByNumber(descriptor, field_number);
  351. }
  352. }
  353. // If that failed, but we're a MessageSet, and this is the tag for a
  354. // MessageSet item, then parse that.
  355. if (field == NULL &&
  356. descriptor->options().message_set_wire_format() &&
  357. tag == WireFormatLite::kMessageSetItemStartTag) {
  358. if (!ParseAndMergeMessageSetItem(input, message)) {
  359. return false;
  360. }
  361. continue; // Skip ParseAndMergeField(); already taken care of.
  362. }
  363. }
  364. if (!ParseAndMergeField(tag, field, message, input)) {
  365. return false;
  366. }
  367. }
  368. }
  369. bool WireFormat::ParseAndMergeField(
  370. uint32 tag,
  371. const FieldDescriptor* field, // May be NULL for unknown
  372. Message* message,
  373. io::CodedInputStream* input) {
  374. const Reflection* message_reflection = message->GetReflection();
  375. enum { UNKNOWN, NORMAL_FORMAT, PACKED_FORMAT } value_format;
  376. if (field == NULL) {
  377. value_format = UNKNOWN;
  378. } else if (WireFormatLite::GetTagWireType(tag) ==
  379. WireTypeForFieldType(field->type())) {
  380. value_format = NORMAL_FORMAT;
  381. } else if (field->is_packable() &&
  382. WireFormatLite::GetTagWireType(tag) ==
  383. WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
  384. value_format = PACKED_FORMAT;
  385. } else {
  386. // We don't recognize this field. Either the field number is unknown
  387. // or the wire type doesn't match. Put it in our unknown field set.
  388. value_format = UNKNOWN;
  389. }
  390. if (value_format == UNKNOWN) {
  391. return SkipField(input, tag,
  392. message_reflection->MutableUnknownFields(message));
  393. } else if (value_format == PACKED_FORMAT) {
  394. uint32 length;
  395. if (!input->ReadVarint32(&length)) return false;
  396. io::CodedInputStream::Limit limit = input->PushLimit(length);
  397. switch (field->type()) {
  398. #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
  399. case FieldDescriptor::TYPE_##TYPE: { \
  400. while (input->BytesUntilLimit() > 0) { \
  401. CPPTYPE value; \
  402. if (!WireFormatLite::ReadPrimitive< \
  403. CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \
  404. return false; \
  405. message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
  406. } \
  407. break; \
  408. }
  409. HANDLE_PACKED_TYPE( INT32, int32, Int32)
  410. HANDLE_PACKED_TYPE( INT64, int64, Int64)
  411. HANDLE_PACKED_TYPE(SINT32, int32, Int32)
  412. HANDLE_PACKED_TYPE(SINT64, int64, Int64)
  413. HANDLE_PACKED_TYPE(UINT32, uint32, UInt32)
  414. HANDLE_PACKED_TYPE(UINT64, uint64, UInt64)
  415. HANDLE_PACKED_TYPE( FIXED32, uint32, UInt32)
  416. HANDLE_PACKED_TYPE( FIXED64, uint64, UInt64)
  417. HANDLE_PACKED_TYPE(SFIXED32, int32, Int32)
  418. HANDLE_PACKED_TYPE(SFIXED64, int64, Int64)
  419. HANDLE_PACKED_TYPE(FLOAT , float , Float )
  420. HANDLE_PACKED_TYPE(DOUBLE, double, Double)
  421. HANDLE_PACKED_TYPE(BOOL, bool, Bool)
  422. #undef HANDLE_PACKED_TYPE
  423. case FieldDescriptor::TYPE_ENUM: {
  424. while (input->BytesUntilLimit() > 0) {
  425. int value;
  426. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  427. input, &value)) return false;
  428. const EnumValueDescriptor* enum_value =
  429. field->enum_type()->FindValueByNumber(value);
  430. if (enum_value != NULL) {
  431. message_reflection->AddEnum(message, field, enum_value);
  432. }
  433. }
  434. break;
  435. }
  436. case FieldDescriptor::TYPE_STRING:
  437. case FieldDescriptor::TYPE_GROUP:
  438. case FieldDescriptor::TYPE_MESSAGE:
  439. case FieldDescriptor::TYPE_BYTES:
  440. // Can't have packed fields of these types: these should be caught by
  441. // the protocol compiler.
  442. return false;
  443. break;
  444. }
  445. input->PopLimit(limit);
  446. } else {
  447. // Non-packed value (value_format == NORMAL_FORMAT)
  448. switch (field->type()) {
  449. #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
  450. case FieldDescriptor::TYPE_##TYPE: { \
  451. CPPTYPE value; \
  452. if (!WireFormatLite::ReadPrimitive< \
  453. CPPTYPE, WireFormatLite::TYPE_##TYPE>(input, &value)) \
  454. return false; \
  455. if (field->is_repeated()) { \
  456. message_reflection->Add##CPPTYPE_METHOD(message, field, value); \
  457. } else { \
  458. message_reflection->Set##CPPTYPE_METHOD(message, field, value); \
  459. } \
  460. break; \
  461. }
  462. HANDLE_TYPE( INT32, int32, Int32)
  463. HANDLE_TYPE( INT64, int64, Int64)
  464. HANDLE_TYPE(SINT32, int32, Int32)
  465. HANDLE_TYPE(SINT64, int64, Int64)
  466. HANDLE_TYPE(UINT32, uint32, UInt32)
  467. HANDLE_TYPE(UINT64, uint64, UInt64)
  468. HANDLE_TYPE( FIXED32, uint32, UInt32)
  469. HANDLE_TYPE( FIXED64, uint64, UInt64)
  470. HANDLE_TYPE(SFIXED32, int32, Int32)
  471. HANDLE_TYPE(SFIXED64, int64, Int64)
  472. HANDLE_TYPE(FLOAT , float , Float )
  473. HANDLE_TYPE(DOUBLE, double, Double)
  474. HANDLE_TYPE(BOOL, bool, Bool)
  475. #undef HANDLE_TYPE
  476. case FieldDescriptor::TYPE_ENUM: {
  477. int value;
  478. if (!WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  479. input, &value)) return false;
  480. const EnumValueDescriptor* enum_value =
  481. field->enum_type()->FindValueByNumber(value);
  482. if (enum_value != NULL) {
  483. if (field->is_repeated()) {
  484. message_reflection->AddEnum(message, field, enum_value);
  485. } else {
  486. message_reflection->SetEnum(message, field, enum_value);
  487. }
  488. } else {
  489. // The enum value is not one of the known values. Add it to the
  490. // UnknownFieldSet.
  491. int64 sign_extended_value = static_cast<int64>(value);
  492. message_reflection->MutableUnknownFields(message)
  493. ->AddVarint(WireFormatLite::GetTagFieldNumber(tag),
  494. sign_extended_value);
  495. }
  496. break;
  497. }
  498. // Handle strings separately so that we can optimize the ctype=CORD case.
  499. case FieldDescriptor::TYPE_STRING: {
  500. string value;
  501. if (!WireFormatLite::ReadString(input, &value)) return false;
  502. VerifyUTF8String(value.data(), value.length(), PARSE);
  503. if (field->is_repeated()) {
  504. message_reflection->AddString(message, field, value);
  505. } else {
  506. message_reflection->SetString(message, field, value);
  507. }
  508. break;
  509. }
  510. case FieldDescriptor::TYPE_BYTES: {
  511. string value;
  512. if (!WireFormatLite::ReadBytes(input, &value)) return false;
  513. if (field->is_repeated()) {
  514. message_reflection->AddString(message, field, value);
  515. } else {
  516. message_reflection->SetString(message, field, value);
  517. }
  518. break;
  519. }
  520. case FieldDescriptor::TYPE_GROUP: {
  521. Message* sub_message;
  522. if (field->is_repeated()) {
  523. sub_message = message_reflection->AddMessage(
  524. message, field, input->GetExtensionFactory());
  525. } else {
  526. sub_message = message_reflection->MutableMessage(
  527. message, field, input->GetExtensionFactory());
  528. }
  529. if (!WireFormatLite::ReadGroup(WireFormatLite::GetTagFieldNumber(tag),
  530. input, sub_message))
  531. return false;
  532. break;
  533. }
  534. case FieldDescriptor::TYPE_MESSAGE: {
  535. Message* sub_message;
  536. if (field->is_repeated()) {
  537. sub_message = message_reflection->AddMessage(
  538. message, field, input->GetExtensionFactory());
  539. } else {
  540. sub_message = message_reflection->MutableMessage(
  541. message, field, input->GetExtensionFactory());
  542. }
  543. if (!WireFormatLite::ReadMessage(input, sub_message)) return false;
  544. break;
  545. }
  546. }
  547. }
  548. return true;
  549. }
  550. bool WireFormat::ParseAndMergeMessageSetItem(
  551. io::CodedInputStream* input,
  552. Message* message) {
  553. const Reflection* message_reflection = message->GetReflection();
  554. // This method parses a group which should contain two fields:
  555. // required int32 type_id = 2;
  556. // required data message = 3;
  557. // Once we see a type_id, we'll construct a fake tag for this extension
  558. // which is the tag it would have had under the proto2 extensions wire
  559. // format.
  560. uint32 fake_tag = 0;
  561. // Once we see a type_id, we'll look up the FieldDescriptor for the
  562. // extension.
  563. const FieldDescriptor* field = NULL;
  564. // If we see message data before the type_id, we'll append it to this so
  565. // we can parse it later. This will probably never happen in practice,
  566. // as no MessageSet encoder I know of writes the message before the type ID.
  567. // But, it's technically valid so we should allow it.
  568. // TODO(kenton): Use a Cord instead? Do I care?
  569. string message_data;
  570. while (true) {
  571. uint32 tag = input->ReadTag();
  572. if (tag == 0) return false;
  573. switch (tag) {
  574. case WireFormatLite::kMessageSetTypeIdTag: {
  575. uint32 type_id;
  576. if (!input->ReadVarint32(&type_id)) return false;
  577. fake_tag = WireFormatLite::MakeTag(
  578. type_id, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
  579. field = message_reflection->FindKnownExtensionByNumber(type_id);
  580. if (!message_data.empty()) {
  581. // We saw some message data before the type_id. Have to parse it
  582. // now.
  583. io::ArrayInputStream raw_input(message_data.data(),
  584. message_data.size());
  585. io::CodedInputStream sub_input(&raw_input);
  586. if (!ParseAndMergeField(fake_tag, field, message,
  587. &sub_input)) {
  588. return false;
  589. }
  590. message_data.clear();
  591. }
  592. break;
  593. }
  594. case WireFormatLite::kMessageSetMessageTag: {
  595. if (fake_tag == 0) {
  596. // We haven't seen a type_id yet. Append this data to message_data.
  597. string temp;
  598. uint32 length;
  599. if (!input->ReadVarint32(&length)) return false;
  600. if (!input->ReadString(&temp, length)) return false;
  601. message_data.append(temp);
  602. } else {
  603. // Already saw type_id, so we can parse this directly.
  604. if (!ParseAndMergeField(fake_tag, field, message, input)) {
  605. return false;
  606. }
  607. }
  608. break;
  609. }
  610. case WireFormatLite::kMessageSetItemEndTag: {
  611. return true;
  612. }
  613. default: {
  614. if (!SkipField(input, tag, NULL)) return false;
  615. }
  616. }
  617. }
  618. }
  619. // ===================================================================
  620. void WireFormat::SerializeWithCachedSizes(
  621. const Message& message,
  622. int size, io::CodedOutputStream* output) {
  623. const Descriptor* descriptor = message.GetDescriptor();
  624. const Reflection* message_reflection = message.GetReflection();
  625. int expected_endpoint = output->ByteCount() + size;
  626. vector<const FieldDescriptor*> fields;
  627. message_reflection->ListFields(message, &fields);
  628. for (int i = 0; i < fields.size(); i++) {
  629. SerializeFieldWithCachedSizes(fields[i], message, output);
  630. }
  631. if (descriptor->options().message_set_wire_format()) {
  632. SerializeUnknownMessageSetItems(
  633. message_reflection->GetUnknownFields(message), output);
  634. } else {
  635. SerializeUnknownFields(
  636. message_reflection->GetUnknownFields(message), output);
  637. }
  638. GOOGLE_CHECK_EQ(output->ByteCount(), expected_endpoint)
  639. << ": Protocol message serialized to a size different from what was "
  640. "originally expected. Perhaps it was modified by another thread "
  641. "during serialization?";
  642. }
  643. void WireFormat::SerializeFieldWithCachedSizes(
  644. const FieldDescriptor* field,
  645. const Message& message,
  646. io::CodedOutputStream* output) {
  647. const Reflection* message_reflection = message.GetReflection();
  648. if (field->is_extension() &&
  649. field->containing_type()->options().message_set_wire_format() &&
  650. field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  651. !field->is_repeated()) {
  652. SerializeMessageSetItemWithCachedSizes(field, message, output);
  653. return;
  654. }
  655. int count = 0;
  656. if (field->is_repeated()) {
  657. count = message_reflection->FieldSize(message, field);
  658. } else if (message_reflection->HasField(message, field)) {
  659. count = 1;
  660. }
  661. const bool is_packed = field->options().packed();
  662. if (is_packed && count > 0) {
  663. WireFormatLite::WriteTag(field->number(),
  664. WireFormatLite::WIRETYPE_LENGTH_DELIMITED, output);
  665. const int data_size = FieldDataOnlyByteSize(field, message);
  666. output->WriteVarint32(data_size);
  667. }
  668. for (int j = 0; j < count; j++) {
  669. switch (field->type()) {
  670. #define HANDLE_PRIMITIVE_TYPE(TYPE, CPPTYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  671. case FieldDescriptor::TYPE_##TYPE: { \
  672. const CPPTYPE value = field->is_repeated() ? \
  673. message_reflection->GetRepeated##CPPTYPE_METHOD( \
  674. message, field, j) : \
  675. message_reflection->Get##CPPTYPE_METHOD( \
  676. message, field); \
  677. if (is_packed) { \
  678. WireFormatLite::Write##TYPE_METHOD##NoTag(value, output); \
  679. } else { \
  680. WireFormatLite::Write##TYPE_METHOD(field->number(), value, output); \
  681. } \
  682. break; \
  683. }
  684. HANDLE_PRIMITIVE_TYPE( INT32, int32, Int32, Int32)
  685. HANDLE_PRIMITIVE_TYPE( INT64, int64, Int64, Int64)
  686. HANDLE_PRIMITIVE_TYPE(SINT32, int32, SInt32, Int32)
  687. HANDLE_PRIMITIVE_TYPE(SINT64, int64, SInt64, Int64)
  688. HANDLE_PRIMITIVE_TYPE(UINT32, uint32, UInt32, UInt32)
  689. HANDLE_PRIMITIVE_TYPE(UINT64, uint64, UInt64, UInt64)
  690. HANDLE_PRIMITIVE_TYPE( FIXED32, uint32, Fixed32, UInt32)
  691. HANDLE_PRIMITIVE_TYPE( FIXED64, uint64, Fixed64, UInt64)
  692. HANDLE_PRIMITIVE_TYPE(SFIXED32, int32, SFixed32, Int32)
  693. HANDLE_PRIMITIVE_TYPE(SFIXED64, int64, SFixed64, Int64)
  694. HANDLE_PRIMITIVE_TYPE(FLOAT , float , Float , Float )
  695. HANDLE_PRIMITIVE_TYPE(DOUBLE, double, Double, Double)
  696. HANDLE_PRIMITIVE_TYPE(BOOL, bool, Bool, Bool)
  697. #undef HANDLE_PRIMITIVE_TYPE
  698. #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  699. case FieldDescriptor::TYPE_##TYPE: \
  700. WireFormatLite::Write##TYPE_METHOD( \
  701. field->number(), \
  702. field->is_repeated() ? \
  703. message_reflection->GetRepeated##CPPTYPE_METHOD( \
  704. message, field, j) : \
  705. message_reflection->Get##CPPTYPE_METHOD(message, field), \
  706. output); \
  707. break;
  708. HANDLE_TYPE(GROUP , Group , Message)
  709. HANDLE_TYPE(MESSAGE, Message, Message)
  710. #undef HANDLE_TYPE
  711. case FieldDescriptor::TYPE_ENUM: {
  712. const EnumValueDescriptor* value = field->is_repeated() ?
  713. message_reflection->GetRepeatedEnum(message, field, j) :
  714. message_reflection->GetEnum(message, field);
  715. if (is_packed) {
  716. WireFormatLite::WriteEnumNoTag(value->number(), output);
  717. } else {
  718. WireFormatLite::WriteEnum(field->number(), value->number(), output);
  719. }
  720. break;
  721. }
  722. // Handle strings separately so that we can get string references
  723. // instead of copying.
  724. case FieldDescriptor::TYPE_STRING: {
  725. string scratch;
  726. const string& value = field->is_repeated() ?
  727. message_reflection->GetRepeatedStringReference(
  728. message, field, j, &scratch) :
  729. message_reflection->GetStringReference(message, field, &scratch);
  730. VerifyUTF8String(value.data(), value.length(), SERIALIZE);
  731. WireFormatLite::WriteString(field->number(), value, output);
  732. break;
  733. }
  734. case FieldDescriptor::TYPE_BYTES: {
  735. string scratch;
  736. const string& value = field->is_repeated() ?
  737. message_reflection->GetRepeatedStringReference(
  738. message, field, j, &scratch) :
  739. message_reflection->GetStringReference(message, field, &scratch);
  740. WireFormatLite::WriteBytes(field->number(), value, output);
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. void WireFormat::SerializeMessageSetItemWithCachedSizes(
  747. const FieldDescriptor* field,
  748. const Message& message,
  749. io::CodedOutputStream* output) {
  750. const Reflection* message_reflection = message.GetReflection();
  751. // Start group.
  752. output->WriteVarint32(WireFormatLite::kMessageSetItemStartTag);
  753. // Write type ID.
  754. output->WriteVarint32(WireFormatLite::kMessageSetTypeIdTag);
  755. output->WriteVarint32(field->number());
  756. // Write message.
  757. output->WriteVarint32(WireFormatLite::kMessageSetMessageTag);
  758. const Message& sub_message = message_reflection->GetMessage(message, field);
  759. output->WriteVarint32(sub_message.GetCachedSize());
  760. sub_message.SerializeWithCachedSizes(output);
  761. // End group.
  762. output->WriteVarint32(WireFormatLite::kMessageSetItemEndTag);
  763. }
  764. // ===================================================================
  765. int WireFormat::ByteSize(const Message& message) {
  766. const Descriptor* descriptor = message.GetDescriptor();
  767. const Reflection* message_reflection = message.GetReflection();
  768. int our_size = 0;
  769. vector<const FieldDescriptor*> fields;
  770. message_reflection->ListFields(message, &fields);
  771. for (int i = 0; i < fields.size(); i++) {
  772. our_size += FieldByteSize(fields[i], message);
  773. }
  774. if (descriptor->options().message_set_wire_format()) {
  775. our_size += ComputeUnknownMessageSetItemsSize(
  776. message_reflection->GetUnknownFields(message));
  777. } else {
  778. our_size += ComputeUnknownFieldsSize(
  779. message_reflection->GetUnknownFields(message));
  780. }
  781. return our_size;
  782. }
  783. int WireFormat::FieldByteSize(
  784. const FieldDescriptor* field,
  785. const Message& message) {
  786. const Reflection* message_reflection = message.GetReflection();
  787. if (field->is_extension() &&
  788. field->containing_type()->options().message_set_wire_format() &&
  789. field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  790. !field->is_repeated()) {
  791. return MessageSetItemByteSize(field, message);
  792. }
  793. int count = 0;
  794. if (field->is_repeated()) {
  795. count = message_reflection->FieldSize(message, field);
  796. } else if (message_reflection->HasField(message, field)) {
  797. count = 1;
  798. }
  799. const int data_size = FieldDataOnlyByteSize(field, message);
  800. int our_size = data_size;
  801. if (field->options().packed()) {
  802. if (data_size > 0) {
  803. // Packed fields get serialized like a string, not their native type.
  804. // Technically this doesn't really matter; the size only changes if it's
  805. // a GROUP
  806. our_size += TagSize(field->number(), FieldDescriptor::TYPE_STRING);
  807. our_size += io::CodedOutputStream::VarintSize32(data_size);
  808. }
  809. } else {
  810. our_size += count * TagSize(field->number(), field->type());
  811. }
  812. return our_size;
  813. }
  814. int WireFormat::FieldDataOnlyByteSize(
  815. const FieldDescriptor* field,
  816. const Message& message) {
  817. const Reflection* message_reflection = message.GetReflection();
  818. int count = 0;
  819. if (field->is_repeated()) {
  820. count = message_reflection->FieldSize(message, field);
  821. } else if (message_reflection->HasField(message, field)) {
  822. count = 1;
  823. }
  824. int data_size = 0;
  825. switch (field->type()) {
  826. #define HANDLE_TYPE(TYPE, TYPE_METHOD, CPPTYPE_METHOD) \
  827. case FieldDescriptor::TYPE_##TYPE: \
  828. if (field->is_repeated()) { \
  829. for (int j = 0; j < count; j++) { \
  830. data_size += WireFormatLite::TYPE_METHOD##Size( \
  831. message_reflection->GetRepeated##CPPTYPE_METHOD( \
  832. message, field, j)); \
  833. } \
  834. } else { \
  835. data_size += WireFormatLite::TYPE_METHOD##Size( \
  836. message_reflection->Get##CPPTYPE_METHOD(message, field)); \
  837. } \
  838. break;
  839. #define HANDLE_FIXED_TYPE(TYPE, TYPE_METHOD) \
  840. case FieldDescriptor::TYPE_##TYPE: \
  841. data_size += count * WireFormatLite::k##TYPE_METHOD##Size; \
  842. break;
  843. HANDLE_TYPE( INT32, Int32, Int32)
  844. HANDLE_TYPE( INT64, Int64, Int64)
  845. HANDLE_TYPE(SINT32, SInt32, Int32)
  846. HANDLE_TYPE(SINT64, SInt64, Int64)
  847. HANDLE_TYPE(UINT32, UInt32, UInt32)
  848. HANDLE_TYPE(UINT64, UInt64, UInt64)
  849. HANDLE_FIXED_TYPE( FIXED32, Fixed32)
  850. HANDLE_FIXED_TYPE( FIXED64, Fixed64)
  851. HANDLE_FIXED_TYPE(SFIXED32, SFixed32)
  852. HANDLE_FIXED_TYPE(SFIXED64, SFixed64)
  853. HANDLE_FIXED_TYPE(FLOAT , Float )
  854. HANDLE_FIXED_TYPE(DOUBLE, Double)
  855. HANDLE_FIXED_TYPE(BOOL, Bool)
  856. HANDLE_TYPE(GROUP , Group , Message)
  857. HANDLE_TYPE(MESSAGE, Message, Message)
  858. #undef HANDLE_TYPE
  859. #undef HANDLE_FIXED_TYPE
  860. case FieldDescriptor::TYPE_ENUM: {
  861. if (field->is_repeated()) {
  862. for (int j = 0; j < count; j++) {
  863. data_size += WireFormatLite::EnumSize(
  864. message_reflection->GetRepeatedEnum(message, field, j)->number());
  865. }
  866. } else {
  867. data_size += WireFormatLite::EnumSize(
  868. message_reflection->GetEnum(message, field)->number());
  869. }
  870. break;
  871. }
  872. // Handle strings separately so that we can get string references
  873. // instead of copying.
  874. case FieldDescriptor::TYPE_STRING:
  875. case FieldDescriptor::TYPE_BYTES: {
  876. for (int j = 0; j < count; j++) {
  877. string scratch;
  878. const string& value = field->is_repeated() ?
  879. message_reflection->GetRepeatedStringReference(
  880. message, field, j, &scratch) :
  881. message_reflection->GetStringReference(message, field, &scratch);
  882. data_size += WireFormatLite::StringSize(value);
  883. }
  884. break;
  885. }
  886. }
  887. return data_size;
  888. }
  889. int WireFormat::MessageSetItemByteSize(
  890. const FieldDescriptor* field,
  891. const Message& message) {
  892. const Reflection* message_reflection = message.GetReflection();
  893. int our_size = WireFormatLite::kMessageSetItemTagsSize;
  894. // type_id
  895. our_size += io::CodedOutputStream::VarintSize32(field->number());
  896. // message
  897. const Message& sub_message = message_reflection->GetMessage(message, field);
  898. int message_size = sub_message.ByteSize();
  899. our_size += io::CodedOutputStream::VarintSize32(message_size);
  900. our_size += message_size;
  901. return our_size;
  902. }
  903. void WireFormat::VerifyUTF8StringFallback(const char* data,
  904. int size,
  905. Operation op) {
  906. if (!IsStructurallyValidUTF8(data, size)) {
  907. const char* operation_str = NULL;
  908. switch (op) {
  909. case PARSE:
  910. operation_str = "parsing";
  911. break;
  912. case SERIALIZE:
  913. operation_str = "serializing";
  914. break;
  915. // no default case: have the compiler warn if a case is not covered.
  916. }
  917. GOOGLE_LOG(ERROR) << "Encountered string containing invalid UTF-8 data while "
  918. << operation_str
  919. << " protocol buffer. Strings must contain only UTF-8; "
  920. "use the 'bytes' type for raw bytes.";
  921. }
  922. }
  923. } // namespace internal
  924. } // namespace protobuf
  925. } // namespace google