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