GULSecureCoding.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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)unarchivedObjectOfClasses:(NSSet<Class> *)classes
  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 unarchivedObjectOfClasses:classes 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 decodeObjectOfClasses:classes 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 id)unarchivedObjectOfClass:(Class)class
  46. fromData:(NSData *)data
  47. error:(NSError **)outError {
  48. return [self unarchivedObjectOfClasses:[NSSet setWithObject:class] fromData:data error:outError];
  49. }
  50. + (nullable NSData *)archivedDataWithRootObject:(id<NSCoding>)object error:(NSError **)outError {
  51. NSData *archiveData;
  52. #if __has_builtin(__builtin_available)
  53. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  54. archiveData = [NSKeyedArchiver archivedDataWithRootObject:object
  55. requiringSecureCoding:YES
  56. error:outError];
  57. } else
  58. #endif // __has_builtin(__builtin_available)
  59. {
  60. @try {
  61. NSMutableData *data = [NSMutableData data];
  62. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  63. archiver.requiresSecureCoding = YES;
  64. [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
  65. [archiver finishEncoding];
  66. archiveData = [data copy];
  67. } @catch (NSException *exception) {
  68. if (outError) {
  69. *outError = [self archivingErrorWithException:exception];
  70. }
  71. }
  72. }
  73. return archiveData;
  74. }
  75. + (NSError *)archivingErrorWithException:(NSException *)exception {
  76. NSString *failureReason = [NSString
  77. stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
  78. exception.name, exception.reason, exception.userInfo];
  79. NSDictionary *errorUserInfo = @{NSLocalizedFailureReasonErrorKey : failureReason};
  80. return [NSError errorWithDomain:kGULSecureCodingError code:-1 userInfo:errorUserInfo];
  81. }
  82. @end