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 [email protected] (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
37 namespace google {
38 namespace protobuf {
39 namespace util {
40
SerializeDelimitedToFileDescriptor(const MessageLite & message,int file_descriptor)41 bool SerializeDelimitedToFileDescriptor(const MessageLite& message,
42 int file_descriptor) {
43 io::FileOutputStream output(file_descriptor);
44 return SerializeDelimitedToZeroCopyStream(message, &output);
45 }
46
SerializeDelimitedToOstream(const MessageLite & message,std::ostream * output)47 bool SerializeDelimitedToOstream(const MessageLite& message,
48 std::ostream* output) {
49 {
50 io::OstreamOutputStream zero_copy_output(output);
51 if (!SerializeDelimitedToZeroCopyStream(message, &zero_copy_output))
52 return false;
53 }
54 return output->good();
55 }
56
ParseDelimitedFromZeroCopyStream(MessageLite * message,io::ZeroCopyInputStream * input,bool * clean_eof)57 bool ParseDelimitedFromZeroCopyStream(MessageLite* message,
58 io::ZeroCopyInputStream* input,
59 bool* clean_eof) {
60 io::CodedInputStream coded_input(input);
61 return ParseDelimitedFromCodedStream(message, &coded_input, clean_eof);
62 }
63
ParseDelimitedFromCodedStream(MessageLite * message,io::CodedInputStream * input,bool * clean_eof)64 bool 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(&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(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
SerializeDelimitedToZeroCopyStream(const MessageLite & message,io::ZeroCopyOutputStream * output)96 bool SerializeDelimitedToZeroCopyStream(const MessageLite& message,
97 io::ZeroCopyOutputStream* output) {
98 io::CodedOutputStream coded_output(output);
99 return SerializeDelimitedToCodedStream(message, &coded_output);
100 }
101
SerializeDelimitedToCodedStream(const MessageLite & message,io::CodedOutputStream * output)102 bool 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(static_cast<uint32_t>(size));
109
110 // Write the content.
111 uint8_t* buffer =
112 output->GetDirectBufferForNBytesAndAdvance(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(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