xref: /freebsd/sys/compat/linuxkpi/common/include/linux/string.h (revision ade8a27ea4c28d12fabc2d5f8e44386a3add23d1)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef	_LINUXKPI_LINUX_STRING_H_
30 #define	_LINUXKPI_LINUX_STRING_H_
31 
32 #include <sys/ctype.h>
33 
34 #include <linux/types.h>
35 #include <linux/gfp.h>
36 #include <linux/slab.h>
37 #include <linux/uaccess.h>
38 #include <linux/err.h>
39 #include <linux/bitops.h> /* for BITS_PER_LONG */
40 #include <linux/overflow.h>
41 #include <linux/stdarg.h>
42 
43 #include <sys/libkern.h>
44 
45 #define	strnicmp(...) strncasecmp(__VA_ARGS__)
46 
47 static inline int
48 match_string(const char *const *table, int n, const char *key)
49 {
50 	int i;
51 
52 	for (i = 0; i != n && table[i] != NULL; i++) {
53 		if (strcmp(table[i], key) == 0)
54 			return (i);
55 	}
56 	return (-EINVAL);
57 }
58 
59 static inline void *
60 memdup_user(const void *ptr, size_t len)
61 {
62 	void *retval;
63 	int error;
64 
65 	retval = malloc(len, M_KMALLOC, M_WAITOK);
66 	error = linux_copyin(ptr, retval, len);
67 	if (error != 0) {
68 		free(retval, M_KMALLOC);
69 		return (ERR_PTR(error));
70 	}
71 	return (retval);
72 }
73 
74 static inline void *
75 memdup_user_nul(const void *ptr, size_t len)
76 {
77 	char *retval;
78 	int error;
79 
80 	retval = malloc(len + 1, M_KMALLOC, M_WAITOK);
81 	error = linux_copyin(ptr, retval, len);
82 	if (error != 0) {
83 		free(retval, M_KMALLOC);
84 		return (ERR_PTR(error));
85 	}
86 	retval[len] = '\0';
87 	return (retval);
88 }
89 
90 static inline void *
91 kmemdup(const void *src, size_t len, gfp_t gfp)
92 {
93 	void *dst;
94 
95 	dst = kmalloc(len, gfp);
96 	if (dst != NULL)
97 		memcpy(dst, src, len);
98 	return (dst);
99 }
100 
101 static inline void *
102 kmemdup_array(const void *src, size_t count, size_t element_size, gfp_t gfp)
103 {
104 	return (kmemdup(src, size_mul(count, element_size), gfp));
105 }
106 
107 /* See slab.h for kvmalloc/kvfree(). */
108 static inline void *
109 kvmemdup(const void *src, size_t len, gfp_t gfp)
110 {
111 	void *dst;
112 
113 	dst = kvmalloc(len, gfp);
114 	if (dst != NULL)
115 		memcpy(dst, src, len);
116 	return (dst);
117 }
118 
119 static inline char *
120 strndup_user(const char __user *ustr, long n)
121 {
122 	if (n < 1)
123 		return (ERR_PTR(-EINVAL));
124 
125 	return (memdup_user_nul(ustr, n - 1));
126 }
127 
128 static inline char *
129 kstrdup(const char *string, gfp_t gfp)
130 {
131 	char *retval;
132 	size_t len;
133 
134 	if (string == NULL)
135 		return (NULL);
136 	len = strlen(string) + 1;
137 	retval = kmalloc(len, gfp);
138 	if (retval != NULL)
139 		memcpy(retval, string, len);
140 	return (retval);
141 }
142 
143 static inline char *
144 kstrndup(const char *string, size_t len, gfp_t gfp)
145 {
146 	char *retval;
147 
148 	if (string == NULL)
149 		return (NULL);
150 	retval = kmalloc(len + 1, gfp);
151 	if (retval != NULL)
152 		strncpy(retval, string, len);
153 	return (retval);
154 }
155 
156 static inline const char *
157 kstrdup_const(const char *src, gfp_t gfp)
158 {
159 	return (kmemdup(src, strlen(src) + 1, gfp));
160 }
161 
162 static inline char *
163 skip_spaces(const char *str)
164 {
165 	while (isspace(*str))
166 		++str;
167 	return (__DECONST(char *, str));
168 }
169 
170 /*
171  * This function trims whitespaces at the end of a string and returns a pointer
172  * to the first non-whitespace character.
173  */
174 static inline char *
175 strim(char *str)
176 {
177 	char *end;
178 
179 	end = str + strlen(str);
180 	while (end >= str && (*end == '\0' || isspace(*end))) {
181 		*end = '\0';
182 		end--;
183 	}
184 
185 	return (skip_spaces(str));
186 }
187 
188 static inline void *
189 memchr_inv(const void *start, int c, size_t length)
190 {
191 	const u8 *ptr;
192 	const u8 *end;
193 	u8 ch;
194 
195 	ch = c;
196 	ptr = start;
197 	end = ptr + length;
198 
199 	while (ptr != end) {
200 		if (*ptr != ch)
201 			return (__DECONST(void *, ptr));
202 		ptr++;
203 	}
204 	return (NULL);
205 }
206 
207 static inline bool
208 mem_is_zero(const void *start, size_t length)
209 {
210 	return (memchr_inv(start, 0, length) == NULL);
211 }
212 
213 static inline size_t
214 str_has_prefix(const char *str, const char *prefix)
215 {
216 	size_t len;
217 
218 	len = strlen(prefix);
219 	return (strncmp(str, prefix, len) == 0 ? len : 0);
220 }
221 
222 static inline char *
223 strreplace(char *str, char old, char new)
224 {
225 	char *p;
226 
227 	p = strchrnul(str, old);
228 	while (p != NULL && *p != '\0') {
229 		*p = new;
230 		p = strchrnul(str, old);
231 	}
232 	return (p);
233 }
234 
235 static inline ssize_t
236 strscpy(char* dst, const char* src, size_t len)
237 {
238 	size_t i;
239 
240 	if (len <= INT_MAX) {
241 		for (i = 0; i < len; i++)
242 			if ('\0' == (dst[i] = src[i]))
243 				return ((ssize_t)i);
244 		if (i != 0)
245 			dst[--i] = '\0';
246 	}
247 
248 	return (-E2BIG);
249 }
250 
251 static inline ssize_t
252 strscpy_pad(char* dst, const char* src, size_t len)
253 {
254 
255 	bzero(dst, len);
256 
257 	return (strscpy(dst, src, len));
258 }
259 
260 static inline char *
261 strnchr(const char *cp, size_t n, int ch)
262 {
263 	char *p;
264 
265 	for (p = __DECONST(char *, cp); n--; ++p) {
266 		if (*p == ch)
267 			return (p);
268 		if (*p == '\0')
269 			break;
270 	}
271 
272 	return (NULL);
273 }
274 
275 static inline void *
276 memset32(uint32_t *b, uint32_t c, size_t len)
277 {
278 	uint32_t *dst = b;
279 
280 	while (len--)
281 		*dst++ = c;
282 	return (b);
283 }
284 
285 static inline void *
286 memset64(uint64_t *b, uint64_t c, size_t len)
287 {
288 	uint64_t *dst = b;
289 
290 	while (len--)
291 		*dst++ = c;
292 	return (b);
293 }
294 
295 static inline void *
296 memset_p(void **p, void *v, size_t n)
297 {
298 
299 	if (BITS_PER_LONG == 32)
300 		return (memset32((uint32_t *)p, (uintptr_t)v, n));
301 	else
302 		return (memset64((uint64_t *)p, (uintptr_t)v, n));
303 }
304 
305 static inline void
306 memcpy_and_pad(void *dst, size_t dstlen, const void *src, size_t len, int ch)
307 {
308 
309 	if (len >= dstlen) {
310 		memcpy(dst, src, dstlen);
311 	} else {
312 		memcpy(dst, src, len);
313 		/* Pad with given padding character. */
314 		memset((char *)dst + len, ch, dstlen - len);
315 	}
316 }
317 
318 #define strtomem(dst, src)	do {					\
319 	size_t dstlen = ARRAY_SIZE(dst);				\
320 	size_t srclen = __builtin_object_size(src, 1);			\
321 	srclen = MIN(srclen, dstlen);					\
322 	srclen = strnlen(src, srclen);					\
323 	memcpy(dst, src, srclen);					\
324 } while (0)
325 
326 #define strtomem_pad(dst, src, pad)	do {				\
327 	size_t dstlen = ARRAY_SIZE(dst);				\
328 	size_t srclen = __builtin_object_size(src, 1);			\
329 	srclen = MIN(srclen, dstlen);					\
330 	srclen = strnlen(src, srclen);					\
331 	memcpy_and_pad(dst, dstlen, src, srclen, pad);			\
332 } while (0)
333 
334 #define	memset_startat(ptr, bytepat, smember)				\
335 ({									\
336 	uint8_t *_ptr = (uint8_t *)(ptr);				\
337 	int _c = (int)(bytepat);					\
338 	size_t _o = offsetof(typeof(*(ptr)), smember);			\
339 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
340 })
341 
342 #define	memset_after(ptr, bytepat, smember)				\
343 ({									\
344 	uint8_t *_ptr = (uint8_t *)(ptr);				\
345 	int _c = (int)(bytepat);					\
346 	size_t _o = offsetofend(typeof(*(ptr)), smember);		\
347 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
348 })
349 
350 static inline void
351 memzero_explicit(void *p, size_t s)
352 {
353 	memset(p, 0, s);
354 	__asm__ __volatile__("": :"r"(p) :"memory");
355 }
356 
357 #endif	/* _LINUXKPI_LINUX_STRING_H_ */
358