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