FIRCLSMachOBinary.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "Crashlytics/Shared/FIRCLSMachO/FIRCLSMachOBinary.h"
  15. #import <CommonCrypto/CommonHMAC.h>
  16. #import "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
  17. #import "Crashlytics/Shared/FIRCLSByteUtility.h"
  18. #import "Crashlytics/Shared/FIRCLSMachO/FIRCLSMachOSlice.h"
  19. static NSString* FIRCLSHashNSString(NSString* value);
  20. @interface FIRCLSMachOBinary ()
  21. + (NSString*)hashNSString:(NSString*)value;
  22. @end
  23. @implementation FIRCLSMachOBinary
  24. + (id)MachOBinaryWithPath:(NSString*)path {
  25. return [[self alloc] initWithURL:[NSURL fileURLWithPath:path]];
  26. }
  27. @synthesize slices = _slices;
  28. - (id)initWithURL:(NSURL*)url {
  29. self = [super init];
  30. if (self) {
  31. _url = [url copy];
  32. if (!FIRCLSMachOFileInitWithPath(&_file, [[_url path] fileSystemRepresentation])) {
  33. return nil;
  34. }
  35. _slices = [NSMutableArray new];
  36. FIRCLSMachOFileEnumerateSlices(&_file, ^(FIRCLSMachOSliceRef slice) {
  37. FIRCLSMachOSlice* sliceObject;
  38. sliceObject = [[FIRCLSMachOSlice alloc] initWithSlice:slice];
  39. [self->_slices addObject:sliceObject];
  40. });
  41. }
  42. return self;
  43. }
  44. - (void)dealloc {
  45. FIRCLSMachOFileDestroy(&_file);
  46. }
  47. - (NSURL*)URL {
  48. return _url;
  49. }
  50. - (NSString*)path {
  51. return [_url path];
  52. }
  53. - (NSString*)instanceIdentifier {
  54. if (_instanceIdentifier) {
  55. return _instanceIdentifier;
  56. }
  57. NSMutableString* prehashedString = [NSMutableString new];
  58. // sort the slices by architecture
  59. NSArray* sortedSlices =
  60. [_slices sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  61. return [[obj1 architectureName] compare:[obj2 architectureName]];
  62. }];
  63. // append them all into a big string
  64. for (FIRCLSMachOSlice* slice in sortedSlices) {
  65. [prehashedString appendString:[slice uuid]];
  66. }
  67. _instanceIdentifier = [FIRCLSHashNSString(prehashedString) copy];
  68. return _instanceIdentifier;
  69. }
  70. - (void)enumerateUUIDs:(void (^)(NSString* uuid, NSString* architecture))block {
  71. for (FIRCLSMachOSlice* slice in _slices) {
  72. block([slice uuid], [slice architectureName]);
  73. }
  74. }
  75. - (FIRCLSMachOSlice*)sliceForArchitecture:(NSString*)architecture {
  76. for (FIRCLSMachOSlice* slice in [self slices]) {
  77. if ([[slice architectureName] isEqualToString:architecture]) {
  78. return slice;
  79. }
  80. }
  81. return nil;
  82. }
  83. + (NSString*)hashNSString:(NSString*)value {
  84. return FIRCLSHashNSString(value);
  85. }
  86. @end
  87. static NSString* FIRCLSHashNSString(NSString* value) {
  88. const char* s = [value cStringUsingEncoding:NSUTF8StringEncoding];
  89. return FIRCLSHashBytes(s, strlen(s));
  90. }