proto_writer.h 14 KB

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