/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/unknown_field_set.cc

http://github.com/tomahawk-player/tomahawk · C++ · 204 lines · 148 code · 24 blank · 32 comment · 23 complexity · 82a1190aff5cebbb13104a1d32041134 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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/stubs/common.h>
  34. #include <google/protobuf/unknown_field_set.h>
  35. #include <google/protobuf/stubs/stl_util-inl.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/wire_format.h>
  40. namespace google {
  41. namespace protobuf {
  42. UnknownFieldSet::UnknownFieldSet()
  43. : fields_(NULL) {}
  44. UnknownFieldSet::~UnknownFieldSet() {
  45. Clear();
  46. delete fields_;
  47. }
  48. void UnknownFieldSet::ClearFallback() {
  49. GOOGLE_DCHECK(fields_ != NULL);
  50. for (int i = 0; i < fields_->size(); i++) {
  51. (*fields_)[i].Delete();
  52. }
  53. fields_->clear();
  54. }
  55. void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
  56. for (int i = 0; i < other.field_count(); i++) {
  57. AddField(other.field(i));
  58. }
  59. }
  60. int UnknownFieldSet::SpaceUsedExcludingSelf() const {
  61. if (fields_ == NULL) return 0;
  62. int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size();
  63. for (int i = 0; i < fields_->size(); i++) {
  64. const UnknownField& field = (*fields_)[i];
  65. switch (field.type()) {
  66. case UnknownField::TYPE_LENGTH_DELIMITED:
  67. total_size += sizeof(*field.length_delimited_) +
  68. internal::StringSpaceUsedExcludingSelf(*field.length_delimited_);
  69. break;
  70. case UnknownField::TYPE_GROUP:
  71. total_size += field.group_->SpaceUsed();
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. return total_size;
  78. }
  79. int UnknownFieldSet::SpaceUsed() const {
  80. return sizeof(*this) + SpaceUsedExcludingSelf();
  81. }
  82. void UnknownFieldSet::AddVarint(int number, uint64 value) {
  83. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  84. UnknownField field;
  85. field.number_ = number;
  86. field.type_ = UnknownField::TYPE_VARINT;
  87. field.varint_ = value;
  88. fields_->push_back(field);
  89. }
  90. void UnknownFieldSet::AddFixed32(int number, uint32 value) {
  91. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  92. UnknownField field;
  93. field.number_ = number;
  94. field.type_ = UnknownField::TYPE_FIXED32;
  95. field.fixed32_ = value;
  96. fields_->push_back(field);
  97. }
  98. void UnknownFieldSet::AddFixed64(int number, uint64 value) {
  99. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  100. UnknownField field;
  101. field.number_ = number;
  102. field.type_ = UnknownField::TYPE_FIXED64;
  103. field.fixed64_ = value;
  104. fields_->push_back(field);
  105. }
  106. string* UnknownFieldSet::AddLengthDelimited(int number) {
  107. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  108. UnknownField field;
  109. field.number_ = number;
  110. field.type_ = UnknownField::TYPE_LENGTH_DELIMITED;
  111. field.length_delimited_ = new string;
  112. fields_->push_back(field);
  113. return field.length_delimited_;
  114. }
  115. UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
  116. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  117. UnknownField field;
  118. field.number_ = number;
  119. field.type_ = UnknownField::TYPE_GROUP;
  120. field.group_ = new UnknownFieldSet;
  121. fields_->push_back(field);
  122. return field.group_;
  123. }
  124. void UnknownFieldSet::AddField(const UnknownField& field) {
  125. if (fields_ == NULL) fields_ = new vector<UnknownField>;
  126. fields_->push_back(field);
  127. fields_->back().DeepCopy();
  128. }
  129. bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
  130. UnknownFieldSet other;
  131. if (internal::WireFormat::SkipMessage(input, &other) &&
  132. input->ConsumedEntireMessage()) {
  133. MergeFrom(other);
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool UnknownFieldSet::ParseFromCodedStream(io::CodedInputStream* input) {
  140. Clear();
  141. return MergeFromCodedStream(input);
  142. }
  143. bool UnknownFieldSet::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
  144. io::CodedInputStream coded_input(input);
  145. return ParseFromCodedStream(&coded_input) &&
  146. coded_input.ConsumedEntireMessage();
  147. }
  148. bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
  149. io::ArrayInputStream input(data, size);
  150. return ParseFromZeroCopyStream(&input);
  151. }
  152. void UnknownField::Delete() {
  153. switch (type()) {
  154. case UnknownField::TYPE_LENGTH_DELIMITED:
  155. delete length_delimited_;
  156. break;
  157. case UnknownField::TYPE_GROUP:
  158. delete group_;
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. void UnknownField::DeepCopy() {
  165. switch (type()) {
  166. case UnknownField::TYPE_LENGTH_DELIMITED:
  167. length_delimited_ = new string(*length_delimited_);
  168. break;
  169. case UnknownField::TYPE_GROUP: {
  170. UnknownFieldSet* group = new UnknownFieldSet;
  171. group->MergeFrom(*group_);
  172. group_ = group;
  173. break;
  174. }
  175. default:
  176. break;
  177. }
  178. }
  179. } // namespace protobuf
  180. } // namespace google