ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看直播体育app软件下载 >> IOS开发 >> iOS 如何获取 Mach-O 的 UUID

iOS 如何获取 Mach-O 的 UUID(3/3)

来源:网络整理     时间:2018-11-01     关键词:

本篇文章主要介绍了" iOS 如何获取 Mach-O 的 UUID",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: LC_UUID 一般简称为 UUID,是用来标示 Mach-O 文件的,做过崩溃堆栈符号化还原的同学应该都知道有 UUID 这个东西,你在进行符号解析的时候,就...

//
//  LDAPMUUIDTool.m
//  Pods
//
//  Created by wangjiale on 2017/9/7.
//
//
#import "LDAPMUUIDTool.h"
#import 
#include 
#include 
#include 
#include 
static NSMutableArray *_UUIDRecordArray;
@implementation LDAPMUUIDTool
+ (NSDictionary *)getUUIDDictionary {
    NSDictionary *uuidDic = [[NSDictionary alloc] init];
    int imageCount = (int)_dyld_image_count();
    for(int iImg = 0; iImg < imageCount; iImg++) {
        JYGetBinaryImage(iImg);
    }
    return uuidDic;
}
// 获取 Load Command, 会根据 header 的 magic 来判断是 64 位 还是 32 位
static uintptr_t firstCmdAfterHeader(const struct mach_header* const header) {
    switch(header->magic) {
        case MH_MAGIC:
        case MH_CIGAM:
            return (uintptr_t)(header + 1);
        case MH_MAGIC_64:
        case MH_CIGAM_64:
            return (uintptr_t)(((struct mach_header_64*)header) + 1);
        default:
            return 0;
    }
}
bool JYGetBinaryImage(int index) {
    const struct mach_header* header = _dyld_get_image_header((unsigned)index);
    if(header == NULL) {
        return false;
    }
    uintptr_t cmdPtr = firstCmdAfterHeader(header);
    if(cmdPtr == 0) {
        return false;
    }
    uint8_t* uuid = NULL;
    for(uint32_t iCmd = 0; iCmd < header->ncmds; iCmd++)
    {
        struct load_command* loadCmd = (struct load_command*)cmdPtr;
        if (loadCmd->cmd == LC_UUID) {
            struct uuid_command* uuidCmd = (struct uuid_command*)cmdPtr;
            uuid = uuidCmd->uuid;
            break;
        }
        cmdPtr += loadCmd->cmdsize;
    }
    const char* path = _dyld_get_image_name((unsigned)index);
    NSString *imagePath = [NSString stringWithUTF8String:path];
    NSArray *array = [imagePath componentsSeparatedByString:@"/"];
    NSString *imageName = array[array.count - 1];
    NSLog(@"buffer->name:%@",imageName);
    const char* result = nil;
    if(uuid != NULL)
    {
        result = uuidBytesToString(uuid);
        NSString *lduuid = [NSString stringWithUTF8String:result];
        NSLog(@"buffer->uuid:%@",lduuid);
    }
    return true;
}
static const char* uuidBytesToString(const uint8_t* uuidBytes) {
    CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(NULL, *((CFUUIDBytes*)uuidBytes));
    NSString* str = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    return cString(str);
}
const char* cString(NSString* str) {
    return str == NULL ? NULL : strdup(str.UTF8String);
}
@end


作者:Joy___
链接:https://www.jianshu.com/p/9201d5e34eb6

以上就介绍了 iOS 如何获取 Mach-O 的 UUID,包括了方面的内容,希望对IOS开发有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_4596255_3.html

相关图片

相关文章