Igor Opaniuk | 8b23ae2 | 2018-06-03 21:56:36 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) |
| 8 | #error "Never include this file directly, include libavb.h instead." |
| 9 | #endif |
| 10 | |
| 11 | #ifndef AVB_UTIL_H_ |
| 12 | #define AVB_UTIL_H_ |
| 13 | |
| 14 | #include "avb_sysdeps.h" |
| 15 | |
| 16 | #ifdef __cplusplus |
| 17 | extern "C" { |
| 18 | #endif |
| 19 | |
| 20 | #define AVB_STRINGIFY(x) #x |
| 21 | #define AVB_TO_STRING(x) AVB_STRINGIFY(x) |
| 22 | |
| 23 | #ifdef AVB_ENABLE_DEBUG |
| 24 | /* Aborts the program if |expr| is false. |
| 25 | * |
| 26 | * This has no effect unless AVB_ENABLE_DEBUG is defined. |
| 27 | */ |
| 28 | #define avb_assert(expr) \ |
| 29 | do { \ |
| 30 | if (!(expr)) { \ |
| 31 | avb_fatal("assert fail: " #expr "\n"); \ |
| 32 | } \ |
| 33 | } while (0) |
| 34 | #else |
| 35 | #define avb_assert(expr) |
| 36 | #endif |
| 37 | |
| 38 | /* Aborts the program if reached. |
| 39 | * |
| 40 | * This has no effect unless AVB_ENABLE_DEBUG is defined. |
| 41 | */ |
| 42 | #ifdef AVB_ENABLE_DEBUG |
| 43 | #define avb_assert_not_reached() \ |
| 44 | do { \ |
| 45 | avb_fatal("assert_not_reached()\n"); \ |
| 46 | } while (0) |
| 47 | #else |
| 48 | #define avb_assert_not_reached() |
| 49 | #endif |
| 50 | |
| 51 | /* Aborts the program if |addr| is not word-aligned. |
| 52 | * |
| 53 | * This has no effect unless AVB_ENABLE_DEBUG is defined. |
| 54 | */ |
| 55 | #define avb_assert_aligned(addr) \ |
| 56 | avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0) |
| 57 | |
| 58 | #ifdef AVB_ENABLE_DEBUG |
| 59 | /* Print functions, used for diagnostics. |
| 60 | * |
| 61 | * These have no effect unless AVB_ENABLE_DEBUG is defined. |
| 62 | */ |
| 63 | #define avb_debug(message) \ |
| 64 | do { \ |
| 65 | avb_printv(avb_basename(__FILE__), \ |
| 66 | ":", \ |
| 67 | AVB_TO_STRING(__LINE__), \ |
| 68 | ": DEBUG: ", \ |
| 69 | message, \ |
| 70 | NULL); \ |
| 71 | } while (0) |
| 72 | #define avb_debugv(message, ...) \ |
| 73 | do { \ |
| 74 | avb_printv(avb_basename(__FILE__), \ |
| 75 | ":", \ |
| 76 | AVB_TO_STRING(__LINE__), \ |
| 77 | ": DEBUG: ", \ |
| 78 | message, \ |
| 79 | ##__VA_ARGS__); \ |
| 80 | } while (0) |
| 81 | #else |
| 82 | #define avb_debug(message) |
| 83 | #define avb_debugv(message, ...) |
| 84 | #endif |
| 85 | |
| 86 | /* Prints out a message. This is typically used if a runtime-error |
| 87 | * occurs. |
| 88 | */ |
| 89 | #define avb_error(message) \ |
| 90 | do { \ |
| 91 | avb_printv(avb_basename(__FILE__), \ |
| 92 | ":", \ |
| 93 | AVB_TO_STRING(__LINE__), \ |
| 94 | ": ERROR: ", \ |
| 95 | message, \ |
| 96 | NULL); \ |
| 97 | } while (0) |
| 98 | #define avb_errorv(message, ...) \ |
| 99 | do { \ |
| 100 | avb_printv(avb_basename(__FILE__), \ |
| 101 | ":", \ |
| 102 | AVB_TO_STRING(__LINE__), \ |
| 103 | ": ERROR: ", \ |
| 104 | message, \ |
| 105 | ##__VA_ARGS__); \ |
| 106 | } while (0) |
| 107 | |
| 108 | /* Prints out a message and calls avb_abort(). |
| 109 | */ |
| 110 | #define avb_fatal(message) \ |
| 111 | do { \ |
| 112 | avb_printv(avb_basename(__FILE__), \ |
| 113 | ":", \ |
| 114 | AVB_TO_STRING(__LINE__), \ |
| 115 | ": FATAL: ", \ |
| 116 | message, \ |
| 117 | NULL); \ |
| 118 | avb_abort(); \ |
| 119 | } while (0) |
| 120 | #define avb_fatalv(message, ...) \ |
| 121 | do { \ |
| 122 | avb_printv(avb_basename(__FILE__), \ |
| 123 | ":", \ |
| 124 | AVB_TO_STRING(__LINE__), \ |
| 125 | ": FATAL: ", \ |
| 126 | message, \ |
| 127 | ##__VA_ARGS__); \ |
| 128 | avb_abort(); \ |
| 129 | } while (0) |
| 130 | |
| 131 | /* Converts a 32-bit unsigned integer from big-endian to host byte order. */ |
| 132 | uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT; |
| 133 | |
| 134 | /* Converts a 64-bit unsigned integer from big-endian to host byte order. */ |
| 135 | uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT; |
| 136 | |
| 137 | /* Converts a 32-bit unsigned integer from host to big-endian byte order. */ |
| 138 | uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT; |
| 139 | |
| 140 | /* Converts a 64-bit unsigned integer from host to big-endian byte order. */ |
| 141 | uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT; |
| 142 | |
| 143 | /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they |
| 144 | * match, 1 if they don't. Returns 0 if |n|==0, since no bytes |
| 145 | * mismatched. |
| 146 | * |
| 147 | * Time taken to perform the comparison is only dependent on |n| and |
| 148 | * not on the relationship of the match between |s1| and |s2|. |
| 149 | * |
| 150 | * Note that unlike avb_memcmp(), this only indicates inequality, not |
| 151 | * whether |s1| is less than or greater than |s2|. |
| 152 | */ |
| 153 | int avb_safe_memcmp(const void* s1, |
| 154 | const void* s2, |
| 155 | size_t n) AVB_ATTR_WARN_UNUSED_RESULT; |
| 156 | |
| 157 | /* Adds |value_to_add| to |value| with overflow protection. |
| 158 | * |
| 159 | * Returns false if the addition overflows, true otherwise. In either |
| 160 | * case, |value| is always modified. |
| 161 | */ |
| 162 | bool avb_safe_add_to(uint64_t* value, |
| 163 | uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT; |
| 164 | |
| 165 | /* Adds |a| and |b| with overflow protection, returning the value in |
| 166 | * |out_result|. |
| 167 | * |
| 168 | * It's permissible to pass NULL for |out_result| if you just want to |
| 169 | * check that the addition would not overflow. |
| 170 | * |
| 171 | * Returns false if the addition overflows, true otherwise. |
| 172 | */ |
| 173 | bool avb_safe_add(uint64_t* out_result, |
| 174 | uint64_t a, |
| 175 | uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT; |
| 176 | |
| 177 | /* Checks if |num_bytes| data at |data| is a valid UTF-8 |
| 178 | * string. Returns true if valid UTF-8, false otherwise. |
| 179 | */ |
| 180 | bool avb_validate_utf8(const uint8_t* data, |
| 181 | size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT; |
| 182 | |
| 183 | /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len| |
| 184 | * bytes) and puts the result in |buf| which holds |buf_size| |
| 185 | * bytes. The result is also guaranteed to be NUL terminated. Fail if |
| 186 | * there is not enough room in |buf| for the resulting string plus |
| 187 | * terminating NUL byte. |
| 188 | * |
| 189 | * Returns true if the operation succeeds, false otherwise. |
| 190 | */ |
| 191 | bool avb_str_concat(char* buf, |
| 192 | size_t buf_size, |
| 193 | const char* str1, |
| 194 | size_t str1_len, |
| 195 | const char* str2, |
| 196 | size_t str2_len); |
| 197 | |
| 198 | /* Like avb_malloc_() but prints a error using avb_error() if memory |
| 199 | * allocation fails. |
| 200 | */ |
| 201 | void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; |
| 202 | |
| 203 | /* Like avb_malloc() but sets the memory with zeroes. */ |
| 204 | void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; |
| 205 | |
| 206 | /* Duplicates a NUL-terminated string. Returns NULL on OOM. */ |
| 207 | char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT; |
| 208 | |
| 209 | /* Duplicates a NULL-terminated array of NUL-terminated strings by |
| 210 | * concatenating them. The returned string will be |
| 211 | * NUL-terminated. Returns NULL on OOM. |
| 212 | */ |
| 213 | char* avb_strdupv(const char* str, |
| 214 | ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL; |
| 215 | |
| 216 | /* Finds the first occurrence of |needle| in the string |haystack| |
| 217 | * where both strings are NUL-terminated strings. The terminating NUL |
| 218 | * bytes are not compared. |
| 219 | * |
| 220 | * Returns NULL if not found, otherwise points into |haystack| for the |
| 221 | * first occurrence of |needle|. |
| 222 | */ |
| 223 | const char* avb_strstr(const char* haystack, |
| 224 | const char* needle) AVB_ATTR_WARN_UNUSED_RESULT; |
| 225 | |
| 226 | /* Finds the first occurrence of |str| in the NULL-terminated string |
| 227 | * array |strings|. Each element in |strings| must be |
| 228 | * NUL-terminated. The string given by |str| need not be |
| 229 | * NUL-terminated but its size must be given in |str_size|. |
| 230 | * |
| 231 | * Returns NULL if not found, otherwise points into |strings| for the |
| 232 | * first occurrence of |str|. |
| 233 | */ |
| 234 | const char* avb_strv_find_str(const char* const* strings, |
| 235 | const char* str, |
| 236 | size_t str_size); |
| 237 | |
| 238 | /* Replaces all occurrences of |search| with |replace| in |str|. |
| 239 | * |
| 240 | * Returns a newly allocated string or NULL if out of memory. |
| 241 | */ |
| 242 | char* avb_replace(const char* str, |
| 243 | const char* search, |
| 244 | const char* replace) AVB_ATTR_WARN_UNUSED_RESULT; |
| 245 | |
| 246 | /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */ |
| 247 | uint32_t avb_crc32(const uint8_t* buf, size_t buf_size); |
| 248 | |
| 249 | /* Returns the basename of |str|. This is defined as the last path |
| 250 | * component, assuming the normal POSIX separator '/'. If there are no |
| 251 | * separators, returns |str|. |
| 252 | */ |
| 253 | const char* avb_basename(const char* str); |
| 254 | |
| 255 | /* Converts any ascii lowercase characters in |str| to uppercase in-place. |
| 256 | * |str| must be NUL-terminated and valid UTF-8. |
| 257 | */ |
| 258 | void avb_uppercase(char* str); |
| 259 | |
| 260 | /* Converts |data_len| bytes of |data| to hex and returns the result. Returns |
| 261 | * NULL on OOM. Caller must free the returned string with avb_free. |
| 262 | */ |
| 263 | char* avb_bin2hex(const uint8_t* data, size_t data_len); |
| 264 | |
| 265 | #ifdef __cplusplus |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | #endif /* AVB_UTIL_H_ */ |