1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 1994-2009  Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34 FUNCTION
35 	<<memchr>>---find character in memory
36 
37 INDEX
38 	memchr
39 
40 ANSI_SYNOPSIS
41 	#include <string.h>
42 	void *memchr(const void *<[src]>, int <[c]>, size_t <[length]>);
43 
44 TRAD_SYNOPSIS
45 	#include <string.h>
46 	void *memchr(<[src]>, <[c]>, <[length]>)
47 	void *<[src]>;
48 	void *<[c]>;
49 	size_t <[length]>;
50 
51 DESCRIPTION
52 	This function searches memory starting at <<*<[src]>>> for the
53 	character <[c]>.  The search only ends with the first
54 	occurrence of <[c]>, or after <[length]> characters; in
55 	particular, <<NUL>> does not terminate the search.
56 
57 RETURNS
58 	If the character <[c]> is found within <[length]> characters
59 	of <<*<[src]>>>, a pointer to the character is returned. If
60 	<[c]> is not found, then <<NULL>> is returned.
61 
62 PORTABILITY
63 <<memchr>> is ANSI C.
64 
65 <<memchr>> requires no supporting OS subroutines.
66 
67 QUICKREF
68 	memchr ansi pure
69 */
70 
71 #include "_ansi.h"
72 #include <string.h>
73 #include <limits.h>
74 
75 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
76 #define UNALIGNED(X) ((long)X & (sizeof(long) - 1))
77 
78 /* How many bytes are loaded each iteration of the word copy loop.  */
79 #define LBLOCKSIZE (sizeof(long))
80 
81 /* Threshhold for punting to the bytewise iterator.  */
82 #define TOO_SMALL(LEN)  ((LEN) < LBLOCKSIZE)
83 
84 #if LONG_MAX == 2147483647L
85 #define DETECTNULL(X) (((X) - 0x01010101L) & ~(X) & 0x80808080UL)
86 #else
87 #if LONG_MAX == 9223372036854775807L
88 /* Nonzero if X (a long int) contains a NULL byte. */
89 #define DETECTNULL(X) (((X) - 0x0101010101010101L) & ~(X) & \
90 		       0x8080808080808080UL)
91 #else
92 #error long int is not a 32bit or 64bit type.
93 #endif
94 #endif
95 
96 #ifndef DETECTNULL
97 #error long int is not a 32bit or 64bit byte
98 #endif
99 
100 /* DETECTCHAR returns nonzero if (long)X contains the byte used
101    to fill (long)MASK. */
102 #define DETECTCHAR(X, MASK) (DETECTNULL(X ^ MASK))
103 
104 _PTR
105 _DEFUN(memchr, (src_void, c, length), _CONST _PTR src_void _AND int c
106 	_AND size_t length)
107 {
108 	_CONST unsigned char *src = (_CONST unsigned char *)src_void;
109 	unsigned char d = c;
110 
111 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
112 	unsigned long *asrc;
113 	unsigned long mask;
114 	int i;
115 
116 	while (UNALIGNED(src)) {
117 		if (!length--)
118 			return NULL;
119 		if (*src == d)
120 			return (void *)src;
121 		src++;
122 	}
123 
124 	if (!TOO_SMALL(length)) {
125 		/* If we get this far, we know that length is large and src is
126 		   word-aligned. */
127 		/* The fast code reads the source one word at a time and only
128 		   performs the bytewise search on word-sized segments if they
129 		   contain the search character, which is detected by XORing
130 		   the word-sized segment with a word-sized block of the search
131 		   character and then detecting for the presence of NUL in the
132 		   result.  */
133 		asrc = (unsigned long *)src;
134 		mask = d << 8 | d;
135 		mask = mask << 16 | mask;
136 		for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
137 			mask = (mask << i) | mask;
138 
139 		while (length >= LBLOCKSIZE) {
140 			if (DETECTCHAR(*asrc, mask))
141 				break;
142 			length -= LBLOCKSIZE;
143 			asrc++;
144 		}
145 
146 		/* If there are fewer than LBLOCKSIZE characters left,
147 		   then we resort to the bytewise loop.  */
148 
149 		src = (unsigned char *)asrc;
150 	}
151 #endif /* not PREFER_SIZE_OVER_SPEED */
152 
153 	while (length--) {
154 		if (*src == d)
155 			return (void *)src;
156 		src++;
157 	}
158 
159 	return NULL;
160 }
161