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 void *ptr = NULL;
69 char *dst = (char *) dst0;
70 char *src = (char *) src0;
71 char endchar = endchar0 & 0xff;
72
73 while (len0--)
74 {
75 if ((*dst++ = *src++) == endchar)
76 {
77 ptr = dst;
78 break;
79 }
80 }
81
82 return ptr;
83 #else
84 void *ptr = NULL;
85 unsigned char *dst = dst0;
86 const unsigned char *src = src0;
87 long *aligned_dst;
88 const long *aligned_src;
89 unsigned char endchar = endchar0 & 0xff;
90
91 /* If the size is small, or either SRC or DST is unaligned,
92 then punt into the byte copy loop. This should be rare. */
93 if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
94 {
95 unsigned int i;
96 unsigned long mask = 0;
97
98 aligned_dst = (long*)dst;
99 aligned_src = (long*)src;
100
101 /* The fast code reads the ASCII one word at a time and only
102 performs the bytewise search on word-sized segments if they
103 contain the search character, which is detected by XORing
104 the word-sized segment with a word-sized block of the search
105 character and then detecting for the presence of NULL in the
106 result. */
107 for (i = 0; i < LITTLEBLOCKSIZE; i++)
108 mask = (mask << 8) + endchar;
109
110
111 /* Copy one long word at a time if possible. */
112 while (len0 >= LITTLEBLOCKSIZE)
113 {
114 unsigned long buffer = (unsigned long)(*aligned_src);
115 buffer ^= mask;
116 if (DETECTNULL (buffer))
117 break; /* endchar is found, go byte by byte from here */
118 *aligned_dst++ = *aligned_src++;
119 len0 -= LITTLEBLOCKSIZE;
120 }
121
122 /* Pick up any residual with a byte copier. */
123 dst = (unsigned char*)aligned_dst;
124 src = (unsigned char*)aligned_src;
125 }
126
127 while (len0--)
128 {
129 if ((*dst++ = *src++) == endchar)
130 {
131 ptr = dst;
132 break;
133 }
134 }
135
136 return ptr;
137 #endif /* not PREFER_SIZE_OVER_SPEED */
138 }
139