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 whether to always output enums as ints, by default this is off, and 114 // enums are rendered as strings. set_use_ints_for_enums(bool value)115 void set_use_ints_for_enums(bool value) { use_ints_for_enums_ = value; } 116 117 // Sets whether to use original proto field names set_preserve_proto_field_names(bool value)118 void set_preserve_proto_field_names(bool value) { 119 preserve_proto_field_names_ = value; 120 } 121 122 // Sets the max recursion depth of proto message to be deserialized. Proto 123 // messages over this depth will fail to be deserialized. 124 // Default value is 64. set_max_recursion_depth(int max_depth)125 void set_max_recursion_depth(int max_depth) { 126 max_recursion_depth_ = max_depth; 127 } 128 129 130 protected: 131 // Writes a proto2 Message to the ObjectWriter. When the given end_tag is 132 // found this method will complete, allowing it to be used for parsing both 133 // nested messages (end with 0) and nested groups (end with group end tag). 134 // The include_start_and_end parameter allows this method to be called when 135 // already inside of an object, and skip calling StartObject and EndObject. 136 virtual util::Status WriteMessage(const google::protobuf::Type& descriptor, 137 StringPiece name, const uint32 end_tag, 138 bool include_start_and_end, 139 ObjectWriter* ow) const; 140 141 // Renders a repeating field (packed or unpacked). Returns the next tag after 142 // reading all sequential repeating elements. The caller should use this tag 143 // before reading more tags from the stream. 144 virtual util::StatusOr<uint32> RenderList( 145 const google::protobuf::Field* field, StringPiece name, uint32 list_tag, 146 ObjectWriter* ow) const; 147 148 // Looks up a field and verify its consistency with wire type in tag. 149 const google::protobuf::Field* FindAndVerifyField( 150 const google::protobuf::Type& type, uint32 tag) const; 151 152 // Renders a field value to the ObjectWriter. 153 util::Status RenderField(const google::protobuf::Field* field, 154 StringPiece field_name, ObjectWriter* ow) const; 155 156 // Reads field value according to Field spec in 'field' and returns the read 157 // value as string. This only works for primitive datatypes (no message 158 // types). 159 const string ReadFieldValueAsString( 160 const google::protobuf::Field& field) const; 161 162 163 private: 164 ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream, 165 const TypeInfo* typeinfo, 166 const google::protobuf::Type& type); 167 // Function that renders a well known type with a modified behavior. 168 typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*, 169 const google::protobuf::Type&, 170 StringPiece, ObjectWriter*); 171 172 // TODO(skarvaje): Mark these methods as non-const as they modify internal 173 // state (stream_). 174 // 175 // Renders a NWP map. 176 // Returns the next tag after reading all map entries. The caller should use 177 // this tag before reading more tags from the stream. 178 util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field, 179 StringPiece name, uint32 list_tag, 180 ObjectWriter* ow) const; 181 182 // Renders a packed repeating field. A packed field is stored as: 183 // {tag length item1 item2 item3} instead of the less efficient 184 // {tag item1 tag item2 tag item3}. 185 util::Status RenderPacked(const google::protobuf::Field* field, 186 ObjectWriter* ow) const; 187 188 // Renders a google.protobuf.Timestamp value to ObjectWriter 189 static util::Status RenderTimestamp(const ProtoStreamObjectSource* os, 190 const google::protobuf::Type& type, 191 StringPiece name, ObjectWriter* ow); 192 193 // Renders a google.protobuf.Duration value to ObjectWriter 194 static util::Status RenderDuration(const ProtoStreamObjectSource* os, 195 const google::protobuf::Type& type, 196 StringPiece name, ObjectWriter* ow); 197 198 // Following RenderTYPE functions render well known types in 199 // google/protobuf/wrappers.proto corresponding to TYPE. 200 static util::Status RenderDouble(const ProtoStreamObjectSource* os, 201 const google::protobuf::Type& type, 202 StringPiece name, ObjectWriter* ow); 203 static util::Status RenderFloat(const ProtoStreamObjectSource* os, 204 const google::protobuf::Type& type, 205 StringPiece name, ObjectWriter* ow); 206 static util::Status RenderInt64(const ProtoStreamObjectSource* os, 207 const google::protobuf::Type& type, 208 StringPiece name, ObjectWriter* ow); 209 static util::Status RenderUInt64(const ProtoStreamObjectSource* os, 210 const google::protobuf::Type& type, 211 StringPiece name, ObjectWriter* ow); 212 static util::Status RenderInt32(const ProtoStreamObjectSource* os, 213 const google::protobuf::Type& type, 214 StringPiece name, ObjectWriter* ow); 215 static util::Status RenderUInt32(const ProtoStreamObjectSource* os, 216 const google::protobuf::Type& type, 217 StringPiece name, ObjectWriter* ow); 218 static util::Status RenderBool(const ProtoStreamObjectSource* os, 219 const google::protobuf::Type& type, 220 StringPiece name, ObjectWriter* ow); 221 static util::Status RenderString(const ProtoStreamObjectSource* os, 222 const google::protobuf::Type& type, 223 StringPiece name, ObjectWriter* ow); 224 static util::Status RenderBytes(const ProtoStreamObjectSource* os, 225 const google::protobuf::Type& type, 226 StringPiece name, ObjectWriter* ow); 227 228 // Renders a google.protobuf.Struct to ObjectWriter. 229 static util::Status RenderStruct(const ProtoStreamObjectSource* os, 230 const google::protobuf::Type& type, 231 StringPiece name, ObjectWriter* ow); 232 233 // Helper to render google.protobuf.Struct's Value fields to ObjectWriter. 234 static util::Status RenderStructValue(const ProtoStreamObjectSource* os, 235 const google::protobuf::Type& type, 236 StringPiece name, ObjectWriter* ow); 237 238 // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter. 239 static util::Status RenderStructListValue( 240 const ProtoStreamObjectSource* os, const google::protobuf::Type& type, 241 StringPiece name, ObjectWriter* ow); 242 243 // Render the "Any" type. 244 static util::Status RenderAny(const ProtoStreamObjectSource* os, 245 const google::protobuf::Type& type, 246 StringPiece name, ObjectWriter* ow); 247 248 // Render the "FieldMask" type. 249 static util::Status RenderFieldMask(const ProtoStreamObjectSource* os, 250 const google::protobuf::Type& type, 251 StringPiece name, ObjectWriter* ow); 252 253 static hash_map<string, TypeRenderer>* renderers_; 254 static void InitRendererMap(); 255 static void DeleteRendererMap(); 256 static TypeRenderer* FindTypeRenderer(const string& type_url); 257 258 // Same as above but renders all non-message field types. Callers don't call 259 // this function directly. They just use RenderField. 260 util::Status RenderNonMessageField(const google::protobuf::Field* field, 261 StringPiece field_name, 262 ObjectWriter* ow) const; 263 264 265 // Utility function to detect proto maps. The 'field' MUST be repeated. 266 bool IsMap(const google::protobuf::Field& field) const; 267 268 // Utility to read int64 and int32 values from a message type in stream_. 269 // Used for reading google.protobuf.Timestamp and Duration messages. 270 std::pair<int64, int32> ReadSecondsAndNanos( 271 const google::protobuf::Type& type) const; 272 273 // Helper function to check recursion depth and increment it. It will return 274 // Status::OK if the current depth is allowed. Otherwise an error is returned. 275 // type_name and field_name are used for error reporting. 276 util::Status IncrementRecursionDepth(StringPiece type_name, 277 StringPiece field_name) const; 278 279 // Input stream to read from. Ownership rests with the caller. 280 google::protobuf::io::CodedInputStream* stream_; 281 282 // Type information for all the types used in the descriptor. Used to find 283 // google::protobuf::Type of nested messages/enums. 284 const TypeInfo* typeinfo_; 285 286 // Whether this class owns the typeinfo_ object. If true the typeinfo_ object 287 // should be deleted in the destructor. 288 bool own_typeinfo_; 289 290 // google::protobuf::Type of the message source. 291 const google::protobuf::Type& type_; 292 293 294 // Whether to render enums using lowerCamelCase. Defaults to false. 295 bool use_lower_camel_for_enums_; 296 297 // Whether to render enums as ints always. Defaults to false. 298 bool use_ints_for_enums_; 299 300 // Whether to preserve proto field names 301 bool preserve_proto_field_names_; 302 303 // Tracks current recursion depth. 304 mutable int recursion_depth_; 305 306 // Maximum allowed recursion depth. 307 int max_recursion_depth_; 308 309 // Whether to render unknown fields. 310 bool render_unknown_fields_; 311 312 // Whether to render unknown enum values. 313 bool render_unknown_enum_values_; 314 315 // Whether to add trailing zeros for timestamp and duration. 316 bool add_trailing_zeros_for_timestamp_and_duration_; 317 318 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource); 319 }; 320 321 } // namespace converter 322 } // namespace util 323 } // namespace protobuf 324 325 } // namespace google 326 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__ 327