Skip to content

Commit

Permalink
Merge pull request #19 from rockbruno/properties
Browse files Browse the repository at this point in the history
Supporting Outlet Methods
  • Loading branch information
Bruno Rocha authored Aug 23, 2018
2 parents 674f6d2 + 884e9b0 commit e299e1f
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 126 deletions.
Binary file modified bin/swiftshield
Binary file not shown.
21 changes: 11 additions & 10 deletions swiftshield-Sources/AutomaticSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class AutomaticSwiftShield: Protector {
let modules = projectBuilder.getModulesAndCompilerArguments()
let modulesToObfuscate = modules.filter { modulesToIgnore.contains($0.name) == false }
let obfuscationData = index(modules: modulesToObfuscate)
obfuscationData.storyboardToObfuscate = modulesToObfuscate.flatMap { $0.xibFiles }
if obfuscationData.obfuscationDict.isEmpty {
Logger.log(.foundNothingError)
exit(error: true)
Expand All @@ -52,7 +53,7 @@ extension AutomaticSwiftShield {
let obfuscationData = ObfuscationData()
var fileDataArray: [(file: File, module: Module)] = []
for module in modules {
for file in module.files {
for file in module.sourceFiles {
fileDataArray.append((file, module))
}
}
Expand Down Expand Up @@ -115,7 +116,7 @@ extension AutomaticSwiftShield {
let dict = SKApi.sourcekitd_response_get_value(indexResponse)
SK.recurseOver(childID: SK.entitiesID, resp: dict, block: { dict in
let kind = dict.getUUIDString(key: SK.kindID)
guard SK.referenceType(kind: kind) != nil else {
guard let type = SK.referenceType(kind: kind) else {
return
}
guard let usr = dict.getString(key: SK.usrID), let name = dict.getString(key: SK.nameID)?.trueName else {
Expand All @@ -127,10 +128,10 @@ extension AutomaticSwiftShield {
//Operators only get indexed as such if they are declared in a global scope
//Unfortunately, most people use public static func
//So we avoid obfuscating methods with small names to prevent obfuscating operators.
guard SK.referenceType(kind: kind) != .method || name.count > 4 else {
if type == .method && name.count <= 4 {
return
}
guard self.isReferencingInternalMethod(kind: kind, dict: dict, obfuscationData: obfuscationData, sourceKit: SK) == false else {
guard self.isReferencingInternal(type: type, kind: kind, dict: dict, obfuscationData: obfuscationData, sourceKit: SK) == false else {
return
}
let newName = obfuscationData.obfuscationDict[name] ?? name
Expand All @@ -143,15 +144,15 @@ extension AutomaticSwiftShield {
overwriteFiles(obfuscationData: obfuscationData)
}

private func isReferencingInternalMethod(kind: String, dict: sourcekitd_variant_t, obfuscationData: ObfuscationData, sourceKit: SourceKit) -> Bool {
guard sourceKit.referenceType(kind: kind) == .method else {
private func isReferencingInternal(type: SourceKit.DeclarationType, kind: String, dict: sourcekitd_variant_t, obfuscationData: ObfuscationData, sourceKit: SourceKit) -> Bool {
guard type == .method || type == .property else {
return false
}
guard let usr = dict.getString(key: sourceKit.usrID) else {
return false
}
if let relDict = obfuscationData.usrRelationDict[usr], relDict.data != dict.data {
return isReferencingInternalMethod(kind: kind, dict: relDict, obfuscationData: obfuscationData, sourceKit: sourceKit)
return isReferencingInternal(type: type, kind: kind, dict: relDict, obfuscationData: obfuscationData, sourceKit: sourceKit)
}
var isReference = false
sourceKit.recurseOver(childID: sourceKit.relatedID, resp: dict) { dict in
Expand All @@ -164,7 +165,7 @@ extension AutomaticSwiftShield {
if obfuscationData.usrDict.contains(usr) == false {
isReference = true
} else if let relDict = obfuscationData.usrRelationDict[usr] {
isReference = self.isReferencingInternalMethod(kind: kind, dict: relDict, obfuscationData: obfuscationData, sourceKit: sourceKit)
isReference = self.isReferencingInternal(type: type, kind: kind, dict: relDict, obfuscationData: obfuscationData, sourceKit: sourceKit)
}
}
return isReference
Expand Down Expand Up @@ -205,9 +206,9 @@ extension AutomaticSwiftShield {
currentCharIndex += 1
}
}
let joined = charArray.joined()
let obfuscatedFile = charArray.joined()
do {
try joined.write(toFile: file.path, atomically: false, encoding: String.Encoding.utf8)
try obfuscatedFile.write(toFile: file.path, atomically: false, encoding: .utf8)
} catch {
Logger.log(.fatal(error: error.localizedDescription))
exit(error: true)
Expand Down
2 changes: 1 addition & 1 deletion swiftshield-Sources/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ enum LogType {
case .finished:
return "Finished."
case .version:
return "SwiftShield 3.1.1"
return "SwiftShield 3.2.0"
case .verbose:
return "Verbose Mode"
case .mode:
Expand Down
1 change: 1 addition & 0 deletions swiftshield-Sources/ManualSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ManualSwiftShield: Protector {
let files = getSourceFiles()
Logger.log(.scanningDeclarations)
var obfsData = ObfuscationData()
obfsData.storyboardToObfuscate = getStoryboardsAndXibs()
files.forEach { protect(file: $0, obfsData: &obfsData) }
return obfsData
}
Expand Down
8 changes: 5 additions & 3 deletions swiftshield-Sources/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import Cocoa

struct Module {
let name: String
let files: [File]
let sourceFiles: [File]
let xibFiles: [File]
let compilerArguments: [String]

init(name: String, files: [File], compilerArguments: [String]) {
init(name: String, sourceFiles: [File], xibFiles: [File], compilerArguments: [String]) {
self.name = name
self.files = files
self.sourceFiles = sourceFiles
self.xibFiles = xibFiles
self.compilerArguments = compilerArguments
}
}
3 changes: 2 additions & 1 deletion swiftshield-Sources/ObfuscationData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ final class ObfuscationData {
var usrRelationDict: [String: sourcekitd_variant_t] = [:]
var indexedFiles: [(File,sourcekitd_response_t)] = []
var allObfuscatedNames: Set<String> = []

var storyboardToObfuscate: [File] = []

func add(reference: ReferenceData, toFile file: File) {
if referencesDict[file] == nil {
referencesDict[file] = []
Expand Down
5 changes: 4 additions & 1 deletion swiftshield-Sources/Protector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Protector {

func protectStoryboards(data obfuscationData: ObfuscationData) {
Logger.log(.overwritingStoryboards)
for file in getStoryboardsAndXibs() {
for file in obfuscationData.storyboardToObfuscate {
Logger.log(.checking(file: file))
//TODO: We can do the index approach here as well instead of replacingOccurences.
let data = try! String(contentsOfFile: file.path, encoding: .utf8)
Expand All @@ -38,6 +38,9 @@ class Protector {
}
Logger.log(.protectedReference(originalName: `class`, protectedName: protectedClass))
overwrittenData = overwrittenData.replacingOccurrences(of: Storyboard.customClass(class: `class`), with: Storyboard.customClass(class: protectedClass))
if `class`.count > 4 {
overwrittenData = overwrittenData.replacingOccurrences(of: Storyboard.actionSelector(method: `class`), with: Storyboard.actionSelector(method: protectedClass))
}
}
guard overwrittenData != data else {
Logger.log(.fileNotModified(file: file))
Expand Down
48 changes: 30 additions & 18 deletions swiftshield-Sources/SKAPI.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Created by John Holdsworth on 19/12/2015.
// Copyright © 2015 John Holdsworth. All rights reserved.
//
// $Id: //depot/Refactorator/refactord/SourceKit.swift#25 $
//
// Repo: https://github.com/johnno1962/Refactorator
//

/** Thanks to: https://github.com/jpsim/SourceKitten/blob/master/Source/SourceKittenFramework/library_wrapper_sourcekitd.swift **/

import Foundation

class SKAPI {
let SKApi = SKAPI()

final class SKAPI {

static var verbose = false

Expand All @@ -19,51 +31,51 @@ class SKAPI {
internal let sourcekitd_request_dictionary_set_stringbuf: @convention(c) (sourcekitd_object_t, sourcekitd_uid_t, UnsafePointer<Int8>, Int) -> () = library.load(symbol: "sourcekitd_request_dictionary_set_stringbuf")
internal let sourcekitd_request_dictionary_set_int64: @convention(c) (sourcekitd_object_t, sourcekitd_uid_t, Int64) -> () = library.load(symbol: "sourcekitd_request_dictionary_set_int64")
internal let sourcekitd_request_dictionary_set_uid: @convention(c) (sourcekitd_object_t, sourcekitd_uid_t, sourcekitd_uid_t) -> () = library.load(symbol: "sourcekitd_request_dictionary_set_uid")
internal let sourcekitd_request_array_create: @convention(c) (UnsafePointer<sourcekitd_object_t?>?, Int) -> (sourcekitd_object_t!) = library.load(symbol: "sourcekitd_request_array_create")
internal let sourcekitd_request_array_create: @convention(c) (UnsafePointer<sourcekitd_object_t?>?, Int) -> (sourcekitd_object_t?) = library.load(symbol: "sourcekitd_request_array_create")
internal let sourcekitd_request_array_set_value: @convention(c) (sourcekitd_object_t, Int, sourcekitd_object_t) -> () = library.load(symbol: "sourcekitd_request_array_set_value")
internal let sourcekitd_request_array_set_string: @convention(c) (sourcekitd_object_t, Int, UnsafePointer<Int8>) -> () = library.load(symbol: "sourcekitd_request_array_set_string")
internal let sourcekitd_request_array_set_stringbuf: @convention(c) (sourcekitd_object_t, Int, UnsafePointer<Int8>, Int) -> () = library.load(symbol: "sourcekitd_request_array_set_stringbuf")
internal let sourcekitd_request_array_set_int64: @convention(c) (sourcekitd_object_t, Int, Int64) -> () = library.load(symbol: "sourcekitd_request_array_set_int64")
internal let sourcekitd_request_array_set_uid: @convention(c) (sourcekitd_object_t, Int, sourcekitd_uid_t) -> () = library.load(symbol: "sourcekitd_request_array_set_uid")
internal let sourcekitd_request_int64_create: @convention(c) (Int64) -> (sourcekitd_object_t!) = library.load(symbol: "sourcekitd_request_int64_create")
internal let sourcekitd_request_string_create: @convention(c) (UnsafePointer<Int8>) -> (sourcekitd_object_t!) = library.load(symbol: "sourcekitd_request_string_create")
internal let sourcekitd_request_uid_create: @convention(c) (sourcekitd_uid_t) -> (sourcekitd_object_t!) = library.load(symbol: "sourcekitd_request_uid_create")
internal let sourcekitd_request_create_from_yaml: @convention(c) (UnsafePointer<Int8>, UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> (sourcekitd_object_t!) = library.load(symbol: "sourcekitd_request_create_from_yaml")
internal let sourcekitd_request_int64_create: @convention(c) (Int64) -> (sourcekitd_object_t?) = library.load(symbol: "sourcekitd_request_int64_create")
internal let sourcekitd_request_string_create: @convention(c) (UnsafePointer<Int8>) -> (sourcekitd_object_t?) = library.load(symbol: "sourcekitd_request_string_create")
internal let sourcekitd_request_uid_create: @convention(c) (sourcekitd_uid_t) -> (sourcekitd_object_t?) = library.load(symbol: "sourcekitd_request_uid_create")
internal let sourcekitd_request_create_from_yaml: @convention(c) (UnsafePointer<Int8>, UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>?) -> (sourcekitd_object_t?) = library.load(symbol: "sourcekitd_request_create_from_yaml")
internal let sourcekitd_request_description_dump: @convention(c) (sourcekitd_object_t) -> () = library.load(symbol: "sourcekitd_request_description_dump")
internal let sourcekitd_request_description_copy: @convention(c) (sourcekitd_object_t) -> (UnsafeMutablePointer<Int8>!) = library.load(symbol: "sourcekitd_request_description_copy")
internal let sourcekitd_request_description_copy: @convention(c) (sourcekitd_object_t) -> (UnsafeMutablePointer<Int8>?) = library.load(symbol: "sourcekitd_request_description_copy")
internal let sourcekitd_response_dispose: @convention(c) (sourcekitd_response_t) -> () = library.load(symbol: "sourcekitd_response_dispose")
internal let sourcekitd_response_is_error: @convention(c) (sourcekitd_response_t) -> (Bool) = library.load(symbol: "sourcekitd_response_is_error")
internal let sourcekitd_response_error_get_kind: @convention(c) (sourcekitd_response_t) -> (sourcekitd_error_t) = library.load(symbol: "sourcekitd_response_error_get_kind")
internal let sourcekitd_response_error_get_description: @convention(c) (sourcekitd_response_t) -> (UnsafePointer<Int8>!) = library.load(symbol: "sourcekitd_response_error_get_description")
internal let sourcekitd_response_error_get_description: @convention(c) (sourcekitd_response_t) -> (UnsafePointer<Int8>?) = library.load(symbol: "sourcekitd_response_error_get_description")
internal let sourcekitd_response_get_value: @convention(c) (sourcekitd_response_t) -> (sourcekitd_variant_t) = library.load(symbol: "sourcekitd_response_get_value")
internal let sourcekitd_variant_get_type: @convention(c) (sourcekitd_variant_t) -> (sourcekitd_variant_type_t) = library.load(symbol: "sourcekitd_variant_get_type")
internal let sourcekitd_variant_dictionary_get_value: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (sourcekitd_variant_t) = library.load(symbol: "sourcekitd_variant_dictionary_get_value")
internal let sourcekitd_variant_dictionary_get_string: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (UnsafePointer<Int8>!) = library.load(symbol: "sourcekitd_variant_dictionary_get_string")
internal let sourcekitd_variant_dictionary_get_string: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (UnsafePointer<Int8>?) = library.load(symbol: "sourcekitd_variant_dictionary_get_string")
internal let sourcekitd_variant_dictionary_get_int64: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (Int64) = library.load(symbol: "sourcekitd_variant_dictionary_get_int64")
internal let sourcekitd_variant_dictionary_get_bool: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (Bool) = library.load(symbol: "sourcekitd_variant_dictionary_get_bool")
internal let sourcekitd_variant_dictionary_get_uid: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (sourcekitd_uid_t!) = library.load(symbol: "sourcekitd_variant_dictionary_get_uid")
internal let sourcekitd_variant_dictionary_get_uid: @convention(c) (sourcekitd_variant_t, sourcekitd_uid_t) -> (sourcekitd_uid_t?) = library.load(symbol: "sourcekitd_variant_dictionary_get_uid")
internal let sourcekitd_variant_dictionary_apply_f: @convention(c) (sourcekitd_variant_t, @escaping sourcekitd_variant_dictionary_applier_f_t, UnsafeMutableRawPointer?) -> (Bool) = library.load(symbol: "sourcekitd_variant_dictionary_apply_f")
internal let sourcekitd_variant_array_get_count: @convention(c) (sourcekitd_variant_t) -> (Int) = library.load(symbol: "sourcekitd_variant_array_get_count")
internal let sourcekitd_variant_array_get_value: @convention(c) (sourcekitd_variant_t, Int) -> (sourcekitd_variant_t) = library.load(symbol: "sourcekitd_variant_array_get_value")
internal let sourcekitd_variant_array_get_string: @convention(c) (sourcekitd_variant_t, Int) -> (UnsafePointer<Int8>!) = library.load(symbol: "sourcekitd_variant_array_get_string")
internal let sourcekitd_variant_array_get_string: @convention(c) (sourcekitd_variant_t, Int) -> (UnsafePointer<Int8>?) = library.load(symbol: "sourcekitd_variant_array_get_string")
internal let sourcekitd_variant_array_get_int64: @convention(c) (sourcekitd_variant_t, Int) -> (Int64) = library.load(symbol: "sourcekitd_variant_array_get_int64")
internal let sourcekitd_variant_array_get_bool: @convention(c) (sourcekitd_variant_t, Int) -> (Bool) = library.load(symbol: "sourcekitd_variant_array_get_bool")
internal let sourcekitd_variant_array_get_uid: @convention(c) (sourcekitd_variant_t, Int) -> (sourcekitd_uid_t!) = library.load(symbol: "sourcekitd_variant_array_get_uid")
internal let sourcekitd_variant_array_get_uid: @convention(c) (sourcekitd_variant_t, Int) -> (sourcekitd_uid_t?) = library.load(symbol: "sourcekitd_variant_array_get_uid")
internal let sourcekitd_variant_array_apply_f: @convention(c) (sourcekitd_variant_t, @escaping sourcekitd_variant_array_applier_f_t, UnsafeMutableRawPointer?) -> (Bool) = library.load(symbol: "sourcekitd_variant_array_apply_f")
internal let sourcekitd_variant_array_apply: @convention(c) (sourcekitd_variant_t, @escaping sourcekitd_variant_array_applier_t) -> (Bool) = library.load(symbol: "sourcekitd_variant_array_apply")
internal let sourcekitd_variant_int64_get_value: @convention(c) (sourcekitd_variant_t) -> (Int64) = library.load(symbol: "sourcekitd_variant_int64_get_value")
internal let sourcekitd_variant_bool_get_value: @convention(c) (sourcekitd_variant_t) -> (Bool) = library.load(symbol: "sourcekitd_variant_bool_get_value")
internal let sourcekitd_variant_string_get_length: @convention(c) (sourcekitd_variant_t) -> (Int) = library.load(symbol: "sourcekitd_variant_string_get_length")
internal let sourcekitd_variant_string_get_ptr: @convention(c) (sourcekitd_variant_t) -> (UnsafePointer<Int8>!) = library.load(symbol: "sourcekitd_variant_string_get_ptr")
internal let sourcekitd_variant_uid_get_value: @convention(c) (sourcekitd_variant_t) -> (sourcekitd_uid_t!) = library.load(symbol: "sourcekitd_variant_uid_get_value")
internal let sourcekitd_variant_string_get_ptr: @convention(c) (sourcekitd_variant_t) -> (UnsafePointer<Int8>?) = library.load(symbol: "sourcekitd_variant_string_get_ptr")
internal let sourcekitd_variant_uid_get_value: @convention(c) (sourcekitd_variant_t) -> (sourcekitd_uid_t?) = library.load(symbol: "sourcekitd_variant_uid_get_value")
internal let sourcekitd_response_description_dump: @convention(c) (sourcekitd_response_t) -> () = library.load(symbol: "sourcekitd_response_description_dump")
internal let sourcekitd_response_description_dump_filedesc: @convention(c) (sourcekitd_response_t, Int32) -> () = library.load(symbol: "sourcekitd_response_description_dump_filedesc")
internal let sourcekitd_response_description_copy: @convention(c) (sourcekitd_response_t) -> (UnsafeMutablePointer<Int8>!) = library.load(symbol: "sourcekitd_response_description_copy")
internal let sourcekitd_response_description_copy: @convention(c) (sourcekitd_response_t) -> (UnsafeMutablePointer<Int8>?) = library.load(symbol: "sourcekitd_response_description_copy")
internal let sourcekitd_variant_description_dump: @convention(c) (sourcekitd_variant_t) -> () = library.load(symbol: "sourcekitd_variant_description_dump")
internal let sourcekitd_variant_description_dump_filedesc: @convention(c) (sourcekitd_variant_t, Int32) -> () = library.load(symbol: "sourcekitd_variant_description_dump_filedesc")
internal let sourcekitd_variant_description_copy: @convention(c) (sourcekitd_variant_t) -> (UnsafeMutablePointer<Int8>!) = library.load(symbol: "sourcekitd_variant_description_copy")
internal let sourcekitd_variant_json_description_copy: @convention(c) (sourcekitd_variant_t) -> (UnsafeMutablePointer<Int8>!) = library.load(symbol: "sourcekitd_variant_json_description_copy")
internal let sourcekitd_send_request_sync: @convention(c) (sourcekitd_object_t) -> (sourcekitd_response_t!) = library.load(symbol: "sourcekitd_send_request_sync")
internal let sourcekitd_variant_description_copy: @convention(c) (sourcekitd_variant_t) -> (UnsafeMutablePointer<Int8>?) = library.load(symbol: "sourcekitd_variant_description_copy")
internal let sourcekitd_variant_json_description_copy: @convention(c) (sourcekitd_variant_t) -> (UnsafeMutablePointer<Int8>?) = library.load(symbol: "sourcekitd_variant_json_description_copy")
internal let sourcekitd_send_request_sync: @convention(c) (sourcekitd_object_t) -> (sourcekitd_response_t?) = library.load(symbol: "sourcekitd_send_request_sync")
internal let sourcekitd_send_request: @convention(c) (sourcekitd_object_t, UnsafeMutablePointer<sourcekitd_request_handle_t?>?, sourcekitd_response_receiver_t?) -> () = library.load(symbol: "sourcekitd_send_request")

}
Loading

0 comments on commit e299e1f

Please sign in to comment.