/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/wire_format_lite_inl.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 774 lines · 658 code · 41 blank · 75 comment · 37 complexity · 73359376530a82b4a5b388640c829092 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. // wink@google.com (Wink Saville) (refactored from wire_format.h)
  32. // Based on original Protocol Buffers design by
  33. // Sanjay Ghemawat, Jeff Dean, and others.
  34. #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
  35. #define GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__
  36. #include <string>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/message_lite.h>
  39. #include <google/protobuf/repeated_field.h>
  40. #include <google/protobuf/wire_format_lite.h>
  41. #include <google/protobuf/generated_message_util.h>
  42. #include <google/protobuf/io/coded_stream.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace internal {
  46. // Implementation details of ReadPrimitive.
  47. template <>
  48. inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_INT32>(
  49. io::CodedInputStream* input,
  50. int32* value) {
  51. uint32 temp;
  52. if (!input->ReadVarint32(&temp)) return false;
  53. *value = static_cast<int32>(temp);
  54. return true;
  55. }
  56. template <>
  57. inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_INT64>(
  58. io::CodedInputStream* input,
  59. int64* value) {
  60. uint64 temp;
  61. if (!input->ReadVarint64(&temp)) return false;
  62. *value = static_cast<int64>(temp);
  63. return true;
  64. }
  65. template <>
  66. inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_UINT32>(
  67. io::CodedInputStream* input,
  68. uint32* value) {
  69. return input->ReadVarint32(value);
  70. }
  71. template <>
  72. inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_UINT64>(
  73. io::CodedInputStream* input,
  74. uint64* value) {
  75. return input->ReadVarint64(value);
  76. }
  77. template <>
  78. inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SINT32>(
  79. io::CodedInputStream* input,
  80. int32* value) {
  81. uint32 temp;
  82. if (!input->ReadVarint32(&temp)) return false;
  83. *value = ZigZagDecode32(temp);
  84. return true;
  85. }
  86. template <>
  87. inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SINT64>(
  88. io::CodedInputStream* input,
  89. int64* value) {
  90. uint64 temp;
  91. if (!input->ReadVarint64(&temp)) return false;
  92. *value = ZigZagDecode64(temp);
  93. return true;
  94. }
  95. template <>
  96. inline bool WireFormatLite::ReadPrimitive<uint32, WireFormatLite::TYPE_FIXED32>(
  97. io::CodedInputStream* input,
  98. uint32* value) {
  99. return input->ReadLittleEndian32(value);
  100. }
  101. template <>
  102. inline bool WireFormatLite::ReadPrimitive<uint64, WireFormatLite::TYPE_FIXED64>(
  103. io::CodedInputStream* input,
  104. uint64* value) {
  105. return input->ReadLittleEndian64(value);
  106. }
  107. template <>
  108. inline bool WireFormatLite::ReadPrimitive<int32, WireFormatLite::TYPE_SFIXED32>(
  109. io::CodedInputStream* input,
  110. int32* value) {
  111. uint32 temp;
  112. if (!input->ReadLittleEndian32(&temp)) return false;
  113. *value = static_cast<int32>(temp);
  114. return true;
  115. }
  116. template <>
  117. inline bool WireFormatLite::ReadPrimitive<int64, WireFormatLite::TYPE_SFIXED64>(
  118. io::CodedInputStream* input,
  119. int64* value) {
  120. uint64 temp;
  121. if (!input->ReadLittleEndian64(&temp)) return false;
  122. *value = static_cast<int64>(temp);
  123. return true;
  124. }
  125. template <>
  126. inline bool WireFormatLite::ReadPrimitive<float, WireFormatLite::TYPE_FLOAT>(
  127. io::CodedInputStream* input,
  128. float* value) {
  129. uint32 temp;
  130. if (!input->ReadLittleEndian32(&temp)) return false;
  131. *value = DecodeFloat(temp);
  132. return true;
  133. }
  134. template <>
  135. inline bool WireFormatLite::ReadPrimitive<double, WireFormatLite::TYPE_DOUBLE>(
  136. io::CodedInputStream* input,
  137. double* value) {
  138. uint64 temp;
  139. if (!input->ReadLittleEndian64(&temp)) return false;
  140. *value = DecodeDouble(temp);
  141. return true;
  142. }
  143. template <>
  144. inline bool WireFormatLite::ReadPrimitive<bool, WireFormatLite::TYPE_BOOL>(
  145. io::CodedInputStream* input,
  146. bool* value) {
  147. uint32 temp;
  148. if (!input->ReadVarint32(&temp)) return false;
  149. *value = temp != 0;
  150. return true;
  151. }
  152. template <>
  153. inline bool WireFormatLite::ReadPrimitive<int, WireFormatLite::TYPE_ENUM>(
  154. io::CodedInputStream* input,
  155. int* value) {
  156. uint32 temp;
  157. if (!input->ReadVarint32(&temp)) return false;
  158. *value = static_cast<int>(temp);
  159. return true;
  160. }
  161. template <>
  162. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  163. uint32, WireFormatLite::TYPE_FIXED32>(
  164. const uint8* buffer,
  165. uint32* value) {
  166. return io::CodedInputStream::ReadLittleEndian32FromArray(buffer, value);
  167. }
  168. template <>
  169. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  170. uint64, WireFormatLite::TYPE_FIXED64>(
  171. const uint8* buffer,
  172. uint64* value) {
  173. return io::CodedInputStream::ReadLittleEndian64FromArray(buffer, value);
  174. }
  175. template <>
  176. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  177. int32, WireFormatLite::TYPE_SFIXED32>(
  178. const uint8* buffer,
  179. int32* value) {
  180. uint32 temp;
  181. buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
  182. *value = static_cast<int32>(temp);
  183. return buffer;
  184. }
  185. template <>
  186. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  187. int64, WireFormatLite::TYPE_SFIXED64>(
  188. const uint8* buffer,
  189. int64* value) {
  190. uint64 temp;
  191. buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
  192. *value = static_cast<int64>(temp);
  193. return buffer;
  194. }
  195. template <>
  196. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  197. float, WireFormatLite::TYPE_FLOAT>(
  198. const uint8* buffer,
  199. float* value) {
  200. uint32 temp;
  201. buffer = io::CodedInputStream::ReadLittleEndian32FromArray(buffer, &temp);
  202. *value = DecodeFloat(temp);
  203. return buffer;
  204. }
  205. template <>
  206. inline const uint8* WireFormatLite::ReadPrimitiveFromArray<
  207. double, WireFormatLite::TYPE_DOUBLE>(
  208. const uint8* buffer,
  209. double* value) {
  210. uint64 temp;
  211. buffer = io::CodedInputStream::ReadLittleEndian64FromArray(buffer, &temp);
  212. *value = DecodeDouble(temp);
  213. return buffer;
  214. }
  215. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  216. inline bool WireFormatLite::ReadRepeatedPrimitive(int, // tag_size, unused.
  217. uint32 tag,
  218. io::CodedInputStream* input,
  219. RepeatedField<CType>* values) {
  220. CType value;
  221. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  222. values->Add(value);
  223. int elements_already_reserved = values->Capacity() - values->size();
  224. while (elements_already_reserved > 0 && input->ExpectTag(tag)) {
  225. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  226. values->AddAlreadyReserved(value);
  227. elements_already_reserved--;
  228. }
  229. return true;
  230. }
  231. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  232. inline bool WireFormatLite::ReadRepeatedFixedSizePrimitive(
  233. int tag_size,
  234. uint32 tag,
  235. io::CodedInputStream* input,
  236. RepeatedField<CType>* values) {
  237. GOOGLE_DCHECK_EQ(UInt32Size(tag), tag_size);
  238. CType value;
  239. if (!ReadPrimitive<CType, DeclaredType>(input, &value))
  240. return false;
  241. values->Add(value);
  242. // For fixed size values, repeated values can be read more quickly by
  243. // reading directly from a raw array.
  244. //
  245. // We can get a tight loop by only reading as many elements as can be
  246. // added to the RepeatedField without having to do any resizing. Additionally,
  247. // we only try to read as many elements as are available from the current
  248. // buffer space. Doing so avoids having to perform boundary checks when
  249. // reading the value: the maximum number of elements that can be read is
  250. // known outside of the loop.
  251. const void* void_pointer;
  252. int size;
  253. input->GetDirectBufferPointerInline(&void_pointer, &size);
  254. if (size > 0) {
  255. const uint8* buffer = reinterpret_cast<const uint8*>(void_pointer);
  256. // The number of bytes each type occupies on the wire.
  257. const int per_value_size = tag_size + sizeof(value);
  258. int elements_available = min(values->Capacity() - values->size(),
  259. size / per_value_size);
  260. int num_read = 0;
  261. while (num_read < elements_available &&
  262. (buffer = io::CodedInputStream::ExpectTagFromArray(
  263. buffer, tag)) != NULL) {
  264. buffer = ReadPrimitiveFromArray<CType, DeclaredType>(buffer, &value);
  265. values->AddAlreadyReserved(value);
  266. ++num_read;
  267. }
  268. const int read_bytes = num_read * per_value_size;
  269. if (read_bytes > 0) {
  270. input->Skip(read_bytes);
  271. }
  272. }
  273. return true;
  274. }
  275. // Specializations of ReadRepeatedPrimitive for the fixed size types, which use
  276. // the optimized code path.
  277. #define READ_REPEATED_FIXED_SIZE_PRIMITIVE(CPPTYPE, DECLARED_TYPE) \
  278. template <> \
  279. inline bool WireFormatLite::ReadRepeatedPrimitive< \
  280. CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
  281. int tag_size, \
  282. uint32 tag, \
  283. io::CodedInputStream* input, \
  284. RepeatedField<CPPTYPE>* values) { \
  285. return ReadRepeatedFixedSizePrimitive< \
  286. CPPTYPE, WireFormatLite::DECLARED_TYPE>( \
  287. tag_size, tag, input, values); \
  288. }
  289. READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint32, TYPE_FIXED32);
  290. READ_REPEATED_FIXED_SIZE_PRIMITIVE(uint64, TYPE_FIXED64);
  291. READ_REPEATED_FIXED_SIZE_PRIMITIVE(int32, TYPE_SFIXED32);
  292. READ_REPEATED_FIXED_SIZE_PRIMITIVE(int64, TYPE_SFIXED64);
  293. READ_REPEATED_FIXED_SIZE_PRIMITIVE(float, TYPE_FLOAT);
  294. READ_REPEATED_FIXED_SIZE_PRIMITIVE(double, TYPE_DOUBLE);
  295. #undef READ_REPEATED_FIXED_SIZE_PRIMITIVE
  296. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  297. bool WireFormatLite::ReadRepeatedPrimitiveNoInline(
  298. int tag_size,
  299. uint32 tag,
  300. io::CodedInputStream* input,
  301. RepeatedField<CType>* value) {
  302. return ReadRepeatedPrimitive<CType, DeclaredType>(
  303. tag_size, tag, input, value);
  304. }
  305. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  306. inline bool WireFormatLite::ReadPackedPrimitive(io::CodedInputStream* input,
  307. RepeatedField<CType>* values) {
  308. uint32 length;
  309. if (!input->ReadVarint32(&length)) return false;
  310. io::CodedInputStream::Limit limit = input->PushLimit(length);
  311. while (input->BytesUntilLimit() > 0) {
  312. CType value;
  313. if (!ReadPrimitive<CType, DeclaredType>(input, &value)) return false;
  314. values->Add(value);
  315. }
  316. input->PopLimit(limit);
  317. return true;
  318. }
  319. template <typename CType, enum WireFormatLite::FieldType DeclaredType>
  320. bool WireFormatLite::ReadPackedPrimitiveNoInline(io::CodedInputStream* input,
  321. RepeatedField<CType>* values) {
  322. return ReadPackedPrimitive<CType, DeclaredType>(input, values);
  323. }
  324. inline bool WireFormatLite::ReadGroup(int field_number,
  325. io::CodedInputStream* input,
  326. MessageLite* value) {
  327. if (!input->IncrementRecursionDepth()) return false;
  328. if (!value->MergePartialFromCodedStream(input)) return false;
  329. input->DecrementRecursionDepth();
  330. // Make sure the last thing read was an end tag for this group.
  331. if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
  332. return false;
  333. }
  334. return true;
  335. }
  336. inline bool WireFormatLite::ReadMessage(io::CodedInputStream* input,
  337. MessageLite* value) {
  338. uint32 length;
  339. if (!input->ReadVarint32(&length)) return false;
  340. if (!input->IncrementRecursionDepth()) return false;
  341. io::CodedInputStream::Limit limit = input->PushLimit(length);
  342. if (!value->MergePartialFromCodedStream(input)) return false;
  343. // Make sure that parsing stopped when the limit was hit, not at an endgroup
  344. // tag.
  345. if (!input->ConsumedEntireMessage()) return false;
  346. input->PopLimit(limit);
  347. input->DecrementRecursionDepth();
  348. return true;
  349. }
  350. // We name the template parameter something long and extremely unlikely to occur
  351. // elsewhere because a *qualified* member access expression designed to avoid
  352. // virtual dispatch, C++03 [basic.lookup.classref] 3.4.5/4 requires that the
  353. // name of the qualifying class to be looked up both in the context of the full
  354. // expression (finding the template parameter) and in the context of the object
  355. // whose member we are accessing. This could potentially find a nested type
  356. // within that object. The standard goes on to require these names to refer to
  357. // the same entity, which this collision would violate. The lack of a safe way
  358. // to avoid this collision appears to be a defect in the standard, but until it
  359. // is corrected, we choose the name to avoid accidental collisions.
  360. template<typename MessageType_WorkAroundCppLookupDefect>
  361. inline bool WireFormatLite::ReadGroupNoVirtual(
  362. int field_number, io::CodedInputStream* input,
  363. MessageType_WorkAroundCppLookupDefect* value) {
  364. if (!input->IncrementRecursionDepth()) return false;
  365. if (!value->
  366. MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
  367. return false;
  368. input->DecrementRecursionDepth();
  369. // Make sure the last thing read was an end tag for this group.
  370. if (!input->LastTagWas(MakeTag(field_number, WIRETYPE_END_GROUP))) {
  371. return false;
  372. }
  373. return true;
  374. }
  375. template<typename MessageType_WorkAroundCppLookupDefect>
  376. inline bool WireFormatLite::ReadMessageNoVirtual(
  377. io::CodedInputStream* input, MessageType_WorkAroundCppLookupDefect* value) {
  378. uint32 length;
  379. if (!input->ReadVarint32(&length)) return false;
  380. if (!input->IncrementRecursionDepth()) return false;
  381. io::CodedInputStream::Limit limit = input->PushLimit(length);
  382. if (!value->
  383. MessageType_WorkAroundCppLookupDefect::MergePartialFromCodedStream(input))
  384. return false;
  385. // Make sure that parsing stopped when the limit was hit, not at an endgroup
  386. // tag.
  387. if (!input->ConsumedEntireMessage()) return false;
  388. input->PopLimit(limit);
  389. input->DecrementRecursionDepth();
  390. return true;
  391. }
  392. // ===================================================================
  393. inline void WireFormatLite::WriteTag(int field_number, WireType type,
  394. io::CodedOutputStream* output) {
  395. output->WriteTag(MakeTag(field_number, type));
  396. }
  397. inline void WireFormatLite::WriteInt32NoTag(int32 value,
  398. io::CodedOutputStream* output) {
  399. output->WriteVarint32SignExtended(value);
  400. }
  401. inline void WireFormatLite::WriteInt64NoTag(int64 value,
  402. io::CodedOutputStream* output) {
  403. output->WriteVarint64(static_cast<uint64>(value));
  404. }
  405. inline void WireFormatLite::WriteUInt32NoTag(uint32 value,
  406. io::CodedOutputStream* output) {
  407. output->WriteVarint32(value);
  408. }
  409. inline void WireFormatLite::WriteUInt64NoTag(uint64 value,
  410. io::CodedOutputStream* output) {
  411. output->WriteVarint64(value);
  412. }
  413. inline void WireFormatLite::WriteSInt32NoTag(int32 value,
  414. io::CodedOutputStream* output) {
  415. output->WriteVarint32(ZigZagEncode32(value));
  416. }
  417. inline void WireFormatLite::WriteSInt64NoTag(int64 value,
  418. io::CodedOutputStream* output) {
  419. output->WriteVarint64(ZigZagEncode64(value));
  420. }
  421. inline void WireFormatLite::WriteFixed32NoTag(uint32 value,
  422. io::CodedOutputStream* output) {
  423. output->WriteLittleEndian32(value);
  424. }
  425. inline void WireFormatLite::WriteFixed64NoTag(uint64 value,
  426. io::CodedOutputStream* output) {
  427. output->WriteLittleEndian64(value);
  428. }
  429. inline void WireFormatLite::WriteSFixed32NoTag(int32 value,
  430. io::CodedOutputStream* output) {
  431. output->WriteLittleEndian32(static_cast<uint32>(value));
  432. }
  433. inline void WireFormatLite::WriteSFixed64NoTag(int64 value,
  434. io::CodedOutputStream* output) {
  435. output->WriteLittleEndian64(static_cast<uint64>(value));
  436. }
  437. inline void WireFormatLite::WriteFloatNoTag(float value,
  438. io::CodedOutputStream* output) {
  439. output->WriteLittleEndian32(EncodeFloat(value));
  440. }
  441. inline void WireFormatLite::WriteDoubleNoTag(double value,
  442. io::CodedOutputStream* output) {
  443. output->WriteLittleEndian64(EncodeDouble(value));
  444. }
  445. inline void WireFormatLite::WriteBoolNoTag(bool value,
  446. io::CodedOutputStream* output) {
  447. output->WriteVarint32(value ? 1 : 0);
  448. }
  449. inline void WireFormatLite::WriteEnumNoTag(int value,
  450. io::CodedOutputStream* output) {
  451. output->WriteVarint32SignExtended(value);
  452. }
  453. // See comment on ReadGroupNoVirtual to understand the need for this template
  454. // parameter name.
  455. template<typename MessageType_WorkAroundCppLookupDefect>
  456. inline void WireFormatLite::WriteGroupNoVirtual(
  457. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  458. io::CodedOutputStream* output) {
  459. WriteTag(field_number, WIRETYPE_START_GROUP, output);
  460. value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
  461. WriteTag(field_number, WIRETYPE_END_GROUP, output);
  462. }
  463. template<typename MessageType_WorkAroundCppLookupDefect>
  464. inline void WireFormatLite::WriteMessageNoVirtual(
  465. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  466. io::CodedOutputStream* output) {
  467. WriteTag(field_number, WIRETYPE_LENGTH_DELIMITED, output);
  468. output->WriteVarint32(
  469. value.MessageType_WorkAroundCppLookupDefect::GetCachedSize());
  470. value.MessageType_WorkAroundCppLookupDefect::SerializeWithCachedSizes(output);
  471. }
  472. // ===================================================================
  473. inline uint8* WireFormatLite::WriteTagToArray(int field_number,
  474. WireType type,
  475. uint8* target) {
  476. return io::CodedOutputStream::WriteTagToArray(MakeTag(field_number, type),
  477. target);
  478. }
  479. inline uint8* WireFormatLite::WriteInt32NoTagToArray(int32 value,
  480. uint8* target) {
  481. return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
  482. }
  483. inline uint8* WireFormatLite::WriteInt64NoTagToArray(int64 value,
  484. uint8* target) {
  485. return io::CodedOutputStream::WriteVarint64ToArray(
  486. static_cast<uint64>(value), target);
  487. }
  488. inline uint8* WireFormatLite::WriteUInt32NoTagToArray(uint32 value,
  489. uint8* target) {
  490. return io::CodedOutputStream::WriteVarint32ToArray(value, target);
  491. }
  492. inline uint8* WireFormatLite::WriteUInt64NoTagToArray(uint64 value,
  493. uint8* target) {
  494. return io::CodedOutputStream::WriteVarint64ToArray(value, target);
  495. }
  496. inline uint8* WireFormatLite::WriteSInt32NoTagToArray(int32 value,
  497. uint8* target) {
  498. return io::CodedOutputStream::WriteVarint32ToArray(ZigZagEncode32(value),
  499. target);
  500. }
  501. inline uint8* WireFormatLite::WriteSInt64NoTagToArray(int64 value,
  502. uint8* target) {
  503. return io::CodedOutputStream::WriteVarint64ToArray(ZigZagEncode64(value),
  504. target);
  505. }
  506. inline uint8* WireFormatLite::WriteFixed32NoTagToArray(uint32 value,
  507. uint8* target) {
  508. return io::CodedOutputStream::WriteLittleEndian32ToArray(value, target);
  509. }
  510. inline uint8* WireFormatLite::WriteFixed64NoTagToArray(uint64 value,
  511. uint8* target) {
  512. return io::CodedOutputStream::WriteLittleEndian64ToArray(value, target);
  513. }
  514. inline uint8* WireFormatLite::WriteSFixed32NoTagToArray(int32 value,
  515. uint8* target) {
  516. return io::CodedOutputStream::WriteLittleEndian32ToArray(
  517. static_cast<uint32>(value), target);
  518. }
  519. inline uint8* WireFormatLite::WriteSFixed64NoTagToArray(int64 value,
  520. uint8* target) {
  521. return io::CodedOutputStream::WriteLittleEndian64ToArray(
  522. static_cast<uint64>(value), target);
  523. }
  524. inline uint8* WireFormatLite::WriteFloatNoTagToArray(float value,
  525. uint8* target) {
  526. return io::CodedOutputStream::WriteLittleEndian32ToArray(EncodeFloat(value),
  527. target);
  528. }
  529. inline uint8* WireFormatLite::WriteDoubleNoTagToArray(double value,
  530. uint8* target) {
  531. return io::CodedOutputStream::WriteLittleEndian64ToArray(EncodeDouble(value),
  532. target);
  533. }
  534. inline uint8* WireFormatLite::WriteBoolNoTagToArray(bool value,
  535. uint8* target) {
  536. return io::CodedOutputStream::WriteVarint32ToArray(value ? 1 : 0, target);
  537. }
  538. inline uint8* WireFormatLite::WriteEnumNoTagToArray(int value,
  539. uint8* target) {
  540. return io::CodedOutputStream::WriteVarint32SignExtendedToArray(value, target);
  541. }
  542. inline uint8* WireFormatLite::WriteInt32ToArray(int field_number,
  543. int32 value,
  544. uint8* target) {
  545. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  546. return WriteInt32NoTagToArray(value, target);
  547. }
  548. inline uint8* WireFormatLite::WriteInt64ToArray(int field_number,
  549. int64 value,
  550. uint8* target) {
  551. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  552. return WriteInt64NoTagToArray(value, target);
  553. }
  554. inline uint8* WireFormatLite::WriteUInt32ToArray(int field_number,
  555. uint32 value,
  556. uint8* target) {
  557. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  558. return WriteUInt32NoTagToArray(value, target);
  559. }
  560. inline uint8* WireFormatLite::WriteUInt64ToArray(int field_number,
  561. uint64 value,
  562. uint8* target) {
  563. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  564. return WriteUInt64NoTagToArray(value, target);
  565. }
  566. inline uint8* WireFormatLite::WriteSInt32ToArray(int field_number,
  567. int32 value,
  568. uint8* target) {
  569. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  570. return WriteSInt32NoTagToArray(value, target);
  571. }
  572. inline uint8* WireFormatLite::WriteSInt64ToArray(int field_number,
  573. int64 value,
  574. uint8* target) {
  575. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  576. return WriteSInt64NoTagToArray(value, target);
  577. }
  578. inline uint8* WireFormatLite::WriteFixed32ToArray(int field_number,
  579. uint32 value,
  580. uint8* target) {
  581. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  582. return WriteFixed32NoTagToArray(value, target);
  583. }
  584. inline uint8* WireFormatLite::WriteFixed64ToArray(int field_number,
  585. uint64 value,
  586. uint8* target) {
  587. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  588. return WriteFixed64NoTagToArray(value, target);
  589. }
  590. inline uint8* WireFormatLite::WriteSFixed32ToArray(int field_number,
  591. int32 value,
  592. uint8* target) {
  593. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  594. return WriteSFixed32NoTagToArray(value, target);
  595. }
  596. inline uint8* WireFormatLite::WriteSFixed64ToArray(int field_number,
  597. int64 value,
  598. uint8* target) {
  599. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  600. return WriteSFixed64NoTagToArray(value, target);
  601. }
  602. inline uint8* WireFormatLite::WriteFloatToArray(int field_number,
  603. float value,
  604. uint8* target) {
  605. target = WriteTagToArray(field_number, WIRETYPE_FIXED32, target);
  606. return WriteFloatNoTagToArray(value, target);
  607. }
  608. inline uint8* WireFormatLite::WriteDoubleToArray(int field_number,
  609. double value,
  610. uint8* target) {
  611. target = WriteTagToArray(field_number, WIRETYPE_FIXED64, target);
  612. return WriteDoubleNoTagToArray(value, target);
  613. }
  614. inline uint8* WireFormatLite::WriteBoolToArray(int field_number,
  615. bool value,
  616. uint8* target) {
  617. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  618. return WriteBoolNoTagToArray(value, target);
  619. }
  620. inline uint8* WireFormatLite::WriteEnumToArray(int field_number,
  621. int value,
  622. uint8* target) {
  623. target = WriteTagToArray(field_number, WIRETYPE_VARINT, target);
  624. return WriteEnumNoTagToArray(value, target);
  625. }
  626. inline uint8* WireFormatLite::WriteStringToArray(int field_number,
  627. const string& value,
  628. uint8* target) {
  629. // String is for UTF-8 text only
  630. // WARNING: In wire_format.cc, both strings and bytes are handled by
  631. // WriteString() to avoid code duplication. If the implementations become
  632. // different, you will need to update that usage.
  633. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  634. target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
  635. return io::CodedOutputStream::WriteStringToArray(value, target);
  636. }
  637. inline uint8* WireFormatLite::WriteBytesToArray(int field_number,
  638. const string& value,
  639. uint8* target) {
  640. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  641. target = io::CodedOutputStream::WriteVarint32ToArray(value.size(), target);
  642. return io::CodedOutputStream::WriteStringToArray(value, target);
  643. }
  644. inline uint8* WireFormatLite::WriteGroupToArray(int field_number,
  645. const MessageLite& value,
  646. uint8* target) {
  647. target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
  648. target = value.SerializeWithCachedSizesToArray(target);
  649. return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
  650. }
  651. inline uint8* WireFormatLite::WriteMessageToArray(int field_number,
  652. const MessageLite& value,
  653. uint8* target) {
  654. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  655. target = io::CodedOutputStream::WriteVarint32ToArray(
  656. value.GetCachedSize(), target);
  657. return value.SerializeWithCachedSizesToArray(target);
  658. }
  659. // See comment on ReadGroupNoVirtual to understand the need for this template
  660. // parameter name.
  661. template<typename MessageType_WorkAroundCppLookupDefect>
  662. inline uint8* WireFormatLite::WriteGroupNoVirtualToArray(
  663. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  664. uint8* target) {
  665. target = WriteTagToArray(field_number, WIRETYPE_START_GROUP, target);
  666. target = value.MessageType_WorkAroundCppLookupDefect
  667. ::SerializeWithCachedSizesToArray(target);
  668. return WriteTagToArray(field_number, WIRETYPE_END_GROUP, target);
  669. }
  670. template<typename MessageType_WorkAroundCppLookupDefect>
  671. inline uint8* WireFormatLite::WriteMessageNoVirtualToArray(
  672. int field_number, const MessageType_WorkAroundCppLookupDefect& value,
  673. uint8* target) {
  674. target = WriteTagToArray(field_number, WIRETYPE_LENGTH_DELIMITED, target);
  675. target = io::CodedOutputStream::WriteVarint32ToArray(
  676. value.MessageType_WorkAroundCppLookupDefect::GetCachedSize(), target);
  677. return value.MessageType_WorkAroundCppLookupDefect
  678. ::SerializeWithCachedSizesToArray(target);
  679. }
  680. // ===================================================================
  681. inline int WireFormatLite::Int32Size(int32 value) {
  682. return io::CodedOutputStream::VarintSize32SignExtended(value);
  683. }
  684. inline int WireFormatLite::Int64Size(int64 value) {
  685. return io::CodedOutputStream::VarintSize64(static_cast<uint64>(value));
  686. }
  687. inline int WireFormatLite::UInt32Size(uint32 value) {
  688. return io::CodedOutputStream::VarintSize32(value);
  689. }
  690. inline int WireFormatLite::UInt64Size(uint64 value) {
  691. return io::CodedOutputStream::VarintSize64(value);
  692. }
  693. inline int WireFormatLite::SInt32Size(int32 value) {
  694. return io::CodedOutputStream::VarintSize32(ZigZagEncode32(value));
  695. }
  696. inline int WireFormatLite::SInt64Size(int64 value) {
  697. return io::CodedOutputStream::VarintSize64(ZigZagEncode64(value));
  698. }
  699. inline int WireFormatLite::EnumSize(int value) {
  700. return io::CodedOutputStream::VarintSize32SignExtended(value);
  701. }
  702. inline int WireFormatLite::StringSize(const string& value) {
  703. return io::CodedOutputStream::VarintSize32(value.size()) +
  704. value.size();
  705. }
  706. inline int WireFormatLite::BytesSize(const string& value) {
  707. return io::CodedOutputStream::VarintSize32(value.size()) +
  708. value.size();
  709. }
  710. inline int WireFormatLite::GroupSize(const MessageLite& value) {
  711. return value.ByteSize();
  712. }
  713. inline int WireFormatLite::MessageSize(const MessageLite& value) {
  714. int size = value.ByteSize();
  715. return io::CodedOutputStream::VarintSize32(size) + size;
  716. }
  717. // See comment on ReadGroupNoVirtual to understand the need for this template
  718. // parameter name.
  719. template<typename MessageType_WorkAroundCppLookupDefect>
  720. inline int WireFormatLite::GroupSizeNoVirtual(
  721. const MessageType_WorkAroundCppLookupDefect& value) {
  722. return value.MessageType_WorkAroundCppLookupDefect::ByteSize();
  723. }
  724. template<typename MessageType_WorkAroundCppLookupDefect>
  725. inline int WireFormatLite::MessageSizeNoVirtual(
  726. const MessageType_WorkAroundCppLookupDefect& value) {
  727. int size = value.MessageType_WorkAroundCppLookupDefect::ByteSize();
  728. return io::CodedOutputStream::VarintSize32(size) + size;
  729. }
  730. } // namespace internal
  731. } // namespace protobuf
  732. } // namespace google
  733. #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_LITE_INL_H__