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 
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  // 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 
92  io::CodedOutputStream coded_output(output);
93  return SerializeDelimitedToCodedStream(message, &coded_output);
94 }
95 
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
NULL
NULL
Definition: test_security_zap.cpp:405
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::uint8
uint8_t uint8
Definition: protobuf/src/google/protobuf/stubs/port.h:153
google::protobuf::util::SerializeDelimitedToFileDescriptor
bool SerializeDelimitedToFileDescriptor(const MessageLite &message, int file_descriptor)
Definition: delimited_message_util.cc:41
google::protobuf::util::ParseDelimitedFromCodedStream
bool ParseDelimitedFromCodedStream(MessageLite *message, io::CodedInputStream *input, bool *clean_eof)
Definition: delimited_message_util.cc:64
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::MessageLite
Definition: message_lite.h:183
coded_stream.h
start
GLuint start
Definition: glcorearb.h:2858
google::protobuf::io::OstreamOutputStream
Definition: zero_copy_stream_impl.h:258
delimited_message_util.h
size
#define size
Definition: glcorearb.h:2944
buffer
Definition: buffer_processor.h:43
google::protobuf::io::CodedOutputStream
Definition: coded_stream.h:693
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::io::FileOutputStream
Definition: zero_copy_stream_impl.h:141
google::protobuf::util::SerializeDelimitedToCodedStream
bool SerializeDelimitedToCodedStream(const MessageLite &message, io::CodedOutputStream *output)
Definition: delimited_message_util.cc:96
google::protobuf::util::SerializeDelimitedToZeroCopyStream
bool SerializeDelimitedToZeroCopyStream(const MessageLite &message, io::ZeroCopyOutputStream *output)
Definition: delimited_message_util.cc:90
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
google::protobuf::io::CodedInputStream::Limit
int Limit
Definition: coded_stream.h:350
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::util::SerializeDelimitedToOstream
bool SerializeDelimitedToOstream(const MessageLite &message, std::ostream *output)
Definition: delimited_message_util.cc:47
google
Definition: data_proto2_to_proto3_util.h:11
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
google::protobuf::util::ParseDelimitedFromZeroCopyStream
bool ParseDelimitedFromZeroCopyStream(MessageLite *message, io::ZeroCopyInputStream *input, bool *clean_eof)
Definition: delimited_message_util.cc:57


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49