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) |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 37 | __attribute__((weak, alias("default_memset"))); |
| 38 | |
| 39 | void *default_memset(void *s, int c, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 40 | { |
| 41 | char *os = s; |
| 42 | |
| 43 | while (n--) |
| 44 | *(os++) = c; |
| 45 | |
| 46 | return s; |
| 47 | } |
| 48 | |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 49 | void *memcpy(void *dst, const void *src, size_t n) |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 50 | __attribute__((weak, alias("default_memcpy"))); |
| 51 | |
| 52 | void *default_memcpy(void *dst, const void *src, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 53 | { |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 54 | int i; |
Stefan Reinauer | 989c178 | 2008-08-19 19:18:58 +0000 | [diff] [blame] | 55 | void *ret = dst; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 56 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 57 | for(i = 0; i < n % sizeof(unsigned long); i++) |
| 58 | ((unsigned char *) dst)[i] = ((unsigned char *) src)[i]; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 59 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 60 | n -= i; |
| 61 | src += i; |
| 62 | dst += i; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 63 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 64 | for(i = 0; i < n / sizeof(unsigned long); i++) |
| 65 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 66 | |
Stefan Reinauer | 989c178 | 2008-08-19 19:18:58 +0000 | [diff] [blame] | 67 | return ret; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void *memmove(void *dst, const void *src, size_t n) |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 71 | __attribute__((weak, alias("default_memmove"))); |
| 72 | |
| 73 | void *default_memmove(void *dst, const void *src, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 74 | { |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 75 | int i; |
| 76 | unsigned long offs; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 77 | |
| 78 | if (src > dst) |
| 79 | return memcpy(dst, src, n); |
| 80 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 81 | offs = n - (n % sizeof(unsigned long)); |
| 82 | |
| 83 | for (i = (n % sizeof(unsigned long)) - 1; i >= 0; i--) |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 84 | ((unsigned char *)dst)[i + offs] = |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 85 | ((unsigned char *)src)[i + offs]; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 86 | |
| 87 | for (i = n / sizeof(unsigned long) - 1; i >= 0; i--) |
| 88 | ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 89 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 90 | return dst; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Compare two memory areas. |
| 95 | * |
Uwe Hermann | 6a441bf | 2008-03-20 19:54:59 +0000 | [diff] [blame] | 96 | * @param s1 Pointer to the first area to compare. |
| 97 | * @param s2 Pointer to the second area to compare. |
| 98 | * @param len Size of the first area in bytes (both must have the same length). |
| 99 | * @return If len is 0, return zero. If the areas match, return zero. |
| 100 | * Otherwise return non-zero. |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 101 | */ |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 102 | |
Ulf Jordan | 6828569 | 2008-08-09 19:34:56 +0000 | [diff] [blame] | 103 | int memcmp(const void *s1, const void *s2, size_t len) |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 104 | __attribute__((weak, alias("default_memcmp"))); |
| 105 | |
| 106 | int default_memcmp(const void *s1, const void *s2, size_t len) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 107 | { |
Ulf Jordan | 6828569 | 2008-08-09 19:34:56 +0000 | [diff] [blame] | 108 | for (; len && *(char *)s1++ == *(char *)s2++; len--) ; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 109 | return len; |
| 110 | } |