1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * string function definitions for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_STRING_H
8 #define _NOLIBC_STRING_H
12 static void *malloc(size_t len);
15 * As much as possible, please keep functions alphabetically sorted.
18 static __attribute__((unused))
19 int memcmp(const void *s1, const void *s2, size_t n)
24 while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
30 static __attribute__((unused))
31 void *_nolibc_memcpy_up(void *dst, const void *src, size_t len)
36 ((char *)dst)[pos] = ((const char *)src)[pos];
42 static __attribute__((unused))
43 void *_nolibc_memcpy_down(void *dst, const void *src, size_t len)
47 ((char *)dst)[len] = ((const char *)src)[len];
52 /* might be ignored by the compiler without -ffreestanding, then found as
55 __attribute__((weak,unused,section(".text.nolibc_memmove")))
56 void *memmove(void *dst, const void *src, size_t len)
70 ((char *)dst)[pos] = ((const char *)src)[pos];
76 /* must be exported, as it's used by libgcc on ARM */
77 __attribute__((weak,unused,section(".text.nolibc_memcpy")))
78 void *memcpy(void *dst, const void *src, size_t len)
80 return _nolibc_memcpy_up(dst, src, len);
83 /* might be ignored by the compiler without -ffreestanding, then found as
86 __attribute__((weak,unused,section(".text.nolibc_memset")))
87 void *memset(void *dst, int b, size_t len)
92 /* prevent gcc from recognizing memset() here */
99 static __attribute__((unused))
100 char *strchr(const char *s, int c)
110 static __attribute__((unused))
111 int strcmp(const char *a, const char *b)
116 while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
121 static __attribute__((unused))
122 char *strcpy(char *dst, const char *src)
126 while ((*dst++ = *src++));
130 /* this function is only used with arguments that are not constants or when
131 * it's not known because optimizations are disabled. Note that gcc 12
132 * recognizes an strlen() pattern and replaces it with a jump to strlen(),
133 * thus itself, hence the asm() statement below that's meant to disable this
134 * confusing practice.
136 static __attribute__((unused))
137 size_t strlen(const char *str)
141 for (len = 0; str[len]; len++)
146 /* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
147 * the two branches, then will rely on an external definition of strlen().
149 #if defined(__OPTIMIZE__)
150 #define nolibc_strlen(x) strlen(x)
151 #define strlen(str) ({ \
152 __builtin_constant_p((str)) ? \
153 __builtin_strlen((str)) : \
154 nolibc_strlen((str)); \
158 static __attribute__((unused))
159 size_t strnlen(const char *str, size_t maxlen)
163 for (len = 0; (len < maxlen) && str[len]; len++);
167 static __attribute__((unused))
168 char *strdup(const char *str)
174 ret = malloc(len + 1);
175 if (__builtin_expect(ret != NULL, 1))
176 memcpy(ret, str, len + 1);
181 static __attribute__((unused))
182 char *strndup(const char *str, size_t maxlen)
187 len = strnlen(str, maxlen);
188 ret = malloc(len + 1);
189 if (__builtin_expect(ret != NULL, 1)) {
190 memcpy(ret, str, len);
197 static __attribute__((unused))
198 size_t strlcat(char *dst, const char *src, size_t size)
203 for (len = 0; dst[len]; len++)
219 static __attribute__((unused))
220 size_t strlcpy(char *dst, const char *src, size_t size)
236 static __attribute__((unused))
237 char *strncat(char *dst, const char *src, size_t size)
244 while (size && (*dst = *src)) {
254 static __attribute__((unused))
255 int strncmp(const char *a, const char *b, size_t size)
261 !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
267 static __attribute__((unused))
268 char *strncpy(char *dst, const char *src, size_t size)
272 for (len = 0; len < size; len++)
273 if ((dst[len] = *src))
278 static __attribute__((unused))
279 char *strrchr(const char *s, int c)
281 const char *ret = NULL;
291 /* make sure to include all global symbols */
294 #endif /* _NOLIBC_STRING_H */