1 /*
2 * Copyright (C) 2021-2025 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 "string_util.h"
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "securec.h"
22 #include "clib_error.h"
23 #include "hc_types.h"
24 #include "hc_log.h"
25
26 #define OUT_OF_HEX 16
27 #define NUMBER_9_IN_DECIMAL 9
28 #define ASCII_CASE_DIFFERENCE_VALUE 32
29 #define MIN_ANONYMOUS_LEN 12
30 #define ANONYMOUS_ASTERISK_LEN 2
31 #define ANONYMOUS_DIVIDER 2
32
HexToChar(uint8_t hex)33 static char HexToChar(uint8_t hex)
34 {
35 return (hex > NUMBER_9_IN_DECIMAL) ? (hex + 0x37) : (hex + 0x30); /* Convert to the corresponding character */
36 }
37
ByteToHexString(const uint8_t * byte,uint32_t byteLen,char * hexStr,uint32_t hexLen)38 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen)
39 {
40 if (byte == NULL || hexStr == NULL) {
41 return CLIB_ERR_NULL_PTR;
42 }
43 /* The terminator('\0') needs 1 bit */
44 if (hexLen < byteLen * BYTE_TO_HEX_OPER_LENGTH + 1) {
45 return CLIB_ERR_INVALID_LEN;
46 }
47
48 for (uint32_t i = 0; i < byteLen; i++) {
49 hexStr[i * BYTE_TO_HEX_OPER_LENGTH] = HexToChar((byte[i] & 0xF0) >> 4); /* 4: shift right for filling */
50 hexStr[i * BYTE_TO_HEX_OPER_LENGTH + 1] = HexToChar(byte[i] & 0x0F); /* get low four bits */
51 }
52 hexStr[byteLen * BYTE_TO_HEX_OPER_LENGTH] = '\0';
53
54 return CLIB_SUCCESS;
55 }
56
CharToHex(char c)57 static uint8_t CharToHex(char c)
58 {
59 if ((c >= 'A') && (c <= 'F')) {
60 return (c - 'A' + DEC);
61 } else if ((c >= 'a') && (c <= 'f')) {
62 return (c - 'a' + DEC);
63 } else if ((c >= '0') && (c <= '9')) {
64 return (c - '0');
65 } else {
66 return OUT_OF_HEX;
67 }
68 }
69
HexStringToByte(const char * hexStr,uint8_t * byte,uint32_t byteLen)70 int32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen)
71 {
72 if (byte == NULL || hexStr == NULL) {
73 return CLIB_ERR_NULL_PTR;
74 }
75 uint32_t realHexLen = HcStrlen(hexStr);
76 /* even number or not */
77 if (realHexLen % BYTE_TO_HEX_OPER_LENGTH != 0 || byteLen < realHexLen / BYTE_TO_HEX_OPER_LENGTH) {
78 return CLIB_ERR_INVALID_LEN;
79 }
80
81 for (uint32_t i = 0; i < realHexLen / BYTE_TO_HEX_OPER_LENGTH; i++) {
82 uint8_t high = CharToHex(hexStr[i * BYTE_TO_HEX_OPER_LENGTH]);
83 uint8_t low = CharToHex(hexStr[i * BYTE_TO_HEX_OPER_LENGTH + 1]);
84 if (high == OUT_OF_HEX || low == OUT_OF_HEX) {
85 return CLIB_ERR_INVALID_PARAM;
86 }
87 byte[i] = high << 4; /* 4: Set the high nibble */
88 byte[i] |= low; /* Set the low nibble */
89 }
90 return CLIB_SUCCESS;
91 }
92
StringToInt64(const char * cp)93 int64_t StringToInt64(const char *cp)
94 {
95 if (cp == NULL) {
96 return 0;
97 }
98 return strtoll(cp, NULL, DEC);
99 }
100
ToUpperCase(const char * oriStr,char ** desStr)101 int32_t ToUpperCase(const char *oriStr, char **desStr)
102 {
103 if (oriStr == NULL || desStr == NULL) {
104 return CLIB_ERR_NULL_PTR;
105 }
106 uint32_t len = HcStrlen(oriStr);
107 *desStr = HcMalloc(len + 1, 0);
108 if (*desStr == NULL) {
109 return CLIB_ERR_BAD_ALLOC;
110 }
111 for (uint32_t i = 0; i < len; i++) {
112 if ((oriStr[i] >= 'a') && (oriStr[i] <= 'f')) {
113 (*desStr)[i] = oriStr[i] - ASCII_CASE_DIFFERENCE_VALUE;
114 } else {
115 (*desStr)[i] = oriStr[i];
116 }
117 }
118 return CLIB_SUCCESS;
119 }
120
DeepCopyString(const char * str,char ** newStr)121 int32_t DeepCopyString(const char *str, char **newStr)
122 {
123 if (str == NULL || newStr == NULL) {
124 return CLIB_ERR_NULL_PTR;
125 }
126 uint32_t len = HcStrlen(str);
127 if (len == 0) {
128 return CLIB_ERR_INVALID_LEN;
129 }
130 char *val = (char *)HcMalloc(len + 1, 0);
131 if (val == NULL) {
132 return CLIB_ERR_BAD_ALLOC;
133 }
134 (void)memcpy_s(val, len, str, len);
135 *newStr = val;
136 return CLIB_SUCCESS;
137 }
138
PrintBuffer(const uint8_t * msgBuff,uint32_t msgLen,const char * msgTag)139 void PrintBuffer(const uint8_t *msgBuff, uint32_t msgLen, const char *msgTag)
140 {
141 uint32_t hexLen = msgLen * BYTE_TO_HEX_OPER_LENGTH + 1;
142 char *hexStr = (char *)HcMalloc(hexLen, 0);
143 if (hexStr == NULL) {
144 return;
145 }
146 (void)ByteToHexString(msgBuff, msgLen, hexStr, hexLen);
147 LOGD("%" LOG_PUB "s value is: %" LOG_PUB "s", msgTag, hexStr);
148 HcFree(hexStr);
149 }
150
GetAnonymousString(const char * originStr,char * anonymousStr,uint32_t anonymousLen)151 int32_t GetAnonymousString(const char *originStr, char *anonymousStr, uint32_t anonymousLen)
152 {
153 if (originStr == NULL || anonymousStr == NULL) {
154 return CLIB_ERR_NULL_PTR;
155 }
156 if (anonymousLen < MIN_ANONYMOUS_LEN || (anonymousLen - ANONYMOUS_ASTERISK_LEN) % ANONYMOUS_DIVIDER != 0) {
157 return CLIB_ERR_INVALID_LEN;
158 }
159 uint32_t originStrLen = HcStrlen(originStr);
160 if (originStrLen < anonymousLen - ANONYMOUS_ASTERISK_LEN) {
161 return CLIB_ERR_INVALID_LEN;
162 }
163 uint32_t printLen = (anonymousLen - ANONYMOUS_ASTERISK_LEN) / ANONYMOUS_DIVIDER;
164 if (memcpy_s(anonymousStr, printLen, originStr, printLen) != EOK) {
165 return CLIB_ERR_BAD_ALLOC;
166 }
167 if (memcpy_s(anonymousStr + printLen, ANONYMOUS_ASTERISK_LEN, "**", ANONYMOUS_ASTERISK_LEN) != EOK) {
168 return CLIB_ERR_BAD_ALLOC;
169 }
170 if (memcpy_s(anonymousStr + printLen + ANONYMOUS_ASTERISK_LEN, printLen,
171 originStr + originStrLen - printLen, printLen) != EOK) {
172 return CLIB_ERR_BAD_ALLOC;
173 }
174 return CLIB_SUCCESS;
175 }
176
IsStrEqual(const char * str1,const char * str2)177 bool IsStrEqual(const char *str1, const char *str2)
178 {
179 if (str1 == NULL || str2 == NULL) {
180 return false;
181 }
182
183 return strcmp(str1, str2) == 0;
184 }