1 /* 2 * Copyright (c) 2021-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 OHOS_RESTOOL_RESOURCE_TABLE_H 17 #define OHOS_RESTOOL_RESOURCE_TABLE_H 18 19 #include <fstream> 20 #include <memory> 21 #include <sstream> 22 #include "resource_item.h" 23 #include "restool_errors.h" 24 25 namespace OHOS { 26 namespace Global { 27 namespace Restool { 28 class ResourceTable { 29 public: 30 ResourceTable(); 31 virtual ~ResourceTable(); 32 uint32_t CreateResourceTable(); 33 uint32_t CreateResourceTable(const std::map<int64_t, std::vector<std::shared_ptr<ResourceItem>>> &items); 34 uint32_t LoadResTable(const std::string path, std::map<int64_t, std::vector<ResourceItem>> &resInfos); 35 private: 36 struct TableData { 37 int32_t id; 38 ResourceItem resourceItem; 39 }; 40 41 struct IndexHeader { 42 int8_t version[VERSION_MAX_LEN]; 43 uint32_t fileSize; 44 uint32_t limitKeyConfigSize; 45 }; 46 47 struct LimitKeyConfig { 48 int8_t keyTag[TAG_LEN] = {'K', 'E', 'Y', 'S'}; 49 uint32_t offset; // IdSet file address offset 50 uint32_t keyCount; // KeyParam count 51 std::vector<int32_t> data; 52 }; 53 54 struct IdSet { 55 int8_t idTag[TAG_LEN] = {'I', 'D', 'S', 'S'}; 56 uint32_t idCount; 57 std::map<int32_t, uint32_t> data; // pair id and offset 58 }; 59 60 struct RecordItem { 61 uint32_t size; 62 int32_t resType; 63 int32_t id; 64 }; 65 uint32_t SaveToResouorceIndex(const std::map<std::string, std::vector<TableData>> &configs) const; 66 uint32_t CreateIdDefined(const std::map<int64_t, std::vector<ResourceItem>> &allResource) const; 67 bool InitIndexHeader(IndexHeader &indexHeader, uint32_t count) const; 68 bool Prepare(const std::map<std::string, std::vector<TableData>> &configs, 69 std::map<std::string, LimitKeyConfig> &limitKeyConfigs, 70 std::map<std::string, IdSet> &idSets, uint32_t &pos) const; 71 bool SaveRecordItem(const std::map<std::string, std::vector<TableData>> &configs, std::ostringstream &out, 72 std::map<std::string, IdSet> &idSets, uint32_t &pos) const; 73 void SaveHeader(const IndexHeader &indexHeader, std::ostringstream &out) const; 74 void SaveLimitKeyConfigs(const std::map<std::string, LimitKeyConfig> &limitKeyConfigs, 75 std::ostringstream &out) const; 76 void SaveIdSets(const std::map<std::string, IdSet> &idSets, std::ostringstream &out) const; 77 bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const; 78 bool ReadLimitKeys(std::ifstream &in, std::map<int64_t, std::vector<KeyParam>> &limitKeys, 79 uint32_t count, uint64_t &pos, uint64_t length) const; 80 bool ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas, 81 uint32_t count, uint64_t &pos, uint64_t length) const; 82 bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, uint64_t &pos, uint64_t length) const; 83 bool ReadDataRecordStart(std::ifstream &in, RecordItem &record, 84 const std::map<int64_t, std::vector<KeyParam>> &limitKeys, 85 const std::map<int64_t, std::pair<int64_t, int64_t>> &datas, 86 std::map<int64_t, std::vector<ResourceItem>> &resInfos) const; 87 std::string indexFilePath_; 88 std::string idDefinedPath_; 89 }; 90 } 91 } 92 } 93 #endif