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