0
|
1 #include "types.h"
|
|
2 #include "arm.h"
|
|
3
|
|
4
|
|
5 void* memset(void *dst, int v, int n)
|
|
6 {
|
|
7 uint8 *p;
|
|
8 uint8 c;
|
|
9 uint32 val;
|
|
10 uint32 *p4;
|
|
11
|
|
12 p = dst;
|
|
13 c = v & 0xff;
|
|
14 val = (c << 24) | (c << 16) | (c << 8) | c;
|
|
15
|
|
16 // set bytes before whole uint32
|
|
17 for (; (n > 0) && ((uint)p % 4); n--, p++){
|
|
18 *p = c;
|
|
19 }
|
|
20
|
|
21 // set memory 4 bytes a time
|
|
22 p4 = (uint*)p;
|
|
23
|
|
24 for (; n >= 4; n -= 4, p4++) {
|
|
25 *p4 = val;
|
|
26 }
|
|
27
|
|
28 // set leftover one byte a time
|
|
29 p = (uint8*)p4;
|
|
30
|
|
31 for (; n > 0; n--, p++) {
|
|
32 *p = c;
|
|
33 }
|
|
34
|
|
35 return dst;
|
|
36 }
|
|
37
|
|
38
|
|
39 int memcmp(const void *v1, const void *v2, uint n)
|
|
40 {
|
|
41 const uchar *s1, *s2;
|
|
42
|
|
43 s1 = v1;
|
|
44 s2 = v2;
|
|
45
|
|
46 while(n-- > 0){
|
|
47 if(*s1 != *s2) {
|
|
48 return *s1 - *s2;
|
|
49 }
|
|
50
|
|
51 s1++, s2++;
|
|
52 }
|
|
53
|
|
54 return 0;
|
|
55 }
|
|
56
|
|
57 void* memmove(void *dst, const void *src, uint n)
|
|
58 {
|
|
59 const char *s;
|
|
60 char *d;
|
|
61
|
|
62 s = src;
|
|
63 d = dst;
|
|
64
|
|
65 if(s < d && s + n > d){
|
|
66 s += n;
|
|
67 d += n;
|
|
68
|
|
69 while(n-- > 0) {
|
|
70 *--d = *--s;
|
|
71 }
|
|
72
|
|
73 } else {
|
|
74 while(n-- > 0) {
|
|
75 *d++ = *s++;
|
|
76 }
|
|
77 }
|
|
78
|
|
79 return dst;
|
|
80 }
|
|
81
|
|
82 // memcpy exists to placate GCC. Use memmove.
|
|
83 void* memcpy(void *dst, const void *src, uint n)
|
|
84 {
|
|
85 return memmove(dst, src, n);
|
|
86 }
|
|
87
|
|
88 int strncmp(const char *p, const char *q, uint n)
|
|
89 {
|
|
90 while(n > 0 && *p && *p == *q) {
|
|
91 n--, p++, q++;
|
|
92 }
|
|
93
|
|
94 if(n == 0) {
|
|
95 return 0;
|
|
96 }
|
|
97
|
|
98 return (uchar)*p - (uchar)*q;
|
|
99 }
|
|
100
|
|
101 char* strncpy(char *s, const char *t, int n)
|
|
102 {
|
|
103 char *os;
|
|
104
|
|
105 os = s;
|
|
106
|
|
107 while(n-- > 0 && (*s++ = *t++) != 0)
|
|
108 ;
|
|
109
|
|
110 while(n-- > 0) {
|
|
111 *s++ = 0;
|
|
112 }
|
|
113
|
|
114 return os;
|
|
115 }
|
|
116
|
|
117 // Like strncpy but guaranteed to NUL-terminate.
|
|
118 char* safestrcpy(char *s, const char *t, int n)
|
|
119 {
|
|
120 char *os;
|
|
121
|
|
122 os = s;
|
|
123
|
|
124 if(n <= 0) {
|
|
125 return os;
|
|
126 }
|
|
127
|
|
128 while(--n > 0 && (*s++ = *t++) != 0)
|
|
129 ;
|
|
130
|
|
131 *s = 0;
|
|
132 return os;
|
|
133 }
|
|
134
|
|
135 int strlen(const char *s)
|
|
136 {
|
|
137 int n;
|
|
138
|
|
139 for(n = 0; s[n]; n++)
|
|
140 ;
|
|
141
|
|
142 return n;
|
|
143 }
|
|
144
|