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