string.h revision 1.11 1 1.11 riastrad /* $NetBSD: string.h,v 1.11 2021/12/19 11:45:01 riastradh Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #ifndef _LINUX_STRING_H_
33 1.2 riastrad #define _LINUX_STRING_H_
34 1.2 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.2 riastrad #include <sys/cdefs.h>
37 1.6 riastrad #include <sys/errno.h>
38 1.2 riastrad #include <sys/null.h>
39 1.2 riastrad
40 1.3 riastrad #include <linux/slab.h>
41 1.3 riastrad
42 1.2 riastrad static inline void *
43 1.2 riastrad memchr_inv(const void *buffer, int c, size_t len)
44 1.2 riastrad {
45 1.2 riastrad const uint8_t byte = c; /* XXX lose */
46 1.2 riastrad const char *p;
47 1.2 riastrad
48 1.2 riastrad for (p = buffer; len-- > 0; p++)
49 1.2 riastrad if (*p != byte)
50 1.2 riastrad return __UNCONST(p);
51 1.2 riastrad
52 1.2 riastrad return NULL;
53 1.2 riastrad }
54 1.2 riastrad
55 1.3 riastrad static inline void *
56 1.3 riastrad kmemdup(const void *src, size_t len, gfp_t gfp)
57 1.3 riastrad {
58 1.3 riastrad void *dst;
59 1.3 riastrad
60 1.3 riastrad dst = kmalloc(len, gfp);
61 1.3 riastrad if (dst == NULL)
62 1.3 riastrad return NULL;
63 1.3 riastrad
64 1.3 riastrad (void)memcpy(dst, src, len);
65 1.3 riastrad return dst;
66 1.3 riastrad }
67 1.3 riastrad
68 1.4 riastrad static inline char *
69 1.4 riastrad kstrndup(const char *src, size_t maxlen, gfp_t gfp)
70 1.4 riastrad {
71 1.4 riastrad char *dst;
72 1.4 riastrad size_t len;
73 1.4 riastrad
74 1.4 riastrad if (src == NULL)
75 1.4 riastrad return NULL;
76 1.4 riastrad
77 1.4 riastrad len = strnlen(src, maxlen);
78 1.4 riastrad dst = kmalloc(len + 1, gfp);
79 1.4 riastrad if (dst == NULL)
80 1.4 riastrad return NULL;
81 1.4 riastrad
82 1.4 riastrad (void)memcpy(dst, src, len);
83 1.4 riastrad dst[len] = '\0';
84 1.4 riastrad
85 1.4 riastrad return dst;
86 1.4 riastrad }
87 1.4 riastrad
88 1.5 riastrad static inline char *
89 1.5 riastrad kstrdup(const char *src, gfp_t gfp)
90 1.5 riastrad {
91 1.5 riastrad
92 1.5 riastrad if (src == NULL)
93 1.5 riastrad return NULL;
94 1.5 riastrad return kstrndup(src, strlen(src), gfp);
95 1.5 riastrad }
96 1.5 riastrad
97 1.6 riastrad static inline ssize_t
98 1.6 riastrad strscpy(char *dst, const char *src, size_t dstsize)
99 1.6 riastrad {
100 1.6 riastrad size_t n = dstsize;
101 1.6 riastrad
102 1.6 riastrad /* If no space for a NUL terminator, fail. */
103 1.6 riastrad if (n == 0)
104 1.6 riastrad return -E2BIG;
105 1.6 riastrad
106 1.6 riastrad /* Copy until we get a NUL terminator or the end of the buffer. */
107 1.6 riastrad while ((*dst++ = *src++) != '\0') {
108 1.6 riastrad if (__predict_false(--n == 0)) {
109 1.6 riastrad dst[-1] = '\0'; /* NUL-terminate */
110 1.6 riastrad return -E2BIG;
111 1.6 riastrad }
112 1.6 riastrad }
113 1.6 riastrad
114 1.6 riastrad /* Return the number of bytes copied, excluding NUL. */
115 1.6 riastrad return dstsize - n;
116 1.6 riastrad }
117 1.6 riastrad
118 1.11 riastrad static inline void *
119 1.11 riastrad memset32(uint32_t *buf, uint32_t v, size_t n)
120 1.10 riastrad {
121 1.11 riastrad uint32_t *p = buf;
122 1.10 riastrad
123 1.10 riastrad while (n --> 0)
124 1.10 riastrad *p++ = v;
125 1.11 riastrad
126 1.11 riastrad return buf;
127 1.10 riastrad }
128 1.10 riastrad
129 1.11 riastrad static inline void *
130 1.11 riastrad memset64(uint64_t *buf, uint64_t v, size_t n)
131 1.7 riastrad {
132 1.11 riastrad uint64_t *p = buf;
133 1.7 riastrad
134 1.7 riastrad while (n --> 0)
135 1.7 riastrad *p++ = v;
136 1.11 riastrad
137 1.11 riastrad return buf;
138 1.7 riastrad }
139 1.7 riastrad
140 1.11 riastrad static inline void *
141 1.11 riastrad memset_p(void **buf, void *v, size_t n)
142 1.7 riastrad {
143 1.11 riastrad void **p = buf;
144 1.7 riastrad
145 1.7 riastrad while (n --> 0)
146 1.7 riastrad *p++ = v;
147 1.11 riastrad
148 1.11 riastrad return buf;
149 1.7 riastrad }
150 1.7 riastrad
151 1.8 riastrad #define str_has_prefix(str, prefix) strncmp(str, prefix, strlen(prefix))
152 1.8 riastrad
153 1.9 riastrad static inline int
154 1.9 riastrad match_string(const char *const *haystack, size_t n, const char *needle)
155 1.9 riastrad {
156 1.9 riastrad int i;
157 1.9 riastrad
158 1.9 riastrad for (i = 0; i < n; i++) {
159 1.9 riastrad if (haystack[i] == NULL)
160 1.9 riastrad break;
161 1.9 riastrad if (strcmp(haystack[i], needle) == 0)
162 1.9 riastrad return i;
163 1.9 riastrad if (i == INT_MAX)
164 1.9 riastrad break;
165 1.9 riastrad }
166 1.9 riastrad return -EINVAL;
167 1.9 riastrad }
168 1.9 riastrad
169 1.2 riastrad #endif /* _LINUX_STRING_H_ */
170