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