xref: /OK3568_Linux_fs/u-boot/include/android_avb/avb_util.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2016 The Android Open Source Project
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person
5*4882a593Smuzhiyun  * obtaining a copy of this software and associated documentation
6*4882a593Smuzhiyun  * files (the "Software"), to deal in the Software without
7*4882a593Smuzhiyun  * restriction, including without limitation the rights to use, copy,
8*4882a593Smuzhiyun  * modify, merge, publish, distribute, sublicense, and/or sell copies
9*4882a593Smuzhiyun  * of the Software, and to permit persons to whom the Software is
10*4882a593Smuzhiyun  * furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be
13*4882a593Smuzhiyun  * included in all copies or substantial portions of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*4882a593Smuzhiyun  * SOFTWARE.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
27*4882a593Smuzhiyun #error "Never include this file directly, include libavb.h instead."
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #ifndef AVB_UTIL_H_
32*4882a593Smuzhiyun #define AVB_UTIL_H_
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <android_avb/avb_sysdeps.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #ifdef __cplusplus
37*4882a593Smuzhiyun extern "C" {
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define AVB_STRINGIFY(x) #x
41*4882a593Smuzhiyun #define AVB_TO_STRING(x) AVB_STRINGIFY(x)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #ifdef AVB_ENABLE_DEBUG
44*4882a593Smuzhiyun /* Aborts the program if |expr| is false.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * This has no effect unless AVB_ENABLE_DEBUG is defined.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #define avb_assert(expr)                     \
49*4882a593Smuzhiyun   do {                                       \
50*4882a593Smuzhiyun     if (!(expr)) {                           \
51*4882a593Smuzhiyun       avb_fatal("assert fail: " #expr "\n"); \
52*4882a593Smuzhiyun     }                                        \
53*4882a593Smuzhiyun   } while (0)
54*4882a593Smuzhiyun #else
55*4882a593Smuzhiyun #define avb_assert(expr)
56*4882a593Smuzhiyun #endif
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Aborts the program if reached.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * This has no effect unless AVB_ENABLE_DEBUG is defined.
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun #ifdef AVB_ENABLE_DEBUG
63*4882a593Smuzhiyun #define avb_assert_not_reached()         \
64*4882a593Smuzhiyun   do {                                   \
65*4882a593Smuzhiyun     avb_fatal("assert_not_reached()\n"); \
66*4882a593Smuzhiyun   } while (0)
67*4882a593Smuzhiyun #else
68*4882a593Smuzhiyun #define avb_assert_not_reached()
69*4882a593Smuzhiyun #endif
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /* Aborts the program if |addr| is not word-aligned.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * This has no effect unless AVB_ENABLE_DEBUG is defined.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun #define avb_assert_aligned(addr) \
76*4882a593Smuzhiyun   avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #ifdef AVB_ENABLE_DEBUG
79*4882a593Smuzhiyun /* Print functions, used for diagnostics.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * These have no effect unless AVB_ENABLE_DEBUG is defined.
82*4882a593Smuzhiyun  */
83*4882a593Smuzhiyun #define avb_debug(message)              \
84*4882a593Smuzhiyun   do {                                  \
85*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
86*4882a593Smuzhiyun                ":",                     \
87*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
88*4882a593Smuzhiyun                ": DEBUG: ",             \
89*4882a593Smuzhiyun                message,                 \
90*4882a593Smuzhiyun                NULL);                   \
91*4882a593Smuzhiyun   } while (0)
92*4882a593Smuzhiyun #define avb_debugv(message, ...)        \
93*4882a593Smuzhiyun   do {                                  \
94*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
95*4882a593Smuzhiyun                ":",                     \
96*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
97*4882a593Smuzhiyun                ": DEBUG: ",             \
98*4882a593Smuzhiyun                message,                 \
99*4882a593Smuzhiyun                ##__VA_ARGS__);          \
100*4882a593Smuzhiyun   } while (0)
101*4882a593Smuzhiyun #else
102*4882a593Smuzhiyun #define avb_debug(message)
103*4882a593Smuzhiyun #define avb_debugv(message, ...)
104*4882a593Smuzhiyun #endif
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /* Prints out a message. This is typically used if a runtime-error
107*4882a593Smuzhiyun  * occurs.
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun #define avb_error(message)              \
110*4882a593Smuzhiyun   do {                                  \
111*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
112*4882a593Smuzhiyun                ":",                     \
113*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
114*4882a593Smuzhiyun                ": ERROR: ",             \
115*4882a593Smuzhiyun                message,                 \
116*4882a593Smuzhiyun                NULL);                   \
117*4882a593Smuzhiyun   } while (0)
118*4882a593Smuzhiyun #define avb_errorv(message, ...)        \
119*4882a593Smuzhiyun   do {                                  \
120*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
121*4882a593Smuzhiyun                ":",                     \
122*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
123*4882a593Smuzhiyun                ": ERROR: ",             \
124*4882a593Smuzhiyun                message,                 \
125*4882a593Smuzhiyun                ##__VA_ARGS__);          \
126*4882a593Smuzhiyun   } while (0)
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /* Prints out a message and calls avb_abort().
129*4882a593Smuzhiyun  */
130*4882a593Smuzhiyun #define avb_fatal(message)              \
131*4882a593Smuzhiyun   do {                                  \
132*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
133*4882a593Smuzhiyun                ":",                     \
134*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
135*4882a593Smuzhiyun                ": FATAL: ",             \
136*4882a593Smuzhiyun                message,                 \
137*4882a593Smuzhiyun                NULL);                   \
138*4882a593Smuzhiyun     avb_abort();                        \
139*4882a593Smuzhiyun   } while (0)
140*4882a593Smuzhiyun #define avb_fatalv(message, ...)        \
141*4882a593Smuzhiyun   do {                                  \
142*4882a593Smuzhiyun     avb_printv(avb_basename(__FILE__),  \
143*4882a593Smuzhiyun                ":",                     \
144*4882a593Smuzhiyun                AVB_TO_STRING(__LINE__), \
145*4882a593Smuzhiyun                ": FATAL: ",             \
146*4882a593Smuzhiyun                message,                 \
147*4882a593Smuzhiyun                ##__VA_ARGS__);          \
148*4882a593Smuzhiyun     avb_abort();                        \
149*4882a593Smuzhiyun   } while (0)
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /* Converts a 32-bit unsigned integer from big-endian to host byte order. */
152*4882a593Smuzhiyun uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* Converts a 64-bit unsigned integer from big-endian to host byte order. */
155*4882a593Smuzhiyun uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
158*4882a593Smuzhiyun uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
161*4882a593Smuzhiyun uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
164*4882a593Smuzhiyun  * match, 1 if they don't.  Returns 0 if |n|==0, since no bytes
165*4882a593Smuzhiyun  * mismatched.
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  * Time taken to perform the comparison is only dependent on |n| and
168*4882a593Smuzhiyun  * not on the relationship of the match between |s1| and |s2|.
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * Note that unlike avb_memcmp(), this only indicates inequality, not
171*4882a593Smuzhiyun  * whether |s1| is less than or greater than |s2|.
172*4882a593Smuzhiyun  */
173*4882a593Smuzhiyun int avb_safe_memcmp(const void* s1,
174*4882a593Smuzhiyun                     const void* s2,
175*4882a593Smuzhiyun                     size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /* Adds |value_to_add| to |value| with overflow protection.
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  * Returns false if the addition overflows, true otherwise. In either
180*4882a593Smuzhiyun  * case, |value| is always modified.
181*4882a593Smuzhiyun  */
182*4882a593Smuzhiyun bool avb_safe_add_to(uint64_t* value,
183*4882a593Smuzhiyun                      uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /* Adds |a| and |b| with overflow protection, returning the value in
186*4882a593Smuzhiyun  * |out_result|.
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * It's permissible to pass NULL for |out_result| if you just want to
189*4882a593Smuzhiyun  * check that the addition would not overflow.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * Returns false if the addition overflows, true otherwise.
192*4882a593Smuzhiyun  */
193*4882a593Smuzhiyun bool avb_safe_add(uint64_t* out_result,
194*4882a593Smuzhiyun                   uint64_t a,
195*4882a593Smuzhiyun                   uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /* Checks if |num_bytes| data at |data| is a valid UTF-8
198*4882a593Smuzhiyun  * string. Returns true if valid UTF-8, false otherwise.
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun bool avb_validate_utf8(const uint8_t* data,
201*4882a593Smuzhiyun                        size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
204*4882a593Smuzhiyun  * bytes) and puts the result in |buf| which holds |buf_size|
205*4882a593Smuzhiyun  * bytes. The result is also guaranteed to be NUL terminated. Fail if
206*4882a593Smuzhiyun  * there is not enough room in |buf| for the resulting string plus
207*4882a593Smuzhiyun  * terminating NUL byte.
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * Returns true if the operation succeeds, false otherwise.
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun bool avb_str_concat(char* buf,
212*4882a593Smuzhiyun                     size_t buf_size,
213*4882a593Smuzhiyun                     const char* str1,
214*4882a593Smuzhiyun                     size_t str1_len,
215*4882a593Smuzhiyun                     const char* str2,
216*4882a593Smuzhiyun                     size_t str2_len);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /* Like avb_malloc_() but prints a error using avb_error() if memory
219*4882a593Smuzhiyun  * allocation fails.
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /* Like avb_malloc() but sets the memory with zeroes. */
224*4882a593Smuzhiyun void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /* Duplicates a NUL-terminated string. Returns NULL on OOM. */
227*4882a593Smuzhiyun char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /* Duplicates a NULL-terminated array of NUL-terminated strings by
230*4882a593Smuzhiyun  * concatenating them. The returned string will be
231*4882a593Smuzhiyun  * NUL-terminated. Returns NULL on OOM.
232*4882a593Smuzhiyun  */
233*4882a593Smuzhiyun char* avb_strdupv(const char* str,
234*4882a593Smuzhiyun                   ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /* Finds the first occurrence of |needle| in the string |haystack|
237*4882a593Smuzhiyun  * where both strings are NUL-terminated strings. The terminating NUL
238*4882a593Smuzhiyun  * bytes are not compared.
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Returns NULL if not found, otherwise points into |haystack| for the
241*4882a593Smuzhiyun  * first occurrence of |needle|.
242*4882a593Smuzhiyun  */
243*4882a593Smuzhiyun const char* avb_strstr(const char* haystack,
244*4882a593Smuzhiyun                        const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun /* Finds the first occurrence of |str| in the NULL-terminated string
247*4882a593Smuzhiyun  * array |strings|. Each element in |strings| must be
248*4882a593Smuzhiyun  * NUL-terminated. The string given by |str| need not be
249*4882a593Smuzhiyun  * NUL-terminated but its size must be given in |str_size|.
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * Returns NULL if not found, otherwise points into |strings| for the
252*4882a593Smuzhiyun  * first occurrence of |str|.
253*4882a593Smuzhiyun  */
254*4882a593Smuzhiyun const char* avb_strv_find_str(const char* const* strings,
255*4882a593Smuzhiyun                               const char* str,
256*4882a593Smuzhiyun                               size_t str_size);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /* Replaces all occurrences of |search| with |replace| in |str|.
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * Returns a newly allocated string or NULL if out of memory.
261*4882a593Smuzhiyun  */
262*4882a593Smuzhiyun char* avb_replace(const char* str,
263*4882a593Smuzhiyun                   const char* search,
264*4882a593Smuzhiyun                   const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
267*4882a593Smuzhiyun uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /* Returns the basename of |str|. This is defined as the last path
270*4882a593Smuzhiyun  * component, assuming the normal POSIX separator '/'. If there are no
271*4882a593Smuzhiyun  * separators, returns |str|.
272*4882a593Smuzhiyun  */
273*4882a593Smuzhiyun const char* avb_basename(const char* str);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /* Converts any ascii lowercase characters in |str| to uppercase in-place.
276*4882a593Smuzhiyun  * |str| must be NUL-terminated and valid UTF-8.
277*4882a593Smuzhiyun  */
278*4882a593Smuzhiyun void avb_uppercase(char* str);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /* Converts |data_len| bytes of |data| to hex and returns the result. Returns
281*4882a593Smuzhiyun  * NULL on OOM. Caller must free the returned string with avb_free.
282*4882a593Smuzhiyun  */
283*4882a593Smuzhiyun char* avb_bin2hex(const uint8_t* data, size_t data_len);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun #ifdef __cplusplus
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun #endif
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun #endif /* AVB_UTIL_H_ */
290