GULSecureCoding.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Public/GULSecureCoding.h"
  15. NSString *const kGULSecureCodingError = @"GULSecureCodingError";
  16. @implementation GULSecureCoding
  17. + (nullable id)unarchivedObjectOfClass:(Class)class
  18. fromData:(NSData *)data
  19. error:(NSError **)outError {
  20. id object;
  21. #if __has_builtin(__builtin_available)
  22. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  23. object = [NSKeyedUnarchiver unarchivedObjectOfClass:class fromData:data error:outError];
  24. } else
  25. #endif // __has_builtin(__builtin_available)
  26. {
  27. @try {
  28. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  29. unarchiver.requiresSecureCoding = YES;
  30. object = [unarchiver decodeObjectOfClass:class forKey:NSKeyedArchiveRootObjectKey];
  31. } @catch (NSException *exception) {
  32. if (outError) {
  33. *outError = [self archivingErrorWithException:exception];
  34. }
  35. }
  36. if (object == nil && outError && *outError == nil) {
  37. NSString *failureReason = @"NSKeyedUnarchiver failed to unarchive data.";
  38. *outError = [NSError errorWithDomain:kGULSecureCodingError
  39. code:-1
  40. userInfo:@{NSLocalizedFailureReasonErrorKey : failureReason}];
  41. }
  42. }
  43. return object;
  44. }
  45. + (nullable NSData *)archivedDataWithRootObject:(id<NSCoding>)object error:(NSError **)outError {
  46. NSData *archiveData;
  47. #if __has_builtin(__builtin_available)
  48. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  49. archiveData = [NSKeyedArchiver archivedDataWithRootObject:object
  50. requiringSecureCoding:YES
  51. error:outError];
  52. } else
  53. #endif // __has_builtin(__builtin_available)
  54. {
  55. @try {
  56. NSMutableData *data = [NSMutableData data];
  57. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  58. archiver.requiresSecureCoding = YES;
  59. [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
  60. [archiver finishEncoding];
  61. archiveData = [data copy];
  62. } @catch (NSException *exception) {
  63. if (outError) {
  64. *outError = [self archivingErrorWithException:exception];
  65. }
  66. }
  67. }
  68. return archiveData;
  69. }
  70. + (NSError *)archivingErrorWithException:(NSException *)exception {
  71. NSString *failureReason = [NSString
  72. stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
  73. exception.name, exception.reason, exception.userInfo];
  74. NSDictionary *errorUserInfo = @{NSLocalizedFailureReasonErrorKey : failureReason};
  75. return [NSError errorWithDomain:kGULSecureCodingError code:-1 userInfo:errorUserInfo];
  76. }
  77. @end