Created
March 21, 2016 10:26
-
-
Save zqqf16/1ab2bc9612657ae96441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *)executableUUID { | |
const struct mach_header *executableHeader = NULL; | |
for (uint32_t i = 0; i < _dyld_image_count(); i++) { | |
const struct mach_header *header = _dyld_get_image_header(i); | |
if (header->filetype == MH_EXECUTE) { | |
executableHeader = header; | |
break; | |
} | |
} | |
if (!executableHeader) | |
return @""; | |
BOOL is64bit = executableHeader->magic == MH_MAGIC_64 || executableHeader->magic == MH_CIGAM_64; | |
uintptr_t cursor = (uintptr_t)executableHeader + (is64bit ? sizeof(struct mach_header_64) : sizeof(struct mach_header)); | |
const struct segment_command *segmentCommand = NULL; | |
for (uint32_t i = 0; i < executableHeader->ncmds; i++, cursor += segmentCommand->cmdsize) { | |
segmentCommand = (struct segment_command *)cursor; | |
if (segmentCommand->cmd == LC_UUID) { | |
const struct uuid_command *uuidCommand = (const struct uuid_command *)segmentCommand; | |
const uint8_t *uuid = uuidCommand->uuid; | |
return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", | |
uuid[0], uuid[1], uuid[2], uuid[3], | |
uuid[4], uuid[5], uuid[6], uuid[7], | |
uuid[8], uuid[9], uuid[10], uuid[11], | |
uuid[12], uuid[13], uuid[14], uuid[15]] | |
lowercaseString]; | |
} | |
} | |
return @""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment