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