jens@16: // jens@16: // MYASN1Object.m jens@16: // MYCrypto-iPhone jens@16: // jens@16: // Created by Jens Alfke on 5/28/09. jens@16: // Copyright 2009 Jens Alfke. All rights reserved. jens@16: // jens@16: jens@16: #import "MYASN1Object.h" jens@16: jens@16: jens@16: @implementation MYASN1Object jens@16: jens@16: jens@16: - (id) initWithTag: (uint32_t)tag jens@16: ofClass: (uint8_t)tagClass jens@16: constructed: (BOOL)constructed jens@16: value: (NSData*)value jens@16: { jens@16: Assert(value); jens@16: self = [super init]; jens@16: if (self != nil) { jens@16: _tag = tag; jens@16: _tagClass = tagClass; jens@16: _constructed = constructed; jens@16: _value = [value copy]; jens@16: } jens@16: return self; jens@16: } jens@16: jens@16: - (id) initWithTag: (uint32_t)tag jens@16: ofClass: (uint8_t)tagClass jens@16: components: (NSArray*)components jens@16: { jens@16: Assert(components); jens@16: self = [super init]; jens@16: if (self != nil) { jens@16: _tag = tag; jens@16: _tagClass = tagClass; jens@16: _constructed = YES; jens@16: _components = [components copy]; jens@16: } jens@16: return self; jens@16: } jens@16: jens@16: - (void) dealloc jens@16: { jens@16: [_value release]; jens@16: [_components release]; jens@16: [super dealloc]; jens@16: } jens@16: jens@16: jens@16: @synthesize tag=_tag, tagClass=_tagClass, constructed=_constructed, value=_value, components=_components; jens@16: jens@16: jens@16: - (NSString*)description { jens@16: if (_components) jens@16: return $sprintf(@"%@[%hhu/%u/%u]%@", self.class, _tagClass,(unsigned)_constructed,_tag, _components); jens@16: else jens@16: return $sprintf(@"%@[%hhu/%u/%u, %u bytes]", self.class, _tagClass,(unsigned)_constructed,_tag, _value.length); jens@16: } jens@16: jens@16: static void dump(id object, NSMutableString *output, NSString *indent) { jens@16: if ([object isKindOfClass: [MYASN1Object class]]) { jens@16: MYASN1Object *asn1Obj = object; jens@16: [output appendFormat: @"%@%@[%hhu/%u]", indent, asn1Obj.class, asn1Obj.tagClass,asn1Obj.tag]; jens@16: if (asn1Obj.components) { jens@16: [output appendString: @":\n"]; jens@16: NSString *subindent = [indent stringByAppendingString: @" "]; jens@16: for (id o in asn1Obj.components) jens@16: dump(o,output, subindent); jens@16: } else jens@16: [output appendFormat: @" %@\n", asn1Obj.value]; jens@16: } else if([object respondsToSelector: @selector(objectEnumerator)]) { jens@16: [output appendString: indent]; jens@16: if ([object isKindOfClass: [NSArray class]]) jens@16: [output appendString: @"Sequence:\n"]; jens@16: else if ([object isKindOfClass: [NSSet class]]) jens@16: [output appendString: @"Set:\n"]; jens@16: else jens@16: [output appendFormat: @"%@:\n", [object class]]; jens@16: NSString *subindent = [indent stringByAppendingString: @" "]; jens@16: for (id o in object) jens@16: dump(o,output, subindent); jens@16: } else { jens@16: [output appendFormat: @"%@%@\n", indent, object]; jens@16: } jens@16: } jens@16: jens@16: + (NSString*) dump: (id)object { jens@16: NSMutableString *output = [NSMutableString stringWithCapacity: 512]; jens@16: dump(object,output,@""); jens@16: return output; jens@16: } jens@16: jens@16: jens@16: @end jens@16: jens@16: jens@16: jens@16: @implementation MYASN1BigInteger jens@16: jens@16: @end jens@16: jens@16: jens@16: jens@16: @implementation MYBitString jens@16: jens@16: jens@16: - (id)initWithBits: (NSData*)bits count: (unsigned)bitCount; jens@16: { jens@16: Assert(bits); jens@16: Assert(bitCount <= 8*bits.length); jens@16: self = [super init]; jens@16: if (self != nil) { jens@16: _bits = [bits copy]; jens@16: _bitCount = bitCount; jens@16: } jens@16: return self; jens@16: } jens@16: jens@16: - (void) dealloc jens@16: { jens@16: [_bits release]; jens@16: [super dealloc]; jens@16: } jens@16: jens@16: jens@16: @synthesize bits=_bits, bitCount=_bitCount; jens@16: jens@16: @end