• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MOCK_PARSE_REQUESTCODE_H
18 #define MOCK_PARSE_REQUESTCODE_H
19 
20 #include <cstdlib>
21 #include <regex>
22 
23 #include "hilog_tag_wrapper.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 constexpr int64_t MAX_REQUEST_CODE = 562949953421311;
28 constexpr size_t MAX_REQUEST_CODE_LENGTH = 15;
29 constexpr int32_t BASE_REQUEST_CODE_NUM = 10;
30 
RequestCodeFromStringToInt64(const std::string & requestCode)31 int64_t RequestCodeFromStringToInt64(const std::string &requestCode)
32 {
33     if (requestCode.size() > MAX_REQUEST_CODE_LENGTH) {
34         TAG_LOGW(AAFwkTag::CONTEXT, "requestCode too long: %{public}s", requestCode.c_str());
35         return 0;
36     }
37     std::regex formatRegex("^[1-9]\\d*|0$");
38     std::smatch sm;
39     if (!std::regex_match(requestCode, sm, formatRegex)) {
40         TAG_LOGW(AAFwkTag::CONTEXT, "requestCode match failed: %{public}s", requestCode.c_str());
41         return 0;
42     }
43     int64_t parsedRequestCode = 0;
44     parsedRequestCode = strtoll(requestCode.c_str(), nullptr, BASE_REQUEST_CODE_NUM);
45     if (parsedRequestCode > MAX_REQUEST_CODE) {
46         TAG_LOGW(AAFwkTag::CONTEXT, "requestCode too large: %{public}s", requestCode.c_str());
47         return 0;
48     }
49     return parsedRequestCode;
50 }
51 }  // namespace AbilityRuntime
52 }  // namespace OHOS
53 #endif // MOCK_PARSE_REQUESTCODE_H