1 /*
2 * Copyright (C) 2022 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 #include "telephony_common_utils.h"
17
18 #include <cstdint>
19 #include <regex>
20 #include <string>
21
22 #include "ipc_skeleton.h"
23 #include "telephony_log_wrapper.h"
24 #include "telephony_permission.h"
25
26 namespace OHOS {
27 namespace Telephony {
28 constexpr uint32_t INPUT_VALUE_LENGTH = 10;
29 constexpr uint8_t HEX_TYPE = 16;
GetBundleName()30 std::string GetBundleName()
31 {
32 int32_t uid = IPCSkeleton::GetCallingUid();
33 std::string bundleName = "";
34 TelephonyPermission::GetBundleNameByUid(uid, bundleName);
35 if (bundleName.empty()) {
36 bundleName.append(std::to_string(uid));
37 bundleName.append(std::to_string(IPCSkeleton::GetCallingPid()));
38 }
39 return bundleName;
40 }
41
IsValidDecValue(const std::string & inputValue)42 bool IsValidDecValue(const std::string &inputValue)
43 {
44 if (inputValue.length() > INPUT_VALUE_LENGTH) {
45 TELEPHONY_LOGE("The value entered is out of range, value:%{public}s", inputValue.c_str());
46 return false;
47 }
48 bool isValueNumber = regex_match(inputValue, std::regex("(-[\\d+]+)|(\\d+)"));
49 if (isValueNumber) {
50 int64_t numberValue = std::stoll(inputValue);
51 if ((numberValue >= INT32_MIN) && (numberValue <= INT32_MAX)) {
52 return true;
53 }
54 }
55 TELEPHONY_LOGI("InputValue is not a decimal number");
56 return false;
57 }
58
IsValidHexValue(const std::string & inputValue)59 bool IsValidHexValue(const std::string &inputValue)
60 {
61 if (inputValue.length() > INPUT_VALUE_LENGTH) {
62 TELEPHONY_LOGE("The value entered is out of range, value:%{public}s", inputValue.c_str());
63 return false;
64 }
65 bool isValueNumber = regex_match(inputValue, std::regex("0[xX][0-9a-fA-F]+"));
66 if (isValueNumber) {
67 int64_t numberValue = std::stoll(inputValue, nullptr, HEX_TYPE);
68 if ((numberValue >= INT32_MIN) && (numberValue <= INT32_MAX)) {
69 return true;
70 }
71 }
72 TELEPHONY_LOGI("InputValue is not a hexadecimal number");
73 return false;
74 }
75 } // namespace Telephony
76 } // namespace OHOS