forked from Polidea/ios-class-guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Polidea#5 from marcinkiewiczblazej/pods-support
CocoaPods obfuscation
- Loading branch information
Showing
20 changed files
with
515 additions
and
42 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
#import <Foundation/Foundation.h> | ||
#import "CDParserSharedBase.h" | ||
#import "CDXMLParserSharedBase.h" | ||
|
||
@class GDataXMLElement; | ||
|
||
|
||
@interface CDCoreDataModelParser : CDParserSharedBase | ||
@interface CDCoreDataModelParser : CDXMLParserSharedBase | ||
@end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface CDPbxProjectParser : NSObject | ||
|
||
- (instancetype)initWithJsonDictionary:(NSDictionary *)project; | ||
- (NSSet *)findTargets; | ||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#import "CDPbxProjectParser.h" | ||
#import "CDPbxProjectTarget.h" | ||
|
||
|
||
@interface CDPbxProjectParser () | ||
@property(nonatomic, strong) NSDictionary *project; | ||
@end | ||
|
||
@implementation CDPbxProjectParser { | ||
|
||
} | ||
- (instancetype)initWithJsonDictionary:(NSDictionary *)project { | ||
self = [self init]; | ||
if (self) { | ||
self.project = project; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSSet *)findTargets { | ||
__weak CDPbxProjectParser *weakSelf = self; | ||
NSMutableSet *targets = [NSMutableSet set]; | ||
[self iterateBuildConfigurationsWithBlock:^(NSString *targetName, NSDictionary *buildConfiguration) { | ||
NSDictionary *buildSettings = buildConfiguration[@"buildSettings"]; | ||
NSString *prefixHeaderName = buildSettings[@"GCC_PREFIX_HEADER"]; | ||
|
||
NSDictionary *baseConfiguration = [weakSelf getObjectWithHash:buildConfiguration[@"baseConfigurationReference"]]; | ||
NSString *configFileName = baseConfiguration[@"path"]; | ||
|
||
[targets addObject:[[CDPbxProjectTarget alloc] initWithTargetName:targetName precompiledHeaderName:prefixHeaderName configurationFile:configFileName]]; | ||
|
||
}]; | ||
|
||
return targets; | ||
} | ||
|
||
#pragma mark - helpers | ||
|
||
- (void)iterateBuildConfigurationsWithBlock:(void (^)(NSString *, NSDictionary *))iterationBlock { | ||
__weak CDPbxProjectParser *weakSelf = self; | ||
[self iterateTargetsWithBlock:^(NSDictionary *target) { | ||
NSDictionary *buildConfigurationList = [weakSelf getObjectWithHash:target[@"buildConfigurationList"]]; | ||
NSArray *buildConfigurations = buildConfigurationList[@"buildConfigurations"]; | ||
for (NSString *buildConfigurationHash in buildConfigurations) { | ||
NSDictionary *buildConfiguration = [weakSelf getObjectWithHash:buildConfigurationHash]; | ||
if (iterationBlock) { | ||
iterationBlock(target[@"name"], buildConfiguration); | ||
} else { | ||
break; | ||
} | ||
} | ||
}]; | ||
} | ||
|
||
- (void)iterateTargetsWithBlock:(void (^)(NSDictionary *))iterationBlock { | ||
NSString *rootObjectHash = self.project[@"rootObject"]; | ||
NSDictionary *rootObject = [self getObjectWithHash:rootObjectHash]; | ||
NSArray *targetsHashArray = rootObject[@"targets"]; | ||
|
||
for (NSString *targetHash in targetsHashArray) { | ||
if (iterationBlock) { | ||
NSDictionary *target = [self getObjectWithHash:targetHash]; | ||
iterationBlock(target); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
- (id)getObjectWithHash:(NSString *)hash { | ||
return self.project[@"objects"][hash]; | ||
} | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface CDPbxProjectProcessor : NSObject | ||
- (void)processPodsProjectAtPath:(NSString *)podsPath symbolsFilePath:(NSString *)symbolsPath; | ||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#import "CDPbxProjectProcessor.h" | ||
#import "CDPbxProjectParser.h" | ||
#import "CDPbxProjectTarget.h" | ||
#import "CDPodsFileProcessor.h" | ||
|
||
@interface CDPbxProjectProcessor () | ||
@property(nonatomic, strong) NSPipe *outputPipe; | ||
@end | ||
|
||
@implementation CDPbxProjectProcessor { | ||
|
||
} | ||
|
||
- (void)processPodsProjectAtPath:(NSString *)podsPath symbolsFilePath:(NSString *)symbolsPath { | ||
NSTask *task = [[NSTask alloc] init]; | ||
task.launchPath = @"/usr/bin/plutil"; | ||
task.arguments = @[@"-convert", @"json", podsPath, @"-o", @"-"]; | ||
|
||
self.outputPipe = [[NSPipe alloc] init]; | ||
task.standardOutput = self.outputPipe; | ||
[[self.outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify]; | ||
|
||
[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleReadToEndOfFileCompletionNotification object:[self.outputPipe fileHandleForReading] queue:nil usingBlock:^(NSNotification *notification){ | ||
NSData *output = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]; | ||
NSDictionary *projectJSON = [NSJSONSerialization JSONObjectWithData:output options:0 error:nil]; | ||
CDPbxProjectParser *parser = [[CDPbxProjectParser alloc] initWithJsonDictionary:projectJSON]; | ||
NSSet *targets = [parser findTargets]; | ||
CDPodsFileProcessor *processor = [[CDPodsFileProcessor alloc] init]; | ||
for (CDPbxProjectTarget *target in targets) { | ||
[processor processTarget:target symbolsFilePath:symbolsPath]; | ||
} | ||
}]; | ||
|
||
[task launch]; | ||
|
||
[task waitUntilExit]; | ||
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface CDPbxProjectTarget : NSObject | ||
|
||
@property(nonatomic, readonly) NSString *targetName; | ||
|
||
@property(nonatomic, readonly) NSString *headerName; | ||
|
||
@property(nonatomic, readonly) NSString *configFile; | ||
|
||
- (instancetype)initWithTargetName:(NSString *)targetName precompiledHeaderName:(NSString *)precompiledHeader configurationFile:(NSString *)configurationFile; | ||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#import "CDPbxProjectTarget.h" | ||
|
||
|
||
@interface CDPbxProjectTarget () | ||
@property(nonatomic, readwrite) NSString *targetName; | ||
@property(nonatomic, readwrite) NSString *headerName; | ||
@property(nonatomic, readwrite) NSString *configFile; | ||
@end | ||
|
||
@implementation CDPbxProjectTarget { | ||
|
||
} | ||
- (instancetype)initWithTargetName:(NSString *)targetName precompiledHeaderName:(NSString *)precompiledHeader configurationFile:(NSString *)configurationFile { | ||
self = [self init]; | ||
if (self) { | ||
self.targetName = targetName; | ||
self.headerName = precompiledHeader; | ||
self.configFile = configurationFile; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)isEqual:(id)other { | ||
if ([other isKindOfClass:[self class]]) { | ||
CDPbxProjectTarget *otherTarget = other; | ||
if ([otherTarget.targetName ?: @"" isEqualToString:self.targetName ?: @""] && | ||
[otherTarget.headerName ?: @"" isEqualToString:self.headerName ?: @""] && | ||
[otherTarget.configFile ?: @"" isEqualToString:self.configFile ?: @""]) { | ||
return YES; | ||
} else { | ||
return NO; | ||
} | ||
} | ||
return [super isEqual:other]; | ||
} | ||
|
||
- (NSUInteger)hash { | ||
NSString *string = [NSString stringWithFormat:@"%@_%@_%@", self.targetName, self.configFile, self.headerName]; | ||
return [string hash]; //Must be a unique unsigned integer | ||
} | ||
|
||
- (NSString *)debugDescription { | ||
|
||
return [NSString stringWithFormat:@"<CDPbxProjectTarget target=%@ header=%@ config=%@>", self.targetName, self.headerName, self.configFile]; | ||
} | ||
|
||
|
||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
@class CDPbxProjectTarget; | ||
|
||
|
||
@interface CDPodsFileProcessor : NSObject | ||
- (void)processTarget:(CDPbxProjectTarget *)target symbolsFilePath:(NSString *)symbolsFilePath; | ||
@end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#import "CDPodsFileProcessor.h" | ||
#import "CDPbxProjectTarget.h" | ||
|
||
|
||
@implementation CDPodsFileProcessor { | ||
|
||
} | ||
- (void)processTarget:(CDPbxProjectTarget *)target symbolsFilePath:(NSString *)symbolsFilePath { | ||
if (target.headerName.length == 0 || target.configFile.length == 0) { | ||
NSLog(@"Error: Could not process target %@ config %@ header %@. Some values are missing.", target.targetName, target.configFile, target.headerName); | ||
return; | ||
} | ||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey]; | ||
NSURL *directoryURL = [[NSURL alloc] initWithString:@"."]; | ||
|
||
NSDirectoryEnumerator *enumerator = [fileManager | ||
enumeratorAtURL:directoryURL | ||
includingPropertiesForKeys:keys | ||
options:0 | ||
errorHandler:^(NSURL *url, NSError *error) { | ||
// Handle the error. | ||
// Return YES if the enumeration should continue after the error. | ||
return YES; | ||
}]; | ||
|
||
|
||
for (NSURL *url in enumerator) { | ||
NSError *error; | ||
NSNumber *isDirectory = nil; | ||
if ([url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error] && ![isDirectory boolValue]) { | ||
NSString *symbolsFileName = [[symbolsFilePath pathComponents] lastObject]; | ||
if ([[[url.absoluteString pathComponents] lastObject] isEqualToString:target.headerName]) { | ||
NSLog(@"Obfuscating precompiled header %@", target.headerName); | ||
NSString *headerData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; | ||
NSString *obfuscatedData = [NSString stringWithFormat:@"#import \"%@\"\n%@", symbolsFileName, headerData]; | ||
[obfuscatedData writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil]; | ||
} | ||
if ([[[url.absoluteString pathComponents] lastObject] isEqualToString:target.configFile]) { | ||
NSLog(@"Adding symbols file path to configuration file %@", target.configFile); | ||
NSString *configData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; | ||
NSArray *pathComponents = [symbolsFilePath pathComponents]; | ||
|
||
NSString *symbolsDirectory = @""; | ||
for (NSString *component in pathComponents) { | ||
if (![component isEqualToString:symbolsFileName]) { | ||
symbolsDirectory = [symbolsDirectory stringByAppendingPathComponent:component]; | ||
} | ||
} | ||
NSString *processedData = [configData stringByReplacingOccurrencesOfString:@"HEADER_SEARCH_PATHS = " withString:[NSString stringWithFormat:@"HEADER_SEARCH_PATHS = \"${PODS_ROOT}/../%@\" ", symbolsDirectory]]; | ||
[processedData writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@end |
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
Oops, something went wrong.