1 /*
2 * Copyright 2018 The Hafnium Authors.
3 *
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
7 */
8
9 #include "hf/arch/std.h"
10
memset(void * s,int c,size_t n)11 void *memset(void *s, int c, size_t n)
12 {
13 char *p = (char *)s;
14
15 while (n--) {
16 *p++ = c;
17 }
18
19 return s;
20 }
21
memcpy(void * dst,const void * src,size_t n)22 void *memcpy(void *dst, const void *src, size_t n)
23 {
24 char *x = dst;
25 const char *y = src;
26
27 while (n--) {
28 *x = *y;
29 x++;
30 y++;
31 }
32
33 return dst;
34 }
35
memmove(void * dst,const void * src,size_t n)36 void *memmove(void *dst, const void *src, size_t n)
37 {
38 char *x;
39 const char *y;
40
41 if (dst < src) {
42 /*
43 * Clang analyzer doesn't like us calling unsafe memory
44 * functions, so make it ignore this while still knowing that
45 * the function returns.
46 */
47 #ifdef __clang_analyzer__
48 return dst;
49 #else
50 return memcpy(dst, src, n);
51 #endif
52 }
53
54 x = (char *)dst + n - 1;
55 y = (const char *)src + n - 1;
56
57 while (n--) {
58 *x = *y;
59 x--;
60 y--;
61 }
62
63 return dst;
64 }
65
memcmp(const void * a,const void * b,size_t n)66 int memcmp(const void *a, const void *b, size_t n)
67 {
68 const char *x = a;
69 const char *y = b;
70
71 while (n--) {
72 if (*x != *y) {
73 return *x - *y;
74 }
75 x++;
76 y++;
77 }
78
79 return 0;
80 }
81
strncmp(const char * a,const char * b,size_t n)82 int strncmp(const char *a, const char *b, size_t n)
83 {
84 char x = 0;
85 char y = 0;
86
87 while (n > 0) {
88 x = *a++;
89 y = *b++;
90 if (x == 0 || x != y) {
91 break;
92 }
93 --n;
94 }
95
96 return x - y;
97 }
98