• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "zip_utils.h"
16 
17 #include <regex>
18 
19 #include "parallel_task_dispatcher.h"
20 #include "runnable.h"
21 #include "task_dispatcher_context.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace LIBZIP {
26 namespace {
27 const std::string SEPARATOR = "/";
28 const std::regex FILE_PATH_REGEX("([0-9A-Za-z/+_=\\-,.])+");
29 }  // namespace
30 using namespace OHOS::AppExecFwk;
31 
32 std::shared_ptr<ParallelTaskDispatcher> g_TaskDispatcher = nullptr;
PostTask(const std::shared_ptr<Runnable> & runnable)33 void PostTask(const std::shared_ptr<Runnable> &runnable)
34 {
35     static TaskDispatcherContext taskContext;
36 
37     if (g_TaskDispatcher == nullptr) {
38         g_TaskDispatcher = taskContext.CreateParallelDispatcher(std::string("toolszip"), TaskPriority::DEFAULT);
39     }
40 
41     if (g_TaskDispatcher != nullptr) {
42         g_TaskDispatcher->AsyncDispatch(runnable);
43     }
44 }
45 
GetCurrentSystemTime(void)46 struct tm *GetCurrentSystemTime(void)
47 {
48     auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
49     struct tm *time = localtime(&tt);
50     if (time == nullptr) {
51         return nullptr;
52     }
53     int baseYear = 1900;
54     time->tm_mday = time->tm_mday + baseYear;
55     time->tm_mday = time->tm_mon + 1;
56     return time;
57 }
58 
StartsWith(const std::string & str,const std::string & searchFor)59 bool StartsWith(const std::string &str, const std::string &searchFor)
60 {
61     if (searchFor.size() > str.size()) {
62         return false;
63     }
64 
65     std::string source = str.substr(0, searchFor.size());
66     return source == searchFor;
67 }
EndsWith(const std::string & str,const std::string & searchFor)68 bool EndsWith(const std::string &str, const std::string &searchFor)
69 {
70     if (searchFor.size() > str.size()) {
71         return false;
72     }
73 
74     std::string source = str.substr(str.size() - searchFor.size(), searchFor.size());
75     return source == searchFor;
76 }
77 
IsPathAbsolute(const std::string & path)78 bool IsPathAbsolute(const std::string &path)
79 {
80     // Look for a separator in the first position.
81     return path.length() > 0 && StartsWith(path, SEPARATOR);
82 }
83 
IsASCIIChar(const char * pszStr)84 bool IsASCIIChar(const char *pszStr)
85 {
86     if (pszStr == nullptr) {
87         return false;
88     }
89 
90     // Judge whether the first character in the string is ASCII character (0 – 127), and ASCII character occupies one
91     // byte
92     return (static_cast<unsigned char>(pszStr[0]) & 0x80) == 0x80 ? false : true;
93 }
FilePathCheckValid(const std::string & str)94 bool FilePathCheckValid(const std::string &str)
95 {
96     return std::regex_match(str, FILE_PATH_REGEX);
97 }
98 
99 }  // namespace LIBZIP
100 }  // namespace AppExecFwk
101 }  // namespace OHOS
102