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