1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/unknown_field_set.h>
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/io/coded_stream.h>
39 #include <google/protobuf/io/zero_copy_stream.h>
40 #include <google/protobuf/io/zero_copy_stream_impl.h>
41 #include <google/protobuf/wire_format.h>
42 #include <google/protobuf/stubs/stl_util.h>
47 UnknownFieldSet::UnknownFieldSet()
50 UnknownFieldSet::~UnknownFieldSet() {
55 void UnknownFieldSet::ClearFallback() {
56 GOOGLE_DCHECK(fields_ != NULL);
57 for (int i = 0; i < fields_->size(); i++) {
58 (*fields_)[i].Delete();
63 void UnknownFieldSet::ClearAndFreeMemory() {
64 if (fields_ != NULL) {
71 void UnknownFieldSet::MergeFrom(const UnknownFieldSet& other) {
72 for (int i = 0; i < other.field_count(); i++) {
73 AddField(other.field(i));
77 int UnknownFieldSet::SpaceUsedExcludingSelf() const {
78 if (fields_ == NULL) return 0;
80 int total_size = sizeof(*fields_) + sizeof(UnknownField) * fields_->size();
81 for (int i = 0; i < fields_->size(); i++) {
82 const UnknownField& field = (*fields_)[i];
83 switch (field.type()) {
84 case UnknownField::TYPE_LENGTH_DELIMITED:
85 total_size += sizeof(*field.length_delimited_.string_value_) +
86 internal::StringSpaceUsedExcludingSelf(
87 *field.length_delimited_.string_value_);
89 case UnknownField::TYPE_GROUP:
90 total_size += field.group_->SpaceUsed();
99 int UnknownFieldSet::SpaceUsed() const {
100 return sizeof(*this) + SpaceUsedExcludingSelf();
103 void UnknownFieldSet::AddVarint(int number, uint64 value) {
104 if (fields_ == NULL) fields_ = new vector<UnknownField>;
106 field.number_ = number;
107 field.SetType(UnknownField::TYPE_VARINT);
108 field.varint_ = value;
109 fields_->push_back(field);
112 void UnknownFieldSet::AddFixed32(int number, uint32 value) {
113 if (fields_ == NULL) fields_ = new vector<UnknownField>;
115 field.number_ = number;
116 field.SetType(UnknownField::TYPE_FIXED32);
117 field.fixed32_ = value;
118 fields_->push_back(field);
121 void UnknownFieldSet::AddFixed64(int number, uint64 value) {
122 if (fields_ == NULL) fields_ = new vector<UnknownField>;
124 field.number_ = number;
125 field.SetType(UnknownField::TYPE_FIXED64);
126 field.fixed64_ = value;
127 fields_->push_back(field);
130 string* UnknownFieldSet::AddLengthDelimited(int number) {
131 if (fields_ == NULL) fields_ = new vector<UnknownField>;
133 field.number_ = number;
134 field.SetType(UnknownField::TYPE_LENGTH_DELIMITED);
135 field.length_delimited_.string_value_ = new string;
136 fields_->push_back(field);
137 return field.length_delimited_.string_value_;
141 UnknownFieldSet* UnknownFieldSet::AddGroup(int number) {
142 if (fields_ == NULL) fields_ = new vector<UnknownField>;
144 field.number_ = number;
145 field.SetType(UnknownField::TYPE_GROUP);
146 field.group_ = new UnknownFieldSet;
147 fields_->push_back(field);
151 void UnknownFieldSet::AddField(const UnknownField& field) {
152 if (fields_ == NULL) fields_ = new vector<UnknownField>;
153 fields_->push_back(field);
154 fields_->back().DeepCopy();
157 void UnknownFieldSet::DeleteSubrange(int start, int num) {
158 GOOGLE_DCHECK(fields_ != NULL);
159 // Delete the specified fields.
160 for (int i = 0; i < num; ++i) {
161 (*fields_)[i + start].Delete();
163 // Slide down the remaining fields.
164 for (int i = start + num; i < fields_->size(); ++i) {
165 (*fields_)[i - num] = (*fields_)[i];
167 // Pop off the # of deleted fields.
168 for (int i = 0; i < num; ++i) {
173 void UnknownFieldSet::DeleteByNumber(int number) {
174 if (fields_ == NULL) return;
175 int left = 0; // The number of fields left after deletion.
176 for (int i = 0; i < fields_->size(); ++i) {
177 UnknownField* field = &(*fields_)[i];
178 if (field->number() == number) {
182 (*fields_)[left] = (*fields_)[i];
187 fields_->resize(left);
190 bool UnknownFieldSet::MergeFromCodedStream(io::CodedInputStream* input) {
191 UnknownFieldSet other;
192 if (internal::WireFormat::SkipMessage(input, &other) &&
193 input->ConsumedEntireMessage()) {
201 bool UnknownFieldSet::ParseFromCodedStream(io::CodedInputStream* input) {
203 return MergeFromCodedStream(input);
206 bool UnknownFieldSet::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
207 io::CodedInputStream coded_input(input);
208 return (ParseFromCodedStream(&coded_input) &&
209 coded_input.ConsumedEntireMessage());
212 bool UnknownFieldSet::ParseFromArray(const void* data, int size) {
213 io::ArrayInputStream input(data, size);
214 return ParseFromZeroCopyStream(&input);
217 void UnknownField::Delete() {
219 case UnknownField::TYPE_LENGTH_DELIMITED:
220 delete length_delimited_.string_value_;
222 case UnknownField::TYPE_GROUP:
230 void UnknownField::DeepCopy() {
232 case UnknownField::TYPE_LENGTH_DELIMITED:
233 length_delimited_.string_value_ = new string(
234 *length_delimited_.string_value_);
236 case UnknownField::TYPE_GROUP: {
237 UnknownFieldSet* group = new UnknownFieldSet;
238 group->MergeFrom(*group_);
248 void UnknownField::SerializeLengthDelimitedNoTag(
249 io::CodedOutputStream* output) const {
250 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
251 const string& data = *length_delimited_.string_value_;
252 output->WriteVarint32(data.size());
253 output->WriteRawMaybeAliased(data.data(), data.size());
256 uint8* UnknownField::SerializeLengthDelimitedNoTagToArray(uint8* target) const {
257 GOOGLE_DCHECK_EQ(TYPE_LENGTH_DELIMITED, type());
258 const string& data = *length_delimited_.string_value_;
259 target = io::CodedOutputStream::WriteVarint32ToArray(data.size(), target);
260 target = io::CodedOutputStream::WriteStringToArray(data, target);
264 } // namespace protobuf
265 } // namespace google