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// Adapted from the patch of kenton@google.com (Kenton Varda)
32// See https://github.com/protocolbuffers/protobuf/pull/710 for details.
33
34#include <google/protobuf/util/delimited_message_util.h>
35#include <google/protobuf/io/coded_stream.h>
36
37namespace google {
38namespace protobuf {
39namespace util {
40
41bool SerializeDelimitedToFileDescriptor(const MessageLite& message,
42 int file_descriptor) {
43 io::FileOutputStream output(file_descriptor);
44 return SerializeDelimitedToZeroCopyStream(message, output: &output);
45}
46
47bool SerializeDelimitedToOstream(const MessageLite& message,
48 std::ostream* output) {
49 {
50 io::OstreamOutputStream zero_copy_output(output);
51 if (!SerializeDelimitedToZeroCopyStream(message, output: &zero_copy_output))
52 return false;
53 }
54 return output->good();
55}
56
57bool ParseDelimitedFromZeroCopyStream(MessageLite* message,
58 io::ZeroCopyInputStream* input,
59 bool* clean_eof) {
60 io::CodedInputStream coded_input(input);
61 return ParseDelimitedFromCodedStream(message, input: &coded_input, clean_eof);
62}
63
64bool ParseDelimitedFromCodedStream(MessageLite* message,
65 io::CodedInputStream* input,
66 bool* clean_eof) {
67 if (clean_eof != nullptr) *clean_eof = false;
68 int start = input->CurrentPosition();
69
70 // Read the size.
71 uint32_t size;
72 if (!input->ReadVarint32(value: &size)) {
73 if (clean_eof != nullptr) *clean_eof = input->CurrentPosition() == start;
74 return false;
75 }
76
77 // Get the position after any size bytes have been read (and only the message
78 // itself remains).
79 int position_after_size = input->CurrentPosition();
80
81 // Tell the stream not to read beyond that size.
82 io::CodedInputStream::Limit limit = input->PushLimit(byte_limit: static_cast<int>(size));
83
84 // Parse the message.
85 if (!message->MergeFromCodedStream(input)) return false;
86 if (!input->ConsumedEntireMessage()) return false;
87 if (input->CurrentPosition() - position_after_size != static_cast<int>(size))
88 return false;
89
90 // Release the limit.
91 input->PopLimit(limit);
92
93 return true;
94}
95
96bool SerializeDelimitedToZeroCopyStream(const MessageLite& message,
97 io::ZeroCopyOutputStream* output) {
98 io::CodedOutputStream coded_output(output);
99 return SerializeDelimitedToCodedStream(message, output: &coded_output);
100}
101
102bool SerializeDelimitedToCodedStream(const MessageLite& message,
103 io::CodedOutputStream* output) {
104 // Write the size.
105 size_t size = message.ByteSizeLong();
106 if (size > INT_MAX) return false;
107
108 output->WriteVarint32(value: static_cast<uint32_t>(size));
109
110 // Write the content.
111 uint8_t* buffer =
112 output->GetDirectBufferForNBytesAndAdvance(size: static_cast<int>(size));
113 if (buffer != nullptr) {
114 // Optimization: The message fits in one buffer, so use the faster
115 // direct-to-array serialization path.
116 message.SerializeWithCachedSizesToArray(target: buffer);
117 } else {
118 // Slightly-slower path when the message is multiple buffers.
119 message.SerializeWithCachedSizes(output);
120 if (output->HadError()) return false;
121 }
122
123 return true;
124}
125
126} // namespace util
127} // namespace protobuf
128} // namespace google
129