| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // http://code.google.com/p/protobuf/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | 34 |
| 35 #include <google/protobuf/unknown_field_set.h> |
| 36 |
| 35 #include <google/protobuf/stubs/common.h> | 37 #include <google/protobuf/stubs/common.h> |
| 36 #include <google/protobuf/unknown_field_set.h> | |
| 37 #include <google/protobuf/stubs/stl_util-inl.h> | |
| 38 #include <google/protobuf/io/coded_stream.h> | 38 #include <google/protobuf/io/coded_stream.h> |
| 39 #include <google/protobuf/io/zero_copy_stream.h> | 39 #include <google/protobuf/io/zero_copy_stream.h> |
| 40 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> | 40 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 41 #include <google/protobuf/wire_format_lite.h> | 41 #include <google/protobuf/wire_format_lite.h> |
| 42 #include <google/protobuf/stubs/stl_util.h> |
| 42 | 43 |
| 43 namespace google { | 44 namespace google { |
| 44 namespace protobuf { | 45 namespace protobuf { |
| 45 | 46 |
| 46 namespace internal { | 47 namespace internal { |
| 47 | 48 |
| 48 int StringSpaceUsedExcludingSelf(const string& str) { | 49 int StringSpaceUsedExcludingSelf(const string& str) { |
| 49 const void* start = &str; | 50 const void* start = &str; |
| 50 const void* end = &str + 1; | 51 const void* end = &str + 1; |
| 51 | 52 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 } | 69 } |
| 69 | 70 |
| 70 void UnknownFieldSet::ClearFallback() { | 71 void UnknownFieldSet::ClearFallback() { |
| 71 GOOGLE_DCHECK(fields_ != NULL); | 72 GOOGLE_DCHECK(fields_ != NULL); |
| 72 for (int i = 0; i < fields_->size(); i++) { | 73 for (int i = 0; i < fields_->size(); i++) { |
| 73 (*fields_)[i].Delete(); | 74 (*fields_)[i].Delete(); |
| 74 } | 75 } |
| 75 fields_->clear(); | 76 fields_->clear(); |
| 76 } | 77 } |
| 77 | 78 |
| 79 void UnknownFieldSet::ClearAndFreeMemory() { |
| 80 if (fields_ != NULL) { |
| 81 Clear(); |
| 82 delete fields_; |
| 83 fields_ = NULL; |
| 84 } |
| 85 } |
| 86 |
| 78 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) { | 87 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) { |
| 79 for (int i = 0; i < other.field_count(); i++) { | 88 for (int i = 0; i < other.field_count(); i++) { |
| 80 AddField(other.field(i)); | 89 AddField(other.field(i)); |
| 81 } | 90 } |
| 82 } | 91 } |
| 83 | 92 |
| 84 int UnknownFieldSet::SpaceUsedExcludingSelf() const { | 93 int UnknownFieldSet::SpaceUsedExcludingSelf() const { |
| 85 if (fields_ == NULL) return 0; | 94 if (fields_ == NULL) return 0; |
| 86 | 95 |
| 87 int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size(); | 96 int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size(); |
| 88 for (int i = 0; i < fields_->size(); i++) { | 97 for (int i = 0; i < fields_->size(); i++) { |
| 89 const UnknownField& field = (*fields_)[i]; | 98 const UnknownField& field = (*fields_)[i]; |
| 90 switch (field.type()) { | 99 switch (field.type()) { |
| 91 case UnknownField::TYPE_LENGTH_DELIMITED: | 100 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 92 total_size += sizeof(*field.length_delimited_) + | 101 total_size += sizeof(*field.length_delimited_.string_value_) + |
| 93 internal::StringSpaceUsedExcludingSelf(*field.length_delimited_); | 102 internal::StringSpaceUsedExcludingSelf( |
| 103 *field.length_delimited_.string_value_); |
| 94 break; | 104 break; |
| 95 case UnknownField::TYPE_GROUP: | 105 case UnknownField::TYPE_GROUP: |
| 96 total_size += field.group_->SpaceUsed(); | 106 total_size += field.group_->SpaceUsed(); |
| 97 break; | 107 break; |
| 98 default: | 108 default: |
| 99 break; | 109 break; |
| 100 } | 110 } |
| 101 } | 111 } |
| 102 return total_size; | 112 return total_size; |
| 103 } | 113 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 131 field.type_ = UnknownField::TYPE_FIXED64; | 141 field.type_ = UnknownField::TYPE_FIXED64; |
| 132 field.fixed64_ = value; | 142 field.fixed64_ = value; |
| 133 fields_->push_back(field); | 143 fields_->push_back(field); |
| 134 } | 144 } |
| 135 | 145 |
| 136 string* UnknownFieldSet::AddLengthDelimited(int number) { | 146 string* UnknownFieldSet::AddLengthDelimited(int number) { |
| 137 if (fields_ == NULL) fields_ = new vector<UnknownField>; | 147 if (fields_ == NULL) fields_ = new vector<UnknownField>; |
| 138 UnknownField field; | 148 UnknownField field; |
| 139 field.number_ = number; | 149 field.number_ = number; |
| 140 field.type_ = UnknownField::TYPE_LENGTH_DELIMITED; | 150 field.type_ = UnknownField::TYPE_LENGTH_DELIMITED; |
| 141 field.length_delimited_ = new string; | 151 field.length_delimited_.string_value_ = new string; |
| 142 fields_->push_back(field); | 152 fields_->push_back(field); |
| 143 return field.length_delimited_; | 153 return field.length_delimited_.string_value_; |
| 144 } | 154 } |
| 145 | 155 |
| 156 |
| 146 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) { | 157 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) { |
| 147 if (fields_ == NULL) fields_ = new vector<UnknownField>; | 158 if (fields_ == NULL) fields_ = new vector<UnknownField>; |
| 148 UnknownField field; | 159 UnknownField field; |
| 149 field.number_ = number; | 160 field.number_ = number; |
| 150 field.type_ = UnknownField::TYPE_GROUP; | 161 field.type_ = UnknownField::TYPE_GROUP; |
| 151 field.group_ = new UnknownFieldSet; | 162 field.group_ = new UnknownFieldSet; |
| 152 fields_->push_back(field); | 163 fields_->push_back(field); |
| 153 return field.group_; | 164 return field.group_; |
| 154 } | 165 } |
| 155 | 166 |
| 156 void UnknownFieldSet::AddField(const UnknownField& field) { | 167 void UnknownFieldSet::AddField(const UnknownField& field) { |
| 157 if (fields_ == NULL) fields_ = new vector<UnknownField>; | 168 if (fields_ == NULL) fields_ = new vector<UnknownField>; |
| 158 fields_->push_back(field); | 169 fields_->push_back(field); |
| 159 fields_->back().DeepCopy(); | 170 fields_->back().DeepCopy(); |
| 160 } | 171 } |
| 161 | 172 |
| 173 void UnknownFieldSet::DeleteSubrange(int start, int num) { |
| 174 GOOGLE_DCHECK(fields_ != NULL); |
| 175 // Delete the specified fields. |
| 176 for (int i = 0; i < num; ++i) { |
| 177 (*fields_)[i + start].Delete(); |
| 178 } |
| 179 // Slide down the remaining fields. |
| 180 for (int i = start + num; i < fields_->size(); ++i) { |
| 181 (*fields_)[i - num] = (*fields_)[i]; |
| 182 } |
| 183 // Pop off the # of deleted fields. |
| 184 for (int i = 0; i < num; ++i) { |
| 185 fields_->pop_back(); |
| 186 } |
| 187 } |
| 188 |
| 189 void UnknownFieldSet::DeleteByNumber(int number) { |
| 190 if (fields_ == NULL) return; |
| 191 int left = 0; // The number of fields left after deletion. |
| 192 for (int i = 0; i < fields_->size(); ++i) { |
| 193 UnknownField* field = &(*fields_)[i]; |
| 194 if (field->number() == number) { |
| 195 field->Delete(); |
| 196 } else { |
| 197 if (i != left) { |
| 198 (*fields_)[left] = (*fields_)[i]; |
| 199 } |
| 200 ++left; |
| 201 } |
| 202 } |
| 203 fields_->resize(left); |
| 204 } |
| 205 |
| 162 bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) { | 206 bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) { |
| 163 | 207 |
| 164 UnknownFieldSet other; | 208 UnknownFieldSet other; |
| 165 if (internal::WireFormatLite::SkipMessage(input, &other) && | 209 if (internal::WireFormatLite::SkipMessage(input, &other) && |
| 166 input->ConsumedEntireMessage()) { | 210 input->ConsumedEntireMessage()) { |
| 167 MergeFrom(other); | 211 MergeFrom(other); |
| 168 return true; | 212 return true; |
| 169 } else { | 213 } else { |
| 170 return false; | 214 return false; |
| 171 } | 215 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 183 } | 227 } |
| 184 | 228 |
| 185 bool UnknownFieldSet::ParseFromArray(const void* data, int size) { | 229 bool UnknownFieldSet::ParseFromArray(const void* data, int size) { |
| 186 io::ArrayInputStream input(data, size); | 230 io::ArrayInputStream input(data, size); |
| 187 return ParseFromZeroCopyStream(&input); | 231 return ParseFromZeroCopyStream(&input); |
| 188 } | 232 } |
| 189 | 233 |
| 190 void UnknownField::Delete() { | 234 void UnknownField::Delete() { |
| 191 switch (type()) { | 235 switch (type()) { |
| 192 case UnknownField::TYPE_LENGTH_DELIMITED: | 236 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 193 delete length_delimited_; | 237 delete length_delimited_.string_value_; |
| 194 break; | 238 break; |
| 195 case UnknownField::TYPE_GROUP: | 239 case UnknownField::TYPE_GROUP: |
| 196 delete group_; | 240 delete group_; |
| 197 break; | 241 break; |
| 198 default: | 242 default: |
| 199 break; | 243 break; |
| 200 } | 244 } |
| 201 } | 245 } |
| 202 | 246 |
| 203 void UnknownField::DeepCopy() { | 247 void UnknownField::DeepCopy() { |
| 204 switch (type()) { | 248 switch (type()) { |
| 205 case UnknownField::TYPE_LENGTH_DELIMITED: | 249 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 206 length_delimited_ = new string(*length_delimited_); | 250 length_delimited_.string_value_ = new string( |
| 251 *length_delimited_.string_value_); |
| 207 break; | 252 break; |
| 208 case UnknownField::TYPE_GROUP: { | 253 case UnknownField::TYPE_GROUP: { |
| 209 UnknownFieldSet* group = new UnknownFieldSet; | 254 UnknownFieldSet* group = new UnknownFieldSet; |
| 210 group->MergeFrom(*group_); | 255 group->MergeFrom(*group_); |
| 211 group_ = group; | 256 group_ = group; |
| 212 break; | 257 break; |
| 213 } | 258 } |
| 214 default: | 259 default: |
| 215 break; | 260 break; |
| 216 } | 261 } |
| 217 } | 262 } |
| 218 | 263 |
| 264 |
| 265 void UnknownField::SerializeLengthDelimitedNoTag( |
| 266 io::CodedOutputStream* output) const { |
| 267 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type_); |
| 268 const string& data = *length_delimited_.string_value_; |
| 269 output->WriteVarint32(data.size()); |
| 270 output->WriteString(data); |
| 271 } |
| 272 |
| 273 uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const { |
| 274 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type_); |
| 275 const string& data = *length_delimited_.string_value_; |
| 276 target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target); |
| 277 target = io::CodedOutputStream::WriteStringToArray(data, target); |
| 278 return target; |
| 279 } |
| 280 |
| 219 } // namespace protobuf | 281 } // namespace protobuf |
| 220 } // namespace google | 282 } // namespace google |
| OLD | NEW |