]>
Commit | Line | Data |
---|---|---|
e1f414b6 | 1 | /** @file\r |
2 | Unicode and ASCII string primatives.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2007, Intel Corporation<BR>\r | |
5 | All rights reserved. This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
e1f414b6 | 13 | **/\r |
14 | \r | |
15 | //\r | |
16 | // Include common header file for this module.\r | |
17 | //\r | |
f734a10a | 18 | \r |
e1f414b6 | 19 | \r |
20 | #include "BaseLibInternals.h"\r | |
21 | \r | |
22 | /**\r | |
23 | Copies one Null-terminated Unicode string to another Null-terminated Unicode\r | |
24 | string and returns the new Unicode string.\r | |
25 | \r | |
26 | This function copies the contents of the Unicode string Source to the Unicode\r | |
27 | string Destination, and returns Destination. If Source and Destination\r | |
28 | overlap, then the results are undefined.\r | |
29 | \r | |
30 | If Destination is NULL, then ASSERT().\r | |
31 | If Destination is not aligned on a 16-bit boundary, then ASSERT().\r | |
32 | If Source is NULL, then ASSERT().\r | |
33 | If Source is not aligned on a 16-bit boundary, then ASSERT().\r | |
34 | If Source and Destination overlap, then ASSERT().\r | |
35 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r | |
36 | PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
37 | Null-terminator, then ASSERT().\r | |
38 | \r | |
39 | @param Destination Pointer to a Null-terminated Unicode string.\r | |
40 | @param Source Pointer to a Null-terminated Unicode string.\r | |
41 | \r | |
42 | @return Destiantion\r | |
43 | \r | |
44 | **/\r | |
45 | CHAR16 *\r | |
46 | EFIAPI\r | |
47 | StrCpy (\r | |
48 | OUT CHAR16 *Destination,\r | |
49 | IN CONST CHAR16 *Source\r | |
50 | )\r | |
51 | {\r | |
52 | CHAR16 *ReturnValue;\r | |
53 | \r | |
54 | //\r | |
55 | // Destination cannot be NULL\r | |
56 | //\r | |
57 | ASSERT (Destination != NULL);\r | |
58 | ASSERT (((UINTN) Destination & 0x01) == 0);\r | |
59 | \r | |
60 | //\r | |
61 | // Destination and source cannot overlap\r | |
62 | //\r | |
63 | ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r | |
64 | ASSERT ((UINTN)(Source - Destination) > StrLen (Source));\r | |
65 | \r | |
66 | ReturnValue = Destination;\r | |
67 | while (*Source) {\r | |
68 | *(Destination++) = *(Source++);\r | |
69 | }\r | |
70 | *Destination = 0;\r | |
71 | return ReturnValue;\r | |
72 | }\r | |
73 | \r | |
74 | /**\r | |
75 | Copies one Null-terminated Unicode string with a maximum length to another\r | |
76 | Null-terminated Unicode string with a maximum length and returns the new\r | |
77 | Unicode string.\r | |
78 | \r | |
79 | This function copies the contents of the Unicode string Source to the Unicode\r | |
80 | string Destination, and returns Destination. At most, Length Unicode\r | |
81 | characters are copied from Source to Destination. If Length is 0, then\r | |
82 | Destination is returned unmodified. If Length is greater that the number of\r | |
83 | Unicode characters in Source, then Destination is padded with Null Unicode\r | |
84 | characters. If Source and Destination overlap, then the results are\r | |
85 | undefined.\r | |
86 | \r | |
87 | If Length > 0 and Destination is NULL, then ASSERT().\r | |
88 | If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r | |
89 | If Length > 0 and Source is NULL, then ASSERT().\r | |
90 | If Length > 0 and Source is not aligned on a 16-bit bounadry, then ASSERT().\r | |
91 | If Source and Destination overlap, then ASSERT().\r | |
92 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r | |
93 | PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
94 | Null-terminator, then ASSERT().\r | |
95 | \r | |
96 | @param Destination Pointer to a Null-terminated Unicode string.\r | |
97 | @param Source Pointer to a Null-terminated Unicode string.\r | |
98 | @param Length Maximum number of Unicode characters to copy.\r | |
99 | \r | |
100 | @return Destination\r | |
101 | \r | |
102 | **/\r | |
103 | CHAR16 *\r | |
104 | EFIAPI\r | |
105 | StrnCpy (\r | |
106 | OUT CHAR16 *Destination,\r | |
107 | IN CONST CHAR16 *Source,\r | |
108 | IN UINTN Length\r | |
109 | )\r | |
110 | {\r | |
111 | CHAR16 *ReturnValue;\r | |
112 | \r | |
113 | if (Length == 0) {\r | |
114 | return Destination;\r | |
115 | }\r | |
116 | \r | |
117 | //\r | |
118 | // Destination cannot be NULL if Length is not zero\r | |
119 | //\r | |
120 | ASSERT (Destination != NULL);\r | |
121 | ASSERT (((UINTN) Destination & 0x01) == 0);\r | |
122 | \r | |
123 | //\r | |
124 | // Destination and source cannot overlap\r | |
125 | // Q: Does Source have to be NULL-terminated?\r | |
126 | //\r | |
127 | ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r | |
128 | ASSERT ((UINTN)(Source - Destination) >= Length);\r | |
129 | \r | |
130 | ReturnValue = Destination;\r | |
131 | \r | |
132 | while ((*Source != L'\0') && (Length > 0)) {\r | |
133 | *(Destination++) = *(Source++);\r | |
134 | Length--;\r | |
135 | }\r | |
136 | \r | |
137 | ZeroMem (Destination, Length * sizeof (*Destination));\r | |
138 | return ReturnValue;\r | |
139 | }\r | |
140 | \r | |
141 | /**\r | |
142 | Returns the length of a Null-terminated Unicode string.\r | |
143 | \r | |
144 | This function returns the number of Unicode characters in the Null-terminated\r | |
145 | Unicode string specified by String.\r | |
146 | \r | |
147 | If String is NULL, then ASSERT().\r | |
148 | If String is not aligned on a 16-bit boundary, then ASSERT().\r | |
149 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r | |
150 | PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
151 | Null-terminator, then ASSERT().\r | |
152 | \r | |
153 | @param String Pointer to a Null-terminated Unicode string.\r | |
154 | \r | |
155 | @return The length of String.\r | |
156 | \r | |
157 | **/\r | |
158 | UINTN\r | |
159 | EFIAPI\r | |
160 | StrLen (\r | |
161 | IN CONST CHAR16 *String\r | |
162 | )\r | |
163 | {\r | |
164 | UINTN Length;\r | |
165 | \r | |
166 | ASSERT (String != NULL);\r | |
167 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
168 | \r | |
169 | for (Length = 0; *String != L'\0'; String++, Length++) {\r | |
170 | //\r | |
171 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
172 | // length should not more than PcdMaximumUnicodeStringLength\r | |
173 | //\r | |
174 | if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r | |
175 | ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
176 | }\r | |
177 | }\r | |
178 | return Length;\r | |
179 | }\r | |
180 | \r | |
181 | /**\r | |
182 | Returns the size of a Null-terminated Unicode string in bytes, including the\r | |
183 | Null terminator.\r | |
184 | \r | |
185 | This function returns the size, in bytes, of the Null-terminated Unicode\r | |
186 | string specified by String.\r | |
187 | \r | |
188 | If String is NULL, then ASSERT().\r | |
189 | If String is not aligned on a 16-bit boundary, then ASSERT().\r | |
190 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r | |
191 | PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
192 | Null-terminator, then ASSERT().\r | |
193 | \r | |
194 | @param String Pointer to a Null-terminated Unicode string.\r | |
195 | \r | |
196 | @return The size of String.\r | |
197 | \r | |
198 | **/\r | |
199 | UINTN\r | |
200 | EFIAPI\r | |
201 | StrSize (\r | |
202 | IN CONST CHAR16 *String\r | |
203 | )\r | |
204 | {\r | |
205 | return (StrLen (String) + 1) * sizeof (*String);\r | |
206 | }\r | |
207 | \r | |
208 | /**\r | |
209 | Compares two Null-terminated Unicode strings, and returns the difference\r | |
210 | between the first mismatched Unicode characters.\r | |
211 | \r | |
212 | This function compares the Null-terminated Unicode string FirstString to the\r | |
213 | Null-terminated Unicode string SecondString. If FirstString is identical to\r | |
214 | SecondString, then 0 is returned. Otherwise, the value returned is the first\r | |
215 | mismatched Unicode character in SecondString subtracted from the first\r | |
216 | mismatched Unicode character in FirstString.\r | |
217 | \r | |
218 | If FirstString is NULL, then ASSERT().\r | |
219 | If FirstString is not aligned on a 16-bit boundary, then ASSERT().\r | |
220 | If SecondString is NULL, then ASSERT().\r | |
221 | If SecondString is not aligned on a 16-bit boundary, then ASSERT().\r | |
222 | If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r | |
223 | than PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
224 | Null-terminator, then ASSERT().\r | |
225 | If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r | |
226 | than PcdMaximumUnicodeStringLength Unicode characters not including the \r | |
227 | Null-terminator, then ASSERT().\r | |
228 | \r | |
229 | @param FirstString Pointer to a Null-terminated Unicode string.\r | |
230 | @param SecondString Pointer to a Null-terminated Unicode string.\r | |
231 | \r | |
232 | @retval 0 FirstString is identical to SecondString.\r | |
233 | @retval !=0 FirstString is not identical to SecondString.\r | |
234 | \r | |
235 | **/\r | |
236 | INTN\r | |
237 | EFIAPI\r | |
238 | StrCmp (\r | |
239 | IN CONST CHAR16 *FirstString,\r | |
240 | IN CONST CHAR16 *SecondString\r | |
241 | )\r | |
242 | {\r | |
243 | //\r | |
244 | // ASSERT both strings are less long than PcdMaximumUnicodeStringLength\r | |
245 | //\r | |
246 | ASSERT (StrSize (FirstString) != 0);\r | |
247 | ASSERT (StrSize (SecondString) != 0);\r | |
248 | \r | |
249 | while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {\r | |
250 | FirstString++;\r | |
251 | SecondString++;\r | |
252 | }\r | |
253 | return *FirstString - *SecondString;\r | |
254 | }\r | |
255 | \r | |
256 | /**\r | |
257 | Compares two Null-terminated Unicode strings with maximum lengths, and\r | |
258 | returns the difference between the first mismatched Unicode characters.\r | |
259 | \r | |
260 | This function compares the Null-terminated Unicode string FirstString to the\r | |
261 | Null-terminated Unicode string SecondString. At most, Length Unicode\r | |
262 | characters will be compared. If Length is 0, then 0 is returned. If\r | |
263 | FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r | |
264 | value returned is the first mismatched Unicode character in SecondString\r | |
265 | subtracted from the first mismatched Unicode character in FirstString.\r | |
266 | \r | |
267 | If Length > 0 and FirstString is NULL, then ASSERT().\r | |
268 | If Length > 0 and FirstString is not aligned on a 16-bit bounadary, then ASSERT().\r | |
269 | If Length > 0 and SecondString is NULL, then ASSERT().\r | |
270 | If Length > 0 and SecondString is not aligned on a 16-bit bounadary, then ASSERT().\r | |
271 | If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r | |
272 | than PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
273 | Null-terminator, then ASSERT().\r | |
274 | If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r | |
275 | than PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
276 | Null-terminator, then ASSERT().\r | |
277 | \r | |
278 | @param FirstString Pointer to a Null-terminated Unicode string.\r | |
279 | @param SecondString Pointer to a Null-terminated Unicode string.\r | |
280 | @param Length Maximum number of Unicode characters to compare.\r | |
281 | \r | |
282 | @retval 0 FirstString is identical to SecondString.\r | |
283 | @retval !=0 FirstString is not identical to SecondString.\r | |
284 | \r | |
285 | **/\r | |
286 | INTN\r | |
287 | EFIAPI\r | |
288 | StrnCmp (\r | |
289 | IN CONST CHAR16 *FirstString,\r | |
290 | IN CONST CHAR16 *SecondString,\r | |
291 | IN UINTN Length\r | |
292 | )\r | |
293 | {\r | |
294 | if (Length == 0) {\r | |
295 | return 0;\r | |
296 | }\r | |
297 | \r | |
298 | //\r | |
299 | // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r | |
300 | // Length tests are performed inside StrLen().\r | |
301 | //\r | |
302 | ASSERT (StrSize (FirstString) != 0);\r | |
303 | ASSERT (StrSize (SecondString) != 0);\r | |
304 | \r | |
305 | while ((*FirstString != L'\0') &&\r | |
306 | (*FirstString == *SecondString) &&\r | |
307 | (Length > 1)) {\r | |
308 | FirstString++;\r | |
309 | SecondString++;\r | |
310 | Length--;\r | |
311 | }\r | |
312 | \r | |
313 | return *FirstString - *SecondString;\r | |
314 | }\r | |
315 | \r | |
316 | /**\r | |
317 | Concatenates one Null-terminated Unicode string to another Null-terminated\r | |
318 | Unicode string, and returns the concatenated Unicode string.\r | |
319 | \r | |
320 | This function concatenates two Null-terminated Unicode strings. The contents\r | |
321 | of Null-terminated Unicode string Source are concatenated to the end of\r | |
322 | Null-terminated Unicode string Destination. The Null-terminated concatenated\r | |
323 | Unicode String is returned. If Source and Destination overlap, then the\r | |
324 | results are undefined.\r | |
325 | \r | |
326 | If Destination is NULL, then ASSERT().\r | |
327 | If Source is NULL, then ASSERT().\r | |
328 | If Source and Destination overlap, then ASSERT().\r | |
329 | If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r | |
330 | than PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
331 | Null-terminator, then ASSERT().\r | |
332 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r | |
333 | PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
334 | Null-terminator, then ASSERT().\r | |
335 | If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r | |
336 | and Source results in a Unicode string with more than\r | |
337 | PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
338 | Null-terminator, then ASSERT().\r | |
339 | \r | |
340 | @param Destination Pointer to a Null-terminated Unicode string.\r | |
341 | @param Source Pointer to a Null-terminated Unicode string.\r | |
342 | \r | |
343 | @return Destination\r | |
344 | \r | |
345 | **/\r | |
346 | CHAR16 *\r | |
347 | EFIAPI\r | |
348 | StrCat (\r | |
349 | IN OUT CHAR16 *Destination,\r | |
350 | IN CONST CHAR16 *Source\r | |
351 | )\r | |
352 | {\r | |
353 | StrCpy (Destination + StrLen (Destination), Source);\r | |
354 | \r | |
355 | //\r | |
356 | // Size of the resulting string should never be zero.\r | |
357 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r | |
358 | //\r | |
359 | ASSERT (StrSize (Destination) != 0);\r | |
360 | return Destination;\r | |
361 | }\r | |
362 | \r | |
363 | /**\r | |
364 | Concatenates one Null-terminated Unicode string with a maximum length to the\r | |
365 | end of another Null-terminated Unicode string, and returns the concatenated\r | |
366 | Unicode string.\r | |
367 | \r | |
368 | This function concatenates two Null-terminated Unicode strings. The contents\r | |
369 | of Null-terminated Unicode string Source are concatenated to the end of\r | |
370 | Null-terminated Unicode string Destination, and Destination is returned. At\r | |
371 | most, Length Unicode characters are concatenated from Source to the end of\r | |
372 | Destination, and Destination is always Null-terminated. If Length is 0, then\r | |
373 | Destination is returned unmodified. If Source and Destination overlap, then\r | |
374 | the results are undefined.\r | |
375 | \r | |
376 | If Destination is NULL, then ASSERT().\r | |
377 | If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r | |
378 | If Length > 0 and Source is NULL, then ASSERT().\r | |
379 | If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r | |
380 | If Source and Destination overlap, then ASSERT().\r | |
381 | If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r | |
382 | than PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
383 | Null-terminator, then ASSERT().\r | |
384 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r | |
385 | PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
386 | Null-terminator, then ASSERT().\r | |
387 | If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r | |
388 | and Source results in a Unicode string with more than\r | |
389 | PcdMaximumUnicodeStringLength Unicode characters not including the\r | |
390 | Null-terminator, then ASSERT().\r | |
391 | \r | |
392 | @param Destination Pointer to a Null-terminated Unicode string.\r | |
393 | @param Source Pointer to a Null-terminated Unicode string.\r | |
394 | @param Length Maximum number of Unicode characters to concatenate from\r | |
395 | Source.\r | |
396 | \r | |
397 | @return Destination\r | |
398 | \r | |
399 | **/\r | |
400 | CHAR16 *\r | |
401 | EFIAPI\r | |
402 | StrnCat (\r | |
403 | IN OUT CHAR16 *Destination,\r | |
404 | IN CONST CHAR16 *Source,\r | |
405 | IN UINTN Length\r | |
406 | )\r | |
407 | {\r | |
408 | StrnCpy (Destination + StrLen (Destination), Source, Length);\r | |
409 | \r | |
410 | //\r | |
411 | // Size of the resulting string should never be zero.\r | |
412 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r | |
413 | //\r | |
414 | ASSERT (StrSize (Destination) != 0);\r | |
415 | return Destination;\r | |
416 | }\r | |
417 | \r | |
418 | /**\r | |
419 | Returns the first occurance of a Null-terminated Unicode sub-string \r | |
420 | in a Null-terminated Unicode string.\r | |
421 | \r | |
422 | This function scans the contents of the Null-terminated Unicode string \r | |
423 | specified by String and returns the first occurrence of SearchString. \r | |
424 | If SearchString is not found in String, then NULL is returned. If \r | |
425 | the length of SearchString is zero, then String is \r | |
426 | returned.\r | |
427 | \r | |
428 | If String is NULL, then ASSERT().\r | |
429 | If String is not aligned on a 16-bit boundary, then ASSERT().\r | |
430 | If SearchString is NULL, then ASSERT().\r | |
431 | If SearchString is not aligned on a 16-bit boundary, then ASSERT().\r | |
432 | \r | |
433 | If PcdMaximumUnicodeStringLength is not zero, and SearchString \r | |
434 | or String contains more than PcdMaximumUnicodeStringLength Unicode \r | |
435 | characters not including the Null-terminator, then ASSERT().\r | |
436 | \r | |
437 | @param String Pointer to a Null-terminated Unicode string.\r | |
438 | @param SearchString Pointer to a Null-terminated Unicode string to search for.\r | |
439 | \r | |
440 | @retval NULL If the SearchString does not appear in String.\r | |
441 | @retval !NULL If there is a match.\r | |
442 | \r | |
443 | **/\r | |
444 | CHAR16 *\r | |
445 | EFIAPI\r | |
446 | StrStr (\r | |
447 | IN CONST CHAR16 *String,\r | |
448 | IN CONST CHAR16 *SearchString\r | |
449 | )\r | |
450 | {\r | |
451 | CONST CHAR16 *FirstMatch;\r | |
452 | CONST CHAR16 *SearchStringTmp;\r | |
453 | \r | |
454 | ASSERT (String != NULL);\r | |
455 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
456 | ASSERT (SearchString != NULL);\r | |
457 | ASSERT (((UINTN) SearchString & 0x01) == 0);\r | |
458 | \r | |
459 | //\r | |
460 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
461 | // length of String should not more than PcdMaximumUnicodeStringLength\r | |
462 | //\r | |
463 | if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r | |
464 | ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
465 | }\r | |
466 | \r | |
467 | //\r | |
468 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
469 | // length of SearchString should not more than PcdMaximumUnicodeStringLength\r | |
470 | //\r | |
471 | if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r | |
472 | ASSERT (StrLen (SearchString) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
473 | }\r | |
474 | \r | |
475 | while (*String != '\0') {\r | |
476 | SearchStringTmp = SearchString;\r | |
477 | FirstMatch = String;\r | |
478 | \r | |
479 | while ((*String == *SearchStringTmp) \r | |
480 | && (*SearchStringTmp != '\0') \r | |
481 | && (*String != '\0')) {\r | |
482 | String++;\r | |
483 | SearchStringTmp++;\r | |
484 | } \r | |
485 | \r | |
486 | if (*SearchStringTmp == '\0') {\r | |
487 | return (CHAR16 *) FirstMatch;\r | |
488 | }\r | |
489 | \r | |
490 | if (SearchStringTmp == SearchString) {\r | |
491 | //\r | |
492 | // If no character from SearchString match,\r | |
493 | // move the pointer to the String under search\r | |
494 | // by one character.\r | |
495 | //\r | |
496 | String++;\r | |
497 | }\r | |
498 | }\r | |
499 | \r | |
500 | return NULL;\r | |
501 | }\r | |
502 | \r | |
503 | /**\r | |
504 | Check if a Unicode character is a decimal character.\r | |
505 | \r | |
506 | This internal function checks if a Unicode character is a \r | |
507 | decimal character. The valid decimal character is from\r | |
508 | L'0' to L'9'.\r | |
509 | \r | |
510 | \r | |
511 | @param Char The character to check against.\r | |
512 | \r | |
513 | @retval TRUE If the Char is a decmial character.\r | |
514 | @retval FALSE Otherwise.\r | |
515 | \r | |
516 | **/\r | |
517 | STATIC\r | |
518 | BOOLEAN\r | |
519 | InternalIsDecimalDigitCharacter (\r | |
520 | IN CHAR16 Char\r | |
521 | )\r | |
522 | {\r | |
523 | return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r | |
524 | }\r | |
525 | \r | |
526 | /**\r | |
527 | Convert a Unicode character to upper case only if \r | |
528 | it maps to a valid small-case ASCII character.\r | |
529 | \r | |
530 | This internal function only deal with Unicode character\r | |
531 | which maps to a valid small-case ASII character, i.e.\r | |
532 | L'a' to L'z'. For other Unicode character, the input character\r | |
533 | is returned directly.\r | |
534 | \r | |
535 | \r | |
536 | @param Char The character to convert.\r | |
537 | \r | |
538 | @retval LowerCharacter If the Char is with range L'a' to L'z'.\r | |
539 | @retval Unchanged Otherwise.\r | |
540 | \r | |
541 | **/\r | |
542 | STATIC\r | |
543 | CHAR16\r | |
544 | InternalCharToUpper (\r | |
545 | IN CHAR16 Char\r | |
546 | )\r | |
547 | {\r | |
548 | if (Char >= L'a' && Char <= L'z') {\r | |
549 | return (CHAR16) (Char - (L'a' - L'A'));\r | |
550 | }\r | |
551 | \r | |
552 | return Char;\r | |
553 | }\r | |
554 | \r | |
555 | /**\r | |
556 | Convert a Unicode character to numerical value.\r | |
557 | \r | |
558 | This internal function only deal with Unicode character\r | |
559 | which maps to a valid hexadecimal ASII character, i.e.\r | |
560 | L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r | |
561 | Unicode character, the value returned does not make sense.\r | |
562 | \r | |
563 | @param Char The character to convert.\r | |
564 | \r | |
565 | @retval UINTN The numerical value converted.\r | |
566 | \r | |
567 | **/\r | |
568 | STATIC\r | |
569 | UINTN\r | |
570 | InternalHexCharToUintn (\r | |
571 | IN CHAR16 Char\r | |
572 | )\r | |
573 | {\r | |
574 | if (InternalIsDecimalDigitCharacter (Char)) {\r | |
575 | return Char - L'0';\r | |
576 | }\r | |
577 | \r | |
578 | return (UINTN) (10 + InternalCharToUpper (Char) - L'A');\r | |
579 | }\r | |
580 | \r | |
581 | /**\r | |
582 | Check if a Unicode character is a hexadecimal character.\r | |
583 | \r | |
584 | This internal function checks if a Unicode character is a \r | |
585 | decimal character. The valid hexadecimal character is \r | |
586 | L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r | |
587 | \r | |
588 | \r | |
589 | @param Char The character to check against.\r | |
590 | \r | |
591 | @retval TRUE If the Char is a hexadecmial character.\r | |
592 | @retval FALSE Otherwise.\r | |
593 | \r | |
594 | **/\r | |
595 | STATIC\r | |
596 | BOOLEAN\r | |
597 | InternalIsHexaDecimalDigitCharacter (\r | |
598 | IN CHAR16 Char\r | |
599 | )\r | |
600 | {\r | |
601 | \r | |
602 | return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||\r | |
603 | (Char >= L'A' && Char <= L'F') ||\r | |
604 | (Char >= L'a' && Char <= L'f'));\r | |
605 | }\r | |
606 | \r | |
607 | /**\r | |
608 | Convert a Null-terminated Unicode decimal string to a value of \r | |
609 | type UINTN.\r | |
610 | \r | |
611 | This function returns a value of type UINTN by interpreting the contents \r | |
612 | of the Unicode string specified by String as a decimal number. The format \r | |
613 | of the input Unicode string String is:\r | |
614 | \r | |
615 | [spaces] [decimal digits].\r | |
616 | \r | |
617 | The valid decimal digit character is in the range [0-9]. The \r | |
618 | function will ignore the pad space, which includes spaces or \r | |
619 | tab characters, before [decimal digits]. The running zero in the \r | |
620 | beginning of [decimal digits] will be ignored. Then, the function \r | |
621 | stops at the first character that is a not a valid decimal character \r | |
622 | or a Null-terminator, whichever one comes first. \r | |
623 | \r | |
624 | If String is NULL, then ASSERT().\r | |
625 | If String is not aligned in a 16-bit boundary, then ASSERT(). \r | |
626 | If String has only pad spaces, then 0 is returned.\r | |
627 | If String has no pad spaces or valid decimal digits, \r | |
628 | then 0 is returned.\r | |
629 | If the number represented by String overflows according \r | |
630 | to the range defined by UINTN, then ASSERT().\r | |
631 | \r | |
632 | If PcdMaximumUnicodeStringLength is not zero, and String contains \r | |
633 | more than PcdMaximumUnicodeStringLength Unicode characters not including \r | |
634 | the Null-terminator, then ASSERT().\r | |
635 | \r | |
636 | @param String Pointer to a Null-terminated Unicode string.\r | |
637 | \r | |
638 | @retval UINTN \r | |
639 | \r | |
640 | **/\r | |
641 | UINTN\r | |
642 | EFIAPI\r | |
643 | StrDecimalToUintn (\r | |
644 | IN CONST CHAR16 *String\r | |
645 | )\r | |
646 | {\r | |
647 | UINTN Result;\r | |
648 | \r | |
649 | ASSERT (String != NULL);\r | |
650 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
651 | ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
652 | \r | |
653 | //\r | |
654 | // Ignore the pad spaces (space or tab)\r | |
655 | //\r | |
656 | while ((*String == L' ') || (*String == L'\t')) {\r | |
657 | String++;\r | |
658 | }\r | |
659 | \r | |
660 | //\r | |
661 | // Ignore leading Zeros after the spaces\r | |
662 | //\r | |
663 | while (*String == L'0') {\r | |
664 | String++;\r | |
665 | }\r | |
666 | \r | |
667 | Result = 0;\r | |
668 | \r | |
669 | while (InternalIsDecimalDigitCharacter (*String)) {\r | |
670 | //\r | |
671 | // If the number represented by String overflows according \r | |
672 | // to the range defined by UINTN, then ASSERT().\r | |
673 | //\r | |
674 | ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r | |
675 | ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) &&\r | |
676 | (*String - L'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r | |
677 | );\r | |
678 | \r | |
679 | Result = Result * 10 + (*String - L'0');\r | |
680 | String++;\r | |
681 | }\r | |
682 | \r | |
683 | return Result;\r | |
684 | }\r | |
685 | \r | |
686 | \r | |
687 | /**\r | |
688 | Convert a Null-terminated Unicode decimal string to a value of \r | |
689 | type UINT64.\r | |
690 | \r | |
691 | This function returns a value of type UINT64 by interpreting the contents \r | |
692 | of the Unicode string specified by String as a decimal number. The format \r | |
693 | of the input Unicode string String is:\r | |
694 | \r | |
695 | [spaces] [decimal digits].\r | |
696 | \r | |
697 | The valid decimal digit character is in the range [0-9]. The \r | |
698 | function will ignore the pad space, which includes spaces or \r | |
699 | tab characters, before [decimal digits]. The running zero in the \r | |
700 | beginning of [decimal digits] will be ignored. Then, the function \r | |
701 | stops at the first character that is a not a valid decimal character \r | |
702 | or a Null-terminator, whichever one comes first. \r | |
703 | \r | |
704 | If String is NULL, then ASSERT().\r | |
705 | If String is not aligned in a 16-bit boundary, then ASSERT(). \r | |
706 | If String has only pad spaces, then 0 is returned.\r | |
707 | If String has no pad spaces or valid decimal digits, \r | |
708 | then 0 is returned.\r | |
709 | If the number represented by String overflows according \r | |
710 | to the range defined by UINT64, then ASSERT().\r | |
711 | \r | |
712 | If PcdMaximumUnicodeStringLength is not zero, and String contains \r | |
713 | more than PcdMaximumUnicodeStringLength Unicode characters not including \r | |
714 | the Null-terminator, then ASSERT().\r | |
715 | \r | |
716 | @param String Pointer to a Null-terminated Unicode string.\r | |
717 | \r | |
718 | @retval UINT64 \r | |
719 | \r | |
720 | **/\r | |
721 | UINT64\r | |
722 | EFIAPI\r | |
723 | StrDecimalToUint64 (\r | |
724 | IN CONST CHAR16 *String\r | |
725 | )\r | |
726 | {\r | |
727 | UINT64 Result;\r | |
728 | \r | |
729 | ASSERT (String != NULL);\r | |
730 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
731 | ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
732 | \r | |
733 | //\r | |
734 | // Ignore the pad spaces (space or tab)\r | |
735 | //\r | |
736 | while ((*String == L' ') || (*String == L'\t')) {\r | |
737 | String++;\r | |
738 | }\r | |
739 | \r | |
740 | //\r | |
741 | // Ignore leading Zeros after the spaces\r | |
742 | //\r | |
743 | while (*String == L'0') {\r | |
744 | String++;\r | |
745 | }\r | |
746 | \r | |
747 | Result = 0;\r | |
748 | \r | |
749 | while (InternalIsDecimalDigitCharacter (*String)) {\r | |
750 | //\r | |
751 | // If the number represented by String overflows according \r | |
752 | // to the range defined by UINTN, then ASSERT().\r | |
753 | //\r | |
754 | ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r | |
755 | ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r | |
756 | (*String - L'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r | |
757 | );\r | |
758 | \r | |
759 | Result = MultU64x32 (Result, 10) + (*String - L'0');\r | |
760 | String++;\r | |
761 | }\r | |
762 | \r | |
763 | return Result;\r | |
764 | }\r | |
765 | \r | |
766 | /**\r | |
767 | Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.\r | |
768 | \r | |
769 | This function returns a value of type UINTN by interpreting the contents \r | |
770 | of the Unicode string specified by String as a hexadecimal number. \r | |
771 | The format of the input Unicode string String is:\r | |
772 | \r | |
773 | [spaces][zeros][x][hexadecimal digits]. \r | |
774 | \r | |
775 | The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r | |
776 | The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. \r | |
777 | If "x" appears in the input string, it must be prefixed with at least one 0. \r | |
778 | The function will ignore the pad space, which includes spaces or tab characters, \r | |
779 | before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or \r | |
780 | [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the \r | |
781 | first valid hexadecimal digit. Then, the function stops at the first character that is \r | |
782 | a not a valid hexadecimal character or NULL, whichever one comes first.\r | |
783 | \r | |
784 | If String is NULL, then ASSERT().\r | |
785 | If String is not aligned in a 16-bit boundary, then ASSERT().\r | |
786 | If String has only pad spaces, then zero is returned.\r | |
787 | If String has no leading pad spaces, leading zeros or valid hexadecimal digits, \r | |
788 | then zero is returned.\r | |
789 | If the number represented by String overflows according to the range defined by \r | |
790 | UINTN, then ASSERT().\r | |
791 | \r | |
792 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than \r | |
793 | PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, \r | |
794 | then ASSERT().\r | |
795 | \r | |
796 | @param String Pointer to a Null-terminated Unicode string.\r | |
797 | \r | |
798 | @retval UINTN\r | |
799 | \r | |
800 | **/\r | |
801 | UINTN\r | |
802 | EFIAPI\r | |
803 | StrHexToUintn (\r | |
804 | IN CONST CHAR16 *String\r | |
805 | )\r | |
806 | {\r | |
807 | UINTN Result;\r | |
808 | \r | |
809 | ASSERT (String != NULL);\r | |
810 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
811 | ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
812 | \r | |
813 | //\r | |
814 | // Ignore the pad spaces (space or tab) \r | |
815 | //\r | |
816 | while ((*String == L' ') || (*String == L'\t')) {\r | |
817 | String++;\r | |
818 | }\r | |
819 | \r | |
820 | //\r | |
821 | // Ignore leading Zeros after the spaces\r | |
822 | //\r | |
823 | while (*String == L'0') {\r | |
824 | String++;\r | |
825 | }\r | |
826 | \r | |
827 | if (InternalCharToUpper (*String) == L'X') {\r | |
828 | ASSERT (*(String - 1) == L'0');\r | |
829 | if (*(String - 1) != L'0') {\r | |
830 | return 0;\r | |
831 | }\r | |
832 | //\r | |
833 | // Skip the 'X'\r | |
834 | //\r | |
835 | String++;\r | |
836 | }\r | |
837 | \r | |
838 | Result = 0;\r | |
839 | \r | |
840 | while (InternalIsHexaDecimalDigitCharacter (*String)) {\r | |
841 | //\r | |
842 | // If the Hex Number represented by String overflows according \r | |
843 | // to the range defined by UINTN, then ASSERT().\r | |
844 | //\r | |
845 | ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r | |
846 | ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r | |
847 | (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r | |
848 | );\r | |
849 | \r | |
850 | Result = (Result << 4) + InternalHexCharToUintn (*String);\r | |
851 | String++;\r | |
852 | }\r | |
853 | \r | |
854 | return Result;\r | |
855 | }\r | |
856 | \r | |
857 | \r | |
858 | /**\r | |
859 | Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r | |
860 | \r | |
861 | This function returns a value of type UINT64 by interpreting the contents \r | |
862 | of the Unicode string specified by String as a hexadecimal number. \r | |
863 | The format of the input Unicode string String is \r | |
864 | \r | |
865 | [spaces][zeros][x][hexadecimal digits]. \r | |
866 | \r | |
867 | The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r | |
868 | The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. \r | |
869 | If "x" appears in the input string, it must be prefixed with at least one 0. \r | |
870 | The function will ignore the pad space, which includes spaces or tab characters, \r | |
871 | before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or \r | |
872 | [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the \r | |
873 | first valid hexadecimal digit. Then, the function stops at the first character that is \r | |
874 | a not a valid hexadecimal character or NULL, whichever one comes first.\r | |
875 | \r | |
876 | If String is NULL, then ASSERT().\r | |
877 | If String is not aligned in a 16-bit boundary, then ASSERT().\r | |
878 | If String has only pad spaces, then zero is returned.\r | |
879 | If String has no leading pad spaces, leading zeros or valid hexadecimal digits, \r | |
880 | then zero is returned.\r | |
881 | If the number represented by String overflows according to the range defined by \r | |
882 | UINT64, then ASSERT().\r | |
883 | \r | |
884 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than \r | |
885 | PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, \r | |
886 | then ASSERT().\r | |
887 | \r | |
888 | @param String Pointer to a Null-terminated Unicode string.\r | |
889 | \r | |
890 | @retval UINT64\r | |
891 | \r | |
892 | **/\r | |
893 | UINT64\r | |
894 | EFIAPI\r | |
895 | StrHexToUint64 (\r | |
896 | IN CONST CHAR16 *String\r | |
897 | )\r | |
898 | {\r | |
899 | UINT64 Result;\r | |
900 | \r | |
901 | ASSERT (String != NULL);\r | |
902 | ASSERT (((UINTN) String & 0x01) == 0);\r | |
903 | ASSERT (StrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
904 | \r | |
905 | //\r | |
906 | // Ignore the pad spaces (space or tab) \r | |
907 | //\r | |
908 | while ((*String == L' ') || (*String == L'\t')) {\r | |
909 | String++;\r | |
910 | }\r | |
911 | \r | |
912 | //\r | |
913 | // Ignore leading Zeros after the spaces\r | |
914 | //\r | |
915 | while (*String == L'0') {\r | |
916 | String++;\r | |
917 | }\r | |
918 | \r | |
919 | if (InternalCharToUpper (*String) == L'X') {\r | |
920 | ASSERT (*(String - 1) == L'0');\r | |
921 | if (*(String - 1) != L'0') {\r | |
922 | return 0;\r | |
923 | }\r | |
924 | //\r | |
925 | // Skip the 'X'\r | |
926 | //\r | |
927 | String++;\r | |
928 | }\r | |
929 | \r | |
930 | Result = 0;\r | |
931 | \r | |
932 | while (InternalIsHexaDecimalDigitCharacter (*String)) {\r | |
933 | //\r | |
934 | // If the Hex Number represented by String overflows according \r | |
935 | // to the range defined by UINTN, then ASSERT().\r | |
936 | //\r | |
937 | ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16)|| \r | |
938 | ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r | |
939 | (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r | |
940 | );\r | |
941 | \r | |
942 | Result = LShiftU64 (Result, 4);\r | |
943 | Result = Result + InternalHexCharToUintn (*String);\r | |
944 | String++;\r | |
945 | }\r | |
946 | \r | |
947 | return Result;\r | |
948 | }\r | |
949 | \r | |
950 | /**\r | |
951 | Check if a ASCII character is a decimal character.\r | |
952 | \r | |
953 | This internal function checks if a Unicode character is a \r | |
954 | decimal character. The valid decimal character is from\r | |
955 | '0' to '9'.\r | |
956 | \r | |
957 | @param Char The character to check against.\r | |
958 | \r | |
959 | @retval TRUE If the Char is a decmial character.\r | |
960 | @retval FALSE Otherwise.\r | |
961 | \r | |
962 | **/\r | |
963 | STATIC\r | |
964 | BOOLEAN\r | |
965 | InternalAsciiIsDecimalDigitCharacter (\r | |
966 | IN CHAR8 Char\r | |
967 | )\r | |
968 | {\r | |
969 | return (BOOLEAN) (Char >= '0' && Char <= '9');\r | |
970 | }\r | |
971 | \r | |
972 | /**\r | |
973 | Check if a ASCII character is a hexadecimal character.\r | |
974 | \r | |
975 | This internal function checks if a ASCII character is a \r | |
976 | decimal character. The valid hexadecimal character is \r | |
977 | L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r | |
978 | \r | |
979 | \r | |
980 | @param Char The character to check against.\r | |
981 | \r | |
982 | @retval TRUE If the Char is a hexadecmial character.\r | |
983 | @retval FALSE Otherwise.\r | |
984 | \r | |
985 | **/\r | |
986 | STATIC\r | |
987 | BOOLEAN\r | |
988 | InternalAsciiIsHexaDecimalDigitCharacter (\r | |
989 | IN CHAR8 Char\r | |
990 | )\r | |
991 | {\r | |
992 | \r | |
993 | return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||\r | |
994 | (Char >= 'A' && Char <= 'F') ||\r | |
995 | (Char >= 'a' && Char <= 'f'));\r | |
996 | }\r | |
997 | \r | |
998 | /**\r | |
999 | Convert a Null-terminated Unicode string to a Null-terminated \r | |
1000 | ASCII string and returns the ASCII string.\r | |
1001 | \r | |
1002 | This function converts the content of the Unicode string Source \r | |
1003 | to the ASCII string Destination by copying the lower 8 bits of \r | |
1004 | each Unicode character. It returns Destination. The function terminates \r | |
1005 | the ASCII string Destination by appending a Null-terminator character \r | |
1006 | at the end. The caller is responsible to make sure Destination points \r | |
1007 | to a buffer with size equal or greater than (StrLen (Source) + 1) in bytes.\r | |
1008 | \r | |
1009 | If Destination is NULL, then ASSERT().\r | |
1010 | If Source is NULL, then ASSERT().\r | |
1011 | If Source is not aligned on a 16-bit boundary, then ASSERT().\r | |
1012 | If Source and Destination overlap, then ASSERT().\r | |
1013 | \r | |
1014 | If any Unicode characters in Source contain non-zero value in \r | |
1015 | the upper 8 bits, then ASSERT().\r | |
1016 | \r | |
1017 | If PcdMaximumUnicodeStringLength is not zero, and Source contains \r | |
1018 | more than PcdMaximumUnicodeStringLength Unicode characters not including \r | |
1019 | the Null-terminator, then ASSERT().\r | |
1020 | \r | |
1021 | If PcdMaximumAsciiStringLength is not zero, and Source contains more \r | |
1022 | than PcdMaximumAsciiStringLength Unicode characters not including the \r | |
1023 | Null-terminator, then ASSERT().\r | |
1024 | \r | |
1025 | @param Source Pointer to a Null-terminated Unicode string.\r | |
1026 | @param Destination Pointer to a Null-terminated ASCII string.\r | |
1027 | \r | |
1028 | @reture Destination\r | |
1029 | \r | |
1030 | **/\r | |
1031 | CHAR8 *\r | |
1032 | EFIAPI\r | |
1033 | UnicodeStrToAsciiStr (\r | |
1034 | IN CONST CHAR16 *Source,\r | |
1035 | OUT CHAR8 *Destination\r | |
1036 | )\r | |
1037 | {\r | |
1038 | ASSERT (Destination != NULL);\r | |
1039 | ASSERT (Source != NULL);\r | |
1040 | ASSERT (((UINTN) Source & 0x01) == 0);\r | |
1041 | \r | |
1042 | //\r | |
1043 | // Source and Destination should not overlap\r | |
1044 | //\r | |
1045 | ASSERT ((UINTN) ((CHAR16 *) Destination - Source) > StrLen (Source));\r | |
1046 | ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r | |
1047 | \r | |
1048 | //\r | |
1049 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
1050 | // length of Source should not more than PcdMaximumUnicodeStringLength\r | |
1051 | //\r | |
1052 | if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r | |
1053 | ASSERT (StrLen (Source) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
1054 | }\r | |
1055 | \r | |
1056 | while (*Source != '\0') {\r | |
1057 | //\r | |
1058 | // If any Unicode characters in Source contain \r | |
1059 | // non-zero value in the upper 8 bits, then ASSERT().\r | |
1060 | //\r | |
1061 | ASSERT (*Source < 0x100);\r | |
1062 | *(Destination++) = (CHAR8) *(Source++);\r | |
1063 | }\r | |
1064 | \r | |
1065 | *Destination = '\0';\r | |
1066 | \r | |
1067 | return Destination;\r | |
1068 | }\r | |
1069 | \r | |
1070 | \r | |
1071 | /**\r | |
1072 | Copies one Null-terminated ASCII string to another Null-terminated ASCII\r | |
1073 | string and returns the new ASCII string.\r | |
1074 | \r | |
1075 | This function copies the contents of the ASCII string Source to the ASCII\r | |
1076 | string Destination, and returns Destination. If Source and Destination\r | |
1077 | overlap, then the results are undefined.\r | |
1078 | \r | |
1079 | If Destination is NULL, then ASSERT().\r | |
1080 | If Source is NULL, then ASSERT().\r | |
1081 | If Source and Destination overlap, then ASSERT().\r | |
1082 | If PcdMaximumAsciiStringLength is not zero and Source contains more than\r | |
1083 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1084 | then ASSERT().\r | |
1085 | \r | |
1086 | @param Destination Pointer to a Null-terminated ASCII string.\r | |
1087 | @param Source Pointer to a Null-terminated ASCII string.\r | |
1088 | \r | |
1089 | @return Destination\r | |
1090 | \r | |
1091 | **/\r | |
1092 | CHAR8 *\r | |
1093 | EFIAPI\r | |
1094 | AsciiStrCpy (\r | |
1095 | OUT CHAR8 *Destination,\r | |
1096 | IN CONST CHAR8 *Source\r | |
1097 | )\r | |
1098 | {\r | |
1099 | CHAR8 *ReturnValue;\r | |
1100 | \r | |
1101 | //\r | |
1102 | // Destination cannot be NULL\r | |
1103 | //\r | |
1104 | ASSERT (Destination != NULL);\r | |
1105 | \r | |
1106 | //\r | |
1107 | // Destination and source cannot overlap\r | |
1108 | //\r | |
1109 | ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r | |
1110 | ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r | |
1111 | \r | |
1112 | ReturnValue = Destination;\r | |
1113 | while (*Source) {\r | |
1114 | *(Destination++) = *(Source++);\r | |
1115 | }\r | |
1116 | *Destination = 0;\r | |
1117 | return ReturnValue;\r | |
1118 | }\r | |
1119 | \r | |
1120 | /**\r | |
1121 | Copies one Null-terminated ASCII string with a maximum length to another\r | |
1122 | Null-terminated ASCII string with a maximum length and returns the new ASCII\r | |
1123 | string.\r | |
1124 | \r | |
1125 | This function copies the contents of the ASCII string Source to the ASCII\r | |
1126 | string Destination, and returns Destination. At most, Length ASCII characters\r | |
1127 | are copied from Source to Destination. If Length is 0, then Destination is\r | |
1128 | returned unmodified. If Length is greater that the number of ASCII characters\r | |
1129 | in Source, then Destination is padded with Null ASCII characters. If Source\r | |
1130 | and Destination overlap, then the results are undefined.\r | |
1131 | \r | |
1132 | If Destination is NULL, then ASSERT().\r | |
1133 | If Source is NULL, then ASSERT().\r | |
1134 | If Source and Destination overlap, then ASSERT().\r | |
1135 | If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r | |
1136 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1137 | then ASSERT().\r | |
1138 | \r | |
1139 | @param Destination Pointer to a Null-terminated ASCII string.\r | |
1140 | @param Source Pointer to a Null-terminated ASCII string.\r | |
1141 | @param Length Maximum number of ASCII characters to copy.\r | |
1142 | \r | |
1143 | @return Destination\r | |
1144 | \r | |
1145 | **/\r | |
1146 | CHAR8 *\r | |
1147 | EFIAPI\r | |
1148 | AsciiStrnCpy (\r | |
1149 | OUT CHAR8 *Destination,\r | |
1150 | IN CONST CHAR8 *Source,\r | |
1151 | IN UINTN Length\r | |
1152 | )\r | |
1153 | {\r | |
1154 | CHAR8 *ReturnValue;\r | |
1155 | \r | |
1156 | if (Length == 0) {\r | |
1157 | return Destination;\r | |
1158 | }\r | |
1159 | \r | |
1160 | //\r | |
1161 | // Destination cannot be NULL\r | |
1162 | //\r | |
1163 | ASSERT (Destination != NULL);\r | |
1164 | \r | |
1165 | //\r | |
1166 | // Destination and source cannot overlap\r | |
1167 | //\r | |
1168 | ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r | |
1169 | ASSERT ((UINTN)(Source - Destination) >= Length);\r | |
1170 | \r | |
1171 | ReturnValue = Destination;\r | |
1172 | \r | |
1173 | while (*Source && Length > 0) {\r | |
1174 | *(Destination++) = *(Source++);\r | |
1175 | Length--;\r | |
1176 | }\r | |
1177 | \r | |
1178 | ZeroMem (Destination, Length * sizeof (*Destination));\r | |
1179 | return ReturnValue;\r | |
1180 | }\r | |
1181 | \r | |
1182 | /**\r | |
1183 | Returns the length of a Null-terminated ASCII string.\r | |
1184 | \r | |
1185 | This function returns the number of ASCII characters in the Null-terminated\r | |
1186 | ASCII string specified by String.\r | |
1187 | \r | |
1188 | If String is NULL, then ASSERT().\r | |
1189 | If PcdMaximumAsciiStringLength is not zero and String contains more than\r | |
1190 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1191 | then ASSERT().\r | |
1192 | \r | |
1193 | @param String Pointer to a Null-terminated ASCII string.\r | |
1194 | \r | |
1195 | @return The length of String.\r | |
1196 | \r | |
1197 | **/\r | |
1198 | UINTN\r | |
1199 | EFIAPI\r | |
1200 | AsciiStrLen (\r | |
1201 | IN CONST CHAR8 *String\r | |
1202 | )\r | |
1203 | {\r | |
1204 | UINTN Length;\r | |
1205 | \r | |
1206 | ASSERT (String != NULL);\r | |
1207 | \r | |
1208 | for (Length = 0; *String != '\0'; String++, Length++) {\r | |
1209 | //\r | |
1210 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
1211 | // length should not more than PcdMaximumUnicodeStringLength\r | |
1212 | //\r | |
1213 | if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r | |
1214 | ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1215 | }\r | |
1216 | }\r | |
1217 | return Length;\r | |
1218 | }\r | |
1219 | \r | |
1220 | /**\r | |
1221 | Returns the size of a Null-terminated ASCII string in bytes, including the\r | |
1222 | Null terminator.\r | |
1223 | \r | |
1224 | This function returns the size, in bytes, of the Null-terminated ASCII string\r | |
1225 | specified by String.\r | |
1226 | \r | |
1227 | If String is NULL, then ASSERT().\r | |
1228 | If PcdMaximumAsciiStringLength is not zero and String contains more than\r | |
1229 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1230 | then ASSERT().\r | |
1231 | \r | |
1232 | @param String Pointer to a Null-terminated ASCII string.\r | |
1233 | \r | |
1234 | @return The size of String.\r | |
1235 | \r | |
1236 | **/\r | |
1237 | UINTN\r | |
1238 | EFIAPI\r | |
1239 | AsciiStrSize (\r | |
1240 | IN CONST CHAR8 *String\r | |
1241 | )\r | |
1242 | {\r | |
1243 | return (AsciiStrLen (String) + 1) * sizeof (*String);\r | |
1244 | }\r | |
1245 | \r | |
1246 | /**\r | |
1247 | Compares two Null-terminated ASCII strings, and returns the difference\r | |
1248 | between the first mismatched ASCII characters.\r | |
1249 | \r | |
1250 | This function compares the Null-terminated ASCII string FirstString to the\r | |
1251 | Null-terminated ASCII string SecondString. If FirstString is identical to\r | |
1252 | SecondString, then 0 is returned. Otherwise, the value returned is the first\r | |
1253 | mismatched ASCII character in SecondString subtracted from the first\r | |
1254 | mismatched ASCII character in FirstString.\r | |
1255 | \r | |
1256 | If FirstString is NULL, then ASSERT().\r | |
1257 | If SecondString is NULL, then ASSERT().\r | |
1258 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r | |
1259 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1260 | then ASSERT().\r | |
1261 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r | |
1262 | than PcdMaximumAsciiStringLength ASCII characters not including the\r | |
1263 | Null-terminator, then ASSERT().\r | |
1264 | \r | |
1265 | @param FirstString Pointer to a Null-terminated ASCII string.\r | |
1266 | @param SecondString Pointer to a Null-terminated ASCII string.\r | |
1267 | \r | |
1268 | @retval 0 FirstString is identical to SecondString.\r | |
1269 | @retval !=0 FirstString is not identical to SecondString.\r | |
1270 | \r | |
1271 | **/\r | |
1272 | INTN\r | |
1273 | EFIAPI\r | |
1274 | AsciiStrCmp (\r | |
1275 | IN CONST CHAR8 *FirstString,\r | |
1276 | IN CONST CHAR8 *SecondString\r | |
1277 | )\r | |
1278 | {\r | |
1279 | //\r | |
1280 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r | |
1281 | //\r | |
1282 | ASSERT (AsciiStrSize (FirstString));\r | |
1283 | ASSERT (AsciiStrSize (SecondString));\r | |
1284 | \r | |
1285 | while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r | |
1286 | FirstString++;\r | |
1287 | SecondString++;\r | |
1288 | }\r | |
1289 | \r | |
1290 | return *FirstString - *SecondString;\r | |
1291 | }\r | |
1292 | \r | |
1293 | /**\r | |
1294 | Converts a lowercase Ascii character to upper one\r | |
1295 | \r | |
1296 | If Chr is lowercase Ascii character, then converts it to upper one.\r | |
1297 | \r | |
1298 | If Value >= 0xA0, then ASSERT().\r | |
1299 | If (Value & 0x0F) >= 0x0A, then ASSERT().\r | |
1300 | \r | |
1301 | @param chr one Ascii character\r | |
1302 | \r | |
1303 | @return The uppercase value of Ascii character \r | |
1304 | \r | |
1305 | **/\r | |
1306 | STATIC\r | |
1307 | CHAR8\r | |
1308 | AsciiToUpper (\r | |
1309 | IN CHAR8 Chr\r | |
1310 | )\r | |
1311 | {\r | |
1312 | return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);\r | |
1313 | }\r | |
1314 | \r | |
1315 | /**\r | |
1316 | Convert a ASCII character to numerical value.\r | |
1317 | \r | |
1318 | This internal function only deal with Unicode character\r | |
1319 | which maps to a valid hexadecimal ASII character, i.e.\r | |
1320 | '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r | |
1321 | ASCII character, the value returned does not make sense.\r | |
1322 | \r | |
1323 | @param Char The character to convert.\r | |
1324 | \r | |
1325 | @retval UINTN The numerical value converted.\r | |
1326 | \r | |
1327 | **/\r | |
1328 | STATIC\r | |
1329 | UINTN\r | |
1330 | InternalAsciiHexCharToUintn (\r | |
1331 | IN CHAR8 Char\r | |
1332 | )\r | |
1333 | {\r | |
1334 | if (InternalIsDecimalDigitCharacter (Char)) {\r | |
1335 | return Char - '0';\r | |
1336 | }\r | |
1337 | \r | |
1338 | return (UINTN) (10 + AsciiToUpper (Char) - 'A');\r | |
1339 | }\r | |
1340 | \r | |
1341 | \r | |
1342 | /**\r | |
1343 | Performs a case insensitive comparison of two Null-terminated ASCII strings,\r | |
1344 | and returns the difference between the first mismatched ASCII characters.\r | |
1345 | \r | |
1346 | This function performs a case insensitive comparison of the Null-terminated\r | |
1347 | ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r | |
1348 | FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r | |
1349 | value returned is the first mismatched lower case ASCII character in\r | |
1350 | SecondString subtracted from the first mismatched lower case ASCII character\r | |
1351 | in FirstString.\r | |
1352 | \r | |
1353 | If FirstString is NULL, then ASSERT().\r | |
1354 | If SecondString is NULL, then ASSERT().\r | |
1355 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r | |
1356 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1357 | then ASSERT().\r | |
1358 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r | |
1359 | than PcdMaximumAsciiStringLength ASCII characters not including the\r | |
1360 | Null-terminator, then ASSERT().\r | |
1361 | \r | |
1362 | @param FirstString Pointer to a Null-terminated ASCII string.\r | |
1363 | @param SecondString Pointer to a Null-terminated ASCII string.\r | |
1364 | \r | |
1365 | @retval 0 FirstString is identical to SecondString using case insensitive\r | |
1366 | comparisons.\r | |
1367 | @retval !=0 FirstString is not identical to SecondString using case\r | |
1368 | insensitive comparisons.\r | |
1369 | \r | |
1370 | **/\r | |
1371 | INTN\r | |
1372 | EFIAPI\r | |
1373 | AsciiStriCmp (\r | |
1374 | IN CONST CHAR8 *FirstString,\r | |
1375 | IN CONST CHAR8 *SecondString\r | |
1376 | )\r | |
1377 | {\r | |
1378 | CHAR8 UpperFirstString;\r | |
1379 | CHAR8 UpperSecondString;\r | |
1380 | \r | |
1381 | //\r | |
1382 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r | |
1383 | //\r | |
1384 | ASSERT (AsciiStrSize (FirstString));\r | |
1385 | ASSERT (AsciiStrSize (SecondString));\r | |
1386 | \r | |
1387 | UpperFirstString = AsciiToUpper (*FirstString);\r | |
1388 | UpperSecondString = AsciiToUpper (*SecondString);\r | |
1389 | while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r | |
1390 | FirstString++;\r | |
1391 | SecondString++;\r | |
1392 | UpperFirstString = AsciiToUpper (*FirstString);\r | |
1393 | UpperSecondString = AsciiToUpper (*SecondString);\r | |
1394 | }\r | |
1395 | \r | |
1396 | return UpperFirstString - UpperSecondString;\r | |
1397 | }\r | |
1398 | \r | |
1399 | /**\r | |
1400 | Compares two Null-terminated ASCII strings with maximum lengths, and returns\r | |
1401 | the difference between the first mismatched ASCII characters.\r | |
1402 | \r | |
1403 | This function compares the Null-terminated ASCII string FirstString to the\r | |
1404 | Null-terminated ASCII string SecondString. At most, Length ASCII characters\r | |
1405 | will be compared. If Length is 0, then 0 is returned. If FirstString is\r | |
1406 | identical to SecondString, then 0 is returned. Otherwise, the value returned\r | |
1407 | is the first mismatched ASCII character in SecondString subtracted from the\r | |
1408 | first mismatched ASCII character in FirstString.\r | |
1409 | \r | |
1410 | If FirstString is NULL, then ASSERT().\r | |
1411 | If SecondString is NULL, then ASSERT().\r | |
1412 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r | |
1413 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1414 | then ASSERT().\r | |
1415 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more than\r | |
1416 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1417 | then ASSERT().\r | |
1418 | \r | |
1419 | @param FirstString Pointer to a Null-terminated ASCII string.\r | |
1420 | @param SecondString Pointer to a Null-terminated ASCII string.\r | |
1421 | \r | |
1422 | @retval 0 FirstString is identical to SecondString.\r | |
1423 | @retval !=0 FirstString is not identical to SecondString.\r | |
1424 | \r | |
1425 | **/\r | |
1426 | INTN\r | |
1427 | EFIAPI\r | |
1428 | AsciiStrnCmp (\r | |
1429 | IN CONST CHAR8 *FirstString,\r | |
1430 | IN CONST CHAR8 *SecondString,\r | |
1431 | IN UINTN Length\r | |
1432 | )\r | |
1433 | {\r | |
1434 | if (Length == 0) {\r | |
1435 | return 0;\r | |
1436 | }\r | |
1437 | \r | |
1438 | //\r | |
1439 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r | |
1440 | //\r | |
1441 | ASSERT (AsciiStrSize (FirstString));\r | |
1442 | ASSERT (AsciiStrSize (SecondString));\r | |
1443 | \r | |
1444 | while ((*FirstString != '\0') &&\r | |
1445 | (*FirstString == *SecondString) &&\r | |
1446 | (Length > 1)) {\r | |
1447 | FirstString++;\r | |
1448 | SecondString++;\r | |
1449 | Length--;\r | |
1450 | }\r | |
1451 | return *FirstString - *SecondString;\r | |
1452 | }\r | |
1453 | \r | |
1454 | /**\r | |
1455 | Concatenates one Null-terminated ASCII string to another Null-terminated\r | |
1456 | ASCII string, and returns the concatenated ASCII string.\r | |
1457 | \r | |
1458 | This function concatenates two Null-terminated ASCII strings. The contents of\r | |
1459 | Null-terminated ASCII string Source are concatenated to the end of Null-\r | |
1460 | terminated ASCII string Destination. The Null-terminated concatenated ASCII\r | |
1461 | String is returned.\r | |
1462 | \r | |
1463 | If Destination is NULL, then ASSERT().\r | |
1464 | If Source is NULL, then ASSERT().\r | |
1465 | If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r | |
1466 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1467 | then ASSERT().\r | |
1468 | If PcdMaximumAsciiStringLength is not zero and Source contains more than\r | |
1469 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1470 | then ASSERT().\r | |
1471 | If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r | |
1472 | Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r | |
1473 | ASCII characters, then ASSERT().\r | |
1474 | \r | |
1475 | @param Destination Pointer to a Null-terminated ASCII string.\r | |
1476 | @param Source Pointer to a Null-terminated ASCII string.\r | |
1477 | \r | |
1478 | @return Destination\r | |
1479 | \r | |
1480 | **/\r | |
1481 | CHAR8 *\r | |
1482 | EFIAPI\r | |
1483 | AsciiStrCat (\r | |
1484 | IN OUT CHAR8 *Destination,\r | |
1485 | IN CONST CHAR8 *Source\r | |
1486 | )\r | |
1487 | {\r | |
1488 | AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r | |
1489 | \r | |
1490 | //\r | |
1491 | // Size of the resulting string should never be zero.\r | |
1492 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r | |
1493 | //\r | |
1494 | ASSERT (AsciiStrSize (Destination) != 0);\r | |
1495 | return Destination;\r | |
1496 | }\r | |
1497 | \r | |
1498 | /**\r | |
1499 | Concatenates one Null-terminated ASCII string with a maximum length to the\r | |
1500 | end of another Null-terminated ASCII string, and returns the concatenated\r | |
1501 | ASCII string.\r | |
1502 | \r | |
1503 | This function concatenates two Null-terminated ASCII strings. The contents\r | |
1504 | of Null-terminated ASCII string Source are concatenated to the end of Null-\r | |
1505 | terminated ASCII string Destination, and Destination is returned. At most,\r | |
1506 | Length ASCII characters are concatenated from Source to the end of\r | |
1507 | Destination, and Destination is always Null-terminated. If Length is 0, then\r | |
1508 | Destination is returned unmodified. If Source and Destination overlap, then\r | |
1509 | the results are undefined.\r | |
1510 | \r | |
1511 | If Destination is NULL, then ASSERT().\r | |
1512 | If Source is NULL, then ASSERT().\r | |
1513 | If Source and Destination overlap, then ASSERT().\r | |
1514 | If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r | |
1515 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1516 | then ASSERT().\r | |
1517 | If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r | |
1518 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r | |
1519 | then ASSERT().\r | |
1520 | If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r | |
1521 | Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r | |
1522 | ASCII characters not including the Null-terminator, then ASSERT().\r | |
1523 | \r | |
1524 | @param Destination Pointer to a Null-terminated ASCII string.\r | |
1525 | @param Source Pointer to a Null-terminated ASCII string.\r | |
1526 | @param Length Maximum number of ASCII characters to concatenate from\r | |
1527 | Source.\r | |
1528 | \r | |
1529 | @return Destination\r | |
1530 | \r | |
1531 | **/\r | |
1532 | CHAR8 *\r | |
1533 | EFIAPI\r | |
1534 | AsciiStrnCat (\r | |
1535 | IN OUT CHAR8 *Destination,\r | |
1536 | IN CONST CHAR8 *Source,\r | |
1537 | IN UINTN Length\r | |
1538 | )\r | |
1539 | {\r | |
1540 | AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);\r | |
1541 | \r | |
1542 | //\r | |
1543 | // Size of the resulting string should never be zero.\r | |
1544 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r | |
1545 | //\r | |
1546 | ASSERT (AsciiStrSize (Destination) != 0);\r | |
1547 | return Destination;\r | |
1548 | }\r | |
1549 | \r | |
1550 | /**\r | |
1551 | Returns the first occurance of a Null-terminated ASCII sub-string \r | |
1552 | in a Null-terminated ASCII string.\r | |
1553 | \r | |
1554 | This function scans the contents of the ASCII string specified by String \r | |
1555 | and returns the first occurrence of SearchString. If SearchString is not \r | |
1556 | found in String, then NULL is returned. If the length of SearchString is zero, \r | |
1557 | then String is returned.\r | |
1558 | \r | |
1559 | If String is NULL, then ASSERT().\r | |
1560 | If SearchString is NULL, then ASSERT().\r | |
1561 | \r | |
1562 | If PcdMaximumAsciiStringLength is not zero, and SearchString or \r | |
1563 | String contains more than PcdMaximumAsciiStringLength Unicode characters \r | |
1564 | not including the Null-terminator, then ASSERT().\r | |
1565 | \r | |
1566 | @param String Pointer to a Null-terminated ASCII string.\r | |
1567 | @param SearchString Pointer to a Null-terminated ASCII string to search for.\r | |
1568 | \r | |
1569 | @retval NULL If the SearchString does not appear in String.\r | |
1570 | @retval !NULL If there is a match.\r | |
1571 | \r | |
1572 | **/\r | |
1573 | CHAR8 *\r | |
1574 | EFIAPI\r | |
1575 | AsciiStrStr (\r | |
1576 | IN CONST CHAR8 *String,\r | |
1577 | IN CONST CHAR8 *SearchString\r | |
1578 | )\r | |
1579 | {\r | |
1580 | CONST CHAR8 *FirstMatch;\r | |
1581 | CONST CHAR8 *SearchStringTmp;\r | |
1582 | \r | |
1583 | ASSERT (String != NULL);\r | |
1584 | ASSERT (SearchString != NULL);\r | |
1585 | \r | |
1586 | //\r | |
1587 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
1588 | // length of String should not more than PcdMaximumUnicodeStringLength\r | |
1589 | //\r | |
1590 | if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r | |
1591 | ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1592 | }\r | |
1593 | \r | |
1594 | //\r | |
1595 | // If PcdMaximumUnicodeStringLength is not zero,\r | |
1596 | // length of SearchString should not more than PcdMaximumUnicodeStringLength\r | |
1597 | //\r | |
1598 | if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r | |
1599 | ASSERT (AsciiStrLen (SearchString) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1600 | }\r | |
1601 | \r | |
1602 | while (*String != '\0') {\r | |
1603 | SearchStringTmp = SearchString;\r | |
1604 | FirstMatch = String;\r | |
1605 | \r | |
1606 | while ((*String == *SearchStringTmp) \r | |
1607 | && (*SearchStringTmp != '\0') \r | |
1608 | && (*String != '\0')) {\r | |
1609 | String++;\r | |
1610 | SearchStringTmp++;\r | |
1611 | } \r | |
1612 | \r | |
1613 | if (*SearchStringTmp == '\0') {\r | |
1614 | return (CHAR8 *) FirstMatch;\r | |
1615 | }\r | |
1616 | \r | |
1617 | if (SearchStringTmp == SearchString) {\r | |
1618 | //\r | |
1619 | // If no character from SearchString match,\r | |
1620 | // move the pointer to the String under search\r | |
1621 | // by one character.\r | |
1622 | //\r | |
1623 | String++;\r | |
1624 | }\r | |
1625 | \r | |
1626 | }\r | |
1627 | \r | |
1628 | return NULL;\r | |
1629 | }\r | |
1630 | \r | |
1631 | /**\r | |
1632 | Convert a Null-terminated ASCII decimal string to a value of type \r | |
1633 | UINTN.\r | |
1634 | \r | |
1635 | This function returns a value of type UINTN by interpreting the contents \r | |
1636 | of the ASCII string String as a decimal number. The format of the input \r | |
1637 | ASCII string String is:\r | |
1638 | \r | |
1639 | [spaces] [decimal digits].\r | |
1640 | \r | |
1641 | The valid decimal digit character is in the range [0-9]. The function will \r | |
1642 | ignore the pad space, which includes spaces or tab characters, before the digits. \r | |
1643 | The running zero in the beginning of [decimal digits] will be ignored. Then, the \r | |
1644 | function stops at the first character that is a not a valid decimal character or \r | |
1645 | Null-terminator, whichever on comes first.\r | |
1646 | \r | |
1647 | If String has only pad spaces, then 0 is returned.\r | |
1648 | If String has no pad spaces or valid decimal digits, then 0 is returned.\r | |
1649 | If the number represented by String overflows according to the range defined by \r | |
1650 | UINTN, then ASSERT().\r | |
1651 | If String is NULL, then ASSERT().\r | |
1652 | If PcdMaximumAsciiStringLength is not zero, and String contains more than \r | |
1653 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r | |
1654 | then ASSERT().\r | |
1655 | \r | |
1656 | @param String Pointer to a Null-terminated ASCII string.\r | |
1657 | \r | |
1658 | @retval UINTN \r | |
1659 | \r | |
1660 | **/\r | |
1661 | UINTN\r | |
1662 | EFIAPI\r | |
1663 | AsciiStrDecimalToUintn (\r | |
1664 | IN CONST CHAR8 *String\r | |
1665 | )\r | |
1666 | {\r | |
1667 | UINTN Result;\r | |
1668 | \r | |
1669 | ASSERT (String != NULL);\r | |
1670 | ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1671 | \r | |
1672 | //\r | |
1673 | // Ignore the pad spaces (space or tab)\r | |
1674 | //\r | |
1675 | while ((*String == ' ') || (*String == '\t')) {\r | |
1676 | String++;\r | |
1677 | }\r | |
1678 | \r | |
1679 | //\r | |
1680 | // Ignore leading Zeros after the spaces\r | |
1681 | //\r | |
1682 | while (*String == '0') {\r | |
1683 | String++;\r | |
1684 | }\r | |
1685 | \r | |
1686 | Result = 0;\r | |
1687 | \r | |
1688 | while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r | |
1689 | //\r | |
1690 | // If the number represented by String overflows according \r | |
1691 | // to the range defined by UINTN, then ASSERT().\r | |
1692 | //\r | |
1693 | ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r | |
1694 | ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) && \r | |
1695 | (*String - '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r | |
1696 | );\r | |
1697 | \r | |
1698 | Result = Result * 10 + (*String - '0');\r | |
1699 | String++;\r | |
1700 | }\r | |
1701 | \r | |
1702 | return Result;\r | |
1703 | }\r | |
1704 | \r | |
1705 | \r | |
1706 | /**\r | |
1707 | Convert a Null-terminated ASCII decimal string to a value of type \r | |
1708 | UINT64.\r | |
1709 | \r | |
1710 | This function returns a value of type UINT64 by interpreting the contents \r | |
1711 | of the ASCII string String as a decimal number. The format of the input \r | |
1712 | ASCII string String is:\r | |
1713 | \r | |
1714 | [spaces] [decimal digits].\r | |
1715 | \r | |
1716 | The valid decimal digit character is in the range [0-9]. The function will \r | |
1717 | ignore the pad space, which includes spaces or tab characters, before the digits. \r | |
1718 | The running zero in the beginning of [decimal digits] will be ignored. Then, the \r | |
1719 | function stops at the first character that is a not a valid decimal character or \r | |
1720 | Null-terminator, whichever on comes first.\r | |
1721 | \r | |
1722 | If String has only pad spaces, then 0 is returned.\r | |
1723 | If String has no pad spaces or valid decimal digits, then 0 is returned.\r | |
1724 | If the number represented by String overflows according to the range defined by \r | |
1725 | UINT64, then ASSERT().\r | |
1726 | If String is NULL, then ASSERT().\r | |
1727 | If PcdMaximumAsciiStringLength is not zero, and String contains more than \r | |
1728 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r | |
1729 | then ASSERT().\r | |
1730 | \r | |
1731 | @param String Pointer to a Null-terminated ASCII string.\r | |
1732 | \r | |
1733 | @retval UINT64 \r | |
1734 | \r | |
1735 | **/\r | |
1736 | UINT64\r | |
1737 | EFIAPI\r | |
1738 | AsciiStrDecimalToUint64 (\r | |
1739 | IN CONST CHAR8 *String\r | |
1740 | )\r | |
1741 | {\r | |
1742 | UINT64 Result;\r | |
1743 | \r | |
1744 | ASSERT (String != NULL);\r | |
1745 | ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1746 | \r | |
1747 | //\r | |
1748 | // Ignore the pad spaces (space or tab)\r | |
1749 | //\r | |
1750 | while ((*String == ' ') || (*String == '\t')) {\r | |
1751 | String++;\r | |
1752 | }\r | |
1753 | \r | |
1754 | //\r | |
1755 | // Ignore leading Zeros after the spaces\r | |
1756 | //\r | |
1757 | while (*String == '0') {\r | |
1758 | String++;\r | |
1759 | }\r | |
1760 | \r | |
1761 | Result = 0;\r | |
1762 | \r | |
1763 | while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r | |
1764 | //\r | |
1765 | // If the number represented by String overflows according \r | |
1766 | // to the range defined by UINTN, then ASSERT().\r | |
1767 | //\r | |
1768 | ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r | |
1769 | ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r | |
1770 | (*String - '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r | |
1771 | );\r | |
1772 | \r | |
1773 | Result = MultU64x32 (Result, 10) + (*String - '0');\r | |
1774 | String++;\r | |
1775 | }\r | |
1776 | \r | |
1777 | return Result;\r | |
1778 | }\r | |
1779 | \r | |
1780 | /**\r | |
1781 | Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r | |
1782 | \r | |
1783 | This function returns a value of type UINTN by interpreting the contents of \r | |
1784 | the ASCII string String as a hexadecimal number. The format of the input ASCII \r | |
1785 | string String is:\r | |
1786 | \r | |
1787 | [spaces][zeros][x][hexadecimal digits].\r | |
1788 | \r | |
1789 | The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r | |
1790 | The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x" \r | |
1791 | appears in the input string, it must be prefixed with at least one 0. The function \r | |
1792 | will ignore the pad space, which includes spaces or tab characters, before [zeros], \r | |
1793 | [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits] \r | |
1794 | will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal \r | |
1795 | digit. Then, the function stops at the first character that is a not a valid \r | |
1796 | hexadecimal character or Null-terminator, whichever on comes first.\r | |
1797 | \r | |
1798 | If String has only pad spaces, then 0 is returned.\r | |
1799 | If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r | |
1800 | 0 is returned.\r | |
1801 | \r | |
1802 | If the number represented by String overflows according to the range defined by UINTN, \r | |
1803 | then ASSERT().\r | |
1804 | If String is NULL, then ASSERT().\r | |
1805 | If PcdMaximumAsciiStringLength is not zero, \r | |
1806 | and String contains more than PcdMaximumAsciiStringLength ASCII characters not including \r | |
1807 | the Null-terminator, then ASSERT().\r | |
1808 | \r | |
1809 | @param String Pointer to a Null-terminated ASCII string.\r | |
1810 | \r | |
1811 | @retval UINTN\r | |
1812 | \r | |
1813 | **/\r | |
1814 | UINTN\r | |
1815 | EFIAPI\r | |
1816 | AsciiStrHexToUintn (\r | |
1817 | IN CONST CHAR8 *String\r | |
1818 | )\r | |
1819 | {\r | |
1820 | UINTN Result;\r | |
1821 | \r | |
1822 | ASSERT (String != NULL);\r | |
1823 | ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
1824 | \r | |
1825 | //\r | |
1826 | // Ignore the pad spaces (space or tab) \r | |
1827 | //\r | |
1828 | while ((*String == ' ') || (*String == '\t')) {\r | |
1829 | String++;\r | |
1830 | }\r | |
1831 | \r | |
1832 | //\r | |
1833 | // Ignore leading Zeros after the spaces\r | |
1834 | //\r | |
1835 | while (*String == '0') {\r | |
1836 | String++;\r | |
1837 | }\r | |
1838 | \r | |
1839 | if (AsciiToUpper (*String) == 'X') {\r | |
1840 | ASSERT (*(String - 1) == '0');\r | |
1841 | if (*(String - 1) != '0') {\r | |
1842 | return 0;\r | |
1843 | }\r | |
1844 | //\r | |
1845 | // Skip the 'X'\r | |
1846 | //\r | |
1847 | String++;\r | |
1848 | }\r | |
1849 | \r | |
1850 | Result = 0;\r | |
1851 | \r | |
1852 | while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r | |
1853 | //\r | |
1854 | // If the Hex Number represented by String overflows according \r | |
1855 | // to the range defined by UINTN, then ASSERT().\r | |
1856 | //\r | |
1857 | ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r | |
1858 | ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r | |
1859 | (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r | |
1860 | );\r | |
1861 | \r | |
1862 | Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r | |
1863 | String++;\r | |
1864 | }\r | |
1865 | \r | |
1866 | return Result;\r | |
1867 | }\r | |
1868 | \r | |
1869 | \r | |
1870 | /**\r | |
1871 | Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r | |
1872 | \r | |
1873 | This function returns a value of type UINT64 by interpreting the contents of \r | |
1874 | the ASCII string String as a hexadecimal number. The format of the input ASCII \r | |
1875 | string String is:\r | |
1876 | \r | |
1877 | [spaces][zeros][x][hexadecimal digits].\r | |
1878 | \r | |
1879 | The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r | |
1880 | The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x" \r | |
1881 | appears in the input string, it must be prefixed with at least one 0. The function \r | |
1882 | will ignore the pad space, which includes spaces or tab characters, before [zeros], \r | |
1883 | [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits] \r | |
1884 | will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal \r | |
1885 | digit. Then, the function stops at the first character that is a not a valid \r | |
1886 | hexadecimal character or Null-terminator, whichever on comes first.\r | |
1887 | \r | |
1888 | If String has only pad spaces, then 0 is returned.\r | |
1889 | If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r | |
1890 | 0 is returned.\r | |
1891 | \r | |
1892 | If the number represented by String overflows according to the range defined by UINT64, \r | |
1893 | then ASSERT().\r | |
1894 | If String is NULL, then ASSERT().\r | |
1895 | If PcdMaximumAsciiStringLength is not zero, \r | |
1896 | and String contains more than PcdMaximumAsciiStringLength ASCII characters not including \r | |
1897 | the Null-terminator, then ASSERT().\r | |
1898 | \r | |
1899 | @param String Pointer to a Null-terminated ASCII string.\r | |
1900 | \r | |
1901 | @retval UINT64\r | |
1902 | \r | |
1903 | **/\r | |
1904 | UINT64\r | |
1905 | EFIAPI\r | |
1906 | AsciiStrHexToUint64 (\r | |
1907 | IN CONST CHAR8 *String\r | |
1908 | )\r | |
1909 | {\r | |
1910 | UINT64 Result;\r | |
1911 | \r | |
1912 | ASSERT (String != NULL);\r | |
1913 | ASSERT (AsciiStrLen (String) < PcdGet32 (PcdMaximumUnicodeStringLength));\r | |
1914 | \r | |
1915 | //\r | |
1916 | // Ignore the pad spaces (space or tab) and leading Zeros\r | |
1917 | //\r | |
1918 | //\r | |
1919 | // Ignore the pad spaces (space or tab) \r | |
1920 | //\r | |
1921 | while ((*String == ' ') || (*String == '\t')) {\r | |
1922 | String++;\r | |
1923 | }\r | |
1924 | \r | |
1925 | //\r | |
1926 | // Ignore leading Zeros after the spaces\r | |
1927 | //\r | |
1928 | while (*String == '0') {\r | |
1929 | String++;\r | |
1930 | }\r | |
1931 | \r | |
1932 | if (AsciiToUpper (*String) == 'X') {\r | |
1933 | ASSERT (*(String - 1) == '0');\r | |
1934 | if (*(String - 1) != '0') {\r | |
1935 | return 0;\r | |
1936 | }\r | |
1937 | //\r | |
1938 | // Skip the 'X'\r | |
1939 | //\r | |
1940 | String++;\r | |
1941 | }\r | |
1942 | \r | |
1943 | Result = 0;\r | |
1944 | \r | |
1945 | while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r | |
1946 | //\r | |
1947 | // If the Hex Number represented by String overflows according \r | |
1948 | // to the range defined by UINTN, then ASSERT().\r | |
1949 | //\r | |
1950 | ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16) ||\r | |
1951 | ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r | |
1952 | (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r | |
1953 | );\r | |
1954 | \r | |
1955 | Result = LShiftU64 (Result, 4);\r | |
1956 | Result = Result + InternalAsciiHexCharToUintn (*String);\r | |
1957 | String++;\r | |
1958 | }\r | |
1959 | \r | |
1960 | return Result;\r | |
1961 | }\r | |
1962 | \r | |
1963 | \r | |
1964 | /**\r | |
1965 | Convert one Null-terminated ASCII string to a Null-terminated \r | |
1966 | Unicode string and returns the Unicode string.\r | |
1967 | \r | |
1968 | This function converts the contents of the ASCII string Source to the Unicode \r | |
1969 | string Destination, and returns Destination. The function terminates the \r | |
1970 | Unicode string Destination by appending a Null-terminator character at the end. \r | |
1971 | The caller is responsible to make sure Destination points to a buffer with size \r | |
1972 | equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r | |
1973 | \r | |
1974 | If Destination is NULL, then ASSERT().\r | |
1975 | If Destination is not aligned on a 16-bit boundary, then ASSERT().\r | |
1976 | If Source is NULL, then ASSERT().\r | |
1977 | If Source and Destination overlap, then ASSERT().\r | |
1978 | If PcdMaximumAsciiStringLength is not zero, and Source contains more than \r | |
1979 | PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r | |
1980 | then ASSERT().\r | |
1981 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than \r | |
1982 | PcdMaximumUnicodeStringLength ASCII characters not including the \r | |
1983 | Null-terminator, then ASSERT().\r | |
1984 | \r | |
1985 | @param Source Pointer to a Null-terminated ASCII string.\r | |
1986 | @param Destination Pointer to a Null-terminated Unicode string.\r | |
1987 | \r | |
1988 | @reture Destination\r | |
1989 | \r | |
1990 | **/\r | |
1991 | CHAR16 *\r | |
1992 | EFIAPI\r | |
1993 | AsciiStrToUnicodeStr (\r | |
1994 | IN CONST CHAR8 *Source,\r | |
1995 | OUT CHAR16 *Destination\r | |
1996 | )\r | |
1997 | {\r | |
1998 | ASSERT (Destination != NULL);\r | |
1999 | ASSERT (Source != NULL);\r | |
2000 | \r | |
2001 | //\r | |
2002 | // Source and Destination should not overlap\r | |
2003 | //\r | |
2004 | ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r | |
2005 | ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r | |
2006 | \r | |
2007 | //\r | |
2008 | // If PcdMaximumAsciiStringLength is not zero,\r | |
2009 | // length of Source should not more than PcdMaximumUnicodeStringLength\r | |
2010 | //\r | |
2011 | if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r | |
2012 | ASSERT (AsciiStrLen (Source) < PcdGet32 (PcdMaximumAsciiStringLength));\r | |
2013 | }\r | |
2014 | \r | |
2015 | while (*Source != '\0') {\r | |
2016 | *(Destination++) = (CHAR16) *(Source++);\r | |
2017 | }\r | |
2018 | //\r | |
2019 | // End the Destination with a NULL.\r | |
2020 | //\r | |
2021 | *Destination = '\0';\r | |
2022 | \r | |
2023 | return Destination;\r | |
2024 | }\r | |
2025 | \r | |
2026 | /**\r | |
2027 | Converts an 8-bit value to an 8-bit BCD value.\r | |
2028 | \r | |
2029 | Converts the 8-bit value specified by Value to BCD. The BCD value is\r | |
2030 | returned.\r | |
2031 | \r | |
2032 | If Value >= 100, then ASSERT().\r | |
2033 | \r | |
2034 | @param Value The 8-bit value to convert to BCD. Range 0..99.\r | |
2035 | \r | |
2036 | @return The BCD value\r | |
2037 | \r | |
2038 | **/\r | |
2039 | UINT8\r | |
2040 | EFIAPI\r | |
2041 | DecimalToBcd8 (\r | |
2042 | IN UINT8 Value\r | |
2043 | )\r | |
2044 | {\r | |
2045 | ASSERT (Value < 100);\r | |
2046 | return (UINT8) (((Value / 10) << 4) | (Value % 10));\r | |
2047 | }\r | |
2048 | \r | |
2049 | /**\r | |
2050 | Converts an 8-bit BCD value to an 8-bit value.\r | |
2051 | \r | |
2052 | Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r | |
2053 | value is returned.\r | |
2054 | \r | |
2055 | If Value >= 0xA0, then ASSERT().\r | |
2056 | If (Value & 0x0F) >= 0x0A, then ASSERT().\r | |
2057 | \r | |
2058 | @param Value The 8-bit BCD value to convert to an 8-bit value.\r | |
2059 | \r | |
2060 | @return The 8-bit value is returned.\r | |
2061 | \r | |
2062 | **/\r | |
2063 | UINT8\r | |
2064 | EFIAPI\r | |
2065 | BcdToDecimal8 (\r | |
2066 | IN UINT8 Value\r | |
2067 | )\r | |
2068 | {\r | |
2069 | ASSERT (Value < 0xa0);\r | |
2070 | ASSERT ((Value & 0xf) < 0xa);\r | |
2071 | return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r | |
2072 | }\r | |
2073 | \r | |
2074 | \r |