-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring.c
240 lines (202 loc) · 4 KB
/
string.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "string.h"
int hexlut[1 + 'F' - '0'] = {
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, /* 0 - 9 */
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf /* A - F */
};
int strlen(const char *str)
{
const char *c = str;
while (*c++)
;
return c - str;
}
size_t strnlen(const char *s, size_t maxlen)
{
size_t len = 0;
while (*s++ && len < maxlen)
len++;
return len;
}
char *strrchr(const char *s, int c)
{
char *ret = NULL;
do {
if (*s == c)
ret = (char *) s;
} while (*s++);
return ret;
}
int strncmp(const char *stra, const char *strb, int len)
{
unsigned char c1, c2;
while (len) {
c1 = *stra++;
c2 = *strb++;
if (c1 != c2)
return c1 < c2 ? -1 : 1;
if (!c1)
break;
len--;
}
return 0;
}
void *gethexaddr(const char *str, const char **end)
{
const char *s = str;
char *e;
void *addr = NULL;
int shift = 0;
/* set the end */
while (*s) {
if (*s == ' ' || *s == '\t' || *s == ',')
break;
s++;
}
if (!*s)
return NULL;
e = (char *)s;
s = (const char *)(e - 1);
while (s >= str && *s != 'x') {
/* we assume base16! */
int off = (*s > 'F') ? 0x20 : 0x0;
addr += hexlut[*s - ('0' + off)] << shift;
shift += 4;
s--;
}
if (end)
*end = e;
return addr;
}
int getaddrs(void **k, void **d, const char *str)
{
const char *s = str;
const char *end;
while (*s) {
if (*s == 'l')
if (strncmp(s, "loadaddrs=", 10) == 0)
break;
s++;
}
if (!*s)
return -1;
s += 10; /* skip over 'loadaddrs=' */
/* allow address to be 'appended' for some use cases */
if (strncmp(s, "appended", 8) == 0) {
*k = NULL;
end = s + 8;
} else
*k = gethexaddr(s, &end);
if (*end == ',')
s = end + 1;
else
return -1;
*d = gethexaddr(s, NULL);
return 0;
}
/**
* memchr - Find a character in an area of memory.
* @s: The memory area
* @c: The byte to search for
* @n: The size of the area.
*
* returns the address of the first occurrence of @c, or %NULL
* if @c is not found
*/
void *memchr(const void *s, int c, size_t n)
{
const unsigned char *p = s;
while (n-- != 0) {
if ((unsigned char)c == *p++) {
return (void *)(p - 1);
}
}
return NULL;
}
/**
* strchr - Find the first occurrence of a character in a string
* @s: The string to be searched
* @c: The character to search for
*/
char *strchr(const char *s, int c)
{
for (; *s != (char)c; ++s)
if (*s == '\0')
return NULL;
return (char *)s;
}
/**
* memset - Fill a region of memory with the given value
* @s: Pointer to the start of the area.
* @c: The byte to fill the area with
* @count: The size of the area.
*
* Do not use memset() to access IO space, use memset_io() instead.
*/
void *memset(void *s, int c, size_t count)
{
char *xs = s;
while (count--)
*xs++ = c;
return s;
}
/**
* memcpy - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should not use this function to access IO space, use memcpy_toio()
* or memcpy_fromio() instead.
*/
void *memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
const char *s = src;
while (count--)
*tmp++ = *s++;
return dest;
}
/**
* memmove - Copy one area of memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* Unlike memcpy(), memmove() copes with overlapping areas.
*/
void *memmove(void *dest, const void *src, size_t count)
{
char *tmp;
const char *s;
if (dest <= src) {
tmp = dest;
s = src;
while (count--)
*tmp++ = *s++;
} else {
tmp = dest;
tmp += count;
s = src;
s += count;
while (count--)
*--tmp = *--s;
}
return dest;
}
/**
* memcmp - Compare two areas of memory
* @cs: One area of memory
* @ct: Another area of memory
* @count: The size of the area.
*/
#undef memcmp
int memcmp(const void *cs, const void *ct, size_t count)
{
const unsigned char *su1, *su2;
int res = 0;
for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0)
break;
return res;
}