Debian Codegraph @

gnu-efi/lib/str.c

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