avb_util.h 10.0 KB

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