| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include <libpayload.h> |
| 34 |
|
| 35 |
void *default_memset(void *const s, const int c, size_t n) |
| 36 |
{ |
| 37 |
size_t i; |
| 38 |
u8 *dst = s; |
| 39 |
unsigned long w = c & 0xff; |
| 40 |
|
| 41 |
const u8 *const aligned_start = |
| 42 |
(const u8 *)ALIGN_UP((uintptr_t)dst, sizeof(unsigned long)); |
| 43 |
for (; n > 0 && dst != aligned_start; --n, ++dst) |
| 44 |
*dst = (u8)c; |
| 45 |
|
| 46 |
for (i = 1; i < sizeof(unsigned long); i <<= 1) |
| 47 |
w = (w << (i * 8)) | w; |
| 48 |
|
| 49 |
for (i = 0; i < n / sizeof(unsigned long); i++) |
| 50 |
((unsigned long *)dst)[i] = w; |
| 51 |
|
| 52 |
dst += i * sizeof(unsigned long); |
| 53 |
|
| 54 |
for (i = 0; i < n % sizeof(unsigned long); i++) |
| 55 |
dst[i] = (u8)c; |
| 56 |
|
| 57 |
return s; |
| 58 |
} |
| 59 |
|
| 60 |
void *default_memcpy(void *dst, const void *src, size_t n) |
| 61 |
{ |
| 62 |
size_t i; |
| 63 |
void *ret = dst; |
| 64 |
|
| 65 |
if (IS_ALIGNED((uintptr_t)dst, sizeof(unsigned long)) && |
| 66 |
IS_ALIGNED((uintptr_t)src, sizeof(unsigned long))) { |
| 67 |
for (i = 0; i < n / sizeof(unsigned long); i++) |
| 68 |
((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 69 |
|
| 70 |
src += i * sizeof(unsigned long); |
| 71 |
dst += i * sizeof(unsigned long); |
| 72 |
n -= i * sizeof(unsigned long); |
| 73 |
} |
| 74 |
|
| 75 |
for (i = 0; i < n; i++) |
| 76 |
((u8 *)dst)[i] = ((u8 *)src)[i]; |
| 77 |
|
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
void *default_memmove(void *dst, const void *src, size_t n) |
| 82 |
{ |
| 83 |
size_t offs; |
| 84 |
ssize_t i; |
| 85 |
|
| 86 |
if (src > dst) |
| 87 |
return default_memcpy(dst, src, n); |
| 88 |
|
| 89 |
if (!IS_ALIGNED((uintptr_t)dst, sizeof(unsigned long)) || |
| 90 |
!IS_ALIGNED((uintptr_t)src, sizeof(unsigned long))) { |
| 91 |
for (i = n - 1; i >= 0; i--) |
| 92 |
((u8 *)dst)[i] = ((u8 *)src)[i]; |
| 93 |
return dst; |
| 94 |
} |
| 95 |
|
| 96 |
offs = n - (n % sizeof(unsigned long)); |
| 97 |
|
| 98 |
for (i = (n % sizeof(unsigned long)) - 1; i >= 0; i--) |
| 99 |
((u8 *)dst)[i + offs] = ((u8 *)src)[i + offs]; |
| 100 |
|
| 101 |
for (i = n / sizeof(unsigned long) - 1; i >= 0; i--) |
| 102 |
((unsigned long *)dst)[i] = ((unsigned long *)src)[i]; |
| 103 |
|
| 104 |
return dst; |
| 105 |
} |
| 106 |
|
| 107 |
#if !CONFIG(LP_ARCH_HAS_MEM_FUNCTIONS) |
| 108 |
void *memcpy(void *dst, const void *src, size_t n) |
| 109 |
__attribute__((alias("default_memcpy"))); |
| 110 |
void *memset(void *s, int c, size_t n) |
| 111 |
__attribute__((alias("default_memset"))); |
| 112 |
void *memmove(void *dst, const void *src, size_t n) |
| 113 |
__attribute__((alias("default_memmove"))); |
| 114 |
#endif |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int memcmp(const void *s1, const void *s2, size_t n) |
| 128 |
{ |
| 129 |
size_t i = 0; |
| 130 |
const unsigned long *w1 = s1, *w2 = s2; |
| 131 |
|
| 132 |
if (IS_ALIGNED((uintptr_t)s1, sizeof(unsigned long)) && |
| 133 |
IS_ALIGNED((uintptr_t)s2, sizeof(unsigned long))) |
| 134 |
for (; i < n / sizeof(unsigned long); i++) |
| 135 |
if (w1[i] != w2[i]) |
| 136 |
break; |
| 137 |
|
| 138 |
for (i *= sizeof(unsigned long); i < n; i++) |
| 139 |
if (((u8 *)s1)[i] != ((u8 *)s2)[i]) |
| 140 |
return ((u8 *)s1)[i] - ((u8 *)s2)[i]; |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
void *memchr(const void *s, int c, size_t n) |
| 146 |
{ |
| 147 |
unsigned char *p = (unsigned char *)s; |
| 148 |
while (n--) |
| 149 |
if (*p != (unsigned char)c) |
| 150 |
p++; |
| 151 |
else |
| 152 |
return p; |
| 153 |
return 0; |
| 154 |
} |