Coverage Report

Created: 2026-02-22 06:11

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