FIRCLSMachOBinary.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "Crashlytics/Shared/FIRCLSMachO/FIRCLSMachOSlice.h"
  16. #import <CommonCrypto/CommonHMAC.h>
  17. static void FIRCLSSafeHexToString(const uint8_t* value, size_t length, char* outputBuffer);
  18. static NSString* FIRCLSNSDataToNSString(NSData* data);
  19. static NSString* FIRCLSHashBytes(const void* bytes, size_t length);
  20. static NSString* FIRCLSHashNSString(NSString* value);
  21. @interface FIRCLSMachOBinary ()
  22. + (NSString*)hashNSString:(NSString*)value;
  23. @end
  24. @implementation FIRCLSMachOBinary
  25. + (id)MachOBinaryWithPath:(NSString*)path {
  26. return [[self alloc] initWithURL:[NSURL fileURLWithPath:path]];
  27. }
  28. @synthesize slices = _slices;
  29. - (id)initWithURL:(NSURL*)url {
  30. self = [super init];
  31. if (self) {
  32. _url = [url copy];
  33. if (!FIRCLSMachOFileInitWithPath(&_file, [[_url path] fileSystemRepresentation])) {
  34. return nil;
  35. }
  36. _slices = [NSMutableArray new];
  37. FIRCLSMachOFileEnumerateSlices(&_file, ^(FIRCLSMachOSliceRef slice) {
  38. FIRCLSMachOSlice* sliceObject;
  39. sliceObject = [[FIRCLSMachOSlice alloc] initWithSlice:slice];
  40. [self->_slices addObject:sliceObject];
  41. });
  42. }
  43. return self;
  44. }
  45. - (void)dealloc {
  46. FIRCLSMachOFileDestroy(&_file);
  47. }
  48. - (NSURL*)URL {
  49. return _url;
  50. }
  51. - (NSString*)path {
  52. return [_url path];
  53. }
  54. - (NSString*)instanceIdentifier {
  55. if (_instanceIdentifier) {
  56. return _instanceIdentifier;
  57. }
  58. NSMutableString* prehashedString = [NSMutableString new];
  59. // sort the slices by architecture
  60. NSArray* sortedSlices =
  61. [_slices sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  62. return [[obj1 architectureName] compare:[obj2 architectureName]];
  63. }];
  64. // append them all into a big string
  65. for (FIRCLSMachOSlice* slice in sortedSlices) {
  66. [prehashedString appendString:[slice uuid]];
  67. }
  68. _instanceIdentifier = [FIRCLSHashNSString(prehashedString) copy];
  69. return _instanceIdentifier;
  70. }
  71. - (void)enumerateUUIDs:(void (^)(NSString* uuid, NSString* architecture))block {
  72. for (FIRCLSMachOSlice* slice in _slices) {
  73. block([slice uuid], [slice architectureName]);
  74. }
  75. }
  76. - (FIRCLSMachOSlice*)sliceForArchitecture:(NSString*)architecture {
  77. for (FIRCLSMachOSlice* slice in [self slices]) {
  78. if ([[slice architectureName] isEqualToString:architecture]) {
  79. return slice;
  80. }
  81. }
  82. return nil;
  83. }
  84. + (NSString*)hashNSString:(NSString*)value {
  85. return FIRCLSHashNSString(value);
  86. }
  87. @end
  88. // TODO: Functions copied from the SDK. We should figure out a way to share this.
  89. static void FIRCLSSafeHexToString(const uint8_t* value, size_t length, char* outputBuffer) {
  90. const char hex[] = "0123456789abcdef";
  91. if (!value) {
  92. outputBuffer[0] = '\0';
  93. return;
  94. }
  95. for (size_t i = 0; i < length; ++i) {
  96. unsigned char c = value[i];
  97. outputBuffer[i * 2] = hex[c >> 4];
  98. outputBuffer[i * 2 + 1] = hex[c & 0x0F];
  99. }
  100. outputBuffer[length * 2] = '\0'; // null terminate
  101. }
  102. static NSString* FIRCLSNSDataToNSString(NSData* data) {
  103. NSString* string;
  104. char* buffer;
  105. size_t size;
  106. NSUInteger length;
  107. // we need 2 hex char for every byte of data, plus one more spot for a
  108. // null terminator
  109. length = [data length];
  110. size = (length * 2) + 1;
  111. buffer = malloc(sizeof(char) * size);
  112. if (!buffer) {
  113. return nil;
  114. }
  115. FIRCLSSafeHexToString([data bytes], length, buffer);
  116. string = [NSString stringWithUTF8String:buffer];
  117. free(buffer);
  118. return string;
  119. }
  120. static NSString* FIRCLSHashBytes(const void* bytes, size_t length) {
  121. uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
  122. CC_SHA1(bytes, (CC_LONG)length, digest);
  123. NSData* result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
  124. return FIRCLSNSDataToNSString(result);
  125. }
  126. static NSString* FIRCLSHashNSString(NSString* value) {
  127. const char* s = [value cStringUsingEncoding:NSUTF8StringEncoding];
  128. return FIRCLSHashBytes(s, strlen(s));
  129. }