1/*
2 * Copyright (c) 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 "utils_hexstring.h"
17
18#include <stdio.h>
19
20#include "utils_log.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#define ERR (-1)
27#define OK 0
28
29#define BYTE_TO_HEX_OPER_LENGTH 2
30#define MAX_MALLOC_LEN (1 * 1024 * 1024)
31
32static char HexToChar(uint8_t hex)
33{
34    return (hex > 9) ? (hex + 0x37) : (hex + 0x30); /* check if bigger than 9, then add 0x37 or 0x30 */
35}
36
37void DslmByteToHexString(const uint8_t *hex, uint32_t hexLen, uint8_t *str, uint32_t strLen)
38{
39    if ((hex == NULL) || (str == NULL) || (strLen < hexLen * BYTE_TO_HEX_OPER_LENGTH)) {
40        return;
41    }
42
43    for (uint32_t i = 0; i < hexLen; i++) {
44        str[i * BYTE_TO_HEX_OPER_LENGTH] = HexToChar((hex[i] & 0xF0) >> 4); /* shift 4 right for filling */
45        str[i * BYTE_TO_HEX_OPER_LENGTH + 1] = HexToChar(hex[i] & 0x0F);    /* get low four bits */
46    }
47}
48
49int32_t DslmHexStringToByte(const char *str, uint32_t strLen, uint8_t *hex, uint32_t hexLen)
50{
51    if ((str == NULL) || (hex == NULL) || (strLen % BYTE_TO_HEX_OPER_LENGTH)) { /* even number or not */
52        return ERR;
53    }
54    uint32_t outLen = strLen / BYTE_TO_HEX_OPER_LENGTH;
55
56    if (hexLen < outLen) { /* test the length */
57        SECURITY_LOG_DEBUG("length error");
58        return ERR;
59    }
60
61    uint8_t nibble[BYTE_TO_HEX_OPER_LENGTH]; /* create array */
62
63    for (uint32_t i = 0; i < outLen; i++) {
64        nibble[0] = str[i * BYTE_TO_HEX_OPER_LENGTH];           /* hex conversion */
65        nibble[1] = str[i * BYTE_TO_HEX_OPER_LENGTH + 1];       /* hex conversion */
66        for (int32_t j = 0; j < BYTE_TO_HEX_OPER_LENGTH; j++) { /* iterate through array */
67            if ((nibble[j] <= 'F') && (nibble[j] >= 'A')) {
68                nibble[j] = nibble[j] - 'A' + 10; /* decimal conversion, add 10 */
69            } else if ((nibble[j] <= 'f') && (nibble[j] >= 'a')) {
70                nibble[j] = nibble[j] - 'a' + 10; /* decimal conversion, add 10 */
71            } else if ((nibble[j] >= '0') && (nibble[j] <= '9')) {
72                nibble[j] = nibble[j] - '0';
73            } else {
74                return ERR;
75            }
76        }
77        hex[i] = nibble[0] << 4; /* Set the high nibble, shift 4 */
78        hex[i] |= nibble[1];     /* Set the low nibble */
79    }
80    return OK;
81}
82
83#ifdef __cplusplus
84}
85#endif