Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:27

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: jschorr@google.com (Joseph Schorr)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 //
0012 // Utilities for printing and parsing protocol messages in a human-readable,
0013 // text-based format.
0014 
0015 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
0016 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
0017 
0018 #include <atomic>
0019 #include <memory>
0020 #include <string>
0021 #include <vector>
0022 
0023 #include "absl/container/flat_hash_map.h"
0024 #include "absl/container/flat_hash_set.h"
0025 #include "absl/strings/cord.h"
0026 #include "absl/strings/string_view.h"
0027 #include "google/protobuf/descriptor.h"
0028 #include "google/protobuf/message.h"
0029 #include "google/protobuf/message_lite.h"
0030 #include "google/protobuf/port.h"
0031 
0032 
0033 // Must be included last.
0034 #include "google/protobuf/port_def.inc"
0035 
0036 #ifdef SWIG
0037 #error "You cannot SWIG proto headers"
0038 #endif
0039 
0040 namespace google {
0041 namespace protobuf {
0042 
0043 namespace internal {
0044 PROTOBUF_EXPORT extern const char kDebugStringSilentMarker[1];
0045 PROTOBUF_EXPORT extern const char kDebugStringSilentMarkerForDetection[3];
0046 
0047 PROTOBUF_EXPORT extern std::atomic<bool> enable_debug_string_safe_format;
0048 PROTOBUF_EXPORT int64_t GetRedactedFieldCount();
0049 PROTOBUF_EXPORT bool ShouldRedactField(const FieldDescriptor* field);
0050 
0051 // This enum contains all the APIs that convert protos to human-readable
0052 // formats. A higher-level API must correspond to a greater number than any
0053 // lower-level APIs it calls under the hood (e.g kDebugString >
0054 // kMemberPrintToString > kPrintWithStream).
0055 PROTOBUF_EXPORT enum class FieldReporterLevel {
0056   kNoReport = 0,
0057   kPrintMessage = 1,
0058   kPrintWithGenerator = 2,
0059   kPrintWithStream = 3,
0060   kMemberPrintToString = 4,
0061   kStaticPrintToString = 5,
0062   kAbslStringify = 6,
0063   kShortFormat = 7,
0064   kUtf8Format = 8,
0065   kDebugString = 12,
0066   kShortDebugString = 13,
0067   kUtf8DebugString = 14,
0068   kUnredactedDebugFormatForTest = 15,
0069   kUnredactedShortDebugFormatForTest = 16,
0070   kUnredactedUtf8DebugFormatForTest = 17
0071 };
0072 
0073 }  // namespace internal
0074 
0075 namespace io {
0076 class ErrorCollector;  // tokenizer.h
0077 }
0078 
0079 namespace python {
0080 namespace cmessage {
0081 class PythonFieldValuePrinter;
0082 }
0083 }  // namespace python
0084 
0085 namespace internal {
0086 // Enum used to set printing options for StringifyMessage.
0087 PROTOBUF_EXPORT enum class Option;
0088 
0089 // Converts a protobuf message to a string. If enable_safe_format is true,
0090 // sensitive fields are redacted, and a per-process randomized prefix is
0091 // inserted.
0092 PROTOBUF_EXPORT std::string StringifyMessage(const Message& message,
0093                                              Option option,
0094                                              FieldReporterLevel reporter_level,
0095                                              bool enable_safe_format);
0096 
0097 class UnsetFieldsMetadataTextFormatTestUtil;
0098 class UnsetFieldsMetadataMessageDifferencerTestUtil;
0099 }  // namespace internal
0100 
0101 // This class implements protocol buffer text format, colloquially known as text
0102 // proto.  Printing and parsing protocol messages in text format is useful for
0103 // debugging and human editing of messages.
0104 //
0105 // This class is really a namespace that contains only static methods.
0106 class PROTOBUF_EXPORT TextFormat {
0107  public:
0108   TextFormat(const TextFormat&) = delete;
0109   TextFormat& operator=(const TextFormat&) = delete;
0110 
0111   // Outputs a textual representation of the given message to the given
0112   // output stream. Returns false if printing fails.
0113   static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
0114 
0115   // Print the fields in an UnknownFieldSet.  They are printed by tag number
0116   // only.  Embedded messages are heuristically identified by attempting to
0117   // parse them. Returns false if printing fails.
0118   static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0119                                  io::ZeroCopyOutputStream* output);
0120 
0121   // Like Print(), but outputs directly to a string.
0122   // Note: output will be cleared prior to printing, and will be left empty
0123   // even if printing fails. Returns false if printing fails.
0124   static bool PrintToString(const Message& message, std::string* output);
0125 
0126   // Like PrintUnknownFields(), but outputs directly to a string. Returns
0127   // false if printing fails.
0128   static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
0129                                          std::string* output);
0130 
0131   // Outputs a textual representation of the value of the field supplied on
0132   // the message supplied. For non-repeated fields, an index of -1 must
0133   // be supplied. Note that this method will print the default value for a
0134   // field if it is not set.
0135   static void PrintFieldValueToString(const Message& message,
0136                                       const FieldDescriptor* field, int index,
0137                                       std::string* output);
0138 
0139   // Forward declare `Printer` for `BaseTextGenerator::MarkerToken` which
0140   // restricts some methods of `BaseTextGenerator` to the class `Printer`.
0141   class Printer;
0142 
0143   class PROTOBUF_EXPORT BaseTextGenerator {
0144    private:
0145     // Passkey (go/totw/134#what-about-stdshared-ptr) that allows `Printer`
0146     // (but not derived classes) to call `PrintMaybeWithMarker` and its
0147     // `Printer::TextGenerator` to overload it.
0148     // This prevents users from bypassing the marker generation.
0149     class MarkerToken {
0150      private:
0151       explicit MarkerToken() = default;  // 'explicit' prevents aggregate init.
0152       friend class Printer;
0153     };
0154 
0155    public:
0156     virtual ~BaseTextGenerator();
0157 
0158     virtual void Indent() {}
0159     virtual void Outdent() {}
0160     // Returns the current indentation size in characters.
0161     virtual size_t GetCurrentIndentationSize() const { return 0; }
0162 
0163     // Print text to the output stream.
0164     virtual void Print(const char* text, size_t size) = 0;
0165 
0166     void PrintString(absl::string_view str) { Print(str.data(), str.size()); }
0167 
0168     template <size_t n>
0169     void PrintLiteral(const char (&text)[n]) {
0170       Print(text, n - 1);  // n includes the terminating zero character.
0171     }
0172 
0173     // Internal to Printer, access regulated by `MarkerToken`.
0174     virtual void PrintMaybeWithMarker(MarkerToken, absl::string_view text) {
0175       Print(text.data(), text.size());
0176     }
0177 
0178     // Internal to Printer, access regulated by `MarkerToken`.
0179     virtual void PrintMaybeWithMarker(MarkerToken, absl::string_view text_head,
0180                                       absl::string_view text_tail) {
0181       Print(text_head.data(), text_head.size());
0182       Print(text_tail.data(), text_tail.size());
0183     }
0184 
0185     friend class Printer;
0186   };
0187 
0188   // The default printer that converts scalar values from fields into their
0189   // string representation.
0190   // You can derive from this FastFieldValuePrinter if you want to have fields
0191   // to be printed in a different way and register it at the Printer.
0192   class PROTOBUF_EXPORT FastFieldValuePrinter {
0193    public:
0194     FastFieldValuePrinter();
0195     FastFieldValuePrinter(const FastFieldValuePrinter&) = delete;
0196     FastFieldValuePrinter& operator=(const FastFieldValuePrinter&) = delete;
0197     virtual ~FastFieldValuePrinter();
0198     virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
0199     virtual void PrintInt32(int32_t val, BaseTextGenerator* generator) const;
0200     virtual void PrintUInt32(uint32_t val, BaseTextGenerator* generator) const;
0201     virtual void PrintInt64(int64_t val, BaseTextGenerator* generator) const;
0202     virtual void PrintUInt64(uint64_t val, BaseTextGenerator* generator) const;
0203     virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
0204     virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
0205     virtual void PrintString(const std::string& val,
0206                              BaseTextGenerator* generator) const;
0207     virtual void PrintBytes(const std::string& val,
0208                             BaseTextGenerator* generator) const;
0209     virtual void PrintEnum(int32_t val, const std::string& name,
0210                            BaseTextGenerator* generator) const;
0211     virtual void PrintFieldName(const Message& message, int field_index,
0212                                 int field_count, const Reflection* reflection,
0213                                 const FieldDescriptor* field,
0214                                 BaseTextGenerator* generator) const;
0215     virtual void PrintFieldName(const Message& message,
0216                                 const Reflection* reflection,
0217                                 const FieldDescriptor* field,
0218                                 BaseTextGenerator* generator) const;
0219     virtual void PrintMessageStart(const Message& message, int field_index,
0220                                    int field_count, bool single_line_mode,
0221                                    BaseTextGenerator* generator) const;
0222     // Allows to override the logic on how to print the content of a message.
0223     // Return false to use the default printing logic. Note that it is legal for
0224     // this function to print something and then return false to use the default
0225     // content printing (although at that point it would behave similarly to
0226     // PrintMessageStart).
0227     virtual bool PrintMessageContent(const Message& message, int field_index,
0228                                      int field_count, bool single_line_mode,
0229                                      BaseTextGenerator* generator) const;
0230     virtual void PrintMessageEnd(const Message& message, int field_index,
0231                                  int field_count, bool single_line_mode,
0232                                  BaseTextGenerator* generator) const;
0233   };
0234 
0235   // Deprecated: please use FastFieldValuePrinter instead.
0236   class PROTOBUF_EXPORT FieldValuePrinter {
0237    public:
0238     FieldValuePrinter();
0239     FieldValuePrinter(const FieldValuePrinter&) = delete;
0240     FieldValuePrinter& operator=(const FieldValuePrinter&) = delete;
0241     virtual ~FieldValuePrinter();
0242     virtual std::string PrintBool(bool val) const;
0243     virtual std::string PrintInt32(int32_t val) const;
0244     virtual std::string PrintUInt32(uint32_t val) const;
0245     virtual std::string PrintInt64(int64_t val) const;
0246     virtual std::string PrintUInt64(uint64_t val) const;
0247     virtual std::string PrintFloat(float val) const;
0248     virtual std::string PrintDouble(double val) const;
0249     virtual std::string PrintString(const std::string& val) const;
0250     virtual std::string PrintBytes(const std::string& val) const;
0251     virtual std::string PrintEnum(int32_t val, const std::string& name) const;
0252     virtual std::string PrintFieldName(const Message& message,
0253                                        const Reflection* reflection,
0254                                        const FieldDescriptor* field) const;
0255     virtual std::string PrintMessageStart(const Message& message,
0256                                           int field_index, int field_count,
0257                                           bool single_line_mode) const;
0258     virtual std::string PrintMessageEnd(const Message& message, int field_index,
0259                                         int field_count,
0260                                         bool single_line_mode) const;
0261 
0262    private:
0263     FastFieldValuePrinter delegate_;
0264   };
0265 
0266   class PROTOBUF_EXPORT MessagePrinter {
0267    public:
0268     MessagePrinter() {}
0269     MessagePrinter(const MessagePrinter&) = delete;
0270     MessagePrinter& operator=(const MessagePrinter&) = delete;
0271     virtual ~MessagePrinter() {}
0272     virtual void Print(const Message& message, bool single_line_mode,
0273                        BaseTextGenerator* generator) const = 0;
0274   };
0275 
0276   // Interface that Printers or Parsers can use to find extensions, or types
0277   // referenced in Any messages.
0278   class PROTOBUF_EXPORT Finder {
0279    public:
0280     virtual ~Finder();
0281 
0282     // Try to find an extension of *message by fully-qualified field
0283     // name.  Returns nullptr if no extension is known for this name or number.
0284     // The base implementation uses the extensions already known by the message.
0285     virtual const FieldDescriptor* FindExtension(Message* message,
0286                                                  const std::string& name) const;
0287 
0288     // Similar to FindExtension, but uses a Descriptor and the extension number
0289     // instead of using a Message and the name when doing the look up.
0290     virtual const FieldDescriptor* FindExtensionByNumber(
0291         const Descriptor* descriptor, int number) const;
0292 
0293     // Find the message type for an Any proto.
0294     // Returns nullptr if no message is known for this name.
0295     // The base implementation only accepts prefixes of type.googleprod.com/ or
0296     // type.googleapis.com/, and searches the DescriptorPool of the parent
0297     // message.
0298     virtual const Descriptor* FindAnyType(const Message& message,
0299                                           const std::string& prefix,
0300                                           const std::string& name) const;
0301 
0302     // Find the message factory for the given extension field. This can be used
0303     // to generalize the Parser to add extension fields to a message in the same
0304     // way as the "input" message for the Parser.
0305     virtual MessageFactory* FindExtensionFactory(
0306         const FieldDescriptor* field) const;
0307   };
0308 
0309   // Class for those users which require more fine-grained control over how
0310   // a protobuffer message is printed out.
0311   class PROTOBUF_EXPORT Printer {
0312    public:
0313     Printer();
0314 
0315     // Like TextFormat::Print
0316     bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
0317     // Like TextFormat::Printer::Print but takes an additional
0318     // internal::FieldReporterLevel
0319     bool Print(const Message& message, io::ZeroCopyOutputStream* output,
0320                internal::FieldReporterLevel reporter) const;
0321     // Like TextFormat::PrintUnknownFields
0322     bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0323                             io::ZeroCopyOutputStream* output) const;
0324     // Like TextFormat::PrintToString
0325     bool PrintToString(const Message& message, std::string* output) const;
0326     // Like TextFormat::PrintUnknownFieldsToString
0327     bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
0328                                     std::string* output) const;
0329     // Like TextFormat::PrintFieldValueToString
0330     void PrintFieldValueToString(const Message& message,
0331                                  const FieldDescriptor* field, int index,
0332                                  std::string* output) const;
0333 
0334     // Adjust the initial indent level of all output.  Each indent level is
0335     // equal to two spaces.
0336     void SetInitialIndentLevel(int indent_level) {
0337       initial_indent_level_ = indent_level;
0338     }
0339 
0340     // If printing in single line mode, then the entire message will be output
0341     // on a single line with no line breaks.
0342     void SetSingleLineMode(bool single_line_mode) {
0343       single_line_mode_ = single_line_mode;
0344     }
0345 
0346     bool IsInSingleLineMode() const { return single_line_mode_; }
0347 
0348     // If use_field_number is true, uses field number instead of field name.
0349     void SetUseFieldNumber(bool use_field_number) {
0350       use_field_number_ = use_field_number;
0351     }
0352 
0353     // Set true to print repeated primitives in a format like:
0354     //   field_name: [1, 2, 3, 4]
0355     // instead of printing each value on its own line.  Short format applies
0356     // only to primitive values -- i.e. everything except strings and
0357     // sub-messages/groups.
0358     void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) {
0359       use_short_repeated_primitives_ = use_short_repeated_primitives;
0360     }
0361 
0362     // Set true to output UTF-8 instead of ASCII.  The only difference
0363     // is that bytes >= 0x80 in string fields will not be escaped,
0364     // because they are assumed to be part of UTF-8 multi-byte
0365     // sequences. This will change the default FastFieldValuePrinter.
0366     void SetUseUtf8StringEscaping(bool as_utf8);
0367 
0368     // Set the default FastFieldValuePrinter that is used for all fields that
0369     // don't have a field-specific printer registered.
0370     // Takes ownership of the printer.
0371     void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
0372 
0373     [[deprecated("Please use FastFieldValuePrinter")]] void
0374     SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
0375 
0376     // Sets whether we want to hide unknown fields or not.
0377     // Usually unknown fields are printed in a generic way that includes the
0378     // tag number of the field instead of field name. However, sometimes it
0379     // is useful to be able to print the message without unknown fields (e.g.
0380     // for the python protobuf version to maintain consistency between its pure
0381     // python and c++ implementations).
0382     void SetHideUnknownFields(bool hide) { hide_unknown_fields_ = hide; }
0383 
0384     // If print_message_fields_in_index_order is true, fields of a proto message
0385     // will be printed using the order defined in source code instead of the
0386     // field number, extensions will be printed at the end of the message
0387     // and their relative order is determined by the extension number.
0388     // By default, use the field number order.
0389     void SetPrintMessageFieldsInIndexOrder(
0390         bool print_message_fields_in_index_order) {
0391       print_message_fields_in_index_order_ =
0392           print_message_fields_in_index_order;
0393     }
0394 
0395     // If expand==true, expand google.protobuf.Any payloads. The output
0396     // will be of form
0397     //    [type_url] { <value_printed_in_text> }
0398     //
0399     // If expand==false, print Any using the default printer. The output will
0400     // look like
0401     //    type_url: "<type_url>"  value: "serialized_content"
0402     void SetExpandAny(bool expand) { expand_any_ = expand; }
0403 
0404     // Set how parser finds message for Any payloads.
0405     void SetFinder(const Finder* finder) { finder_ = finder; }
0406 
0407     // If non-zero, we truncate all string fields that are  longer than
0408     // this threshold.  This is useful when the proto message has very long
0409     // strings, e.g., dump of encoded image file.
0410     //
0411     // NOTE:  Setting a non-zero value breaks round-trip safe
0412     // property of TextFormat::Printer.  That is, from the printed message, we
0413     // cannot fully recover the original string field any more.
0414     void SetTruncateStringFieldLongerThan(
0415         const int64_t truncate_string_field_longer_than) {
0416       truncate_string_field_longer_than_ = truncate_string_field_longer_than;
0417     }
0418 
0419     // Sets whether sensitive fields found in the message will be reported or
0420     // not.
0421     void SetReportSensitiveFields(internal::FieldReporterLevel reporter) {
0422       if (report_sensitive_fields_ < reporter) {
0423         report_sensitive_fields_ = reporter;
0424       }
0425     }
0426 
0427     // Register a custom field-specific FastFieldValuePrinter for fields
0428     // with a particular FieldDescriptor.
0429     // Returns "true" if the registration succeeded, or "false", if there is
0430     // already a printer for that FieldDescriptor.
0431     // Takes ownership of the printer on successful registration.
0432     bool RegisterFieldValuePrinter(const FieldDescriptor* field,
0433                                    const FastFieldValuePrinter* printer);
0434 
0435     [[deprecated("Please use FastFieldValuePrinter")]] bool
0436     RegisterFieldValuePrinter(const FieldDescriptor* field,
0437                               const FieldValuePrinter* printer);
0438 
0439     // Register a custom message-specific MessagePrinter for messages with a
0440     // particular Descriptor.
0441     // Returns "true" if the registration succeeded, or "false" if there is
0442     // already a printer for that Descriptor.
0443     bool RegisterMessagePrinter(const Descriptor* descriptor,
0444                                 const MessagePrinter* printer);
0445 
0446     // Default printing for messages, which allows registered message printers
0447     // to fall back to default printing without losing the ability to control
0448     // sub-messages or fields.
0449     // NOTE: If the passed in `text_generaor` is not actually the current
0450     // `TextGenerator`, then no output will be produced.
0451     void PrintMessage(const Message& message,
0452                       BaseTextGenerator* generator) const;
0453 
0454    private:
0455     friend std::string Message::DebugString() const;
0456     friend std::string Message::ShortDebugString() const;
0457     friend std::string Message::Utf8DebugString() const;
0458     friend std::string internal::StringifyMessage(
0459         const Message& message, internal::Option option,
0460         internal::FieldReporterLevel reporter_level, bool enable_safe_format);
0461 
0462     // Sets whether silent markers will be inserted.
0463     void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }
0464 
0465     // Sets whether strings will be redacted and thus unparsable.
0466     void SetRedactDebugString(bool redact) { redact_debug_string_ = redact; }
0467 
0468     // Sets whether the output string should be made non-deterministic.
0469     // This discourages equality checks based on serialized string comparisons.
0470     void SetRandomizeDebugString(bool randomize) {
0471       randomize_debug_string_ = randomize;
0472     }
0473 
0474     // Forward declaration of an internal class used to print the text
0475     // output to the OutputStream (see text_format.cc for implementation).
0476     class TextGenerator;
0477     using MarkerToken = BaseTextGenerator::MarkerToken;
0478 
0479     // Forward declaration of an internal class used to print field values for
0480     // DebugString APIs (see text_format.cc for implementation).
0481     class DebugStringFieldValuePrinter;
0482 
0483     // Forward declaration of an internal class used to print UTF-8 escaped
0484     // strings (see text_format.cc for implementation).
0485     class FastFieldValuePrinterUtf8Escaping;
0486 
0487     // Internal Print method, used for writing to the OutputStream via
0488     // the TextGenerator class.
0489     void Print(const Message& message, BaseTextGenerator* generator) const;
0490 
0491     // Print a single field.
0492     void PrintField(const Message& message, const Reflection* reflection,
0493                     const FieldDescriptor* field,
0494                     BaseTextGenerator* generator) const;
0495 
0496     // Print a repeated primitive field in short form.
0497     void PrintShortRepeatedField(const Message& message,
0498                                  const Reflection* reflection,
0499                                  const FieldDescriptor* field,
0500                                  BaseTextGenerator* generator) const;
0501 
0502     // Print the name of a field -- i.e. everything that comes before the
0503     // ':' for a single name/value pair.
0504     void PrintFieldName(const Message& message, int field_index,
0505                         int field_count, const Reflection* reflection,
0506                         const FieldDescriptor* field,
0507                         BaseTextGenerator* generator) const;
0508 
0509     // Outputs a textual representation of the value of the field supplied on
0510     // the message supplied or the default value if not set.
0511     void PrintFieldValue(const Message& message, const Reflection* reflection,
0512                          const FieldDescriptor* field, int index,
0513                          BaseTextGenerator* generator) const;
0514 
0515     // Print the fields in an UnknownFieldSet.  They are printed by tag number
0516     // only.  Embedded messages are heuristically identified by attempting to
0517     // parse them (subject to the recursion budget).
0518     void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0519                             BaseTextGenerator* generator,
0520                             int recursion_budget) const;
0521 
0522     bool PrintAny(const Message& message, BaseTextGenerator* generator) const;
0523 
0524     // Try to redact a field value based on the annotations associated with
0525     // the field. This function returns true if it redacts the field value.
0526     bool TryRedactFieldValue(const Message& message,
0527                              const FieldDescriptor* field,
0528                              BaseTextGenerator* generator,
0529                              bool insert_value_separator) const;
0530 
0531     const FastFieldValuePrinter* GetFieldPrinter(
0532         const FieldDescriptor* field) const {
0533       auto it = custom_printers_.find(field);
0534       return it == custom_printers_.end() ? default_field_value_printer_.get()
0535                                           : it->second.get();
0536     }
0537 
0538     friend class google::protobuf::python::cmessage::PythonFieldValuePrinter;
0539     static void HardenedPrintString(absl::string_view src,
0540                                     TextFormat::BaseTextGenerator* generator);
0541 
0542     int initial_indent_level_;
0543     bool single_line_mode_;
0544     bool use_field_number_;
0545     bool use_short_repeated_primitives_;
0546     bool insert_silent_marker_;
0547     bool redact_debug_string_;
0548     bool randomize_debug_string_;
0549     internal::FieldReporterLevel report_sensitive_fields_;
0550     bool hide_unknown_fields_;
0551     bool print_message_fields_in_index_order_;
0552     bool expand_any_;
0553     int64_t truncate_string_field_longer_than_;
0554 
0555     std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
0556     absl::flat_hash_map<const FieldDescriptor*,
0557                         std::unique_ptr<const FastFieldValuePrinter>>
0558         custom_printers_;
0559 
0560     absl::flat_hash_map<const Descriptor*,
0561                         std::unique_ptr<const MessagePrinter>>
0562         custom_message_printers_;
0563 
0564     const Finder* finder_;
0565   };
0566 
0567   // Parses a text-format protocol message from the given input stream to
0568   // the given message object. This function parses the human-readable
0569   // serialization format written by Print(). Returns true on success. The
0570   // message is cleared first, even if the function fails -- See Merge() to
0571   // avoid this behavior.
0572   //
0573   // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}"
0574   //
0575   // One common use for this function is parsing handwritten strings in test
0576   // code.
0577   //
0578   // If you would like to read a protocol buffer serialized in the
0579   // (non-human-readable) binary wire format, see
0580   // google::protobuf::MessageLite::ParseFromString().
0581   static bool Parse(io::ZeroCopyInputStream* input, Message* output);
0582   // Like Parse(), but reads directly from a string.
0583   static bool ParseFromString(absl::string_view input, Message* output);
0584   // Like Parse(), but reads directly from a Cord.
0585   static bool ParseFromCord(const absl::Cord& input, Message* output);
0586 
0587   // Like Parse(), but the data is merged into the given message, as if
0588   // using Message::MergeFrom().
0589   static bool Merge(io::ZeroCopyInputStream* input, Message* output);
0590   // Like Merge(), but reads directly from a string.
0591   static bool MergeFromString(absl::string_view input, Message* output);
0592 
0593   // Parse the given text as a single field value and store it into the
0594   // given field of the given message. If the field is a repeated field,
0595   // the new value will be added to the end
0596   static bool ParseFieldValueFromString(absl::string_view input,
0597                                         const FieldDescriptor* field,
0598                                         Message* message);
0599 
0600   // A location in the parsed text.
0601   struct ParseLocation {
0602     int line;
0603     int column;
0604 
0605     ParseLocation() : line(-1), column(-1) {}
0606     ParseLocation(int line_param, int column_param)
0607         : line(line_param), column(column_param) {}
0608   };
0609 
0610   // A range of locations in the parsed text, including `start` and excluding
0611   // `end`.
0612   struct ParseLocationRange {
0613     ParseLocation start;
0614     ParseLocation end;
0615     ParseLocationRange() : start(), end() {}
0616     ParseLocationRange(ParseLocation start_param, ParseLocation end_param)
0617         : start(start_param), end(end_param) {}
0618   };
0619 
0620   // Data structure which is populated with the locations of each field
0621   // value parsed from the text.
0622   class PROTOBUF_EXPORT ParseInfoTree {
0623    public:
0624     ParseInfoTree() = default;
0625     ParseInfoTree(const ParseInfoTree&) = delete;
0626     ParseInfoTree& operator=(const ParseInfoTree&) = delete;
0627 
0628     // Returns the parse location range for index-th value of the field in
0629     // the parsed text. If none exists, returns a location with start and end
0630     // line -1. Index should be -1 for not-repeated fields.
0631     ParseLocationRange GetLocationRange(const FieldDescriptor* field,
0632                                         int index) const;
0633 
0634     // Returns the starting parse location for index-th value of the field in
0635     // the parsed text. If none exists, returns a location with line = -1. Index
0636     // should be -1 for not-repeated fields.
0637     ParseLocation GetLocation(const FieldDescriptor* field, int index) const {
0638       return GetLocationRange(field, index).start;
0639     }
0640 
0641     // Returns the parse info tree for the given field, which must be a message
0642     // type. The nested information tree is owned by the root tree and will be
0643     // deleted when it is deleted.
0644     ParseInfoTree* GetTreeForNested(const FieldDescriptor* field,
0645                                     int index) const;
0646 
0647    private:
0648     // Allow the text format parser to record information into the tree.
0649     friend class TextFormat;
0650 
0651     // Records the starting and ending locations of a single value for a field.
0652     void RecordLocation(const FieldDescriptor* field, ParseLocationRange range);
0653 
0654     // Create and records a nested tree for a nested message field.
0655     ParseInfoTree* CreateNested(const FieldDescriptor* field);
0656 
0657     // Defines the map from the index-th field descriptor to its parse location.
0658     absl::flat_hash_map<const FieldDescriptor*, std::vector<ParseLocationRange>>
0659         locations_;
0660     // Defines the map from the index-th field descriptor to the nested parse
0661     // info tree.
0662     absl::flat_hash_map<const FieldDescriptor*,
0663                         std::vector<std::unique_ptr<ParseInfoTree>>>
0664         nested_;
0665   };
0666 
0667   // For more control over parsing, use this class.
0668   class PROTOBUF_EXPORT Parser {
0669    public:
0670     Parser();
0671     ~Parser();
0672 
0673     // Like TextFormat::Parse().
0674     bool Parse(io::ZeroCopyInputStream* input, Message* output);
0675     // Like TextFormat::ParseFromString().
0676     bool ParseFromString(absl::string_view input, Message* output);
0677     // Like TextFormat::ParseFromCord().
0678     bool ParseFromCord(const absl::Cord& input, Message* output);
0679     // Like TextFormat::Merge().
0680     bool Merge(io::ZeroCopyInputStream* input, Message* output);
0681     // Like TextFormat::MergeFromString().
0682     bool MergeFromString(absl::string_view input, Message* output);
0683 
0684     // Set where to report parse errors.  If nullptr (the default), errors will
0685     // be printed to stderr.
0686     void RecordErrorsTo(io::ErrorCollector* error_collector) {
0687       error_collector_ = error_collector;
0688     }
0689 
0690     // Set how parser finds extensions.  If nullptr (the default), the
0691     // parser will use the standard Reflection object associated with
0692     // the message being parsed.
0693     void SetFinder(const Finder* finder) { finder_ = finder; }
0694 
0695     // Sets where location information about the parse will be written. If
0696     // nullptr
0697     // (the default), then no location will be written.
0698     void WriteLocationsTo(ParseInfoTree* tree) { parse_info_tree_ = tree; }
0699 
0700     // Normally parsing fails if, after parsing, output->IsInitialized()
0701     // returns false.  Call AllowPartialMessage(true) to skip this check.
0702     void AllowPartialMessage(bool allow) { allow_partial_ = allow; }
0703 
0704     // Allow field names to be matched case-insensitively.
0705     // This is not advisable if there are fields that only differ in case, or
0706     // if you want to enforce writing in the canonical form.
0707     // This is 'false' by default.
0708     void AllowCaseInsensitiveField(bool allow) {
0709       allow_case_insensitive_field_ = allow;
0710     }
0711 
0712     // Like TextFormat::ParseFieldValueFromString
0713     bool ParseFieldValueFromString(absl::string_view input,
0714                                    const FieldDescriptor* field,
0715                                    Message* output);
0716 
0717     // When an unknown extension is met, parsing will fail if this option is
0718     // set to false (the default). If true, unknown extensions will be ignored
0719     // and a warning message will be generated.
0720     // Beware! Setting this option true may hide some errors (e.g. spelling
0721     // error on extension name).  This allows data loss; unlike binary format,
0722     // text format cannot preserve unknown extensions.  Avoid using this option
0723     // if possible.
0724     void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; }
0725 
0726     // When an unknown field is met, parsing will fail if this option is set
0727     // to false (the default). If true, unknown fields will be ignored and
0728     // a warning message will be generated.
0729     // Beware! Setting this option true may hide some errors (e.g. spelling
0730     // error on field name). This allows data loss; unlike binary format, text
0731     // format cannot preserve unknown fields.  Avoid using this option
0732     // if possible.
0733     void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; }
0734 
0735 
0736     void AllowFieldNumber(bool allow) { allow_field_number_ = allow; }
0737 
0738     // Sets maximum recursion depth which parser can use. This is effectively
0739     // the maximum allowed nesting of proto messages.
0740     void SetRecursionLimit(int limit) { recursion_limit_ = limit; }
0741 
0742     // Metadata representing all the fields that were explicitly unset in
0743     // textproto. Example:
0744     // "some_int_field: 0"
0745     // where some_int_field has implicit presence.
0746     //
0747     // This class should only be used to pass data between TextFormat and the
0748     // MessageDifferencer.
0749     class UnsetFieldsMetadata {
0750      public:
0751       UnsetFieldsMetadata() = default;
0752 
0753      private:
0754       using Id = std::pair<const Message*, const FieldDescriptor*>;
0755       // Return an id representing the unset field in the given message.
0756       static Id GetUnsetFieldId(const Message& message,
0757                                 const FieldDescriptor& fd);
0758 
0759       // List of ids of explicitly unset proto fields.
0760       absl::flat_hash_set<Id> ids_;
0761 
0762       friend class ::google::protobuf::internal::
0763           UnsetFieldsMetadataMessageDifferencerTestUtil;
0764       friend class ::google::protobuf::internal::UnsetFieldsMetadataTextFormatTestUtil;
0765       friend class ::google::protobuf::util::MessageDifferencer;
0766       friend class ::google::protobuf::TextFormat::Parser;
0767     };
0768 
0769     // If called, the parser will report the parsed fields that had no
0770     // effect on the resulting proto (for example, fields with no presence that
0771     // were set to their default value). These can be passed to the Partially()
0772     // matcher as an indicator to explicitly check these fields are missing
0773     // in the actual.
0774     void OutputNoOpFields(UnsetFieldsMetadata* no_op_fields) {
0775       no_op_fields_ = no_op_fields;
0776     }
0777 
0778    private:
0779     // Forward declaration of an internal class used to parse text
0780     // representations (see text_format.cc for implementation).
0781     class ParserImpl;
0782 
0783     // Like TextFormat::Merge().  The provided implementation is used
0784     // to do the parsing.
0785     bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output,
0786                         ParserImpl* parser_impl);
0787 
0788     io::ErrorCollector* error_collector_;
0789     const Finder* finder_;
0790     ParseInfoTree* parse_info_tree_;
0791     bool allow_partial_;
0792     bool allow_case_insensitive_field_;
0793     bool allow_unknown_field_;
0794     bool allow_unknown_extension_;
0795     bool allow_unknown_enum_;
0796     bool allow_field_number_;
0797     bool allow_relaxed_whitespace_;
0798     bool allow_singular_overwrites_;
0799     int recursion_limit_;
0800     UnsetFieldsMetadata* no_op_fields_ = nullptr;
0801   };
0802 
0803 
0804  private:
0805   // Hack: ParseInfoTree declares TextFormat as a friend which should extend
0806   // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some
0807   // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide
0808   // helpers for ParserImpl to call methods of ParseInfoTree.
0809   static inline void RecordLocation(ParseInfoTree* info_tree,
0810                                     const FieldDescriptor* field,
0811                                     ParseLocationRange location);
0812   static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree,
0813                                             const FieldDescriptor* field);
0814   // To reduce stack frame bloat we use an out-of-line function to print
0815   // strings. This avoid local std::string temporaries.
0816   template <typename... T>
0817   static void OutOfLinePrintString(BaseTextGenerator* generator,
0818                                    const T&... values);
0819 };
0820 
0821 
0822 inline void TextFormat::RecordLocation(ParseInfoTree* info_tree,
0823                                        const FieldDescriptor* field,
0824                                        ParseLocationRange location) {
0825   info_tree->RecordLocation(field, location);
0826 }
0827 
0828 inline TextFormat::ParseInfoTree* TextFormat::CreateNested(
0829     ParseInfoTree* info_tree, const FieldDescriptor* field) {
0830   return info_tree->CreateNested(field);
0831 }
0832 
0833 }  // namespace protobuf
0834 }  // namespace google
0835 
0836 #include "google/protobuf/port_undef.inc"
0837 
0838 #endif  // GOOGLE_PROTOBUF_TEXT_FORMAT_H__