Linux Audio

Check our new training course

Loading...
v6.8
  1/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2/*
  3 * string function definitions for NOLIBC
  4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
  5 */
  6
  7#ifndef _NOLIBC_STRING_H
  8#define _NOLIBC_STRING_H
  9
 10#include "std.h"
 11
 12static void *malloc(size_t len);
 13
 14/*
 15 * As much as possible, please keep functions alphabetically sorted.
 16 */
 17
 18static __attribute__((unused))
 19int memcmp(const void *s1, const void *s2, size_t n)
 20{
 21	size_t ofs = 0;
 22	int c1 = 0;
 23
 24	while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
 25		ofs++;
 26	}
 27	return c1;
 28}
 29
 30#ifndef NOLIBC_ARCH_HAS_MEMMOVE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 31/* might be ignored by the compiler without -ffreestanding, then found as
 32 * missing.
 33 */
 34__attribute__((weak,unused,section(".text.nolibc_memmove")))
 35void *memmove(void *dst, const void *src, size_t len)
 36{
 37	size_t dir, pos;
 38
 39	pos = len;
 40	dir = -1;
 41
 42	if (dst < src) {
 43		pos = -1;
 44		dir = 1;
 45	}
 46
 47	while (len) {
 48		pos += dir;
 49		((char *)dst)[pos] = ((const char *)src)[pos];
 50		len--;
 51	}
 52	return dst;
 53}
 54#endif /* #ifndef NOLIBC_ARCH_HAS_MEMMOVE */
 55
 56#ifndef NOLIBC_ARCH_HAS_MEMCPY
 57/* must be exported, as it's used by libgcc on ARM */
 58__attribute__((weak,unused,section(".text.nolibc_memcpy")))
 59void *memcpy(void *dst, const void *src, size_t len)
 60{
 61	size_t pos = 0;
 62
 63	while (pos < len) {
 64		((char *)dst)[pos] = ((const char *)src)[pos];
 65		pos++;
 66	}
 67	return dst;
 68}
 69#endif /* #ifndef NOLIBC_ARCH_HAS_MEMCPY */
 70
 71#ifndef NOLIBC_ARCH_HAS_MEMSET
 72/* might be ignored by the compiler without -ffreestanding, then found as
 73 * missing.
 74 */
 75__attribute__((weak,unused,section(".text.nolibc_memset")))
 76void *memset(void *dst, int b, size_t len)
 77{
 78	char *p = dst;
 79
 80	while (len--) {
 81		/* prevent gcc from recognizing memset() here */
 82		__asm__ volatile("");
 83		*(p++) = b;
 84	}
 85	return dst;
 86}
 87#endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 88
 89static __attribute__((unused))
 90char *strchr(const char *s, int c)
 91{
 92	while (*s) {
 93		if (*s == (char)c)
 94			return (char *)s;
 95		s++;
 96	}
 97	return NULL;
 98}
 99
100static __attribute__((unused))
101int strcmp(const char *a, const char *b)
102{
103	unsigned int c;
104	int diff;
105
106	while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
107		;
108	return diff;
109}
110
111static __attribute__((unused))
112char *strcpy(char *dst, const char *src)
113{
114	char *ret = dst;
115
116	while ((*dst++ = *src++));
117	return ret;
118}
119
120/* this function is only used with arguments that are not constants or when
121 * it's not known because optimizations are disabled. Note that gcc 12
122 * recognizes an strlen() pattern and replaces it with a jump to strlen(),
123 * thus itself, hence the asm() statement below that's meant to disable this
124 * confusing practice.
125 */
126static __attribute__((unused))
127size_t strlen(const char *str)
128{
129	size_t len;
130
131	for (len = 0; str[len]; len++)
132		__asm__("");
133	return len;
134}
135
136/* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
137 * the two branches, then will rely on an external definition of strlen().
138 */
139#if defined(__OPTIMIZE__)
140#define nolibc_strlen(x) strlen(x)
141#define strlen(str) ({                          \
142	__builtin_constant_p((str)) ?           \
143		__builtin_strlen((str)) :       \
144		nolibc_strlen((str));           \
145})
146#endif
147
148static __attribute__((unused))
149size_t strnlen(const char *str, size_t maxlen)
150{
151	size_t len;
152
153	for (len = 0; (len < maxlen) && str[len]; len++);
154	return len;
155}
156
157static __attribute__((unused))
158char *strdup(const char *str)
159{
160	size_t len;
161	char *ret;
162
163	len = strlen(str);
164	ret = malloc(len + 1);
165	if (__builtin_expect(ret != NULL, 1))
166		memcpy(ret, str, len + 1);
167
168	return ret;
169}
170
171static __attribute__((unused))
172char *strndup(const char *str, size_t maxlen)
173{
174	size_t len;
175	char *ret;
176
177	len = strnlen(str, maxlen);
178	ret = malloc(len + 1);
179	if (__builtin_expect(ret != NULL, 1)) {
180		memcpy(ret, str, len);
181		ret[len] = '\0';
182	}
183
184	return ret;
185}
186
187static __attribute__((unused))
188size_t strlcat(char *dst, const char *src, size_t size)
189{
190	size_t len;
191	char c;
192
193	for (len = 0; dst[len];	len++)
194		;
195
196	for (;;) {
197		c = *src;
198		if (len < size)
199			dst[len] = c;
200		if (!c)
201			break;
202		len++;
203		src++;
204	}
205
206	return len;
207}
208
209static __attribute__((unused))
210size_t strlcpy(char *dst, const char *src, size_t size)
211{
212	size_t len;
213	char c;
214
215	for (len = 0;;) {
216		c = src[len];
217		if (len < size)
218			dst[len] = c;
219		if (!c)
220			break;
221		len++;
222	}
223	return len;
224}
225
226static __attribute__((unused))
227char *strncat(char *dst, const char *src, size_t size)
228{
229	char *orig = dst;
230
231	while (*dst)
232		dst++;
233
234	while (size && (*dst = *src)) {
235		src++;
236		dst++;
237		size--;
238	}
239
240	*dst = 0;
241	return orig;
242}
243
244static __attribute__((unused))
245int strncmp(const char *a, const char *b, size_t size)
246{
247	unsigned int c;
248	int diff = 0;
249
250	while (size-- &&
251	       !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
252		;
253
254	return diff;
255}
256
257static __attribute__((unused))
258char *strncpy(char *dst, const char *src, size_t size)
259{
260	size_t len;
261
262	for (len = 0; len < size; len++)
263		if ((dst[len] = *src))
264			src++;
265	return dst;
266}
267
268static __attribute__((unused))
269char *strrchr(const char *s, int c)
270{
271	const char *ret = NULL;
272
273	while (*s) {
274		if (*s == (char)c)
275			ret = s;
276		s++;
277	}
278	return (char *)ret;
279}
280
281/* make sure to include all global symbols */
282#include "nolibc.h"
283
284#endif /* _NOLIBC_STRING_H */
v6.2
  1/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2/*
  3 * string function definitions for NOLIBC
  4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
  5 */
  6
  7#ifndef _NOLIBC_STRING_H
  8#define _NOLIBC_STRING_H
  9
 10#include "std.h"
 11
 12static void *malloc(size_t len);
 13
 14/*
 15 * As much as possible, please keep functions alphabetically sorted.
 16 */
 17
 18static __attribute__((unused))
 19int memcmp(const void *s1, const void *s2, size_t n)
 20{
 21	size_t ofs = 0;
 22	int c1 = 0;
 23
 24	while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
 25		ofs++;
 26	}
 27	return c1;
 28}
 29
 30static __attribute__((unused))
 31void *_nolibc_memcpy_up(void *dst, const void *src, size_t len)
 32{
 33	size_t pos = 0;
 34
 35	while (pos < len) {
 36		((char *)dst)[pos] = ((const char *)src)[pos];
 37		pos++;
 38	}
 39	return dst;
 40}
 41
 42static __attribute__((unused))
 43void *_nolibc_memcpy_down(void *dst, const void *src, size_t len)
 44{
 45	while (len) {
 46		len--;
 47		((char *)dst)[len] = ((const char *)src)[len];
 48	}
 49	return dst;
 50}
 51
 52/* might be ignored by the compiler without -ffreestanding, then found as
 53 * missing.
 54 */
 55__attribute__((weak,unused,section(".text.nolibc_memmove")))
 56void *memmove(void *dst, const void *src, size_t len)
 57{
 58	size_t dir, pos;
 59
 60	pos = len;
 61	dir = -1;
 62
 63	if (dst < src) {
 64		pos = -1;
 65		dir = 1;
 66	}
 67
 68	while (len) {
 69		pos += dir;
 70		((char *)dst)[pos] = ((const char *)src)[pos];
 71		len--;
 72	}
 73	return dst;
 74}
 
 75
 
 76/* must be exported, as it's used by libgcc on ARM */
 77__attribute__((weak,unused,section(".text.nolibc_memcpy")))
 78void *memcpy(void *dst, const void *src, size_t len)
 79{
 80	return _nolibc_memcpy_up(dst, src, len);
 
 
 
 
 
 
 81}
 
 82
 
 83/* might be ignored by the compiler without -ffreestanding, then found as
 84 * missing.
 85 */
 86__attribute__((weak,unused,section(".text.nolibc_memset")))
 87void *memset(void *dst, int b, size_t len)
 88{
 89	char *p = dst;
 90
 91	while (len--) {
 92		/* prevent gcc from recognizing memset() here */
 93		asm volatile("");
 94		*(p++) = b;
 95	}
 96	return dst;
 97}
 
 98
 99static __attribute__((unused))
100char *strchr(const char *s, int c)
101{
102	while (*s) {
103		if (*s == (char)c)
104			return (char *)s;
105		s++;
106	}
107	return NULL;
108}
109
110static __attribute__((unused))
111int strcmp(const char *a, const char *b)
112{
113	unsigned int c;
114	int diff;
115
116	while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
117		;
118	return diff;
119}
120
121static __attribute__((unused))
122char *strcpy(char *dst, const char *src)
123{
124	char *ret = dst;
125
126	while ((*dst++ = *src++));
127	return ret;
128}
129
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.
135 */
136static __attribute__((unused))
137size_t strlen(const char *str)
138{
139	size_t len;
140
141	for (len = 0; str[len]; len++)
142		asm("");
143	return len;
144}
145
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().
148 */
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));           \
155})
156#endif
157
158static __attribute__((unused))
159size_t strnlen(const char *str, size_t maxlen)
160{
161	size_t len;
162
163	for (len = 0; (len < maxlen) && str[len]; len++);
164	return len;
165}
166
167static __attribute__((unused))
168char *strdup(const char *str)
169{
170	size_t len;
171	char *ret;
172
173	len = strlen(str);
174	ret = malloc(len + 1);
175	if (__builtin_expect(ret != NULL, 1))
176		memcpy(ret, str, len + 1);
177
178	return ret;
179}
180
181static __attribute__((unused))
182char *strndup(const char *str, size_t maxlen)
183{
184	size_t len;
185	char *ret;
186
187	len = strnlen(str, maxlen);
188	ret = malloc(len + 1);
189	if (__builtin_expect(ret != NULL, 1)) {
190		memcpy(ret, str, len);
191		ret[len] = '\0';
192	}
193
194	return ret;
195}
196
197static __attribute__((unused))
198size_t strlcat(char *dst, const char *src, size_t size)
199{
200	size_t len;
201	char c;
202
203	for (len = 0; dst[len];	len++)
204		;
205
206	for (;;) {
207		c = *src;
208		if (len < size)
209			dst[len] = c;
210		if (!c)
211			break;
212		len++;
213		src++;
214	}
215
216	return len;
217}
218
219static __attribute__((unused))
220size_t strlcpy(char *dst, const char *src, size_t size)
221{
222	size_t len;
223	char c;
224
225	for (len = 0;;) {
226		c = src[len];
227		if (len < size)
228			dst[len] = c;
229		if (!c)
230			break;
231		len++;
232	}
233	return len;
234}
235
236static __attribute__((unused))
237char *strncat(char *dst, const char *src, size_t size)
238{
239	char *orig = dst;
240
241	while (*dst)
242		dst++;
243
244	while (size && (*dst = *src)) {
245		src++;
246		dst++;
247		size--;
248	}
249
250	*dst = 0;
251	return orig;
252}
253
254static __attribute__((unused))
255int strncmp(const char *a, const char *b, size_t size)
256{
257	unsigned int c;
258	int diff = 0;
259
260	while (size-- &&
261	       !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
262		;
263
264	return diff;
265}
266
267static __attribute__((unused))
268char *strncpy(char *dst, const char *src, size_t size)
269{
270	size_t len;
271
272	for (len = 0; len < size; len++)
273		if ((dst[len] = *src))
274			src++;
275	return dst;
276}
277
278static __attribute__((unused))
279char *strrchr(const char *s, int c)
280{
281	const char *ret = NULL;
282
283	while (*s) {
284		if (*s == (char)c)
285			ret = s;
286		s++;
287	}
288	return (char *)ret;
289}
290
291/* make sure to include all global symbols */
292#include "nolibc.h"
293
294#endif /* _NOLIBC_STRING_H */