xref: /illumos-gate/usr/src/common/util/memmem.c (revision 28de4f3c)
123a1cceaSRoger A. Faulkner /*
223a1cceaSRoger A. Faulkner  * CDDL HEADER START
323a1cceaSRoger A. Faulkner  *
423a1cceaSRoger A. Faulkner  * The contents of this file are subject to the terms of the
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
723a1cceaSRoger A. Faulkner  *
823a1cceaSRoger A. Faulkner  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923a1cceaSRoger A. Faulkner  * or http://www.opensolaris.org/os/licensing.
1023a1cceaSRoger A. Faulkner  * See the License for the specific language governing permissions
1123a1cceaSRoger A. Faulkner  * and limitations under the License.
1223a1cceaSRoger A. Faulkner  *
1323a1cceaSRoger A. Faulkner  * When distributing Covered Code, include this CDDL HEADER in each
1423a1cceaSRoger A. Faulkner  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523a1cceaSRoger A. Faulkner  * If applicable, add the following below this CDDL HEADER, with the
1623a1cceaSRoger A. Faulkner  * fields enclosed by brackets "[]" replaced with your own identifying
1723a1cceaSRoger A. Faulkner  * information: Portions Copyright [yyyy] [name of copyright owner]
1823a1cceaSRoger A. Faulkner  *
1923a1cceaSRoger A. Faulkner  * CDDL HEADER END
2023a1cceaSRoger A. Faulkner  */
2123a1cceaSRoger A. Faulkner 
2223a1cceaSRoger A. Faulkner /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2423a1cceaSRoger A. Faulkner  */
2523a1cceaSRoger A. Faulkner 
2623a1cceaSRoger A. Faulkner /*
2723a1cceaSRoger A. Faulkner  * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
2823a1cceaSRoger A. Faulkner  *
2923a1cceaSRoger A. Faulkner  * Redistribution and use in source and binary forms, with or without
3023a1cceaSRoger A. Faulkner  * modification, are permitted provided that the following conditions
3123a1cceaSRoger A. Faulkner  * are met:
3223a1cceaSRoger A. Faulkner  * 1. Redistributions of source code must retain the above copyright
3323a1cceaSRoger A. Faulkner  *    notice, this list of conditions and the following disclaimer.
3423a1cceaSRoger A. Faulkner  * 2. Redistributions in binary form must reproduce the above copyright
3523a1cceaSRoger A. Faulkner  *    notice, this list of conditions and the following disclaimer in the
3623a1cceaSRoger A. Faulkner  *    documentation and/or other materials provided with the distribution.
3723a1cceaSRoger A. Faulkner  * 3. The name of the author may not be used to endorse or promote
3823a1cceaSRoger A. Faulkner  *    products derived from this software without specific prior written
3923a1cceaSRoger A. Faulkner  *    permission.
4023a1cceaSRoger A. Faulkner  *
4123a1cceaSRoger A. Faulkner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4223a1cceaSRoger A. Faulkner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4323a1cceaSRoger A. Faulkner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4423a1cceaSRoger A. Faulkner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4523a1cceaSRoger A. Faulkner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4623a1cceaSRoger A. Faulkner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4723a1cceaSRoger A. Faulkner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4823a1cceaSRoger A. Faulkner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4923a1cceaSRoger A. Faulkner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5023a1cceaSRoger A. Faulkner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5123a1cceaSRoger A. Faulkner  * SUCH DAMAGE.
5223a1cceaSRoger A. Faulkner  */
5323a1cceaSRoger A. Faulkner 
5423a1cceaSRoger A. Faulkner #include <string.h>
5523a1cceaSRoger A. Faulkner 
5623a1cceaSRoger A. Faulkner /*
5723a1cceaSRoger A. Faulkner  * Find the first occurrence of the byte string s in byte string l.
5823a1cceaSRoger A. Faulkner  */
5923a1cceaSRoger A. Faulkner 
6023a1cceaSRoger A. Faulkner void *
memmem(const void * l,size_t l_len,const void * s,size_t s_len)6123a1cceaSRoger A. Faulkner memmem(const void *l, size_t l_len, const void *s, size_t s_len)
6223a1cceaSRoger A. Faulkner {
6323a1cceaSRoger A. Faulkner 	char *cur, *last;
6423a1cceaSRoger A. Faulkner 	const char *cl = (const char *)l;
6523a1cceaSRoger A. Faulkner 	const char *cs = (const char *)s;
6623a1cceaSRoger A. Faulkner 
6723a1cceaSRoger A. Faulkner 	/* we need something to compare */
6823a1cceaSRoger A. Faulkner 	if (l_len == 0 || s_len == 0)
6923a1cceaSRoger A. Faulkner 		return (NULL);
7023a1cceaSRoger A. Faulkner 
7123a1cceaSRoger A. Faulkner 	/* "s" must be smaller or equal to "l" */
7223a1cceaSRoger A. Faulkner 	if (l_len < s_len)
7323a1cceaSRoger A. Faulkner 		return (NULL);
7423a1cceaSRoger A. Faulkner 
7523a1cceaSRoger A. Faulkner 	/* special case where s_len == 1 */
7623a1cceaSRoger A. Faulkner 	if (s_len == 1)
7723a1cceaSRoger A. Faulkner 		return (memchr(l, (int)*cs, l_len));
7823a1cceaSRoger A. Faulkner 
7923a1cceaSRoger A. Faulkner 	/* the last position where its possible to find "s" in "l" */
8023a1cceaSRoger A. Faulkner 	last = (char *)cl + l_len - s_len;
8123a1cceaSRoger A. Faulkner 
8223a1cceaSRoger A. Faulkner 	for (cur = (char *)cl; cur <= last; cur++)
8323a1cceaSRoger A. Faulkner 		if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
8423a1cceaSRoger A. Faulkner 			return (cur);
8523a1cceaSRoger A. Faulkner 
8623a1cceaSRoger A. Faulkner 	return (NULL);
8723a1cceaSRoger A. Faulkner }
88