13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2023 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_ciimport path from 'path'; 173af6ab5fSopenharmony_ciimport {ApiExtractor} from './ApiExtractor'; 183af6ab5fSopenharmony_ciimport {ListUtil} from '../utils/ListUtil'; 193af6ab5fSopenharmony_ciimport type {IOptions} from '../configs/IOptions'; 203af6ab5fSopenharmony_ciimport { stringPropsSet, structPropsSet, enumPropsSet } from '../utils/OhsUtil'; 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_ciexport const scanProjectConfig: { 233af6ab5fSopenharmony_ci mPropertyObfuscation?: boolean, 243af6ab5fSopenharmony_ci mKeepStringProperty?: boolean, 253af6ab5fSopenharmony_ci mExportObfuscation?: boolean, 263af6ab5fSopenharmony_ci mkeepFilesAndDependencies?: Set<string>, 273af6ab5fSopenharmony_ci isHarCompiled?: boolean 283af6ab5fSopenharmony_ci} = {}; 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci/** 313af6ab5fSopenharmony_ci * if rename property is not open, api read and extract can be skipped 323af6ab5fSopenharmony_ci * 333af6ab5fSopenharmony_ci * init plugin, read api info of openHarmony sdk and generate file of reserved name, property and string. 343af6ab5fSopenharmony_ci * @param sdkDir absolute path like D:\\HuaweiApp\\ohsdk 353af6ab5fSopenharmony_ci * @param outputDir 363af6ab5fSopenharmony_ci */ 373af6ab5fSopenharmony_ciexport function initPlugin(sdkDir: string, outputDir: string): void { 383af6ab5fSopenharmony_ci // create sdk api file if not exist 393af6ab5fSopenharmony_ci const ohSdkPath: string = path.resolve(sdkDir); 403af6ab5fSopenharmony_ci if (!ohSdkPath) { 413af6ab5fSopenharmony_ci console.error('SDK path is not found.'); 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ci const apiVersions: string[] = ['']; 453af6ab5fSopenharmony_ci 463af6ab5fSopenharmony_ci apiVersions.forEach((versionString) => { 473af6ab5fSopenharmony_ci ApiExtractor.parseOhSdk(ohSdkPath, versionString, true, outputDir); 483af6ab5fSopenharmony_ci }); 493af6ab5fSopenharmony_ci} 503af6ab5fSopenharmony_ci 513af6ab5fSopenharmony_ci/** 523af6ab5fSopenharmony_ci * need read api info or not 533af6ab5fSopenharmony_ci * @param customProfiles 543af6ab5fSopenharmony_ci */ 553af6ab5fSopenharmony_ciexport function needReadApiInfo(customProfiles: IOptions): boolean { 563af6ab5fSopenharmony_ci return isEnabledPropertyObfuscation(customProfiles) || customProfiles.mExportObfuscation; 573af6ab5fSopenharmony_ci} 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_ciexport function isEnabledPropertyObfuscation(customProfiles: IOptions): boolean { 603af6ab5fSopenharmony_ci return (customProfiles.mNameObfuscation && 613af6ab5fSopenharmony_ci customProfiles.mNameObfuscation.mEnable && 623af6ab5fSopenharmony_ci customProfiles.mNameObfuscation.mRenameProperties); 633af6ab5fSopenharmony_ci} 643af6ab5fSopenharmony_ci 653af6ab5fSopenharmony_cifunction initScanProjectConfig(customProfiles: IOptions, isHarCompiled?: boolean) { 663af6ab5fSopenharmony_ci scanProjectConfig.mPropertyObfuscation = customProfiles.mNameObfuscation?.mRenameProperties; 673af6ab5fSopenharmony_ci scanProjectConfig.mKeepStringProperty = customProfiles.mNameObfuscation?.mKeepStringProperty; 683af6ab5fSopenharmony_ci scanProjectConfig.mExportObfuscation = customProfiles.mExportObfuscation; 693af6ab5fSopenharmony_ci scanProjectConfig.mkeepFilesAndDependencies = customProfiles.mKeepFileSourceCode?.mkeepFilesAndDependencies; 703af6ab5fSopenharmony_ci scanProjectConfig.isHarCompiled = isHarCompiled; 713af6ab5fSopenharmony_ci} 723af6ab5fSopenharmony_ci 733af6ab5fSopenharmony_ciexport interface ReseverdSetForArkguard { 743af6ab5fSopenharmony_ci structPropertySet: Set<string> | undefined; 753af6ab5fSopenharmony_ci stringPropertySet: Set<string> | undefined; 763af6ab5fSopenharmony_ci exportNameAndPropSet: Set<string> | undefined; 773af6ab5fSopenharmony_ci exportNameSet: Set<string> | undefined; 783af6ab5fSopenharmony_ci enumPropertySet: Set<string> | undefined; 793af6ab5fSopenharmony_ci} 803af6ab5fSopenharmony_ci 813af6ab5fSopenharmony_ci/** 823af6ab5fSopenharmony_ci * read project reserved properties by collected paths 833af6ab5fSopenharmony_ci * @param filesForCompilation set collection of files 843af6ab5fSopenharmony_ci * @param customProfiles 853af6ab5fSopenharmony_ci */ 863af6ab5fSopenharmony_ciexport function readProjectPropertiesByCollectedPaths(filesForCompilation: Set<string>, 873af6ab5fSopenharmony_ci customProfiles: IOptions, isHarCompiled: boolean): ReseverdSetForArkguard { 883af6ab5fSopenharmony_ci const ApiType = ApiExtractor.ApiType; 893af6ab5fSopenharmony_ci let scanningCommonType = undefined; 903af6ab5fSopenharmony_ci let scanningLibsType = undefined; 913af6ab5fSopenharmony_ci if (needReadApiInfo(customProfiles)) { 923af6ab5fSopenharmony_ci scanningCommonType = ApiType.PROJECT; 933af6ab5fSopenharmony_ci scanningLibsType = ApiType.PROJECT_DEPENDS; 943af6ab5fSopenharmony_ci } else { 953af6ab5fSopenharmony_ci scanningCommonType = ApiType.CONSTRUCTOR_PROPERTY; 963af6ab5fSopenharmony_ci scanningLibsType = ApiType.CONSTRUCTOR_PROPERTY; 973af6ab5fSopenharmony_ci } 983af6ab5fSopenharmony_ci // The purpose of collecting constructor properties is to avoid generating the same name as the constructor property when obfuscating identifier names. 993af6ab5fSopenharmony_ci ApiExtractor.mConstructorPropertySet = new Set(); 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ci initScanProjectConfig(customProfiles, isHarCompiled); 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ci stringPropsSet.clear(); 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ci const exportWhiteList = ApiExtractor.parseFileByPaths(filesForCompilation, scanningCommonType); 1063af6ab5fSopenharmony_ci const exportNamesAndProperties: Set<string> | undefined = exportWhiteList.reservedExportPropertyAndName; 1073af6ab5fSopenharmony_ci const exportNames: Set<string> | undefined = exportWhiteList.reservedExportNames; 1083af6ab5fSopenharmony_ci 1093af6ab5fSopenharmony_ci // if -enable-property-obfuscation, collect structPropsSet, exportNamesAndProperties and 1103af6ab5fSopenharmony_ci // stringPropsSet(if -enable-string-property-obufscation is not enabled) as whitelists. 1113af6ab5fSopenharmony_ci let exportNameAndPropSet: Set<string>; 1123af6ab5fSopenharmony_ci let structPropertySet: Set<string>; 1133af6ab5fSopenharmony_ci let stringPropertySet: Set<string>; 1143af6ab5fSopenharmony_ci let enumPropertySet: Set<string>; 1153af6ab5fSopenharmony_ci if (isEnabledPropertyObfuscation(customProfiles)) { 1163af6ab5fSopenharmony_ci exportNameAndPropSet = new Set(exportNamesAndProperties); 1173af6ab5fSopenharmony_ci structPropertySet = new Set(structPropsSet); 1183af6ab5fSopenharmony_ci enumPropertySet = new Set(enumPropsSet); 1193af6ab5fSopenharmony_ci if (scanProjectConfig.mKeepStringProperty) { 1203af6ab5fSopenharmony_ci stringPropertySet = new Set(stringPropsSet); 1213af6ab5fSopenharmony_ci } 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci structPropsSet.clear(); 1243af6ab5fSopenharmony_ci stringPropsSet.clear(); 1253af6ab5fSopenharmony_ci enumPropsSet.clear(); 1263af6ab5fSopenharmony_ci 1273af6ab5fSopenharmony_ci let exportNameSet: Set<string>; 1283af6ab5fSopenharmony_ci if (scanProjectConfig.mExportObfuscation) { 1293af6ab5fSopenharmony_ci exportNameSet = new Set(exportNames); 1303af6ab5fSopenharmony_ci } 1313af6ab5fSopenharmony_ci 1323af6ab5fSopenharmony_ci return { 1333af6ab5fSopenharmony_ci structPropertySet: structPropertySet, 1343af6ab5fSopenharmony_ci stringPropertySet: stringPropertySet, 1353af6ab5fSopenharmony_ci exportNameAndPropSet: exportNameAndPropSet, 1363af6ab5fSopenharmony_ci exportNameSet: exportNameSet, 1373af6ab5fSopenharmony_ci enumPropertySet: enumPropertySet, 1383af6ab5fSopenharmony_ci }; 1393af6ab5fSopenharmony_ci} 140