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