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 18char *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 37uint64_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 57uint64_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 73uint64_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 91void *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 102void *hvb_calloc(uint64_t size) 103{ 104 void *ret = hvb_malloc(size); 105 106 if (!ret) 107 return NULL; 108 109 if (hvb_memset_s(ret, size, 0, size) != 0) { 110 hvb_free(ret); 111 return NULL; 112 } 113 114 return ret; 115} 116 117char *hvb_strdup(const char *str) 118{ 119 size_t len = hvb_strlen(str); 120 char *new_str = hvb_malloc(len + 1); 121 122 if (!new_str) 123 return NULL; 124 125 hvb_memcpy(new_str, str, len); 126 127 new_str[len] = '\0'; 128 129 return new_str; 130} 131 132enum hvb_errno check_hvb_ops(struct hvb_ops *ops) 133{ 134 hvb_return_hvb_err_if_null(ops); 135 hvb_return_hvb_err_if_null(ops->user_data); 136 hvb_return_hvb_err_if_null(ops->read_partition); 137 hvb_return_hvb_err_if_null(ops->write_partition); 138 hvb_return_hvb_err_if_null(ops->valid_rvt_key); 139 hvb_return_hvb_err_if_null(ops->read_rollback); 140 hvb_return_hvb_err_if_null(ops->write_rollback); 141 hvb_return_hvb_err_if_null(ops->read_lock_state); 142 hvb_return_hvb_err_if_null(ops->get_partiton_size); 143 return HVB_OK; 144}