]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/String.c
1. Add conformance checking to ensure the input & output string are well-defined.
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
CommitLineData
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
45CHAR16 *\r
46EFIAPI\r
47StrCpy (\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
103CHAR16 *\r
104EFIAPI\r
105StrnCpy (\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
158UINTN\r
159EFIAPI\r
160StrLen (\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
199UINTN\r
200EFIAPI\r
201StrSize (\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
236INTN\r
237EFIAPI\r
238StrCmp (\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
286INTN\r
287EFIAPI\r
288StrnCmp (\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
346CHAR16 *\r
347EFIAPI\r
348StrCat (\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
400CHAR16 *\r
401EFIAPI\r
402StrnCat (\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
4df26661 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
e1f414b6 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
444CHAR16 *\r
445EFIAPI\r
446StrStr (\r
4df26661 447 IN CONST CHAR16 *String,\r
448 IN CONST CHAR16 *SearchString\r
e1f414b6 449 )\r
450{\r
451 CONST CHAR16 *FirstMatch;\r
452 CONST CHAR16 *SearchStringTmp;\r
453\r
e1f414b6 454 //\r
4df26661 455 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
456 // Length tests are performed inside StrLen().\r
e1f414b6 457 //\r
4df26661 458 ASSERT (StrSize (String) != 0);\r
459 ASSERT (StrSize (SearchString) != 0);\r
e1f414b6 460\r
461 while (*String != '\0') {\r
462 SearchStringTmp = SearchString;\r
463 FirstMatch = String;\r
464 \r
465 while ((*String == *SearchStringTmp) \r
466 && (*SearchStringTmp != '\0') \r
467 && (*String != '\0')) {\r
468 String++;\r
469 SearchStringTmp++;\r
470 } \r
471 \r
472 if (*SearchStringTmp == '\0') {\r
473 return (CHAR16 *) FirstMatch;\r
474 }\r
475\r
476 if (SearchStringTmp == SearchString) {\r
477 //\r
478 // If no character from SearchString match,\r
479 // move the pointer to the String under search\r
480 // by one character.\r
481 //\r
482 String++;\r
483 }\r
484 }\r
485\r
486 return NULL;\r
487}\r
488\r
489/**\r
490 Check if a Unicode character is a decimal character.\r
491\r
492 This internal function checks if a Unicode character is a \r
493 decimal character. The valid decimal character is from\r
494 L'0' to L'9'.\r
495\r
496\r
497 @param Char The character to check against.\r
498\r
499 @retval TRUE If the Char is a decmial character.\r
500 @retval FALSE Otherwise.\r
501\r
502**/\r
503STATIC\r
504BOOLEAN\r
505InternalIsDecimalDigitCharacter (\r
506 IN CHAR16 Char\r
507 )\r
508{\r
509 return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r
510}\r
511\r
512/**\r
513 Convert a Unicode character to upper case only if \r
514 it maps to a valid small-case ASCII character.\r
515\r
516 This internal function only deal with Unicode character\r
517 which maps to a valid small-case ASII character, i.e.\r
518 L'a' to L'z'. For other Unicode character, the input character\r
519 is returned directly.\r
520\r
521\r
522 @param Char The character to convert.\r
523\r
524 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
525 @retval Unchanged Otherwise.\r
526\r
527**/\r
528STATIC\r
529CHAR16\r
530InternalCharToUpper (\r
531 IN CHAR16 Char\r
532 )\r
533{\r
534 if (Char >= L'a' && Char <= L'z') {\r
535 return (CHAR16) (Char - (L'a' - L'A'));\r
536 }\r
537\r
538 return Char;\r
539}\r
540\r
541/**\r
542 Convert a Unicode character to numerical value.\r
543\r
544 This internal function only deal with Unicode character\r
545 which maps to a valid hexadecimal ASII character, i.e.\r
546 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
547 Unicode character, the value returned does not make sense.\r
548\r
549 @param Char The character to convert.\r
550\r
551 @retval UINTN The numerical value converted.\r
552\r
553**/\r
554STATIC\r
555UINTN\r
556InternalHexCharToUintn (\r
557 IN CHAR16 Char\r
558 )\r
559{\r
560 if (InternalIsDecimalDigitCharacter (Char)) {\r
561 return Char - L'0';\r
562 }\r
563\r
564 return (UINTN) (10 + InternalCharToUpper (Char) - L'A');\r
565}\r
566\r
567/**\r
568 Check if a Unicode character is a hexadecimal character.\r
569\r
570 This internal function checks if a Unicode character is a \r
571 decimal character. The valid hexadecimal character is \r
572 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
573\r
574\r
575 @param Char The character to check against.\r
576\r
577 @retval TRUE If the Char is a hexadecmial character.\r
578 @retval FALSE Otherwise.\r
579\r
580**/\r
581STATIC\r
582BOOLEAN\r
583InternalIsHexaDecimalDigitCharacter (\r
584 IN CHAR16 Char\r
585 )\r
586{\r
587\r
588 return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||\r
589 (Char >= L'A' && Char <= L'F') ||\r
590 (Char >= L'a' && Char <= L'f'));\r
591}\r
592\r
593/**\r
594 Convert a Null-terminated Unicode decimal string to a value of \r
595 type UINTN.\r
596\r
597 This function returns a value of type UINTN by interpreting the contents \r
598 of the Unicode string specified by String as a decimal number. The format \r
599 of the input Unicode string String is:\r
600 \r
601 [spaces] [decimal digits].\r
602 \r
603 The valid decimal digit character is in the range [0-9]. The \r
604 function will ignore the pad space, which includes spaces or \r
605 tab characters, before [decimal digits]. The running zero in the \r
606 beginning of [decimal digits] will be ignored. Then, the function \r
607 stops at the first character that is a not a valid decimal character \r
608 or a Null-terminator, whichever one comes first. \r
609 \r
610 If String is NULL, then ASSERT().\r
611 If String is not aligned in a 16-bit boundary, then ASSERT(). \r
612 If String has only pad spaces, then 0 is returned.\r
613 If String has no pad spaces or valid decimal digits, \r
614 then 0 is returned.\r
615 If the number represented by String overflows according \r
616 to the range defined by UINTN, then ASSERT().\r
617 \r
618 If PcdMaximumUnicodeStringLength is not zero, and String contains \r
619 more than PcdMaximumUnicodeStringLength Unicode characters not including \r
620 the Null-terminator, then ASSERT().\r
621\r
4df26661 622 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 623\r
624 @retval UINTN \r
625\r
626**/\r
627UINTN\r
628EFIAPI\r
629StrDecimalToUintn (\r
4df26661 630 IN CONST CHAR16 *String\r
e1f414b6 631 )\r
632{\r
633 UINTN Result;\r
634 \r
4df26661 635 //\r
636 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
637 // Length tests are performed inside StrLen().\r
638 //\r
639 ASSERT (StrSize (String) != 0);\r
e1f414b6 640\r
641 //\r
642 // Ignore the pad spaces (space or tab)\r
643 //\r
644 while ((*String == L' ') || (*String == L'\t')) {\r
645 String++;\r
646 }\r
647\r
648 //\r
649 // Ignore leading Zeros after the spaces\r
650 //\r
651 while (*String == L'0') {\r
652 String++;\r
653 }\r
654\r
655 Result = 0;\r
656\r
657 while (InternalIsDecimalDigitCharacter (*String)) {\r
658 //\r
659 // If the number represented by String overflows according \r
660 // to the range defined by UINTN, then ASSERT().\r
661 //\r
662 ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
663 ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) &&\r
664 (*String - L'0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r
665 );\r
666\r
667 Result = Result * 10 + (*String - L'0');\r
668 String++;\r
669 }\r
670 \r
671 return Result;\r
672}\r
673\r
674\r
675/**\r
676 Convert a Null-terminated Unicode decimal string to a value of \r
677 type UINT64.\r
678\r
679 This function returns a value of type UINT64 by interpreting the contents \r
680 of the Unicode string specified by String as a decimal number. The format \r
681 of the input Unicode string String is:\r
682 \r
683 [spaces] [decimal digits].\r
684 \r
685 The valid decimal digit character is in the range [0-9]. The \r
686 function will ignore the pad space, which includes spaces or \r
687 tab characters, before [decimal digits]. The running zero in the \r
688 beginning of [decimal digits] will be ignored. Then, the function \r
689 stops at the first character that is a not a valid decimal character \r
690 or a Null-terminator, whichever one comes first. \r
691 \r
692 If String is NULL, then ASSERT().\r
693 If String is not aligned in a 16-bit boundary, then ASSERT(). \r
694 If String has only pad spaces, then 0 is returned.\r
695 If String has no pad spaces or valid decimal digits, \r
696 then 0 is returned.\r
697 If the number represented by String overflows according \r
698 to the range defined by UINT64, then ASSERT().\r
699 \r
700 If PcdMaximumUnicodeStringLength is not zero, and String contains \r
701 more than PcdMaximumUnicodeStringLength Unicode characters not including \r
702 the Null-terminator, then ASSERT().\r
703\r
4df26661 704 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 705\r
706 @retval UINT64 \r
707\r
708**/\r
709UINT64\r
710EFIAPI\r
711StrDecimalToUint64 (\r
4df26661 712 IN CONST CHAR16 *String\r
e1f414b6 713 )\r
714{\r
715 UINT64 Result;\r
716 \r
4df26661 717 //\r
718 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
719 // Length tests are performed inside StrLen().\r
720 //\r
721 ASSERT (StrSize (String) != 0);\r
e1f414b6 722\r
723 //\r
724 // Ignore the pad spaces (space or tab)\r
725 //\r
726 while ((*String == L' ') || (*String == L'\t')) {\r
727 String++;\r
728 }\r
729\r
730 //\r
731 // Ignore leading Zeros after the spaces\r
732 //\r
733 while (*String == L'0') {\r
734 String++;\r
735 }\r
736\r
737 Result = 0;\r
738\r
739 while (InternalIsDecimalDigitCharacter (*String)) {\r
740 //\r
741 // If the number represented by String overflows according \r
742 // to the range defined by UINTN, then ASSERT().\r
743 //\r
744 ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r
745 ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r
746 (*String - L'0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r
747 );\r
748\r
749 Result = MultU64x32 (Result, 10) + (*String - L'0');\r
750 String++;\r
751 }\r
752 \r
753 return Result;\r
754}\r
755\r
756/**\r
757 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.\r
758\r
759 This function returns a value of type UINTN by interpreting the contents \r
760 of the Unicode string specified by String as a hexadecimal number. \r
761 The format of the input Unicode string String is:\r
762 \r
763 [spaces][zeros][x][hexadecimal digits]. \r
764\r
765 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r
766 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. \r
767 If "x" appears in the input string, it must be prefixed with at least one 0. \r
768 The function will ignore the pad space, which includes spaces or tab characters, \r
769 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or \r
770 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the \r
771 first valid hexadecimal digit. Then, the function stops at the first character that is \r
772 a not a valid hexadecimal character or NULL, whichever one comes first.\r
773\r
774 If String is NULL, then ASSERT().\r
775 If String is not aligned in a 16-bit boundary, then ASSERT().\r
776 If String has only pad spaces, then zero is returned.\r
777 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, \r
778 then zero is returned.\r
779 If the number represented by String overflows according to the range defined by \r
780 UINTN, then ASSERT().\r
781\r
782 If PcdMaximumUnicodeStringLength is not zero, and String contains more than \r
783 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, \r
784 then ASSERT().\r
785\r
4df26661 786 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 787\r
788 @retval UINTN\r
789\r
790**/\r
791UINTN\r
792EFIAPI\r
793StrHexToUintn (\r
4df26661 794 IN CONST CHAR16 *String\r
e1f414b6 795 )\r
796{\r
797 UINTN Result;\r
798\r
4df26661 799 //\r
800 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
801 // Length tests are performed inside StrLen().\r
802 //\r
803 ASSERT (StrSize (String) != 0);\r
e1f414b6 804 \r
805 //\r
806 // Ignore the pad spaces (space or tab) \r
807 //\r
808 while ((*String == L' ') || (*String == L'\t')) {\r
809 String++;\r
810 }\r
811\r
812 //\r
813 // Ignore leading Zeros after the spaces\r
814 //\r
815 while (*String == L'0') {\r
816 String++;\r
817 }\r
818\r
819 if (InternalCharToUpper (*String) == L'X') {\r
820 ASSERT (*(String - 1) == L'0');\r
821 if (*(String - 1) != L'0') {\r
822 return 0;\r
823 }\r
824 //\r
825 // Skip the 'X'\r
826 //\r
827 String++;\r
828 }\r
829\r
830 Result = 0;\r
831 \r
832 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
833 //\r
834 // If the Hex Number represented by String overflows according \r
835 // to the range defined by UINTN, then ASSERT().\r
836 //\r
837 ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
838 ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r
839 (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r
840 );\r
841\r
842 Result = (Result << 4) + InternalHexCharToUintn (*String);\r
843 String++;\r
844 }\r
845\r
846 return Result;\r
847}\r
848\r
849\r
850/**\r
851 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
852\r
853 This function returns a value of type UINT64 by interpreting the contents \r
854 of the Unicode string specified by String as a hexadecimal number. \r
855 The format of the input Unicode string String is \r
856 \r
857 [spaces][zeros][x][hexadecimal digits]. \r
858\r
859 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r
860 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. \r
861 If "x" appears in the input string, it must be prefixed with at least one 0. \r
862 The function will ignore the pad space, which includes spaces or tab characters, \r
863 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or \r
864 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the \r
865 first valid hexadecimal digit. Then, the function stops at the first character that is \r
866 a not a valid hexadecimal character or NULL, whichever one comes first.\r
867\r
868 If String is NULL, then ASSERT().\r
869 If String is not aligned in a 16-bit boundary, then ASSERT().\r
870 If String has only pad spaces, then zero is returned.\r
871 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, \r
872 then zero is returned.\r
873 If the number represented by String overflows according to the range defined by \r
874 UINT64, then ASSERT().\r
875\r
876 If PcdMaximumUnicodeStringLength is not zero, and String contains more than \r
877 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator, \r
878 then ASSERT().\r
879\r
4df26661 880 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 881\r
882 @retval UINT64\r
883\r
884**/\r
885UINT64\r
886EFIAPI\r
887StrHexToUint64 (\r
4df26661 888 IN CONST CHAR16 *String\r
e1f414b6 889 )\r
890{\r
891 UINT64 Result;\r
892\r
4df26661 893 //\r
894 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
895 // Length tests are performed inside StrLen().\r
896 //\r
897 ASSERT (StrSize (String) != 0);\r
e1f414b6 898 \r
899 //\r
900 // Ignore the pad spaces (space or tab) \r
901 //\r
902 while ((*String == L' ') || (*String == L'\t')) {\r
903 String++;\r
904 }\r
905\r
906 //\r
907 // Ignore leading Zeros after the spaces\r
908 //\r
909 while (*String == L'0') {\r
910 String++;\r
911 }\r
912\r
913 if (InternalCharToUpper (*String) == L'X') {\r
914 ASSERT (*(String - 1) == L'0');\r
915 if (*(String - 1) != L'0') {\r
916 return 0;\r
917 }\r
918 //\r
919 // Skip the 'X'\r
920 //\r
921 String++;\r
922 }\r
923\r
924 Result = 0;\r
925 \r
926 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
927 //\r
928 // If the Hex Number represented by String overflows according \r
929 // to the range defined by UINTN, then ASSERT().\r
930 //\r
931 ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16)|| \r
932 ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r
933 (InternalHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r
934 );\r
935\r
936 Result = LShiftU64 (Result, 4);\r
937 Result = Result + InternalHexCharToUintn (*String);\r
938 String++;\r
939 }\r
940\r
941 return Result;\r
942}\r
943\r
944/**\r
945 Check if a ASCII character is a decimal character.\r
946\r
947 This internal function checks if a Unicode character is a \r
948 decimal character. The valid decimal character is from\r
949 '0' to '9'.\r
950\r
951 @param Char The character to check against.\r
952\r
953 @retval TRUE If the Char is a decmial character.\r
954 @retval FALSE Otherwise.\r
955\r
956**/\r
957STATIC\r
958BOOLEAN\r
959InternalAsciiIsDecimalDigitCharacter (\r
960 IN CHAR8 Char\r
961 )\r
962{\r
963 return (BOOLEAN) (Char >= '0' && Char <= '9');\r
964}\r
965\r
966/**\r
967 Check if a ASCII character is a hexadecimal character.\r
968\r
969 This internal function checks if a ASCII character is a \r
970 decimal character. The valid hexadecimal character is \r
971 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
972\r
973\r
974 @param Char The character to check against.\r
975\r
976 @retval TRUE If the Char is a hexadecmial character.\r
977 @retval FALSE Otherwise.\r
978\r
979**/\r
980STATIC\r
981BOOLEAN\r
982InternalAsciiIsHexaDecimalDigitCharacter (\r
983 IN CHAR8 Char\r
984 )\r
985{\r
986\r
987 return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||\r
988 (Char >= 'A' && Char <= 'F') ||\r
989 (Char >= 'a' && Char <= 'f'));\r
990}\r
991\r
992/**\r
993 Convert a Null-terminated Unicode string to a Null-terminated \r
994 ASCII string and returns the ASCII string.\r
995 \r
996 This function converts the content of the Unicode string Source \r
997 to the ASCII string Destination by copying the lower 8 bits of \r
998 each Unicode character. It returns Destination. The function terminates \r
999 the ASCII string Destination by appending a Null-terminator character \r
1000 at the end. The caller is responsible to make sure Destination points \r
1001 to a buffer with size equal or greater than (StrLen (Source) + 1) in bytes.\r
1002\r
1003 If Destination is NULL, then ASSERT().\r
1004 If Source is NULL, then ASSERT().\r
1005 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
1006 If Source and Destination overlap, then ASSERT().\r
1007\r
1008 If any Unicode characters in Source contain non-zero value in \r
1009 the upper 8 bits, then ASSERT().\r
1010 \r
1011 If PcdMaximumUnicodeStringLength is not zero, and Source contains \r
1012 more than PcdMaximumUnicodeStringLength Unicode characters not including \r
1013 the Null-terminator, then ASSERT().\r
1014 \r
1015 If PcdMaximumAsciiStringLength is not zero, and Source contains more \r
1016 than PcdMaximumAsciiStringLength Unicode characters not including the \r
1017 Null-terminator, then ASSERT().\r
1018\r
1019 @param Source Pointer to a Null-terminated Unicode string.\r
1020 @param Destination Pointer to a Null-terminated ASCII string.\r
1021\r
1022 @reture Destination\r
1023\r
1024**/\r
1025CHAR8 *\r
1026EFIAPI\r
1027UnicodeStrToAsciiStr (\r
4df26661 1028 IN CONST CHAR16 *Source,\r
1029 OUT CHAR8 *Destination\r
e1f414b6 1030 )\r
1031{\r
4df26661 1032 CHAR8 *ReturnValue;\r
1033\r
e1f414b6 1034 ASSERT (Destination != NULL);\r
4df26661 1035\r
1036 //\r
1037 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
1038 // Length tests are performed inside StrLen().\r
1039 //\r
1040 ASSERT (StrSize (Source) != 0);\r
e1f414b6 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
e1f414b6 1048\r
4df26661 1049 ReturnValue = Destination;\r
e1f414b6 1050 while (*Source != '\0') {\r
1051 //\r
1052 // If any Unicode characters in Source contain \r
1053 // non-zero value in the upper 8 bits, then ASSERT().\r
1054 //\r
1055 ASSERT (*Source < 0x100);\r
1056 *(Destination++) = (CHAR8) *(Source++);\r
1057 }\r
1058\r
1059 *Destination = '\0';\r
4df26661 1060\r
1061 //\r
1062 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
1063 // Length tests are performed inside AsciiStrLen().\r
1064 //\r
1065 ASSERT (AsciiStrSize (ReturnValue) != 0);\r
1066\r
1067 return ReturnValue;\r
e1f414b6 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
1092CHAR8 *\r
1093EFIAPI\r
1094AsciiStrCpy (\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
1146CHAR8 *\r
1147EFIAPI\r
1148AsciiStrnCpy (\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
1198UINTN\r
1199EFIAPI\r
1200AsciiStrLen (\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
1237UINTN\r
1238EFIAPI\r
1239AsciiStrSize (\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
1272INTN\r
1273EFIAPI\r
1274AsciiStrCmp (\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
1306STATIC\r
1307CHAR8\r
1308AsciiToUpper (\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
1328STATIC\r
1329UINTN\r
1330InternalAsciiHexCharToUintn (\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
1371INTN\r
1372EFIAPI\r
1373AsciiStriCmp (\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
1426INTN\r
1427EFIAPI\r
1428AsciiStrnCmp (\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
1481CHAR8 *\r
1482EFIAPI\r
1483AsciiStrCat (\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
1532CHAR8 *\r
1533EFIAPI\r
1534AsciiStrnCat (\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
4df26661 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
e1f414b6 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
1573CHAR8 *\r
1574EFIAPI\r
1575AsciiStrStr (\r
4df26661 1576 IN CONST CHAR8 *String,\r
e1f414b6 1577 IN CONST CHAR8 *SearchString\r
1578 )\r
1579{\r
1580 CONST CHAR8 *FirstMatch;\r
1581 CONST CHAR8 *SearchStringTmp;\r
1582\r
e1f414b6 1583 //\r
4df26661 1584 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
e1f414b6 1585 //\r
4df26661 1586 ASSERT (AsciiStrSize (String) != 0);\r
1587 ASSERT (AsciiStrSize (SearchString) != 0);\r
e1f414b6 1588\r
1589 while (*String != '\0') {\r
1590 SearchStringTmp = SearchString;\r
1591 FirstMatch = String;\r
1592 \r
1593 while ((*String == *SearchStringTmp) \r
1594 && (*SearchStringTmp != '\0') \r
1595 && (*String != '\0')) {\r
1596 String++;\r
1597 SearchStringTmp++;\r
1598 } \r
1599 \r
1600 if (*SearchStringTmp == '\0') {\r
1601 return (CHAR8 *) FirstMatch;\r
1602 }\r
1603\r
1604 if (SearchStringTmp == SearchString) {\r
1605 //\r
1606 // If no character from SearchString match,\r
1607 // move the pointer to the String under search\r
1608 // by one character.\r
1609 //\r
1610 String++;\r
1611 }\r
1612\r
1613 }\r
1614\r
1615 return NULL;\r
1616}\r
1617\r
1618/**\r
1619 Convert a Null-terminated ASCII decimal string to a value of type \r
1620 UINTN.\r
1621\r
1622 This function returns a value of type UINTN by interpreting the contents \r
1623 of the ASCII string String as a decimal number. The format of the input \r
1624 ASCII string String is:\r
1625 \r
1626 [spaces] [decimal digits].\r
1627 \r
1628 The valid decimal digit character is in the range [0-9]. The function will \r
1629 ignore the pad space, which includes spaces or tab characters, before the digits. \r
1630 The running zero in the beginning of [decimal digits] will be ignored. Then, the \r
1631 function stops at the first character that is a not a valid decimal character or \r
1632 Null-terminator, whichever on comes first.\r
1633 \r
1634 If String has only pad spaces, then 0 is returned.\r
1635 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
1636 If the number represented by String overflows according to the range defined by \r
1637 UINTN, then ASSERT().\r
1638 If String is NULL, then ASSERT().\r
1639 If PcdMaximumAsciiStringLength is not zero, and String contains more than \r
1640 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r
1641 then ASSERT().\r
1642\r
4df26661 1643 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1644\r
1645 @retval UINTN \r
1646\r
1647**/\r
1648UINTN\r
1649EFIAPI\r
1650AsciiStrDecimalToUintn (\r
4df26661 1651 IN CONST CHAR8 *String\r
e1f414b6 1652 )\r
1653{\r
1654 UINTN Result;\r
1655 \r
4df26661 1656 //\r
1657 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1658 //\r
1659 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1660\r
1661 //\r
1662 // Ignore the pad spaces (space or tab)\r
1663 //\r
1664 while ((*String == ' ') || (*String == '\t')) {\r
1665 String++;\r
1666 }\r
1667\r
1668 //\r
1669 // Ignore leading Zeros after the spaces\r
1670 //\r
1671 while (*String == '0') {\r
1672 String++;\r
1673 }\r
1674\r
1675 Result = 0;\r
1676\r
1677 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1678 //\r
1679 // If the number represented by String overflows according \r
1680 // to the range defined by UINTN, then ASSERT().\r
1681 //\r
1682 ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
1683 ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_10) && \r
1684 (*String - '0') <= REMINDER_MAX_UINTN_DIVIDED_BY_10)\r
1685 );\r
1686\r
1687 Result = Result * 10 + (*String - '0');\r
1688 String++;\r
1689 }\r
1690 \r
1691 return Result;\r
1692}\r
1693\r
1694\r
1695/**\r
1696 Convert a Null-terminated ASCII decimal string to a value of type \r
1697 UINT64.\r
1698\r
1699 This function returns a value of type UINT64 by interpreting the contents \r
1700 of the ASCII string String as a decimal number. The format of the input \r
1701 ASCII string String is:\r
1702 \r
1703 [spaces] [decimal digits].\r
1704 \r
1705 The valid decimal digit character is in the range [0-9]. The function will \r
1706 ignore the pad space, which includes spaces or tab characters, before the digits. \r
1707 The running zero in the beginning of [decimal digits] will be ignored. Then, the \r
1708 function stops at the first character that is a not a valid decimal character or \r
1709 Null-terminator, whichever on comes first.\r
1710 \r
1711 If String has only pad spaces, then 0 is returned.\r
1712 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
1713 If the number represented by String overflows according to the range defined by \r
1714 UINT64, then ASSERT().\r
1715 If String is NULL, then ASSERT().\r
1716 If PcdMaximumAsciiStringLength is not zero, and String contains more than \r
1717 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r
1718 then ASSERT().\r
1719\r
4df26661 1720 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1721\r
1722 @retval UINT64 \r
1723\r
1724**/\r
1725UINT64\r
1726EFIAPI\r
1727AsciiStrDecimalToUint64 (\r
4df26661 1728 IN CONST CHAR8 *String\r
e1f414b6 1729 )\r
1730{\r
1731 UINT64 Result;\r
1732 \r
4df26661 1733 //\r
1734 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1735 //\r
1736 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1737\r
1738 //\r
1739 // Ignore the pad spaces (space or tab)\r
1740 //\r
1741 while ((*String == ' ') || (*String == '\t')) {\r
1742 String++;\r
1743 }\r
1744\r
1745 //\r
1746 // Ignore leading Zeros after the spaces\r
1747 //\r
1748 while (*String == '0') {\r
1749 String++;\r
1750 }\r
1751\r
1752 Result = 0;\r
1753\r
1754 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1755 //\r
1756 // If the number represented by String overflows according \r
1757 // to the range defined by UINTN, then ASSERT().\r
1758 //\r
1759 ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_10) || \r
1760 ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_10) && \r
1761 (*String - '0') <= REMINDER_MAX_UINT64_DIVIDED_BY_10)\r
1762 );\r
1763\r
1764 Result = MultU64x32 (Result, 10) + (*String - '0');\r
1765 String++;\r
1766 }\r
1767 \r
1768 return Result;\r
1769}\r
1770\r
1771/**\r
1772 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1773\r
1774 This function returns a value of type UINTN by interpreting the contents of \r
1775 the ASCII string String as a hexadecimal number. The format of the input ASCII \r
1776 string String is:\r
1777 \r
1778 [spaces][zeros][x][hexadecimal digits].\r
1779 \r
1780 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r
1781 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x" \r
1782 appears in the input string, it must be prefixed with at least one 0. The function \r
1783 will ignore the pad space, which includes spaces or tab characters, before [zeros], \r
1784 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits] \r
1785 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal \r
1786 digit. Then, the function stops at the first character that is a not a valid \r
1787 hexadecimal character or Null-terminator, whichever on comes first.\r
1788 \r
1789 If String has only pad spaces, then 0 is returned.\r
1790 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1791 0 is returned.\r
1792\r
1793 If the number represented by String overflows according to the range defined by UINTN, \r
1794 then ASSERT().\r
1795 If String is NULL, then ASSERT().\r
1796 If PcdMaximumAsciiStringLength is not zero, \r
1797 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including \r
1798 the Null-terminator, then ASSERT().\r
1799\r
4df26661 1800 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1801\r
1802 @retval UINTN\r
1803\r
1804**/\r
1805UINTN\r
1806EFIAPI\r
1807AsciiStrHexToUintn (\r
4df26661 1808 IN CONST CHAR8 *String\r
e1f414b6 1809 )\r
1810{\r
1811 UINTN Result;\r
1812\r
4df26661 1813 //\r
1814 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1815 //\r
1816 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1817 \r
1818 //\r
1819 // Ignore the pad spaces (space or tab) \r
1820 //\r
1821 while ((*String == ' ') || (*String == '\t')) {\r
1822 String++;\r
1823 }\r
1824\r
1825 //\r
1826 // Ignore leading Zeros after the spaces\r
1827 //\r
1828 while (*String == '0') {\r
1829 String++;\r
1830 }\r
1831\r
1832 if (AsciiToUpper (*String) == 'X') {\r
1833 ASSERT (*(String - 1) == '0');\r
1834 if (*(String - 1) != '0') {\r
1835 return 0;\r
1836 }\r
1837 //\r
1838 // Skip the 'X'\r
1839 //\r
1840 String++;\r
1841 }\r
1842\r
1843 Result = 0;\r
1844 \r
1845 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1846 //\r
1847 // If the Hex Number represented by String overflows according \r
1848 // to the range defined by UINTN, then ASSERT().\r
1849 //\r
1850 ASSERT ((Result < QUIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
1851 ((Result == QUIENT_MAX_UINTN_DIVIDED_BY_16) && \r
1852 (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINTN_DIVIDED_BY_16))\r
1853 );\r
1854\r
1855 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
1856 String++;\r
1857 }\r
1858\r
1859 return Result;\r
1860}\r
1861\r
1862\r
1863/**\r
1864 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1865\r
1866 This function returns a value of type UINT64 by interpreting the contents of \r
1867 the ASCII string String as a hexadecimal number. The format of the input ASCII \r
1868 string String is:\r
1869 \r
1870 [spaces][zeros][x][hexadecimal digits].\r
1871 \r
1872 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F]. \r
1873 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x" \r
1874 appears in the input string, it must be prefixed with at least one 0. The function \r
1875 will ignore the pad space, which includes spaces or tab characters, before [zeros], \r
1876 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits] \r
1877 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal \r
1878 digit. Then, the function stops at the first character that is a not a valid \r
1879 hexadecimal character or Null-terminator, whichever on comes first.\r
1880 \r
1881 If String has only pad spaces, then 0 is returned.\r
1882 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1883 0 is returned.\r
1884\r
1885 If the number represented by String overflows according to the range defined by UINT64, \r
1886 then ASSERT().\r
1887 If String is NULL, then ASSERT().\r
1888 If PcdMaximumAsciiStringLength is not zero, \r
1889 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including \r
1890 the Null-terminator, then ASSERT().\r
1891\r
4df26661 1892 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1893\r
1894 @retval UINT64\r
1895\r
1896**/\r
1897UINT64\r
1898EFIAPI\r
1899AsciiStrHexToUint64 (\r
4df26661 1900 IN CONST CHAR8 *String\r
e1f414b6 1901 )\r
1902{\r
1903 UINT64 Result;\r
1904\r
4df26661 1905 //\r
1906 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1907 //\r
1908 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1909 \r
1910 //\r
1911 // Ignore the pad spaces (space or tab) and leading Zeros\r
1912 //\r
1913 //\r
1914 // Ignore the pad spaces (space or tab) \r
1915 //\r
1916 while ((*String == ' ') || (*String == '\t')) {\r
1917 String++;\r
1918 }\r
1919\r
1920 //\r
1921 // Ignore leading Zeros after the spaces\r
1922 //\r
1923 while (*String == '0') {\r
1924 String++;\r
1925 }\r
1926\r
1927 if (AsciiToUpper (*String) == 'X') {\r
1928 ASSERT (*(String - 1) == '0');\r
1929 if (*(String - 1) != '0') {\r
1930 return 0;\r
1931 }\r
1932 //\r
1933 // Skip the 'X'\r
1934 //\r
1935 String++;\r
1936 }\r
1937\r
1938 Result = 0;\r
1939 \r
1940 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1941 //\r
1942 // If the Hex Number represented by String overflows according \r
1943 // to the range defined by UINTN, then ASSERT().\r
1944 //\r
1945 ASSERT ((Result < QUIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
1946 ((Result == QUIENT_MAX_UINT64_DIVIDED_BY_16) && \r
1947 (InternalAsciiHexCharToUintn (*String) <= REMINDER_MAX_UINT64_DIVIDED_BY_16))\r
1948 );\r
1949\r
1950 Result = LShiftU64 (Result, 4);\r
1951 Result = Result + InternalAsciiHexCharToUintn (*String);\r
1952 String++;\r
1953 }\r
1954\r
1955 return Result;\r
1956}\r
1957\r
1958\r
1959/**\r
1960 Convert one Null-terminated ASCII string to a Null-terminated \r
1961 Unicode string and returns the Unicode string.\r
1962\r
1963 This function converts the contents of the ASCII string Source to the Unicode \r
1964 string Destination, and returns Destination. The function terminates the \r
1965 Unicode string Destination by appending a Null-terminator character at the end. \r
1966 The caller is responsible to make sure Destination points to a buffer with size \r
1967 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
1968 \r
1969 If Destination is NULL, then ASSERT().\r
1970 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
1971 If Source is NULL, then ASSERT().\r
1972 If Source and Destination overlap, then ASSERT().\r
1973 If PcdMaximumAsciiStringLength is not zero, and Source contains more than \r
1974 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator, \r
1975 then ASSERT().\r
1976 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than \r
1977 PcdMaximumUnicodeStringLength ASCII characters not including the \r
1978 Null-terminator, then ASSERT().\r
1979\r
1980 @param Source Pointer to a Null-terminated ASCII string.\r
1981 @param Destination Pointer to a Null-terminated Unicode string.\r
1982\r
1983 @reture Destination\r
1984\r
1985**/\r
1986CHAR16 *\r
1987EFIAPI\r
1988AsciiStrToUnicodeStr (\r
4df26661 1989 IN CONST CHAR8 *Source,\r
1990 OUT CHAR16 *Destination\r
e1f414b6 1991 )\r
1992{\r
4df26661 1993 CHAR16 *ReturnValue;\r
1994\r
e1f414b6 1995 ASSERT (Destination != NULL);\r
e1f414b6 1996\r
1997 //\r
4df26661 1998 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
e1f414b6 1999 //\r
4df26661 2000 ASSERT (AsciiStrSize (Source) != 0);\r
e1f414b6 2001\r
2002 //\r
4df26661 2003 // Source and Destination should not overlap\r
e1f414b6 2004 //\r
4df26661 2005 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
2006 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
e1f414b6 2007\r
4df26661 2008 \r
2009 ReturnValue = Destination;\r
e1f414b6 2010 while (*Source != '\0') {\r
2011 *(Destination++) = (CHAR16) *(Source++);\r
2012 }\r
2013 //\r
2014 // End the Destination with a NULL.\r
2015 //\r
2016 *Destination = '\0';\r
2017\r
4df26661 2018 //\r
2019 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
2020 //\r
2021 ASSERT (StrSize (ReturnValue) != 0);\r
2022\r
2023 return ReturnValue;\r
e1f414b6 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
2039UINT8\r
2040EFIAPI\r
2041DecimalToBcd8 (\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
2063UINT8\r
2064EFIAPI\r
2065BcdToDecimal8 (\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