• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "netstack_common_utils.h"
17 
18 #include <algorithm>
19 #include <cerrno>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 
24 #include "curl/curl.h"
25 #include "netstack_log.h"
26 
27 namespace OHOS::NetStack::CommonUtils {
Split(const std::string & str,const std::string & sep)28 std::vector<std::string> Split(const std::string &str, const std::string &sep)
29 {
30     std::string s = str;
31     std::vector<std::string> res;
32     while (!s.empty()) {
33         auto pos = s.find(sep);
34         if (pos == std::string::npos) {
35             res.emplace_back(s);
36             break;
37         }
38         res.emplace_back(s.substr(0, pos));
39         s = s.substr(pos + sep.size());
40     }
41     return res;
42 }
43 
Split(const std::string & str,const std::string & sep,size_t size)44 std::vector<std::string> Split(const std::string &str, const std::string &sep, size_t size)
45 {
46     std::string s = str;
47     std::vector<std::string> res;
48     while (!s.empty()) {
49         if (res.size() + 1 == size) {
50             res.emplace_back(s);
51             break;
52         }
53 
54         auto pos = s.find(sep);
55         if (pos == std::string::npos) {
56             res.emplace_back(s);
57             break;
58         }
59         res.emplace_back(s.substr(0, pos));
60         s = s.substr(pos + sep.size());
61     }
62     return res;
63 }
64 
Strip(const std::string & str,char ch)65 std::string Strip(const std::string &str, char ch)
66 {
67     int64_t i = 0;
68     while (static_cast<size_t>(i) < str.size() && str[i] == ch) {
69         ++i;
70     }
71     int64_t j = static_cast<int64_t>(str.size()) - 1;
72     while (j > 0 && str[j] == ch) {
73         --j;
74     }
75     if (i >= 0 && static_cast<size_t>(i) < str.size() && j >= 0 && static_cast<size_t>(j) < str.size() &&
76         j - i + 1 > 0) {
77         return str.substr(i, j - i + 1);
78     }
79     return "";
80 }
81 
ToLower(const std::string & s)82 std::string ToLower(const std::string &s)
83 {
84     std::string res = s;
85     std::transform(res.begin(), res.end(), res.begin(), tolower);
86     return res;
87 }
88 
ToString(const std::list<std::string> & lists,char tab)89 std::string ToString(const std::list<std::string> &lists, char tab)
90 {
91     std::string str;
92     int32_t index = 0;
93     for (const auto &s : lists) {
94         if (index > 0) {
95             str = str + tab;
96         }
97         str = str + s;
98         index++;
99     }
100     return str;
101 }
102 
HasInternetPermission()103 bool HasInternetPermission()
104 {
105 #ifndef OH_CORE_NETSTACK_PERMISSION_CHECK
106 #ifdef FUZZ_TEST
107     return true;
108 #endif
109     int testSock = socket(AF_INET, SOCK_STREAM, 0);
110     if (testSock < 0 && errno == EPERM) {
111         NETSTACK_LOGE("make tcp testSock failed errno is %{public}d %{public}s", errno, strerror(errno));
112         return false;
113     }
114     if (testSock > 0) {
115         close(testSock);
116     }
117     return true;
118 #else
119     constexpr int inetGroup = 40002003; // 3003 in gateway shell.
120     int groupNum = getgroups(0, nullptr);
121     if (groupNum <= 0) {
122         NETSTACK_LOGE("no group of INTERNET permission");
123         return false;
124     }
125     auto groups = (gid_t *)malloc(groupNum * sizeof(gid_t));
126     groupNum = getgroups(groupNum, groups);
127     for (int i = 0; i < groupNum; i++) {
128         if (groups[i] == inetGroup) {
129             free(groups);
130             return true;
131         }
132     }
133     free(groups);
134     NETSTACK_LOGE("INTERNET permission denied by group");
135     return false;
136 #endif
137 }
138 
EndsWith(const std::string & str,const std::string & suffix)139 bool EndsWith(const std::string &str, const std::string &suffix)
140 {
141     if (str.length() < suffix.length()) {
142         return false;
143     }
144     return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
145 }
146 } // namespace OHOS::NetStack::CommonUtils