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