1 /* Copyright (c) 2013 Yaakov Selkowitz <yselkowi@redhat.com> */
2 /*
3 FUNCTION
4 	<<rawmemchr>>---find character in memory
5 
6 INDEX
7 	rawmemchr
8 
9 SYNOPSIS
10 	#include <string.h>
11 	void *rawmemchr(const void *<[src]>, int <[c]>);
12 
13 DESCRIPTION
14 	This function searches memory starting at <<*<[src]>>> for the
15 	character <[c]>.  The search only ends with the first occurrence
16 	of <[c]>; in particular, <<NUL>> does not terminate the search.
17 	No bounds checking is performed, so this function should only
18 	be used when it is certain that the character <[c]> will be found.
19 
20 RETURNS
21 	A pointer to the first occurance of character <[c]>.
22 
23 PORTABILITY
24 <<rawmemchr>> is a GNU extension.
25 
26 <<rawmemchr>> requires no supporting OS subroutines.
27 
28 QUICKREF
29 	rawmemchr
30 */
31 
32 #include <_ansi.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdint.h>
36 
37 /* Nonzero if X is not aligned on a "long" boundary.  */
38 #define UNALIGNED(X) ((uintptr_t)X & (sizeof (long) - 1))
39 
40 /* How many bytes are loaded each iteration of the word copy loop.  */
41 #define LBLOCKSIZE (sizeof (long))
42 
43 /* Threshhold for punting to the bytewise iterator.  */
44 #define TOO_SMALL(LEN)  ((LEN) < LBLOCKSIZE)
45 
46 #if LONG_MAX == 2147483647L
47 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
48 #else
49 #if LONG_MAX == 9223372036854775807L
50 /* Nonzero if X (a long int) contains a NULL byte. */
51 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
52 #else
53 #error long int is not a 32bit or 64bit type.
54 #endif
55 #endif
56 
57 #ifndef DETECTNULL
58 #error long int is not a 32bit or 64bit byte
59 #endif
60 
61 /* DETECTCHAR returns nonzero if (long)X contains the byte used
62    to fill (long)MASK. */
63 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
64 
65 void *
rawmemchr(const void * src_void,int c)66 rawmemchr (const void *src_void,
67 	int c)
68 {
69   const unsigned char *src = (const unsigned char *) src_void;
70   unsigned char d = c;
71 
72 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
73   unsigned long *asrc;
74   unsigned long  mask;
75   unsigned int i;
76 
77   while (UNALIGNED (src))
78     {
79       if (*src == d)
80         return (void *) src;
81       src++;
82     }
83 
84   /* If we get this far, we know that src is word-aligned. */
85   /* The fast code reads the source one word at a time and only
86      performs the bytewise search on word-sized segments if they
87      contain the search character, which is detected by XORing
88      the word-sized segment with a word-sized block of the search
89      character and then detecting for the presence of NUL in the
90      result.  */
91   asrc = (unsigned long *) src;
92   mask = d << 8 | d;
93   mask = mask << 16 | mask;
94   for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
95     mask = (mask << i) | mask;
96 
97   while (1)
98     {
99       if (DETECTCHAR (*asrc, mask))
100         break;
101       asrc++;
102     }
103 
104   /* We have the matching word, now we resort to a bytewise loop. */
105 
106   src = (unsigned char *) asrc;
107 
108 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
109 
110   while (1)
111     {
112       if (*src == d)
113         return (void *) src;
114       src++;
115     }
116 }
117