1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright (C) 2016 The Android Open Source Project
4  */
5 
6 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
7 #error "Never include this file directly, include libavb.h instead."
8 #endif
9 
10 #ifndef AVB_SYSDEPS_H_
11 #define AVB_SYSDEPS_H_
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /* Change these includes to match your platform to bring in the
18  * equivalent types available in a normal C runtime. At least things
19  * like uint8_t, uint64_t, and bool (with |false|, |true| keywords)
20  * must be present.
21  */
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include <linux/types.h>
25 
26 /* If you don't have gcc or clang, these attribute macros may need to
27  * be adjusted.
28  */
29 #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
30 #define AVB_ATTR_PACKED __attribute__((packed))
31 #define AVB_ATTR_NO_RETURN __attribute__((noreturn))
32 #define AVB_ATTR_SENTINEL __attribute__((__sentinel__))
33 
34 /* Size in bytes used for alignment. */
35 #ifdef __LP64__
36 #define AVB_ALIGNMENT_SIZE 8
37 #else
38 #define AVB_ALIGNMENT_SIZE 4
39 #endif
40 
41 /* Compare |n| bytes in |src1| and |src2|.
42  *
43  * Returns an integer less than, equal to, or greater than zero if the
44  * first |n| bytes of |src1| is found, respectively, to be less than,
45  * to match, or be greater than the first |n| bytes of |src2|. */
46 int avb_memcmp(const void* src1,
47                const void* src2,
48                size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
49 
50 /* Compare two strings.
51  *
52  * Return an integer less than, equal to, or greater than zero if |s1|
53  * is found, respectively, to be less than, to match, or be greater
54  * than |s2|.
55  */
56 int avb_strcmp(const char* s1, const char* s2);
57 
58 /* Compare |n| bytes in two strings.
59  *
60  * Return an integer less than, equal to, or greater than zero if the
61  * first |n| bytes of |s1| is found, respectively, to be less than,
62  * to match, or be greater than the first |n| bytes of |s2|.
63  */
64 int avb_strncmp(const char* s1, const char* s2, size_t n);
65 
66 /* Copy |n| bytes from |src| to |dest|. */
67 void* avb_memcpy(void* dest, const void* src, size_t n);
68 
69 /* Set |n| bytes starting at |s| to |c|.  Returns |dest|. */
70 void* avb_memset(void* dest, const int c, size_t n);
71 
72 /* Prints out a message. The string passed must be a NUL-terminated
73  * UTF-8 string.
74  */
75 void avb_print(const char* message);
76 
77 /* Prints out a vector of strings. Each argument must point to a
78  * NUL-terminated UTF-8 string and NULL should be the last argument.
79  */
80 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL;
81 
82 /* Aborts the program or reboots the device. */
83 void avb_abort(void) AVB_ATTR_NO_RETURN;
84 
85 /* Allocates |size| bytes. Returns NULL if no memory is available,
86  * otherwise a pointer to the allocated memory.
87  *
88  * The memory is not initialized.
89  *
90  * The pointer returned is guaranteed to be word-aligned.
91  *
92  * The memory should be freed with avb_free() when you are done with it.
93  */
94 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
95 
96 /* Frees memory previously allocated with avb_malloc(). */
97 void avb_free(void* ptr);
98 
99 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */
100 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
101 
102 /* Divide the |dividend| by 10 and saves back to the pointer. Return the
103  * remainder. */
104 uint32_t avb_div_by_10(uint64_t* dividend);
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* AVB_SYSDEPS_H_ */
111