-
Notifications
You must be signed in to change notification settings - Fork 297
/
string.c
284 lines (240 loc) · 4.3 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* string.c - implementations we need for string finctions
*/
#define SHIM_STRING_C_
#include "shim.h"
#ifdef SHIM_UNIT_TEST
#define strlen shim_strlen
#ifdef strcmp
#undef strcmp
#endif
#define strcmp shim_strcmp
#ifdef strncmp
#undef strncmp
#endif
#define strncmp shim_strncmp
#define strncasecmp shim_strncasecmp
#define strcasecmp shim_strcasecmp
#define strrchr shim_strrchr
#define strlen shim_strlen
#define strnlen shim_strnlen
#define strcpy shim_strcpy
#ifdef strncpy
#undef strncpy
#endif
#define strncpy shim_strncpy
#ifdef strdup
#undef strdup
#endif
#define strdup shim_strdup
#ifdef strndup
#undef strndup
#endif
#define strndup shim_strndup
#ifdef stpcpy
#undef stpcpy
#endif
#define stpcpy shim_stpcpy
#define strchrnul shim_strchrnul
#ifdef strchr
#undef strchr
#endif
#define strchr shim_strchr
#endif
size_t
strlen(const char *s1)
{
size_t len;
for (len = 0; *s1; s1 += 1, len += 1)
;
return len;
}
int
strcmp(const char *s1p, const char *s2p)
{
const uint8_t *s1 = (const uint8_t *)s1p;
const uint8_t *s2 = (const uint8_t *)s2p;
while (*s1) {
if (*s1 != *s2) {
break;
}
s1 += 1;
s2 += 1;
}
return *s1 - *s2;
}
int
strncmp(const char *s1p, const char *s2p, size_t len)
{
const uint8_t *s1 = (const uint8_t *)s1p;
const uint8_t *s2 = (const uint8_t *)s2p;
while (*s1 && len) {
if (*s1 != *s2) {
break;
}
s1 += 1;
s2 += 1;
len -= 1;
}
return len ? *s1 - *s2 : 0;
}
/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
int
strncasecmp(const char *s1p, const char *s2p, size_t n)
{
const uint8_t *s1 = (const uint8_t *)s1p;
const uint8_t *s2 = (const uint8_t *)s2p;
while (*s1 && n) {
if (toupper(*s1) != toupper(*s2)) {
break;
}
s1 += 1;
s2 += 1;
n -= 1;
}
return n ? *s1 - *s2 : 0;
}
/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
int
strcasecmp(const char *str1, const char *str2)
{
uint8_t c1, c2;
c1 = toupper(*str1);
c2 = toupper(*str2);
while ((*str1 != '\0') && (c1 == c2)) {
str1++;
str2++;
c1 = toupper(*str1);
c2 = toupper(*str2);
}
return c1 - c2;
}
/* Scan a string for the last occurrence of a character */
char *
strrchr(const char *str, int c)
{
char *save;
for (save = NULL;; ++str) {
if (*str == c) {
save = (char *)str;
}
if (*str == 0) {
return (save);
}
}
}
NONNULL(1)
size_t
strnlen(const char *s, size_t n)
{
size_t i;
for (i = 0; i < n; i++)
if (s[i] == '\0')
break;
return i;
}
RETURNS_NONNULL NONNULL(1, 2)
char *
strcpy(char *dest, const char *src)
{
size_t i;
for (i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
return dest;
}
RETURNS_NONNULL NONNULL(1, 2)
char *
strncpy(char *dest, const char *src, size_t n)
{
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[i] = src[i];
if (i < n)
dest[i] = '\0';
return dest;
}
RETURNS_NONNULL NONNULL(1, 2)
char *
strcat(char *dest, const char *src)
{
size_t dest_len = strlen(dest);
size_t i;
for (i = 0; src[i] != '\0'; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
return dest;
}
NONNULL(1)
char *
strdup(const char *const src)
{
size_t len;
char *news = NULL;
len = strlen(src);
news = AllocateZeroPool(len + 1);
if (news)
strncpy(news, src, len);
return news;
}
NONNULL(1)
char *
strndup(const char *const src, const size_t srcmax)
{
size_t len;
char *news = NULL;
len = strnlen(src, srcmax);
news = AllocateZeroPool(len + 1);
if (news)
strncpy(news, src, len);
return news;
}
RETURNS_NONNULL NONNULL(1, 2)
char *
stpcpy(char *dest, const char *const src)
{
size_t i = 0;
for (i = 0; src[i]; i++)
dest[i] = src[i];
dest[i] = '\000';
return &dest[i];
}
RETURNS_NONNULL NONNULL(1)
char *
strchrnul(const char *s, int c)
{
unsigned int i;
for (i = 0; s[i] != '\000' && s[i] != c; i++)
;
return (char *)&s[i];
}
NONNULL(1)
char *
strchr(const char *s, int c)
{
const char *s1;
s1 = strchrnul(s, c);
if (!s1 || s1[0] == '\000')
return NULL;
return (char *)s1;
}
char *
translate_slashes(char *out, const char *str)
{
int i;
int j;
if (str == NULL || out == NULL)
return NULL;
for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
if (str[i] == '\\') {
out[j] = '/';
if (str[i + 1] == '\\')
i++;
} else
out[j] = str[i];
}
out[j] = '\0';
return out;
}
// vim:fenc=utf-8:tw=75:noet