ADD: added new version of protobuf

This commit is contained in:
Henry Winkel
2022-12-20 10:09:28 +01:00
parent 4a79559129
commit 1e2b3dda7b
1513 changed files with 123720 additions and 83381 deletions

View File

@@ -32,9 +32,9 @@
#import <objc/runtime.h>
#import "GPBMessage_PackagePrivate.h"
#import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.h"
#import "GPBMessage_PackagePrivate.h"
// 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
@@ -48,15 +48,16 @@ static const char kParentClassValueKey = 0;
static const char kClassNameSuffixKey = 0;
// Utility function to generate selectors on the fly.
static SEL SelFromStrings(const char *prefix, const char *middle, const char *suffix,
BOOL takesArg) {
static SEL SelFromStrings(const char *prefix, const char *middle,
const char *suffix, BOOL takesArg) {
if (prefix == NULL && suffix == NULL && !takesArg) {
return sel_getUid(middle);
}
const size_t prefixLen = prefix != NULL ? strlen(prefix) : 0;
const size_t middleLen = strlen(middle);
const size_t suffixLen = suffix != NULL ? strlen(suffix) : 0;
size_t totalLen = prefixLen + middleLen + suffixLen + 1; // include space for null on end.
size_t totalLen =
prefixLen + middleLen + suffixLen + 1; // include space for null on end.
if (takesArg) {
totalLen += 1;
}
@@ -81,10 +82,12 @@ static SEL SelFromStrings(const char *prefix, const char *middle, const char *su
return result;
}
static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageFields)
static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
NSArray *allMessageFields)
__attribute__((ns_returns_retained));
static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageFields) {
static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
NSArray *allMessageFields) {
NSMutableArray *result = [[NSMutableArray alloc] init];
for (GPBFieldDescriptor *fieldDesc in allMessageFields) {
if (fieldDesc->description_->hasIndex == hasIndex) {
@@ -108,21 +111,25 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
@synthesize file = file_;
@synthesize wireFormat = wireFormat_;
+ (instancetype)allocDescriptorForClass:(Class)messageClass
rootClass:(Class)rootClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags {
+ (instancetype)
allocDescriptorForClass:(Class)messageClass
rootClass:(Class)rootClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags {
// The rootClass is no longer used, but it is passed in to ensure it
// was started up during initialization also.
(void)rootClass;
NSMutableArray *fields = nil;
GPBFileSyntax syntax = file.syntax;
BOOL fieldsIncludeDefault = (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
BOOL usesClassRefs = (flags & GPBDescriptorInitializationFlag_UsesClassRefs) != 0;
BOOL proto3OptionalKnown = (flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) != 0;
BOOL fieldsIncludeDefault =
(flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
BOOL usesClassRefs =
(flags & GPBDescriptorInitializationFlag_UsesClassRefs) != 0;
BOOL proto3OptionalKnown =
(flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) != 0;
void *desc;
for (uint32_t i = 0; i < fieldCount; ++i) {
@@ -186,9 +193,10 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
for (uint32_t i = 0, hasIndex = firstHasIndex; i < count; ++i, --hasIndex) {
const char *name = oneofNames[i];
NSArray *fieldsForOneof = NewFieldsArrayForHasIndex(hasIndex, fields_);
NSCAssert(fieldsForOneof.count > 0, @"No fields for this oneof? (%s:%d)", name, hasIndex);
GPBOneofDescriptor *oneofDescriptor = [[GPBOneofDescriptor alloc] initWithName:name
fields:fieldsForOneof];
NSCAssert(fieldsForOneof.count > 0,
@"No fields for this oneof? (%s:%d)", name, hasIndex);
GPBOneofDescriptor *oneofDescriptor =
[[GPBOneofDescriptor alloc] initWithName:name fields:fieldsForOneof];
[oneofs addObject:oneofDescriptor];
[oneofDescriptor release];
[fieldsForOneof release];
@@ -202,7 +210,8 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo];
for (GPBFieldDescriptor *fieldDescriptor in fields_) {
if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) {
objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey, extraInfoValue,
objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey,
extraInfoValue,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
@@ -215,7 +224,9 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
}
- (void)setupContainingMessageClass:(Class)messageClass {
objc_setAssociatedObject(self, &kParentClassValueKey, messageClass, OBJC_ASSOCIATION_ASSIGN);
objc_setAssociatedObject(self, &kParentClassValueKey,
messageClass,
OBJC_ASSOCIATION_ASSIGN);
}
- (void)setupContainingMessageClassName:(const char *)msgClassName {
@@ -229,7 +240,9 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
- (void)setupMessageClassNameSuffix:(NSString *)suffix {
if (suffix.length) {
objc_setAssociatedObject(self, &kClassNameSuffixKey, suffix, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &kClassNameSuffixKey,
suffix,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
@@ -247,7 +260,9 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
GPBFileDescriptor *file = self.file;
NSString *objcPrefix = file.objcPrefix;
if (objcPrefix && ![className hasPrefix:objcPrefix]) {
NSAssert(0, @"Class didn't have correct prefix? (%@ - %@)", className, objcPrefix);
NSAssert(0,
@"Class didn't have correct prefix? (%@ - %@)",
className, objcPrefix);
return nil;
}
GPBDescriptor *parent = self.containingType;
@@ -259,16 +274,19 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
NSString *suffix = objc_getAssociatedObject(parent, &kClassNameSuffixKey);
if (suffix) {
if (![parentClassName hasSuffix:suffix]) {
NSAssert(0, @"ParentMessage class didn't have correct suffix? (%@ - %@)", className,
suffix);
NSAssert(0,
@"ParentMessage class didn't have correct suffix? (%@ - %@)",
className, suffix);
return nil;
}
parentClassName = [parentClassName substringToIndex:(parentClassName.length - suffix.length)];
parentClassName =
[parentClassName substringToIndex:(parentClassName.length - suffix.length)];
}
NSString *parentPrefix = [parentClassName stringByAppendingString:@"_"];
if (![className hasPrefix:parentPrefix]) {
NSAssert(0, @"Class didn't have the correct parent name prefix? (%@ - %@)", parentPrefix,
className);
NSAssert(0,
@"Class didn't have the correct parent name prefix? (%@ - %@)",
parentPrefix, className);
return nil;
}
name = [className substringFromIndex:parentPrefix.length];
@@ -280,7 +298,9 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
NSString *suffix = objc_getAssociatedObject(self, &kClassNameSuffixKey);
if (suffix) {
if (![name hasSuffix:suffix]) {
NSAssert(0, @"Message class didn't have correct suffix? (%@ - %@)", name, suffix);
NSAssert(0,
@"Message class didn't have correct suffix? (%@ - %@)",
name, suffix);
return nil;
}
name = [name substringToIndex:(name.length - suffix.length)];
@@ -296,7 +316,8 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
return result;
}
- (id)copyWithZone:(__unused NSZone *)zone {
- (id)copyWithZone:(NSZone *)zone {
#pragma unused(zone)
return [self retain];
}
@@ -351,7 +372,8 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
return self;
}
- (instancetype)initWithPackage:(NSString *)package syntax:(GPBFileSyntax)syntax {
- (instancetype)initWithPackage:(NSString *)package
syntax:(GPBFileSyntax)syntax {
self = [super init];
if (self) {
package_ = [package copy];
@@ -392,7 +414,7 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
}
- (NSString *)name {
return (NSString *_Nonnull)@(name_);
return (NSString * _Nonnull)@(name_);
}
- (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber {
@@ -422,17 +444,19 @@ uint32_t GPBFieldTag(GPBFieldDescriptor *self) {
// Maps are repeated messages on the wire.
format = GPBWireFormatForType(GPBDataTypeMessage, NO);
} else {
format =
GPBWireFormatForType(description->dataType, ((description->flags & GPBFieldPacked) != 0));
format = GPBWireFormatForType(description->dataType,
((description->flags & GPBFieldPacked) != 0));
}
return GPBWireFormatMakeTag(description->number, format);
}
uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
GPBMessageFieldDescription *description = self->description_;
NSCAssert((description->flags & GPBFieldRepeated) != 0, @"Only valid on repeated fields");
NSCAssert((description->flags & GPBFieldRepeated) != 0,
@"Only valid on repeated fields");
GPBWireFormat format =
GPBWireFormatForType(description->dataType, ((description->flags & GPBFieldPacked) == 0));
GPBWireFormatForType(description->dataType,
((description->flags & GPBFieldPacked) == 0));
return GPBWireFormatMakeTag(description->number, format);
}
@@ -492,8 +516,10 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
// - not repeated/map
// - not in a oneof (negative has index)
// - not a message (the flag doesn't make sense for messages)
BOOL clearOnZero = ((syntax == GPBFileSyntaxProto3) && !isMapOrArray &&
(coreDesc->hasIndex >= 0) && !isMessage);
BOOL clearOnZero = ((syntax == GPBFileSyntaxProto3) &&
!isMapOrArray &&
(coreDesc->hasIndex >= 0) &&
!isMessage);
if (clearOnZero) {
coreDesc->flags |= GPBFieldClearHasIvarOnZero;
}
@@ -508,7 +534,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
// It is a single field; it gets has/setHas selectors if...
// - not in a oneof (negative has index)
// - not clearing on zero
if ((coreDesc->hasIndex >= 0) && ((coreDesc->flags & GPBFieldClearHasIvarOnZero) == 0)) {
if ((coreDesc->hasIndex >= 0) &&
((coreDesc->flags & GPBFieldClearHasIvarOnZero) == 0)) {
hasOrCountSel_ = SelFromStrings("has", coreDesc->name, NULL, NO);
setHasSel_ = SelFromStrings("setHas", coreDesc->name, NULL, YES);
}
@@ -529,9 +556,11 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
} else if (dataType == GPBDataTypeEnum) {
if ((coreDesc->flags & GPBFieldHasEnumDescriptor) != 0) {
enumHandling_.enumDescriptor_ = coreDesc->dataTypeSpecific.enumDescFunc();
enumHandling_.enumDescriptor_ =
coreDesc->dataTypeSpecific.enumDescFunc();
} else {
enumHandling_.enumVerifier_ = coreDesc->dataTypeSpecific.enumVerifier;
enumHandling_.enumVerifier_ =
coreDesc->dataTypeSpecific.enumVerifier;
}
}
@@ -547,7 +576,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
memcpy(&length, bytes, sizeof(length));
length = ntohl(length);
bytes += sizeof(length);
defaultValue_.valueData = [[NSData alloc] initWithBytes:bytes length:length];
defaultValue_.valueData =
[[NSData alloc] initWithBytes:bytes length:length];
}
}
}
@@ -556,7 +586,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (void)dealloc {
if (description_->dataType == GPBDataTypeBytes && !(description_->flags & GPBFieldRepeated)) {
if (description_->dataType == GPBDataTypeBytes &&
!(description_->flags & GPBFieldRepeated)) {
[defaultValue_.valueData release];
}
[super dealloc];
@@ -575,7 +606,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (NSString *)name {
return (NSString *_Nonnull)@(description_->name);
return (NSString * _Nonnull)@(description_->name);
}
- (BOOL)isRequired {
@@ -635,7 +666,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (BOOL)isValidEnumValue:(int32_t)value {
NSAssert(description_->dataType == GPBDataTypeEnum, @"Field Must be of type GPBDataTypeEnum");
NSAssert(description_->dataType == GPBDataTypeEnum,
@"Field Must be of type GPBDataTypeEnum");
if (description_->flags & GPBFieldHasEnumDescriptor) {
return enumHandling_.enumDescriptor_.enumVerifier(value);
} else {
@@ -671,17 +703,19 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
- (NSString *)textFormatName {
if ((description_->flags & GPBFieldTextFormatNameCustom) != 0) {
NSValue *extraInfoValue = objc_getAssociatedObject(self, &kTextFormatExtraValueKey);
NSValue *extraInfoValue =
objc_getAssociatedObject(self, &kTextFormatExtraValueKey);
// Support can be left out at generation time.
if (!extraInfoValue) {
return nil;
}
const uint8_t *extraTextFormatInfo = [extraInfoValue pointerValue];
return GPBDecodeTextFormatName(extraTextFormatInfo, GPBFieldNumber(self), self.name);
return GPBDecodeTextFormatName(extraTextFormatInfo, GPBFieldNumber(self),
self.name);
}
// The logic here has to match SetCommonFieldVariables() from
// objectivec/field.cc in the proto compiler.
// objectivec_field.cc in the proto compiler.
NSString *name = self.name;
NSUInteger len = [name length];
@@ -692,7 +726,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
// Remove "Array" from the end for repeated fields.
if (((description_->flags & GPBFieldRepeated) != 0) && [name hasSuffix:@"Array"]) {
if (((description_->flags & GPBFieldRepeated) != 0) &&
[name hasSuffix:@"Array"]) {
name = [name substringToIndex:(len - 5)];
len = [name length];
}
@@ -704,8 +739,9 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
if (firstChar >= 'a' && firstChar <= 'z') {
NSString *firstCharString =
[NSString stringWithFormat:@"%C", (unichar)(firstChar - 'a' + 'A')];
NSString *result = [name stringByReplacingCharactersInRange:NSMakeRange(0, 1)
withString:firstCharString];
NSString *result =
[name stringByReplacingCharactersInRange:NSMakeRange(0, 1)
withString:firstCharString];
return result;
}
return name;
@@ -748,11 +784,12 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
@synthesize name = name_;
@synthesize enumVerifier = enumVerifier_;
+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
+ (instancetype)
allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name
valueNames:valueNames
values:values
@@ -761,12 +798,13 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return descriptor;
}
+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo {
+ (instancetype)
allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo {
// Call the common case.
GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name
valueNames:valueNames
@@ -854,26 +892,27 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName {
if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return NO;
if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return NO;
for (uint32_t i = 0; i < valueCount_; ++i) {
NSString *valueTextFormatName = [self getEnumTextFormatNameForIndex:i];
if ([valueTextFormatName isEqual:textFormatName]) {
if (outValue) {
*outValue = values_[i];
}
return YES;
for (uint32_t i = 0; i < valueCount_; ++i) {
NSString *valueTextFormatName = [self getEnumTextFormatNameForIndex:i];
if ([valueTextFormatName isEqual:textFormatName]) {
if (outValue) {
*outValue = values_[i];
}
return YES;
}
}
}
return NO;
return NO;
}
- (NSString *)textFormatNameForValue:(int32_t)number {
// Find the EnumValue descriptor and its index.
BOOL foundIt = NO;
uint32_t valueDescriptorIndex;
for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_; ++valueDescriptorIndex) {
for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_;
++valueDescriptorIndex) {
if (values_[valueDescriptorIndex] == number) {
foundIt = YES;
break;
@@ -916,9 +955,10 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
// See if it is in the map of special format handling.
if (extraTextFormatInfo_) {
result = GPBDecodeTextFormatName(extraTextFormatInfo_, (int32_t)index, shortName);
result = GPBDecodeTextFormatName(extraTextFormatInfo_,
(int32_t)index, shortName);
}
// Logic here needs to match what objectivec/enum.cc does in the proto
// Logic here needs to match what objectivec_enum.cc does in the proto
// compiler.
if (result == nil) {
NSUInteger len = [shortName length];
@@ -965,14 +1005,16 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
GPBDataType type = description_->dataType;
if (type == GPBDataTypeBytes) {
// Data stored as a length prefixed c-string in descriptor records.
const uint8_t *bytes = (const uint8_t *)description_->defaultValue.valueData;
const uint8_t *bytes =
(const uint8_t *)description_->defaultValue.valueData;
if (bytes) {
uint32_t length;
memcpy(&length, bytes, sizeof(length));
// The length is stored in network byte order.
length = ntohl(length);
bytes += sizeof(length);
defaultValue_.valueData = [[NSData alloc] initWithBytes:bytes length:length];
defaultValue_.valueData =
[[NSData alloc] initWithBytes:bytes length:length];
}
} else if (type == GPBDataTypeMessage || type == GPBDataTypeGroup) {
// The default is looked up in -defaultValue instead since extensions
@@ -990,19 +1032,21 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (void)dealloc {
if ((description_->dataType == GPBDataTypeBytes) && !GPBExtensionIsRepeated(description_)) {
if ((description_->dataType == GPBDataTypeBytes) &&
!GPBExtensionIsRepeated(description_)) {
[defaultValue_.valueData release];
}
[super dealloc];
}
- (instancetype)copyWithZone:(__unused NSZone *)zone {
- (instancetype)copyWithZone:(NSZone *)zone {
#pragma unused(zone)
// Immutable.
return [self retain];
}
- (NSString *)singletonName {
return (NSString *_Nonnull)@(description_->singletonName);
return (NSString * _Nonnull)@(description_->singletonName);
}
- (const char *)singletonNameC {
@@ -1018,12 +1062,15 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (GPBWireFormat)wireType {
return GPBWireFormatForType(description_->dataType, GPBExtensionIsPacked(description_));
return GPBWireFormatForType(description_->dataType,
GPBExtensionIsPacked(description_));
}
- (GPBWireFormat)alternateWireType {
NSAssert(GPBExtensionIsRepeated(description_), @"Only valid on repeated extensions");
return GPBWireFormatForType(description_->dataType, !GPBExtensionIsPacked(description_));
NSAssert(GPBExtensionIsRepeated(description_),
@"Only valid on repeated extensions");
return GPBWireFormatForType(description_->dataType,
!GPBExtensionIsPacked(description_));
}
- (BOOL)isRepeated {
@@ -1079,7 +1126,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return @(defaultValue_.valueUInt64);
case GPBDataTypeBytes:
// Like message fields, the default is zero length data.
return (defaultValue_.valueData ? defaultValue_.valueData : GPBEmptyNSData());
return (defaultValue_.valueData ? defaultValue_.valueData
: GPBEmptyNSData());
case GPBDataTypeString:
// Like message fields, the default is zero length string.
return (defaultValue_.valueString ? defaultValue_.valueString : @"");