Coverage Report

Created: 2026-08-13 07:06

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
27.9k
bool ParseTextMessage(const uint8_t* data, size_t size, Message* output) {
25
27.9k
  return ParseTextMessage({reinterpret_cast<const char*>(data), size}, output);
26
27.9k
}
27
28
27.9k
bool ParseTextMessage(const std::string& data, protobuf::Message* output) {
29
27.9k
  output->Clear();
30
27.9k
  TextFormat::Parser parser;
31
27.9k
  parser.AllowPartialMessage(true);
32
27.9k
  PrepareTextParser(parser);
33
27.9k
  if (!parser.ParseFromString(data, output)) {
34
11
    output->Clear();
35
11
    return false;
36
11
  }
37
27.9k
  return true;
38
27.9k
}
39
40
size_t SaveMessageAsText(const Message& message, uint8_t* data,
41
0
                         size_t max_size) {
42
0
  std::string result = SaveMessageAsText(message);
43
0
  if (result.size() <= max_size) {
44
0
    memcpy(data, result.data(), result.size());
45
0
    return result.size();
46
0
  }
47
0
  return 0;
48
0
}
49
50
0
std::string SaveMessageAsText(const protobuf::Message& message) {
51
0
  std::string tmp;
52
0
  if (!protobuf::TextFormat::PrintToString(message, &tmp)) tmp.clear();
53
0
  return tmp;
54
0
}
55
56
}  // namespace protobuf_mutator