2002-07-24 05:38:00 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<memccpy>>---copy memory regions with end-token check
|
|
|
|
|
2017-11-30 16:20:06 +08:00
|
|
|
SYNOPSIS
|
2002-07-24 05:38:00 +08:00
|
|
|
#include <string.h>
|
* libc/sys/linux/include/netdb.h, libc/sys/linux/net/getaddrinfo.c,
libc/sys/linux/net/getnameinfo.c: Add restrict keyword to getnameinfo()
and getaddrinfo() to increase standards compliance and match glibc.
* libc/include/string.h, libc/string/memccpy.c, libc/string/memcpy.c,
libc/string/stpcpy.c, libc/string/stpncpy.c, libc/string/strcat.c,
libc/string/strncat.c, libc/string/strncpy.c, libc/string/strtok.c,
libc/string/strtok_r.c, libc/string/strxfrm.c
libc/machine/microblaze/strcpy.c, libc/machine/xscale/memcpy.c,
libc/machine/cris/memcpy.c: Add __restrict to prototype to
increase standards compliance.
2013-07-23 15:05:31 +08:00
|
|
|
void* memccpy(void *restrict <[out]>, const void *restrict <[in]>,
|
2002-07-24 05:38:00 +08:00
|
|
|
int <[endchar]>, size_t <[n]>);
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This function copies up to <[n]> bytes from the memory region
|
|
|
|
pointed to by <[in]> to the memory region pointed to by
|
|
|
|
<[out]>. If a byte matching the <[endchar]> is encountered,
|
|
|
|
the byte is copied and copying stops.
|
|
|
|
|
|
|
|
If the regions overlap, the behavior is undefined.
|
|
|
|
|
|
|
|
RETURNS
|
|
|
|
<<memccpy>> returns a pointer to the first byte following the
|
|
|
|
<[endchar]> in the <[out]> region. If no byte matching
|
|
|
|
<[endchar]> was copied, then <<NULL>> is returned.
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<memccpy>> is a GNU extension.
|
|
|
|
|
|
|
|
<<memccpy>> requires no supporting OS subroutines.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <_ansi.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
/* Nonzero if either X or Y is not aligned on a "long" boundary. */
|
|
|
|
#define UNALIGNED(X, Y) \
|
|
|
|
(((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
|
|
|
|
|
|
|
|
/* How many bytes are copied each iteration of the word copy loop. */
|
|
|
|
#define LITTLEBLOCKSIZE (sizeof (long))
|
|
|
|
|
|
|
|
/* Threshhold for punting to the byte copier. */
|
|
|
|
#define TOO_SMALL(LEN) ((LEN) < LITTLEBLOCKSIZE)
|
|
|
|
|
|
|
|
/* Macros for detecting endchar */
|
|
|
|
#if LONG_MAX == 2147483647L
|
|
|
|
#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
|
|
|
|
#else
|
|
|
|
#if LONG_MAX == 9223372036854775807L
|
|
|
|
/* Nonzero if X (a long int) contains a NULL byte. */
|
|
|
|
#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
|
|
|
|
#else
|
|
|
|
#error long int is not a 32bit or 64bit type.
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-12-04 10:53:22 +08:00
|
|
|
void *
|
2017-12-04 11:43:30 +08:00
|
|
|
memccpy (void *__restrict dst0,
|
2017-12-04 10:53:22 +08:00
|
|
|
const void *__restrict src0,
|
2017-12-04 09:31:41 +08:00
|
|
|
int endchar0,
|
2002-07-24 05:38:00 +08:00
|
|
|
size_t len0)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
|
2017-12-04 10:53:22 +08:00
|
|
|
void *ptr = NULL;
|
2002-07-24 05:38:00 +08:00
|
|
|
char *dst = (char *) dst0;
|
|
|
|
char *src = (char *) src0;
|
|
|
|
char endchar = endchar0 & 0xff;
|
|
|
|
|
|
|
|
while (len0--)
|
|
|
|
{
|
|
|
|
if ((*dst++ = *src++) == endchar)
|
|
|
|
{
|
|
|
|
ptr = dst;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
#else
|
2017-12-04 10:53:22 +08:00
|
|
|
void *ptr = NULL;
|
2023-01-04 03:45:37 +08:00
|
|
|
unsigned char *dst = dst0;
|
|
|
|
const unsigned char *src = src0;
|
2002-07-24 05:38:00 +08:00
|
|
|
long *aligned_dst;
|
2017-12-04 10:25:16 +08:00
|
|
|
const long *aligned_src;
|
2023-01-04 03:45:37 +08:00
|
|
|
unsigned char endchar = endchar0 & 0xff;
|
2002-07-24 05:38:00 +08:00
|
|
|
|
|
|
|
/* If the size is small, or either SRC or DST is unaligned,
|
|
|
|
then punt into the byte copy loop. This should be rare. */
|
2010-09-22 11:15:07 +08:00
|
|
|
if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
|
2002-07-24 05:38:00 +08:00
|
|
|
{
|
2014-10-09 20:24:56 +08:00
|
|
|
unsigned int i;
|
2002-07-24 05:38:00 +08:00
|
|
|
unsigned long mask = 0;
|
|
|
|
|
|
|
|
aligned_dst = (long*)dst;
|
|
|
|
aligned_src = (long*)src;
|
|
|
|
|
|
|
|
/* The fast code reads the ASCII one word at a time and only
|
|
|
|
performs the bytewise search on word-sized segments if they
|
|
|
|
contain the search character, which is detected by XORing
|
|
|
|
the word-sized segment with a word-sized block of the search
|
|
|
|
character and then detecting for the presence of NULL in the
|
|
|
|
result. */
|
|
|
|
for (i = 0; i < LITTLEBLOCKSIZE; i++)
|
|
|
|
mask = (mask << 8) + endchar;
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy one long word at a time if possible. */
|
2010-09-22 11:15:07 +08:00
|
|
|
while (len0 >= LITTLEBLOCKSIZE)
|
2002-07-24 05:38:00 +08:00
|
|
|
{
|
|
|
|
unsigned long buffer = (unsigned long)(*aligned_src);
|
|
|
|
buffer ^= mask;
|
|
|
|
if (DETECTNULL (buffer))
|
|
|
|
break; /* endchar is found, go byte by byte from here */
|
|
|
|
*aligned_dst++ = *aligned_src++;
|
2010-09-22 11:15:07 +08:00
|
|
|
len0 -= LITTLEBLOCKSIZE;
|
2002-07-24 05:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pick up any residual with a byte copier. */
|
|
|
|
dst = (char*)aligned_dst;
|
|
|
|
src = (char*)aligned_src;
|
|
|
|
}
|
|
|
|
|
2010-09-22 11:15:07 +08:00
|
|
|
while (len0--)
|
2002-07-24 05:38:00 +08:00
|
|
|
{
|
|
|
|
if ((*dst++ = *src++) == endchar)
|
|
|
|
{
|
|
|
|
ptr = dst;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
#endif /* not PREFER_SIZE_OVER_SPEED */
|
|
|
|
}
|