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
FormatUri(std::string & uri)65 void URIUtils::FormatUri(std::string &uri)
66 {
67 auto pos = uri.find_last_of('?');
68 if (pos == std::string::npos) {
69 return;
70 }
71
72 uri.resize(pos);
73 }
74
GetUriConfig(const std::string & uri)75 __attribute__((no_sanitize("cfi"))) UriConfig URIUtils::GetUriConfig(const std::string &uri)
76 {
77 UriConfig uriConfig;
78 Uri uriTemp(uri);
79 uriConfig.authority = uriTemp.GetAuthority();
80 uriConfig.path = uriTemp.GetPath();
81 uriTemp.GetPathSegments(uriConfig.pathSegments);
82 uriConfig.scheme = uriTemp.GetScheme();
83 std::string convertUri = DATA_PROXY_SCHEMA + uriConfig.authority + uriConfig.path;
84 size_t schemePos = convertUri.find(PARAM_URI_SEPARATOR);
85 if (schemePos != std::string::npos) {
86 convertUri.replace(schemePos, PARAM_URI_SEPARATOR_LEN, SCHEME_SEPARATOR);
87 }
88 uriConfig.formatUri = convertUri;
89 return uriConfig;
90 }
91
Anonymous(const std::string & uri)92 std::string URIUtils::Anonymous(const std::string &uri)
93 {
94 if (uri.length() <= END_LENGTH) {
95 return DEFAULT_ANONYMOUS;
96 }
97
98 return (DEFAULT_ANONYMOUS + uri.substr(uri.length() - END_LENGTH, END_LENGTH));
99 }
100
Strtoul(const std::string & str)101 std::pair<bool, uint32_t> URIUtils::Strtoul(const std::string &str)
102 {
103 unsigned long data = 0;
104 if (str.empty()) {
105 return std::make_pair(false, data);
106 }
107 char* end = nullptr;
108 errno = 0;
109 data = strtoul(str.c_str(), &end, 10);
110 if (errno == ERANGE || end == nullptr || end == str || *end != '\0') {
111 return std::make_pair(false, data);
112 }
113 return std::make_pair(true, data);
114 }
115
GetQueryParams(const std::string & uri)116 std::map<std::string, std::string> URIUtils::GetQueryParams(const std::string& uri)
117 {
118 size_t queryStartPos = uri.find('?');
119 if (queryStartPos == std::string::npos) {
120 return {};
121 }
122 std::map<std::string, std::string> params;
123 std::string queryParams = uri.substr(queryStartPos + 1);
124 size_t startPos = 0;
125 while (startPos < queryParams.size()) {
126 size_t delimiterIndex = queryParams.find('&', startPos);
127 if (delimiterIndex == std::string::npos) {
128 delimiterIndex = queryParams.size();
129 }
130 size_t equalIndex = queryParams.find('=', startPos);
131 if (equalIndex == std::string::npos || equalIndex > delimiterIndex) {
132 startPos = delimiterIndex + 1;
133 continue;
134 }
135 std::string key = queryParams.substr(startPos, equalIndex - startPos);
136 std::string value = queryParams.substr(equalIndex + 1, delimiterIndex - equalIndex - 1);
137 params[key] = value;
138 startPos = delimiterIndex + 1;
139 }
140 return params;
141 }
142 } // namespace OHOS::DataShare