]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/SafeString.c
b7895adfc0952b13e75e56f75eee45182d8a3225
[mirror_edk2.git] / MdePkg / Library / BaseLib / SafeString.c
1 /** @file
2 Safe String functions.
3
4 Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "BaseLibInternals.h"
16
17 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
18
19 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
20
21 #define SAFE_STRING_CONSTRAINT_CHECK(Expression, Status) \
22 do { \
23 ASSERT (Expression); \
24 if (!(Expression)) { \
25 return Status; \
26 } \
27 } while (FALSE)
28
29 /**
30 Returns if 2 memory blocks are overlapped.
31
32 @param Base1 Base address of 1st memory block.
33 @param Size1 Size of 1st memory block.
34 @param Base2 Base address of 2nd memory block.
35 @param Size2 Size of 2nd memory block.
36
37 @retval TRUE 2 memory blocks are overlapped.
38 @retval FALSE 2 memory blocks are not overlapped.
39 **/
40 BOOLEAN
41 InternalSafeStringIsOverlap (
42 IN VOID *Base1,
43 IN UINTN Size1,
44 IN VOID *Base2,
45 IN UINTN Size2
46 )
47 {
48 if ((((UINTN)Base1 >= (UINTN)Base2) && ((UINTN)Base1 < (UINTN)Base2 + Size2)) ||
49 (((UINTN)Base2 >= (UINTN)Base1) && ((UINTN)Base2 < (UINTN)Base1 + Size1))) {
50 return TRUE;
51 }
52 return FALSE;
53 }
54
55 /**
56 Returns if 2 Unicode strings are not overlapped.
57
58 @param Str1 Start address of 1st Unicode string.
59 @param Size1 The number of char in 1st Unicode string,
60 including terminating null char.
61 @param Str2 Start address of 2nd Unicode string.
62 @param Size2 The number of char in 2nd Unicode string,
63 including terminating null char.
64
65 @retval TRUE 2 Unicode strings are NOT overlapped.
66 @retval FALSE 2 Unicode strings are overlapped.
67 **/
68 BOOLEAN
69 InternalSafeStringNoStrOverlap (
70 IN CHAR16 *Str1,
71 IN UINTN Size1,
72 IN CHAR16 *Str2,
73 IN UINTN Size2
74 )
75 {
76 return !InternalSafeStringIsOverlap (Str1, Size1 * sizeof(CHAR16), Str2, Size2 * sizeof(CHAR16));
77 }
78
79 /**
80 Returns if 2 Ascii strings are not overlapped.
81
82 @param Str1 Start address of 1st Ascii string.
83 @param Size1 The number of char in 1st Ascii string,
84 including terminating null char.
85 @param Str2 Start address of 2nd Ascii string.
86 @param Size2 The number of char in 2nd Ascii string,
87 including terminating null char.
88
89 @retval TRUE 2 Ascii strings are NOT overlapped.
90 @retval FALSE 2 Ascii strings are overlapped.
91 **/
92 BOOLEAN
93 InternalSafeStringNoAsciiStrOverlap (
94 IN CHAR8 *Str1,
95 IN UINTN Size1,
96 IN CHAR8 *Str2,
97 IN UINTN Size2
98 )
99 {
100 return !InternalSafeStringIsOverlap (Str1, Size1, Str2, Size2);
101 }
102
103 /**
104 Returns the length of a Null-terminated Unicode string.
105
106 This function is similar as strlen_s defined in C11.
107
108 If String is not aligned on a 16-bit boundary, then ASSERT().
109
110 @param String A pointer to a Null-terminated Unicode string.
111 @param MaxSize The maximum number of Destination Unicode
112 char, including terminating null char.
113
114 @retval 0 If String is NULL.
115 @retval MaxSize If there is no null character in the first MaxSize characters of String.
116 @return The number of characters that percede the terminating null character.
117
118 **/
119 UINTN
120 EFIAPI
121 StrnLenS (
122 IN CONST CHAR16 *String,
123 IN UINTN MaxSize
124 )
125 {
126 UINTN Length;
127
128 ASSERT (((UINTN) String & BIT0) == 0);
129
130 //
131 // If String is a null pointer or MaxSize is 0, then the StrnLenS function returns zero.
132 //
133 if ((String == NULL) || (MaxSize == 0)) {
134 return 0;
135 }
136
137 //
138 // Otherwise, the StrnLenS function returns the number of characters that precede the
139 // terminating null character. If there is no null character in the first MaxSize characters of
140 // String then StrnLenS returns MaxSize. At most the first MaxSize characters of String shall
141 // be accessed by StrnLenS.
142 //
143 Length = 0;
144 while (String[Length] != 0) {
145 if (Length >= MaxSize - 1) {
146 return MaxSize;
147 }
148 Length++;
149 }
150 return Length;
151 }
152
153 /**
154 Returns the size of a Null-terminated Unicode string in bytes, including the
155 Null terminator.
156
157 This function returns the size of the Null-terminated Unicode string
158 specified by String in bytes, including the Null terminator.
159
160 If String is not aligned on a 16-bit boundary, then ASSERT().
161
162 @param String A pointer to a Null-terminated Unicode string.
163 @param MaxSize The maximum number of Destination Unicode
164 char, including the Null terminator.
165
166 @retval 0 If String is NULL.
167 @retval (sizeof (CHAR16) * (MaxSize + 1))
168 If there is no Null terminator in the first MaxSize characters of
169 String.
170 @return The size of the Null-terminated Unicode string in bytes, including
171 the Null terminator.
172
173 **/
174 UINTN
175 EFIAPI
176 StrnSizeS (
177 IN CONST CHAR16 *String,
178 IN UINTN MaxSize
179 )
180 {
181 //
182 // If String is a null pointer, then the StrnSizeS function returns zero.
183 //
184 if (String == NULL) {
185 return 0;
186 }
187
188 //
189 // Otherwise, the StrnSizeS function returns the size of the Null-terminated
190 // Unicode string in bytes, including the Null terminator. If there is no
191 // Null terminator in the first MaxSize characters of String, then StrnSizeS
192 // returns (sizeof (CHAR16) * (MaxSize + 1)) to keep a consistent map with
193 // the StrnLenS function.
194 //
195 return (StrnLenS (String, MaxSize) + 1) * sizeof (*String);
196 }
197
198 /**
199 Copies the string pointed to by Source (including the terminating null char)
200 to the array pointed to by Destination.
201
202 This function is similar as strcpy_s defined in C11.
203
204 If Destination is not aligned on a 16-bit boundary, then ASSERT().
205 If Source is not aligned on a 16-bit boundary, then ASSERT().
206 If an error would be returned, then the function will also ASSERT().
207
208 If an error is returned, then the Destination is unmodified.
209
210 @param Destination A pointer to a Null-terminated Unicode string.
211 @param DestMax The maximum number of Destination Unicode
212 char, including terminating null char.
213 @param Source A pointer to a Null-terminated Unicode string.
214
215 @retval RETURN_SUCCESS String is copied.
216 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
217 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
218 If Source is NULL.
219 If PcdMaximumUnicodeStringLength is not zero,
220 and DestMax is greater than
221 PcdMaximumUnicodeStringLength.
222 If DestMax is 0.
223 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
224 **/
225 RETURN_STATUS
226 EFIAPI
227 StrCpyS (
228 OUT CHAR16 *Destination,
229 IN UINTN DestMax,
230 IN CONST CHAR16 *Source
231 )
232 {
233 UINTN SourceLen;
234
235 ASSERT (((UINTN) Destination & BIT0) == 0);
236 ASSERT (((UINTN) Source & BIT0) == 0);
237
238 //
239 // 1. Neither Destination nor Source shall be a null pointer.
240 //
241 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
242 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
243
244 //
245 // 2. DestMax shall not be greater than RSIZE_MAX.
246 //
247 if (RSIZE_MAX != 0) {
248 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
249 }
250
251 //
252 // 3. DestMax shall not equal zero.
253 //
254 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
255
256 //
257 // 4. DestMax shall be greater than StrnLenS(Source, DestMax).
258 //
259 SourceLen = StrnLenS (Source, DestMax);
260 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
261
262 //
263 // 5. Copying shall not take place between objects that overlap.
264 //
265 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
266
267 //
268 // The StrCpyS function copies the string pointed to by Source (including the terminating
269 // null character) into the array pointed to by Destination.
270 //
271 while (*Source != 0) {
272 *(Destination++) = *(Source++);
273 }
274 *Destination = 0;
275
276 return RETURN_SUCCESS;
277 }
278
279 /**
280 Copies not more than Length successive char from the string pointed to by
281 Source to the array pointed to by Destination. If no null char is copied from
282 Source, then Destination[Length] is always set to null.
283
284 This function is similar as strncpy_s defined in C11.
285
286 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().
287 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().
288 If an error would be returned, then the function will also ASSERT().
289
290 If an error is returned, then the Destination is unmodified.
291
292 @param Destination A pointer to a Null-terminated Unicode string.
293 @param DestMax The maximum number of Destination Unicode
294 char, including terminating null char.
295 @param Source A pointer to a Null-terminated Unicode string.
296 @param Length The maximum number of Unicode characters to copy.
297
298 @retval RETURN_SUCCESS String is copied.
299 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than
300 MIN(StrLen(Source), Length).
301 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
302 If Source is NULL.
303 If PcdMaximumUnicodeStringLength is not zero,
304 and DestMax is greater than
305 PcdMaximumUnicodeStringLength.
306 If DestMax is 0.
307 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
308 **/
309 RETURN_STATUS
310 EFIAPI
311 StrnCpyS (
312 OUT CHAR16 *Destination,
313 IN UINTN DestMax,
314 IN CONST CHAR16 *Source,
315 IN UINTN Length
316 )
317 {
318 UINTN SourceLen;
319
320 ASSERT (((UINTN) Destination & BIT0) == 0);
321 ASSERT (((UINTN) Source & BIT0) == 0);
322
323 //
324 // 1. Neither Destination nor Source shall be a null pointer.
325 //
326 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
327 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
328
329 //
330 // 2. Neither DestMax nor Length shall be greater than RSIZE_MAX
331 //
332 if (RSIZE_MAX != 0) {
333 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
334 SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
335 }
336
337 //
338 // 3. DestMax shall not equal zero.
339 //
340 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
341
342 //
343 // 4. If Length is not less than DestMax, then DestMax shall be greater than StrnLenS(Source, DestMax).
344 //
345 SourceLen = StrnLenS (Source, DestMax);
346 if (Length >= DestMax) {
347 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
348 }
349
350 //
351 // 5. Copying shall not take place between objects that overlap.
352 //
353 if (SourceLen > Length) {
354 SourceLen = Length;
355 }
356 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
357
358 //
359 // The StrnCpyS function copies not more than Length successive characters (characters that
360 // follow a null character are not copied) from the array pointed to by Source to the array
361 // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null
362 // character.
363 //
364 while ((*Source != 0) && (SourceLen > 0)) {
365 *(Destination++) = *(Source++);
366 SourceLen--;
367 }
368 *Destination = 0;
369
370 return RETURN_SUCCESS;
371 }
372
373 /**
374 Appends a copy of the string pointed to by Source (including the terminating
375 null char) to the end of the string pointed to by Destination.
376
377 This function is similar as strcat_s defined in C11.
378
379 If Destination is not aligned on a 16-bit boundary, then ASSERT().
380 If Source is not aligned on a 16-bit boundary, then ASSERT().
381 If an error would be returned, then the function will also ASSERT().
382
383 If an error is returned, then the Destination is unmodified.
384
385 @param Destination A pointer to a Null-terminated Unicode string.
386 @param DestMax The maximum number of Destination Unicode
387 char, including terminating null char.
388 @param Source A pointer to a Null-terminated Unicode string.
389
390 @retval RETURN_SUCCESS String is appended.
391 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
392 StrLen(Destination).
393 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
394 greater than StrLen(Source).
395 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
396 If Source is NULL.
397 If PcdMaximumUnicodeStringLength is not zero,
398 and DestMax is greater than
399 PcdMaximumUnicodeStringLength.
400 If DestMax is 0.
401 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
402 **/
403 RETURN_STATUS
404 EFIAPI
405 StrCatS (
406 IN OUT CHAR16 *Destination,
407 IN UINTN DestMax,
408 IN CONST CHAR16 *Source
409 )
410 {
411 UINTN DestLen;
412 UINTN CopyLen;
413 UINTN SourceLen;
414
415 ASSERT (((UINTN) Destination & BIT0) == 0);
416 ASSERT (((UINTN) Source & BIT0) == 0);
417
418 //
419 // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrCatS.
420 //
421 DestLen = StrnLenS (Destination, DestMax);
422 CopyLen = DestMax - DestLen;
423
424 //
425 // 1. Neither Destination nor Source shall be a null pointer.
426 //
427 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
428 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
429
430 //
431 // 2. DestMax shall not be greater than RSIZE_MAX.
432 //
433 if (RSIZE_MAX != 0) {
434 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
435 }
436
437 //
438 // 3. DestMax shall not equal zero.
439 //
440 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
441
442 //
443 // 4. CopyLen shall not equal zero.
444 //
445 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen != 0), RETURN_BAD_BUFFER_SIZE);
446
447 //
448 // 5. CopyLen shall be greater than StrnLenS(Source, CopyLen).
449 //
450 SourceLen = StrnLenS (Source, CopyLen);
451 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);
452
453 //
454 // 6. Copying shall not take place between objects that overlap.
455 //
456 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
457
458 //
459 // The StrCatS function appends a copy of the string pointed to by Source (including the
460 // terminating null character) to the end of the string pointed to by Destination. The initial character
461 // from Source overwrites the null character at the end of Destination.
462 //
463 Destination = Destination + DestLen;
464 while (*Source != 0) {
465 *(Destination++) = *(Source++);
466 }
467 *Destination = 0;
468
469 return RETURN_SUCCESS;
470 }
471
472 /**
473 Appends not more than Length successive char from the string pointed to by
474 Source to the end of the string pointed to by Destination. If no null char is
475 copied from Source, then Destination[StrLen(Destination) + Length] is always
476 set to null.
477
478 This function is similar as strncat_s defined in C11.
479
480 If Destination is not aligned on a 16-bit boundary, then ASSERT().
481 If Source is not aligned on a 16-bit boundary, then ASSERT().
482 If an error would be returned, then the function will also ASSERT().
483
484 If an error is returned, then the Destination is unmodified.
485
486 @param Destination A pointer to a Null-terminated Unicode string.
487 @param DestMax The maximum number of Destination Unicode
488 char, including terminating null char.
489 @param Source A pointer to a Null-terminated Unicode string.
490 @param Length The maximum number of Unicode characters to copy.
491
492 @retval RETURN_SUCCESS String is appended.
493 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
494 StrLen(Destination).
495 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
496 greater than MIN(StrLen(Source), Length).
497 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
498 If Source is NULL.
499 If PcdMaximumUnicodeStringLength is not zero,
500 and DestMax is greater than
501 PcdMaximumUnicodeStringLength.
502 If DestMax is 0.
503 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
504 **/
505 RETURN_STATUS
506 EFIAPI
507 StrnCatS (
508 IN OUT CHAR16 *Destination,
509 IN UINTN DestMax,
510 IN CONST CHAR16 *Source,
511 IN UINTN Length
512 )
513 {
514 UINTN DestLen;
515 UINTN CopyLen;
516 UINTN SourceLen;
517
518 ASSERT (((UINTN) Destination & BIT0) == 0);
519 ASSERT (((UINTN) Source & BIT0) == 0);
520
521 //
522 // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrnCatS.
523 //
524 DestLen = StrnLenS (Destination, DestMax);
525 CopyLen = DestMax - DestLen;
526
527 //
528 // 1. Neither Destination nor Source shall be a null pointer.
529 //
530 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
531 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
532
533 //
534 // 2. Neither DestMax nor Length shall be greater than RSIZE_MAX.
535 //
536 if (RSIZE_MAX != 0) {
537 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
538 SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
539 }
540
541 //
542 // 3. DestMax shall not equal zero.
543 //
544 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
545
546 //
547 // 4. CopyLen shall not equal zero.
548 //
549 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen != 0), RETURN_BAD_BUFFER_SIZE);
550
551 //
552 // 5. If Length is not less than CopyLen, then CopyLen shall be greater than StrnLenS(Source, CopyLen).
553 //
554 SourceLen = StrnLenS (Source, CopyLen);
555 if (Length >= CopyLen) {
556 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);
557 }
558
559 //
560 // 6. Copying shall not take place between objects that overlap.
561 //
562 if (SourceLen > Length) {
563 SourceLen = Length;
564 }
565 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination, DestMax, (CHAR16 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
566
567 //
568 // The StrnCatS function appends not more than Length successive characters (characters
569 // that follow a null character are not copied) from the array pointed to by Source to the end of
570 // the string pointed to by Destination. The initial character from Source overwrites the null character at
571 // the end of Destination. If no null character was copied from Source, then Destination[DestMax-CopyLen+Length] is set to
572 // a null character.
573 //
574 Destination = Destination + DestLen;
575 while ((*Source != 0) && (SourceLen > 0)) {
576 *(Destination++) = *(Source++);
577 SourceLen--;
578 }
579 *Destination = 0;
580
581 return RETURN_SUCCESS;
582 }
583
584 /**
585 Convert a Null-terminated Unicode decimal string to a value of type UINTN.
586
587 This function outputs a value of type UINTN by interpreting the contents of
588 the Unicode string specified by String as a decimal number. The format of the
589 input Unicode string String is:
590
591 [spaces] [decimal digits].
592
593 The valid decimal digit character is in the range [0-9]. The function will
594 ignore the pad space, which includes spaces or tab characters, before
595 [decimal digits]. The running zero in the beginning of [decimal digits] will
596 be ignored. Then, the function stops at the first character that is a not a
597 valid decimal character or a Null-terminator, whichever one comes first.
598
599 If String is NULL, then ASSERT().
600 If Data is NULL, then ASSERT().
601 If String is not aligned in a 16-bit boundary, then ASSERT().
602 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
603 PcdMaximumUnicodeStringLength Unicode characters, not including the
604 Null-terminator, then ASSERT().
605
606 If String has no valid decimal digits in the above format, then 0 is stored
607 at the location pointed to by Data.
608 If the number represented by String exceeds the range defined by UINTN, then
609 MAX_UINTN is stored at the location pointed to by Data.
610
611 If EndPointer is not NULL, a pointer to the character that stopped the scan
612 is stored at the location pointed to by EndPointer. If String has no valid
613 decimal digits right after the optional pad spaces, the value of String is
614 stored at the location pointed to by EndPointer.
615
616 @param String Pointer to a Null-terminated Unicode string.
617 @param EndPointer Pointer to character that stops scan.
618 @param Data Pointer to the converted value.
619
620 @retval RETURN_SUCCESS Value is translated from String.
621 @retval RETURN_INVALID_PARAMETER If String is NULL.
622 If Data is NULL.
623 If PcdMaximumUnicodeStringLength is not
624 zero, and String contains more than
625 PcdMaximumUnicodeStringLength Unicode
626 characters, not including the
627 Null-terminator.
628 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
629 the range defined by UINTN.
630
631 **/
632 RETURN_STATUS
633 EFIAPI
634 StrDecimalToUintnS (
635 IN CONST CHAR16 *String,
636 OUT CHAR16 **EndPointer, OPTIONAL
637 OUT UINTN *Data
638 )
639 {
640 ASSERT (((UINTN) String & BIT0) == 0);
641
642 //
643 // 1. Neither String nor Data shall be a null pointer.
644 //
645 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
646 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
647
648 //
649 // 2. The length of String shall not be greater than RSIZE_MAX.
650 //
651 if (RSIZE_MAX != 0) {
652 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
653 }
654
655 if (EndPointer != NULL) {
656 *EndPointer = (CHAR16 *) String;
657 }
658
659 //
660 // Ignore the pad spaces (space or tab)
661 //
662 while ((*String == L' ') || (*String == L'\t')) {
663 String++;
664 }
665
666 //
667 // Ignore leading Zeros after the spaces
668 //
669 while (*String == L'0') {
670 String++;
671 }
672
673 *Data = 0;
674
675 while (InternalIsDecimalDigitCharacter (*String)) {
676 //
677 // If the number represented by String overflows according to the range
678 // defined by UINTN, then MAX_UINTN is stored in *Data and
679 // RETURN_UNSUPPORTED is returned.
680 //
681 if (*Data > ((MAX_UINTN - (*String - L'0')) / 10)) {
682 *Data = MAX_UINTN;
683 if (EndPointer != NULL) {
684 *EndPointer = (CHAR16 *) String;
685 }
686 return RETURN_UNSUPPORTED;
687 }
688
689 *Data = *Data * 10 + (*String - L'0');
690 String++;
691 }
692
693 if (EndPointer != NULL) {
694 *EndPointer = (CHAR16 *) String;
695 }
696 return RETURN_SUCCESS;
697 }
698
699 /**
700 Convert a Null-terminated Unicode decimal string to a value of type UINT64.
701
702 This function outputs a value of type UINT64 by interpreting the contents of
703 the Unicode string specified by String as a decimal number. The format of the
704 input Unicode string String is:
705
706 [spaces] [decimal digits].
707
708 The valid decimal digit character is in the range [0-9]. The function will
709 ignore the pad space, which includes spaces or tab characters, before
710 [decimal digits]. The running zero in the beginning of [decimal digits] will
711 be ignored. Then, the function stops at the first character that is a not a
712 valid decimal character or a Null-terminator, whichever one comes first.
713
714 If String is NULL, then ASSERT().
715 If Data is NULL, then ASSERT().
716 If String is not aligned in a 16-bit boundary, then ASSERT().
717 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
718 PcdMaximumUnicodeStringLength Unicode characters, not including the
719 Null-terminator, then ASSERT().
720
721 If String has no valid decimal digits in the above format, then 0 is stored
722 at the location pointed to by Data.
723 If the number represented by String exceeds the range defined by UINT64, then
724 MAX_UINT64 is stored at the location pointed to by Data.
725
726 If EndPointer is not NULL, a pointer to the character that stopped the scan
727 is stored at the location pointed to by EndPointer. If String has no valid
728 decimal digits right after the optional pad spaces, the value of String is
729 stored at the location pointed to by EndPointer.
730
731 @param String Pointer to a Null-terminated Unicode string.
732 @param EndPointer Pointer to character that stops scan.
733 @param Data Pointer to the converted value.
734
735 @retval RETURN_SUCCESS Value is translated from String.
736 @retval RETURN_INVALID_PARAMETER If String is NULL.
737 If Data is NULL.
738 If PcdMaximumUnicodeStringLength is not
739 zero, and String contains more than
740 PcdMaximumUnicodeStringLength Unicode
741 characters, not including the
742 Null-terminator.
743 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
744 the range defined by UINT64.
745
746 **/
747 RETURN_STATUS
748 EFIAPI
749 StrDecimalToUint64S (
750 IN CONST CHAR16 *String,
751 OUT CHAR16 **EndPointer, OPTIONAL
752 OUT UINT64 *Data
753 )
754 {
755 ASSERT (((UINTN) String & BIT0) == 0);
756
757 //
758 // 1. Neither String nor Data shall be a null pointer.
759 //
760 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
761 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
762
763 //
764 // 2. The length of String shall not be greater than RSIZE_MAX.
765 //
766 if (RSIZE_MAX != 0) {
767 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
768 }
769
770 if (EndPointer != NULL) {
771 *EndPointer = (CHAR16 *) String;
772 }
773
774 //
775 // Ignore the pad spaces (space or tab)
776 //
777 while ((*String == L' ') || (*String == L'\t')) {
778 String++;
779 }
780
781 //
782 // Ignore leading Zeros after the spaces
783 //
784 while (*String == L'0') {
785 String++;
786 }
787
788 *Data = 0;
789
790 while (InternalIsDecimalDigitCharacter (*String)) {
791 //
792 // If the number represented by String overflows according to the range
793 // defined by UINT64, then MAX_UINT64 is stored in *Data and
794 // RETURN_UNSUPPORTED is returned.
795 //
796 if (*Data > DivU64x32 (MAX_UINT64 - (*String - L'0'), 10)) {
797 *Data = MAX_UINT64;
798 if (EndPointer != NULL) {
799 *EndPointer = (CHAR16 *) String;
800 }
801 return RETURN_UNSUPPORTED;
802 }
803
804 *Data = MultU64x32 (*Data, 10) + (*String - L'0');
805 String++;
806 }
807
808 if (EndPointer != NULL) {
809 *EndPointer = (CHAR16 *) String;
810 }
811 return RETURN_SUCCESS;
812 }
813
814 /**
815 Convert a Null-terminated Unicode hexadecimal string to a value of type
816 UINTN.
817
818 This function outputs a value of type UINTN by interpreting the contents of
819 the Unicode string specified by String as a hexadecimal number. The format of
820 the input Unicode string String is:
821
822 [spaces][zeros][x][hexadecimal digits].
823
824 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
825 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
826 If "x" appears in the input string, it must be prefixed with at least one 0.
827 The function will ignore the pad space, which includes spaces or tab
828 characters, before [zeros], [x] or [hexadecimal digit]. The running zero
829 before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts
830 after [x] or the first valid hexadecimal digit. Then, the function stops at
831 the first character that is a not a valid hexadecimal character or NULL,
832 whichever one comes first.
833
834 If String is NULL, then ASSERT().
835 If Data is NULL, then ASSERT().
836 If String is not aligned in a 16-bit boundary, then ASSERT().
837 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
838 PcdMaximumUnicodeStringLength Unicode characters, not including the
839 Null-terminator, then ASSERT().
840
841 If String has no valid hexadecimal digits in the above format, then 0 is
842 stored at the location pointed to by Data.
843 If the number represented by String exceeds the range defined by UINTN, then
844 MAX_UINTN is stored at the location pointed to by Data.
845
846 If EndPointer is not NULL, a pointer to the character that stopped the scan
847 is stored at the location pointed to by EndPointer. If String has no valid
848 hexadecimal digits right after the optional pad spaces, the value of String
849 is stored at the location pointed to by EndPointer.
850
851 @param String Pointer to a Null-terminated Unicode string.
852 @param EndPointer Pointer to character that stops scan.
853 @param Data Pointer to the converted value.
854
855 @retval RETURN_SUCCESS Value is translated from String.
856 @retval RETURN_INVALID_PARAMETER If String is NULL.
857 If Data is NULL.
858 If PcdMaximumUnicodeStringLength is not
859 zero, and String contains more than
860 PcdMaximumUnicodeStringLength Unicode
861 characters, not including the
862 Null-terminator.
863 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
864 the range defined by UINTN.
865
866 **/
867 RETURN_STATUS
868 EFIAPI
869 StrHexToUintnS (
870 IN CONST CHAR16 *String,
871 OUT CHAR16 **EndPointer, OPTIONAL
872 OUT UINTN *Data
873 )
874 {
875 ASSERT (((UINTN) String & BIT0) == 0);
876
877 //
878 // 1. Neither String nor Data shall be a null pointer.
879 //
880 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
881 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
882
883 //
884 // 2. The length of String shall not be greater than RSIZE_MAX.
885 //
886 if (RSIZE_MAX != 0) {
887 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
888 }
889
890 if (EndPointer != NULL) {
891 *EndPointer = (CHAR16 *) String;
892 }
893
894 //
895 // Ignore the pad spaces (space or tab)
896 //
897 while ((*String == L' ') || (*String == L'\t')) {
898 String++;
899 }
900
901 //
902 // Ignore leading Zeros after the spaces
903 //
904 while (*String == L'0') {
905 String++;
906 }
907
908 if (InternalCharToUpper (*String) == L'X') {
909 if (*(String - 1) != L'0') {
910 *Data = 0;
911 return RETURN_SUCCESS;
912 }
913 //
914 // Skip the 'X'
915 //
916 String++;
917 }
918
919 *Data = 0;
920
921 while (InternalIsHexaDecimalDigitCharacter (*String)) {
922 //
923 // If the number represented by String overflows according to the range
924 // defined by UINTN, then MAX_UINTN is stored in *Data and
925 // RETURN_UNSUPPORTED is returned.
926 //
927 if (*Data > ((MAX_UINTN - InternalHexCharToUintn (*String)) >> 4)) {
928 *Data = MAX_UINTN;
929 if (EndPointer != NULL) {
930 *EndPointer = (CHAR16 *) String;
931 }
932 return RETURN_UNSUPPORTED;
933 }
934
935 *Data = (*Data << 4) + InternalHexCharToUintn (*String);
936 String++;
937 }
938
939 if (EndPointer != NULL) {
940 *EndPointer = (CHAR16 *) String;
941 }
942 return RETURN_SUCCESS;
943 }
944
945 /**
946 Convert a Null-terminated Unicode hexadecimal string to a value of type
947 UINT64.
948
949 This function outputs a value of type UINT64 by interpreting the contents of
950 the Unicode string specified by String as a hexadecimal number. The format of
951 the input Unicode string String is:
952
953 [spaces][zeros][x][hexadecimal digits].
954
955 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
956 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
957 If "x" appears in the input string, it must be prefixed with at least one 0.
958 The function will ignore the pad space, which includes spaces or tab
959 characters, before [zeros], [x] or [hexadecimal digit]. The running zero
960 before [x] or [hexadecimal digit] will be ignored. Then, the decoding starts
961 after [x] or the first valid hexadecimal digit. Then, the function stops at
962 the first character that is a not a valid hexadecimal character or NULL,
963 whichever one comes first.
964
965 If String is NULL, then ASSERT().
966 If Data is NULL, then ASSERT().
967 If String is not aligned in a 16-bit boundary, then ASSERT().
968 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
969 PcdMaximumUnicodeStringLength Unicode characters, not including the
970 Null-terminator, then ASSERT().
971
972 If String has no valid hexadecimal digits in the above format, then 0 is
973 stored at the location pointed to by Data.
974 If the number represented by String exceeds the range defined by UINT64, then
975 MAX_UINT64 is stored at the location pointed to by Data.
976
977 If EndPointer is not NULL, a pointer to the character that stopped the scan
978 is stored at the location pointed to by EndPointer. If String has no valid
979 hexadecimal digits right after the optional pad spaces, the value of String
980 is stored at the location pointed to by EndPointer.
981
982 @param String Pointer to a Null-terminated Unicode string.
983 @param EndPointer Pointer to character that stops scan.
984 @param Data Pointer to the converted value.
985
986 @retval RETURN_SUCCESS Value is translated from String.
987 @retval RETURN_INVALID_PARAMETER If String is NULL.
988 If Data is NULL.
989 If PcdMaximumUnicodeStringLength is not
990 zero, and String contains more than
991 PcdMaximumUnicodeStringLength Unicode
992 characters, not including the
993 Null-terminator.
994 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
995 the range defined by UINT64.
996
997 **/
998 RETURN_STATUS
999 EFIAPI
1000 StrHexToUint64S (
1001 IN CONST CHAR16 *String,
1002 OUT CHAR16 **EndPointer, OPTIONAL
1003 OUT UINT64 *Data
1004 )
1005 {
1006 ASSERT (((UINTN) String & BIT0) == 0);
1007
1008 //
1009 // 1. Neither String nor Data shall be a null pointer.
1010 //
1011 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
1012 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
1013
1014 //
1015 // 2. The length of String shall not be greater than RSIZE_MAX.
1016 //
1017 if (RSIZE_MAX != 0) {
1018 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String, RSIZE_MAX + 1) <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
1019 }
1020
1021 if (EndPointer != NULL) {
1022 *EndPointer = (CHAR16 *) String;
1023 }
1024
1025 //
1026 // Ignore the pad spaces (space or tab)
1027 //
1028 while ((*String == L' ') || (*String == L'\t')) {
1029 String++;
1030 }
1031
1032 //
1033 // Ignore leading Zeros after the spaces
1034 //
1035 while (*String == L'0') {
1036 String++;
1037 }
1038
1039 if (InternalCharToUpper (*String) == L'X') {
1040 if (*(String - 1) != L'0') {
1041 *Data = 0;
1042 return RETURN_SUCCESS;
1043 }
1044 //
1045 // Skip the 'X'
1046 //
1047 String++;
1048 }
1049
1050 *Data = 0;
1051
1052 while (InternalIsHexaDecimalDigitCharacter (*String)) {
1053 //
1054 // If the number represented by String overflows according to the range
1055 // defined by UINT64, then MAX_UINT64 is stored in *Data and
1056 // RETURN_UNSUPPORTED is returned.
1057 //
1058 if (*Data > RShiftU64 (MAX_UINT64 - InternalHexCharToUintn (*String), 4)) {
1059 *Data = MAX_UINT64;
1060 if (EndPointer != NULL) {
1061 *EndPointer = (CHAR16 *) String;
1062 }
1063 return RETURN_UNSUPPORTED;
1064 }
1065
1066 *Data = LShiftU64 (*Data, 4) + InternalHexCharToUintn (*String);
1067 String++;
1068 }
1069
1070 if (EndPointer != NULL) {
1071 *EndPointer = (CHAR16 *) String;
1072 }
1073 return RETURN_SUCCESS;
1074 }
1075
1076 /**
1077 Convert a Null-terminated Unicode string to IPv6 address and prefix length.
1078
1079 This function outputs a value of type IPv6_ADDRESS and may output a value
1080 of type UINT8 by interpreting the contents of the Unicode string specified
1081 by String. The format of the input Unicode string String is as follows:
1082
1083 X:X:X:X:X:X:X:X[/P]
1084
1085 X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and
1086 [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low
1087 memory address and high byte is stored in high memory address. P contains decimal
1088 digit characters in the range [0-9]. The running zero in the beginning of P will
1089 be ignored. /P is optional.
1090
1091 When /P is not in the String, the function stops at the first character that is
1092 not a valid hexadecimal digit character after eight X's are converted.
1093
1094 When /P is in the String, the function stops at the first character that is not
1095 a valid decimal digit character after P is converted.
1096
1097 "::" can be used to compress one or more groups of X when X contains only 0.
1098 The "::" can only appear once in the String.
1099
1100 If String is NULL, then ASSERT().
1101
1102 If Address is NULL, then ASSERT().
1103
1104 If String is not aligned in a 16-bit boundary, then ASSERT().
1105
1106 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1107 PcdMaximumUnicodeStringLength Unicode characters, not including the
1108 Null-terminator, then ASSERT().
1109
1110 If EndPointer is not NULL and Address is translated from String, a pointer
1111 to the character that stopped the scan is stored at the location pointed to
1112 by EndPointer.
1113
1114 @param String Pointer to a Null-terminated Unicode string.
1115 @param EndPointer Pointer to character that stops scan.
1116 @param Address Pointer to the converted IPv6 address.
1117 @param PrefixLength Pointer to the converted IPv6 address prefix
1118 length. MAX_UINT8 is returned when /P is
1119 not in the String.
1120
1121 @retval RETURN_SUCCESS Address is translated from String.
1122 @retval RETURN_INVALID_PARAMETER If String is NULL.
1123 If Data is NULL.
1124 @retval RETURN_UNSUPPORTED If X contains more than four hexadecimal
1125 digit characters.
1126 If String contains "::" and number of X
1127 is not less than 8.
1128 If P starts with character that is not a
1129 valid decimal digit character.
1130 If the decimal number converted from P
1131 exceeds 128.
1132
1133 **/
1134 RETURN_STATUS
1135 EFIAPI
1136 StrToIpv6Address (
1137 IN CONST CHAR16 *String,
1138 OUT CHAR16 **EndPointer, OPTIONAL
1139 OUT IPv6_ADDRESS *Address,
1140 OUT UINT8 *PrefixLength OPTIONAL
1141 )
1142 {
1143 RETURN_STATUS Status;
1144 UINTN AddressIndex;
1145 UINTN Uintn;
1146 IPv6_ADDRESS LocalAddress;
1147 UINT8 LocalPrefixLength;
1148 CONST CHAR16 *Pointer;
1149 CHAR16 *End;
1150 UINTN CompressStart;
1151 BOOLEAN ExpectPrefix;
1152
1153 LocalPrefixLength = MAX_UINT8;
1154 CompressStart = ARRAY_SIZE (Address->Addr);
1155 ExpectPrefix = FALSE;
1156
1157 ASSERT (((UINTN) String & BIT0) == 0);
1158
1159 //
1160 // 1. None of String or Guid shall be a null pointer.
1161 //
1162 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
1163 SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
1164
1165 for (Pointer = String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
1166 if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {
1167 if (*Pointer != L':') {
1168 //
1169 // ":" or "/" should be followed by digit characters.
1170 //
1171 return RETURN_UNSUPPORTED;
1172 }
1173
1174 //
1175 // Meet second ":" after previous ":" or "/"
1176 // or meet first ":" in the beginning of String.
1177 //
1178 if (ExpectPrefix) {
1179 //
1180 // ":" shall not be after "/"
1181 //
1182 return RETURN_UNSUPPORTED;
1183 }
1184
1185 if (CompressStart != ARRAY_SIZE (Address->Addr) || AddressIndex == ARRAY_SIZE (Address->Addr)) {
1186 //
1187 // "::" can only appear once.
1188 // "::" can only appear when address is not full length.
1189 //
1190 return RETURN_UNSUPPORTED;
1191 } else {
1192 //
1193 // Remember the start of zero compressing.
1194 //
1195 CompressStart = AddressIndex;
1196 Pointer++;
1197
1198 if (CompressStart == 0) {
1199 if (*Pointer != L':') {
1200 //
1201 // Single ":" shall not be in the beginning of String.
1202 //
1203 return RETURN_UNSUPPORTED;
1204 }
1205 Pointer++;
1206 }
1207 }
1208 }
1209
1210 if (!InternalIsHexaDecimalDigitCharacter (*Pointer)) {
1211 if (*Pointer == L'/') {
1212 //
1213 // Might be optional "/P" after "::".
1214 //
1215 if (CompressStart != AddressIndex) {
1216 return RETURN_UNSUPPORTED;
1217 }
1218 } else {
1219 break;
1220 }
1221 } else {
1222 if (!ExpectPrefix) {
1223 //
1224 // Get X.
1225 //
1226 Status = StrHexToUintnS (Pointer, &End, &Uintn);
1227 if (RETURN_ERROR (Status) || End - Pointer > 4) {
1228 //
1229 // Number of hexadecimal digit characters is no more than 4.
1230 //
1231 return RETURN_UNSUPPORTED;
1232 }
1233 Pointer = End;
1234 //
1235 // Uintn won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.
1236 //
1237 LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uintn >> 8);
1238 LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uintn;
1239 AddressIndex += 2;
1240 } else {
1241 //
1242 // Get P, then exit the loop.
1243 //
1244 Status = StrDecimalToUintnS (Pointer, &End, &Uintn);
1245 if (RETURN_ERROR (Status) || End == Pointer || Uintn > 128) {
1246 //
1247 // Prefix length should not exceed 128.
1248 //
1249 return RETURN_UNSUPPORTED;
1250 }
1251 LocalPrefixLength = (UINT8) Uintn;
1252 Pointer = End;
1253 break;
1254 }
1255 }
1256
1257 //
1258 // Skip ':' or "/"
1259 //
1260 if (*Pointer == L'/') {
1261 ExpectPrefix = TRUE;
1262 } else if (*Pointer == L':') {
1263 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
1264 //
1265 // Meet additional ":" after all 8 16-bit address
1266 //
1267 break;
1268 }
1269 } else {
1270 //
1271 // Meet other character that is not "/" or ":" after all 8 16-bit address
1272 //
1273 break;
1274 }
1275 Pointer++;
1276 }
1277
1278 if ((AddressIndex == ARRAY_SIZE (Address->Addr) && CompressStart != ARRAY_SIZE (Address->Addr)) ||
1279 (AddressIndex != ARRAY_SIZE (Address->Addr) && CompressStart == ARRAY_SIZE (Address->Addr))
1280 ) {
1281 //
1282 // Full length of address shall not have compressing zeros.
1283 // Non-full length of address shall have compressing zeros.
1284 //
1285 return RETURN_UNSUPPORTED;
1286 }
1287 CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);
1288 ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);
1289 CopyMem (
1290 &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],
1291 &LocalAddress.Addr[CompressStart],
1292 AddressIndex - CompressStart
1293 );
1294
1295 if (PrefixLength != NULL) {
1296 *PrefixLength = LocalPrefixLength;
1297 }
1298 if (EndPointer != NULL) {
1299 *EndPointer = (CHAR16 *) Pointer;
1300 }
1301
1302 return RETURN_SUCCESS;
1303 }
1304
1305 /**
1306 Convert a Null-terminated Unicode string to IPv4 address and prefix length.
1307
1308 This function outputs a value of type IPv4_ADDRESS and may output a value
1309 of type UINT8 by interpreting the contents of the Unicode string specified
1310 by String. The format of the input Unicode string String is as follows:
1311
1312 D.D.D.D[/P]
1313
1314 D and P are decimal digit characters in the range [0-9]. The running zero in
1315 the beginning of D and P will be ignored. /P is optional.
1316
1317 When /P is not in the String, the function stops at the first character that is
1318 not a valid decimal digit character after four D's are converted.
1319
1320 When /P is in the String, the function stops at the first character that is not
1321 a valid decimal digit character after P is converted.
1322
1323 If String is NULL, then ASSERT().
1324
1325 If Address is NULL, then ASSERT().
1326
1327 If String is not aligned in a 16-bit boundary, then ASSERT().
1328
1329 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
1330 PcdMaximumUnicodeStringLength Unicode characters, not including the
1331 Null-terminator, then ASSERT().
1332
1333 If EndPointer is not NULL and Address is translated from String, a pointer
1334 to the character that stopped the scan is stored at the location pointed to
1335 by EndPointer.
1336
1337 @param String Pointer to a Null-terminated Unicode string.
1338 @param EndPointer Pointer to character that stops scan.
1339 @param Address Pointer to the converted IPv4 address.
1340 @param PrefixLength Pointer to the converted IPv4 address prefix
1341 length. MAX_UINT8 is returned when /P is
1342 not in the String.
1343
1344 @retval RETURN_SUCCESS Address is translated from String.
1345 @retval RETURN_INVALID_PARAMETER If String is NULL.
1346 If Data is NULL.
1347 @retval RETURN_UNSUPPORTED If String is not in the correct format.
1348 If any decimal number converted from D
1349 exceeds 255.
1350 If the decimal number converted from P
1351 exceeds 32.
1352
1353 **/
1354 RETURN_STATUS
1355 EFIAPI
1356 StrToIpv4Address (
1357 IN CONST CHAR16 *String,
1358 OUT CHAR16 **EndPointer, OPTIONAL
1359 OUT IPv4_ADDRESS *Address,
1360 OUT UINT8 *PrefixLength OPTIONAL
1361 )
1362 {
1363 RETURN_STATUS Status;
1364 UINTN AddressIndex;
1365 UINTN Uintn;
1366 IPv4_ADDRESS LocalAddress;
1367 UINT8 LocalPrefixLength;
1368 CHAR16 *Pointer;
1369
1370 LocalPrefixLength = MAX_UINT8;
1371
1372 ASSERT (((UINTN) String & BIT0) == 0);
1373
1374 //
1375 // 1. None of String or Guid shall be a null pointer.
1376 //
1377 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
1378 SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
1379
1380 for (Pointer = (CHAR16 *) String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
1381 if (!InternalIsDecimalDigitCharacter (*Pointer)) {
1382 //
1383 // D or P contains invalid characters.
1384 //
1385 break;
1386 }
1387
1388 //
1389 // Get D or P.
1390 //
1391 Status = StrDecimalToUintnS ((CONST CHAR16 *) Pointer, &Pointer, &Uintn);
1392 if (RETURN_ERROR (Status)) {
1393 return RETURN_UNSUPPORTED;
1394 }
1395 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
1396 //
1397 // It's P.
1398 //
1399 if (Uintn > 32) {
1400 return RETURN_UNSUPPORTED;
1401 }
1402 LocalPrefixLength = (UINT8) Uintn;
1403 } else {
1404 //
1405 // It's D.
1406 //
1407 if (Uintn > MAX_UINT8) {
1408 return RETURN_UNSUPPORTED;
1409 }
1410 LocalAddress.Addr[AddressIndex] = (UINT8) Uintn;
1411 AddressIndex++;
1412 }
1413
1414 //
1415 // Check the '.' or '/', depending on the AddressIndex.
1416 //
1417 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
1418 if (*Pointer == L'/') {
1419 //
1420 // '/P' is in the String.
1421 // Skip "/" and get P in next loop.
1422 //
1423 Pointer++;
1424 } else {
1425 //
1426 // '/P' is not in the String.
1427 //
1428 break;
1429 }
1430 } else if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
1431 if (*Pointer == L'.') {
1432 //
1433 // D should be followed by '.'
1434 //
1435 Pointer++;
1436 } else {
1437 return RETURN_UNSUPPORTED;
1438 }
1439 }
1440 }
1441
1442 if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
1443 return RETURN_UNSUPPORTED;
1444 }
1445
1446 CopyMem (Address, &LocalAddress, sizeof (*Address));
1447 if (PrefixLength != NULL) {
1448 *PrefixLength = LocalPrefixLength;
1449 }
1450 if (EndPointer != NULL) {
1451 *EndPointer = Pointer;
1452 }
1453
1454 return RETURN_SUCCESS;
1455 }
1456
1457 /**
1458 Convert a Null-terminated Unicode GUID string to a value of type
1459 EFI_GUID.
1460
1461 This function outputs a GUID value by interpreting the contents of
1462 the Unicode string specified by String. The format of the input
1463 Unicode string String consists of 36 characters, as follows:
1464
1465 aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1466
1467 The pairs aa - pp are two characters in the range [0-9], [a-f] and
1468 [A-F], with each pair representing a single byte hexadecimal value.
1469
1470 The mapping between String and the EFI_GUID structure is as follows:
1471 aa Data1[24:31]
1472 bb Data1[16:23]
1473 cc Data1[8:15]
1474 dd Data1[0:7]
1475 ee Data2[8:15]
1476 ff Data2[0:7]
1477 gg Data3[8:15]
1478 hh Data3[0:7]
1479 ii Data4[0:7]
1480 jj Data4[8:15]
1481 kk Data4[16:23]
1482 ll Data4[24:31]
1483 mm Data4[32:39]
1484 nn Data4[40:47]
1485 oo Data4[48:55]
1486 pp Data4[56:63]
1487
1488 If String is NULL, then ASSERT().
1489 If Guid is NULL, then ASSERT().
1490 If String is not aligned in a 16-bit boundary, then ASSERT().
1491
1492 @param String Pointer to a Null-terminated Unicode string.
1493 @param Guid Pointer to the converted GUID.
1494
1495 @retval RETURN_SUCCESS Guid is translated from String.
1496 @retval RETURN_INVALID_PARAMETER If String is NULL.
1497 If Data is NULL.
1498 @retval RETURN_UNSUPPORTED If String is not as the above format.
1499
1500 **/
1501 RETURN_STATUS
1502 EFIAPI
1503 StrToGuid (
1504 IN CONST CHAR16 *String,
1505 OUT GUID *Guid
1506 )
1507 {
1508 RETURN_STATUS Status;
1509 GUID LocalGuid;
1510
1511 ASSERT (((UINTN) String & BIT0) == 0);
1512
1513 //
1514 // 1. None of String or Guid shall be a null pointer.
1515 //
1516 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
1517 SAFE_STRING_CONSTRAINT_CHECK ((Guid != NULL), RETURN_INVALID_PARAMETER);
1518
1519 //
1520 // Get aabbccdd in big-endian.
1521 //
1522 Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data1), (UINT8 *) &LocalGuid.Data1, sizeof (LocalGuid.Data1));
1523 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data1)] != L'-') {
1524 return RETURN_UNSUPPORTED;
1525 }
1526 //
1527 // Convert big-endian to little-endian.
1528 //
1529 LocalGuid.Data1 = SwapBytes32 (LocalGuid.Data1);
1530 String += 2 * sizeof (LocalGuid.Data1) + 1;
1531
1532 //
1533 // Get eeff in big-endian.
1534 //
1535 Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data2), (UINT8 *) &LocalGuid.Data2, sizeof (LocalGuid.Data2));
1536 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data2)] != L'-') {
1537 return RETURN_UNSUPPORTED;
1538 }
1539 //
1540 // Convert big-endian to little-endian.
1541 //
1542 LocalGuid.Data2 = SwapBytes16 (LocalGuid.Data2);
1543 String += 2 * sizeof (LocalGuid.Data2) + 1;
1544
1545 //
1546 // Get gghh in big-endian.
1547 //
1548 Status = StrHexToBytes (String, 2 * sizeof (LocalGuid.Data3), (UINT8 *) &LocalGuid.Data3, sizeof (LocalGuid.Data3));
1549 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data3)] != L'-') {
1550 return RETURN_UNSUPPORTED;
1551 }
1552 //
1553 // Convert big-endian to little-endian.
1554 //
1555 LocalGuid.Data3 = SwapBytes16 (LocalGuid.Data3);
1556 String += 2 * sizeof (LocalGuid.Data3) + 1;
1557
1558 //
1559 // Get iijj.
1560 //
1561 Status = StrHexToBytes (String, 2 * 2, &LocalGuid.Data4[0], 2);
1562 if (RETURN_ERROR (Status) || String[2 * 2] != L'-') {
1563 return RETURN_UNSUPPORTED;
1564 }
1565 String += 2 * 2 + 1;
1566
1567 //
1568 // Get kkllmmnnoopp.
1569 //
1570 Status = StrHexToBytes (String, 2 * 6, &LocalGuid.Data4[2], 6);
1571 if (RETURN_ERROR (Status)) {
1572 return RETURN_UNSUPPORTED;
1573 }
1574
1575 CopyGuid (Guid, &LocalGuid);
1576 return RETURN_SUCCESS;
1577 }
1578
1579 /**
1580 Convert a Null-terminated Unicode hexadecimal string to a byte array.
1581
1582 This function outputs a byte array by interpreting the contents of
1583 the Unicode string specified by String in hexadecimal format. The format of
1584 the input Unicode string String is:
1585
1586 [XX]*
1587
1588 X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].
1589 The function decodes every two hexadecimal digit characters as one byte. The
1590 decoding stops after Length of characters and outputs Buffer containing
1591 (Length / 2) bytes.
1592
1593 If String is not aligned in a 16-bit boundary, then ASSERT().
1594
1595 If String is NULL, then ASSERT().
1596
1597 If Buffer is NULL, then ASSERT().
1598
1599 If Length is not multiple of 2, then ASSERT().
1600
1601 If PcdMaximumUnicodeStringLength is not zero and Length is greater than
1602 PcdMaximumUnicodeStringLength, then ASSERT().
1603
1604 If MaxBufferSize is less than (Length / 2), then ASSERT().
1605
1606 @param String Pointer to a Null-terminated Unicode string.
1607 @param Length The number of Unicode characters to decode.
1608 @param Buffer Pointer to the converted bytes array.
1609 @param MaxBufferSize The maximum size of Buffer.
1610
1611 @retval RETURN_SUCCESS Buffer is translated from String.
1612 @retval RETURN_INVALID_PARAMETER If String is NULL.
1613 If Data is NULL.
1614 If Length is not multiple of 2.
1615 If PcdMaximumUnicodeStringLength is not zero,
1616 and Length is greater than
1617 PcdMaximumUnicodeStringLength.
1618 @retval RETURN_UNSUPPORTED If Length of characters from String contain
1619 a character that is not valid hexadecimal
1620 digit characters, or a Null-terminator.
1621 @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2).
1622 **/
1623 RETURN_STATUS
1624 EFIAPI
1625 StrHexToBytes (
1626 IN CONST CHAR16 *String,
1627 IN UINTN Length,
1628 OUT UINT8 *Buffer,
1629 IN UINTN MaxBufferSize
1630 )
1631 {
1632 UINTN Index;
1633
1634 ASSERT (((UINTN) String & BIT0) == 0);
1635
1636 //
1637 // 1. None of String or Buffer shall be a null pointer.
1638 //
1639 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
1640 SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);
1641
1642 //
1643 // 2. Length shall not be greater than RSIZE_MAX.
1644 //
1645 if (RSIZE_MAX != 0) {
1646 SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
1647 }
1648
1649 //
1650 // 3. Length shall not be odd.
1651 //
1652 SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER);
1653
1654 //
1655 // 4. MaxBufferSize shall equal to or greater than Length / 2.
1656 //
1657 SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL);
1658
1659 //
1660 // 5. String shall not contains invalid hexadecimal digits.
1661 //
1662 for (Index = 0; Index < Length; Index++) {
1663 if (!InternalIsHexaDecimalDigitCharacter (String[Index])) {
1664 break;
1665 }
1666 }
1667 if (Index != Length) {
1668 return RETURN_UNSUPPORTED;
1669 }
1670
1671 //
1672 // Convert the hex string to bytes.
1673 //
1674 for(Index = 0; Index < Length; Index++) {
1675
1676 //
1677 // For even characters, write the upper nibble for each buffer byte,
1678 // and for even characters, the lower nibble.
1679 //
1680 if ((Index & BIT0) == 0) {
1681 Buffer[Index / 2] = (UINT8) InternalHexCharToUintn (String[Index]) << 4;
1682 } else {
1683 Buffer[Index / 2] |= (UINT8) InternalHexCharToUintn (String[Index]);
1684 }
1685 }
1686 return RETURN_SUCCESS;
1687 }
1688
1689 /**
1690 Returns the length of a Null-terminated Ascii string.
1691
1692 This function is similar as strlen_s defined in C11.
1693
1694 @param String A pointer to a Null-terminated Ascii string.
1695 @param MaxSize The maximum number of Destination Ascii
1696 char, including terminating null char.
1697
1698 @retval 0 If String is NULL.
1699 @retval MaxSize If there is no null character in the first MaxSize characters of String.
1700 @return The number of characters that percede the terminating null character.
1701
1702 **/
1703 UINTN
1704 EFIAPI
1705 AsciiStrnLenS (
1706 IN CONST CHAR8 *String,
1707 IN UINTN MaxSize
1708 )
1709 {
1710 UINTN Length;
1711
1712 //
1713 // If String is a null pointer or MaxSize is 0, then the AsciiStrnLenS function returns zero.
1714 //
1715 if ((String == NULL) || (MaxSize == 0)) {
1716 return 0;
1717 }
1718
1719 //
1720 // Otherwise, the AsciiStrnLenS function returns the number of characters that precede the
1721 // terminating null character. If there is no null character in the first MaxSize characters of
1722 // String then AsciiStrnLenS returns MaxSize. At most the first MaxSize characters of String shall
1723 // be accessed by AsciiStrnLenS.
1724 //
1725 Length = 0;
1726 while (String[Length] != 0) {
1727 if (Length >= MaxSize - 1) {
1728 return MaxSize;
1729 }
1730 Length++;
1731 }
1732 return Length;
1733 }
1734
1735 /**
1736 Returns the size of a Null-terminated Ascii string in bytes, including the
1737 Null terminator.
1738
1739 This function returns the size of the Null-terminated Ascii string specified
1740 by String in bytes, including the Null terminator.
1741
1742 @param String A pointer to a Null-terminated Ascii string.
1743 @param MaxSize The maximum number of Destination Ascii
1744 char, including the Null terminator.
1745
1746 @retval 0 If String is NULL.
1747 @retval (sizeof (CHAR8) * (MaxSize + 1))
1748 If there is no Null terminator in the first MaxSize characters of
1749 String.
1750 @return The size of the Null-terminated Ascii string in bytes, including the
1751 Null terminator.
1752
1753 **/
1754 UINTN
1755 EFIAPI
1756 AsciiStrnSizeS (
1757 IN CONST CHAR8 *String,
1758 IN UINTN MaxSize
1759 )
1760 {
1761 //
1762 // If String is a null pointer, then the AsciiStrnSizeS function returns
1763 // zero.
1764 //
1765 if (String == NULL) {
1766 return 0;
1767 }
1768
1769 //
1770 // Otherwise, the AsciiStrnSizeS function returns the size of the
1771 // Null-terminated Ascii string in bytes, including the Null terminator. If
1772 // there is no Null terminator in the first MaxSize characters of String,
1773 // then AsciiStrnSizeS returns (sizeof (CHAR8) * (MaxSize + 1)) to keep a
1774 // consistent map with the AsciiStrnLenS function.
1775 //
1776 return (AsciiStrnLenS (String, MaxSize) + 1) * sizeof (*String);
1777 }
1778
1779 /**
1780 Copies the string pointed to by Source (including the terminating null char)
1781 to the array pointed to by Destination.
1782
1783 This function is similar as strcpy_s defined in C11.
1784
1785 If an error would be returned, then the function will also ASSERT().
1786
1787 If an error is returned, then the Destination is unmodified.
1788
1789 @param Destination A pointer to a Null-terminated Ascii string.
1790 @param DestMax The maximum number of Destination Ascii
1791 char, including terminating null char.
1792 @param Source A pointer to a Null-terminated Ascii string.
1793
1794 @retval RETURN_SUCCESS String is copied.
1795 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
1796 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1797 If Source is NULL.
1798 If PcdMaximumAsciiStringLength is not zero,
1799 and DestMax is greater than
1800 PcdMaximumAsciiStringLength.
1801 If DestMax is 0.
1802 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1803 **/
1804 RETURN_STATUS
1805 EFIAPI
1806 AsciiStrCpyS (
1807 OUT CHAR8 *Destination,
1808 IN UINTN DestMax,
1809 IN CONST CHAR8 *Source
1810 )
1811 {
1812 UINTN SourceLen;
1813
1814 //
1815 // 1. Neither Destination nor Source shall be a null pointer.
1816 //
1817 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
1818 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
1819
1820 //
1821 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX.
1822 //
1823 if (ASCII_RSIZE_MAX != 0) {
1824 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
1825 }
1826
1827 //
1828 // 3. DestMax shall not equal zero.
1829 //
1830 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
1831
1832 //
1833 // 4. DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
1834 //
1835 SourceLen = AsciiStrnLenS (Source, DestMax);
1836 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
1837
1838 //
1839 // 5. Copying shall not take place between objects that overlap.
1840 //
1841 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
1842
1843 //
1844 // The AsciiStrCpyS function copies the string pointed to by Source (including the terminating
1845 // null character) into the array pointed to by Destination.
1846 //
1847 while (*Source != 0) {
1848 *(Destination++) = *(Source++);
1849 }
1850 *Destination = 0;
1851
1852 return RETURN_SUCCESS;
1853 }
1854
1855 /**
1856 Copies not more than Length successive char from the string pointed to by
1857 Source to the array pointed to by Destination. If no null char is copied from
1858 Source, then Destination[Length] is always set to null.
1859
1860 This function is similar as strncpy_s defined in C11.
1861
1862 If an error would be returned, then the function will also ASSERT().
1863
1864 If an error is returned, then the Destination is unmodified.
1865
1866 @param Destination A pointer to a Null-terminated Ascii string.
1867 @param DestMax The maximum number of Destination Ascii
1868 char, including terminating null char.
1869 @param Source A pointer to a Null-terminated Ascii string.
1870 @param Length The maximum number of Ascii characters to copy.
1871
1872 @retval RETURN_SUCCESS String is copied.
1873 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than
1874 MIN(StrLen(Source), Length).
1875 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1876 If Source is NULL.
1877 If PcdMaximumAsciiStringLength is not zero,
1878 and DestMax is greater than
1879 PcdMaximumAsciiStringLength.
1880 If DestMax is 0.
1881 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1882 **/
1883 RETURN_STATUS
1884 EFIAPI
1885 AsciiStrnCpyS (
1886 OUT CHAR8 *Destination,
1887 IN UINTN DestMax,
1888 IN CONST CHAR8 *Source,
1889 IN UINTN Length
1890 )
1891 {
1892 UINTN SourceLen;
1893
1894 //
1895 // 1. Neither Destination nor Source shall be a null pointer.
1896 //
1897 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
1898 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
1899
1900 //
1901 // 2. Neither DestMax nor Length shall be greater than ASCII_RSIZE_MAX
1902 //
1903 if (ASCII_RSIZE_MAX != 0) {
1904 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
1905 SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
1906 }
1907
1908 //
1909 // 3. DestMax shall not equal zero.
1910 //
1911 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
1912
1913 //
1914 // 4. If Length is not less than DestMax, then DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
1915 //
1916 SourceLen = AsciiStrnLenS (Source, DestMax);
1917 if (Length >= DestMax) {
1918 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
1919 }
1920
1921 //
1922 // 5. Copying shall not take place between objects that overlap.
1923 //
1924 if (SourceLen > Length) {
1925 SourceLen = Length;
1926 }
1927 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
1928
1929 //
1930 // The AsciiStrnCpyS function copies not more than Length successive characters (characters that
1931 // follow a null character are not copied) from the array pointed to by Source to the array
1932 // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null
1933 // character.
1934 //
1935 while ((*Source != 0) && (SourceLen > 0)) {
1936 *(Destination++) = *(Source++);
1937 SourceLen--;
1938 }
1939 *Destination = 0;
1940
1941 return RETURN_SUCCESS;
1942 }
1943
1944 /**
1945 Appends a copy of the string pointed to by Source (including the terminating
1946 null char) to the end of the string pointed to by Destination.
1947
1948 This function is similar as strcat_s defined in C11.
1949
1950 If an error would be returned, then the function will also ASSERT().
1951
1952 If an error is returned, then the Destination is unmodified.
1953
1954 @param Destination A pointer to a Null-terminated Ascii string.
1955 @param DestMax The maximum number of Destination Ascii
1956 char, including terminating null char.
1957 @param Source A pointer to a Null-terminated Ascii string.
1958
1959 @retval RETURN_SUCCESS String is appended.
1960 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
1961 StrLen(Destination).
1962 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
1963 greater than StrLen(Source).
1964 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1965 If Source is NULL.
1966 If PcdMaximumAsciiStringLength is not zero,
1967 and DestMax is greater than
1968 PcdMaximumAsciiStringLength.
1969 If DestMax is 0.
1970 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1971 **/
1972 RETURN_STATUS
1973 EFIAPI
1974 AsciiStrCatS (
1975 IN OUT CHAR8 *Destination,
1976 IN UINTN DestMax,
1977 IN CONST CHAR8 *Source
1978 )
1979 {
1980 UINTN DestLen;
1981 UINTN CopyLen;
1982 UINTN SourceLen;
1983
1984 //
1985 // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrCatS.
1986 //
1987 DestLen = AsciiStrnLenS (Destination, DestMax);
1988 CopyLen = DestMax - DestLen;
1989
1990 //
1991 // 1. Neither Destination nor Source shall be a null pointer.
1992 //
1993 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
1994 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
1995
1996 //
1997 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX.
1998 //
1999 if (ASCII_RSIZE_MAX != 0) {
2000 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2001 }
2002
2003 //
2004 // 3. DestMax shall not equal zero.
2005 //
2006 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
2007
2008 //
2009 // 4. CopyLen shall not equal zero.
2010 //
2011 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen != 0), RETURN_BAD_BUFFER_SIZE);
2012
2013 //
2014 // 5. CopyLen shall be greater than AsciiStrnLenS(Source, CopyLen).
2015 //
2016 SourceLen = AsciiStrnLenS (Source, CopyLen);
2017 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);
2018
2019 //
2020 // 6. Copying shall not take place between objects that overlap.
2021 //
2022 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
2023
2024 //
2025 // The AsciiStrCatS function appends a copy of the string pointed to by Source (including the
2026 // terminating null character) to the end of the string pointed to by Destination. The initial character
2027 // from Source overwrites the null character at the end of Destination.
2028 //
2029 Destination = Destination + DestLen;
2030 while (*Source != 0) {
2031 *(Destination++) = *(Source++);
2032 }
2033 *Destination = 0;
2034
2035 return RETURN_SUCCESS;
2036 }
2037
2038 /**
2039 Appends not more than Length successive char from the string pointed to by
2040 Source to the end of the string pointed to by Destination. If no null char is
2041 copied from Source, then Destination[StrLen(Destination) + Length] is always
2042 set to null.
2043
2044 This function is similar as strncat_s defined in C11.
2045
2046 If an error would be returned, then the function will also ASSERT().
2047
2048 If an error is returned, then the Destination is unmodified.
2049
2050 @param Destination A pointer to a Null-terminated Ascii string.
2051 @param DestMax The maximum number of Destination Ascii
2052 char, including terminating null char.
2053 @param Source A pointer to a Null-terminated Ascii string.
2054 @param Length The maximum number of Ascii characters to copy.
2055
2056 @retval RETURN_SUCCESS String is appended.
2057 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
2058 StrLen(Destination).
2059 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
2060 greater than MIN(StrLen(Source), Length).
2061 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2062 If Source is NULL.
2063 If PcdMaximumAsciiStringLength is not zero,
2064 and DestMax is greater than
2065 PcdMaximumAsciiStringLength.
2066 If DestMax is 0.
2067 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2068 **/
2069 RETURN_STATUS
2070 EFIAPI
2071 AsciiStrnCatS (
2072 IN OUT CHAR8 *Destination,
2073 IN UINTN DestMax,
2074 IN CONST CHAR8 *Source,
2075 IN UINTN Length
2076 )
2077 {
2078 UINTN DestLen;
2079 UINTN CopyLen;
2080 UINTN SourceLen;
2081
2082 //
2083 // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrnCatS.
2084 //
2085 DestLen = AsciiStrnLenS (Destination, DestMax);
2086 CopyLen = DestMax - DestLen;
2087
2088 //
2089 // 1. Neither Destination nor Source shall be a null pointer.
2090 //
2091 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
2092 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
2093
2094 //
2095 // 2. Neither DestMax nor Length shall be greater than ASCII_RSIZE_MAX.
2096 //
2097 if (ASCII_RSIZE_MAX != 0) {
2098 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2099 SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2100 }
2101
2102 //
2103 // 3. DestMax shall not equal zero.
2104 //
2105 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
2106
2107 //
2108 // 4. CopyLen shall not equal zero.
2109 //
2110 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen != 0), RETURN_BAD_BUFFER_SIZE);
2111
2112 //
2113 // 5. If Length is not less than CopyLen, then CopyLen shall be greater than AsciiStrnLenS(Source, CopyLen).
2114 //
2115 SourceLen = AsciiStrnLenS (Source, CopyLen);
2116 if (Length >= CopyLen) {
2117 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen > SourceLen), RETURN_BUFFER_TOO_SMALL);
2118 }
2119
2120 //
2121 // 6. Copying shall not take place between objects that overlap.
2122 //
2123 if (SourceLen > Length) {
2124 SourceLen = Length;
2125 }
2126 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination, DestMax, (CHAR8 *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
2127
2128 //
2129 // The AsciiStrnCatS function appends not more than Length successive characters (characters
2130 // that follow a null character are not copied) from the array pointed to by Source to the end of
2131 // the string pointed to by Destination. The initial character from Source overwrites the null character at
2132 // the end of Destination. If no null character was copied from Source, then Destination[DestMax-CopyLen+Length] is set to
2133 // a null character.
2134 //
2135 Destination = Destination + DestLen;
2136 while ((*Source != 0) && (SourceLen > 0)) {
2137 *(Destination++) = *(Source++);
2138 SourceLen--;
2139 }
2140 *Destination = 0;
2141
2142 return RETURN_SUCCESS;
2143 }
2144
2145 /**
2146 Convert a Null-terminated Ascii decimal string to a value of type UINTN.
2147
2148 This function outputs a value of type UINTN by interpreting the contents of
2149 the Ascii string specified by String as a decimal number. The format of the
2150 input Ascii string String is:
2151
2152 [spaces] [decimal digits].
2153
2154 The valid decimal digit character is in the range [0-9]. The function will
2155 ignore the pad space, which includes spaces or tab characters, before
2156 [decimal digits]. The running zero in the beginning of [decimal digits] will
2157 be ignored. Then, the function stops at the first character that is a not a
2158 valid decimal character or a Null-terminator, whichever one comes first.
2159
2160 If String is NULL, then ASSERT().
2161 If Data is NULL, then ASSERT().
2162 If PcdMaximumAsciiStringLength is not zero, and String contains more than
2163 PcdMaximumAsciiStringLength Ascii characters, not including the
2164 Null-terminator, then ASSERT().
2165
2166 If String has no valid decimal digits in the above format, then 0 is stored
2167 at the location pointed to by Data.
2168 If the number represented by String exceeds the range defined by UINTN, then
2169 MAX_UINTN is stored at the location pointed to by Data.
2170
2171 If EndPointer is not NULL, a pointer to the character that stopped the scan
2172 is stored at the location pointed to by EndPointer. If String has no valid
2173 decimal digits right after the optional pad spaces, the value of String is
2174 stored at the location pointed to by EndPointer.
2175
2176 @param String Pointer to a Null-terminated Ascii string.
2177 @param EndPointer Pointer to character that stops scan.
2178 @param Data Pointer to the converted value.
2179
2180 @retval RETURN_SUCCESS Value is translated from String.
2181 @retval RETURN_INVALID_PARAMETER If String is NULL.
2182 If Data is NULL.
2183 If PcdMaximumAsciiStringLength is not zero,
2184 and String contains more than
2185 PcdMaximumAsciiStringLength Ascii
2186 characters, not including the
2187 Null-terminator.
2188 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
2189 the range defined by UINTN.
2190
2191 **/
2192 RETURN_STATUS
2193 EFIAPI
2194 AsciiStrDecimalToUintnS (
2195 IN CONST CHAR8 *String,
2196 OUT CHAR8 **EndPointer, OPTIONAL
2197 OUT UINTN *Data
2198 )
2199 {
2200 //
2201 // 1. Neither String nor Data shall be a null pointer.
2202 //
2203 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
2204 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
2205
2206 //
2207 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
2208 //
2209 if (ASCII_RSIZE_MAX != 0) {
2210 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2211 }
2212
2213 if (EndPointer != NULL) {
2214 *EndPointer = (CHAR8 *) String;
2215 }
2216
2217 //
2218 // Ignore the pad spaces (space or tab)
2219 //
2220 while ((*String == ' ') || (*String == '\t')) {
2221 String++;
2222 }
2223
2224 //
2225 // Ignore leading Zeros after the spaces
2226 //
2227 while (*String == '0') {
2228 String++;
2229 }
2230
2231 *Data = 0;
2232
2233 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
2234 //
2235 // If the number represented by String overflows according to the range
2236 // defined by UINTN, then MAX_UINTN is stored in *Data and
2237 // RETURN_UNSUPPORTED is returned.
2238 //
2239 if (*Data > ((MAX_UINTN - (*String - '0')) / 10)) {
2240 *Data = MAX_UINTN;
2241 if (EndPointer != NULL) {
2242 *EndPointer = (CHAR8 *) String;
2243 }
2244 return RETURN_UNSUPPORTED;
2245 }
2246
2247 *Data = *Data * 10 + (*String - '0');
2248 String++;
2249 }
2250
2251 if (EndPointer != NULL) {
2252 *EndPointer = (CHAR8 *) String;
2253 }
2254 return RETURN_SUCCESS;
2255 }
2256
2257 /**
2258 Convert a Null-terminated Ascii decimal string to a value of type UINT64.
2259
2260 This function outputs a value of type UINT64 by interpreting the contents of
2261 the Ascii string specified by String as a decimal number. The format of the
2262 input Ascii string String is:
2263
2264 [spaces] [decimal digits].
2265
2266 The valid decimal digit character is in the range [0-9]. The function will
2267 ignore the pad space, which includes spaces or tab characters, before
2268 [decimal digits]. The running zero in the beginning of [decimal digits] will
2269 be ignored. Then, the function stops at the first character that is a not a
2270 valid decimal character or a Null-terminator, whichever one comes first.
2271
2272 If String is NULL, then ASSERT().
2273 If Data is NULL, then ASSERT().
2274 If PcdMaximumAsciiStringLength is not zero, and String contains more than
2275 PcdMaximumAsciiStringLength Ascii characters, not including the
2276 Null-terminator, then ASSERT().
2277
2278 If String has no valid decimal digits in the above format, then 0 is stored
2279 at the location pointed to by Data.
2280 If the number represented by String exceeds the range defined by UINT64, then
2281 MAX_UINT64 is stored at the location pointed to by Data.
2282
2283 If EndPointer is not NULL, a pointer to the character that stopped the scan
2284 is stored at the location pointed to by EndPointer. If String has no valid
2285 decimal digits right after the optional pad spaces, the value of String is
2286 stored at the location pointed to by EndPointer.
2287
2288 @param String Pointer to a Null-terminated Ascii string.
2289 @param EndPointer Pointer to character that stops scan.
2290 @param Data Pointer to the converted value.
2291
2292 @retval RETURN_SUCCESS Value is translated from String.
2293 @retval RETURN_INVALID_PARAMETER If String is NULL.
2294 If Data is NULL.
2295 If PcdMaximumAsciiStringLength is not zero,
2296 and String contains more than
2297 PcdMaximumAsciiStringLength Ascii
2298 characters, not including the
2299 Null-terminator.
2300 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
2301 the range defined by UINT64.
2302
2303 **/
2304 RETURN_STATUS
2305 EFIAPI
2306 AsciiStrDecimalToUint64S (
2307 IN CONST CHAR8 *String,
2308 OUT CHAR8 **EndPointer, OPTIONAL
2309 OUT UINT64 *Data
2310 )
2311 {
2312 //
2313 // 1. Neither String nor Data shall be a null pointer.
2314 //
2315 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
2316 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
2317
2318 //
2319 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
2320 //
2321 if (ASCII_RSIZE_MAX != 0) {
2322 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2323 }
2324
2325 if (EndPointer != NULL) {
2326 *EndPointer = (CHAR8 *) String;
2327 }
2328
2329 //
2330 // Ignore the pad spaces (space or tab)
2331 //
2332 while ((*String == ' ') || (*String == '\t')) {
2333 String++;
2334 }
2335
2336 //
2337 // Ignore leading Zeros after the spaces
2338 //
2339 while (*String == '0') {
2340 String++;
2341 }
2342
2343 *Data = 0;
2344
2345 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
2346 //
2347 // If the number represented by String overflows according to the range
2348 // defined by UINT64, then MAX_UINT64 is stored in *Data and
2349 // RETURN_UNSUPPORTED is returned.
2350 //
2351 if (*Data > DivU64x32 (MAX_UINT64 - (*String - '0'), 10)) {
2352 *Data = MAX_UINT64;
2353 if (EndPointer != NULL) {
2354 *EndPointer = (CHAR8 *) String;
2355 }
2356 return RETURN_UNSUPPORTED;
2357 }
2358
2359 *Data = MultU64x32 (*Data, 10) + (*String - '0');
2360 String++;
2361 }
2362
2363 if (EndPointer != NULL) {
2364 *EndPointer = (CHAR8 *) String;
2365 }
2366 return RETURN_SUCCESS;
2367 }
2368
2369 /**
2370 Convert a Null-terminated Ascii hexadecimal string to a value of type UINTN.
2371
2372 This function outputs a value of type UINTN by interpreting the contents of
2373 the Ascii string specified by String as a hexadecimal number. The format of
2374 the input Ascii string String is:
2375
2376 [spaces][zeros][x][hexadecimal digits].
2377
2378 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
2379 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
2380 "x" appears in the input string, it must be prefixed with at least one 0. The
2381 function will ignore the pad space, which includes spaces or tab characters,
2382 before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
2383 [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
2384 the first valid hexadecimal digit. Then, the function stops at the first
2385 character that is a not a valid hexadecimal character or Null-terminator,
2386 whichever on comes first.
2387
2388 If String is NULL, then ASSERT().
2389 If Data is NULL, then ASSERT().
2390 If PcdMaximumAsciiStringLength is not zero, and String contains more than
2391 PcdMaximumAsciiStringLength Ascii characters, not including the
2392 Null-terminator, then ASSERT().
2393
2394 If String has no valid hexadecimal digits in the above format, then 0 is
2395 stored at the location pointed to by Data.
2396 If the number represented by String exceeds the range defined by UINTN, then
2397 MAX_UINTN is stored at the location pointed to by Data.
2398
2399 If EndPointer is not NULL, a pointer to the character that stopped the scan
2400 is stored at the location pointed to by EndPointer. If String has no valid
2401 hexadecimal digits right after the optional pad spaces, the value of String
2402 is stored at the location pointed to by EndPointer.
2403
2404 @param String Pointer to a Null-terminated Ascii string.
2405 @param EndPointer Pointer to character that stops scan.
2406 @param Data Pointer to the converted value.
2407
2408 @retval RETURN_SUCCESS Value is translated from String.
2409 @retval RETURN_INVALID_PARAMETER If String is NULL.
2410 If Data is NULL.
2411 If PcdMaximumAsciiStringLength is not zero,
2412 and String contains more than
2413 PcdMaximumAsciiStringLength Ascii
2414 characters, not including the
2415 Null-terminator.
2416 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
2417 the range defined by UINTN.
2418
2419 **/
2420 RETURN_STATUS
2421 EFIAPI
2422 AsciiStrHexToUintnS (
2423 IN CONST CHAR8 *String,
2424 OUT CHAR8 **EndPointer, OPTIONAL
2425 OUT UINTN *Data
2426 )
2427 {
2428 //
2429 // 1. Neither String nor Data shall be a null pointer.
2430 //
2431 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
2432 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
2433
2434 //
2435 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
2436 //
2437 if (ASCII_RSIZE_MAX != 0) {
2438 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2439 }
2440
2441 if (EndPointer != NULL) {
2442 *EndPointer = (CHAR8 *) String;
2443 }
2444
2445 //
2446 // Ignore the pad spaces (space or tab)
2447 //
2448 while ((*String == ' ') || (*String == '\t')) {
2449 String++;
2450 }
2451
2452 //
2453 // Ignore leading Zeros after the spaces
2454 //
2455 while (*String == '0') {
2456 String++;
2457 }
2458
2459 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
2460 if (*(String - 1) != '0') {
2461 *Data = 0;
2462 return RETURN_SUCCESS;
2463 }
2464 //
2465 // Skip the 'X'
2466 //
2467 String++;
2468 }
2469
2470 *Data = 0;
2471
2472 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
2473 //
2474 // If the number represented by String overflows according to the range
2475 // defined by UINTN, then MAX_UINTN is stored in *Data and
2476 // RETURN_UNSUPPORTED is returned.
2477 //
2478 if (*Data > ((MAX_UINTN - InternalAsciiHexCharToUintn (*String)) >> 4)) {
2479 *Data = MAX_UINTN;
2480 if (EndPointer != NULL) {
2481 *EndPointer = (CHAR8 *) String;
2482 }
2483 return RETURN_UNSUPPORTED;
2484 }
2485
2486 *Data = (*Data << 4) + InternalAsciiHexCharToUintn (*String);
2487 String++;
2488 }
2489
2490 if (EndPointer != NULL) {
2491 *EndPointer = (CHAR8 *) String;
2492 }
2493 return RETURN_SUCCESS;
2494 }
2495
2496 /**
2497 Convert a Null-terminated Ascii hexadecimal string to a value of type UINT64.
2498
2499 This function outputs a value of type UINT64 by interpreting the contents of
2500 the Ascii string specified by String as a hexadecimal number. The format of
2501 the input Ascii string String is:
2502
2503 [spaces][zeros][x][hexadecimal digits].
2504
2505 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
2506 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
2507 "x" appears in the input string, it must be prefixed with at least one 0. The
2508 function will ignore the pad space, which includes spaces or tab characters,
2509 before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
2510 [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
2511 the first valid hexadecimal digit. Then, the function stops at the first
2512 character that is a not a valid hexadecimal character or Null-terminator,
2513 whichever on comes first.
2514
2515 If String is NULL, then ASSERT().
2516 If Data is NULL, then ASSERT().
2517 If PcdMaximumAsciiStringLength is not zero, and String contains more than
2518 PcdMaximumAsciiStringLength Ascii characters, not including the
2519 Null-terminator, then ASSERT().
2520
2521 If String has no valid hexadecimal digits in the above format, then 0 is
2522 stored at the location pointed to by Data.
2523 If the number represented by String exceeds the range defined by UINT64, then
2524 MAX_UINT64 is stored at the location pointed to by Data.
2525
2526 If EndPointer is not NULL, a pointer to the character that stopped the scan
2527 is stored at the location pointed to by EndPointer. If String has no valid
2528 hexadecimal digits right after the optional pad spaces, the value of String
2529 is stored at the location pointed to by EndPointer.
2530
2531 @param String Pointer to a Null-terminated Ascii string.
2532 @param EndPointer Pointer to character that stops scan.
2533 @param Data Pointer to the converted value.
2534
2535 @retval RETURN_SUCCESS Value is translated from String.
2536 @retval RETURN_INVALID_PARAMETER If String is NULL.
2537 If Data is NULL.
2538 If PcdMaximumAsciiStringLength is not zero,
2539 and String contains more than
2540 PcdMaximumAsciiStringLength Ascii
2541 characters, not including the
2542 Null-terminator.
2543 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
2544 the range defined by UINT64.
2545
2546 **/
2547 RETURN_STATUS
2548 EFIAPI
2549 AsciiStrHexToUint64S (
2550 IN CONST CHAR8 *String,
2551 OUT CHAR8 **EndPointer, OPTIONAL
2552 OUT UINT64 *Data
2553 )
2554 {
2555 //
2556 // 1. Neither String nor Data shall be a null pointer.
2557 //
2558 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
2559 SAFE_STRING_CONSTRAINT_CHECK ((Data != NULL), RETURN_INVALID_PARAMETER);
2560
2561 //
2562 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
2563 //
2564 if (ASCII_RSIZE_MAX != 0) {
2565 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2566 }
2567
2568 if (EndPointer != NULL) {
2569 *EndPointer = (CHAR8 *) String;
2570 }
2571
2572 //
2573 // Ignore the pad spaces (space or tab)
2574 //
2575 while ((*String == ' ') || (*String == '\t')) {
2576 String++;
2577 }
2578
2579 //
2580 // Ignore leading Zeros after the spaces
2581 //
2582 while (*String == '0') {
2583 String++;
2584 }
2585
2586 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
2587 if (*(String - 1) != '0') {
2588 *Data = 0;
2589 return RETURN_SUCCESS;
2590 }
2591 //
2592 // Skip the 'X'
2593 //
2594 String++;
2595 }
2596
2597 *Data = 0;
2598
2599 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
2600 //
2601 // If the number represented by String overflows according to the range
2602 // defined by UINT64, then MAX_UINT64 is stored in *Data and
2603 // RETURN_UNSUPPORTED is returned.
2604 //
2605 if (*Data > RShiftU64 (MAX_UINT64 - InternalAsciiHexCharToUintn (*String), 4)) {
2606 *Data = MAX_UINT64;
2607 if (EndPointer != NULL) {
2608 *EndPointer = (CHAR8 *) String;
2609 }
2610 return RETURN_UNSUPPORTED;
2611 }
2612
2613 *Data = LShiftU64 (*Data, 4) + InternalAsciiHexCharToUintn (*String);
2614 String++;
2615 }
2616
2617 if (EndPointer != NULL) {
2618 *EndPointer = (CHAR8 *) String;
2619 }
2620 return RETURN_SUCCESS;
2621 }
2622
2623 /**
2624 Convert a Null-terminated Unicode string to a Null-terminated
2625 ASCII string.
2626
2627 This function is similar to AsciiStrCpyS.
2628
2629 This function converts the content of the Unicode string Source
2630 to the ASCII string Destination by copying the lower 8 bits of
2631 each Unicode character. The function terminates the ASCII string
2632 Destination by appending a Null-terminator character at the end.
2633
2634 The caller is responsible to make sure Destination points to a buffer with size
2635 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
2636
2637 If any Unicode characters in Source contain non-zero value in
2638 the upper 8 bits, then ASSERT().
2639
2640 If Source is not aligned on a 16-bit boundary, then ASSERT().
2641 If an error would be returned, then the function will also ASSERT().
2642
2643 If an error is returned, then the Destination is unmodified.
2644
2645 @param Source The pointer to a Null-terminated Unicode string.
2646 @param Destination The pointer to a Null-terminated ASCII string.
2647 @param DestMax The maximum number of Destination Ascii
2648 char, including terminating null char.
2649
2650 @retval RETURN_SUCCESS String is converted.
2651 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
2652 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2653 If Source is NULL.
2654 If PcdMaximumAsciiStringLength is not zero,
2655 and DestMax is greater than
2656 PcdMaximumAsciiStringLength.
2657 If PcdMaximumUnicodeStringLength is not zero,
2658 and DestMax is greater than
2659 PcdMaximumUnicodeStringLength.
2660 If DestMax is 0.
2661 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2662
2663 **/
2664 RETURN_STATUS
2665 EFIAPI
2666 UnicodeStrToAsciiStrS (
2667 IN CONST CHAR16 *Source,
2668 OUT CHAR8 *Destination,
2669 IN UINTN DestMax
2670 )
2671 {
2672 UINTN SourceLen;
2673
2674 ASSERT (((UINTN) Source & BIT0) == 0);
2675
2676 //
2677 // 1. Neither Destination nor Source shall be a null pointer.
2678 //
2679 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
2680 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
2681
2682 //
2683 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX or RSIZE_MAX.
2684 //
2685 if (ASCII_RSIZE_MAX != 0) {
2686 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2687 }
2688 if (RSIZE_MAX != 0) {
2689 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
2690 }
2691
2692 //
2693 // 3. DestMax shall not equal zero.
2694 //
2695 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
2696
2697 //
2698 // 4. DestMax shall be greater than StrnLenS (Source, DestMax).
2699 //
2700 SourceLen = StrnLenS (Source, DestMax);
2701 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
2702
2703 //
2704 // 5. Copying shall not take place between objects that overlap.
2705 //
2706 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof(CHAR16)), RETURN_ACCESS_DENIED);
2707
2708 //
2709 // convert string
2710 //
2711 while (*Source != '\0') {
2712 //
2713 // If any Unicode characters in Source contain
2714 // non-zero value in the upper 8 bits, then ASSERT().
2715 //
2716 ASSERT (*Source < 0x100);
2717 *(Destination++) = (CHAR8) *(Source++);
2718 }
2719 *Destination = '\0';
2720
2721 return RETURN_SUCCESS;
2722 }
2723
2724 /**
2725 Convert not more than Length successive characters from a Null-terminated
2726 Unicode string to a Null-terminated Ascii string. If no null char is copied
2727 from Source, then Destination[Length] is always set to null.
2728
2729 This function converts not more than Length successive characters from the
2730 Unicode string Source to the Ascii string Destination by copying the lower 8
2731 bits of each Unicode character. The function terminates the Ascii string
2732 Destination by appending a Null-terminator character at the end.
2733
2734 The caller is responsible to make sure Destination points to a buffer with
2735 size not smaller than ((MIN(StrLen(Source), Length) + 1) * sizeof (CHAR8))
2736 in bytes.
2737
2738 If any Unicode characters in Source contain non-zero value in the upper 8
2739 bits, then ASSERT().
2740 If Source is not aligned on a 16-bit boundary, then ASSERT().
2741 If an error would be returned, then the function will also ASSERT().
2742
2743 If an error is returned, then Destination and DestinationLength are
2744 unmodified.
2745
2746 @param Source The pointer to a Null-terminated Unicode string.
2747 @param Length The maximum number of Unicode characters to
2748 convert.
2749 @param Destination The pointer to a Null-terminated Ascii string.
2750 @param DestMax The maximum number of Destination Ascii char,
2751 including terminating null char.
2752 @param DestinationLength The number of Unicode characters converted.
2753
2754 @retval RETURN_SUCCESS String is converted.
2755 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2756 If Source is NULL.
2757 If DestinationLength is NULL.
2758 If PcdMaximumAsciiStringLength is not zero,
2759 and Length or DestMax is greater than
2760 PcdMaximumAsciiStringLength.
2761 If PcdMaximumUnicodeStringLength is not
2762 zero, and Length or DestMax is greater than
2763 PcdMaximumUnicodeStringLength.
2764 If DestMax is 0.
2765 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than
2766 MIN(StrLen(Source), Length).
2767 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2768
2769 **/
2770 RETURN_STATUS
2771 EFIAPI
2772 UnicodeStrnToAsciiStrS (
2773 IN CONST CHAR16 *Source,
2774 IN UINTN Length,
2775 OUT CHAR8 *Destination,
2776 IN UINTN DestMax,
2777 OUT UINTN *DestinationLength
2778 )
2779 {
2780 UINTN SourceLen;
2781
2782 ASSERT (((UINTN) Source & BIT0) == 0);
2783
2784 //
2785 // 1. None of Destination, Source or DestinationLength shall be a null
2786 // pointer.
2787 //
2788 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
2789 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
2790 SAFE_STRING_CONSTRAINT_CHECK ((DestinationLength != NULL), RETURN_INVALID_PARAMETER);
2791
2792 //
2793 // 2. Neither Length nor DestMax shall be greater than ASCII_RSIZE_MAX or
2794 // RSIZE_MAX.
2795 //
2796 if (ASCII_RSIZE_MAX != 0) {
2797 SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2798 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2799 }
2800 if (RSIZE_MAX != 0) {
2801 SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
2802 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
2803 }
2804
2805 //
2806 // 3. DestMax shall not equal zero.
2807 //
2808 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
2809
2810 //
2811 // 4. If Length is not less than DestMax, then DestMax shall be greater than
2812 // StrnLenS(Source, DestMax).
2813 //
2814 SourceLen = StrnLenS (Source, DestMax);
2815 if (Length >= DestMax) {
2816 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
2817 }
2818
2819 //
2820 // 5. Copying shall not take place between objects that overlap.
2821 //
2822 if (SourceLen > Length) {
2823 SourceLen = Length;
2824 }
2825 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax, (VOID *)Source, (SourceLen + 1) * sizeof(CHAR16)), RETURN_ACCESS_DENIED);
2826
2827 *DestinationLength = 0;
2828
2829 //
2830 // Convert string
2831 //
2832 while ((*Source != 0) && (SourceLen > 0)) {
2833 //
2834 // If any Unicode characters in Source contain non-zero value in the upper
2835 // 8 bits, then ASSERT().
2836 //
2837 ASSERT (*Source < 0x100);
2838 *(Destination++) = (CHAR8) *(Source++);
2839 SourceLen--;
2840 (*DestinationLength)++;
2841 }
2842 *Destination = 0;
2843
2844 return RETURN_SUCCESS;
2845 }
2846
2847 /**
2848 Convert one Null-terminated ASCII string to a Null-terminated
2849 Unicode string.
2850
2851 This function is similar to StrCpyS.
2852
2853 This function converts the contents of the ASCII string Source to the Unicode
2854 string Destination. The function terminates the Unicode string Destination by
2855 appending a Null-terminator character at the end.
2856
2857 The caller is responsible to make sure Destination points to a buffer with size
2858 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
2859
2860 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2861 If an error would be returned, then the function will also ASSERT().
2862
2863 If an error is returned, then the Destination is unmodified.
2864
2865 @param Source The pointer to a Null-terminated ASCII string.
2866 @param Destination The pointer to a Null-terminated Unicode string.
2867 @param DestMax The maximum number of Destination Unicode
2868 char, including terminating null char.
2869
2870 @retval RETURN_SUCCESS String is converted.
2871 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
2872 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2873 If Source is NULL.
2874 If PcdMaximumUnicodeStringLength is not zero,
2875 and DestMax is greater than
2876 PcdMaximumUnicodeStringLength.
2877 If PcdMaximumAsciiStringLength is not zero,
2878 and DestMax is greater than
2879 PcdMaximumAsciiStringLength.
2880 If DestMax is 0.
2881 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2882
2883 **/
2884 RETURN_STATUS
2885 EFIAPI
2886 AsciiStrToUnicodeStrS (
2887 IN CONST CHAR8 *Source,
2888 OUT CHAR16 *Destination,
2889 IN UINTN DestMax
2890 )
2891 {
2892 UINTN SourceLen;
2893
2894 ASSERT (((UINTN) Destination & BIT0) == 0);
2895
2896 //
2897 // 1. Neither Destination nor Source shall be a null pointer.
2898 //
2899 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
2900 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
2901
2902 //
2903 // 2. DestMax shall not be greater than RSIZE_MAX or ASCII_RSIZE_MAX.
2904 //
2905 if (RSIZE_MAX != 0) {
2906 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
2907 }
2908 if (ASCII_RSIZE_MAX != 0) {
2909 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
2910 }
2911
2912 //
2913 // 3. DestMax shall not equal zero.
2914 //
2915 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
2916
2917 //
2918 // 4. DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
2919 //
2920 SourceLen = AsciiStrnLenS (Source, DestMax);
2921 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
2922
2923 //
2924 // 5. Copying shall not take place between objects that overlap.
2925 //
2926 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof(CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
2927
2928 //
2929 // Convert string
2930 //
2931 while (*Source != '\0') {
2932 *(Destination++) = (CHAR16)*(Source++);
2933 }
2934 *Destination = '\0';
2935
2936 return RETURN_SUCCESS;
2937 }
2938
2939 /**
2940 Convert not more than Length successive characters from a Null-terminated
2941 Ascii string to a Null-terminated Unicode string. If no null char is copied
2942 from Source, then Destination[Length] is always set to null.
2943
2944 This function converts not more than Length successive characters from the
2945 Ascii string Source to the Unicode string Destination. The function
2946 terminates the Unicode string Destination by appending a Null-terminator
2947 character at the end.
2948
2949 The caller is responsible to make sure Destination points to a buffer with
2950 size not smaller than
2951 ((MIN(AsciiStrLen(Source), Length) + 1) * sizeof (CHAR8)) in bytes.
2952
2953 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2954 If an error would be returned, then the function will also ASSERT().
2955
2956 If an error is returned, then Destination and DestinationLength are
2957 unmodified.
2958
2959 @param Source The pointer to a Null-terminated Ascii string.
2960 @param Length The maximum number of Ascii characters to convert.
2961 @param Destination The pointer to a Null-terminated Unicode string.
2962 @param DestMax The maximum number of Destination Unicode char,
2963 including terminating null char.
2964 @param DestinationLength The number of Ascii characters converted.
2965
2966 @retval RETURN_SUCCESS String is converted.
2967 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2968 If Source is NULL.
2969 If DestinationLength is NULL.
2970 If PcdMaximumUnicodeStringLength is not
2971 zero, and Length or DestMax is greater than
2972 PcdMaximumUnicodeStringLength.
2973 If PcdMaximumAsciiStringLength is not zero,
2974 and Length or DestMax is greater than
2975 PcdMaximumAsciiStringLength.
2976 If DestMax is 0.
2977 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than
2978 MIN(AsciiStrLen(Source), Length).
2979 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2980
2981 **/
2982 RETURN_STATUS
2983 EFIAPI
2984 AsciiStrnToUnicodeStrS (
2985 IN CONST CHAR8 *Source,
2986 IN UINTN Length,
2987 OUT CHAR16 *Destination,
2988 IN UINTN DestMax,
2989 OUT UINTN *DestinationLength
2990 )
2991 {
2992 UINTN SourceLen;
2993
2994 ASSERT (((UINTN) Destination & BIT0) == 0);
2995
2996 //
2997 // 1. None of Destination, Source or DestinationLength shall be a null
2998 // pointer.
2999 //
3000 SAFE_STRING_CONSTRAINT_CHECK ((Destination != NULL), RETURN_INVALID_PARAMETER);
3001 SAFE_STRING_CONSTRAINT_CHECK ((Source != NULL), RETURN_INVALID_PARAMETER);
3002 SAFE_STRING_CONSTRAINT_CHECK ((DestinationLength != NULL), RETURN_INVALID_PARAMETER);
3003
3004 //
3005 // 2. Neither Length nor DestMax shall be greater than ASCII_RSIZE_MAX or
3006 // RSIZE_MAX.
3007 //
3008 if (RSIZE_MAX != 0) {
3009 SAFE_STRING_CONSTRAINT_CHECK ((Length <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
3010 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= RSIZE_MAX), RETURN_INVALID_PARAMETER);
3011 }
3012 if (ASCII_RSIZE_MAX != 0) {
3013 SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
3014 SAFE_STRING_CONSTRAINT_CHECK ((DestMax <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
3015 }
3016
3017 //
3018 // 3. DestMax shall not equal zero.
3019 //
3020 SAFE_STRING_CONSTRAINT_CHECK ((DestMax != 0), RETURN_INVALID_PARAMETER);
3021
3022 //
3023 // 4. If Length is not less than DestMax, then DestMax shall be greater than
3024 // AsciiStrnLenS(Source, DestMax).
3025 //
3026 SourceLen = AsciiStrnLenS (Source, DestMax);
3027 if (Length >= DestMax) {
3028 SAFE_STRING_CONSTRAINT_CHECK ((DestMax > SourceLen), RETURN_BUFFER_TOO_SMALL);
3029 }
3030
3031 //
3032 // 5. Copying shall not take place between objects that overlap.
3033 //
3034 if (SourceLen > Length) {
3035 SourceLen = Length;
3036 }
3037 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination, DestMax * sizeof(CHAR16), (VOID *)Source, SourceLen + 1), RETURN_ACCESS_DENIED);
3038
3039 *DestinationLength = 0;
3040
3041 //
3042 // Convert string
3043 //
3044 while ((*Source != 0) && (SourceLen > 0)) {
3045 *(Destination++) = (CHAR16)*(Source++);
3046 SourceLen--;
3047 (*DestinationLength)++;
3048 }
3049 *Destination = 0;
3050
3051 return RETURN_SUCCESS;
3052 }
3053
3054 /**
3055 Convert a Null-terminated ASCII string to IPv6 address and prefix length.
3056
3057 This function outputs a value of type IPv6_ADDRESS and may output a value
3058 of type UINT8 by interpreting the contents of the ASCII string specified
3059 by String. The format of the input ASCII string String is as follows:
3060
3061 X:X:X:X:X:X:X:X[/P]
3062
3063 X contains one to four hexadecimal digit characters in the range [0-9], [a-f] and
3064 [A-F]. X is converted to a value of type UINT16, whose low byte is stored in low
3065 memory address and high byte is stored in high memory address. P contains decimal
3066 digit characters in the range [0-9]. The running zero in the beginning of P will
3067 be ignored. /P is optional.
3068
3069 When /P is not in the String, the function stops at the first character that is
3070 not a valid hexadecimal digit character after eight X's are converted.
3071
3072 When /P is in the String, the function stops at the first character that is not
3073 a valid decimal digit character after P is converted.
3074
3075 "::" can be used to compress one or more groups of X when X contains only 0.
3076 The "::" can only appear once in the String.
3077
3078 If String is NULL, then ASSERT().
3079
3080 If Address is NULL, then ASSERT().
3081
3082 If EndPointer is not NULL and Address is translated from String, a pointer
3083 to the character that stopped the scan is stored at the location pointed to
3084 by EndPointer.
3085
3086 @param String Pointer to a Null-terminated ASCII string.
3087 @param EndPointer Pointer to character that stops scan.
3088 @param Address Pointer to the converted IPv6 address.
3089 @param PrefixLength Pointer to the converted IPv6 address prefix
3090 length. MAX_UINT8 is returned when /P is
3091 not in the String.
3092
3093 @retval RETURN_SUCCESS Address is translated from String.
3094 @retval RETURN_INVALID_PARAMETER If String is NULL.
3095 If Data is NULL.
3096 @retval RETURN_UNSUPPORTED If X contains more than four hexadecimal
3097 digit characters.
3098 If String contains "::" and number of X
3099 is not less than 8.
3100 If P starts with character that is not a
3101 valid decimal digit character.
3102 If the decimal number converted from P
3103 exceeds 128.
3104
3105 **/
3106 RETURN_STATUS
3107 EFIAPI
3108 AsciiStrToIpv6Address (
3109 IN CONST CHAR8 *String,
3110 OUT CHAR8 **EndPointer, OPTIONAL
3111 OUT IPv6_ADDRESS *Address,
3112 OUT UINT8 *PrefixLength OPTIONAL
3113 )
3114 {
3115 RETURN_STATUS Status;
3116 UINTN AddressIndex;
3117 UINTN Uintn;
3118 IPv6_ADDRESS LocalAddress;
3119 UINT8 LocalPrefixLength;
3120 CONST CHAR8 *Pointer;
3121 CHAR8 *End;
3122 UINTN CompressStart;
3123 BOOLEAN ExpectPrefix;
3124
3125 LocalPrefixLength = MAX_UINT8;
3126 CompressStart = ARRAY_SIZE (Address->Addr);
3127 ExpectPrefix = FALSE;
3128
3129 //
3130 // None of String or Address shall be a null pointer.
3131 //
3132 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
3133 SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
3134
3135 for (Pointer = String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
3136 if (!InternalAsciiIsHexaDecimalDigitCharacter (*Pointer)) {
3137 if (*Pointer != ':') {
3138 //
3139 // ":" or "/" should be followed by digit characters.
3140 //
3141 return RETURN_UNSUPPORTED;
3142 }
3143
3144 //
3145 // Meet second ":" after previous ":" or "/"
3146 // or meet first ":" in the beginning of String.
3147 //
3148 if (ExpectPrefix) {
3149 //
3150 // ":" shall not be after "/"
3151 //
3152 return RETURN_UNSUPPORTED;
3153 }
3154
3155 if (CompressStart != ARRAY_SIZE (Address->Addr) || AddressIndex == ARRAY_SIZE (Address->Addr)) {
3156 //
3157 // "::" can only appear once.
3158 // "::" can only appear when address is not full length.
3159 //
3160 return RETURN_UNSUPPORTED;
3161 } else {
3162 //
3163 // Remember the start of zero compressing.
3164 //
3165 CompressStart = AddressIndex;
3166 Pointer++;
3167
3168 if (CompressStart == 0) {
3169 if (*Pointer != ':') {
3170 //
3171 // Single ":" shall not be in the beginning of String.
3172 //
3173 return RETURN_UNSUPPORTED;
3174 }
3175 Pointer++;
3176 }
3177 }
3178 }
3179
3180 if (!InternalAsciiIsHexaDecimalDigitCharacter (*Pointer)) {
3181 if (*Pointer == '/') {
3182 //
3183 // Might be optional "/P" after "::".
3184 //
3185 if (CompressStart != AddressIndex) {
3186 return RETURN_UNSUPPORTED;
3187 }
3188 } else {
3189 break;
3190 }
3191 } else {
3192 if (!ExpectPrefix) {
3193 //
3194 // Get X.
3195 //
3196 Status = AsciiStrHexToUintnS (Pointer, &End, &Uintn);
3197 if (RETURN_ERROR (Status) || End - Pointer > 4) {
3198 //
3199 // Number of hexadecimal digit characters is no more than 4.
3200 //
3201 return RETURN_UNSUPPORTED;
3202 }
3203 Pointer = End;
3204 //
3205 // Uintn won't exceed MAX_UINT16 if number of hexadecimal digit characters is no more than 4.
3206 //
3207 LocalAddress.Addr[AddressIndex] = (UINT8) ((UINT16) Uintn >> 8);
3208 LocalAddress.Addr[AddressIndex + 1] = (UINT8) Uintn;
3209 AddressIndex += 2;
3210 } else {
3211 //
3212 // Get P, then exit the loop.
3213 //
3214 Status = AsciiStrDecimalToUintnS (Pointer, &End, &Uintn);
3215 if (RETURN_ERROR (Status) || End == Pointer || Uintn > 128) {
3216 //
3217 // Prefix length should not exceed 128.
3218 //
3219 return RETURN_UNSUPPORTED;
3220 }
3221 LocalPrefixLength = (UINT8) Uintn;
3222 Pointer = End;
3223 break;
3224 }
3225 }
3226
3227 //
3228 // Skip ':' or "/"
3229 //
3230 if (*Pointer == '/') {
3231 ExpectPrefix = TRUE;
3232 } else if (*Pointer == ':') {
3233 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
3234 //
3235 // Meet additional ":" after all 8 16-bit address
3236 //
3237 break;
3238 }
3239 } else {
3240 //
3241 // Meet other character that is not "/" or ":" after all 8 16-bit address
3242 //
3243 break;
3244 }
3245 Pointer++;
3246 }
3247
3248 if ((AddressIndex == ARRAY_SIZE (Address->Addr) && CompressStart != ARRAY_SIZE (Address->Addr)) ||
3249 (AddressIndex != ARRAY_SIZE (Address->Addr) && CompressStart == ARRAY_SIZE (Address->Addr))
3250 ) {
3251 //
3252 // Full length of address shall not have compressing zeros.
3253 // Non-full length of address shall have compressing zeros.
3254 //
3255 return RETURN_UNSUPPORTED;
3256 }
3257 CopyMem (&Address->Addr[0], &LocalAddress.Addr[0], CompressStart);
3258 ZeroMem (&Address->Addr[CompressStart], ARRAY_SIZE (Address->Addr) - AddressIndex);
3259 CopyMem (
3260 &Address->Addr[CompressStart + ARRAY_SIZE (Address->Addr) - AddressIndex],
3261 &LocalAddress.Addr[CompressStart],
3262 AddressIndex - CompressStart
3263 );
3264
3265 if (PrefixLength != NULL) {
3266 *PrefixLength = LocalPrefixLength;
3267 }
3268 if (EndPointer != NULL) {
3269 *EndPointer = (CHAR8 *) Pointer;
3270 }
3271
3272 return RETURN_SUCCESS;
3273 }
3274
3275 /**
3276 Convert a Null-terminated ASCII string to IPv4 address and prefix length.
3277
3278 This function outputs a value of type IPv4_ADDRESS and may output a value
3279 of type UINT8 by interpreting the contents of the ASCII string specified
3280 by String. The format of the input ASCII string String is as follows:
3281
3282 D.D.D.D[/P]
3283
3284 D and P are decimal digit characters in the range [0-9]. The running zero in
3285 the beginning of D and P will be ignored. /P is optional.
3286
3287 When /P is not in the String, the function stops at the first character that is
3288 not a valid decimal digit character after four D's are converted.
3289
3290 When /P is in the String, the function stops at the first character that is not
3291 a valid decimal digit character after P is converted.
3292
3293 If String is NULL, then ASSERT().
3294
3295 If Address is NULL, then ASSERT().
3296
3297 If EndPointer is not NULL and Address is translated from String, a pointer
3298 to the character that stopped the scan is stored at the location pointed to
3299 by EndPointer.
3300
3301 @param String Pointer to a Null-terminated ASCII string.
3302 @param EndPointer Pointer to character that stops scan.
3303 @param Address Pointer to the converted IPv4 address.
3304 @param PrefixLength Pointer to the converted IPv4 address prefix
3305 length. MAX_UINT8 is returned when /P is
3306 not in the String.
3307
3308 @retval RETURN_SUCCESS Address is translated from String.
3309 @retval RETURN_INVALID_PARAMETER If String is NULL.
3310 If Data is NULL.
3311 @retval RETURN_UNSUPPORTED If String is not in the correct format.
3312 If any decimal number converted from D
3313 exceeds 255.
3314 If the decimal number converted from P
3315 exceeds 32.
3316
3317 **/
3318 RETURN_STATUS
3319 EFIAPI
3320 AsciiStrToIpv4Address (
3321 IN CONST CHAR8 *String,
3322 OUT CHAR8 **EndPointer, OPTIONAL
3323 OUT IPv4_ADDRESS *Address,
3324 OUT UINT8 *PrefixLength OPTIONAL
3325 )
3326 {
3327 RETURN_STATUS Status;
3328 UINTN AddressIndex;
3329 UINTN Uintn;
3330 IPv4_ADDRESS LocalAddress;
3331 UINT8 LocalPrefixLength;
3332 CHAR8 *Pointer;
3333
3334 LocalPrefixLength = MAX_UINT8;
3335
3336 //
3337 // None of String or Address shall be a null pointer.
3338 //
3339 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
3340 SAFE_STRING_CONSTRAINT_CHECK ((Address != NULL), RETURN_INVALID_PARAMETER);
3341
3342 for (Pointer = (CHAR8 *) String, AddressIndex = 0; AddressIndex < ARRAY_SIZE (Address->Addr) + 1;) {
3343 if (!InternalAsciiIsDecimalDigitCharacter (*Pointer)) {
3344 //
3345 // D or P contains invalid characters.
3346 //
3347 break;
3348 }
3349
3350 //
3351 // Get D or P.
3352 //
3353 Status = AsciiStrDecimalToUintnS ((CONST CHAR8 *) Pointer, &Pointer, &Uintn);
3354 if (RETURN_ERROR (Status)) {
3355 return RETURN_UNSUPPORTED;
3356 }
3357 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
3358 //
3359 // It's P.
3360 //
3361 if (Uintn > 32) {
3362 return RETURN_UNSUPPORTED;
3363 }
3364 LocalPrefixLength = (UINT8) Uintn;
3365 } else {
3366 //
3367 // It's D.
3368 //
3369 if (Uintn > MAX_UINT8) {
3370 return RETURN_UNSUPPORTED;
3371 }
3372 LocalAddress.Addr[AddressIndex] = (UINT8) Uintn;
3373 AddressIndex++;
3374 }
3375
3376 //
3377 // Check the '.' or '/', depending on the AddressIndex.
3378 //
3379 if (AddressIndex == ARRAY_SIZE (Address->Addr)) {
3380 if (*Pointer == '/') {
3381 //
3382 // '/P' is in the String.
3383 // Skip "/" and get P in next loop.
3384 //
3385 Pointer++;
3386 } else {
3387 //
3388 // '/P' is not in the String.
3389 //
3390 break;
3391 }
3392 } else if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
3393 if (*Pointer == '.') {
3394 //
3395 // D should be followed by '.'
3396 //
3397 Pointer++;
3398 } else {
3399 return RETURN_UNSUPPORTED;
3400 }
3401 }
3402 }
3403
3404 if (AddressIndex < ARRAY_SIZE (Address->Addr)) {
3405 return RETURN_UNSUPPORTED;
3406 }
3407
3408 CopyMem (Address, &LocalAddress, sizeof (*Address));
3409 if (PrefixLength != NULL) {
3410 *PrefixLength = LocalPrefixLength;
3411 }
3412 if (EndPointer != NULL) {
3413 *EndPointer = Pointer;
3414 }
3415
3416 return RETURN_SUCCESS;
3417 }
3418
3419 /**
3420 Convert a Null-terminated ASCII GUID string to a value of type
3421 EFI_GUID.
3422
3423 This function outputs a GUID value by interpreting the contents of
3424 the ASCII string specified by String. The format of the input
3425 ASCII string String consists of 36 characters, as follows:
3426
3427 aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
3428
3429 The pairs aa - pp are two characters in the range [0-9], [a-f] and
3430 [A-F], with each pair representing a single byte hexadecimal value.
3431
3432 The mapping between String and the EFI_GUID structure is as follows:
3433 aa Data1[24:31]
3434 bb Data1[16:23]
3435 cc Data1[8:15]
3436 dd Data1[0:7]
3437 ee Data2[8:15]
3438 ff Data2[0:7]
3439 gg Data3[8:15]
3440 hh Data3[0:7]
3441 ii Data4[0:7]
3442 jj Data4[8:15]
3443 kk Data4[16:23]
3444 ll Data4[24:31]
3445 mm Data4[32:39]
3446 nn Data4[40:47]
3447 oo Data4[48:55]
3448 pp Data4[56:63]
3449
3450 If String is NULL, then ASSERT().
3451 If Guid is NULL, then ASSERT().
3452
3453 @param String Pointer to a Null-terminated ASCII string.
3454 @param Guid Pointer to the converted GUID.
3455
3456 @retval RETURN_SUCCESS Guid is translated from String.
3457 @retval RETURN_INVALID_PARAMETER If String is NULL.
3458 If Data is NULL.
3459 @retval RETURN_UNSUPPORTED If String is not as the above format.
3460
3461 **/
3462 RETURN_STATUS
3463 EFIAPI
3464 AsciiStrToGuid (
3465 IN CONST CHAR8 *String,
3466 OUT GUID *Guid
3467 )
3468 {
3469 RETURN_STATUS Status;
3470 GUID LocalGuid;
3471
3472 //
3473 // None of String or Guid shall be a null pointer.
3474 //
3475 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
3476 SAFE_STRING_CONSTRAINT_CHECK ((Guid != NULL), RETURN_INVALID_PARAMETER);
3477
3478 //
3479 // Get aabbccdd in big-endian.
3480 //
3481 Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data1), (UINT8 *) &LocalGuid.Data1, sizeof (LocalGuid.Data1));
3482 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data1)] != '-') {
3483 return RETURN_UNSUPPORTED;
3484 }
3485 //
3486 // Convert big-endian to little-endian.
3487 //
3488 LocalGuid.Data1 = SwapBytes32 (LocalGuid.Data1);
3489 String += 2 * sizeof (LocalGuid.Data1) + 1;
3490
3491 //
3492 // Get eeff in big-endian.
3493 //
3494 Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data2), (UINT8 *) &LocalGuid.Data2, sizeof (LocalGuid.Data2));
3495 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data2)] != '-') {
3496 return RETURN_UNSUPPORTED;
3497 }
3498 //
3499 // Convert big-endian to little-endian.
3500 //
3501 LocalGuid.Data2 = SwapBytes16 (LocalGuid.Data2);
3502 String += 2 * sizeof (LocalGuid.Data2) + 1;
3503
3504 //
3505 // Get gghh in big-endian.
3506 //
3507 Status = AsciiStrHexToBytes (String, 2 * sizeof (LocalGuid.Data3), (UINT8 *) &LocalGuid.Data3, sizeof (LocalGuid.Data3));
3508 if (RETURN_ERROR (Status) || String[2 * sizeof (LocalGuid.Data3)] != '-') {
3509 return RETURN_UNSUPPORTED;
3510 }
3511 //
3512 // Convert big-endian to little-endian.
3513 //
3514 LocalGuid.Data3 = SwapBytes16 (LocalGuid.Data3);
3515 String += 2 * sizeof (LocalGuid.Data3) + 1;
3516
3517 //
3518 // Get iijj.
3519 //
3520 Status = AsciiStrHexToBytes (String, 2 * 2, &LocalGuid.Data4[0], 2);
3521 if (RETURN_ERROR (Status) || String[2 * 2] != '-') {
3522 return RETURN_UNSUPPORTED;
3523 }
3524 String += 2 * 2 + 1;
3525
3526 //
3527 // Get kkllmmnnoopp.
3528 //
3529 Status = AsciiStrHexToBytes (String, 2 * 6, &LocalGuid.Data4[2], 6);
3530 if (RETURN_ERROR (Status)) {
3531 return RETURN_UNSUPPORTED;
3532 }
3533
3534 CopyGuid (Guid, &LocalGuid);
3535 return RETURN_SUCCESS;
3536 }
3537
3538 /**
3539 Convert a Null-terminated ASCII hexadecimal string to a byte array.
3540
3541 This function outputs a byte array by interpreting the contents of
3542 the ASCII string specified by String in hexadecimal format. The format of
3543 the input ASCII string String is:
3544
3545 [XX]*
3546
3547 X is a hexadecimal digit character in the range [0-9], [a-f] and [A-F].
3548 The function decodes every two hexadecimal digit characters as one byte. The
3549 decoding stops after Length of characters and outputs Buffer containing
3550 (Length / 2) bytes.
3551
3552 If String is NULL, then ASSERT().
3553
3554 If Buffer is NULL, then ASSERT().
3555
3556 If Length is not multiple of 2, then ASSERT().
3557
3558 If PcdMaximumAsciiStringLength is not zero and Length is greater than
3559 PcdMaximumAsciiStringLength, then ASSERT().
3560
3561 If MaxBufferSize is less than (Length / 2), then ASSERT().
3562
3563 @param String Pointer to a Null-terminated ASCII string.
3564 @param Length The number of ASCII characters to decode.
3565 @param Buffer Pointer to the converted bytes array.
3566 @param MaxBufferSize The maximum size of Buffer.
3567
3568 @retval RETURN_SUCCESS Buffer is translated from String.
3569 @retval RETURN_INVALID_PARAMETER If String is NULL.
3570 If Data is NULL.
3571 If Length is not multiple of 2.
3572 If PcdMaximumAsciiStringLength is not zero,
3573 and Length is greater than
3574 PcdMaximumAsciiStringLength.
3575 @retval RETURN_UNSUPPORTED If Length of characters from String contain
3576 a character that is not valid hexadecimal
3577 digit characters, or a Null-terminator.
3578 @retval RETURN_BUFFER_TOO_SMALL If MaxBufferSize is less than (Length / 2).
3579 **/
3580 RETURN_STATUS
3581 EFIAPI
3582 AsciiStrHexToBytes (
3583 IN CONST CHAR8 *String,
3584 IN UINTN Length,
3585 OUT UINT8 *Buffer,
3586 IN UINTN MaxBufferSize
3587 )
3588 {
3589 UINTN Index;
3590
3591 //
3592 // 1. None of String or Buffer shall be a null pointer.
3593 //
3594 SAFE_STRING_CONSTRAINT_CHECK ((String != NULL), RETURN_INVALID_PARAMETER);
3595 SAFE_STRING_CONSTRAINT_CHECK ((Buffer != NULL), RETURN_INVALID_PARAMETER);
3596
3597 //
3598 // 2. Length shall not be greater than ASCII_RSIZE_MAX.
3599 //
3600 if (ASCII_RSIZE_MAX != 0) {
3601 SAFE_STRING_CONSTRAINT_CHECK ((Length <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
3602 }
3603
3604 //
3605 // 3. Length shall not be odd.
3606 //
3607 SAFE_STRING_CONSTRAINT_CHECK (((Length & BIT0) == 0), RETURN_INVALID_PARAMETER);
3608
3609 //
3610 // 4. MaxBufferSize shall equal to or greater than Length / 2.
3611 //
3612 SAFE_STRING_CONSTRAINT_CHECK ((MaxBufferSize >= Length / 2), RETURN_BUFFER_TOO_SMALL);
3613
3614 //
3615 // 5. String shall not contains invalid hexadecimal digits.
3616 //
3617 for (Index = 0; Index < Length; Index++) {
3618 if (!InternalAsciiIsHexaDecimalDigitCharacter (String[Index])) {
3619 break;
3620 }
3621 }
3622 if (Index != Length) {
3623 return RETURN_UNSUPPORTED;
3624 }
3625
3626 //
3627 // Convert the hex string to bytes.
3628 //
3629 for(Index = 0; Index < Length; Index++) {
3630
3631 //
3632 // For even characters, write the upper nibble for each buffer byte,
3633 // and for even characters, the lower nibble.
3634 //
3635 if ((Index & BIT0) == 0) {
3636 Buffer[Index / 2] = (UINT8) InternalAsciiHexCharToUintn (String[Index]) << 4;
3637 } else {
3638 Buffer[Index / 2] |= (UINT8) InternalAsciiHexCharToUintn (String[Index]);
3639 }
3640 }
3641 return RETURN_SUCCESS;
3642 }
3643