Coverage Report

Created: 2025-04-27 06:20

/src/libprotobuf-mutator/port/protobuf.h
Line
Count
Source (jump to first uncovered line)
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
#ifndef PORT_PROTOBUF_H_
16
#define PORT_PROTOBUF_H_
17
18
#include <string>
19
20
#include "google/protobuf/any.pb.h"
21
#include "google/protobuf/descriptor.pb.h"
22
#include "google/protobuf/message.h"
23
#include "google/protobuf/stubs/common.h"  // for GOOGLE_PROTOBUF_VERSION
24
#include "google/protobuf/text_format.h"
25
#include "google/protobuf/util/message_differencer.h"
26
#include "google/protobuf/wire_format.h"
27
28
namespace google {
29
namespace protobuf {
30
#if GOOGLE_PROTOBUF_VERSION < 4025000
31
32
template <typename T>
33
const T* DownCastMessage(const Message* message) {
34
  return static_cast<const T*>(message);
35
}
36
37
template <typename T>
38
T* DownCastMessage(Message* message) {
39
  const Message* message_const = message;
40
  return const_cast<T*>(DownCastMessage<T>(message_const));
41
}
42
43
#elif GOOGLE_PROTOBUF_VERSION < 5029000
44
45
template <typename T>
46
const T* DownCastMessage(const Message* message) {
47
  return DownCastToGenerated<T>(message);
48
}
49
50
template <typename T>
51
T* DownCastMessage(Message* message) {
52
  return DownCastToGenerated<T>(message);
53
}
54
55
#endif  // GOOGLE_PROTOBUF_VERSION
56
}  // namespace protobuf
57
}  // namespace google
58
59
namespace protobuf_mutator {
60
61
namespace protobuf = google::protobuf;
62
63
inline bool RequiresUtf8Validation(
64
0
    const google::protobuf::FieldDescriptor& descriptor) {
65
0
  // commit d85c9944c55fb38f4eae149979a0f680ea125ecb of >= v3(!).22.0
66
0
#if GOOGLE_PROTOBUF_VERSION >= 4022000
67
0
  return descriptor.requires_utf8_validation();
68
0
#else
69
0
  return descriptor.type() == google::protobuf::FieldDescriptor::TYPE_STRING &&
70
0
         descriptor.file()->syntax() ==
71
0
             google::protobuf::FileDescriptor::SYNTAX_PROTO3;
72
0
#endif
73
0
}
74
75
0
inline bool HasPresence(const google::protobuf::FieldDescriptor& descriptor) {
76
0
  // commit bb30225f06c36399757dc698b409d5f79738e8d1 of >=3.12.0
77
0
#if GOOGLE_PROTOBUF_VERSION >= 3012000
78
0
  return descriptor.has_presence();
79
0
#else
80
0
  // NOTE: This mimics Protobuf 3.21.12 ("3021012")
81
0
  return !descriptor.is_repeated() &&
82
0
         (descriptor.cpp_type() ==
83
0
              google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE ||
84
0
          descriptor.containing_oneof() ||
85
0
          descriptor.file()->syntax() ==
86
0
              google::protobuf::FileDescriptor::SYNTAX_PROTO2);
87
0
#endif
88
0
}
89
90
0
inline void PrepareTextParser(google::protobuf::TextFormat::Parser& parser) {
91
0
  // commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0 for .SetRecursionLimit
92
0
  // commit 176f7db11d8242b36a3ea6abb1cc436fca5bf75d of >=3.8.0 for .AllowUnknownField
93
0
#if GOOGLE_PROTOBUF_VERSION >= 3008000
94
0
  parser.SetRecursionLimit(100);
95
0
  parser.AllowUnknownField(true);
96
0
#endif
97
0
}
98
99
0
constexpr bool TextParserCanSetRecursionLimit() {
100
0
  // commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0
101
0
  return GOOGLE_PROTOBUF_VERSION >= 3008000;
102
0
}
103
104
0
constexpr bool TextParserCanAllowUnknownField() {
105
0
  // commit 176f7db11d8242b36a3ea6abb1cc436fca5bf75d of >=3.8.0
106
0
  return GOOGLE_PROTOBUF_VERSION >= 3008000;
107
0
}
108
109
}  // namespace protobuf_mutator
110
111
#endif  // PORT_PROTOBUF_H_