1/* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16export class StringBase64 { 17 static readonly TO_BASE64_TABLE : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/"; 18 static readonly BASE64PAD : char = c'='; 19 static readonly TO_BINARY_TABLE : int[] = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -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, -1, -1, -1, -1, -1]; 20 final toBase64(data : String): String { 21 let result : StringBuilder = new StringBuilder(); 22 let length : int = data.length as int; 23 let i : int ; 24 for (i = 0; i < (length - 2); i += 3) { 25 result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i) >> 2)); 26 result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i) & 0x03) << 4) + (data.charAt(i + 1) >> 4))); 27 result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i + 1) & 0x0f) << 2) + (data.charAt(i + 2) >> 6))); 28 result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i + 2) & 0x3f)); 29 } 30 if (length % 3 != 0) { 31 i = length - (length % 3); 32 result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i) >> 2)); 33 if ((length % 3) == 2) { 34 result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i) & 0x03) << 4) + (data.charAt(i + 1) >> 4))); 35 result.append(StringBase64.TO_BASE64_TABLE.charAt((data.charAt(i + 1) & 0x0f) << 2)); 36 result.append(StringBase64.BASE64PAD); 37 } 38 else { 39 result.append(StringBase64.TO_BASE64_TABLE.charAt((data.charAt(i) & 0x03) << 4)); 40 result.append(StringBase64.BASE64PAD); 41 result.append(StringBase64.BASE64PAD); 42 } 43 } 44 return result.toString(); 45 } 46 final base64ToString(data : String): String { 47 let result : StringBuilder = new StringBuilder(); 48 let leftbits : int = 0; 49 let leftdata : int = 0; 50 for (let i : int = 0; i < data.length; i++) { 51 let c : int = StringBase64.TO_BINARY_TABLE[data.charAt(i) & 0x7f]; 52 let padding : boolean = data.charAt(i) == StringBase64.BASE64PAD; 53 if (c == -1) { 54 continue; 55 } 56 leftdata = (leftdata << 6) | c; 57 leftbits += 6; 58 if (leftbits >= 8) { 59 leftbits -= 8; 60 if (!padding) { 61 result.append(((leftdata >> leftbits) & 0xff) as char); 62 } 63 leftdata &= (1 << leftbits) - 1; 64 } 65 } 66 if (leftbits != 0) { 67 } 68 return result.toString(); 69 } 70 n1 : int = 4096; 71 n2 : int = 16384; 72 public run(): void { 73 let str : String = ""; 74 for (let i : int = 0; i < this.n1; i++) { 75 str += ((25 * random()) + 97) as char; 76 } 77 for (let i : int = this.n1; i <= this.n2; i *= 2) { 78 let base64 : String = this.toBase64(str); 79 let encoded : String = this.base64ToString(base64); 80 if (!encoded.equals(str)) { 81 } 82 str += str; 83 } 84// Consumer.consumeObj(str); 85 } 86} 87 88function main(): void { 89 let a = new StringBase64; 90 a.run(); 91} 92