• 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 // Author: jschorr@google.com (Joseph Schorr)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/text_format.h>
36 
37 #include <float.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <algorithm>
41 #include <climits>
42 #include <limits>
43 #include <vector>
44 
45 #include <google/protobuf/stubs/stringprintf.h>
46 #include <google/protobuf/any.h>
47 #include <google/protobuf/descriptor.pb.h>
48 #include <google/protobuf/io/strtod.h>
49 #include <google/protobuf/io/coded_stream.h>
50 #include <google/protobuf/io/tokenizer.h>
51 #include <google/protobuf/io/zero_copy_stream.h>
52 #include <google/protobuf/io/zero_copy_stream_impl.h>
53 #include <google/protobuf/descriptor.h>
54 #include <google/protobuf/dynamic_message.h>
55 #include <google/protobuf/map_field.h>
56 #include <google/protobuf/message.h>
57 #include <google/protobuf/repeated_field.h>
58 #include <google/protobuf/unknown_field_set.h>
59 #include <google/protobuf/wire_format_lite.h>
60 #include <google/protobuf/stubs/strutil.h>
61 
62 
63 
64 #include <google/protobuf/stubs/map_util.h>
65 #include <google/protobuf/stubs/stl_util.h>
66 
67 #include <google/protobuf/port_def.inc>
68 
69 
70 namespace google {
71 namespace protobuf {
72 
73 namespace {
74 
IsHexNumber(const std::string & str)75 inline bool IsHexNumber(const std::string& str) {
76   return (str.length() >= 2 && str[0] == '0' &&
77           (str[1] == 'x' || str[1] == 'X'));
78 }
79 
IsOctNumber(const std::string & str)80 inline bool IsOctNumber(const std::string& str) {
81   return (str.length() >= 2 && str[0] == '0' &&
82           (str[1] >= '0' && str[1] < '8'));
83 }
84 
85 }  // namespace
86 
DebugString() const87 std::string Message::DebugString() const {
88   std::string debug_string;
89 
90   TextFormat::Printer printer;
91   printer.SetExpandAny(true);
92 
93   printer.PrintToString(*this, &debug_string);
94 
95   return debug_string;
96 }
97 
ShortDebugString() const98 std::string Message::ShortDebugString() const {
99   std::string debug_string;
100 
101   TextFormat::Printer printer;
102   printer.SetSingleLineMode(true);
103   printer.SetExpandAny(true);
104 
105   printer.PrintToString(*this, &debug_string);
106   // Single line mode currently might have an extra space at the end.
107   if (debug_string.size() > 0 && debug_string[debug_string.size() - 1] == ' ') {
108     debug_string.resize(debug_string.size() - 1);
109   }
110 
111   return debug_string;
112 }
113 
Utf8DebugString() const114 std::string Message::Utf8DebugString() const {
115   std::string debug_string;
116 
117   TextFormat::Printer printer;
118   printer.SetUseUtf8StringEscaping(true);
119   printer.SetExpandAny(true);
120 
121   printer.PrintToString(*this, &debug_string);
122 
123   return debug_string;
124 }
125 
PrintDebugString() const126 void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); }
127 
128 
129 // ===========================================================================
130 // Implementation of the parse information tree class.
ParseInfoTree()131 TextFormat::ParseInfoTree::ParseInfoTree() {}
132 
~ParseInfoTree()133 TextFormat::ParseInfoTree::~ParseInfoTree() {
134   // Remove any nested information trees, as they are owned by this tree.
135   for (NestedMap::iterator it = nested_.begin(); it != nested_.end(); ++it) {
136     STLDeleteElements(&(it->second));
137   }
138 }
139 
RecordLocation(const FieldDescriptor * field,TextFormat::ParseLocation location)140 void TextFormat::ParseInfoTree::RecordLocation(
141     const FieldDescriptor* field, TextFormat::ParseLocation location) {
142   locations_[field].push_back(location);
143 }
144 
CreateNested(const FieldDescriptor * field)145 TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::CreateNested(
146     const FieldDescriptor* field) {
147   // Owned by us in the map.
148   TextFormat::ParseInfoTree* instance = new TextFormat::ParseInfoTree();
149   std::vector<TextFormat::ParseInfoTree*>* trees = &nested_[field];
150   GOOGLE_CHECK(trees);
151   trees->push_back(instance);
152   return instance;
153 }
154 
CheckFieldIndex(const FieldDescriptor * field,int index)155 void CheckFieldIndex(const FieldDescriptor* field, int index) {
156   if (field == nullptr) {
157     return;
158   }
159 
160   if (field->is_repeated() && index == -1) {
161     GOOGLE_LOG(DFATAL) << "Index must be in range of repeated field values. "
162                 << "Field: " << field->name();
163   } else if (!field->is_repeated() && index != -1) {
164     GOOGLE_LOG(DFATAL) << "Index must be -1 for singular fields."
165                 << "Field: " << field->name();
166   }
167 }
168 
GetLocation(const FieldDescriptor * field,int index) const169 TextFormat::ParseLocation TextFormat::ParseInfoTree::GetLocation(
170     const FieldDescriptor* field, int index) const {
171   CheckFieldIndex(field, index);
172   if (index == -1) {
173     index = 0;
174   }
175 
176   const std::vector<TextFormat::ParseLocation>* locations =
177       FindOrNull(locations_, field);
178   if (locations == nullptr || index >= locations->size()) {
179     return TextFormat::ParseLocation();
180   }
181 
182   return (*locations)[index];
183 }
184 
GetTreeForNested(const FieldDescriptor * field,int index) const185 TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested(
186     const FieldDescriptor* field, int index) const {
187   CheckFieldIndex(field, index);
188   if (index == -1) {
189     index = 0;
190   }
191 
192   const std::vector<TextFormat::ParseInfoTree*>* trees =
193       FindOrNull(nested_, field);
194   if (trees == nullptr || index >= trees->size()) {
195     return nullptr;
196   }
197 
198   return (*trees)[index];
199 }
200 
201 namespace {
202 // These functions implement the behavior of the "default" TextFormat::Finder,
203 // they are defined as standalone to be called when finder_ is nullptr.
DefaultFinderFindExtension(Message * message,const std::string & name)204 const FieldDescriptor* DefaultFinderFindExtension(Message* message,
205                                                   const std::string& name) {
206   const Descriptor* descriptor = message->GetDescriptor();
207   return descriptor->file()->pool()->FindExtensionByPrintableName(descriptor,
208                                                                   name);
209 }
210 
DefaultFinderFindExtensionByNumber(const Descriptor * descriptor,int number)211 const FieldDescriptor* DefaultFinderFindExtensionByNumber(
212     const Descriptor* descriptor, int number) {
213   return descriptor->file()->pool()->FindExtensionByNumber(descriptor, number);
214 }
215 
DefaultFinderFindAnyType(const Message & message,const std::string & prefix,const std::string & name)216 const Descriptor* DefaultFinderFindAnyType(const Message& message,
217                                            const std::string& prefix,
218                                            const std::string& name) {
219   if (prefix != internal::kTypeGoogleApisComPrefix &&
220       prefix != internal::kTypeGoogleProdComPrefix) {
221     return nullptr;
222   }
223   return message.GetDescriptor()->file()->pool()->FindMessageTypeByName(name);
224 }
225 }  // namespace
226 
227 // ===========================================================================
228 // Internal class for parsing an ASCII representation of a Protocol Message.
229 // This class makes use of the Protocol Message compiler's tokenizer found
230 // in //net/proto2/io/public/tokenizer.h. Note that class's Parse
231 // method is *not* thread-safe and should only be used in a single thread at
232 // a time.
233 
234 // Makes code slightly more readable.  The meaning of "DO(foo)" is
235 // "Execute foo and fail if it fails.", where failure is indicated by
236 // returning false. Borrowed from parser.cc (Thanks Kenton!).
237 #define DO(STATEMENT) \
238   if (STATEMENT) {    \
239   } else {            \
240     return false;     \
241   }
242 
243 class TextFormat::Parser::ParserImpl {
244  public:
245   // Determines if repeated values for non-repeated fields and
246   // oneofs are permitted, e.g., the string "foo: 1 foo: 2" for a
247   // required/optional field named "foo", or "baz: 1 qux: 2"
248   // where "baz" and "qux" are members of the same oneof.
249   enum SingularOverwritePolicy {
250     ALLOW_SINGULAR_OVERWRITES = 0,   // the last value is retained
251     FORBID_SINGULAR_OVERWRITES = 1,  // an error is issued
252   };
253 
ParserImpl(const Descriptor * root_message_type,io::ZeroCopyInputStream * input_stream,io::ErrorCollector * error_collector,const TextFormat::Finder * finder,ParseInfoTree * parse_info_tree,SingularOverwritePolicy singular_overwrite_policy,bool allow_case_insensitive_field,bool allow_unknown_field,bool allow_unknown_extension,bool allow_unknown_enum,bool allow_field_number,bool allow_relaxed_whitespace,bool allow_partial,int recursion_limit)254   ParserImpl(const Descriptor* root_message_type,
255              io::ZeroCopyInputStream* input_stream,
256              io::ErrorCollector* error_collector,
257              const TextFormat::Finder* finder, ParseInfoTree* parse_info_tree,
258              SingularOverwritePolicy singular_overwrite_policy,
259              bool allow_case_insensitive_field, bool allow_unknown_field,
260              bool allow_unknown_extension, bool allow_unknown_enum,
261              bool allow_field_number, bool allow_relaxed_whitespace,
262              bool allow_partial, int recursion_limit)
263       : error_collector_(error_collector),
264         finder_(finder),
265         parse_info_tree_(parse_info_tree),
266         tokenizer_error_collector_(this),
267         tokenizer_(input_stream, &tokenizer_error_collector_),
268         root_message_type_(root_message_type),
269         singular_overwrite_policy_(singular_overwrite_policy),
270         allow_case_insensitive_field_(allow_case_insensitive_field),
271         allow_unknown_field_(allow_unknown_field),
272         allow_unknown_extension_(allow_unknown_extension),
273         allow_unknown_enum_(allow_unknown_enum),
274         allow_field_number_(allow_field_number),
275         allow_partial_(allow_partial),
276         recursion_limit_(recursion_limit),
277         had_errors_(false) {
278     // For backwards-compatibility with proto1, we need to allow the 'f' suffix
279     // for floats.
280     tokenizer_.set_allow_f_after_float(true);
281 
282     // '#' starts a comment.
283     tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE);
284 
285     if (allow_relaxed_whitespace) {
286       tokenizer_.set_require_space_after_number(false);
287       tokenizer_.set_allow_multiline_strings(true);
288     }
289 
290     // Consume the starting token.
291     tokenizer_.Next();
292   }
~ParserImpl()293   ~ParserImpl() {}
294 
295   // Parses the ASCII representation specified in input and saves the
296   // information into the output pointer (a Message). Returns
297   // false if an error occurs (an error will also be logged to
298   // GOOGLE_LOG(ERROR)).
Parse(Message * output)299   bool Parse(Message* output) {
300     // Consume fields until we cannot do so anymore.
301     while (true) {
302       if (LookingAtType(io::Tokenizer::TYPE_END)) {
303         return !had_errors_;
304       }
305 
306       DO(ConsumeField(output));
307     }
308   }
309 
ParseField(const FieldDescriptor * field,Message * output)310   bool ParseField(const FieldDescriptor* field, Message* output) {
311     bool suc;
312     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
313       suc = ConsumeFieldMessage(output, output->GetReflection(), field);
314     } else {
315       suc = ConsumeFieldValue(output, output->GetReflection(), field);
316     }
317     return suc && LookingAtType(io::Tokenizer::TYPE_END);
318   }
319 
ReportError(int line,int col,const std::string & message)320   void ReportError(int line, int col, const std::string& message) {
321     had_errors_ = true;
322     if (error_collector_ == nullptr) {
323       if (line >= 0) {
324         GOOGLE_LOG(ERROR) << "Error parsing text-format "
325                    << root_message_type_->full_name() << ": " << (line + 1)
326                    << ":" << (col + 1) << ": " << message;
327       } else {
328         GOOGLE_LOG(ERROR) << "Error parsing text-format "
329                    << root_message_type_->full_name() << ": " << message;
330       }
331     } else {
332       error_collector_->AddError(line, col, message);
333     }
334   }
335 
ReportWarning(int line,int col,const std::string & message)336   void ReportWarning(int line, int col, const std::string& message) {
337     if (error_collector_ == nullptr) {
338       if (line >= 0) {
339         GOOGLE_LOG(WARNING) << "Warning parsing text-format "
340                      << root_message_type_->full_name() << ": " << (line + 1)
341                      << ":" << (col + 1) << ": " << message;
342       } else {
343         GOOGLE_LOG(WARNING) << "Warning parsing text-format "
344                      << root_message_type_->full_name() << ": " << message;
345       }
346     } else {
347       error_collector_->AddWarning(line, col, message);
348     }
349   }
350 
351  private:
352   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserImpl);
353 
354   // Reports an error with the given message with information indicating
355   // the position (as derived from the current token).
ReportError(const std::string & message)356   void ReportError(const std::string& message) {
357     ReportError(tokenizer_.current().line, tokenizer_.current().column,
358                 message);
359   }
360 
361   // Reports a warning with the given message with information indicating
362   // the position (as derived from the current token).
ReportWarning(const std::string & message)363   void ReportWarning(const std::string& message) {
364     ReportWarning(tokenizer_.current().line, tokenizer_.current().column,
365                   message);
366   }
367 
368   // Consumes the specified message with the given starting delimiter.
369   // This method checks to see that the end delimiter at the conclusion of
370   // the consumption matches the starting delimiter passed in here.
ConsumeMessage(Message * message,const std::string delimiter)371   bool ConsumeMessage(Message* message, const std::string delimiter) {
372     while (!LookingAt(">") && !LookingAt("}")) {
373       DO(ConsumeField(message));
374     }
375 
376     // Confirm that we have a valid ending delimiter.
377     DO(Consume(delimiter));
378     return true;
379   }
380 
381   // Consume either "<" or "{".
ConsumeMessageDelimiter(std::string * delimiter)382   bool ConsumeMessageDelimiter(std::string* delimiter) {
383     if (TryConsume("<")) {
384       *delimiter = ">";
385     } else {
386       DO(Consume("{"));
387       *delimiter = "}";
388     }
389     return true;
390   }
391 
392 
393   // Consumes the current field (as returned by the tokenizer) on the
394   // passed in message.
ConsumeField(Message * message)395   bool ConsumeField(Message* message) {
396     const Reflection* reflection = message->GetReflection();
397     const Descriptor* descriptor = message->GetDescriptor();
398 
399     std::string field_name;
400     bool reserved_field = false;
401     const FieldDescriptor* field = nullptr;
402     int start_line = tokenizer_.current().line;
403     int start_column = tokenizer_.current().column;
404 
405     const FieldDescriptor* any_type_url_field;
406     const FieldDescriptor* any_value_field;
407     if (internal::GetAnyFieldDescriptors(*message, &any_type_url_field,
408                                          &any_value_field) &&
409         TryConsume("[")) {
410       std::string full_type_name, prefix;
411       DO(ConsumeAnyTypeUrl(&full_type_name, &prefix));
412       DO(Consume("]"));
413       TryConsume(":");  // ':' is optional between message labels and values.
414       std::string serialized_value;
415       const Descriptor* value_descriptor =
416           finder_ ? finder_->FindAnyType(*message, prefix, full_type_name)
417                   : DefaultFinderFindAnyType(*message, prefix, full_type_name);
418       if (value_descriptor == nullptr) {
419         ReportError("Could not find type \"" + prefix + full_type_name +
420                     "\" stored in google.protobuf.Any.");
421         return false;
422       }
423       DO(ConsumeAnyValue(value_descriptor, &serialized_value));
424       if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) {
425         // Fail if any_type_url_field has already been specified.
426         if ((!any_type_url_field->is_repeated() &&
427              reflection->HasField(*message, any_type_url_field)) ||
428             (!any_value_field->is_repeated() &&
429              reflection->HasField(*message, any_value_field))) {
430           ReportError("Non-repeated Any specified multiple times.");
431           return false;
432         }
433       }
434       reflection->SetString(message, any_type_url_field,
435                             std::string(prefix + full_type_name));
436       reflection->SetString(message, any_value_field, serialized_value);
437       return true;
438     }
439     if (TryConsume("[")) {
440       // Extension.
441       DO(ConsumeFullTypeName(&field_name));
442       DO(Consume("]"));
443 
444       field = finder_ ? finder_->FindExtension(message, field_name)
445                       : DefaultFinderFindExtension(message, field_name);
446 
447       if (field == nullptr) {
448         if (!allow_unknown_field_ && !allow_unknown_extension_) {
449           ReportError("Extension \"" + field_name +
450                       "\" is not defined or "
451                       "is not an extension of \"" +
452                       descriptor->full_name() + "\".");
453           return false;
454         } else {
455           ReportWarning("Ignoring extension \"" + field_name +
456                         "\" which is not defined or is not an extension of \"" +
457                         descriptor->full_name() + "\".");
458         }
459       }
460     } else {
461       DO(ConsumeIdentifier(&field_name));
462 
463       int32 field_number;
464       if (allow_field_number_ &&
465           safe_strto32(field_name, &field_number)) {
466         if (descriptor->IsExtensionNumber(field_number)) {
467           field = finder_
468                       ? finder_->FindExtensionByNumber(descriptor, field_number)
469                       : DefaultFinderFindExtensionByNumber(descriptor,
470                                                            field_number);
471         } else if (descriptor->IsReservedNumber(field_number)) {
472           reserved_field = true;
473         } else {
474           field = descriptor->FindFieldByNumber(field_number);
475         }
476       } else {
477         field = descriptor->FindFieldByName(field_name);
478         // Group names are expected to be capitalized as they appear in the
479         // .proto file, which actually matches their type names, not their
480         // field names.
481         if (field == nullptr) {
482           std::string lower_field_name = field_name;
483           LowerString(&lower_field_name);
484           field = descriptor->FindFieldByName(lower_field_name);
485           // If the case-insensitive match worked but the field is NOT a group,
486           if (field != nullptr &&
487               field->type() != FieldDescriptor::TYPE_GROUP) {
488             field = nullptr;
489           }
490         }
491         // Again, special-case group names as described above.
492         if (field != nullptr && field->type() == FieldDescriptor::TYPE_GROUP &&
493             field->message_type()->name() != field_name) {
494           field = nullptr;
495         }
496 
497         if (field == nullptr && allow_case_insensitive_field_) {
498           std::string lower_field_name = field_name;
499           LowerString(&lower_field_name);
500           field = descriptor->FindFieldByLowercaseName(lower_field_name);
501         }
502 
503         if (field == nullptr) {
504           reserved_field = descriptor->IsReservedName(field_name);
505         }
506       }
507 
508       if (field == nullptr && !reserved_field) {
509         if (!allow_unknown_field_) {
510           ReportError("Message type \"" + descriptor->full_name() +
511                       "\" has no field named \"" + field_name + "\".");
512           return false;
513         } else {
514           ReportWarning("Message type \"" + descriptor->full_name() +
515                         "\" has no field named \"" + field_name + "\".");
516         }
517       }
518     }
519 
520     // Skips unknown or reserved fields.
521     if (field == nullptr) {
522       GOOGLE_CHECK(allow_unknown_field_ || allow_unknown_extension_ || reserved_field);
523 
524       // Try to guess the type of this field.
525       // If this field is not a message, there should be a ":" between the
526       // field name and the field value and also the field value should not
527       // start with "{" or "<" which indicates the beginning of a message body.
528       // If there is no ":" or there is a "{" or "<" after ":", this field has
529       // to be a message or the input is ill-formed.
530       if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
531         return SkipFieldValue();
532       } else {
533         return SkipFieldMessage();
534       }
535     }
536 
537     if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) {
538       // Fail if the field is not repeated and it has already been specified.
539       if (!field->is_repeated() && reflection->HasField(*message, field)) {
540         ReportError("Non-repeated field \"" + field_name +
541                     "\" is specified multiple times.");
542         return false;
543       }
544       // Fail if the field is a member of a oneof and another member has already
545       // been specified.
546       const OneofDescriptor* oneof = field->containing_oneof();
547       if (oneof != nullptr && reflection->HasOneof(*message, oneof)) {
548         const FieldDescriptor* other_field =
549             reflection->GetOneofFieldDescriptor(*message, oneof);
550         ReportError("Field \"" + field_name +
551                     "\" is specified along with "
552                     "field \"" +
553                     other_field->name() +
554                     "\", another member "
555                     "of oneof \"" +
556                     oneof->name() + "\".");
557         return false;
558       }
559     }
560 
561     // Perform special handling for embedded message types.
562     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
563       // ':' is optional here.
564       bool consumed_semicolon = TryConsume(":");
565       if (consumed_semicolon && field->options().weak() &&
566           LookingAtType(io::Tokenizer::TYPE_STRING)) {
567         // we are getting a bytes string for a weak field.
568         std::string tmp;
569         DO(ConsumeString(&tmp));
570         MessageFactory* factory =
571             finder_ ? finder_->FindExtensionFactory(field) : nullptr;
572         reflection->MutableMessage(message, field, factory)
573             ->ParseFromString(tmp);
574         goto label_skip_parsing;
575       }
576     } else {
577       // ':' is required here.
578       DO(Consume(":"));
579     }
580 
581     if (field->is_repeated() && TryConsume("[")) {
582       // Short repeated format, e.g.  "foo: [1, 2, 3]".
583       if (!TryConsume("]")) {
584         // "foo: []" is treated as empty.
585         while (true) {
586           if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
587             // Perform special handling for embedded message types.
588             DO(ConsumeFieldMessage(message, reflection, field));
589           } else {
590             DO(ConsumeFieldValue(message, reflection, field));
591           }
592           if (TryConsume("]")) {
593             break;
594           }
595           DO(Consume(","));
596         }
597       }
598     } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
599       DO(ConsumeFieldMessage(message, reflection, field));
600     } else {
601       DO(ConsumeFieldValue(message, reflection, field));
602     }
603   label_skip_parsing:
604     // For historical reasons, fields may optionally be separated by commas or
605     // semicolons.
606     TryConsume(";") || TryConsume(",");
607 
608     if (field->options().deprecated()) {
609       ReportWarning("text format contains deprecated field \"" + field_name +
610                     "\"");
611     }
612 
613     // If a parse info tree exists, add the location for the parsed
614     // field.
615     if (parse_info_tree_ != nullptr) {
616       RecordLocation(parse_info_tree_, field,
617                      ParseLocation(start_line, start_column));
618     }
619 
620     return true;
621   }
622 
623   // Skips the next field including the field's name and value.
SkipField()624   bool SkipField() {
625     if (TryConsume("[")) {
626       // Extension name or type URL.
627       DO(ConsumeTypeUrlOrFullTypeName());
628       DO(Consume("]"));
629     } else {
630       std::string field_name;
631       DO(ConsumeIdentifier(&field_name));
632     }
633 
634     // Try to guess the type of this field.
635     // If this field is not a message, there should be a ":" between the
636     // field name and the field value and also the field value should not
637     // start with "{" or "<" which indicates the beginning of a message body.
638     // If there is no ":" or there is a "{" or "<" after ":", this field has
639     // to be a message or the input is ill-formed.
640     if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
641       DO(SkipFieldValue());
642     } else {
643       DO(SkipFieldMessage());
644     }
645     // For historical reasons, fields may optionally be separated by commas or
646     // semicolons.
647     TryConsume(";") || TryConsume(",");
648     return true;
649   }
650 
ConsumeFieldMessage(Message * message,const Reflection * reflection,const FieldDescriptor * field)651   bool ConsumeFieldMessage(Message* message, const Reflection* reflection,
652                            const FieldDescriptor* field) {
653     if (--recursion_limit_ < 0) {
654       ReportError("Message is too deep");
655       return false;
656     }
657     // If the parse information tree is not nullptr, create a nested one
658     // for the nested message.
659     ParseInfoTree* parent = parse_info_tree_;
660     if (parent != nullptr) {
661       parse_info_tree_ = CreateNested(parent, field);
662     }
663 
664     std::string delimiter;
665     DO(ConsumeMessageDelimiter(&delimiter));
666     MessageFactory* factory =
667         finder_ ? finder_->FindExtensionFactory(field) : nullptr;
668     if (field->is_repeated()) {
669       DO(ConsumeMessage(reflection->AddMessage(message, field, factory),
670                         delimiter));
671     } else {
672       DO(ConsumeMessage(reflection->MutableMessage(message, field, factory),
673                         delimiter));
674     }
675 
676     ++recursion_limit_;
677 
678     // Reset the parse information tree.
679     parse_info_tree_ = parent;
680     return true;
681   }
682 
683   // Skips the whole body of a message including the beginning delimiter and
684   // the ending delimiter.
SkipFieldMessage()685   bool SkipFieldMessage() {
686     std::string delimiter;
687     DO(ConsumeMessageDelimiter(&delimiter));
688     while (!LookingAt(">") && !LookingAt("}")) {
689       DO(SkipField());
690     }
691     DO(Consume(delimiter));
692     return true;
693   }
694 
ConsumeFieldValue(Message * message,const Reflection * reflection,const FieldDescriptor * field)695   bool ConsumeFieldValue(Message* message, const Reflection* reflection,
696                          const FieldDescriptor* field) {
697 // Define an easy to use macro for setting fields. This macro checks
698 // to see if the field is repeated (in which case we need to use the Add
699 // methods or not (in which case we need to use the Set methods).
700 #define SET_FIELD(CPPTYPE, VALUE)                    \
701   if (field->is_repeated()) {                        \
702     reflection->Add##CPPTYPE(message, field, VALUE); \
703   } else {                                           \
704     reflection->Set##CPPTYPE(message, field, VALUE); \
705   }
706 
707     switch (field->cpp_type()) {
708       case FieldDescriptor::CPPTYPE_INT32: {
709         int64 value;
710         DO(ConsumeSignedInteger(&value, kint32max));
711         SET_FIELD(Int32, static_cast<int32>(value));
712         break;
713       }
714 
715       case FieldDescriptor::CPPTYPE_UINT32: {
716         uint64 value;
717         DO(ConsumeUnsignedInteger(&value, kuint32max));
718         SET_FIELD(UInt32, static_cast<uint32>(value));
719         break;
720       }
721 
722       case FieldDescriptor::CPPTYPE_INT64: {
723         int64 value;
724         DO(ConsumeSignedInteger(&value, kint64max));
725         SET_FIELD(Int64, value);
726         break;
727       }
728 
729       case FieldDescriptor::CPPTYPE_UINT64: {
730         uint64 value;
731         DO(ConsumeUnsignedInteger(&value, kuint64max));
732         SET_FIELD(UInt64, value);
733         break;
734       }
735 
736       case FieldDescriptor::CPPTYPE_FLOAT: {
737         double value;
738         DO(ConsumeDouble(&value));
739         SET_FIELD(Float, io::SafeDoubleToFloat(value));
740         break;
741       }
742 
743       case FieldDescriptor::CPPTYPE_DOUBLE: {
744         double value;
745         DO(ConsumeDouble(&value));
746         SET_FIELD(Double, value);
747         break;
748       }
749 
750       case FieldDescriptor::CPPTYPE_STRING: {
751         std::string value;
752         DO(ConsumeString(&value));
753         SET_FIELD(String, value);
754         break;
755       }
756 
757       case FieldDescriptor::CPPTYPE_BOOL: {
758         if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
759           uint64 value;
760           DO(ConsumeUnsignedInteger(&value, 1));
761           SET_FIELD(Bool, value);
762         } else {
763           std::string value;
764           DO(ConsumeIdentifier(&value));
765           if (value == "true" || value == "True" || value == "t") {
766             SET_FIELD(Bool, true);
767           } else if (value == "false" || value == "False" || value == "f") {
768             SET_FIELD(Bool, false);
769           } else {
770             ReportError("Invalid value for boolean field \"" + field->name() +
771                         "\". Value: \"" + value + "\".");
772             return false;
773           }
774         }
775         break;
776       }
777 
778       case FieldDescriptor::CPPTYPE_ENUM: {
779         std::string value;
780         int64 int_value = kint64max;
781         const EnumDescriptor* enum_type = field->enum_type();
782         const EnumValueDescriptor* enum_value = nullptr;
783 
784         if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
785           DO(ConsumeIdentifier(&value));
786           // Find the enumeration value.
787           enum_value = enum_type->FindValueByName(value);
788 
789         } else if (LookingAt("-") ||
790                    LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
791           DO(ConsumeSignedInteger(&int_value, kint32max));
792           value = StrCat(int_value);  // for error reporting
793           enum_value = enum_type->FindValueByNumber(int_value);
794         } else {
795           ReportError("Expected integer or identifier, got: " +
796                       tokenizer_.current().text);
797           return false;
798         }
799 
800         if (enum_value == nullptr) {
801           if (int_value != kint64max &&
802               reflection->SupportsUnknownEnumValues()) {
803             SET_FIELD(EnumValue, int_value);
804             return true;
805           } else if (!allow_unknown_enum_) {
806             ReportError("Unknown enumeration value of \"" + value +
807                         "\" for "
808                         "field \"" +
809                         field->name() + "\".");
810             return false;
811           } else {
812             ReportWarning("Unknown enumeration value of \"" + value +
813                           "\" for "
814                           "field \"" +
815                           field->name() + "\".");
816             return true;
817           }
818         }
819 
820         SET_FIELD(Enum, enum_value);
821         break;
822       }
823 
824       case FieldDescriptor::CPPTYPE_MESSAGE: {
825         // We should never get here. Put here instead of a default
826         // so that if new types are added, we get a nice compiler warning.
827         GOOGLE_LOG(FATAL) << "Reached an unintended state: CPPTYPE_MESSAGE";
828         break;
829       }
830     }
831 #undef SET_FIELD
832     return true;
833   }
834 
SkipFieldValue()835   bool SkipFieldValue() {
836     if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
837       while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
838         tokenizer_.Next();
839       }
840       return true;
841     }
842     if (TryConsume("[")) {
843       while (true) {
844         if (!LookingAt("{") && !LookingAt("<")) {
845           DO(SkipFieldValue());
846         } else {
847           DO(SkipFieldMessage());
848         }
849         if (TryConsume("]")) {
850           break;
851         }
852         DO(Consume(","));
853       }
854       return true;
855     }
856     // Possible field values other than string:
857     //   12345        => TYPE_INTEGER
858     //   -12345       => TYPE_SYMBOL + TYPE_INTEGER
859     //   1.2345       => TYPE_FLOAT
860     //   -1.2345      => TYPE_SYMBOL + TYPE_FLOAT
861     //   inf          => TYPE_IDENTIFIER
862     //   -inf         => TYPE_SYMBOL + TYPE_IDENTIFIER
863     //   TYPE_INTEGER => TYPE_IDENTIFIER
864     // Divides them into two group, one with TYPE_SYMBOL
865     // and the other without:
866     //   Group one:
867     //     12345        => TYPE_INTEGER
868     //     1.2345       => TYPE_FLOAT
869     //     inf          => TYPE_IDENTIFIER
870     //     TYPE_INTEGER => TYPE_IDENTIFIER
871     //   Group two:
872     //     -12345       => TYPE_SYMBOL + TYPE_INTEGER
873     //     -1.2345      => TYPE_SYMBOL + TYPE_FLOAT
874     //     -inf         => TYPE_SYMBOL + TYPE_IDENTIFIER
875     // As we can see, the field value consists of an optional '-' and one of
876     // TYPE_INTEGER, TYPE_FLOAT and TYPE_IDENTIFIER.
877     bool has_minus = TryConsume("-");
878     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER) &&
879         !LookingAtType(io::Tokenizer::TYPE_FLOAT) &&
880         !LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
881       std::string text = tokenizer_.current().text;
882       ReportError("Cannot skip field value, unexpected token: " + text);
883       return false;
884     }
885     // Combination of '-' and TYPE_IDENTIFIER may result in an invalid field
886     // value while other combinations all generate valid values.
887     // We check if the value of this combination is valid here.
888     // TYPE_IDENTIFIER after a '-' should be one of the float values listed
889     // below:
890     //   inf, inff, infinity, nan
891     if (has_minus && LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
892       std::string text = tokenizer_.current().text;
893       LowerString(&text);
894       if (text != "inf" &&
895           text != "infinity" && text != "nan") {
896         ReportError("Invalid float number: " + text);
897         return false;
898       }
899     }
900     tokenizer_.Next();
901     return true;
902   }
903 
904   // Returns true if the current token's text is equal to that specified.
LookingAt(const std::string & text)905   bool LookingAt(const std::string& text) {
906     return tokenizer_.current().text == text;
907   }
908 
909   // Returns true if the current token's type is equal to that specified.
LookingAtType(io::Tokenizer::TokenType token_type)910   bool LookingAtType(io::Tokenizer::TokenType token_type) {
911     return tokenizer_.current().type == token_type;
912   }
913 
914   // Consumes an identifier and saves its value in the identifier parameter.
915   // Returns false if the token is not of type IDENTFIER.
ConsumeIdentifier(std::string * identifier)916   bool ConsumeIdentifier(std::string* identifier) {
917     if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
918       *identifier = tokenizer_.current().text;
919       tokenizer_.Next();
920       return true;
921     }
922 
923     // If allow_field_numer_ or allow_unknown_field_ is true, we should able
924     // to parse integer identifiers.
925     if ((allow_field_number_ || allow_unknown_field_ ||
926          allow_unknown_extension_) &&
927         LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
928       *identifier = tokenizer_.current().text;
929       tokenizer_.Next();
930       return true;
931     }
932 
933     ReportError("Expected identifier, got: " + tokenizer_.current().text);
934     return false;
935   }
936 
937   // Consume a string of form "<id1>.<id2>....<idN>".
ConsumeFullTypeName(std::string * name)938   bool ConsumeFullTypeName(std::string* name) {
939     DO(ConsumeIdentifier(name));
940     while (TryConsume(".")) {
941       std::string part;
942       DO(ConsumeIdentifier(&part));
943       *name += ".";
944       *name += part;
945     }
946     return true;
947   }
948 
ConsumeTypeUrlOrFullTypeName()949   bool ConsumeTypeUrlOrFullTypeName() {
950     std::string discarded;
951     DO(ConsumeIdentifier(&discarded));
952     while (TryConsume(".") || TryConsume("/")) {
953       DO(ConsumeIdentifier(&discarded));
954     }
955     return true;
956   }
957 
958   // Consumes a string and saves its value in the text parameter.
959   // Returns false if the token is not of type STRING.
ConsumeString(std::string * text)960   bool ConsumeString(std::string* text) {
961     if (!LookingAtType(io::Tokenizer::TYPE_STRING)) {
962       ReportError("Expected string, got: " + tokenizer_.current().text);
963       return false;
964     }
965 
966     text->clear();
967     while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
968       io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text);
969 
970       tokenizer_.Next();
971     }
972 
973     return true;
974   }
975 
976   // Consumes a uint64 and saves its value in the value parameter.
977   // Returns false if the token is not of type INTEGER.
ConsumeUnsignedInteger(uint64 * value,uint64 max_value)978   bool ConsumeUnsignedInteger(uint64* value, uint64 max_value) {
979     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
980       ReportError("Expected integer, got: " + tokenizer_.current().text);
981       return false;
982     }
983 
984     if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, max_value,
985                                      value)) {
986       ReportError("Integer out of range (" + tokenizer_.current().text + ")");
987       return false;
988     }
989 
990     tokenizer_.Next();
991     return true;
992   }
993 
994   // Consumes an int64 and saves its value in the value parameter.
995   // Note that since the tokenizer does not support negative numbers,
996   // we actually may consume an additional token (for the minus sign) in this
997   // method. Returns false if the token is not an integer
998   // (signed or otherwise).
ConsumeSignedInteger(int64 * value,uint64 max_value)999   bool ConsumeSignedInteger(int64* value, uint64 max_value) {
1000     bool negative = false;
1001 
1002     if (TryConsume("-")) {
1003       negative = true;
1004       // Two's complement always allows one more negative integer than
1005       // positive.
1006       ++max_value;
1007     }
1008 
1009     uint64 unsigned_value;
1010 
1011     DO(ConsumeUnsignedInteger(&unsigned_value, max_value));
1012 
1013     if (negative) {
1014       if ((static_cast<uint64>(kint64max) + 1) == unsigned_value) {
1015         *value = kint64min;
1016       } else {
1017         *value = -static_cast<int64>(unsigned_value);
1018       }
1019     } else {
1020       *value = static_cast<int64>(unsigned_value);
1021     }
1022 
1023     return true;
1024   }
1025 
1026   // Consumes a double and saves its value in the value parameter.
1027   // Accepts decimal numbers only, rejects hex or oct numbers.
ConsumeUnsignedDecimalAsDouble(double * value,uint64 max_value)1028   bool ConsumeUnsignedDecimalAsDouble(double* value, uint64 max_value) {
1029     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
1030       ReportError("Expected integer, got: " + tokenizer_.current().text);
1031       return false;
1032     }
1033 
1034     const std::string& text = tokenizer_.current().text;
1035     if (IsHexNumber(text) || IsOctNumber(text)) {
1036       ReportError("Expect a decimal number, got: " + text);
1037       return false;
1038     }
1039 
1040     uint64 uint64_value;
1041     if (io::Tokenizer::ParseInteger(text, max_value, &uint64_value)) {
1042       *value = static_cast<double>(uint64_value);
1043     } else {
1044       // Uint64 overflow, attempt to parse as a double instead.
1045       *value = io::Tokenizer::ParseFloat(text);
1046     }
1047 
1048     tokenizer_.Next();
1049     return true;
1050   }
1051 
1052   // Consumes a double and saves its value in the value parameter.
1053   // Note that since the tokenizer does not support negative numbers,
1054   // we actually may consume an additional token (for the minus sign) in this
1055   // method. Returns false if the token is not a double
1056   // (signed or otherwise).
ConsumeDouble(double * value)1057   bool ConsumeDouble(double* value) {
1058     bool negative = false;
1059 
1060     if (TryConsume("-")) {
1061       negative = true;
1062     }
1063 
1064     // A double can actually be an integer, according to the tokenizer.
1065     // Therefore, we must check both cases here.
1066     if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
1067       // We have found an integer value for the double.
1068       DO(ConsumeUnsignedDecimalAsDouble(value, kuint64max));
1069     } else if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) {
1070       // We have found a float value for the double.
1071       *value = io::Tokenizer::ParseFloat(tokenizer_.current().text);
1072 
1073       // Mark the current token as consumed.
1074       tokenizer_.Next();
1075     } else if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
1076       std::string text = tokenizer_.current().text;
1077       LowerString(&text);
1078       if (text == "inf" ||
1079           text == "infinity") {
1080         *value = std::numeric_limits<double>::infinity();
1081         tokenizer_.Next();
1082       } else if (text == "nan") {
1083         *value = std::numeric_limits<double>::quiet_NaN();
1084         tokenizer_.Next();
1085       } else {
1086         ReportError("Expected double, got: " + text);
1087         return false;
1088       }
1089     } else {
1090       ReportError("Expected double, got: " + tokenizer_.current().text);
1091       return false;
1092     }
1093 
1094     if (negative) {
1095       *value = -*value;
1096     }
1097 
1098     return true;
1099   }
1100 
1101   // Consumes Any::type_url value, of form "type.googleapis.com/full.type.Name"
1102   // or "type.googleprod.com/full.type.Name"
ConsumeAnyTypeUrl(std::string * full_type_name,std::string * prefix)1103   bool ConsumeAnyTypeUrl(std::string* full_type_name, std::string* prefix) {
1104     // TODO(saito) Extend Consume() to consume multiple tokens at once, so that
1105     // this code can be written as just DO(Consume(kGoogleApisTypePrefix)).
1106     DO(ConsumeIdentifier(prefix));
1107     while (TryConsume(".")) {
1108       std::string url;
1109       DO(ConsumeIdentifier(&url));
1110       *prefix += "." + url;
1111     }
1112     DO(Consume("/"));
1113     *prefix += "/";
1114     DO(ConsumeFullTypeName(full_type_name));
1115 
1116     return true;
1117   }
1118 
1119   // A helper function for reconstructing Any::value. Consumes a text of
1120   // full_type_name, then serializes it into serialized_value.
ConsumeAnyValue(const Descriptor * value_descriptor,std::string * serialized_value)1121   bool ConsumeAnyValue(const Descriptor* value_descriptor,
1122                        std::string* serialized_value) {
1123     DynamicMessageFactory factory;
1124     const Message* value_prototype = factory.GetPrototype(value_descriptor);
1125     if (value_prototype == nullptr) {
1126       return false;
1127     }
1128     std::unique_ptr<Message> value(value_prototype->New());
1129     std::string sub_delimiter;
1130     DO(ConsumeMessageDelimiter(&sub_delimiter));
1131     DO(ConsumeMessage(value.get(), sub_delimiter));
1132 
1133     if (allow_partial_) {
1134       value->AppendPartialToString(serialized_value);
1135     } else {
1136       if (!value->IsInitialized()) {
1137         ReportError(
1138             "Value of type \"" + value_descriptor->full_name() +
1139             "\" stored in google.protobuf.Any has missing required fields");
1140         return false;
1141       }
1142       value->AppendToString(serialized_value);
1143     }
1144     return true;
1145   }
1146 
1147   // Consumes a token and confirms that it matches that specified in the
1148   // value parameter. Returns false if the token found does not match that
1149   // which was specified.
Consume(const std::string & value)1150   bool Consume(const std::string& value) {
1151     const std::string& current_value = tokenizer_.current().text;
1152 
1153     if (current_value != value) {
1154       ReportError("Expected \"" + value + "\", found \"" + current_value +
1155                   "\".");
1156       return false;
1157     }
1158 
1159     tokenizer_.Next();
1160 
1161     return true;
1162   }
1163 
1164   // Attempts to consume the supplied value. Returns false if a the
1165   // token found does not match the value specified.
TryConsume(const std::string & value)1166   bool TryConsume(const std::string& value) {
1167     if (tokenizer_.current().text == value) {
1168       tokenizer_.Next();
1169       return true;
1170     } else {
1171       return false;
1172     }
1173   }
1174 
1175   // An internal instance of the Tokenizer's error collector, used to
1176   // collect any base-level parse errors and feed them to the ParserImpl.
1177   class ParserErrorCollector : public io::ErrorCollector {
1178    public:
ParserErrorCollector(TextFormat::Parser::ParserImpl * parser)1179     explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser)
1180         : parser_(parser) {}
1181 
~ParserErrorCollector()1182     ~ParserErrorCollector() override {}
1183 
AddError(int line,int column,const std::string & message)1184     void AddError(int line, int column, const std::string& message) override {
1185       parser_->ReportError(line, column, message);
1186     }
1187 
AddWarning(int line,int column,const std::string & message)1188     void AddWarning(int line, int column, const std::string& message) override {
1189       parser_->ReportWarning(line, column, message);
1190     }
1191 
1192    private:
1193     GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserErrorCollector);
1194     TextFormat::Parser::ParserImpl* parser_;
1195   };
1196 
1197   io::ErrorCollector* error_collector_;
1198   const TextFormat::Finder* finder_;
1199   ParseInfoTree* parse_info_tree_;
1200   ParserErrorCollector tokenizer_error_collector_;
1201   io::Tokenizer tokenizer_;
1202   const Descriptor* root_message_type_;
1203   SingularOverwritePolicy singular_overwrite_policy_;
1204   const bool allow_case_insensitive_field_;
1205   const bool allow_unknown_field_;
1206   const bool allow_unknown_extension_;
1207   const bool allow_unknown_enum_;
1208   const bool allow_field_number_;
1209   const bool allow_partial_;
1210   int recursion_limit_;
1211   bool had_errors_;
1212 };
1213 
1214 // ===========================================================================
1215 // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted
1216 // from the Printer found in //net/proto2/io/public/printer.h
1217 class TextFormat::Printer::TextGenerator
1218     : public TextFormat::BaseTextGenerator {
1219  public:
TextGenerator(io::ZeroCopyOutputStream * output,int initial_indent_level)1220   explicit TextGenerator(io::ZeroCopyOutputStream* output,
1221                          int initial_indent_level)
1222       : output_(output),
1223         buffer_(nullptr),
1224         buffer_size_(0),
1225         at_start_of_line_(true),
1226         failed_(false),
1227         indent_level_(initial_indent_level),
1228         initial_indent_level_(initial_indent_level) {}
1229 
~TextGenerator()1230   ~TextGenerator() {
1231     // Only BackUp() if we're sure we've successfully called Next() at least
1232     // once.
1233     if (!failed_ && buffer_size_ > 0) {
1234       output_->BackUp(buffer_size_);
1235     }
1236   }
1237 
1238   // Indent text by two spaces.  After calling Indent(), two spaces will be
1239   // inserted at the beginning of each line of text.  Indent() may be called
1240   // multiple times to produce deeper indents.
Indent()1241   void Indent() override { ++indent_level_; }
1242 
1243   // Reduces the current indent level by two spaces, or crashes if the indent
1244   // level is zero.
Outdent()1245   void Outdent() override {
1246     if (indent_level_ == 0 || indent_level_ < initial_indent_level_) {
1247       GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
1248       return;
1249     }
1250 
1251     --indent_level_;
1252   }
1253 
1254   // Print text to the output stream.
Print(const char * text,size_t size)1255   void Print(const char* text, size_t size) override {
1256     if (indent_level_ > 0) {
1257       size_t pos = 0;  // The number of bytes we've written so far.
1258       for (size_t i = 0; i < size; i++) {
1259         if (text[i] == '\n') {
1260           // Saw newline.  If there is more text, we may need to insert an
1261           // indent here.  So, write what we have so far, including the '\n'.
1262           Write(text + pos, i - pos + 1);
1263           pos = i + 1;
1264 
1265           // Setting this true will cause the next Write() to insert an indent
1266           // first.
1267           at_start_of_line_ = true;
1268         }
1269       }
1270       // Write the rest.
1271       Write(text + pos, size - pos);
1272     } else {
1273       Write(text, size);
1274       if (size > 0 && text[size - 1] == '\n') {
1275         at_start_of_line_ = true;
1276       }
1277     }
1278   }
1279 
1280   // True if any write to the underlying stream failed.  (We don't just
1281   // crash in this case because this is an I/O failure, not a programming
1282   // error.)
failed() const1283   bool failed() const { return failed_; }
1284 
1285  private:
1286   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextGenerator);
1287 
Write(const char * data,size_t size)1288   void Write(const char* data, size_t size) {
1289     if (failed_) return;
1290     if (size == 0) return;
1291 
1292     if (at_start_of_line_) {
1293       // Insert an indent.
1294       at_start_of_line_ = false;
1295       WriteIndent();
1296       if (failed_) return;
1297     }
1298 
1299     while (size > buffer_size_) {
1300       // Data exceeds space in the buffer.  Copy what we can and request a
1301       // new buffer.
1302       if (buffer_size_ > 0) {
1303         memcpy(buffer_, data, buffer_size_);
1304         data += buffer_size_;
1305         size -= buffer_size_;
1306       }
1307       void* void_buffer = nullptr;
1308       failed_ = !output_->Next(&void_buffer, &buffer_size_);
1309       if (failed_) return;
1310       buffer_ = reinterpret_cast<char*>(void_buffer);
1311     }
1312 
1313     // Buffer is big enough to receive the data; copy it.
1314     memcpy(buffer_, data, size);
1315     buffer_ += size;
1316     buffer_size_ -= size;
1317   }
1318 
WriteIndent()1319   void WriteIndent() {
1320     if (indent_level_ == 0) {
1321       return;
1322     }
1323     GOOGLE_DCHECK(!failed_);
1324     int size = 2 * indent_level_;
1325 
1326     while (size > buffer_size_) {
1327       // Data exceeds space in the buffer. Write what we can and request a new
1328       // buffer.
1329       if (buffer_size_ > 0) {
1330         memset(buffer_, ' ', buffer_size_);
1331       }
1332       size -= buffer_size_;
1333       void* void_buffer;
1334       failed_ = !output_->Next(&void_buffer, &buffer_size_);
1335       if (failed_) return;
1336       buffer_ = reinterpret_cast<char*>(void_buffer);
1337     }
1338 
1339     // Buffer is big enough to receive the data; copy it.
1340     memset(buffer_, ' ', size);
1341     buffer_ += size;
1342     buffer_size_ -= size;
1343   }
1344 
1345   io::ZeroCopyOutputStream* const output_;
1346   char* buffer_;
1347   int buffer_size_;
1348   bool at_start_of_line_;
1349   bool failed_;
1350 
1351   int indent_level_;
1352   int initial_indent_level_;
1353 };
1354 
1355 // ===========================================================================
1356 // Implementation of the default Finder for extensions.
~Finder()1357 TextFormat::Finder::~Finder() {}
1358 
FindExtension(Message * message,const std::string & name) const1359 const FieldDescriptor* TextFormat::Finder::FindExtension(
1360     Message* message, const std::string& name) const {
1361   return DefaultFinderFindExtension(message, name);
1362 }
1363 
FindExtensionByNumber(const Descriptor * descriptor,int number) const1364 const FieldDescriptor* TextFormat::Finder::FindExtensionByNumber(
1365     const Descriptor* descriptor, int number) const {
1366   return DefaultFinderFindExtensionByNumber(descriptor, number);
1367 }
1368 
FindAnyType(const Message & message,const std::string & prefix,const std::string & name) const1369 const Descriptor* TextFormat::Finder::FindAnyType(
1370     const Message& message, const std::string& prefix,
1371     const std::string& name) const {
1372   return DefaultFinderFindAnyType(message, prefix, name);
1373 }
1374 
FindExtensionFactory(const FieldDescriptor * field) const1375 MessageFactory* TextFormat::Finder::FindExtensionFactory(
1376     const FieldDescriptor* field) const {
1377   return nullptr;
1378 }
1379 
1380 // ===========================================================================
1381 
Parser()1382 TextFormat::Parser::Parser()
1383     : error_collector_(nullptr),
1384       finder_(nullptr),
1385       parse_info_tree_(nullptr),
1386       allow_partial_(false),
1387       allow_case_insensitive_field_(false),
1388       allow_unknown_field_(false),
1389       allow_unknown_extension_(false),
1390       allow_unknown_enum_(false),
1391       allow_field_number_(false),
1392       allow_relaxed_whitespace_(false),
1393       allow_singular_overwrites_(false),
1394       recursion_limit_(std::numeric_limits<int>::max()) {}
1395 
~Parser()1396 TextFormat::Parser::~Parser() {}
1397 
1398 namespace {
1399 
CheckParseInputSize(StringPiece input,io::ErrorCollector * error_collector)1400 bool CheckParseInputSize(StringPiece input,
1401                          io::ErrorCollector* error_collector) {
1402   if (input.size() > INT_MAX) {
1403     error_collector->AddError(
1404         -1, 0,
1405         StrCat("Input size too large: ", static_cast<int64>(input.size()),
1406                      " bytes", " > ", INT_MAX, " bytes."));
1407     return false;
1408   }
1409   return true;
1410 }
1411 
1412 }  // namespace
1413 
Parse(io::ZeroCopyInputStream * input,Message * output)1414 bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input,
1415                                Message* output) {
1416   output->Clear();
1417 
1418   ParserImpl::SingularOverwritePolicy overwrites_policy =
1419       allow_singular_overwrites_ ? ParserImpl::ALLOW_SINGULAR_OVERWRITES
1420                                  : ParserImpl::FORBID_SINGULAR_OVERWRITES;
1421 
1422   ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_,
1423                     parse_info_tree_, overwrites_policy,
1424                     allow_case_insensitive_field_, allow_unknown_field_,
1425                     allow_unknown_extension_, allow_unknown_enum_,
1426                     allow_field_number_, allow_relaxed_whitespace_,
1427                     allow_partial_, recursion_limit_);
1428   return MergeUsingImpl(input, output, &parser);
1429 }
1430 
ParseFromString(const std::string & input,Message * output)1431 bool TextFormat::Parser::ParseFromString(const std::string& input,
1432                                          Message* output) {
1433   DO(CheckParseInputSize(input, error_collector_));
1434   io::ArrayInputStream input_stream(input.data(), input.size());
1435   return Parse(&input_stream, output);
1436 }
1437 
1438 
Merge(io::ZeroCopyInputStream * input,Message * output)1439 bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input,
1440                                Message* output) {
1441   ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_,
1442                     parse_info_tree_, ParserImpl::ALLOW_SINGULAR_OVERWRITES,
1443                     allow_case_insensitive_field_, allow_unknown_field_,
1444                     allow_unknown_extension_, allow_unknown_enum_,
1445                     allow_field_number_, allow_relaxed_whitespace_,
1446                     allow_partial_, recursion_limit_);
1447   return MergeUsingImpl(input, output, &parser);
1448 }
1449 
MergeFromString(const std::string & input,Message * output)1450 bool TextFormat::Parser::MergeFromString(const std::string& input,
1451                                          Message* output) {
1452   DO(CheckParseInputSize(input, error_collector_));
1453   io::ArrayInputStream input_stream(input.data(), input.size());
1454   return Merge(&input_stream, output);
1455 }
1456 
1457 
MergeUsingImpl(io::ZeroCopyInputStream *,Message * output,ParserImpl * parser_impl)1458 bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */,
1459                                         Message* output,
1460                                         ParserImpl* parser_impl) {
1461   if (!parser_impl->Parse(output)) return false;
1462   if (!allow_partial_ && !output->IsInitialized()) {
1463     std::vector<std::string> missing_fields;
1464     output->FindInitializationErrors(&missing_fields);
1465     parser_impl->ReportError(-1, 0,
1466                              "Message missing required fields: " +
1467                                  Join(missing_fields, ", "));
1468     return false;
1469   }
1470   return true;
1471 }
1472 
ParseFieldValueFromString(const std::string & input,const FieldDescriptor * field,Message * output)1473 bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input,
1474                                                    const FieldDescriptor* field,
1475                                                    Message* output) {
1476   io::ArrayInputStream input_stream(input.data(), input.size());
1477   ParserImpl parser(
1478       output->GetDescriptor(), &input_stream, error_collector_, finder_,
1479       parse_info_tree_, ParserImpl::ALLOW_SINGULAR_OVERWRITES,
1480       allow_case_insensitive_field_, allow_unknown_field_,
1481       allow_unknown_extension_, allow_unknown_enum_, allow_field_number_,
1482       allow_relaxed_whitespace_, allow_partial_, recursion_limit_);
1483   return parser.ParseField(field, output);
1484 }
1485 
Parse(io::ZeroCopyInputStream * input,Message * output)1486 /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input,
1487                                     Message* output) {
1488   return Parser().Parse(input, output);
1489 }
1490 
Merge(io::ZeroCopyInputStream * input,Message * output)1491 /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input,
1492                                     Message* output) {
1493   return Parser().Merge(input, output);
1494 }
1495 
ParseFromString(const std::string & input,Message * output)1496 /* static */ bool TextFormat::ParseFromString(const std::string& input,
1497                                               Message* output) {
1498   return Parser().ParseFromString(input, output);
1499 }
1500 
MergeFromString(const std::string & input,Message * output)1501 /* static */ bool TextFormat::MergeFromString(const std::string& input,
1502                                               Message* output) {
1503   return Parser().MergeFromString(input, output);
1504 }
1505 
1506 
1507 #undef DO
1508 
1509 // ===========================================================================
1510 
~BaseTextGenerator()1511 TextFormat::BaseTextGenerator::~BaseTextGenerator() {}
1512 
1513 namespace {
1514 
1515 // A BaseTextGenerator that writes to a string.
1516 class StringBaseTextGenerator : public TextFormat::BaseTextGenerator {
1517  public:
Print(const char * text,size_t size)1518   void Print(const char* text, size_t size) override {
1519     output_.append(text, size);
1520   }
1521 
1522 // Some compilers do not support ref-qualifiers even in C++11 mode.
1523 // Disable the optimization for now and revisit it later.
1524 #if 0   // LANG_CXX11
1525   std::string Consume() && { return std::move(output_); }
1526 #else   // !LANG_CXX11
Get()1527   const std::string& Get() { return output_; }
1528 #endif  // LANG_CXX11
1529 
1530  private:
1531   std::string output_;
1532 };
1533 
1534 }  // namespace
1535 
1536 // The default implementation for FieldValuePrinter. We just delegate the
1537 // implementation to the default FastFieldValuePrinter to avoid duplicating the
1538 // logic.
FieldValuePrinter()1539 TextFormat::FieldValuePrinter::FieldValuePrinter() {}
~FieldValuePrinter()1540 TextFormat::FieldValuePrinter::~FieldValuePrinter() {}
1541 
1542 #if 0  // LANG_CXX11
1543 #define FORWARD_IMPL(fn, ...)            \
1544   StringBaseTextGenerator generator;     \
1545   delegate_.fn(__VA_ARGS__, &generator); \
1546   return std::move(generator).Consume()
1547 #else  // !LANG_CXX11
1548 #define FORWARD_IMPL(fn, ...)            \
1549   StringBaseTextGenerator generator;     \
1550   delegate_.fn(__VA_ARGS__, &generator); \
1551   return generator.Get()
1552 #endif  // LANG_CXX11
1553 
PrintBool(bool val) const1554 std::string TextFormat::FieldValuePrinter::PrintBool(bool val) const {
1555   FORWARD_IMPL(PrintBool, val);
1556 }
PrintInt32(int32 val) const1557 std::string TextFormat::FieldValuePrinter::PrintInt32(int32 val) const {
1558   FORWARD_IMPL(PrintInt32, val);
1559 }
PrintUInt32(uint32 val) const1560 std::string TextFormat::FieldValuePrinter::PrintUInt32(uint32 val) const {
1561   FORWARD_IMPL(PrintUInt32, val);
1562 }
PrintInt64(int64 val) const1563 std::string TextFormat::FieldValuePrinter::PrintInt64(int64 val) const {
1564   FORWARD_IMPL(PrintInt64, val);
1565 }
PrintUInt64(uint64 val) const1566 std::string TextFormat::FieldValuePrinter::PrintUInt64(uint64 val) const {
1567   FORWARD_IMPL(PrintUInt64, val);
1568 }
PrintFloat(float val) const1569 std::string TextFormat::FieldValuePrinter::PrintFloat(float val) const {
1570   FORWARD_IMPL(PrintFloat, val);
1571 }
PrintDouble(double val) const1572 std::string TextFormat::FieldValuePrinter::PrintDouble(double val) const {
1573   FORWARD_IMPL(PrintDouble, val);
1574 }
PrintString(const std::string & val) const1575 std::string TextFormat::FieldValuePrinter::PrintString(
1576     const std::string& val) const {
1577   FORWARD_IMPL(PrintString, val);
1578 }
PrintBytes(const std::string & val) const1579 std::string TextFormat::FieldValuePrinter::PrintBytes(
1580     const std::string& val) const {
1581   return PrintString(val);
1582 }
PrintEnum(int32 val,const std::string & name) const1583 std::string TextFormat::FieldValuePrinter::PrintEnum(
1584     int32 val, const std::string& name) const {
1585   FORWARD_IMPL(PrintEnum, val, name);
1586 }
PrintFieldName(const Message & message,const Reflection * reflection,const FieldDescriptor * field) const1587 std::string TextFormat::FieldValuePrinter::PrintFieldName(
1588     const Message& message, const Reflection* reflection,
1589     const FieldDescriptor* field) const {
1590   FORWARD_IMPL(PrintFieldName, message, reflection, field);
1591 }
PrintMessageStart(const Message & message,int field_index,int field_count,bool single_line_mode) const1592 std::string TextFormat::FieldValuePrinter::PrintMessageStart(
1593     const Message& message, int field_index, int field_count,
1594     bool single_line_mode) const {
1595   FORWARD_IMPL(PrintMessageStart, message, field_index, field_count,
1596                single_line_mode);
1597 }
PrintMessageEnd(const Message & message,int field_index,int field_count,bool single_line_mode) const1598 std::string TextFormat::FieldValuePrinter::PrintMessageEnd(
1599     const Message& message, int field_index, int field_count,
1600     bool single_line_mode) const {
1601   FORWARD_IMPL(PrintMessageEnd, message, field_index, field_count,
1602                single_line_mode);
1603 }
1604 #undef FORWARD_IMPL
1605 
FastFieldValuePrinter()1606 TextFormat::FastFieldValuePrinter::FastFieldValuePrinter() {}
~FastFieldValuePrinter()1607 TextFormat::FastFieldValuePrinter::~FastFieldValuePrinter() {}
PrintBool(bool val,BaseTextGenerator * generator) const1608 void TextFormat::FastFieldValuePrinter::PrintBool(
1609     bool val, BaseTextGenerator* generator) const {
1610   if (val) {
1611     generator->PrintLiteral("true");
1612   } else {
1613     generator->PrintLiteral("false");
1614   }
1615 }
PrintInt32(int32 val,BaseTextGenerator * generator) const1616 void TextFormat::FastFieldValuePrinter::PrintInt32(
1617     int32 val, BaseTextGenerator* generator) const {
1618   generator->PrintString(StrCat(val));
1619 }
PrintUInt32(uint32 val,BaseTextGenerator * generator) const1620 void TextFormat::FastFieldValuePrinter::PrintUInt32(
1621     uint32 val, BaseTextGenerator* generator) const {
1622   generator->PrintString(StrCat(val));
1623 }
PrintInt64(int64 val,BaseTextGenerator * generator) const1624 void TextFormat::FastFieldValuePrinter::PrintInt64(
1625     int64 val, BaseTextGenerator* generator) const {
1626   generator->PrintString(StrCat(val));
1627 }
PrintUInt64(uint64 val,BaseTextGenerator * generator) const1628 void TextFormat::FastFieldValuePrinter::PrintUInt64(
1629     uint64 val, BaseTextGenerator* generator) const {
1630   generator->PrintString(StrCat(val));
1631 }
PrintFloat(float val,BaseTextGenerator * generator) const1632 void TextFormat::FastFieldValuePrinter::PrintFloat(
1633     float val, BaseTextGenerator* generator) const {
1634   generator->PrintString(SimpleFtoa(val));
1635 }
PrintDouble(double val,BaseTextGenerator * generator) const1636 void TextFormat::FastFieldValuePrinter::PrintDouble(
1637     double val, BaseTextGenerator* generator) const {
1638   generator->PrintString(SimpleDtoa(val));
1639 }
PrintEnum(int32 val,const std::string & name,BaseTextGenerator * generator) const1640 void TextFormat::FastFieldValuePrinter::PrintEnum(
1641     int32 val, const std::string& name, BaseTextGenerator* generator) const {
1642   generator->PrintString(name);
1643 }
1644 
PrintString(const std::string & val,BaseTextGenerator * generator) const1645 void TextFormat::FastFieldValuePrinter::PrintString(
1646     const std::string& val, BaseTextGenerator* generator) const {
1647   generator->PrintLiteral("\"");
1648   generator->PrintString(CEscape(val));
1649   generator->PrintLiteral("\"");
1650 }
PrintBytes(const std::string & val,BaseTextGenerator * generator) const1651 void TextFormat::FastFieldValuePrinter::PrintBytes(
1652     const std::string& val, BaseTextGenerator* generator) const {
1653   PrintString(val, generator);
1654 }
PrintFieldName(const Message & message,int field_index,int field_count,const Reflection * reflection,const FieldDescriptor * field,BaseTextGenerator * generator) const1655 void TextFormat::FastFieldValuePrinter::PrintFieldName(
1656     const Message& message, int field_index, int field_count,
1657     const Reflection* reflection, const FieldDescriptor* field,
1658     BaseTextGenerator* generator) const {
1659   PrintFieldName(message, reflection, field, generator);
1660 }
PrintFieldName(const Message & message,const Reflection * reflection,const FieldDescriptor * field,BaseTextGenerator * generator) const1661 void TextFormat::FastFieldValuePrinter::PrintFieldName(
1662     const Message& message, const Reflection* reflection,
1663     const FieldDescriptor* field, BaseTextGenerator* generator) const {
1664   if (field->is_extension()) {
1665     generator->PrintLiteral("[");
1666     generator->PrintString(field->PrintableNameForExtension());
1667     generator->PrintLiteral("]");
1668   } else if (field->type() == FieldDescriptor::TYPE_GROUP) {
1669     // Groups must be serialized with their original capitalization.
1670     generator->PrintString(field->message_type()->name());
1671   } else {
1672     generator->PrintString(field->name());
1673   }
1674 }
PrintMessageStart(const Message & message,int field_index,int field_count,bool single_line_mode,BaseTextGenerator * generator) const1675 void TextFormat::FastFieldValuePrinter::PrintMessageStart(
1676     const Message& message, int field_index, int field_count,
1677     bool single_line_mode, BaseTextGenerator* generator) const {
1678   if (single_line_mode) {
1679     generator->PrintLiteral(" { ");
1680   } else {
1681     generator->PrintLiteral(" {\n");
1682   }
1683 }
PrintMessageEnd(const Message & message,int field_index,int field_count,bool single_line_mode,BaseTextGenerator * generator) const1684 void TextFormat::FastFieldValuePrinter::PrintMessageEnd(
1685     const Message& message, int field_index, int field_count,
1686     bool single_line_mode, BaseTextGenerator* generator) const {
1687   if (single_line_mode) {
1688     generator->PrintLiteral("} ");
1689   } else {
1690     generator->PrintLiteral("}\n");
1691   }
1692 }
1693 
1694 namespace {
1695 
1696 // A legacy compatibility wrapper. Takes ownership of the delegate.
1697 class FieldValuePrinterWrapper : public TextFormat::FastFieldValuePrinter {
1698  public:
FieldValuePrinterWrapper(const TextFormat::FieldValuePrinter * delegate)1699   explicit FieldValuePrinterWrapper(
1700       const TextFormat::FieldValuePrinter* delegate)
1701       : delegate_(delegate) {}
1702 
SetDelegate(const TextFormat::FieldValuePrinter * delegate)1703   void SetDelegate(const TextFormat::FieldValuePrinter* delegate) {
1704     delegate_.reset(delegate);
1705   }
1706 
PrintBool(bool val,TextFormat::BaseTextGenerator * generator) const1707   void PrintBool(bool val,
1708                  TextFormat::BaseTextGenerator* generator) const override {
1709     generator->PrintString(delegate_->PrintBool(val));
1710   }
PrintInt32(int32 val,TextFormat::BaseTextGenerator * generator) const1711   void PrintInt32(int32 val,
1712                   TextFormat::BaseTextGenerator* generator) const override {
1713     generator->PrintString(delegate_->PrintInt32(val));
1714   }
PrintUInt32(uint32 val,TextFormat::BaseTextGenerator * generator) const1715   void PrintUInt32(uint32 val,
1716                    TextFormat::BaseTextGenerator* generator) const override {
1717     generator->PrintString(delegate_->PrintUInt32(val));
1718   }
PrintInt64(int64 val,TextFormat::BaseTextGenerator * generator) const1719   void PrintInt64(int64 val,
1720                   TextFormat::BaseTextGenerator* generator) const override {
1721     generator->PrintString(delegate_->PrintInt64(val));
1722   }
PrintUInt64(uint64 val,TextFormat::BaseTextGenerator * generator) const1723   void PrintUInt64(uint64 val,
1724                    TextFormat::BaseTextGenerator* generator) const override {
1725     generator->PrintString(delegate_->PrintUInt64(val));
1726   }
PrintFloat(float val,TextFormat::BaseTextGenerator * generator) const1727   void PrintFloat(float val,
1728                   TextFormat::BaseTextGenerator* generator) const override {
1729     generator->PrintString(delegate_->PrintFloat(val));
1730   }
PrintDouble(double val,TextFormat::BaseTextGenerator * generator) const1731   void PrintDouble(double val,
1732                    TextFormat::BaseTextGenerator* generator) const override {
1733     generator->PrintString(delegate_->PrintDouble(val));
1734   }
PrintString(const std::string & val,TextFormat::BaseTextGenerator * generator) const1735   void PrintString(const std::string& val,
1736                    TextFormat::BaseTextGenerator* generator) const override {
1737     generator->PrintString(delegate_->PrintString(val));
1738   }
PrintBytes(const std::string & val,TextFormat::BaseTextGenerator * generator) const1739   void PrintBytes(const std::string& val,
1740                   TextFormat::BaseTextGenerator* generator) const override {
1741     generator->PrintString(delegate_->PrintBytes(val));
1742   }
PrintEnum(int32 val,const std::string & name,TextFormat::BaseTextGenerator * generator) const1743   void PrintEnum(int32 val, const std::string& name,
1744                  TextFormat::BaseTextGenerator* generator) const override {
1745     generator->PrintString(delegate_->PrintEnum(val, name));
1746   }
PrintFieldName(const Message & message,int field_index,int field_count,const Reflection * reflection,const FieldDescriptor * field,TextFormat::BaseTextGenerator * generator) const1747   void PrintFieldName(const Message& message, int field_index, int field_count,
1748                       const Reflection* reflection,
1749                       const FieldDescriptor* field,
1750                       TextFormat::BaseTextGenerator* generator) const override {
1751     generator->PrintString(
1752         delegate_->PrintFieldName(message, reflection, field));
1753   }
PrintFieldName(const Message & message,const Reflection * reflection,const FieldDescriptor * field,TextFormat::BaseTextGenerator * generator) const1754   void PrintFieldName(const Message& message, const Reflection* reflection,
1755                       const FieldDescriptor* field,
1756                       TextFormat::BaseTextGenerator* generator) const override {
1757     generator->PrintString(
1758         delegate_->PrintFieldName(message, reflection, field));
1759   }
PrintMessageStart(const Message & message,int field_index,int field_count,bool single_line_mode,TextFormat::BaseTextGenerator * generator) const1760   void PrintMessageStart(
1761       const Message& message, int field_index, int field_count,
1762       bool single_line_mode,
1763       TextFormat::BaseTextGenerator* generator) const override {
1764     generator->PrintString(delegate_->PrintMessageStart(
1765         message, field_index, field_count, single_line_mode));
1766   }
PrintMessageEnd(const Message & message,int field_index,int field_count,bool single_line_mode,TextFormat::BaseTextGenerator * generator) const1767   void PrintMessageEnd(
1768       const Message& message, int field_index, int field_count,
1769       bool single_line_mode,
1770       TextFormat::BaseTextGenerator* generator) const override {
1771     generator->PrintString(delegate_->PrintMessageEnd(
1772         message, field_index, field_count, single_line_mode));
1773   }
1774 
1775  private:
1776   std::unique_ptr<const TextFormat::FieldValuePrinter> delegate_;
1777 };
1778 
1779 // Our own specialization: for UTF8 escaped strings.
1780 class FastFieldValuePrinterUtf8Escaping
1781     : public TextFormat::FastFieldValuePrinter {
1782  public:
PrintString(const std::string & val,TextFormat::BaseTextGenerator * generator) const1783   void PrintString(const std::string& val,
1784                    TextFormat::BaseTextGenerator* generator) const override {
1785     generator->PrintLiteral("\"");
1786     generator->PrintString(strings::Utf8SafeCEscape(val));
1787     generator->PrintLiteral("\"");
1788   }
PrintBytes(const std::string & val,TextFormat::BaseTextGenerator * generator) const1789   void PrintBytes(const std::string& val,
1790                   TextFormat::BaseTextGenerator* generator) const override {
1791     return FastFieldValuePrinter::PrintString(val, generator);
1792   }
1793 };
1794 
1795 }  // namespace
1796 
Printer()1797 TextFormat::Printer::Printer()
1798     : initial_indent_level_(0),
1799       single_line_mode_(false),
1800       use_field_number_(false),
1801       use_short_repeated_primitives_(false),
1802       hide_unknown_fields_(false),
1803       print_message_fields_in_index_order_(false),
1804       expand_any_(false),
1805       truncate_string_field_longer_than_(0LL),
1806       finder_(nullptr) {
1807   SetUseUtf8StringEscaping(false);
1808 }
1809 
~Printer()1810 TextFormat::Printer::~Printer() {
1811   STLDeleteValues(&custom_printers_);
1812   STLDeleteValues(&custom_message_printers_);
1813 }
1814 
SetUseUtf8StringEscaping(bool as_utf8)1815 void TextFormat::Printer::SetUseUtf8StringEscaping(bool as_utf8) {
1816   SetDefaultFieldValuePrinter(as_utf8 ? new FastFieldValuePrinterUtf8Escaping()
1817                                       : new FastFieldValuePrinter());
1818 }
1819 
SetDefaultFieldValuePrinter(const FieldValuePrinter * printer)1820 void TextFormat::Printer::SetDefaultFieldValuePrinter(
1821     const FieldValuePrinter* printer) {
1822   default_field_value_printer_.reset(new FieldValuePrinterWrapper(printer));
1823 }
1824 
SetDefaultFieldValuePrinter(const FastFieldValuePrinter * printer)1825 void TextFormat::Printer::SetDefaultFieldValuePrinter(
1826     const FastFieldValuePrinter* printer) {
1827   default_field_value_printer_.reset(printer);
1828 }
1829 
RegisterFieldValuePrinter(const FieldDescriptor * field,const FieldValuePrinter * printer)1830 bool TextFormat::Printer::RegisterFieldValuePrinter(
1831     const FieldDescriptor* field, const FieldValuePrinter* printer) {
1832   if (field == nullptr || printer == nullptr) {
1833     return false;
1834   }
1835   FieldValuePrinterWrapper* const wrapper =
1836       new FieldValuePrinterWrapper(nullptr);
1837   if (custom_printers_.insert(std::make_pair(field, wrapper)).second) {
1838     wrapper->SetDelegate(printer);
1839     return true;
1840   } else {
1841     delete wrapper;
1842     return false;
1843   }
1844 }
1845 
RegisterFieldValuePrinter(const FieldDescriptor * field,const FastFieldValuePrinter * printer)1846 bool TextFormat::Printer::RegisterFieldValuePrinter(
1847     const FieldDescriptor* field, const FastFieldValuePrinter* printer) {
1848   return field != nullptr && printer != nullptr &&
1849          custom_printers_.insert(std::make_pair(field, printer)).second;
1850 }
1851 
RegisterMessagePrinter(const Descriptor * descriptor,const MessagePrinter * printer)1852 bool TextFormat::Printer::RegisterMessagePrinter(
1853     const Descriptor* descriptor, const MessagePrinter* printer) {
1854   return descriptor != nullptr && printer != nullptr &&
1855          custom_message_printers_.insert(std::make_pair(descriptor, printer))
1856              .second;
1857 }
1858 
PrintToString(const Message & message,std::string * output) const1859 bool TextFormat::Printer::PrintToString(const Message& message,
1860                                         std::string* output) const {
1861   GOOGLE_DCHECK(output) << "output specified is nullptr";
1862 
1863   output->clear();
1864   io::StringOutputStream output_stream(output);
1865 
1866   return Print(message, &output_stream);
1867 }
1868 
PrintUnknownFieldsToString(const UnknownFieldSet & unknown_fields,std::string * output) const1869 bool TextFormat::Printer::PrintUnknownFieldsToString(
1870     const UnknownFieldSet& unknown_fields, std::string* output) const {
1871   GOOGLE_DCHECK(output) << "output specified is nullptr";
1872 
1873   output->clear();
1874   io::StringOutputStream output_stream(output);
1875   return PrintUnknownFields(unknown_fields, &output_stream);
1876 }
1877 
Print(const Message & message,io::ZeroCopyOutputStream * output) const1878 bool TextFormat::Printer::Print(const Message& message,
1879                                 io::ZeroCopyOutputStream* output) const {
1880   TextGenerator generator(output, initial_indent_level_);
1881 
1882   Print(message, &generator);
1883 
1884   // Output false if the generator failed internally.
1885   return !generator.failed();
1886 }
1887 
PrintUnknownFields(const UnknownFieldSet & unknown_fields,io::ZeroCopyOutputStream * output) const1888 bool TextFormat::Printer::PrintUnknownFields(
1889     const UnknownFieldSet& unknown_fields,
1890     io::ZeroCopyOutputStream* output) const {
1891   TextGenerator generator(output, initial_indent_level_);
1892 
1893   PrintUnknownFields(unknown_fields, &generator);
1894 
1895   // Output false if the generator failed internally.
1896   return !generator.failed();
1897 }
1898 
1899 namespace {
1900 // Comparison functor for sorting FieldDescriptors by field index.
1901 // Normal fields have higher precedence than extensions.
1902 struct FieldIndexSorter {
operator ()google::protobuf::__anon54141f600611::FieldIndexSorter1903   bool operator()(const FieldDescriptor* left,
1904                   const FieldDescriptor* right) const {
1905     if (left->is_extension() && right->is_extension()) {
1906       return left->number() < right->number();
1907     } else if (left->is_extension()) {
1908       return false;
1909     } else if (right->is_extension()) {
1910       return true;
1911     } else {
1912       return left->index() < right->index();
1913     }
1914   }
1915 };
1916 
1917 }  // namespace
1918 
PrintAny(const Message & message,TextGenerator * generator) const1919 bool TextFormat::Printer::PrintAny(const Message& message,
1920                                    TextGenerator* generator) const {
1921   const FieldDescriptor* type_url_field;
1922   const FieldDescriptor* value_field;
1923   if (!internal::GetAnyFieldDescriptors(message, &type_url_field,
1924                                         &value_field)) {
1925     return false;
1926   }
1927 
1928   const Reflection* reflection = message.GetReflection();
1929 
1930   // Extract the full type name from the type_url field.
1931   const std::string& type_url = reflection->GetString(message, type_url_field);
1932   std::string url_prefix;
1933   std::string full_type_name;
1934   if (!internal::ParseAnyTypeUrl(type_url, &url_prefix, &full_type_name)) {
1935     return false;
1936   }
1937 
1938   // Print the "value" in text.
1939   const Descriptor* value_descriptor =
1940       finder_ ? finder_->FindAnyType(message, url_prefix, full_type_name)
1941               : DefaultFinderFindAnyType(message, url_prefix, full_type_name);
1942   if (value_descriptor == nullptr) {
1943     GOOGLE_LOG(WARNING) << "Proto type " << type_url << " not found";
1944     return false;
1945   }
1946   DynamicMessageFactory factory;
1947   std::unique_ptr<Message> value_message(
1948       factory.GetPrototype(value_descriptor)->New());
1949   std::string serialized_value = reflection->GetString(message, value_field);
1950   if (!value_message->ParseFromString(serialized_value)) {
1951     GOOGLE_LOG(WARNING) << type_url << ": failed to parse contents";
1952     return false;
1953   }
1954   generator->PrintLiteral("[");
1955   generator->PrintString(type_url);
1956   generator->PrintLiteral("]");
1957   const FastFieldValuePrinter* printer = FindWithDefault(
1958       custom_printers_, value_field, default_field_value_printer_.get());
1959   printer->PrintMessageStart(message, -1, 0, single_line_mode_, generator);
1960   generator->Indent();
1961   Print(*value_message, generator);
1962   generator->Outdent();
1963   printer->PrintMessageEnd(message, -1, 0, single_line_mode_, generator);
1964   return true;
1965 }
1966 
Print(const Message & message,TextGenerator * generator) const1967 void TextFormat::Printer::Print(const Message& message,
1968                                 TextGenerator* generator) const {
1969   const Reflection* reflection = message.GetReflection();
1970   if (!reflection) {
1971     // This message does not provide any way to describe its structure.
1972     // Parse it again in an UnknownFieldSet, and display this instead.
1973     UnknownFieldSet unknown_fields;
1974     {
1975       std::string serialized = message.SerializeAsString();
1976       io::ArrayInputStream input(serialized.data(), serialized.size());
1977       unknown_fields.ParseFromZeroCopyStream(&input);
1978     }
1979     PrintUnknownFields(unknown_fields, generator);
1980     return;
1981   }
1982   const Descriptor* descriptor = message.GetDescriptor();
1983   auto itr = custom_message_printers_.find(descriptor);
1984   if (itr != custom_message_printers_.end()) {
1985     itr->second->Print(message, single_line_mode_, generator);
1986     return;
1987   }
1988   if (descriptor->full_name() == internal::kAnyFullTypeName && expand_any_ &&
1989       PrintAny(message, generator)) {
1990     return;
1991   }
1992   std::vector<const FieldDescriptor*> fields;
1993   if (descriptor->options().map_entry()) {
1994     fields.push_back(descriptor->field(0));
1995     fields.push_back(descriptor->field(1));
1996   } else {
1997     reflection->ListFields(message, &fields);
1998   }
1999 
2000   if (print_message_fields_in_index_order_) {
2001     std::sort(fields.begin(), fields.end(), FieldIndexSorter());
2002   }
2003   for (int i = 0; i < fields.size(); i++) {
2004     PrintField(message, reflection, fields[i], generator);
2005   }
2006   if (!hide_unknown_fields_) {
2007     PrintUnknownFields(reflection->GetUnknownFields(message), generator);
2008   }
2009 }
2010 
PrintFieldValueToString(const Message & message,const FieldDescriptor * field,int index,std::string * output) const2011 void TextFormat::Printer::PrintFieldValueToString(const Message& message,
2012                                                   const FieldDescriptor* field,
2013                                                   int index,
2014                                                   std::string* output) const {
2015   GOOGLE_DCHECK(output) << "output specified is nullptr";
2016 
2017   output->clear();
2018   io::StringOutputStream output_stream(output);
2019   TextGenerator generator(&output_stream, initial_indent_level_);
2020 
2021   PrintFieldValue(message, message.GetReflection(), field, index, &generator);
2022 }
2023 
2024 class MapEntryMessageComparator {
2025  public:
MapEntryMessageComparator(const Descriptor * descriptor)2026   explicit MapEntryMessageComparator(const Descriptor* descriptor)
2027       : field_(descriptor->field(0)) {}
2028 
operator ()(const Message * a,const Message * b)2029   bool operator()(const Message* a, const Message* b) {
2030     const Reflection* reflection = a->GetReflection();
2031     switch (field_->cpp_type()) {
2032       case FieldDescriptor::CPPTYPE_BOOL: {
2033         bool first = reflection->GetBool(*a, field_);
2034         bool second = reflection->GetBool(*b, field_);
2035         return first < second;
2036       }
2037       case FieldDescriptor::CPPTYPE_INT32: {
2038         int32 first = reflection->GetInt32(*a, field_);
2039         int32 second = reflection->GetInt32(*b, field_);
2040         return first < second;
2041       }
2042       case FieldDescriptor::CPPTYPE_INT64: {
2043         int64 first = reflection->GetInt64(*a, field_);
2044         int64 second = reflection->GetInt64(*b, field_);
2045         return first < second;
2046       }
2047       case FieldDescriptor::CPPTYPE_UINT32: {
2048         uint32 first = reflection->GetUInt32(*a, field_);
2049         uint32 second = reflection->GetUInt32(*b, field_);
2050         return first < second;
2051       }
2052       case FieldDescriptor::CPPTYPE_UINT64: {
2053         uint64 first = reflection->GetUInt64(*a, field_);
2054         uint64 second = reflection->GetUInt64(*b, field_);
2055         return first < second;
2056       }
2057       case FieldDescriptor::CPPTYPE_STRING: {
2058         std::string first = reflection->GetString(*a, field_);
2059         std::string second = reflection->GetString(*b, field_);
2060         return first < second;
2061       }
2062       default:
2063         GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
2064         return true;
2065     }
2066   }
2067 
2068  private:
2069   const FieldDescriptor* field_;
2070 };
2071 
2072 namespace internal {
2073 class MapFieldPrinterHelper {
2074  public:
2075   // DynamicMapSorter::Sort cannot be used because it enfores syncing with
2076   // repeated field.
2077   static bool SortMap(const Message& message, const Reflection* reflection,
2078                       const FieldDescriptor* field, MessageFactory* factory,
2079                       std::vector<const Message*>* sorted_map_field);
2080   static void CopyKey(const MapKey& key, Message* message,
2081                       const FieldDescriptor* field_desc);
2082   static void CopyValue(const MapValueRef& value, Message* message,
2083                         const FieldDescriptor* field_desc);
2084 };
2085 
2086 // Returns true if elements contained in sorted_map_field need to be released.
SortMap(const Message & message,const Reflection * reflection,const FieldDescriptor * field,MessageFactory * factory,std::vector<const Message * > * sorted_map_field)2087 bool MapFieldPrinterHelper::SortMap(
2088     const Message& message, const Reflection* reflection,
2089     const FieldDescriptor* field, MessageFactory* factory,
2090     std::vector<const Message*>* sorted_map_field) {
2091   bool need_release = false;
2092   const MapFieldBase& base = *reflection->GetMapData(message, field);
2093 
2094   if (base.IsRepeatedFieldValid()) {
2095     const RepeatedPtrField<Message>& map_field =
2096         reflection->GetRepeatedPtrField<Message>(message, field);
2097     for (int i = 0; i < map_field.size(); ++i) {
2098       sorted_map_field->push_back(
2099           const_cast<RepeatedPtrField<Message>*>(&map_field)->Mutable(i));
2100     }
2101   } else {
2102     // TODO(teboring): For performance, instead of creating map entry message
2103     // for each element, just store map keys and sort them.
2104     const Descriptor* map_entry_desc = field->message_type();
2105     const Message* prototype = factory->GetPrototype(map_entry_desc);
2106     for (MapIterator iter =
2107              reflection->MapBegin(const_cast<Message*>(&message), field);
2108          iter != reflection->MapEnd(const_cast<Message*>(&message), field);
2109          ++iter) {
2110       Message* map_entry_message = prototype->New();
2111       CopyKey(iter.GetKey(), map_entry_message, map_entry_desc->field(0));
2112       CopyValue(iter.GetValueRef(), map_entry_message,
2113                 map_entry_desc->field(1));
2114       sorted_map_field->push_back(map_entry_message);
2115     }
2116     need_release = true;
2117   }
2118 
2119   MapEntryMessageComparator comparator(field->message_type());
2120   std::stable_sort(sorted_map_field->begin(), sorted_map_field->end(),
2121                    comparator);
2122   return need_release;
2123 }
2124 
CopyKey(const MapKey & key,Message * message,const FieldDescriptor * field_desc)2125 void MapFieldPrinterHelper::CopyKey(const MapKey& key, Message* message,
2126                                     const FieldDescriptor* field_desc) {
2127   const Reflection* reflection = message->GetReflection();
2128   switch (field_desc->cpp_type()) {
2129     case FieldDescriptor::CPPTYPE_DOUBLE:
2130     case FieldDescriptor::CPPTYPE_FLOAT:
2131     case FieldDescriptor::CPPTYPE_ENUM:
2132     case FieldDescriptor::CPPTYPE_MESSAGE:
2133       GOOGLE_LOG(ERROR) << "Not supported.";
2134       break;
2135     case FieldDescriptor::CPPTYPE_STRING:
2136       reflection->SetString(message, field_desc, key.GetStringValue());
2137       return;
2138     case FieldDescriptor::CPPTYPE_INT64:
2139       reflection->SetInt64(message, field_desc, key.GetInt64Value());
2140       return;
2141     case FieldDescriptor::CPPTYPE_INT32:
2142       reflection->SetInt32(message, field_desc, key.GetInt32Value());
2143       return;
2144     case FieldDescriptor::CPPTYPE_UINT64:
2145       reflection->SetUInt64(message, field_desc, key.GetUInt64Value());
2146       return;
2147     case FieldDescriptor::CPPTYPE_UINT32:
2148       reflection->SetUInt32(message, field_desc, key.GetUInt32Value());
2149       return;
2150     case FieldDescriptor::CPPTYPE_BOOL:
2151       reflection->SetBool(message, field_desc, key.GetBoolValue());
2152       return;
2153   }
2154 }
2155 
CopyValue(const MapValueRef & value,Message * message,const FieldDescriptor * field_desc)2156 void MapFieldPrinterHelper::CopyValue(const MapValueRef& value,
2157                                       Message* message,
2158                                       const FieldDescriptor* field_desc) {
2159   const Reflection* reflection = message->GetReflection();
2160   switch (field_desc->cpp_type()) {
2161     case FieldDescriptor::CPPTYPE_DOUBLE:
2162       reflection->SetDouble(message, field_desc, value.GetDoubleValue());
2163       return;
2164     case FieldDescriptor::CPPTYPE_FLOAT:
2165       reflection->SetFloat(message, field_desc, value.GetFloatValue());
2166       return;
2167     case FieldDescriptor::CPPTYPE_ENUM:
2168       reflection->SetEnumValue(message, field_desc, value.GetEnumValue());
2169       return;
2170     case FieldDescriptor::CPPTYPE_MESSAGE: {
2171       Message* sub_message = value.GetMessageValue().New();
2172       sub_message->CopyFrom(value.GetMessageValue());
2173       reflection->SetAllocatedMessage(message, sub_message, field_desc);
2174       return;
2175     }
2176     case FieldDescriptor::CPPTYPE_STRING:
2177       reflection->SetString(message, field_desc, value.GetStringValue());
2178       return;
2179     case FieldDescriptor::CPPTYPE_INT64:
2180       reflection->SetInt64(message, field_desc, value.GetInt64Value());
2181       return;
2182     case FieldDescriptor::CPPTYPE_INT32:
2183       reflection->SetInt32(message, field_desc, value.GetInt32Value());
2184       return;
2185     case FieldDescriptor::CPPTYPE_UINT64:
2186       reflection->SetUInt64(message, field_desc, value.GetUInt64Value());
2187       return;
2188     case FieldDescriptor::CPPTYPE_UINT32:
2189       reflection->SetUInt32(message, field_desc, value.GetUInt32Value());
2190       return;
2191     case FieldDescriptor::CPPTYPE_BOOL:
2192       reflection->SetBool(message, field_desc, value.GetBoolValue());
2193       return;
2194   }
2195 }
2196 }  // namespace internal
2197 
PrintField(const Message & message,const Reflection * reflection,const FieldDescriptor * field,TextGenerator * generator) const2198 void TextFormat::Printer::PrintField(const Message& message,
2199                                      const Reflection* reflection,
2200                                      const FieldDescriptor* field,
2201                                      TextGenerator* generator) const {
2202   if (use_short_repeated_primitives_ && field->is_repeated() &&
2203       field->cpp_type() != FieldDescriptor::CPPTYPE_STRING &&
2204       field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
2205     PrintShortRepeatedField(message, reflection, field, generator);
2206     return;
2207   }
2208 
2209   int count = 0;
2210 
2211   if (field->is_repeated()) {
2212     count = reflection->FieldSize(message, field);
2213   } else if (reflection->HasField(message, field) ||
2214              field->containing_type()->options().map_entry()) {
2215     count = 1;
2216   }
2217 
2218   DynamicMessageFactory factory;
2219   std::vector<const Message*> sorted_map_field;
2220   bool need_release = false;
2221   bool is_map = field->is_map();
2222   if (is_map) {
2223     need_release = internal::MapFieldPrinterHelper::SortMap(
2224         message, reflection, field, &factory, &sorted_map_field);
2225   }
2226 
2227   for (int j = 0; j < count; ++j) {
2228     const int field_index = field->is_repeated() ? j : -1;
2229 
2230     PrintFieldName(message, field_index, count, reflection, field, generator);
2231 
2232     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
2233       const FastFieldValuePrinter* printer = FindWithDefault(
2234           custom_printers_, field, default_field_value_printer_.get());
2235       const Message& sub_message =
2236           field->is_repeated()
2237               ? (is_map ? *sorted_map_field[j]
2238                         : reflection->GetRepeatedMessage(message, field, j))
2239               : reflection->GetMessage(message, field);
2240       printer->PrintMessageStart(sub_message, field_index, count,
2241                                  single_line_mode_, generator);
2242       generator->Indent();
2243       Print(sub_message, generator);
2244       generator->Outdent();
2245       printer->PrintMessageEnd(sub_message, field_index, count,
2246                                single_line_mode_, generator);
2247     } else {
2248       generator->PrintLiteral(": ");
2249       // Write the field value.
2250       PrintFieldValue(message, reflection, field, field_index, generator);
2251       if (single_line_mode_) {
2252         generator->PrintLiteral(" ");
2253       } else {
2254         generator->PrintLiteral("\n");
2255       }
2256     }
2257   }
2258 
2259   if (need_release) {
2260     for (int j = 0; j < sorted_map_field.size(); ++j) {
2261       delete sorted_map_field[j];
2262     }
2263   }
2264 }
2265 
PrintShortRepeatedField(const Message & message,const Reflection * reflection,const FieldDescriptor * field,TextGenerator * generator) const2266 void TextFormat::Printer::PrintShortRepeatedField(
2267     const Message& message, const Reflection* reflection,
2268     const FieldDescriptor* field, TextGenerator* generator) const {
2269   // Print primitive repeated field in short form.
2270   int size = reflection->FieldSize(message, field);
2271   PrintFieldName(message, /*field_index=*/-1, /*field_count=*/size, reflection,
2272                  field, generator);
2273   generator->PrintLiteral(": [");
2274   for (int i = 0; i < size; i++) {
2275     if (i > 0) generator->PrintLiteral(", ");
2276     PrintFieldValue(message, reflection, field, i, generator);
2277   }
2278   if (single_line_mode_) {
2279     generator->PrintLiteral("] ");
2280   } else {
2281     generator->PrintLiteral("]\n");
2282   }
2283 }
2284 
PrintFieldName(const Message & message,int field_index,int field_count,const Reflection * reflection,const FieldDescriptor * field,TextGenerator * generator) const2285 void TextFormat::Printer::PrintFieldName(const Message& message,
2286                                          int field_index, int field_count,
2287                                          const Reflection* reflection,
2288                                          const FieldDescriptor* field,
2289                                          TextGenerator* generator) const {
2290   // if use_field_number_ is true, prints field number instead
2291   // of field name.
2292   if (use_field_number_) {
2293     generator->PrintString(StrCat(field->number()));
2294     return;
2295   }
2296 
2297   const FastFieldValuePrinter* printer = FindWithDefault(
2298       custom_printers_, field, default_field_value_printer_.get());
2299   printer->PrintFieldName(message, field_index, field_count, reflection, field,
2300                           generator);
2301 }
2302 
PrintFieldValue(const Message & message,const Reflection * reflection,const FieldDescriptor * field,int index,TextGenerator * generator) const2303 void TextFormat::Printer::PrintFieldValue(const Message& message,
2304                                           const Reflection* reflection,
2305                                           const FieldDescriptor* field,
2306                                           int index,
2307                                           TextGenerator* generator) const {
2308   GOOGLE_DCHECK(field->is_repeated() || (index == -1))
2309       << "Index must be -1 for non-repeated fields";
2310 
2311   const FastFieldValuePrinter* printer = FindWithDefault(
2312       custom_printers_, field, default_field_value_printer_.get());
2313 
2314   switch (field->cpp_type()) {
2315 #define OUTPUT_FIELD(CPPTYPE, METHOD)                                \
2316   case FieldDescriptor::CPPTYPE_##CPPTYPE:                           \
2317     printer->Print##METHOD(                                          \
2318         field->is_repeated()                                         \
2319             ? reflection->GetRepeated##METHOD(message, field, index) \
2320             : reflection->Get##METHOD(message, field),               \
2321         generator);                                                  \
2322     break
2323 
2324     OUTPUT_FIELD(INT32, Int32);
2325     OUTPUT_FIELD(INT64, Int64);
2326     OUTPUT_FIELD(UINT32, UInt32);
2327     OUTPUT_FIELD(UINT64, UInt64);
2328     OUTPUT_FIELD(FLOAT, Float);
2329     OUTPUT_FIELD(DOUBLE, Double);
2330     OUTPUT_FIELD(BOOL, Bool);
2331 #undef OUTPUT_FIELD
2332 
2333     case FieldDescriptor::CPPTYPE_STRING: {
2334       std::string scratch;
2335       const std::string& value =
2336           field->is_repeated()
2337               ? reflection->GetRepeatedStringReference(message, field, index,
2338                                                        &scratch)
2339               : reflection->GetStringReference(message, field, &scratch);
2340       const std::string* value_to_print = &value;
2341       std::string truncated_value;
2342       if (truncate_string_field_longer_than_ > 0 &&
2343           truncate_string_field_longer_than_ < value.size()) {
2344         truncated_value = value.substr(0, truncate_string_field_longer_than_) +
2345                           "...<truncated>...";
2346         value_to_print = &truncated_value;
2347       }
2348       if (field->type() == FieldDescriptor::TYPE_STRING) {
2349         printer->PrintString(*value_to_print, generator);
2350       } else {
2351         GOOGLE_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_BYTES);
2352         printer->PrintBytes(*value_to_print, generator);
2353       }
2354       break;
2355     }
2356 
2357     case FieldDescriptor::CPPTYPE_ENUM: {
2358       int enum_value =
2359           field->is_repeated()
2360               ? reflection->GetRepeatedEnumValue(message, field, index)
2361               : reflection->GetEnumValue(message, field);
2362       const EnumValueDescriptor* enum_desc =
2363           field->enum_type()->FindValueByNumber(enum_value);
2364       if (enum_desc != nullptr) {
2365         printer->PrintEnum(enum_value, enum_desc->name(), generator);
2366       } else {
2367         // Ordinarily, enum_desc should not be null, because proto2 has the
2368         // invariant that set enum field values must be in-range, but with the
2369         // new integer-based API for enums (or the RepeatedField<int> loophole),
2370         // it is possible for the user to force an unknown integer value.  So we
2371         // simply use the integer value itself as the enum value name in this
2372         // case.
2373         printer->PrintEnum(enum_value, StringPrintf("%d", enum_value),
2374                            generator);
2375       }
2376       break;
2377     }
2378 
2379     case FieldDescriptor::CPPTYPE_MESSAGE:
2380       Print(field->is_repeated()
2381                 ? reflection->GetRepeatedMessage(message, field, index)
2382                 : reflection->GetMessage(message, field),
2383             generator);
2384       break;
2385   }
2386 }
2387 
Print(const Message & message,io::ZeroCopyOutputStream * output)2388 /* static */ bool TextFormat::Print(const Message& message,
2389                                     io::ZeroCopyOutputStream* output) {
2390   return Printer().Print(message, output);
2391 }
2392 
PrintUnknownFields(const UnknownFieldSet & unknown_fields,io::ZeroCopyOutputStream * output)2393 /* static */ bool TextFormat::PrintUnknownFields(
2394     const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output) {
2395   return Printer().PrintUnknownFields(unknown_fields, output);
2396 }
2397 
PrintToString(const Message & message,std::string * output)2398 /* static */ bool TextFormat::PrintToString(const Message& message,
2399                                             std::string* output) {
2400   return Printer().PrintToString(message, output);
2401 }
2402 
PrintUnknownFieldsToString(const UnknownFieldSet & unknown_fields,std::string * output)2403 /* static */ bool TextFormat::PrintUnknownFieldsToString(
2404     const UnknownFieldSet& unknown_fields, std::string* output) {
2405   return Printer().PrintUnknownFieldsToString(unknown_fields, output);
2406 }
2407 
PrintFieldValueToString(const Message & message,const FieldDescriptor * field,int index,std::string * output)2408 /* static */ void TextFormat::PrintFieldValueToString(
2409     const Message& message, const FieldDescriptor* field, int index,
2410     std::string* output) {
2411   return Printer().PrintFieldValueToString(message, field, index, output);
2412 }
2413 
ParseFieldValueFromString(const std::string & input,const FieldDescriptor * field,Message * message)2414 /* static */ bool TextFormat::ParseFieldValueFromString(
2415     const std::string& input, const FieldDescriptor* field, Message* message) {
2416   return Parser().ParseFieldValueFromString(input, field, message);
2417 }
2418 
PrintUnknownFields(const UnknownFieldSet & unknown_fields,TextGenerator * generator) const2419 void TextFormat::Printer::PrintUnknownFields(
2420     const UnknownFieldSet& unknown_fields, TextGenerator* generator) const {
2421   for (int i = 0; i < unknown_fields.field_count(); i++) {
2422     const UnknownField& field = unknown_fields.field(i);
2423     std::string field_number = StrCat(field.number());
2424 
2425     switch (field.type()) {
2426       case UnknownField::TYPE_VARINT:
2427         generator->PrintString(field_number);
2428         generator->PrintLiteral(": ");
2429         generator->PrintString(StrCat(field.varint()));
2430         if (single_line_mode_) {
2431           generator->PrintLiteral(" ");
2432         } else {
2433           generator->PrintLiteral("\n");
2434         }
2435         break;
2436       case UnknownField::TYPE_FIXED32: {
2437         generator->PrintString(field_number);
2438         generator->PrintLiteral(": 0x");
2439         generator->PrintString(
2440             StrCat(strings::Hex(field.fixed32(), strings::ZERO_PAD_8)));
2441         if (single_line_mode_) {
2442           generator->PrintLiteral(" ");
2443         } else {
2444           generator->PrintLiteral("\n");
2445         }
2446         break;
2447       }
2448       case UnknownField::TYPE_FIXED64: {
2449         generator->PrintString(field_number);
2450         generator->PrintLiteral(": 0x");
2451         generator->PrintString(
2452             StrCat(strings::Hex(field.fixed64(), strings::ZERO_PAD_16)));
2453         if (single_line_mode_) {
2454           generator->PrintLiteral(" ");
2455         } else {
2456           generator->PrintLiteral("\n");
2457         }
2458         break;
2459       }
2460       case UnknownField::TYPE_LENGTH_DELIMITED: {
2461         generator->PrintString(field_number);
2462         const std::string& value = field.length_delimited();
2463         UnknownFieldSet embedded_unknown_fields;
2464         if (!value.empty() && embedded_unknown_fields.ParseFromString(value)) {
2465           // This field is parseable as a Message.
2466           // So it is probably an embedded message.
2467           if (single_line_mode_) {
2468             generator->PrintLiteral(" { ");
2469           } else {
2470             generator->PrintLiteral(" {\n");
2471             generator->Indent();
2472           }
2473           PrintUnknownFields(embedded_unknown_fields, generator);
2474           if (single_line_mode_) {
2475             generator->PrintLiteral("} ");
2476           } else {
2477             generator->Outdent();
2478             generator->PrintLiteral("}\n");
2479           }
2480         } else {
2481           // This field is not parseable as a Message.
2482           // So it is probably just a plain string.
2483           generator->PrintLiteral(": \"");
2484           generator->PrintString(CEscape(value));
2485           if (single_line_mode_) {
2486             generator->PrintLiteral("\" ");
2487           } else {
2488             generator->PrintLiteral("\"\n");
2489           }
2490         }
2491         break;
2492       }
2493       case UnknownField::TYPE_GROUP:
2494         generator->PrintString(field_number);
2495         if (single_line_mode_) {
2496           generator->PrintLiteral(" { ");
2497         } else {
2498           generator->PrintLiteral(" {\n");
2499           generator->Indent();
2500         }
2501         PrintUnknownFields(field.group(), generator);
2502         if (single_line_mode_) {
2503           generator->PrintLiteral("} ");
2504         } else {
2505           generator->Outdent();
2506           generator->PrintLiteral("}\n");
2507         }
2508         break;
2509     }
2510   }
2511 }
2512 
2513 }  // namespace protobuf
2514 }  // namespace google
2515