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 #ifndef __HVB_UTIL_H_ 16 #define __HVB_UTIL_H_ 17 18 #include <stddef.h> 19 #include "hvb_sysdeps.h" 20 21 #ifdef __cplusplus 22 extern "C" 23 { 24 #endif 25 26 #define MAX_STRING_LEN 17 27 #define HVB_MAX_DIGITS_UINT64 32 28 29 #ifndef NULL 30 #define NULL 0 31 #endif 32 33 #ifdef offsetof 34 #define hvb_offsetof(type, member) offsetof(type, member) 35 #else 36 #define hvb_offsetof(type, member) ((size_t)&(((type *)0)->member)) 37 #endif 38 39 #define hvb_return_hvb_err_if_null(__ptr) \ 40 do { \ 41 if ((__ptr) == NULL) { \ 42 hvb_printv("error, %s is NULL\n", #__ptr); \ 43 return HVB_ERROR_INVALID_ARGUMENT; \ 44 } \ 45 } while (0) 46 47 uint64_t hvb_be64toh(uint64_t data); 48 uint64_t hvb_htobe64(uint64_t data); 49 50 char *hvb_strdup(const char *str); 51 void *hvb_malloc(uint64_t size); 52 void *hvb_calloc(uint64_t size); 53 54 uint64_t hvb_uint64_to_base10(uint64_t value, char digits[HVB_MAX_DIGITS_UINT64]); 55 char *hvb_bin2hex(const uint8_t *data, size_t data_len); 56 57 #ifdef __cplusplus 58 } 59 #endif 60 61 #endif