• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "hvb_util.h"
16 #include "hvb_types.h"
17 
hvb_bin2hex(const uint8_t * value,size_t len)18 char *hvb_bin2hex(const uint8_t *value, size_t len)
19 {
20     const char digits[MAX_STRING_LEN] = "0123456789abcdef";
21     char *hex;
22     size_t n;
23 
24     hex = hvb_malloc(len * 2 + 1);
25     if (hex == NULL)
26         return NULL;
27 
28     for (n = 0; n < len; n++) {
29         hex[n * 2] = digits[value[n] >> 4];
30         hex[n * 2 + 1] = digits[value[n] & 0x0f];
31     }
32     hex[n * 2] = '\0';
33 
34     return hex;
35 }
36 
hvb_uint64_to_base10(uint64_t value,char digits[HVB_MAX_DIGITS_UINT64])37 uint64_t hvb_uint64_to_base10(uint64_t value, char digits[HVB_MAX_DIGITS_UINT64])
38 {
39     char rev_digits[HVB_MAX_DIGITS_UINT64];
40     uint64_t n, num_digits;
41 
42     for (num_digits = 0; num_digits < HVB_MAX_DIGITS_UINT64 - 1;) {
43         rev_digits[num_digits++] = hvb_div_by_10(&value) + '0';
44         if (value == 0) {
45             break;
46         }
47     }
48 
49     for (n = 0; n < num_digits; n++)
50         digits[n] = rev_digits[num_digits - 1 - n];
51     digits[n] = '\0';
52 
53     return n;
54 }
55 
56 
hvb_be64toh(uint64_t data)57 uint64_t hvb_be64toh(uint64_t data)
58 {
59     uint8_t *value = (uint8_t *)&data;
60     uint64_t hex;
61 
62     hex = ((uint64_t)value[0]) << 56;
63     hex |= ((uint64_t)value[1]) << 48;
64     hex |= ((uint64_t)value[2]) << 40;
65     hex |= ((uint64_t)value[3]) << 32;
66     hex |= ((uint64_t)value[4]) << 24;
67     hex |= ((uint64_t)value[5]) << 16;
68     hex |= ((uint64_t)value[6]) << 8;
69     hex |= ((uint64_t)value[7]);
70     return hex;
71 }
72 
hvb_htobe64(uint64_t data)73 uint64_t hvb_htobe64(uint64_t data)
74 {
75     union {
76         uint64_t word;
77         uint8_t bytes[8];
78     } ret;
79 
80     ret.bytes[0] = (data >> 56) & 0xff;
81     ret.bytes[1] = (data >> 48) & 0xff;
82     ret.bytes[2] = (data >> 40) & 0xff;
83     ret.bytes[3] = (data >> 32) & 0xff;
84     ret.bytes[4] = (data >> 24) & 0xff;
85     ret.bytes[5] = (data >> 16) & 0xff;
86     ret.bytes[6] = (data >> 8) & 0xff;
87     ret.bytes[7] = data & 0xff;
88     return ret.word;
89 }
90 
hvb_malloc(uint64_t size)91 void *hvb_malloc(uint64_t size)
92 {
93     void *ret = hvb_malloc_(size);
94 
95     if (!ret) {
96         hvb_print("Failed to allocate memory.\n");
97         return NULL;
98     }
99     return ret;
100 }
101 
hvb_calloc(uint64_t size)102 void *hvb_calloc(uint64_t size)
103 {
104     void *ret = hvb_malloc(size);
105 
106     if (!ret)
107         return NULL;
108 
109     hvb_memset(ret, 0, size);
110 
111     return ret;
112 }
113 
hvb_strdup(const char * str)114 char *hvb_strdup(const char *str)
115 {
116     size_t len = hvb_strlen(str);
117     char *new_str = hvb_malloc(len + 1);
118 
119     if (!new_str)
120         return NULL;
121 
122     hvb_memcpy(new_str, str, len);
123 
124     new_str[len] = '\0';
125 
126     return new_str;
127 }
128