Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 1 | /* string.c - common string routines */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2014 Wind River Systems, Inc. |
| 5 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <string.h> |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | #include <sys/types.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 12 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 13 | /** |
| 14 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 15 | * @brief Copy a string |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 16 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 17 | * @return pointer to destination buffer <d> |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 18 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 19 | |
Andrew Boie | c2a91b1 | 2017-02-02 12:01:59 -0800 | [diff] [blame] | 20 | char *strcpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 21 | { |
| 22 | char *dest = d; |
| 23 | |
| 24 | while (*s != '\0') { |
| 25 | *d = *s; |
| 26 | d++; |
| 27 | s++; |
| 28 | } |
| 29 | |
| 30 | *d = '\0'; |
| 31 | |
| 32 | return dest; |
| 33 | } |
| 34 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 35 | /** |
| 36 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 37 | * @brief Copy part of a string |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 38 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 39 | * @return pointer to destination buffer <d> |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 40 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 41 | |
Andrew Boie | c2a91b1 | 2017-02-02 12:01:59 -0800 | [diff] [blame] | 42 | char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s, size_t n) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 43 | { |
| 44 | char *dest = d; |
| 45 | |
| 46 | while ((n > 0) && *s != '\0') { |
| 47 | *d = *s; |
| 48 | s++; |
| 49 | d++; |
| 50 | n--; |
| 51 | } |
| 52 | |
| 53 | while (n > 0) { |
| 54 | *d = '\0'; |
Peter Mitsis | 7637d81 | 2015-04-28 10:10:42 -0400 | [diff] [blame] | 55 | d++; |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 56 | n--; |
| 57 | } |
| 58 | |
| 59 | return dest; |
| 60 | } |
| 61 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 62 | /** |
| 63 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 64 | * @brief String scanning operation |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 65 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 66 | * @return pointer to 1st instance of found byte, or NULL if not found |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 67 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 68 | |
| 69 | char *strchr(const char *s, int c) |
| 70 | { |
| 71 | char tmp = (char) c; |
| 72 | |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 73 | while ((*s != tmp) && (*s != '\0')) { |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 74 | s++; |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 75 | } |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 76 | |
| 77 | return (*s == tmp) ? (char *) s : NULL; |
| 78 | } |
| 79 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 80 | /** |
| 81 | * |
Jaakko Hannikainen | 24a2fb1 | 2016-08-24 14:56:02 +0300 | [diff] [blame] | 82 | * @brief String scanning operation |
| 83 | * |
| 84 | * @return pointer to last instance of found byte, or NULL if not found |
| 85 | */ |
| 86 | |
| 87 | char *strrchr(const char *s, int c) |
| 88 | { |
| 89 | char *match = NULL; |
| 90 | |
| 91 | do { |
| 92 | if (*s == (char)c) { |
| 93 | match = (char *)s; |
| 94 | } |
| 95 | } while (*s++); |
| 96 | |
| 97 | return match; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 102 | * @brief Get string length |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 103 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 104 | * @return number of bytes in string <s> |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 105 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 106 | |
| 107 | size_t strlen(const char *s) |
| 108 | { |
| 109 | size_t n = 0; |
| 110 | |
| 111 | while (*s != '\0') { |
| 112 | s++; |
| 113 | n++; |
| 114 | } |
| 115 | |
| 116 | return n; |
| 117 | } |
| 118 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 119 | /** |
| 120 | * |
Timo Teräs | 55dc481 | 2018-09-26 13:02:41 +0300 | [diff] [blame] | 121 | * @brief Get fixed-size string length |
| 122 | * |
| 123 | * @return number of bytes in fixed-size string <s> |
| 124 | */ |
| 125 | |
| 126 | size_t strnlen(const char *s, size_t maxlen) |
| 127 | { |
| 128 | size_t n = 0; |
| 129 | |
| 130 | while (*s != '\0' && n < maxlen) { |
| 131 | s++; |
| 132 | n++; |
| 133 | } |
| 134 | |
| 135 | return n; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 140 | * @brief Compare two strings |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 141 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 142 | * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive # |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 143 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 144 | |
| 145 | int strcmp(const char *s1, const char *s2) |
| 146 | { |
| 147 | while ((*s1 == *s2) && (*s1 != '\0')) { |
| 148 | s1++; |
| 149 | s2++; |
| 150 | } |
| 151 | |
| 152 | return *s1 - *s2; |
| 153 | } |
| 154 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 155 | /** |
| 156 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 157 | * @brief Compare part of two strings |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 158 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 159 | * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive # |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 160 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 161 | |
| 162 | int strncmp(const char *s1, const char *s2, size_t n) |
| 163 | { |
| 164 | while ((n > 0) && (*s1 == *s2) && (*s1 != '\0')) { |
| 165 | s1++; |
| 166 | s2++; |
| 167 | n--; |
| 168 | } |
| 169 | |
| 170 | return (n == 0) ? 0 : (*s1 - *s2); |
| 171 | } |
| 172 | |
Siddharth Chandrasekaran | 0637595 | 2020-08-17 18:46:48 +0530 | [diff] [blame] | 173 | /** |
| 174 | * @brief Separate `str` by any char in `sep` and return NULL terminated |
| 175 | * sections. Consecutive `sep` chars in `str` are treated as a single |
| 176 | * separator. |
| 177 | * |
| 178 | * @return pointer to NULL terminated string or NULL on errors. |
| 179 | */ |
| 180 | char *strtok_r(char *str, const char *sep, char **state) |
| 181 | { |
| 182 | char *start, *end; |
| 183 | |
| 184 | start = str ? str : *state; |
| 185 | |
| 186 | /* skip leading delimiters */ |
| 187 | while (*start && strchr(sep, *start)) { |
| 188 | start++; |
| 189 | } |
| 190 | |
| 191 | if (*start == '\0') { |
| 192 | *state = start; |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | /* look for token chars */ |
| 197 | end = start; |
| 198 | while (*end && !strchr(sep, *end)) { |
| 199 | end++; |
| 200 | } |
| 201 | |
| 202 | if (*end != '\0') { |
| 203 | *end = '\0'; |
| 204 | *state = end + 1; |
| 205 | } else { |
| 206 | *state = end; |
| 207 | } |
| 208 | |
| 209 | return start; |
| 210 | } |
| 211 | |
Andrew Boie | c2a91b1 | 2017-02-02 12:01:59 -0800 | [diff] [blame] | 212 | char *strcat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src) |
Anas Nashif | 9d648eb | 2015-07-31 06:57:00 -0400 | [diff] [blame] | 213 | { |
| 214 | strcpy(dest + strlen(dest), src); |
| 215 | return dest; |
| 216 | } |
| 217 | |
Andrew Boie | c2a91b1 | 2017-02-02 12:01:59 -0800 | [diff] [blame] | 218 | char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src, |
| 219 | size_t n) |
Benjamin Walsh | f557d71 | 2016-04-11 17:14:47 -0400 | [diff] [blame] | 220 | { |
| 221 | char *orig_dest = dest; |
| 222 | size_t len = strlen(dest); |
| 223 | |
| 224 | dest += len; |
| 225 | while ((n-- > 0) && (*src != '\0')) { |
| 226 | *dest++ = *src++; |
| 227 | } |
| 228 | *dest = '\0'; |
| 229 | |
| 230 | return orig_dest; |
| 231 | } |
| 232 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 233 | /** |
| 234 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 235 | * @brief Compare two memory areas |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 236 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 237 | * @return negative # if <m1> < <m2>, 0 if <m1> == <m2>, else positive # |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 238 | */ |
Johan Hedberg | 7fc1c37 | 2015-05-19 11:36:53 +0300 | [diff] [blame] | 239 | int memcmp(const void *m1, const void *m2, size_t n) |
| 240 | { |
| 241 | const char *c1 = m1; |
| 242 | const char *c2 = m2; |
| 243 | |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 244 | if (!n) { |
Johan Hedberg | afffab1 | 2015-05-19 20:43:30 +0300 | [diff] [blame] | 245 | return 0; |
Anas Nashif | 4c32258 | 2019-06-04 10:52:23 -0400 | [diff] [blame] | 246 | } |
Johan Hedberg | afffab1 | 2015-05-19 20:43:30 +0300 | [diff] [blame] | 247 | |
| 248 | while ((--n > 0) && (*c1 == *c2)) { |
Johan Hedberg | 7fc1c37 | 2015-05-19 11:36:53 +0300 | [diff] [blame] | 249 | c1++; |
| 250 | c2++; |
Johan Hedberg | 7fc1c37 | 2015-05-19 11:36:53 +0300 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return *c1 - *c2; |
| 254 | } |
| 255 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 256 | /** |
| 257 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 258 | * @brief Copy bytes in memory with overlapping areas |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 259 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 260 | * @return pointer to destination buffer <d> |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 261 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 262 | |
| 263 | void *memmove(void *d, const void *s, size_t n) |
| 264 | { |
| 265 | char *dest = d; |
| 266 | const char *src = s; |
| 267 | |
Mark Ruvald Pedersen | d67096d | 2018-09-14 14:24:09 +0200 | [diff] [blame] | 268 | if ((size_t) (dest - src) < n) { |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 269 | /* |
| 270 | * The <src> buffer overlaps with the start of the <dest> buffer. |
| 271 | * Copy backwards to prevent the premature corruption of <src>. |
| 272 | */ |
| 273 | |
| 274 | while (n > 0) { |
| 275 | n--; |
| 276 | dest[n] = src[n]; |
| 277 | } |
| 278 | } else { |
| 279 | /* It is safe to perform a forward-copy */ |
| 280 | while (n > 0) { |
| 281 | *dest = *src; |
| 282 | dest++; |
| 283 | src++; |
| 284 | n--; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return d; |
| 289 | } |
| 290 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 291 | /** |
| 292 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 293 | * @brief Copy bytes in memory |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 294 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 295 | * @return pointer to start of destination buffer |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 296 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 297 | |
Andrew Boie | c2a91b1 | 2017-02-02 12:01:59 -0800 | [diff] [blame] | 298 | void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 299 | { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 300 | /* attempt word-sized copying only if buffers have identical alignment */ |
| 301 | |
| 302 | unsigned char *d_byte = (unsigned char *)d; |
| 303 | const unsigned char *s_byte = (const unsigned char *)s; |
Michael Hope | 5d55730 | 2021-06-25 20:17:36 +0200 | [diff] [blame^] | 304 | |
| 305 | #if !defined(CONFIG_MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE) |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 306 | const uintptr_t mask = sizeof(mem_word_t) - 1; |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 307 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 308 | if ((((uintptr_t)d ^ (uintptr_t)s_byte) & mask) == 0) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 309 | |
| 310 | /* do byte-sized copying until word-aligned or finished */ |
| 311 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 312 | while (((uintptr_t)d_byte) & mask) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 313 | if (n == 0) { |
| 314 | return d; |
| 315 | } |
| 316 | *(d_byte++) = *(s_byte++); |
| 317 | n--; |
Flavio Ceolin | 9fd4ea9 | 2021-03-24 16:39:15 -0700 | [diff] [blame] | 318 | } |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 319 | |
| 320 | /* do word-sized copying as long as possible */ |
| 321 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 322 | mem_word_t *d_word = (mem_word_t *)d_byte; |
| 323 | const mem_word_t *s_word = (const mem_word_t *)s_byte; |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 324 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 325 | while (n >= sizeof(mem_word_t)) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 326 | *(d_word++) = *(s_word++); |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 327 | n -= sizeof(mem_word_t); |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | d_byte = (unsigned char *)d_word; |
| 331 | s_byte = (unsigned char *)s_word; |
| 332 | } |
Michael Hope | 5d55730 | 2021-06-25 20:17:36 +0200 | [diff] [blame^] | 333 | #endif |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 334 | |
| 335 | /* do byte-sized copying until finished */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 336 | |
| 337 | while (n > 0) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 338 | *(d_byte++) = *(s_byte++); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 339 | n--; |
| 340 | } |
| 341 | |
| 342 | return d; |
| 343 | } |
| 344 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 345 | /** |
| 346 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 347 | * @brief Set bytes in memory |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 348 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 349 | * @return pointer to start of buffer |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 350 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 351 | |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 352 | void *memset(void *buf, int c, size_t n) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 353 | { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 354 | /* do byte-sized initialization until word-aligned or finished */ |
| 355 | |
| 356 | unsigned char *d_byte = (unsigned char *)buf; |
| 357 | unsigned char c_byte = (unsigned char)c; |
| 358 | |
Michael Hope | 5d55730 | 2021-06-25 20:17:36 +0200 | [diff] [blame^] | 359 | #if !defined(CONFIG_MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE) |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 360 | while (((uintptr_t)d_byte) & (sizeof(mem_word_t) - 1)) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 361 | if (n == 0) { |
| 362 | return buf; |
| 363 | } |
| 364 | *(d_byte++) = c_byte; |
| 365 | n--; |
Flavio Ceolin | 9fd4ea9 | 2021-03-24 16:39:15 -0700 | [diff] [blame] | 366 | } |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 367 | |
| 368 | /* do word-sized initialization as long as possible */ |
| 369 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 370 | mem_word_t *d_word = (mem_word_t *)d_byte; |
| 371 | mem_word_t c_word = (mem_word_t)c_byte; |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 372 | |
| 373 | c_word |= c_word << 8; |
| 374 | c_word |= c_word << 16; |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 375 | #if Z_MEM_WORD_T_WIDTH > 32 |
| 376 | c_word |= c_word << 32; |
| 377 | #endif |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 378 | |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 379 | while (n >= sizeof(mem_word_t)) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 380 | *(d_word++) = c_word; |
Nicolas Pitre | 03170c0 | 2019-05-27 19:26:46 -0400 | [diff] [blame] | 381 | n -= sizeof(mem_word_t); |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* do byte-sized initialization until finished */ |
| 385 | |
| 386 | d_byte = (unsigned char *)d_word; |
Michael Hope | 5d55730 | 2021-06-25 20:17:36 +0200 | [diff] [blame^] | 387 | #endif |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 388 | |
| 389 | while (n > 0) { |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 390 | *(d_byte++) = c_byte; |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 391 | n--; |
| 392 | } |
| 393 | |
Allan Stephens | b52a09f | 2015-05-25 13:50:16 -0400 | [diff] [blame] | 394 | return buf; |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 395 | } |
Jukka Rissanen | efed446 | 2015-08-14 15:03:59 +0300 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * |
| 399 | * @brief Scan byte in memory |
| 400 | * |
| 401 | * @return pointer to start of found byte |
| 402 | */ |
| 403 | |
Nicolas Pitre | ffab197 | 2019-07-08 23:02:59 -0400 | [diff] [blame] | 404 | void *memchr(const void *s, int c, size_t n) |
Jukka Rissanen | efed446 | 2015-08-14 15:03:59 +0300 | [diff] [blame] | 405 | { |
| 406 | if (n != 0) { |
| 407 | const unsigned char *p = s; |
| 408 | |
| 409 | do { |
Nicolas Pitre | ffab197 | 2019-07-08 23:02:59 -0400 | [diff] [blame] | 410 | if (*p++ == (unsigned char)c) { |
Jukka Rissanen | efed446 | 2015-08-14 15:03:59 +0300 | [diff] [blame] | 411 | return ((void *)(p - 1)); |
| 412 | } |
| 413 | |
| 414 | } while (--n != 0); |
| 415 | } |
| 416 | |
| 417 | return NULL; |
| 418 | } |