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
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
25 #define OUT_OF_HEX 16
26 #define NUMBER_9_IN_DECIMAL 9
27 #define ASCII_CASE_DIFFERENCE_VALUE 32
28
HexToChar(uint8_t hex)29 static char HexToChar(uint8_t hex)
30 {
31 return (hex > NUMBER_9_IN_DECIMAL) ? (hex + 0x37) : (hex + 0x30); /* Convert to the corresponding character */
32 }
33
ByteToHexString(const uint8_t * byte,uint32_t byteLen,char * hexStr,uint32_t hexLen)34 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen)
35 {
36 if (byte == NULL || hexStr == NULL) {
37 return CLIB_ERR_NULL_PTR;
38 }
39 /* The terminator('\0') needs 1 bit */
40 if (hexLen < byteLen * BYTE_TO_HEX_OPER_LENGTH + 1) {
41 return CLIB_ERR_INVALID_LEN;
42 }
43
44 for (uint32_t i = 0; i < byteLen; i++) {
45 hexStr[i * BYTE_TO_HEX_OPER_LENGTH] = HexToChar((byte[i] & 0xF0) >> 4); /* 4: shift right for filling */
46 hexStr[i * BYTE_TO_HEX_OPER_LENGTH + 1] = HexToChar(byte[i] & 0x0F); /* get low four bits */
47 }
48 hexStr[byteLen * BYTE_TO_HEX_OPER_LENGTH] = '\0';
49
50 return CLIB_SUCCESS;
51 }
52
CharToHex(char c)53 static uint8_t CharToHex(char c)
54 {
55 if ((c >= 'A') && (c <= 'F')) {
56 return (c - 'A' + DEC);
57 } else if ((c >= 'a') && (c <= 'f')) {
58 return (c - 'a' + DEC);
59 } else if ((c >= '0') && (c <= '9')) {
60 return (c - '0');
61 } else {
62 return OUT_OF_HEX;
63 }
64 }
65
HexStringToByte(const char * hexStr,uint8_t * byte,uint32_t byteLen)66 int32_t HexStringToByte(const char *hexStr, uint8_t *byte, uint32_t byteLen)
67 {
68 if (byte == NULL || hexStr == NULL) {
69 return CLIB_ERR_NULL_PTR;
70 }
71 uint32_t realHexLen = strlen(hexStr);
72 /* even number or not */
73 if (realHexLen % BYTE_TO_HEX_OPER_LENGTH != 0 || byteLen < realHexLen / BYTE_TO_HEX_OPER_LENGTH) {
74 return CLIB_ERR_INVALID_LEN;
75 }
76
77 for (uint32_t i = 0; i < realHexLen / BYTE_TO_HEX_OPER_LENGTH; i++) {
78 uint8_t high = CharToHex(hexStr[i * BYTE_TO_HEX_OPER_LENGTH]);
79 uint8_t low = CharToHex(hexStr[i * BYTE_TO_HEX_OPER_LENGTH + 1]);
80 if (high == OUT_OF_HEX || low == OUT_OF_HEX) {
81 return CLIB_ERR_INVALID_PARAM;
82 }
83 byte[i] = high << 4; /* 4: Set the high nibble */
84 byte[i] |= low; /* Set the low nibble */
85 }
86 return CLIB_SUCCESS;
87 }
88
StringToInt64(const char * cp)89 int64_t StringToInt64(const char *cp)
90 {
91 if (cp == NULL) {
92 return 0;
93 }
94 return strtoll(cp, NULL, DEC);
95 }
96
ToUpperCase(const char * oriStr,char ** desStr)97 int32_t ToUpperCase(const char *oriStr, char **desStr)
98 {
99 if (oriStr == NULL || desStr == NULL) {
100 return CLIB_ERR_NULL_PTR;
101 }
102 uint32_t len = strlen(oriStr);
103 *desStr = HcMalloc(len + 1, 0);
104 if (*desStr == NULL) {
105 return CLIB_ERR_BAD_ALLOC;
106 }
107 for (uint32_t i = 0; i < len; i++) {
108 if ((oriStr[i] >= 'a') && (oriStr[i] <= 'f')) {
109 (*desStr)[i] = oriStr[i] - ASCII_CASE_DIFFERENCE_VALUE;
110 } else {
111 (*desStr)[i] = oriStr[i];
112 }
113 }
114 return CLIB_SUCCESS;
115 }
116
DeepCopyString(const char * str,char ** newStr)117 int32_t DeepCopyString(const char *str, char **newStr)
118 {
119 if (str == NULL || newStr == NULL) {
120 return CLIB_ERR_NULL_PTR;
121 }
122 uint32_t len = strlen(str);
123 if (len == 0) {
124 return CLIB_ERR_INVALID_LEN;
125 }
126 char *val = (char *)HcMalloc(len + 1, 0);
127 if (val == NULL) {
128 return CLIB_ERR_BAD_ALLOC;
129 }
130 (void)memcpy_s(val, len, str, len);
131 *newStr = val;
132 return CLIB_SUCCESS;
133 }
134