1 /*
2 * Copyright (C) 2021 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 #include "nfc_sdk_common.h"
16 #include <algorithm>
17 #include <sstream>
18 #include <securec.h>
19 #include "loghelper.h"
20
21 namespace OHOS {
22 namespace NFC {
23 namespace KITS {
24
IsLittleEndian()25 bool NfcSdkCommon::IsLittleEndian()
26 {
27 const char LAST_DATA_BYTE = 0x78;
28 union CheckData {
29 int x;
30 char y;
31 };
32
33 union CheckData data;
34 data.x = 0x12345678;
35 if (data.y == LAST_DATA_BYTE) {
36 return true;
37 }
38 return false;
39 }
40
BytesVecToHexString(const unsigned char * src,uint32_t length)41 std::string NfcSdkCommon::BytesVecToHexString(const unsigned char* src, uint32_t length)
42 {
43 std::string result = "";
44 if (length <= 0) {
45 return result;
46 }
47 const std::string hexKeys = "0123456789ABCDEF";
48 for (uint32_t i = 0; i < length; i++) {
49 result.push_back(hexKeys[(src[i] & 0xF0) >> HALF_BYTE_BITS]);
50 result.push_back(hexKeys[src[i] & 0x0F]);
51 }
52 return result;
53 }
54
UnsignedCharToHexString(const unsigned char src)55 std::string NfcSdkCommon::UnsignedCharToHexString(const unsigned char src)
56 {
57 std::string result = "";
58 const std::string hexKeys = "0123456789ABCDEF";
59 result.push_back(hexKeys[(src & 0xF0) >> HALF_BYTE_BITS]);
60 result.push_back(hexKeys[src & 0x0F]);
61 return result;
62 }
63
HexStringToBytes(std::string & src,std::vector<unsigned char> & bytes)64 void NfcSdkCommon::HexStringToBytes(std::string &src, std::vector<unsigned char> &bytes)
65 {
66 if (src.empty()) {
67 return;
68 }
69
70 uint32_t bytesLen = src.length() / HEX_BYTE_LEN;
71 std::string strByte;
72 unsigned int srcIntValue;
73 for (uint32_t i = 0; i < bytesLen; i++) {
74 strByte = src.substr(i * HEX_BYTE_LEN, HEX_BYTE_LEN);
75 if (sscanf_s(strByte.c_str(), "%x", &srcIntValue) <= 0) {
76 ErrorLog("HexStringToBytes, sscanf_s failed.");
77 bytes.clear();
78 return;
79 }
80 bytes.push_back(static_cast<unsigned char>(srcIntValue & 0xFF));
81 }
82 }
83
GetHexStrBytesLen(const std::string src)84 uint32_t NfcSdkCommon::GetHexStrBytesLen(const std::string src)
85 {
86 // 2 charactors consist of one byte.
87 if (src.empty()) {
88 return 0;
89 }
90 uint32_t length = src.length();
91 if (length % HEX_BYTE_LEN == 0) {
92 return (length / HEX_BYTE_LEN);
93 } else {
94 return ((length / HEX_BYTE_LEN) + 1);
95 }
96 }
97
GetByteFromHexStr(const std::string src,uint32_t index)98 unsigned char NfcSdkCommon::GetByteFromHexStr(const std::string src, uint32_t index)
99 {
100 // 2 charactors consist of one byte.
101 if (src.empty() || index >= (src.length() - 1)) {
102 return 0;
103 }
104 std::string strByte = src.substr(index * HEX_BYTE_LEN, HEX_BYTE_LEN);
105 unsigned int srcIntValue;
106 if (sscanf_s(strByte.c_str(), "%x", &srcIntValue) <= 0) {
107 ErrorLog("GetByteFromHexStr, sscanf_s failed.");
108 return 0;
109 }
110 return static_cast<unsigned char>(srcIntValue & 0xFF);
111 }
112
StringToInt(std::string src,bool bLittleEndian)113 uint32_t NfcSdkCommon::StringToInt(std::string src, bool bLittleEndian)
114 {
115 uint32_t value = 0;
116 if (bLittleEndian) {
117 for (size_t i = SHIFT_TIME; i > 0; i--) {
118 value += (uint32_t)(src.at(SHIFT_TIME - i)) << (i * SHIFT_SIZE - SHIFT_SIZE);
119 }
120 } else {
121 for (size_t i = 0; i < SHIFT_TIME; i++) {
122 value += (uint32_t)(src.at(i)) << (i * SHIFT_SIZE);
123 }
124 }
125 return value;
126 }
127
IntToHexString(uint32_t num)128 std::string NfcSdkCommon::IntToHexString(uint32_t num)
129 {
130 std::stringstream ss;
131 ss << std::hex << num;
132 std::string result = ss.str();
133 transform(result.begin(), result.end(), result.begin(), ::toupper);
134 if (result.length() % HEX_BYTE_LEN > 0) { // expend "0" if string length is odd
135 result = "0" + result;
136 }
137 return result;
138 }
139
StringToAsciiBytes(const std::string & src,std::vector<unsigned char> & bytes)140 void NfcSdkCommon::StringToAsciiBytes(const std::string &src, std::vector<unsigned char> &bytes)
141 {
142 if (src.empty()) {
143 return;
144 }
145 uint32_t bytesLen = src.length();
146 for (uint32_t i = 0; i < bytesLen; i++) {
147 unsigned int srcAsciiIntVal = static_cast<unsigned int>(src[i]);
148 bytes.push_back(static_cast<unsigned char>(srcAsciiIntVal & 0xFF));
149 }
150 }
151
StringToHexString(const std::string & src)152 std::string NfcSdkCommon::StringToHexString(const std::string &src)
153 {
154 std::vector<unsigned char> bytes;
155 StringToAsciiBytes(src, bytes);
156 uint32_t len = src.length();
157 std::string result = BytesVecToHexString(&bytes[0], len);
158 return result;
159 }
160 } // namespace KITS
161 } // namespace NFC
162 } // namespace OHOS
163