blob: 65b62fde6636d8b9f0211d52851d7a2ce7afecc4 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
7#include <stddef.h> /* size_t */
8
9/*
10 * Fill @count bytes of memory pointed to by @dst with @val
11 */
12void *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 */
25int memcmp(const void *s1, const void *s2, size_t len)
26{
dp-arm731cfd02016-12-06 15:20:25 +000027 const unsigned char *s = s1;
28 const unsigned char *d = s2;
29 unsigned char sc;
30 unsigned char dc;
Achin Gupta4f6ad662013-10-25 09:08:21 +010031
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 Gupta4f6ad662013-10-25 09:08:21 +010042/*
Jon Medhurstc3810c82014-01-15 18:22:34 +000043 * Copy @len bytes from @src to @dst
Achin Gupta4f6ad662013-10-25 09:08:21 +010044 */
Jon Medhurstc3810c82014-01-15 18:22:34 +000045void *memcpy(void *dst, const void *src, size_t len)
Achin Gupta4f6ad662013-10-25 09:08:21 +010046{
47 const char *s = src;
48 char *d = dst;
49
50 while (len--)
51 *d++ = *s++;
Jon Medhurstc3810c82014-01-15 18:22:34 +000052
53 return dst;
Achin Gupta4f6ad662013-10-25 09:08:21 +010054}
55
56/*
Jon Medhurstc3810c82014-01-15 18:22:34 +000057 * Move @len bytes from @src to @dst
Achin Gupta4f6ad662013-10-25 09:08:21 +010058 */
Jon Medhurstc3810c82014-01-15 18:22:34 +000059void *memmove(void *dst, const void *src, size_t len)
Achin Gupta4f6ad662013-10-25 09:08:21 +010060{
Jon Medhurstc3810c82014-01-15 18:22:34 +000061 /*
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 Gupta4f6ad662013-10-25 09:08:21 +010081}
82
Achin Gupta4f6ad662013-10-25 09:08:21 +010083/*
84 * Scan @len bytes of @src for value @c
85 */
86void *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}