Created
January 25, 2017 04:48
-
-
Save tyrone-sudeium/1323d8afa0b33f8860c8951322dd39df to your computer and use it in GitHub Desktop.
Gets every method in the current binary image (not in linked frameworks or dylibs) and orders them in a CSV by length
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
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
int main(int argc, char *argv[]) | |
{ | |
unsigned int imageCount = 0; | |
const char** imageNames = objc_copyImageNames(&imageCount); | |
const char* thisImage = imageNames[imageCount-1]; | |
unsigned int numberOfClasses = 0; | |
const char** classNames = objc_copyClassNamesForImage(thisImage, &numberOfClasses); | |
NSMutableDictionary *methodsByLength = [NSMutableDictionary new]; | |
for (int i = 0; i < numberOfClasses; i++) { | |
Class class = objc_lookUpClass(classNames[i]); | |
unsigned int numberOfMethods = 0; | |
Method *instanceMethods = class_copyMethodList(class, &numberOfMethods); | |
for (int j = 0; j < numberOfMethods; j++) { | |
Method method = instanceMethods[j]; | |
SEL selector = method_getName(method); | |
NSString *methodName = NSStringFromSelector(selector); | |
NSUInteger length = [methodName length]; | |
NSMutableArray *methods = methodsByLength[@(length)]; | |
if (methods == nil) { | |
methods = [NSMutableArray new]; | |
methodsByLength[@(length)] = methods; | |
} | |
[methods addObject: methodName]; | |
} | |
Class metaClass = objc_getMetaClass([NSStringFromClass(class) cStringUsingEncoding:NSASCIIStringEncoding]); | |
Method *classMethods = class_copyMethodList(metaClass, &numberOfMethods); | |
for (int j = 0; j < numberOfMethods; j++) { | |
Method method = classMethods[j]; | |
SEL selector = method_getName(method); | |
NSString *methodName = NSStringFromSelector(selector); | |
NSUInteger length = [methodName length]; | |
NSMutableArray *methods = methodsByLength[@(length)]; | |
if (methods == nil) { | |
methods = [NSMutableArray new]; | |
methodsByLength[@(length)] = methods; | |
} | |
[methods addObject: methodName]; | |
} | |
} | |
NSArray *keys = [[methodsByLength allKeys] sortedArrayUsingSelector: @selector(compare:)]; | |
NSMutableString *csv = [NSMutableString new]; | |
for (NSNumber *len in keys) { | |
NSArray *methods = methodsByLength[len]; | |
[csv appendFormat: @"%@", len]; | |
for (NSString *methodName in methods) { | |
[csv appendFormat: @",%@", methodName]; | |
} | |
[csv appendString: @"\n"]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment