/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Ed Schouten <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/cdefs.h>29#include <sys/libkern.h>30#include <sys/limits.h>31#include <sys/param.h>3233/*34* memcchr(): find first character in buffer not matching `c'.35*36* This function performs the complement of memchr(). To provide decent37* performance, this function compares data from the buffer one word at38* a time.39*40* This code is inspired by libc's strlen(), written by Xin Li.41*/4243#if LONG_BIT != 32 && LONG_BIT != 6444#error Unsupported word size45#endif4647#define LONGPTR_MASK (sizeof(long) - 1)4849#define TESTBYTE \50do { \51if (*p != (unsigned char)c) \52goto done; \53p++; \54} while (0)5556void *57memcchr(const void *begin, int c, size_t n)58{59const unsigned long *lp;60const unsigned char *p, *end;61unsigned long word;6263/* Four or eight repetitions of `c'. */64word = (unsigned char)c;65word |= word << 8;66word |= word << 16;67#if LONG_BIT >= 6468word |= word << 32;69#endif7071/* Don't perform memory I/O when passing a zero-length buffer. */72if (n == 0)73return (NULL);7475/*76* First determine whether there is a character unequal to `c'77* in the first word. As this word may contain bytes before78* `begin', we may execute this loop spuriously.79*/80lp = (const unsigned long *)((uintptr_t)begin & ~LONGPTR_MASK);81end = (const unsigned char *)begin + n;82if (*lp++ != word)83for (p = begin; p < (const unsigned char *)lp;)84TESTBYTE;8586/* Now compare the data one word at a time. */87for (; (const unsigned char *)lp < end; lp++) {88if (*lp != word) {89p = (const unsigned char *)lp;90TESTBYTE;91TESTBYTE;92TESTBYTE;93#if LONG_BIT >= 6494TESTBYTE;95TESTBYTE;96TESTBYTE;97TESTBYTE;98#endif99goto done;100}101}102103return (NULL);104105done:106/*107* If the end of the buffer is not word aligned, the previous108* loops may obtain an address that's beyond the end of the109* buffer.110*/111if (p < end)112return (__DECONST(void *, p));113return (NULL);114}115116117