1 /*
2 * Copyright (c) 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 #define LOG_TAG "URIUtils"
16
17 #include "uri_utils.h"
18
19 #include <string>
20
21 #include "log_print.h"
22 #include "string_ex.h"
23 #include "uri.h"
24 #include "utils/anonymous.h"
25
26 namespace OHOS::DataShare {
GetInfoFromURI(const std::string & uri,UriInfo & uriInfo)27 bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo)
28 {
29 Uri uriTemp(uri);
30 std::vector<std::string> splitUri;
31 SplitStr(uriTemp.GetPath(), "/", splitUri);
32 if (splitUri.size() < PARAM_SIZE) {
33 ZLOGE("Invalid uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
34 return false;
35 }
36
37 if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() ||
38 splitUri[STORE_NAME].empty() || splitUri[TABLE_NAME].empty()) {
39 ZLOGE("Uri has empty field! bundleName: %{public}s uri: %{public}s", splitUri[BUNDLE_NAME].c_str(),
40 DistributedData::Anonymous::Change(uri).c_str());
41 return false;
42 }
43
44 uriInfo.bundleName = splitUri[BUNDLE_NAME];
45 uriInfo.moduleName = splitUri[MODULE_NAME];
46 uriInfo.storeName = splitUri[STORE_NAME];
47 uriInfo.tableName = splitUri[TABLE_NAME];
48 return true;
49 }
50
IsDataProxyURI(const std::string & uri)51 bool URIUtils::IsDataProxyURI(const std::string &uri)
52 {
53 return uri.compare(0, DATA_PROXY_SCHEMA_LEN, URIUtils::DATA_PROXY_SCHEMA) == 0;
54 }
55
GetBundleNameFromProxyURI(const std::string & uri,std::string & bundleName)56 bool URIUtils::GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName)
57 {
58 Uri uriTemp(uri);
59 if (!uriTemp.GetAuthority().empty()) {
60 bundleName = uriTemp.GetAuthority();
61 }
62 return true;
63 }
64
GetAppIndexFromProxyURI(const std::string & uri,int32_t & appIndex)65 bool URIUtils::GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex)
66 {
67 auto queryParams = URIUtils::GetQueryParams(uri);
68 if (!queryParams[APP_INDEX].empty()) {
69 auto [success, data] = URIUtils::Strtoul(queryParams[APP_INDEX]);
70 if (!success) {
71 appIndex = -1;
72 ZLOGE("appIndex is invalid! appIndex: %{public}s", queryParams[APP_INDEX].c_str());
73 return false;
74 }
75 appIndex = static_cast<int32_t>(data);
76 }
77 return true;
78 }
79
FormatUri(std::string & uri)80 void URIUtils::FormatUri(std::string &uri)
81 {
82 auto pos = uri.find_last_of('?');
83 if (pos == std::string::npos) {
84 return;
85 }
86
87 uri.resize(pos);
88 }
89
GetUriConfig(const std::string & uri)90 __attribute__((no_sanitize("cfi"))) UriConfig URIUtils::GetUriConfig(const std::string &uri)
91 {
92 UriConfig uriConfig;
93 Uri uriTemp(uri);
94 uriConfig.authority = uriTemp.GetAuthority();
95 uriConfig.path = uriTemp.GetPath();
96 uriTemp.GetPathSegments(uriConfig.pathSegments);
97 uriConfig.scheme = uriTemp.GetScheme();
98 std::string convertUri = DATA_PROXY_SCHEMA + uriConfig.authority + uriConfig.path;
99 size_t schemePos = convertUri.find(PARAM_URI_SEPARATOR);
100 if (schemePos != std::string::npos) {
101 convertUri.replace(schemePos, PARAM_URI_SEPARATOR_LEN, SCHEME_SEPARATOR);
102 }
103 uriConfig.formatUri = convertUri;
104 return uriConfig;
105 }
106
Anonymous(const std::string & uri)107 std::string URIUtils::Anonymous(const std::string &uri)
108 {
109 if (uri.length() <= END_LENGTH) {
110 return DEFAULT_ANONYMOUS;
111 }
112
113 return (DEFAULT_ANONYMOUS + uri.substr(uri.length() - END_LENGTH, END_LENGTH));
114 }
115
Strtoul(const std::string & str)116 std::pair<bool, uint32_t> URIUtils::Strtoul(const std::string &str)
117 {
118 unsigned long data = 0;
119 if (str.empty()) {
120 return std::make_pair(false, data);
121 }
122 char* end = nullptr;
123 errno = 0;
124 data = strtoul(str.c_str(), &end, 10);
125 if (errno == ERANGE || end == nullptr || end == str || *end != '\0') {
126 return std::make_pair(false, data);
127 }
128 return std::make_pair(true, data);
129 }
130
GetQueryParams(const std::string & uri)131 std::map<std::string, std::string> URIUtils::GetQueryParams(const std::string& uri)
132 {
133 size_t queryStartPos = uri.find('?');
134 if (queryStartPos == std::string::npos) {
135 return {};
136 }
137 std::map<std::string, std::string> params;
138 std::string queryParams = uri.substr(queryStartPos + 1);
139 size_t startPos = 0;
140 while (startPos < queryParams.size()) {
141 size_t delimiterIndex = queryParams.find('&', startPos);
142 if (delimiterIndex == std::string::npos) {
143 delimiterIndex = queryParams.size();
144 }
145 size_t equalIndex = queryParams.find('=', startPos);
146 if (equalIndex == std::string::npos || equalIndex > delimiterIndex) {
147 startPos = delimiterIndex + 1;
148 continue;
149 }
150 std::string key = queryParams.substr(startPos, equalIndex - startPos);
151 std::string value = queryParams.substr(equalIndex + 1, delimiterIndex - equalIndex - 1);
152 params[key] = value;
153 startPos = delimiterIndex + 1;
154 }
155 return params;
156 }
157 } // namespace OHOS::DataShare