message_lite.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. // Authors: wink@google.com (Wink Saville),
  31. // kenton@google.com (Kenton Varda)
  32. // Based on original Protocol Buffers design by
  33. // Sanjay Ghemawat, Jeff Dean, and others.
  34. #include <climits>
  35. #include <google/protobuf/arena.h>
  36. #include <google/protobuf/generated_message_util.h>
  37. #include <google/protobuf/message_lite.h>
  38. #include <google/protobuf/repeated_field.h>
  39. #include <string>
  40. #include <google/protobuf/stubs/logging.h>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/io/coded_stream.h>
  43. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  44. #include <google/protobuf/stubs/stl_util.h>
  45. namespace google {
  46. namespace protobuf {
  47. string MessageLite::InitializationErrorString() const {
  48. return "(cannot determine missing fields for lite message)";
  49. }
  50. namespace {
  51. // When serializing, we first compute the byte size, then serialize the message.
  52. // If serialization produces a different number of bytes than expected, we
  53. // call this function, which crashes. The problem could be due to a bug in the
  54. // protobuf implementation but is more likely caused by concurrent modification
  55. // of the message. This function attempts to distinguish between the two and
  56. // provide a useful error message.
  57. void ByteSizeConsistencyError(size_t byte_size_before_serialization,
  58. size_t byte_size_after_serialization,
  59. size_t bytes_produced_by_serialization,
  60. const MessageLite& message) {
  61. GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization)
  62. << message.GetTypeName()
  63. << " was modified concurrently during serialization.";
  64. GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization)
  65. << "Byte size calculation and serialization were inconsistent. This "
  66. "may indicate a bug in protocol buffers or it may be caused by "
  67. "concurrent modification of " << message.GetTypeName() << ".";
  68. GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal.";
  69. }
  70. string InitializationErrorMessage(const char* action,
  71. const MessageLite& message) {
  72. // Note: We want to avoid depending on strutil in the lite library, otherwise
  73. // we'd use:
  74. //
  75. // return strings::Substitute(
  76. // "Can't $0 message of type \"$1\" because it is missing required "
  77. // "fields: $2",
  78. // action, message.GetTypeName(),
  79. // message.InitializationErrorString());
  80. string result;
  81. result += "Can't ";
  82. result += action;
  83. result += " message of type \"";
  84. result += message.GetTypeName();
  85. result += "\" because it is missing required fields: ";
  86. result += message.InitializationErrorString();
  87. return result;
  88. }
  89. // Several of the Parse methods below just do one thing and then call another
  90. // method. In a naive implementation, we might have ParseFromString() call
  91. // ParseFromArray() which would call ParseFromZeroCopyStream() which would call
  92. // ParseFromCodedStream() which would call MergeFromCodedStream() which would
  93. // call MergePartialFromCodedStream(). However, when parsing very small
  94. // messages, every function call introduces significant overhead. To avoid
  95. // this without reproducing code, we use these forced-inline helpers.
  96. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool InlineMergeFromCodedStream(
  97. io::CodedInputStream* input, MessageLite* message);
  98. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool InlineParseFromCodedStream(
  99. io::CodedInputStream* input, MessageLite* message);
  100. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool InlineParsePartialFromCodedStream(
  101. io::CodedInputStream* input, MessageLite* message);
  102. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool InlineParseFromArray(
  103. const void* data, int size, MessageLite* message);
  104. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE bool InlineParsePartialFromArray(
  105. const void* data, int size, MessageLite* message);
  106. inline bool InlineMergeFromCodedStream(io::CodedInputStream* input,
  107. MessageLite* message) {
  108. if (!message->MergePartialFromCodedStream(input)) return false;
  109. if (!message->IsInitialized()) {
  110. GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *message);
  111. return false;
  112. }
  113. return true;
  114. }
  115. inline bool InlineParseFromCodedStream(io::CodedInputStream* input,
  116. MessageLite* message) {
  117. message->Clear();
  118. return InlineMergeFromCodedStream(input, message);
  119. }
  120. inline bool InlineParsePartialFromCodedStream(io::CodedInputStream* input,
  121. MessageLite* message) {
  122. message->Clear();
  123. return message->MergePartialFromCodedStream(input);
  124. }
  125. inline bool InlineParseFromArray(
  126. const void* data, int size, MessageLite* message) {
  127. io::CodedInputStream input(reinterpret_cast<const uint8*>(data), size);
  128. return InlineParseFromCodedStream(&input, message) &&
  129. input.ConsumedEntireMessage();
  130. }
  131. inline bool InlineParsePartialFromArray(
  132. const void* data, int size, MessageLite* message) {
  133. io::CodedInputStream input(reinterpret_cast<const uint8*>(data), size);
  134. return InlineParsePartialFromCodedStream(&input, message) &&
  135. input.ConsumedEntireMessage();
  136. }
  137. } // namespace
  138. MessageLite* MessageLite::New(::google::protobuf::Arena* arena) const {
  139. MessageLite* message = New();
  140. if (arena != NULL) {
  141. arena->Own(message);
  142. }
  143. return message;
  144. }
  145. bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) {
  146. return InlineMergeFromCodedStream(input, this);
  147. }
  148. bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) {
  149. return InlineParseFromCodedStream(input, this);
  150. }
  151. bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) {
  152. return InlineParsePartialFromCodedStream(input, this);
  153. }
  154. bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
  155. io::CodedInputStream decoder(input);
  156. return ParseFromCodedStream(&decoder) && decoder.ConsumedEntireMessage();
  157. }
  158. bool MessageLite::ParsePartialFromZeroCopyStream(
  159. io::ZeroCopyInputStream* input) {
  160. io::CodedInputStream decoder(input);
  161. return ParsePartialFromCodedStream(&decoder) &&
  162. decoder.ConsumedEntireMessage();
  163. }
  164. bool MessageLite::ParseFromBoundedZeroCopyStream(
  165. io::ZeroCopyInputStream* input, int size) {
  166. io::CodedInputStream decoder(input);
  167. decoder.PushLimit(size);
  168. return ParseFromCodedStream(&decoder) &&
  169. decoder.ConsumedEntireMessage() &&
  170. decoder.BytesUntilLimit() == 0;
  171. }
  172. bool MessageLite::ParsePartialFromBoundedZeroCopyStream(
  173. io::ZeroCopyInputStream* input, int size) {
  174. io::CodedInputStream decoder(input);
  175. decoder.PushLimit(size);
  176. return ParsePartialFromCodedStream(&decoder) &&
  177. decoder.ConsumedEntireMessage() &&
  178. decoder.BytesUntilLimit() == 0;
  179. }
  180. bool MessageLite::ParseFromString(const string& data) {
  181. return InlineParseFromArray(data.data(), data.size(), this);
  182. }
  183. bool MessageLite::ParsePartialFromString(const string& data) {
  184. return InlineParsePartialFromArray(data.data(), data.size(), this);
  185. }
  186. bool MessageLite::ParseFromArray(const void* data, int size) {
  187. return InlineParseFromArray(data, size, this);
  188. }
  189. bool MessageLite::ParsePartialFromArray(const void* data, int size) {
  190. return InlineParsePartialFromArray(data, size, this);
  191. }
  192. // ===================================================================
  193. uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const {
  194. return InternalSerializeWithCachedSizesToArray(
  195. io::CodedOutputStream::IsDefaultSerializationDeterministic(), target);
  196. }
  197. bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
  198. GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
  199. return SerializePartialToCodedStream(output);
  200. }
  201. bool MessageLite::SerializePartialToCodedStream(
  202. io::CodedOutputStream* output) const {
  203. const size_t size = ByteSizeLong(); // Force size to be cached.
  204. if (size > INT_MAX) {
  205. GOOGLE_LOG(ERROR) << "Exceeded maximum protobuf size of 2GB: " << size;
  206. return false;
  207. }
  208. uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
  209. if (buffer != NULL) {
  210. uint8* end = InternalSerializeWithCachedSizesToArray(
  211. output->IsSerializationDeterministic(), buffer);
  212. if (end - buffer != size) {
  213. ByteSizeConsistencyError(size, ByteSizeLong(), end - buffer, *this);
  214. }
  215. return true;
  216. } else {
  217. int original_byte_count = output->ByteCount();
  218. SerializeWithCachedSizes(output);
  219. if (output->HadError()) {
  220. return false;
  221. }
  222. int final_byte_count = output->ByteCount();
  223. if (final_byte_count - original_byte_count != size) {
  224. ByteSizeConsistencyError(size, ByteSizeLong(),
  225. final_byte_count - original_byte_count, *this);
  226. }
  227. return true;
  228. }
  229. }
  230. bool MessageLite::SerializeToZeroCopyStream(
  231. io::ZeroCopyOutputStream* output) const {
  232. io::CodedOutputStream encoder(output);
  233. return SerializeToCodedStream(&encoder);
  234. }
  235. bool MessageLite::SerializePartialToZeroCopyStream(
  236. io::ZeroCopyOutputStream* output) const {
  237. io::CodedOutputStream encoder(output);
  238. return SerializePartialToCodedStream(&encoder);
  239. }
  240. bool MessageLite::AppendToString(string* output) const {
  241. GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
  242. return AppendPartialToString(output);
  243. }
  244. bool MessageLite::AppendPartialToString(string* output) const {
  245. size_t old_size = output->size();
  246. size_t byte_size = ByteSizeLong();
  247. if (byte_size > INT_MAX) {
  248. GOOGLE_LOG(ERROR) << "Exceeded maximum protobuf size of 2GB: " << byte_size;
  249. return false;
  250. }
  251. STLStringResizeUninitialized(output, old_size + byte_size);
  252. uint8* start =
  253. reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size);
  254. uint8* end = SerializeWithCachedSizesToArray(start);
  255. if (end - start != byte_size) {
  256. ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
  257. }
  258. return true;
  259. }
  260. bool MessageLite::SerializeToString(string* output) const {
  261. output->clear();
  262. return AppendToString(output);
  263. }
  264. bool MessageLite::SerializePartialToString(string* output) const {
  265. output->clear();
  266. return AppendPartialToString(output);
  267. }
  268. bool MessageLite::SerializeToArray(void* data, int size) const {
  269. GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
  270. return SerializePartialToArray(data, size);
  271. }
  272. bool MessageLite::SerializePartialToArray(void* data, int size) const {
  273. int byte_size = ByteSizeLong();
  274. if (size < byte_size) return false;
  275. uint8* start = reinterpret_cast<uint8*>(data);
  276. uint8* end = SerializeWithCachedSizesToArray(start);
  277. if (end - start != byte_size) {
  278. ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
  279. }
  280. return true;
  281. }
  282. string MessageLite::SerializeAsString() const {
  283. // If the compiler implements the (Named) Return Value Optimization,
  284. // the local variable 'output' will not actually reside on the stack
  285. // of this function, but will be overlaid with the object that the
  286. // caller supplied for the return value to be constructed in.
  287. string output;
  288. if (!AppendToString(&output))
  289. output.clear();
  290. return output;
  291. }
  292. string MessageLite::SerializePartialAsString() const {
  293. string output;
  294. if (!AppendPartialToString(&output))
  295. output.clear();
  296. return output;
  297. }
  298. void MessageLite::SerializeWithCachedSizes(
  299. io::CodedOutputStream* output) const {
  300. GOOGLE_DCHECK(InternalGetTable());
  301. internal::TableSerialize(
  302. *this,
  303. static_cast<const internal::SerializationTable*>(InternalGetTable()),
  304. output);
  305. }
  306. // The table driven code optimizes the case that the CodedOutputStream buffer
  307. // is large enough to serialize into it directly.
  308. // If the proto is optimized for speed, this method will be overridden by
  309. // generated code for maximum speed. If the proto is optimized for size or
  310. // is lite, then we need to specialize this to avoid infinite recursion.
  311. uint8* MessageLite::InternalSerializeWithCachedSizesToArray(
  312. bool deterministic, uint8* target) const {
  313. const internal::SerializationTable* table =
  314. static_cast<const internal::SerializationTable*>(InternalGetTable());
  315. if (table == NULL) {
  316. // We only optimize this when using optimize_for = SPEED. In other cases
  317. // we just use the CodedOutputStream path.
  318. int size = GetCachedSize();
  319. io::ArrayOutputStream out(target, size);
  320. io::CodedOutputStream coded_out(&out);
  321. coded_out.SetSerializationDeterministic(deterministic);
  322. SerializeWithCachedSizes(&coded_out);
  323. GOOGLE_CHECK(!coded_out.HadError());
  324. return target + size;
  325. } else {
  326. return internal::TableSerializeToArray(*this, table, deterministic, target);
  327. }
  328. }
  329. namespace internal {
  330. template<>
  331. MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype(
  332. const MessageLite* prototype, google::protobuf::Arena* arena) {
  333. return prototype->New(arena);
  334. }
  335. template <>
  336. void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
  337. MessageLite* to) {
  338. to->CheckTypeAndMergeFrom(from);
  339. }
  340. template<>
  341. void GenericTypeHandler<string>::Merge(const string& from,
  342. string* to) {
  343. *to = from;
  344. }
  345. bool proto3_preserve_unknown_ = true;
  346. void SetProto3PreserveUnknownsDefault(bool preserve) {
  347. proto3_preserve_unknown_ = preserve;
  348. }
  349. } // namespace internal
  350. } // namespace protobuf
  351. } // namespace google