forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPBUnknownField.m
482 lines (433 loc) · 16.7 KB
/
GPBUnknownField.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#import "GPBUnknownField.h"
#import "GPBArray.h"
#import "GPBCodedOutputStream_PackagePrivate.h"
#import "GPBUnknownFieldSet.h"
#import "GPBUnknownField_PackagePrivate.h"
#import "GPBUnknownFields_PackagePrivate.h"
#import "GPBWireFormat.h"
#define ASSERT_FIELD_TYPE(type) \
if (type_ != type) { \
[NSException raise:NSInternalInconsistencyException \
format:@"GPBUnknownField is the wrong type"]; \
}
@implementation GPBUnknownField
@synthesize number = number_;
@synthesize type = type_;
- (instancetype)initWithNumber:(int32_t)number {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeLegacy;
}
return self;
}
- (instancetype)initWithNumber:(int32_t)number varint:(uint64_t)varint {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeVarint;
storage_.intValue = varint;
}
return self;
}
- (instancetype)initWithNumber:(int32_t)number fixed32:(uint32_t)fixed32 {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeFixed32;
storage_.intValue = fixed32;
}
return self;
}
- (instancetype)initWithNumber:(int32_t)number fixed64:(uint64_t)fixed64 {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeFixed64;
storage_.intValue = fixed64;
}
return self;
}
- (instancetype)initWithNumber:(int32_t)number lengthDelimited:(nonnull NSData *)data {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeLengthDelimited;
storage_.lengthDelimited = [data copy];
}
return self;
}
- (instancetype)initWithNumber:(int32_t)number group:(nonnull GPBUnknownFields *)group {
if ((self = [super init])) {
number_ = number;
type_ = GPBUnknownFieldTypeGroup;
// Taking ownership of the group; so retain, not copy.
storage_.group = [group retain];
}
return self;
}
- (void)dealloc {
switch (type_) {
case GPBUnknownFieldTypeVarint:
case GPBUnknownFieldTypeFixed32:
case GPBUnknownFieldTypeFixed64:
break;
case GPBUnknownFieldTypeLengthDelimited:
[storage_.lengthDelimited release];
break;
case GPBUnknownFieldTypeGroup:
[storage_.group release];
break;
case GPBUnknownFieldTypeLegacy:
[storage_.legacy.mutableVarintList release];
[storage_.legacy.mutableFixed32List release];
[storage_.legacy.mutableFixed64List release];
[storage_.legacy.mutableLengthDelimitedList release];
[storage_.legacy.mutableGroupList release];
break;
}
[super dealloc];
}
// Direct access is use for speed, to avoid even internally declaring things
// read/write, etc. The warning is enabled in the project to ensure code calling
// protos can turn on -Wdirect-ivar-access without issues.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
- (uint64_t)varint {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeVarint);
return storage_.intValue;
}
- (uint32_t)fixed32 {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeFixed32);
return (uint32_t)storage_.intValue;
}
- (uint64_t)fixed64 {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeFixed64);
return storage_.intValue;
}
- (NSData *)lengthDelimited {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLengthDelimited);
return storage_.lengthDelimited;
}
- (GPBUnknownFields *)group {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeGroup);
return storage_.group;
}
- (GPBUInt64Array *)varintList {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
return storage_.legacy.mutableVarintList;
}
- (GPBUInt32Array *)fixed32List {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
return storage_.legacy.mutableFixed32List;
}
- (GPBUInt64Array *)fixed64List {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
return storage_.legacy.mutableFixed64List;
}
- (NSArray<NSData *> *)lengthDelimitedList {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
return storage_.legacy.mutableLengthDelimitedList;
}
- (NSArray<GPBUnknownFieldSet *> *)groupList {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
return storage_.legacy.mutableGroupList;
}
- (id)copyWithZone:(NSZone *)zone {
switch (type_) {
case GPBUnknownFieldTypeVarint:
case GPBUnknownFieldTypeFixed32:
case GPBUnknownFieldTypeFixed64:
case GPBUnknownFieldTypeLengthDelimited:
case GPBUnknownFieldTypeGroup:
// In these modes, the object isn't mutable, so just return self.
return [self retain];
case GPBUnknownFieldTypeLegacy: {
GPBUnknownField *result = [[GPBUnknownField allocWithZone:zone] initWithNumber:number_];
result->storage_.legacy.mutableFixed32List =
[storage_.legacy.mutableFixed32List copyWithZone:zone];
result->storage_.legacy.mutableFixed64List =
[storage_.legacy.mutableFixed64List copyWithZone:zone];
result->storage_.legacy.mutableLengthDelimitedList =
[storage_.legacy.mutableLengthDelimitedList mutableCopyWithZone:zone];
result->storage_.legacy.mutableVarintList =
[storage_.legacy.mutableVarintList copyWithZone:zone];
if (storage_.legacy.mutableGroupList.count) {
result->storage_.legacy.mutableGroupList = [[NSMutableArray allocWithZone:zone]
initWithCapacity:storage_.legacy.mutableGroupList.count];
for (GPBUnknownFieldSet *group in storage_.legacy.mutableGroupList) {
GPBUnknownFieldSet *copied = [group copyWithZone:zone];
[result->storage_.legacy.mutableGroupList addObject:copied];
[copied release];
}
}
return result;
}
}
}
- (BOOL)isEqual:(id)object {
if (self == object) return YES;
if (![object isKindOfClass:[GPBUnknownField class]]) return NO;
GPBUnknownField *field = (GPBUnknownField *)object;
if (number_ != field->number_) return NO;
if (type_ != field->type_) return NO;
switch (type_) {
case GPBUnknownFieldTypeVarint:
case GPBUnknownFieldTypeFixed32:
case GPBUnknownFieldTypeFixed64:
return storage_.intValue == field->storage_.intValue;
case GPBUnknownFieldTypeLengthDelimited:
return [storage_.lengthDelimited isEqual:field->storage_.lengthDelimited];
case GPBUnknownFieldTypeGroup:
return [storage_.group isEqual:field->storage_.group];
case GPBUnknownFieldTypeLegacy: {
BOOL equalVarint =
(storage_.legacy.mutableVarintList.count == 0 &&
field->storage_.legacy.mutableVarintList.count == 0) ||
[storage_.legacy.mutableVarintList isEqual:field->storage_.legacy.mutableVarintList];
if (!equalVarint) return NO;
BOOL equalFixed32 =
(storage_.legacy.mutableFixed32List.count == 0 &&
field->storage_.legacy.mutableFixed32List.count == 0) ||
[storage_.legacy.mutableFixed32List isEqual:field->storage_.legacy.mutableFixed32List];
if (!equalFixed32) return NO;
BOOL equalFixed64 =
(storage_.legacy.mutableFixed64List.count == 0 &&
field->storage_.legacy.mutableFixed64List.count == 0) ||
[storage_.legacy.mutableFixed64List isEqual:field->storage_.legacy.mutableFixed64List];
if (!equalFixed64) return NO;
BOOL equalLDList = (storage_.legacy.mutableLengthDelimitedList.count == 0 &&
field->storage_.legacy.mutableLengthDelimitedList.count == 0) ||
[storage_.legacy.mutableLengthDelimitedList
isEqual:field->storage_.legacy.mutableLengthDelimitedList];
if (!equalLDList) return NO;
BOOL equalGroupList =
(storage_.legacy.mutableGroupList.count == 0 &&
field->storage_.legacy.mutableGroupList.count == 0) ||
[storage_.legacy.mutableGroupList isEqual:field->storage_.legacy.mutableGroupList];
if (!equalGroupList) return NO;
return YES;
}
}
}
- (NSUInteger)hash {
const int prime = 31;
NSUInteger result = prime * number_ + type_;
switch (type_) {
case GPBUnknownFieldTypeVarint:
case GPBUnknownFieldTypeFixed32:
case GPBUnknownFieldTypeFixed64:
result = prime * result + (NSUInteger)storage_.intValue;
break;
case GPBUnknownFieldTypeLengthDelimited:
result = prime * result + [storage_.lengthDelimited hash];
break;
case GPBUnknownFieldTypeGroup:
result = prime * result + [storage_.group hash];
case GPBUnknownFieldTypeLegacy:
result = prime * result + [storage_.legacy.mutableVarintList hash];
result = prime * result + [storage_.legacy.mutableFixed32List hash];
result = prime * result + [storage_.legacy.mutableFixed64List hash];
result = prime * result + [storage_.legacy.mutableLengthDelimitedList hash];
result = prime * result + [storage_.legacy.mutableGroupList hash];
break;
}
return result;
}
- (void)writeToOutput:(GPBCodedOutputStream *)output {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
NSUInteger count = storage_.legacy.mutableVarintList.count;
if (count > 0) {
[output writeUInt64Array:number_ values:storage_.legacy.mutableVarintList tag:0];
}
count = storage_.legacy.mutableFixed32List.count;
if (count > 0) {
[output writeFixed32Array:number_ values:storage_.legacy.mutableFixed32List tag:0];
}
count = storage_.legacy.mutableFixed64List.count;
if (count > 0) {
[output writeFixed64Array:number_ values:storage_.legacy.mutableFixed64List tag:0];
}
count = storage_.legacy.mutableLengthDelimitedList.count;
if (count > 0) {
[output writeBytesArray:number_ values:storage_.legacy.mutableLengthDelimitedList];
}
count = storage_.legacy.mutableGroupList.count;
if (count > 0) {
[output writeUnknownGroupArray:number_ values:storage_.legacy.mutableGroupList];
}
}
- (size_t)serializedSize {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
__block size_t result = 0;
int32_t number = number_;
[storage_.legacy.mutableVarintList
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeUInt64Size(number, value);
}];
[storage_.legacy.mutableFixed32List
enumerateValuesWithBlock:^(uint32_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeFixed32Size(number, value);
}];
[storage_.legacy.mutableFixed64List
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
result += GPBComputeFixed64Size(number, value);
}];
for (NSData *data in storage_.legacy.mutableLengthDelimitedList) {
result += GPBComputeBytesSize(number, data);
}
for (GPBUnknownFieldSet *set in storage_.legacy.mutableGroupList) {
result += GPBComputeUnknownGroupSize(number, set);
}
return result;
}
- (void)writeAsMessageSetExtensionToOutput:(GPBCodedOutputStream *)output {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
for (NSData *data in storage_.legacy.mutableLengthDelimitedList) {
[output writeRawMessageSetExtension:number_ value:data];
}
}
- (size_t)serializedSizeAsMessageSetExtension {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
size_t result = 0;
for (NSData *data in storage_.legacy.mutableLengthDelimitedList) {
result += GPBComputeRawMessageSetExtensionSize(number_, data);
}
return result;
}
- (NSString *)description {
NSMutableString *description =
[NSMutableString stringWithFormat:@"<%@ %p>: Field: %d", [self class], self, number_];
switch (type_) {
case GPBUnknownFieldTypeVarint:
[description appendFormat:@" varint: %llu", storage_.intValue];
break;
case GPBUnknownFieldTypeFixed32:
[description appendFormat:@" fixed32: %u", (uint32_t)storage_.intValue];
break;
case GPBUnknownFieldTypeFixed64:
[description appendFormat:@" fixed64: %llu", storage_.intValue];
break;
case GPBUnknownFieldTypeLengthDelimited:
[description appendFormat:@" fixed64: %@", storage_.lengthDelimited];
break;
case GPBUnknownFieldTypeGroup:
[description appendFormat:@" group: %@", storage_.group];
break;
case GPBUnknownFieldTypeLegacy:
[description appendString:@" {\n"];
[storage_.legacy.mutableVarintList
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%llu\n", value];
}];
[storage_.legacy.mutableFixed32List
enumerateValuesWithBlock:^(uint32_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%u\n", value];
}];
[storage_.legacy.mutableFixed64List
enumerateValuesWithBlock:^(uint64_t value, __unused NSUInteger idx, __unused BOOL *stop) {
[description appendFormat:@"\t%llu\n", value];
}];
for (NSData *data in storage_.legacy.mutableLengthDelimitedList) {
[description appendFormat:@"\t%@\n", data];
}
for (GPBUnknownFieldSet *set in storage_.legacy.mutableGroupList) {
[description appendFormat:@"\t%@\n", set];
}
[description appendString:@"}"];
break;
}
return description;
}
- (void)mergeFromField:(GPBUnknownField *)other {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
GPBUInt64Array *otherVarintList = other.varintList;
if (otherVarintList.count > 0) {
if (storage_.legacy.mutableVarintList == nil) {
storage_.legacy.mutableVarintList = [otherVarintList copy];
} else {
[storage_.legacy.mutableVarintList addValuesFromArray:otherVarintList];
}
}
GPBUInt32Array *otherFixed32List = other.fixed32List;
if (otherFixed32List.count > 0) {
if (storage_.legacy.mutableFixed32List == nil) {
storage_.legacy.mutableFixed32List = [otherFixed32List copy];
} else {
[storage_.legacy.mutableFixed32List addValuesFromArray:otherFixed32List];
}
}
GPBUInt64Array *otherFixed64List = other.fixed64List;
if (otherFixed64List.count > 0) {
if (storage_.legacy.mutableFixed64List == nil) {
storage_.legacy.mutableFixed64List = [otherFixed64List copy];
} else {
[storage_.legacy.mutableFixed64List addValuesFromArray:otherFixed64List];
}
}
NSArray *otherLengthDelimitedList = other.lengthDelimitedList;
if (otherLengthDelimitedList.count > 0) {
if (storage_.legacy.mutableLengthDelimitedList == nil) {
storage_.legacy.mutableLengthDelimitedList = [otherLengthDelimitedList mutableCopy];
} else {
[storage_.legacy.mutableLengthDelimitedList addObjectsFromArray:otherLengthDelimitedList];
}
}
NSArray *otherGroupList = other.groupList;
if (otherGroupList.count > 0) {
if (storage_.legacy.mutableGroupList == nil) {
storage_.legacy.mutableGroupList =
[[NSMutableArray alloc] initWithCapacity:otherGroupList.count];
}
// Make our own mutable copies.
for (GPBUnknownFieldSet *group in otherGroupList) {
GPBUnknownFieldSet *copied = [group copy];
[storage_.legacy.mutableGroupList addObject:copied];
[copied release];
}
}
}
- (void)addVarint:(uint64_t)value {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
if (storage_.legacy.mutableVarintList == nil) {
storage_.legacy.mutableVarintList = [[GPBUInt64Array alloc] initWithValues:&value count:1];
} else {
[storage_.legacy.mutableVarintList addValue:value];
}
}
- (void)addFixed32:(uint32_t)value {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
if (storage_.legacy.mutableFixed32List == nil) {
storage_.legacy.mutableFixed32List = [[GPBUInt32Array alloc] initWithValues:&value count:1];
} else {
[storage_.legacy.mutableFixed32List addValue:value];
}
}
- (void)addFixed64:(uint64_t)value {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
if (storage_.legacy.mutableFixed64List == nil) {
storage_.legacy.mutableFixed64List = [[GPBUInt64Array alloc] initWithValues:&value count:1];
} else {
[storage_.legacy.mutableFixed64List addValue:value];
}
}
- (void)addLengthDelimited:(NSData *)value {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
if (storage_.legacy.mutableLengthDelimitedList == nil) {
storage_.legacy.mutableLengthDelimitedList = [[NSMutableArray alloc] initWithObjects:&value
count:1];
} else {
[storage_.legacy.mutableLengthDelimitedList addObject:value];
}
}
- (void)addGroup:(GPBUnknownFieldSet *)value {
ASSERT_FIELD_TYPE(GPBUnknownFieldTypeLegacy);
if (storage_.legacy.mutableGroupList == nil) {
storage_.legacy.mutableGroupList = [[NSMutableArray alloc] initWithObjects:&value count:1];
} else {
[storage_.legacy.mutableGroupList addObject:value];
}
}
#pragma clang diagnostic pop
@end