blob: 156018d01a95ae4c0f54b250447c5d13f357d355 [file] [log] [blame]
HojjatK 💾d67e5c22023-06-05 19:17:26 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20import Foundation
21
22public class TBase64Utils {
23 private static let EncodeTable: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
24 private static let NA: UInt8 = UInt8(255)
25
26 private static let DecodeTable: [UInt8] = [
27 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
28 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
29 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 62, NA, NA, NA, 63,
30 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NA, NA, NA, NA, NA, NA,
31 NA, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
32 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, NA, NA, NA, NA, NA,
33 NA, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
34 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, NA, NA, NA, NA, NA,
35 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
36 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
37 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
38 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
39 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
40 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
41 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
42 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
43 ]
44
45 static func encode(src: [UInt8], srcOff: Int, len: Int, dst: inout [UInt8], dstOff: Int) {
46 if (src.count == 0) {
47 return
48 }
49 var index: UInt8 = 0
50
51 index = src[srcOff] >> 2 & 0x3F
52 dst[dstOff] = EncodeTable[Int(index)].asciiValue!
53
54 if (len == 3) {
55 index = ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)
56 dst[dstOff + 1] = EncodeTable[Int(index)].asciiValue!
57
58 index = ((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)
59 dst[dstOff + 2] = EncodeTable[Int(index)].asciiValue!
60
61 index = (src[srcOff + 2] & 0x3F)
62 dst[dstOff + 3] = EncodeTable[Int(index)].asciiValue!
63 } else if (len == 2) {
64 index = ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)
65 dst[dstOff + 1] = EncodeTable[Int(index)].asciiValue!
66
67 index = ((src[srcOff + 1] << 2) & 0x3C)
68 dst[dstOff + 2] = EncodeTable[Int(index)].asciiValue!
69 } else {
70 // len == 1
71 index = ((src[srcOff] << 4) & 0x30)
72 dst[dstOff + 1] = EncodeTable[Int(index)].asciiValue!
73 }
74 }
75
76 static func decode(src: [UInt8], srcOff: Int, len: Int, dst: inout [UInt8], dstOff: Int) {
77 if (src.count == 0) {
78 return
79 }
80
81 dst[dstOff] = (DecodeTable[Int(src[srcOff] & 0x0FF)] << 2) | (DecodeTable[Int(src[srcOff + 1] & 0x0FF)] >> 4)
82 if (len > 2) {
83 dst[dstOff + 1] = ((DecodeTable[Int(src[srcOff + 1] & 0x0FF)] << 4) & 0xF0) | (DecodeTable[Int(src[srcOff + 2] & 0x0FF)] >> 2)
84 if (len > 3) {
85 dst[dstOff + 2] = ((DecodeTable[Int(src[srcOff + 2] & 0x0FF)] << 6) & 0xC0) | (DecodeTable[Int(src[srcOff + 3] & 0x0FF)])
86 }
87 }
88 }
89}