Skip to content

Instantly share code, notes, and snippets.

@jazzbox
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzbox/78d8f2727c4fa76db906 to your computer and use it in GitHub Desktop.
Save jazzbox/78d8f2727c4fa76db906 to your computer and use it in GitHub Desktop.
// Unobfuscate the iRealPro data format
//
// information and test data taken from:
// https://github.com/ironss/accompaniser/blob/master/irealb_parser.lua
// https://github.com/ironss/accompaniser/blob/59fd4cfb45447ac77ae7cc2bfca2e25bc9ce218d/test/test_irealb_parser.lua
// ********
import Darwin
func flatten<T>(a: [[T]]) -> [T] {
return reduce(a, [] as [T]) { $0 + $1 }
}
func partition<S: SequenceType>(seq: S, count: Int, partial: Bool = false) -> SequenceOf<[S.Generator.Element]> {
typealias ResultType = [S.Generator.Element]
return SequenceOf { ()->GeneratorOf<ResultType> in
var generator = seq.generate()
let range = 0..<count
return GeneratorOf {
var result: ResultType = []
for _ in range {
let elem = generator.next()
if let elem = elem {
result.append(elem)
} else {
return partial && result.count > 0 ? result : nil
}
}
return result
}
}
}
// ********
func obfusc50(a: [Character]) -> [Character] {
let slices = [
a[45...49].reverse(),
a[5...9],
a[26...39].reverse(),
a[24...25],
a[10...23].reverse(),
a[40...44],
a[0...4].reverse()
]
return flatten( slices.map { Array($0) })
}
func obfusc(s: String) -> String {
let p1 = map(partition(s, 50, partial: true)) { Array($0) }
let p2 = dropFirst(p1.map { $0.count > 1 } + [false])
return join("", map(Zip2(p1,p2)) { (a: [Character], obfuscated: Bool) in String( obfuscated ? obfusc50(a) : a) })
}
func unobfusc(s: String) -> String {
return obfusc(s)
}
// ********
func unobfusc(# test: String, # tune: String, # expected: String) {
if unobfusc(tune) != expected {
println("unobfusc fail \(test)")
}
if obfusc(expected) != tune {
println("obfusc fail \(test)")
}
}
unobfusc(test:"50", tune: "[T44A BLZC DLZE FLZG, ALZA BLZC DLZE FLZG ALZA B | ", expected: "[T44A BLZC DLZE FLZG, ALZA BLZC DLZE FLZG ALZA B | ")
unobfusc(test:"51", tune:"| B A BLZCZLF EZLD CZLB ZALA ,GZLF EZLD G ALZA44T[C ", expected: "[T44A BLZC DLZE FLZG, ALZA BLZC DLZE FLZG ALZA B |C ")
unobfusc(test:"100", tune: "ZLB A BLZCZLF EZLD CZLB ZALA ,GZLF EZLD G ALZA44T[C- DLZE FLZG A,LZA BLZC DLZE FLZG ALZA BLZC- D |E ", expected: "[T44A BLZC DLZE FLZG, ALZA BLZC DLZE FLZG ALZA BLZC- DLZE FLZG A,LZA BLZC DLZE FLZG ALZA BLZC- D |E ")
unobfusc(test:"101", tune: "ZLB A BLZCZLF EZLD CZLB ZALA ,GZLF EZLD G ALZA44T[ EZLDZE FLB AZLA GZLF EZDL CZLB AZL,A GZLZC- LD -CF ", expected: "[T44A BLZC DLZE FLZG, ALZA BLZC DLZE FLZG ALZA BLZC- DLZE FLZG A,LZA BLZC DLZE FLZG ALZA BLZC- DLZE F ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment