Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * It has originally been taken from the HelenOS project |
| 5 | * (http://www.helenos.eu), and slightly modified for our purposes. |
| 6 | * |
| 7 | * Copyright (c) 2005 Martin Decky |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * - Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * - The name of the author may not be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 25 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <libpayload.h> |
| 35 | |
| 36 | void *memset(void *s, int c, size_t n) |
| 37 | { |
| 38 | char *os = s; |
| 39 | |
| 40 | while (n--) |
| 41 | *(os++) = c; |
| 42 | |
| 43 | return s; |
| 44 | } |
| 45 | |
| 46 | struct along { |
| 47 | unsigned long n; |
| 48 | } __attribute__ ((packed)); |
| 49 | |
| 50 | static void *unaligned_memcpy(void *dst, const void *src, size_t n) |
| 51 | { |
| 52 | int i, j; |
| 53 | struct along *adst = dst; |
| 54 | const struct along *asrc = src; |
| 55 | |
| 56 | for (i = 0; i < n / sizeof(unsigned long); i++) |
| 57 | adst[i].n = asrc[i].n; |
| 58 | |
| 59 | for (j = 0; j < n % sizeof(unsigned long); j++) |
| 60 | ((unsigned char *)(((unsigned long *)dst) + i))[j] = |
| 61 | ((unsigned char *)(((unsigned long *)src) + i))[j]; |
| 62 | |
| 63 | return (char *)src; |
| 64 | } |
| 65 | |
| 66 | void *memcpy(void *dst, const void *src, size_t n) |
| 67 | { |
| 68 | int i, j; |
| 69 | |
| 70 | if (((long)dst & (sizeof(long) - 1)) |
| 71 | || ((long)src & (sizeof(long) - 1))) |
| 72 | return unaligned_memcpy(dst, src, n); |
| 73 | |
| 74 | for (i = 0; i < n / sizeof(unsigned long); i++) |
| 75 | ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 76 | |
| 77 | for (j = 0; j < n % sizeof(unsigned long); j++) |
| 78 | ((unsigned char *)(((unsigned long *)dst) + i))[j] = |
| 79 | ((unsigned char *)(((unsigned long *)src) + i))[j]; |
| 80 | |
| 81 | return (char *)src; |
| 82 | } |
| 83 | |
| 84 | void *memmove(void *dst, const void *src, size_t n) |
| 85 | { |
| 86 | int i, j; |
| 87 | |
| 88 | if (src > dst) |
| 89 | return memcpy(dst, src, n); |
| 90 | |
| 91 | for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--) |
| 92 | ((unsigned char *)((unsigned long *)dst))[j] = |
| 93 | ((unsigned char *)((unsigned long *)src))[j]; |
| 94 | |
| 95 | for (i = n / sizeof(unsigned long) - 1; i >= 0; i--) |
| 96 | ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 97 | |
| 98 | return (char *)src; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Compare two memory areas. |
| 103 | * |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame^] | 104 | * @param s1 Pointer to the first area to compare. |
| 105 | * @param s2 Pointer to the second area to compare. |
| 106 | * @param len Size of the first area in bytes (both must have the same length). |
| 107 | * @return If len is 0, return zero. If the areas match, return zero. |
| 108 | * Otherwise return non-zero. |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 109 | */ |
| 110 | int memcmp(const char *s1, const char *s2, size_t len) |
| 111 | { |
| 112 | for (; len && *s1++ == *s2++; len--) ; |
| 113 | return len; |
| 114 | } |