2 * Copyright (C) 2016 The Android Open Source Project
4 * SPDX-License-Identifier: MIT
7 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
8 #error "Never include this file directly, include libavb.h instead."
14 #include "avb_sysdeps.h"
20 #define AVB_STRINGIFY(x) #x
21 #define AVB_TO_STRING(x) AVB_STRINGIFY(x)
23 #ifdef AVB_ENABLE_DEBUG
24 /* Aborts the program if |expr| is false.
26 * This has no effect unless AVB_ENABLE_DEBUG is defined.
28 #define avb_assert(expr) \
31 avb_fatal("assert fail: " #expr "\n"); \
35 #define avb_assert(expr)
38 /* Aborts the program if reached.
40 * This has no effect unless AVB_ENABLE_DEBUG is defined.
42 #ifdef AVB_ENABLE_DEBUG
43 #define avb_assert_not_reached() \
45 avb_fatal("assert_not_reached()\n"); \
48 #define avb_assert_not_reached()
51 /* Aborts the program if |addr| is not word-aligned.
53 * This has no effect unless AVB_ENABLE_DEBUG is defined.
55 #define avb_assert_aligned(addr) \
56 avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
58 #ifdef AVB_ENABLE_DEBUG
59 /* Print functions, used for diagnostics.
61 * These have no effect unless AVB_ENABLE_DEBUG is defined.
63 #define avb_debug(message) \
65 avb_printv(avb_basename(__FILE__), \
67 AVB_TO_STRING(__LINE__), \
72 #define avb_debugv(message, ...) \
74 avb_printv(avb_basename(__FILE__), \
76 AVB_TO_STRING(__LINE__), \
82 #define avb_debug(message)
83 #define avb_debugv(message, ...)
86 /* Prints out a message. This is typically used if a runtime-error
89 #define avb_error(message) \
91 avb_printv(avb_basename(__FILE__), \
93 AVB_TO_STRING(__LINE__), \
98 #define avb_errorv(message, ...) \
100 avb_printv(avb_basename(__FILE__), \
102 AVB_TO_STRING(__LINE__), \
108 /* Prints out a message and calls avb_abort().
110 #define avb_fatal(message) \
112 avb_printv(avb_basename(__FILE__), \
114 AVB_TO_STRING(__LINE__), \
120 #define avb_fatalv(message, ...) \
122 avb_printv(avb_basename(__FILE__), \
124 AVB_TO_STRING(__LINE__), \
131 /* Converts a 32-bit unsigned integer from big-endian to host byte order. */
132 uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
134 /* Converts a 64-bit unsigned integer from big-endian to host byte order. */
135 uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
137 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
138 uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
140 /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
141 uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
143 /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
144 * match, 1 if they don't. Returns 0 if |n|==0, since no bytes
147 * Time taken to perform the comparison is only dependent on |n| and
148 * not on the relationship of the match between |s1| and |s2|.
150 * Note that unlike avb_memcmp(), this only indicates inequality, not
151 * whether |s1| is less than or greater than |s2|.
153 int avb_safe_memcmp(const void* s1,
155 size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
157 /* Adds |value_to_add| to |value| with overflow protection.
159 * Returns false if the addition overflows, true otherwise. In either
160 * case, |value| is always modified.
162 bool avb_safe_add_to(uint64_t* value,
163 uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
165 /* Adds |a| and |b| with overflow protection, returning the value in
168 * It's permissible to pass NULL for |out_result| if you just want to
169 * check that the addition would not overflow.
171 * Returns false if the addition overflows, true otherwise.
173 bool avb_safe_add(uint64_t* out_result,
175 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.
180 bool avb_validate_utf8(const uint8_t* data,
181 size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
183 /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
184 * bytes) and puts the result in |buf| which holds |buf_size|
185 * bytes. The result is also guaranteed to be NUL terminated. Fail if
186 * there is not enough room in |buf| for the resulting string plus
187 * terminating NUL byte.
189 * Returns true if the operation succeeds, false otherwise.
191 bool avb_str_concat(char* buf,
198 /* Like avb_malloc_() but prints a error using avb_error() if memory
201 void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
203 /* Like avb_malloc() but sets the memory with zeroes. */
204 void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
206 /* Duplicates a NUL-terminated string. Returns NULL on OOM. */
207 char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
209 /* Duplicates a NULL-terminated array of NUL-terminated strings by
210 * concatenating them. The returned string will be
211 * NUL-terminated. Returns NULL on OOM.
213 char* avb_strdupv(const char* str,
214 ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
216 /* Finds the first occurrence of |needle| in the string |haystack|
217 * where both strings are NUL-terminated strings. The terminating NUL
218 * bytes are not compared.
220 * Returns NULL if not found, otherwise points into |haystack| for the
221 * first occurrence of |needle|.
223 const char* avb_strstr(const char* haystack,
224 const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
226 /* Finds the first occurrence of |str| in the NULL-terminated string
227 * array |strings|. Each element in |strings| must be
228 * NUL-terminated. The string given by |str| need not be
229 * NUL-terminated but its size must be given in |str_size|.
231 * Returns NULL if not found, otherwise points into |strings| for the
232 * first occurrence of |str|.
234 const char* avb_strv_find_str(const char* const* strings,
238 /* Replaces all occurrences of |search| with |replace| in |str|.
240 * Returns a newly allocated string or NULL if out of memory.
242 char* avb_replace(const char* str,
244 const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
246 /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
247 uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
249 /* Returns the basename of |str|. This is defined as the last path
250 * component, assuming the normal POSIX separator '/'. If there are no
251 * separators, returns |str|.
253 const char* avb_basename(const char* str);
255 /* Converts any ascii lowercase characters in |str| to uppercase in-place.
256 * |str| must be NUL-terminated and valid UTF-8.
258 void avb_uppercase(char* str);
260 /* Converts |data_len| bytes of |data| to hex and returns the result. Returns
261 * NULL on OOM. Caller must free the returned string with avb_free.
263 char* avb_bin2hex(const uint8_t* data, size_t data_len);
269 #endif /* AVB_UTIL_H_ */