• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/io/coded_stream.h>
34 #include <google/protobuf/io/zero_copy_stream.h>
35 #include <google/protobuf/arenastring.h>
36 #include <google/protobuf/message_lite.h>
37 #include <google/protobuf/repeated_field.h>
38 #include <google/protobuf/stubs/strutil.h>
39 #include <google/protobuf/wire_format_lite.h>
40 
41 // Must be included last.
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_GE(overrun, 0);
54   GOOGLE_DCHECK_LE(overrun, kSlopBytes);
55   auto ptr = begin + overrun;
56   auto end = begin + kSlopBytes;
57   while (ptr < end) {
58     uint32_t 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_t 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_t 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_GT(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_GE(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_GT(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 *)240 void byteswap<1>(void* /*p*/) {}
241 template <>
byteswap(void * p)242 void byteswap<4>(void* p) {
243   *static_cast<uint32_t*>(p) = bswap_32(*static_cast<uint32_t*>(p));
244 }
245 template <>
byteswap(void * p)246 void byteswap<8>(void* p) {
247   *static_cast<uint64_t*>(p) = bswap_64(*static_cast<uint64_t*>(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   int old;
293   ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
294   ptr = ptr ? msg->_InternalParse(ptr, this) : nullptr;
295   depth_++;
296   if (!PopLimit(old)) return nullptr;
297   return ptr;
298 }
299 
WriteVarint(uint64_t val,std::string * s)300 inline void WriteVarint(uint64_t val, std::string* s) {
301   while (val >= 128) {
302     uint8_t c = val | 0x80;
303     s->push_back(c);
304     val >>= 7;
305   }
306   s->push_back(val);
307 }
308 
WriteVarint(uint32_t num,uint64_t val,std::string * s)309 void WriteVarint(uint32_t num, uint64_t val, std::string* s) {
310   WriteVarint(num << 3, s);
311   WriteVarint(val, s);
312 }
313 
WriteLengthDelimited(uint32_t num,StringPiece val,std::string * s)314 void WriteLengthDelimited(uint32_t num, StringPiece val, std::string* s) {
315   WriteVarint((num << 3) + 2, s);
316   WriteVarint(val.size(), s);
317   s->append(val.data(), val.size());
318 }
319 
VarintParseSlow32(const char * p,uint32_t res)320 std::pair<const char*, uint32_t> VarintParseSlow32(const char* p,
321                                                    uint32_t res) {
322   for (std::uint32_t i = 2; i < 5; i++) {
323     uint32_t byte = static_cast<uint8_t>(p[i]);
324     res += (byte - 1) << (7 * i);
325     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
326       return {p + i + 1, res};
327     }
328   }
329   // Accept >5 bytes
330   for (std::uint32_t i = 5; i < 10; i++) {
331     uint32_t byte = static_cast<uint8_t>(p[i]);
332     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
333       return {p + i + 1, res};
334     }
335   }
336   return {nullptr, 0};
337 }
338 
VarintParseSlow64(const char * p,uint32_t res32)339 std::pair<const char*, uint64_t> VarintParseSlow64(const char* p,
340                                                    uint32_t res32) {
341   uint64_t res = res32;
342   for (std::uint32_t i = 2; i < 10; i++) {
343     uint64_t byte = static_cast<uint8_t>(p[i]);
344     res += (byte - 1) << (7 * i);
345     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
346       return {p + i + 1, res};
347     }
348   }
349   return {nullptr, 0};
350 }
351 
ReadTagFallback(const char * p,uint32_t res)352 std::pair<const char*, uint32_t> ReadTagFallback(const char* p, uint32_t res) {
353   for (std::uint32_t i = 2; i < 5; i++) {
354     uint32_t byte = static_cast<uint8_t>(p[i]);
355     res += (byte - 1) << (7 * i);
356     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
357       return {p + i + 1, res};
358     }
359   }
360   return {nullptr, 0};
361 }
362 
ReadSizeFallback(const char * p,uint32_t res)363 std::pair<const char*, int32_t> ReadSizeFallback(const char* p, uint32_t res) {
364   for (std::uint32_t i = 1; i < 4; i++) {
365     uint32_t byte = static_cast<uint8_t>(p[i]);
366     res += (byte - 1) << (7 * i);
367     if (PROTOBUF_PREDICT_TRUE(byte < 128)) {
368       return {p + i + 1, res};
369     }
370   }
371   std::uint32_t byte = static_cast<uint8_t>(p[4]);
372   if (PROTOBUF_PREDICT_FALSE(byte >= 8)) return {nullptr, 0};  // size >= 2gb
373   res += (byte - 1) << 28;
374   // Protect against sign integer overflow in PushLimit. Limits are relative
375   // to buffer ends and ptr could potential be kSlopBytes beyond a buffer end.
376   // To protect against overflow we reject limits absurdly close to INT_MAX.
377   if (PROTOBUF_PREDICT_FALSE(res > INT_MAX - ParseContext::kSlopBytes)) {
378     return {nullptr, 0};
379   }
380   return {p + 5, res};
381 }
382 
StringParser(const char * begin,const char * end,void * object,ParseContext *)383 const char* StringParser(const char* begin, const char* end, void* object,
384                          ParseContext*) {
385   auto str = static_cast<std::string*>(object);
386   str->append(begin, end - begin);
387   return end;
388 }
389 
390 // Defined in wire_format_lite.cc
391 void PrintUTF8ErrorLog(StringPiece message_name,
392                        StringPiece field_name, const char* operation_str,
393                        bool emit_stacktrace);
394 
VerifyUTF8(StringPiece str,const char * field_name)395 bool VerifyUTF8(StringPiece str, const char* field_name) {
396   if (!IsStructurallyValidUTF8(str)) {
397     PrintUTF8ErrorLog("", field_name, "parsing", false);
398     return false;
399   }
400   return true;
401 }
402 
InlineGreedyStringParser(std::string * s,const char * ptr,ParseContext * ctx)403 const char* InlineGreedyStringParser(std::string* s, const char* ptr,
404                                      ParseContext* ctx) {
405   int size = ReadSize(&ptr);
406   if (!ptr) return nullptr;
407   return ctx->ReadString(ptr, size, s);
408 }
409 
410 
411 template <typename T, bool sign>
VarintParser(void * object,const char * ptr,ParseContext * ctx)412 const char* VarintParser(void* object, const char* ptr, ParseContext* ctx) {
413   return ctx->ReadPackedVarint(ptr, [object](uint64_t varint) {
414     T val;
415     if (sign) {
416       if (sizeof(T) == 8) {
417         val = WireFormatLite::ZigZagDecode64(varint);
418       } else {
419         val = WireFormatLite::ZigZagDecode32(varint);
420       }
421     } else {
422       val = varint;
423     }
424     static_cast<RepeatedField<T>*>(object)->Add(val);
425   });
426 }
427 
PackedInt32Parser(void * object,const char * ptr,ParseContext * ctx)428 const char* PackedInt32Parser(void* object, const char* ptr,
429                               ParseContext* ctx) {
430   return VarintParser<int32_t, false>(object, ptr, ctx);
431 }
PackedUInt32Parser(void * object,const char * ptr,ParseContext * ctx)432 const char* PackedUInt32Parser(void* object, const char* ptr,
433                                ParseContext* ctx) {
434   return VarintParser<uint32_t, false>(object, ptr, ctx);
435 }
PackedInt64Parser(void * object,const char * ptr,ParseContext * ctx)436 const char* PackedInt64Parser(void* object, const char* ptr,
437                               ParseContext* ctx) {
438   return VarintParser<int64_t, false>(object, ptr, ctx);
439 }
PackedUInt64Parser(void * object,const char * ptr,ParseContext * ctx)440 const char* PackedUInt64Parser(void* object, const char* ptr,
441                                ParseContext* ctx) {
442   return VarintParser<uint64_t, false>(object, ptr, ctx);
443 }
PackedSInt32Parser(void * object,const char * ptr,ParseContext * ctx)444 const char* PackedSInt32Parser(void* object, const char* ptr,
445                                ParseContext* ctx) {
446   return VarintParser<int32_t, true>(object, ptr, ctx);
447 }
PackedSInt64Parser(void * object,const char * ptr,ParseContext * ctx)448 const char* PackedSInt64Parser(void* object, const char* ptr,
449                                ParseContext* ctx) {
450   return VarintParser<int64_t, true>(object, ptr, ctx);
451 }
452 
PackedEnumParser(void * object,const char * ptr,ParseContext * ctx)453 const char* PackedEnumParser(void* object, const char* ptr, ParseContext* ctx) {
454   return VarintParser<int, false>(object, ptr, ctx);
455 }
456 
PackedBoolParser(void * object,const char * ptr,ParseContext * ctx)457 const char* PackedBoolParser(void* object, const char* ptr, ParseContext* ctx) {
458   return VarintParser<bool, false>(object, ptr, ctx);
459 }
460 
461 template <typename T>
FixedParser(void * object,const char * ptr,ParseContext * ctx)462 const char* FixedParser(void* object, const char* ptr, ParseContext* ctx) {
463   int size = ReadSize(&ptr);
464   return ctx->ReadPackedFixed(ptr, size,
465                               static_cast<RepeatedField<T>*>(object));
466 }
467 
PackedFixed32Parser(void * object,const char * ptr,ParseContext * ctx)468 const char* PackedFixed32Parser(void* object, const char* ptr,
469                                 ParseContext* ctx) {
470   return FixedParser<uint32_t>(object, ptr, ctx);
471 }
PackedSFixed32Parser(void * object,const char * ptr,ParseContext * ctx)472 const char* PackedSFixed32Parser(void* object, const char* ptr,
473                                  ParseContext* ctx) {
474   return FixedParser<int32_t>(object, ptr, ctx);
475 }
PackedFixed64Parser(void * object,const char * ptr,ParseContext * ctx)476 const char* PackedFixed64Parser(void* object, const char* ptr,
477                                 ParseContext* ctx) {
478   return FixedParser<uint64_t>(object, ptr, ctx);
479 }
PackedSFixed64Parser(void * object,const char * ptr,ParseContext * ctx)480 const char* PackedSFixed64Parser(void* object, const char* ptr,
481                                  ParseContext* ctx) {
482   return FixedParser<int64_t>(object, ptr, ctx);
483 }
PackedFloatParser(void * object,const char * ptr,ParseContext * ctx)484 const char* PackedFloatParser(void* object, const char* ptr,
485                               ParseContext* ctx) {
486   return FixedParser<float>(object, ptr, ctx);
487 }
PackedDoubleParser(void * object,const char * ptr,ParseContext * ctx)488 const char* PackedDoubleParser(void* object, const char* ptr,
489                                ParseContext* ctx) {
490   return FixedParser<double>(object, ptr, ctx);
491 }
492 
493 class UnknownFieldLiteParserHelper {
494  public:
UnknownFieldLiteParserHelper(std::string * unknown)495   explicit UnknownFieldLiteParserHelper(std::string* unknown)
496       : unknown_(unknown) {}
497 
AddVarint(uint32_t num,uint64_t value)498   void AddVarint(uint32_t num, uint64_t value) {
499     if (unknown_ == nullptr) return;
500     WriteVarint(num * 8, unknown_);
501     WriteVarint(value, unknown_);
502   }
AddFixed64(uint32_t num,uint64_t value)503   void AddFixed64(uint32_t num, uint64_t value) {
504     if (unknown_ == nullptr) return;
505     WriteVarint(num * 8 + 1, unknown_);
506     char buffer[8];
507     io::CodedOutputStream::WriteLittleEndian64ToArray(
508         value, reinterpret_cast<uint8_t*>(buffer));
509     unknown_->append(buffer, 8);
510   }
ParseLengthDelimited(uint32_t num,const char * ptr,ParseContext * ctx)511   const char* ParseLengthDelimited(uint32_t num, const char* ptr,
512                                    ParseContext* ctx) {
513     int size = ReadSize(&ptr);
514     GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
515     if (unknown_ == nullptr) return ctx->Skip(ptr, size);
516     WriteVarint(num * 8 + 2, unknown_);
517     WriteVarint(size, unknown_);
518     return ctx->AppendString(ptr, size, unknown_);
519   }
ParseGroup(uint32_t num,const char * ptr,ParseContext * ctx)520   const char* ParseGroup(uint32_t num, const char* ptr, ParseContext* ctx) {
521     if (unknown_) WriteVarint(num * 8 + 3, unknown_);
522     ptr = ctx->ParseGroup(this, ptr, num * 8 + 3);
523     GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
524     if (unknown_) WriteVarint(num * 8 + 4, unknown_);
525     return ptr;
526   }
AddFixed32(uint32_t num,uint32_t value)527   void AddFixed32(uint32_t num, uint32_t value) {
528     if (unknown_ == nullptr) return;
529     WriteVarint(num * 8 + 5, unknown_);
530     char buffer[4];
531     io::CodedOutputStream::WriteLittleEndian32ToArray(
532         value, reinterpret_cast<uint8_t*>(buffer));
533     unknown_->append(buffer, 4);
534   }
535 
_InternalParse(const char * ptr,ParseContext * ctx)536   const char* _InternalParse(const char* ptr, ParseContext* ctx) {
537     return WireFormatParser(*this, ptr, ctx);
538   }
539 
540  private:
541   std::string* unknown_;
542 };
543 
UnknownGroupLiteParse(std::string * unknown,const char * ptr,ParseContext * ctx)544 const char* UnknownGroupLiteParse(std::string* unknown, const char* ptr,
545                                   ParseContext* ctx) {
546   UnknownFieldLiteParserHelper field_parser(unknown);
547   return WireFormatParser(field_parser, ptr, ctx);
548 }
549 
UnknownFieldParse(uint32_t tag,std::string * unknown,const char * ptr,ParseContext * ctx)550 const char* UnknownFieldParse(uint32_t tag, std::string* unknown,
551                               const char* ptr, ParseContext* ctx) {
552   UnknownFieldLiteParserHelper field_parser(unknown);
553   return FieldParser(tag, field_parser, ptr, ctx);
554 }
555 
556 }  // namespace internal
557 }  // namespace protobuf
558 }  // namespace google
559 
560 #include <google/protobuf/port_undef.inc>
561