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