1 /*
2 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <string.h>
8
memmove(void * dst,const void * src,size_t len)9 void *memmove(void *dst, const void *src, size_t len)
10 {
11 /*
12 * The following test makes use of unsigned arithmetic overflow to
13 * more efficiently test the condition !(src <= dst && dst < str+len).
14 * It also avoids the situation where the more explicit test would give
15 * incorrect results were the calculation str+len to overflow (though
16 * that issue is probably moot as such usage is probably undefined
17 * behaviour and a bug anyway.
18 */
19 if ((size_t)dst - (size_t)src >= len) {
20 /* destination not in source data, so can safely use memcpy */
21 return memcpy(dst, src, len);
22 } else {
23 /* copy backwards... */
24 const char *end = dst;
25 const char *s = (const char *)src + len;
26 char *d = (char *)dst + len;
27 while (d != end)
28 *--d = *--s;
29 }
30 return dst;
31 }
32