GULSecureCoding.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "GoogleUtilities/Environment/Private/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, watchOS 4.0, *)) {
  23. object = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:data error:outError];
  24. } else
  25. #endif // __has_builtin(__builtin_available)
  26. {
  27. @try {
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  30. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  31. #pragma clang diagnostic pop
  32. unarchiver.requiresSecureCoding = YES;
  33. object = [unarchiver decodeObjectOfClasses:classes forKey:NSKeyedArchiveRootObjectKey];
  34. } @catch (NSException *exception) {
  35. if (outError) {
  36. *outError = [self archivingErrorWithException:exception];
  37. }
  38. }
  39. if (object == nil && outError && *outError == nil) {
  40. NSString *failureReason = @"NSKeyedUnarchiver failed to unarchive data.";
  41. *outError = [NSError errorWithDomain:kGULSecureCodingError
  42. code:-1
  43. userInfo:@{NSLocalizedFailureReasonErrorKey : failureReason}];
  44. }
  45. }
  46. return object;
  47. }
  48. + (nullable id)unarchivedObjectOfClass:(Class)class
  49. fromData:(NSData *)data
  50. error:(NSError **)outError {
  51. return [self unarchivedObjectOfClasses:[NSSet setWithObject:class] fromData:data error:outError];
  52. }
  53. + (nullable NSData *)archivedDataWithRootObject:(id<NSCoding>)object error:(NSError **)outError {
  54. NSData *archiveData;
  55. #if __has_builtin(__builtin_available)
  56. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)) {
  57. archiveData = [NSKeyedArchiver archivedDataWithRootObject:object
  58. requiringSecureCoding:YES
  59. error:outError];
  60. } else
  61. #endif // __has_builtin(__builtin_available)
  62. {
  63. @try {
  64. NSMutableData *data = [NSMutableData data];
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  67. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  68. #pragma clang diagnostic pop
  69. archiver.requiresSecureCoding = YES;
  70. [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
  71. [archiver finishEncoding];
  72. archiveData = [data copy];
  73. } @catch (NSException *exception) {
  74. if (outError) {
  75. *outError = [self archivingErrorWithException:exception];
  76. }
  77. }
  78. }
  79. return archiveData;
  80. }
  81. + (NSError *)archivingErrorWithException:(NSException *)exception {
  82. NSString *failureReason = [NSString
  83. stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
  84. exception.name, exception.reason, exception.userInfo];
  85. NSDictionary *errorUserInfo = @{NSLocalizedFailureReasonErrorKey : failureReason};
  86. return [NSError errorWithDomain:kGULSecureCodingError code:-1 userInfo:errorUserInfo];
  87. }
  88. @end