1 #import "MTeSerialNumber.h"
5 @interface MTeSerialNumber (Private)
7 - (eSellerate_String)eSellerateStringForString:(NSString *)string;
10 @implementation MTeSerialNumber
12 /*************************************************************************/
14 #pragma mark INITIALIZATION METHODS
15 /*************************************************************************/
17 - (id)initWithSerialNumber:(NSString *)serial
19 extra:(NSString *)extra
20 publisher:(NSString *)publisher
22 if ( (self = [super init]) ) {
23 _serialNumber = serial;
25 _extraDataKey = nil; //extra data is currently unused.
26 _publisherKey = publisher;
32 - (id)initWithDictionary:(NSDictionary *)dict
34 return [self initWithSerialNumber:[dict objectForKey:@"Key"]
35 name:[dict objectForKey:@"Owner"]
36 extra:[dict objectForKey:@"Extra"]
37 publisher:[dict objectForKey:@"Publisher"]];
40 - (id)initWithContentsOfFile:(NSString *)path
41 extra:(NSString *)extra
42 publisher:(NSString *)publisher
44 NSDictionary *fileDict = [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease];
47 NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease];
49 [dict setObject:[fileDict objectForKey:@"Key"] forKey:@"Key"];
50 [dict setObject:[fileDict objectForKey:@"Owner"] forKey:@"Owner"];
51 [dict setObject:extra forKey:@"Extra"];
52 [dict setObject:publisher forKey:@"Publisher"];
54 return [self initWithDictionary:dict];
61 /*************************************************************************/
63 #pragma mark ACCESSOR METHODS
64 /*************************************************************************/
66 - (NSString *)serialNumber
71 - (void)setSerialNumber:(NSString *)newSerial
73 [_serialNumber autorelease];
74 _serialNumber = [newSerial copy];
77 - (NSString *)nameBasedKey
82 - (void)setNameBasedKey:(NSString *)newName
84 [_nameBasedKey autorelease];
85 _nameBasedKey = [newName copy];
88 - (NSString *)extraDataKey
93 - (void)setExtraDataKey:(NSString *)newData
95 [_extraDataKey autorelease];
96 _extraDataKey = [newData copy];
99 - (NSString *)publisherKey
101 return _publisherKey;
104 - (void)setPublisherKey:(NSString *)newPublisher
106 [_publisherKey autorelease];
107 _publisherKey = [newPublisher copy];
110 - (NSArray *)deadSerials
115 - (void)setDeadSerials:(NSArray *)newList
117 [_deadSerials autorelease];
118 _deadSerials = [newList copy];
122 /*************************************************************************/
124 #pragma mark INSTANCE METHODS
125 /*************************************************************************/
127 - (MTeSerialNumberValidationResult)isValid
129 if ( _serialNumber ) {
133 if ( [_deadSerials count] ) {
134 NSEnumerator *deadEnum = [_deadSerials objectEnumerator];
137 while ( (aDeadSerial = [deadEnum nextObject]) ) {
138 if ( [aDeadSerial isEqualToString:_serialNumber] ) {
145 return ITeSerialNumberIsDead;
147 return ( ( [self validate] > 0 ) ? ITeSerialNumberIsValid : ITeSerialNumberIsInvalid );
154 - (MTeSerialNumberExpirationResult)isExpired;
156 return ( ! [self secondsRemaining] > 0 );
159 - (NSDate *)storedDate
161 NSCalendarDate *refDate = [NSCalendarDate dateWithYear:2000 month:1 day:1
162 hour:0 minute:0 second:0
163 timeZone:[NSTimeZone systemTimeZone]];
165 NSTimeInterval secondsFromRefToExp = ([self validate] * 86400);
167 return [[[NSDate alloc] initWithTimeInterval:secondsFromRefToExp
168 sinceDate:refDate] autorelease];
171 - (NSTimeInterval)secondsRemaining
173 return [[self storedDate] timeIntervalSinceDate:[NSDate date]];
176 - (NSDictionary *)infoDictionary
178 NSString *prefix = [[_serialNumber componentsSeparatedByString:@"-"] objectAtIndex:0];
179 NSString *appIdentifier = nil;
180 NSString *version = nil;
181 NSString *typeCode = nil;
182 NSString *quantity = nil;
184 if ( ( [prefix length] == 10 ) || ( [prefix length] == 7 ) ) {
185 appIdentifier = [_serialNumber substringWithRange:NSMakeRange(0,2)];
186 version = [_serialNumber substringWithRange:NSMakeRange(2,3)];
187 typeCode = [_serialNumber substringWithRange:NSMakeRange(5,2)];
192 if ( [prefix length] == 10 ) {
193 quantity = [_serialNumber substringWithRange:NSMakeRange(7,3)];
196 return [NSDictionary dictionaryWithObjectsAndKeys:
197 appIdentifier, @"appIdentifier",
199 typeCode, @"typeCode",
200 quantity, @"quantity", nil];
204 /*************************************************************************/
206 #pragma mark PRIVATE IMPLEMENTATIONS
207 /*************************************************************************/
211 eSellerate_String pSerial = [self eSellerateStringForString:_serialNumber];
212 eSellerate_String pName = [self eSellerateStringForString:_nameBasedKey];
213 eSellerate_String pExtraData = [self eSellerateStringForString:_extraDataKey];
214 eSellerate_String pPublisher = [self eSellerateStringForString:_publisherKey];
216 return eSellerate_ValidateSerialNumber(pSerial,
222 - (eSellerate_String)eSellerateStringForString:(NSString *)string
225 NSMutableData *buffer = [[[NSMutableData alloc] initWithCapacity:256] autorelease];
227 CFStringGetPascalString( (CFStringRef)string,
228 [buffer mutableBytes],
230 CFStringGetSystemEncoding());
232 return (eSellerate_String)[buffer bytes];