xref: /aosp_15_r20/external/libprotobuf-mutator/src/text_format.cc (revision fd525a9c096e28cf6f8d8719388df0568a611e7b)
1*fd525a9cSAndroid Build Coastguard Worker // Copyright 2017 Google Inc. All rights reserved.
2*fd525a9cSAndroid Build Coastguard Worker //
3*fd525a9cSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*fd525a9cSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*fd525a9cSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*fd525a9cSAndroid Build Coastguard Worker //
7*fd525a9cSAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*fd525a9cSAndroid Build Coastguard Worker //
9*fd525a9cSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*fd525a9cSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*fd525a9cSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*fd525a9cSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*fd525a9cSAndroid Build Coastguard Worker // limitations under the License.
14*fd525a9cSAndroid Build Coastguard Worker 
15*fd525a9cSAndroid Build Coastguard Worker #include "src/text_format.h"
16*fd525a9cSAndroid Build Coastguard Worker 
17*fd525a9cSAndroid Build Coastguard Worker #include "port/protobuf.h"
18*fd525a9cSAndroid Build Coastguard Worker 
19*fd525a9cSAndroid Build Coastguard Worker namespace protobuf_mutator {
20*fd525a9cSAndroid Build Coastguard Worker 
21*fd525a9cSAndroid Build Coastguard Worker using protobuf::Message;
22*fd525a9cSAndroid Build Coastguard Worker using protobuf::TextFormat;
23*fd525a9cSAndroid Build Coastguard Worker 
ParseTextMessage(const uint8_t * data,size_t size,Message * output)24*fd525a9cSAndroid Build Coastguard Worker bool ParseTextMessage(const uint8_t* data, size_t size, Message* output) {
25*fd525a9cSAndroid Build Coastguard Worker   return ParseTextMessage({reinterpret_cast<const char*>(data), size}, output);
26*fd525a9cSAndroid Build Coastguard Worker }
27*fd525a9cSAndroid Build Coastguard Worker 
ParseTextMessage(const std::string & data,protobuf::Message * output)28*fd525a9cSAndroid Build Coastguard Worker bool ParseTextMessage(const std::string& data, protobuf::Message* output) {
29*fd525a9cSAndroid Build Coastguard Worker   output->Clear();
30*fd525a9cSAndroid Build Coastguard Worker   TextFormat::Parser parser;
31*fd525a9cSAndroid Build Coastguard Worker   parser.SetRecursionLimit(100);
32*fd525a9cSAndroid Build Coastguard Worker   parser.AllowPartialMessage(true);
33*fd525a9cSAndroid Build Coastguard Worker   parser.AllowUnknownField(true);
34*fd525a9cSAndroid Build Coastguard Worker   if (!parser.ParseFromString(data, output)) {
35*fd525a9cSAndroid Build Coastguard Worker     output->Clear();
36*fd525a9cSAndroid Build Coastguard Worker     return false;
37*fd525a9cSAndroid Build Coastguard Worker   }
38*fd525a9cSAndroid Build Coastguard Worker   return true;
39*fd525a9cSAndroid Build Coastguard Worker }
40*fd525a9cSAndroid Build Coastguard Worker 
SaveMessageAsText(const Message & message,uint8_t * data,size_t max_size)41*fd525a9cSAndroid Build Coastguard Worker size_t SaveMessageAsText(const Message& message, uint8_t* data,
42*fd525a9cSAndroid Build Coastguard Worker                          size_t max_size) {
43*fd525a9cSAndroid Build Coastguard Worker   std::string result = SaveMessageAsText(message);
44*fd525a9cSAndroid Build Coastguard Worker   if (result.size() <= max_size) {
45*fd525a9cSAndroid Build Coastguard Worker     memcpy(data, result.data(), result.size());
46*fd525a9cSAndroid Build Coastguard Worker     return result.size();
47*fd525a9cSAndroid Build Coastguard Worker   }
48*fd525a9cSAndroid Build Coastguard Worker   return 0;
49*fd525a9cSAndroid Build Coastguard Worker }
50*fd525a9cSAndroid Build Coastguard Worker 
SaveMessageAsText(const protobuf::Message & message)51*fd525a9cSAndroid Build Coastguard Worker std::string SaveMessageAsText(const protobuf::Message& message) {
52*fd525a9cSAndroid Build Coastguard Worker   String tmp;
53*fd525a9cSAndroid Build Coastguard Worker   if (!protobuf::TextFormat::PrintToString(message, &tmp)) return {};
54*fd525a9cSAndroid Build Coastguard Worker   return tmp;
55*fd525a9cSAndroid Build Coastguard Worker }
56*fd525a9cSAndroid Build Coastguard Worker 
57*fd525a9cSAndroid Build Coastguard Worker }  // namespace protobuf_mutator
58