xref: /rk3399_rockchip-uboot/include/android_avb/avb_util.h (revision ab608f806ee1d7fa63a18cc035e8ea62b67634e5)
15b69db07SJason Zhu /*
25b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
35b69db07SJason Zhu  *
45b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
55b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
65b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
75b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
85b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
95b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
105b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
115b69db07SJason Zhu  *
125b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
135b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
145b69db07SJason Zhu  *
155b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
165b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
175b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
185b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
195b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
205b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
215b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
225b69db07SJason Zhu  * SOFTWARE.
235b69db07SJason Zhu  */
245b69db07SJason Zhu 
255b69db07SJason Zhu /*
265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
285b69db07SJason Zhu #endif
295b69db07SJason Zhu */
305b69db07SJason Zhu 
315b69db07SJason Zhu #ifndef AVB_UTIL_H_
325b69db07SJason Zhu #define AVB_UTIL_H_
335b69db07SJason Zhu 
345b69db07SJason Zhu #include <android_avb/avb_sysdeps.h>
355b69db07SJason Zhu 
3637a7bc39SJason Zhu #ifdef __cplusplus
3737a7bc39SJason Zhu extern "C" {
3837a7bc39SJason Zhu #endif
3937a7bc39SJason Zhu 
4037a7bc39SJason Zhu #define AVB_STRINGIFY(x) #x
4137a7bc39SJason Zhu #define AVB_TO_STRING(x) AVB_STRINGIFY(x)
4237a7bc39SJason Zhu 
4337a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG
4437a7bc39SJason Zhu /* Aborts the program if |expr| is false.
4537a7bc39SJason Zhu  *
4637a7bc39SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
4737a7bc39SJason Zhu  */
4837a7bc39SJason Zhu #define avb_assert(expr)                     \
4937a7bc39SJason Zhu   do {                                       \
5037a7bc39SJason Zhu     if (!(expr)) {                           \
5137a7bc39SJason Zhu       avb_fatal("assert fail: " #expr "\n"); \
5237a7bc39SJason Zhu     }                                        \
5337a7bc39SJason Zhu   } while (0)
5437a7bc39SJason Zhu #else
5537a7bc39SJason Zhu #define avb_assert(expr)
5637a7bc39SJason Zhu #endif
5737a7bc39SJason Zhu 
5837a7bc39SJason Zhu /* Aborts the program if reached.
5937a7bc39SJason Zhu  *
6037a7bc39SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
6137a7bc39SJason Zhu  */
6237a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG
6337a7bc39SJason Zhu #define avb_assert_not_reached()         \
6437a7bc39SJason Zhu   do {                                   \
6537a7bc39SJason Zhu     avb_fatal("assert_not_reached()\n"); \
6637a7bc39SJason Zhu   } while (0)
6737a7bc39SJason Zhu #else
6837a7bc39SJason Zhu #define avb_assert_not_reached()
6937a7bc39SJason Zhu #endif
7037a7bc39SJason Zhu 
7137a7bc39SJason Zhu /* Aborts the program if |addr| is not word-aligned.
7237a7bc39SJason Zhu  *
7337a7bc39SJason Zhu  * This has no effect unless AVB_ENABLE_DEBUG is defined.
7437a7bc39SJason Zhu  */
7537a7bc39SJason Zhu #define avb_assert_aligned(addr) \
7637a7bc39SJason Zhu   avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
7737a7bc39SJason Zhu 
7837a7bc39SJason Zhu #ifdef AVB_ENABLE_DEBUG
7937a7bc39SJason Zhu /* Print functions, used for diagnostics.
8037a7bc39SJason Zhu  *
8137a7bc39SJason Zhu  * These have no effect unless AVB_ENABLE_DEBUG is defined.
8237a7bc39SJason Zhu  */
8337a7bc39SJason Zhu #define avb_debug(message)              \
8437a7bc39SJason Zhu   do {                                  \
8537a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
8637a7bc39SJason Zhu                ":",                     \
8737a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
8837a7bc39SJason Zhu                ": DEBUG: ",             \
8937a7bc39SJason Zhu                message,                 \
9037a7bc39SJason Zhu                NULL);                   \
9137a7bc39SJason Zhu   } while (0)
9237a7bc39SJason Zhu #define avb_debugv(message, ...)        \
9337a7bc39SJason Zhu   do {                                  \
9437a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
9537a7bc39SJason Zhu                ":",                     \
9637a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
9737a7bc39SJason Zhu                ": DEBUG: ",             \
9837a7bc39SJason Zhu                message,                 \
9937a7bc39SJason Zhu                ##__VA_ARGS__);          \
10037a7bc39SJason Zhu   } while (0)
10137a7bc39SJason Zhu #else
10237a7bc39SJason Zhu #define avb_debug(message)
10337a7bc39SJason Zhu #define avb_debugv(message, ...)
10437a7bc39SJason Zhu #endif
10537a7bc39SJason Zhu 
10637a7bc39SJason Zhu /* Prints out a message. This is typically used if a runtime-error
10737a7bc39SJason Zhu  * occurs.
10837a7bc39SJason Zhu  */
10937a7bc39SJason Zhu #define avb_error(message)              \
11037a7bc39SJason Zhu   do {                                  \
11137a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
11237a7bc39SJason Zhu                ":",                     \
11337a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
11437a7bc39SJason Zhu                ": ERROR: ",             \
11537a7bc39SJason Zhu                message,                 \
11637a7bc39SJason Zhu                NULL);                   \
11737a7bc39SJason Zhu   } while (0)
11837a7bc39SJason Zhu #define avb_errorv(message, ...)        \
11937a7bc39SJason Zhu   do {                                  \
12037a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
12137a7bc39SJason Zhu                ":",                     \
12237a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
12337a7bc39SJason Zhu                ": ERROR: ",             \
12437a7bc39SJason Zhu                message,                 \
12537a7bc39SJason Zhu                ##__VA_ARGS__);          \
12637a7bc39SJason Zhu   } while (0)
12737a7bc39SJason Zhu 
12837a7bc39SJason Zhu /* Prints out a message and calls avb_abort().
12937a7bc39SJason Zhu  */
13037a7bc39SJason Zhu #define avb_fatal(message)              \
13137a7bc39SJason Zhu   do {                                  \
13237a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
13337a7bc39SJason Zhu                ":",                     \
13437a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
13537a7bc39SJason Zhu                ": FATAL: ",             \
13637a7bc39SJason Zhu                message,                 \
13737a7bc39SJason Zhu                NULL);                   \
13837a7bc39SJason Zhu     avb_abort();                        \
13937a7bc39SJason Zhu   } while (0)
14037a7bc39SJason Zhu #define avb_fatalv(message, ...)        \
14137a7bc39SJason Zhu   do {                                  \
14237a7bc39SJason Zhu     avb_printv(avb_basename(__FILE__),  \
14337a7bc39SJason Zhu                ":",                     \
14437a7bc39SJason Zhu                AVB_TO_STRING(__LINE__), \
14537a7bc39SJason Zhu                ": FATAL: ",             \
14637a7bc39SJason Zhu                message,                 \
14737a7bc39SJason Zhu                ##__VA_ARGS__);          \
14837a7bc39SJason Zhu     avb_abort();                        \
14937a7bc39SJason Zhu   } while (0)
15037a7bc39SJason Zhu 
1515b69db07SJason Zhu /* Converts a 32-bit unsigned integer from big-endian to host byte order. */
1525b69db07SJason Zhu uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
1535b69db07SJason Zhu 
1545b69db07SJason Zhu /* Converts a 64-bit unsigned integer from big-endian to host byte order. */
1555b69db07SJason Zhu uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
1565b69db07SJason Zhu 
1575b69db07SJason Zhu /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
1585b69db07SJason Zhu uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
1595b69db07SJason Zhu 
1605b69db07SJason Zhu /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
1615b69db07SJason Zhu uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
1625b69db07SJason Zhu 
1635b69db07SJason Zhu /* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
1645b69db07SJason Zhu  * match, 1 if they don't.  Returns 0 if |n|==0, since no bytes
1655b69db07SJason Zhu  * mismatched.
1665b69db07SJason Zhu  *
1675b69db07SJason Zhu  * Time taken to perform the comparison is only dependent on |n| and
1685b69db07SJason Zhu  * not on the relationship of the match between |s1| and |s2|.
1695b69db07SJason Zhu  *
1705b69db07SJason Zhu  * Note that unlike avb_memcmp(), this only indicates inequality, not
1715b69db07SJason Zhu  * whether |s1| is less than or greater than |s2|.
1725b69db07SJason Zhu  */
1735b69db07SJason Zhu int avb_safe_memcmp(const void* s1,
1745b69db07SJason Zhu                     const void* s2,
1755b69db07SJason Zhu                     size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
1765b69db07SJason Zhu 
1775b69db07SJason Zhu /* Adds |value_to_add| to |value| with overflow protection.
1785b69db07SJason Zhu  *
1795b69db07SJason Zhu  * Returns false if the addition overflows, true otherwise. In either
1805b69db07SJason Zhu  * case, |value| is always modified.
1815b69db07SJason Zhu  */
1825b69db07SJason Zhu bool avb_safe_add_to(uint64_t* value,
1835b69db07SJason Zhu                      uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
1845b69db07SJason Zhu 
1855b69db07SJason Zhu /* Adds |a| and |b| with overflow protection, returning the value in
1865b69db07SJason Zhu  * |out_result|.
1875b69db07SJason Zhu  *
1885b69db07SJason Zhu  * It's permissible to pass NULL for |out_result| if you just want to
1895b69db07SJason Zhu  * check that the addition would not overflow.
1905b69db07SJason Zhu  *
1915b69db07SJason Zhu  * Returns false if the addition overflows, true otherwise.
1925b69db07SJason Zhu  */
1935b69db07SJason Zhu bool avb_safe_add(uint64_t* out_result,
1945b69db07SJason Zhu                   uint64_t a,
1955b69db07SJason Zhu                   uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
1965b69db07SJason Zhu 
1975b69db07SJason Zhu /* Checks if |num_bytes| data at |data| is a valid UTF-8
1985b69db07SJason Zhu  * string. Returns true if valid UTF-8, false otherwise.
1995b69db07SJason Zhu  */
2005b69db07SJason Zhu bool avb_validate_utf8(const uint8_t* data,
2015b69db07SJason Zhu                        size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
2025b69db07SJason Zhu 
2035b69db07SJason Zhu /* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
2045b69db07SJason Zhu  * bytes) and puts the result in |buf| which holds |buf_size|
2055b69db07SJason Zhu  * bytes. The result is also guaranteed to be NUL terminated. Fail if
2065b69db07SJason Zhu  * there is not enough room in |buf| for the resulting string plus
2075b69db07SJason Zhu  * terminating NUL byte.
2085b69db07SJason Zhu  *
2095b69db07SJason Zhu  * Returns true if the operation succeeds, false otherwise.
2105b69db07SJason Zhu  */
2115b69db07SJason Zhu bool avb_str_concat(char* buf,
2125b69db07SJason Zhu                     size_t buf_size,
2135b69db07SJason Zhu                     const char* str1,
2145b69db07SJason Zhu                     size_t str1_len,
2155b69db07SJason Zhu                     const char* str2,
2165b69db07SJason Zhu                     size_t str2_len);
2175b69db07SJason Zhu 
2185b69db07SJason Zhu /* Like avb_malloc_() but prints a error using avb_error() if memory
2195b69db07SJason Zhu  * allocation fails.
2205b69db07SJason Zhu  */
2215b69db07SJason Zhu void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
2225b69db07SJason Zhu 
2235b69db07SJason Zhu /* Like avb_malloc() but sets the memory with zeroes. */
2245b69db07SJason Zhu void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
2255b69db07SJason Zhu 
2265b69db07SJason Zhu /* Duplicates a NUL-terminated string. Returns NULL on OOM. */
2275b69db07SJason Zhu char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
2285b69db07SJason Zhu 
2295b69db07SJason Zhu /* Duplicates a NULL-terminated array of NUL-terminated strings by
2305b69db07SJason Zhu  * concatenating them. The returned string will be
2315b69db07SJason Zhu  * NUL-terminated. Returns NULL on OOM.
2325b69db07SJason Zhu  */
2335b69db07SJason Zhu char* avb_strdupv(const char* str,
2345b69db07SJason Zhu                   ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
2355b69db07SJason Zhu 
2365b69db07SJason Zhu /* Finds the first occurrence of |needle| in the string |haystack|
2375b69db07SJason Zhu  * where both strings are NUL-terminated strings. The terminating NUL
2385b69db07SJason Zhu  * bytes are not compared.
2395b69db07SJason Zhu  *
2405b69db07SJason Zhu  * Returns NULL if not found, otherwise points into |haystack| for the
2415b69db07SJason Zhu  * first occurrence of |needle|.
2425b69db07SJason Zhu  */
2435b69db07SJason Zhu const char* avb_strstr(const char* haystack,
2445b69db07SJason Zhu                        const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
2455b69db07SJason Zhu 
2465b69db07SJason Zhu /* Finds the first occurrence of |str| in the NULL-terminated string
2475b69db07SJason Zhu  * array |strings|. Each element in |strings| must be
2485b69db07SJason Zhu  * NUL-terminated. The string given by |str| need not be
2495b69db07SJason Zhu  * NUL-terminated but its size must be given in |str_size|.
2505b69db07SJason Zhu  *
2515b69db07SJason Zhu  * Returns NULL if not found, otherwise points into |strings| for the
2525b69db07SJason Zhu  * first occurrence of |str|.
2535b69db07SJason Zhu  */
2545b69db07SJason Zhu const char* avb_strv_find_str(const char* const* strings,
2555b69db07SJason Zhu                               const char* str,
2565b69db07SJason Zhu                               size_t str_size);
2575b69db07SJason Zhu 
2585b69db07SJason Zhu /* Replaces all occurrences of |search| with |replace| in |str|.
2595b69db07SJason Zhu  *
2605b69db07SJason Zhu  * Returns a newly allocated string or NULL if out of memory.
2615b69db07SJason Zhu  */
2625b69db07SJason Zhu char* avb_replace(const char* str,
2635b69db07SJason Zhu                   const char* search,
2645b69db07SJason Zhu                   const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
2655b69db07SJason Zhu 
2665b69db07SJason Zhu /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
2675b69db07SJason Zhu uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
2685b69db07SJason Zhu 
2695b69db07SJason Zhu /* Returns the basename of |str|. This is defined as the last path
2705b69db07SJason Zhu  * component, assuming the normal POSIX separator '/'. If there are no
2715b69db07SJason Zhu  * separators, returns |str|.
2725b69db07SJason Zhu  */
2735b69db07SJason Zhu const char* avb_basename(const char* str);
2745b69db07SJason Zhu 
275*ab608f80SJason Zhu /* Converts any ascii lowercase characters in |str| to uppercase in-place.
276*ab608f80SJason Zhu  * |str| must be NUL-terminated and valid UTF-8.
277*ab608f80SJason Zhu  */
278*ab608f80SJason Zhu void avb_uppercase(char* str);
279*ab608f80SJason Zhu 
280*ab608f80SJason Zhu /* Converts |data_len| bytes of |data| to hex and returns the result. Returns
281*ab608f80SJason Zhu  * NULL on OOM. Caller must free the returned string with avb_free.
282*ab608f80SJason Zhu  */
283*ab608f80SJason Zhu char* avb_bin2hex(const uint8_t* data, size_t data_len);
284*ab608f80SJason Zhu 
28537a7bc39SJason Zhu #ifdef __cplusplus
28637a7bc39SJason Zhu }
2875b69db07SJason Zhu #endif
2885b69db07SJason Zhu 
2895b69db07SJason Zhu #endif /* AVB_UTIL_H_ */
290