1/* 2 * Copyright (c) 2023-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 16import {FileUtils} from './FileUtils'; 17 18export const NAME_CACHE_SUFFIX: string = '.cache.json'; 19export const PROPERTY_CACHE_FILE: string = 'property.cache.json'; 20export const IDENTIFIER_CACHE: string = 'IdentifierCache'; 21export const MEM_METHOD_CACHE: string = 'MemberMethodCache'; 22export const spaceOfNameCache: number = 2; 23 24export function writeCache(cache: Map<string, string> | undefined, destFileName: string): void { 25 // convert map to json string 26 if (!cache) { 27 return; 28 } 29 30 const cacheString: string = JSON.stringify(Object.fromEntries(cache), null, spaceOfNameCache); 31 FileUtils.writeFile(destFileName, cacheString); 32} 33 34export function readCache(filePath: string): Object | undefined { 35 // read json string from file 36 const cacheString: string = FileUtils.readFile(filePath); 37 if (cacheString === undefined) { 38 return undefined; 39 } 40 41 // get map from json string 42 return JSON.parse(cacheString); 43} 44 45export function getMapFromJson(jsonObj: Object | undefined): Map<string, string> { 46 if (jsonObj === undefined) { 47 return new Map<string, string>(); 48 } 49 50 return new Map<string, string>(Object.entries(jsonObj)); 51} 52 53export function deleteLineInfoForNameString(historyNameCache: Map<string, string>, identifierCache: Object | undefined): void { 54 if (identifierCache) { 55 for (const [key, value] of Object.entries(identifierCache)) { 56 let newKey = key.includes(':') ? key.split(':')[0] : key; 57 historyNameCache.set(newKey, value as string); 58 } 59 } 60} 61 62// The original name of the member method is recorded during the identifier obfuscation. 63// After the property obfuscation, it needs to be updated to the mangled name. 64export function UpdateMemberMethodName(nameCache: Map<string, string | Map<string, string>>, globalMangledTable: Map<string, string>, 65 classInfoInMemberMethodCache: Set<string>): void { 66 let memberMethodCache: Map<string, string> = nameCache.get(MEM_METHOD_CACHE) as Map<string, string>; 67 if (!memberMethodCache) { 68 return; 69 } 70 // the valueName is the orignal name of member method. 71 for (const [key, valueName] of memberMethodCache.entries()) { 72 // It is used to prevent the class name from being updated incorrectly, since the obfuscated class name 73 // is recorded during identifier obfuscation. 74 if (classInfoInMemberMethodCache.has(key)) { 75 continue; 76 } 77 const mangledName: string = globalMangledTable.get(valueName); 78 if (mangledName) { 79 memberMethodCache.set(key, mangledName); 80 } 81 } 82}