blob: 12d7e33dfdce4b40b9e6a3b1d6bec5bac0d252fa [file] [log] [blame]
Uwe Hermannc52761b2008-03-20 00:02:07 +00001/*
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. Minnichc1ee8642013-02-14 14:36:46 -080036static void *default_memset(void *s, int c, size_t n)
Uwe Hermannc52761b2008-03-20 00:02:07 +000037{
38 char *os = s;
39
40 while (n--)
41 *(os++) = c;
42
43 return s;
44}
45
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -080046void *memset(void *s, int c, size_t n)
47 __attribute__((weak, alias("default_memset")));
Gabe Blackc3247942012-09-27 17:39:41 -070048
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -080049static void *default_memcpy(void *dst, const void *src, size_t n)
Uwe Hermannc52761b2008-03-20 00:02:07 +000050{
Jordan Crouse96f57ae2008-08-19 16:55:05 +000051 int i;
Stefan Reinauer989c1782008-08-19 19:18:58 +000052 void *ret = dst;
Uwe Hermannc52761b2008-03-20 00:02:07 +000053
Jordan Crouse96f57ae2008-08-19 16:55:05 +000054 for(i = 0; i < n % sizeof(unsigned long); i++)
55 ((unsigned char *) dst)[i] = ((unsigned char *) src)[i];
Uwe Hermannc52761b2008-03-20 00:02:07 +000056
Jordan Crouse96f57ae2008-08-19 16:55:05 +000057 n -= i;
58 src += i;
59 dst += i;
Uwe Hermannc52761b2008-03-20 00:02:07 +000060
Jordan Crouse96f57ae2008-08-19 16:55:05 +000061 for(i = 0; i < n / sizeof(unsigned long); i++)
62 ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
Uwe Hermannc52761b2008-03-20 00:02:07 +000063
Stefan Reinauer989c1782008-08-19 19:18:58 +000064 return ret;
Uwe Hermannc52761b2008-03-20 00:02:07 +000065}
66
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -080067void *memcpy(void *dst, const void *src, size_t n)
68 __attribute__((weak, alias("default_memcpy")));
Gabe Blackc3247942012-09-27 17:39:41 -070069
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -080070static void *default_memmove(void *dst, const void *src, size_t n)
Uwe Hermannc52761b2008-03-20 00:02:07 +000071{
Jordan Crouse96f57ae2008-08-19 16:55:05 +000072 int i;
73 unsigned long offs;
Uwe Hermannc52761b2008-03-20 00:02:07 +000074
75 if (src > dst)
76 return memcpy(dst, src, n);
77
Jordan Crouse96f57ae2008-08-19 16:55:05 +000078 offs = n - (n % sizeof(unsigned long));
79
80 for (i = (n % sizeof(unsigned long)) - 1; i >= 0; i--)
Stefan Reinauer14e22772010-04-27 06:56:47 +000081 ((unsigned char *)dst)[i + offs] =
Jordan Crouse96f57ae2008-08-19 16:55:05 +000082 ((unsigned char *)src)[i + offs];
Uwe Hermannc52761b2008-03-20 00:02:07 +000083
84 for (i = n / sizeof(unsigned long) - 1; i >= 0; i--)
85 ((unsigned long *)dst)[i] = ((unsigned long *)src)[i];
86
Jordan Crouse96f57ae2008-08-19 16:55:05 +000087 return dst;
Uwe Hermannc52761b2008-03-20 00:02:07 +000088}
89
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -080090void *memmove(void *dst, const void *src, size_t n)
91 __attribute__((weak, alias("default_memmove")));
92
Uwe Hermannc52761b2008-03-20 00:02:07 +000093/**
94 * Compare two memory areas.
95 *
Uwe Hermann6a441bf2008-03-20 19:54:59 +000096 * @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 Hermannc52761b2008-03-20 00:02:07 +0000101 */
Gabe Blackc3247942012-09-27 17:39:41 -0700102
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -0800103static int default_memcmp(const void *s1, const void *s2, size_t len)
Uwe Hermannc52761b2008-03-20 00:02:07 +0000104{
Ulf Jordan68285692008-08-09 19:34:56 +0000105 for (; len && *(char *)s1++ == *(char *)s2++; len--) ;
Uwe Hermannc52761b2008-03-20 00:02:07 +0000106 return len;
107}
Ronald G. Minnichc1ee8642013-02-14 14:36:46 -0800108
109int memcmp(const void *s1, const void *s2, size_t len)
110 __attribute__((weak, alias("default_memcmp")));
111