-
-
Save dreampiggy/d19dc76f109b231ecd8e1e7f6f1eec1a to your computer and use it in GitHub Desktop.
Dynamic UTI decoding
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
extension String | |
{ | |
/// Creates a string by decoding a sequence of UTF-8 code units. | |
init?<S: Sequence>(utf8 input: S) where S.Iterator.Element == UTF8.CodeUnit { | |
var chars: [Character] = [] | |
var decoder = UTF8() | |
var iter = input.makeIterator() | |
loop: while true { | |
switch decoder.decode(&iter) { | |
case .scalarValue(let scalar): chars.append(Character(scalar)) | |
case .emptyInput: break loop | |
case .error: return nil | |
} | |
} | |
self = String(chars) | |
} | |
} | |
/// Decodes a dynamically-generated Uniform Type Identifier for inspection purposes. (**NOT FOR PRODUCTION USE!**) | |
/// Many, many thanks to http://alastairs-place.net/blog/2012/06/06/utis-are-better-than-you-think-and-heres-why/ | |
/// | |
/// <https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html#//apple_ref/doc/uid/TP40001319-CH202-BCGCDHIJ> | |
func decodeDynUTI(_ uti: String) -> String? | |
{ | |
let vec = Array("abcdefghkmnpqrstuvwxyz0123456789") | |
let encoded = Array(uti).suffix(from: 5) | |
var result: [UInt8] = [] | |
var decoded = 0 | |
var decodedBits = 0 | |
for char in encoded { | |
// Each encoded character represents 5 bits (by its position in the length-32 vector). | |
guard let pos = vec.index(of: char) else { | |
print("unrecognized encoded character '\(char)'") | |
return nil | |
} | |
decoded = (decoded << 5) | pos | |
decodedBits += 5 | |
// Every 8 decoded bits represent a UTF-8 code unit. | |
if decodedBits >= 8 { | |
let extra = decodedBits - 8 | |
result.append(UInt8(decoded >> extra)) | |
decoded &= (1 << extra) - 1 | |
decodedBits = extra | |
} | |
} | |
if decoded != 0 { | |
print("\(decodedBits) leftover bits: \(decoded)") | |
return nil | |
} | |
return String(utf8: result) | |
} | |
// Examples | |
decodeDynUTI("dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu") // "?0=6:4=Apple files promise pasteboard type" | |
decodeDynUTI("dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k") // "?0=6:4=NSPromiseContentsPboardType" | |
decodeDynUTI("dyn.ah62d46dzqm0gw23srf4gn5m4ge8068dytf2gn3dgrf0gn25tr34gn5xysr2ge55bsmwhk8puqzsdy4xuq6") // "?0=public.item:1=nxtypedfilecontentspboardtype\:jpg" | |
decodeDynUTI("dyn.ah62d4uv4ge804550") // "?0=B:1=mov" | |
decodeDynUTI("dyn.agq80c6durvy0g2pyrf106p5rsa4a") // "3=application/mp4" | |
decodeDynUTI("dyn.ah62d4rv4gu8ykzwynr11n6xdqzngn8dyn3y0n74qqf1gs7pbq7wza2xtqf3gkzd3sbwu") // "?0=6:4=DVTSourceTextViewLanguagePboardType" | |
decodeDynUTI("dyn.ah62d4rv4gu8ykzwynr11n6xdqzngn8dyn3y0n74xqr11a3nqqf1gs7pbq7wyg55ssvw1u7cuqm10c6xenv61a3k") // "?0=6:4=DVTSourceTextViewScopeLanguageContextPboardType" | |
decodeDynUTI("dyn.ah62d4rv4gu8ykzwynr11n6xdqzngn8dyn3y0n74msra1kuwtmvkge55bsmwfk8puqy") // "?0=6:4=DVTSourceTextViewIsAtBOLPboardType" | |
decodeDynUTI("dyn.ah62d4rv4gk81n65yru") // "?0=6:2=ustl" | |
decodeDynUTI("dyn.ah62d4rv4gk81g7d3ru") // "?0=6:2=styl" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment