/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <string.h>

/** @addtogroup string */
/** @{ */

/**
*	Copies a memory range to an other.
*
*	The function copies \p len bytes from the memory area pointed by \p src
*	to the memory are pointed by \p dst and returns \p dst. The copying is
*	done in such way that is equivalent of first copying the source range
*	to a temporary buffer, distinct from both \p src and \p dst, then copying
*	the temporary to the \p dst area. That is, the memory ranges \p src and
*	\p dst may overlap.
*
*	@param	dst	Pointer to the destination memory range
*	@param	src	Pointer to the source memory range
*	@param	len	The number of bytes to copy
*	@return		The value of dst
*
*	@attention
*		The C99 standard specifies the prototype of this function as <br>
*		<i>void *memmove( void *dst, <b>const</b> void *src, size_t len ) </i><br>
*		but then it explicitely states that \p src and \p dst can overlap,
*		that is, the function has every right to overwrite the \p src area.
*		It is different from using memcpy() to fill a memory with a repetitive
*		pattern knowing that it performs a forward, byte-by-byte copy. That is
*		abuse and is against the standard that clearly specifies that the
*		source and destination areas of memcpy() must not overlap. In case
*		of memmove(), such a usage is actually allowed and in fact the only
*		reason to have memmove() on top of memcpy() is the overlap safe
*		copying. Therefore this library omits the <b>const</b> qualifier from
*		the second argument's type in the prototype. Therefore, this function
*		is not strictly standard compliant.
*/

void	*memmove( void *dst, void *src, size_t len )
{
char	*s, *d;

	s = (char *) src;
	d = (char *) dst;

	if ( (unsigned int) d <= (unsigned int) s ) {

		while ( len-- ) *d++ = *s++;
	}
	else {

		d += len;
		s += len;
		while ( len-- ) *--d = *--s;
	}

	return( dst );
}

/** @} */

