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 
16 #ifndef DATASHARESERVICE_URI_UTILS_H
17 #define DATASHARESERVICE_URI_UTILS_H
18 
19 #include <map>
20 #include <vector>
21 #include <string>
22 namespace OHOS::DataShare {
23 struct UriInfo {
24     std::string bundleName;
25     std::string moduleName;
26     std::string storeName;
27     std::string tableName;
28 };
29 
30 struct UriConfig {
31     std::string authority;
32     std::string path;
33     std::vector<std::string> pathSegments;
34     std::string formatUri;
35     std::string scheme;
36 };
37 
38 class URIUtils {
39 public:
40     static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo);
41     static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName);
42     static bool GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex);
43     static bool IsDataProxyURI(const std::string &uri);
44     static void FormatUri(std::string &uri);
45     static UriConfig GetUriConfig(const std::string &uri);
46     static std::string Anonymous(const std::string &uri);
47     static std::map<std::string, std::string> GetQueryParams(const std::string& uri);
48     static std::pair<bool, uint32_t> Strtoul(const std::string &str);
49     static constexpr const char *DATA_SHARE_SCHEMA = "datashare:///";;
50     static constexpr const char DATA_PROXY_SCHEMA[] = "datashareproxy://";
51     static constexpr const char *PARAM_URI_SEPARATOR = ":///";
52     static constexpr const char *SCHEME_SEPARATOR = "://";
53     static constexpr const char *URI_SEPARATOR = "/";
54     static constexpr const char *APP_INDEX = "appIndex";  // for Application Clone
55     static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA) - 1;
56     static constexpr uint32_t PARAM_URI_SEPARATOR_LEN = 4;
57 
58 private:
59     enum PATH_PARAM : int32_t {
60         BUNDLE_NAME = 0,
61         MODULE_NAME,
62         STORE_NAME,
63         TABLE_NAME,
64         PARAM_SIZE
65     };
66     static constexpr int32_t END_LENGTH = 10;
67     static constexpr const char *DEFAULT_ANONYMOUS = "******";
68 };
69 } // namespace OHOS::DataShare
70 #endif // DATASHARESERVICE_URI_UTILS_H
71