Skip to content

Commit

Permalink
Merge pull request Polidea#4 from marcinkiewiczblazej/xcdatamodel
Browse files Browse the repository at this point in the history
Added Storyboard and Xib files obfuscation
  • Loading branch information
ayufan committed Jul 30, 2014
2 parents d7332e8 + 5dbef78 commit 828031f
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 33 deletions.
8 changes: 8 additions & 0 deletions CDCoreDataModelParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import "CDParserSharedBase.h"

@class GDataXMLElement;


@interface CDCoreDataModelParser : CDParserSharedBase
@end
36 changes: 36 additions & 0 deletions CDCoreDataModelParser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#import "CDCoreDataModelParser.h"
#import "GDataXMLNode.h"


@implementation CDCoreDataModelParser

- (void)addSymbolsFromNode:(GDataXMLElement *)element toArray:(NSMutableArray *)symbolsArray {
NSArray *childNodes = element.children;

// Get the class name
GDataXMLNode *className = [element attributeForName:@"representedClassName"];
if (className) {
[symbolsArray addObject:[NSString stringWithFormat:@"!%@", className.stringValue]];
}

// Get the class name
GDataXMLNode *parentClassName = [element attributeForName:@"parentEntity"];
if (parentClassName) {
[symbolsArray addObject:[NSString stringWithFormat:@"!%@", parentClassName.stringValue]];
}

// Recursively process rest of the elements
for (GDataXMLElement *childNode in childNodes) {
// Skip comments
if ([childNode isKindOfClass:[GDataXMLElement class]]) {
[self addSymbolsFromNode:childNode toArray:symbolsArray];
}
}
}

- (void)obfuscateElement:(GDataXMLElement *)element usingSymbols:(NSDictionary *)symbols {
// TODO implement later
}


@end
6 changes: 6 additions & 0 deletions CDCoreDataModelProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <Foundation/Foundation.h>


@interface CDCoreDataModelProcessor : NSObject
- (NSArray *)coreDataModelSymbolsToExclude;
@end
69 changes: 69 additions & 0 deletions CDCoreDataModelProcessor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#import "CDCoreDataModelProcessor.h"
#import "CDCoreDataModelParser.h"


@implementation CDCoreDataModelProcessor

- (NSArray *)coreDataModelSymbolsToExclude {
NSMutableArray *coreDataModelSymbols = [NSMutableArray array];
CDCoreDataModelParser *parser = [[CDCoreDataModelParser alloc] init];

__weak CDCoreDataModelProcessor *weakSelf = self;


void (^modelCallback)(NSURL *) = ^(NSURL *url){
NSLog(@"Fetching symbols from CoreData model at path %@", url);
[coreDataModelSymbols addObjectsFromArray:[parser symbolsInData:[NSData dataWithContentsOfURL:url]]];
};

void (^xcdatamodelCallback)(NSURL *) = ^(NSURL *url){
[weakSelf findFileWithSuffix:@"contents" inDirectoryURL:url foundCallback:modelCallback];
};

void (^xcdatamodeldCallback)(NSURL *) = ^(NSURL *url){
[weakSelf findDirectoryWithSuffix:@".xcdatamodel/" inDirectoryURL:url foundCallback:xcdatamodelCallback];
};


[self findDirectoryWithSuffix:@".xcdatamodeld/" inDirectoryURL:[NSURL URLWithString:@"."] foundCallback:xcdatamodeldCallback];

return coreDataModelSymbols;
}

- (void)findFileWithSuffix:(NSString *)string inDirectoryURL:(NSURL *)URL foundCallback:(void (^)(NSURL *))callback {
[self findFilesOrDirectoryWithString:string isDirectory:NO URL:URL callback:callback];
}

- (void)findDirectoryWithSuffix:(NSString *)string inDirectoryURL:(NSURL *)URL foundCallback:(void (^)(NSURL *))callback {
[self findFilesOrDirectoryWithString:string isDirectory:YES URL:URL callback:callback];
}

- (void)findFilesOrDirectoryWithString:(NSString *)string isDirectory:(BOOL)directory URL:(NSURL *)URL callback:(void (^)(NSURL *))callback {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];

NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtURL:URL
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] == directory) {
if ([url.absoluteString hasSuffix:string]) {
if (callback) {
callback(url);
}
}
}
}
}

@end
12 changes: 12 additions & 0 deletions CDParserSharedBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import <Foundation/Foundation.h>

@class GDataXMLElement;

@interface CDParserSharedBase : NSObject
- (NSArray *)symbolsInData:(NSData *)data;

- (NSData *)obfuscatedXmlData:(NSData *)data symbols:(NSDictionary *)symbols;

- (void)addSymbolsFromNode:(GDataXMLElement *)xmlDictionary toArray:(NSMutableArray *)symbolsArray;
- (void)obfuscateElement:(GDataXMLElement *)element usingSymbols:(NSDictionary *)symbols;
@end
28 changes: 28 additions & 0 deletions CDParserSharedBase.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#import "CDParserSharedBase.h"
#import "GDataXMLNode.h"


@implementation CDParserSharedBase

- (NSArray *)symbolsInData:(NSData *)data {
NSMutableArray *array = [NSMutableArray array];

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data error:nil];

[self addSymbolsFromNode:doc.rootElement toArray:array];

return array;
}

- (NSData *)obfuscatedXmlData:(NSData *)data symbols:(NSDictionary *)symbols {
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data error:nil];

[self obfuscateElement:doc.rootElement usingSymbols:symbols];

return doc.XMLData;
}

- (void)addSymbolsFromNode:(GDataXMLElement *)xmlDictionary toArray:(NSMutableArray *)symbolsArray {}
- (void)obfuscateElement:(GDataXMLElement *)element usingSymbols:(NSDictionary *)symbols {}

@end
7 changes: 4 additions & 3 deletions CDXibStoryBoardProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#import "CDXibStoryboardParser.h"


@implementation CDXibStoryBoardProcessor {

}
@implementation CDXibStoryBoardProcessor

- (void)obfuscateFilesUsingSymbols:(NSDictionary *)symbols {
NSFileManager *fileManager = [NSFileManager defaultManager];
Expand Down Expand Up @@ -39,4 +37,7 @@ - (void)obfuscateFilesUsingSymbols:(NSDictionary *)symbols {
}
}
}



@end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#import "CDVisitor.h"


@interface CDSymoblsGeneratorVisitor : CDVisitor
@interface CDSymbolsGeneratorVisitor : CDVisitor
@property (nonatomic, copy) NSArray *classFilter;
@property (nonatomic, copy) NSArray *ignoreSymbols;
@property (nonatomic, readonly) NSString *resultString;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "CDSymoblsGeneratorVisitor.h"
#import "CDSymbolsGeneratorVisitor.h"
#import "CDOCProtocol.h"
#import "CDOCClass.h"
#import "CDOCCategory.h"
Expand All @@ -17,7 +17,7 @@
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
};

@implementation CDSymoblsGeneratorVisitor {
@implementation CDSymbolsGeneratorVisitor {
NSMutableSet *_protocolNames;
NSMutableSet *_classNames;
NSMutableSet *_categoryNames;
Expand Down
5 changes: 2 additions & 3 deletions Source/CDXibStoryboardParser.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import <Foundation/Foundation.h>
#import "CDParserSharedBase.h"


@interface CDXibStoryboardParser : NSObject

- (NSData *)obfuscatedXmlData:(NSData *)data symbols:(NSDictionary *)symbols;
@interface CDXibStoryboardParser : CDParserSharedBase
@end
9 changes: 7 additions & 2 deletions class-dump.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#import "CDFatFile.h"
#import "CDFatArch.h"
#import "CDSearchPathState.h"
#import "CDSymoblsGeneratorVisitor.h"
#import "CDSymbolsGeneratorVisitor.h"
#import "CDXibStoryboardParser.h"
#import "CDXibStoryBoardProcessor.h"
#import "CDCoreDataModelProcessor.h"

void print_usage(void)
{
Expand Down Expand Up @@ -336,14 +337,18 @@ int main(int argc, char *argv[])
} else {
[classDump processObjectiveCData];
[classDump registerTypes];

CDCoreDataModelProcessor *coreDataModelProcessor = [[CDCoreDataModelProcessor alloc] init];
[classFilter addObjectsFromArray:[coreDataModelProcessor coreDataModelSymbolsToExclude]];


if (searchString != nil) {
CDFindMethodVisitor *visitor = [[CDFindMethodVisitor alloc] init];
visitor.classDump = classDump;
visitor.searchString = searchString;
[classDump recursivelyVisit:visitor];
} else if (generateSymbolsTable) {
CDSymoblsGeneratorVisitor *visitor = [CDSymoblsGeneratorVisitor new];
CDSymbolsGeneratorVisitor *visitor = [CDSymbolsGeneratorVisitor new];
visitor.classDump = classDump;
visitor.classFilter = classFilter;
visitor.ignoreSymbols = ignoreSymbols;
Expand Down
40 changes: 40 additions & 0 deletions ios-class-guard Spec/CDCoreDataModelParserSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#import <Kiwi/Kiwi.h>

#import "CDCoreDataModelParser.h"

SPEC_BEGIN(CDCoreDataModelParserSpec)

describe(@"CDCoreDataModelParser", ^{
__block CDCoreDataModelParser *parser;


__block NSURL *fileUrl;
NSString *className = @"TestEntityA";
NSString *parentClass = @"TestParentEntity";

beforeEach(^{
parser = [[CDCoreDataModelParser alloc] init];
for (NSBundle *bundle in [NSBundle allBundles]) {
NSURL *url = [bundle URLForResource:@"contents" withExtension:nil];
if (url) {
fileUrl = url;
}
}
});

describe(@"parsing core data model", ^{
it(@"should generate symbol for class name", ^{
NSArray *symbols = [parser symbolsInData:[NSData dataWithContentsOfURL:fileUrl]];

[[symbols should] contain:[NSString stringWithFormat:@"!%@", className]];
});

it(@"should generate symbol for parent class name", ^{
NSArray *symbols = [parser symbolsInData:[NSData dataWithContentsOfURL:fileUrl]];

[[symbols should] contain:[NSString stringWithFormat:@"!%@", parentClass]];
});
});
});

SPEC_END
8 changes: 4 additions & 4 deletions ios-class-guard Spec/CDSymoblsGeneratorVisitorSpec.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#import <Kiwi/Kiwi.h>

#import "CDSymoblsGeneratorVisitor.h"
#import "CDSymbolsGeneratorVisitor.h"
#import "CDOCProperty.h"
#import "NSString-CDExtensions.h"
#import "CDOCMethod.h"

SPEC_BEGIN(CDSymoblsGeneratorVisitorSpec)

describe(@"CDSymoblsGeneratorVisitor", ^{
__block CDSymoblsGeneratorVisitor *visitor;
describe(@"CDSymbolsGeneratorVisitor", ^{
__block CDSymbolsGeneratorVisitor *visitor;

beforeEach(^{
visitor = [[CDSymoblsGeneratorVisitor alloc] init];
visitor = [[CDSymbolsGeneratorVisitor alloc] init];
});

describe(@"visiting property", ^{
Expand Down
19 changes: 19 additions & 0 deletions ios-class-guard Spec/contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5064" systemVersion="13E28" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="TestEntityA" representedClassName="TestEntityA" syncable="YES">
<attribute name="attributeA" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/>
<relationship name="bs" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="TestEntityB" inverseName="a" inverseEntity="TestEntityB" syncable="YES"/>
</entity>
<entity name="TestEntityB" representedClassName="TestEntityB" syncable="YES">
<attribute name="attributeB" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="a" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="TestEntityA" inverseName="bs" inverseEntity="TestEntityA" syncable="YES"/>
</entity>
<entity name="TestEntityC" representedClassName="TestEntityC" parentEntity="TestParentEntity" syncable="YES"/>
<entity name="TestParentEntity" representedClassName="TestParentEntity" syncable="YES"/>
<elements>
<element name="TestEntityA" positionX="-63" positionY="-18" width="128" height="73"/>
<element name="TestEntityB" positionX="-54" positionY="-9" width="128" height="73"/>
<element name="TestEntityC" positionX="-45" positionY="27" width="128" height="43"/>
<element name="TestParentEntity" positionX="-36" positionY="36" width="128" height="43"/>
</elements>
</model>
Loading

0 comments on commit 828031f

Please sign in to comment.