15b69db07SJason Zhu /* 25b69db07SJason Zhu * Copyright (C) 2016 The Android Open Source Project 35b69db07SJason Zhu * 45b69db07SJason Zhu * Permission is hereby granted, free of charge, to any person 55b69db07SJason Zhu * obtaining a copy of this software and associated documentation 65b69db07SJason Zhu * files (the "Software"), to deal in the Software without 75b69db07SJason Zhu * restriction, including without limitation the rights to use, copy, 85b69db07SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 95b69db07SJason Zhu * of the Software, and to permit persons to whom the Software is 105b69db07SJason Zhu * furnished to do so, subject to the following conditions: 115b69db07SJason Zhu * 125b69db07SJason Zhu * The above copyright notice and this permission notice shall be 135b69db07SJason Zhu * included in all copies or substantial portions of the Software. 145b69db07SJason Zhu * 155b69db07SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 165b69db07SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 175b69db07SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 185b69db07SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 195b69db07SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 205b69db07SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 215b69db07SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 225b69db07SJason Zhu * SOFTWARE. 235b69db07SJason Zhu */ 245b69db07SJason Zhu 255b69db07SJason Zhu /* 265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead." 285b69db07SJason Zhu #endif 295b69db07SJason Zhu */ 305b69db07SJason Zhu 315b69db07SJason Zhu #ifndef AVB_UTIL_H_ 325b69db07SJason Zhu #define AVB_UTIL_H_ 335b69db07SJason Zhu 345b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 355b69db07SJason Zhu 36*37a7bc39SJason Zhu #ifdef __cplusplus 37*37a7bc39SJason Zhu extern "C" { 38*37a7bc39SJason Zhu #endif 39*37a7bc39SJason Zhu 40*37a7bc39SJason Zhu #define AVB_STRINGIFY(x) #x 41*37a7bc39SJason Zhu #define AVB_TO_STRING(x) AVB_STRINGIFY(x) 42*37a7bc39SJason Zhu 43*37a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG 44*37a7bc39SJason Zhu /* Aborts the program if |expr| is false. 45*37a7bc39SJason Zhu * 46*37a7bc39SJason Zhu * This has no effect unless AVB_ENABLE_DEBUG is defined. 47*37a7bc39SJason Zhu */ 48*37a7bc39SJason Zhu #define avb_assert(expr) \ 49*37a7bc39SJason Zhu do { \ 50*37a7bc39SJason Zhu if (!(expr)) { \ 51*37a7bc39SJason Zhu avb_fatal("assert fail: " #expr "\n"); \ 52*37a7bc39SJason Zhu } \ 53*37a7bc39SJason Zhu } while (0) 54*37a7bc39SJason Zhu #else 55*37a7bc39SJason Zhu #define avb_assert(expr) 56*37a7bc39SJason Zhu #endif 57*37a7bc39SJason Zhu 58*37a7bc39SJason Zhu /* Aborts the program if reached. 59*37a7bc39SJason Zhu * 60*37a7bc39SJason Zhu * This has no effect unless AVB_ENABLE_DEBUG is defined. 61*37a7bc39SJason Zhu */ 62*37a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG 63*37a7bc39SJason Zhu #define avb_assert_not_reached() \ 64*37a7bc39SJason Zhu do { \ 65*37a7bc39SJason Zhu avb_fatal("assert_not_reached()\n"); \ 66*37a7bc39SJason Zhu } while (0) 67*37a7bc39SJason Zhu #else 68*37a7bc39SJason Zhu #define avb_assert_not_reached() 69*37a7bc39SJason Zhu #endif 70*37a7bc39SJason Zhu 71*37a7bc39SJason Zhu /* Aborts the program if |addr| is not word-aligned. 72*37a7bc39SJason Zhu * 73*37a7bc39SJason Zhu * This has no effect unless AVB_ENABLE_DEBUG is defined. 74*37a7bc39SJason Zhu */ 75*37a7bc39SJason Zhu #define avb_assert_aligned(addr) \ 76*37a7bc39SJason Zhu avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0) 77*37a7bc39SJason Zhu 78*37a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG 79*37a7bc39SJason Zhu /* Print functions, used for diagnostics. 80*37a7bc39SJason Zhu * 81*37a7bc39SJason Zhu * These have no effect unless AVB_ENABLE_DEBUG is defined. 82*37a7bc39SJason Zhu */ 83*37a7bc39SJason Zhu #define avb_debug(message) \ 84*37a7bc39SJason Zhu do { \ 85*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 86*37a7bc39SJason Zhu ":", \ 87*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 88*37a7bc39SJason Zhu ": DEBUG: ", \ 89*37a7bc39SJason Zhu message, \ 90*37a7bc39SJason Zhu NULL); \ 91*37a7bc39SJason Zhu } while (0) 92*37a7bc39SJason Zhu #define avb_debugv(message, ...) \ 93*37a7bc39SJason Zhu do { \ 94*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 95*37a7bc39SJason Zhu ":", \ 96*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 97*37a7bc39SJason Zhu ": DEBUG: ", \ 98*37a7bc39SJason Zhu message, \ 99*37a7bc39SJason Zhu ##__VA_ARGS__); \ 100*37a7bc39SJason Zhu } while (0) 101*37a7bc39SJason Zhu #else 102*37a7bc39SJason Zhu #define avb_debug(message) 103*37a7bc39SJason Zhu #define avb_debugv(message, ...) 104*37a7bc39SJason Zhu #endif 105*37a7bc39SJason Zhu 106*37a7bc39SJason Zhu /* Prints out a message. This is typically used if a runtime-error 107*37a7bc39SJason Zhu * occurs. 108*37a7bc39SJason Zhu */ 109*37a7bc39SJason Zhu #define avb_error(message) \ 110*37a7bc39SJason Zhu do { \ 111*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 112*37a7bc39SJason Zhu ":", \ 113*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 114*37a7bc39SJason Zhu ": ERROR: ", \ 115*37a7bc39SJason Zhu message, \ 116*37a7bc39SJason Zhu NULL); \ 117*37a7bc39SJason Zhu } while (0) 118*37a7bc39SJason Zhu #define avb_errorv(message, ...) \ 119*37a7bc39SJason Zhu do { \ 120*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 121*37a7bc39SJason Zhu ":", \ 122*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 123*37a7bc39SJason Zhu ": ERROR: ", \ 124*37a7bc39SJason Zhu message, \ 125*37a7bc39SJason Zhu ##__VA_ARGS__); \ 126*37a7bc39SJason Zhu } while (0) 127*37a7bc39SJason Zhu 128*37a7bc39SJason Zhu /* Prints out a message and calls avb_abort(). 129*37a7bc39SJason Zhu */ 130*37a7bc39SJason Zhu #define avb_fatal(message) \ 131*37a7bc39SJason Zhu do { \ 132*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 133*37a7bc39SJason Zhu ":", \ 134*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 135*37a7bc39SJason Zhu ": FATAL: ", \ 136*37a7bc39SJason Zhu message, \ 137*37a7bc39SJason Zhu NULL); \ 138*37a7bc39SJason Zhu avb_abort(); \ 139*37a7bc39SJason Zhu } while (0) 140*37a7bc39SJason Zhu #define avb_fatalv(message, ...) \ 141*37a7bc39SJason Zhu do { \ 142*37a7bc39SJason Zhu avb_printv(avb_basename(__FILE__), \ 143*37a7bc39SJason Zhu ":", \ 144*37a7bc39SJason Zhu AVB_TO_STRING(__LINE__), \ 145*37a7bc39SJason Zhu ": FATAL: ", \ 146*37a7bc39SJason Zhu message, \ 147*37a7bc39SJason Zhu ##__VA_ARGS__); \ 148*37a7bc39SJason Zhu avb_abort(); \ 149*37a7bc39SJason Zhu } while (0) 150*37a7bc39SJason Zhu 1515b69db07SJason Zhu /* Converts a 32-bit unsigned integer from big-endian to host byte order. */ 1525b69db07SJason Zhu uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT; 1535b69db07SJason Zhu 1545b69db07SJason Zhu /* Converts a 64-bit unsigned integer from big-endian to host byte order. */ 1555b69db07SJason Zhu uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT; 1565b69db07SJason Zhu 1575b69db07SJason Zhu /* Converts a 32-bit unsigned integer from host to big-endian byte order. */ 1585b69db07SJason Zhu uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT; 1595b69db07SJason Zhu 1605b69db07SJason Zhu /* Converts a 64-bit unsigned integer from host to big-endian byte order. */ 1615b69db07SJason Zhu uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT; 1625b69db07SJason Zhu 1635b69db07SJason Zhu /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they 1645b69db07SJason Zhu * match, 1 if they don't. Returns 0 if |n|==0, since no bytes 1655b69db07SJason Zhu * mismatched. 1665b69db07SJason Zhu * 1675b69db07SJason Zhu * Time taken to perform the comparison is only dependent on |n| and 1685b69db07SJason Zhu * not on the relationship of the match between |s1| and |s2|. 1695b69db07SJason Zhu * 1705b69db07SJason Zhu * Note that unlike avb_memcmp(), this only indicates inequality, not 1715b69db07SJason Zhu * whether |s1| is less than or greater than |s2|. 1725b69db07SJason Zhu */ 1735b69db07SJason Zhu int avb_safe_memcmp(const void* s1, 1745b69db07SJason Zhu const void* s2, 1755b69db07SJason Zhu size_t n) AVB_ATTR_WARN_UNUSED_RESULT; 1765b69db07SJason Zhu 1775b69db07SJason Zhu /* Adds |value_to_add| to |value| with overflow protection. 1785b69db07SJason Zhu * 1795b69db07SJason Zhu * Returns false if the addition overflows, true otherwise. In either 1805b69db07SJason Zhu * case, |value| is always modified. 1815b69db07SJason Zhu */ 1825b69db07SJason Zhu bool avb_safe_add_to(uint64_t* value, 1835b69db07SJason Zhu uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT; 1845b69db07SJason Zhu 1855b69db07SJason Zhu /* Adds |a| and |b| with overflow protection, returning the value in 1865b69db07SJason Zhu * |out_result|. 1875b69db07SJason Zhu * 1885b69db07SJason Zhu * It's permissible to pass NULL for |out_result| if you just want to 1895b69db07SJason Zhu * check that the addition would not overflow. 1905b69db07SJason Zhu * 1915b69db07SJason Zhu * Returns false if the addition overflows, true otherwise. 1925b69db07SJason Zhu */ 1935b69db07SJason Zhu bool avb_safe_add(uint64_t* out_result, 1945b69db07SJason Zhu uint64_t a, 1955b69db07SJason Zhu uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT; 1965b69db07SJason Zhu 1975b69db07SJason Zhu /* Checks if |num_bytes| data at |data| is a valid UTF-8 1985b69db07SJason Zhu * string. Returns true if valid UTF-8, false otherwise. 1995b69db07SJason Zhu */ 2005b69db07SJason Zhu bool avb_validate_utf8(const uint8_t* data, 2015b69db07SJason Zhu size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT; 2025b69db07SJason Zhu 2035b69db07SJason Zhu /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len| 2045b69db07SJason Zhu * bytes) and puts the result in |buf| which holds |buf_size| 2055b69db07SJason Zhu * bytes. The result is also guaranteed to be NUL terminated. Fail if 2065b69db07SJason Zhu * there is not enough room in |buf| for the resulting string plus 2075b69db07SJason Zhu * terminating NUL byte. 2085b69db07SJason Zhu * 2095b69db07SJason Zhu * Returns true if the operation succeeds, false otherwise. 2105b69db07SJason Zhu */ 2115b69db07SJason Zhu bool avb_str_concat(char* buf, 2125b69db07SJason Zhu size_t buf_size, 2135b69db07SJason Zhu const char* str1, 2145b69db07SJason Zhu size_t str1_len, 2155b69db07SJason Zhu const char* str2, 2165b69db07SJason Zhu size_t str2_len); 2175b69db07SJason Zhu 2185b69db07SJason Zhu /* Like avb_malloc_() but prints a error using avb_error() if memory 2195b69db07SJason Zhu * allocation fails. 2205b69db07SJason Zhu */ 2215b69db07SJason Zhu void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; 2225b69db07SJason Zhu 2235b69db07SJason Zhu /* Like avb_malloc() but sets the memory with zeroes. */ 2245b69db07SJason Zhu void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; 2255b69db07SJason Zhu 2265b69db07SJason Zhu /* Duplicates a NUL-terminated string. Returns NULL on OOM. */ 2275b69db07SJason Zhu char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT; 2285b69db07SJason Zhu 2295b69db07SJason Zhu /* Duplicates a NULL-terminated array of NUL-terminated strings by 2305b69db07SJason Zhu * concatenating them. The returned string will be 2315b69db07SJason Zhu * NUL-terminated. Returns NULL on OOM. 2325b69db07SJason Zhu */ 2335b69db07SJason Zhu char* avb_strdupv(const char* str, 2345b69db07SJason Zhu ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL; 2355b69db07SJason Zhu 2365b69db07SJason Zhu /* Finds the first occurrence of |needle| in the string |haystack| 2375b69db07SJason Zhu * where both strings are NUL-terminated strings. The terminating NUL 2385b69db07SJason Zhu * bytes are not compared. 2395b69db07SJason Zhu * 2405b69db07SJason Zhu * Returns NULL if not found, otherwise points into |haystack| for the 2415b69db07SJason Zhu * first occurrence of |needle|. 2425b69db07SJason Zhu */ 2435b69db07SJason Zhu const char* avb_strstr(const char* haystack, 2445b69db07SJason Zhu const char* needle) AVB_ATTR_WARN_UNUSED_RESULT; 2455b69db07SJason Zhu 2465b69db07SJason Zhu /* Finds the first occurrence of |str| in the NULL-terminated string 2475b69db07SJason Zhu * array |strings|. Each element in |strings| must be 2485b69db07SJason Zhu * NUL-terminated. The string given by |str| need not be 2495b69db07SJason Zhu * NUL-terminated but its size must be given in |str_size|. 2505b69db07SJason Zhu * 2515b69db07SJason Zhu * Returns NULL if not found, otherwise points into |strings| for the 2525b69db07SJason Zhu * first occurrence of |str|. 2535b69db07SJason Zhu */ 2545b69db07SJason Zhu const char* avb_strv_find_str(const char* const* strings, 2555b69db07SJason Zhu const char* str, 2565b69db07SJason Zhu size_t str_size); 2575b69db07SJason Zhu 2585b69db07SJason Zhu /* Replaces all occurrences of |search| with |replace| in |str|. 2595b69db07SJason Zhu * 2605b69db07SJason Zhu * Returns a newly allocated string or NULL if out of memory. 2615b69db07SJason Zhu */ 2625b69db07SJason Zhu char* avb_replace(const char* str, 2635b69db07SJason Zhu const char* search, 2645b69db07SJason Zhu const char* replace) AVB_ATTR_WARN_UNUSED_RESULT; 2655b69db07SJason Zhu 2665b69db07SJason Zhu /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */ 2675b69db07SJason Zhu uint32_t avb_crc32(const uint8_t* buf, size_t buf_size); 2685b69db07SJason Zhu 2695b69db07SJason Zhu /* Returns the basename of |str|. This is defined as the last path 2705b69db07SJason Zhu * component, assuming the normal POSIX separator '/'. If there are no 2715b69db07SJason Zhu * separators, returns |str|. 2725b69db07SJason Zhu */ 2735b69db07SJason Zhu const char* avb_basename(const char* str); 2745b69db07SJason Zhu 275*37a7bc39SJason Zhu #ifdef __cplusplus 276*37a7bc39SJason Zhu } 2775b69db07SJason Zhu #endif 2785b69db07SJason Zhu 2795b69db07SJason Zhu #endif /* AVB_UTIL_H_ */ 280