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