• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_RESDESC_H
17 #define OHOS_RESOURCE_MANAGER_RESDESC_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_VERSION_LEN = 128;
30 
31 /**
32  * old module resource.index file header
33  */
34 class ResHeader {
35 public:
36     static const uint32_t RES_HEADER_LEN = 136;
37 
38     // Type identifier for this chunk.  The meaning of this value depends
39     // on the containing chunk.
40     char version_[RES_VERSION_LEN];
41 
42     // Size of the resource.index file (in bytes).  Including header
43     uint32_t length_;
44 
45     // determiner key count
46     uint32_t keyCount_;
47 };
48 
49 class IdItem {
50 public:
51     static const uint32_t HEADER_LEN = 12;
52     static const uint32_t SIZE_LEN = 2;
53     static std::map<ResType, std::string> resTypeStrList;
54 
55     /**
56      * Whether the resType is array or not
57      * @param type the resType
58      * @return true if the resType is array, else false
59      */
60     static bool IsArrayOfType(const ResType &type);
61 
62     /**
63      * Judge the IdItem is array resource or not
64      */
65     void JudgeArray();
66 
67     /**
68      * only theme and pattern may have parent
69      * @return true when have parent
70      */
71     bool HaveParent() const;
72 
73     /**
74      * Whether the std::string value is ref or not
75      * ref start with '$' end with id
76      * sample: "$string:16777225"
77      * @param value
78      * @param resType when return true, set resType. as sample : ResType:STRING
79      * @param id      when return true, set id. as sample : 16777225
80      * @return        true: value is ref
81      */
82     static bool IsRef(const std::string &value, ResType &resType, uint32_t &id);
83 
84     std::string ToString() const;
85 
86     uint32_t size_;
87     ResType resType_;
88     uint32_t id_;
89     uint16_t valueLen_;
90     bool isArray_ = false;
91     std::string value_;
92     std::vector<std::string> values_;
93     std::string name_;
94 };
95 
96 class IdParam {
97 public:
98     ~IdParam();
99     std::string ToString() const;
100 
101     uint32_t id_;
102     uint32_t offset_;
103     std::shared_ptr<IdItem> idItem_;
104 };
105 
106 class ResId {
107 public:
108     static const uint32_t RESID_HEADER_LEN = 8;
109     static const uint32_t IDPARAM_HEADER_LEN = 8;
110 
111     std::string ToString() const;
112 
113     char tag_[4];
114     uint32_t count_; // ID count
115     std::vector<std::shared_ptr<IdParam>> idParams_;
116 };
117 
118 /**
119  * describe the qualifier
120  */
121 class KeyParam {
122 public:
123     static const uint32_t KEYPARAM_LEN = 8;
124 
125     // type of qualifier
126     KeyType type_;
127 
128     // value of qualifiers
129     uint32_t value_;
130 
131     // convert from value_
132     std::string str_;
133 
InitStr()134     void InitStr()
135     {
136         str_ = ConvertToStr();
137     }
138 
GetStr()139     const std::string &GetStr() const
140     {
141         return str_;
142     }
143 
144     std::string ToString() const;
145 
146     std::string GetDeviceTypeStr() const;
147 
148 private:
149     const std::string ConvertToStr() const;
150     std::string GetScreenDensityStr() const;
151     std::string GetColorModeStr() const;
152     std::string GetMccStr() const;
153     std::string GetMncStr() const;
154     std::string GetInputDeviceStr() const;
155 };
156 
157 /**
158  * a ResKey means a Qualifiers Sub-directories
159  */
160 class ResKey {
161 public:
162     static const uint32_t RESKEY_HEADER_LEN = 12;
163 
164     ~ResKey();
165 
166     std::string ToString() const;
167 
168     // always 'KEYS'
169     char tag_[4];
170 
171     // offset from the beginning of the index file, pointing to the resource ID data block
172     uint32_t offset_;
173 
174     // count of qualifiers
175     uint32_t keyParamsCount_;
176 
177     // the resource ID data
178     std::shared_ptr<ResId> resId_;
179 
180     // the resConfig of each ResKey and all resConfig_ in ValueUnderQualifierDir will point to this resConfig_
181     std::shared_ptr<ResConfigImpl> resConfig_;
182 };
183 
184 /**
185  * a ResDesc means a index file in hap zip
186  */
187 class ResDesc {
188 public:
189     ResDesc();
190 
191     ~ResDesc();
192 
193     std::string ToString() const;
194 
195     std::vector<std::shared_ptr<ResKey>> keys_;
196 };
197 
198 /**
199  * new module resource.index file header
200  */
201 class ResIndexHeader {
202 public:
203     static const uint32_t RES_HEADER_LEN = 140;
204 
205     // Type identifier for this chunk.  The meaning of this value depends
206     // on the containing chunk.
207     char version_[RES_VERSION_LEN];
208 
209     // Size of the resource.index file (in bytes).  Including header
210     uint32_t length_;
211 
212     // determiner key count
213     uint32_t keyCount_;
214 
215     // offset from the beginning of the index file, pointing to the data block
216     uint32_t dataBlockOffset_;
217 };
218 
219 /**
220  * a ResKey means a Qualifiers Sub-directories
221  */
222 class KeyInfo {
223 public:
224     static const uint32_t RESKEY_HEADER_LEN = 12;
225 
226     // always 'KEYS'
227     char tag_[4];
228 
229     // unique resconfig id
230     uint32_t resConfigId_;
231 
232     // count of qualifiers
233     uint32_t keyParamsCount_;
234 
235     // key param list
236     std::vector<std::shared_ptr<KeyParam>> params_;
237 };
238 
239 class IdsHeader {
240 public:
241     static const uint32_t IDS_HEADER_LEN = 16;
242 
243     // always 'IDSS'
244     char tag_[4];
245 
246     // size of the idss block
247     uint32_t length_;
248 
249     // resource type count
250     uint32_t typeCount_;
251 
252     // resource id count
253     uint32_t idCount_;
254 };
255 
256 class TypeInfo {
257 public:
258     static const uint32_t TYPE_INFO_LEN = 12;
259 
260     // resource type
261     uint32_t type_;
262 
263     // size of current resource type
264     uint32_t length_;
265 
266     // resource id count
267     uint32_t count_;
268 };
269 
270 class ResItem {
271 public:
272     static const uint32_t RES_ITEM_LEN = 12;
273 
274     // resource id
275     uint32_t resId_;
276 
277     // offset from the beginning of the index file, pointing to the resource data
278     uint32_t offset_;
279 
280     // size of resource name
281     uint32_t length_;
282 
283     // resource name
284     std::string name_;
285 };
286 
287 class DataHander {
288 public:
289     static const uint32_t DATA_HANDER_LEN = 12;
290 
291     // always 'DATA'
292     char tag_[4];
293 
294     // size of the data block
295     uint32_t length_;
296 
297     // resource id count
298     uint32_t idCount_;
299 };
300 
301 class ResInfo {
302 public:
303     static const uint32_t RES_INFO_LEN = 12;
304 
305     // resource id
306     uint32_t resId_;
307 
308     // size of the resource information
309     uint32_t length_;
310 
311     // resource key count
312     uint32_t valueCount_;
313 };
314 
315 class ConfigItem {
316 public:
317     static const uint32_t CONFIG_ITEM_LEN = 8;
318 
319     // resource config id
320     uint32_t resCfgId_;
321 
322     // offset from the beginning of the index file, pointing to the resource data
323     uint32_t offset_;
324 };
325 } // namespace Resource
326 } // namespace Global
327 } // namespace OHOS
328 #endif
329