1*b7579f77SDag-Erling Smørgrav /* 2*b7579f77SDag-Erling Smørgrav * memmove.c: memmove compat implementation. 3*b7579f77SDag-Erling Smørgrav * 4*b7579f77SDag-Erling Smørgrav * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5*b7579f77SDag-Erling Smørgrav * 6*b7579f77SDag-Erling Smørgrav * See LICENSE for the license. 7*b7579f77SDag-Erling Smørgrav */ 8*b7579f77SDag-Erling Smørgrav 9*b7579f77SDag-Erling Smørgrav #include <config.h> 10*b7579f77SDag-Erling Smørgrav #include <stdlib.h> 11*b7579f77SDag-Erling Smørgrav 12*b7579f77SDag-Erling Smørgrav void *memmove(void *dest, const void *src, size_t n); 13*b7579f77SDag-Erling Smørgrav 14*b7579f77SDag-Erling Smørgrav void *memmove(void *dest, const void *src, size_t n) 15*b7579f77SDag-Erling Smørgrav { 16*b7579f77SDag-Erling Smørgrav uint8_t* from = (uint8_t*) src; 17*b7579f77SDag-Erling Smørgrav uint8_t* to = (uint8_t*) dest; 18*b7579f77SDag-Erling Smørgrav 19*b7579f77SDag-Erling Smørgrav if (from == to || n == 0) 20*b7579f77SDag-Erling Smørgrav return dest; 21*b7579f77SDag-Erling Smørgrav if (to > from && to-from < (int)n) { 22*b7579f77SDag-Erling Smørgrav /* to overlaps with from */ 23*b7579f77SDag-Erling Smørgrav /* <from......> */ 24*b7579f77SDag-Erling Smørgrav /* <to........> */ 25*b7579f77SDag-Erling Smørgrav /* copy in reverse, to avoid overwriting from */ 26*b7579f77SDag-Erling Smørgrav int i; 27*b7579f77SDag-Erling Smørgrav for(i=n-1; i>=0; i--) 28*b7579f77SDag-Erling Smørgrav to[i] = from[i]; 29*b7579f77SDag-Erling Smørgrav return dest; 30*b7579f77SDag-Erling Smørgrav } 31*b7579f77SDag-Erling Smørgrav if (from > to && from-to < (int)n) { 32*b7579f77SDag-Erling Smørgrav /* to overlaps with from */ 33*b7579f77SDag-Erling Smørgrav /* <from......> */ 34*b7579f77SDag-Erling Smørgrav /* <to........> */ 35*b7579f77SDag-Erling Smørgrav /* copy forwards, to avoid overwriting from */ 36*b7579f77SDag-Erling Smørgrav size_t i; 37*b7579f77SDag-Erling Smørgrav for(i=0; i<n; i++) 38*b7579f77SDag-Erling Smørgrav to[i] = from[i]; 39*b7579f77SDag-Erling Smørgrav return dest; 40*b7579f77SDag-Erling Smørgrav } 41*b7579f77SDag-Erling Smørgrav memcpy(dest, src, n); 42*b7579f77SDag-Erling Smørgrav return dest; 43*b7579f77SDag-Erling Smørgrav } 44