]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseLib/String.c
MdePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
... / ...
CommitLineData
1/** @file\r
2 Unicode and ASCII string primitives.\r
3\r
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "BaseLibInternals.h"\r
16\r
17#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
18\r
19/**\r
20 [ATTENTION] This function will be deprecated for security reason.\r
21\r
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
35 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
36 Null-terminator, then ASSERT().\r
37\r
38 @param Destination A pointer to a Null-terminated Unicode string.\r
39 @param Source A pointer to a Null-terminated Unicode string.\r
40\r
41 @return Destination.\r
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
57 ASSERT (((UINTN) Destination & BIT0) == 0);\r
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
66 while (*Source != 0) {\r
67 *(Destination++) = *(Source++);\r
68 }\r
69 *Destination = 0;\r
70 return ReturnValue;\r
71}\r
72\r
73/**\r
74 [ATTENTION] This function will be deprecated for security reason.\r
75\r
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
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
90 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
91 If Source and Destination overlap, then ASSERT().\r
92 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
93 PcdMaximumUnicodeStringLength, then ASSERT().\r
94 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
95 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
96 then ASSERT().\r
97\r
98 @param Destination A pointer to a Null-terminated Unicode string.\r
99 @param Source A pointer to a Null-terminated Unicode string.\r
100 @param Length The maximum number of Unicode characters to copy.\r
101\r
102 @return Destination.\r
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
123 ASSERT (((UINTN) Destination & BIT0) == 0);\r
124\r
125 //\r
126 // Destination and source cannot overlap\r
127 //\r
128 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
129 ASSERT ((UINTN)(Source - Destination) >= Length);\r
130\r
131 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
132 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
133 }\r
134\r
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
145#endif\r
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
156 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
157 Null-terminator, then ASSERT().\r
158\r
159 @param String A pointer to a Null-terminated Unicode string.\r
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
173 ASSERT (((UINTN) String & BIT0) == 0);\r
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
191 This function returns the size, in bytes, of the Null-terminated Unicode string \r
192 specified by String.\r
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
197 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
198 Null-terminator, then ASSERT().\r
199\r
200 @param String A pointer to a Null-terminated Unicode string.\r
201\r
202 @return The size of String.\r
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
229 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
230 Null-terminator, then ASSERT().\r
231 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
232 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
233 Null-terminator, then ASSERT().\r
234\r
235 @param FirstString A pointer to a Null-terminated Unicode string.\r
236 @param SecondString A pointer to a Null-terminated Unicode string.\r
237\r
238 @retval 0 FirstString is identical to SecondString.\r
239 @return others FirstString is not identical to SecondString.\r
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
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
265 \r
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
274 If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
275 If Length > 0 and SecondString is NULL, then ASSERT().\r
276 If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
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
284 then ASSERT().\r
285\r
286 @param FirstString A pointer to a Null-terminated Unicode string.\r
287 @param SecondString A pointer to a Null-terminated Unicode string.\r
288 @param Length The maximum number of Unicode characters to compare.\r
289\r
290 @retval 0 FirstString is identical to SecondString.\r
291 @return others FirstString is not identical to SecondString.\r
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
302 if (Length == 0) {\r
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
313 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
314 ASSERT (Length <= PcdGet32 (PcdMaximumUnicodeStringLength));\r
315 }\r
316\r
317 while ((*FirstString != L'\0') &&\r
318 (*SecondString != L'\0') &&\r
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
329#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
330\r
331/**\r
332 [ATTENTION] This function will be deprecated for security reason.\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
344 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
345 If Source is NULL, then ASSERT().\r
346 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
347 If Source and Destination overlap, then ASSERT().\r
348 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
349 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
350 Null-terminator, then ASSERT().\r
351 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
352 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
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
356 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
357 Null-terminator, then ASSERT().\r
358\r
359 @param Destination A pointer to a Null-terminated Unicode string.\r
360 @param Source A pointer to a Null-terminated Unicode string.\r
361\r
362 @return Destination.\r
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
383 [ATTENTION] This function will be deprecated for security reason.\r
384\r
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
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
402 If PcdMaximumUnicodeStringLength is not zero, and Length is greater than \r
403 PcdMaximumUnicodeStringLength, then ASSERT().\r
404 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
405 than PcdMaximumUnicodeStringLength Unicode characters, not including the\r
406 Null-terminator, then ASSERT().\r
407 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
408 PcdMaximumUnicodeStringLength Unicode characters, not including the\r
409 Null-terminator, then ASSERT().\r
410 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
411 and Source results in a Unicode string with more than PcdMaximumUnicodeStringLength\r
412 Unicode characters, not including the Null-terminator, then ASSERT().\r
413\r
414 @param Destination A pointer to a Null-terminated Unicode string.\r
415 @param Source A pointer to a Null-terminated Unicode string.\r
416 @param Length The maximum number of Unicode characters to concatenate from\r
417 Source.\r
418\r
419 @return Destination.\r
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
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
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
443#endif\r
444\r
445/**\r
446 Returns the first occurrence of a Null-terminated Unicode sub-string\r
447 in a Null-terminated Unicode string.\r
448\r
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
453 returned.\r
454\r
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
460 If PcdMaximumUnicodeStringLength is not zero, and SearchString\r
461 or String contains more than PcdMaximumUnicodeStringLength Unicode\r
462 characters, not including the Null-terminator, then ASSERT().\r
463\r
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
466\r
467 @retval NULL If the SearchString does not appear in String.\r
468 @return others If there is a match.\r
469\r
470**/\r
471CHAR16 *\r
472EFIAPI\r
473StrStr (\r
474 IN CONST CHAR16 *String,\r
475 IN CONST CHAR16 *SearchString\r
476 )\r
477{\r
478 CONST CHAR16 *FirstMatch;\r
479 CONST CHAR16 *SearchStringTmp;\r
480\r
481 //\r
482 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
483 // Length tests are performed inside StrLen().\r
484 //\r
485 ASSERT (StrSize (String) != 0);\r
486 ASSERT (StrSize (SearchString) != 0);\r
487\r
488 if (*SearchString == L'\0') {\r
489 return (CHAR16 *) String;\r
490 }\r
491\r
492 while (*String != L'\0') {\r
493 SearchStringTmp = SearchString;\r
494 FirstMatch = String;\r
495 \r
496 while ((*String == *SearchStringTmp) \r
497 && (*String != L'\0')) {\r
498 String++;\r
499 SearchStringTmp++;\r
500 } \r
501 \r
502 if (*SearchStringTmp == L'\0') {\r
503 return (CHAR16 *) FirstMatch;\r
504 }\r
505\r
506 if (*String == L'\0') {\r
507 return NULL;\r
508 }\r
509\r
510 String = FirstMatch + 1;\r
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
523 @param Char The character to check against.\r
524\r
525 @retval TRUE If the Char is a decmial character.\r
526 @retval FALSE If the Char is not a decmial character.\r
527\r
528**/\r
529BOOLEAN\r
530EFIAPI\r
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
543 which maps to a valid small-case ASCII character, i.e.\r
544 L'a' to L'z'. For other Unicode character, the input character\r
545 is returned directly.\r
546\r
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
553CHAR16\r
554EFIAPI\r
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
576 @return The numerical value converted.\r
577\r
578**/\r
579UINTN\r
580EFIAPI\r
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 (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
603 @retval FALSE If the Char is not a hexadecmial character.\r
604\r
605**/\r
606BOOLEAN\r
607EFIAPI\r
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
619 Convert a Null-terminated Unicode decimal string to a value of\r
620 type UINTN.\r
621\r
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
624 of the input Unicode string String is:\r
625\r
626 [spaces] [decimal digits].\r
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
635 If String is NULL, then ASSERT().\r
636 If String is not aligned in a 16-bit boundary, then ASSERT().\r
637 If String has only pad spaces, then 0 is returned.\r
638 If String has no pad spaces or valid decimal digits,\r
639 then 0 is returned.\r
640 If the number represented by String overflows according\r
641 to the range defined by UINTN, then MAX_UINTN is returned.\r
642\r
643 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
644 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
645 the Null-terminator, then ASSERT().\r
646\r
647 @param String A pointer to a Null-terminated Unicode string.\r
648\r
649 @retval Value translated from String.\r
650\r
651**/\r
652UINTN\r
653EFIAPI\r
654StrDecimalToUintn (\r
655 IN CONST CHAR16 *String\r
656 )\r
657{\r
658 UINTN Result;\r
659\r
660 StrDecimalToUintnS (String, (CHAR16 **) NULL, &Result);\r
661 return Result;\r
662}\r
663\r
664\r
665/**\r
666 Convert a Null-terminated Unicode decimal string to a value of\r
667 type UINT64.\r
668\r
669 This function returns a value of type UINT64 by interpreting the contents\r
670 of the Unicode string specified by String as a decimal number. The format\r
671 of the input Unicode string String is:\r
672\r
673 [spaces] [decimal digits].\r
674\r
675 The valid decimal digit character is in the range [0-9]. The\r
676 function will ignore the pad space, which includes spaces or\r
677 tab characters, before [decimal digits]. The running zero in the\r
678 beginning of [decimal digits] will be ignored. Then, the function\r
679 stops at the first character that is a not a valid decimal character\r
680 or a Null-terminator, whichever one comes first.\r
681\r
682 If String is NULL, then ASSERT().\r
683 If String is not aligned in a 16-bit boundary, then ASSERT().\r
684 If String has only pad spaces, then 0 is returned.\r
685 If String has no pad spaces or valid decimal digits,\r
686 then 0 is returned.\r
687 If the number represented by String overflows according\r
688 to the range defined by UINT64, then MAX_UINT64 is returned.\r
689\r
690 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
691 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
692 the Null-terminator, then ASSERT().\r
693\r
694 @param String A pointer to a Null-terminated Unicode string.\r
695\r
696 @retval Value translated from String.\r
697\r
698**/\r
699UINT64\r
700EFIAPI\r
701StrDecimalToUint64 (\r
702 IN CONST CHAR16 *String\r
703 )\r
704{\r
705 UINT64 Result;\r
706 \r
707 StrDecimalToUint64S (String, (CHAR16 **) NULL, &Result);\r
708 return Result;\r
709}\r
710\r
711/**\r
712 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.\r
713\r
714 This function returns a value of type UINTN by interpreting the contents\r
715 of the Unicode string specified by String as a hexadecimal number.\r
716 The format of the input Unicode string String is:\r
717\r
718 [spaces][zeros][x][hexadecimal digits].\r
719\r
720 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
721 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
722 If "x" appears in the input string, it must be prefixed with at least one 0.\r
723 The function will ignore the pad space, which includes spaces or tab characters,\r
724 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
725 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
726 first valid hexadecimal digit. Then, the function stops at the first character that is\r
727 a not a valid hexadecimal character or NULL, whichever one comes first.\r
728\r
729 If String is NULL, then ASSERT().\r
730 If String is not aligned in a 16-bit boundary, then ASSERT().\r
731 If String has only pad spaces, then zero is returned.\r
732 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
733 then zero is returned.\r
734 If the number represented by String overflows according to the range defined by\r
735 UINTN, then MAX_UINTN is returned.\r
736\r
737 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
738 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
739 then ASSERT().\r
740\r
741 @param String A pointer to a Null-terminated Unicode string.\r
742\r
743 @retval Value translated from String.\r
744\r
745**/\r
746UINTN\r
747EFIAPI\r
748StrHexToUintn (\r
749 IN CONST CHAR16 *String\r
750 )\r
751{\r
752 UINTN Result;\r
753\r
754 StrHexToUintnS (String, (CHAR16 **) NULL, &Result);\r
755 return Result;\r
756}\r
757\r
758\r
759/**\r
760 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
761\r
762 This function returns a value of type UINT64 by interpreting the contents\r
763 of the Unicode string specified by String as a hexadecimal number.\r
764 The format of the input Unicode string String is\r
765\r
766 [spaces][zeros][x][hexadecimal digits].\r
767\r
768 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
769 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
770 If "x" appears in the input string, it must be prefixed with at least one 0.\r
771 The function will ignore the pad space, which includes spaces or tab characters,\r
772 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
773 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
774 first valid hexadecimal digit. Then, the function stops at the first character that is\r
775 a not a valid hexadecimal character or NULL, whichever one comes first.\r
776\r
777 If String is NULL, then ASSERT().\r
778 If String is not aligned in a 16-bit boundary, then ASSERT().\r
779 If String has only pad spaces, then zero is returned.\r
780 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
781 then zero is returned.\r
782 If the number represented by String overflows according to the range defined by\r
783 UINT64, then MAX_UINT64 is returned.\r
784\r
785 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
786 PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,\r
787 then ASSERT().\r
788\r
789 @param String A pointer to a Null-terminated Unicode string.\r
790\r
791 @retval Value translated from String.\r
792\r
793**/\r
794UINT64\r
795EFIAPI\r
796StrHexToUint64 (\r
797 IN CONST CHAR16 *String\r
798 )\r
799{\r
800 UINT64 Result;\r
801\r
802 StrHexToUint64S (String, (CHAR16 **) NULL, &Result);\r
803 return Result;\r
804}\r
805\r
806/**\r
807 Check if a ASCII character is a decimal character.\r
808\r
809 This internal function checks if a Unicode character is a \r
810 decimal character. The valid decimal character is from\r
811 '0' to '9'.\r
812\r
813 @param Char The character to check against.\r
814\r
815 @retval TRUE If the Char is a decmial character.\r
816 @retval FALSE If the Char is not a decmial character.\r
817\r
818**/\r
819BOOLEAN\r
820EFIAPI\r
821InternalAsciiIsDecimalDigitCharacter (\r
822 IN CHAR8 Char\r
823 )\r
824{\r
825 return (BOOLEAN) (Char >= '0' && Char <= '9');\r
826}\r
827\r
828/**\r
829 Check if a ASCII character is a hexadecimal character.\r
830\r
831 This internal function checks if a ASCII character is a \r
832 decimal character. The valid hexadecimal character is \r
833 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
834\r
835\r
836 @param Char The character to check against.\r
837\r
838 @retval TRUE If the Char is a hexadecmial character.\r
839 @retval FALSE If the Char is not a hexadecmial character.\r
840\r
841**/\r
842BOOLEAN\r
843EFIAPI\r
844InternalAsciiIsHexaDecimalDigitCharacter (\r
845 IN CHAR8 Char\r
846 )\r
847{\r
848\r
849 return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||\r
850 (Char >= 'A' && Char <= 'F') ||\r
851 (Char >= 'a' && Char <= 'f'));\r
852}\r
853\r
854#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
855\r
856/**\r
857 [ATTENTION] This function is deprecated for security reason.\r
858\r
859 Convert a Null-terminated Unicode string to a Null-terminated\r
860 ASCII string and returns the ASCII string.\r
861\r
862 This function converts the content of the Unicode string Source\r
863 to the ASCII string Destination by copying the lower 8 bits of\r
864 each Unicode character. It returns Destination.\r
865\r
866 The caller is responsible to make sure Destination points to a buffer with size\r
867 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.\r
868\r
869 If any Unicode characters in Source contain non-zero value in\r
870 the upper 8 bits, then ASSERT().\r
871\r
872 If Destination is NULL, then ASSERT().\r
873 If Source is NULL, then ASSERT().\r
874 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
875 If Source and Destination overlap, then ASSERT().\r
876\r
877 If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
878 more than PcdMaximumUnicodeStringLength Unicode characters, not including\r
879 the Null-terminator, then ASSERT().\r
880\r
881 If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
882 than PcdMaximumAsciiStringLength Unicode characters, not including the\r
883 Null-terminator, then ASSERT().\r
884\r
885 @param Source A pointer to a Null-terminated Unicode string.\r
886 @param Destination A pointer to a Null-terminated ASCII string.\r
887\r
888 @return Destination.\r
889\r
890**/\r
891CHAR8 *\r
892EFIAPI\r
893UnicodeStrToAsciiStr (\r
894 IN CONST CHAR16 *Source,\r
895 OUT CHAR8 *Destination\r
896 )\r
897{\r
898 CHAR8 *ReturnValue;\r
899\r
900 ASSERT (Destination != NULL);\r
901\r
902 //\r
903 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
904 // Length tests are performed inside StrLen().\r
905 //\r
906 ASSERT (StrSize (Source) != 0);\r
907\r
908 //\r
909 // Source and Destination should not overlap\r
910 //\r
911 ASSERT ((UINTN) (Destination - (CHAR8 *) Source) >= StrSize (Source));\r
912 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
913\r
914\r
915 ReturnValue = Destination;\r
916 while (*Source != '\0') {\r
917 //\r
918 // If any Unicode characters in Source contain \r
919 // non-zero value in the upper 8 bits, then ASSERT().\r
920 //\r
921 ASSERT (*Source < 0x100);\r
922 *(Destination++) = (CHAR8) *(Source++);\r
923 }\r
924\r
925 *Destination = '\0';\r
926\r
927 //\r
928 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
929 // Length tests are performed inside AsciiStrLen().\r
930 //\r
931 ASSERT (AsciiStrSize (ReturnValue) != 0);\r
932\r
933 return ReturnValue;\r
934}\r
935\r
936/**\r
937 [ATTENTION] This function will be deprecated for security reason.\r
938\r
939 Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
940 string and returns the new ASCII string.\r
941\r
942 This function copies the contents of the ASCII string Source to the ASCII\r
943 string Destination, and returns Destination. If Source and Destination\r
944 overlap, then the results are undefined.\r
945\r
946 If Destination is NULL, then ASSERT().\r
947 If Source is NULL, then ASSERT().\r
948 If Source and Destination overlap, then ASSERT().\r
949 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
950 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
951 then ASSERT().\r
952\r
953 @param Destination A pointer to a Null-terminated ASCII string.\r
954 @param Source A pointer to a Null-terminated ASCII string.\r
955\r
956 @return Destination\r
957\r
958**/\r
959CHAR8 *\r
960EFIAPI\r
961AsciiStrCpy (\r
962 OUT CHAR8 *Destination,\r
963 IN CONST CHAR8 *Source\r
964 )\r
965{\r
966 CHAR8 *ReturnValue;\r
967\r
968 //\r
969 // Destination cannot be NULL\r
970 //\r
971 ASSERT (Destination != NULL);\r
972\r
973 //\r
974 // Destination and source cannot overlap\r
975 //\r
976 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
977 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r
978\r
979 ReturnValue = Destination;\r
980 while (*Source != 0) {\r
981 *(Destination++) = *(Source++);\r
982 }\r
983 *Destination = 0;\r
984 return ReturnValue;\r
985}\r
986\r
987/**\r
988 [ATTENTION] This function will be deprecated for security reason.\r
989\r
990 Copies up to a specified length one Null-terminated ASCII string to another \r
991 Null-terminated ASCII string and returns the new ASCII string.\r
992\r
993 This function copies the contents of the ASCII string Source to the ASCII\r
994 string Destination, and returns Destination. At most, Length ASCII characters\r
995 are copied from Source to Destination. If Length is 0, then Destination is\r
996 returned unmodified. If Length is greater that the number of ASCII characters\r
997 in Source, then Destination is padded with Null ASCII characters. If Source\r
998 and Destination overlap, then the results are undefined.\r
999\r
1000 If Destination is NULL, then ASSERT().\r
1001 If Source is NULL, then ASSERT().\r
1002 If Source and Destination overlap, then ASSERT().\r
1003 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1004 PcdMaximumAsciiStringLength, then ASSERT().\r
1005 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1006 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1007 then ASSERT().\r
1008\r
1009 @param Destination A pointer to a Null-terminated ASCII string.\r
1010 @param Source A pointer to a Null-terminated ASCII string.\r
1011 @param Length The maximum number of ASCII characters to copy.\r
1012\r
1013 @return Destination\r
1014\r
1015**/\r
1016CHAR8 *\r
1017EFIAPI\r
1018AsciiStrnCpy (\r
1019 OUT CHAR8 *Destination,\r
1020 IN CONST CHAR8 *Source,\r
1021 IN UINTN Length\r
1022 )\r
1023{\r
1024 CHAR8 *ReturnValue;\r
1025\r
1026 if (Length == 0) {\r
1027 return Destination;\r
1028 }\r
1029\r
1030 //\r
1031 // Destination cannot be NULL\r
1032 //\r
1033 ASSERT (Destination != NULL);\r
1034\r
1035 //\r
1036 // Destination and source cannot overlap\r
1037 //\r
1038 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1039 ASSERT ((UINTN)(Source - Destination) >= Length);\r
1040\r
1041 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1042 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1043 }\r
1044\r
1045 ReturnValue = Destination;\r
1046\r
1047 while (*Source != 0 && Length > 0) {\r
1048 *(Destination++) = *(Source++);\r
1049 Length--;\r
1050 }\r
1051\r
1052 ZeroMem (Destination, Length * sizeof (*Destination));\r
1053 return ReturnValue;\r
1054}\r
1055#endif\r
1056\r
1057/**\r
1058 Returns the length of a Null-terminated ASCII string.\r
1059\r
1060 This function returns the number of ASCII characters in the Null-terminated\r
1061 ASCII string specified by String.\r
1062\r
1063 If Length > 0 and Destination is NULL, then ASSERT().\r
1064 If Length > 0 and Source is NULL, then ASSERT().\r
1065 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
1066 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1067 then ASSERT().\r
1068\r
1069 @param String A pointer to a Null-terminated ASCII string.\r
1070\r
1071 @return The length of String.\r
1072\r
1073**/\r
1074UINTN\r
1075EFIAPI\r
1076AsciiStrLen (\r
1077 IN CONST CHAR8 *String\r
1078 )\r
1079{\r
1080 UINTN Length;\r
1081\r
1082 ASSERT (String != NULL);\r
1083\r
1084 for (Length = 0; *String != '\0'; String++, Length++) {\r
1085 //\r
1086 // If PcdMaximumUnicodeStringLength is not zero,\r
1087 // length should not more than PcdMaximumUnicodeStringLength\r
1088 //\r
1089 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1090 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r
1091 }\r
1092 }\r
1093 return Length;\r
1094}\r
1095\r
1096/**\r
1097 Returns the size of a Null-terminated ASCII string in bytes, including the\r
1098 Null terminator.\r
1099\r
1100 This function returns the size, in bytes, of the Null-terminated ASCII string\r
1101 specified by String.\r
1102\r
1103 If String is NULL, then ASSERT().\r
1104 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
1105 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1106 then ASSERT().\r
1107\r
1108 @param String A pointer to a Null-terminated ASCII string.\r
1109\r
1110 @return The size of String.\r
1111\r
1112**/\r
1113UINTN\r
1114EFIAPI\r
1115AsciiStrSize (\r
1116 IN CONST CHAR8 *String\r
1117 )\r
1118{\r
1119 return (AsciiStrLen (String) + 1) * sizeof (*String);\r
1120}\r
1121\r
1122/**\r
1123 Compares two Null-terminated ASCII strings, and returns the difference\r
1124 between the first mismatched ASCII characters.\r
1125\r
1126 This function compares the Null-terminated ASCII string FirstString to the\r
1127 Null-terminated ASCII string SecondString. If FirstString is identical to\r
1128 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
1129 mismatched ASCII character in SecondString subtracted from the first\r
1130 mismatched ASCII character in FirstString.\r
1131\r
1132 If FirstString is NULL, then ASSERT().\r
1133 If SecondString is NULL, then ASSERT().\r
1134 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
1135 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1136 then ASSERT().\r
1137 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
1138 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
1139 Null-terminator, then ASSERT().\r
1140\r
1141 @param FirstString A pointer to a Null-terminated ASCII string.\r
1142 @param SecondString A pointer to a Null-terminated ASCII string.\r
1143\r
1144 @retval ==0 FirstString is identical to SecondString.\r
1145 @retval !=0 FirstString is not identical to SecondString.\r
1146\r
1147**/\r
1148INTN\r
1149EFIAPI\r
1150AsciiStrCmp (\r
1151 IN CONST CHAR8 *FirstString,\r
1152 IN CONST CHAR8 *SecondString\r
1153 )\r
1154{\r
1155 //\r
1156 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1157 //\r
1158 ASSERT (AsciiStrSize (FirstString));\r
1159 ASSERT (AsciiStrSize (SecondString));\r
1160\r
1161 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r
1162 FirstString++;\r
1163 SecondString++;\r
1164 }\r
1165\r
1166 return *FirstString - *SecondString;\r
1167}\r
1168\r
1169/**\r
1170 Converts a lowercase Ascii character to upper one.\r
1171\r
1172 If Chr is lowercase Ascii character, then converts it to upper one.\r
1173\r
1174 If Value >= 0xA0, then ASSERT().\r
1175 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
1176\r
1177 @param Chr one Ascii character\r
1178\r
1179 @return The uppercase value of Ascii character \r
1180\r
1181**/\r
1182CHAR8\r
1183EFIAPI\r
1184InternalBaseLibAsciiToUpper (\r
1185 IN CHAR8 Chr\r
1186 )\r
1187{\r
1188 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);\r
1189}\r
1190\r
1191/**\r
1192 Convert a ASCII character to numerical value.\r
1193\r
1194 This internal function only deal with Unicode character\r
1195 which maps to a valid hexadecimal ASII character, i.e.\r
1196 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r
1197 ASCII character, the value returned does not make sense.\r
1198\r
1199 @param Char The character to convert.\r
1200\r
1201 @return The numerical value converted.\r
1202\r
1203**/\r
1204UINTN\r
1205EFIAPI\r
1206InternalAsciiHexCharToUintn (\r
1207 IN CHAR8 Char\r
1208 )\r
1209{\r
1210 if (InternalIsDecimalDigitCharacter (Char)) {\r
1211 return Char - '0';\r
1212 }\r
1213\r
1214 return (10 + InternalBaseLibAsciiToUpper (Char) - 'A');\r
1215}\r
1216\r
1217\r
1218/**\r
1219 Performs a case insensitive comparison of two Null-terminated ASCII strings,\r
1220 and returns the difference between the first mismatched ASCII characters.\r
1221\r
1222 This function performs a case insensitive comparison of the Null-terminated\r
1223 ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r
1224 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
1225 value returned is the first mismatched lower case ASCII character in\r
1226 SecondString subtracted from the first mismatched lower case ASCII character\r
1227 in FirstString.\r
1228\r
1229 If FirstString is NULL, then ASSERT().\r
1230 If SecondString is NULL, then ASSERT().\r
1231 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
1232 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1233 then ASSERT().\r
1234 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
1235 than PcdMaximumAsciiStringLength ASCII characters, not including the\r
1236 Null-terminator, then ASSERT().\r
1237\r
1238 @param FirstString A pointer to a Null-terminated ASCII string.\r
1239 @param SecondString A pointer to a Null-terminated ASCII string.\r
1240\r
1241 @retval ==0 FirstString is identical to SecondString using case insensitive\r
1242 comparisons.\r
1243 @retval !=0 FirstString is not identical to SecondString using case\r
1244 insensitive comparisons.\r
1245\r
1246**/\r
1247INTN\r
1248EFIAPI\r
1249AsciiStriCmp (\r
1250 IN CONST CHAR8 *FirstString,\r
1251 IN CONST CHAR8 *SecondString\r
1252 )\r
1253{\r
1254 CHAR8 UpperFirstString;\r
1255 CHAR8 UpperSecondString;\r
1256\r
1257 //\r
1258 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1259 //\r
1260 ASSERT (AsciiStrSize (FirstString));\r
1261 ASSERT (AsciiStrSize (SecondString));\r
1262\r
1263 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1264 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
1265 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
1266 FirstString++;\r
1267 SecondString++;\r
1268 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);\r
1269 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);\r
1270 }\r
1271\r
1272 return UpperFirstString - UpperSecondString;\r
1273}\r
1274\r
1275/**\r
1276 Compares two Null-terminated ASCII strings with maximum lengths, and returns\r
1277 the difference between the first mismatched ASCII characters.\r
1278\r
1279 This function compares the Null-terminated ASCII string FirstString to the\r
1280 Null-terminated ASCII string SecondString. At most, Length ASCII characters\r
1281 will be compared. If Length is 0, then 0 is returned. If FirstString is\r
1282 identical to SecondString, then 0 is returned. Otherwise, the value returned\r
1283 is the first mismatched ASCII character in SecondString subtracted from the\r
1284 first mismatched ASCII character in FirstString.\r
1285\r
1286 If Length > 0 and FirstString is NULL, then ASSERT().\r
1287 If Length > 0 and SecondString is NULL, then ASSERT().\r
1288 If PcdMaximumAsciiStringLength is not zero, and Length is greater than \r
1289 PcdMaximumAsciiStringLength, then ASSERT().\r
1290 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than\r
1291 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1292 then ASSERT().\r
1293 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than\r
1294 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1295 then ASSERT().\r
1296\r
1297 @param FirstString A pointer to a Null-terminated ASCII string.\r
1298 @param SecondString A pointer to a Null-terminated ASCII string.\r
1299 @param Length The maximum number of ASCII characters for compare.\r
1300 \r
1301 @retval ==0 FirstString is identical to SecondString.\r
1302 @retval !=0 FirstString is not identical to SecondString.\r
1303\r
1304**/\r
1305INTN\r
1306EFIAPI\r
1307AsciiStrnCmp (\r
1308 IN CONST CHAR8 *FirstString,\r
1309 IN CONST CHAR8 *SecondString,\r
1310 IN UINTN Length\r
1311 )\r
1312{\r
1313 if (Length == 0) {\r
1314 return 0;\r
1315 }\r
1316\r
1317 //\r
1318 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1319 //\r
1320 ASSERT (AsciiStrSize (FirstString));\r
1321 ASSERT (AsciiStrSize (SecondString));\r
1322\r
1323 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1324 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));\r
1325 }\r
1326\r
1327 while ((*FirstString != '\0') &&\r
1328 (*SecondString != '\0') &&\r
1329 (*FirstString == *SecondString) &&\r
1330 (Length > 1)) {\r
1331 FirstString++;\r
1332 SecondString++;\r
1333 Length--;\r
1334 }\r
1335 return *FirstString - *SecondString;\r
1336}\r
1337\r
1338#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1339\r
1340/**\r
1341 [ATTENTION] This function will be deprecated for security reason.\r
1342\r
1343 Concatenates one Null-terminated ASCII string to another Null-terminated\r
1344 ASCII string, and returns the concatenated ASCII string.\r
1345\r
1346 This function concatenates two Null-terminated ASCII strings. The contents of\r
1347 Null-terminated ASCII string Source are concatenated to the end of Null-\r
1348 terminated ASCII string Destination. The Null-terminated concatenated ASCII\r
1349 String is returned.\r
1350\r
1351 If Destination is NULL, then ASSERT().\r
1352 If Source is NULL, then ASSERT().\r
1353 If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
1354 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1355 then ASSERT().\r
1356 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
1357 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1358 then ASSERT().\r
1359 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
1360 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1361 ASCII characters, then ASSERT().\r
1362\r
1363 @param Destination A pointer to a Null-terminated ASCII string.\r
1364 @param Source A pointer to a Null-terminated ASCII string.\r
1365\r
1366 @return Destination\r
1367\r
1368**/\r
1369CHAR8 *\r
1370EFIAPI\r
1371AsciiStrCat (\r
1372 IN OUT CHAR8 *Destination,\r
1373 IN CONST CHAR8 *Source\r
1374 )\r
1375{\r
1376 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r
1377\r
1378 //\r
1379 // Size of the resulting string should never be zero.\r
1380 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1381 //\r
1382 ASSERT (AsciiStrSize (Destination) != 0);\r
1383 return Destination;\r
1384}\r
1385\r
1386/**\r
1387 [ATTENTION] This function will be deprecated for security reason.\r
1388\r
1389 Concatenates up to a specified length one Null-terminated ASCII string to \r
1390 the end of another Null-terminated ASCII string, and returns the \r
1391 concatenated ASCII string.\r
1392\r
1393 This function concatenates two Null-terminated ASCII strings. The contents\r
1394 of Null-terminated ASCII string Source are concatenated to the end of Null-\r
1395 terminated ASCII string Destination, and Destination is returned. At most,\r
1396 Length ASCII characters are concatenated from Source to the end of\r
1397 Destination, and Destination is always Null-terminated. If Length is 0, then\r
1398 Destination is returned unmodified. If Source and Destination overlap, then\r
1399 the results are undefined.\r
1400\r
1401 If Length > 0 and Destination is NULL, then ASSERT().\r
1402 If Length > 0 and Source is NULL, then ASSERT().\r
1403 If Source and Destination overlap, then ASSERT().\r
1404 If PcdMaximumAsciiStringLength is not zero, and Length is greater than\r
1405 PcdMaximumAsciiStringLength, then ASSERT().\r
1406 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
1407 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1408 then ASSERT().\r
1409 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1410 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,\r
1411 then ASSERT().\r
1412 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
1413 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1414 ASCII characters, not including the Null-terminator, then ASSERT().\r
1415\r
1416 @param Destination A pointer to a Null-terminated ASCII string.\r
1417 @param Source A pointer to a Null-terminated ASCII string.\r
1418 @param Length The maximum number of ASCII characters to concatenate from\r
1419 Source.\r
1420\r
1421 @return Destination\r
1422\r
1423**/\r
1424CHAR8 *\r
1425EFIAPI\r
1426AsciiStrnCat (\r
1427 IN OUT CHAR8 *Destination,\r
1428 IN CONST CHAR8 *Source,\r
1429 IN UINTN Length\r
1430 )\r
1431{\r
1432 UINTN DestinationLen;\r
1433\r
1434 DestinationLen = AsciiStrLen (Destination);\r
1435 AsciiStrnCpy (Destination + DestinationLen, Source, Length);\r
1436 Destination[DestinationLen + Length] = '\0';\r
1437\r
1438 //\r
1439 // Size of the resulting string should never be zero.\r
1440 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1441 //\r
1442 ASSERT (AsciiStrSize (Destination) != 0);\r
1443 return Destination;\r
1444}\r
1445#endif\r
1446\r
1447/**\r
1448 Returns the first occurrence of a Null-terminated ASCII sub-string\r
1449 in a Null-terminated ASCII string.\r
1450\r
1451 This function scans the contents of the ASCII string specified by String\r
1452 and returns the first occurrence of SearchString. If SearchString is not\r
1453 found in String, then NULL is returned. If the length of SearchString is zero,\r
1454 then String is returned.\r
1455\r
1456 If String is NULL, then ASSERT().\r
1457 If SearchString is NULL, then ASSERT().\r
1458\r
1459 If PcdMaximumAsciiStringLength is not zero, and SearchString or\r
1460 String contains more than PcdMaximumAsciiStringLength Unicode characters\r
1461 not including the Null-terminator, then ASSERT().\r
1462\r
1463 @param String A pointer to a Null-terminated ASCII string.\r
1464 @param SearchString A pointer to a Null-terminated ASCII string to search for.\r
1465\r
1466 @retval NULL If the SearchString does not appear in String.\r
1467 @retval others If there is a match return the first occurrence of SearchingString.\r
1468 If the length of SearchString is zero,return String.\r
1469\r
1470**/\r
1471CHAR8 *\r
1472EFIAPI\r
1473AsciiStrStr (\r
1474 IN CONST CHAR8 *String,\r
1475 IN CONST CHAR8 *SearchString\r
1476 )\r
1477{\r
1478 CONST CHAR8 *FirstMatch;\r
1479 CONST CHAR8 *SearchStringTmp;\r
1480\r
1481 //\r
1482 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1483 //\r
1484 ASSERT (AsciiStrSize (String) != 0);\r
1485 ASSERT (AsciiStrSize (SearchString) != 0);\r
1486\r
1487 if (*SearchString == '\0') {\r
1488 return (CHAR8 *) String;\r
1489 }\r
1490\r
1491 while (*String != '\0') {\r
1492 SearchStringTmp = SearchString;\r
1493 FirstMatch = String;\r
1494 \r
1495 while ((*String == *SearchStringTmp) \r
1496 && (*String != '\0')) {\r
1497 String++;\r
1498 SearchStringTmp++;\r
1499 } \r
1500 \r
1501 if (*SearchStringTmp == '\0') {\r
1502 return (CHAR8 *) FirstMatch;\r
1503 }\r
1504\r
1505 if (*String == '\0') {\r
1506 return NULL;\r
1507 }\r
1508\r
1509 String = FirstMatch + 1;\r
1510 }\r
1511\r
1512 return NULL;\r
1513}\r
1514\r
1515/**\r
1516 Convert a Null-terminated ASCII decimal string to a value of type\r
1517 UINTN.\r
1518\r
1519 This function returns a value of type UINTN by interpreting the contents\r
1520 of the ASCII string String as a decimal number. The format of the input\r
1521 ASCII string String is:\r
1522\r
1523 [spaces] [decimal digits].\r
1524\r
1525 The valid decimal digit character is in the range [0-9]. The function will\r
1526 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1527 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1528 function stops at the first character that is a not a valid decimal character or\r
1529 Null-terminator, whichever on comes first.\r
1530\r
1531 If String has only pad spaces, then 0 is returned.\r
1532 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
1533 If the number represented by String overflows according to the range defined by\r
1534 UINTN, then MAX_UINTN is returned.\r
1535 If String is NULL, then ASSERT().\r
1536 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1537 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1538 then ASSERT().\r
1539\r
1540 @param String A pointer to a Null-terminated ASCII string.\r
1541\r
1542 @retval Value translated from String.\r
1543\r
1544**/\r
1545UINTN\r
1546EFIAPI\r
1547AsciiStrDecimalToUintn (\r
1548 IN CONST CHAR8 *String\r
1549 )\r
1550{\r
1551 UINTN Result;\r
1552 \r
1553 AsciiStrDecimalToUintnS (String, (CHAR8 **) NULL, &Result);\r
1554 return Result;\r
1555}\r
1556\r
1557\r
1558/**\r
1559 Convert a Null-terminated ASCII decimal string to a value of type\r
1560 UINT64.\r
1561\r
1562 This function returns a value of type UINT64 by interpreting the contents\r
1563 of the ASCII string String as a decimal number. The format of the input\r
1564 ASCII string String is:\r
1565\r
1566 [spaces] [decimal digits].\r
1567\r
1568 The valid decimal digit character is in the range [0-9]. The function will\r
1569 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1570 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1571 function stops at the first character that is a not a valid decimal character or\r
1572 Null-terminator, whichever on comes first.\r
1573\r
1574 If String has only pad spaces, then 0 is returned.\r
1575 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
1576 If the number represented by String overflows according to the range defined by\r
1577 UINT64, then MAX_UINT64 is returned.\r
1578 If String is NULL, then ASSERT().\r
1579 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1580 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1581 then ASSERT().\r
1582\r
1583 @param String A pointer to a Null-terminated ASCII string.\r
1584\r
1585 @retval Value translated from String.\r
1586\r
1587**/\r
1588UINT64\r
1589EFIAPI\r
1590AsciiStrDecimalToUint64 (\r
1591 IN CONST CHAR8 *String\r
1592 )\r
1593{\r
1594 UINT64 Result;\r
1595 \r
1596 AsciiStrDecimalToUint64S (String, (CHAR8 **) NULL, &Result);\r
1597 return Result;\r
1598}\r
1599\r
1600/**\r
1601 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1602\r
1603 This function returns a value of type UINTN by interpreting the contents of\r
1604 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
1605 string String is:\r
1606\r
1607 [spaces][zeros][x][hexadecimal digits].\r
1608\r
1609 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1610 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1611 appears in the input string, it must be prefixed with at least one 0. The function\r
1612 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1613 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1614 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1615 digit. Then, the function stops at the first character that is a not a valid\r
1616 hexadecimal character or Null-terminator, whichever on comes first.\r
1617\r
1618 If String has only pad spaces, then 0 is returned.\r
1619 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1620 0 is returned.\r
1621\r
1622 If the number represented by String overflows according to the range defined by UINTN,\r
1623 then MAX_UINTN is returned.\r
1624 If String is NULL, then ASSERT().\r
1625 If PcdMaximumAsciiStringLength is not zero,\r
1626 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
1627 the Null-terminator, then ASSERT().\r
1628\r
1629 @param String A pointer to a Null-terminated ASCII string.\r
1630\r
1631 @retval Value translated from String.\r
1632\r
1633**/\r
1634UINTN\r
1635EFIAPI\r
1636AsciiStrHexToUintn (\r
1637 IN CONST CHAR8 *String\r
1638 )\r
1639{\r
1640 UINTN Result;\r
1641\r
1642 AsciiStrHexToUintnS (String, (CHAR8 **) NULL, &Result);\r
1643 return Result;\r
1644}\r
1645\r
1646\r
1647/**\r
1648 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1649\r
1650 This function returns a value of type UINT64 by interpreting the contents of\r
1651 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
1652 string String is:\r
1653\r
1654 [spaces][zeros][x][hexadecimal digits].\r
1655\r
1656 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1657 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1658 appears in the input string, it must be prefixed with at least one 0. The function\r
1659 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1660 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1661 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1662 digit. Then, the function stops at the first character that is a not a valid\r
1663 hexadecimal character or Null-terminator, whichever on comes first.\r
1664\r
1665 If String has only pad spaces, then 0 is returned.\r
1666 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1667 0 is returned.\r
1668\r
1669 If the number represented by String overflows according to the range defined by UINT64,\r
1670 then MAX_UINT64 is returned.\r
1671 If String is NULL, then ASSERT().\r
1672 If PcdMaximumAsciiStringLength is not zero,\r
1673 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
1674 the Null-terminator, then ASSERT().\r
1675\r
1676 @param String A pointer to a Null-terminated ASCII string.\r
1677\r
1678 @retval Value translated from String.\r
1679\r
1680**/\r
1681UINT64\r
1682EFIAPI\r
1683AsciiStrHexToUint64 (\r
1684 IN CONST CHAR8 *String\r
1685 )\r
1686{\r
1687 UINT64 Result;\r
1688\r
1689 AsciiStrHexToUint64S (String, (CHAR8 **) NULL, &Result);\r
1690 return Result;\r
1691}\r
1692\r
1693#ifndef DISABLE_NEW_DEPRECATED_INTERFACES\r
1694\r
1695/**\r
1696 [ATTENTION] This function is deprecated for security reason.\r
1697\r
1698 Convert one Null-terminated ASCII string to a Null-terminated\r
1699 Unicode string and returns the Unicode string.\r
1700\r
1701 This function converts the contents of the ASCII string Source to the Unicode\r
1702 string Destination, and returns Destination. The function terminates the\r
1703 Unicode string Destination by appending a Null-terminator character at the end.\r
1704 The caller is responsible to make sure Destination points to a buffer with size\r
1705 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
1706\r
1707 If Destination is NULL, then ASSERT().\r
1708 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
1709 If Source is NULL, then ASSERT().\r
1710 If Source and Destination overlap, then ASSERT().\r
1711 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1712 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1713 then ASSERT().\r
1714 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
1715 PcdMaximumUnicodeStringLength ASCII characters not including the\r
1716 Null-terminator, then ASSERT().\r
1717\r
1718 @param Source A pointer to a Null-terminated ASCII string.\r
1719 @param Destination A pointer to a Null-terminated Unicode string.\r
1720\r
1721 @return Destination.\r
1722\r
1723**/\r
1724CHAR16 *\r
1725EFIAPI\r
1726AsciiStrToUnicodeStr (\r
1727 IN CONST CHAR8 *Source,\r
1728 OUT CHAR16 *Destination\r
1729 )\r
1730{\r
1731 CHAR16 *ReturnValue;\r
1732\r
1733 ASSERT (Destination != NULL);\r
1734\r
1735 //\r
1736 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
1737 //\r
1738 ASSERT (AsciiStrSize (Source) != 0);\r
1739\r
1740 //\r
1741 // Source and Destination should not overlap\r
1742 //\r
1743 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
1744 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) >= (AsciiStrSize (Source) * sizeof (CHAR16)));\r
1745\r
1746\r
1747 ReturnValue = Destination;\r
1748 while (*Source != '\0') {\r
1749 *(Destination++) = (CHAR16) *(Source++);\r
1750 }\r
1751 //\r
1752 // End the Destination with a NULL.\r
1753 //\r
1754 *Destination = '\0';\r
1755\r
1756 //\r
1757 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
1758 //\r
1759 ASSERT (StrSize (ReturnValue) != 0);\r
1760\r
1761 return ReturnValue;\r
1762}\r
1763\r
1764#endif\r
1765\r
1766/**\r
1767 Converts an 8-bit value to an 8-bit BCD value.\r
1768\r
1769 Converts the 8-bit value specified by Value to BCD. The BCD value is\r
1770 returned.\r
1771\r
1772 If Value >= 100, then ASSERT().\r
1773\r
1774 @param Value The 8-bit value to convert to BCD. Range 0..99.\r
1775\r
1776 @return The BCD value.\r
1777\r
1778**/\r
1779UINT8\r
1780EFIAPI\r
1781DecimalToBcd8 (\r
1782 IN UINT8 Value\r
1783 )\r
1784{\r
1785 ASSERT (Value < 100);\r
1786 return (UINT8) (((Value / 10) << 4) | (Value % 10));\r
1787}\r
1788\r
1789/**\r
1790 Converts an 8-bit BCD value to an 8-bit value.\r
1791\r
1792 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
1793 value is returned.\r
1794\r
1795 If Value >= 0xA0, then ASSERT().\r
1796 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
1797\r
1798 @param Value The 8-bit BCD value to convert to an 8-bit value.\r
1799\r
1800 @return The 8-bit value is returned.\r
1801\r
1802**/\r
1803UINT8\r
1804EFIAPI\r
1805BcdToDecimal8 (\r
1806 IN UINT8 Value\r
1807 )\r
1808{\r
1809 ASSERT (Value < 0xa0);\r
1810 ASSERT ((Value & 0xf) < 0xa);\r
1811 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
1812}\r