]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/String.c
update string function comment to clear the semantic meaning.
[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
481 while (*String != '\0') {\r
482 SearchStringTmp = SearchString;\r
483 FirstMatch = String;\r
484 \r
485 while ((*String == *SearchStringTmp) \r
486 && (*SearchStringTmp != '\0') \r
487 && (*String != '\0')) {\r
488 String++;\r
489 SearchStringTmp++;\r
490 } \r
491 \r
955c32f2 492 if (*SearchStringTmp == '\0') {\r
e1f414b6 493 return (CHAR16 *) FirstMatch;\r
494 }\r
495\r
496 if (SearchStringTmp == SearchString) {\r
497 //\r
498 // If no character from SearchString match,\r
499 // move the pointer to the String under search\r
500 // by one character.\r
501 //\r
502 String++;\r
503 }\r
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
e1f414b6 1328AsciiToUpper (\r
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
1358 return (UINTN) (10 + AsciiToUpper (Char) - 'A');\r
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
1407 UpperFirstString = AsciiToUpper (*FirstString);\r
1408 UpperSecondString = AsciiToUpper (*SecondString);\r
1409 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
1410 FirstString++;\r
1411 SecondString++;\r
1412 UpperFirstString = AsciiToUpper (*FirstString);\r
1413 UpperSecondString = AsciiToUpper (*SecondString);\r
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
1619 while (*String != '\0') {\r
1620 SearchStringTmp = SearchString;\r
1621 FirstMatch = String;\r
1622 \r
1623 while ((*String == *SearchStringTmp) \r
1624 && (*SearchStringTmp != '\0') \r
1625 && (*String != '\0')) {\r
1626 String++;\r
1627 SearchStringTmp++;\r
1628 } \r
1629 \r
1630 if (*SearchStringTmp == '\0') {\r
1631 return (CHAR8 *) FirstMatch;\r
1632 }\r
1633\r
1634 if (SearchStringTmp == SearchString) {\r
1635 //\r
1636 // If no character from SearchString match,\r
1637 // move the pointer to the String under search\r
1638 // by one character.\r
1639 //\r
1640 String++;\r
1641 }\r
1642\r
1643 }\r
1644\r
1645 return NULL;\r
1646}\r
1647\r
1648/**\r
9aa049d9 1649 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1650 UINTN.\r
1651\r
9aa049d9 1652 This function returns a value of type UINTN by interpreting the contents\r
1653 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1654 ASCII string String is:\r
9aa049d9 1655\r
e1f414b6 1656 [spaces] [decimal digits].\r
9aa049d9 1657\r
1658 The valid decimal digit character is in the range [0-9]. The function will\r
1659 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1660 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1661 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1662 Null-terminator, whichever on comes first.\r
9aa049d9 1663\r
e1f414b6 1664 If String has only pad spaces, then 0 is returned.\r
1665 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1666 If the number represented by String overflows according to the range defined by\r
e1f414b6 1667 UINTN, then ASSERT().\r
1668 If String is NULL, then ASSERT().\r
9aa049d9 1669 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1670 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1671 then ASSERT().\r
1672\r
9aa049d9 1673 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1674\r
9aa049d9 1675 @retval Value translated from String.\r
e1f414b6 1676\r
1677**/\r
1678UINTN\r
1679EFIAPI\r
1680AsciiStrDecimalToUintn (\r
4df26661 1681 IN CONST CHAR8 *String\r
e1f414b6 1682 )\r
1683{\r
1684 UINTN Result;\r
1685 \r
4df26661 1686 //\r
1687 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1688 //\r
1689 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1690\r
1691 //\r
1692 // Ignore the pad spaces (space or tab)\r
1693 //\r
955c32f2 1694 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1695 String++;\r
1696 }\r
1697\r
1698 //\r
1699 // Ignore leading Zeros after the spaces\r
1700 //\r
955c32f2 1701 while (*String == '0') {\r
e1f414b6 1702 String++;\r
1703 }\r
1704\r
1705 Result = 0;\r
1706\r
1707 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1708 //\r
1709 // If the number represented by String overflows according \r
1710 // to the range defined by UINTN, then ASSERT().\r
1711 //\r
24dcb5e5 1712 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
955c32f2 1713 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) && \r
24dcb5e5 1714 (*String - '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
e1f414b6 1715 );\r
1716\r
1717 Result = Result * 10 + (*String - '0');\r
1718 String++;\r
1719 }\r
1720 \r
1721 return Result;\r
1722}\r
1723\r
1724\r
1725/**\r
9aa049d9 1726 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1727 UINT64.\r
1728\r
9aa049d9 1729 This function returns a value of type UINT64 by interpreting the contents\r
1730 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1731 ASCII string String is:\r
9aa049d9 1732\r
e1f414b6 1733 [spaces] [decimal digits].\r
9aa049d9 1734\r
1735 The valid decimal digit character is in the range [0-9]. The function will\r
1736 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1737 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1738 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1739 Null-terminator, whichever on comes first.\r
9aa049d9 1740\r
e1f414b6 1741 If String has only pad spaces, then 0 is returned.\r
1742 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1743 If the number represented by String overflows according to the range defined by\r
e1f414b6 1744 UINT64, then ASSERT().\r
1745 If String is NULL, then ASSERT().\r
9aa049d9 1746 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1747 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1748 then ASSERT().\r
1749\r
9aa049d9 1750 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1751\r
9aa049d9 1752 @retval Value translated from String.\r
e1f414b6 1753\r
1754**/\r
1755UINT64\r
1756EFIAPI\r
1757AsciiStrDecimalToUint64 (\r
2fc60b70 1758 IN CONST CHAR8 *String\r
e1f414b6 1759 )\r
1760{\r
1761 UINT64 Result;\r
1762 \r
4df26661 1763 //\r
1764 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1765 //\r
1766 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1767\r
1768 //\r
1769 // Ignore the pad spaces (space or tab)\r
1770 //\r
955c32f2 1771 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1772 String++;\r
1773 }\r
1774\r
1775 //\r
1776 // Ignore leading Zeros after the spaces\r
1777 //\r
955c32f2 1778 while (*String == '0') {\r
e1f414b6 1779 String++;\r
1780 }\r
1781\r
1782 Result = 0;\r
1783\r
1784 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1785 //\r
1786 // If the number represented by String overflows according \r
1787 // to the range defined by UINTN, then ASSERT().\r
1788 //\r
24dcb5e5 1789 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
955c32f2 1790 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
24dcb5e5 1791 (*String - '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
e1f414b6 1792 );\r
1793\r
1794 Result = MultU64x32 (Result, 10) + (*String - '0');\r
1795 String++;\r
1796 }\r
1797 \r
1798 return Result;\r
1799}\r
1800\r
1801/**\r
1802 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1803\r
9aa049d9 1804 This function returns a value of type UINTN by interpreting the contents of\r
1805 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1806 string String is:\r
9aa049d9 1807\r
e1f414b6 1808 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1809\r
1810 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1811 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1812 appears in the input string, it must be prefixed with at least one 0. The function\r
1813 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1814 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1815 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1816 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1817 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1818\r
e1f414b6 1819 If String has only pad spaces, then 0 is returned.\r
1820 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1821 0 is returned.\r
1822\r
9aa049d9 1823 If the number represented by String overflows according to the range defined by UINTN,\r
e1f414b6 1824 then ASSERT().\r
1825 If String is NULL, then ASSERT().\r
9aa049d9 1826 If PcdMaximumAsciiStringLength is not zero,\r
1827 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1828 the Null-terminator, then ASSERT().\r
1829\r
9aa049d9 1830 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1831\r
9aa049d9 1832 @retval Value translated from String.\r
e1f414b6 1833\r
1834**/\r
1835UINTN\r
1836EFIAPI\r
1837AsciiStrHexToUintn (\r
2fc60b70 1838 IN CONST CHAR8 *String\r
e1f414b6 1839 )\r
1840{\r
1841 UINTN Result;\r
1842\r
4df26661 1843 //\r
1844 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1845 //\r
1846 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1847 \r
1848 //\r
1849 // Ignore the pad spaces (space or tab) \r
1850 //\r
955c32f2 1851 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1852 String++;\r
1853 }\r
1854\r
1855 //\r
1856 // Ignore leading Zeros after the spaces\r
1857 //\r
955c32f2 1858 while (*String == '0') {\r
e1f414b6 1859 String++;\r
1860 }\r
1861\r
1862 if (AsciiToUpper (*String) == 'X') {\r
955c32f2 1863 ASSERT (*(String - 1) == '0');\r
1864 if (*(String - 1) != '0') {\r
e1f414b6 1865 return 0;\r
1866 }\r
1867 //\r
1868 // Skip the 'X'\r
1869 //\r
1870 String++;\r
1871 }\r
1872\r
1873 Result = 0;\r
1874 \r
1875 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1876 //\r
1877 // If the Hex Number represented by String overflows according \r
1878 // to the range defined by UINTN, then ASSERT().\r
1879 //\r
24dcb5e5 1880 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
955c32f2 1881 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
24dcb5e5 1882 (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
e1f414b6 1883 );\r
1884\r
1885 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
1886 String++;\r
1887 }\r
1888\r
1889 return Result;\r
1890}\r
1891\r
1892\r
1893/**\r
1894 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1895\r
9aa049d9 1896 This function returns a value of type UINT64 by interpreting the contents of\r
1897 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1898 string String is:\r
9aa049d9 1899\r
e1f414b6 1900 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1901\r
1902 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1903 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1904 appears in the input string, it must be prefixed with at least one 0. The function\r
1905 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1906 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1907 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1908 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1909 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1910\r
e1f414b6 1911 If String has only pad spaces, then 0 is returned.\r
1912 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1913 0 is returned.\r
1914\r
9aa049d9 1915 If the number represented by String overflows according to the range defined by UINT64,\r
e1f414b6 1916 then ASSERT().\r
1917 If String is NULL, then ASSERT().\r
9aa049d9 1918 If PcdMaximumAsciiStringLength is not zero,\r
1919 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1920 the Null-terminator, then ASSERT().\r
1921\r
9aa049d9 1922 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1923\r
9aa049d9 1924 @retval Value translated from String.\r
e1f414b6 1925\r
1926**/\r
1927UINT64\r
1928EFIAPI\r
1929AsciiStrHexToUint64 (\r
2fc60b70 1930 IN CONST CHAR8 *String\r
e1f414b6 1931 )\r
1932{\r
1933 UINT64 Result;\r
1934\r
4df26661 1935 //\r
1936 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1937 //\r
1938 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1939 \r
1940 //\r
1941 // Ignore the pad spaces (space or tab) and leading Zeros\r
1942 //\r
1943 //\r
1944 // Ignore the pad spaces (space or tab) \r
1945 //\r
955c32f2 1946 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1947 String++;\r
1948 }\r
1949\r
1950 //\r
1951 // Ignore leading Zeros after the spaces\r
1952 //\r
955c32f2 1953 while (*String == '0') {\r
e1f414b6 1954 String++;\r
1955 }\r
1956\r
1957 if (AsciiToUpper (*String) == 'X') {\r
955c32f2 1958 ASSERT (*(String - 1) == '0');\r
1959 if (*(String - 1) != '0') {\r
e1f414b6 1960 return 0;\r
1961 }\r
1962 //\r
1963 // Skip the 'X'\r
1964 //\r
1965 String++;\r
1966 }\r
1967\r
1968 Result = 0;\r
1969 \r
1970 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1971 //\r
1972 // If the Hex Number represented by String overflows according \r
1973 // to the range defined by UINTN, then ASSERT().\r
1974 //\r
24dcb5e5 1975 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
955c32f2 1976 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
24dcb5e5 1977 (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
e1f414b6 1978 );\r
1979\r
1980 Result = LShiftU64 (Result, 4);\r
1981 Result = Result + InternalAsciiHexCharToUintn (*String);\r
1982 String++;\r
1983 }\r
1984\r
1985 return Result;\r
1986}\r
1987\r
1988\r
1989/**\r
9aa049d9 1990 Convert one Null-terminated ASCII string to a Null-terminated\r
e1f414b6 1991 Unicode string and returns the Unicode string.\r
1992\r
9aa049d9 1993 This function converts the contents of the ASCII string Source to the Unicode\r
1994 string Destination, and returns Destination. The function terminates the\r
1995 Unicode string Destination by appending a Null-terminator character at the end.\r
1996 The caller is responsible to make sure Destination points to a buffer with size\r
e1f414b6 1997 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
9aa049d9 1998\r
e1f414b6 1999 If Destination is NULL, then ASSERT().\r
2000 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
2001 If Source is NULL, then ASSERT().\r
2002 If Source and Destination overlap, then ASSERT().\r
9aa049d9 2003 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
2004 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 2005 then ASSERT().\r
9aa049d9 2006 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
2007 PcdMaximumUnicodeStringLength ASCII characters not including the\r
e1f414b6 2008 Null-terminator, then ASSERT().\r
2009\r
2010 @param Source Pointer to a Null-terminated ASCII string.\r
2011 @param Destination Pointer to a Null-terminated Unicode string.\r
2012\r
9aa049d9 2013 @return Destination.\r
e1f414b6 2014\r
2015**/\r
2016CHAR16 *\r
2017EFIAPI\r
2018AsciiStrToUnicodeStr (\r
4df26661 2019 IN CONST CHAR8 *Source,\r
2020 OUT CHAR16 *Destination\r
e1f414b6 2021 )\r
2022{\r
4df26661 2023 CHAR16 *ReturnValue;\r
2024\r
e1f414b6 2025 ASSERT (Destination != NULL);\r
e1f414b6 2026\r
2027 //\r
4df26661 2028 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
e1f414b6 2029 //\r
4df26661 2030 ASSERT (AsciiStrSize (Source) != 0);\r
e1f414b6 2031\r
2032 //\r
4df26661 2033 // Source and Destination should not overlap\r
e1f414b6 2034 //\r
4df26661 2035 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
2036 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
e1f414b6 2037\r
4df26661 2038 \r
2039 ReturnValue = Destination;\r
e1f414b6 2040 while (*Source != '\0') {\r
2041 *(Destination++) = (CHAR16) *(Source++);\r
2042 }\r
2043 //\r
2044 // End the Destination with a NULL.\r
2045 //\r
2046 *Destination = '\0';\r
2047\r
4df26661 2048 //\r
2049 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
2050 //\r
2051 ASSERT (StrSize (ReturnValue) != 0);\r
2052\r
2053 return ReturnValue;\r
e1f414b6 2054}\r
2055\r
2056/**\r
2057 Converts an 8-bit value to an 8-bit BCD value.\r
2058\r
2059 Converts the 8-bit value specified by Value to BCD. The BCD value is\r
2060 returned.\r
2061\r
2062 If Value >= 100, then ASSERT().\r
2063\r
2064 @param Value The 8-bit value to convert to BCD. Range 0..99.\r
2065\r
9aa049d9 2066 @return The BCD value.\r
e1f414b6 2067\r
2068**/\r
2069UINT8\r
2070EFIAPI\r
2071DecimalToBcd8 (\r
2072 IN UINT8 Value\r
2073 )\r
2074{\r
2075 ASSERT (Value < 100);\r
2076 return (UINT8) (((Value / 10) << 4) | (Value % 10));\r
2077}\r
2078\r
2079/**\r
2080 Converts an 8-bit BCD value to an 8-bit value.\r
2081\r
2082 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
2083 value is returned.\r
2084\r
2085 If Value >= 0xA0, then ASSERT().\r
2086 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
2087\r
2088 @param Value The 8-bit BCD value to convert to an 8-bit value.\r
2089\r
9aa049d9 2090 @return The 8-bit value is returned.\r
e1f414b6 2091\r
2092**/\r
2093UINT8\r
2094EFIAPI\r
2095BcdToDecimal8 (\r
2096 IN UINT8 Value\r
2097 )\r
2098{\r
2099 ASSERT (Value < 0xa0);\r
2100 ASSERT ((Value & 0xf) < 0xa);\r
2101 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
2102}\r