-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobuf_utils.cpp
executable file
·191 lines (158 loc) · 5.84 KB
/
protobuf_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2018-2020 tsurugi project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tateyama/utils/protobuf_utils.h>
#include <ostream>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/io/coded_stream.h>
namespace tateyama::utils {
bool ParseDelimitedFromZeroCopyStream(
google::protobuf::MessageLite* message,
google::protobuf::io::ZeroCopyInputStream* input,
bool* clean_eof
) {
google::protobuf::io::CodedInputStream coded_input(input);
return ParseDelimitedFromCodedStream(message, &coded_input, clean_eof);
}
bool ParseDelimitedFromCodedStream(
google::protobuf::MessageLite* message,
google::protobuf::io::CodedInputStream* input,
bool* clean_eof
) {
if (clean_eof != nullptr) *clean_eof = false;
int start = input->CurrentPosition();
// Read the size.
google::protobuf::uint32 size{};
if (!input->ReadVarint32(&size)) {
if (clean_eof != nullptr) *clean_eof = input->CurrentPosition() == start;
return false;
}
// Get the position after any size bytes have been read (and only the message
// itself remains).
int position_after_size = input->CurrentPosition();
// Tell the stream not to read beyond that size.
google::protobuf::io::CodedInputStream::Limit limit = input->PushLimit(size);
// Parse the message.
if (!message->MergeFromCodedStream(input)) return false;
if (!input->ConsumedEntireMessage()) return false;
if (input->CurrentPosition() - position_after_size != static_cast<int>(size)) {
return false;
}
// Release the limit.
input->PopLimit(limit);
return true;
}
bool SerializeDelimitedToZeroCopyStream(
google::protobuf::MessageLite const& message,
google::protobuf::io::ZeroCopyOutputStream* output
) {
google::protobuf::io::CodedOutputStream coded_output(output);
return SerializeDelimitedToCodedStream(message, &coded_output);
}
bool SerializeDelimitedToCodedStream(
google::protobuf::MessageLite const& message,
google::protobuf::io::CodedOutputStream* output
) {
// Write the size.
size_t size = message.ByteSizeLong();
if (size > INT_MAX) return false;
output->WriteVarint32(size);
// Write the content.
google::protobuf::uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
if (buffer != nullptr) {
// Optimization: The message fits in one buffer, so use the faster
// direct-to-array serialization path.
message.SerializeWithCachedSizesToArray(buffer);
} else {
// Slightly-slower path when the message is multiple buffers.
message.SerializeWithCachedSizes(output);
if (output->HadError()) return false;
}
return true;
}
bool SerializeDelimitedToOstream(
google::protobuf::MessageLite const& message,
std::ostream* output
) {
google::protobuf::io::OstreamOutputStream zero_copy_output(output);
if (!SerializeDelimitedToZeroCopyStream(message, &zero_copy_output)) {
return false;
}
return output->good();
}
bool GetDelimitedBodyFromCodedStream(
google::protobuf::io::CodedInputStream* input,
bool* clean_eof,
std::string_view& out
) {
if (clean_eof != nullptr) *clean_eof = false;
int start = input->CurrentPosition();
// Read the size.
google::protobuf::uint32 size{};
if (!input->ReadVarint32(&size)) {
if (clean_eof != nullptr) *clean_eof = input->CurrentPosition() == start;
return false;
}
// Get the position after any size bytes have been read (and only the message
// itself remains).
int position_after_size = input->CurrentPosition();
// Tell the stream not to read beyond that size.
google::protobuf::io::CodedInputStream::Limit limit = input->PushLimit(size);
void const* data{};
int sz{};
if (! input->GetDirectBufferPointer(std::addressof(data), std::addressof(sz))) {
return false;
}
out = {static_cast<char const*>(data), static_cast<std::size_t>(sz)};
if (! input->Skip(sz)) {
return false;
}
// if (!input->ConsumedEntireMessage()) return false;
if (input->CurrentPosition() - position_after_size != static_cast<int>(size)) {
return false;
}
// Release the limit.
input->PopLimit(limit);
return true;
}
bool GetDelimitedBodyFromZeroCopyStream(
google::protobuf::io::ZeroCopyInputStream* input,
bool* clean_eof,
std::string_view& out
) {
google::protobuf::io::CodedInputStream coded_input(input);
return GetDelimitedBodyFromCodedStream(&coded_input, clean_eof, out);
}
bool PutDelimitedBodyToOstream(std::string_view in, std::ostream* output) {
google::protobuf::io::OstreamOutputStream zero_copy_output(output);
if (!PutDelimitedBodyToZeroCopyStream(in, &zero_copy_output)) {
return false;
}
return output->good();
}
bool PutDelimitedBodyToZeroCopyStream(std::string_view in, google::protobuf::io::ZeroCopyOutputStream* output) {
google::protobuf::io::CodedOutputStream coded_output(output);
return PutDelimitedBodyToCodedStream(in, &coded_output);
}
bool PutDelimitedBodyToCodedStream(std::string_view in, google::protobuf::io::CodedOutputStream* output) {
// Write the size.
size_t size = in.size();
if (size > INT_MAX) return false;
output->WriteVarint32(size);
// Write the content.
output->WriteRaw(in.data(), size);
return true;
}
}