| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Martin Decky
|
|---|
| 3 | * Copyright (c) 2008 Jiri Svoboda
|
|---|
| 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
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libc
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <mem.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 |
|
|---|
| 40 | /** Move memory block with possible overlapping. */
|
|---|
| 41 | void *memmove(void *dst, const void *src, size_t n)
|
|---|
| 42 | {
|
|---|
| 43 | const uint8_t *sp;
|
|---|
| 44 | uint8_t *dp;
|
|---|
| 45 |
|
|---|
| 46 | /* Nothing to do? */
|
|---|
| 47 | if (src == dst)
|
|---|
| 48 | return dst;
|
|---|
| 49 |
|
|---|
| 50 | /* Non-overlapping? */
|
|---|
| 51 | if (dst >= src + n || src >= dst + n) {
|
|---|
| 52 | return memcpy(dst, src, n);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /* Which direction? */
|
|---|
| 56 | if (src > dst) {
|
|---|
| 57 | /* Forwards. */
|
|---|
| 58 | sp = src;
|
|---|
| 59 | dp = dst;
|
|---|
| 60 |
|
|---|
| 61 | while (n-- != 0)
|
|---|
| 62 | *dp++ = *sp++;
|
|---|
| 63 | } else {
|
|---|
| 64 | /* Backwards. */
|
|---|
| 65 | sp = src + (n - 1);
|
|---|
| 66 | dp = dst + (n - 1);
|
|---|
| 67 |
|
|---|
| 68 | while (n-- != 0)
|
|---|
| 69 | *dp-- = *sp--;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | return dst;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** Compare two memory areas.
|
|---|
| 76 | *
|
|---|
| 77 | * @param s1 Pointer to the first area to compare.
|
|---|
| 78 | * @param s2 Pointer to the second area to compare.
|
|---|
| 79 | * @param len Size of the first area in bytes. Both areas must have
|
|---|
| 80 | * the same length.
|
|---|
| 81 | *
|
|---|
| 82 | * @return If len is 0, return zero. If the areas match, return
|
|---|
| 83 | * zero. Otherwise return non-zero.
|
|---|
| 84 | *
|
|---|
| 85 | */
|
|---|
| 86 | int bcmp(const void *s1, const void *s2, size_t len)
|
|---|
| 87 | {
|
|---|
| 88 | uint8_t *u1 = (uint8_t *) s1;
|
|---|
| 89 | uint8_t *u2 = (uint8_t *) s2;
|
|---|
| 90 |
|
|---|
| 91 | for (; (len != 0) && (*u1++ == *u2++); len--);
|
|---|
| 92 |
|
|---|
| 93 | return len;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** @}
|
|---|
| 97 | */
|
|---|