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#ifndef GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
35#define GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
36
37
38#include <ostream>
39
40#include <google/protobuf/message_lite.h>
41#include <google/protobuf/io/coded_stream.h>
42#include <google/protobuf/io/zero_copy_stream_impl.h>
43
44// Must be included last.
45#include <google/protobuf/port_def.inc>
46
47namespace google {
48namespace protobuf {
49namespace util {
50
51// Write a single size-delimited message from the given stream. Delimited
52// format allows a single file or stream to contain multiple messages,
53// whereas normally writing multiple non-delimited messages to the same
54// stream would cause them to be merged. A delimited message is a varint
55// encoding the message size followed by a message of exactly that size.
56//
57// Note that if you want to *read* a delimited message from a file descriptor
58// or istream, you will need to construct an io::FileInputStream or
59// io::OstreamInputStream (implementations of io::ZeroCopyStream) and use the
60// utility function ParseDelimitedFromZeroCopyStream(). You must then
61// continue to use the same ZeroCopyInputStream to read all further data from
62// the stream until EOF. This is because these ZeroCopyInputStream
63// implementations are buffered: they read a big chunk of data at a time,
64// then parse it. As a result, they may read past the end of the delimited
65// message. There is no way for them to push the extra data back into the
66// underlying source, so instead you must keep using the same stream object.
67bool PROTOBUF_EXPORT SerializeDelimitedToFileDescriptor(
68 const MessageLite& message, int file_descriptor);
69
70bool PROTOBUF_EXPORT SerializeDelimitedToOstream(const MessageLite& message,
71 std::ostream* output);
72
73// Read a single size-delimited message from the given stream. Delimited
74// format allows a single file or stream to contain multiple messages,
75// whereas normally parsing consumes the entire input. A delimited message
76// is a varint encoding the message size followed by a message of exactly
77// that size.
78//
79// If |clean_eof| is not NULL, then it will be set to indicate whether the
80// stream ended cleanly. That is, if the stream ends without this method
81// having read any data at all from it, then *clean_eof will be set true,
82// otherwise it will be set false. Note that these methods return false
83// on EOF, but they also return false on other errors, so |clean_eof| is
84// needed to distinguish a clean end from errors.
85bool PROTOBUF_EXPORT ParseDelimitedFromZeroCopyStream(
86 MessageLite* message, io::ZeroCopyInputStream* input, bool* clean_eof);
87
88bool PROTOBUF_EXPORT ParseDelimitedFromCodedStream(MessageLite* message,
89 io::CodedInputStream* input,
90 bool* clean_eof);
91
92// Write a single size-delimited message from the given stream. Delimited
93// format allows a single file or stream to contain multiple messages,
94// whereas normally writing multiple non-delimited messages to the same
95// stream would cause them to be merged. A delimited message is a varint
96// encoding the message size followed by a message of exactly that size.
97bool PROTOBUF_EXPORT SerializeDelimitedToZeroCopyStream(
98 const MessageLite& message, io::ZeroCopyOutputStream* output);
99
100bool PROTOBUF_EXPORT SerializeDelimitedToCodedStream(
101 const MessageLite& message, io::CodedOutputStream* output);
102
103} // namespace util
104} // namespace protobuf
105} // namespace google
106
107#include <google/protobuf/port_undef.inc>
108
109#endif // GOOGLE_PROTOBUF_UTIL_DELIMITED_MESSAGE_UTIL_H__
110