@@ -28,27 +28,55 @@ #import "OFOutOfRangeException.h" @implementation OFASN1ObjectIdentifier @synthesize subidentifiers = _subidentifiers; -- (instancetype)init ++ (instancetype)objectIdentifierWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers +{ + return [[[self alloc] + initWithSubidentifiers: subidentifiers] autorelease]; +} + +- (instancetype)initWithSubidentifiers: + (OFArray OF_GENERIC(OFNumber *) *)subidentifiers { - OF_INVALID_INIT_METHOD + self = [super init]; + + @try { + if ([subidentifiers count] < 1) + @throw [OFInvalidFormatException exception]; + + switch ([[subidentifiers objectAtIndex: 0] intMaxValue]) { + case 0: + case 1: + case 2: + break; + default: + @throw [OFInvalidFormatException exception]; + } + + _subidentifiers = [subidentifiers copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; } - (instancetype)initWithTagClass: (of_asn1_tag_class_t)tagClass tagNumber: (of_asn1_tag_number_t)tagNumber constructed: (bool)constructed DEREncodedContents: (OFData *)DEREncodedContents { - self = [super init]; + void *pool = objc_autoreleasePoolPush(); + OFMutableArray OF_GENERIC(OFNumber *) *subidentifiers; @try { - void *pool = objc_autoreleasePoolPush(); const unsigned char *items = [DEREncodedContents items]; size_t count = [DEREncodedContents count]; - OFMutableArray *subidentifiers = [OFMutableArray array]; uintmax_t value = 0; uint_fast8_t bits = 0; if (tagClass != OF_ASN1_TAG_CLASS_UNIVERSAL || tagNumber != OF_ASN1_TAG_NUMBER_OBJECT_IDENTIFIER || @@ -55,10 +83,12 @@ constructed) @throw [OFInvalidArgumentException exception]; if ([DEREncodedContents itemSize] != 1 || count == 0) @throw [OFInvalidArgumentException exception]; + + subidentifiers = [OFMutableArray array]; for (size_t i = 0; i < count; i++) { if (bits == 0 && items[i] == 0x80) @throw [OFInvalidFormatException exception]; @@ -95,20 +125,26 @@ if (items[count - 1] & 0x80) @throw [OFInvalidFormatException exception]; [subidentifiers makeImmutable]; - _subidentifiers = [subidentifiers copy]; - - objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } + self = [self initWithSubidentifiers: subidentifiers]; + + objc_autoreleasePoolPop(pool); + return self; } + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} - (void)dealloc { [_subidentifiers release];