1 /* SPDX-License-Identifier: MIT */
3 * Copyright (C) 2016 The Android Open Source Project
6 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
7 #error "Never include this file directly, include libavb.h instead."
13 #include "avb_sysdeps.h"
19 #define AVB_STRINGIFY(x) #x
20 #define AVB_TO_STRING(x) AVB_STRINGIFY(x)
22 #ifdef AVB_ENABLE_DEBUG
23 /* Aborts the program if |expr| is false.
25 * This has no effect unless AVB_ENABLE_DEBUG is defined.
27 #define avb_assert(expr) \
30 avb_fatal("assert fail: " #expr "\n"); \
34 #define avb_assert(expr)
37 /* Aborts the program if reached.
39 * This has no effect unless AVB_ENABLE_DEBUG is defined.
41 #ifdef AVB_ENABLE_DEBUG
42 #define avb_assert_not_reached() \
44 avb_fatal("assert_not_reached()\n"); \
47 #define avb_assert_not_reached()
50 /* Aborts the program if |addr| is not word-aligned.
52 * This has no effect unless AVB_ENABLE_DEBUG is defined.
54 #define avb_assert_aligned(addr) \
55 avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
57 #ifdef AVB_ENABLE_DEBUG
58 /* Print functions, used for diagnostics.
60 * These have no effect unless AVB_ENABLE_DEBUG is defined.
62 #define avb_debug(message) \
64 avb_printv(avb_basename(__FILE__), \
66 AVB_TO_STRING(__LINE__), \
71 #define avb_debugv(message, ...) \
73 avb_printv(avb_basename(__FILE__), \
75 AVB_TO_STRING(__LINE__), \
81 #define avb_debug(message)
82 #define avb_debugv(message, ...)
85 /* Prints out a message. This is typically used if a runtime-error
88 #define avb_error(message) \
90 avb_printv(avb_basename(__FILE__), \
92 AVB_TO_STRING(__LINE__), \
97 #define avb_errorv(message, ...) \
99 avb_printv(avb_basename(__FILE__), \
101 AVB_TO_STRING(__LINE__), \
107 /* Prints out a message and calls avb_abort().
109 #define avb_fatal(message) \
111 avb_printv(avb_basename(__FILE__), \
113 AVB_TO_STRING(__LINE__), \
119 #define avb_fatalv(message, ...) \
121 avb_printv(avb_basename(__FILE__), \
123 AVB_TO_STRING(__LINE__), \
130 /* Converts a 32-bit unsigned integer from big-endian to host byte order. */
131 uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
133 /* Converts a 64-bit unsigned integer from big-endian to host byte order. */
134 uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
136 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
137 uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
139 /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
140 uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
142 /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
143 * match, 1 if they don't. Returns 0 if |n|==0, since no bytes
146 * Time taken to perform the comparison is only dependent on |n| and
147 * not on the relationship of the match between |s1| and |s2|.
149 * Note that unlike avb_memcmp(), this only indicates inequality, not
150 * whether |s1| is less than or greater than |s2|.
152 int avb_safe_memcmp(const void* s1,
154 size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
156 /* Adds |value_to_add| to |value| with overflow protection.
158 * Returns false if the addition overflows, true otherwise. In either
159 * case, |value| is always modified.
161 bool avb_safe_add_to(uint64_t* value,
162 uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
164 /* Adds |a| and |b| with overflow protection, returning the value in
167 * It's permissible to pass NULL for |out_result| if you just want to
168 * check that the addition would not overflow.
170 * Returns false if the addition overflows, true otherwise.
172 bool avb_safe_add(uint64_t* out_result,
174 uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
176 /* Checks if |num_bytes| data at |data| is a valid UTF-8
177 * string. Returns true if valid UTF-8, false otherwise.
179 bool avb_validate_utf8(const uint8_t* data,
180 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.
188 * Returns true if the operation succeeds, false otherwise.
190 bool avb_str_concat(char* buf,
197 /* Like avb_malloc_() but prints a error using avb_error() if memory
200 void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
202 /* Like avb_malloc() but sets the memory with zeroes. */
203 void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
205 /* Duplicates a NUL-terminated string. Returns NULL on OOM. */
206 char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
208 /* Duplicates a NULL-terminated array of NUL-terminated strings by
209 * concatenating them. The returned string will be
210 * NUL-terminated. Returns NULL on OOM.
212 char* avb_strdupv(const char* str,
213 ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
215 /* Finds the first occurrence of |needle| in the string |haystack|
216 * where both strings are NUL-terminated strings. The terminating NUL
217 * bytes are not compared.
219 * Returns NULL if not found, otherwise points into |haystack| for the
220 * first occurrence of |needle|.
222 const char* avb_strstr(const char* haystack,
223 const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
225 /* Finds the first occurrence of |str| in the NULL-terminated string
226 * array |strings|. Each element in |strings| must be
227 * NUL-terminated. The string given by |str| need not be
228 * NUL-terminated but its size must be given in |str_size|.
230 * Returns NULL if not found, otherwise points into |strings| for the
231 * first occurrence of |str|.
233 const char* avb_strv_find_str(const char* const* strings,
237 /* Replaces all occurrences of |search| with |replace| in |str|.
239 * Returns a newly allocated string or NULL if out of memory.
241 char* avb_replace(const char* str,
243 const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
245 /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
246 uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
248 /* Returns the basename of |str|. This is defined as the last path
249 * component, assuming the normal POSIX separator '/'. If there are no
250 * separators, returns |str|.
252 const char* avb_basename(const char* str);
254 /* Converts any ascii lowercase characters in |str| to uppercase in-place.
255 * |str| must be NUL-terminated and valid UTF-8.
257 void avb_uppercase(char* str);
259 /* Converts |data_len| bytes of |data| to hex and returns the result. Returns
260 * NULL on OOM. Caller must free the returned string with avb_free.
262 char* avb_bin2hex(const uint8_t* data, size_t data_len);
268 #endif /* AVB_UTIL_H_ */