diff --git a/Headers/Foundation/NSKeyedArchiver.h b/Headers/Foundation/NSKeyedArchiver.h index 7e6e621ab..32790351f 100644 --- a/Headers/Foundation/NSKeyedArchiver.h +++ b/Headers/Foundation/NSKeyedArchiver.h @@ -87,7 +87,8 @@ extern "C" { */ + (NSData*) archivedDataWithRootObject: (id)anObject; - + +#if OS_API_VERSION(MAC_OS_X_VERSION_10_13,GS_API_LATEST) /** * Encodes anObject and returns the resulting data object. Allows * secure coding if specified. Returns an error if an object @@ -96,6 +97,7 @@ extern "C" { + (NSData *) archivedDataWithRootObject: (id)anObject requiringSecureCoding: (BOOL)requiresSecureCoding error: (NSError **)error; +#endif /** * Encodes anObject and writes the resulting data ti aPath. diff --git a/Source/NSKeyedArchiver.m b/Source/NSKeyedArchiver.m index e4a01d130..13c8d1285 100644 --- a/Source/NSKeyedArchiver.m +++ b/Source/NSKeyedArchiver.m @@ -430,56 +430,60 @@ static NSDictionary *makeReference(unsigned ref) @implementation NSKeyedArchiver ++ (NSData *) archivedDataWithRootObject: (id)anObject + requiringSecureCoding: (BOOL)requiresSecureCoding + error: (NSError **)error +{ + NSData *d = nil; + + if (requiresSecureCoding == YES) + { + if (error != NULL) + { + *error = [NSError errorWithDomain: @"NSKeyedArchiver" + code: 0 + userInfo: nil]; + } + } + else + { + NSMutableData *m = nil; + NSKeyedArchiver *a = nil; + + error = NULL; + NS_DURING + { + m = [[NSMutableData alloc] initWithCapacity: 10240]; + a = [[NSKeyedArchiver alloc] initForWritingWithMutableData: m]; + [a encodeObject: anObject forKey: @"root"]; + [a finishEncoding]; + d = [m copy]; + DESTROY(m); + DESTROY(a); + } + NS_HANDLER + { + DESTROY(m); + DESTROY(a); + [localException raise]; + } + NS_ENDHANDLER; + } + + return AUTORELEASE(d); +} + /* * When I tried this on MacOS 10.3 it encoded the object with the key 'root', * so this implementation does the same. */ + (NSData*) archivedDataWithRootObject: (id)anObject { - NSMutableData *m = nil; - NSKeyedArchiver *a = nil; - NSData *d = nil; - - NS_DURING - { - m = [[NSMutableData alloc] initWithCapacity: 10240]; - a = [[NSKeyedArchiver alloc] initForWritingWithMutableData: m]; - [a encodeObject: anObject forKey: @"root"]; - [a finishEncoding]; - d = [m copy]; - DESTROY(m); - DESTROY(a); - } - NS_HANDLER - { - DESTROY(m); - DESTROY(a); - [localException raise]; - } - NS_ENDHANDLER - return AUTORELEASE(d); + return [self archivedDataWithRootObject: anObject + requiringSecureCoding: NO + error: NULL]; } -+ (NSData *) archivedDataWithRootObject: (id)anObject - requiringSecureCoding: (BOOL)requiresSecureCoding - error: (NSError **)error -{ - NSData *data = nil; - if (requiresSecureCoding == YES) - { - *error = [NSError errorWithDomain: @"NSKeyedArchiver" - code: 0 - userInfo: nil]; - } - else - { - error = NULL; - data = [self archivedDataWithRootObject: anObject]; - } - return data; -} - - + (BOOL) archiveRootObject: (id)anObject toFile: (NSString*)aPath { NSAutoreleasePool *pool = [NSAutoreleasePool new];