• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2026 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 "networksliceutil.h"
17 #include <locale>
18 #include <codecvt>
19 #include <openssl/sha.h>
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 static constexpr int CONVERT_INT_AND_BYTE = 0x000000ff;
24 static constexpr int CONVERT_INT_AND_SHORT = 0x0000ffff;
25 static constexpr int LEN_UINT32 = 4;
26 static constexpr int BIT0 = 0;
27 static constexpr int BIT1 = 1;
28 static constexpr int BIT2 = 2;
29 static constexpr int BIT3 = 3;
30 static constexpr int MOVE_4 = 4;
31 static constexpr int MOVE_8 = 8;
32 static constexpr int MOVE_16 = 16;
33 static constexpr int MOVE_24 = 24;
34 static constexpr int LEN_IPV6ADDR = 16;
35 static constexpr int LEN_14 = 4;
36 static constexpr int LEN_4 = 4;
37 constexpr int32_t HEX_WIDTH = 2;
38 const char DEFAULT_STRING[] = "error";
39 const std::wstring DEFAULT_WSTRING = L"error";
40 const std::u16string DEFAULT_USTRING = u"error";
41 
Split(const std::string & str,const std::string delimiter)42 std::vector<std::string> Split(const std::string &str, const std::string delimiter)
43 {
44     std::vector<std::string> items;
45     size_t start = 0;
46     size_t pos = str.find(delimiter);
47     while (pos != std::string::npos) {
48         std::string item = str.substr(start, pos - start);
49         items.emplace_back(item);
50         start = pos + 1;
51         pos = str.find(delimiter, start);
52     }
53     if (start < str.length()) {
54         std::string tail = str.substr(start);
55         items.emplace_back(tail);
56     }
57     return items;
58 }
59 
ConvertInt2UnsignedByte(int value)60 uint8_t ConvertInt2UnsignedByte(int value)
61 {
62     return static_cast<uint8_t>(value & CONVERT_INT_AND_BYTE);
63 }
64 
ConvertInt2UnsignedShort(int value)65 short ConvertInt2UnsignedShort(int value)
66 {
67     return static_cast<short>(value & CONVERT_INT_AND_SHORT);
68 }
69 
ConvertUnsignedShort2Int(short value)70 int ConvertUnsignedShort2Int(short value)
71 {
72     return value & CONVERT_INT_AND_SHORT;
73 }
74 
ByteToHexStr(uint8_t byte)75 std::string ByteToHexStr(uint8_t byte)
76 {
77     static const char* hexDigits = "0123456789abcdef";
78     std::string result;
79     result.push_back(hexDigits[(byte >> MOVE_4) & 0x0F]);
80     result.push_back(hexDigits[byte & 0x0F]);
81     return result;
82 }
83 
GetShort(int & startIndex,std::vector<uint8_t> buffer,bool isLitterEndian)84 short GetShort(int& startIndex, std::vector<uint8_t> buffer, bool isLitterEndian)
85 {
86     if (startIndex >= (int)buffer.size()) {
87         NETMGR_EXT_LOG_E("GetShort fail!");
88         return 0;
89     }
90     std::pair<uint8_t, uint8_t> buff_pair;
91     if (!isLitterEndian) {
92         buff_pair = std::make_pair(buffer[startIndex], buffer[startIndex + 1]);
93     } else {
94         buff_pair = std::make_pair(buffer[startIndex + 1], buffer[startIndex]);
95     }
96     short buffshort = static_cast<short>((buff_pair.first << MOVE_8) | buff_pair.second);
97     startIndex += NetworkSliceCommConfig::LEN_SHORT;
98     return buffshort;
99 }
100 
PutShort(std::vector<uint8_t> & buffer,short value,bool isLitterEndian)101 void PutShort(std::vector<uint8_t>& buffer, short value, bool isLitterEndian)
102 {
103     uint8_t lowByte = static_cast<uint8_t>((value >> MOVE_8) & 0xFF);
104     uint8_t highByte = static_cast<uint8_t>(value & 0xFF);
105     if (isLitterEndian) {
106         buffer.push_back(lowByte);
107         buffer.push_back(highByte);
108     } else {
109         buffer.push_back(highByte);
110         buffer.push_back(lowByte);
111     }
112 }
113 
GetInt(int & startIndex,std::vector<uint8_t> buffer,bool isLitterEndian)114 int GetInt(int& startIndex, std::vector<uint8_t> buffer, bool isLitterEndian)
115 {
116     if (startIndex >= (int)buffer.size()) {
117         NETMGR_EXT_LOG_E("GetShort fail!");
118         return 0;
119     }
120     std::array<uint8_t, NetworkSliceCommConfig::LEN_INT> buff_quad;
121     if (!isLitterEndian) {
122         buff_quad = {
123             buffer[startIndex],
124             buffer[startIndex + 1],
125             buffer[startIndex + 2],
126             buffer[startIndex + 3]
127         };
128     } else {
129         buff_quad = {
130             buffer[startIndex + 3],
131             buffer[startIndex + 2],
132             buffer[startIndex + 1],
133             buffer[startIndex]
134         };
135     }
136     int buffint = static_cast<int>((buff_quad[0] << MOVE_24) | (buff_quad[1] << MOVE_16) |
137                     (buff_quad[2] << MOVE_8) | buff_quad[3]);
138     startIndex += NetworkSliceCommConfig::LEN_INT;
139     return buffint;
140 }
141 
PutInt(std::vector<uint8_t> & buffer,int value,bool isLitterEndian)142 void PutInt(std::vector<uint8_t>& buffer, int value, bool isLitterEndian)
143 {
144     uint8_t lowByte = static_cast<uint8_t>((value >> MOVE_24) & 0xFF);
145     uint8_t middleByte = static_cast<uint8_t>((value >> MOVE_16) & 0xFF);
146     uint8_t highByte = static_cast<uint8_t>((value >> MOVE_8) & 0xFF);
147     uint8_t highestByte = static_cast<uint8_t>(value & 0xFF);
148     if (isLitterEndian) {
149         buffer.push_back(lowByte);
150         buffer.push_back(middleByte);
151         buffer.push_back(highByte);
152         buffer.push_back(highestByte);
153     } else {
154         buffer.push_back(highestByte);
155         buffer.push_back(highByte);
156         buffer.push_back(middleByte);
157         buffer.push_back(lowByte);
158     }
159 }
160 
vectorToUint32(const std::vector<uint8_t> & vec)161 uint32_t vectorToUint32(const std::vector<uint8_t>& vec)
162 {
163     if (vec.size() != LEN_UINT32) {
164         NETMGR_EXT_LOG_E("The vector must have exactly 4 elements.");
165     }
166 
167     uint32_t result = 0;
168     result |= static_cast<uint32_t>(vec[BIT0]) << MOVE_24;
169     result |= static_cast<uint32_t>(vec[BIT1]) << MOVE_16;
170     result |= static_cast<uint32_t>(vec[BIT2]) << MOVE_8;
171     result |= static_cast<uint32_t>(vec[BIT3]);
172     return result;
173 }
174 
uInt32ToVector(uint32_t value)175 std::vector<uint8_t> uInt32ToVector(uint32_t value)
176 {
177     std::vector<uint8_t> vec(LEN_UINT32);
178     vec[BIT0] = static_cast<uint8_t>((value >> MOVE_24) & 0xFF);
179     vec[BIT1] = static_cast<uint8_t>((value >> MOVE_16) & 0xFF);
180     vec[BIT2] = static_cast<uint8_t>((value >> MOVE_8) & 0xFF);
181     vec[BIT3] = static_cast<uint8_t>(value & 0xFF);
182     return vec;
183 }
184 
vectorToIPv6Array(const std::vector<uint8_t> & vec)185 std::array<uint8_t, LEN_IPV6ADDR> vectorToIPv6Array(const std::vector<uint8_t>& vec)
186 {
187     if (vec.size() != LEN_IPV6ADDR) {
188         NETMGR_EXT_LOG_E("The vector must have exactly 16 elements.");
189     }
190     std::array<uint8_t, LEN_IPV6ADDR> ipv6Addr;
191     std::copy(vec.begin(), vec.end(), ipv6Addr.begin());
192     return ipv6Addr;
193 }
194 
ConvertstringTouInt8Vector(const std::string & str)195 std::vector<uint8_t> ConvertstringTouInt8Vector(const std::string& str)
196 {
197     std::vector<uint8_t> vec;
198     vec.resize(str.size());
199     for (char c : str) {
200         vec.push_back(static_cast<uint8_t>(c));
201     }
202     return vec;
203 }
204 
transIpv6AddrToStr(std::array<uint8_t,NetworkSliceCommConfig::LEN_IPV6ADDR> Ipv6Addr)205 std::string transIpv6AddrToStr(std::array<uint8_t, NetworkSliceCommConfig::LEN_IPV6ADDR> Ipv6Addr)
206 {
207     std::string ipv6AddrStr;
208     std::stringstream ss;
209     for (size_t i = 0; i < Ipv6Addr.size(); i += BIT2) {
210         ss << std::hex << std::setw(LEN_4) << std::setfill('0') <<
211             static_cast<uint32_t>((Ipv6Addr[i] << MOVE_8) | Ipv6Addr[i + 1]);
212         if (i < LEN_14) {
213             ss << ":";
214         }
215     }
216     ipv6AddrStr = ss.str();
217     return ipv6AddrStr;
218 }
219 
ConvertIntListToString(const std::vector<int> & intList)220 std::string ConvertIntListToString(const std::vector<int>& intList)
221 {
222     std::string str;
223     bool first = true;
224     for (int num : intList) {
225         if (!first) {
226             str += ",";
227         } else {
228             first = false;
229         }
230         str += std::to_string(num);
231     }
232     return str;
233 }
234 
ConvertUint8vecToString(const std::vector<uint8_t> & vec)235 std::string ConvertUint8vecToString(const std::vector<uint8_t>& vec)
236 {
237     std::string str;
238     bool first = true;
239     for (uint8_t num : vec) {
240         if (!first) {
241             str += ",";
242         } else {
243             first = false;
244         }
245         str += std::to_string(num);
246     }
247     return str;
248 }
249 
ConvertIntSetToString(const std::set<int> & intList)250 std::string ConvertIntSetToString(const std::set<int>& intList)
251 {
252     std::string str;
253     for (int num : intList) {
254         str += std::to_string(num);
255     }
256     return str;
257 }
258 
GetSha256Str(const std::string & str)259 std::string GetSha256Str(const std::string &str)
260 {
261     unsigned char hash[SHA256_DIGEST_LENGTH];
262     SHA256_CTX ctx;
263     SHA256_Init(&ctx);
264     SHA256_Update(&ctx, str.c_str(), str.length());
265     SHA256_Final(hash, &ctx);
266 
267     std::stringstream ss;
268     for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
269         ss << std::hex << std::setw(HEX_WIDTH) << std::setfill('0') << static_cast<uint32_t>(hash[i]);
270     }
271     return ss.str();
272 }
273 
Str16ToStr8(std::u16string str)274 std::string Str16ToStr8(std::u16string str)
275 {
276     if (str == DEFAULT_USTRING) {
277         return DEFAULT_STRING;
278     }
279     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(DEFAULT_STRING);
280     std::string result = convert.to_bytes(str);
281     return result == DEFAULT_STRING ? "" : result;
282 }
283 
284 } // namespace NetManagerStandard
285 } // namespace OHOS
286