/proc/self/cwd/external/com_google_protobuf/src/google/protobuf/text_format.cc
Line | Count | Source (jump to first uncovered line) |
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 <stdio.h> |
39 | | |
40 | | #include <algorithm> |
41 | | #include <atomic> |
42 | | #include <climits> |
43 | | #include <cmath> |
44 | | #include <limits> |
45 | | #include <string> |
46 | | #include <utility> |
47 | | #include <vector> |
48 | | |
49 | | #include "absl/container/btree_set.h" |
50 | | #include "absl/strings/ascii.h" |
51 | | #include "absl/strings/cord.h" |
52 | | #include "absl/strings/escaping.h" |
53 | | #include "absl/strings/numbers.h" |
54 | | #include "absl/strings/str_cat.h" |
55 | | #include "absl/strings/str_join.h" |
56 | | #include "absl/strings/string_view.h" |
57 | | #include "google/protobuf/any.h" |
58 | | #include "google/protobuf/descriptor.h" |
59 | | #include "google/protobuf/descriptor.pb.h" |
60 | | #include "google/protobuf/dynamic_message.h" |
61 | | #include "google/protobuf/io/coded_stream.h" |
62 | | #include "google/protobuf/io/strtod.h" |
63 | | #include "google/protobuf/io/tokenizer.h" |
64 | | #include "google/protobuf/io/zero_copy_stream.h" |
65 | | #include "google/protobuf/io/zero_copy_stream_impl.h" |
66 | | #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
67 | | #include "google/protobuf/map_field.h" |
68 | | #include "google/protobuf/message.h" |
69 | | #include "google/protobuf/reflection_mode.h" |
70 | | #include "google/protobuf/repeated_field.h" |
71 | | #include "google/protobuf/unknown_field_set.h" |
72 | | #include "google/protobuf/wire_format_lite.h" |
73 | | |
74 | | // Must be included last. |
75 | | #include "google/protobuf/port_def.inc" |
76 | | |
77 | | namespace google { |
78 | | namespace protobuf { |
79 | | |
80 | | using internal::ReflectionMode; |
81 | | using internal::ScopedReflectionMode; |
82 | | |
83 | | namespace { |
84 | | |
85 | 22.2k | inline bool IsHexNumber(const std::string& str) { |
86 | 22.2k | return (str.length() >= 2 && str[0] == '0' && |
87 | 22.2k | (str[1] == 'x' || str[1] == 'X')); |
88 | 22.2k | } |
89 | | |
90 | 22.2k | inline bool IsOctNumber(const std::string& str) { |
91 | 22.2k | return (str.length() >= 2 && str[0] == '0' && |
92 | 22.2k | (str[1] >= '0' && str[1] < '8')); |
93 | 22.2k | } |
94 | | |
95 | | // The number of fields that are redacted in AbslStringify. |
96 | | std::atomic<int64_t> num_redacted_field{0}; |
97 | | |
98 | 0 | inline void IncrementRedactedFieldCounter() { |
99 | 0 | num_redacted_field.fetch_add(1, std::memory_order_relaxed); |
100 | 0 | } |
101 | | |
102 | | } // namespace |
103 | | |
104 | | namespace internal { |
105 | | const char kDebugStringSilentMarker[] = ""; |
106 | | const char kDebugStringSilentMarkerForDetection[] = "\t "; |
107 | | |
108 | | // Controls insertion of a marker making debug strings non-parseable. |
109 | | PROTOBUF_EXPORT std::atomic<bool> enable_debug_text_redaction_marker; |
110 | | |
111 | | // Controls insertion of a randomized marker in debug strings. |
112 | | PROTOBUF_EXPORT std::atomic<bool> enable_debug_text_random_marker; |
113 | | |
114 | | // Controls insertion of kDebugStringSilentMarker. |
115 | | PROTOBUF_EXPORT std::atomic<bool> enable_debug_text_format_marker; |
116 | | |
117 | 0 | int64_t GetRedactedFieldCount() { |
118 | 0 | return num_redacted_field.load(std::memory_order_relaxed); |
119 | 0 | } |
120 | | } // namespace internal |
121 | | |
122 | 63.9k | std::string Message::DebugString() const { |
123 | | // Indicate all scoped reflection calls are from DebugString function. |
124 | 63.9k | ScopedReflectionMode scope(ReflectionMode::kDebugString); |
125 | 63.9k | std::string debug_string; |
126 | | |
127 | 63.9k | TextFormat::Printer printer; |
128 | 63.9k | printer.SetExpandAny(true); |
129 | 63.9k | printer.SetInsertSilentMarker(internal::enable_debug_text_format_marker.load( |
130 | 63.9k | std::memory_order_relaxed)); |
131 | 63.9k | printer.SetRedactDebugString( |
132 | 63.9k | internal::enable_debug_text_redaction_marker.load( |
133 | 63.9k | std::memory_order_relaxed)); |
134 | 63.9k | printer.SetRandomizeDebugString( |
135 | 63.9k | internal::enable_debug_text_random_marker.load( |
136 | 63.9k | std::memory_order_relaxed)); |
137 | | |
138 | 63.9k | printer.PrintToString(*this, &debug_string); |
139 | | |
140 | 63.9k | return debug_string; |
141 | 63.9k | } |
142 | | |
143 | 0 | std::string Message::ShortDebugString() const { |
144 | | // Indicate all scoped reflection calls are from DebugString function. |
145 | 0 | ScopedReflectionMode scope(ReflectionMode::kDebugString); |
146 | 0 | std::string debug_string; |
147 | |
|
148 | 0 | TextFormat::Printer printer; |
149 | 0 | printer.SetSingleLineMode(true); |
150 | 0 | printer.SetExpandAny(true); |
151 | 0 | printer.SetInsertSilentMarker(internal::enable_debug_text_format_marker.load( |
152 | 0 | std::memory_order_relaxed)); |
153 | 0 | printer.SetRedactDebugString( |
154 | 0 | internal::enable_debug_text_redaction_marker.load( |
155 | 0 | std::memory_order_relaxed)); |
156 | 0 | printer.SetRandomizeDebugString( |
157 | 0 | internal::enable_debug_text_random_marker.load( |
158 | 0 | std::memory_order_relaxed)); |
159 | |
|
160 | 0 | printer.PrintToString(*this, &debug_string); |
161 | | // Single line mode currently might have an extra space at the end. |
162 | 0 | if (!debug_string.empty() && debug_string[debug_string.size() - 1] == ' ') { |
163 | 0 | debug_string.resize(debug_string.size() - 1); |
164 | 0 | } |
165 | |
|
166 | 0 | return debug_string; |
167 | 0 | } |
168 | | |
169 | 0 | std::string Message::Utf8DebugString() const { |
170 | | // Indicate all scoped reflection calls are from DebugString function. |
171 | 0 | ScopedReflectionMode scope(ReflectionMode::kDebugString); |
172 | 0 | std::string debug_string; |
173 | |
|
174 | 0 | TextFormat::Printer printer; |
175 | 0 | printer.SetUseUtf8StringEscaping(true); |
176 | 0 | printer.SetExpandAny(true); |
177 | 0 | printer.SetInsertSilentMarker(internal::enable_debug_text_format_marker.load( |
178 | 0 | std::memory_order_relaxed)); |
179 | 0 | printer.SetRedactDebugString( |
180 | 0 | internal::enable_debug_text_redaction_marker.load( |
181 | 0 | std::memory_order_relaxed)); |
182 | 0 | printer.SetRandomizeDebugString( |
183 | 0 | internal::enable_debug_text_random_marker.load( |
184 | 0 | std::memory_order_relaxed)); |
185 | |
|
186 | 0 | printer.PrintToString(*this, &debug_string); |
187 | |
|
188 | 0 | return debug_string; |
189 | 0 | } |
190 | | |
191 | 0 | void Message::PrintDebugString() const { printf("%s", DebugString().c_str()); } |
192 | | |
193 | | namespace internal { |
194 | | |
195 | | PROTOBUF_EXPORT void PerformAbslStringify( |
196 | 0 | const Message& message, absl::FunctionRef<void(absl::string_view)> append) { |
197 | | // Indicate all scoped reflection calls are from DebugString function. |
198 | 0 | ScopedReflectionMode scope(ReflectionMode::kDebugString); |
199 | | |
200 | | // TODO(b/249835002): consider using the single line version for short |
201 | 0 | TextFormat::Printer printer; |
202 | 0 | printer.SetExpandAny(true); |
203 | 0 | printer.SetRedactDebugString(true); |
204 | 0 | printer.SetRandomizeDebugString(true); |
205 | 0 | std::string result; |
206 | 0 | printer.PrintToString(message, &result); |
207 | 0 | append(result); |
208 | 0 | } |
209 | | |
210 | | } // namespace internal |
211 | | |
212 | | |
213 | | // =========================================================================== |
214 | | // Implementation of the parse information tree class. |
215 | | void TextFormat::ParseInfoTree::RecordLocation( |
216 | 0 | const FieldDescriptor* field, TextFormat::ParseLocationRange range) { |
217 | 0 | locations_[field].push_back(range); |
218 | 0 | } |
219 | | |
220 | | TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::CreateNested( |
221 | 0 | const FieldDescriptor* field) { |
222 | | // Owned by us in the map. |
223 | 0 | auto& vec = nested_[field]; |
224 | 0 | vec.emplace_back(new TextFormat::ParseInfoTree()); |
225 | 0 | return vec.back().get(); |
226 | 0 | } |
227 | | |
228 | 0 | void CheckFieldIndex(const FieldDescriptor* field, int index) { |
229 | 0 | if (field == nullptr) { |
230 | 0 | return; |
231 | 0 | } |
232 | | |
233 | 0 | if (field->is_repeated() && index == -1) { |
234 | 0 | ABSL_DLOG(FATAL) << "Index must be in range of repeated field values. " |
235 | 0 | << "Field: " << field->name(); |
236 | 0 | } else if (!field->is_repeated() && index != -1) { |
237 | 0 | ABSL_DLOG(FATAL) << "Index must be -1 for singular fields." |
238 | 0 | << "Field: " << field->name(); |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | TextFormat::ParseLocationRange TextFormat::ParseInfoTree::GetLocationRange( |
243 | 0 | const FieldDescriptor* field, int index) const { |
244 | 0 | CheckFieldIndex(field, index); |
245 | 0 | if (index == -1) { |
246 | 0 | index = 0; |
247 | 0 | } |
248 | |
|
249 | 0 | auto it = locations_.find(field); |
250 | 0 | if (it == locations_.end() || |
251 | 0 | index >= static_cast<int64_t>(it->second.size())) { |
252 | 0 | return TextFormat::ParseLocationRange(); |
253 | 0 | } |
254 | | |
255 | 0 | return it->second[static_cast<size_t>(index)]; |
256 | 0 | } |
257 | | |
258 | | TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested( |
259 | 0 | const FieldDescriptor* field, int index) const { |
260 | 0 | CheckFieldIndex(field, index); |
261 | 0 | if (index == -1) { |
262 | 0 | index = 0; |
263 | 0 | } |
264 | |
|
265 | 0 | auto it = nested_.find(field); |
266 | 0 | if (it == nested_.end() || index >= static_cast<int64_t>(it->second.size())) { |
267 | 0 | return nullptr; |
268 | 0 | } |
269 | | |
270 | 0 | return it->second[static_cast<size_t>(index)].get(); |
271 | 0 | } |
272 | | |
273 | | namespace { |
274 | | // These functions implement the behavior of the "default" TextFormat::Finder, |
275 | | // they are defined as standalone to be called when finder_ is nullptr. |
276 | | const FieldDescriptor* DefaultFinderFindExtension(Message* message, |
277 | 10 | const std::string& name) { |
278 | 10 | const Descriptor* descriptor = message->GetDescriptor(); |
279 | 10 | return descriptor->file()->pool()->FindExtensionByPrintableName(descriptor, |
280 | 10 | name); |
281 | 10 | } |
282 | | |
283 | | const FieldDescriptor* DefaultFinderFindExtensionByNumber( |
284 | 0 | const Descriptor* descriptor, int number) { |
285 | 0 | return descriptor->file()->pool()->FindExtensionByNumber(descriptor, number); |
286 | 0 | } |
287 | | |
288 | | const Descriptor* DefaultFinderFindAnyType(const Message& message, |
289 | | const std::string& prefix, |
290 | 60.9k | const std::string& name) { |
291 | 60.9k | if (prefix != internal::kTypeGoogleApisComPrefix && |
292 | 60.9k | prefix != internal::kTypeGoogleProdComPrefix) { |
293 | 1.45k | return nullptr; |
294 | 1.45k | } |
295 | 59.4k | return message.GetDescriptor()->file()->pool()->FindMessageTypeByName(name); |
296 | 60.9k | } |
297 | | } // namespace |
298 | | |
299 | | // =========================================================================== |
300 | | // Internal class for parsing an ASCII representation of a Protocol Message. |
301 | | // This class makes use of the Protocol Message compiler's tokenizer found |
302 | | // in //third_party/protobuf/io/tokenizer.h. Note that class's Parse |
303 | | // method is *not* thread-safe and should only be used in a single thread at |
304 | | // a time. |
305 | | |
306 | | // Makes code slightly more readable. The meaning of "DO(foo)" is |
307 | | // "Execute foo and fail if it fails.", where failure is indicated by |
308 | | // returning false. Borrowed from parser.cc (Thanks Kenton!). |
309 | | #define DO(STATEMENT) \ |
310 | 94.1M | if (STATEMENT) { \ |
311 | 94.1M | } else { \ |
312 | 10.6k | return false; \ |
313 | 10.6k | } |
314 | | |
315 | | class TextFormat::Parser::ParserImpl { |
316 | | public: |
317 | | // Determines if repeated values for non-repeated fields and |
318 | | // oneofs are permitted, e.g., the string "foo: 1 foo: 2" for a |
319 | | // required/optional field named "foo", or "baz: 1 bar: 2" |
320 | | // where "baz" and "bar" are members of the same oneof. |
321 | | enum SingularOverwritePolicy { |
322 | | ALLOW_SINGULAR_OVERWRITES = 0, // the last value is retained |
323 | | FORBID_SINGULAR_OVERWRITES = 1, // an error is issued |
324 | | }; |
325 | | |
326 | | ParserImpl(const Descriptor* root_message_type, |
327 | | io::ZeroCopyInputStream* input_stream, |
328 | | io::ErrorCollector* error_collector, |
329 | | const TextFormat::Finder* finder, ParseInfoTree* parse_info_tree, |
330 | | SingularOverwritePolicy singular_overwrite_policy, |
331 | | bool allow_case_insensitive_field, bool allow_unknown_field, |
332 | | bool allow_unknown_extension, bool allow_unknown_enum, |
333 | | bool allow_field_number, bool allow_relaxed_whitespace, |
334 | | bool allow_partial, int recursion_limit, |
335 | | bool error_on_no_op_fields) |
336 | | : error_collector_(error_collector), |
337 | | finder_(finder), |
338 | | parse_info_tree_(parse_info_tree), |
339 | | tokenizer_error_collector_(this), |
340 | | tokenizer_(input_stream, &tokenizer_error_collector_), |
341 | | root_message_type_(root_message_type), |
342 | | singular_overwrite_policy_(singular_overwrite_policy), |
343 | | allow_case_insensitive_field_(allow_case_insensitive_field), |
344 | | allow_unknown_field_(allow_unknown_field), |
345 | | allow_unknown_extension_(allow_unknown_extension), |
346 | | allow_unknown_enum_(allow_unknown_enum), |
347 | | allow_field_number_(allow_field_number), |
348 | | allow_partial_(allow_partial), |
349 | | initial_recursion_limit_(recursion_limit), |
350 | | recursion_limit_(recursion_limit), |
351 | | had_silent_marker_(false), |
352 | | had_errors_(false), |
353 | 149k | error_on_no_op_fields_(error_on_no_op_fields) { |
354 | | // For backwards-compatibility with proto1, we need to allow the 'f' suffix |
355 | | // for floats. |
356 | 149k | tokenizer_.set_allow_f_after_float(true); |
357 | | |
358 | | // '#' starts a comment. |
359 | 149k | tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE); |
360 | | |
361 | 149k | if (allow_relaxed_whitespace) { |
362 | 0 | tokenizer_.set_require_space_after_number(false); |
363 | 0 | tokenizer_.set_allow_multiline_strings(true); |
364 | 0 | } |
365 | | |
366 | | // Consume the starting token. |
367 | 149k | tokenizer_.Next(); |
368 | 149k | } |
369 | | ParserImpl(const ParserImpl&) = delete; |
370 | | ParserImpl& operator=(const ParserImpl&) = delete; |
371 | 149k | ~ParserImpl() {} |
372 | | |
373 | | // Parses the ASCII representation specified in input and saves the |
374 | | // information into the output pointer (a Message). Returns |
375 | | // false if an error occurs (an error will also be logged to |
376 | | // ABSL_LOG(ERROR)). |
377 | 149k | bool Parse(Message* output) { |
378 | | // Consume fields until we cannot do so anymore. |
379 | 1.89M | while (true) { |
380 | 1.89M | if (LookingAtType(io::Tokenizer::TYPE_END)) { |
381 | | // Ensures recursion limit properly unwinded, but only for success |
382 | | // cases. This implicitly avoids the check when `Parse` returns false |
383 | | // via `DO(...)`. |
384 | 296k | ABSL_DCHECK(had_errors_ || recursion_limit_ == initial_recursion_limit_) |
385 | 296k | << "Recursion limit at end of parse should be " |
386 | 296k | << initial_recursion_limit_ << ", but was " << recursion_limit_ |
387 | 296k | << ". Difference of " << initial_recursion_limit_ - recursion_limit_ |
388 | 296k | << " stack frames not accounted for stack unwind."; |
389 | | |
390 | 148k | return !had_errors_; |
391 | 148k | } |
392 | | |
393 | 1.74M | DO(ConsumeField(output)); |
394 | 1.74M | } |
395 | 149k | } |
396 | | |
397 | 0 | bool ParseField(const FieldDescriptor* field, Message* output) { |
398 | 0 | bool suc; |
399 | 0 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
400 | 0 | suc = ConsumeFieldMessage(output, output->GetReflection(), field); |
401 | 0 | } else { |
402 | 0 | suc = ConsumeFieldValue(output, output->GetReflection(), field); |
403 | 0 | } |
404 | 0 | return suc && LookingAtType(io::Tokenizer::TYPE_END); |
405 | 0 | } |
406 | | |
407 | 5.45M | void ReportError(int line, int col, absl::string_view message) { |
408 | 5.45M | had_errors_ = true; |
409 | 5.45M | if (error_collector_ == nullptr) { |
410 | 5.45M | if (line >= 0) { |
411 | 10.9M | ABSL_LOG(ERROR) << "Error parsing text-format " |
412 | 10.9M | << root_message_type_->full_name() << ": " << (line + 1) |
413 | 10.9M | << ":" << (col + 1) << ": " << message; |
414 | 5.45M | } else { |
415 | 0 | ABSL_LOG(ERROR) << "Error parsing text-format " |
416 | 0 | << root_message_type_->full_name() << ": " << message; |
417 | 0 | } |
418 | 5.45M | } else { |
419 | 0 | error_collector_->RecordError(line, col, message); |
420 | 0 | } |
421 | 5.45M | } |
422 | | |
423 | 102k | void ReportWarning(int line, int col, const absl::string_view message) { |
424 | 102k | if (error_collector_ == nullptr) { |
425 | 102k | if (line >= 0) { |
426 | 205k | ABSL_LOG(WARNING) << "Warning parsing text-format " |
427 | 205k | << root_message_type_->full_name() << ": " |
428 | 205k | << (line + 1) << ":" << (col + 1) << ": " << message; |
429 | 102k | } else { |
430 | 0 | ABSL_LOG(WARNING) << "Warning parsing text-format " |
431 | 0 | << root_message_type_->full_name() << ": " << message; |
432 | 0 | } |
433 | 102k | } else { |
434 | 0 | error_collector_->RecordWarning(line, col, message); |
435 | 0 | } |
436 | 102k | } |
437 | | |
438 | | private: |
439 | | static constexpr int32_t kint32max = std::numeric_limits<int32_t>::max(); |
440 | | static constexpr uint32_t kuint32max = std::numeric_limits<uint32_t>::max(); |
441 | | static constexpr int64_t kint64min = std::numeric_limits<int64_t>::min(); |
442 | | static constexpr int64_t kint64max = std::numeric_limits<int64_t>::max(); |
443 | | static constexpr uint64_t kuint64max = std::numeric_limits<uint64_t>::max(); |
444 | | |
445 | | // Reports an error with the given message with information indicating |
446 | | // the position (as derived from the current token). |
447 | 1.40k | void ReportError(absl::string_view message) { |
448 | 1.40k | ReportError(tokenizer_.current().line, tokenizer_.current().column, |
449 | 1.40k | message); |
450 | 1.40k | } |
451 | | |
452 | | // Reports a warning with the given message with information indicating |
453 | | // the position (as derived from the current token). |
454 | 102k | void ReportWarning(absl::string_view message) { |
455 | 102k | ReportWarning(tokenizer_.current().line, tokenizer_.current().column, |
456 | 102k | message); |
457 | 102k | } |
458 | | |
459 | | // Consumes the specified message with the given starting delimiter. |
460 | | // This method checks to see that the end delimiter at the conclusion of |
461 | | // the consumption matches the starting delimiter passed in here. |
462 | 8.34M | bool ConsumeMessage(Message* message, const std::string delimiter) { |
463 | 22.0M | while (!LookingAt(">") && !LookingAt("}")) { |
464 | 13.6M | DO(ConsumeField(message)); |
465 | 13.6M | } |
466 | | |
467 | | // Confirm that we have a valid ending delimiter. |
468 | 8.34M | DO(Consume(delimiter)); |
469 | 8.34M | return true; |
470 | 8.34M | } |
471 | | |
472 | | // Consume either "<" or "{". |
473 | 8.35M | bool ConsumeMessageDelimiter(std::string* delimiter) { |
474 | 8.35M | if (TryConsume("<")) { |
475 | 166 | *delimiter = ">"; |
476 | 8.35M | } else { |
477 | 8.35M | DO(Consume("{")); |
478 | 8.35M | *delimiter = "}"; |
479 | 8.35M | } |
480 | 8.35M | return true; |
481 | 8.35M | } |
482 | | |
483 | | |
484 | | // Consumes the current field (as returned by the tokenizer) on the |
485 | | // passed in message. |
486 | 15.4M | bool ConsumeField(Message* message) { |
487 | 15.4M | const Reflection* reflection = message->GetReflection(); |
488 | 15.4M | const Descriptor* descriptor = message->GetDescriptor(); |
489 | | |
490 | 15.4M | std::string field_name; |
491 | 15.4M | bool reserved_field = false; |
492 | 15.4M | const FieldDescriptor* field = nullptr; |
493 | 15.4M | int start_line = tokenizer_.current().line; |
494 | 15.4M | int start_column = tokenizer_.current().column; |
495 | | |
496 | 15.4M | const FieldDescriptor* any_type_url_field; |
497 | 15.4M | const FieldDescriptor* any_value_field; |
498 | 15.4M | if (internal::GetAnyFieldDescriptors(*message, &any_type_url_field, |
499 | 15.4M | &any_value_field) && |
500 | 15.4M | TryConsume("[")) { |
501 | 843 | std::string full_type_name, prefix; |
502 | 843 | DO(ConsumeAnyTypeUrl(&full_type_name, &prefix)); |
503 | 843 | std::string prefix_and_full_type_name = |
504 | 843 | absl::StrCat(prefix, full_type_name); |
505 | 843 | DO(ConsumeBeforeWhitespace("]")); |
506 | 843 | TryConsumeWhitespace(); |
507 | | // ':' is optional between message labels and values. |
508 | 843 | if (TryConsumeBeforeWhitespace(":")) { |
509 | 0 | TryConsumeWhitespace(); |
510 | 0 | } |
511 | 843 | std::string serialized_value; |
512 | 843 | const Descriptor* value_descriptor = |
513 | 843 | finder_ ? finder_->FindAnyType(*message, prefix, full_type_name) |
514 | 843 | : DefaultFinderFindAnyType(*message, prefix, full_type_name); |
515 | 843 | if (value_descriptor == nullptr) { |
516 | 0 | ReportError(absl::StrCat("Could not find type \"", |
517 | 0 | prefix_and_full_type_name, |
518 | 0 | "\" stored in google.protobuf.Any.")); |
519 | 0 | return false; |
520 | 0 | } |
521 | 843 | DO(ConsumeAnyValue(value_descriptor, &serialized_value)); |
522 | 806 | if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) { |
523 | | // Fail if any_type_url_field has already been specified. |
524 | 806 | if ((!any_type_url_field->is_repeated() && |
525 | 806 | reflection->HasField(*message, any_type_url_field)) || |
526 | 806 | (!any_value_field->is_repeated() && |
527 | 806 | reflection->HasField(*message, any_value_field))) { |
528 | 0 | ReportError("Non-repeated Any specified multiple times."); |
529 | 0 | return false; |
530 | 0 | } |
531 | 806 | } |
532 | 806 | reflection->SetString(message, any_type_url_field, |
533 | 806 | std::move(prefix_and_full_type_name)); |
534 | 806 | reflection->SetString(message, any_value_field, |
535 | 806 | std::move(serialized_value)); |
536 | 806 | return true; |
537 | 806 | } |
538 | 15.4M | if (TryConsume("[")) { |
539 | | // Extension. |
540 | 10 | DO(ConsumeFullTypeName(&field_name)); |
541 | 10 | DO(ConsumeBeforeWhitespace("]")); |
542 | 10 | TryConsumeWhitespace(); |
543 | | |
544 | 10 | field = finder_ ? finder_->FindExtension(message, field_name) |
545 | 10 | : DefaultFinderFindExtension(message, field_name); |
546 | | |
547 | 10 | if (field == nullptr) { |
548 | 10 | if (!allow_unknown_field_ && !allow_unknown_extension_) { |
549 | 0 | ReportError(absl::StrCat("Extension \"", field_name, |
550 | 0 | "\" is not defined or " |
551 | 0 | "is not an extension of \"", |
552 | 0 | descriptor->full_name(), "\".")); |
553 | 0 | return false; |
554 | 10 | } else { |
555 | 10 | ReportWarning(absl::StrCat( |
556 | 10 | "Ignoring extension \"", field_name, |
557 | 10 | "\" which is not defined or is not an extension of \"", |
558 | 10 | descriptor->full_name(), "\".")); |
559 | 10 | } |
560 | 10 | } |
561 | 15.4M | } else { |
562 | 15.4M | DO(ConsumeIdentifierBeforeWhitespace(&field_name)); |
563 | 15.4M | TryConsumeWhitespace(); |
564 | | |
565 | 15.4M | int32_t field_number; |
566 | 15.4M | if (allow_field_number_ && absl::SimpleAtoi(field_name, &field_number)) { |
567 | 0 | if (descriptor->IsExtensionNumber(field_number)) { |
568 | 0 | field = finder_ |
569 | 0 | ? finder_->FindExtensionByNumber(descriptor, field_number) |
570 | 0 | : DefaultFinderFindExtensionByNumber(descriptor, |
571 | 0 | field_number); |
572 | 0 | } else if (descriptor->IsReservedNumber(field_number)) { |
573 | 0 | reserved_field = true; |
574 | 0 | } else { |
575 | 0 | field = descriptor->FindFieldByNumber(field_number); |
576 | 0 | } |
577 | 15.4M | } else { |
578 | 15.4M | field = descriptor->FindFieldByName(field_name); |
579 | | // Group names are expected to be capitalized as they appear in the |
580 | | // .proto file, which actually matches their type names, not their |
581 | | // field names. |
582 | 15.4M | if (field == nullptr) { |
583 | 10.0k | std::string lower_field_name = field_name; |
584 | 10.0k | absl::AsciiStrToLower(&lower_field_name); |
585 | 10.0k | field = descriptor->FindFieldByName(lower_field_name); |
586 | | // If the case-insensitive match worked but the field is NOT a group, |
587 | 10.0k | if (field != nullptr && |
588 | 10.0k | field->type() != FieldDescriptor::TYPE_GROUP) { |
589 | 9 | field = nullptr; |
590 | 9 | } |
591 | 10.0k | } |
592 | | // Again, special-case group names as described above. |
593 | 15.4M | if (field != nullptr && field->type() == FieldDescriptor::TYPE_GROUP && |
594 | 15.4M | field->message_type()->name() != field_name) { |
595 | 0 | field = nullptr; |
596 | 0 | } |
597 | | |
598 | 15.4M | if (field == nullptr && allow_case_insensitive_field_) { |
599 | 0 | std::string lower_field_name = field_name; |
600 | 0 | absl::AsciiStrToLower(&lower_field_name); |
601 | 0 | field = descriptor->FindFieldByLowercaseName(lower_field_name); |
602 | 0 | } |
603 | | |
604 | 15.4M | if (field == nullptr) { |
605 | 10.0k | reserved_field = descriptor->IsReservedName(field_name); |
606 | 10.0k | } |
607 | 15.4M | } |
608 | | |
609 | 15.4M | if (field == nullptr && !reserved_field) { |
610 | 9.99k | if (!allow_unknown_field_) { |
611 | 0 | ReportError(absl::StrCat("Message type \"", descriptor->full_name(), |
612 | 0 | "\" has no field named \"", field_name, |
613 | 0 | "\".")); |
614 | 0 | return false; |
615 | 9.99k | } else { |
616 | 9.99k | ReportWarning(absl::StrCat("Message type \"", descriptor->full_name(), |
617 | 9.99k | "\" has no field named \"", field_name, |
618 | 9.99k | "\".")); |
619 | 9.99k | } |
620 | 9.99k | } |
621 | 15.4M | } |
622 | | |
623 | | // Skips unknown or reserved fields. |
624 | 15.4M | if (field == nullptr) { |
625 | 10.0k | ABSL_CHECK(allow_unknown_field_ || allow_unknown_extension_ || |
626 | 10.0k | reserved_field); |
627 | | |
628 | | // Try to guess the type of this field. |
629 | | // If this field is not a message, there should be a ":" between the |
630 | | // field name and the field value and also the field value should not |
631 | | // start with "{" or "<" which indicates the beginning of a message body. |
632 | | // If there is no ":" or there is a "{" or "<" after ":", this field has |
633 | | // to be a message or the input is ill-formed. |
634 | 10.0k | if (TryConsumeBeforeWhitespace(":")) { |
635 | 5.36k | TryConsumeWhitespace(); |
636 | 5.36k | if (!LookingAt("{") && !LookingAt("<")) { |
637 | 5.36k | return SkipFieldValue(); |
638 | 5.36k | } |
639 | 5.36k | } |
640 | 4.66k | return SkipFieldMessage(); |
641 | 10.0k | } |
642 | | |
643 | 15.4M | if (field->options().deprecated()) { |
644 | 92.5k | ReportWarning(absl::StrCat("text format contains deprecated field \"", |
645 | 92.5k | field_name, "\"")); |
646 | 92.5k | } |
647 | | |
648 | 15.4M | if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) { |
649 | | // Fail if the field is not repeated and it has already been specified. |
650 | 15.4M | if (!field->is_repeated() && reflection->HasField(*message, field)) { |
651 | 0 | ReportError(absl::StrCat("Non-repeated field \"", field_name, |
652 | 0 | "\" is specified multiple times.")); |
653 | 0 | return false; |
654 | 0 | } |
655 | | // Fail if the field is a member of a oneof and another member has already |
656 | | // been specified. |
657 | 15.4M | const OneofDescriptor* oneof = field->containing_oneof(); |
658 | 15.4M | if (oneof != nullptr && reflection->HasOneof(*message, oneof)) { |
659 | 0 | const FieldDescriptor* other_field = |
660 | 0 | reflection->GetOneofFieldDescriptor(*message, oneof); |
661 | 0 | ReportError(absl::StrCat("Field \"", field_name, |
662 | 0 | "\" is specified along with " |
663 | 0 | "field \"", |
664 | 0 | other_field->name(), |
665 | 0 | "\", another member " |
666 | 0 | "of oneof \"", |
667 | 0 | oneof->name(), "\".")); |
668 | 0 | return false; |
669 | 0 | } |
670 | 15.4M | } |
671 | | |
672 | | // Perform special handling for embedded message types. |
673 | 15.4M | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
674 | | // ':' is optional here. |
675 | 8.34M | bool consumed_semicolon = TryConsumeBeforeWhitespace(":"); |
676 | 8.34M | if (consumed_semicolon) { |
677 | 8 | TryConsumeWhitespace(); |
678 | 8 | } |
679 | 8.34M | if (consumed_semicolon && field->options().weak() && |
680 | 8.34M | LookingAtType(io::Tokenizer::TYPE_STRING)) { |
681 | | // we are getting a bytes string for a weak field. |
682 | 0 | std::string tmp; |
683 | 0 | DO(ConsumeString(&tmp)); |
684 | 0 | MessageFactory* factory = |
685 | 0 | finder_ ? finder_->FindExtensionFactory(field) : nullptr; |
686 | 0 | reflection->MutableMessage(message, field, factory) |
687 | 0 | ->ParseFromString(tmp); |
688 | 0 | goto label_skip_parsing; |
689 | 0 | } |
690 | 8.34M | } else { |
691 | | // ':' is required here. |
692 | 7.05M | DO(ConsumeBeforeWhitespace(":")); |
693 | 7.05M | TryConsumeWhitespace(); |
694 | 7.05M | } |
695 | | |
696 | 15.4M | if (field->is_repeated() && TryConsume("[")) { |
697 | | // Short repeated format, e.g. "foo: [1, 2, 3]". |
698 | 89 | if (!TryConsume("]")) { |
699 | | // "foo: []" is treated as empty. |
700 | 185 | while (true) { |
701 | 185 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
702 | | // Perform special handling for embedded message types. |
703 | 185 | DO(ConsumeFieldMessage(message, reflection, field)); |
704 | 185 | } else { |
705 | 0 | DO(ConsumeFieldValue(message, reflection, field)); |
706 | 0 | } |
707 | 185 | if (TryConsume("]")) { |
708 | 79 | break; |
709 | 79 | } |
710 | 106 | DO(Consume(",")); |
711 | 106 | } |
712 | 79 | } |
713 | 15.4M | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
714 | 8.34M | DO(ConsumeFieldMessage(message, reflection, field)); |
715 | 8.34M | } else { |
716 | 7.05M | DO(ConsumeFieldValue(message, reflection, field)); |
717 | 7.05M | } |
718 | 15.3M | label_skip_parsing: |
719 | | // For historical reasons, fields may optionally be separated by commas or |
720 | | // semicolons. |
721 | 15.3M | TryConsume(";") || TryConsume(","); |
722 | | |
723 | | // If a parse info tree exists, add the location for the parsed |
724 | | // field. |
725 | 15.3M | if (parse_info_tree_ != nullptr) { |
726 | 0 | int end_line = tokenizer_.previous().line; |
727 | 0 | int end_column = tokenizer_.previous().end_column; |
728 | |
|
729 | 0 | RecordLocation(parse_info_tree_, field, |
730 | 0 | ParseLocationRange(ParseLocation(start_line, start_column), |
731 | 0 | ParseLocation(end_line, end_column))); |
732 | 0 | } |
733 | | |
734 | 15.3M | return true; |
735 | 15.4M | } |
736 | | |
737 | | // Skips the next field including the field's name and value. |
738 | 11.7k | bool SkipField() { |
739 | 11.7k | std::string field_name; |
740 | 11.7k | if (TryConsume("[")) { |
741 | | // Extension name or type URL. |
742 | 21 | DO(ConsumeTypeUrlOrFullTypeName(&field_name)); |
743 | 21 | DO(ConsumeBeforeWhitespace("]")); |
744 | 11.6k | } else { |
745 | 11.6k | DO(ConsumeIdentifierBeforeWhitespace(&field_name)); |
746 | 11.6k | } |
747 | 11.6k | TryConsumeWhitespace(); |
748 | | |
749 | | // Try to guess the type of this field. |
750 | | // If this field is not a message, there should be a ":" between the |
751 | | // field name and the field value and also the field value should not |
752 | | // start with "{" or "<" which indicates the beginning of a message body. |
753 | | // If there is no ":" or there is a "{" or "<" after ":", this field has |
754 | | // to be a message or the input is ill-formed. |
755 | 11.6k | if (TryConsumeBeforeWhitespace(":")) { |
756 | 5.64k | TryConsumeWhitespace(); |
757 | 5.64k | if (!LookingAt("{") && !LookingAt("<")) { |
758 | 5.64k | DO(SkipFieldValue()); |
759 | 5.59k | } else { |
760 | 6 | DO(SkipFieldMessage()); |
761 | 0 | } |
762 | 6.04k | } else { |
763 | 6.04k | DO(SkipFieldMessage()); |
764 | 5.72k | } |
765 | | // For historical reasons, fields may optionally be separated by commas or |
766 | | // semicolons. |
767 | 11.3k | TryConsume(";") || TryConsume(","); |
768 | 11.3k | return true; |
769 | 11.6k | } |
770 | | |
771 | | bool ConsumeFieldMessage(Message* message, const Reflection* reflection, |
772 | 8.34M | const FieldDescriptor* field) { |
773 | 8.34M | if (--recursion_limit_ < 0) { |
774 | 0 | ReportError( |
775 | 0 | absl::StrCat("Message is too deep, the parser exceeded the " |
776 | 0 | "configured recursion limit of ", |
777 | 0 | initial_recursion_limit_, ".")); |
778 | 0 | return false; |
779 | 0 | } |
780 | | // If the parse information tree is not nullptr, create a nested one |
781 | | // for the nested message. |
782 | 8.34M | ParseInfoTree* parent = parse_info_tree_; |
783 | 8.34M | if (parent != nullptr) { |
784 | 0 | parse_info_tree_ = CreateNested(parent, field); |
785 | 0 | } |
786 | | |
787 | 8.34M | std::string delimiter; |
788 | 8.34M | DO(ConsumeMessageDelimiter(&delimiter)); |
789 | 8.34M | MessageFactory* factory = |
790 | 8.34M | finder_ ? finder_->FindExtensionFactory(field) : nullptr; |
791 | 8.34M | if (field->is_repeated()) { |
792 | 4.83M | DO(ConsumeMessage(reflection->AddMessage(message, field, factory), |
793 | 4.83M | delimiter)); |
794 | 4.83M | } else { |
795 | 3.50M | DO(ConsumeMessage(reflection->MutableMessage(message, field, factory), |
796 | 3.50M | delimiter)); |
797 | 3.50M | } |
798 | | |
799 | 8.34M | ++recursion_limit_; |
800 | | |
801 | | // Reset the parse information tree. |
802 | 8.34M | parse_info_tree_ = parent; |
803 | 8.34M | return true; |
804 | 8.34M | } |
805 | | |
806 | | // Skips the whole body of a message including the beginning delimiter and |
807 | | // the ending delimiter. |
808 | 10.7k | bool SkipFieldMessage() { |
809 | 10.7k | if (--recursion_limit_ < 0) { |
810 | 0 | ReportError( |
811 | 0 | absl::StrCat("Message is too deep, the parser exceeded the " |
812 | 0 | "configured recursion limit of ", |
813 | 0 | initial_recursion_limit_, ".")); |
814 | 0 | return false; |
815 | 0 | } |
816 | | |
817 | 10.7k | std::string delimiter; |
818 | 10.7k | DO(ConsumeMessageDelimiter(&delimiter)); |
819 | 21.6k | while (!LookingAt(">") && !LookingAt("}")) { |
820 | 11.7k | DO(SkipField()); |
821 | 11.3k | } |
822 | 9.97k | DO(Consume(delimiter)); |
823 | | |
824 | 9.97k | ++recursion_limit_; |
825 | 9.97k | return true; |
826 | 9.97k | } |
827 | | |
828 | | bool ConsumeFieldValue(Message* message, const Reflection* reflection, |
829 | 7.05M | const FieldDescriptor* field) { |
830 | | // Define an easy to use macro for setting fields. This macro checks |
831 | | // to see if the field is repeated (in which case we need to use the Add |
832 | | // methods or not (in which case we need to use the Set methods). |
833 | | // When checking for no-op operations, We verify that both the existing value in |
834 | | // the message and the new value are the default. If the existing field value is |
835 | | // not the default, setting it to the default should not be treated as a no-op. |
836 | 7.05M | #define SET_FIELD(CPPTYPE, CPPTYPELCASE, VALUE) \ |
837 | 7.05M | if (field->is_repeated()) { \ |
838 | 335k | reflection->Add##CPPTYPE(message, field, VALUE); \ |
839 | 6.72M | } else { \ |
840 | 6.72M | if (error_on_no_op_fields_ && !field->has_presence() && \ |
841 | 6.72M | field->default_value_##CPPTYPELCASE() == \ |
842 | 0 | reflection->Get##CPPTYPE(*message, field) && \ |
843 | 6.72M | field->default_value_##CPPTYPELCASE() == VALUE) { \ |
844 | 0 | ReportError("Input field " + field->full_name() + \ |
845 | 0 | " did not change resulting proto."); \ |
846 | 6.72M | } else { \ |
847 | 6.72M | reflection->Set##CPPTYPE(message, field, std::move(VALUE)); \ |
848 | 6.72M | } \ |
849 | 6.72M | } |
850 | | |
851 | 7.05M | switch (field->cpp_type()) { |
852 | 36.5k | case FieldDescriptor::CPPTYPE_INT32: { |
853 | 36.5k | int64_t value; |
854 | 36.5k | DO(ConsumeSignedInteger(&value, kint32max)); |
855 | 36.5k | SET_FIELD(Int32, int32, static_cast<int32_t>(value)); |
856 | 36.5k | break; |
857 | 36.5k | } |
858 | | |
859 | 279k | case FieldDescriptor::CPPTYPE_UINT32: { |
860 | 279k | uint64_t value; |
861 | 279k | DO(ConsumeUnsignedInteger(&value, kuint32max)); |
862 | 279k | SET_FIELD(UInt32, uint32, static_cast<uint32_t>(value)); |
863 | 279k | break; |
864 | 279k | } |
865 | | |
866 | 152k | case FieldDescriptor::CPPTYPE_INT64: { |
867 | 152k | int64_t value; |
868 | 152k | DO(ConsumeSignedInteger(&value, kint64max)); |
869 | 152k | SET_FIELD(Int64, int64, value); |
870 | 152k | break; |
871 | 152k | } |
872 | | |
873 | 39.2k | case FieldDescriptor::CPPTYPE_UINT64: { |
874 | 39.2k | uint64_t value; |
875 | 39.2k | DO(ConsumeUnsignedInteger(&value, kuint64max)); |
876 | 39.2k | SET_FIELD(UInt64, uint64, value); |
877 | 39.2k | break; |
878 | 39.2k | } |
879 | | |
880 | 0 | case FieldDescriptor::CPPTYPE_FLOAT: { |
881 | 0 | double value; |
882 | 0 | DO(ConsumeDouble(&value)); |
883 | 0 | SET_FIELD(Float, float, io::SafeDoubleToFloat(value)); |
884 | 0 | break; |
885 | 0 | } |
886 | | |
887 | 40.0k | case FieldDescriptor::CPPTYPE_DOUBLE: { |
888 | 40.0k | double value; |
889 | 40.0k | DO(ConsumeDouble(&value)); |
890 | 40.0k | SET_FIELD(Double, double, value); |
891 | 40.0k | break; |
892 | 40.0k | } |
893 | | |
894 | 6.04M | case FieldDescriptor::CPPTYPE_STRING: { |
895 | 6.04M | std::string value; |
896 | 6.04M | DO(ConsumeString(&value)); |
897 | 6.04M | SET_FIELD(String, string, value); |
898 | 6.04M | break; |
899 | 6.04M | } |
900 | | |
901 | 297k | case FieldDescriptor::CPPTYPE_BOOL: { |
902 | 297k | if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
903 | 0 | uint64_t value; |
904 | 0 | DO(ConsumeUnsignedInteger(&value, 1)); |
905 | 0 | SET_FIELD(Bool, bool, value); |
906 | 297k | } else { |
907 | 297k | std::string value; |
908 | 297k | DO(ConsumeIdentifier(&value)); |
909 | 297k | if (value == "true" || value == "True" || value == "t") { |
910 | 289k | SET_FIELD(Bool, bool, true); |
911 | 289k | } else if (value == "false" || value == "False" || value == "f") { |
912 | 7.88k | SET_FIELD(Bool, bool, false); |
913 | 7.88k | } else { |
914 | 75 | ReportError(absl::StrCat("Invalid value for boolean field \"", |
915 | 75 | field->name(), "\". Value: \"", value, |
916 | 75 | "\".")); |
917 | 75 | return false; |
918 | 75 | } |
919 | 297k | } |
920 | 297k | break; |
921 | 297k | } |
922 | | |
923 | 297k | case FieldDescriptor::CPPTYPE_ENUM: { |
924 | 167k | std::string value; |
925 | 167k | int64_t int_value = kint64max; |
926 | 167k | const EnumDescriptor* enum_type = field->enum_type(); |
927 | 167k | const EnumValueDescriptor* enum_value = nullptr; |
928 | | |
929 | 167k | if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { |
930 | 167k | DO(ConsumeIdentifier(&value)); |
931 | | // Find the enumeration value. |
932 | 167k | enum_value = enum_type->FindValueByName(value); |
933 | | |
934 | 167k | } else if (LookingAt("-") || |
935 | 145 | LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
936 | 144 | DO(ConsumeSignedInteger(&int_value, kint32max)); |
937 | 144 | value = absl::StrCat(int_value); // for error reporting |
938 | 144 | enum_value = enum_type->FindValueByNumber(int_value); |
939 | 144 | } else { |
940 | 1 | ReportError(absl::StrCat("Expected integer or identifier, got: ", |
941 | 1 | tokenizer_.current().text)); |
942 | 1 | return false; |
943 | 1 | } |
944 | | |
945 | 167k | if (enum_value == nullptr) { |
946 | 75 | if (int_value != kint64max && |
947 | 75 | !field->legacy_enum_field_treated_as_closed()) { |
948 | 69 | SET_FIELD(EnumValue, int64, int_value); |
949 | 69 | return true; |
950 | 69 | } else if (!allow_unknown_enum_) { |
951 | 6 | ReportError(absl::StrCat("Unknown enumeration value of \"", value, |
952 | 6 | "\" for field \"", field->name(), "\".")); |
953 | 6 | return false; |
954 | 6 | } else { |
955 | 0 | ReportWarning(absl::StrCat("Unknown enumeration value of \"", value, |
956 | 0 | "\" for field \"", field->name(), |
957 | 0 | "\".")); |
958 | 0 | return true; |
959 | 0 | } |
960 | 75 | } |
961 | | |
962 | 167k | SET_FIELD(Enum, enum, enum_value); |
963 | 167k | break; |
964 | 167k | } |
965 | | |
966 | 0 | case FieldDescriptor::CPPTYPE_MESSAGE: { |
967 | | // We should never get here. Put here instead of a default |
968 | | // so that if new types are added, we get a nice compiler warning. |
969 | 0 | ABSL_LOG(FATAL) << "Reached an unintended state: CPPTYPE_MESSAGE"; |
970 | 0 | break; |
971 | 0 | } |
972 | 7.05M | } |
973 | 7.05M | #undef SET_FIELD |
974 | 7.05M | return true; |
975 | 7.05M | } |
976 | | |
977 | 11.1k | bool SkipFieldValue() { |
978 | 11.1k | if (--recursion_limit_ < 0) { |
979 | 1 | ReportError( |
980 | 1 | absl::StrCat("Message is too deep, the parser exceeded the " |
981 | 1 | "configured recursion limit of ", |
982 | 1 | initial_recursion_limit_, ".")); |
983 | 1 | return false; |
984 | 1 | } |
985 | | |
986 | 11.1k | if (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
987 | 42.3k | while (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
988 | 37.6k | tokenizer_.Next(); |
989 | 37.6k | } |
990 | 4.69k | ++recursion_limit_; |
991 | 4.69k | return true; |
992 | 4.69k | } |
993 | 6.49k | if (TryConsume("[")) { |
994 | 170 | if (!TryConsume("]")) { |
995 | 197 | while (true) { |
996 | 197 | if (!LookingAt("{") && !LookingAt("<")) { |
997 | 196 | DO(SkipFieldValue()); |
998 | 35 | } else { |
999 | 1 | DO(SkipFieldMessage()); |
1000 | 0 | } |
1001 | 35 | if (TryConsume("]")) { |
1002 | 8 | break; |
1003 | 8 | } |
1004 | 27 | DO(Consume(",")); |
1005 | 27 | } |
1006 | 170 | } |
1007 | 8 | ++recursion_limit_; |
1008 | 8 | return true; |
1009 | 170 | } |
1010 | | // Possible field values other than string: |
1011 | | // 12345 => TYPE_INTEGER |
1012 | | // -12345 => TYPE_SYMBOL + TYPE_INTEGER |
1013 | | // 1.2345 => TYPE_FLOAT |
1014 | | // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT |
1015 | | // inf => TYPE_IDENTIFIER |
1016 | | // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER |
1017 | | // TYPE_INTEGER => TYPE_IDENTIFIER |
1018 | | // Divides them into two group, one with TYPE_SYMBOL |
1019 | | // and the other without: |
1020 | | // Group one: |
1021 | | // 12345 => TYPE_INTEGER |
1022 | | // 1.2345 => TYPE_FLOAT |
1023 | | // inf => TYPE_IDENTIFIER |
1024 | | // TYPE_INTEGER => TYPE_IDENTIFIER |
1025 | | // Group two: |
1026 | | // -12345 => TYPE_SYMBOL + TYPE_INTEGER |
1027 | | // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT |
1028 | | // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER |
1029 | | // As we can see, the field value consists of an optional '-' and one of |
1030 | | // TYPE_INTEGER, TYPE_FLOAT and TYPE_IDENTIFIER. |
1031 | 6.32k | bool has_minus = TryConsume("-"); |
1032 | 6.32k | if (!LookingAtType(io::Tokenizer::TYPE_INTEGER) && |
1033 | 6.32k | !LookingAtType(io::Tokenizer::TYPE_FLOAT) && |
1034 | 6.32k | !LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { |
1035 | 31 | std::string text = tokenizer_.current().text; |
1036 | 31 | ReportError( |
1037 | 31 | absl::StrCat("Cannot skip field value, unexpected token: ", text)); |
1038 | 31 | ++recursion_limit_; |
1039 | 31 | return false; |
1040 | 31 | } |
1041 | | // Combination of '-' and TYPE_IDENTIFIER may result in an invalid field |
1042 | | // value while other combinations all generate valid values. |
1043 | | // We check if the value of this combination is valid here. |
1044 | | // TYPE_IDENTIFIER after a '-' should be one of the float values listed |
1045 | | // below: |
1046 | | // inf, inff, infinity, nan |
1047 | 6.29k | if (has_minus && LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { |
1048 | 836 | std::string text = tokenizer_.current().text; |
1049 | 836 | absl::AsciiStrToLower(&text); |
1050 | 836 | if (text != "inf" && |
1051 | 836 | text != "infinity" && text != "nan") { |
1052 | 293 | ReportError(absl::StrCat("Invalid float number: ", text)); |
1053 | 293 | ++recursion_limit_; |
1054 | 293 | return false; |
1055 | 293 | } |
1056 | 836 | } |
1057 | 6.00k | tokenizer_.Next(); |
1058 | 6.00k | ++recursion_limit_; |
1059 | 6.00k | return true; |
1060 | 6.29k | } |
1061 | | |
1062 | | // Returns true if the current token's text is equal to that specified. |
1063 | 44.0M | bool LookingAt(const std::string& text) { |
1064 | 44.0M | return tokenizer_.current().text == text; |
1065 | 44.0M | } |
1066 | | |
1067 | | // Returns true if the current token's type is equal to that specified. |
1068 | 62.2M | bool LookingAtType(io::Tokenizer::TokenType token_type) { |
1069 | 62.2M | return tokenizer_.current().type == token_type; |
1070 | 62.2M | } |
1071 | | |
1072 | | // Consumes an identifier and saves its value in the identifier parameter. |
1073 | | // Returns false if the token is not of type IDENTIFIER. |
1074 | 15.8M | bool ConsumeIdentifier(std::string* identifier) { |
1075 | 15.8M | if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { |
1076 | 15.8M | *identifier = tokenizer_.current().text; |
1077 | 15.8M | tokenizer_.Next(); |
1078 | 15.8M | return true; |
1079 | 15.8M | } |
1080 | | |
1081 | | // If allow_field_numer_ or allow_unknown_field_ is true, we should able |
1082 | | // to parse integer identifiers. |
1083 | 1.04k | if ((allow_field_number_ || allow_unknown_field_ || |
1084 | 1.04k | allow_unknown_extension_) && |
1085 | 1.04k | LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
1086 | 417 | *identifier = tokenizer_.current().text; |
1087 | 417 | tokenizer_.Next(); |
1088 | 417 | return true; |
1089 | 417 | } |
1090 | | |
1091 | 624 | ReportError( |
1092 | 624 | absl::StrCat("Expected identifier, got: ", tokenizer_.current().text)); |
1093 | 624 | return false; |
1094 | 1.04k | } |
1095 | | |
1096 | | // Similar to `ConsumeIdentifier`, but any following whitespace token may |
1097 | | // be reported. |
1098 | 15.4M | bool ConsumeIdentifierBeforeWhitespace(std::string* identifier) { |
1099 | 15.4M | tokenizer_.set_report_whitespace(true); |
1100 | 15.4M | bool result = ConsumeIdentifier(identifier); |
1101 | 15.4M | tokenizer_.set_report_whitespace(false); |
1102 | 15.4M | return result; |
1103 | 15.4M | } |
1104 | | |
1105 | | // Consume a string of form "<id1>.<id2>....<idN>". |
1106 | 853 | bool ConsumeFullTypeName(std::string* name) { |
1107 | 853 | DO(ConsumeIdentifier(name)); |
1108 | 2.69k | while (TryConsume(".")) { |
1109 | 1.84k | std::string part; |
1110 | 1.84k | DO(ConsumeIdentifier(&part)); |
1111 | 1.84k | absl::StrAppend(name, ".", part); |
1112 | 1.84k | } |
1113 | 853 | return true; |
1114 | 853 | } |
1115 | | |
1116 | 21 | bool ConsumeTypeUrlOrFullTypeName(std::string* name) { |
1117 | 21 | DO(ConsumeIdentifier(name)); |
1118 | 21 | while (true) { |
1119 | 21 | std::string connector; |
1120 | 21 | if (TryConsume(".")) { |
1121 | 0 | connector = "."; |
1122 | 21 | } else if (TryConsume("/")) { |
1123 | 0 | connector = "/"; |
1124 | 21 | } else { |
1125 | 21 | break; |
1126 | 21 | } |
1127 | 0 | std::string part; |
1128 | 0 | DO(ConsumeIdentifier(&part)); |
1129 | 0 | *name += connector; |
1130 | 0 | *name += part; |
1131 | 0 | } |
1132 | 21 | return true; |
1133 | 21 | } |
1134 | | |
1135 | | // Consumes a string and saves its value in the text parameter. |
1136 | | // Returns false if the token is not of type STRING. |
1137 | 6.04M | bool ConsumeString(std::string* text) { |
1138 | 6.04M | if (!LookingAtType(io::Tokenizer::TYPE_STRING)) { |
1139 | 1 | ReportError( |
1140 | 1 | absl::StrCat("Expected string, got: ", tokenizer_.current().text)); |
1141 | 1 | return false; |
1142 | 1 | } |
1143 | | |
1144 | 6.04M | text->clear(); |
1145 | 14.7M | while (LookingAtType(io::Tokenizer::TYPE_STRING)) { |
1146 | 8.74M | io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text); |
1147 | | |
1148 | 8.74M | tokenizer_.Next(); |
1149 | 8.74M | } |
1150 | | |
1151 | 6.04M | return true; |
1152 | 6.04M | } |
1153 | | |
1154 | | // Consumes a uint64_t and saves its value in the value parameter. |
1155 | | // Returns false if the token is not of type INTEGER. |
1156 | 507k | bool ConsumeUnsignedInteger(uint64_t* value, uint64_t max_value) { |
1157 | 507k | if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
1158 | 1 | ReportError( |
1159 | 1 | absl::StrCat("Expected integer, got: ", tokenizer_.current().text)); |
1160 | 1 | return false; |
1161 | 1 | } |
1162 | | |
1163 | 507k | if (!io::Tokenizer::ParseInteger(tokenizer_.current().text, max_value, |
1164 | 507k | value)) { |
1165 | 0 | ReportError(absl::StrCat("Integer out of range (", |
1166 | 0 | tokenizer_.current().text, ")")); |
1167 | 0 | return false; |
1168 | 0 | } |
1169 | | |
1170 | 507k | tokenizer_.Next(); |
1171 | 507k | return true; |
1172 | 507k | } |
1173 | | |
1174 | | // Consumes an int64_t and saves its value in the value parameter. |
1175 | | // Note that since the tokenizer does not support negative numbers, |
1176 | | // we actually may consume an additional token (for the minus sign) in this |
1177 | | // method. Returns false if the token is not an integer |
1178 | | // (signed or otherwise). |
1179 | 189k | bool ConsumeSignedInteger(int64_t* value, uint64_t max_value) { |
1180 | 189k | bool negative = false; |
1181 | | |
1182 | 189k | if (TryConsume("-")) { |
1183 | 17.3k | negative = true; |
1184 | | // Two's complement always allows one more negative integer than |
1185 | | // positive. |
1186 | 17.3k | ++max_value; |
1187 | 17.3k | } |
1188 | | |
1189 | 189k | uint64_t unsigned_value; |
1190 | | |
1191 | 189k | DO(ConsumeUnsignedInteger(&unsigned_value, max_value)); |
1192 | | |
1193 | 189k | if (negative) { |
1194 | 17.3k | if ((static_cast<uint64_t>(kint64max) + 1) == unsigned_value) { |
1195 | 1.16k | *value = kint64min; |
1196 | 16.1k | } else { |
1197 | 16.1k | *value = -static_cast<int64_t>(unsigned_value); |
1198 | 16.1k | } |
1199 | 172k | } else { |
1200 | 172k | *value = static_cast<int64_t>(unsigned_value); |
1201 | 172k | } |
1202 | | |
1203 | 189k | return true; |
1204 | 189k | } |
1205 | | |
1206 | | // Consumes a double and saves its value in the value parameter. |
1207 | | // Accepts decimal numbers only, rejects hex or oct numbers. |
1208 | 22.2k | bool ConsumeUnsignedDecimalAsDouble(double* value, uint64_t max_value) { |
1209 | 22.2k | if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
1210 | 0 | ReportError( |
1211 | 0 | absl::StrCat("Expected integer, got: ", tokenizer_.current().text)); |
1212 | 0 | return false; |
1213 | 0 | } |
1214 | | |
1215 | 22.2k | const std::string& text = tokenizer_.current().text; |
1216 | 22.2k | if (IsHexNumber(text) || IsOctNumber(text)) { |
1217 | 0 | ReportError(absl::StrCat("Expect a decimal number, got: ", text)); |
1218 | 0 | return false; |
1219 | 0 | } |
1220 | | |
1221 | 22.2k | uint64_t uint64_value; |
1222 | 22.2k | if (io::Tokenizer::ParseInteger(text, max_value, &uint64_value)) { |
1223 | 22.2k | *value = static_cast<double>(uint64_value); |
1224 | 22.2k | } else { |
1225 | | // Uint64 overflow, attempt to parse as a double instead. |
1226 | 0 | *value = io::Tokenizer::ParseFloat(text); |
1227 | 0 | } |
1228 | | |
1229 | 22.2k | tokenizer_.Next(); |
1230 | 22.2k | return true; |
1231 | 22.2k | } |
1232 | | |
1233 | | // Consumes a double and saves its value in the value parameter. |
1234 | | // Note that since the tokenizer does not support negative numbers, |
1235 | | // we actually may consume an additional token (for the minus sign) in this |
1236 | | // method. Returns false if the token is not a double |
1237 | | // (signed or otherwise). |
1238 | 40.0k | bool ConsumeDouble(double* value) { |
1239 | 40.0k | bool negative = false; |
1240 | | |
1241 | 40.0k | if (TryConsume("-")) { |
1242 | 2.04k | negative = true; |
1243 | 2.04k | } |
1244 | | |
1245 | | // A double can actually be an integer, according to the tokenizer. |
1246 | | // Therefore, we must check both cases here. |
1247 | 40.0k | if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) { |
1248 | | // We have found an integer value for the double. |
1249 | 22.2k | DO(ConsumeUnsignedDecimalAsDouble(value, kuint64max)); |
1250 | 22.2k | } else if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) { |
1251 | | // We have found a float value for the double. |
1252 | 13.5k | *value = io::Tokenizer::ParseFloat(tokenizer_.current().text); |
1253 | | |
1254 | | // Mark the current token as consumed. |
1255 | 13.5k | tokenizer_.Next(); |
1256 | 13.5k | } else if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) { |
1257 | 4.24k | std::string text = tokenizer_.current().text; |
1258 | 4.24k | absl::AsciiStrToLower(&text); |
1259 | 4.24k | if (text == "inf" || |
1260 | 4.24k | text == "infinity") { |
1261 | 64 | *value = std::numeric_limits<double>::infinity(); |
1262 | 64 | tokenizer_.Next(); |
1263 | 4.18k | } else if (text == "nan") { |
1264 | 4.18k | *value = std::numeric_limits<double>::quiet_NaN(); |
1265 | 4.18k | tokenizer_.Next(); |
1266 | 4.18k | } else { |
1267 | 0 | ReportError(absl::StrCat("Expected double, got: ", text)); |
1268 | 0 | return false; |
1269 | 0 | } |
1270 | 4.24k | } else { |
1271 | 0 | ReportError( |
1272 | 0 | absl::StrCat("Expected double, got: ", tokenizer_.current().text)); |
1273 | 0 | return false; |
1274 | 0 | } |
1275 | | |
1276 | 40.0k | if (negative) { |
1277 | 2.04k | *value = -*value; |
1278 | 2.04k | } |
1279 | | |
1280 | 40.0k | return true; |
1281 | 40.0k | } |
1282 | | |
1283 | | // Consumes Any::type_url value, of form "type.googleapis.com/full.type.Name" |
1284 | | // or "type.googleprod.com/full.type.Name" |
1285 | 843 | bool ConsumeAnyTypeUrl(std::string* full_type_name, std::string* prefix) { |
1286 | | // TODO(saito) Extend Consume() to consume multiple tokens at once, so that |
1287 | | // this code can be written as just DO(Consume(kGoogleApisTypePrefix)). |
1288 | 843 | DO(ConsumeIdentifier(prefix)); |
1289 | 2.52k | while (TryConsume(".")) { |
1290 | 1.68k | std::string url; |
1291 | 1.68k | DO(ConsumeIdentifier(&url)); |
1292 | 1.68k | absl::StrAppend(prefix, ".", url); |
1293 | 1.68k | } |
1294 | 843 | DO(Consume("/")); |
1295 | 843 | absl::StrAppend(prefix, "/"); |
1296 | 843 | DO(ConsumeFullTypeName(full_type_name)); |
1297 | | |
1298 | 843 | return true; |
1299 | 843 | } |
1300 | | |
1301 | | // A helper function for reconstructing Any::value. Consumes a text of |
1302 | | // full_type_name, then serializes it into serialized_value. |
1303 | | bool ConsumeAnyValue(const Descriptor* value_descriptor, |
1304 | 843 | std::string* serialized_value) { |
1305 | 843 | DynamicMessageFactory factory; |
1306 | 843 | const Message* value_prototype = factory.GetPrototype(value_descriptor); |
1307 | 843 | if (value_prototype == nullptr) { |
1308 | 0 | return false; |
1309 | 0 | } |
1310 | 843 | std::unique_ptr<Message> value(value_prototype->New()); |
1311 | 843 | std::string sub_delimiter; |
1312 | 843 | DO(ConsumeMessageDelimiter(&sub_delimiter)); |
1313 | 843 | DO(ConsumeMessage(value.get(), sub_delimiter)); |
1314 | | |
1315 | 806 | if (allow_partial_) { |
1316 | 3 | value->AppendPartialToString(serialized_value); |
1317 | 803 | } else { |
1318 | 803 | if (!value->IsInitialized()) { |
1319 | 0 | ReportError(absl::StrCat( |
1320 | 0 | "Value of type \"", value_descriptor->full_name(), |
1321 | 0 | "\" stored in google.protobuf.Any has missing required fields")); |
1322 | 0 | return false; |
1323 | 0 | } |
1324 | 803 | value->AppendToString(serialized_value); |
1325 | 803 | } |
1326 | 806 | return true; |
1327 | 806 | } |
1328 | | |
1329 | | // Consumes a token and confirms that it matches that specified in the |
1330 | | // value parameter. Returns false if the token found does not match that |
1331 | | // which was specified. |
1332 | 23.7M | bool Consume(const std::string& value) { |
1333 | 23.7M | const std::string& current_value = tokenizer_.current().text; |
1334 | | |
1335 | 23.7M | if (current_value != value) { |
1336 | 371 | ReportError(absl::StrCat("Expected \"", value, "\", found \"", |
1337 | 371 | current_value, "\".")); |
1338 | 371 | return false; |
1339 | 371 | } |
1340 | | |
1341 | 23.7M | tokenizer_.Next(); |
1342 | | |
1343 | 23.7M | return true; |
1344 | 23.7M | } |
1345 | | |
1346 | | // Similar to `Consume`, but the following token may be tokenized as |
1347 | | // TYPE_WHITESPACE. |
1348 | 7.05M | bool ConsumeBeforeWhitespace(const std::string& value) { |
1349 | | // Report whitespace after this token, but only once. |
1350 | 7.05M | tokenizer_.set_report_whitespace(true); |
1351 | 7.05M | bool result = Consume(value); |
1352 | 7.05M | tokenizer_.set_report_whitespace(false); |
1353 | 7.05M | return result; |
1354 | 7.05M | } |
1355 | | |
1356 | | // Attempts to consume the supplied value. Returns false if a the |
1357 | | // token found does not match the value specified. |
1358 | 68.6M | bool TryConsume(const std::string& value) { |
1359 | 68.6M | if (tokenizer_.current().text == value) { |
1360 | 36.9k | tokenizer_.Next(); |
1361 | 36.9k | return true; |
1362 | 68.6M | } else { |
1363 | 68.6M | return false; |
1364 | 68.6M | } |
1365 | 68.6M | } |
1366 | | |
1367 | | // Similar to `TryConsume`, but the following token may be tokenized as |
1368 | | // TYPE_WHITESPACE. |
1369 | 8.36M | bool TryConsumeBeforeWhitespace(const std::string& value) { |
1370 | | // Report whitespace after this token, but only once. |
1371 | 8.36M | tokenizer_.set_report_whitespace(true); |
1372 | 8.36M | bool result = TryConsume(value); |
1373 | 8.36M | tokenizer_.set_report_whitespace(false); |
1374 | 8.36M | return result; |
1375 | 8.36M | } |
1376 | | |
1377 | 22.4M | bool TryConsumeWhitespace() { |
1378 | 22.4M | had_silent_marker_ = false; |
1379 | 22.4M | if (LookingAtType(io::Tokenizer::TYPE_WHITESPACE)) { |
1380 | 15.4M | if (tokenizer_.current().text == |
1381 | 15.4M | absl::StrCat(" ", internal::kDebugStringSilentMarkerForDetection)) { |
1382 | 6 | had_silent_marker_ = true; |
1383 | 6 | } |
1384 | 15.4M | tokenizer_.Next(); |
1385 | 15.4M | return true; |
1386 | 15.4M | } |
1387 | 7.07M | return false; |
1388 | 22.4M | } |
1389 | | |
1390 | | // An internal instance of the Tokenizer's error collector, used to |
1391 | | // collect any base-level parse errors and feed them to the ParserImpl. |
1392 | | class ParserErrorCollector : public io::ErrorCollector { |
1393 | | public: |
1394 | | explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser) |
1395 | 149k | : parser_(parser) {} |
1396 | | |
1397 | | ParserErrorCollector(const ParserErrorCollector&) = delete; |
1398 | | ParserErrorCollector& operator=(const ParserErrorCollector&) = delete; |
1399 | 0 | ~ParserErrorCollector() override {} |
1400 | | |
1401 | 5.45M | void RecordError(int line, int column, absl::string_view message) override { |
1402 | 5.45M | parser_->ReportError(line, column, message); |
1403 | 5.45M | } |
1404 | | |
1405 | | void RecordWarning(int line, int column, |
1406 | 0 | absl::string_view message) override { |
1407 | 0 | parser_->ReportWarning(line, column, message); |
1408 | 0 | } |
1409 | | |
1410 | | private: |
1411 | | TextFormat::Parser::ParserImpl* parser_; |
1412 | | }; |
1413 | | |
1414 | | io::ErrorCollector* error_collector_; |
1415 | | const TextFormat::Finder* finder_; |
1416 | | ParseInfoTree* parse_info_tree_; |
1417 | | ParserErrorCollector tokenizer_error_collector_; |
1418 | | io::Tokenizer tokenizer_; |
1419 | | const Descriptor* root_message_type_; |
1420 | | SingularOverwritePolicy singular_overwrite_policy_; |
1421 | | const bool allow_case_insensitive_field_; |
1422 | | const bool allow_unknown_field_; |
1423 | | const bool allow_unknown_extension_; |
1424 | | const bool allow_unknown_enum_; |
1425 | | const bool allow_field_number_; |
1426 | | const bool allow_partial_; |
1427 | | const int initial_recursion_limit_; |
1428 | | int recursion_limit_; |
1429 | | bool had_silent_marker_; |
1430 | | bool had_errors_; |
1431 | | bool error_on_no_op_fields_; |
1432 | | |
1433 | | }; |
1434 | | |
1435 | | // =========================================================================== |
1436 | | // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted |
1437 | | // from the Printer found in //third_party/protobuf/io/printer.h |
1438 | | class TextFormat::Printer::TextGenerator |
1439 | | : public TextFormat::BaseTextGenerator { |
1440 | | public: |
1441 | | explicit TextGenerator(io::ZeroCopyOutputStream* output, |
1442 | | int initial_indent_level) |
1443 | | : output_(output), |
1444 | | buffer_(nullptr), |
1445 | | buffer_size_(0), |
1446 | | at_start_of_line_(true), |
1447 | | failed_(false), |
1448 | | insert_silent_marker_(false), |
1449 | | indent_level_(initial_indent_level), |
1450 | 0 | initial_indent_level_(initial_indent_level) {} |
1451 | | |
1452 | | explicit TextGenerator(io::ZeroCopyOutputStream* output, |
1453 | | bool insert_silent_marker, int initial_indent_level) |
1454 | | : output_(output), |
1455 | | buffer_(nullptr), |
1456 | | buffer_size_(0), |
1457 | | at_start_of_line_(true), |
1458 | | failed_(false), |
1459 | | insert_silent_marker_(insert_silent_marker), |
1460 | | indent_level_(initial_indent_level), |
1461 | 269k | initial_indent_level_(initial_indent_level) {} |
1462 | | |
1463 | | TextGenerator(const TextGenerator&) = delete; |
1464 | | TextGenerator& operator=(const TextGenerator&) = delete; |
1465 | 269k | ~TextGenerator() override { |
1466 | | // Only BackUp() if we're sure we've successfully called Next() at least |
1467 | | // once. |
1468 | 269k | if (!failed_) { |
1469 | 269k | output_->BackUp(buffer_size_); |
1470 | 269k | } |
1471 | 269k | } |
1472 | | |
1473 | | // Indent text by two spaces. After calling Indent(), two spaces will be |
1474 | | // inserted at the beginning of each line of text. Indent() may be called |
1475 | | // multiple times to produce deeper indents. |
1476 | 1.85M | void Indent() override { ++indent_level_; } |
1477 | | |
1478 | | // Reduces the current indent level by two spaces, or crashes if the indent |
1479 | | // level is zero. |
1480 | 1.85M | void Outdent() override { |
1481 | 1.85M | if (indent_level_ == 0 || indent_level_ < initial_indent_level_) { |
1482 | 0 | ABSL_DLOG(FATAL) << " Outdent() without matching Indent()."; |
1483 | 0 | return; |
1484 | 0 | } |
1485 | | |
1486 | 1.85M | --indent_level_; |
1487 | 1.85M | } |
1488 | | |
1489 | 5.06M | size_t GetCurrentIndentationSize() const override { |
1490 | 5.06M | return 2 * indent_level_; |
1491 | 5.06M | } |
1492 | | |
1493 | | // Print text to the output stream. |
1494 | 23.1M | void Print(const char* text, size_t size) override { |
1495 | 23.1M | if (indent_level_ > 0) { |
1496 | 21.3M | size_t pos = 0; // The number of bytes we've written so far. |
1497 | 906M | for (size_t i = 0; i < size; i++) { |
1498 | 885M | if (text[i] == '\n') { |
1499 | | // Saw newline. If there is more text, we may need to insert an |
1500 | | // indent here. So, write what we have so far, including the '\n'. |
1501 | 5.06M | Write(text + pos, i - pos + 1); |
1502 | 5.06M | pos = i + 1; |
1503 | | |
1504 | | // Setting this true will cause the next Write() to insert an indent |
1505 | | // first. |
1506 | 5.06M | at_start_of_line_ = true; |
1507 | 5.06M | } |
1508 | 885M | } |
1509 | | // Write the rest. |
1510 | 21.3M | Write(text + pos, size - pos); |
1511 | 21.3M | } else { |
1512 | 1.77M | Write(text, size); |
1513 | 1.77M | if (size > 0 && text[size - 1] == '\n') { |
1514 | 171k | at_start_of_line_ = true; |
1515 | 171k | } |
1516 | 1.77M | } |
1517 | 23.1M | } |
1518 | | |
1519 | | // True if any write to the underlying stream failed. (We don't just |
1520 | | // crash in this case because this is an I/O failure, not a programming |
1521 | | // error.) |
1522 | 269k | bool failed() const { return failed_; } |
1523 | | |
1524 | 2.87M | void PrintMaybeWithMarker(MarkerToken, absl::string_view text) override { |
1525 | 2.87M | Print(text.data(), text.size()); |
1526 | 2.87M | if (ConsumeInsertSilentMarker()) { |
1527 | 0 | PrintLiteral(internal::kDebugStringSilentMarker); |
1528 | 0 | } |
1529 | 2.87M | } |
1530 | | |
1531 | | void PrintMaybeWithMarker(MarkerToken, absl::string_view text_head, |
1532 | 1.86M | absl::string_view text_tail) override { |
1533 | 1.86M | Print(text_head.data(), text_head.size()); |
1534 | 1.86M | if (ConsumeInsertSilentMarker()) { |
1535 | 0 | PrintLiteral(internal::kDebugStringSilentMarker); |
1536 | 0 | } |
1537 | 1.86M | Print(text_tail.data(), text_tail.size()); |
1538 | 1.86M | } |
1539 | | |
1540 | | private: |
1541 | 28.2M | void Write(const char* data, size_t size) { |
1542 | 28.2M | if (failed_) return; |
1543 | 28.2M | if (size == 0) return; |
1544 | | |
1545 | 23.0M | if (at_start_of_line_) { |
1546 | | // Insert an indent. |
1547 | 5.32M | at_start_of_line_ = false; |
1548 | 5.32M | WriteIndent(); |
1549 | 5.32M | if (failed_) return; |
1550 | 5.32M | } |
1551 | | |
1552 | 23.8M | while (static_cast<int64_t>(size) > buffer_size_) { |
1553 | | // Data exceeds space in the buffer. Copy what we can and request a |
1554 | | // new buffer. |
1555 | 794k | if (buffer_size_ > 0) { |
1556 | 543k | memcpy(buffer_, data, buffer_size_); |
1557 | 543k | data += buffer_size_; |
1558 | 543k | size -= buffer_size_; |
1559 | 543k | } |
1560 | 794k | void* void_buffer = nullptr; |
1561 | 794k | failed_ = !output_->Next(&void_buffer, &buffer_size_); |
1562 | 794k | if (failed_) return; |
1563 | 794k | buffer_ = reinterpret_cast<char*>(void_buffer); |
1564 | 794k | } |
1565 | | |
1566 | | // Buffer is big enough to receive the data; copy it. |
1567 | 23.0M | memcpy(buffer_, data, size); |
1568 | 23.0M | buffer_ += size; |
1569 | 23.0M | buffer_size_ -= size; |
1570 | 23.0M | } |
1571 | | |
1572 | 5.32M | void WriteIndent() { |
1573 | 5.32M | if (indent_level_ == 0) { |
1574 | 260k | return; |
1575 | 260k | } |
1576 | 5.06M | ABSL_DCHECK(!failed_); |
1577 | 5.06M | int size = GetCurrentIndentationSize(); |
1578 | | |
1579 | 5.11M | while (size > buffer_size_) { |
1580 | | // Data exceeds space in the buffer. Write what we can and request a new |
1581 | | // buffer. |
1582 | 43.5k | if (buffer_size_ > 0) { |
1583 | 36.0k | memset(buffer_, ' ', buffer_size_); |
1584 | 36.0k | } |
1585 | 43.5k | size -= buffer_size_; |
1586 | 43.5k | void* void_buffer; |
1587 | 43.5k | failed_ = !output_->Next(&void_buffer, &buffer_size_); |
1588 | 43.5k | if (failed_) return; |
1589 | 43.5k | buffer_ = reinterpret_cast<char*>(void_buffer); |
1590 | 43.5k | } |
1591 | | |
1592 | | // Buffer is big enough to receive the data; copy it. |
1593 | 5.06M | memset(buffer_, ' ', size); |
1594 | 5.06M | buffer_ += size; |
1595 | 5.06M | buffer_size_ -= size; |
1596 | 5.06M | } |
1597 | | |
1598 | | // Return the current value of insert_silent_marker_. If it is true, set it |
1599 | | // to false as we assume that a silent marker is inserted after a call to this |
1600 | | // function. |
1601 | 4.73M | bool ConsumeInsertSilentMarker() { |
1602 | 4.73M | if (insert_silent_marker_) { |
1603 | 0 | insert_silent_marker_ = false; |
1604 | 0 | return true; |
1605 | 0 | } |
1606 | 4.73M | return false; |
1607 | 4.73M | } |
1608 | | |
1609 | | io::ZeroCopyOutputStream* const output_; |
1610 | | char* buffer_; |
1611 | | int buffer_size_; |
1612 | | bool at_start_of_line_; |
1613 | | bool failed_; |
1614 | | // This flag is false when inserting silent marker is disabled or a silent |
1615 | | // marker has been inserted. |
1616 | | bool insert_silent_marker_; |
1617 | | |
1618 | | int indent_level_; |
1619 | | int initial_indent_level_; |
1620 | | }; |
1621 | | |
1622 | | // =========================================================================== |
1623 | | // An internal field value printer that may insert a silent marker in |
1624 | | // DebugStrings. |
1625 | | class TextFormat::Printer::DebugStringFieldValuePrinter |
1626 | | : public TextFormat::FastFieldValuePrinter { |
1627 | | public: |
1628 | | void PrintMessageStart(const Message& /*message*/, int /*field_index*/, |
1629 | | int /*field_count*/, bool single_line_mode, |
1630 | 1.84M | BaseTextGenerator* generator) const override { |
1631 | 1.84M | if (single_line_mode) { |
1632 | 402k | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{ "); |
1633 | 1.44M | } else { |
1634 | 1.44M | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{\n"); |
1635 | 1.44M | } |
1636 | 1.84M | } |
1637 | | }; |
1638 | | |
1639 | | // =========================================================================== |
1640 | | // An internal field value printer that escape UTF8 strings. |
1641 | | class TextFormat::Printer::FastFieldValuePrinterUtf8Escaping |
1642 | | : public TextFormat::Printer::DebugStringFieldValuePrinter { |
1643 | | public: |
1644 | | void PrintString(const std::string& val, |
1645 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
1646 | 0 | generator->PrintLiteral("\""); |
1647 | 0 | generator->PrintString(absl::Utf8SafeCEscape(val)); |
1648 | 0 | generator->PrintLiteral("\""); |
1649 | 0 | } |
1650 | | void PrintBytes(const std::string& val, |
1651 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
1652 | 0 | return FastFieldValuePrinter::PrintString(val, generator); |
1653 | 0 | } |
1654 | | }; |
1655 | | |
1656 | | // =========================================================================== |
1657 | | // Implementation of the default Finder for extensions. |
1658 | 0 | TextFormat::Finder::~Finder() {} |
1659 | | |
1660 | | const FieldDescriptor* TextFormat::Finder::FindExtension( |
1661 | 0 | Message* message, const std::string& name) const { |
1662 | 0 | return DefaultFinderFindExtension(message, name); |
1663 | 0 | } |
1664 | | |
1665 | | const FieldDescriptor* TextFormat::Finder::FindExtensionByNumber( |
1666 | 0 | const Descriptor* descriptor, int number) const { |
1667 | 0 | return DefaultFinderFindExtensionByNumber(descriptor, number); |
1668 | 0 | } |
1669 | | |
1670 | | const Descriptor* TextFormat::Finder::FindAnyType( |
1671 | | const Message& message, const std::string& prefix, |
1672 | 0 | const std::string& name) const { |
1673 | 0 | return DefaultFinderFindAnyType(message, prefix, name); |
1674 | 0 | } |
1675 | | |
1676 | | MessageFactory* TextFormat::Finder::FindExtensionFactory( |
1677 | 0 | const FieldDescriptor* /*field*/) const { |
1678 | 0 | return nullptr; |
1679 | 0 | } |
1680 | | |
1681 | | // =========================================================================== |
1682 | | |
1683 | | TextFormat::Parser::Parser() |
1684 | | : error_collector_(nullptr), |
1685 | | finder_(nullptr), |
1686 | | parse_info_tree_(nullptr), |
1687 | | allow_partial_(false), |
1688 | | allow_case_insensitive_field_(false), |
1689 | | allow_unknown_field_(false), |
1690 | | allow_unknown_extension_(false), |
1691 | | allow_unknown_enum_(false), |
1692 | | allow_field_number_(false), |
1693 | | allow_relaxed_whitespace_(false), |
1694 | | allow_singular_overwrites_(false), |
1695 | 149k | recursion_limit_(std::numeric_limits<int>::max()) {} |
1696 | | |
1697 | 149k | TextFormat::Parser::~Parser() {} |
1698 | | |
1699 | | namespace { |
1700 | | |
1701 | | template <typename T> |
1702 | 149k | bool CheckParseInputSize(T& input, io::ErrorCollector* error_collector) { |
1703 | 149k | if (input.size() > INT_MAX) { |
1704 | 0 | error_collector->RecordError( |
1705 | 0 | -1, 0, |
1706 | 0 | absl::StrCat( |
1707 | 0 | "Input size too large: ", static_cast<int64_t>(input.size()), |
1708 | 0 | " bytes", " > ", INT_MAX, " bytes.")); |
1709 | 0 | return false; |
1710 | 0 | } |
1711 | 149k | return true; |
1712 | 149k | } text_format.cc:bool google::protobuf::(anonymous namespace)::CheckParseInputSize<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> >&, google::protobuf::io::ErrorCollector*) Line | Count | Source | 1702 | 149k | bool CheckParseInputSize(T& input, io::ErrorCollector* error_collector) { | 1703 | 149k | if (input.size() > INT_MAX) { | 1704 | 0 | error_collector->RecordError( | 1705 | 0 | -1, 0, | 1706 | 0 | absl::StrCat( | 1707 | 0 | "Input size too large: ", static_cast<int64_t>(input.size()), | 1708 | 0 | " bytes", " > ", INT_MAX, " bytes.")); | 1709 | 0 | return false; | 1710 | 0 | } | 1711 | 149k | return true; | 1712 | 149k | } |
Unexecuted instantiation: text_format.cc:bool google::protobuf::(anonymous namespace)::CheckParseInputSize<absl::Cord const>(absl::Cord const&, google::protobuf::io::ErrorCollector*) |
1713 | | |
1714 | | } // namespace |
1715 | | |
1716 | | bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input, |
1717 | 149k | Message* output) { |
1718 | 149k | output->Clear(); |
1719 | | |
1720 | 149k | ParserImpl::SingularOverwritePolicy overwrites_policy = |
1721 | 149k | allow_singular_overwrites_ ? ParserImpl::ALLOW_SINGULAR_OVERWRITES |
1722 | 149k | : ParserImpl::FORBID_SINGULAR_OVERWRITES; |
1723 | | |
1724 | 149k | ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_, |
1725 | 149k | parse_info_tree_, overwrites_policy, |
1726 | 149k | allow_case_insensitive_field_, allow_unknown_field_, |
1727 | 149k | allow_unknown_extension_, allow_unknown_enum_, |
1728 | 149k | allow_field_number_, allow_relaxed_whitespace_, |
1729 | 149k | allow_partial_, recursion_limit_, error_on_no_op_fields_); |
1730 | 149k | return MergeUsingImpl(input, output, &parser); |
1731 | 149k | } |
1732 | | |
1733 | | bool TextFormat::Parser::ParseFromString(absl::string_view input, |
1734 | 149k | Message* output) { |
1735 | 149k | DO(CheckParseInputSize(input, error_collector_)); |
1736 | 149k | io::ArrayInputStream input_stream(input.data(), input.size()); |
1737 | 149k | return Parse(&input_stream, output); |
1738 | 149k | } |
1739 | | |
1740 | | bool TextFormat::Parser::ParseFromCord(const absl::Cord& input, |
1741 | 0 | Message* output) { |
1742 | 0 | DO(CheckParseInputSize(input, error_collector_)); |
1743 | 0 | io::CordInputStream input_stream(&input); |
1744 | 0 | return Parse(&input_stream, output); |
1745 | 0 | } |
1746 | | |
1747 | | bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, |
1748 | 0 | Message* output) { |
1749 | 0 | ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_, |
1750 | 0 | parse_info_tree_, ParserImpl::ALLOW_SINGULAR_OVERWRITES, |
1751 | 0 | allow_case_insensitive_field_, allow_unknown_field_, |
1752 | 0 | allow_unknown_extension_, allow_unknown_enum_, |
1753 | 0 | allow_field_number_, allow_relaxed_whitespace_, |
1754 | 0 | allow_partial_, recursion_limit_, error_on_no_op_fields_); |
1755 | 0 | return MergeUsingImpl(input, output, &parser); |
1756 | 0 | } |
1757 | | |
1758 | | bool TextFormat::Parser::MergeFromString(absl::string_view input, |
1759 | 0 | Message* output) { |
1760 | 0 | DO(CheckParseInputSize(input, error_collector_)); |
1761 | 0 | io::ArrayInputStream input_stream(input.data(), input.size()); |
1762 | 0 | return Merge(&input_stream, output); |
1763 | 0 | } |
1764 | | |
1765 | | bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */, |
1766 | | Message* output, |
1767 | 149k | ParserImpl* parser_impl) { |
1768 | 149k | if (!parser_impl->Parse(output)) return false; |
1769 | 148k | if (!allow_partial_ && !output->IsInitialized()) { |
1770 | 0 | std::vector<std::string> missing_fields; |
1771 | 0 | output->FindInitializationErrors(&missing_fields); |
1772 | 0 | parser_impl->ReportError(-1, 0, |
1773 | 0 | absl::StrCat("Message missing required fields: ", |
1774 | 0 | absl::StrJoin(missing_fields, ", "))); |
1775 | 0 | return false; |
1776 | 0 | } |
1777 | 148k | return true; |
1778 | 148k | } |
1779 | | |
1780 | | bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input, |
1781 | | const FieldDescriptor* field, |
1782 | 0 | Message* output) { |
1783 | 0 | io::ArrayInputStream input_stream(input.data(), input.size()); |
1784 | 0 | ParserImpl parser(output->GetDescriptor(), &input_stream, error_collector_, |
1785 | 0 | finder_, parse_info_tree_, |
1786 | 0 | ParserImpl::ALLOW_SINGULAR_OVERWRITES, |
1787 | 0 | allow_case_insensitive_field_, allow_unknown_field_, |
1788 | 0 | allow_unknown_extension_, allow_unknown_enum_, |
1789 | 0 | allow_field_number_, allow_relaxed_whitespace_, |
1790 | 0 | allow_partial_, recursion_limit_, error_on_no_op_fields_); |
1791 | 0 | return parser.ParseField(field, output); |
1792 | 0 | } |
1793 | | |
1794 | | /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input, |
1795 | 0 | Message* output) { |
1796 | 0 | return Parser().Parse(input, output); |
1797 | 0 | } |
1798 | | |
1799 | | /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input, |
1800 | 0 | Message* output) { |
1801 | 0 | return Parser().Merge(input, output); |
1802 | 0 | } |
1803 | | |
1804 | | /* static */ bool TextFormat::ParseFromString(absl::string_view input, |
1805 | 9.52k | Message* output) { |
1806 | 9.52k | return Parser().ParseFromString(input, output); |
1807 | 9.52k | } |
1808 | | |
1809 | | /* static */ bool TextFormat::ParseFromCord(const absl::Cord& input, |
1810 | 0 | Message* output) { |
1811 | 0 | return Parser().ParseFromCord(input, output); |
1812 | 0 | } |
1813 | | |
1814 | | /* static */ bool TextFormat::MergeFromString(absl::string_view input, |
1815 | 0 | Message* output) { |
1816 | 0 | return Parser().MergeFromString(input, output); |
1817 | 0 | } |
1818 | | |
1819 | | #undef DO |
1820 | | |
1821 | | // =========================================================================== |
1822 | | |
1823 | 269k | TextFormat::BaseTextGenerator::~BaseTextGenerator() {} |
1824 | | |
1825 | | namespace { |
1826 | | |
1827 | | // A BaseTextGenerator that writes to a string. |
1828 | | class StringBaseTextGenerator : public TextFormat::BaseTextGenerator { |
1829 | | public: |
1830 | 0 | void Print(const char* text, size_t size) override { |
1831 | 0 | output_.append(text, size); |
1832 | 0 | } |
1833 | | |
1834 | 0 | std::string Consume() && { return std::move(output_); } |
1835 | | |
1836 | | private: |
1837 | | std::string output_; |
1838 | | }; |
1839 | | |
1840 | | } // namespace |
1841 | | |
1842 | | // The default implementation for FieldValuePrinter. We just delegate the |
1843 | | // implementation to the default FastFieldValuePrinter to avoid duplicating the |
1844 | | // logic. |
1845 | 0 | TextFormat::FieldValuePrinter::FieldValuePrinter() {} |
1846 | 0 | TextFormat::FieldValuePrinter::~FieldValuePrinter() {} |
1847 | | |
1848 | | #define FORWARD_IMPL(fn, ...) \ |
1849 | 0 | StringBaseTextGenerator generator; \ |
1850 | 0 | delegate_.fn(__VA_ARGS__, &generator); \ |
1851 | 0 | return std::move(generator).Consume() |
1852 | | |
1853 | 0 | std::string TextFormat::FieldValuePrinter::PrintBool(bool val) const { |
1854 | 0 | FORWARD_IMPL(PrintBool, val); |
1855 | 0 | } |
1856 | 0 | std::string TextFormat::FieldValuePrinter::PrintInt32(int32_t val) const { |
1857 | 0 | FORWARD_IMPL(PrintInt32, val); |
1858 | 0 | } |
1859 | 0 | std::string TextFormat::FieldValuePrinter::PrintUInt32(uint32_t val) const { |
1860 | 0 | FORWARD_IMPL(PrintUInt32, val); |
1861 | 0 | } |
1862 | 0 | std::string TextFormat::FieldValuePrinter::PrintInt64(int64_t val) const { |
1863 | 0 | FORWARD_IMPL(PrintInt64, val); |
1864 | 0 | } |
1865 | 0 | std::string TextFormat::FieldValuePrinter::PrintUInt64(uint64_t val) const { |
1866 | 0 | FORWARD_IMPL(PrintUInt64, val); |
1867 | 0 | } |
1868 | 0 | std::string TextFormat::FieldValuePrinter::PrintFloat(float val) const { |
1869 | 0 | FORWARD_IMPL(PrintFloat, val); |
1870 | 0 | } |
1871 | 0 | std::string TextFormat::FieldValuePrinter::PrintDouble(double val) const { |
1872 | 0 | FORWARD_IMPL(PrintDouble, val); |
1873 | 0 | } |
1874 | | std::string TextFormat::FieldValuePrinter::PrintString( |
1875 | 0 | const std::string& val) const { |
1876 | 0 | FORWARD_IMPL(PrintString, val); |
1877 | 0 | } |
1878 | | std::string TextFormat::FieldValuePrinter::PrintBytes( |
1879 | 0 | const std::string& val) const { |
1880 | 0 | return PrintString(val); |
1881 | 0 | } |
1882 | | std::string TextFormat::FieldValuePrinter::PrintEnum( |
1883 | 0 | int32_t val, const std::string& name) const { |
1884 | 0 | FORWARD_IMPL(PrintEnum, val, name); |
1885 | 0 | } |
1886 | | std::string TextFormat::FieldValuePrinter::PrintFieldName( |
1887 | | const Message& message, const Reflection* reflection, |
1888 | 0 | const FieldDescriptor* field) const { |
1889 | 0 | FORWARD_IMPL(PrintFieldName, message, reflection, field); |
1890 | 0 | } |
1891 | | std::string TextFormat::FieldValuePrinter::PrintMessageStart( |
1892 | | const Message& message, int field_index, int field_count, |
1893 | 0 | bool single_line_mode) const { |
1894 | 0 | FORWARD_IMPL(PrintMessageStart, message, field_index, field_count, |
1895 | 0 | single_line_mode); |
1896 | 0 | } |
1897 | | std::string TextFormat::FieldValuePrinter::PrintMessageEnd( |
1898 | | const Message& message, int field_index, int field_count, |
1899 | 0 | bool single_line_mode) const { |
1900 | 0 | FORWARD_IMPL(PrintMessageEnd, message, field_index, field_count, |
1901 | 0 | single_line_mode); |
1902 | 0 | } |
1903 | | #undef FORWARD_IMPL |
1904 | | |
1905 | 269k | TextFormat::FastFieldValuePrinter::FastFieldValuePrinter() {} |
1906 | 269k | TextFormat::FastFieldValuePrinter::~FastFieldValuePrinter() {} |
1907 | | void TextFormat::FastFieldValuePrinter::PrintBool( |
1908 | 56.0k | bool val, BaseTextGenerator* generator) const { |
1909 | 56.0k | if (val) { |
1910 | 51.0k | generator->PrintLiteral("true"); |
1911 | 51.0k | } else { |
1912 | 4.90k | generator->PrintLiteral("false"); |
1913 | 4.90k | } |
1914 | 56.0k | } |
1915 | | void TextFormat::FastFieldValuePrinter::PrintInt32( |
1916 | 581k | int32_t val, BaseTextGenerator* generator) const { |
1917 | 581k | generator->PrintString(absl::StrCat(val)); |
1918 | 581k | } |
1919 | | void TextFormat::FastFieldValuePrinter::PrintUInt32( |
1920 | 63.6k | uint32_t val, BaseTextGenerator* generator) const { |
1921 | 63.6k | generator->PrintString(absl::StrCat(val)); |
1922 | 63.6k | } |
1923 | | void TextFormat::FastFieldValuePrinter::PrintInt64( |
1924 | 22.9k | int64_t val, BaseTextGenerator* generator) const { |
1925 | 22.9k | generator->PrintString(absl::StrCat(val)); |
1926 | 22.9k | } |
1927 | | void TextFormat::FastFieldValuePrinter::PrintUInt64( |
1928 | 854 | uint64_t val, BaseTextGenerator* generator) const { |
1929 | 854 | generator->PrintString(absl::StrCat(val)); |
1930 | 854 | } |
1931 | | void TextFormat::FastFieldValuePrinter::PrintFloat( |
1932 | 0 | float val, BaseTextGenerator* generator) const { |
1933 | 0 | generator->PrintString(!std::isnan(val) ? io::SimpleFtoa(val) : "nan"); |
1934 | 0 | } |
1935 | | void TextFormat::FastFieldValuePrinter::PrintDouble( |
1936 | 16.3k | double val, BaseTextGenerator* generator) const { |
1937 | 16.3k | generator->PrintString(!std::isnan(val) ? io::SimpleDtoa(val) : "nan"); |
1938 | 16.3k | } |
1939 | | void TextFormat::FastFieldValuePrinter::PrintEnum( |
1940 | | int32_t /*val*/, const std::string& name, |
1941 | 72.2k | BaseTextGenerator* generator) const { |
1942 | 72.2k | generator->PrintString(name); |
1943 | 72.2k | } |
1944 | | |
1945 | | void TextFormat::FastFieldValuePrinter::PrintString( |
1946 | 2.05M | const std::string& val, BaseTextGenerator* generator) const { |
1947 | 2.05M | generator->PrintLiteral("\""); |
1948 | 2.05M | generator->PrintString(absl::CEscape(val)); |
1949 | 2.05M | generator->PrintLiteral("\""); |
1950 | 2.05M | } |
1951 | | void TextFormat::FastFieldValuePrinter::PrintBytes( |
1952 | 7.78k | const std::string& val, BaseTextGenerator* generator) const { |
1953 | 7.78k | PrintString(val, generator); |
1954 | 7.78k | } |
1955 | | void TextFormat::FastFieldValuePrinter::PrintFieldName( |
1956 | | const Message& message, int /*field_index*/, int /*field_count*/, |
1957 | | const Reflection* reflection, const FieldDescriptor* field, |
1958 | 3.75M | BaseTextGenerator* generator) const { |
1959 | 3.75M | PrintFieldName(message, reflection, field, generator); |
1960 | 3.75M | } |
1961 | | void TextFormat::FastFieldValuePrinter::PrintFieldName( |
1962 | | const Message& /*message*/, const Reflection* /*reflection*/, |
1963 | 3.75M | const FieldDescriptor* field, BaseTextGenerator* generator) const { |
1964 | 3.75M | if (field->is_extension()) { |
1965 | 0 | generator->PrintLiteral("["); |
1966 | 0 | generator->PrintString(field->PrintableNameForExtension()); |
1967 | 0 | generator->PrintLiteral("]"); |
1968 | 3.75M | } else if (field->type() == FieldDescriptor::TYPE_GROUP) { |
1969 | | // Groups must be serialized with their original capitalization. |
1970 | 0 | generator->PrintString(field->message_type()->name()); |
1971 | 3.75M | } else { |
1972 | 3.75M | generator->PrintString(field->name()); |
1973 | 3.75M | } |
1974 | 3.75M | } |
1975 | | void TextFormat::FastFieldValuePrinter::PrintMessageStart( |
1976 | | const Message& /*message*/, int /*field_index*/, int /*field_count*/, |
1977 | 0 | bool single_line_mode, BaseTextGenerator* generator) const { |
1978 | 0 | if (single_line_mode) { |
1979 | 0 | generator->PrintLiteral(" { "); |
1980 | 0 | } else { |
1981 | 0 | generator->PrintLiteral(" {\n"); |
1982 | 0 | } |
1983 | 0 | } |
1984 | | bool TextFormat::FastFieldValuePrinter::PrintMessageContent( |
1985 | | const Message& /*message*/, int /*field_index*/, int /*field_count*/, |
1986 | 1.81M | bool /*single_line_mode*/, BaseTextGenerator* /*generator*/) const { |
1987 | 1.81M | return false; // Use the default printing function. |
1988 | 1.81M | } |
1989 | | void TextFormat::FastFieldValuePrinter::PrintMessageEnd( |
1990 | | const Message& /*message*/, int /*field_index*/, int /*field_count*/, |
1991 | 1.84M | bool single_line_mode, BaseTextGenerator* generator) const { |
1992 | 1.84M | if (single_line_mode) { |
1993 | 402k | generator->PrintLiteral("} "); |
1994 | 1.44M | } else { |
1995 | 1.44M | generator->PrintLiteral("}\n"); |
1996 | 1.44M | } |
1997 | 1.84M | } |
1998 | | |
1999 | | namespace { |
2000 | | |
2001 | | // A legacy compatibility wrapper. Takes ownership of the delegate. |
2002 | | class FieldValuePrinterWrapper : public TextFormat::FastFieldValuePrinter { |
2003 | | public: |
2004 | | explicit FieldValuePrinterWrapper( |
2005 | | const TextFormat::FieldValuePrinter* delegate) |
2006 | 0 | : delegate_(delegate) {} |
2007 | | |
2008 | 0 | void SetDelegate(const TextFormat::FieldValuePrinter* delegate) { |
2009 | 0 | delegate_.reset(delegate); |
2010 | 0 | } |
2011 | | |
2012 | | void PrintBool(bool val, |
2013 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2014 | 0 | generator->PrintString(delegate_->PrintBool(val)); |
2015 | 0 | } |
2016 | | void PrintInt32(int32_t val, |
2017 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2018 | 0 | generator->PrintString(delegate_->PrintInt32(val)); |
2019 | 0 | } |
2020 | | void PrintUInt32(uint32_t val, |
2021 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2022 | 0 | generator->PrintString(delegate_->PrintUInt32(val)); |
2023 | 0 | } |
2024 | | void PrintInt64(int64_t val, |
2025 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2026 | 0 | generator->PrintString(delegate_->PrintInt64(val)); |
2027 | 0 | } |
2028 | | void PrintUInt64(uint64_t val, |
2029 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2030 | 0 | generator->PrintString(delegate_->PrintUInt64(val)); |
2031 | 0 | } |
2032 | | void PrintFloat(float val, |
2033 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2034 | 0 | generator->PrintString(delegate_->PrintFloat(val)); |
2035 | 0 | } |
2036 | | void PrintDouble(double val, |
2037 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2038 | 0 | generator->PrintString(delegate_->PrintDouble(val)); |
2039 | 0 | } |
2040 | | void PrintString(const std::string& val, |
2041 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2042 | 0 | generator->PrintString(delegate_->PrintString(val)); |
2043 | 0 | } |
2044 | | void PrintBytes(const std::string& val, |
2045 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2046 | 0 | generator->PrintString(delegate_->PrintBytes(val)); |
2047 | 0 | } |
2048 | | void PrintEnum(int32_t val, const std::string& name, |
2049 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2050 | 0 | generator->PrintString(delegate_->PrintEnum(val, name)); |
2051 | 0 | } |
2052 | | void PrintFieldName(const Message& message, int /*field_index*/, |
2053 | | int /*field_count*/, const Reflection* reflection, |
2054 | | const FieldDescriptor* field, |
2055 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2056 | 0 | generator->PrintString( |
2057 | 0 | delegate_->PrintFieldName(message, reflection, field)); |
2058 | 0 | } |
2059 | | void PrintFieldName(const Message& message, const Reflection* reflection, |
2060 | | const FieldDescriptor* field, |
2061 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2062 | 0 | generator->PrintString( |
2063 | 0 | delegate_->PrintFieldName(message, reflection, field)); |
2064 | 0 | } |
2065 | | void PrintMessageStart( |
2066 | | const Message& message, int field_index, int field_count, |
2067 | | bool single_line_mode, |
2068 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2069 | 0 | generator->PrintString(delegate_->PrintMessageStart( |
2070 | 0 | message, field_index, field_count, single_line_mode)); |
2071 | 0 | } |
2072 | | void PrintMessageEnd( |
2073 | | const Message& message, int field_index, int field_count, |
2074 | | bool single_line_mode, |
2075 | 0 | TextFormat::BaseTextGenerator* generator) const override { |
2076 | 0 | generator->PrintString(delegate_->PrintMessageEnd( |
2077 | 0 | message, field_index, field_count, single_line_mode)); |
2078 | 0 | } |
2079 | | |
2080 | | private: |
2081 | | std::unique_ptr<const TextFormat::FieldValuePrinter> delegate_; |
2082 | | }; |
2083 | | |
2084 | | } // namespace |
2085 | | |
2086 | | TextFormat::Printer::Printer() |
2087 | | : initial_indent_level_(0), |
2088 | | single_line_mode_(false), |
2089 | | use_field_number_(false), |
2090 | | use_short_repeated_primitives_(false), |
2091 | | insert_silent_marker_(false), |
2092 | | redact_debug_string_(false), |
2093 | | randomize_debug_string_(false), |
2094 | | hide_unknown_fields_(false), |
2095 | | print_message_fields_in_index_order_(false), |
2096 | | expand_any_(false), |
2097 | | truncate_string_field_longer_than_(0LL), |
2098 | 269k | finder_(nullptr) { |
2099 | 269k | SetUseUtf8StringEscaping(false); |
2100 | 269k | } |
2101 | | |
2102 | 269k | void TextFormat::Printer::SetUseUtf8StringEscaping(bool as_utf8) { |
2103 | 269k | SetDefaultFieldValuePrinter(as_utf8 ? new FastFieldValuePrinterUtf8Escaping() |
2104 | 269k | : new DebugStringFieldValuePrinter()); |
2105 | 269k | } |
2106 | | |
2107 | | void TextFormat::Printer::SetDefaultFieldValuePrinter( |
2108 | 0 | const FieldValuePrinter* printer) { |
2109 | 0 | default_field_value_printer_.reset(new FieldValuePrinterWrapper(printer)); |
2110 | 0 | } |
2111 | | |
2112 | | void TextFormat::Printer::SetDefaultFieldValuePrinter( |
2113 | 269k | const FastFieldValuePrinter* printer) { |
2114 | 269k | default_field_value_printer_.reset(printer); |
2115 | 269k | } |
2116 | | |
2117 | | bool TextFormat::Printer::RegisterFieldValuePrinter( |
2118 | 0 | const FieldDescriptor* field, const FieldValuePrinter* printer) { |
2119 | 0 | if (field == nullptr || printer == nullptr) { |
2120 | 0 | return false; |
2121 | 0 | } |
2122 | 0 | std::unique_ptr<FieldValuePrinterWrapper> wrapper( |
2123 | 0 | new FieldValuePrinterWrapper(nullptr)); |
2124 | 0 | auto pair = custom_printers_.emplace(field, nullptr); |
2125 | 0 | if (pair.second) { |
2126 | 0 | wrapper->SetDelegate(printer); |
2127 | 0 | pair.first->second = std::move(wrapper); |
2128 | 0 | return true; |
2129 | 0 | } else { |
2130 | 0 | return false; |
2131 | 0 | } |
2132 | 0 | } |
2133 | | |
2134 | | bool TextFormat::Printer::RegisterFieldValuePrinter( |
2135 | 0 | const FieldDescriptor* field, const FastFieldValuePrinter* printer) { |
2136 | 0 | if (field == nullptr || printer == nullptr) { |
2137 | 0 | return false; |
2138 | 0 | } |
2139 | 0 | auto pair = custom_printers_.emplace(field, nullptr); |
2140 | 0 | if (pair.second) { |
2141 | 0 | pair.first->second.reset(printer); |
2142 | 0 | return true; |
2143 | 0 | } else { |
2144 | 0 | return false; |
2145 | 0 | } |
2146 | 0 | } |
2147 | | |
2148 | | bool TextFormat::Printer::RegisterMessagePrinter( |
2149 | 0 | const Descriptor* descriptor, const MessagePrinter* printer) { |
2150 | 0 | if (descriptor == nullptr || printer == nullptr) { |
2151 | 0 | return false; |
2152 | 0 | } |
2153 | 0 | auto pair = custom_message_printers_.emplace(descriptor, nullptr); |
2154 | 0 | if (pair.second) { |
2155 | 0 | pair.first->second.reset(printer); |
2156 | 0 | return true; |
2157 | 0 | } else { |
2158 | 0 | return false; |
2159 | 0 | } |
2160 | 0 | } |
2161 | | |
2162 | | bool TextFormat::Printer::PrintToString(const Message& message, |
2163 | 269k | std::string* output) const { |
2164 | 538k | ABSL_DCHECK(output) << "output specified is nullptr"; |
2165 | | |
2166 | 269k | output->clear(); |
2167 | 269k | io::StringOutputStream output_stream(output); |
2168 | | |
2169 | 269k | return Print(message, &output_stream); |
2170 | 269k | } |
2171 | | |
2172 | | bool TextFormat::Printer::PrintUnknownFieldsToString( |
2173 | 0 | const UnknownFieldSet& unknown_fields, std::string* output) const { |
2174 | 0 | ABSL_DCHECK(output) << "output specified is nullptr"; |
2175 | | |
2176 | 0 | output->clear(); |
2177 | 0 | io::StringOutputStream output_stream(output); |
2178 | 0 | return PrintUnknownFields(unknown_fields, &output_stream); |
2179 | 0 | } |
2180 | | |
2181 | | bool TextFormat::Printer::Print(const Message& message, |
2182 | 269k | io::ZeroCopyOutputStream* output) const { |
2183 | 269k | TextGenerator generator(output, insert_silent_marker_, initial_indent_level_); |
2184 | | |
2185 | | |
2186 | 269k | Print(message, &generator); |
2187 | | |
2188 | | // Output false if the generator failed internally. |
2189 | 269k | return !generator.failed(); |
2190 | 269k | } |
2191 | | |
2192 | | // Maximum recursion depth for heuristically printing out length-delimited |
2193 | | // unknown fields as messages. |
2194 | | static constexpr int kUnknownFieldRecursionLimit = 10; |
2195 | | |
2196 | | bool TextFormat::Printer::PrintUnknownFields( |
2197 | | const UnknownFieldSet& unknown_fields, |
2198 | 0 | io::ZeroCopyOutputStream* output) const { |
2199 | 0 | TextGenerator generator(output, initial_indent_level_); |
2200 | |
|
2201 | 0 | PrintUnknownFields(unknown_fields, &generator, kUnknownFieldRecursionLimit); |
2202 | | |
2203 | | // Output false if the generator failed internally. |
2204 | 0 | return !generator.failed(); |
2205 | 0 | } |
2206 | | |
2207 | | namespace { |
2208 | | // Comparison functor for sorting FieldDescriptors by field index. |
2209 | | // Normal fields have higher precedence than extensions. |
2210 | | struct FieldIndexSorter { |
2211 | | bool operator()(const FieldDescriptor* left, |
2212 | 0 | const FieldDescriptor* right) const { |
2213 | 0 | if (left->is_extension() && right->is_extension()) { |
2214 | 0 | return left->number() < right->number(); |
2215 | 0 | } else if (left->is_extension()) { |
2216 | 0 | return false; |
2217 | 0 | } else if (right->is_extension()) { |
2218 | 0 | return true; |
2219 | 0 | } else { |
2220 | 0 | return left->index() < right->index(); |
2221 | 0 | } |
2222 | 0 | } |
2223 | | }; |
2224 | | |
2225 | | } // namespace |
2226 | | |
2227 | | bool TextFormat::Printer::PrintAny(const Message& message, |
2228 | 66.3k | BaseTextGenerator* generator) const { |
2229 | 66.3k | const FieldDescriptor* type_url_field; |
2230 | 66.3k | const FieldDescriptor* value_field; |
2231 | 66.3k | if (!internal::GetAnyFieldDescriptors(message, &type_url_field, |
2232 | 66.3k | &value_field)) { |
2233 | 0 | return false; |
2234 | 0 | } |
2235 | | |
2236 | 66.3k | const Reflection* reflection = message.GetReflection(); |
2237 | | |
2238 | | // Extract the full type name from the type_url field. |
2239 | 66.3k | const std::string& type_url = reflection->GetString(message, type_url_field); |
2240 | 66.3k | std::string url_prefix; |
2241 | 66.3k | std::string full_type_name; |
2242 | 66.3k | if (!internal::ParseAnyTypeUrl(type_url, &url_prefix, &full_type_name)) { |
2243 | 6.28k | return false; |
2244 | 6.28k | } |
2245 | | |
2246 | | // Print the "value" in text. |
2247 | 60.0k | const Descriptor* value_descriptor = |
2248 | 60.0k | finder_ ? finder_->FindAnyType(message, url_prefix, full_type_name) |
2249 | 60.0k | : DefaultFinderFindAnyType(message, url_prefix, full_type_name); |
2250 | 60.0k | if (value_descriptor == nullptr) { |
2251 | 44.7k | ABSL_LOG(WARNING) << "Can't print proto content: proto type " << type_url |
2252 | 44.7k | << " not found"; |
2253 | 22.3k | return false; |
2254 | 22.3k | } |
2255 | 37.7k | DynamicMessageFactory factory; |
2256 | 37.7k | std::unique_ptr<Message> value_message( |
2257 | 37.7k | factory.GetPrototype(value_descriptor)->New()); |
2258 | 37.7k | std::string serialized_value = reflection->GetString(message, value_field); |
2259 | 37.7k | if (!value_message->ParseFromString(serialized_value)) { |
2260 | 864 | ABSL_LOG(WARNING) << type_url << ": failed to parse contents"; |
2261 | 432 | return false; |
2262 | 432 | } |
2263 | 37.2k | generator->PrintLiteral("["); |
2264 | 37.2k | generator->PrintString(type_url); |
2265 | 37.2k | generator->PrintLiteral("]"); |
2266 | 37.2k | const FastFieldValuePrinter* printer = GetFieldPrinter(value_field); |
2267 | 37.2k | printer->PrintMessageStart(message, -1, 0, single_line_mode_, generator); |
2268 | 37.2k | generator->Indent(); |
2269 | 37.2k | Print(*value_message, generator); |
2270 | 37.2k | generator->Outdent(); |
2271 | 37.2k | printer->PrintMessageEnd(message, -1, 0, single_line_mode_, generator); |
2272 | 37.2k | return true; |
2273 | 37.7k | } |
2274 | | |
2275 | | void TextFormat::Printer::Print(const Message& message, |
2276 | 2.11M | BaseTextGenerator* generator) const { |
2277 | 2.11M | const Reflection* reflection = message.GetReflection(); |
2278 | 2.11M | if (!reflection) { |
2279 | | // This message does not provide any way to describe its structure. |
2280 | | // Parse it again in an UnknownFieldSet, and display this instead. |
2281 | 0 | UnknownFieldSet unknown_fields; |
2282 | 0 | { |
2283 | 0 | std::string serialized = message.SerializeAsString(); |
2284 | 0 | io::ArrayInputStream input(serialized.data(), serialized.size()); |
2285 | 0 | unknown_fields.ParseFromZeroCopyStream(&input); |
2286 | 0 | } |
2287 | 0 | PrintUnknownFields(unknown_fields, generator, kUnknownFieldRecursionLimit); |
2288 | 0 | return; |
2289 | 0 | } |
2290 | 2.11M | const Descriptor* descriptor = message.GetDescriptor(); |
2291 | 2.11M | auto itr = custom_message_printers_.find(descriptor); |
2292 | 2.11M | if (itr != custom_message_printers_.end()) { |
2293 | 0 | itr->second->Print(message, single_line_mode_, generator); |
2294 | 0 | return; |
2295 | 0 | } |
2296 | 2.11M | PrintMessage(message, generator); |
2297 | 2.11M | } |
2298 | | |
2299 | | void TextFormat::Printer::PrintMessage(const Message& message, |
2300 | 2.11M | BaseTextGenerator* generator) const { |
2301 | 2.11M | if (generator == nullptr) { |
2302 | 0 | return; |
2303 | 0 | } |
2304 | 2.11M | const Descriptor* descriptor = message.GetDescriptor(); |
2305 | 2.11M | if (descriptor->full_name() == internal::kAnyFullTypeName && expand_any_ && |
2306 | 2.11M | PrintAny(message, generator)) { |
2307 | 37.2k | return; |
2308 | 37.2k | } |
2309 | 2.08M | const Reflection* reflection = message.GetReflection(); |
2310 | 2.08M | std::vector<const FieldDescriptor*> fields; |
2311 | 2.08M | if (descriptor->options().map_entry()) { |
2312 | 157k | fields.push_back(descriptor->field(0)); |
2313 | 157k | fields.push_back(descriptor->field(1)); |
2314 | 1.92M | } else { |
2315 | 1.92M | reflection->ListFields(message, &fields); |
2316 | 1.92M | } |
2317 | | |
2318 | 2.08M | if (print_message_fields_in_index_order_) { |
2319 | 0 | std::sort(fields.begin(), fields.end(), FieldIndexSorter()); |
2320 | 0 | } |
2321 | 2.92M | for (const FieldDescriptor* field : fields) { |
2322 | 2.92M | PrintField(message, reflection, field, generator); |
2323 | 2.92M | } |
2324 | 2.08M | if (!hide_unknown_fields_) { |
2325 | 1.50M | PrintUnknownFields(reflection->GetUnknownFields(message), generator, |
2326 | 1.50M | kUnknownFieldRecursionLimit); |
2327 | 1.50M | } |
2328 | 2.08M | } |
2329 | | |
2330 | | void TextFormat::Printer::PrintFieldValueToString(const Message& message, |
2331 | | const FieldDescriptor* field, |
2332 | | int index, |
2333 | 0 | std::string* output) const { |
2334 | 0 | ABSL_DCHECK(output) << "output specified is nullptr"; |
2335 | | |
2336 | 0 | output->clear(); |
2337 | 0 | io::StringOutputStream output_stream(output); |
2338 | 0 | TextGenerator generator(&output_stream, initial_indent_level_); |
2339 | |
|
2340 | 0 | PrintFieldValue(message, message.GetReflection(), field, index, &generator); |
2341 | 0 | } |
2342 | | |
2343 | | class MapEntryMessageComparator { |
2344 | | public: |
2345 | | explicit MapEntryMessageComparator(const Descriptor* descriptor) |
2346 | 53.1k | : field_(descriptor->field(0)) {} |
2347 | | |
2348 | 219k | bool operator()(const Message* a, const Message* b) { |
2349 | 219k | const Reflection* reflection = a->GetReflection(); |
2350 | 219k | switch (field_->cpp_type()) { |
2351 | 0 | case FieldDescriptor::CPPTYPE_BOOL: { |
2352 | 0 | bool first = reflection->GetBool(*a, field_); |
2353 | 0 | bool second = reflection->GetBool(*b, field_); |
2354 | 0 | return first < second; |
2355 | 0 | } |
2356 | 0 | case FieldDescriptor::CPPTYPE_INT32: { |
2357 | 0 | int32_t first = reflection->GetInt32(*a, field_); |
2358 | 0 | int32_t second = reflection->GetInt32(*b, field_); |
2359 | 0 | return first < second; |
2360 | 0 | } |
2361 | 1.81k | case FieldDescriptor::CPPTYPE_INT64: { |
2362 | 1.81k | int64_t first = reflection->GetInt64(*a, field_); |
2363 | 1.81k | int64_t second = reflection->GetInt64(*b, field_); |
2364 | 1.81k | return first < second; |
2365 | 0 | } |
2366 | 0 | case FieldDescriptor::CPPTYPE_UINT32: { |
2367 | 0 | uint32_t first = reflection->GetUInt32(*a, field_); |
2368 | 0 | uint32_t second = reflection->GetUInt32(*b, field_); |
2369 | 0 | return first < second; |
2370 | 0 | } |
2371 | 0 | case FieldDescriptor::CPPTYPE_UINT64: { |
2372 | 0 | uint64_t first = reflection->GetUInt64(*a, field_); |
2373 | 0 | uint64_t second = reflection->GetUInt64(*b, field_); |
2374 | 0 | return first < second; |
2375 | 0 | } |
2376 | 217k | case FieldDescriptor::CPPTYPE_STRING: { |
2377 | 217k | std::string first = reflection->GetString(*a, field_); |
2378 | 217k | std::string second = reflection->GetString(*b, field_); |
2379 | 217k | return first < second; |
2380 | 0 | } |
2381 | 0 | default: |
2382 | 0 | ABSL_DLOG(FATAL) << "Invalid key for map field."; |
2383 | 0 | return true; |
2384 | 219k | } |
2385 | 219k | } |
2386 | | |
2387 | | private: |
2388 | | const FieldDescriptor* field_; |
2389 | | }; |
2390 | | |
2391 | | namespace internal { |
2392 | | class MapFieldPrinterHelper { |
2393 | | public: |
2394 | | // DynamicMapSorter::Sort cannot be used because it enforces syncing with |
2395 | | // repeated field. |
2396 | | static bool SortMap(const Message& message, const Reflection* reflection, |
2397 | | const FieldDescriptor* field, |
2398 | | std::vector<const Message*>* sorted_map_field); |
2399 | | static void CopyKey(const MapKey& key, Message* message, |
2400 | | const FieldDescriptor* field_desc); |
2401 | | static void CopyValue(const MapValueRef& value, Message* message, |
2402 | | const FieldDescriptor* field_desc); |
2403 | | }; |
2404 | | |
2405 | | // Returns true if elements contained in sorted_map_field need to be released. |
2406 | | bool MapFieldPrinterHelper::SortMap( |
2407 | | const Message& message, const Reflection* reflection, |
2408 | | const FieldDescriptor* field, |
2409 | 53.1k | std::vector<const Message*>* sorted_map_field) { |
2410 | 53.1k | bool need_release = false; |
2411 | 53.1k | const MapFieldBase& base = *reflection->GetMapData(message, field); |
2412 | | |
2413 | 53.1k | if (base.IsRepeatedFieldValid()) { |
2414 | 24.6k | const RepeatedPtrField<Message>& map_field = |
2415 | 24.6k | reflection->GetRepeatedPtrFieldInternal<Message>(message, field); |
2416 | 100k | for (int i = 0; i < map_field.size(); ++i) { |
2417 | 75.8k | sorted_map_field->push_back( |
2418 | 75.8k | const_cast<RepeatedPtrField<Message>*>(&map_field)->Mutable(i)); |
2419 | 75.8k | } |
2420 | 28.5k | } else { |
2421 | | // TODO(teboring): For performance, instead of creating map entry message |
2422 | | // for each element, just store map keys and sort them. |
2423 | 28.5k | const Descriptor* map_entry_desc = field->message_type(); |
2424 | 28.5k | const Message* prototype = |
2425 | 28.5k | reflection->GetMessageFactory()->GetPrototype(map_entry_desc); |
2426 | 28.5k | for (MapIterator iter = |
2427 | 28.5k | reflection->MapBegin(const_cast<Message*>(&message), field); |
2428 | 110k | iter != reflection->MapEnd(const_cast<Message*>(&message), field); |
2429 | 81.9k | ++iter) { |
2430 | 81.9k | Message* map_entry_message = prototype->New(); |
2431 | 81.9k | CopyKey(iter.GetKey(), map_entry_message, map_entry_desc->field(0)); |
2432 | 81.9k | CopyValue(iter.GetValueRef(), map_entry_message, |
2433 | 81.9k | map_entry_desc->field(1)); |
2434 | 81.9k | sorted_map_field->push_back(map_entry_message); |
2435 | 81.9k | } |
2436 | 28.5k | need_release = true; |
2437 | 28.5k | } |
2438 | | |
2439 | 53.1k | MapEntryMessageComparator comparator(field->message_type()); |
2440 | 53.1k | std::stable_sort(sorted_map_field->begin(), sorted_map_field->end(), |
2441 | 53.1k | comparator); |
2442 | 53.1k | return need_release; |
2443 | 53.1k | } |
2444 | | |
2445 | | void MapFieldPrinterHelper::CopyKey(const MapKey& key, Message* message, |
2446 | 81.9k | const FieldDescriptor* field_desc) { |
2447 | 81.9k | const Reflection* reflection = message->GetReflection(); |
2448 | 81.9k | switch (field_desc->cpp_type()) { |
2449 | 0 | case FieldDescriptor::CPPTYPE_DOUBLE: |
2450 | 0 | case FieldDescriptor::CPPTYPE_FLOAT: |
2451 | 0 | case FieldDescriptor::CPPTYPE_ENUM: |
2452 | 0 | case FieldDescriptor::CPPTYPE_MESSAGE: |
2453 | 0 | ABSL_LOG(ERROR) << "Not supported."; |
2454 | 0 | break; |
2455 | 81.9k | case FieldDescriptor::CPPTYPE_STRING: |
2456 | 81.9k | reflection->SetString(message, field_desc, key.GetStringValue()); |
2457 | 81.9k | return; |
2458 | 0 | case FieldDescriptor::CPPTYPE_INT64: |
2459 | 0 | reflection->SetInt64(message, field_desc, key.GetInt64Value()); |
2460 | 0 | return; |
2461 | 0 | case FieldDescriptor::CPPTYPE_INT32: |
2462 | 0 | reflection->SetInt32(message, field_desc, key.GetInt32Value()); |
2463 | 0 | return; |
2464 | 0 | case FieldDescriptor::CPPTYPE_UINT64: |
2465 | 0 | reflection->SetUInt64(message, field_desc, key.GetUInt64Value()); |
2466 | 0 | return; |
2467 | 0 | case FieldDescriptor::CPPTYPE_UINT32: |
2468 | 0 | reflection->SetUInt32(message, field_desc, key.GetUInt32Value()); |
2469 | 0 | return; |
2470 | 0 | case FieldDescriptor::CPPTYPE_BOOL: |
2471 | 0 | reflection->SetBool(message, field_desc, key.GetBoolValue()); |
2472 | 0 | return; |
2473 | 81.9k | } |
2474 | 81.9k | } |
2475 | | |
2476 | | void MapFieldPrinterHelper::CopyValue(const MapValueRef& value, |
2477 | | Message* message, |
2478 | 81.9k | const FieldDescriptor* field_desc) { |
2479 | 81.9k | const Reflection* reflection = message->GetReflection(); |
2480 | 81.9k | switch (field_desc->cpp_type()) { |
2481 | 0 | case FieldDescriptor::CPPTYPE_DOUBLE: |
2482 | 0 | reflection->SetDouble(message, field_desc, value.GetDoubleValue()); |
2483 | 0 | return; |
2484 | 0 | case FieldDescriptor::CPPTYPE_FLOAT: |
2485 | 0 | reflection->SetFloat(message, field_desc, value.GetFloatValue()); |
2486 | 0 | return; |
2487 | 0 | case FieldDescriptor::CPPTYPE_ENUM: |
2488 | 0 | reflection->SetEnumValue(message, field_desc, value.GetEnumValue()); |
2489 | 0 | return; |
2490 | 81.7k | case FieldDescriptor::CPPTYPE_MESSAGE: { |
2491 | 81.7k | Message* sub_message = value.GetMessageValue().New(); |
2492 | 81.7k | sub_message->CopyFrom(value.GetMessageValue()); |
2493 | 81.7k | reflection->SetAllocatedMessage(message, sub_message, field_desc); |
2494 | 81.7k | return; |
2495 | 0 | } |
2496 | 219 | case FieldDescriptor::CPPTYPE_STRING: |
2497 | 219 | reflection->SetString(message, field_desc, value.GetStringValue()); |
2498 | 219 | return; |
2499 | 0 | case FieldDescriptor::CPPTYPE_INT64: |
2500 | 0 | reflection->SetInt64(message, field_desc, value.GetInt64Value()); |
2501 | 0 | return; |
2502 | 0 | case FieldDescriptor::CPPTYPE_INT32: |
2503 | 0 | reflection->SetInt32(message, field_desc, value.GetInt32Value()); |
2504 | 0 | return; |
2505 | 0 | case FieldDescriptor::CPPTYPE_UINT64: |
2506 | 0 | reflection->SetUInt64(message, field_desc, value.GetUInt64Value()); |
2507 | 0 | return; |
2508 | 0 | case FieldDescriptor::CPPTYPE_UINT32: |
2509 | 0 | reflection->SetUInt32(message, field_desc, value.GetUInt32Value()); |
2510 | 0 | return; |
2511 | 0 | case FieldDescriptor::CPPTYPE_BOOL: |
2512 | 0 | reflection->SetBool(message, field_desc, value.GetBoolValue()); |
2513 | 0 | return; |
2514 | 81.9k | } |
2515 | 81.9k | } |
2516 | | } // namespace internal |
2517 | | |
2518 | | void TextFormat::Printer::PrintField(const Message& message, |
2519 | | const Reflection* reflection, |
2520 | | const FieldDescriptor* field, |
2521 | 2.92M | BaseTextGenerator* generator) const { |
2522 | 2.92M | if (use_short_repeated_primitives_ && field->is_repeated() && |
2523 | 2.92M | field->cpp_type() != FieldDescriptor::CPPTYPE_STRING && |
2524 | 2.92M | field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { |
2525 | 0 | PrintShortRepeatedField(message, reflection, field, generator); |
2526 | 0 | return; |
2527 | 0 | } |
2528 | | |
2529 | 2.92M | int count = 0; |
2530 | | |
2531 | 2.92M | if (field->is_repeated()) { |
2532 | 305k | count = reflection->FieldSize(message, field); |
2533 | 2.62M | } else if (reflection->HasField(message, field) || |
2534 | 2.62M | field->containing_type()->options().map_entry()) { |
2535 | 2.62M | count = 1; |
2536 | 2.62M | } |
2537 | | |
2538 | 2.92M | std::vector<const Message*> sorted_map_field; |
2539 | 2.92M | bool need_release = false; |
2540 | 2.92M | bool is_map = field->is_map(); |
2541 | 2.92M | if (is_map) { |
2542 | 53.1k | need_release = internal::MapFieldPrinterHelper::SortMap( |
2543 | 53.1k | message, reflection, field, &sorted_map_field); |
2544 | 53.1k | } |
2545 | | |
2546 | 7.60M | for (int j = 0; j < count; ++j) { |
2547 | 4.67M | const int field_index = field->is_repeated() ? j : -1; |
2548 | | |
2549 | 4.67M | PrintFieldName(message, field_index, count, reflection, field, generator); |
2550 | | |
2551 | 4.67M | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
2552 | 1.81M | const FastFieldValuePrinter* printer = GetFieldPrinter(field); |
2553 | 1.81M | const Message& sub_message = |
2554 | 1.81M | field->is_repeated() |
2555 | 1.81M | ? (is_map ? *sorted_map_field[j] |
2556 | 1.15M | : reflection->GetRepeatedMessage(message, field, j)) |
2557 | 1.81M | : reflection->GetMessage(message, field); |
2558 | 1.81M | printer->PrintMessageStart(sub_message, field_index, count, |
2559 | 1.81M | single_line_mode_, generator); |
2560 | 1.81M | generator->Indent(); |
2561 | 1.81M | if (!printer->PrintMessageContent(sub_message, field_index, count, |
2562 | 1.81M | single_line_mode_, generator)) { |
2563 | 1.81M | Print(sub_message, generator); |
2564 | 1.81M | } |
2565 | 1.81M | generator->Outdent(); |
2566 | 1.81M | printer->PrintMessageEnd(sub_message, field_index, count, |
2567 | 1.81M | single_line_mode_, generator); |
2568 | 2.86M | } else { |
2569 | 2.86M | generator->PrintMaybeWithMarker(MarkerToken(), ": "); |
2570 | | // Write the field value. |
2571 | 2.86M | PrintFieldValue(message, reflection, field, field_index, generator); |
2572 | 2.86M | if (single_line_mode_) { |
2573 | 546k | generator->PrintLiteral(" "); |
2574 | 2.32M | } else { |
2575 | 2.32M | generator->PrintLiteral("\n"); |
2576 | 2.32M | } |
2577 | 2.86M | } |
2578 | 4.67M | } |
2579 | | |
2580 | 2.92M | if (need_release) { |
2581 | 81.9k | for (const Message* message_to_delete : sorted_map_field) { |
2582 | 81.9k | delete message_to_delete; |
2583 | 81.9k | } |
2584 | 28.5k | } |
2585 | 2.92M | } |
2586 | | |
2587 | | void TextFormat::Printer::PrintShortRepeatedField( |
2588 | | const Message& message, const Reflection* reflection, |
2589 | 0 | const FieldDescriptor* field, BaseTextGenerator* generator) const { |
2590 | | // Print primitive repeated field in short form. |
2591 | 0 | int size = reflection->FieldSize(message, field); |
2592 | 0 | PrintFieldName(message, /*field_index=*/-1, /*field_count=*/size, reflection, |
2593 | 0 | field, generator); |
2594 | 0 | generator->PrintMaybeWithMarker(MarkerToken(), ": ", "["); |
2595 | 0 | for (int i = 0; i < size; i++) { |
2596 | 0 | if (i > 0) generator->PrintLiteral(", "); |
2597 | 0 | PrintFieldValue(message, reflection, field, i, generator); |
2598 | 0 | } |
2599 | 0 | if (single_line_mode_) { |
2600 | 0 | generator->PrintLiteral("] "); |
2601 | 0 | } else { |
2602 | 0 | generator->PrintLiteral("]\n"); |
2603 | 0 | } |
2604 | 0 | } |
2605 | | |
2606 | | void TextFormat::Printer::PrintFieldName(const Message& message, |
2607 | | int field_index, int field_count, |
2608 | | const Reflection* reflection, |
2609 | | const FieldDescriptor* field, |
2610 | 4.67M | BaseTextGenerator* generator) const { |
2611 | | // if use_field_number_ is true, prints field number instead |
2612 | | // of field name. |
2613 | 4.67M | if (use_field_number_) { |
2614 | 920k | generator->PrintString(absl::StrCat(field->number())); |
2615 | 920k | return; |
2616 | 920k | } |
2617 | | |
2618 | 3.75M | const FastFieldValuePrinter* printer = GetFieldPrinter(field); |
2619 | 3.75M | printer->PrintFieldName(message, field_index, field_count, reflection, field, |
2620 | 3.75M | generator); |
2621 | 3.75M | } |
2622 | | |
2623 | | void TextFormat::Printer::PrintFieldValue(const Message& message, |
2624 | | const Reflection* reflection, |
2625 | | const FieldDescriptor* field, |
2626 | | int index, |
2627 | 2.86M | BaseTextGenerator* generator) const { |
2628 | 5.73M | ABSL_DCHECK(field->is_repeated() || (index == -1)) |
2629 | 5.73M | << "Index must be -1 for non-repeated fields"; |
2630 | | |
2631 | 2.86M | const FastFieldValuePrinter* printer = GetFieldPrinter(field); |
2632 | 2.86M | if (redact_debug_string_ && field->options().debug_redact()) { |
2633 | 0 | IncrementRedactedFieldCounter(); |
2634 | | // TODO(b/258975650): Create OSS redaction documentation |
2635 | 0 | generator->PrintString("[REDACTED]"); |
2636 | 0 | return; |
2637 | 0 | } |
2638 | | |
2639 | 2.86M | switch (field->cpp_type()) { |
2640 | 0 | #define OUTPUT_FIELD(CPPTYPE, METHOD) \ |
2641 | 741k | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
2642 | 741k | printer->Print##METHOD( \ |
2643 | 741k | field->is_repeated() \ |
2644 | 741k | ? reflection->GetRepeated##METHOD(message, field, index) \ |
2645 | 741k | : reflection->Get##METHOD(message, field), \ |
2646 | 741k | generator); \ |
2647 | 741k | break |
2648 | | |
2649 | 581k | OUTPUT_FIELD(INT32, Int32); |
2650 | 22.9k | OUTPUT_FIELD(INT64, Int64); |
2651 | 63.6k | OUTPUT_FIELD(UINT32, UInt32); |
2652 | 854 | OUTPUT_FIELD(UINT64, UInt64); |
2653 | 0 | OUTPUT_FIELD(FLOAT, Float); |
2654 | 16.3k | OUTPUT_FIELD(DOUBLE, Double); |
2655 | 56.0k | OUTPUT_FIELD(BOOL, Bool); |
2656 | 0 | #undef OUTPUT_FIELD |
2657 | | |
2658 | 2.05M | case FieldDescriptor::CPPTYPE_STRING: { |
2659 | 2.05M | std::string scratch; |
2660 | 2.05M | const std::string& value = |
2661 | 2.05M | field->is_repeated() |
2662 | 2.05M | ? reflection->GetRepeatedStringReference(message, field, index, |
2663 | 302k | &scratch) |
2664 | 2.05M | : reflection->GetStringReference(message, field, &scratch); |
2665 | 2.05M | const std::string* value_to_print = &value; |
2666 | 2.05M | std::string truncated_value; |
2667 | 2.05M | if (truncate_string_field_longer_than_ > 0 && |
2668 | 2.05M | static_cast<size_t>(truncate_string_field_longer_than_) < |
2669 | 0 | value.size()) { |
2670 | 0 | truncated_value = value.substr(0, truncate_string_field_longer_than_) + |
2671 | 0 | "...<truncated>..."; |
2672 | 0 | value_to_print = &truncated_value; |
2673 | 0 | } |
2674 | 2.05M | if (field->type() == FieldDescriptor::TYPE_STRING) { |
2675 | 2.04M | printer->PrintString(*value_to_print, generator); |
2676 | 2.04M | } else { |
2677 | 7.78k | ABSL_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_BYTES); |
2678 | 7.78k | printer->PrintBytes(*value_to_print, generator); |
2679 | 7.78k | } |
2680 | 2.05M | break; |
2681 | 0 | } |
2682 | | |
2683 | 72.2k | case FieldDescriptor::CPPTYPE_ENUM: { |
2684 | 72.2k | int enum_value = |
2685 | 72.2k | field->is_repeated() |
2686 | 72.2k | ? reflection->GetRepeatedEnumValue(message, field, index) |
2687 | 72.2k | : reflection->GetEnumValue(message, field); |
2688 | 72.2k | const EnumValueDescriptor* enum_desc = |
2689 | 72.2k | field->enum_type()->FindValueByNumber(enum_value); |
2690 | 72.2k | if (enum_desc != nullptr) { |
2691 | 72.2k | printer->PrintEnum(enum_value, enum_desc->name(), generator); |
2692 | 72.2k | } else { |
2693 | | // Ordinarily, enum_desc should not be null, because proto2 has the |
2694 | | // invariant that set enum field values must be in-range, but with the |
2695 | | // new integer-based API for enums (or the RepeatedField<int> loophole), |
2696 | | // it is possible for the user to force an unknown integer value. So we |
2697 | | // simply use the integer value itself as the enum value name in this |
2698 | | // case. |
2699 | 53 | printer->PrintEnum(enum_value, absl::StrCat(enum_value), generator); |
2700 | 53 | } |
2701 | 72.2k | break; |
2702 | 0 | } |
2703 | | |
2704 | 0 | case FieldDescriptor::CPPTYPE_MESSAGE: |
2705 | 0 | Print(field->is_repeated() |
2706 | 0 | ? reflection->GetRepeatedMessage(message, field, index) |
2707 | 0 | : reflection->GetMessage(message, field), |
2708 | 0 | generator); |
2709 | 0 | break; |
2710 | 2.86M | } |
2711 | 2.86M | } |
2712 | | |
2713 | | /* static */ bool TextFormat::Print(const Message& message, |
2714 | 0 | io::ZeroCopyOutputStream* output) { |
2715 | 0 | return Printer().Print(message, output); |
2716 | 0 | } |
2717 | | |
2718 | | /* static */ bool TextFormat::PrintUnknownFields( |
2719 | 0 | const UnknownFieldSet& unknown_fields, io::ZeroCopyOutputStream* output) { |
2720 | 0 | return Printer().PrintUnknownFields(unknown_fields, output); |
2721 | 0 | } |
2722 | | |
2723 | | /* static */ bool TextFormat::PrintToString(const Message& message, |
2724 | 0 | std::string* output) { |
2725 | 0 | return Printer().PrintToString(message, output); |
2726 | 0 | } |
2727 | | |
2728 | | /* static */ bool TextFormat::PrintUnknownFieldsToString( |
2729 | 0 | const UnknownFieldSet& unknown_fields, std::string* output) { |
2730 | 0 | return Printer().PrintUnknownFieldsToString(unknown_fields, output); |
2731 | 0 | } |
2732 | | |
2733 | | /* static */ void TextFormat::PrintFieldValueToString( |
2734 | | const Message& message, const FieldDescriptor* field, int index, |
2735 | 0 | std::string* output) { |
2736 | 0 | return Printer().PrintFieldValueToString(message, field, index, output); |
2737 | 0 | } |
2738 | | |
2739 | | /* static */ bool TextFormat::ParseFieldValueFromString( |
2740 | 0 | const std::string& input, const FieldDescriptor* field, Message* message) { |
2741 | 0 | return Parser().ParseFieldValueFromString(input, field, message); |
2742 | 0 | } |
2743 | | |
2744 | | template <typename... T> |
2745 | | PROTOBUF_NOINLINE void TextFormat::OutOfLinePrintString( |
2746 | 41.0k | BaseTextGenerator* generator, const T&... values) { |
2747 | 41.0k | generator->PrintString(absl::StrCat(values...)); |
2748 | 41.0k | } void google::protobuf::TextFormat::OutOfLinePrintString<int>(google::protobuf::TextFormat::BaseTextGenerator*, int const&) Line | Count | Source | 2746 | 23.4k | BaseTextGenerator* generator, const T&... values) { | 2747 | 23.4k | generator->PrintString(absl::StrCat(values...)); | 2748 | 23.4k | } |
void google::protobuf::TextFormat::OutOfLinePrintString<unsigned long>(google::protobuf::TextFormat::BaseTextGenerator*, unsigned long const&) Line | Count | Source | 2746 | 10.3k | BaseTextGenerator* generator, const T&... values) { | 2747 | 10.3k | generator->PrintString(absl::StrCat(values...)); | 2748 | 10.3k | } |
void google::protobuf::TextFormat::OutOfLinePrintString<absl::Hex>(google::protobuf::TextFormat::BaseTextGenerator*, absl::Hex const&) Line | Count | Source | 2746 | 7.25k | BaseTextGenerator* generator, const T&... values) { | 2747 | 7.25k | generator->PrintString(absl::StrCat(values...)); | 2748 | 7.25k | } |
|
2749 | | |
2750 | | void TextFormat::Printer::PrintUnknownFields( |
2751 | | const UnknownFieldSet& unknown_fields, BaseTextGenerator* generator, |
2752 | 1.50M | int recursion_budget) const { |
2753 | 1.52M | for (int i = 0; i < unknown_fields.field_count(); i++) { |
2754 | 23.4k | const UnknownField& field = unknown_fields.field(i); |
2755 | | |
2756 | 23.4k | switch (field.type()) { |
2757 | 10.3k | case UnknownField::TYPE_VARINT: |
2758 | 10.3k | OutOfLinePrintString(generator, field.number()); |
2759 | 10.3k | generator->PrintMaybeWithMarker(MarkerToken(), ": "); |
2760 | 10.3k | OutOfLinePrintString(generator, field.varint()); |
2761 | 10.3k | if (single_line_mode_) { |
2762 | 0 | generator->PrintLiteral(" "); |
2763 | 10.3k | } else { |
2764 | 10.3k | generator->PrintLiteral("\n"); |
2765 | 10.3k | } |
2766 | 10.3k | break; |
2767 | 4.88k | case UnknownField::TYPE_FIXED32: { |
2768 | 4.88k | OutOfLinePrintString(generator, field.number()); |
2769 | 4.88k | generator->PrintMaybeWithMarker(MarkerToken(), ": ", "0x"); |
2770 | 4.88k | OutOfLinePrintString(generator, |
2771 | 4.88k | absl::Hex(field.fixed32(), absl::kZeroPad8)); |
2772 | 4.88k | if (single_line_mode_) { |
2773 | 0 | generator->PrintLiteral(" "); |
2774 | 4.88k | } else { |
2775 | 4.88k | generator->PrintLiteral("\n"); |
2776 | 4.88k | } |
2777 | 4.88k | break; |
2778 | 0 | } |
2779 | 2.36k | case UnknownField::TYPE_FIXED64: { |
2780 | 2.36k | OutOfLinePrintString(generator, field.number()); |
2781 | 2.36k | generator->PrintMaybeWithMarker(MarkerToken(), ": ", "0x"); |
2782 | 2.36k | OutOfLinePrintString(generator, |
2783 | 2.36k | absl::Hex(field.fixed64(), absl::kZeroPad16)); |
2784 | 2.36k | if (single_line_mode_) { |
2785 | 0 | generator->PrintLiteral(" "); |
2786 | 2.36k | } else { |
2787 | 2.36k | generator->PrintLiteral("\n"); |
2788 | 2.36k | } |
2789 | 2.36k | break; |
2790 | 0 | } |
2791 | 5.73k | case UnknownField::TYPE_LENGTH_DELIMITED: { |
2792 | 5.73k | OutOfLinePrintString(generator, field.number()); |
2793 | 5.73k | const std::string& value = field.length_delimited(); |
2794 | | // We create a CodedInputStream so that we can adhere to our recursion |
2795 | | // budget when we attempt to parse the data. UnknownFieldSet parsing is |
2796 | | // recursive because of groups. |
2797 | 5.73k | io::CodedInputStream input_stream( |
2798 | 5.73k | reinterpret_cast<const uint8_t*>(value.data()), value.size()); |
2799 | 5.73k | input_stream.SetRecursionLimit(recursion_budget); |
2800 | 5.73k | UnknownFieldSet embedded_unknown_fields; |
2801 | 5.73k | if (!value.empty() && recursion_budget > 0 && |
2802 | 5.73k | embedded_unknown_fields.ParseFromCodedStream(&input_stream)) { |
2803 | | // This field is parseable as a Message. |
2804 | | // So it is probably an embedded message. |
2805 | 1.77k | if (single_line_mode_) { |
2806 | 0 | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{ "); |
2807 | 1.77k | } else { |
2808 | 1.77k | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{\n"); |
2809 | 1.77k | generator->Indent(); |
2810 | 1.77k | } |
2811 | 1.77k | PrintUnknownFields(embedded_unknown_fields, generator, |
2812 | 1.77k | recursion_budget - 1); |
2813 | 1.77k | if (single_line_mode_) { |
2814 | 0 | generator->PrintLiteral("} "); |
2815 | 1.77k | } else { |
2816 | 1.77k | generator->Outdent(); |
2817 | 1.77k | generator->PrintLiteral("}\n"); |
2818 | 1.77k | } |
2819 | 3.95k | } else { |
2820 | | // This field is not parseable as a Message (or we ran out of |
2821 | | // recursion budget). So it is probably just a plain string. |
2822 | 3.95k | generator->PrintMaybeWithMarker(MarkerToken(), ": ", "\""); |
2823 | 3.95k | generator->PrintString(absl::CEscape(value)); |
2824 | 3.95k | if (single_line_mode_) { |
2825 | 0 | generator->PrintLiteral("\" "); |
2826 | 3.95k | } else { |
2827 | 3.95k | generator->PrintLiteral("\"\n"); |
2828 | 3.95k | } |
2829 | 3.95k | } |
2830 | 5.73k | break; |
2831 | 0 | } |
2832 | 71 | case UnknownField::TYPE_GROUP: |
2833 | 71 | OutOfLinePrintString(generator, field.number()); |
2834 | 71 | if (single_line_mode_) { |
2835 | 0 | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{ "); |
2836 | 71 | } else { |
2837 | 71 | generator->PrintMaybeWithMarker(MarkerToken(), " ", "{\n"); |
2838 | 71 | generator->Indent(); |
2839 | 71 | } |
2840 | | // For groups, we recurse without checking the budget. This is OK, |
2841 | | // because if the groups were too deeply nested then we would have |
2842 | | // already rejected the message when we originally parsed it. |
2843 | 71 | PrintUnknownFields(field.group(), generator, recursion_budget - 1); |
2844 | 71 | if (single_line_mode_) { |
2845 | 0 | generator->PrintLiteral("} "); |
2846 | 71 | } else { |
2847 | 71 | generator->Outdent(); |
2848 | 71 | generator->PrintLiteral("}\n"); |
2849 | 71 | } |
2850 | 71 | break; |
2851 | 23.4k | } |
2852 | 23.4k | } |
2853 | 1.50M | } |
2854 | | |
2855 | | } // namespace protobuf |
2856 | | } // namespace google |
2857 | | |
2858 | | #include "google/protobuf/port_undef.inc" |