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 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 36 | static void *default_memset(void *s, int c, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 37 | { |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 38 | size_t i; |
| 39 | void *ret = s; |
| 40 | unsigned long w = c & 0xff; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 41 | |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 42 | for (i = 1; i < sizeof(unsigned long); i <<= 1) |
| 43 | w = (w << (i * 8)) | w; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 44 | |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 45 | for (i = 0; i < n / sizeof(unsigned long); i++) |
| 46 | ((unsigned long *)s)[i] = w; |
| 47 | |
| 48 | s += i * sizeof(unsigned long); |
| 49 | |
| 50 | for (i = 0; i < n % sizeof(unsigned long); i++) |
| 51 | ((u8 *)s)[i] = (u8)c; |
| 52 | |
| 53 | return ret; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 56 | void *memset(void *s, int c, size_t n) |
| 57 | __attribute__((weak, alias("default_memset"))); |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 58 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 59 | static void *default_memcpy(void *dst, const void *src, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 60 | { |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 61 | size_t i; |
Stefan Reinauer | 989c178 | 2008-08-19 19:18:58 +0000 | [diff] [blame] | 62 | void *ret = dst; |
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++) |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 65 | ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 66 | |
| 67 | src += i * sizeof(unsigned long); |
| 68 | dst += i * sizeof(unsigned long); |
| 69 | |
| 70 | for(i = 0; i < n % sizeof(unsigned long); i++) |
| 71 | ((u8 *)dst)[i] = ((u8 *)src)[i]; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 72 | |
Stefan Reinauer | 989c178 | 2008-08-19 19:18:58 +0000 | [diff] [blame] | 73 | return ret; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 76 | void *memcpy(void *dst, const void *src, size_t n) |
| 77 | __attribute__((weak, alias("default_memcpy"))); |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 78 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 79 | static void *default_memmove(void *dst, const void *src, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 80 | { |
Nico Huber | f58746b | 2015-08-19 17:22:26 +0200 | [diff] [blame] | 81 | size_t offs; |
| 82 | ssize_t i; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 83 | |
| 84 | if (src > dst) |
| 85 | return memcpy(dst, src, n); |
| 86 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 87 | offs = n - (n % sizeof(unsigned long)); |
| 88 | |
| 89 | for (i = (n % sizeof(unsigned long)) - 1; i >= 0; i--) |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 90 | ((u8 *)dst)[i + offs] = ((u8 *)src)[i + offs]; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 91 | |
| 92 | for (i = n / sizeof(unsigned long) - 1; i >= 0; i--) |
| 93 | ((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 94 | |
Jordan Crouse | 96f57ae | 2008-08-19 16:55:05 +0000 | [diff] [blame] | 95 | return dst; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 98 | void *memmove(void *dst, const void *src, size_t n) |
| 99 | __attribute__((weak, alias("default_memmove"))); |
| 100 | |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 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. |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 106 | * @param n Size of the first area in bytes (both must have the same length). |
| 107 | * @return If n is 0, return zero. Otherwise, return a value less than, equal |
| 108 | * to, or greater than zero if s1 is found less than, equal to, or |
| 109 | * greater than s2 respectively. |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 110 | */ |
Gabe Black | c324794 | 2012-09-27 17:39:41 -0700 | [diff] [blame] | 111 | |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 112 | static int default_memcmp(const void *s1, const void *s2, size_t n) |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 113 | { |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 114 | size_t i; |
| 115 | |
| 116 | for (i = 0; i < n / sizeof(unsigned long); i++) |
| 117 | if (((unsigned long *)s1)[i] != ((unsigned long *)s2)[i]) |
| 118 | break; /* fall through to find differing byte */ |
| 119 | |
| 120 | for (i *= sizeof(unsigned long); i < n; i++) |
| 121 | if (((u8 *)s1)[i] != ((u8 *)s2)[i]) |
| 122 | return ((u8 *)s1)[i] - ((u8 *)s2)[i]; |
| 123 | |
| 124 | return 0; |
Uwe Hermann | c52761b | 2008-03-20 00:02:07 +0000 | [diff] [blame] | 125 | } |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 126 | |
Julius Werner | dbadb1d | 2014-06-12 10:28:57 -0700 | [diff] [blame] | 127 | int memcmp(const void *s1, const void *s2, size_t n) |
Ronald G. Minnich | c1ee864 | 2013-02-14 14:36:46 -0800 | [diff] [blame] | 128 | __attribute__((weak, alias("default_memcmp"))); |