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