xref: /aosp_15_r20/external/avb/libavb/avb_sysdeps.h (revision d289c2ba6de359471b23d594623b906876bc48a0)
1 /*
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_SYSDEPS_H_
30 #define AVB_SYSDEPS_H_
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /* Change these includes to match your platform to bring in the
37  * equivalent types available in a normal C runtime. At least things
38  * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
39  * must be present.
40  */
41 #include <inttypes.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdint.h>
45 
46 /* If you don't have gcc or clang, these attribute macros may need to
47  * be adjusted.
48  */
49 #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
50 #define AVB_ATTR_PACKED __attribute__((packed))
51 #define AVB_ATTR_PRINTF(x, y) __attribute__((format(printf, x, y)))
52 #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
53 #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
54 
55 /* Size in bytes used for alignment. */
56 #ifdef __LP64__
57 #define AVB_ALIGNMENT_SIZE 8
58 #else
59 #define AVB_ALIGNMENT_SIZE 4
60 #endif
61 
62 /* Compare |n| bytes in |src1| and |src2|.
63  *
64  * Returns an integer less than, equal to, or greater than zero if the
65  * first |n| bytes of |src1| is found, respectively, to be less than,
66  * to match, or be greater than the first |n| bytes of |src2|. */
67 int avb_memcmp(const void* src1,
68                const void* src2,
69                size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
70 
71 /* Compare two strings.
72  *
73  * Return an integer less than, equal to, or greater than zero if |s1|
74  * is found, respectively, to be less than, to match, or be greater
75  * than |s2|.
76  */
77 int avb_strcmp(const char* s1, const char* s2);
78 
79 /* Compare |n| bytes in two strings.
80  *
81  * Return an integer less than, equal to, or greater than zero if the
82  * first |n| bytes of |s1| is found, respectively, to be less than,
83  * to match, or be greater than the first |n| bytes of |s2|.
84  */
85 int avb_strncmp(const char* s1, const char* s2, size_t n);
86 
87 /* Copy |n| bytes from |src| to |dest|. */
88 void* avb_memcpy(void* dest, const void* src, size_t n);
89 
90 /* Set |n| bytes starting at |s| to |c|.  Returns |dest|. */
91 void* avb_memset(void* dest, const int c, size_t n);
92 
93 /* Prints out a message. The string passed must be a NUL-terminated
94  * UTF-8 string.
95  */
96 void avb_print(const char* message);
97 
98 /* Prints out a vector of strings. Each argument must point to a
99  * NUL-terminated UTF-8 string and NULL must be the last argument.
100  */
101 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
102 
103 /* Prints out a formatted string.
104  *
105  * Replaces avb_printv when AVB_USE_PRINTF_LOGS is enabled.
106  */
107 void avb_printf(const char* fmt, ...) AVB_ATTR_PRINTF(1, 2);
108 
109 /* Aborts the program or reboots the device. */
110 void avb_abort(void) AVB_ATTR_NO_RETURN;
111 
112 /* Allocates |size| bytes. Returns NULL if no memory is available,
113  * otherwise a pointer to the allocated memory.
114  *
115  * The memory is not initialized.
116  *
117  * The pointer returned is guaranteed to be word-aligned.
118  *
119  * The memory should be freed with avb_free() when you are done with it.
120  */
121 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
122 
123 /* Frees memory previously allocated with avb_malloc(). */
124 void avb_free(void* ptr);
125 
126 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
127 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
128 
129 /* Divide the |dividend| by 10 and saves back to the pointer. Return the
130  * remainder. */
131 uint32_t avb_div_by_10(uint64_t* dividend);
132 
133 #ifdef __cplusplus
134 }
135 #endif
136 
137 #endif /* AVB_SYSDEPS_H_ */
138