1 /*
2 * Copyright (c) 2020-2022 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 "commonutil.h"
17 #include "securec.h"
18 #include "base.h"
19 #include "log.h"
20 #include "mem_stat.h"
21 #include "hichain.h"
22
hex_to_char(uint8_t hex)23 char hex_to_char(uint8_t hex)
24 {
25 /* Convert to the corresponding character, hex + 0x37 -> A-F, hex + 0x30 -> 0-9 */
26 return (hex > 9) ? hex + 0x37 : hex + 0x30;
27 }
28
byte_to_hex_string(const uint8_t * hex,int32_t hex_len,uint8_t * buf,int32_t buf_len)29 void byte_to_hex_string(const uint8_t *hex, int32_t hex_len, uint8_t *buf, int32_t buf_len)
30 {
31 if (buf_len < hex_len * BYTE_TO_HEX_OPER_LENGTH) {
32 return;
33 }
34 for (int32_t i = 0; i < hex_len; i++) {
35 buf[i * BYTE_TO_HEX_OPER_LENGTH] = hex_to_char((hex[i] & 0xF0) >> 4); /* shift right for filling */
36 buf[i * BYTE_TO_HEX_OPER_LENGTH + 1] = hex_to_char(hex[i] & 0x0F); /* get low four bits */
37 }
38 }
39
hex_string_to_byte(const char * str,int32_t len,uint8_t * hex)40 int32_t hex_string_to_byte(const char *str, int32_t len, uint8_t *hex)
41 {
42 if (len % BYTE_TO_HEX_OPER_LENGTH) { /* even number or not */
43 LOGE("Length is not even number");
44 return HC_INPUT_ERROR;
45 }
46
47 int32_t hex_len = len / BYTE_TO_HEX_OPER_LENGTH; /* Halve the length */
48 uint8_t nibble[BYTE_TO_HEX_OPER_LENGTH]; /* create array */
49
50 for (int32_t i = 0; i < hex_len; i++) {
51 nibble[0] = str[i * BYTE_TO_HEX_OPER_LENGTH]; /* hex conversion */
52 nibble[1] = str[i * BYTE_TO_HEX_OPER_LENGTH + 1]; /* hex conversion */
53 for (int32_t j = 0; j < BYTE_TO_HEX_OPER_LENGTH; j++) { /* iterate through array */
54 if ((nibble[j] <= 'F') && (nibble[j] >= 'A')) {
55 nibble[j] = nibble[j] - 'A' + 10; /* decimal conversion */
56 } else if ((nibble[j] <= 'f') && (nibble[j] >= 'a')) {
57 nibble[j] = nibble[j] - 'a' + 10; /* decimal conversion */
58 } else if ((nibble[j] >= '0') && (nibble[j] <= '9')) {
59 nibble[j] = nibble[j] - '0';
60 } else {
61 LOGE("Message is invalid");
62 return HC_INPUT_ERROR;
63 }
64 }
65 hex[i] = nibble[0] << 4; /* Set the high nibble */
66 hex[i] |= nibble[1]; /* Set the low nibble */
67 }
68 return HC_OK;
69 }
70
print_bytes(uint8_t * buf,int32_t buf_len)71 void print_bytes(uint8_t *buf, int32_t buf_len)
72 {
73 int32_t tmp_data_hex_len = buf_len * BYTE_TO_HEX_OPER_LENGTH + sizeof("");
74 uint8_t *tmp_data_hex = (uint8_t *)MALLOC(tmp_data_hex_len);
75 if (tmp_data_hex == NULL) {
76 LOGE("Malloc failed");
77 return;
78 }
79 (void)memset_s(tmp_data_hex, tmp_data_hex_len, 0, tmp_data_hex_len);
80 byte_to_hex_string(buf, buf_len, tmp_data_hex, buf_len * BYTE_TO_HEX_OPER_LENGTH);
81 DBG_OUT("PrintBytes:%s", (char *)tmp_data_hex);
82 FREE(tmp_data_hex);
83 }
84
byte_convert(json_pobject obj,const char * field,uint8_t * hex,uint32_t * length,uint32_t max_len)85 int32_t byte_convert(json_pobject obj, const char *field, uint8_t *hex, uint32_t *length, uint32_t max_len)
86 {
87 const char *str_json = get_json_string(obj, field);
88 if (str_json == NULL) {
89 return HC_INPUT_ERROR;
90 }
91
92 uint32_t len = strlen(str_json);
93 if ((len / BYTE_TO_HEX_OPER_LENGTH) > max_len) {
94 return HC_INPUT_ERROR;
95 }
96 if (hex_string_to_byte(str_json, len, hex) != HC_OK) {
97 return HC_INPUT_ERROR;
98 }
99 *length = len / BYTE_TO_HEX_OPER_LENGTH;
100 return HC_OK;
101 }
102
string_convert(json_pobject obj,const char * field,uint8_t * str,uint32_t * length,uint32_t max_len)103 int32_t string_convert(json_pobject obj, const char *field, uint8_t *str, uint32_t *length, uint32_t max_len)
104 {
105 const char *str_json = get_json_string(obj, field);
106 if (str_json == NULL) {
107 return HC_INPUT_ERROR;
108 }
109
110 uint32_t len = strlen(str_json);
111 if (len > max_len) {
112 return HC_INPUT_ERROR;
113 }
114 if (memcpy_s(str, max_len, str_json, len) != EOK) {
115 return memory_copy_error(__func__, __LINE__);
116 }
117 *length = len;
118 return HC_OK;
119 }
120
memory_copy_error(const char * fun,unsigned int line)121 int32_t memory_copy_error(const char *fun, unsigned int line)
122 {
123 (void)fun;
124 (void)line;
125 LOGE("memory copy error in fun:%s line:%u", fun, line);
126 return HC_MEMCPY_ERROR;
127 }
128
129