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
31 // Authors: wink@google.com (Wink Saville),
32 // kenton@google.com (Kenton Varda)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
35
36 #include <google/protobuf/message_lite.h>
37
38 #include <climits>
39 #include <cstdint>
40 #include <string>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/stubs/stringprintf.h>
45 #include <google/protobuf/parse_context.h>
46 #include <google/protobuf/io/coded_stream.h>
47 #include <google/protobuf/io/zero_copy_stream.h>
48 #include <google/protobuf/io/zero_copy_stream_impl.h>
49 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
50 #include <google/protobuf/arena.h>
51 #include <google/protobuf/generated_message_table_driven.h>
52 #include <google/protobuf/generated_message_util.h>
53 #include <google/protobuf/repeated_field.h>
54
55 #include <google/protobuf/stubs/strutil.h>
56 #include <google/protobuf/stubs/stl_util.h>
57
58 #include <google/protobuf/port_def.inc>
59
60 namespace google {
61 namespace protobuf {
62
InitializationErrorString() const63 std::string MessageLite::InitializationErrorString() const {
64 return "(cannot determine missing fields for lite message)";
65 }
66
DebugString() const67 std::string MessageLite::DebugString() const {
68 std::uintptr_t address = reinterpret_cast<std::uintptr_t>(this);
69 return StrCat("MessageLite at 0x", strings::Hex(address));
70 }
71
72 namespace {
73
74 // When serializing, we first compute the byte size, then serialize the message.
75 // If serialization produces a different number of bytes than expected, we
76 // call this function, which crashes. The problem could be due to a bug in the
77 // protobuf implementation but is more likely caused by concurrent modification
78 // of the message. This function attempts to distinguish between the two and
79 // provide a useful error message.
ByteSizeConsistencyError(size_t byte_size_before_serialization,size_t byte_size_after_serialization,size_t bytes_produced_by_serialization,const MessageLite & message)80 void ByteSizeConsistencyError(size_t byte_size_before_serialization,
81 size_t byte_size_after_serialization,
82 size_t bytes_produced_by_serialization,
83 const MessageLite& message) {
84 GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization)
85 << message.GetTypeName()
86 << " was modified concurrently during serialization.";
87 GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization)
88 << "Byte size calculation and serialization were inconsistent. This "
89 "may indicate a bug in protocol buffers or it may be caused by "
90 "concurrent modification of "
91 << message.GetTypeName() << ".";
92 GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal.";
93 }
94
InitializationErrorMessage(const char * action,const MessageLite & message)95 std::string InitializationErrorMessage(const char* action,
96 const MessageLite& message) {
97 // Note: We want to avoid depending on strutil in the lite library, otherwise
98 // we'd use:
99 //
100 // return strings::Substitute(
101 // "Can't $0 message of type \"$1\" because it is missing required "
102 // "fields: $2",
103 // action, message.GetTypeName(),
104 // message.InitializationErrorString());
105
106 std::string result;
107 result += "Can't ";
108 result += action;
109 result += " message of type \"";
110 result += message.GetTypeName();
111 result += "\" because it is missing required fields: ";
112 result += message.InitializationErrorString();
113 return result;
114 }
115
as_string_view(const void * data,int size)116 inline StringPiece as_string_view(const void* data, int size) {
117 return StringPiece(static_cast<const char*>(data), size);
118 }
119
120 } // namespace
121
LogInitializationErrorMessage() const122 void MessageLite::LogInitializationErrorMessage() const {
123 GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
124 }
125
126 namespace internal {
127
128 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
129
130 template <bool aliasing>
MergePartialFromImpl(StringPiece input,MessageLite * msg)131 bool MergePartialFromImpl(StringPiece input, MessageLite* msg) {
132 const char* ptr;
133 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
134 aliasing, &ptr, input);
135 ptr = msg->_InternalParse(ptr, &ctx);
136 // ctx has an explicit limit set (length of string_view).
137 return ptr && ctx.EndedAtLimit();
138 }
139
140 template <bool aliasing>
MergePartialFromImpl(io::ZeroCopyInputStream * input,MessageLite * msg)141 bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) {
142 const char* ptr;
143 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
144 aliasing, &ptr, input);
145 ptr = msg->_InternalParse(ptr, &ctx);
146 // ctx has no explicit limit (hence we end on end of stream)
147 return ptr && ctx.EndedAtEndOfStream();
148 }
149
150 template <bool aliasing>
MergePartialFromImpl(BoundedZCIS input,MessageLite * msg)151 bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) {
152 const char* ptr;
153 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
154 aliasing, &ptr, input.zcis, input.limit);
155 ptr = msg->_InternalParse(ptr, &ctx);
156 if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
157 ctx.BackUp(ptr);
158 return ctx.EndedAtLimit();
159 }
160
161 #else // !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
162
163 inline bool InlineMergePartialEntireStream(io::CodedInputStream* cis,
164 MessageLite* message,
165 bool aliasing) {
166 return message->MergePartialFromCodedStream(cis) &&
167 cis->ConsumedEntireMessage();
168 }
169
170 template <bool aliasing>
171 bool MergePartialFromImpl(StringPiece input, MessageLite* msg) {
172 io::CodedInputStream decoder(reinterpret_cast<const uint8*>(input.data()),
173 input.size());
174 return InlineMergePartialEntireStream(&decoder, msg, aliasing);
175 }
176
177 template <bool aliasing>
178 bool MergePartialFromImpl(BoundedZCIS input, MessageLite* msg) {
179 io::CodedInputStream decoder(input.zcis);
180 decoder.PushLimit(input.limit);
181 return InlineMergePartialEntireStream(&decoder, msg, aliasing) &&
182 decoder.BytesUntilLimit() == 0;
183 }
184
185 template <bool aliasing>
186 bool MergePartialFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg) {
187 io::CodedInputStream decoder(input);
188 return InlineMergePartialEntireStream(&decoder, msg, aliasing);
189 }
190
191 #endif // !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
192
193 template bool MergePartialFromImpl<false>(StringPiece input,
194 MessageLite* msg);
195 template bool MergePartialFromImpl<true>(StringPiece input,
196 MessageLite* msg);
197 template bool MergePartialFromImpl<false>(io::ZeroCopyInputStream* input,
198 MessageLite* msg);
199 template bool MergePartialFromImpl<true>(io::ZeroCopyInputStream* input,
200 MessageLite* msg);
201 template bool MergePartialFromImpl<false>(BoundedZCIS input, MessageLite* msg);
202 template bool MergePartialFromImpl<true>(BoundedZCIS input, MessageLite* msg);
203
204 } // namespace internal
205
New(Arena * arena) const206 MessageLite* MessageLite::New(Arena* arena) const {
207 MessageLite* message = New();
208 if (arena != NULL) {
209 arena->Own(message);
210 }
211 return message;
212 }
213
214 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
215 class ZeroCopyCodedInputStream : public io::ZeroCopyInputStream {
216 public:
ZeroCopyCodedInputStream(io::CodedInputStream * cis)217 ZeroCopyCodedInputStream(io::CodedInputStream* cis) : cis_(cis) {}
Next(const void ** data,int * size)218 bool Next(const void** data, int* size) final {
219 if (!cis_->GetDirectBufferPointer(data, size)) return false;
220 cis_->Skip(*size);
221 return true;
222 }
BackUp(int count)223 void BackUp(int count) final { cis_->Advance(-count); }
Skip(int count)224 bool Skip(int count) final { return cis_->Skip(count); }
ByteCount() const225 int64 ByteCount() const final { return 0; }
226
aliasing_enabled()227 bool aliasing_enabled() { return cis_->aliasing_enabled_; }
228
229 private:
230 io::CodedInputStream* cis_;
231 };
232
MergePartialFromCodedStream(io::CodedInputStream * input)233 bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) {
234 ZeroCopyCodedInputStream zcis(input);
235 const char* ptr;
236 internal::ParseContext ctx(input->RecursionBudget(), zcis.aliasing_enabled(),
237 &ptr, &zcis);
238 // MergePartialFromCodedStream allows terminating the wireformat by 0 or
239 // end-group tag. Leaving it up to the caller to verify correct ending by
240 // calling LastTagWas on input. We need to maintain this behavior.
241 ctx.TrackCorrectEnding();
242 ctx.data().pool = input->GetExtensionPool();
243 ctx.data().factory = input->GetExtensionFactory();
244 ptr = _InternalParse(ptr, &ctx);
245 if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
246 ctx.BackUp(ptr);
247 if (!ctx.EndedAtEndOfStream()) {
248 GOOGLE_DCHECK(ctx.LastTag() != 1); // We can't end on a pushed limit.
249 if (ctx.IsExceedingLimit(ptr)) return false;
250 input->SetLastTag(ctx.LastTag());
251 return true;
252 }
253 input->SetConsumed();
254 return true;
255 }
256 #endif // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
257
MergeFromCodedStream(io::CodedInputStream * input)258 bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) {
259 return MergePartialFromCodedStream(input) && IsInitializedWithErrors();
260 }
261
ParseFromCodedStream(io::CodedInputStream * input)262 bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) {
263 Clear();
264 return MergeFromCodedStream(input);
265 }
266
ParsePartialFromCodedStream(io::CodedInputStream * input)267 bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) {
268 Clear();
269 return MergePartialFromCodedStream(input);
270 }
271
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)272 bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
273 return ParseFrom<kParse>(input);
274 }
275
ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream * input)276 bool MessageLite::ParsePartialFromZeroCopyStream(
277 io::ZeroCopyInputStream* input) {
278 return ParseFrom<kParsePartial>(input);
279 }
280
ParseFromFileDescriptor(int file_descriptor)281 bool MessageLite::ParseFromFileDescriptor(int file_descriptor) {
282 io::FileInputStream input(file_descriptor);
283 return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
284 }
285
ParsePartialFromFileDescriptor(int file_descriptor)286 bool MessageLite::ParsePartialFromFileDescriptor(int file_descriptor) {
287 io::FileInputStream input(file_descriptor);
288 return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
289 }
290
ParseFromIstream(std::istream * input)291 bool MessageLite::ParseFromIstream(std::istream* input) {
292 io::IstreamInputStream zero_copy_input(input);
293 return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
294 }
295
ParsePartialFromIstream(std::istream * input)296 bool MessageLite::ParsePartialFromIstream(std::istream* input) {
297 io::IstreamInputStream zero_copy_input(input);
298 return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
299 }
300
MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)301 bool MessageLite::MergePartialFromBoundedZeroCopyStream(
302 io::ZeroCopyInputStream* input, int size) {
303 return ParseFrom<kMergePartial>(internal::BoundedZCIS{input, size});
304 }
305
MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)306 bool MessageLite::MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
307 int size) {
308 return ParseFrom<kMerge>(internal::BoundedZCIS{input, size});
309 }
310
ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)311 bool MessageLite::ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
312 int size) {
313 return ParseFrom<kParse>(internal::BoundedZCIS{input, size});
314 }
315
ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)316 bool MessageLite::ParsePartialFromBoundedZeroCopyStream(
317 io::ZeroCopyInputStream* input, int size) {
318 return ParseFrom<kParsePartial>(internal::BoundedZCIS{input, size});
319 }
320
ParseFromString(const std::string & data)321 bool MessageLite::ParseFromString(const std::string& data) {
322 return ParseFrom<kParse>(data);
323 }
324
ParsePartialFromString(const std::string & data)325 bool MessageLite::ParsePartialFromString(const std::string& data) {
326 return ParseFrom<kParsePartial>(data);
327 }
328
ParseFromArray(const void * data,int size)329 bool MessageLite::ParseFromArray(const void* data, int size) {
330 return ParseFrom<kParse>(as_string_view(data, size));
331 }
332
ParsePartialFromArray(const void * data,int size)333 bool MessageLite::ParsePartialFromArray(const void* data, int size) {
334 return ParseFrom<kParsePartial>(as_string_view(data, size));
335 }
336
MergeFromString(const std::string & data)337 bool MessageLite::MergeFromString(const std::string& data) {
338 return ParseFrom<kMerge>(data);
339 }
340
341
342 // ===================================================================
343
SerializeWithCachedSizesToArray(uint8 * target) const344 uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const {
345 const internal::SerializationTable* table =
346 static_cast<const internal::SerializationTable*>(InternalGetTable());
347 auto deterministic =
348 io::CodedOutputStream::IsDefaultSerializationDeterministic();
349 if (table) {
350 return internal::TableSerializeToArray(*this, table, deterministic, target);
351 } else {
352 if (deterministic) {
353 // We only optimize this when using optimize_for = SPEED. In other cases
354 // we just use the CodedOutputStream path.
355 int size = GetCachedSize();
356 io::ArrayOutputStream out(target, size);
357 io::CodedOutputStream coded_out(&out);
358 coded_out.SetSerializationDeterministic(true);
359 SerializeWithCachedSizes(&coded_out);
360 GOOGLE_CHECK(!coded_out.HadError());
361 return target + size;
362 } else {
363 return InternalSerializeWithCachedSizesToArray(target);
364 }
365 }
366 }
367
SerializeToCodedStream(io::CodedOutputStream * output) const368 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
369 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
370 return SerializePartialToCodedStream(output);
371 }
372
SerializePartialToCodedStream(io::CodedOutputStream * output) const373 bool MessageLite::SerializePartialToCodedStream(
374 io::CodedOutputStream* output) const {
375 const size_t size = ByteSizeLong(); // Force size to be cached.
376 if (size > INT_MAX) {
377 GOOGLE_LOG(ERROR) << GetTypeName()
378 << " exceeded maximum protobuf size of 2GB: " << size;
379 return false;
380 }
381
382 if (!output->IsSerializationDeterministic()) {
383 uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
384 if (buffer != nullptr) {
385 uint8* end = InternalSerializeWithCachedSizesToArray(buffer);
386 if (end - buffer != size) {
387 ByteSizeConsistencyError(size, ByteSizeLong(), end - buffer, *this);
388 }
389 return true;
390 }
391 }
392 int original_byte_count = output->ByteCount();
393 SerializeWithCachedSizes(output);
394 if (output->HadError()) {
395 return false;
396 }
397 int final_byte_count = output->ByteCount();
398
399 if (final_byte_count - original_byte_count != size) {
400 ByteSizeConsistencyError(size, ByteSizeLong(),
401 final_byte_count - original_byte_count, *this);
402 }
403
404 return true;
405 }
406
SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) const407 bool MessageLite::SerializeToZeroCopyStream(
408 io::ZeroCopyOutputStream* output) const {
409 io::CodedOutputStream encoder(output);
410 return SerializeToCodedStream(&encoder);
411 }
412
SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream * output) const413 bool MessageLite::SerializePartialToZeroCopyStream(
414 io::ZeroCopyOutputStream* output) const {
415 io::CodedOutputStream encoder(output);
416 return SerializePartialToCodedStream(&encoder);
417 }
418
SerializeToFileDescriptor(int file_descriptor) const419 bool MessageLite::SerializeToFileDescriptor(int file_descriptor) const {
420 io::FileOutputStream output(file_descriptor);
421 return SerializeToZeroCopyStream(&output) && output.Flush();
422 }
423
SerializePartialToFileDescriptor(int file_descriptor) const424 bool MessageLite::SerializePartialToFileDescriptor(int file_descriptor) const {
425 io::FileOutputStream output(file_descriptor);
426 return SerializePartialToZeroCopyStream(&output) && output.Flush();
427 }
428
SerializeToOstream(std::ostream * output) const429 bool MessageLite::SerializeToOstream(std::ostream* output) const {
430 {
431 io::OstreamOutputStream zero_copy_output(output);
432 if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
433 }
434 return output->good();
435 }
436
SerializePartialToOstream(std::ostream * output) const437 bool MessageLite::SerializePartialToOstream(std::ostream* output) const {
438 io::OstreamOutputStream zero_copy_output(output);
439 return SerializePartialToZeroCopyStream(&zero_copy_output);
440 }
441
AppendToString(std::string * output) const442 bool MessageLite::AppendToString(std::string* output) const {
443 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
444 return AppendPartialToString(output);
445 }
446
AppendPartialToString(std::string * output) const447 bool MessageLite::AppendPartialToString(std::string* output) const {
448 size_t old_size = output->size();
449 size_t byte_size = ByteSizeLong();
450 if (byte_size > INT_MAX) {
451 GOOGLE_LOG(ERROR) << GetTypeName()
452 << " exceeded maximum protobuf size of 2GB: " << byte_size;
453 return false;
454 }
455
456 STLStringResizeUninitialized(output, old_size + byte_size);
457 uint8* start =
458 reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size);
459 uint8* end = SerializeWithCachedSizesToArray(start);
460 if (end - start != byte_size) {
461 ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
462 }
463 return true;
464 }
465
SerializeToString(std::string * output) const466 bool MessageLite::SerializeToString(std::string* output) const {
467 output->clear();
468 return AppendToString(output);
469 }
470
SerializePartialToString(std::string * output) const471 bool MessageLite::SerializePartialToString(std::string* output) const {
472 output->clear();
473 return AppendPartialToString(output);
474 }
475
SerializeToArray(void * data,int size) const476 bool MessageLite::SerializeToArray(void* data, int size) const {
477 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
478 return SerializePartialToArray(data, size);
479 }
480
SerializePartialToArray(void * data,int size) const481 bool MessageLite::SerializePartialToArray(void* data, int size) const {
482 const size_t byte_size = ByteSizeLong();
483 if (byte_size > INT_MAX) {
484 GOOGLE_LOG(ERROR) << GetTypeName()
485 << " exceeded maximum protobuf size of 2GB: " << byte_size;
486 return false;
487 }
488 if (size < byte_size) return false;
489 uint8* start = reinterpret_cast<uint8*>(data);
490 uint8* end = SerializeWithCachedSizesToArray(start);
491 if (end - start != byte_size) {
492 ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
493 }
494 return true;
495 }
496
SerializeAsString() const497 std::string MessageLite::SerializeAsString() const {
498 // If the compiler implements the (Named) Return Value Optimization,
499 // the local variable 'output' will not actually reside on the stack
500 // of this function, but will be overlaid with the object that the
501 // caller supplied for the return value to be constructed in.
502 std::string output;
503 if (!AppendToString(&output)) output.clear();
504 return output;
505 }
506
SerializePartialAsString() const507 std::string MessageLite::SerializePartialAsString() const {
508 std::string output;
509 if (!AppendPartialToString(&output)) output.clear();
510 return output;
511 }
512
SerializeWithCachedSizes(io::CodedOutputStream * output) const513 void MessageLite::SerializeWithCachedSizes(
514 io::CodedOutputStream* output) const {
515 GOOGLE_DCHECK(InternalGetTable());
516 internal::TableSerialize(
517 *this,
518 static_cast<const internal::SerializationTable*>(InternalGetTable()),
519 output);
520 }
521
522
523 // The table driven code optimizes the case that the CodedOutputStream buffer
524 // is large enough to serialize into it directly.
525 // If the proto is optimized for speed, this method will be overridden by
526 // generated code for maximum speed. If the proto is optimized for size or
527 // is lite, then we need to specialize this to avoid infinite recursion.
InternalSerializeWithCachedSizesToArray(uint8 * target) const528 uint8* MessageLite::InternalSerializeWithCachedSizesToArray(
529 uint8* target) const {
530 const internal::SerializationTable* table =
531 static_cast<const internal::SerializationTable*>(InternalGetTable());
532 if (table == NULL) {
533 // We only optimize this when using optimize_for = SPEED. In other cases
534 // we just use the CodedOutputStream path.
535 int size = GetCachedSize();
536 io::ArrayOutputStream out(target, size);
537 io::CodedOutputStream coded_out(&out);
538 SerializeWithCachedSizes(&coded_out);
539 GOOGLE_CHECK(!coded_out.HadError());
540 return target + size;
541 } else {
542 return internal::TableSerializeToArray(*this, table, false, target);
543 }
544 }
545
546 namespace internal {
547
548 template <>
NewFromPrototype(const MessageLite * prototype,Arena * arena)549 MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype(
550 const MessageLite* prototype, Arena* arena) {
551 return prototype->New(arena);
552 }
553 template <>
Merge(const MessageLite & from,MessageLite * to)554 void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
555 MessageLite* to) {
556 to->CheckTypeAndMergeFrom(from);
557 }
558 template <>
Merge(const std::string & from,std::string * to)559 void GenericTypeHandler<std::string>::Merge(const std::string& from,
560 std::string* to) {
561 *to = from;
562 }
563
564 } // namespace internal
565
566 } // namespace protobuf
567 } // namespace google
568