1 /*
2 * Copyright (C) 2024 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 #ifdef FEATURE_GNSS_SUPPORT
17 #include "string_utils.h"
18
19 #include <codecvt>
20 #include <sstream>
21 #include <string_ex.h>
22
23 #include "cstddef"
24 #include "cstdint"
25 #include "cstdlib"
26 #include "ostream"
27 #include "string"
28 #include "vector"
29
30 namespace OHOS {
31 namespace Location {
32 static constexpr uint8_t HEX_OFFSET = 4;
33 static constexpr uint8_t STEP_2BIT = 2;
34 static constexpr uint8_t GSM_BIT = 7;
35 static constexpr char HEX_TABLE[] = "0123456789ABCDEF";
36
StringUtils()37 StringUtils::StringUtils() {}
38
~StringUtils()39 StringUtils::~StringUtils() {}
40
HexCharToInt(char c)41 uint16_t StringUtils::HexCharToInt(char c)
42 {
43 const uint8_t decimal = 10;
44 if (c >= '0' && c <= '9') {
45 return (c - '0');
46 }
47 if (c >= 'A' && c <= 'F') {
48 return (c - 'A' + decimal);
49 }
50 if (c >= 'a' && c <= 'f') {
51 return (c - 'a' + decimal);
52 }
53 return 0;
54 }
55
StringToHex(const std::string & data)56 std::string StringUtils::StringToHex(const std::string &data)
57 {
58 std::stringstream ss;
59 for (std::string::size_type i = 0; i < data.size(); ++i) {
60 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
61 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
62 }
63 return ss.str();
64 }
65
StringToHex(const char * data,int byteLength)66 std::string StringUtils::StringToHex(const char *data, int byteLength)
67 {
68 std::stringstream ss;
69 for (int i = 0; i < byteLength; ++i) {
70 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
71 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
72 }
73 return ss.str();
74 }
75
StringToHex(const std::vector<uint8_t> & data)76 std::string StringUtils::StringToHex(const std::vector<uint8_t> &data)
77 {
78 std::stringstream ss;
79 for (std::size_t i = 0; i < data.size(); ++i) {
80 unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
81 ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
82 }
83 return ss.str();
84 }
85
HexToString(const std::string & str)86 std::string StringUtils::HexToString(const std::string &str)
87 {
88 std::string result;
89 uint8_t hexDecimal = 16;
90 uint8_t hexStep = 2;
91 if (str.length() <= 0) {
92 return result;
93 }
94 for (size_t i = 0; i < str.length() - 1; i += STEP_2BIT) {
95 std::string byte = str.substr(i, hexStep);
96 char chr = 0;
97 long strTemp = strtol(byte.c_str(), nullptr, hexDecimal);
98 if (strTemp > 0) {
99 chr = static_cast<char>(strTemp);
100 }
101 result.push_back(chr);
102 }
103 return result;
104 }
105
HexToByteVector(const std::string & str)106 std::vector<uint8_t> StringUtils::HexToByteVector(const std::string &str)
107 {
108 std::vector<uint8_t> ret;
109 int sz = static_cast<int>(str.length());
110 if (sz <= 0) {
111 return ret;
112 }
113 for (int i = 0; i < (sz - 1); i += STEP_2BIT) {
114 auto temp = static_cast<uint8_t>((HexCharToInt(str.at(i)) << HEX_OFFSET) | HexCharToInt(str.at(i + 1)));
115 ret.push_back(temp);
116 }
117 return ret;
118 }
119
ToUtf8(const std::u16string & str16)120 std::string StringUtils::ToUtf8(const std::u16string &str16)
121 {
122 if (str16.empty()) {
123 std::string ret;
124 return ret;
125 }
126 return Str16ToStr8(str16);
127 }
128
ToUtf16(const std::string & str)129 std::u16string StringUtils::ToUtf16(const std::string &str)
130 {
131 if (str.empty()) {
132 std::u16string ret;
133 return ret;
134 }
135 return Str8ToStr16(str);
136 }
137
Utf8ToWstring(const std::string & utf8Str)138 std::wstring StringUtils::Utf8ToWstring(const std::string& utf8Str)
139 {
140 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
141 return converter.from_bytes(utf8Str);
142 }
143
Ucs2ToWstring(const std::string & ucs2Str)144 std::wstring StringUtils::Ucs2ToWstring(const std::string& ucs2Str)
145 {
146 std::wstring_convert<std::codecvt_utf16<wchar_t>> converter;
147 return converter.from_bytes(ucs2Str);
148 }
149
WstringToString(const std::wstring & wStr)150 std::string StringUtils::WstringToString(const std::wstring& wStr)
151 {
152 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
153 return converter.to_bytes(wStr);
154 }
155
Gsm7Decode(const std::string & gsm7Str)156 std::string StringUtils::Gsm7Decode(const std::string &gsm7Str)
157 {
158 const std::string gsm7Chars = "@£$¥èéùìòÇ\nØø\n\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?"
159 "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà";
160 const std::string extChar = "^{}\\[~]|€";
161
162 std::string decodedString;
163 std::bitset<GSM_BIT> bits(0);
164
165 for (char c : gsm7Str) {
166 unsigned char index = static_cast<unsigned char>(c);
167 if (index < 0x20) {
168 bits = (index == 0x1B) ? 0x1B : 0x5E;
169 } else if (index < 0x60) {
170 bits = gsm7Chars[index - 0x20];
171 } else {
172 bits = extChar[index - 0x60];
173 }
174
175 for (int i = 6; i >= 0; --i) {
176 if (bits[i]) {
177 bits <<= i + 1;
178 decodedString += static_cast<char>(bits.to_ulong());
179 bits = bits >> (i + 1);
180 break;
181 }
182 }
183 }
184
185 return decodedString;
186 }
187
188 } // namespace Location
189 } // namespace OHOS
190 #endif // FEATURE_GNSS_SUPPORT