/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/types.h>3536typedef intptr_t word; /* "word" used for optimal copy speed */3738#define wsize sizeof(word)39#define wmask (wsize - 1)4041/*42* Copy a block of memory, handling overlap.43* This is the routine that actually implements44* (the portable versions of) bcopy, memcpy, and memmove.45*/46#if defined(MEMCOPY) || defined(MEMMOVE)47#include <string.h>4849#undef memcpy /* _FORTIFY_SOURCE */50#undef memmove /* _FORTIFY_SOURCE */5152void *53#ifdef MEMCOPY54memcpy55#else56memmove57#endif58(void *dst0, const void *src0, size_t length)59#else60#include <strings.h>6162#undef bcopy /* _FORTIFY_SOURCE */6364void65bcopy(const void *src0, void *dst0, size_t length)66#endif67{68char *dst = dst0;69const char *src = src0;70size_t t;7172if (length == 0 || dst == src) /* nothing to do */73goto done;7475/*76* Macros: loop-t-times; and loop-t-times, t>077*/78#define TLOOP(s) if (t) TLOOP1(s)79#define TLOOP1(s) do { s; } while (--t)8081if ((unsigned long)dst < (unsigned long)src) {82/*83* Copy forward.84*/85t = (uintptr_t)src; /* only need low bits */86if ((t | (uintptr_t)dst) & wmask) {87/*88* Try to align operands. This cannot be done89* unless the low bits match.90*/91if ((t ^ (uintptr_t)dst) & wmask || length < wsize)92t = length;93else94t = wsize - (t & wmask);95length -= t;96TLOOP1(*dst++ = *src++);97}98/*99* Copy whole words, then mop up any trailing bytes.100*/101t = length / wsize;102TLOOP(*(word *)(void *)dst = *(const word *)(const void *)src;103src += wsize; dst += wsize);104t = length & wmask;105TLOOP(*dst++ = *src++);106} else {107/*108* Copy backwards. Otherwise essentially the same.109* Alignment works as before, except that it takes110* (t&wmask) bytes to align, not wsize-(t&wmask).111*/112src += length;113dst += length;114t = (uintptr_t)src;115if ((t | (uintptr_t)dst) & wmask) {116if ((t ^ (uintptr_t)dst) & wmask || length <= wsize)117t = length;118else119t &= wmask;120length -= t;121TLOOP1(*--dst = *--src);122}123t = length / wsize;124TLOOP(src -= wsize; dst -= wsize;125*(word *)(void *)dst = *(const word *)(const void *)src);126t = length & wmask;127TLOOP(*--dst = *--src);128}129done:130#if defined(MEMCOPY) || defined(MEMMOVE)131return (dst0);132#else133return;134#endif135}136137138