avb_util.h 8.9 KB

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