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