Debian Codegraph @

src/boot/efi-string.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "efi.h"
5
6 size_t strnlen8(const char *s, size_t n);
7 size_t strnlen16(const char16_t *s, size_t n);
8
9 static inline size_t strlen8(const char *s) {
10 return strnlen8(s, SIZE_MAX);
11 }
12
13 static inline size_t strlen16(const char16_t *s) {
14 return strnlen16(s, SIZE_MAX);
15 }
16
17 static inline size_t strsize8(const char *s) {
18 return s ? (strlen8(s) + 1) * sizeof(*s) : 0;
19 }
20
21 static inline size_t strsize16(const char16_t *s) {
22 return s ? (strlen16(s) + 1) * sizeof(*s) : 0;
23 }
24
25 char* strtolower8(char *s);
26 char16_t* strtolower16(char16_t *s);
27
28 int strncmp8(const char *s1, const char *s2, size_t n);
29 int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
30 int strncasecmp8(const char *s1, const char *s2, size_t n);
31 int strncasecmp16(const char16_t *s1, const char16_t *s2, size_t n);
32
33 static inline int strcmp8(const char *s1, const char *s2) {
34 return strncmp8(s1, s2, SIZE_MAX);
35 }
36
37 static inline int strcmp16(const char16_t *s1, const char16_t *s2) {
38 return strncmp16(s1, s2, SIZE_MAX);
39 }
40
41 static inline int strcasecmp8(const char *s1, const char *s2) {
42 return strncasecmp8(s1, s2, SIZE_MAX);
43 }
44
45 static inline int strcasecmp16(const char16_t *s1, const char16_t *s2) {
46 return strncasecmp16(s1, s2, SIZE_MAX);
47 }
48
49 static inline bool strneq8(const char *s1, const char *s2, size_t n) {
50 return strncmp8(s1, s2, n) == 0;
51 }
52
53 static inline bool strneq16(const char16_t *s1, const char16_t *s2, size_t n) {
54 return strncmp16(s1, s2, n) == 0;
55 }
56
57 static inline bool streq8(const char *s1, const char *s2) {
58 return strcmp8(s1, s2) == 0;
59 }
60
61 static inline bool streq16(const char16_t *s1, const char16_t *s2) {
62 return strcmp16(s1, s2) == 0;
63 }
64
65 static inline int strncaseeq8(const char *s1, const char *s2, size_t n) {
66 return strncasecmp8(s1, s2, n) == 0;
67 }
68
69 static inline int strncaseeq16(const char16_t *s1, const char16_t *s2, size_t n) {
70 return strncasecmp16(s1, s2, n) == 0;
71 }
72
73 static inline bool strcaseeq8(const char *s1, const char *s2) {
74 return strcasecmp8(s1, s2) == 0;
75 }
76
77 static inline bool strcaseeq16(const char16_t *s1, const char16_t *s2) {
78 return strcasecmp16(s1, s2) == 0;
79 }
80
81 char* strcpy8(char * restrict dest, const char * restrict src);
82 char16_t *strcpy16(char16_t * restrict dest, const char16_t * restrict src);
83
84 char* strchr8(const char *s, char c);
85 char16_t *strchr16(const char16_t *s, char16_t c);
86
87 char* xstrndup8(const char *s, size_t n);
88 char16_t *xstrndup16(const char16_t *s, size_t n);
89
90 static inline char* xstrdup8(const char *s) {
91 return xstrndup8(s, SIZE_MAX);
92 }
93
94 static inline char16_t *xstrdup16(const char16_t *s) {
95 return xstrndup16(s, SIZE_MAX);
96 }
97
98 char16_t *xstrn8_to_16(const char *str8, size_t n);
99 static inline char16_t *xstr8_to_16(const char *str8) {
100 return xstrn8_to_16(str8, SIZE_MAX);
101 }
102
103 char *xstrn16_to_ascii(const char16_t *str16, size_t n);
104 static inline char *xstr16_to_ascii(const char16_t *str16) {
105 return xstrn16_to_ascii(str16, SIZE_MAX);
106 }
107
108 char* startswith8(const char *s, const char *prefix);
109
110 size_t strspn16(const char16_t *p, const char16_t *good);
111 size_t strcspn16(const char16_t *p, const char16_t *bad);
112
113 bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack);
114
115 bool parse_number8(const char *s, uint64_t *ret_u, const char **ret_tail);
116 bool parse_number16(const char16_t *s, uint64_t *ret_u, const char16_t **ret_tail);
117
118 bool parse_boolean(const char *v, bool *ret);
119
120 char* line_get_key_value(char *s, const char *sep, size_t *pos, char **ret_key, char **ret_value);
121
122 char16_t *hexdump(const void *data, size_t size);
123
124 #ifdef __clang__
125 # define _gnu_printf_(a, b) _printf_(a, b)
126 #else
127 # define _gnu_printf_(a, b) __attribute__((format(gnu_printf, a, b)))
128 #endif
129
130 _gnu_printf_(2, 3) void printf_status(EFI_STATUS status, const char *format, ...);
131 _gnu_printf_(2, 0) void vprintf_status(EFI_STATUS status, const char *format, va_list ap);
132 _gnu_printf_(2, 3) _warn_unused_result_ char16_t *xasprintf_status(EFI_STATUS status, const char *format, ...);
133 _gnu_printf_(2, 0) _warn_unused_result_ char16_t *xvasprintf_status(EFI_STATUS status, const char *format, va_list ap);
134
135 #if SD_BOOT
136 # define printf(...) printf_status(EFI_SUCCESS, __VA_ARGS__)
137 # define xasprintf(...) xasprintf_status(EFI_SUCCESS, __VA_ARGS__)
138
139 /* inttypes.h is provided by libc instead of the compiler and is not supposed to be used in freestanding
140 * environments. We could use clang __*_FMT*__ constants for this, bug gcc does not have them. :( */
141
142 # if defined(__ILP32__) || defined(__arm__) || defined(__i386__)
143 # define PRI64_PREFIX "ll"
144 # elif defined(__LP64__)
145 # define PRI64_PREFIX "l"
146 # elif defined(__LLP64__) || (__SIZEOF_LONG__ == 4 && __SIZEOF_POINTER__ == 8)
147 # define PRI64_PREFIX "ll"
148 # else
149 # error Unknown 64-bit data model
150 # endif
151
152 # define PRIi32 "i"
153 # define PRIu32 "u"
154 # define PRIx32 "x"
155 # define PRIX32 "X"
156 # define PRIiPTR "zi"
157 # define PRIuPTR "zu"
158 # define PRIxPTR "zx"
159 # define PRIXPTR "zX"
160 # define PRIi64 PRI64_PREFIX "i"
161 # define PRIu64 PRI64_PREFIX "u"
162 # define PRIx64 PRI64_PREFIX "x"
163 # define PRIX64 PRI64_PREFIX "X"
164
165 /* The compiler normally has knowledge about standard functions such as memcmp, but this is not the case when
166 * compiling with -ffreestanding. By referring to builtins, the compiler can check arguments and do
167 * optimizations again. Note that we still need to provide implementations as the compiler is free to not
168 * inline its own implementation and instead issue a library call. */
169 # define memchr __builtin_memchr
170 # define memcmp __builtin_memcmp
171 # define memcpy __builtin_memcpy
172 # define memset __builtin_memset
173
174 static inline void *mempcpy(void * restrict dest, const void * restrict src, size_t n) {
175 if (!dest || !src || n == 0)
176 return dest;
177 memcpy(dest, src, n);
178 return (uint8_t *) dest + n;
179 }
180
181 #else
182 /* For unit testing. */
183 void *efi_memchr(const void *p, int c, size_t n);
184 int efi_memcmp(const void *p1, const void *p2, size_t n);
185 void *efi_memcpy(void * restrict dest, const void * restrict src, size_t n);
186 void *efi_memset(void *p, int c, size_t n);
187 #endif