/*** *memcpy - Copy source buffer to destination buffer * *Purpose: * memcpy() copies a source memory buffer to a destination memory buffer. * This routine does NOT recognize overlapping buffers, and thus can lead * to propogation. * * For cases where propogation must be avoided, memmove() must be used. * *Entry: * void *dst = pointer to destination buffer * const void *src = pointer to source buffer * size_t count = number of bytes to copy * *Exit: * Returns a pointer to the destination buffer * *Exceptions: *******************************************************************************/ void * __cdecl memcpy ( void * dst, const void * src, size_t count ) { void * ret = dst; #if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) { extern void RtlMoveMemory( void *, const void *, size_t count ); RtlMoveMemory( dst, src, count ); } #else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ /* * copy from lower addresses to higher addresses */ while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst + 1; src = (char *)src + 1; } #endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ return(ret); } /*** *memmove - Copy source buffer to destination buffer * *Purpose: * memmove() copies a source memory buffer to a destination memory buffer. * This routine recognize overlapping buffers to avoid propogation. * For cases where propogation is not a problem, memcpy() can be used. * *Entry: * void *dst = pointer to destination buffer * const void *src = pointer to source buffer * size_t count = number of bytes to copy * *Exit: * Returns a pointer to the destination buffer * *Exceptions: *******************************************************************************/ void * __cdecl memmove ( void * dst, const void * src, size_t count ) { void * ret = dst; #if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) { extern void RtlMoveMemory( void *, const void *, size_t count ); RtlMoveMemory( dst, src, count ); } #else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ if (dst <= src || (char *)dst >= ((char *)src + count)) { /* * Non-Overlapping Buffers * copy from lower addresses to higher addresses */ while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst + 1; src = (char *)src + 1; } } else { /* * Overlapping Buffers * copy from higher addresses to lower addresses */ dst = (char *)dst + count - 1; src = (char *)src + count - 1; while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst - 1; src = (char *)src - 1; } } #endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ return(ret); } /*** *char *strcat(dst, src) - concatenate (append) one string to another * *Purpose: * Concatenates src onto the end of dest. Assumes enough * space in dest. * *Entry: * char *dst - string to which "src" is to be appended * const char *src - string to be appended to the end of "dst" * *Exit: * The address of "dst" * *Exceptions: * *******************************************************************************/ char * __cdecl strcat ( char * dst, const char * src ) { char * cp = dst; while( *cp ) cp++; /* find end of dst */ while( *cp++ = *src++ ) ; /* Copy src to end of dst */ return( dst ); /* return dst */ } /*** *char *strcpy(dst, src) - copy one string over another * *Purpose: * Copies the string src into the spot specified by * dest; assumes enough room. * *Entry: * char * dst - string over which "src" is to be copied * const char * src - string to be copied over "dst" * *Exit: * The address of "dst" * *Exceptions: *******************************************************************************/ char * __cdecl strcpy(char * dst, const char * src) { char * cp = dst; while( *cp++ = *src++ ) ; /* Copy src over dst */ return( dst ); } /*** *char *memset(dst, val, count) - sets "count" bytes at "dst" to "val" * *Purpose: * Sets the first "count" bytes of the memory starting * at "dst" to the character value "val". * *Entry: * void *dst - pointer to memory to fill with val * int val - value to put in dst bytes * size_t count - number of bytes of dst to fill * *Exit: * returns dst, with filled bytes * *Exceptions: * *******************************************************************************/ void * __cdecl memset ( void *dst, int val, size_t count ) { void *start = dst; #if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) { extern void RtlFillMemory( void *, size_t count, char ); RtlFillMemory( dst, count, (char)val ); } #else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ while (count--) { *(char *)dst = (char)val; dst = (char *)dst + 1; } #endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) || defined (_M_IA64) */ return(start); } /*** *char *strncpy(dest, source, count) - copy at most n characters * *Purpose: * Copies count characters from the source string to the * destination. If count is less than the length of source, * NO NULL CHARACTER is put onto the end of the copied string. * If count is greater than the length of sources, dest is padded * with null characters to length count. * * *Entry: * char *dest - pointer to destination * char *source - source string for copy * unsigned count - max number of characters to copy * *Exit: * returns dest * *Exceptions: * *******************************************************************************/ char * __cdecl strncpy ( char * dest, const char * source, size_t count ) { char *start = dest; while (count && (*dest++ = *source++)) /* copy string */ count--; if (count) /* pad out with zeroes */ while (--count) *dest++ = '/0'; return(start); } /*** *strlen - return the length of a null-terminated string * *Purpose: * Finds the length in bytes of the given string, not including * the final null character. * *Entry: * const char * str - string whose length is to be computed * *Exit: * length of the string "str", exclusive of the final null byte * *Exceptions: * *******************************************************************************/ size_t __cdecl strlen ( const char * str ) { const char *eos = str; while( *eos++ ) ; return( (int)(eos - str - 1) ); } /*** *int memcmp(buf1, buf2, count) - compare memory for lexical order * *Purpose: * Compares count bytes of memory starting at buf1 and buf2 * and find if equal or which one is first in lexical order. * *Entry: * void *buf1, *buf2 - pointers to memory sections to compare * size_t count - length of sections to compare * *Exit: * returns < 0 if buf1 < buf2 * returns 0 if buf1 == buf2 * returns > 0 if buf1 > buf2 * *Exceptions: * *******************************************************************************/ int __cdecl memcmp ( const void * buf1, const void * buf2, size_t count ) { if (!count) return(0); while ( --count && *(char *)buf1 == *(char *)buf2 ) { buf1 = (char *)buf1 + 1; buf2 = (char *)buf2 + 1; } return( *((unsigned char *)buf1) - *((unsigned char *)buf2) ); } /*** *strcmp - compare two strings, returning less than, equal to, or greater than * *Purpose: * STRCMP compares two strings and returns an integer * to indicate whether the first is less than the second, the two are * equal, or whether the first is greater than the second. * * Comparison is done byte by byte on an UNSIGNED basis, which is to * say that Null (0) is less than any other character (1-255). * *Entry: * const char * src - string for left-hand side of comparison * const char * dst - string for right-hand side of comparison * *Exit: * returns -1 if src < dst * returns 0 if src == dst * returns +1 if src > dst * *Exceptions: * *******************************************************************************/ int __cdecl strcmp ( const char * src, const char * dst ) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); }