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