1530d68baSNigel Croxon /*++
2530d68baSNigel Croxon
3530d68baSNigel Croxon Copyright (c) 1998 Intel Corporation
4530d68baSNigel Croxon
5530d68baSNigel Croxon Module Name:
6530d68baSNigel Croxon
7530d68baSNigel Croxon str.c
8530d68baSNigel Croxon
9530d68baSNigel Croxon Abstract:
10530d68baSNigel Croxon
11530d68baSNigel Croxon
12530d68baSNigel Croxon
13530d68baSNigel Croxon
14530d68baSNigel Croxon Revision History
15530d68baSNigel Croxon
16530d68baSNigel Croxon --*/
17530d68baSNigel Croxon
18530d68baSNigel Croxon #include "lib.h"
19530d68baSNigel Croxon
20530d68baSNigel Croxon
21530d68baSNigel Croxon INTN
StrCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2)22530d68baSNigel Croxon StrCmp (
23530d68baSNigel Croxon IN CONST CHAR16 *s1,
24530d68baSNigel Croxon IN CONST CHAR16 *s2
25530d68baSNigel Croxon )
26530d68baSNigel Croxon // compare strings
27530d68baSNigel Croxon {
28530d68baSNigel Croxon return RtStrCmp(s1, s2);
29530d68baSNigel Croxon }
30530d68baSNigel Croxon
31530d68baSNigel Croxon INTN
StrnCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2,IN UINTN len)32530d68baSNigel Croxon StrnCmp (
33530d68baSNigel Croxon IN CONST CHAR16 *s1,
34530d68baSNigel Croxon IN CONST CHAR16 *s2,
35530d68baSNigel Croxon IN UINTN len
36530d68baSNigel Croxon )
37530d68baSNigel Croxon // compare strings
38530d68baSNigel Croxon {
39530d68baSNigel Croxon while (*s1 && len) {
40530d68baSNigel Croxon if (*s1 != *s2) {
41530d68baSNigel Croxon break;
42530d68baSNigel Croxon }
43530d68baSNigel Croxon
44530d68baSNigel Croxon s1 += 1;
45530d68baSNigel Croxon s2 += 1;
46530d68baSNigel Croxon len -= 1;
47530d68baSNigel Croxon }
48530d68baSNigel Croxon
49530d68baSNigel Croxon return len ? *s1 - *s2 : 0;
50530d68baSNigel Croxon }
51530d68baSNigel Croxon
52530d68baSNigel Croxon
53530d68baSNigel Croxon INTN EFIAPI
LibStubStriCmp(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * s1,IN CHAR16 * s2)54530d68baSNigel Croxon LibStubStriCmp (
555ff74ecaSNigel Croxon IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
56530d68baSNigel Croxon IN CHAR16 *s1,
57530d68baSNigel Croxon IN CHAR16 *s2
58530d68baSNigel Croxon )
59530d68baSNigel Croxon {
60530d68baSNigel Croxon return StrCmp (s1, s2);
61530d68baSNigel Croxon }
62530d68baSNigel Croxon
63530d68baSNigel Croxon VOID EFIAPI
LibStubStrLwrUpr(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * Str EFI_UNUSED)64530d68baSNigel Croxon LibStubStrLwrUpr (
655ff74ecaSNigel Croxon IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
665ff74ecaSNigel Croxon IN CHAR16 *Str EFI_UNUSED
67530d68baSNigel Croxon )
68530d68baSNigel Croxon {
69530d68baSNigel Croxon }
70530d68baSNigel Croxon
71530d68baSNigel Croxon INTN
StriCmp(IN CONST CHAR16 * s1,IN CONST CHAR16 * s2)72530d68baSNigel Croxon StriCmp (
73530d68baSNigel Croxon IN CONST CHAR16 *s1,
74530d68baSNigel Croxon IN CONST CHAR16 *s2
75530d68baSNigel Croxon )
76530d68baSNigel Croxon // compare strings
77530d68baSNigel Croxon {
78530d68baSNigel Croxon if (UnicodeInterface == &LibStubUnicodeInterface)
79530d68baSNigel Croxon return UnicodeInterface->StriColl(UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
80530d68baSNigel Croxon else
81530d68baSNigel Croxon return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
82530d68baSNigel Croxon }
83530d68baSNigel Croxon
84530d68baSNigel Croxon VOID
StrLwr(IN CHAR16 * Str)85530d68baSNigel Croxon StrLwr (
86530d68baSNigel Croxon IN CHAR16 *Str
87530d68baSNigel Croxon )
88530d68baSNigel Croxon // lwoer case string
89530d68baSNigel Croxon {
90530d68baSNigel Croxon if (UnicodeInterface == &LibStubUnicodeInterface)
91530d68baSNigel Croxon UnicodeInterface->StrLwr(UnicodeInterface, Str);
92530d68baSNigel Croxon else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
93530d68baSNigel Croxon }
94530d68baSNigel Croxon
95530d68baSNigel Croxon VOID
StrUpr(IN CHAR16 * Str)96530d68baSNigel Croxon StrUpr (
97530d68baSNigel Croxon IN CHAR16 *Str
98530d68baSNigel Croxon )
99530d68baSNigel Croxon // upper case string
100530d68baSNigel Croxon {
101530d68baSNigel Croxon if (UnicodeInterface == &LibStubUnicodeInterface)
102530d68baSNigel Croxon UnicodeInterface->StrUpr(UnicodeInterface, Str);
103530d68baSNigel Croxon else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
104530d68baSNigel Croxon }
105530d68baSNigel Croxon
106530d68baSNigel Croxon VOID
StrCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src)107530d68baSNigel Croxon StrCpy (
108530d68baSNigel Croxon IN CHAR16 *Dest,
109530d68baSNigel Croxon IN CONST CHAR16 *Src
110530d68baSNigel Croxon )
111530d68baSNigel Croxon // copy strings
112530d68baSNigel Croxon {
113530d68baSNigel Croxon RtStrCpy (Dest, Src);
114530d68baSNigel Croxon }
115530d68baSNigel Croxon
116530d68baSNigel Croxon VOID
StrnCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)1179485c65fSPeter Jones StrnCpy (
1189485c65fSPeter Jones IN CHAR16 *Dest,
1199485c65fSPeter Jones IN CONST CHAR16 *Src,
1209485c65fSPeter Jones IN UINTN Len
1219485c65fSPeter Jones )
1229485c65fSPeter Jones // copy strings
1239485c65fSPeter Jones {
1249485c65fSPeter Jones RtStrnCpy (Dest, Src, Len);
1259485c65fSPeter Jones }
1269485c65fSPeter Jones
1279485c65fSPeter Jones CHAR16 *
StpCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src)1289485c65fSPeter Jones StpCpy (
1299485c65fSPeter Jones IN CHAR16 *Dest,
1309485c65fSPeter Jones IN CONST CHAR16 *Src
1319485c65fSPeter Jones )
1329485c65fSPeter Jones // copy strings
1339485c65fSPeter Jones {
1349485c65fSPeter Jones return RtStpCpy (Dest, Src);
1359485c65fSPeter Jones }
1369485c65fSPeter Jones
1379485c65fSPeter Jones CHAR16 *
StpnCpy(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)1389485c65fSPeter Jones StpnCpy (
1399485c65fSPeter Jones IN CHAR16 *Dest,
1409485c65fSPeter Jones IN CONST CHAR16 *Src,
1419485c65fSPeter Jones IN UINTN Len
1429485c65fSPeter Jones )
1439485c65fSPeter Jones // copy strings
1449485c65fSPeter Jones {
1459485c65fSPeter Jones return RtStpnCpy (Dest, Src, Len);
1469485c65fSPeter Jones }
1479485c65fSPeter Jones
1489485c65fSPeter Jones VOID
StrCat(IN CHAR16 * Dest,IN CONST CHAR16 * Src)149530d68baSNigel Croxon StrCat (
150530d68baSNigel Croxon IN CHAR16 *Dest,
151530d68baSNigel Croxon IN CONST CHAR16 *Src
152530d68baSNigel Croxon )
153530d68baSNigel Croxon {
154530d68baSNigel Croxon RtStrCat(Dest, Src);
155530d68baSNigel Croxon }
156530d68baSNigel Croxon
1579485c65fSPeter Jones VOID
StrnCat(IN CHAR16 * Dest,IN CONST CHAR16 * Src,IN UINTN Len)1589485c65fSPeter Jones StrnCat (
1599485c65fSPeter Jones IN CHAR16 *Dest,
1609485c65fSPeter Jones IN CONST CHAR16 *Src,
1619485c65fSPeter Jones IN UINTN Len
1629485c65fSPeter Jones )
1639485c65fSPeter Jones {
1649485c65fSPeter Jones RtStrnCat(Dest, Src, Len);
1659485c65fSPeter Jones }
1669485c65fSPeter Jones
1679485c65fSPeter Jones
1689485c65fSPeter Jones UINTN
StrnLen(IN CONST CHAR16 * s1,IN UINTN Len)1699485c65fSPeter Jones StrnLen (
1709485c65fSPeter Jones IN CONST CHAR16 *s1,
1719485c65fSPeter Jones IN UINTN Len
1729485c65fSPeter Jones )
1739485c65fSPeter Jones // string length
1749485c65fSPeter Jones {
1759485c65fSPeter Jones return RtStrnLen(s1, Len);
1769485c65fSPeter Jones }
1779485c65fSPeter Jones
178530d68baSNigel Croxon UINTN
StrLen(IN CONST CHAR16 * s1)179530d68baSNigel Croxon StrLen (
180530d68baSNigel Croxon IN CONST CHAR16 *s1
181530d68baSNigel Croxon )
182530d68baSNigel Croxon // string length
183530d68baSNigel Croxon {
184530d68baSNigel Croxon return RtStrLen(s1);
185530d68baSNigel Croxon }
186530d68baSNigel Croxon
187530d68baSNigel Croxon UINTN
StrSize(IN CONST CHAR16 * s1)188530d68baSNigel Croxon StrSize (
189530d68baSNigel Croxon IN CONST CHAR16 *s1
190530d68baSNigel Croxon )
191530d68baSNigel Croxon // string size
192530d68baSNigel Croxon {
193530d68baSNigel Croxon return RtStrSize(s1);
194530d68baSNigel Croxon }
195530d68baSNigel Croxon
196530d68baSNigel Croxon CHAR16 *
StrDuplicate(IN CONST CHAR16 * Src)197530d68baSNigel Croxon StrDuplicate (
198530d68baSNigel Croxon IN CONST CHAR16 *Src
199530d68baSNigel Croxon )
200530d68baSNigel Croxon // duplicate a string
201530d68baSNigel Croxon {
202530d68baSNigel Croxon CHAR16 *Dest;
203530d68baSNigel Croxon UINTN Size;
204530d68baSNigel Croxon
205530d68baSNigel Croxon Size = StrSize(Src);
206530d68baSNigel Croxon Dest = AllocatePool (Size);
207530d68baSNigel Croxon if (Dest) {
208*deb8a7f2SPeter Jones CopyMem (Dest, (void *)Src, Size);
209530d68baSNigel Croxon }
210530d68baSNigel Croxon return Dest;
211530d68baSNigel Croxon }
212530d68baSNigel Croxon
213530d68baSNigel Croxon UINTN
strlena(IN CONST CHAR8 * s1)214530d68baSNigel Croxon strlena (
215530d68baSNigel Croxon IN CONST CHAR8 *s1
216530d68baSNigel Croxon )
217530d68baSNigel Croxon // string length
218530d68baSNigel Croxon {
219530d68baSNigel Croxon UINTN len;
220530d68baSNigel Croxon
221530d68baSNigel Croxon for (len=0; *s1; s1+=1, len+=1) ;
222530d68baSNigel Croxon return len;
223530d68baSNigel Croxon }
224530d68baSNigel Croxon
225530d68baSNigel Croxon UINTN
strcmpa(IN CONST CHAR8 * s1,IN CONST CHAR8 * s2)226530d68baSNigel Croxon strcmpa (
227530d68baSNigel Croxon IN CONST CHAR8 *s1,
228530d68baSNigel Croxon IN CONST CHAR8 *s2
229530d68baSNigel Croxon )
230530d68baSNigel Croxon // compare strings
231530d68baSNigel Croxon {
232530d68baSNigel Croxon while (*s1) {
233530d68baSNigel Croxon if (*s1 != *s2) {
234530d68baSNigel Croxon break;
235530d68baSNigel Croxon }
236530d68baSNigel Croxon
237530d68baSNigel Croxon s1 += 1;
238530d68baSNigel Croxon s2 += 1;
239530d68baSNigel Croxon }
240530d68baSNigel Croxon
241530d68baSNigel Croxon return *s1 - *s2;
242530d68baSNigel Croxon }
243530d68baSNigel Croxon
244530d68baSNigel Croxon UINTN
strncmpa(IN CONST CHAR8 * s1,IN CONST CHAR8 * s2,IN UINTN len)245530d68baSNigel Croxon strncmpa (
246530d68baSNigel Croxon IN CONST CHAR8 *s1,
247530d68baSNigel Croxon IN CONST CHAR8 *s2,
248530d68baSNigel Croxon IN UINTN len
249530d68baSNigel Croxon )
250530d68baSNigel Croxon // compare strings
251530d68baSNigel Croxon {
252530d68baSNigel Croxon while (*s1 && len) {
253530d68baSNigel Croxon if (*s1 != *s2) {
254530d68baSNigel Croxon break;
255530d68baSNigel Croxon }
256530d68baSNigel Croxon
257530d68baSNigel Croxon s1 += 1;
258530d68baSNigel Croxon s2 += 1;
259530d68baSNigel Croxon len -= 1;
260530d68baSNigel Croxon }
261530d68baSNigel Croxon
262530d68baSNigel Croxon return len ? *s1 - *s2 : 0;
263530d68baSNigel Croxon }
264530d68baSNigel Croxon
265530d68baSNigel Croxon
266530d68baSNigel Croxon
267530d68baSNigel Croxon UINTN
xtoi(CONST CHAR16 * str)268530d68baSNigel Croxon xtoi (
269530d68baSNigel Croxon CONST CHAR16 *str
270530d68baSNigel Croxon )
271530d68baSNigel Croxon // convert hex string to uint
272530d68baSNigel Croxon {
273530d68baSNigel Croxon UINTN u;
274530d68baSNigel Croxon CHAR16 c;
275530d68baSNigel Croxon
276530d68baSNigel Croxon // skip preceeding white space
2771771b6e7SHeinrich Schuchardt while (*str == ' ') {
278530d68baSNigel Croxon str += 1;
279530d68baSNigel Croxon }
280530d68baSNigel Croxon
281530d68baSNigel Croxon // convert hex digits
282530d68baSNigel Croxon u = 0;
283530d68baSNigel Croxon while ((c = *(str++))) {
284530d68baSNigel Croxon if (c >= 'a' && c <= 'f') {
285530d68baSNigel Croxon c -= 'a' - 'A';
286530d68baSNigel Croxon }
287530d68baSNigel Croxon
288530d68baSNigel Croxon if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
2894ef18335SPete Batard u = (u << 4) | ((UINTN)c - (c >= 'A' ? 'A'-10 : '0'));
290530d68baSNigel Croxon } else {
291530d68baSNigel Croxon break;
292530d68baSNigel Croxon }
293530d68baSNigel Croxon }
294530d68baSNigel Croxon
295530d68baSNigel Croxon return u;
296530d68baSNigel Croxon }
297530d68baSNigel Croxon
298530d68baSNigel Croxon UINTN
Atoi(CONST CHAR16 * str)299530d68baSNigel Croxon Atoi (
300530d68baSNigel Croxon CONST CHAR16 *str
301530d68baSNigel Croxon )
302530d68baSNigel Croxon // convert hex string to uint
303530d68baSNigel Croxon {
304530d68baSNigel Croxon UINTN u;
305530d68baSNigel Croxon CHAR16 c;
306530d68baSNigel Croxon
307530d68baSNigel Croxon // skip preceeding white space
308e2fc15aeSHeinrich Schuchardt while (*str == ' ') {
309530d68baSNigel Croxon str += 1;
310530d68baSNigel Croxon }
311530d68baSNigel Croxon
312530d68baSNigel Croxon // convert digits
313530d68baSNigel Croxon u = 0;
314530d68baSNigel Croxon while ((c = *(str++))) {
315530d68baSNigel Croxon if (c >= '0' && c <= '9') {
316530d68baSNigel Croxon u = (u * 10) + c - '0';
317530d68baSNigel Croxon } else {
318530d68baSNigel Croxon break;
319530d68baSNigel Croxon }
320530d68baSNigel Croxon }
321530d68baSNigel Croxon
322530d68baSNigel Croxon return u;
323530d68baSNigel Croxon }
324530d68baSNigel Croxon
325530d68baSNigel Croxon BOOLEAN
MetaMatch(IN CHAR16 * String,IN CHAR16 * Pattern)326530d68baSNigel Croxon MetaMatch (
327530d68baSNigel Croxon IN CHAR16 *String,
328530d68baSNigel Croxon IN CHAR16 *Pattern
329530d68baSNigel Croxon )
330530d68baSNigel Croxon {
331530d68baSNigel Croxon CHAR16 c, p, l;
332530d68baSNigel Croxon
333530d68baSNigel Croxon for (; ;) {
334530d68baSNigel Croxon p = *Pattern;
335530d68baSNigel Croxon Pattern += 1;
336530d68baSNigel Croxon
337530d68baSNigel Croxon switch (p) {
338530d68baSNigel Croxon case 0:
339530d68baSNigel Croxon // End of pattern. If end of string, TRUE match
340530d68baSNigel Croxon return *String ? FALSE : TRUE;
341530d68baSNigel Croxon
342530d68baSNigel Croxon case '*':
343530d68baSNigel Croxon // Match zero or more chars
344530d68baSNigel Croxon while (*String) {
345530d68baSNigel Croxon if (MetaMatch (String, Pattern)) {
346530d68baSNigel Croxon return TRUE;
347530d68baSNigel Croxon }
348530d68baSNigel Croxon String += 1;
349530d68baSNigel Croxon }
350530d68baSNigel Croxon return MetaMatch (String, Pattern);
351530d68baSNigel Croxon
352530d68baSNigel Croxon case '?':
353530d68baSNigel Croxon // Match any one char
354530d68baSNigel Croxon if (!*String) {
355530d68baSNigel Croxon return FALSE;
356530d68baSNigel Croxon }
357530d68baSNigel Croxon String += 1;
358530d68baSNigel Croxon break;
359530d68baSNigel Croxon
360530d68baSNigel Croxon case '[':
361530d68baSNigel Croxon // Match char set
362530d68baSNigel Croxon c = *String;
363530d68baSNigel Croxon if (!c) {
364530d68baSNigel Croxon return FALSE; // syntax problem
365530d68baSNigel Croxon }
366530d68baSNigel Croxon
367530d68baSNigel Croxon l = 0;
368530d68baSNigel Croxon while ((p = *Pattern++)) {
369530d68baSNigel Croxon if (p == ']') {
370530d68baSNigel Croxon return FALSE;
371530d68baSNigel Croxon }
372530d68baSNigel Croxon
373530d68baSNigel Croxon if (p == '-') { // if range of chars,
374530d68baSNigel Croxon p = *Pattern; // get high range
375530d68baSNigel Croxon if (p == 0 || p == ']') {
376530d68baSNigel Croxon return FALSE; // syntax problem
377530d68baSNigel Croxon }
378530d68baSNigel Croxon
379530d68baSNigel Croxon if (c >= l && c <= p) { // if in range,
380530d68baSNigel Croxon break; // it's a match
381530d68baSNigel Croxon }
382530d68baSNigel Croxon }
383530d68baSNigel Croxon
384530d68baSNigel Croxon l = p;
385530d68baSNigel Croxon if (c == p) { // if char matches
386530d68baSNigel Croxon break; // move on
387530d68baSNigel Croxon }
388530d68baSNigel Croxon }
389530d68baSNigel Croxon
390530d68baSNigel Croxon // skip to end of match char set
391530d68baSNigel Croxon while (p && p != ']') {
392530d68baSNigel Croxon p = *Pattern;
393530d68baSNigel Croxon Pattern += 1;
394530d68baSNigel Croxon }
395530d68baSNigel Croxon
396530d68baSNigel Croxon String += 1;
397530d68baSNigel Croxon break;
398530d68baSNigel Croxon
399530d68baSNigel Croxon default:
400530d68baSNigel Croxon c = *String;
401530d68baSNigel Croxon if (c != p) {
402530d68baSNigel Croxon return FALSE;
403530d68baSNigel Croxon }
404530d68baSNigel Croxon
405530d68baSNigel Croxon String += 1;
406530d68baSNigel Croxon break;
407530d68baSNigel Croxon }
408530d68baSNigel Croxon }
409530d68baSNigel Croxon }
410530d68baSNigel Croxon
411530d68baSNigel Croxon
412530d68baSNigel Croxon BOOLEAN EFIAPI
LibStubMetaiMatch(IN EFI_UNICODE_COLLATION_INTERFACE * This EFI_UNUSED,IN CHAR16 * String,IN CHAR16 * Pattern)413530d68baSNigel Croxon LibStubMetaiMatch (
4145ff74ecaSNigel Croxon IN EFI_UNICODE_COLLATION_INTERFACE *This EFI_UNUSED,
415530d68baSNigel Croxon IN CHAR16 *String,
416530d68baSNigel Croxon IN CHAR16 *Pattern
417530d68baSNigel Croxon )
418530d68baSNigel Croxon {
419530d68baSNigel Croxon return MetaMatch (String, Pattern);
420530d68baSNigel Croxon }
421530d68baSNigel Croxon
422530d68baSNigel Croxon
423530d68baSNigel Croxon BOOLEAN
MetaiMatch(IN CHAR16 * String,IN CHAR16 * Pattern)424530d68baSNigel Croxon MetaiMatch (
425530d68baSNigel Croxon IN CHAR16 *String,
426530d68baSNigel Croxon IN CHAR16 *Pattern
427530d68baSNigel Croxon )
428530d68baSNigel Croxon {
429530d68baSNigel Croxon if (UnicodeInterface == &LibStubUnicodeInterface)
430530d68baSNigel Croxon return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
431530d68baSNigel Croxon else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
432530d68baSNigel Croxon }
433