Coder Social home page Coder Social logo

mdabbagh88 / nskeyedarchiver-butwithnserror Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rentzsch/nskeyedarchiver-butwithnserror

0.0 0.0 0.0 204 KB

Categories to make NSKeyedArchiver & NSKeyedUnarchiver easier to use and report errors via NSError**

Objective-C 100.00%

nskeyedarchiver-butwithnserror's Introduction

NSKeyedArchiver+butWithNSError

NSKeyedArchiver+butWithNSError is a very small project that provides two categories: one on NSKeyedArchiver and one on NSKeyedUnarchiver:

@interface NSKeyedArchiver (butWithNSError)
+ (NSData*)jr_archivedDataWithRootObject:(id)rootObject
					requiresSecureCoding:(BOOL)requiresSecureCoding
								   error:(NSError**)error
@end

@interface NSKeyedUnarchiver (butWithNSError)
+ (id)jr_unarchiveData:(NSData*)data
  requiresSecureCoding:(BOOL)requiresSecureCoding
			 whitelist:(NSArray*)customClassWhitelist
				 error:(NSError**)error;
@end

These categories make it easier to use NSKeyedArchiver and NSKeyedUnarchiver correctly, securely and cope with the fact NSKeyedUnarchiver throws exceptions.

These should be compatible with both ARC and MRC.

Use

The categories are mostly self-evident.

Call +[NSKeyedArchiver(butWithNSError) jr_archivedDataWithRootObject:requiresSecureCoding:error:] to serialize rootObject's object graph into an NSData. Set requiresSecureCoding to YES if you want an error returned if any object doesn't support NSSecureCoding.

Call +[NSKeyedUnarchiver(butWithNSError) jr_unarchiveData:requiresSecureCoding:whitelist:error:] to deserialize the data back into objects. requiresSecureCoding works the same way as above.

whitelist can be nil or empty, but if supplied will be added to the list of trusted classes allowed to be deserialized. Naturally these classes must support NSSecureCoding otherwise you'll get a runtime error.

The Plist-standard are automatically whitelisted for you: NSArray, NSDictionary, NSString, NSNumber, NSDate, NSData.

Ease

In the Good Old Days, you'd call

+[NSKeyedArchiver archivedDataWithRootObject:]

and

+[NSKeyedUnarchiver unarchiveObjectWithData:]

and would be done for the day and catch an X-Files rerun. I hope the Smoking Man's in this one.

But then NSSecureCoding burst onto the scene. Now you need to manually create your archivers and unarchivers just so you can call their concealed -setRequiresSecureCoding: methods.

Not only are you now in the creation and configuration business, you need to use it correctly. And it turns out there's a trap for the unwary.

Having to reimplement +archivedDataWithRootObject:, you'd be forgiven thinking that you should utilize -[NSKeyedArchiver encodeRootObject:]. That's what I did.

Bzzt. Unfortunately it turns out generates a binary plist in a format (I call this "Format 1") that's incompatible with +[NSKeyedUnarchiver unarchiveObjectWithData:] (which requires what I call "Format 2").

Here's a table of which methods play with which format. I think.

Method Binary Plist Format NSSecureCoding compatible
-[NSKeyedArchiver encodeRootObject:] Format 1 no
-[NSKeyedUnarchiver decodeObject] Format 1 no
+[NSKeyedArchiver archivedDataWithRootObject:] Format 2 no
+[NSKeyedUnarchiver unarchiveObjectWithData:] Format 2 no
-[NSKeyedArchiver encodeObject:forKey:NSKeyedArchiveRootObjectKey] Format 2 yes
-[NSKeyedUnarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey] Format 2 yes

The Bad News is that you must use -[NSKeyedUnarchiver decodeObject] to decode what -[NSKeyedArchiver encodeRootObject:] is cooking, and -decodeObject isn't compatible with NSSecureCoding (you need to use -decodeObjectOfClasses:forKey:).

tl;dr: don't use -[NSKeyedArchiver encodeRootObject:] or -[NSKeyedUnarchiver decodeObject] in new code unless you need compatibility with Format 1 archives.

Since +[NSKeyedArchiver archivedDataWithRootObject:] and +[NSKeyedUnarchiver unarchiveObjectWithData:] don't give you an opportunity to call -setRequiresSecureCoding:YES, they're out of the party as well.

That leaves us with -[NSKeyedArchiver encodeObject:forKey:NSKeyedArchiveRootObjectKey] and -[NSKeyedUnarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]. Which is what NSKeyedArchiver+butWithNSError uses.

NSError**

I could make a case for NSKeyedArchiver throwing exceptions since as the programmer you can know what you're passing it. But it seems wrong that NSKeyedUnarchiver throws exceptions.

Here is a class responsible for decoding potentially hostile data and it blows up your entire process if things aren't exactly to its liking?! That's just nuts.

NSKeyedArchiver+butWithNSError catches exceptions, unravels them and transmogrifies them into NSErrors for you so you can handle them normally (I'm biased, but I recommend JRErr).

TODO

Promote to v1.0 when I ship.

Version History

v1.0b1: Mar 28 2014

  • Initial release.

nskeyedarchiver-butwithnserror's People

Contributors

rentzsch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.