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
|
// Protocol Buffers - Google's data interchange format
// Copyright 2024 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 "GPBUnknownFields.h"
#import "GPBUnknownFields_PackagePrivate.h"
#import <Foundation/Foundation.h>
#import "GPBCodedInputStream.h"
#import "GPBCodedInputStream_PackagePrivate.h"
#import "GPBCodedOutputStream.h"
#import "GPBCodedOutputStream_PackagePrivate.h"
#import "GPBDescriptor.h"
#import "GPBMessage.h"
#import "GPBMessage_PackagePrivate.h"
#import "GPBUnknownField.h"
#import "GPBUnknownField_PackagePrivate.h"
#import "GPBWireFormat.h"
#define CHECK_FIELD_NUMBER(number) \
if (number <= 0) { \
[NSException raise:NSInvalidArgumentException format:@"Not a valid field number."]; \
}
// TODO: Consider using on other functions to reduce bloat when
// some compiler optimizations are enabled.
#define GPB_NOINLINE __attribute__((noinline))
@interface GPBUnknownFields () {
@package
NSMutableArray<GPBUnknownField *> *fields_;
}
@end
// 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"
GPB_NOINLINE
static size_t ComputeSerializeSize(GPBUnknownFields *_Nonnull self) {
size_t result = 0;
for (GPBUnknownField *field in self->fields_) {
uint32_t fieldNumber = field->number_;
switch (field->type_) {
case GPBUnknownFieldTypeVarint:
result += GPBComputeUInt64Size(fieldNumber, field->storage_.intValue);
break;
case GPBUnknownFieldTypeFixed32:
result += GPBComputeFixed32Size(fieldNumber, (uint32_t)field->storage_.intValue);
break;
case GPBUnknownFieldTypeFixed64:
result += GPBComputeFixed64Size(fieldNumber, field->storage_.intValue);
break;
case GPBUnknownFieldTypeLengthDelimited:
result += GPBComputeBytesSize(fieldNumber, field->storage_.lengthDelimited);
break;
case GPBUnknownFieldTypeGroup:
result +=
(GPBComputeTagSize(fieldNumber) * 2) + ComputeSerializeSize(field->storage_.group);
break;
}
}
return result;
}
GPB_NOINLINE
static void WriteToCoddedOutputStream(GPBUnknownFields *_Nonnull self,
GPBCodedOutputStream *_Nonnull output) {
for (GPBUnknownField *field in self->fields_) {
uint32_t fieldNumber = field->number_;
switch (field->type_) {
case GPBUnknownFieldTypeVarint:
[output writeUInt64:fieldNumber value:field->storage_.intValue];
break;
case GPBUnknownFieldTypeFixed32:
[output writeFixed32:fieldNumber value:(uint32_t)field->storage_.intValue];
break;
case GPBUnknownFieldTypeFixed64:
[output writeFixed64:fieldNumber value:field->storage_.intValue];
break;
case GPBUnknownFieldTypeLengthDelimited:
[output writeBytes:fieldNumber value:field->storage_.lengthDelimited];
break;
case GPBUnknownFieldTypeGroup:
[output writeRawVarint32:GPBWireFormatMakeTag(fieldNumber, GPBWireFormatStartGroup)];
WriteToCoddedOutputStream(field->storage_.group, output);
[output writeRawVarint32:GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup)];
break;
}
}
}
GPB_NOINLINE
static BOOL MergeFromInputStream(GPBUnknownFields *self, GPBCodedInputStream *input,
uint32_t endTag) {
#if defined(DEBUG) && DEBUG
NSCAssert(endTag == 0 || GPBWireFormatGetTagWireType(endTag) == GPBWireFormatEndGroup,
@"Internal error:Invalid end tag: %u", endTag);
#endif
GPBCodedInputStreamState *state = &input->state_;
NSMutableArray<GPBUnknownField *> *fields = self->fields_;
@try {
while (YES) {
uint32_t tag = GPBCodedInputStreamReadTag(state);
if (tag == endTag) {
return YES;
}
if (tag == 0) {
// Reached end of input without finding the end tag.
return NO;
}
GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
switch (wireType) {
case GPBWireFormatVarint: {
uint64_t value = GPBCodedInputStreamReadInt64(state);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
varint:value];
[fields addObject:field];
[field release];
break;
}
case GPBWireFormatFixed32: {
uint32_t value = GPBCodedInputStreamReadFixed32(state);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
fixed32:value];
[fields addObject:field];
[field release];
break;
}
case GPBWireFormatFixed64: {
uint64_t value = GPBCodedInputStreamReadFixed64(state);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
fixed64:value];
[fields addObject:field];
[field release];
break;
}
case GPBWireFormatLengthDelimited: {
NSData *data = GPBCodedInputStreamReadRetainedBytes(state);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
lengthDelimited:data];
[fields addObject:field];
[field release];
[data release];
break;
}
case GPBWireFormatStartGroup: {
GPBUnknownFields *group = [[GPBUnknownFields alloc] init];
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber group:group];
[fields addObject:field];
[field release];
[group release]; // Still will be held in the field/fields.
uint32_t endGroupTag = GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup);
if (MergeFromInputStream(group, input, endGroupTag)) {
GPBCodedInputStreamCheckLastTagWas(state, endGroupTag);
} else {
[NSException
raise:NSInternalInconsistencyException
format:@"Internal error: Unknown field data for nested group was malformed."];
}
break;
}
case GPBWireFormatEndGroup:
[NSException raise:NSInternalInconsistencyException
format:@"Unexpected end group tag: %u", tag];
break;
}
}
} @catch (NSException *exception) {
#if defined(DEBUG) && DEBUG
NSLog(@"%@: Internal exception while parsing unknown data, this shouldn't happen!: %@",
[self class], exception);
#endif
}
}
@implementation GPBUnknownFields
- (instancetype)initFromMessage:(nonnull GPBMessage *)message {
self = [super init];
if (self) {
fields_ = [[NSMutableArray alloc] init];
// This shouldn't happen with the annotations, but just incase something claiming nonnull
// does return nil, block it.
if (!message) {
[self release];
[NSException raise:NSInvalidArgumentException format:@"Message cannot be nil"];
}
NSData *data = GPBMessageUnknownFieldsData(message);
if (data) {
GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
// Parse until the end of the data (tag will be zero).
if (!MergeFromInputStream(self, input, 0)) {
[input release];
[self release];
[NSException raise:NSInternalInconsistencyException
format:@"Internal error: Unknown field data from message was malformed."];
}
[input release];
}
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
fields_ = [[NSMutableArray alloc] init];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
GPBUnknownFields *copy = [[GPBUnknownFields allocWithZone:zone] init];
copy->fields_ = [[NSMutableArray allocWithZone:zone] initWithArray:fields_ copyItems:YES];
return copy;
}
- (void)dealloc {
[fields_ release];
[super dealloc];
}
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[GPBUnknownFields class]]) {
return NO;
}
GPBUnknownFields *ufs = (GPBUnknownFields *)object;
// The type is defined with order of fields mattering, so just compare the arrays.
return [fields_ isEqual:ufs->fields_];
}
- (NSUInteger)hash {
return [fields_ hash];
}
- (NSString *)description {
return [NSString
stringWithFormat:@"<%@ %p>: %lu fields", [self class], self, (unsigned long)fields_.count];
}
#pragma mark - Public Methods
- (NSUInteger)count {
return fields_.count;
}
- (BOOL)empty {
return fields_.count == 0;
}
- (void)clear {
[fields_ removeAllObjects];
}
- (NSArray<GPBUnknownField *> *)fields:(int32_t)fieldNumber {
CHECK_FIELD_NUMBER(fieldNumber);
NSMutableArray<GPBUnknownField *> *result = [[NSMutableArray alloc] init];
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber) {
[result addObject:field];
}
}
if (result.count == 0) {
[result release];
return nil;
}
return [result autorelease];
}
- (void)addFieldNumber:(int32_t)fieldNumber varint:(uint64_t)value {
CHECK_FIELD_NUMBER(fieldNumber);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber varint:value];
[fields_ addObject:field];
[field release];
}
- (void)addFieldNumber:(int32_t)fieldNumber fixed32:(uint32_t)value {
CHECK_FIELD_NUMBER(fieldNumber);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber fixed32:value];
[fields_ addObject:field];
[field release];
}
- (void)addFieldNumber:(int32_t)fieldNumber fixed64:(uint64_t)value {
CHECK_FIELD_NUMBER(fieldNumber);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber fixed64:value];
[fields_ addObject:field];
[field release];
}
- (void)addFieldNumber:(int32_t)fieldNumber lengthDelimited:(NSData *)value {
CHECK_FIELD_NUMBER(fieldNumber);
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber
lengthDelimited:value];
[fields_ addObject:field];
[field release];
}
- (GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber {
CHECK_FIELD_NUMBER(fieldNumber);
GPBUnknownFields *group = [[GPBUnknownFields alloc] init];
GPBUnknownField *field = [[GPBUnknownField alloc] initWithNumber:fieldNumber group:group];
[fields_ addObject:field];
[field release];
return [group autorelease];
}
- (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field {
GPBUnknownField *result = [field copy];
[fields_ addObject:result];
return [result autorelease];
}
- (void)removeField:(nonnull GPBUnknownField *)field {
NSUInteger count = fields_.count;
[fields_ removeObjectIdenticalTo:field];
if (count == fields_.count) {
[NSException raise:NSInvalidArgumentException format:@"The field was not present."];
}
}
- (void)clearFieldNumber:(int32_t)fieldNumber {
CHECK_FIELD_NUMBER(fieldNumber);
NSMutableIndexSet *toRemove = nil;
NSUInteger idx = 0;
for (GPBUnknownField *field in fields_) {
if (field->number_ == fieldNumber) {
if (toRemove == nil) {
toRemove = [[NSMutableIndexSet alloc] initWithIndex:idx];
} else {
[toRemove addIndex:idx];
}
}
++idx;
}
if (toRemove) {
[fields_ removeObjectsAtIndexes:toRemove];
[toRemove release];
}
}
#pragma mark - NSFastEnumeration protocol
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
objects:(__unsafe_unretained id _Nonnull *)stackbuf
count:(NSUInteger)len {
return [fields_ countByEnumeratingWithState:state objects:stackbuf count:len];
}
#pragma mark - Internal Methods
- (NSData *)serializeAsData {
if (fields_.count == 0) {
return [NSData data];
}
size_t expectedSize = ComputeSerializeSize(self);
NSMutableData *data = [NSMutableData dataWithLength:expectedSize];
GPBCodedOutputStream *stream = [[GPBCodedOutputStream alloc] initWithData:data];
@try {
WriteToCoddedOutputStream(self, stream);
[stream flush];
} @catch (NSException *exception) {
#if defined(DEBUG) && DEBUG
NSLog(@"Internal exception while building GPBUnknownFields serialized data: %@", exception);
#endif
}
#if defined(DEBUG) && DEBUG
NSAssert([stream bytesWritten] == expectedSize, @"Internal error within the library");
#endif
[stream release];
return data;
}
@end
@implementation GPBUnknownFields (AccessHelpers)
- (BOOL)getFirst:(int32_t)fieldNumber varint:(nonnull uint64_t *)outValue {
CHECK_FIELD_NUMBER(fieldNumber);
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeVarint) {
*outValue = field.varint;
return YES;
}
}
return NO;
}
- (BOOL)getFirst:(int32_t)fieldNumber fixed32:(nonnull uint32_t *)outValue {
CHECK_FIELD_NUMBER(fieldNumber);
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeFixed32) {
*outValue = field.fixed32;
return YES;
}
}
return NO;
}
- (BOOL)getFirst:(int32_t)fieldNumber fixed64:(nonnull uint64_t *)outValue {
CHECK_FIELD_NUMBER(fieldNumber);
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeFixed64) {
*outValue = field.fixed64;
return YES;
}
}
return NO;
}
- (nullable NSData *)firstLengthDelimited:(int32_t)fieldNumber {
CHECK_FIELD_NUMBER(fieldNumber);
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeLengthDelimited) {
return field.lengthDelimited;
}
}
return nil;
}
- (nullable GPBUnknownFields *)firstGroup:(int32_t)fieldNumber {
CHECK_FIELD_NUMBER(fieldNumber);
for (GPBUnknownField *field in fields_) {
if (field.number == fieldNumber && field.type == GPBUnknownFieldTypeGroup) {
return field.group;
}
}
return nil;
}
@end
#pragma clang diagnostic pop
|