Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 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 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 31 | #import "GPBUnknownField_PackagePrivate.h" |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 32 | |
| 33 | #import "GPBArray.h" |
Thomas Van Lenten | 36650a0 | 2016-03-07 12:07:03 -0500 | [diff] [blame] | 34 | #import "GPBCodedOutputStream_PackagePrivate.h" |
dmaclach | 3389bd9 | 2018-08-06 14:56:38 -0700 | [diff] [blame] | 35 | #import "GPBUnknownFieldSet.h" |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 36 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 37 | @implementation GPBUnknownField { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 38 | @protected |
| 39 | int32_t number_; |
| 40 | GPBUInt64Array *mutableVarintList_; |
| 41 | GPBUInt32Array *mutableFixed32List_; |
| 42 | GPBUInt64Array *mutableFixed64List_; |
Thomas Van Lenten | 2480acb | 2015-11-30 14:38:04 -0500 | [diff] [blame] | 43 | NSMutableArray<NSData*> *mutableLengthDelimitedList_; |
| 44 | NSMutableArray<GPBUnknownFieldSet*> *mutableGroupList_; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 45 | } |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 46 | |
| 47 | @synthesize number = number_; |
| 48 | @synthesize varintList = mutableVarintList_; |
| 49 | @synthesize fixed32List = mutableFixed32List_; |
| 50 | @synthesize fixed64List = mutableFixed64List_; |
| 51 | @synthesize lengthDelimitedList = mutableLengthDelimitedList_; |
| 52 | @synthesize groupList = mutableGroupList_; |
| 53 | |
| 54 | - (instancetype)initWithNumber:(int32_t)number { |
| 55 | if ((self = [super init])) { |
| 56 | number_ = number; |
| 57 | } |
| 58 | return self; |
| 59 | } |
| 60 | |
| 61 | - (void)dealloc { |
| 62 | [mutableVarintList_ release]; |
| 63 | [mutableFixed32List_ release]; |
| 64 | [mutableFixed64List_ release]; |
| 65 | [mutableLengthDelimitedList_ release]; |
| 66 | [mutableGroupList_ release]; |
| 67 | |
| 68 | [super dealloc]; |
| 69 | } |
| 70 | |
Thomas Van Lenten | c8a440d | 2016-05-25 13:46:00 -0400 | [diff] [blame] | 71 | // Direct access is use for speed, to avoid even internally declaring things |
| 72 | // read/write, etc. The warning is enabled in the project to ensure code calling |
| 73 | // protos can turn on -Wdirect-ivar-access without issues. |
| 74 | #pragma clang diagnostic push |
| 75 | #pragma clang diagnostic ignored "-Wdirect-ivar-access" |
| 76 | |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 77 | - (id)copyWithZone:(NSZone *)zone { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 78 | GPBUnknownField *result = |
| 79 | [[GPBUnknownField allocWithZone:zone] initWithNumber:number_]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 80 | result->mutableFixed32List_ = [mutableFixed32List_ copyWithZone:zone]; |
| 81 | result->mutableFixed64List_ = [mutableFixed64List_ copyWithZone:zone]; |
| 82 | result->mutableLengthDelimitedList_ = |
Thomas Van Lenten | 3f2dcae | 2017-10-02 19:56:25 -0400 | [diff] [blame] | 83 | [mutableLengthDelimitedList_ mutableCopyWithZone:zone]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 84 | result->mutableVarintList_ = [mutableVarintList_ copyWithZone:zone]; |
| 85 | if (mutableGroupList_.count) { |
| 86 | result->mutableGroupList_ = [[NSMutableArray allocWithZone:zone] |
| 87 | initWithCapacity:mutableGroupList_.count]; |
| 88 | for (GPBUnknownFieldSet *group in mutableGroupList_) { |
| 89 | GPBUnknownFieldSet *copied = [group copyWithZone:zone]; |
| 90 | [result->mutableGroupList_ addObject:copied]; |
| 91 | [copied release]; |
| 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | - (BOOL)isEqual:(id)object { |
| 98 | if (self == object) return YES; |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 99 | if (![object isKindOfClass:[GPBUnknownField class]]) return NO; |
| 100 | GPBUnknownField *field = (GPBUnknownField *)object; |
Thomas Van Lenten | a274c67 | 2017-10-03 09:56:54 -0400 | [diff] [blame] | 101 | if (number_ != field->number_) return NO; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 102 | BOOL equalVarint = |
| 103 | (mutableVarintList_.count == 0 && field->mutableVarintList_.count == 0) || |
| 104 | [mutableVarintList_ isEqual:field->mutableVarintList_]; |
| 105 | if (!equalVarint) return NO; |
| 106 | BOOL equalFixed32 = (mutableFixed32List_.count == 0 && |
| 107 | field->mutableFixed32List_.count == 0) || |
| 108 | [mutableFixed32List_ isEqual:field->mutableFixed32List_]; |
| 109 | if (!equalFixed32) return NO; |
| 110 | BOOL equalFixed64 = (mutableFixed64List_.count == 0 && |
| 111 | field->mutableFixed64List_.count == 0) || |
| 112 | [mutableFixed64List_ isEqual:field->mutableFixed64List_]; |
| 113 | if (!equalFixed64) return NO; |
| 114 | BOOL equalLDList = |
| 115 | (mutableLengthDelimitedList_.count == 0 && |
| 116 | field->mutableLengthDelimitedList_.count == 0) || |
| 117 | [mutableLengthDelimitedList_ isEqual:field->mutableLengthDelimitedList_]; |
| 118 | if (!equalLDList) return NO; |
| 119 | BOOL equalGroupList = |
| 120 | (mutableGroupList_.count == 0 && field->mutableGroupList_.count == 0) || |
| 121 | [mutableGroupList_ isEqual:field->mutableGroupList_]; |
| 122 | if (!equalGroupList) return NO; |
| 123 | return YES; |
| 124 | } |
| 125 | |
| 126 | - (NSUInteger)hash { |
| 127 | // Just mix the hashes of the possible sub arrays. |
| 128 | const int prime = 31; |
| 129 | NSUInteger result = prime + [mutableVarintList_ hash]; |
| 130 | result = prime * result + [mutableFixed32List_ hash]; |
| 131 | result = prime * result + [mutableFixed64List_ hash]; |
| 132 | result = prime * result + [mutableLengthDelimitedList_ hash]; |
| 133 | result = prime * result + [mutableGroupList_ hash]; |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | - (void)writeToOutput:(GPBCodedOutputStream *)output { |
| 138 | NSUInteger count = mutableVarintList_.count; |
| 139 | if (count > 0) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 140 | [output writeUInt64Array:number_ values:mutableVarintList_ tag:0]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 141 | } |
| 142 | count = mutableFixed32List_.count; |
| 143 | if (count > 0) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 144 | [output writeFixed32Array:number_ values:mutableFixed32List_ tag:0]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 145 | } |
| 146 | count = mutableFixed64List_.count; |
| 147 | if (count > 0) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 148 | [output writeFixed64Array:number_ values:mutableFixed64List_ tag:0]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 149 | } |
| 150 | count = mutableLengthDelimitedList_.count; |
| 151 | if (count > 0) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 152 | [output writeBytesArray:number_ values:mutableLengthDelimitedList_]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 153 | } |
| 154 | count = mutableGroupList_.count; |
| 155 | if (count > 0) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 156 | [output writeUnknownGroupArray:number_ values:mutableGroupList_]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | - (size_t)serializedSize { |
| 161 | __block size_t result = 0; |
| 162 | int32_t number = number_; |
| 163 | [mutableVarintList_ |
| 164 | enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) { |
| 165 | #pragma unused(idx, stop) |
| 166 | result += GPBComputeUInt64Size(number, value); |
| 167 | }]; |
| 168 | |
| 169 | [mutableFixed32List_ |
| 170 | enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) { |
| 171 | #pragma unused(idx, stop) |
| 172 | result += GPBComputeFixed32Size(number, value); |
| 173 | }]; |
| 174 | |
| 175 | [mutableFixed64List_ |
| 176 | enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) { |
| 177 | #pragma unused(idx, stop) |
| 178 | result += GPBComputeFixed64Size(number, value); |
| 179 | }]; |
| 180 | |
| 181 | for (NSData *data in mutableLengthDelimitedList_) { |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 182 | result += GPBComputeBytesSize(number, data); |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | for (GPBUnknownFieldSet *set in mutableGroupList_) { |
| 186 | result += GPBComputeUnknownGroupSize(number, set); |
| 187 | } |
| 188 | |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | - (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output { |
| 193 | for (NSData *data in mutableLengthDelimitedList_) { |
| 194 | [output writeRawMessageSetExtension:number_ value:data]; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | - (size_t)serializedSizeAsMessageSetExtension { |
| 199 | size_t result = 0; |
| 200 | for (NSData *data in mutableLengthDelimitedList_) { |
| 201 | result += GPBComputeRawMessageSetExtensionSize(number_, data); |
| 202 | } |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | - (NSString *)description { |
Thomas Van Lenten | a274c67 | 2017-10-03 09:56:54 -0400 | [diff] [blame] | 207 | NSMutableString *description = |
| 208 | [NSMutableString stringWithFormat:@"<%@ %p>: Field: %d {\n", |
| 209 | [self class], self, number_]; |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 210 | [mutableVarintList_ |
| 211 | enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) { |
| 212 | #pragma unused(idx, stop) |
| 213 | [description appendFormat:@"\t%llu\n", value]; |
| 214 | }]; |
| 215 | |
| 216 | [mutableFixed32List_ |
| 217 | enumerateValuesWithBlock:^(uint32_t value, NSUInteger idx, BOOL *stop) { |
| 218 | #pragma unused(idx, stop) |
| 219 | [description appendFormat:@"\t%u\n", value]; |
| 220 | }]; |
| 221 | |
| 222 | [mutableFixed64List_ |
| 223 | enumerateValuesWithBlock:^(uint64_t value, NSUInteger idx, BOOL *stop) { |
| 224 | #pragma unused(idx, stop) |
| 225 | [description appendFormat:@"\t%llu\n", value]; |
| 226 | }]; |
| 227 | |
| 228 | for (NSData *data in mutableLengthDelimitedList_) { |
| 229 | [description appendFormat:@"\t%@\n", data]; |
| 230 | } |
| 231 | |
| 232 | for (GPBUnknownFieldSet *set in mutableGroupList_) { |
| 233 | [description appendFormat:@"\t%@\n", set]; |
| 234 | } |
| 235 | [description appendString:@"}"]; |
| 236 | return description; |
| 237 | } |
| 238 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 239 | - (void)mergeFromField:(GPBUnknownField *)other { |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 240 | GPBUInt64Array *otherVarintList = other.varintList; |
| 241 | if (otherVarintList.count > 0) { |
| 242 | if (mutableVarintList_ == nil) { |
| 243 | mutableVarintList_ = [otherVarintList copy]; |
| 244 | } else { |
| 245 | [mutableVarintList_ addValuesFromArray:otherVarintList]; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | GPBUInt32Array *otherFixed32List = other.fixed32List; |
| 250 | if (otherFixed32List.count > 0) { |
| 251 | if (mutableFixed32List_ == nil) { |
| 252 | mutableFixed32List_ = [otherFixed32List copy]; |
| 253 | } else { |
| 254 | [mutableFixed32List_ addValuesFromArray:otherFixed32List]; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | GPBUInt64Array *otherFixed64List = other.fixed64List; |
| 259 | if (otherFixed64List.count > 0) { |
| 260 | if (mutableFixed64List_ == nil) { |
| 261 | mutableFixed64List_ = [otherFixed64List copy]; |
| 262 | } else { |
| 263 | [mutableFixed64List_ addValuesFromArray:otherFixed64List]; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | NSArray *otherLengthDelimitedList = other.lengthDelimitedList; |
| 268 | if (otherLengthDelimitedList.count > 0) { |
| 269 | if (mutableLengthDelimitedList_ == nil) { |
| 270 | mutableLengthDelimitedList_ = [otherLengthDelimitedList mutableCopy]; |
| 271 | } else { |
| 272 | [mutableLengthDelimitedList_ |
| 273 | addObjectsFromArray:otherLengthDelimitedList]; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | NSArray *otherGroupList = other.groupList; |
| 278 | if (otherGroupList.count > 0) { |
| 279 | if (mutableGroupList_ == nil) { |
| 280 | mutableGroupList_ = |
| 281 | [[NSMutableArray alloc] initWithCapacity:otherGroupList.count]; |
| 282 | } |
| 283 | // Make our own mutable copies. |
| 284 | for (GPBUnknownFieldSet *group in otherGroupList) { |
| 285 | GPBUnknownFieldSet *copied = [group copy]; |
| 286 | [mutableGroupList_ addObject:copied]; |
| 287 | [copied release]; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | - (void)addVarint:(uint64_t)value { |
| 293 | if (mutableVarintList_ == nil) { |
| 294 | mutableVarintList_ = [[GPBUInt64Array alloc] initWithValues:&value count:1]; |
| 295 | } else { |
| 296 | [mutableVarintList_ addValue:value]; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | - (void)addFixed32:(uint32_t)value { |
| 301 | if (mutableFixed32List_ == nil) { |
| 302 | mutableFixed32List_ = |
| 303 | [[GPBUInt32Array alloc] initWithValues:&value count:1]; |
| 304 | } else { |
| 305 | [mutableFixed32List_ addValue:value]; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | - (void)addFixed64:(uint64_t)value { |
| 310 | if (mutableFixed64List_ == nil) { |
| 311 | mutableFixed64List_ = |
| 312 | [[GPBUInt64Array alloc] initWithValues:&value count:1]; |
| 313 | } else { |
| 314 | [mutableFixed64List_ addValue:value]; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | - (void)addLengthDelimited:(NSData *)value { |
| 319 | if (mutableLengthDelimitedList_ == nil) { |
| 320 | mutableLengthDelimitedList_ = |
| 321 | [[NSMutableArray alloc] initWithObjects:&value count:1]; |
| 322 | } else { |
| 323 | [mutableLengthDelimitedList_ addObject:value]; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | - (void)addGroup:(GPBUnknownFieldSet *)value { |
| 328 | if (mutableGroupList_ == nil) { |
| 329 | mutableGroupList_ = [[NSMutableArray alloc] initWithObjects:&value count:1]; |
| 330 | } else { |
| 331 | [mutableGroupList_ addObject:value]; |
| 332 | } |
| 333 | } |
| 334 | |
Thomas Van Lenten | c8a440d | 2016-05-25 13:46:00 -0400 | [diff] [blame] | 335 | #pragma clang diagnostic pop |
| 336 | |
Thomas Van Lenten | 30650d8 | 2015-05-01 08:57:16 -0400 | [diff] [blame] | 337 | @end |