unknown_field_set.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/unknown_field_set.h>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/io/coded_stream.h>
  37. #include <google/protobuf/io/zero_copy_stream.h>
  38. #include <google/protobuf/io/zero_copy_stream_impl.h>
  39. #include <google/protobuf/metadata.h>
  40. #include <google/protobuf/wire_format.h>
  41. #include <google/protobuf/stubs/stl_util.h>
  42. namespace google {
  43. namespace protobuf {
  44. const UnknownFieldSet* UnknownFieldSet::default_instance() {
  45. static auto instance = internal::OnShutdownDelete(new UnknownFieldSet());
  46. return instance;
  47. }
  48. void UnknownFieldSet::ClearFallback() {
  49. GOOGLE_DCHECK(fields_ != NULL && fields_->size() > 0);
  50. int n = fields_->size();
  51. do {
  52. (*fields_)[--n].Delete();
  53. } while (n > 0);
  54. delete fields_;
  55. fields_ = NULL;
  56. }
  57. void UnknownFieldSet::InternalMergeFrom(const UnknownFieldSet& other) {
  58. int other_field_count = other.field_count();
  59. if (other_field_count > 0) {
  60. fields_ = new std::vector<UnknownField>();
  61. for (int i = 0; i < other_field_count; i++) {
  62. fields_->push_back((*other.fields_)[i]);
  63. fields_->back().DeepCopy((*other.fields_)[i]);
  64. }
  65. }
  66. }
  67. void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
  68. int other_field_count = other.field_count();
  69. if (other_field_count > 0) {
  70. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  71. for (int i = 0; i < other_field_count; i++) {
  72. fields_->push_back((*other.fields_)[i]);
  73. fields_->back().DeepCopy((*other.fields_)[i]);
  74. }
  75. }
  76. }
  77. // A specialized MergeFrom for performance when we are merging from an UFS that
  78. // is temporary and can be destroyed in the process.
  79. void UnknownFieldSet::MergeFromAndDestroy(UnknownFieldSet* other) {
  80. int other_field_count = other->field_count();
  81. if (other_field_count > 0) {
  82. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  83. for (int i = 0; i < other_field_count; i++) {
  84. fields_->push_back((*other->fields_)[i]);
  85. (*other->fields_)[i].Reset();
  86. }
  87. }
  88. delete other->fields_;
  89. other->fields_ = NULL;
  90. }
  91. void UnknownFieldSet::MergeToInternalMetdata(
  92. const UnknownFieldSet& other,
  93. internal::InternalMetadataWithArena* metadata) {
  94. metadata->mutable_unknown_fields()->MergeFrom(other);
  95. }
  96. size_t UnknownFieldSet::SpaceUsedExcludingSelfLong() const {
  97. if (fields_ == NULL) return 0;
  98. size_t total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size();
  99. for (int i = 0; i < fields_->size(); i++) {
  100. const UnknownField& field = (*fields_)[i];
  101. switch (field.type()) {
  102. case UnknownField::TYPE_LENGTH_DELIMITED:
  103. total_size += sizeof(*field.data_.length_delimited_.string_value_) +
  104. internal::StringSpaceUsedExcludingSelfLong(
  105. *field.data_.length_delimited_.string_value_);
  106. break;
  107. case UnknownField::TYPE_GROUP:
  108. total_size += field.data_.group_->SpaceUsedLong();
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. return total_size;
  115. }
  116. size_t UnknownFieldSet::SpaceUsedLong() const {
  117. return sizeof(*this) + SpaceUsedExcludingSelf();
  118. }
  119. void UnknownFieldSet::AddVarint(int number, uint64 value) {
  120. UnknownField field;
  121. field.number_ = number;
  122. field.SetType(UnknownField::TYPE_VARINT);
  123. field.data_.varint_ = value;
  124. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  125. fields_->push_back(field);
  126. }
  127. void UnknownFieldSet::AddFixed32(int number, uint32 value) {
  128. UnknownField field;
  129. field.number_ = number;
  130. field.SetType(UnknownField::TYPE_FIXED32);
  131. field.data_.fixed32_ = value;
  132. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  133. fields_->push_back(field);
  134. }
  135. void UnknownFieldSet::AddFixed64(int number, uint64 value) {
  136. UnknownField field;
  137. field.number_ = number;
  138. field.SetType(UnknownField::TYPE_FIXED64);
  139. field.data_.fixed64_ = value;
  140. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  141. fields_->push_back(field);
  142. }
  143. string* UnknownFieldSet::AddLengthDelimited(int number) {
  144. UnknownField field;
  145. field.number_ = number;
  146. field.SetType(UnknownField::TYPE_LENGTH_DELIMITED);
  147. field.data_.length_delimited_.string_value_ = new string;
  148. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  149. fields_->push_back(field);
  150. return field.data_.length_delimited_.string_value_;
  151. }
  152. UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
  153. UnknownField field;
  154. field.number_ = number;
  155. field.SetType(UnknownField::TYPE_GROUP);
  156. field.data_.group_ = new UnknownFieldSet;
  157. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  158. fields_->push_back(field);
  159. return field.data_.group_;
  160. }
  161. void UnknownFieldSet::AddField(const UnknownField& field) {
  162. if (fields_ == NULL) fields_ = new std::vector<UnknownField>();
  163. fields_->push_back(field);
  164. fields_->back().DeepCopy(field);
  165. }
  166. void UnknownFieldSet::DeleteSubrange(int start, int num) {
  167. // Delete the specified fields.
  168. for (int i = 0; i < num; ++i) {
  169. (*fields_)[i + start].Delete();
  170. }
  171. // Slide down the remaining fields.
  172. for (int i = start + num; i < fields_->size(); ++i) {
  173. (*fields_)[i - num] = (*fields_)[i];
  174. }
  175. // Pop off the # of deleted fields.
  176. for (int i = 0; i < num; ++i) {
  177. fields_->pop_back();
  178. }
  179. if (fields_ && fields_->size() == 0) {
  180. // maintain invariant: never hold fields_ if empty.
  181. delete fields_;
  182. fields_ = NULL;
  183. }
  184. }
  185. void UnknownFieldSet::DeleteByNumber(int number) {
  186. if (fields_ == NULL) return;
  187. int left = 0; // The number of fields left after deletion.
  188. for (int i = 0; i < fields_->size(); ++i) {
  189. UnknownField* field = &(*fields_)[i];
  190. if (field->number() == number) {
  191. field->Delete();
  192. } else {
  193. if (i != left) {
  194. (*fields_)[left] = (*fields_)[i];
  195. }
  196. ++left;
  197. }
  198. }
  199. fields_->resize(left);
  200. if (left == 0) {
  201. // maintain invariant: never hold fields_ if empty.
  202. delete fields_;
  203. fields_ = NULL;
  204. }
  205. }
  206. bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
  207. UnknownFieldSet other;
  208. if (internal::WireFormat::SkipMessage(input, &other) &&
  209. input->ConsumedEntireMessage()) {
  210. MergeFromAndDestroy(&other);
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. bool UnknownFieldSet::ParseFromCodedStream(io::CodedInputStream* input) {
  217. Clear();
  218. return MergeFromCodedStream(input);
  219. }
  220. bool UnknownFieldSet::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
  221. io::CodedInputStream coded_input(input);
  222. return (ParseFromCodedStream(&coded_input) &&
  223. coded_input.ConsumedEntireMessage());
  224. }
  225. bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
  226. io::ArrayInputStream input(data, size);
  227. return ParseFromZeroCopyStream(&input);
  228. }
  229. void UnknownField::Delete() {
  230. switch (type()) {
  231. case UnknownField::TYPE_LENGTH_DELIMITED:
  232. delete data_.length_delimited_.string_value_;
  233. break;
  234. case UnknownField::TYPE_GROUP:
  235. delete data_.group_;
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. // Reset all owned ptrs, a special function for performance, to avoid double
  242. // owning the ptrs, when we merge from a temporary UnknownFieldSet objects.
  243. void UnknownField::Reset() {
  244. switch (type()) {
  245. case UnknownField::TYPE_LENGTH_DELIMITED:
  246. data_.length_delimited_.string_value_ = NULL;
  247. break;
  248. case UnknownField::TYPE_GROUP: {
  249. data_.group_ = NULL;
  250. break;
  251. }
  252. default:
  253. break;
  254. }
  255. }
  256. void UnknownField::DeepCopy(const UnknownField& other) {
  257. switch (type()) {
  258. case UnknownField::TYPE_LENGTH_DELIMITED:
  259. data_.length_delimited_.string_value_ = new string(
  260. *data_.length_delimited_.string_value_);
  261. break;
  262. case UnknownField::TYPE_GROUP: {
  263. UnknownFieldSet* group = new UnknownFieldSet();
  264. group->InternalMergeFrom(*data_.group_);
  265. data_.group_ = group;
  266. break;
  267. }
  268. default:
  269. break;
  270. }
  271. }
  272. void UnknownField::SerializeLengthDelimitedNoTag(
  273. io::CodedOutputStream* output) const {
  274. GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
  275. const string& data = *data_.length_delimited_.string_value_;
  276. output->WriteVarint32(data.size());
  277. output->WriteRawMaybeAliased(data.data(), data.size());
  278. }
  279. uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const {
  280. GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
  281. const string& data = *data_.length_delimited_.string_value_;
  282. target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target);
  283. target = io::CodedOutputStream::WriteStringToArray(data, target);
  284. return target;
  285. }
  286. } // namespace protobuf
  287. } // namespace google