avb_util.h 8.9 KB

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