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 #include <google/protobuf/parse_context.h>
32 
33 #include <google/protobuf/stubs/stringprintf.h>
34 #include <google/protobuf/io/coded_stream.h>
35 #include <google/protobuf/io/zero_copy_stream.h>
36 #include <google/protobuf/arenastring.h>
37 #include <google/protobuf/message_lite.h>
38 #include <google/protobuf/repeated_field.h>
39 #include <google/protobuf/wire_format_lite.h>
40 #include <google/protobuf/stubs/strutil.h>
41 
42 #include <google/protobuf/port_def.inc>
43 
44 namespace google {
45 namespace protobuf {
46 namespace internal {
47 
48 namespace {
49 
50 // Only call if at start of tag.
ParseEndsInSlopRegion(const char * begin,int overrun,int depth)51 bool ParseEndsInSlopRegion(const char* begin, int overrun, int depth) {
52   constexpr int kSlopBytes = EpsCopyInputStream::kSlopBytes;
53   GOOGLE_DCHECK(overrun >= 0);
54   GOOGLE_DCHECK(overrun <= kSlopBytes);
55   auto ptr = begin + overrun;
56   auto end = begin + kSlopBytes;
57   while (ptr < end) {
58     uint32 tag;
59     ptr = ReadTag(ptr, &tag);
60     if (ptr == nullptr || ptr > end) return false;
61     // ending on 0 tag is allowed and is the major reason for the necessity of
62     // this function.
63     if (tag == 0) return true;
64     switch (tag & 7) {
65       case 0: {  // Varint
66         uint64 val;
67         ptr = VarintParse(ptr, &val);
68         if (ptr == nullptr) return false;
69         break;
70       }
71       case 1: {  // fixed64
72         ptr += 8;
73         break;
74       }
75       case 2: {  // len delim
76         int32 size = ReadSize(&ptr);
77         if (ptr == nullptr || size > end - ptr) return false;
78         ptr += size;
79         break;
80       }
81       case 3: {  // start group
82         depth++;
83         break;
84       }
85       case 4: {                    // end group
86         if (--depth < 0) return true;  // We exit early
87         break;
88       }
89       case 5: {  // fixed32
90         ptr += 4;
91         break;
92       }
93       default:
94         return false;  // Unknown wireformat
95     }
96   }
97   return false;
98 }
99 
100 }  // namespace
101 
NextBuffer(int overrun,int depth)102 const char* EpsCopyInputStream::NextBuffer(int overrun, int depth) {
103   if (next_chunk_ == nullptr) return nullptr;  // We've reached end of stream.
104   if (next_chunk_ != buffer_) {
105     GOOGLE_DCHECK(size_ > kSlopBytes);
106     // The chunk is large enough to be used directly
107     buffer_end_ = next_chunk_ + size_ - kSlopBytes;
108     auto res = next_chunk_;
109     next_chunk_ = buffer_;
110     if (aliasing_ == kOnPatch) aliasing_ = kNoDelta;
111     return res;
112   }
113   // Move the slop bytes of previous buffer to start of the patch buffer.
114   // Note we must use memmove because the previous buffer could be part of
115   // buffer_.
116   std::memmove(buffer_, buffer_end_, kSlopBytes);
117   if (overall_limit_ > 0 &&
118       (depth < 0 || !ParseEndsInSlopRegion(buffer_, overrun, depth))) {
119     const void* data;
120     // ZeroCopyInputStream indicates Next may return 0 size buffers. Hence
121     // we loop.
122     while (StreamNext(&data)) {
123       if (size_ > kSlopBytes) {
124         // We got a large chunk
125         std::memcpy(buffer_ + kSlopBytes, data, kSlopBytes);
126         next_chunk_ = static_cast<const char*>(data);
127         buffer_end_ = buffer_ + kSlopBytes;
128         if (aliasing_ >= kNoDelta) aliasing_ = kOnPatch;
129         return buffer_;
130       } else if (size_ > 0) {
131         std::memcpy(buffer_ + kSlopBytes, data, size_);
132         next_chunk_ = buffer_;
133         buffer_end_ = buffer_ + size_;
134         if (aliasing_ >= kNoDelta) aliasing_ = kOnPatch;
135         return buffer_;
136       }
137       GOOGLE_DCHECK(size_ == 0) << size_;
138     }
139     overall_limit_ = 0;  // Next failed, no more needs for next
140   }
141   // End of stream or array
142   if (aliasing_ == kNoDelta) {
143     // If there is no more block and aliasing is true, the previous block
144     // is still valid and we can alias. We have users relying on string_view's
145     // obtained from protos to outlive the proto, when the parse was from an
146     // array. This guarantees string_view's are always aliased if parsed from
147     // an array.
148     aliasing_ = reinterpret_cast<std::uintptr_t>(buffer_end_) -
149                 reinterpret_cast<std::uintptr_t>(buffer_);
150   }
151   next_chunk_ = nullptr;
152   buffer_end_ = buffer_ + kSlopBytes;
153   size_ = 0;
154   return buffer_;
155 }
156 
Next()157 const char* EpsCopyInputStream::Next() {
158   GOOGLE_DCHECK(limit_ > kSlopBytes);
159   auto p = NextBuffer(0 /* immaterial */, -1);
160   if (p == nullptr) {
161     limit_end_ = buffer_end_;
162     // Distinguish ending on a pushed limit or ending on end-of-stream.
163     SetEndOfStream();
164     return nullptr;
165   }
166   limit_ -= buffer_end_ - p;  // Adjust limit_ relative to new anchor
167   limit_end_ = buffer_end_ + std::min(0, limit_);
168   return p;
169 }
170 
DoneFallback(int overrun,int depth)171 std::pair<const char*, bool> EpsCopyInputStream::DoneFallback(int overrun,
172                                                               int depth) {
173   // Did we exceeded the limit (parse error).
174   if (PROTOBUF_PREDICT_FALSE(overrun > limit_)) return {nullptr, true};
175   GOOGLE_DCHECK(overrun != limit_);  // Guaranteed by caller.
176   GOOGLE_DCHECK(overrun < limit_);   // Follows from above
177   // TODO(gerbens) Instead of this dcheck we could just assign, and remove
178   // updating the limit_end from PopLimit, ie.
179   // limit_end_ = buffer_end_ + (std::min)(0, limit_);
180   // if (ptr < limit_end_) return {ptr, false};
181   GOOGLE_DCHECK(limit_end_ == buffer_end_ + (std::min)(0, limit_));
182   // At this point we know the following assertion holds.
183   GOOGLE_DCHECK(limit_ > 0);
184   GOOGLE_DCHECK(limit_end_ == buffer_end_);  // because limit_ > 0
185   const char* p;
186   do {
187     // We are past the end of buffer_end_, in the slop region.
188     GOOGLE_DCHECK(overrun >= 0);
189     p = NextBuffer(overrun, depth);
190     if (p == nullptr) {
191       // We are at the end of the stream
192       if (PROTOBUF_PREDICT_FALSE(overrun != 0)) return {nullptr, true};
193       GOOGLE_DCHECK(limit_ > 0);
194       limit_end_ = buffer_end_;
195       // Distinguish ending on a pushed limit or ending on end-of-stream.
196       SetEndOfStream();
197       return {buffer_end_, true};
198     }
199     limit_ -= buffer_end_ - p;  // Adjust limit_ relative to new anchor
200     p += overrun;
201     overrun = p - buffer_end_;
202   } while (overrun >= 0);
203   limit_end_ = buffer_end_ + std::min(0, limit_);
204   return {p, false};
205 }
206 
SkipFallback(const char * ptr,int size)207 const char* EpsCopyInputStream::SkipFallback(const char* ptr, int size) {
208   return AppendSize(ptr, size, [](const char* p, int s) {});
209 }
210 
ReadStringFallback(const char * ptr,int size,std::string * str)211 const char* EpsCopyInputStream::ReadStringFallback(const char* ptr, int size,
212                                                    std::string* str) {
213   str->clear();
214   if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) {
215     // Reserve the string up to a static safe size. If strings are bigger than
216     // this we proceed by growing the string as needed. This protects against
217     // malicious payloads making protobuf hold on to a lot of memory.
218     str->reserve(str->size() + std::min<int>(size, kSafeStringSize));
219   }
220   return AppendSize(ptr, size,
221                     [str](const char* p, int s) { str->append(p, s); });
222 }
223 
AppendStringFallback(const char * ptr,int size,std::string * str)224 const char* EpsCopyInputStream::AppendStringFallback(const char* ptr, int size,
225                                                      std::string* str) {
226   if (PROTOBUF_PREDICT_TRUE(size <= buffer_end_ - ptr + limit_)) {
227     // Reserve the string up to a static safe size. If strings are bigger than
228     // this we proceed by growing the string as needed. This protects against
229     // malicious payloads making protobuf hold on to a lot of memory.
230     str->reserve(str->size() + std::min<int>(size, kSafeStringSize));
231   }
232   return AppendSize(ptr, size,
233                     [str](const char* p, int s) { str->append(p, s); });
234 }
235 
236 
237 template <int>
238 void byteswap(void* p);
239 template <>
byteswap(void * p)240 void byteswap<1>(void* p) {}
241 template <>
byteswap(void * p)242 void byteswap<4>(void* p) {
243   *static_cast<uint32*>(p) = bswap_32(*static_cast<uint32*>(p));
244 }
245 template <>
byteswap(void * p)246 void byteswap<8>(void* p) {
247   *static_cast<uint64*>(p) = bswap_64(*static_cast<uint64*>(p));
248 }
249 
InitFrom(io::ZeroCopyInputStream * zcis)250 const char* EpsCopyInputStream::InitFrom(io::ZeroCopyInputStream* zcis) {
251   zcis_ = zcis;
252   const void* data;
253   int size;
254   limit_ = INT_MAX;
255   if (zcis->Next(&data, &size)) {
256     overall_limit_ -= size;
257     if (size > kSlopBytes) {
258       auto ptr = static_cast<const char*>(data);
259       limit_ -= size - kSlopBytes;
260       limit_end_ = buffer_end_ = ptr + size - kSlopBytes;
261       next_chunk_ = buffer_;
262       if (aliasing_ == kOnPatch) aliasing_ = kNoDelta;
263       return ptr;
264     } else {
265       limit_end_ = buffer_end_ = buffer_ + kSlopBytes;
266       next_chunk_ = buffer_;
267       auto ptr = buffer_ + 2 * kSlopBytes - size;
268       std::memcpy(ptr, data, size);
269       return ptr;
270     }
271   }
272   overall_limit_ = 0;
273   next_chunk_ = nullptr;
274   size_ = 0;
275   limit_end_ = buffer_end_ = buffer_;
276   return buffer_;
277 }
278 
ReadSizeAndPushLimitAndDepth(const char * ptr,int * old_limit)279 const char* ParseContext::ReadSizeAndPushLimitAndDepth(const char* ptr,
280                                                        int* old_limit) {
281   int size = ReadSize(&ptr);
282   if (PROTOBUF_PREDICT_FALSE(!ptr)) {
283     *old_limit = 0;  // Make sure this isn't uninitialized even on error return
284     return nullptr;
285   }
286   *old_limit = PushLimit(ptr, size);
287   if (--depth_ < 0) return nullptr;
288   return ptr;
289 }
290 
ParseMessage(MessageLite * msg,const char * ptr)291 const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
292   return ParseMessage<MessageLite>(msg, ptr);
293 }
ParseMessage(Message * msg,const char * ptr)294 const char* ParseContext::ParseMessage(Message* msg, const char* ptr) {
295   // Use reinterptret case to prevent inclusion of non lite header
296   return ParseMessage(reinterpret_cast<MessageLite*>(msg), ptr);
297 }
298 
WriteVarint(uint64 val,std::string * s)299 inline void WriteVarint(uint64 val, std::string* s) {
300   while (val >= 128) {
301     uint8 c = val | 0x80;
302     s->push_back(c);
303     val >>= 7;
304   }
305   s->push_back(val);
306 }
307 
WriteVarint(uint32 num,uint64 val,std::string * s)308 void WriteVarint(uint32 num, uint64 val, std::string* s) {
309   WriteVarint(num << 3, s);
310   WriteVarint(val, s);
311 }
312 
WriteLengthDelimited(uint32 num,StringPiece val,std::string * s)313 void WriteLengthDelimited(uint32 num, StringPiece val, std::string* s) {
314   WriteVarint((num << 3) + 2, s);
315   WriteVarint(val.size(), s);
316   s->append(val.data(), val.size());
317 }
318 
VarintParseSlow32(const char * p,uint32 res)319 std::pair<const char*, uint32> VarintParseSlow32(const char* p, uint32 res) {
320   for (std::uint32_t i = 2; i < 5; i++) {
321     uint32 byte = static_cast<uint8>(p[i]);
322     res += (byte - 1) << (7 * i);
323     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
324       return {p + i + 1, res};
325     }
326   }
327   // Accept >5 bytes
328   for (std::uint32_t i = 5; i < 10; i++) {
329     uint32 byte = static_cast<uint8>(p[i]);
330     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
331       return {p + i + 1, res};
332     }
333   }
334   return {nullptr, 0};
335 }
336 
VarintParseSlow64(const char * p,uint32 res32)337 std::pair<const char*, uint64> VarintParseSlow64(const char* p, uint32 res32) {
338   uint64 res = res32;
339   for (std::uint32_t i = 2; i < 10; i++) {
340     uint64 byte = static_cast<uint8>(p[i]);
341     res += (byte - 1) << (7 * i);
342     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
343       return {p + i + 1, res};
344     }
345   }
346   return {nullptr, 0};
347 }
348 
ReadTagFallback(const char * p,uint32 res)349 std::pair<const char*, uint32> ReadTagFallback(const char* p, uint32 res) {
350   for (std::uint32_t i = 2; i < 5; i++) {
351     uint32 byte = static_cast<uint8>(p[i]);
352     res += (byte - 1) << (7 * i);
353     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
354       return {p + i + 1, res};
355     }
356   }
357   return {nullptr, 0};
358 }
359 
ReadSizeFallback(const char * p,uint32 res)360 std::pair<const char*, int32> ReadSizeFallback(const char* p, uint32 res) {
361   for (std::uint32_t i = 1; i < 4; i++) {
362     uint32 byte = static_cast<uint8>(p[i]);
363     res += (byte - 1) << (7 * i);
364     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
365       return {p + i + 1, res};
366     }
367   }
368   std::uint32_t byte = static_cast<uint8>(p[4]);
369   if (PROTOBUF_PREDICT_FALSE(byte >= 8)) return {nullptr, 0};  // size >= 2gb
370   res += (byte - 1) << 28;
371   // Protect against sign integer overflow in PushLimit. Limits are relative
372   // to buffer ends and ptr could potential be kSlopBytes beyond a buffer end.
373   // To protect against overflow we reject limits absurdly close to INT_MAX.
374   if (PROTOBUF_PREDICT_FALSE(res > INT_MAX - ParseContext::kSlopBytes)) {
375     return {nullptr, 0};
376   }
377   return {p + 5, res};
378 }
379 
StringParser(const char * begin,const char * end,void * object,ParseContext *)380 const char* StringParser(const char* begin, const char* end, void* object,
381                          ParseContext*) {
382   auto str = static_cast<std::string*>(object);
383   str->append(begin, end - begin);
384   return end;
385 }
386 
387 // Defined in wire_format_lite.cc
388 void PrintUTF8ErrorLog(const char* field_name, const char* operation_str,
389                        bool emit_stacktrace);
390 
VerifyUTF8(StringPiece str,const char * field_name)391 bool VerifyUTF8(StringPiece str, const char* field_name) {
392   if (!IsStructurallyValidUTF8(str)) {
393     PrintUTF8ErrorLog(field_name, "parsing", false);
394     return false;
395   }
396   return true;
397 }
398 
InlineGreedyStringParser(std::string * s,const char * ptr,ParseContext * ctx)399 const char* InlineGreedyStringParser(std::string* s, const char* ptr,
400                                      ParseContext* ctx) {
401   int size = ReadSize(&ptr);
402   if (!ptr) return nullptr;
403   return ctx->ReadString(ptr, size, s);
404 }
405 
406 
407 template <typename T, bool sign>
VarintParser(void * object,const char * ptr,ParseContext * ctx)408 const char* VarintParser(void* object, const char* ptr, ParseContext* ctx) {
409   return ctx->ReadPackedVarint(ptr, [object](uint64 varint) {
410     T val;
411     if (sign) {
412       if (sizeof(T) == 8) {
413         val = WireFormatLite::ZigZagDecode64(varint);
414       } else {
415         val = WireFormatLite::ZigZagDecode32(varint);
416       }
417     } else {
418       val = varint;
419     }
420     static_cast<RepeatedField<T>*>(object)->Add(val);
421   });
422 }
423 
PackedInt32Parser(void * object,const char * ptr,ParseContext * ctx)424 const char* PackedInt32Parser(void* object, const char* ptr,
425                               ParseContext* ctx) {
426   return VarintParser<int32, false>(object, ptr, ctx);
427 }
PackedUInt32Parser(void * object,const char * ptr,ParseContext * ctx)428 const char* PackedUInt32Parser(void* object, const char* ptr,
429                                ParseContext* ctx) {
430   return VarintParser<uint32, false>(object, ptr, ctx);
431 }
PackedInt64Parser(void * object,const char * ptr,ParseContext * ctx)432 const char* PackedInt64Parser(void* object, const char* ptr,
433                               ParseContext* ctx) {
434   return VarintParser<int64, false>(object, ptr, ctx);
435 }
PackedUInt64Parser(void * object,const char * ptr,ParseContext * ctx)436 const char* PackedUInt64Parser(void* object, const char* ptr,
437                                ParseContext* ctx) {
438   return VarintParser<uint64, false>(object, ptr, ctx);
439 }
PackedSInt32Parser(void * object,const char * ptr,ParseContext * ctx)440 const char* PackedSInt32Parser(void* object, const char* ptr,
441                                ParseContext* ctx) {
442   return VarintParser<int32, true>(object, ptr, ctx);
443 }
PackedSInt64Parser(void * object,const char * ptr,ParseContext * ctx)444 const char* PackedSInt64Parser(void* object, const char* ptr,
445                                ParseContext* ctx) {
446   return VarintParser<int64, true>(object, ptr, ctx);
447 }
448 
PackedEnumParser(void * object,const char * ptr,ParseContext * ctx)449 const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx) {
450   return VarintParser<int, false>(object, ptr, ctx);
451 }
452 
PackedBoolParser(void * object,const char * ptr,ParseContext * ctx)453 const char* PackedBoolParser(void* object, const char* ptr, ParseContext* ctx) {
454   return VarintParser<bool, false>(object, ptr, ctx);
455 }
456 
457 template <typename T>
FixedParser(void * object,const char * ptr,ParseContext * ctx)458 const char* FixedParser(void* object, const char* ptr, ParseContext* ctx) {
459   int size = ReadSize(&ptr);
460   GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
461   return ctx->ReadPackedFixed(ptr, size,
462                               static_cast<RepeatedField<T>*>(object));
463 }
464 
PackedFixed32Parser(void * object,const char * ptr,ParseContext * ctx)465 const char* PackedFixed32Parser(void* object, const char* ptr,
466                                 ParseContext* ctx) {
467   return FixedParser<uint32>(object, ptr, ctx);
468 }
PackedSFixed32Parser(void * object,const char * ptr,ParseContext * ctx)469 const char* PackedSFixed32Parser(void* object, const char* ptr,
470                                  ParseContext* ctx) {
471   return FixedParser<int32>(object, ptr, ctx);
472 }
PackedFixed64Parser(void * object,const char * ptr,ParseContext * ctx)473 const char* PackedFixed64Parser(void* object, const char* ptr,
474                                 ParseContext* ctx) {
475   return FixedParser<uint64>(object, ptr, ctx);
476 }
PackedSFixed64Parser(void * object,const char * ptr,ParseContext * ctx)477 const char* PackedSFixed64Parser(void* object, const char* ptr,
478                                  ParseContext* ctx) {
479   return FixedParser<int64>(object, ptr, ctx);
480 }
PackedFloatParser(void * object,const char * ptr,ParseContext * ctx)481 const char* PackedFloatParser(void* object, const char* ptr,
482                               ParseContext* ctx) {
483   return FixedParser<float>(object, ptr, ctx);
484 }
PackedDoubleParser(void * object,const char * ptr,ParseContext * ctx)485 const char* PackedDoubleParser(void* object, const char* ptr,
486                                ParseContext* ctx) {
487   return FixedParser<double>(object, ptr, ctx);
488 }
489 
490 class UnknownFieldLiteParserHelper {
491  public:
UnknownFieldLiteParserHelper(std::string * unknown)492   explicit UnknownFieldLiteParserHelper(std::string* unknown)
493       : unknown_(unknown) {}
494 
AddVarint(uint32 num,uint64 value)495   void AddVarint(uint32 num, uint64 value) {
496     if (unknown_ == nullptr) return;
497     WriteVarint(num * 8, unknown_);
498     WriteVarint(value, unknown_);
499   }
AddFixed64(uint32 num,uint64 value)500   void AddFixed64(uint32 num, uint64 value) {
501     if (unknown_ == nullptr) return;
502     WriteVarint(num * 8 + 1, unknown_);
503     char buffer[8];
504     io::CodedOutputStream::WriteLittleEndian64ToArray(
505         value, reinterpret_cast<uint8*>(buffer));
506     unknown_->append(buffer, 8);
507   }
ParseLengthDelimited(uint32 num,const char * ptr,ParseContext * ctx)508   const char* ParseLengthDelimited(uint32 num, const char* ptr,
509                                    ParseContext* ctx) {
510     int size = ReadSize(&ptr);
511     GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
512     if (unknown_ == nullptr) return ctx->Skip(ptr, size);
513     WriteVarint(num * 8 + 2, unknown_);
514     WriteVarint(size, unknown_);
515     return ctx->AppendString(ptr, size, unknown_);
516   }
ParseGroup(uint32 num,const char * ptr,ParseContext * ctx)517   const char* ParseGroup(uint32 num, const char* ptr, ParseContext* ctx) {
518     if (unknown_) WriteVarint(num * 8 + 3, unknown_);
519     ptr = ctx->ParseGroup(this, ptr, num * 8 + 3);
520     GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
521     if (unknown_) WriteVarint(num * 8 + 4, unknown_);
522     return ptr;
523   }
AddFixed32(uint32 num,uint32 value)524   void AddFixed32(uint32 num, uint32 value) {
525     if (unknown_ == nullptr) return;
526     WriteVarint(num * 8 + 5, unknown_);
527     char buffer[4];
528     io::CodedOutputStream::WriteLittleEndian32ToArray(
529         value, reinterpret_cast<uint8*>(buffer));
530     unknown_->append(buffer, 4);
531   }
532 
_InternalParse(const char * ptr,ParseContext * ctx)533   const char* _InternalParse(const char* ptr, ParseContext* ctx) {
534     return WireFormatParser(*this, ptr, ctx);
535   }
536 
537  private:
538   std::string* unknown_;
539 };
540 
UnknownGroupLiteParse(std::string * unknown,const char * ptr,ParseContext * ctx)541 const char* UnknownGroupLiteParse(std::string* unknown, const char* ptr,
542                                   ParseContext* ctx) {
543   UnknownFieldLiteParserHelper field_parser(unknown);
544   return WireFormatParser(field_parser, ptr, ctx);
545 }
546 
UnknownFieldParse(uint32 tag,std::string * unknown,const char * ptr,ParseContext * ctx)547 const char* UnknownFieldParse(uint32 tag, std::string* unknown, const char* ptr,
548                               ParseContext* ctx) {
549   UnknownFieldLiteParserHelper field_parser(unknown);
550   return FieldParser(tag, field_parser, ptr, ctx);
551 }
552 
553 }  // namespace internal
554 }  // namespace protobuf
555 }  // namespace google
556 
557 #include <google/protobuf/port_undef.inc>
558