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 #include "id_worker.h"
17 #include<iostream>
18 #include<regex>
19 #include "cmd_parser.h"
20 #include "file_entry.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
26 const int32_t IdWorker::START_SYS_ID = 0x07800000;
Init(ResourceIdCluster type,int32_t startId)27 uint32_t IdWorker::Init(ResourceIdCluster type, int32_t startId)
28 {
29 type_ = type;
30 if (InitIdDefined() != RESTOOL_SUCCESS) {
31 return RESTOOL_ERROR;
32 }
33
34 if (type == ResourceIdCluster::RES_ID_APP) {
35 appId_ = startId;
36 maxId_ = GetMaxId(startId);
37 }
38 return RESTOOL_SUCCESS;
39 }
40
GenerateId(ResType resType,const string & name)41 int32_t IdWorker::GenerateId(ResType resType, const string &name)
42 {
43 if (type_ == ResourceIdCluster::RES_ID_APP) {
44 return GenerateAppId(resType, name);
45 }
46 return GenerateSysId(resType, name);
47 }
48
GetHeaderId() const49 vector<IdWorker::ResourceId> IdWorker::GetHeaderId() const
50 {
51 map<ResType, vector<ResourceId>> idClassify;
52 for (const auto &it : ids_) {
53 ResourceId resourceId;
54 resourceId.id = it.second;
55 resourceId.type = ResourceUtil::ResTypeToString(it.first.first);
56 resourceId.name = it.first.second;
57 idClassify[it.first.first].push_back(resourceId);
58 }
59
60 vector<ResourceId> ids;
61 for (const auto &item : idClassify) {
62 ids.insert(ids.end(), item.second.begin(), item.second.end());
63 }
64 return ids;
65 }
66
GetId(ResType resType,const string & name) const67 int32_t IdWorker::GetId(ResType resType, const string &name) const
68 {
69 auto result = ids_.find(make_pair(resType, name));
70 if (result == ids_.end()) {
71 return -1;
72 }
73 return result->second;
74 }
75
GetSystemId(ResType resType,const string & name) const76 int32_t IdWorker::GetSystemId(ResType resType, const string &name) const
77 {
78 auto result = definedIds_.find(make_pair(resType, name));
79 if (result == definedIds_.end()) {
80 return -1;
81 }
82 return result->second.id;
83 }
84
IsValidName(const string & name) const85 bool IdWorker::IsValidName(const string &name) const
86 {
87 if (!regex_match(name, regex("[a-zA-z0-9_]+"))) {
88 cerr << "Error: '" << name << "' only contain [a-zA-z0-9_]." << endl;
89 return false;
90 }
91 if (type_ != ResourceIdCluster::RES_ID_SYS) {
92 return true;
93 }
94 return IsValidSystemName(name);
95 }
96
PushCache(ResType resType,const string & name,int32_t id)97 bool IdWorker::PushCache(ResType resType, const string &name, int32_t id)
98 {
99 auto result = cacheIds_.emplace(make_pair(resType, name), id);
100 if (!result.second) {
101 return false;
102 }
103 if (appId_ == id) {
104 appId_ = id + 1;
105 return true;
106 }
107
108 if (id < appId_) {
109 return false;
110 }
111
112 for (int32_t i = appId_; i < id; i++) {
113 delIds_.push_back(i);
114 }
115 appId_ = id + 1;
116 return true;
117 }
118
PushDelId(int32_t id)119 void IdWorker::PushDelId(int32_t id)
120 {
121 delIds_.push_back(id);
122 }
123
GenerateAppId(ResType resType,const string & name)124 int32_t IdWorker::GenerateAppId(ResType resType, const string &name)
125 {
126 auto result = ids_.find(make_pair(resType, name));
127 if (result != ids_.end()) {
128 return result->second;
129 }
130
131 result = cacheIds_.find(make_pair(resType, name));
132 if (result != cacheIds_.end()) {
133 ids_.emplace(make_pair(resType, name), result->second);
134 return result->second;
135 }
136
137 if (appId_ > maxId_) {
138 cerr << "Error: id count exceed " << appId_ << ">" << maxId_ << endl;
139 return -1;
140 }
141 int32_t id = 0;
142 if (!delIds_.empty()) {
143 id = delIds_.front();
144 delIds_.erase(delIds_.begin());
145 } else {
146 id = appId_;
147 appId_++;
148 }
149 ids_.emplace(make_pair(resType, name), id);
150 return id;
151 }
152
GenerateSysId(ResType resType,const string & name)153 int32_t IdWorker::GenerateSysId(ResType resType, const string &name)
154 {
155 auto result = ids_.find(make_pair(resType, name));
156 if (result != ids_.end()) {
157 return result->second;
158 }
159
160 auto defined = definedIds_.find(make_pair(resType, name));
161 if (defined != definedIds_.end()) {
162 ids_.emplace(make_pair(resType, name), defined->second.id);
163 return defined->second.id;
164 }
165 return -1;
166 }
167
InitIdDefined()168 uint32_t IdWorker::InitIdDefined()
169 {
170 InitParser();
171 CmdParser<PackageParser> &parser = CmdParser<PackageParser>::GetInstance();
172 PackageParser &packageParser = parser.GetCmdParser();
173 string idDefinedPath;
174 if (type_ == ResourceIdCluster::RES_ID_SYS) {
175 for (const auto &inputPath : packageParser.GetInputs()) {
176 idDefinedPath = FileEntry::FilePath(inputPath).Append(RESOURCES_DIR)
177 .Append("base").Append("element").Append(ID_DEFINED_FILE).GetPath();
178 if (InitIdDefined(idDefinedPath) != RESTOOL_SUCCESS) {
179 return RESTOOL_ERROR;
180 }
181 }
182 return RESTOOL_SUCCESS;
183 }
184
185 idDefinedPath = FileEntry::FilePath(packageParser.GetRestoolPath()).GetParent().Append(ID_DEFINED_FILE).GetPath();
186 if (InitIdDefined(idDefinedPath) != RESTOOL_SUCCESS) {
187 return RESTOOL_ERROR;
188 }
189 return RESTOOL_SUCCESS;
190 }
191
InitIdDefined(const std::string & filePath)192 uint32_t IdWorker::InitIdDefined(const std::string &filePath)
193 {
194 if (!ResourceUtil::FileExist(filePath)) {
195 return RESTOOL_SUCCESS;
196 }
197
198 Json::Value root;
199 if (!ResourceUtil::OpenJsonFile(filePath, root)) {
200 return RESTOOL_ERROR;
201 }
202
203 int32_t startSysId = GetStartId(root);
204 if (startSysId < 0) {
205 return RESTOOL_ERROR;
206 }
207
208 auto record = root["record"];
209 if (record.empty()) {
210 cerr << "Error: id_defined.json record empty." << endl;
211 return RESTOOL_ERROR;
212 }
213 if (!record.isArray()) {
214 cerr << "Error: id_defined.json record not array." << endl;
215 return RESTOOL_ERROR;
216 }
217
218 for (Json::ArrayIndex index = 0; index < record.size(); index++) {
219 auto arrayItem = record[index];
220 ResourceId resourceId;
221 resourceId.seq = index;
222 resourceId.id = startSysId;
223 if (!arrayItem.isObject()) {
224 return RESTOOL_ERROR;
225 }
226 for (const auto &handle : handles_) {
227 if (!handle.second(arrayItem[handle.first], resourceId)) {
228 return RESTOOL_ERROR;
229 }
230 }
231 if (!PushResourceId(resourceId)) {
232 return RESTOOL_ERROR;
233 }
234 }
235 return RESTOOL_SUCCESS;
236 }
237
InitParser()238 void IdWorker::InitParser()
239 {
240 using namespace placeholders;
241 handles_.emplace("type", bind(&IdWorker::ParseType, this, _1, _2));
242 handles_.emplace("name", bind(&IdWorker::ParseName, this, _1, _2));
243 handles_.emplace("order", bind(&IdWorker::ParseOrder, this, _1, _2));
244 }
245
ParseType(const Json::Value & type,ResourceId & resourceId)246 bool IdWorker::ParseType(const Json::Value &type, ResourceId &resourceId)
247 {
248 if (type.empty()) {
249 cerr << "Error: id_defined.json seq=" << resourceId.seq << " type empty." << endl;
250 return false;
251 }
252 if (!type.isString()) {
253 cerr << "Error: id_defined.json seq=" << resourceId.seq << " type not string." << endl;
254 return false;
255 }
256 if (ResourceUtil::GetResTypeFromString(type.asString()) == ResType::INVALID_RES_TYPE) {
257 cerr << "Error: id_defined.json seq=" << resourceId.seq << " type '";
258 cerr << type.asString() << "' invalid." << endl;
259 return false;
260 }
261 resourceId.type = type.asString();
262 return true;
263 }
264
ParseName(const Json::Value & name,ResourceId & resourceId)265 bool IdWorker::ParseName(const Json::Value &name, ResourceId &resourceId)
266 {
267 if (name.empty()) {
268 cerr << "Error: id_defined.json seq=" << resourceId.seq << " name empty." << endl;
269 return false;
270 }
271 if (!name.isString()) {
272 cerr << "Error: id_defined.json seq=" << resourceId.seq << " name not string." << endl;
273 return false;
274 }
275 resourceId.name = name.asString();
276 if ((resourceId.id & START_SYS_ID) == START_SYS_ID && !IsValidSystemName(resourceId.name)) {
277 cerr << "Error: id_defined.json."<< endl;
278 return false;
279 }
280 return true;
281 }
282
ParseOrder(const Json::Value & order,ResourceId & resourceId)283 bool IdWorker::ParseOrder(const Json::Value &order, ResourceId &resourceId)
284 {
285 if (order.empty()) {
286 cerr << "Error: id_defined.json seq=" << resourceId.seq << " order empty." << endl;
287 return false;
288 }
289 if (!order.isInt()) {
290 cerr << "Error: id_defined.json seq=" << resourceId.seq << " order not int." << endl;
291 return false;
292 }
293 if (order.asInt() != resourceId.seq) {
294 cerr << "Error: id_defined.json seq=" << resourceId.seq << " order value ";
295 cerr << order.asInt() << " vs expect " << resourceId.seq << endl;
296 return false;
297 }
298 resourceId.id = resourceId.id + order.asInt();
299 return true;
300 }
301
PushResourceId(const ResourceId & resourceId)302 bool IdWorker::PushResourceId(const ResourceId &resourceId)
303 {
304 ResType resType = ResourceUtil::GetResTypeFromString(resourceId.type);
305 auto result = definedIds_.emplace(make_pair(resType, resourceId.name), resourceId);
306 if (!result.second) {
307 cerr << "Error: '" << resourceId.type << "' '" << resourceId.name << "' duplicated." << endl;
308 return false;
309 }
310 return true;
311 }
312
IsValidSystemName(const string & name) const313 bool IdWorker::IsValidSystemName(const string &name) const
314 {
315 if (regex_match(name, regex("^ohos.+"))) {
316 return true;
317 }
318 cerr << "Error: '" << name << "' must start with 'ohos'" << endl;
319 return false;
320 }
321
GetStartId(const Json::Value & root) const322 int32_t IdWorker::GetStartId(const Json::Value &root) const
323 {
324 auto startId = root["startId"];
325 if (startId.empty()) {
326 cerr << "Error: id_defined.json 'startId' empty." << endl;
327 return -1;
328 }
329
330 if (!startId.isString()) {
331 cerr << "Error: id_defined.json 'startId' not string." << endl;
332 return -1;
333 }
334
335 int32_t id = strtol(startId.asString().c_str(), nullptr, 16);
336 return id;
337 }
338
GetMaxId(int32_t startId) const339 int32_t IdWorker::GetMaxId(int32_t startId) const
340 {
341 int32_t flag = 1;
342 while ((flag & startId) == 0) {
343 flag = flag << 1;
344 }
345 return (startId + flag - 1);
346 }
347 }
348 }
349 }