protobuf/src/google/protobuf/util/delimited_message_util.cc
Go to the documentation of this file.
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 
42  int file_descriptor) {
43  io::FileOutputStream output(file_descriptor);
45 }
46 
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 
59  bool* clean_eof) {
60  io::CodedInputStream coded_input(input);
61  return ParseDelimitedFromCodedStream(message, &coded_input, clean_eof);
62 }
63 
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  // 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(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 
98  io::CodedOutputStream coded_output(output);
99  return SerializeDelimitedToCodedStream(message, &coded_output);
100 }
101 
104  // Write the size.
105  size_t size = message.ByteSizeLong();
106  if (size > INT_MAX) return false;
107 
108  output->WriteVarint32(size);
109 
110  // Write the content.
111  uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
112  if (buffer != NULL) {
113  // Optimization: The message fits in one buffer, so use the faster
114  // direct-to-array serialization path.
115  message.SerializeWithCachedSizesToArray(buffer);
116  } else {
117  // Slightly-slower path when the message is multiple buffers.
118  message.SerializeWithCachedSizes(output);
119  if (output->HadError()) return false;
120  }
121 
122  return true;
123 }
124 
125 } // namespace util
126 } // namespace protobuf
127 } // namespace google
grpc::protobuf::io::ZeroCopyInputStream
GRPC_CUSTOM_ZEROCOPYINPUTSTREAM ZeroCopyInputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:101
google::protobuf::uint8
uint8_t uint8
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::util::SerializeDelimitedToFileDescriptor
bool SerializeDelimitedToFileDescriptor(const MessageLite &message, int file_descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:41
google::protobuf::util::ParseDelimitedFromCodedStream
bool ParseDelimitedFromCodedStream(MessageLite *message, io::CodedInputStream *input, bool *clean_eof)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:64
google::protobuf::uint32
uint32_t uint32
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
start
static uint64_t start
Definition: benchmark-pound.c:74
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc::protobuf::MessageLite
GRPC_CUSTOM_MESSAGELITE MessageLite
Definition: include/grpcpp/impl/codegen/config_protobuf.h:79
grpc::protobuf::io::CodedInputStream
GRPC_CUSTOM_CODEDINPUTSTREAM CodedInputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:102
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
google::protobuf::util::SerializeDelimitedToCodedStream
bool SerializeDelimitedToCodedStream(const MessageLite &message, io::CodedOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:96
google::protobuf::util::SerializeDelimitedToZeroCopyStream
bool SerializeDelimitedToZeroCopyStream(const MessageLite &message, io::ZeroCopyOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:90
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::io::CodedInputStream::Limit
int Limit
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream.h:348
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google::protobuf::util::SerializeDelimitedToOstream
bool SerializeDelimitedToOstream(const MessageLite &message, std::ostream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:47
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::util::ParseDelimitedFromZeroCopyStream
bool ParseDelimitedFromZeroCopyStream(MessageLite *message, io::ZeroCopyInputStream *input, bool *clean_eof)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util.cc:57


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:09