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