1 /*
2  * Copyright (c) 2021-2022 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_RESOURCE_MANAGER_RES_DESC_H
17 #define OHOS_RESOURCE_MANAGER_RES_DESC_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include "res_common.h"
24 #include "res_config_impl.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Resource {
29 static constexpr uint32_t RES_HEADER_LEN = 136;
30 
31 static constexpr uint32_t RES_VERSION_LEN = 128;
32 
33 /**
34  * resource.index file header
35  */
36 typedef struct ResHeader {
37     // Type identifier for this chunk.  The meaning of this value depends
38     // on the containing chunk.
39     char version_[RES_VERSION_LEN];
40 
41     // Size of the resource.index file (in bytes).  Including header
42     uint32_t length_;
43 
44     // determiner key count
45     uint32_t keyCount_;
46 } ResHeader;
47 
48 class IdItem {
49 public:
50     static const uint32_t HEADER_LEN = 12;
51     static const uint32_t SIZE_LEN = 2;
52 
53     /**
54      * Whether the resType is array or not
55      * @param type the resType
56      * @return true if the resType is array, else false
57      */
IsArrayOfType(ResType type)58     static bool IsArrayOfType(ResType type)
59     {
60         if (type == ResType::STRINGARRAY || type == ResType::INTARRAY || type == ResType::THEME ||
61             type == ResType::PLURALS || type == ResType::PATTERN) {
62             return true;
63         }
64         return false;
65     }
66 
JudgeArray()67     void JudgeArray()
68     {
69         this->isArray_ = IsArrayOfType(resType_);
70     }
71 
72     /**
73      * only theme and pattern may have parent
74      * @return true when have parent
75      */
76     bool HaveParent() const;
77 
78     static std::map<ResType, std::string> resTypeStrList;
79     /**
80      * judge the std::string value is ref or not
81      * ref start with '$' end with id
82      * sample: "$string:16777225"
83      * @param value
84      * @param resType when return true, set resType. as sample : ResType:STRING
85      * @param id      when return true, set id. as sample : 16777225
86      * @return        true: value is ref
87      */
88     static bool IsRef(const std::string &value, ResType &resType, int &id);
89 
90     std::string ToString() const;
91 
GetItemResName()92     std::string GetItemResName() const
93     {
94         return name_;
95     }
96 
GetItemResId()97     uint32_t GetItemResId() const
98     {
99         return id_;
100     }
101 
GetItemResType()102     ResType GetItemResType() const
103     {
104         return resType_;
105     }
106 
107     uint32_t size_;
108     ResType resType_;
109     uint32_t id_;
110     uint16_t valueLen_;
111     bool isArray_ = false;
112     std::string value_;
113     std::vector<std::string> values_;
114     std::string name_;
115 
116 private:
117     static bool sInit;
118     static bool Init();
119 };
120 
121 class IdParam {
122 public:
123     ~IdParam();
124     std::string ToString() const;
125 
126     uint32_t id_;
127     uint32_t offset_;
128     std::shared_ptr<IdItem> idItem_;
129 };
130 
131 class ResId {
132 public:
133     static const uint32_t RESID_HEADER_LEN = 8;
134     static const uint32_t IDPARAM_HEADER_LEN = 8;
135 
136     ~ResId();
137     std::string ToString() const;
138 
139     char tag_[4];
140     uint32_t count_; // ID count
141     std::vector<std::shared_ptr<IdParam>> idParams_;
142 };
143 
144 /**
145  * describe the qualifier
146  */
147 class KeyParam {
148 public:
149     // type of qualifier
150     KeyType type_;
151 
152     // value of qualifiers
153     uint32_t value_;
154 
155     // convert from value_
156     std::string str_;
157 
InitStr()158     void InitStr()
159     {
160         str_ = ConvertToStr();
161     }
162 
GetStr()163     const std::string &GetStr() const
164     {
165         return str_;
166     }
167 
168     std::string ToString() const;
169 
170     std::string GetDeviceTypeStr() const;
171 
172 private:
173     const std::string ConvertToStr() const;
174     std::string GetScreenDensityStr() const;
175     std::string GetColorModeStr() const;
176     std::string GetMccStr() const;
177     std::string GetMncStr() const;
178     std::string GetInputDeviceStr() const;
179 };
180 
181 /**
182  * a ResKey means a Qualifiers Sub-directories
183  *
184  */
185 class ResKey {
186 public:
187     static const uint32_t RESKEY_HEADER_LEN = 12;
188 
189     static const uint32_t KEYPARAM_HEADER_LEN = 8;
190 
191     ~ResKey();
192 
193     std::string ToString() const;
194     // always 'KEYS'
195     char tag_[4];
196 
197     // offset from the beginning of the index file, pointing to the resource ID data block
198     uint32_t offset_;
199 
200     // count of qualifiers
201     uint32_t keyParamsCount_;
202 
203     // the qualifiers
204     std::vector<std::shared_ptr<KeyParam>> keyParams_;
205 
206     // the resource ID data
207     std::shared_ptr<ResId> resId_;
208 
209     // the resConfig of each ResKey and all resConfig_ in ValueUnderQualifierDir will point to this resConfig_
210     std::shared_ptr<ResConfigImpl> resConfig_;
211 };
212 /**
213  * a ResDesc means a index file in hap zip
214  */
215 class ResDesc {
216 public:
217     ResDesc();
218 
219     ~ResDesc();
220 
221     std::string ToString() const;
222 
223     ResHeader *resHeader_;
224 
225     std::vector<std::shared_ptr<ResKey>> keys_;
226 
227     std::string GetCurrentDeviceType();
228 };
229 } // namespace Resource
230 } // namespace Global
231 } // namespace OHOS
232 #endif
233