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