13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciexport class StringBase64 { 173af6ab5fSopenharmony_ci static readonly TO_BASE64_TABLE : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/"; 183af6ab5fSopenharmony_ci static readonly BASE64PAD : char = c'='; 193af6ab5fSopenharmony_ci 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]; 203af6ab5fSopenharmony_ci final toBase64(data : String): String { 213af6ab5fSopenharmony_ci let result : StringBuilder = new StringBuilder(); 223af6ab5fSopenharmony_ci let length : int = data.length as int; 233af6ab5fSopenharmony_ci let i : int ; 243af6ab5fSopenharmony_ci for (i = 0; i < (length - 2); i += 3) { 253af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i) >> 2)); 263af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i) & 0x03) << 4) + (data.charAt(i + 1) >> 4))); 273af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i + 1) & 0x0f) << 2) + (data.charAt(i + 2) >> 6))); 283af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i + 2) & 0x3f)); 293af6ab5fSopenharmony_ci } 303af6ab5fSopenharmony_ci if (length % 3 != 0) { 313af6ab5fSopenharmony_ci i = length - (length % 3); 323af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(data.charAt(i) >> 2)); 333af6ab5fSopenharmony_ci if ((length % 3) == 2) { 343af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt(((data.charAt(i) & 0x03) << 4) + (data.charAt(i + 1) >> 4))); 353af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt((data.charAt(i + 1) & 0x0f) << 2)); 363af6ab5fSopenharmony_ci result.append(StringBase64.BASE64PAD); 373af6ab5fSopenharmony_ci } 383af6ab5fSopenharmony_ci else { 393af6ab5fSopenharmony_ci result.append(StringBase64.TO_BASE64_TABLE.charAt((data.charAt(i) & 0x03) << 4)); 403af6ab5fSopenharmony_ci result.append(StringBase64.BASE64PAD); 413af6ab5fSopenharmony_ci result.append(StringBase64.BASE64PAD); 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci } 443af6ab5fSopenharmony_ci return result.toString(); 453af6ab5fSopenharmony_ci } 463af6ab5fSopenharmony_ci final base64ToString(data : String): String { 473af6ab5fSopenharmony_ci let result : StringBuilder = new StringBuilder(); 483af6ab5fSopenharmony_ci let leftbits : int = 0; 493af6ab5fSopenharmony_ci let leftdata : int = 0; 503af6ab5fSopenharmony_ci for (let i : int = 0; i < data.length; i++) { 513af6ab5fSopenharmony_ci let c : int = StringBase64.TO_BINARY_TABLE[data.charAt(i) & 0x7f]; 523af6ab5fSopenharmony_ci let padding : boolean = data.charAt(i) == StringBase64.BASE64PAD; 533af6ab5fSopenharmony_ci if (c == -1) { 543af6ab5fSopenharmony_ci continue; 553af6ab5fSopenharmony_ci } 563af6ab5fSopenharmony_ci leftdata = (leftdata << 6) | c; 573af6ab5fSopenharmony_ci leftbits += 6; 583af6ab5fSopenharmony_ci if (leftbits >= 8) { 593af6ab5fSopenharmony_ci leftbits -= 8; 603af6ab5fSopenharmony_ci if (!padding) { 613af6ab5fSopenharmony_ci result.append(((leftdata >> leftbits) & 0xff) as char); 623af6ab5fSopenharmony_ci } 633af6ab5fSopenharmony_ci leftdata &= (1 << leftbits) - 1; 643af6ab5fSopenharmony_ci } 653af6ab5fSopenharmony_ci } 663af6ab5fSopenharmony_ci if (leftbits != 0) { 673af6ab5fSopenharmony_ci } 683af6ab5fSopenharmony_ci return result.toString(); 693af6ab5fSopenharmony_ci } 703af6ab5fSopenharmony_ci n1 : int = 4096; 713af6ab5fSopenharmony_ci n2 : int = 16384; 723af6ab5fSopenharmony_ci public run(): void { 733af6ab5fSopenharmony_ci let str : String = ""; 743af6ab5fSopenharmony_ci for (let i : int = 0; i < this.n1; i++) { 753af6ab5fSopenharmony_ci str += ((25 * random()) + 97) as char; 763af6ab5fSopenharmony_ci } 773af6ab5fSopenharmony_ci for (let i : int = this.n1; i <= this.n2; i *= 2) { 783af6ab5fSopenharmony_ci let base64 : String = this.toBase64(str); 793af6ab5fSopenharmony_ci let encoded : String = this.base64ToString(base64); 803af6ab5fSopenharmony_ci if (!encoded.equals(str)) { 813af6ab5fSopenharmony_ci } 823af6ab5fSopenharmony_ci str += str; 833af6ab5fSopenharmony_ci } 843af6ab5fSopenharmony_ci// Consumer.consumeObj(str); 853af6ab5fSopenharmony_ci } 863af6ab5fSopenharmony_ci} 873af6ab5fSopenharmony_ci 883af6ab5fSopenharmony_cifunction main(): void { 893af6ab5fSopenharmony_ci let a = new StringBase64; 903af6ab5fSopenharmony_ci a.run(); 913af6ab5fSopenharmony_ci} 92