1 /** 2 * Copyright (c) 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 16 #ifndef PANDA_GUARD_CONFIGS_GUARD_NAME_CACHE_H 17 #define PANDA_GUARD_CONFIGS_GUARD_NAME_CACHE_H 18 19 #include <map> 20 #include <memory> 21 #include <set> 22 #include <string> 23 24 #include "guard_options.h" 25 26 namespace panda::guard { 27 28 struct FileNameCacheInfo { 29 std::map<std::string, std::string> identifierCacheMap; 30 std::map<std::string, std::string> memberMethodCacheMap; 31 std::string obfName; 32 std::string oriSourceFile; 33 std::string obfSourceFile; 34 }; 35 36 struct ProjectNameCacheInfo { 37 std::map<std::string, FileNameCacheInfo> fileCacheInfoMap; 38 std::string entryPackageInfo; 39 std::string compileSdkVersion; 40 std::map<std::string, std::string> propertyCacheMap; 41 std::map<std::string, std::string> fileNameCacheMap; 42 }; 43 44 class NameCache { 45 public: NameCache(std::shared_ptr<GuardOptions> options)46 explicit NameCache(std::shared_ptr<GuardOptions> options) : options_(std::move(options)) {} 47 48 void Load(const std::string &applyNameCachePath); 49 50 [[nodiscard]] const std::set<std::string> &GetHistoryUsedNames() const; 51 52 [[nodiscard]] std::map<std::string, std::string> GetHistoryFileNameMapping() const; 53 54 [[nodiscard]] std::map<std::string, std::string> GetHistoryNameMapping() const; 55 56 void AddObfIdentifierName(const std::string &filePath, const std::string &origin, const std::string &obfName); 57 58 void AddObfMemberMethodName(const std::string &filePath, const std::string &origin, const std::string &obfName); 59 60 void AddObfName(const std::string &filePath, const std::string &obfName); 61 62 void AddSourceFile(const std::string &filePath, const std::string &oriSource, const std::string &obfSource); 63 64 void AddNewNameCacheObfFileName(const std::string &origin, const std::string &obfName); 65 66 void AddObfPropertyName(const std::string &origin, const std::string &obfName); 67 68 void WriteCache(); 69 70 private: 71 void ParseAppliedNameCache(const std::string &content); 72 73 void ParseHistoryNameCacheToUsedNames(); 74 75 void AddHistoryUsedNames(const std::string &value); 76 77 void AddHistoryUsedNames(const std::map<std::string, std::string> &values); 78 79 std::string BuildJson(const ProjectNameCacheInfo &nameCacheInfo); 80 81 void MergeNameCache(ProjectNameCacheInfo &merge); 82 83 std::shared_ptr<GuardOptions> options_ = nullptr; 84 85 std::set<std::string> historyUsedNames_ {}; 86 87 ProjectNameCacheInfo historyNameCache_ {}; 88 89 ProjectNameCacheInfo newNameCache_ {}; 90 }; 91 92 } // namespace panda::guard 93 94 #endif // PANDA_GUARD_CONFIGS_GUARD_NAME_CACHE_H 95