|
14 | 14 | #include <cstddef> |
15 | 15 | #include <cstdint> |
16 | 16 | #include <cstring> |
| 17 | +#include <initializer_list> |
17 | 18 | #include <new> // IWYU pragma: keep for operator new(). |
18 | 19 | #include <queue> |
19 | 20 | #include <string> |
| 21 | +#include <utility> |
| 22 | +#include <variant> |
20 | 23 | #include <vector> |
21 | 24 |
|
| 25 | +#include "absl/algorithm/container.h" |
22 | 26 | #include "absl/base/call_once.h" |
23 | 27 | #include "absl/base/optimization.h" |
24 | 28 | #include "absl/container/flat_hash_map.h" |
25 | 29 | #include "absl/container/flat_hash_set.h" |
26 | 30 | #include "absl/hash/hash.h" |
27 | 31 | #include "absl/log/absl_check.h" |
28 | 32 | #include "absl/log/absl_log.h" |
| 33 | +#include "absl/strings/escaping.h" |
| 34 | +#include "absl/strings/match.h" |
| 35 | +#include "absl/strings/str_cat.h" |
| 36 | +#include "absl/strings/str_format.h" |
29 | 37 | #include "absl/strings/str_join.h" |
| 38 | +#include "absl/strings/str_split.h" |
30 | 39 | #include "absl/strings/string_view.h" |
| 40 | +#include "absl/strings/strip.h" |
31 | 41 | #include "absl/synchronization/mutex.h" |
32 | 42 | #include "absl/types/optional.h" |
33 | 43 | #include "google/protobuf/descriptor.h" |
|
37 | 47 | #include "google/protobuf/generated_message_tctable_impl.h" |
38 | 48 | #include "google/protobuf/generated_message_util.h" |
39 | 49 | #include "google/protobuf/io/coded_stream.h" |
| 50 | +#include "google/protobuf/io/tokenizer.h" |
40 | 51 | #include "google/protobuf/map_field.h" |
41 | 52 | #include "google/protobuf/message_lite.h" |
42 | 53 | #include "google/protobuf/parse_context.h" |
43 | 54 | #include "google/protobuf/port.h" |
44 | 55 | #include "google/protobuf/reflection_internal.h" |
45 | 56 | #include "google/protobuf/reflection_ops.h" |
46 | 57 | #include "google/protobuf/reflection_visit_fields.h" |
| 58 | +#include "google/protobuf/text_format.h" |
47 | 59 | #include "google/protobuf/unknown_field_set.h" |
48 | 60 | #include "google/protobuf/wire_format.h" |
49 | 61 |
|
@@ -124,6 +136,205 @@ PROTOBUF_CONSTINIT PROTOBUF_EXPORT const DescriptorMethods |
124 | 136 | using internal::ReflectionOps; |
125 | 137 | using internal::WireFormat; |
126 | 138 |
|
| 139 | +namespace { |
| 140 | + |
| 141 | +enum class AbslFlagFormat { |
| 142 | + kTextFormat, |
| 143 | + kSerialized, |
| 144 | +}; |
| 145 | + |
| 146 | +struct AbslFlagHeader { |
| 147 | + AbslFlagFormat format; |
| 148 | + absl::string_view format_name; |
| 149 | + std::vector<absl::string_view> options; |
| 150 | + bool uses_dead_char = false; |
| 151 | + bool uses_prefix = false; |
| 152 | +}; |
| 153 | + |
| 154 | +std::variant<AbslFlagHeader, std::string> ConsumeAbslFlagHeader( |
| 155 | + absl::string_view& text) { |
| 156 | + AbslFlagHeader header; |
| 157 | + |
| 158 | + if (text.empty()) { |
| 159 | + // Whatever format is fine. |
| 160 | + header.format = AbslFlagFormat::kTextFormat; |
| 161 | + return header; |
| 162 | + } |
| 163 | + |
| 164 | + if (absl::ConsumePrefix(&text, ":")) { |
| 165 | + header.uses_dead_char = true; |
| 166 | + } |
| 167 | + |
| 168 | + auto pos = text.find(':'); |
| 169 | + if (pos == text.npos) { |
| 170 | + header.format = AbslFlagFormat::kTextFormat; |
| 171 | + return header; |
| 172 | + } |
| 173 | + |
| 174 | + header.uses_prefix = true; |
| 175 | + |
| 176 | + absl::string_view format_spec = text.substr(0, pos); |
| 177 | + if (!header.uses_dead_char) { |
| 178 | + header.format_name = format_spec; |
| 179 | + // Legacy specs. |
| 180 | + if (format_spec == "text") { |
| 181 | + header.format = AbslFlagFormat::kTextFormat; |
| 182 | + } else if (format_spec == "base64text") { |
| 183 | + header.format = AbslFlagFormat::kTextFormat; |
| 184 | + header.options = {"base64"}; |
| 185 | + } else if (format_spec == "base64serialized") { |
| 186 | + header.format = AbslFlagFormat::kSerialized; |
| 187 | + header.options = {"base64"}; |
| 188 | + } else { |
| 189 | + if (absl::StrContains(format_spec, ",")) { |
| 190 | + return absl::StrFormat( |
| 191 | + "Format options are only allowed with delimited format specifier. " |
| 192 | + "Use `:%1$s:` instead of `%1$s:`", |
| 193 | + format_spec); |
| 194 | + } |
| 195 | + header.uses_prefix = false; |
| 196 | + header.format = AbslFlagFormat::kTextFormat; |
| 197 | + return header; |
| 198 | + } |
| 199 | + } else { |
| 200 | + std::vector<absl::string_view> parts = absl::StrSplit(format_spec, ','); |
| 201 | + header.format_name = parts[0]; |
| 202 | + |
| 203 | + if (header.format_name == "text") { |
| 204 | + header.format = AbslFlagFormat::kTextFormat; |
| 205 | + } else if (header.format_name == "serialized") { |
| 206 | + header.format = AbslFlagFormat::kSerialized; |
| 207 | + } else { |
| 208 | + return absl::StrFormat("Invalid format `%s`.", header.format_name); |
| 209 | + } |
| 210 | + |
| 211 | + header.options.assign(parts.begin() + 1, parts.end()); |
| 212 | + } |
| 213 | + |
| 214 | + if (header.uses_prefix) { |
| 215 | + text.remove_prefix(pos + 1); |
| 216 | + } |
| 217 | + return header; |
| 218 | +} |
| 219 | + |
| 220 | +} // namespace |
| 221 | + |
| 222 | +bool Message::AbslParseFlagImpl(absl::string_view text, std::string& error) { |
| 223 | + Clear(); |
| 224 | + |
| 225 | + auto header_or_error = ConsumeAbslFlagHeader(text); |
| 226 | + if (std::holds_alternative<std::string>(header_or_error)) { |
| 227 | + error = std::get<std::string>(header_or_error); |
| 228 | + return false; |
| 229 | + } |
| 230 | + auto header = std::get<AbslFlagHeader>(std::move(header_or_error)); |
| 231 | + |
| 232 | + if (!header.uses_dead_char) { |
| 233 | + error = "Prefix must start with a `:`. Eg `:text:`."; |
| 234 | + return false; |
| 235 | + } |
| 236 | + |
| 237 | + // If we have a prefix without a dead char, verify that the message does not |
| 238 | + // have a field by that name as that would be ambiguous. |
| 239 | + if (!header.uses_dead_char && header.uses_prefix && |
| 240 | + GetDescriptor()->FindFieldByName(header.format_name) != nullptr) { |
| 241 | + error = absl::StrFormat( |
| 242 | + "Prefix `%s:` used is ambiguous with message fields. If you meant to " |
| 243 | + "use this prefix, use `:%s:` instead. If you meant to use text " |
| 244 | + "format, use `:text:` as a prefix.", |
| 245 | + header.format_name, header.format_name); |
| 246 | + return false; |
| 247 | + } |
| 248 | + |
| 249 | + const auto verify_options = |
| 250 | + [&](std::initializer_list<absl::string_view> valid_options) -> bool { |
| 251 | + for (absl::string_view o : header.options) { |
| 252 | + if (!absl::c_linear_search(valid_options, o)) { |
| 253 | + error = absl::StrFormat("Unknown option `%s` for format `%s`.", o, |
| 254 | + header.format_name); |
| 255 | + return false; |
| 256 | + } |
| 257 | + } |
| 258 | + return true; |
| 259 | + }; |
| 260 | + |
| 261 | + static constexpr absl::string_view kBase64 = "base64"; |
| 262 | + |
| 263 | + std::string unescaped; |
| 264 | + const auto unescape_if_needed = [&] { |
| 265 | + if (absl::c_linear_search(header.options, kBase64)) { |
| 266 | + if (!absl::Base64Unescape(text, &unescaped)) { |
| 267 | + error = absl::StrFormat("Invalid base64 input."); |
| 268 | + return false; |
| 269 | + } |
| 270 | + text = unescaped; |
| 271 | + } |
| 272 | + return true; |
| 273 | + }; |
| 274 | + |
| 275 | + switch (header.format) { |
| 276 | + case AbslFlagFormat::kTextFormat: { |
| 277 | + static constexpr absl::string_view kIgnoreUnknown = "ignore_unknown"; |
| 278 | + if (!verify_options({kIgnoreUnknown, kBase64})) return false; |
| 279 | + if (!unescape_if_needed()) return false; |
| 280 | + TextFormat::Parser parser; |
| 281 | + struct StringErrorCollector : io::ErrorCollector { |
| 282 | + explicit StringErrorCollector(std::string& error) : error(error) {} |
| 283 | + std::string& error; |
| 284 | + void RecordError(int line, io::ColumnNumber column, |
| 285 | + absl::string_view message) override { |
| 286 | + error = absl::StrFormat("(Line %v, Column %v): %v", line, column, |
| 287 | + message); |
| 288 | + } |
| 289 | + } collector(error); |
| 290 | + if (absl::c_linear_search(header.options, kIgnoreUnknown)) { |
| 291 | + parser.AllowUnknownField(true); |
| 292 | + parser.AllowUnknownExtension(true); |
| 293 | + } |
| 294 | + parser.RecordErrorsTo(&collector); |
| 295 | + return parser.ParseFromString(text, this); |
| 296 | + } |
| 297 | + |
| 298 | + case AbslFlagFormat::kSerialized: { |
| 299 | + if (!verify_options({kBase64})) return false; |
| 300 | + if (!unescape_if_needed()) return false; |
| 301 | + return ParseFromString(text); |
| 302 | + } |
| 303 | + |
| 304 | + default: |
| 305 | + internal::Unreachable(); |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +std::string Message::AbslUnparseFlagImpl() const { |
| 310 | + bool has_ufs = !GetReflection()->GetUnknownFields(*this).empty(); |
| 311 | + internal::VisitMessageFields(*this, [&](const auto& msg) { |
| 312 | + has_ufs = has_ufs || !msg.GetReflection()->GetUnknownFields(msg).empty(); |
| 313 | + }); |
| 314 | + |
| 315 | + if (has_ufs) { |
| 316 | + // We can't use text format because it won't round trip |
| 317 | + // Use binary instead. |
| 318 | + return absl::StrCat(":serialized,base64:", |
| 319 | + absl::Base64Escape(SerializeAsString())); |
| 320 | + } else { |
| 321 | + TextFormat::Printer printer; |
| 322 | + printer.SetSingleLineMode(true); |
| 323 | + printer.SetUseShortRepeatedPrimitives(true); |
| 324 | + std::string str; |
| 325 | + // PrintToString can't really fail. |
| 326 | + (void)printer.PrintToString(*this, &str); |
| 327 | + |
| 328 | + // If completely empty, just return the empty string. |
| 329 | + // It is usually the default and nicer to read. |
| 330 | + if (str.empty()) { |
| 331 | + return str; |
| 332 | + } |
| 333 | + |
| 334 | + return absl::StrCat(":text:", str); |
| 335 | + } |
| 336 | +} |
| 337 | + |
127 | 338 | void Message::MergeImpl(MessageLite& to, const MessageLite& from) { |
128 | 339 | ReflectionOps::Merge(DownCastMessage<Message>(from), |
129 | 340 | DownCastMessage<Message>(&to)); |
|
0 commit comments