1 /* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
2 /*
3 FUNCTION
4         <<memccpy>>---copy memory regions with end-token check
5 
6 SYNOPSIS
7         #include <string.h>
8         void* memccpy(void *restrict <[out]>, const void *restrict <[in]>,
9                       int <[endchar]>, size_t <[n]>);
10 
11 DESCRIPTION
12         This function copies up to <[n]> bytes from the memory region
13         pointed to by <[in]> to the memory region pointed to by
14         <[out]>.  If a byte matching the <[endchar]> is encountered,
15 	the byte is copied and copying stops.
16 
17         If the regions overlap, the behavior is undefined.
18 
19 RETURNS
20         <<memccpy>> returns a pointer to the first byte following the
21 	<[endchar]> in the <[out]> region.  If no byte matching
22 	<[endchar]> was copied, then <<NULL>> is returned.
23 
24 PORTABILITY
25 <<memccpy>> is a GNU extension.
26 
27 <<memccpy>> requires no supporting OS subroutines.
28 
29 	*/
30 
31 #include <_ansi.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdint.h>
36 
37 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
38 #define UNALIGNED(X, Y) \
39   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
40 
41 /* How many bytes are copied each iteration of the word copy loop.  */
42 #define LITTLEBLOCKSIZE (sizeof (long))
43 
44 /* Threshhold for punting to the byte copier.  */
45 #define TOO_SMALL(LEN)  ((LEN) < LITTLEBLOCKSIZE)
46 
47 /* Macros for detecting endchar */
48 #if LONG_MAX == 2147483647L
49 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
50 #else
51 #if LONG_MAX == 9223372036854775807L
52 /* Nonzero if X (a long int) contains a NULL byte. */
53 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
54 #else
55 #error long int is not a 32bit or 64bit type.
56 #endif
57 #endif
58 
59 
60 void *
memccpy(void * __restrict dst0,const void * __restrict src0,int endchar0,size_t len0)61 memccpy (void *__restrict dst0,
62 	const void *__restrict src0,
63 	int endchar0,
64 	size_t len0)
65 {
66 
67 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
68     defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
69   void *ptr = NULL;
70   char *dst = (char *) dst0;
71   char *src = (char *) src0;
72   char endchar = endchar0 & 0xff;
73 
74   while (len0--)
75     {
76       if ((*dst++ = *src++) == endchar)
77         {
78           ptr = dst;
79           break;
80         }
81     }
82 
83   return ptr;
84 #else
85   void *ptr = NULL;
86   unsigned char *dst = dst0;
87   const unsigned char *src = src0;
88   long *aligned_dst;
89   const long *aligned_src;
90   unsigned char endchar = endchar0 & 0xff;
91 
92   /* If the size is small, or either SRC or DST is unaligned,
93      then punt into the byte copy loop.  This should be rare.  */
94   if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
95     {
96       unsigned int i;
97       unsigned long mask = 0;
98 
99       aligned_dst = (long*)dst;
100       aligned_src = (long*)src;
101 
102       /* The fast code reads the ASCII one word at a time and only
103          performs the bytewise search on word-sized segments if they
104          contain the search character, which is detected by XORing
105          the word-sized segment with a word-sized block of the search
106          character and then detecting for the presence of NULL in the
107          result.  */
108       for (i = 0; i < LITTLEBLOCKSIZE; i++)
109         mask = (mask << 8) + endchar;
110 
111 
112       /* Copy one long word at a time if possible.  */
113       while (len0 >= LITTLEBLOCKSIZE)
114         {
115           unsigned long buffer = (unsigned long)(*aligned_src);
116           buffer ^=  mask;
117           if (DETECTNULL (buffer))
118             break; /* endchar is found, go byte by byte from here */
119           *aligned_dst++ = *aligned_src++;
120           len0 -= LITTLEBLOCKSIZE;
121         }
122 
123        /* Pick up any residual with a byte copier.  */
124       dst = (unsigned char*)aligned_dst;
125       src = (unsigned char*)aligned_src;
126     }
127 
128   while (len0--)
129     {
130       if ((*dst++ = *src++) == endchar)
131         {
132           ptr = dst;
133           break;
134         }
135     }
136 
137   return ptr;
138 #endif /* not PREFER_SIZE_OVER_SPEED */
139 }
140