1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strchr>>---search for character in string
20 
21 INDEX
22 	strchr
23 
24 SYNOPSIS
25 	#include <string.h>
26 	char * strchr(const char *<[string]>, int <[c]>);
27 
28 DESCRIPTION
29 	This function finds the first occurence of <[c]> (converted to
30 	a char) in the string pointed to by <[string]> (including the
31 	terminating null character).
32 
33 RETURNS
34 	Returns a pointer to the located character, or a null pointer
35 	if <[c]> does not occur in <[string]>.
36 
37 PORTABILITY
38 <<strchr>> is ANSI C.
39 
40 <<strchr>> requires no supporting OS subroutines.
41 
42 QUICKREF
43 	strchr ansi pure
44 */
45 
46 #include <string.h>
47 #include <limits.h>
48 #include <stdint.h>
49 
50 /* Nonzero if X is not aligned on a "long" boundary.  */
51 #define UNALIGNED(X) ((uintptr_t)X & (sizeof (long) - 1))
52 
53 /* How many bytes are loaded each iteration of the word copy loop.  */
54 #define LBLOCKSIZE (sizeof (long))
55 
56 #if LONG_MAX == 2147483647L
57 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
58 #else
59 #if LONG_MAX == 9223372036854775807L
60 /* Nonzero if X (a long int) contains a NULL byte. */
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66 
67 /* DETECTCHAR returns nonzero if (long)X contains the byte used
68    to fill (long)MASK. */
69 #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
70 
71 char *
strchr(const char * s1,int i)72 strchr (const char *s1,
73 	int i)
74 {
75   const unsigned char *s = (const unsigned char *)s1;
76   unsigned char c = i;
77 
78 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && \
79     !defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
80   unsigned long mask,j;
81   unsigned long *aligned_addr;
82 
83   /* Special case for finding 0.  */
84   if (!c)
85     {
86       while (UNALIGNED (s))
87         {
88           if (!*s)
89             return (char *) s;
90           s++;
91         }
92       /* Operate a word at a time.  */
93       aligned_addr = (unsigned long *) s;
94       while (!DETECTNULL (*aligned_addr))
95         aligned_addr++;
96       /* Found the end of string.  */
97       s = (const unsigned char *) aligned_addr;
98       while (*s)
99         s++;
100       return (char *) s;
101     }
102 
103   /* All other bytes.  Align the pointer, then search a long at a time.  */
104   while (UNALIGNED (s))
105     {
106       if (!*s)
107         return NULL;
108       if (*s == c)
109         return (char *) s;
110       s++;
111     }
112 
113   mask = c;
114   for (j = 8; j < LBLOCKSIZE * 8; j <<= 1)
115     mask = (mask << j) | mask;
116 
117   aligned_addr = (unsigned long *) s;
118   while (!DETECTNULL (*aligned_addr) && !DETECTCHAR (*aligned_addr, mask))
119     aligned_addr++;
120 
121   /* The block of bytes currently pointed to by aligned_addr
122      contains either a null or the target char, or both.  We
123      catch it using the bytewise search.  */
124 
125   s = (unsigned char *) aligned_addr;
126 
127 #endif /* not PREFER_SIZE_OVER_SPEED */
128 
129   while (*s && *s != c)
130     s++;
131   if (*s == c)
132     return (char *)s;
133   return NULL;
134 }
135