• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static uint32_t LoadResTable(const std::string path, std::map<int64_t, std::vector<ResourceItem>> &resInfos);
35     static uint32_t LoadResTable(std::basic_istream<char> &in, std::map<int64_t, std::vector<ResourceItem>> &resInfos);
36 private:
37     struct TableData {
38         uint32_t id;
39         ResourceItem resourceItem;
40     };
41 
42     struct IndexHeader {
43         int8_t version[VERSION_MAX_LEN];
44         uint32_t fileSize;
45         uint32_t limitKeyConfigSize;
46     };
47 
48     struct LimitKeyConfig {
49         int8_t keyTag[TAG_LEN] = {'K', 'E', 'Y', 'S'};
50         uint32_t offset; // IdSet file address offset
51         uint32_t keyCount; // KeyParam count
52         std::vector<int32_t> data;
53     };
54 
55     struct IdSet {
56         int8_t idTag[TAG_LEN] = {'I', 'D', 'S', 'S'};
57         uint32_t idCount;
58         std::map<uint32_t, uint32_t> data; // pair id and offset
59     };
60 
61     struct RecordItem {
62         uint32_t size;
63         int32_t resType;
64         uint32_t id;
65     };
66     uint32_t SaveToResouorceIndex(const std::map<std::string, std::vector<TableData>> &configs) const;
67     uint32_t CreateIdDefined(const std::map<int64_t, std::vector<ResourceItem>> &allResource) const;
68     bool InitIndexHeader(IndexHeader &indexHeader, uint32_t count) const;
69     bool Prepare(const std::map<std::string, std::vector<TableData>> &configs,
70                  std::map<std::string, LimitKeyConfig> &limitKeyConfigs,
71                  std::map<std::string, IdSet> &idSets, uint32_t &pos) const;
72     bool SaveRecordItem(const std::map<std::string, std::vector<TableData>> &configs, std::ostringstream &out,
73                         std::map<std::string, IdSet> &idSets, uint32_t &pos) const;
74     void SaveHeader(const IndexHeader &indexHeader, std::ostringstream &out) const;
75     void SaveLimitKeyConfigs(const std::map<std::string, LimitKeyConfig> &limitKeyConfigs,
76                              std::ostringstream &out) const;
77     void SaveIdSets(const std::map<std::string, IdSet> &idSets, std::ostringstream &out) const;
78     static bool ReadFileHeader(std::basic_istream<char> &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length);
79     static bool ReadLimitKeys(std::basic_istream<char> &in, std::map<int64_t, std::vector<KeyParam>> &limitKeys,
80                        uint32_t count, uint64_t &pos, uint64_t length);
81     static bool ReadIdTables(std::basic_istream<char> &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
82                       uint32_t count, uint64_t &pos, uint64_t length);
83     static bool ReadDataRecordPrepare(std::basic_istream<char> &in, RecordItem &record, uint64_t &pos, uint64_t length);
84     static bool ReadDataRecordStart(std::basic_istream<char> &in, RecordItem &record,
85                              const std::map<int64_t, std::vector<KeyParam>> &limitKeys,
86                              const std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
87                              std::map<int64_t, std::vector<ResourceItem>> &resInfos);
88     std::string indexFilePath_;
89     std::string idDefinedPath_;
90 };
91 }
92 }
93 }
94 #endif