1*ea6088e7Sguenther /* $OpenBSD: memcpy.c,v 1.4 2017/11/29 05:13:57 guenther Exp $ */
25b859c19Sderaadt /*-
35b859c19Sderaadt * Copyright (c) 1990 The Regents of the University of California.
45b859c19Sderaadt * All rights reserved.
55b859c19Sderaadt *
65b859c19Sderaadt * This code is derived from software contributed to Berkeley by
75b859c19Sderaadt * Chris Torek.
85b859c19Sderaadt *
95b859c19Sderaadt * Redistribution and use in source and binary forms, with or without
105b859c19Sderaadt * modification, are permitted provided that the following conditions
115b859c19Sderaadt * are met:
125b859c19Sderaadt * 1. Redistributions of source code must retain the above copyright
135b859c19Sderaadt * notice, this list of conditions and the following disclaimer.
145b859c19Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
155b859c19Sderaadt * notice, this list of conditions and the following disclaimer in the
165b859c19Sderaadt * documentation and/or other materials provided with the distribution.
175b859c19Sderaadt * 3. Neither the name of the University nor the names of its contributors
185b859c19Sderaadt * may be used to endorse or promote products derived from this software
195b859c19Sderaadt * without specific prior written permission.
205b859c19Sderaadt *
215b859c19Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225b859c19Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235b859c19Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245b859c19Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255b859c19Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265b859c19Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275b859c19Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285b859c19Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295b859c19Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305b859c19Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315b859c19Sderaadt * SUCH DAMAGE.
325b859c19Sderaadt */
335b859c19Sderaadt
345b859c19Sderaadt #include <string.h>
355b859c19Sderaadt #include <stdlib.h>
365b859c19Sderaadt #include <syslog.h>
375b859c19Sderaadt
385b859c19Sderaadt /*
395b859c19Sderaadt * sizeof(word) MUST BE A POWER OF TWO
405b859c19Sderaadt * SO THAT wmask BELOW IS ALL ONES
415b859c19Sderaadt */
425b859c19Sderaadt typedef long word; /* "word" used for optimal copy speed */
435b859c19Sderaadt
445b859c19Sderaadt #define wsize sizeof(word)
455b859c19Sderaadt #define wmask (wsize - 1)
465b859c19Sderaadt
4744a12cdbSguenther static const char backwards_msg[] = ": backwards memcpy";
4844a12cdbSguenther
495b859c19Sderaadt /*
505b859c19Sderaadt * Copy a block of memory, not handling overlap.
515b859c19Sderaadt */
525b859c19Sderaadt void *
memcpy(void * dst0,const void * src0,size_t length)535b859c19Sderaadt memcpy(void *dst0, const void *src0, size_t length)
545b859c19Sderaadt {
555b859c19Sderaadt char *dst = dst0;
565b859c19Sderaadt const char *src = src0;
575b859c19Sderaadt size_t t;
585b859c19Sderaadt
595b859c19Sderaadt if (length == 0 || dst == src) /* nothing to do */
605b859c19Sderaadt goto done;
615b859c19Sderaadt
625b859c19Sderaadt if ((dst < src && dst + length > src) ||
635b859c19Sderaadt (src < dst && src + length > dst)) {
6444a12cdbSguenther char buf[1024];
655b859c19Sderaadt
6644a12cdbSguenther /* <10> is LOG_CRIT */
6744a12cdbSguenther strlcpy(buf, "<10>", sizeof buf);
6844a12cdbSguenther
6944a12cdbSguenther /* Make sure progname does not fill the whole buffer */
7044a12cdbSguenther strlcat(buf, __progname, sizeof(buf) - sizeof backwards_msg);
7144a12cdbSguenther strlcat(buf, backwards_msg, sizeof buf);
7244a12cdbSguenther
7344a12cdbSguenther sendsyslog(buf, strlen(buf), LOG_CONS);
745b859c19Sderaadt abort();
755b859c19Sderaadt }
765b859c19Sderaadt
775b859c19Sderaadt /*
785b859c19Sderaadt * Macros: loop-t-times; and loop-t-times, t>0
795b859c19Sderaadt */
805b859c19Sderaadt #define TLOOP(s) if (t) TLOOP1(s)
815b859c19Sderaadt #define TLOOP1(s) do { s; } while (--t)
825b859c19Sderaadt
835b859c19Sderaadt /*
845b859c19Sderaadt * Copy forward.
855b859c19Sderaadt */
865b859c19Sderaadt t = (long)src; /* only need low bits */
875b859c19Sderaadt if ((t | (long)dst) & wmask) {
885b859c19Sderaadt /*
895b859c19Sderaadt * Try to align operands. This cannot be done
905b859c19Sderaadt * unless the low bits match.
915b859c19Sderaadt */
925b859c19Sderaadt if ((t ^ (long)dst) & wmask || length < wsize)
935b859c19Sderaadt t = length;
945b859c19Sderaadt else
955b859c19Sderaadt t = wsize - (t & wmask);
965b859c19Sderaadt length -= t;
975b859c19Sderaadt TLOOP1(*dst++ = *src++);
985b859c19Sderaadt }
995b859c19Sderaadt /*
1005b859c19Sderaadt * Copy whole words, then mop up any trailing bytes.
1015b859c19Sderaadt */
1025b859c19Sderaadt t = length / wsize;
1035b859c19Sderaadt TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
1045b859c19Sderaadt t = length & wmask;
1055b859c19Sderaadt TLOOP(*dst++ = *src++);
1065b859c19Sderaadt done:
1075b859c19Sderaadt return (dst0);
1085b859c19Sderaadt }
109*ea6088e7Sguenther DEF_BUILTIN(memcpy);
110