xref: /freebsd/sys/compat/linuxkpi/common/include/linux/string.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 
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 kstrdup(const char *string, gfp_t gfp)
103 {
104 	char *retval;
105 	size_t len;
106 
107 	if (string == NULL)
108 		return (NULL);
109 	len = strlen(string) + 1;
110 	retval = kmalloc(len, gfp);
111 	if (retval != NULL)
112 		memcpy(retval, string, len);
113 	return (retval);
114 }
115 
116 static inline char *
117 kstrndup(const char *string, size_t len, gfp_t gfp)
118 {
119 	char *retval;
120 
121 	if (string == NULL)
122 		return (NULL);
123 	retval = kmalloc(len + 1, gfp);
124 	if (retval != NULL)
125 		strncpy(retval, string, len);
126 	return (retval);
127 }
128 
129 static inline const char *
130 kstrdup_const(const char *src, gfp_t gfp)
131 {
132 	return (kmemdup(src, strlen(src) + 1, gfp));
133 }
134 
135 static inline char *
136 skip_spaces(const char *str)
137 {
138 	while (isspace(*str))
139 		++str;
140 	return (__DECONST(char *, str));
141 }
142 
143 static inline void *
144 memchr_inv(const void *start, int c, size_t length)
145 {
146 	const u8 *ptr;
147 	const u8 *end;
148 	u8 ch;
149 
150 	ch = c;
151 	ptr = start;
152 	end = ptr + length;
153 
154 	while (ptr != end) {
155 		if (*ptr != ch)
156 			return (__DECONST(void *, ptr));
157 		ptr++;
158 	}
159 	return (NULL);
160 }
161 
162 static inline size_t
163 str_has_prefix(const char *str, const char *prefix)
164 {
165 	size_t len;
166 
167 	len = strlen(prefix);
168 	return (strncmp(str, prefix, len) == 0 ? len : 0);
169 }
170 
171 static inline char *
172 strreplace(char *str, char old, char new)
173 {
174 	char *p;
175 
176 	p = strchrnul(str, old);
177 	while (p != NULL && *p != '\0') {
178 		*p = new;
179 		p = strchrnul(str, old);
180 	}
181 	return (p);
182 }
183 
184 static inline ssize_t
185 strscpy(char* dst, const char* src, size_t len)
186 {
187 	size_t i;
188 
189 	if (len <= INT_MAX) {
190 		for (i = 0; i < len; i++)
191 			if ('\0' == (dst[i] = src[i]))
192 				return ((ssize_t)i);
193 		if (i != 0)
194 			dst[--i] = '\0';
195 	}
196 
197 	return (-E2BIG);
198 }
199 
200 static inline void *
201 memset32(uint32_t *b, uint32_t c, size_t len)
202 {
203 	uint32_t *dst = b;
204 
205 	while (len--)
206 		*dst++ = c;
207 	return (b);
208 }
209 
210 static inline void *
211 memset64(uint64_t *b, uint64_t c, size_t len)
212 {
213 	uint64_t *dst = b;
214 
215 	while (len--)
216 		*dst++ = c;
217 	return (b);
218 }
219 
220 static inline void *
221 memset_p(void **p, void *v, size_t n)
222 {
223 
224 	if (BITS_PER_LONG == 32)
225 		return (memset32((uint32_t *)p, (uintptr_t)v, n));
226 	else
227 		return (memset64((uint64_t *)p, (uintptr_t)v, n));
228 }
229 
230 #endif	/* _LINUXKPI_LINUX_STRING_H_ */
231