• 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 #include "hap_parser.h"
17 
18 #include <cstdlib>
19 #include <string>
20 #include <fcntl.h>
21 #include <fstream>
22 #include <unzip.h>
23 #include <unistd.h>
24 #include <set>
25 #include <sys/stat.h>
26 #include <unordered_map>
27 
28 #include "hap_resource_manager.h"
29 #include "hap_resource_v1.h"
30 #include "hilog_wrapper.h"
31 #include "locale_matcher.h"
32 #if defined(__WINNT__)
33 #include <cstring>
34 #else
35 #include "securec.h"
36 #endif
37 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
38 #include "hitrace_meter.h"
39 #endif
40 #include "utils/errors.h"
41 #include "utils/string_utils.h"
42 #include "utils/utils.h"
43 
44 namespace OHOS {
45 namespace Global {
46 namespace Resource {
HapParser()47 HapParser::HapParser()
48 {}
49 
~HapParser()50 HapParser::~HapParser()
51 {}
52 
Init(const char * path)53 bool HapParser::Init(const char *path)
54 {
55     return true;
56 }
57 
ParseResHex()58 int32_t HapParser::ParseResHex()
59 {
60     return OK;
61 }
62 
GetIndexData(const char * path,std::unique_ptr<uint8_t[]> & buf,size_t & bufLen)63 bool HapParser::GetIndexData(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen)
64 {
65     if (Utils::ContainsTail(path, Utils::tailSet)) {
66         return GetIndexDataFromHap(path, buf, bufLen);
67     } else {
68         return GetIndexDataFromIndex(path, buf, bufLen);
69     }
70 }
71 
72 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetModuleName(const char * configStr,size_t len)73 std::string GetModuleName(const char *configStr, size_t len)
74 {
75     if (configStr == nullptr) {
76         return std::string();
77     }
78     std::string config(configStr, len);
79     static const char *key = "\"moduleName\"";
80     auto idx = config.find(key);
81     if (idx == std::string::npos) {
82         return std::string();
83     }
84     auto start = config.find("\"", idx + strlen(key));
85     if (start == std::string::npos) {
86         return std::string();
87     }
88     auto end = config.find("\"", start + 1);
89     if (end == std::string::npos) {
90         return std::string();
91     }
92 
93     if (end < start + 1) {
94         return std::string();
95     }
96     std::string retStr = std::string(configStr + start + 1, end - start - 1);
97     return retStr;
98 }
99 #endif
100 
101 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
ParseModuleName(std::shared_ptr<AbilityBase::Extractor> & extractor)102 std::string HapParser::ParseModuleName(std::shared_ptr<AbilityBase::Extractor> &extractor)
103 {
104     if (extractor == nullptr) {
105         return std::string();
106     }
107     std::unique_ptr<uint8_t[]> configBuf;
108     size_t len;
109     bool ret = extractor->ExtractToBufByName("config.json", configBuf, len);
110     if (!ret) {
111         RESMGR_HILOGE(RESMGR_TAG, "failed to get config data from ability");
112         return std::string();
113     }
114     // parse config.json
115     std::string mName = GetModuleName(reinterpret_cast<char *>(configBuf.get()), len);
116     if (mName.size() == 0) {
117         RESMGR_HILOGE(RESMGR_TAG, "parse moduleName from config.json error");
118         return std::string();
119     }
120     return mName;
121 }
122 #endif
123 
124 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetIndexFilePath(std::shared_ptr<AbilityBase::Extractor> & extractor)125 std::string HapParser::GetIndexFilePath(std::shared_ptr<AbilityBase::Extractor> &extractor)
126 {
127     std::string mName = ParseModuleName(extractor);
128     std::string indexFilePath = std::string("assets/");
129     indexFilePath.append(mName);
130     indexFilePath.append("/resources.index");
131     return indexFilePath;
132 }
133 #endif
134 
GetIndexDataFromHap(const char * path,std::unique_ptr<uint8_t[]> & buf,size_t & bufLen)135 bool HapParser::GetIndexDataFromHap(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen)
136 {
137 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
138     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
139     bool isNewExtractor = false;
140     std::shared_ptr<AbilityBase::Extractor> extractor = AbilityBase::ExtractorUtil::GetExtractor(path, isNewExtractor);
141     if (extractor == nullptr) {
142         return false;
143     }
144     std::string indexFilePath;
145     if (extractor->IsStageModel()) {
146         indexFilePath = "resources.index";
147     } else {
148         indexFilePath = GetIndexFilePath(extractor);
149     }
150     bool ret = extractor->ExtractToBufByName(indexFilePath, buf, bufLen);
151     if (!ret) {
152         RESMGR_HILOGE(RESMGR_TAG, "failed to get buf data indexFilePath");
153         return false;
154     }
155 #endif
156     return true;
157 }
158 
GetIndexDataFromIndex(const char * path,std::unique_ptr<uint8_t[]> & buf,size_t & bufLen)159 bool HapParser::GetIndexDataFromIndex(const char *path, std::unique_ptr<uint8_t[]> &buf, size_t &bufLen)
160 {
161     char outPath[PATH_MAX + 1] = {0};
162     Utils::CanonicalizePath(path, outPath, PATH_MAX);
163     std::ifstream inFile(outPath, std::ios::binary | std::ios::in);
164     if (!inFile.good()) {
165         return false;
166     }
167     inFile.seekg(0, std::ios::end);
168     int fileLen = inFile.tellg();
169     if (fileLen <= 0) {
170         RESMGR_HILOGE(RESMGR_TAG, "file size is zero");
171         inFile.close();
172         return false;
173     }
174     bufLen = static_cast<size_t>(fileLen);
175     buf = std::make_unique<uint8_t[]>(fileLen);
176     if (buf == nullptr) {
177         RESMGR_HILOGE(RESMGR_TAG, "Error allocating memory");
178         inFile.close();
179         return false;
180     }
181     inFile.seekg(0, std::ios::beg);
182     inFile.read(reinterpret_cast<char*>(buf.get()), fileLen);
183     inFile.close();
184     RESMGR_HILOGD(RESMGR_TAG, "extract success, bufLen:%d", fileLen);
185     return true;
186 }
187 
GetHapResource(const char * path,bool isSystem,bool isOverlay)188 std::shared_ptr<HapResource> HapParser::GetHapResource(const char *path, bool isSystem, bool isOverlay)
189 {
190     return nullptr;
191 }
192 
GetPath(const std::string & filePath,std::string & rawFilePath)193 std::string GetPath(const std::string &filePath, std::string &rawFilePath)
194 {
195     std::string tempName = filePath;
196     const std::string rawFileDirName = "rawfile/";
197     if (tempName.length() <= rawFileDirName.length() ||
198         (tempName.compare(0, rawFileDirName.length(), rawFileDirName) != 0)) {
199         tempName = rawFileDirName + tempName;
200     }
201     rawFilePath.append(tempName);
202     return rawFilePath;
203 }
204 
205 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetRawFilePathFromFa(std::shared_ptr<AbilityBase::Extractor> & extractor,const std::string & filePath)206 std::string GetRawFilePathFromFa(std::shared_ptr<AbilityBase::Extractor> &extractor,
207     const std::string &filePath)
208 {
209     std::string moduleName = HapParser::ParseModuleName(extractor);
210     std::string rawFilePath("assets/");
211     rawFilePath.append(moduleName);
212     rawFilePath.append("/resources/");
213     GetPath(filePath, rawFilePath);
214     return rawFilePath;
215 }
216 #endif
217 
218 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetRawFilePathFromStage(const std::string & filePath)219 std::string GetRawFilePathFromStage(const std::string &filePath)
220 {
221     std::string rawFilePath("resources/");
222     GetPath(filePath, rawFilePath);
223     return rawFilePath;
224 }
225 #endif
226 
227 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetRawFilePath(std::shared_ptr<AbilityBase::Extractor> & extractor,const std::string & rawFileName)228 std::string GetRawFilePath(std::shared_ptr<AbilityBase::Extractor> &extractor,
229     const std::string &rawFileName)
230 {
231     std::string rawfilePath;
232     if (extractor->IsStageModel()) {
233         rawfilePath = GetRawFilePathFromStage(rawFileName);
234     } else {
235         rawfilePath = GetRawFilePathFromFa(extractor, rawFileName);
236     }
237     return rawfilePath;
238 }
239 #endif
240 
ReadRawFileFromHap(const std::string & hapPath,const std::string & patchPath,const std::string & rawFileName,size_t & len,std::unique_ptr<uint8_t[]> & outValue)241 RState HapParser::ReadRawFileFromHap(const std::string &hapPath, const std::string &patchPath,
242     const std::string &rawFileName, size_t &len, std::unique_ptr<uint8_t[]> &outValue)
243 {
244 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
245     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
246     bool isNewExtractor = false;
247     std::string tempPath = patchPath.empty() ? hapPath : patchPath;
248     auto extractor = AbilityBase::ExtractorUtil::GetExtractor(tempPath, isNewExtractor);
249     if (extractor == nullptr) {
250         RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor by hapPath");
251         return NOT_FOUND;
252     }
253     std::string rawfilePath = GetRawFilePath(extractor, rawFileName);
254     if (!extractor->HasEntry(rawfilePath) && patchPath.empty()) {
255         RESMGR_HILOGD(RESMGR_TAG, "the rawfile file is not exist in hap");
256         return ERROR_CODE_RES_PATH_INVALID;
257     }
258     if (!extractor->HasEntry(rawfilePath) && !patchPath.empty()) {
259         extractor = AbilityBase::ExtractorUtil::GetExtractor(hapPath, isNewExtractor);
260         if (extractor == nullptr) {
261             RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor by patch");
262             return NOT_FOUND;
263         }
264         rawfilePath = GetRawFilePath(extractor, rawFileName);
265         if (!extractor->HasEntry(rawfilePath)) {
266             RESMGR_HILOGD(RESMGR_TAG, "the rawfile file is not exist patch");
267             return ERROR_CODE_RES_PATH_INVALID;
268         }
269     }
270     bool ret = extractor->ExtractToBufByName(rawfilePath, outValue, len);
271     if (!ret) {
272         RESMGR_HILOGE(RESMGR_TAG, "failed to get rawfile data");
273         return NOT_FOUND;
274     }
275 #endif
276     return SUCCESS;
277 }
278 
279 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
GetExtractor(const char * hapPath,const char * patchPath,char * outPath,const std::string & rawFileName,std::string & rawfilePath,std::shared_ptr<AbilityBase::Extractor> & extractor)280 RState GetExtractor(const char *hapPath, const char *patchPath, char *outPath, const std::string &rawFileName,
281                     std::string &rawfilePath, std::shared_ptr<AbilityBase::Extractor> &extractor)
282 {
283     bool isNewExtractor = false;
284     bool patchPathHasEntry = false;
285     if (strlen(patchPath) != 0) {
286         Utils::CanonicalizePath(patchPath, outPath, PATH_MAX);
287         extractor = AbilityBase::ExtractorUtil::GetExtractor(outPath, isNewExtractor);
288         if (extractor == nullptr) {
289             RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor in ReadRawFileDescriptor");
290             return NOT_FOUND;
291         }
292         rawfilePath = GetRawFilePath(extractor, rawFileName);
293         if (extractor->HasEntry(rawfilePath)) {
294             patchPathHasEntry = true;
295         }
296     }
297     if (!patchPathHasEntry) {
298         Utils::CanonicalizePath(hapPath, outPath, PATH_MAX);
299         extractor = AbilityBase::ExtractorUtil::GetExtractor(outPath, isNewExtractor);
300         if (extractor == nullptr) {
301             RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor in patch hap");
302             return NOT_FOUND;
303         }
304         rawfilePath = GetRawFilePath(extractor, rawFileName);
305         if (!extractor->HasEntry(rawfilePath)) {
306             RESMGR_HILOGD(RESMGR_TAG, "the rawfile file is not exist in patch hap");
307             return ERROR_CODE_RES_PATH_INVALID;
308         }
309     }
310     return SUCCESS;
311 }
312 #endif
313 
ReadRawFileDescriptor(const char * hapPath,const char * patchPath,const std::string & rawFileName,ResourceManager::RawFileDescriptor & descriptor)314 RState HapParser::ReadRawFileDescriptor(const char *hapPath, const char *patchPath, const std::string &rawFileName,
315     ResourceManager::RawFileDescriptor &descriptor)
316 {
317 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
318     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
319     char outPath[PATH_MAX + 1] = {0};
320     std::string rawfilePath = "";
321     std::shared_ptr<AbilityBase::Extractor> extractor = nullptr;
322     RState resoult = GetExtractor(hapPath, patchPath, outPath, rawFileName, rawfilePath, extractor);
323     if (resoult != SUCCESS) {
324         return resoult;
325     }
326     AbilityBase::FileInfo fileInfo;
327     bool ret = extractor->GetFileInfo(rawfilePath, fileInfo);
328     if (!ret) {
329         RESMGR_HILOGE(RESMGR_TAG, "failed to get rawFileDescriptor rawfilePath");
330         return NOT_FOUND;
331     }
332     int zipFd = open(outPath, O_RDONLY);
333     if (zipFd < 0) {
334         RESMGR_HILOGE(RESMGR_TAG, "failed open file by path");
335         return NOT_FOUND;
336     }
337     descriptor.offset = static_cast<long>(fileInfo.offset);
338     descriptor.length = static_cast<long>(fileInfo.length);
339     descriptor.fd = zipFd;
340 #endif
341     return SUCCESS;
342 }
343 
GetRawFileList(const std::string & hapPath,const std::string & rawDirPath,std::set<std::string> & fileSet)344 RState HapParser::GetRawFileList(const std::string &hapPath, const std::string &rawDirPath,
345     std::set<std::string>& fileSet)
346 {
347 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
348     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
349     bool isNewExtractor = false;
350     auto extractor = AbilityBase::ExtractorUtil::GetExtractor(hapPath, isNewExtractor);
351     if (extractor == nullptr) {
352         RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor from ability in GetRawFileList hapPath");
353         return NOT_FOUND;
354     }
355     std::string rawfilePath = GetRawFilePath(extractor, rawDirPath);
356     if (!extractor->IsDirExist(rawfilePath)) {
357         RESMGR_HILOGD(RESMGR_TAG, "the rawfile dir is not exist in hap");
358         return ERROR_CODE_RES_PATH_INVALID;
359     }
360     bool ret = extractor->GetFileList(rawfilePath, fileSet);
361     if (!ret) {
362         RESMGR_HILOGE(RESMGR_TAG, "failed to get fileSet from ability rawfilePath");
363         return ERROR_CODE_RES_PATH_INVALID;
364     }
365 #endif
366     return SUCCESS;
367 }
368 
GetRawFileListUnCompressed(const std::string & indexPath,const std::string & rawDirPath,std::vector<std::string> & fileList)369 RState HapParser::GetRawFileListUnCompressed(const std::string &indexPath, const std::string &rawDirPath,
370     std::vector<std::string>& fileList)
371 {
372 #ifdef __WINNT__
373     char seperator = '\\';
374 #else
375     char seperator = '/';
376 #endif
377     auto pos = indexPath.rfind(seperator);
378     if (pos == std::string::npos) {
379         return ERROR_CODE_RES_PATH_INVALID;
380     }
381     std::string rawFilePath = indexPath.substr(0, pos) + "/resources/";
382     GetPath(rawDirPath, rawFilePath);
383     return Utils::GetFiles(rawFilePath, fileList);
384 }
385 
BuildResConfig(ResConfigKey * configKey)386 std::shared_ptr<ResConfigImpl> HapParser::BuildResConfig(ResConfigKey *configKey)
387 {
388     if (configKey == nullptr) {
389         RESMGR_HILOGE(RESMGR_TAG, "configKey is null");
390         return nullptr;
391     }
392     auto resConfig = std::make_shared<ResConfigImpl>();
393     if (resConfig == nullptr) {
394         RESMGR_HILOGE(RESMGR_TAG, "new ResConfigImpl failed when BuildResConfig");
395         return nullptr;
396     }
397     resConfig->SetDeviceType(configKey->deviceType);
398     resConfig->SetDirection(configKey->direction);
399     resConfig->SetColorMode(configKey->colorMode);
400     resConfig->SetMcc(configKey->mcc);
401     resConfig->SetMnc(configKey->mnc);
402     resConfig->SetInputDevice(configKey->inputDevice);
403     resConfig->SetScreenDensity((configKey->screenDensity) / Utils::DPI_BASE);
404     RState r = resConfig->SetLocaleInfo(configKey->language, configKey->script, configKey->region);
405     if (r != SUCCESS) {
406         RESMGR_HILOGE(RESMGR_TAG,
407             "error set locale,lang %s,script %s,region %s", configKey->language, configKey->script, configKey->region);
408     }
409 
410     return resConfig;
411 }
412 
GetKeyParamsLocales(std::shared_ptr<KeyParam> kp,std::string & locale,bool & isLocale)413 void HapParser::GetKeyParamsLocales(std::shared_ptr<KeyParam> kp, std::string &locale, bool &isLocale)
414 {
415     KeyType keyType = kp->type_;
416     if (keyType == KeyType::MCC || keyType == KeyType::MNC) {
417         return;
418     }
419     if (keyType == KeyType::LANGUAGES) {
420         locale = kp->GetStr();
421         isLocale = true;
422         return;
423     }
424     if (keyType == KeyType::SCRIPT) {
425         locale.append("-");
426         locale.append(kp->GetStr());
427         return;
428     }
429     if (keyType == KeyType::REGION) {
430         locale.append("-");
431         locale.append(kp->GetStr());
432     }
433 }
434 
GetDeviceType(uint32_t value)435 DeviceType HapParser::GetDeviceType(uint32_t value)
436 {
437     DeviceType deviceType = DEVICE_NOT_SET;
438     if (value == DEVICE_CAR) {
439         deviceType = DEVICE_CAR;
440     } else if (value == DEVICE_PAD) {
441         deviceType = DEVICE_PAD;
442     } else if (value == DEVICE_PHONE) {
443         deviceType = DEVICE_PHONE;
444     } else if (value == DEVICE_TABLET) {
445         deviceType = DEVICE_TABLET;
446     } else if (value == DEVICE_TV) {
447         deviceType = DEVICE_TV;
448     } else if (value == DEVICE_WEARABLE) {
449         deviceType = DEVICE_WEARABLE;
450     } else if (value == DEVICE_TWOINONE) {
451         deviceType = DEVICE_TWOINONE;
452     }
453     return deviceType;
454 }
455 
GetMcc(uint32_t value)456 uint32_t HapParser::GetMcc(uint32_t value)
457 {
458     return value;
459 }
460 
GetMnc(uint32_t value)461 uint32_t HapParser::GetMnc(uint32_t value)
462 {
463     return value;
464 }
465 
GetColorMode(uint32_t value)466 ColorMode HapParser::GetColorMode(uint32_t value)
467 {
468     ColorMode colorMode = COLOR_MODE_NOT_SET;
469     if (value == DARK) {
470         colorMode = DARK;
471     } else {
472         colorMode = LIGHT;
473     }
474     return colorMode;
475 }
476 
GetInputDevice(uint32_t value)477 InputDevice HapParser::GetInputDevice(uint32_t value)
478 {
479     InputDevice inputDevice = INPUTDEVICE_NOT_SET;
480     if (value == INPUTDEVICE_POINTINGDEVICE) {
481         inputDevice = INPUTDEVICE_POINTINGDEVICE;
482     }
483     return inputDevice;
484 }
485 
GetScreenDensity(uint32_t value)486 ScreenDensity HapParser::GetScreenDensity(uint32_t value)
487 {
488     ScreenDensity screenDensity = SCREEN_DENSITY_NOT_SET;
489     if (value == SCREEN_DENSITY_SDPI) {
490         screenDensity = SCREEN_DENSITY_SDPI;
491     } else if (value == SCREEN_DENSITY_MDPI) {
492         screenDensity = SCREEN_DENSITY_MDPI;
493     } else if (value == SCREEN_DENSITY_LDPI) {
494         screenDensity = SCREEN_DENSITY_LDPI;
495     } else if (value == SCREEN_DENSITY_XLDPI) {
496         screenDensity = SCREEN_DENSITY_XLDPI;
497     } else if (value == SCREEN_DENSITY_XXLDPI) {
498         screenDensity = SCREEN_DENSITY_XXLDPI;
499     } else if (value == SCREEN_DENSITY_XXXLDPI) {
500         screenDensity = SCREEN_DENSITY_XXXLDPI;
501     }
502     return screenDensity;
503 }
504 
IsRawDirFromHap(const char * hapPath,const std::string & pathName,bool & outValue)505 RState HapParser::IsRawDirFromHap(const char *hapPath, const std::string &pathName, bool &outValue)
506 {
507 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
508     if (pathName.empty()) {
509         RESMGR_HILOGE(RESMGR_TAG, "the rawfile path is empty");
510         return ERROR_CODE_RES_PATH_INVALID;
511     }
512     bool isNewExtractor = false;
513     auto extractor = AbilityBase::ExtractorUtil::GetExtractor(hapPath, isNewExtractor);
514     if (extractor == nullptr) {
515         RESMGR_HILOGE(RESMGR_TAG, "failed to get extractor hapPath");
516         return NOT_FOUND;
517     }
518     std::string rawPath = GetRawFilePath(extractor, pathName);
519     if (extractor->HasEntry(rawPath)) {
520         outValue = false;
521     } else if (extractor->IsDirExist(rawPath)) {
522         outValue = true;
523     } else {
524         RESMGR_HILOGD(RESMGR_TAG, "the rawfile file is not exist in hap");
525         return ERROR_CODE_RES_PATH_INVALID;
526     }
527 #endif
528     return SUCCESS;
529 }
530 
IsRawDirUnCompressed(const std::string & pathName,bool & outValue)531 RState HapParser::IsRawDirUnCompressed(const std::string &pathName, bool &outValue)
532 {
533     char outPath[PATH_MAX + 1] = {0};
534     Utils::CanonicalizePath(pathName.c_str(), outPath, PATH_MAX);
535     struct stat fileStat {};
536     if (stat(outPath, &fileStat) != 0) {
537         RESMGR_HILOGE(RESMGR_TAG, "failed to get rawfile file info");
538         return ERROR_CODE_RES_PATH_INVALID;
539     }
540     if ((fileStat.st_mode & S_IFDIR)) {
541         outValue = true;
542     } else if ((fileStat.st_mode & S_IFREG)) {
543         outValue = false;
544     } else {
545         RESMGR_HILOGE(RESMGR_TAG, "the rawfile file is not exist");
546         return ERROR_CODE_RES_PATH_INVALID;
547     }
548     return SUCCESS;
549 }
550 
CreateResConfigFromKeyParams(const std::vector<std::shared_ptr<KeyParam>> & keyParams)551 std::shared_ptr<ResConfigImpl> HapParser::CreateResConfigFromKeyParams(
552     const std::vector<std::shared_ptr<KeyParam>> &keyParams)
553 {
554     auto resConfig = std::make_shared<ResConfigImpl>();
555     if (resConfig == nullptr) {
556         RESMGR_HILOGE(RESMGR_TAG, "new ResConfigImpl failed when CreateResConfigFromKeyParams");
557         return nullptr;
558     }
559     size_t len = keyParams.size();
560     // default path
561     if (len == 0) {
562         resConfig->SetColorMode(COLOR_MODE_NOT_SET);
563         return resConfig;
564     }
565     size_t i = 0;
566     ResConfigKey configKey;
567     for (i = 0; i < len; ++i) {
568         const std::shared_ptr<KeyParam> kp = keyParams.at(i);
569         if (kp->type_ == LANGUAGES) {
570             configKey.language = kp->GetStr().c_str();
571         } else if (kp->type_ == REGION) {
572             configKey.region = kp->GetStr().c_str();
573         } else if (kp->type_ == SCRIPT) {
574             configKey.script = kp->GetStr().c_str();
575         } else if (kp->type_ == SCREEN_DENSITY) {
576             configKey.screenDensity = GetScreenDensity(kp->value_);
577         } else if (kp->type_ == DEVICETYPE) {
578             configKey.deviceType = GetDeviceType(kp->value_);
579         } else if (kp->type_ == DIRECTION) {
580             if (kp->value_ == 0) {
581                 configKey.direction = DIRECTION_VERTICAL;
582             } else {
583                 configKey.direction = DIRECTION_HORIZONTAL;
584             }
585         } else if (kp->type_ == INPUTDEVICE) {
586             configKey.inputDevice = GetInputDevice(kp->value_);
587         } else if (kp->type_ == COLORMODE) {
588             configKey.colorMode = GetColorMode(kp->value_);
589         } else if (kp->type_ == MCC) {
590             configKey.mcc = GetMcc(kp->value_);
591         } else if (kp->type_ == MNC) {
592             configKey.mnc = GetMnc(kp->value_);
593         }
594     }
595     return BuildResConfig(&configKey);
596 }
597 } // namespace Resource
598 } // namespace Global
599 } // namespace OHOS
600