0001    // Base64.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    private class Base64Encoder
Base64.swift:54
		guard value <= 64 else { return Base64Encoder.paddingChar }
Base64.swift:81
					output.append(Base64Encoder.newlineChar)
Base64.swift:101
			output.append(Base64Encoder.paddingChar)
Base64.swift:102
			output.append(Base64Encoder.paddingChar)
Base64.swift:105
			output.append(Base64Encoder.paddingChar)
Base64.swift:108
			output.append(Base64Encoder.newlineChar)
Base64.swift:175
		let encoder = Base64Encoder(bytes: data.uBytes, charsPerLine: charsPerLine, specialChars: specialChars)
{ 0026 0027 enum Step
Base64.swift:34
	var step: Step = .A
{ case A
Base64.swift:34
	var step: Step = .A
Base64.swift:63
		case .A:
Base64.swift:85
			step = .A
Base64.swift:97
		case .A:
, B
Base64.swift:67
			step = .B
Base64.swift:68
		case .B:
Base64.swift:99
		case .B:
, C
Base64.swift:72
			step = .C
Base64.swift:73
		case .C:
Base64.swift:103
		case .C:
} 0028 0029 static let paddingChar
Base64.swift:54
		guard value <= 64 else { return Base64Encoder.paddingChar }
Base64.swift:101
			output.append(Base64Encoder.paddingChar)
Base64.swift:102
			output.append(Base64Encoder.paddingChar)
Base64.swift:105
			output.append(Base64Encoder.paddingChar)
: UInt8 = 0x3D // = 0030 static let newlineChar
Base64.swift:81
					output.append(Base64Encoder.newlineChar)
Base64.swift:108
			output.append(Base64Encoder.newlineChar)
: UInt8 = 0x0A // \n 0031 0032 let chars
Base64.swift:48
		self.chars = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".unicodeScalars) + Array((specialChars ?? "+/").unicodeScalars)
Base64.swift:55
		return UInt8(chars[Int(value)].value)
: [UnicodeScalar] 0033 0034 var step
Base64.swift:62
		switch step {
Base64.swift:67
			step = .B
Base64.swift:72
			step = .C
Base64.swift:85
			step = .A
Base64.swift:96
		switch step {
: Step = .A 0035 var result
Base64.swift:64
			result = (fragment & 0x0fc) >> 2
Base64.swift:65
			output.append(encodeValue(result))
Base64.swift:66
			result = (fragment & 0x003) << 4
Base64.swift:69
			result |= (fragment & 0x0f0) >> 4
Base64.swift:70
			output.append(encodeValue(result))
Base64.swift:71
			result = (fragment & 0x00f) << 2
Base64.swift:74
			result |= (fragment & 0x0c0) >> 6
Base64.swift:75
			output.append(encodeValue(result))
Base64.swift:76
			result  = (fragment & 0x03f) >> 0
Base64.swift:77
			output.append(encodeValue(result))
Base64.swift:100
			output.append(encodeValue(result))
Base64.swift:104
			output.append(encodeValue(result))
: UInt8 = 0 0036 0037 var charsPerLine
Base64.swift:46
		self.charsPerLine = charsPerLine
Base64.swift:78
			if let charsPerLine = self.charsPerLine {
Base64.swift:107
		if let _ = self.charsPerLine {
: Int? 0038 var stepcount
Base64.swift:79
				stepcount++
Base64.swift:80
				if stepcount == charsPerLine/4 {
Base64.swift:82
					stepcount = 0
: Int = 0 0039 0040 let bytes
Base64.swift:47
		self.bytes = bytes
Base64.swift:59
		let fragment = bytes[offset]
Base64.swift:88
		if offset < bytes.count {
: [UInt8] 0041 0042 var offset
Base64.swift:59
		let fragment = bytes[offset]
Base64.swift:60
		offset++
Base64.swift:88
		if offset < bytes.count {
= 0 0043 var output
Base64.swift:65
			output.append(encodeValue(result))
Base64.swift:70
			output.append(encodeValue(result))
Base64.swift:75
			output.append(encodeValue(result))
Base64.swift:77
			output.append(encodeValue(result))
Base64.swift:81
					output.append(Base64Encoder.newlineChar)
Base64.swift:100
			output.append(encodeValue(result))
Base64.swift:101
			output.append(Base64Encoder.paddingChar)
Base64.swift:102
			output.append(Base64Encoder.paddingChar)
Base64.swift:104
			output.append(encodeValue(result))
Base64.swift:105
			output.append(Base64Encoder.paddingChar)
Base64.swift:108
			output.append(Base64Encoder.newlineChar)
Base64.swift:176
		return Data(uBytes: encoder.output)
: [UInt8] = [] 0044 0045 init
Base64.swift:175
		let encoder = Base64Encoder(bytes: data.uBytes, charsPerLine: charsPerLine, specialChars: specialChars)
(bytes: [UInt8], charsPerLine: Int? = nil, specialChars: String? = nil) { 0046 self.charsPerLine = charsPerLine 0047 self.bytes = bytes 0048 self.chars = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".unicodeScalars) + Array((specialChars ?? "+/").unicodeScalars) 0049 guard bytes.count > 0 else { return } 0050 encodeBlock() 0051 } 0052 0053 func encodeValue
Base64.swift:65
			output.append(encodeValue(result))
Base64.swift:70
			output.append(encodeValue(result))
Base64.swift:75
			output.append(encodeValue(result))
Base64.swift:77
			output.append(encodeValue(result))
Base64.swift:100
			output.append(encodeValue(result))
Base64.swift:104
			output.append(encodeValue(result))
(value: UInt8) -> UInt8 { 0054 guard value <= 64 else { return Base64Encoder.paddingChar } 0055 return UInt8(chars[Int(value)].value) 0056 } 0057 0058 func encodeBlock
Base64.swift:50
		encodeBlock()
Base64.swift:89
			encodeBlock()
() { 0059 let fragment = bytes[offset] 0060 offset++ 0061 0062 switch step { 0063 case .A: 0064 result = (fragment & 0x0fc) >> 2 0065 output.append(encodeValue(result)) 0066 result = (fragment & 0x003) << 4 0067 step = .B 0068 case .B: 0069 result |= (fragment & 0x0f0) >> 4 0070 output.append(encodeValue(result)) 0071 result = (fragment & 0x00f) << 2 0072 step = .C 0073 case .C: 0074 result |= (fragment & 0x0c0) >> 6 0075 output.append(encodeValue(result)) 0076 result = (fragment & 0x03f) >> 0 0077 output.append(encodeValue(result)) 0078 if let charsPerLine = self.charsPerLine { 0079 stepcount++ 0080 if stepcount == charsPerLine/4 { 0081 output.append(Base64Encoder.newlineChar) 0082 stepcount = 0 0083 } 0084 } 0085 step = .A 0086 } 0087 0088 if offset < bytes.count { 0089 encodeBlock() 0090 } else { 0091 encodeBlockEnd() 0092 } 0093 } 0094 0095 func encodeBlockEnd
Base64.swift:91
			encodeBlockEnd()
() { 0096 switch step { 0097 case .A: 0098 break 0099 case .B: 0100 output.append(encodeValue(result)) 0101 output.append(Base64Encoder.paddingChar) 0102 output.append(Base64Encoder.paddingChar) 0103 case .C: 0104 output.append(encodeValue(result)) 0105 output.append(Base64Encoder.paddingChar) 0106 } 0107 if let _ = self.charsPerLine { 0108 output.append(Base64Encoder.newlineChar) 0109 } 0110 } 0111 0112 } 0113 0114 private class Base64Decoder
Base64.swift:122
		guard tmp >= 0 && tmp < Base64Decoder.decoding.count else { return -1 }
Base64.swift:123
		return Base64Decoder.decoding[tmp]
Base64.swift:146
			tmpFragment = Base64Decoder.decodeValue(byte)
Base64.swift:180
		let decoder = Base64Decoder(bytes: data.uBytes)
{ 0115 0116 enum Step
Base64.swift:126
	var step: Step = .A
{ case A
Base64.swift:126
	var step: Step = .A
Base64.swift:151
		case .A:
Base64.swift:164
			step = .A
, B
Base64.swift:153
			step = .B
Base64.swift:154
		case .B:
, C
Base64.swift:157
			step = .C
Base64.swift:158
		case .C:
, D
Base64.swift:161
			step = .D
Base64.swift:162
		case .D:
} 0117 0118 static let decoding
Base64.swift:122
		guard tmp >= 0 && tmp < Base64Decoder.decoding.count else { return -1 }
Base64.swift:123
		return Base64Decoder.decoding[tmp]
: [Int8] = [62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51] 0119 0120 static func decodeValue
Base64.swift:146
			tmpFragment = Base64Decoder.decodeValue(byte)
(value: UInt8) -> Int8 { 0121 let tmp = Int(value) - 43 0122 guard tmp >= 0 && tmp < Base64Decoder.decoding.count else { return -1 } 0123 return Base64Decoder.decoding[tmp] 0124 } 0125 0126 var step
Base64.swift:150
		switch step {
Base64.swift:153
			step = .B
Base64.swift:157
			step = .C
Base64.swift:161
			step = .D
Base64.swift:164
			step = .A
: Step = .A 0127 0128 let bytes
Base64.swift:135
		self.bytes = bytes
Base64.swift:144
			guard offset < bytes.count else { return }
Base64.swift:145
			let byte = bytes[offset++]
: [UInt8] 0129 var offset
Base64.swift:144
			guard offset < bytes.count else { return }
Base64.swift:145
			let byte = bytes[offset++]
= 0 0130 0131 var output
Base64.swift:136
		self.output = [UInt8](count: bytes.count, repeatedValue: 0)
Base64.swift:152
			output[outputOffset]	 = (fragment & 0x03f) << 2
Base64.swift:155
			output[outputOffset++]	|= (fragment & 0x030) >> 4
Base64.swift:156
			output[outputOffset]	 = (fragment & 0x00f) << 4
Base64.swift:159
			output[outputOffset++]	|= (fragment & 0x03c) >> 2
Base64.swift:160
			output[outputOffset]	 = (fragment & 0x003) << 6
Base64.swift:163
			output[outputOffset++]	|= (fragment & 0x03f)
Base64.swift:181
		return Data(uBytes: decoder.output)
: [UInt8] 0132 var outputOffset
Base64.swift:152
			output[outputOffset]	 = (fragment & 0x03f) << 2
Base64.swift:155
			output[outputOffset++]	|= (fragment & 0x030) >> 4
Base64.swift:156
			output[outputOffset]	 = (fragment & 0x00f) << 4
Base64.swift:159
			output[outputOffset++]	|= (fragment & 0x03c) >> 2
Base64.swift:160
			output[outputOffset]	 = (fragment & 0x003) << 6
Base64.swift:163
			output[outputOffset++]	|= (fragment & 0x03f)
= 0 0133 0134 init
Base64.swift:180
		let decoder = Base64Decoder(bytes: data.uBytes)
(bytes: [UInt8]) { 0135 self.bytes = bytes 0136 self.output = [UInt8](count: bytes.count, repeatedValue: 0) 0137 guard bytes.count > 0 else { return } 0138 decodeBlock() 0139 } 0140 0141 func decodeBlock
Base64.swift:138
		decodeBlock()
Base64.swift:167
		decodeBlock()
() { 0142 var tmpFragment: Int8 0143 repeat { 0144 guard offset < bytes.count else { return } 0145 let byte = bytes[offset++] 0146 tmpFragment = Base64Decoder.decodeValue(byte) 0147 } while (tmpFragment < 0) 0148 let fragment = UInt8(bitPattern: tmpFragment) 0149 0150 switch step { 0151 case .A: 0152 output[outputOffset] = (fragment & 0x03f) << 2 0153 step = .B 0154 case .B: 0155 output[outputOffset++] |= (fragment & 0x030) >> 4 0156 output[outputOffset] = (fragment & 0x00f) << 4 0157 step = .C 0158 case .C: 0159 output[outputOffset++] |= (fragment & 0x03c) >> 2 0160 output[outputOffset] = (fragment & 0x003) << 6 0161 step = .D 0162 case .D: 0163 output[outputOffset++] |= (fragment & 0x03f) 0164 step = .A 0165 } 0166 0167 decodeBlock() 0168 } 0169 0170 } 0171 0172 public final class Base64 { 0173 0174 public static func encode(data: Data, charsPerLine: Int? = nil, specialChars: String? = nil) -> Data { 0175 let encoder = Base64Encoder(bytes: data.uBytes, charsPerLine: charsPerLine, specialChars: specialChars) 0176 return Data(uBytes: encoder.output) 0177 } 0178 0179 public static func decode(data: Data) -> Data { 0180 let decoder = Base64Decoder(bytes: data.uBytes) 0181 return Data(uBytes: decoder.output) 0182 } 0183 0184 } 0185