1238104Sdes/*
2238104Sdes *	memmove.c: memmove compat implementation.
3238104Sdes *
4238104Sdes *	Copyright (c) 2001-2008, NLnet Labs. All rights reserved.
5238104Sdes *
6238104Sdes * See LICENSE for the license.
7238104Sdes*/
8238104Sdes
9238104Sdes#include <ldns/config.h>
10238104Sdes#include <stdlib.h>
11238104Sdes
12238104Sdesvoid *memmove(void *dest, const void *src, size_t n);
13238104Sdes
14238104Sdesvoid *memmove(void *dest, const void *src, size_t n)
15238104Sdes{
16238104Sdes	uint8_t* from = (uint8_t*) src;
17238104Sdes	uint8_t* to = (uint8_t*) dest;
18238104Sdes
19238104Sdes	if (from == to || n == 0)
20238104Sdes		return dest;
21238104Sdes	if (to > from && to-from < (int)n) {
22238104Sdes		/* to overlaps with from */
23238104Sdes		/*  <from......>         */
24238104Sdes		/*         <to........>  */
25238104Sdes		/* copy in reverse, to avoid overwriting from */
26238104Sdes		int i;
27238104Sdes		for(i=n-1; i>=0; i--)
28238104Sdes			to[i] = from[i];
29238104Sdes		return dest;
30238104Sdes	}
31238104Sdes	if (from > to  && from-to < (int)n) {
32238104Sdes		/* to overlaps with from */
33238104Sdes		/*        <from......>   */
34238104Sdes		/*  <to........>         */
35238104Sdes		/* copy forwards, to avoid overwriting from */
36238104Sdes		size_t i;
37238104Sdes		for(i=0; i<n; i++)
38238104Sdes			to[i] = from[i];
39238104Sdes		return dest;
40238104Sdes	}
41238104Sdes	memcpy(dest, src, n);
42238104Sdes	return dest;
43238104Sdes}
44