/src/sentencepiece/third_party/protobuf-lite/message_lite.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Protocol Buffers - Google's data interchange format |
2 | | // Copyright 2008 Google Inc. All rights reserved. |
3 | | // https://developers.google.com/protocol-buffers/ |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without |
6 | | // modification, are permitted provided that the following conditions are |
7 | | // met: |
8 | | // |
9 | | // * Redistributions of source code must retain the above copyright |
10 | | // notice, this list of conditions and the following disclaimer. |
11 | | // * Redistributions in binary form must reproduce the above |
12 | | // copyright notice, this list of conditions and the following disclaimer |
13 | | // in the documentation and/or other materials provided with the |
14 | | // distribution. |
15 | | // * Neither the name of Google Inc. nor the names of its |
16 | | // contributors may be used to endorse or promote products derived from |
17 | | // this software without specific prior written permission. |
18 | | // |
19 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | | |
31 | | // Authors: wink@google.com (Wink Saville), |
32 | | // kenton@google.com (Kenton Varda) |
33 | | // Based on original Protocol Buffers design by |
34 | | // Sanjay Ghemawat, Jeff Dean, and others. |
35 | | |
36 | | #include <google/protobuf/message_lite.h> |
37 | | |
38 | | #include <climits> |
39 | | #include <cstdint> |
40 | | #include <string> |
41 | | |
42 | | #include <google/protobuf/stubs/logging.h> |
43 | | #include <google/protobuf/stubs/common.h> |
44 | | #include <google/protobuf/stubs/stringprintf.h> |
45 | | #include <google/protobuf/parse_context.h> |
46 | | #include <google/protobuf/io/coded_stream.h> |
47 | | #include <google/protobuf/io/zero_copy_stream.h> |
48 | | #include <google/protobuf/io/zero_copy_stream_impl.h> |
49 | | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
50 | | #include <google/protobuf/arena.h> |
51 | | #include <google/protobuf/generated_message_table_driven.h> |
52 | | #include <google/protobuf/generated_message_util.h> |
53 | | #include <google/protobuf/repeated_field.h> |
54 | | #include <google/protobuf/stubs/strutil.h> |
55 | | #include <google/protobuf/stubs/stl_util.h> |
56 | | #include <google/protobuf/stubs/mutex.h> |
57 | | |
58 | | #include <google/protobuf/port_def.inc> |
59 | | |
60 | | namespace google { |
61 | | namespace protobuf { |
62 | | |
63 | 0 | std::string MessageLite::InitializationErrorString() const { |
64 | 0 | return "(cannot determine missing fields for lite message)"; |
65 | 0 | } |
66 | | |
67 | 0 | std::string MessageLite::DebugString() const { |
68 | 0 | std::uintptr_t address = reinterpret_cast<std::uintptr_t>(this); |
69 | 0 | return StrCat("MessageLite at 0x", strings::Hex(address)); |
70 | 0 | } |
71 | | |
72 | | namespace { |
73 | | |
74 | | // When serializing, we first compute the byte size, then serialize the message. |
75 | | // If serialization produces a different number of bytes than expected, we |
76 | | // call this function, which crashes. The problem could be due to a bug in the |
77 | | // protobuf implementation but is more likely caused by concurrent modification |
78 | | // of the message. This function attempts to distinguish between the two and |
79 | | // provide a useful error message. |
80 | | void ByteSizeConsistencyError(size_t byte_size_before_serialization, |
81 | | size_t byte_size_after_serialization, |
82 | | size_t bytes_produced_by_serialization, |
83 | 0 | const MessageLite& message) { |
84 | 0 | GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization) |
85 | 0 | << message.GetTypeName() |
86 | 0 | << " was modified concurrently during serialization."; |
87 | 0 | GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization) |
88 | 0 | << "Byte size calculation and serialization were inconsistent. This " |
89 | 0 | "may indicate a bug in protocol buffers or it may be caused by " |
90 | 0 | "concurrent modification of " |
91 | 0 | << message.GetTypeName() << "."; |
92 | 0 | GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal."; |
93 | 0 | } |
94 | | |
95 | | std::string InitializationErrorMessage(const char* action, |
96 | 0 | const MessageLite& message) { |
97 | | // Note: We want to avoid depending on strutil in the lite library, otherwise |
98 | | // we'd use: |
99 | | // |
100 | | // return strings::Substitute( |
101 | | // "Can't $0 message of type \"$1\" because it is missing required " |
102 | | // "fields: $2", |
103 | | // action, message.GetTypeName(), |
104 | | // message.InitializationErrorString()); |
105 | |
|
106 | 0 | std::string result; |
107 | 0 | result += "Can't "; |
108 | 0 | result += action; |
109 | 0 | result += " message of type \""; |
110 | 0 | result += message.GetTypeName(); |
111 | 0 | result += "\" because it is missing required fields: "; |
112 | 0 | result += message.InitializationErrorString(); |
113 | 0 | return result; |
114 | 0 | } |
115 | | |
116 | 0 | inline StringPiece as_string_view(const void* data, int size) { |
117 | 0 | return StringPiece(static_cast<const char*>(data), size); |
118 | 0 | } |
119 | | |
120 | | // Returns true of all required fields are present / have values. |
121 | | inline bool CheckFieldPresence(const internal::ParseContext& ctx, |
122 | | const MessageLite& msg, |
123 | 0 | MessageLite::ParseFlags parse_flags) { |
124 | 0 | if (PROTOBUF_PREDICT_FALSE((parse_flags & MessageLite::kMergePartial) != 0)) { |
125 | 0 | return true; |
126 | 0 | } |
127 | 0 | return msg.IsInitializedWithErrors(); |
128 | 0 | } |
129 | | |
130 | | } // namespace |
131 | | |
132 | 0 | void MessageLite::LogInitializationErrorMessage() const { |
133 | 0 | GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this); |
134 | 0 | } |
135 | | |
136 | | namespace internal { |
137 | | |
138 | | template <bool aliasing> |
139 | | bool MergeFromImpl(StringPiece input, MessageLite* msg, |
140 | 0 | MessageLite::ParseFlags parse_flags) { |
141 | 0 | const char* ptr; |
142 | 0 | internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), |
143 | 0 | aliasing, &ptr, input); |
144 | 0 | ptr = msg->_InternalParse(ptr, &ctx); |
145 | | // ctx has an explicit limit set (length of string_view). |
146 | 0 | if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtLimit())) { |
147 | 0 | return CheckFieldPresence(ctx, *msg, parse_flags); |
148 | 0 | } |
149 | 0 | return false; |
150 | 0 | } Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<false>(google::protobuf::StringPiece, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<true>(google::protobuf::StringPiece, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) |
151 | | |
152 | | template <bool aliasing> |
153 | | bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg, |
154 | 0 | MessageLite::ParseFlags parse_flags) { |
155 | 0 | const char* ptr; |
156 | 0 | internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), |
157 | 0 | aliasing, &ptr, input); |
158 | 0 | ptr = msg->_InternalParse(ptr, &ctx); |
159 | | // ctx has no explicit limit (hence we end on end of stream) |
160 | 0 | if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtEndOfStream())) { |
161 | 0 | return CheckFieldPresence(ctx, *msg, parse_flags); |
162 | 0 | } |
163 | 0 | return false; |
164 | 0 | } Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<false>(google::protobuf::io::ZeroCopyInputStream*, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<true>(google::protobuf::io::ZeroCopyInputStream*, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) |
165 | | |
166 | | template <bool aliasing> |
167 | | bool MergeFromImpl(BoundedZCIS input, MessageLite* msg, |
168 | 0 | MessageLite::ParseFlags parse_flags) { |
169 | 0 | const char* ptr; |
170 | 0 | internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(), |
171 | 0 | aliasing, &ptr, input.zcis, input.limit); |
172 | 0 | ptr = msg->_InternalParse(ptr, &ctx); |
173 | 0 | if (PROTOBUF_PREDICT_FALSE(!ptr)) return false; |
174 | 0 | ctx.BackUp(ptr); |
175 | 0 | if (PROTOBUF_PREDICT_TRUE(ctx.EndedAtLimit())) { |
176 | 0 | return CheckFieldPresence(ctx, *msg, parse_flags); |
177 | 0 | } |
178 | 0 | return false; |
179 | 0 | } Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<false>(google::protobuf::internal::BoundedZCIS, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) Unexecuted instantiation: bool google::protobuf::internal::MergeFromImpl<true>(google::protobuf::internal::BoundedZCIS, google::protobuf::MessageLite*, google::protobuf::MessageLite::ParseFlags) |
180 | | |
181 | | template bool MergeFromImpl<false>(StringPiece input, MessageLite* msg, |
182 | | MessageLite::ParseFlags parse_flags); |
183 | | template bool MergeFromImpl<true>(StringPiece input, MessageLite* msg, |
184 | | MessageLite::ParseFlags parse_flags); |
185 | | template bool MergeFromImpl<false>(io::ZeroCopyInputStream* input, |
186 | | MessageLite* msg, |
187 | | MessageLite::ParseFlags parse_flags); |
188 | | template bool MergeFromImpl<true>(io::ZeroCopyInputStream* input, |
189 | | MessageLite* msg, |
190 | | MessageLite::ParseFlags parse_flags); |
191 | | template bool MergeFromImpl<false>(BoundedZCIS input, MessageLite* msg, |
192 | | MessageLite::ParseFlags parse_flags); |
193 | | template bool MergeFromImpl<true>(BoundedZCIS input, MessageLite* msg, |
194 | | MessageLite::ParseFlags parse_flags); |
195 | | |
196 | | } // namespace internal |
197 | | |
198 | 0 | MessageLite* MessageLite::New(Arena* arena) const { |
199 | 0 | MessageLite* message = New(); |
200 | 0 | if (arena != NULL) { |
201 | 0 | arena->Own(message); |
202 | 0 | } |
203 | 0 | return message; |
204 | 0 | } |
205 | | |
206 | | class ZeroCopyCodedInputStream : public io::ZeroCopyInputStream { |
207 | | public: |
208 | 0 | ZeroCopyCodedInputStream(io::CodedInputStream* cis) : cis_(cis) {} |
209 | 0 | bool Next(const void** data, int* size) final { |
210 | 0 | if (!cis_->GetDirectBufferPointer(data, size)) return false; |
211 | 0 | cis_->Skip(*size); |
212 | 0 | return true; |
213 | 0 | } |
214 | 0 | void BackUp(int count) final { cis_->Advance(-count); } |
215 | 0 | bool Skip(int count) final { return cis_->Skip(count); } |
216 | 0 | int64_t ByteCount() const final { return 0; } |
217 | | |
218 | 0 | bool aliasing_enabled() { return cis_->aliasing_enabled_; } |
219 | | |
220 | | private: |
221 | | io::CodedInputStream* cis_; |
222 | | }; |
223 | | |
224 | | bool MessageLite::MergeFromImpl(io::CodedInputStream* input, |
225 | 0 | MessageLite::ParseFlags parse_flags) { |
226 | 0 | ZeroCopyCodedInputStream zcis(input); |
227 | 0 | const char* ptr; |
228 | 0 | internal::ParseContext ctx(input->RecursionBudget(), zcis.aliasing_enabled(), |
229 | 0 | &ptr, &zcis); |
230 | | // MergePartialFromCodedStream allows terminating the wireformat by 0 or |
231 | | // end-group tag. Leaving it up to the caller to verify correct ending by |
232 | | // calling LastTagWas on input. We need to maintain this behavior. |
233 | 0 | ctx.TrackCorrectEnding(); |
234 | 0 | ctx.data().pool = input->GetExtensionPool(); |
235 | 0 | ctx.data().factory = input->GetExtensionFactory(); |
236 | 0 | ptr = _InternalParse(ptr, &ctx); |
237 | 0 | if (PROTOBUF_PREDICT_FALSE(!ptr)) return false; |
238 | 0 | ctx.BackUp(ptr); |
239 | 0 | if (!ctx.EndedAtEndOfStream()) { |
240 | 0 | GOOGLE_DCHECK(ctx.LastTag() != 1); // We can't end on a pushed limit. |
241 | 0 | if (ctx.IsExceedingLimit(ptr)) return false; |
242 | 0 | input->SetLastTag(ctx.LastTag()); |
243 | 0 | } else { |
244 | 0 | input->SetConsumed(); |
245 | 0 | } |
246 | 0 | return CheckFieldPresence(ctx, *this, parse_flags); |
247 | 0 | } |
248 | | |
249 | 0 | bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) { |
250 | 0 | return MergeFromImpl(input, kMergePartial); |
251 | 0 | } |
252 | | |
253 | 0 | bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) { |
254 | 0 | return MergeFromImpl(input, kMerge); |
255 | 0 | } |
256 | | |
257 | 0 | bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) { |
258 | 0 | Clear(); |
259 | 0 | return MergeFromImpl(input, kParse); |
260 | 0 | } |
261 | | |
262 | 0 | bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) { |
263 | 0 | Clear(); |
264 | 0 | return MergeFromImpl(input, kParsePartial); |
265 | 0 | } |
266 | | |
267 | 0 | bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) { |
268 | 0 | return ParseFrom<kParse>(input); |
269 | 0 | } |
270 | | |
271 | | bool MessageLite::ParsePartialFromZeroCopyStream( |
272 | 0 | io::ZeroCopyInputStream* input) { |
273 | 0 | return ParseFrom<kParsePartial>(input); |
274 | 0 | } |
275 | | |
276 | 0 | bool MessageLite::ParseFromFileDescriptor(int file_descriptor) { |
277 | 0 | io::FileInputStream input(file_descriptor); |
278 | 0 | return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0; |
279 | 0 | } |
280 | | |
281 | 0 | bool MessageLite::ParsePartialFromFileDescriptor(int file_descriptor) { |
282 | 0 | io::FileInputStream input(file_descriptor); |
283 | 0 | return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0; |
284 | 0 | } |
285 | | |
286 | 0 | bool MessageLite::ParseFromIstream(std::istream* input) { |
287 | 0 | io::IstreamInputStream zero_copy_input(input); |
288 | 0 | return ParseFromZeroCopyStream(&zero_copy_input) && input->eof(); |
289 | 0 | } |
290 | | |
291 | 0 | bool MessageLite::ParsePartialFromIstream(std::istream* input) { |
292 | 0 | io::IstreamInputStream zero_copy_input(input); |
293 | 0 | return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof(); |
294 | 0 | } |
295 | | |
296 | | bool MessageLite::MergePartialFromBoundedZeroCopyStream( |
297 | 0 | io::ZeroCopyInputStream* input, int size) { |
298 | 0 | return ParseFrom<kMergePartial>(internal::BoundedZCIS{input, size}); |
299 | 0 | } |
300 | | |
301 | | bool MessageLite::MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, |
302 | 0 | int size) { |
303 | 0 | return ParseFrom<kMerge>(internal::BoundedZCIS{input, size}); |
304 | 0 | } |
305 | | |
306 | | bool MessageLite::ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input, |
307 | 0 | int size) { |
308 | 0 | return ParseFrom<kParse>(internal::BoundedZCIS{input, size}); |
309 | 0 | } |
310 | | |
311 | | bool MessageLite::ParsePartialFromBoundedZeroCopyStream( |
312 | 0 | io::ZeroCopyInputStream* input, int size) { |
313 | 0 | return ParseFrom<kParsePartial>(internal::BoundedZCIS{input, size}); |
314 | 0 | } |
315 | | |
316 | 0 | bool MessageLite::ParseFromString(ConstStringParam data) { |
317 | 0 | return ParseFrom<kParse>(data); |
318 | 0 | } |
319 | | |
320 | 0 | bool MessageLite::ParsePartialFromString(ConstStringParam data) { |
321 | 0 | return ParseFrom<kParsePartial>(data); |
322 | 0 | } |
323 | | |
324 | 0 | bool MessageLite::ParseFromArray(const void* data, int size) { |
325 | 0 | return ParseFrom<kParse>(as_string_view(data, size)); |
326 | 0 | } |
327 | | |
328 | 0 | bool MessageLite::ParsePartialFromArray(const void* data, int size) { |
329 | 0 | return ParseFrom<kParsePartial>(as_string_view(data, size)); |
330 | 0 | } |
331 | | |
332 | 0 | bool MessageLite::MergeFromString(ConstStringParam data) { |
333 | 0 | return ParseFrom<kMerge>(data); |
334 | 0 | } |
335 | | |
336 | | |
337 | | // =================================================================== |
338 | | |
339 | | inline uint8* SerializeToArrayImpl(const MessageLite& msg, uint8* target, |
340 | 128 | int size) { |
341 | 128 | constexpr bool debug = false; |
342 | 128 | if (debug) { |
343 | | // Force serialization to a stream with a block size of 1, which forces |
344 | | // all writes to the stream to cross buffers triggering all fallback paths |
345 | | // in the unittests when serializing to string / array. |
346 | 0 | io::ArrayOutputStream stream(target, size, 1); |
347 | 0 | uint8* ptr; |
348 | 0 | io::EpsCopyOutputStream out( |
349 | 0 | &stream, io::CodedOutputStream::IsDefaultSerializationDeterministic(), |
350 | 0 | &ptr); |
351 | 0 | ptr = msg._InternalSerialize(ptr, &out); |
352 | 0 | out.Trim(ptr); |
353 | 0 | GOOGLE_DCHECK(!out.HadError() && stream.ByteCount() == size); |
354 | 0 | return target + size; |
355 | 128 | } else { |
356 | 128 | io::EpsCopyOutputStream out( |
357 | 128 | target, size, |
358 | 128 | io::CodedOutputStream::IsDefaultSerializationDeterministic()); |
359 | 128 | auto res = msg._InternalSerialize(target, &out); |
360 | 128 | GOOGLE_DCHECK(target + size == res); |
361 | 128 | return res; |
362 | 128 | } |
363 | 128 | } |
364 | | |
365 | 0 | uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const { |
366 | | // We only optimize this when using optimize_for = SPEED. In other cases |
367 | | // we just use the CodedOutputStream path. |
368 | 0 | return SerializeToArrayImpl(*this, target, GetCachedSize()); |
369 | 0 | } |
370 | | |
371 | 0 | bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const { |
372 | 0 | GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); |
373 | 0 | return SerializePartialToCodedStream(output); |
374 | 0 | } |
375 | | |
376 | | bool MessageLite::SerializePartialToCodedStream( |
377 | 0 | io::CodedOutputStream* output) const { |
378 | 0 | const size_t size = ByteSizeLong(); // Force size to be cached. |
379 | 0 | if (size > INT_MAX) { |
380 | 0 | GOOGLE_LOG(ERROR) << GetTypeName() |
381 | 0 | << " exceeded maximum protobuf size of 2GB: " << size; |
382 | 0 | return false; |
383 | 0 | } |
384 | | |
385 | 0 | int original_byte_count = output->ByteCount(); |
386 | 0 | SerializeWithCachedSizes(output); |
387 | 0 | if (output->HadError()) { |
388 | 0 | return false; |
389 | 0 | } |
390 | 0 | int final_byte_count = output->ByteCount(); |
391 | |
|
392 | 0 | if (final_byte_count - original_byte_count != size) { |
393 | 0 | ByteSizeConsistencyError(size, ByteSizeLong(), |
394 | 0 | final_byte_count - original_byte_count, *this); |
395 | 0 | } |
396 | |
|
397 | 0 | return true; |
398 | 0 | } |
399 | | |
400 | | bool MessageLite::SerializeToZeroCopyStream( |
401 | 0 | io::ZeroCopyOutputStream* output) const { |
402 | 0 | GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); |
403 | 0 | return SerializePartialToZeroCopyStream(output); |
404 | 0 | } |
405 | | |
406 | | bool MessageLite::SerializePartialToZeroCopyStream( |
407 | 0 | io::ZeroCopyOutputStream* output) const { |
408 | 0 | const size_t size = ByteSizeLong(); // Force size to be cached. |
409 | 0 | if (size > INT_MAX) { |
410 | 0 | GOOGLE_LOG(ERROR) << GetTypeName() |
411 | 0 | << " exceeded maximum protobuf size of 2GB: " << size; |
412 | 0 | return false; |
413 | 0 | } |
414 | | |
415 | 0 | uint8* target; |
416 | 0 | io::EpsCopyOutputStream stream( |
417 | 0 | output, io::CodedOutputStream::IsDefaultSerializationDeterministic(), |
418 | 0 | &target); |
419 | 0 | target = _InternalSerialize(target, &stream); |
420 | 0 | stream.Trim(target); |
421 | 0 | if (stream.HadError()) return false; |
422 | 0 | return true; |
423 | 0 | } |
424 | | |
425 | 0 | bool MessageLite::SerializeToFileDescriptor(int file_descriptor) const { |
426 | 0 | io::FileOutputStream output(file_descriptor); |
427 | 0 | return SerializeToZeroCopyStream(&output) && output.Flush(); |
428 | 0 | } |
429 | | |
430 | 0 | bool MessageLite::SerializePartialToFileDescriptor(int file_descriptor) const { |
431 | 0 | io::FileOutputStream output(file_descriptor); |
432 | 0 | return SerializePartialToZeroCopyStream(&output) && output.Flush(); |
433 | 0 | } |
434 | | |
435 | 0 | bool MessageLite::SerializeToOstream(std::ostream* output) const { |
436 | 0 | { |
437 | 0 | io::OstreamOutputStream zero_copy_output(output); |
438 | 0 | if (!SerializeToZeroCopyStream(&zero_copy_output)) return false; |
439 | 0 | } |
440 | 0 | return output->good(); |
441 | 0 | } |
442 | | |
443 | 0 | bool MessageLite::SerializePartialToOstream(std::ostream* output) const { |
444 | 0 | io::OstreamOutputStream zero_copy_output(output); |
445 | 0 | return SerializePartialToZeroCopyStream(&zero_copy_output); |
446 | 0 | } |
447 | | |
448 | 128 | bool MessageLite::AppendToString(std::string* output) const { |
449 | 128 | GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); |
450 | 128 | return AppendPartialToString(output); |
451 | 128 | } |
452 | | |
453 | 128 | bool MessageLite::AppendPartialToString(std::string* output) const { |
454 | 128 | size_t old_size = output->size(); |
455 | 128 | size_t byte_size = ByteSizeLong(); |
456 | 128 | if (byte_size > INT_MAX) { |
457 | 0 | GOOGLE_LOG(ERROR) << GetTypeName() |
458 | 0 | << " exceeded maximum protobuf size of 2GB: " << byte_size; |
459 | 0 | return false; |
460 | 0 | } |
461 | | |
462 | 128 | STLStringResizeUninitialized(output, old_size + byte_size); |
463 | 128 | uint8* start = |
464 | 128 | reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size); |
465 | 128 | SerializeToArrayImpl(*this, start, byte_size); |
466 | 128 | return true; |
467 | 128 | } |
468 | | |
469 | 0 | bool MessageLite::SerializeToString(std::string* output) const { |
470 | 0 | output->clear(); |
471 | 0 | return AppendToString(output); |
472 | 0 | } |
473 | | |
474 | 0 | bool MessageLite::SerializePartialToString(std::string* output) const { |
475 | 0 | output->clear(); |
476 | 0 | return AppendPartialToString(output); |
477 | 0 | } |
478 | | |
479 | 0 | bool MessageLite::SerializeToArray(void* data, int size) const { |
480 | 0 | GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); |
481 | 0 | return SerializePartialToArray(data, size); |
482 | 0 | } |
483 | | |
484 | 0 | bool MessageLite::SerializePartialToArray(void* data, int size) const { |
485 | 0 | const size_t byte_size = ByteSizeLong(); |
486 | 0 | if (byte_size > INT_MAX) { |
487 | 0 | GOOGLE_LOG(ERROR) << GetTypeName() |
488 | 0 | << " exceeded maximum protobuf size of 2GB: " << byte_size; |
489 | 0 | return false; |
490 | 0 | } |
491 | 0 | if (size < byte_size) return false; |
492 | 0 | uint8* start = reinterpret_cast<uint8*>(data); |
493 | 0 | SerializeToArrayImpl(*this, start, byte_size); |
494 | 0 | return true; |
495 | 0 | } |
496 | | |
497 | 128 | std::string MessageLite::SerializeAsString() const { |
498 | | // If the compiler implements the (Named) Return Value Optimization, |
499 | | // the local variable 'output' will not actually reside on the stack |
500 | | // of this function, but will be overlaid with the object that the |
501 | | // caller supplied for the return value to be constructed in. |
502 | 128 | std::string output; |
503 | 128 | if (!AppendToString(&output)) output.clear(); |
504 | 128 | return output; |
505 | 128 | } |
506 | | |
507 | 0 | std::string MessageLite::SerializePartialAsString() const { |
508 | 0 | std::string output; |
509 | 0 | if (!AppendPartialToString(&output)) output.clear(); |
510 | 0 | return output; |
511 | 0 | } |
512 | | |
513 | | |
514 | | namespace internal { |
515 | | |
516 | | template <> |
517 | | MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype( |
518 | 0 | const MessageLite* prototype, Arena* arena) { |
519 | 0 | return prototype->New(arena); |
520 | 0 | } |
521 | | template <> |
522 | | void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from, |
523 | 0 | MessageLite* to) { |
524 | 0 | to->CheckTypeAndMergeFrom(from); |
525 | 0 | } |
526 | | template <> |
527 | | void GenericTypeHandler<std::string>::Merge(const std::string& from, |
528 | 0 | std::string* to) { |
529 | 0 | *to = from; |
530 | 0 | } |
531 | | |
532 | | } // namespace internal |
533 | | |
534 | | |
535 | | // =================================================================== |
536 | | // Shutdown support. |
537 | | |
538 | | namespace internal { |
539 | | |
540 | | struct ShutdownData { |
541 | 0 | ~ShutdownData() { |
542 | 0 | std::reverse(functions.begin(), functions.end()); |
543 | 0 | for (auto pair : functions) pair.first(pair.second); |
544 | 0 | } |
545 | | |
546 | 3 | static ShutdownData* get() { |
547 | 3 | static auto* data = new ShutdownData; |
548 | 3 | return data; |
549 | 3 | } |
550 | | |
551 | | std::vector<std::pair<void (*)(const void*), const void*>> functions; |
552 | | Mutex mutex; |
553 | | }; |
554 | | |
555 | 0 | static void RunZeroArgFunc(const void* arg) { |
556 | 0 | void (*func)() = reinterpret_cast<void (*)()>(const_cast<void*>(arg)); |
557 | 0 | func(); |
558 | 0 | } |
559 | | |
560 | 0 | void OnShutdown(void (*func)()) { |
561 | 0 | OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func)); |
562 | 0 | } |
563 | | |
564 | 3 | void OnShutdownRun(void (*f)(const void*), const void* arg) { |
565 | 3 | auto shutdown_data = ShutdownData::get(); |
566 | 3 | MutexLock lock(&shutdown_data->mutex); |
567 | 3 | shutdown_data->functions.push_back(std::make_pair(f, arg)); |
568 | 3 | } |
569 | | |
570 | | } // namespace internal |
571 | | |
572 | 0 | void ShutdownProtobufLibrary() { |
573 | | // This function should be called only once, but accepts multiple calls. |
574 | 0 | static bool is_shutdown = false; |
575 | 0 | if (!is_shutdown) { |
576 | 0 | delete internal::ShutdownData::get(); |
577 | 0 | is_shutdown = true; |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | | |
582 | | } // namespace protobuf |
583 | | } // namespace google |