blob: e631e56f29c419e7711ceada95f24783235c8618 [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// 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#ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTO_WRITER_H__
32#define GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTO_WRITER_H__
33
34#include <deque>
35#include <google/protobuf/stubs/hash.h>
36#include <string>
37
38#include <google/protobuf/stubs/common.h>
39#include <google/protobuf/io/coded_stream.h>
40#include <google/protobuf/io/zero_copy_stream_impl.h>
41#include <google/protobuf/descriptor.h>
42#include <google/protobuf/util/internal/type_info.h>
43#include <google/protobuf/util/internal/datapiece.h>
44#include <google/protobuf/util/internal/error_listener.h>
45#include <google/protobuf/util/internal/structured_objectwriter.h>
46#include <google/protobuf/util/type_resolver.h>
47#include <google/protobuf/stubs/bytestream.h>
48
49namespace google {
50namespace protobuf {
51namespace io {
52class CodedOutputStream;
53} // namespace io
54} // namespace protobuf
55
56
57namespace protobuf {
58class Type;
59class Field;
60} // namespace protobuf
61
62
63namespace protobuf {
64namespace util {
65namespace converter {
66
67class ObjectLocationTracker;
68
69// An ObjectWriter that can write protobuf bytes directly from writer events.
70// This class does not support special types like Struct or Map. However, since
71// this class supports raw protobuf, it can be used to provide support for
72// special types by inheriting from it or by wrapping it.
73//
74// It also supports streaming.
75class LIBPROTOBUF_EXPORT ProtoWriter : public StructuredObjectWriter {
76 public:
77// Constructor. Does not take ownership of any parameter passed in.
78 ProtoWriter(TypeResolver* type_resolver, const google::protobuf::Type& type,
79 strings::ByteSink* output, ErrorListener* listener);
80 virtual ~ProtoWriter();
81
82 // ObjectWriter methods.
83 virtual ProtoWriter* StartObject(StringPiece name);
84 virtual ProtoWriter* EndObject();
85 virtual ProtoWriter* StartList(StringPiece name);
86 virtual ProtoWriter* EndList();
87 virtual ProtoWriter* RenderBool(StringPiece name, bool value) {
88 return RenderDataPiece(name, DataPiece(value));
89 }
90 virtual ProtoWriter* RenderInt32(StringPiece name, int32 value) {
91 return RenderDataPiece(name, DataPiece(value));
92 }
93 virtual ProtoWriter* RenderUint32(StringPiece name, uint32 value) {
94 return RenderDataPiece(name, DataPiece(value));
95 }
96 virtual ProtoWriter* RenderInt64(StringPiece name, int64 value) {
97 return RenderDataPiece(name, DataPiece(value));
98 }
99 virtual ProtoWriter* RenderUint64(StringPiece name, uint64 value) {
100 return RenderDataPiece(name, DataPiece(value));
101 }
102 virtual ProtoWriter* RenderDouble(StringPiece name, double value) {
103 return RenderDataPiece(name, DataPiece(value));
104 }
105 virtual ProtoWriter* RenderFloat(StringPiece name, float value) {
106 return RenderDataPiece(name, DataPiece(value));
107 }
108 virtual ProtoWriter* RenderString(StringPiece name, StringPiece value) {
109 return RenderDataPiece(name, DataPiece(value));
110 }
111 virtual ProtoWriter* RenderBytes(StringPiece name, StringPiece value) {
112 return RenderDataPiece(name, DataPiece(value, false));
113 }
114 virtual ProtoWriter* RenderNull(StringPiece name) {
115 return RenderDataPiece(name, DataPiece::NullData());
116 }
117
118 // Renders a DataPiece 'value' into a field whose wire type is determined
119 // from the given field 'name'.
120 virtual ProtoWriter* RenderDataPiece(StringPiece name,
121 const DataPiece& value);
122
123 // Returns the location tracker to use for tracking locations for errors.
124 const LocationTrackerInterface& location() {
125 return element_ != NULL ? *element_ : *tracker_;
126 }
127
128 // When true, we finished writing to output a complete message.
129 bool done() const { return done_; }
130
131 // Returns the proto stream object.
132 google::protobuf::io::CodedOutputStream* stream() { return stream_.get(); }
133
134 // Getters and mutators of invalid_depth_.
135 void IncrementInvalidDepth() { ++invalid_depth_; }
136 void DecrementInvalidDepth() { --invalid_depth_; }
137 int invalid_depth() { return invalid_depth_; }
138
139 ErrorListener* listener() { return listener_; }
140
141 const TypeInfo* typeinfo() { return typeinfo_; }
142
143 protected:
144 class LIBPROTOBUF_EXPORT ProtoElement : public BaseElement, public LocationTrackerInterface {
145 public:
146 // Constructor for the root element. No parent nor field.
147 ProtoElement(const TypeInfo* typeinfo, const google::protobuf::Type& type,
148 ProtoWriter* enclosing);
149
150 // Constructor for a field of an element.
151 ProtoElement(ProtoElement* parent, const google::protobuf::Field* field,
152 const google::protobuf::Type& type, bool is_list);
153
154 virtual ~ProtoElement() {}
155
156 // Called just before the destructor for clean up:
157 // - reports any missing required fields
158 // - computes the space needed by the size field, and augment the
159 // length of all parent messages by this additional space.
160 // - releases and returns the parent pointer.
161 ProtoElement* pop();
162
163 // Accessors
164 // parent_field() may be NULL if we are at root.
165 const google::protobuf::Field* parent_field() const {
166 return parent_field_;
167 }
168 const google::protobuf::Type& type() const { return type_; }
169
170 // Registers field for accounting required fields.
171 void RegisterField(const google::protobuf::Field* field);
172
173 // To report location on error messages.
174 virtual string ToString() const;
175
176 virtual ProtoElement* parent() const {
177 return static_cast<ProtoElement*>(BaseElement::parent());
178 }
179
180 // Returns true if the index is already taken by a preceeding oneof input.
181 bool IsOneofIndexTaken(int32 index);
182
183 // Marks the oneof 'index' as taken. Future inputs to this oneof will
184 // generate an error.
185 void TakeOneofIndex(int32 index);
186
187 private:
188 // Used for access to variables of the enclosing instance of
189 // ProtoWriter.
190 ProtoWriter* ow_;
191
192 // Describes the element as a field in the parent message.
193 // parent_field_ is NULL if and only if this element is the root element.
194 const google::protobuf::Field* parent_field_;
195
196 // TypeInfo to lookup types.
197 const TypeInfo* typeinfo_;
198
199 // Additional variables if this element is a message:
200 // (Root element is always a message).
201 // type_ : the type of this element.
202 // required_fields_ : set of required fields.
203 // size_index_ : index into ProtoWriter::size_insert_
204 // for later insertion of serialized message length.
205 const google::protobuf::Type& type_;
206 std::set<const google::protobuf::Field*> required_fields_;
207 const int size_index_;
208
209 // Tracks position in repeated fields, needed for LocationTrackerInterface.
210 int array_index_;
211
212 // Set of oneof indices already seen for the type_. Used to validate
213 // incoming messages so no more than one oneof is set.
214 hash_set<int32> oneof_indices_;
215
216 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoElement);
217 };
218
219 // Container for inserting 'size' information at the 'pos' position.
220 struct SizeInfo {
221 const int pos;
222 int size;
223 };
224
225 ProtoWriter(const TypeInfo* typeinfo, const google::protobuf::Type& type,
226 strings::ByteSink* output, ErrorListener* listener);
227
228 virtual ProtoElement* element() { return element_.get(); }
229
230 // Helper methods for calling ErrorListener. See error_listener.h.
231 void InvalidName(StringPiece unknown_name, StringPiece message);
232 void InvalidValue(StringPiece type_name, StringPiece value);
233 void MissingField(StringPiece missing_name);
234
235 // Common code for BeginObject() and BeginList() that does invalid_depth_
236 // bookkeeping associated with name lookup.
237 const google::protobuf::Field* BeginNamed(StringPiece name, bool is_list);
238
239 // Lookup the field in the current element. Looks in the base descriptor
240 // and in any extension. This will report an error if the field cannot be
241 // found or if multiple matching extensions are found.
242 const google::protobuf::Field* Lookup(StringPiece name);
243
244 // Lookup the field type in the type descriptor. Returns NULL if the type
245 // is not known.
246 const google::protobuf::Type* LookupType(
247 const google::protobuf::Field* field);
248
249 // Write serialized output to the final output ByteSink, inserting all
250 // the size information for nested messages that are missing from the
251 // intermediate Cord buffer.
252 void WriteRootMessage();
253
254 // Helper method to write proto tags based on the given field.
255 void WriteTag(const google::protobuf::Field& field);
256
257
258 // Returns true if the field for type_ can be set as a oneof. If field is not
259 // a oneof type, this function does nothing and returns true.
260 // If another field for this oneof is already set, this function returns
261 // false. It also calls the appropriate error callback.
262 // unnormalized_name is used for error string.
263 bool ValidOneof(const google::protobuf::Field& field,
264 StringPiece unnormalized_name);
265
266 // Returns true if the field is repeated.
267 bool IsRepeated(const google::protobuf::Field& field);
268
269 private:
270 // Variables for describing the structure of the input tree:
271 // master_type_: descriptor for the whole protobuf message.
272 // typeinfo_ : the TypeInfo object to lookup types.
273 const google::protobuf::Type& master_type_;
274 const TypeInfo* typeinfo_;
275 // Whether we own the typeinfo_ object.
276 bool own_typeinfo_;
277
278 // Indicates whether we finished writing root message completely.
279 bool done_;
280
281 // Variable for internal state processing:
282 // element_ : the current element.
283 // size_insert_: sizes of nested messages.
284 // pos - position to insert the size field.
285 // size - size value to be inserted.
286 google::protobuf::scoped_ptr<ProtoElement> element_;
287 std::deque<SizeInfo> size_insert_;
288
289 // Variables for output generation:
290 // output_ : pointer to an external ByteSink for final user-visible output.
291 // buffer_ : buffer holding partial message before being ready for output_.
292 // adapter_ : internal adapter between CodedOutputStream and buffer_.
293 // stream_ : wrapper for writing tags and other encodings in wire format.
294 strings::ByteSink* output_;
295 string buffer_;
296 google::protobuf::io::StringOutputStream adapter_;
297 google::protobuf::scoped_ptr<google::protobuf::io::CodedOutputStream> stream_;
298
299 // Variables for error tracking and reporting:
300 // listener_ : a place to report any errors found.
301 // invalid_depth_: number of enclosing invalid nested messages.
302 // tracker_ : the root location tracker interface.
303 ErrorListener* listener_;
304 int invalid_depth_;
305 google::protobuf::scoped_ptr<LocationTrackerInterface> tracker_;
306
307 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoWriter);
308};
309
310} // namespace converter
311} // namespace util
312} // namespace protobuf
313
314} // namespace google
315#endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTO_WRITER_H__