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
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 != NULL) *clean_eof = false;
68 int start = input->CurrentPosition();
69
70 // Read the size.
71 uint32 size;
72 if (!input->ReadVarint32(&size)) {
73 if (clean_eof != NULL) *clean_eof = input->CurrentPosition() == start;
74 return false;
75 }
76
77 // Tell the stream not to read beyond that size.
78 io::CodedInputStream::Limit limit = input->PushLimit(size);
79
80 // Parse the message.
81 if (!message->MergeFromCodedStream(input)) return false;
82 if (!input->ConsumedEntireMessage()) return false;
83
84 // Release the limit.
85 input->PopLimit(limit);
86
87 return true;
88 }
89
SerializeDelimitedToZeroCopyStream(const MessageLite & message,io::ZeroCopyOutputStream * output)90 bool SerializeDelimitedToZeroCopyStream(const MessageLite& message,
91 io::ZeroCopyOutputStream* output) {
92 io::CodedOutputStream coded_output(output);
93 return SerializeDelimitedToCodedStream(message, &coded_output);
94 }
95
SerializeDelimitedToCodedStream(const MessageLite & message,io::CodedOutputStream * output)96 bool SerializeDelimitedToCodedStream(const MessageLite& message,
97 io::CodedOutputStream* output) {
98 // Write the size.
99 size_t size = message.ByteSizeLong();
100 if (size > INT_MAX) return false;
101
102 output->WriteVarint32(size);
103
104 // Write the content.
105 uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
106 if (buffer != NULL) {
107 // Optimization: The message fits in one buffer, so use the faster
108 // direct-to-array serialization path.
109 message.SerializeWithCachedSizesToArray(buffer);
110 } else {
111 // Slightly-slower path when the message is multiple buffers.
112 message.SerializeWithCachedSizes(output);
113 if (output->HadError()) return false;
114 }
115
116 return true;
117 }
118
119 } // namespace util
120 } // namespace protobuf
121 } // namespace google
122