xref: /freebsd/sys/compat/linuxkpi/common/include/linux/string.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 /* See slab.h for kvmalloc/kvfree(). */
102 static inline void *
103 kvmemdup(const void *src, size_t len, gfp_t gfp)
104 {
105 	void *dst;
106 
107 	dst = kvmalloc(len, gfp);
108 	if (dst != NULL)
109 		memcpy(dst, src, len);
110 	return (dst);
111 }
112 
113 static inline char *
114 strndup_user(const char __user *ustr, long n)
115 {
116 	if (n < 1)
117 		return (ERR_PTR(-EINVAL));
118 
119 	return (memdup_user_nul(ustr, n - 1));
120 }
121 
122 static inline char *
123 kstrdup(const char *string, gfp_t gfp)
124 {
125 	char *retval;
126 	size_t len;
127 
128 	if (string == NULL)
129 		return (NULL);
130 	len = strlen(string) + 1;
131 	retval = kmalloc(len, gfp);
132 	if (retval != NULL)
133 		memcpy(retval, string, len);
134 	return (retval);
135 }
136 
137 static inline char *
138 kstrndup(const char *string, size_t len, gfp_t gfp)
139 {
140 	char *retval;
141 
142 	if (string == NULL)
143 		return (NULL);
144 	retval = kmalloc(len + 1, gfp);
145 	if (retval != NULL)
146 		strncpy(retval, string, len);
147 	return (retval);
148 }
149 
150 static inline const char *
151 kstrdup_const(const char *src, gfp_t gfp)
152 {
153 	return (kmemdup(src, strlen(src) + 1, gfp));
154 }
155 
156 static inline char *
157 skip_spaces(const char *str)
158 {
159 	while (isspace(*str))
160 		++str;
161 	return (__DECONST(char *, str));
162 }
163 
164 static inline void *
165 memchr_inv(const void *start, int c, size_t length)
166 {
167 	const u8 *ptr;
168 	const u8 *end;
169 	u8 ch;
170 
171 	ch = c;
172 	ptr = start;
173 	end = ptr + length;
174 
175 	while (ptr != end) {
176 		if (*ptr != ch)
177 			return (__DECONST(void *, ptr));
178 		ptr++;
179 	}
180 	return (NULL);
181 }
182 
183 static inline size_t
184 str_has_prefix(const char *str, const char *prefix)
185 {
186 	size_t len;
187 
188 	len = strlen(prefix);
189 	return (strncmp(str, prefix, len) == 0 ? len : 0);
190 }
191 
192 static inline char *
193 strreplace(char *str, char old, char new)
194 {
195 	char *p;
196 
197 	p = strchrnul(str, old);
198 	while (p != NULL && *p != '\0') {
199 		*p = new;
200 		p = strchrnul(str, old);
201 	}
202 	return (p);
203 }
204 
205 static inline ssize_t
206 strscpy(char* dst, const char* src, size_t len)
207 {
208 	size_t i;
209 
210 	if (len <= INT_MAX) {
211 		for (i = 0; i < len; i++)
212 			if ('\0' == (dst[i] = src[i]))
213 				return ((ssize_t)i);
214 		if (i != 0)
215 			dst[--i] = '\0';
216 	}
217 
218 	return (-E2BIG);
219 }
220 
221 static inline ssize_t
222 strscpy_pad(char* dst, const char* src, size_t len)
223 {
224 
225 	bzero(dst, len);
226 
227 	return (strscpy(dst, src, len));
228 }
229 
230 static inline char *
231 strnchr(const char *cp, size_t n, int ch)
232 {
233 	char *p;
234 
235 	for (p = __DECONST(char *, cp); n--; ++p) {
236 		if (*p == ch)
237 			return (p);
238 		if (*p == '\0')
239 			break;
240 	}
241 
242 	return (NULL);
243 }
244 
245 static inline void *
246 memset32(uint32_t *b, uint32_t c, size_t len)
247 {
248 	uint32_t *dst = b;
249 
250 	while (len--)
251 		*dst++ = c;
252 	return (b);
253 }
254 
255 static inline void *
256 memset64(uint64_t *b, uint64_t c, size_t len)
257 {
258 	uint64_t *dst = b;
259 
260 	while (len--)
261 		*dst++ = c;
262 	return (b);
263 }
264 
265 static inline void *
266 memset_p(void **p, void *v, size_t n)
267 {
268 
269 	if (BITS_PER_LONG == 32)
270 		return (memset32((uint32_t *)p, (uintptr_t)v, n));
271 	else
272 		return (memset64((uint64_t *)p, (uintptr_t)v, n));
273 }
274 
275 static inline void
276 memcpy_and_pad(void *dst, size_t dstlen, const void *src, size_t len, int ch)
277 {
278 
279 	if (len >= dstlen) {
280 		memcpy(dst, src, dstlen);
281 	} else {
282 		memcpy(dst, src, len);
283 		/* Pad with given padding character. */
284 		memset((char *)dst + len, ch, dstlen - len);
285 	}
286 }
287 
288 #define	memset_startat(ptr, bytepat, smember)				\
289 ({									\
290 	uint8_t *_ptr = (uint8_t *)(ptr);				\
291 	int _c = (int)(bytepat);					\
292 	size_t _o = offsetof(typeof(*(ptr)), smember);			\
293 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
294 })
295 
296 #define	memset_after(ptr, bytepat, smember)				\
297 ({									\
298 	uint8_t *_ptr = (uint8_t *)(ptr);				\
299 	int _c = (int)(bytepat);					\
300 	size_t _o = offsetofend(typeof(*(ptr)), smember);		\
301 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
302 })
303 
304 static inline void
305 memzero_explicit(void *p, size_t s)
306 {
307 	memset(p, 0, s);
308 	__asm__ __volatile__("": :"r"(p) :"memory");
309 }
310 
311 #endif	/* _LINUXKPI_LINUX_STRING_H_ */
312