• 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 
18 #define WORD_BYTE_SIZE sizeof(unsigned long)
19 #define BYTES_PER_TYPE(type) sizeof(type)
20 
hvb_bin2hex(const uint8_t * value,size_t len)21 char *hvb_bin2hex(const uint8_t *value, size_t len)
22 {
23     const char digits[MAX_STRING_LEN] = "0123456789abcdef";
24     char *hex;
25     size_t n;
26 
27     hex = hvb_malloc(len * 2 + 1);
28     if (hex == NULL)
29         return NULL;
30 
31     for (n = 0; n < len; n++) {
32         hex[n * 2] = digits[value[n] >> 4];
33         hex[n * 2 + 1] = digits[value[n] & 0x0f];
34     }
35     hex[n * 2] = '\0';
36 
37     return hex;
38 }
39 
hvb_uint64_to_base10(uint64_t value,char digits[HVB_MAX_DIGITS_UINT64])40 uint64_t hvb_uint64_to_base10(uint64_t value, char digits[HVB_MAX_DIGITS_UINT64])
41 {
42     char rev_digits[HVB_MAX_DIGITS_UINT64];
43     uint64_t n, num_digits;
44 
45     for (num_digits = 0; num_digits < HVB_MAX_DIGITS_UINT64 - 1;) {
46         rev_digits[num_digits++] = hvb_div_by_10(&value) + '0';
47         if (value == 0) {
48             break;
49         }
50     }
51 
52     for (n = 0; n < num_digits; n++)
53         digits[n] = rev_digits[num_digits - 1 - n];
54     digits[n] = '\0';
55 
56     return n;
57 }
58 
59 
hvb_be64toh(uint64_t data)60 uint64_t hvb_be64toh(uint64_t data)
61 {
62     uint8_t *value = (uint8_t *)&data;
63     uint64_t hex = 0;
64 
65     for (int i = BYTES_PER_TYPE(unsigned long) - 1; i >= 0; i--)
66         hex |= ((uint64_t)value[i] << ((BYTES_PER_TYPE(unsigned long) - 1 - i) * BYTES_PER_TYPE(unsigned long)));
67 
68     return hex;
69 }
70 
hvb_htobe64(uint64_t data)71 uint64_t hvb_htobe64(uint64_t data)
72 {
73     union {
74         uint64_t word;
75         uint8_t bytes[BYTES_PER_TYPE(unsigned long)];
76     } ret;
77 
78     for (int i = BYTES_PER_TYPE(unsigned long) - 1; i >= 0; i--) {
79         ret.bytes[i] = (uint8_t)(data & 0xff);
80         data >>= BYTES_PER_TYPE(unsigned long);
81     }
82 
83     return ret.word;
84 }
85 
hvb_malloc(uint64_t size)86 void *hvb_malloc(uint64_t size)
87 {
88     void *ret = hvb_malloc_(size);
89 
90     if (!ret) {
91         hvb_print("Failed to allocate memory.\n");
92         return NULL;
93     }
94     return ret;
95 }
96 
hvb_calloc(uint64_t size)97 void *hvb_calloc(uint64_t size)
98 {
99     void *ret = hvb_malloc(size);
100 
101     if (!ret)
102         return NULL;
103 
104     if (hvb_memset_s(ret, size, 0, size) != 0) {
105         hvb_free(ret);
106         return NULL;
107     }
108 
109     return ret;
110 }
111 
hvb_strdup(const char * str)112 char *hvb_strdup(const char *str)
113 {
114     size_t len = hvb_strlen(str);
115     char *new_str = hvb_malloc(len + 1);
116 
117     if (!new_str)
118         return NULL;
119 
120     hvb_memcpy(new_str, str, len);
121 
122     new_str[len] = '\0';
123 
124     return new_str;
125 }
126 
check_hvb_ops(struct hvb_ops * ops)127 enum hvb_errno check_hvb_ops(struct hvb_ops *ops)
128 {
129     hvb_return_hvb_err_if_null(ops);
130     hvb_return_hvb_err_if_null(ops->user_data);
131     hvb_return_hvb_err_if_null(ops->read_partition);
132     hvb_return_hvb_err_if_null(ops->write_partition);
133     hvb_return_hvb_err_if_null(ops->valid_rvt_key);
134     hvb_return_hvb_err_if_null(ops->read_rollback);
135     hvb_return_hvb_err_if_null(ops->write_rollback);
136     hvb_return_hvb_err_if_null(ops->read_lock_state);
137     hvb_return_hvb_err_if_null(ops->get_partiton_size);
138     return HVB_OK;
139 }