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 "security_guard_utils.h"
17 
18 #include <cerrno>
19 #include <fstream>
20 
21 #include "security_guard_log.h"
22 
23 namespace OHOS::Security::SecurityGuard {
24 namespace {
25     constexpr int32_t DEC_RADIX = 10;
26     constexpr int32_t HEX_RADIX = 16;
27     constexpr int32_t STR_INDEX = 2;
28     constexpr int32_t TIME_BUF_LEN = 32;
29     constexpr int32_t FILE_MAX_SIZE = 2 * 1024 * 1024; // byte
30 }
31 
StrToU32(const std::string &str, uint32_t &value)32 bool SecurityGuardUtils::StrToU32(const std::string &str, uint32_t &value)
33 {
34     unsigned long long tmp = 0;
35     bool isOK = StrToULL(str, tmp);
36     value = static_cast<uint32_t>(tmp);
37     return isOK && (tmp <= UINT32_MAX);
38 }
39 
StrToI64(const std::string &str, int64_t &value)40 bool SecurityGuardUtils::StrToI64(const std::string &str, int64_t &value)
41 {
42     long long tmp = 0;
43     bool isOK = StrToLL(str, tmp, DEC_RADIX);
44     value = tmp;
45     return isOK && (tmp >= INT64_MIN && tmp <= INT64_MAX);
46 }
47 
StrToI64Hex(const std::string &str, int64_t &value)48 bool SecurityGuardUtils::StrToI64Hex(const std::string &str, int64_t &value)
49 {
50     long long tmp = 0;
51     bool isOK;
52     if (str.substr(0, STR_INDEX) == "0x") {
53         isOK = StrToLL(str, tmp, HEX_RADIX);
54     } else {
55         isOK = StrToLL(str, tmp, DEC_RADIX);
56     }
57     value = tmp;
58     return isOK && (tmp >= INT64_MIN && tmp <= INT64_MAX);
59 }
60 
StrToLL(const std::string &str, long long &value, int32_t base)61 bool SecurityGuardUtils::StrToLL(const std::string &str, long long &value, int32_t base)
62 {
63     auto add = str.c_str();
64     char *end = nullptr;
65     errno = 0;
66     value = strtoll(add, &end, base);
67     if ((errno == ERANGE && (value == LLONG_MAX || value == LLONG_MIN)) || (errno != 0 && value == 0)) {
68         SGLOGE("strtoll converse error,str=%{public}s", str.c_str());
69         return false;
70     }
71     if (end == add) {
72         SGLOGE("strtoll no digit find");
73         return false;
74     }
75     if (end[0] != '\0') {
76         SGLOGE("strtoll no all digit");
77         return false;
78     }
79 
80     return true;
81 }
82 
StrToULL(const std::string &str, unsigned long long &value)83 bool SecurityGuardUtils::StrToULL(const std::string &str, unsigned long long &value)
84 {
85     auto add = str.c_str();
86     char *end = nullptr;
87     errno = 0;
88     value = strtoull(add, &end, DEC_RADIX);
89     if ((errno == ERANGE && value == ULLONG_MAX) || (errno != 0 && value == 0)) {
90         SGLOGE("strtoull converse error,str=%{public}s", str.c_str());
91         return false;
92     }
93     if (end == add) {
94         SGLOGE("strtoull no digit find");
95         return false;
96     }
97     if (end[0] != '\0') {
98         SGLOGE("strtoull no all digit");
99         return false;
100     }
101 
102     return true;
103 }
104 
GetDate()105 std::string SecurityGuardUtils::GetDate()
106 {
107     time_t timestamp = time(nullptr);
108     struct tm timeInfo{};
109     localtime_r(&timestamp, &timeInfo);
110     char buf[TIME_BUF_LEN] = {};
111     if (strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", &timeInfo) == 0) {
112         return "";
113     }
114     std::string data(buf);
115     return data;
116 }
117 
CopyFile(const std::string &srcPath, const std::string &dstPath)118 bool SecurityGuardUtils::CopyFile(const std::string &srcPath, const std::string &dstPath)
119 {
120     std::ifstream src(srcPath, std::ios::binary);
121     if (!src.is_open()) {
122         SGLOGE("copy file stream error");
123         return false;
124     }
125     if (src.seekg(0, std::ios_base::end).tellg() > FILE_MAX_SIZE) {
126         SGLOGE("cfg file is too large");
127         src.close();
128         return false;
129     }
130     src.seekg(0, std::ios::beg);
131     std::ofstream dst(dstPath, std::ios::binary);
132     if (!dst.is_open()) {
133         SGLOGE("copy file stream error");
134         src.close();
135         return false;
136     }
137 
138     dst << src.rdbuf();
139     src.close();
140     dst.close();
141     return true;
142 }
143 }