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/stdarg.h>
41 
42 #include <sys/libkern.h>
43 
44 #define	strnicmp(...) strncasecmp(__VA_ARGS__)
45 
46 static inline int
47 match_string(const char *const *table, int n, const char *key)
48 {
49 	int i;
50 
51 	for (i = 0; i != n && table[i] != NULL; i++) {
52 		if (strcmp(table[i], key) == 0)
53 			return (i);
54 	}
55 	return (-EINVAL);
56 }
57 
58 static inline void *
59 memdup_user(const void *ptr, size_t len)
60 {
61 	void *retval;
62 	int error;
63 
64 	retval = malloc(len, M_KMALLOC, M_WAITOK);
65 	error = linux_copyin(ptr, retval, len);
66 	if (error != 0) {
67 		free(retval, M_KMALLOC);
68 		return (ERR_PTR(error));
69 	}
70 	return (retval);
71 }
72 
73 static inline void *
74 memdup_user_nul(const void *ptr, size_t len)
75 {
76 	char *retval;
77 	int error;
78 
79 	retval = malloc(len + 1, M_KMALLOC, M_WAITOK);
80 	error = linux_copyin(ptr, retval, len);
81 	if (error != 0) {
82 		free(retval, M_KMALLOC);
83 		return (ERR_PTR(error));
84 	}
85 	retval[len] = '\0';
86 	return (retval);
87 }
88 
89 static inline void *
90 kmemdup(const void *src, size_t len, gfp_t gfp)
91 {
92 	void *dst;
93 
94 	dst = kmalloc(len, gfp);
95 	if (dst != NULL)
96 		memcpy(dst, src, len);
97 	return (dst);
98 }
99 
100 static inline char *
101 strndup_user(const char __user *ustr, long n)
102 {
103 	if (n < 1)
104 		return (ERR_PTR(-EINVAL));
105 
106 	return (memdup_user_nul(ustr, n - 1));
107 }
108 
109 static inline char *
110 kstrdup(const char *string, gfp_t gfp)
111 {
112 	char *retval;
113 	size_t len;
114 
115 	if (string == NULL)
116 		return (NULL);
117 	len = strlen(string) + 1;
118 	retval = kmalloc(len, gfp);
119 	if (retval != NULL)
120 		memcpy(retval, string, len);
121 	return (retval);
122 }
123 
124 static inline char *
125 kstrndup(const char *string, size_t len, gfp_t gfp)
126 {
127 	char *retval;
128 
129 	if (string == NULL)
130 		return (NULL);
131 	retval = kmalloc(len + 1, gfp);
132 	if (retval != NULL)
133 		strncpy(retval, string, len);
134 	return (retval);
135 }
136 
137 static inline const char *
138 kstrdup_const(const char *src, gfp_t gfp)
139 {
140 	return (kmemdup(src, strlen(src) + 1, gfp));
141 }
142 
143 static inline char *
144 skip_spaces(const char *str)
145 {
146 	while (isspace(*str))
147 		++str;
148 	return (__DECONST(char *, str));
149 }
150 
151 static inline void *
152 memchr_inv(const void *start, int c, size_t length)
153 {
154 	const u8 *ptr;
155 	const u8 *end;
156 	u8 ch;
157 
158 	ch = c;
159 	ptr = start;
160 	end = ptr + length;
161 
162 	while (ptr != end) {
163 		if (*ptr != ch)
164 			return (__DECONST(void *, ptr));
165 		ptr++;
166 	}
167 	return (NULL);
168 }
169 
170 static inline size_t
171 str_has_prefix(const char *str, const char *prefix)
172 {
173 	size_t len;
174 
175 	len = strlen(prefix);
176 	return (strncmp(str, prefix, len) == 0 ? len : 0);
177 }
178 
179 static inline char *
180 strreplace(char *str, char old, char new)
181 {
182 	char *p;
183 
184 	p = strchrnul(str, old);
185 	while (p != NULL && *p != '\0') {
186 		*p = new;
187 		p = strchrnul(str, old);
188 	}
189 	return (p);
190 }
191 
192 static inline ssize_t
193 strscpy(char* dst, const char* src, size_t len)
194 {
195 	size_t i;
196 
197 	if (len <= INT_MAX) {
198 		for (i = 0; i < len; i++)
199 			if ('\0' == (dst[i] = src[i]))
200 				return ((ssize_t)i);
201 		if (i != 0)
202 			dst[--i] = '\0';
203 	}
204 
205 	return (-E2BIG);
206 }
207 
208 static inline ssize_t
209 strscpy_pad(char* dst, const char* src, size_t len)
210 {
211 
212 	bzero(dst, len);
213 
214 	return (strscpy(dst, src, len));
215 }
216 
217 static inline void *
218 memset32(uint32_t *b, uint32_t c, size_t len)
219 {
220 	uint32_t *dst = b;
221 
222 	while (len--)
223 		*dst++ = c;
224 	return (b);
225 }
226 
227 static inline void *
228 memset64(uint64_t *b, uint64_t c, size_t len)
229 {
230 	uint64_t *dst = b;
231 
232 	while (len--)
233 		*dst++ = c;
234 	return (b);
235 }
236 
237 static inline void *
238 memset_p(void **p, void *v, size_t n)
239 {
240 
241 	if (BITS_PER_LONG == 32)
242 		return (memset32((uint32_t *)p, (uintptr_t)v, n));
243 	else
244 		return (memset64((uint64_t *)p, (uintptr_t)v, n));
245 }
246 
247 static inline void
248 memcpy_and_pad(void *dst, size_t dstlen, const void *src, size_t len, int ch)
249 {
250 
251 	if (len >= dstlen) {
252 		memcpy(dst, src, dstlen);
253 	} else {
254 		memcpy(dst, src, len);
255 		/* Pad with given padding character. */
256 		memset((char *)dst + len, ch, dstlen - len);
257 	}
258 }
259 
260 #define	memset_startat(ptr, bytepat, smember)				\
261 ({									\
262 	uint8_t *_ptr = (uint8_t *)(ptr);				\
263 	int _c = (int)(bytepat);					\
264 	size_t _o = offsetof(typeof(*(ptr)), smember);			\
265 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
266 })
267 
268 #define	memset_after(ptr, bytepat, smember)				\
269 ({									\
270 	uint8_t *_ptr = (uint8_t *)(ptr);				\
271 	int _c = (int)(bytepat);					\
272 	size_t _o = offsetofend(typeof(*(ptr)), smember);		\
273 	memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);			\
274 })
275 
276 #endif	/* _LINUXKPI_LINUX_STRING_H_ */
277