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