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 #import "GPBUnknownFieldSet_PackagePrivate.h"
33 #import "GPBCodedInputStream_PackagePrivate.h"
34 #import "GPBCodedOutputStream.h"
35 #import "GPBUnknownField_PackagePrivate.h"
36 #import "GPBUtilities.h"
37 #import "GPBWireFormat.h"
41 static void checkNumber(int32_t number) {
43 [NSException raise:NSInvalidArgumentException
44 format:@"Zero is not a valid field number."];
48 @implementation GPBUnknownFieldSet {
50 CFMutableDictionaryRef fields_;
53 static void CopyWorker(const void *key, const void *value, void *context) {
55 GPBUnknownField *field = value;
56 GPBUnknownFieldSet *result = context;
58 GPBUnknownField *copied = [field copy];
59 [result addField:copied];
63 // Direct access is use for speed, to avoid even internally declaring things
64 // read/write, etc. The warning is enabled in the project to ensure code calling
65 // protos can turn on -Wdirect-ivar-access without issues.
66 #pragma clang diagnostic push
67 #pragma clang diagnostic ignored "-Wdirect-ivar-access"
69 - (id)copyWithZone:(NSZone *)zone {
70 GPBUnknownFieldSet *result = [[GPBUnknownFieldSet allocWithZone:zone] init];
72 CFDictionaryApplyFunction(fields_, CopyWorker, result);
84 - (BOOL)isEqual:(id)object {
86 if ([object isKindOfClass:[GPBUnknownFieldSet class]]) {
87 GPBUnknownFieldSet *set = (GPBUnknownFieldSet *)object;
88 if ((fields_ == NULL) && (set->fields_ == NULL)) {
90 } else if ((fields_ != NULL) && (set->fields_ != NULL)) {
91 equal = CFEqual(fields_, set->fields_);
98 // Return the hash of the fields dictionary (or just some value).
100 return CFHash(fields_);
102 return (NSUInteger)[GPBUnknownFieldSet class];
105 #pragma mark - Public Methods
107 - (BOOL)hasField:(int32_t)number {
108 ssize_t key = number;
109 return fields_ ? (CFDictionaryGetValue(fields_, (void *)key) != nil) : NO;
112 - (GPBUnknownField *)getField:(int32_t)number {
113 ssize_t key = number;
114 GPBUnknownField *result =
115 fields_ ? CFDictionaryGetValue(fields_, (void *)key) : nil;
119 - (NSUInteger)countOfFields {
120 return fields_ ? CFDictionaryGetCount(fields_) : 0;
123 - (NSArray *)sortedFields {
124 if (!fields_) return [NSArray array];
125 size_t count = CFDictionaryGetCount(fields_);
127 GPBUnknownField *values[count];
128 CFDictionaryGetKeysAndValues(fields_, (const void **)keys,
129 (const void **)values);
130 struct GPBFieldPair {
132 GPBUnknownField *value;
134 for (size_t i = 0; i < count; ++i) {
135 pairs[i].key = keys[i];
136 pairs[i].value = values[i];
138 qsort_b(pairs, count, sizeof(struct GPBFieldPair),
139 ^(const void *first, const void *second) {
140 const struct GPBFieldPair *a = first;
141 const struct GPBFieldPair *b = second;
142 return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
144 for (size_t i = 0; i < count; ++i) {
145 values[i] = pairs[i].value;
147 return [NSArray arrayWithObjects:values count:count];
150 #pragma mark - Internal Methods
152 - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)output {
153 if (!fields_) return;
154 size_t count = CFDictionaryGetCount(fields_);
156 GPBUnknownField *values[count];
157 CFDictionaryGetKeysAndValues(fields_, (const void **)keys,
158 (const void **)values);
160 struct GPBFieldPair {
162 GPBUnknownField *value;
165 for (size_t i = 0; i < count; ++i) {
166 pairs[i].key = keys[i];
167 pairs[i].value = values[i];
169 qsort_b(pairs, count, sizeof(struct GPBFieldPair),
170 ^(const void *first, const void *second) {
171 const struct GPBFieldPair *a = first;
172 const struct GPBFieldPair *b = second;
173 return (a->key > b->key) ? 1 : ((a->key == b->key) ? 0 : -1);
175 for (size_t i = 0; i < count; ++i) {
176 GPBUnknownField *value = pairs[i].value;
177 [value writeToOutput:output];
180 [values[0] writeToOutput:output];
184 - (NSString *)description {
185 NSMutableString *description = [NSMutableString
186 stringWithFormat:@"<%@ %p>: TextFormat: {\n", [self class], self];
187 NSString *textFormat = GPBTextFormatForUnknownFieldSet(self, @" ");
188 [description appendString:textFormat];
189 [description appendString:@"}"];
193 static void GPBUnknownFieldSetSerializedSize(const void *key, const void *value,
196 GPBUnknownField *field = value;
197 size_t *result = context;
198 *result += [field serializedSize];
201 - (size_t)serializedSize {
204 CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetSerializedSize,
210 static void GPBUnknownFieldSetWriteAsMessageSetTo(const void *key,
214 GPBUnknownField *field = value;
215 GPBCodedOutputStream *output = context;
216 [field writeAsMessageSetExtensionToOutput:output];
219 - (void)writeAsMessageSetTo:(GPBCodedOutputStream *)output {
221 CFDictionaryApplyFunction(fields_, GPBUnknownFieldSetWriteAsMessageSetTo,
226 static void GPBUnknownFieldSetSerializedSizeAsMessageSet(const void *key,
230 GPBUnknownField *field = value;
231 size_t *result = context;
232 *result += [field serializedSizeAsMessageSetExtension];
235 - (size_t)serializedSizeAsMessageSet {
238 CFDictionaryApplyFunction(
239 fields_, GPBUnknownFieldSetSerializedSizeAsMessageSet, &result);
245 NSMutableData *data = [NSMutableData dataWithLength:self.serializedSize];
246 GPBCodedOutputStream *output =
247 [[GPBCodedOutputStream alloc] initWithData:data];
248 [self writeToCodedOutputStream:output];
253 + (BOOL)isFieldTag:(int32_t)tag {
254 return GPBWireFormatGetTagWireType(tag) != GPBWireFormatEndGroup;
257 - (void)addField:(GPBUnknownField *)field {
258 int32_t number = [field number];
261 // Use a custom dictionary here because the keys are numbers and conversion
262 // back and forth from NSNumber isn't worth the cost.
263 fields_ = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL,
264 &kCFTypeDictionaryValueCallBacks);
266 ssize_t key = number;
267 CFDictionarySetValue(fields_, (const void *)key, field);
270 - (GPBUnknownField *)mutableFieldForNumber:(int32_t)number create:(BOOL)create {
271 ssize_t key = number;
272 GPBUnknownField *existing =
273 fields_ ? CFDictionaryGetValue(fields_, (const void *)key) : nil;
274 if (!existing && create) {
275 existing = [[GPBUnknownField alloc] initWithNumber:number];
276 // This retains existing.
277 [self addField:existing];
283 static void GPBUnknownFieldSetMergeUnknownFields(const void *key,
287 GPBUnknownField *field = value;
288 GPBUnknownFieldSet *self = context;
290 int32_t number = [field number];
292 GPBUnknownField *oldField = [self mutableFieldForNumber:number create:NO];
294 [oldField mergeFromField:field];
296 // Merge only comes from GPBMessage's mergeFrom:, so it means we are on
297 // mutable message and are an mutable instance, so make sure we need
299 GPBUnknownField *fieldCopy = [field copy];
300 [self addField:fieldCopy];
305 - (void)mergeUnknownFields:(GPBUnknownFieldSet *)other {
306 if (other && other->fields_) {
307 CFDictionaryApplyFunction(other->fields_,
308 GPBUnknownFieldSetMergeUnknownFields, self);
312 - (void)mergeFromData:(NSData *)data {
313 GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
314 [self mergeFromCodedInputStream:input];
315 [input checkLastTagWas:0];
319 - (void)mergeVarintField:(int32_t)number value:(int32_t)value {
321 [[self mutableFieldForNumber:number create:YES] addVarint:value];
324 - (BOOL)mergeFieldFrom:(int32_t)tag input:(GPBCodedInputStream *)input {
325 NSAssert(GPBWireFormatIsValidTag(tag), @"Got passed an invalid tag");
326 int32_t number = GPBWireFormatGetTagFieldNumber(tag);
327 GPBCodedInputStreamState *state = &input->state_;
328 switch (GPBWireFormatGetTagWireType(tag)) {
329 case GPBWireFormatVarint: {
330 GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
331 [field addVarint:GPBCodedInputStreamReadInt64(state)];
334 case GPBWireFormatFixed64: {
335 GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
336 [field addFixed64:GPBCodedInputStreamReadFixed64(state)];
339 case GPBWireFormatLengthDelimited: {
340 NSData *data = GPBCodedInputStreamReadRetainedBytes(state);
341 GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
342 [field addLengthDelimited:data];
346 case GPBWireFormatStartGroup: {
347 GPBUnknownFieldSet *unknownFieldSet = [[GPBUnknownFieldSet alloc] init];
348 [input readUnknownGroup:number message:unknownFieldSet];
349 GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
350 [field addGroup:unknownFieldSet];
351 [unknownFieldSet release];
354 case GPBWireFormatEndGroup:
356 case GPBWireFormatFixed32: {
357 GPBUnknownField *field = [self mutableFieldForNumber:number create:YES];
358 [field addFixed32:GPBCodedInputStreamReadFixed32(state)];
364 - (void)mergeMessageSetMessage:(int32_t)number data:(NSData *)messageData {
365 [[self mutableFieldForNumber:number create:YES]
366 addLengthDelimited:messageData];
369 - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data {
370 GPBUnknownField *field = [self mutableFieldForNumber:fieldNum create:YES];
371 [field addLengthDelimited:data];
374 - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input {
376 int32_t tag = GPBCodedInputStreamReadTag(&input->state_);
377 if (tag == 0 || ![self mergeFieldFrom:tag input:input]) {
383 - (void)getTags:(int32_t *)tags {
384 if (!fields_) return;
385 size_t count = CFDictionaryGetCount(fields_);
387 CFDictionaryGetKeysAndValues(fields_, (const void **)keys, NULL);
388 for (size_t i = 0; i < count; ++i) {
389 tags[i] = (int32_t)keys[i];
393 #pragma clang diagnostic pop