0001
0025 @_exported import Data
0026
0027 public struct Base64| Base64.swift:136 | extension Base64 { |
{
0028 public static func decode(string: String) throws -> Data {
0029 let ascii: [Byte] = [
0030 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0031 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0032 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 62, 64, 63,
0033 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
0034 64, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14,
0035 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 63,
0036 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
0037 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
0038 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0039 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0040 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0041 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0042 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0043 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0044 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0045 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0046 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
0047 ]
0048
0049 var decoded = Data()
0050 var unreadBytes = 0
0051
0052 for character in string.utf8 {
0053 if ascii[Int(character)] > 63 {
0054 break
0055 }
0056
0057 unreadBytes += 1
0058 }
0059
0060 func byte(index: Int) -> Int {
0061 return Int(Array(string.utf8)[index])
0062 }
0063
0064 var index = 0
0066
0067 while unreadBytes > 4 {
0068 decoded.appendByte(ascii[byte(index + 0)] << 2 | ascii[byte(index + 1)] >> 4)
0069 decoded.appendByte(ascii[byte(index + 1)] << 4 | ascii[byte(index + 2)] >> 2)
0070 decoded.appendByte(ascii[byte(index + 2)] << 6 | ascii[byte(index + 3)])
0071 index += 4
0072 unreadBytes -= 4
0073 }
0074
0075 if unreadBytes > 1 {
0076 decoded.appendByte(ascii[byte(index + 0)] << 2 | ascii[byte(index + 1)] >> 4)
0077 }
0078
0079 if unreadBytes > 2 {
0080 decoded.appendByte(ascii[byte(index + 1)] << 4 | ascii[byte(index + 2)] >> 2)
0081 }
0082
0083 if unreadBytes > 3 {
0084 decoded.appendByte(ascii[byte(index + 2)] << 6 | ascii[byte(index + 3)])
0085 }
0086
0087 return decoded
0088 }
0089
0090 public static func encode(data: Data, specialChars: String = "+/", paddingChar: Character? = "=") throws -> String {
0091 let base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + specialChars
0092 var encoded: String = ""
0093
0094 func appendCharacterFromBase(character: Int) {
0095 encoded.append(base64[base64.startIndex.advancedBy(character)])
0096 }
0097
0098 func byte(index: Int) -> Int {
0099 return Int(data[index])
0100 }
0101
0102 let decodedBytes = data.map { Int($0) }
0103
0104 var i = 0
0105
0106 while i < decodedBytes.count - 2 {
0107 appendCharacterFromBase(( byte(i) >> 2) & 0x3F)
0108 appendCharacterFromBase(((byte(i) & 0x3) << 4) | ((byte(i + 1) & 0xF0) >> 4))
0109 appendCharacterFromBase(((byte(i + 1) & 0xF) << 2) | ((byte(i + 2) & 0xC0) >> 6))
0110 appendCharacterFromBase( byte(i + 2) & 0x3F)
0111 i += 3
0112 }
0113
0114 if i < decodedBytes.count {
0115 appendCharacterFromBase((byte(i) >> 2) & 0x3F)
0116
0117 if i == decodedBytes.count - 1 {
0118 appendCharacterFromBase(((byte(i) & 0x3) << 4))
0119 if let paddingChar = paddingChar {
0120 encoded.append(paddingChar)
0121 }
0122 } else {
0123 appendCharacterFromBase(((byte(i) & 0x3) << 4) | ((byte(i + 1) & 0xF0) >> 4))
0124 appendCharacterFromBase(((byte(i + 1) & 0xF) << 2))
0125 }
0126
0127 if let paddingChar = paddingChar {
0128 encoded.append(paddingChar)
0129 }
0130 }
0131
0132 return encoded
0133 }
0134 }
0135
0136 extension Base64 {
0137 public static func encode| Base64.swift:90 | public static func encode(data: Data, specialChars: String = "+/", paddingChar: Character? = "=") throws -> String { |
(convertible: DataConvertible) throws -> String {
0138 return try encode(convertible.data)
0139 }
0140 }