• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 StringUtils {
39 public:
40     /**
41      * @brief Anonymizes the input string by preserving specified length at head/tail
42      *        and replacing the middle part with placeholder
43      * @param name Original string to be processed (non-empty assumed)
44      * @return std::string Anonymized result according to length rules:
45      *     1. Length <= 4: Returns DEFAULT_ANONYMOUS ("******")
46      *     2. 4 < Length <= 8: Returns first 4 chars + REPLACE_CHAIN ("***")
47      *     3. Length > 8: Returns first 4 chars + REPLACE_CHAIN + last 4 chars
48      * @example
49      *     "abcdefghi" -> "abcd***fghi"
50      *     "12345"   -> "1234***"
51      *     "AB"      -> "******"
52      */
53     static std::string GeneralAnonymous(const std::string &name);
54 };
55 
56 class URIUtils {
57 public:
58     static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo);
59     static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName);
60     static bool GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex);
61     static std::pair<bool, int32_t> GetUserFromProxyURI(const std::string &uri);
62     static bool IsDataProxyURI(const std::string &uri);
63     static void FormatUri(std::string &uri);
64     static std::string FormatConstUri(const std::string &uri);
65     static UriConfig GetUriConfig(const std::string &uri);
66     static std::string Anonymous(const std::string &uri);
67     static std::map<std::string, std::string> GetQueryParams(const std::string& uri);
68     static std::pair<bool, uint32_t> Strtoul(const std::string &str);
69     static constexpr const char *DATA_SHARE_SCHEMA = "datashare:///";;
70     static constexpr const char DATA_PROXY_SCHEMA[] = "datashareproxy://";
71     static constexpr const char *PARAM_URI_SEPARATOR = ":///";
72     static constexpr const char *SCHEME_SEPARATOR = "://";
73     static constexpr const char *URI_SEPARATOR = "/";
74     static constexpr const char *APP_INDEX = "appIndex";  // for Application Clone
75     static constexpr const char USER_PARAM[] = "user";
76     static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA) - 1;
77     static constexpr uint32_t PARAM_URI_SEPARATOR_LEN = 4;
78 
79 private:
80     enum PATH_PARAM : int32_t {
81         BUNDLE_NAME = 0,
82         MODULE_NAME,
83         STORE_NAME,
84         TABLE_NAME,
85         PARAM_SIZE
86     };
87     static constexpr int32_t END_LENGTH = 10;
88 };
89 } // namespace OHOS::DataShare
90 #endif // DATASHARESERVICE_URI_UTILS_H
91