1 /*++
2
3 Copyright (c) 1998 Intel Corporation
4
5 Module Name:
6
7 str.c
8
9 Abstract:
10
11
12
13
14 Revision History
15
16 --*/
17
18 #include "lib.h"
19
20
21 INTN
StrCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2)22 StrCmp (
23 IN CONST CHAR16 *s1,
24 IN CONST CHAR16 *s2
25 )
26 // compare strings
27 {
28 return RtStrCmp(s1, s2);
29 }
30
31 INTN
StrnCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2,IN UINTN len)32 StrnCmp (
33 IN CONST CHAR16 *s1,
34 IN CONST CHAR16 *s2,
35 IN UINTN len
36 )
37 // compare strings
38 {
39 while (*s1 && len) {
40 if (*s1 != *s2) {
41 break;
42 }
43
44 s1 += 1;
45 s2 += 1;
46 len -= 1;
47 }
48
49 return len ? *s1 - *s2 : 0;
50 }
51
52
53 INTN EFIAPI
LibStubStriCmp(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * s1,IN CHAR16 * s2)54 LibStubStriCmp (
55 IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
56 IN CHAR16 *s1,
57 IN CHAR16 *s2
58 )
59 {
60 return StrCmp (s1, s2);
61 }
62
63 VOID EFIAPI
LibStubStrLwrUpr(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * Str EFI_UNUSED)64 LibStubStrLwrUpr (
65 IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
66 IN CHAR16 *Str EFI_UNUSED
67 )
68 {
69 }
70
71 INTN
StriCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2)72 StriCmp (
73 IN CONST CHAR16 *s1,
74 IN CONST CHAR16 *s2
75 )
76 // compare strings
77 {
78 if (UnicodeInterface == &LibStubUnicodeInterface)
79 return UnicodeInterface->StriColl(UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
80 else
81 return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
82 }
83
84 VOID
StrLwr(IN CHAR16 * Str)85 StrLwr (
86 IN CHAR16 *Str
87 )
88 // lwoer case string
89 {
90 if (UnicodeInterface == &LibStubUnicodeInterface)
91 UnicodeInterface->StrLwr(UnicodeInterface, Str);
92 else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
93 }
94
95 VOID
StrUpr(IN CHAR16 * Str)96 StrUpr (
97 IN CHAR16 *Str
98 )
99 // upper case string
100 {
101 if (UnicodeInterface == &LibStubUnicodeInterface)
102 UnicodeInterface->StrUpr(UnicodeInterface, Str);
103 else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
104 }
105
106 VOID
StrCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src)107 StrCpy (
108 IN CHAR16 *Dest,
109 IN CONST CHAR16 *Src
110 )
111 // copy strings
112 {
113 RtStrCpy (Dest, Src);
114 }
115
116 VOID
StrnCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)117 StrnCpy (
118 IN CHAR16 *Dest,
119 IN CONST CHAR16 *Src,
120 IN UINTN Len
121 )
122 // copy strings
123 {
124 RtStrnCpy (Dest, Src, Len);
125 }
126
127 CHAR16 *
StpCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src)128 StpCpy (
129 IN CHAR16 *Dest,
130 IN CONST CHAR16 *Src
131 )
132 // copy strings
133 {
134 return RtStpCpy (Dest, Src);
135 }
136
137 CHAR16 *
StpnCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)138 StpnCpy (
139 IN CHAR16 *Dest,
140 IN CONST CHAR16 *Src,
141 IN UINTN Len
142 )
143 // copy strings
144 {
145 return RtStpnCpy (Dest, Src, Len);
146 }
147
148 VOID
StrCat(IN CHAR16 * Dest,IN CONST CHAR16 * Src)149 StrCat (
150 IN CHAR16 *Dest,
151 IN CONST CHAR16 *Src
152 )
153 {
154 RtStrCat(Dest, Src);
155 }
156
157 VOID
StrnCat(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)158 StrnCat (
159 IN CHAR16 *Dest,
160 IN CONST CHAR16 *Src,
161 IN UINTN Len
162 )
163 {
164 RtStrnCat(Dest, Src, Len);
165 }
166
167
168 UINTN
StrnLen(IN CONST CHAR16 * s1,IN UINTN Len)169 StrnLen (
170 IN CONST CHAR16 *s1,
171 IN UINTN Len
172 )
173 // string length
174 {
175 return RtStrnLen(s1, Len);
176 }
177
178 UINTN
StrLen(IN CONST CHAR16 * s1)179 StrLen (
180 IN CONST CHAR16 *s1
181 )
182 // string length
183 {
184 return RtStrLen(s1);
185 }
186
187 UINTN
StrSize(IN CONST CHAR16 * s1)188 StrSize (
189 IN CONST CHAR16 *s1
190 )
191 // string size
192 {
193 return RtStrSize(s1);
194 }
195
196 CHAR16 *
StrDuplicate(IN CONST CHAR16 * Src)197 StrDuplicate (
198 IN CONST CHAR16 *Src
199 )
200 // duplicate a string
201 {
202 CHAR16 *Dest;
203 UINTN Size;
204
205 Size = StrSize(Src);
206 Dest = AllocatePool (Size);
207 if (Dest) {
208 CopyMem (Dest, (void *)Src, Size);
209 }
210 return Dest;
211 }
212
213 UINTN
strlena(IN CONST CHAR8 * s1)214 strlena (
215 IN CONST CHAR8 *s1
216 )
217 // string length
218 {
219 UINTN len;
220
221 for (len=0; *s1; s1+=1, len+=1) ;
222 return len;
223 }
224
225 UINTN
strcmpa(IN CONST CHAR8 * s1,IN CONST CHAR8 * s2)226 strcmpa (
227 IN CONST CHAR8 *s1,
228 IN CONST CHAR8 *s2
229 )
230 // compare strings
231 {
232 while (*s1) {
233 if (*s1 != *s2) {
234 break;
235 }
236
237 s1 += 1;
238 s2 += 1;
239 }
240
241 return *s1 - *s2;
242 }
243
244 UINTN
strncmpa(IN CONST CHAR8 * s1,IN CONST CHAR8 * s2,IN UINTN len)245 strncmpa (
246 IN CONST CHAR8 *s1,
247 IN CONST CHAR8 *s2,
248 IN UINTN len
249 )
250 // compare strings
251 {
252 while (*s1 && len) {
253 if (*s1 != *s2) {
254 break;
255 }
256
257 s1 += 1;
258 s2 += 1;
259 len -= 1;
260 }
261
262 return len ? *s1 - *s2 : 0;
263 }
264
265
266
267 UINTN
xtoi(CONST CHAR16 * str)268 xtoi (
269 CONST CHAR16 *str
270 )
271 // convert hex string to uint
272 {
273 UINTN u;
274 CHAR16 c;
275
276 // skip preceeding white space
277 while (*str == ' ') {
278 str += 1;
279 }
280
281 // convert hex digits
282 u = 0;
283 while ((c = *(str++))) {
284 if (c >= 'a' && c <= 'f') {
285 c -= 'a' - 'A';
286 }
287
288 if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
289 u = (u << 4) | ((UINTN)c - (c >= 'A' ? 'A'-10 : '0'));
290 } else {
291 break;
292 }
293 }
294
295 return u;
296 }
297
298 UINTN
Atoi(CONST CHAR16 * str)299 Atoi (
300 CONST CHAR16 *str
301 )
302 // convert hex string to uint
303 {
304 UINTN u;
305 CHAR16 c;
306
307 // skip preceeding white space
308 while (*str == ' ') {
309 str += 1;
310 }
311
312 // convert digits
313 u = 0;
314 while ((c = *(str++))) {
315 if (c >= '0' && c <= '9') {
316 u = (u * 10) + c - '0';
317 } else {
318 break;
319 }
320 }
321
322 return u;
323 }
324
325 BOOLEAN
MetaMatch(IN CHAR16 * String,IN CHAR16 * Pattern)326 MetaMatch (
327 IN CHAR16 *String,
328 IN CHAR16 *Pattern
329 )
330 {
331 CHAR16 c, p, l;
332
333 for (; ;) {
334 p = *Pattern;
335 Pattern += 1;
336
337 switch (p) {
338 case 0:
339 // End of pattern. If end of string, TRUE match
340 return *String ? FALSE : TRUE;
341
342 case '*':
343 // Match zero or more chars
344 while (*String) {
345 if (MetaMatch (String, Pattern)) {
346 return TRUE;
347 }
348 String += 1;
349 }
350 return MetaMatch (String, Pattern);
351
352 case '?':
353 // Match any one char
354 if (!*String) {
355 return FALSE;
356 }
357 String += 1;
358 break;
359
360 case '[':
361 // Match char set
362 c = *String;
363 if (!c) {
364 return FALSE; // syntax problem
365 }
366
367 l = 0;
368 while ((p = *Pattern++)) {
369 if (p == ']') {
370 return FALSE;
371 }
372
373 if (p == '-') { // if range of chars,
374 p = *Pattern; // get high range
375 if (p == 0 || p == ']') {
376 return FALSE; // syntax problem
377 }
378
379 if (c >= l && c <= p) { // if in range,
380 break; // it's a match
381 }
382 }
383
384 l = p;
385 if (c == p) { // if char matches
386 break; // move on
387 }
388 }
389
390 // skip to end of match char set
391 while (p && p != ']') {
392 p = *Pattern;
393 Pattern += 1;
394 }
395
396 String += 1;
397 break;
398
399 default:
400 c = *String;
401 if (c != p) {
402 return FALSE;
403 }
404
405 String += 1;
406 break;
407 }
408 }
409 }
410
411
412 BOOLEAN EFIAPI
LibStubMetaiMatch(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * String,IN CHAR16 * Pattern)413 LibStubMetaiMatch (
414 IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
415 IN CHAR16 *String,
416 IN CHAR16 *Pattern
417 )
418 {
419 return MetaMatch (String, Pattern);
420 }
421
422
423 BOOLEAN
MetaiMatch(IN CHAR16 * String,IN CHAR16 * Pattern)424 MetaiMatch (
425 IN CHAR16 *String,
426 IN CHAR16 *Pattern
427 )
428 {
429 if (UnicodeInterface == &LibStubUnicodeInterface)
430 return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
431 else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
432 }
433