Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <stddef.h> /* size_t */ |
| 8 | |
| 9 | /* |
| 10 | * Fill @count bytes of memory pointed to by @dst with @val |
| 11 | */ |
| 12 | void *memset(void *dst, int val, size_t count) |
| 13 | { |
| 14 | char *ptr = dst; |
| 15 | |
| 16 | while (count--) |
| 17 | *ptr++ = val; |
| 18 | |
| 19 | return dst; |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | * Compare @len bytes of @s1 and @s2 |
| 24 | */ |
| 25 | int memcmp(const void *s1, const void *s2, size_t len) |
| 26 | { |
dp-arm | 731cfd0 | 2016-12-06 15:20:25 +0000 | [diff] [blame] | 27 | const unsigned char *s = s1; |
| 28 | const unsigned char *d = s2; |
| 29 | unsigned char sc; |
| 30 | unsigned char dc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 31 | |
| 32 | while (len--) { |
| 33 | sc = *s++; |
| 34 | dc = *d++; |
| 35 | if (sc - dc) |
| 36 | return (sc - dc); |
| 37 | } |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | /* |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 43 | * Copy @len bytes from @src to @dst |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 44 | */ |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 45 | void *memcpy(void *dst, const void *src, size_t len) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 46 | { |
| 47 | const char *s = src; |
| 48 | char *d = dst; |
| 49 | |
| 50 | while (len--) |
| 51 | *d++ = *s++; |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 52 | |
| 53 | return dst; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 57 | * Move @len bytes from @src to @dst |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 58 | */ |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 59 | void *memmove(void *dst, const void *src, size_t len) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 60 | { |
Jon Medhurst | c3810c8 | 2014-01-15 18:22:34 +0000 | [diff] [blame] | 61 | /* |
| 62 | * The following test makes use of unsigned arithmetic overflow to |
| 63 | * more efficiently test the condition !(src <= dst && dst < str+len). |
| 64 | * It also avoids the situation where the more explicit test would give |
| 65 | * incorrect results were the calculation str+len to overflow (though |
| 66 | * that issue is probably moot as such usage is probably undefined |
| 67 | * behaviour and a bug anyway. |
| 68 | */ |
| 69 | if ((size_t)dst - (size_t)src >= len) { |
| 70 | /* destination not in source data, so can safely use memcpy */ |
| 71 | return memcpy(dst, src, len); |
| 72 | } else { |
| 73 | /* copy backwards... */ |
| 74 | const char *end = dst; |
| 75 | const char *s = (const char *)src + len; |
| 76 | char *d = (char *)dst + len; |
| 77 | while (d != end) |
| 78 | *--d = *--s; |
| 79 | } |
| 80 | return dst; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Scan @len bytes of @src for value @c |
| 85 | */ |
| 86 | void *memchr(const void *src, int c, size_t len) |
| 87 | { |
| 88 | const char *s = src; |
| 89 | |
| 90 | while (len--) { |
| 91 | if (*s == c) |
| 92 | return (void *) s; |
| 93 | s++; |
| 94 | } |
| 95 | |
| 96 | return NULL; |
| 97 | } |