1 /*
2  * Copyright (c) 2023 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 "hex_string.h"
17 #include <cstdio>
18 #include <cstring>
19 #include "dlp_permission.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace DlpPermission {
24 static const int32_t MAX_HEX = 16;
25 
HexToChar(uint8_t hex)26 static char HexToChar(uint8_t hex)
27 {
28     return (hex > 9) ? (hex + 0x37) : (hex + 0x30);  // numbers greater than 9 are represented by letters in hex.
29 }
30 
ByteToHexString(const uint8_t * byte,uint32_t byteLen,char * hexStr,uint32_t hexLen)31 int32_t ByteToHexString(const uint8_t *byte, uint32_t byteLen, char *hexStr, uint32_t hexLen)
32 {
33     if (byte == nullptr || hexStr == nullptr) {
34         return DLP_SERVICE_ERROR_VALUE_INVALID;
35     }
36     if (byteLen > (UINT32_MAX / BYTE_TO_HEX_OPER_LENGTH)) {
37         return DLP_SERVICE_ERROR_VALUE_INVALID;
38     }
39     /* The terminator('\0') needs 1 bit */
40     if (hexLen < byteLen * BYTE_TO_HEX_OPER_LENGTH + 1) {
41         return DLP_SERVICE_ERROR_VALUE_INVALID;
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 DLP_OK;
51 }
52 
CharToHex(char c)53 static uint8_t CharToHex(char c)
54 {
55     if ((c >= 'A') && (c <= 'F')) {
56         return (c - 'A' + 10);  // hex trans to dec with base 10
57     }
58     if ((c >= 'a') && (c <= 'f')) {
59         return (c - 'a' + 10);  // hex trans to dec with base 10
60     }
61     if ((c >= '0') && (c <= '9')) {
62         return (c - '0');
63     }
64     return MAX_HEX;  // max hex must < 16
65 }
66 
HexStringToByte(const char * hexStr,uint32_t hexStrLen,uint8_t * byte,uint32_t byteLen)67 int32_t HexStringToByte(const char *hexStr, uint32_t hexStrLen, uint8_t *byte, uint32_t byteLen)
68 {
69     if (byte == nullptr || hexStr == nullptr || hexStrLen == 0) {
70         return DLP_SERVICE_ERROR_VALUE_INVALID;
71     }
72     /* even number or not */
73     if (hexStrLen % BYTE_TO_HEX_OPER_LENGTH != 0 || byteLen < hexStrLen / BYTE_TO_HEX_OPER_LENGTH) {
74         return DLP_SERVICE_ERROR_VALUE_INVALID;
75     }
76 
77     for (uint32_t i = 0; i < hexStrLen / 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 == MAX_HEX || low == MAX_HEX) {  // max hex must < 16
81             return DLP_SERVICE_ERROR_VALUE_INVALID;
82         }
83         byte[i] = high << 4;  // 4: Set the high nibble
84         byte[i] |= low;       // Set the low nibble
85     }
86     return DLP_OK;
87 }
88 }  // namespace DlpPermission
89 }  // namespace Security
90 }  // namespace OHOS