• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
33 
34 #include <functional>
35 #include <google/protobuf/stubs/hash.h>
36 #include <string>
37 
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/type.pb.h>
40 #include <google/protobuf/util/internal/object_source.h>
41 #include <google/protobuf/util/internal/object_writer.h>
42 #include <google/protobuf/util/internal/type_info.h>
43 #include <google/protobuf/util/type_resolver.h>
44 #include <google/protobuf/stubs/stringpiece.h>
45 #include <google/protobuf/stubs/status.h>
46 #include <google/protobuf/stubs/statusor.h>
47 
48 
49 namespace google {
50 namespace protobuf {
51 class Field;
52 class Type;
53 }  // namespace protobuf
54 
55 
56 namespace protobuf {
57 namespace util {
58 namespace converter {
59 
60 class TypeInfo;
61 
62 // An ObjectSource that can parse a stream of bytes as a protocol buffer.
63 // Its WriteTo() method can be given an ObjectWriter.
64 // This implementation uses a google.protobuf.Type for tag and name lookup.
65 // The field names are converted into lower camel-case when writing to the
66 // ObjectWriter.
67 //
68 // Sample usage: (suppose input is: string proto)
69 //   ArrayInputStream arr_stream(proto.data(), proto.size());
70 //   CodedInputStream in_stream(&arr_stream);
71 //   ProtoStreamObjectSource os(&in_stream, /*ServiceTypeInfo*/ typeinfo,
72 //                              <your message google::protobuf::Type>);
73 //
74 //   Status status = os.WriteTo(<some ObjectWriter>);
75 class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
76  public:
77   ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream,
78                           TypeResolver* type_resolver,
79                           const google::protobuf::Type& type);
80 
81   virtual ~ProtoStreamObjectSource();
82 
83   virtual util::Status NamedWriteTo(StringPiece name, ObjectWriter* ow) const;
84 
85   // Sets whether or not to use lowerCamelCase casing for enum values. If set to
86   // false, enum values are output without any case conversions.
87   //
88   // For example, if we have an enum:
89   // enum Type {
90   //   ACTION_AND_ADVENTURE = 1;
91   // }
92   // Type type = 20;
93   //
94   // And this option is set to true. Then the rendered "type" field will have
95   // the string "actionAndAdventure".
96   // {
97   //   ...
98   //   "type": "actionAndAdventure",
99   //   ...
100   // }
101   //
102   // If set to false, the rendered "type" field will have the string
103   // "ACTION_AND_ADVENTURE".
104   // {
105   //   ...
106   //   "type": "ACTION_AND_ADVENTURE",
107   //   ...
108   // }
set_use_lower_camel_for_enums(bool value)109   void set_use_lower_camel_for_enums(bool value) {
110     use_lower_camel_for_enums_ = value;
111   }
112 
113   // Sets the max recursion depth of proto message to be deserialized. Proto
114   // messages over this depth will fail to be deserialized.
115   // Default value is 64.
set_max_recursion_depth(int max_depth)116   void set_max_recursion_depth(int max_depth) {
117     max_recursion_depth_ = max_depth;
118   }
119 
120 
121  protected:
122   // Writes a proto2 Message to the ObjectWriter. When the given end_tag is
123   // found this method will complete, allowing it to be used for parsing both
124   // nested messages (end with 0) and nested groups (end with group end tag).
125   // The include_start_and_end parameter allows this method to be called when
126   // already inside of an object, and skip calling StartObject and EndObject.
127   virtual util::Status WriteMessage(const google::protobuf::Type& descriptor,
128                                       StringPiece name, const uint32 end_tag,
129                                       bool include_start_and_end,
130                                       ObjectWriter* ow) const;
131 
132  private:
133   ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream,
134                           const TypeInfo* typeinfo,
135                           const google::protobuf::Type& type);
136   // Function that renders a well known type with a modified behavior.
137   typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*,
138                                          const google::protobuf::Type&,
139                                          StringPiece, ObjectWriter*);
140 
141   // Looks up a field and verify its consistency with wire type in tag.
142   const google::protobuf::Field* FindAndVerifyField(
143       const google::protobuf::Type& type, uint32 tag) const;
144 
145   // TODO(skarvaje): Mark these methods as non-const as they modify internal
146   // state (stream_).
147   //
148   // Renders a repeating field (packed or unpacked).
149   // Returns the next tag after reading all sequential repeating elements. The
150   // caller should use this tag before reading more tags from the stream.
151   util::StatusOr<uint32> RenderList(const google::protobuf::Field* field,
152                                       StringPiece name, uint32 list_tag,
153                                       ObjectWriter* ow) const;
154   // Renders a NWP map.
155   // Returns the next tag after reading all map entries. The caller should use
156   // this tag before reading more tags from the stream.
157   util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field,
158                                      StringPiece name, uint32 list_tag,
159                                      ObjectWriter* ow) const;
160 
161   // Renders a packed repeating field. A packed field is stored as:
162   // {tag length item1 item2 item3} instead of the less efficient
163   // {tag item1 tag item2 tag item3}.
164   util::Status RenderPacked(const google::protobuf::Field* field,
165                               ObjectWriter* ow) const;
166 
167   // Renders a google.protobuf.Timestamp value to ObjectWriter
168   static util::Status RenderTimestamp(const ProtoStreamObjectSource* os,
169                                         const google::protobuf::Type& type,
170                                         StringPiece name, ObjectWriter* ow);
171 
172   // Renders a google.protobuf.Duration value to ObjectWriter
173   static util::Status RenderDuration(const ProtoStreamObjectSource* os,
174                                        const google::protobuf::Type& type,
175                                        StringPiece name, ObjectWriter* ow);
176 
177   // Following RenderTYPE functions render well known types in
178   // google/protobuf/wrappers.proto corresponding to TYPE.
179   static util::Status RenderDouble(const ProtoStreamObjectSource* os,
180                                      const google::protobuf::Type& type,
181                                      StringPiece name, ObjectWriter* ow);
182   static util::Status RenderFloat(const ProtoStreamObjectSource* os,
183                                     const google::protobuf::Type& type,
184                                     StringPiece name, ObjectWriter* ow);
185   static util::Status RenderInt64(const ProtoStreamObjectSource* os,
186                                     const google::protobuf::Type& type,
187                                     StringPiece name, ObjectWriter* ow);
188   static util::Status RenderUInt64(const ProtoStreamObjectSource* os,
189                                      const google::protobuf::Type& type,
190                                      StringPiece name, ObjectWriter* ow);
191   static util::Status RenderInt32(const ProtoStreamObjectSource* os,
192                                     const google::protobuf::Type& type,
193                                     StringPiece name, ObjectWriter* ow);
194   static util::Status RenderUInt32(const ProtoStreamObjectSource* os,
195                                      const google::protobuf::Type& type,
196                                      StringPiece name, ObjectWriter* ow);
197   static util::Status RenderBool(const ProtoStreamObjectSource* os,
198                                    const google::protobuf::Type& type,
199                                    StringPiece name, ObjectWriter* ow);
200   static util::Status RenderString(const ProtoStreamObjectSource* os,
201                                      const google::protobuf::Type& type,
202                                      StringPiece name, ObjectWriter* ow);
203   static util::Status RenderBytes(const ProtoStreamObjectSource* os,
204                                     const google::protobuf::Type& type,
205                                     StringPiece name, ObjectWriter* ow);
206 
207   // Renders a google.protobuf.Struct to ObjectWriter.
208   static util::Status RenderStruct(const ProtoStreamObjectSource* os,
209                                      const google::protobuf::Type& type,
210                                      StringPiece name, ObjectWriter* ow);
211 
212   // Helper to render google.protobuf.Struct's Value fields to ObjectWriter.
213   static util::Status RenderStructValue(const ProtoStreamObjectSource* os,
214                                           const google::protobuf::Type& type,
215                                           StringPiece name, ObjectWriter* ow);
216 
217   // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter.
218   static util::Status RenderStructListValue(
219       const ProtoStreamObjectSource* os, const google::protobuf::Type& type,
220       StringPiece name, ObjectWriter* ow);
221 
222   // Render the "Any" type.
223   static util::Status RenderAny(const ProtoStreamObjectSource* os,
224                                   const google::protobuf::Type& type,
225                                   StringPiece name, ObjectWriter* ow);
226 
227   // Render the "FieldMask" type.
228   static util::Status RenderFieldMask(const ProtoStreamObjectSource* os,
229                                         const google::protobuf::Type& type,
230                                         StringPiece name, ObjectWriter* ow);
231 
232   static hash_map<string, TypeRenderer>* renderers_;
233   static void InitRendererMap();
234   static void DeleteRendererMap();
235   static TypeRenderer* FindTypeRenderer(const string& type_url);
236 
237   // Renders a field value to the ObjectWriter.
238   util::Status RenderField(const google::protobuf::Field* field,
239                              StringPiece field_name, ObjectWriter* ow) const;
240 
241   // Same as above but renders all non-message field types. Callers don't call
242   // this function directly. They just use RenderField.
243   util::Status RenderNonMessageField(const google::protobuf::Field* field,
244                                        StringPiece field_name,
245                                        ObjectWriter* ow) const;
246 
247 
248   // Reads field value according to Field spec in 'field' and returns the read
249   // value as string. This only works for primitive datatypes (no message
250   // types).
251   const string ReadFieldValueAsString(
252       const google::protobuf::Field& field) const;
253 
254   // Utility function to detect proto maps. The 'field' MUST be repeated.
255   bool IsMap(const google::protobuf::Field& field) const;
256 
257   // Utility to read int64 and int32 values from a message type in stream_.
258   // Used for reading google.protobuf.Timestamp and Duration messages.
259   std::pair<int64, int32> ReadSecondsAndNanos(
260       const google::protobuf::Type& type) const;
261 
262   // Helper function to check recursion depth and increment it. It will return
263   // Status::OK if the current depth is allowed. Otherwise an error is returned.
264   // type_name and field_name are used for error reporting.
265   util::Status IncrementRecursionDepth(StringPiece type_name,
266                                          StringPiece field_name) const;
267 
268   // Input stream to read from. Ownership rests with the caller.
269   google::protobuf::io::CodedInputStream* stream_;
270 
271   // Type information for all the types used in the descriptor. Used to find
272   // google::protobuf::Type of nested messages/enums.
273   const TypeInfo* typeinfo_;
274   // Whether this class owns the typeinfo_ object. If true the typeinfo_ object
275   // should be deleted in the destructor.
276   bool own_typeinfo_;
277 
278   // google::protobuf::Type of the message source.
279   const google::protobuf::Type& type_;
280 
281 
282   // Whether to render enums using lowerCamelCase. Defaults to false.
283   bool use_lower_camel_for_enums_;
284 
285   // Tracks current recursion depth.
286   mutable int recursion_depth_;
287 
288   // Maximum allowed recursion depth.
289   int max_recursion_depth_;
290 
291   // Whether to render unknown fields.
292   bool render_unknown_fields_;
293 
294   GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource);
295 };
296 
297 }  // namespace converter
298 }  // namespace util
299 }  // namespace protobuf
300 
301 }  // namespace google
302 #endif  // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
303