• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 COMMUNICATIONNETSTACK_COMMON_UTILS_H
17 #define COMMUNICATIONNETSTACK_COMMON_UTILS_H
18 
19 #include <iosfwd>
20 #include <list>
21 #include <vector>
22 #include <string>
23 #include <optional>
24 
25 namespace OHOS::NetStack::CommonUtils {
26 static const std::string DOMAIN_TYPE_HTTP_REQUEST = "httpRequest";
27 static const std::string DOMAIN_TYPE_WEBSOCKET_REQUEST = "webSocket";
28 const int INVALID_IP_TYPE = -1;
29 enum SdkVersion {
30     FIVE = 5,
31     SIX,
32     SEVEN,
33     EIGHT,
34     NINE,
35     TEN,
36     ELEVEN,
37     TWELVE
38 };
39 
40 std::vector<std::string> Split(const std::string &str, const std::string &sep);
41 
42 std::vector<std::string> Split(const std::string &str, const std::string &sep, size_t size);
43 
44 std::string Strip(const std::string &str, char ch = ' ');
45 
46 std::string ToLower(const std::string &s);
47 
48 std::string ToString(const std::list<std::string> &lists, char tab = ',');
49 
50 bool HasInternetPermission();
51 
52 bool IsAtomicService(std::string &bundleName);
53 
54 bool IsAllowedHostname(const std::string &bundleName, const std::string &domainType, const std::string &url);
55 
56 bool EndsWith(const std::string &str, const std::string &suffix);
57 
58 std::string Trim(std::string str);
59 
60 bool IsMatch(const std::string &str, const std::string &patternStr);
61 
62 std::string InsertCharBefore(const std::string &input, const char from, const char preChar, const char nextChar);
63 
64 std::string ReplaceCharacters(const std::string &input);
65 
66 bool IsRegexValid(const std::string &regex);
67 
68 std::string GetProtocolFromURL(const std::string &url);
69 
70 std::string GetPortFromURL(const std::string &url);
71 
72 std::string GetHostnameFromURL(const std::string& url);
73 
74 std::string GetHostnameWithProtocolAndPortFromURL(const std::string& url);
75 
76 bool IsExcluded(const std::string &str, const std::string &exclusions, const std::string &split);
77 
78 bool IsHostNameExcluded(const std::string &url, const std::string &exclusions, const std::string &split);
79 
80 int DetectIPType(const std::string &ip);
81 
82 bool IsValidIP(const std::string& ip, int af);
83 
84 bool IsValidIPV4(const std::string &ip);
85 
86 bool IsValidIPV6(const std::string &ip);
87 
88 std::string MaskIpv4(std::string &maskedResult);
89 
90 std::string MaskIpv6(std::string &maskedResult);
91 
92 std::string AnonymizeIp(std::string &input);
93 
94 std::optional<std::string> GetBundleName();
95 
96 bool GetFileDataFromFilePath(const std::string& filePath, std::string& fileData);
97 
98 bool Sha256sum(unsigned char *buf, size_t buflen, std::string &digestStr);
99 
100 bool IsCertPubKeyInPinned(const std::string &certPubKeyDigest, const std::string &pinnedPubkey);
101 
102 bool IsCleartextPermitted(const std::string &url, const std::string &protocol);
103 } // namespace OHOS::NetStack::CommonUtils
104 #endif /* COMMUNICATIONNETSTACK_COMMON_UTILS_H */
105