5edc6ef06211483ca6b1981197a7431b210353fb
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.
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.
15 #include "BaseLibInternals.h"
17 #define RSIZE_MAX (PcdGet32 (PcdMaximumUnicodeStringLength))
19 #define ASCII_RSIZE_MAX (PcdGet32 (PcdMaximumAsciiStringLength))
21 #define SAFE_STRING_CONSTRAINT_CHECK(Expression, Status) \
23 ASSERT (Expression); \
24 if (!(Expression)) { \
30 Returns if 2 memory blocks are overlapped.
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.
37 @retval TRUE 2 memory blocks are overlapped.
38 @retval FALSE 2 memory blocks are not overlapped.
41 InternalSafeStringIsOverlap (
48 if ((((UINTN
)Base1
>= (UINTN
)Base2
) && ((UINTN
)Base1
< (UINTN
)Base2
+ Size2
)) ||
49 (((UINTN
)Base2
>= (UINTN
)Base1
) && ((UINTN
)Base2
< (UINTN
)Base1
+ Size1
))) {
56 Returns if 2 Unicode strings are not overlapped.
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.
65 @retval TRUE 2 Unicode strings are NOT overlapped.
66 @retval FALSE 2 Unicode strings are overlapped.
69 InternalSafeStringNoStrOverlap (
76 return !InternalSafeStringIsOverlap (Str1
, Size1
* sizeof(CHAR16
), Str2
, Size2
* sizeof(CHAR16
));
80 Returns if 2 Ascii strings are not overlapped.
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.
89 @retval TRUE 2 Ascii strings are NOT overlapped.
90 @retval FALSE 2 Ascii strings are overlapped.
93 InternalSafeStringNoAsciiStrOverlap (
100 return !InternalSafeStringIsOverlap (Str1
, Size1
, Str2
, Size2
);
104 Returns the length of a Null-terminated Unicode string.
106 This function is similar as strlen_s defined in C11.
108 If String is not aligned on a 16-bit boundary, then ASSERT().
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.
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.
122 IN CONST CHAR16
*String
,
128 ASSERT (((UINTN
) String
& BIT0
) == 0);
131 // If String is a null pointer, then the StrnLenS function returns zero.
133 if (String
== NULL
) {
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.
144 while (String
[Length
] != 0) {
145 if (Length
>= MaxSize
- 1) {
154 Returns the size of a Null-terminated Unicode string in bytes, including the
157 This function returns the size of the Null-terminated Unicode string
158 specified by String in bytes, including the Null terminator.
160 If String is not aligned on a 16-bit boundary, then ASSERT().
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.
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
170 @return The size of the Null-terminated Unicode string in bytes, including
177 IN CONST CHAR16
*String
,
182 // If String is a null pointer, then the StrnSizeS function returns zero.
184 if (String
== NULL
) {
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.
195 return (StrnLenS (String
, MaxSize
) + 1) * sizeof (*String
);
199 Copies the string pointed to by Source (including the terminating null char)
200 to the array pointed to by Destination.
202 This function is similar as strcpy_s defined in C11.
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().
208 If an error is returned, then the Destination is unmodified.
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.
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.
219 If PcdMaximumUnicodeStringLength is not zero,
220 and DestMax is greater than
221 PcdMaximumUnicodeStringLength.
223 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
228 OUT CHAR16
*Destination
,
230 IN CONST CHAR16
*Source
235 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
236 ASSERT (((UINTN
) Source
& BIT0
) == 0);
239 // 1. Neither Destination nor Source shall be a null pointer.
241 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
242 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
245 // 2. DestMax shall not be greater than RSIZE_MAX.
247 if (RSIZE_MAX
!= 0) {
248 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
252 // 3. DestMax shall not equal zero.
254 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
257 // 4. DestMax shall be greater than StrnLenS(Source, DestMax).
259 SourceLen
= StrnLenS (Source
, DestMax
);
260 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
263 // 5. Copying shall not take place between objects that overlap.
265 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination
, DestMax
, (CHAR16
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
268 // The StrCpyS function copies the string pointed to by Source (including the terminating
269 // null character) into the array pointed to by Destination.
271 while (*Source
!= 0) {
272 *(Destination
++) = *(Source
++);
276 return RETURN_SUCCESS
;
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.
284 This function is similar as strncpy_s defined in C11.
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().
290 If an error is returned, then the Destination is unmodified.
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.
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.
303 If PcdMaximumUnicodeStringLength is not zero,
304 and DestMax is greater than
305 PcdMaximumUnicodeStringLength.
307 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
312 OUT CHAR16
*Destination
,
314 IN CONST CHAR16
*Source
,
320 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
321 ASSERT (((UINTN
) Source
& BIT0
) == 0);
324 // 1. Neither Destination nor Source shall be a null pointer.
326 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
327 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
330 // 2. Neither DestMax nor Length shall be greater than RSIZE_MAX
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
);
338 // 3. DestMax shall not equal zero.
340 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
343 // 4. If Length is not less than DestMax, then DestMax shall be greater than StrnLenS(Source, DestMax).
345 SourceLen
= StrnLenS (Source
, DestMax
);
346 if (Length
>= DestMax
) {
347 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
351 // 5. Copying shall not take place between objects that overlap.
353 if (SourceLen
> Length
) {
356 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination
, DestMax
, (CHAR16
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
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
364 while ((*Source
!= 0) && (SourceLen
> 0)) {
365 *(Destination
++) = *(Source
++);
370 return RETURN_SUCCESS
;
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.
377 This function is similar as strcat_s defined in C11.
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().
383 If an error is returned, then the Destination is unmodified.
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.
390 @retval RETURN_SUCCESS String is appended.
391 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
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.
397 If PcdMaximumUnicodeStringLength is not zero,
398 and DestMax is greater than
399 PcdMaximumUnicodeStringLength.
401 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
406 IN OUT CHAR16
*Destination
,
408 IN CONST CHAR16
*Source
415 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
416 ASSERT (((UINTN
) Source
& BIT0
) == 0);
419 // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrCatS.
421 DestLen
= StrnLenS (Destination
, DestMax
);
422 CopyLen
= DestMax
- DestLen
;
425 // 1. Neither Destination nor Source shall be a null pointer.
427 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
428 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
431 // 2. DestMax shall not be greater than RSIZE_MAX.
433 if (RSIZE_MAX
!= 0) {
434 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
438 // 3. DestMax shall not equal zero.
440 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
443 // 4. CopyLen shall not equal zero.
445 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
!= 0), RETURN_BAD_BUFFER_SIZE
);
448 // 5. CopyLen shall be greater than StrnLenS(Source, CopyLen).
450 SourceLen
= StrnLenS (Source
, CopyLen
);
451 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
454 // 6. Copying shall not take place between objects that overlap.
456 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination
, DestMax
, (CHAR16
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
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.
463 Destination
= Destination
+ DestLen
;
464 while (*Source
!= 0) {
465 *(Destination
++) = *(Source
++);
469 return RETURN_SUCCESS
;
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
478 This function is similar as strncat_s defined in C11.
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().
484 If an error is returned, then the Destination is unmodified.
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.
492 @retval RETURN_SUCCESS String is appended.
493 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
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.
499 If PcdMaximumUnicodeStringLength is not zero,
500 and DestMax is greater than
501 PcdMaximumUnicodeStringLength.
503 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
508 IN OUT CHAR16
*Destination
,
510 IN CONST CHAR16
*Source
,
518 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
519 ASSERT (((UINTN
) Source
& BIT0
) == 0);
522 // Let CopyLen denote the value DestMax - StrnLenS(Destination, DestMax) upon entry to StrnCatS.
524 DestLen
= StrnLenS (Destination
, DestMax
);
525 CopyLen
= DestMax
- DestLen
;
528 // 1. Neither Destination nor Source shall be a null pointer.
530 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
531 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
534 // 2. Neither DestMax nor Length shall be greater than RSIZE_MAX.
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
);
542 // 3. DestMax shall not equal zero.
544 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
547 // 4. CopyLen shall not equal zero.
549 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
!= 0), RETURN_BAD_BUFFER_SIZE
);
552 // 5. If Length is not less than CopyLen, then CopyLen shall be greater than StrnLenS(Source, CopyLen).
554 SourceLen
= StrnLenS (Source
, CopyLen
);
555 if (Length
>= CopyLen
) {
556 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
560 // 6. Copying shall not take place between objects that overlap.
562 if (SourceLen
> Length
) {
565 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoStrOverlap (Destination
, DestMax
, (CHAR16
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
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
574 Destination
= Destination
+ DestLen
;
575 while ((*Source
!= 0) && (SourceLen
> 0)) {
576 *(Destination
++) = *(Source
++);
581 return RETURN_SUCCESS
;
585 Convert a Null-terminated Unicode decimal string to a value of type UINTN.
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:
591 [spaces] [decimal digits].
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.
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().
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.
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.
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.
620 @retval RETURN_SUCCESS Value is translated from String.
621 @retval RETURN_INVALID_PARAMETER If String is NULL.
623 If PcdMaximumUnicodeStringLength is not
624 zero, and String contains more than
625 PcdMaximumUnicodeStringLength Unicode
626 characters, not including the
628 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
629 the range defined by UINTN.
635 IN CONST CHAR16
*String
,
636 OUT CHAR16
**EndPointer
, OPTIONAL
640 ASSERT (((UINTN
) String
& BIT0
) == 0);
643 // 1. Neither String nor Data shall be a null pointer.
645 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
646 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
649 // 2. The length of String shall not be greater than RSIZE_MAX.
651 if (RSIZE_MAX
!= 0) {
652 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
655 if (EndPointer
!= NULL
) {
656 *EndPointer
= (CHAR16
*) String
;
660 // Ignore the pad spaces (space or tab)
662 while ((*String
== L
' ') || (*String
== L
'\t')) {
667 // Ignore leading Zeros after the spaces
669 while (*String
== L
'0') {
675 while (InternalIsDecimalDigitCharacter (*String
)) {
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.
681 if (*Data
> ((MAX_UINTN
- (*String
- L
'0')) / 10)) {
683 if (EndPointer
!= NULL
) {
684 *EndPointer
= (CHAR16
*) String
;
686 return RETURN_UNSUPPORTED
;
689 *Data
= *Data
* 10 + (*String
- L
'0');
693 if (EndPointer
!= NULL
) {
694 *EndPointer
= (CHAR16
*) String
;
696 return RETURN_SUCCESS
;
700 Convert a Null-terminated Unicode decimal string to a value of type UINT64.
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:
706 [spaces] [decimal digits].
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.
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().
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.
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.
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.
735 @retval RETURN_SUCCESS Value is translated from String.
736 @retval RETURN_INVALID_PARAMETER If String is NULL.
738 If PcdMaximumUnicodeStringLength is not
739 zero, and String contains more than
740 PcdMaximumUnicodeStringLength Unicode
741 characters, not including the
743 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
744 the range defined by UINT64.
749 StrDecimalToUint64S (
750 IN CONST CHAR16
*String
,
751 OUT CHAR16
**EndPointer
, OPTIONAL
755 ASSERT (((UINTN
) String
& BIT0
) == 0);
758 // 1. Neither String nor Data shall be a null pointer.
760 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
761 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
764 // 2. The length of String shall not be greater than RSIZE_MAX.
766 if (RSIZE_MAX
!= 0) {
767 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
770 if (EndPointer
!= NULL
) {
771 *EndPointer
= (CHAR16
*) String
;
775 // Ignore the pad spaces (space or tab)
777 while ((*String
== L
' ') || (*String
== L
'\t')) {
782 // Ignore leading Zeros after the spaces
784 while (*String
== L
'0') {
790 while (InternalIsDecimalDigitCharacter (*String
)) {
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.
796 if (*Data
> DivU64x32 (MAX_UINT64
- (*String
- L
'0'), 10)) {
798 if (EndPointer
!= NULL
) {
799 *EndPointer
= (CHAR16
*) String
;
801 return RETURN_UNSUPPORTED
;
804 *Data
= MultU64x32 (*Data
, 10) + (*String
- L
'0');
808 if (EndPointer
!= NULL
) {
809 *EndPointer
= (CHAR16
*) String
;
811 return RETURN_SUCCESS
;
815 Convert a Null-terminated Unicode hexadecimal string to a value of type
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:
822 [spaces][zeros][x][hexadecimal digits].
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.
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().
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.
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.
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.
855 @retval RETURN_SUCCESS Value is translated from String.
856 @retval RETURN_INVALID_PARAMETER If String is NULL.
858 If PcdMaximumUnicodeStringLength is not
859 zero, and String contains more than
860 PcdMaximumUnicodeStringLength Unicode
861 characters, not including the
863 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
864 the range defined by UINTN.
870 IN CONST CHAR16
*String
,
871 OUT CHAR16
**EndPointer
, OPTIONAL
875 ASSERT (((UINTN
) String
& BIT0
) == 0);
878 // 1. Neither String nor Data shall be a null pointer.
880 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
881 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
884 // 2. The length of String shall not be greater than RSIZE_MAX.
886 if (RSIZE_MAX
!= 0) {
887 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
890 if (EndPointer
!= NULL
) {
891 *EndPointer
= (CHAR16
*) String
;
895 // Ignore the pad spaces (space or tab)
897 while ((*String
== L
' ') || (*String
== L
'\t')) {
902 // Ignore leading Zeros after the spaces
904 while (*String
== L
'0') {
908 if (InternalCharToUpper (*String
) == L
'X') {
909 if (*(String
- 1) != L
'0') {
911 return RETURN_SUCCESS
;
921 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
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.
927 if (*Data
> ((MAX_UINTN
- InternalHexCharToUintn (*String
)) >> 4)) {
929 if (EndPointer
!= NULL
) {
930 *EndPointer
= (CHAR16
*) String
;
932 return RETURN_UNSUPPORTED
;
935 *Data
= (*Data
<< 4) + InternalHexCharToUintn (*String
);
939 if (EndPointer
!= NULL
) {
940 *EndPointer
= (CHAR16
*) String
;
942 return RETURN_SUCCESS
;
946 Convert a Null-terminated Unicode hexadecimal string to a value of type
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:
953 [spaces][zeros][x][hexadecimal digits].
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.
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().
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.
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.
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.
986 @retval RETURN_SUCCESS Value is translated from String.
987 @retval RETURN_INVALID_PARAMETER If String is NULL.
989 If PcdMaximumUnicodeStringLength is not
990 zero, and String contains more than
991 PcdMaximumUnicodeStringLength Unicode
992 characters, not including the
994 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
995 the range defined by UINT64.
1001 IN CONST CHAR16
*String
,
1002 OUT CHAR16
**EndPointer
, OPTIONAL
1006 ASSERT (((UINTN
) String
& BIT0
) == 0);
1009 // 1. Neither String nor Data shall be a null pointer.
1011 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
1012 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
1015 // 2. The length of String shall not be greater than RSIZE_MAX.
1017 if (RSIZE_MAX
!= 0) {
1018 SAFE_STRING_CONSTRAINT_CHECK ((StrnLenS (String
, RSIZE_MAX
+ 1) <= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1021 if (EndPointer
!= NULL
) {
1022 *EndPointer
= (CHAR16
*) String
;
1026 // Ignore the pad spaces (space or tab)
1028 while ((*String
== L
' ') || (*String
== L
'\t')) {
1033 // Ignore leading Zeros after the spaces
1035 while (*String
== L
'0') {
1039 if (InternalCharToUpper (*String
) == L
'X') {
1040 if (*(String
- 1) != L
'0') {
1042 return RETURN_SUCCESS
;
1052 while (InternalIsHexaDecimalDigitCharacter (*String
)) {
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.
1058 if (*Data
> RShiftU64 (MAX_UINT64
- InternalHexCharToUintn (*String
), 4)) {
1060 if (EndPointer
!= NULL
) {
1061 *EndPointer
= (CHAR16
*) String
;
1063 return RETURN_UNSUPPORTED
;
1066 *Data
= LShiftU64 (*Data
, 4) + InternalHexCharToUintn (*String
);
1070 if (EndPointer
!= NULL
) {
1071 *EndPointer
= (CHAR16
*) String
;
1073 return RETURN_SUCCESS
;
1077 Returns the length of a Null-terminated Ascii string.
1079 This function is similar as strlen_s defined in C11.
1081 @param String A pointer to a Null-terminated Ascii string.
1082 @param MaxSize The maximum number of Destination Ascii
1083 char, including terminating null char.
1085 @retval 0 If String is NULL.
1086 @retval MaxSize If there is no null character in the first MaxSize characters of String.
1087 @return The number of characters that percede the terminating null character.
1093 IN CONST CHAR8
*String
,
1100 // If String is a null pointer, then the AsciiStrnLenS function returns zero.
1102 if (String
== NULL
) {
1107 // Otherwise, the AsciiStrnLenS function returns the number of characters that precede the
1108 // terminating null character. If there is no null character in the first MaxSize characters of
1109 // String then AsciiStrnLenS returns MaxSize. At most the first MaxSize characters of String shall
1110 // be accessed by AsciiStrnLenS.
1113 while (String
[Length
] != 0) {
1114 if (Length
>= MaxSize
- 1) {
1123 Returns the size of a Null-terminated Ascii string in bytes, including the
1126 This function returns the size of the Null-terminated Ascii string specified
1127 by String in bytes, including the Null terminator.
1129 @param String A pointer to a Null-terminated Ascii string.
1130 @param MaxSize The maximum number of Destination Ascii
1131 char, including the Null terminator.
1133 @retval 0 If String is NULL.
1134 @retval (sizeof (CHAR8) * (MaxSize + 1))
1135 If there is no Null terminator in the first MaxSize characters of
1137 @return The size of the Null-terminated Ascii string in bytes, including the
1144 IN CONST CHAR8
*String
,
1149 // If String is a null pointer, then the AsciiStrnSizeS function returns
1152 if (String
== NULL
) {
1157 // Otherwise, the AsciiStrnSizeS function returns the size of the
1158 // Null-terminated Ascii string in bytes, including the Null terminator. If
1159 // there is no Null terminator in the first MaxSize characters of String,
1160 // then AsciiStrnSizeS returns (sizeof (CHAR8) * (MaxSize + 1)) to keep a
1161 // consistent map with the AsciiStrnLenS function.
1163 return (AsciiStrnLenS (String
, MaxSize
) + 1) * sizeof (*String
);
1167 Copies the string pointed to by Source (including the terminating null char)
1168 to the array pointed to by Destination.
1170 This function is similar as strcpy_s defined in C11.
1172 If an error would be returned, then the function will also ASSERT().
1174 If an error is returned, then the Destination is unmodified.
1176 @param Destination A pointer to a Null-terminated Ascii string.
1177 @param DestMax The maximum number of Destination Ascii
1178 char, including terminating null char.
1179 @param Source A pointer to a Null-terminated Ascii string.
1181 @retval RETURN_SUCCESS String is copied.
1182 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
1183 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1185 If PcdMaximumAsciiStringLength is not zero,
1186 and DestMax is greater than
1187 PcdMaximumAsciiStringLength.
1189 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1194 OUT CHAR8
*Destination
,
1196 IN CONST CHAR8
*Source
1202 // 1. Neither Destination nor Source shall be a null pointer.
1204 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
1205 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
1208 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX.
1210 if (ASCII_RSIZE_MAX
!= 0) {
1211 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1215 // 3. DestMax shall not equal zero.
1217 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
1220 // 4. DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
1222 SourceLen
= AsciiStrnLenS (Source
, DestMax
);
1223 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
1226 // 5. Copying shall not take place between objects that overlap.
1228 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination
, DestMax
, (CHAR8
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
1231 // The AsciiStrCpyS function copies the string pointed to by Source (including the terminating
1232 // null character) into the array pointed to by Destination.
1234 while (*Source
!= 0) {
1235 *(Destination
++) = *(Source
++);
1239 return RETURN_SUCCESS
;
1243 Copies not more than Length successive char from the string pointed to by
1244 Source to the array pointed to by Destination. If no null char is copied from
1245 Source, then Destination[Length] is always set to null.
1247 This function is similar as strncpy_s defined in C11.
1249 If an error would be returned, then the function will also ASSERT().
1251 If an error is returned, then the Destination is unmodified.
1253 @param Destination A pointer to a Null-terminated Ascii string.
1254 @param DestMax The maximum number of Destination Ascii
1255 char, including terminating null char.
1256 @param Source A pointer to a Null-terminated Ascii string.
1257 @param Length The maximum number of Ascii characters to copy.
1259 @retval RETURN_SUCCESS String is copied.
1260 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than
1261 MIN(StrLen(Source), Length).
1262 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1264 If PcdMaximumAsciiStringLength is not zero,
1265 and DestMax is greater than
1266 PcdMaximumAsciiStringLength.
1268 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1273 OUT CHAR8
*Destination
,
1275 IN CONST CHAR8
*Source
,
1282 // 1. Neither Destination nor Source shall be a null pointer.
1284 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
1285 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
1288 // 2. Neither DestMax nor Length shall be greater than ASCII_RSIZE_MAX
1290 if (ASCII_RSIZE_MAX
!= 0) {
1291 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1292 SAFE_STRING_CONSTRAINT_CHECK ((Length
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1296 // 3. DestMax shall not equal zero.
1298 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
1301 // 4. If Length is not less than DestMax, then DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
1303 SourceLen
= AsciiStrnLenS (Source
, DestMax
);
1304 if (Length
>= DestMax
) {
1305 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
1309 // 5. Copying shall not take place between objects that overlap.
1311 if (SourceLen
> Length
) {
1314 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination
, DestMax
, (CHAR8
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
1317 // The AsciiStrnCpyS function copies not more than Length successive characters (characters that
1318 // follow a null character are not copied) from the array pointed to by Source to the array
1319 // pointed to by Destination. If no null character was copied from Source, then Destination[Length] is set to a null
1322 while ((*Source
!= 0) && (SourceLen
> 0)) {
1323 *(Destination
++) = *(Source
++);
1328 return RETURN_SUCCESS
;
1332 Appends a copy of the string pointed to by Source (including the terminating
1333 null char) to the end of the string pointed to by Destination.
1335 This function is similar as strcat_s defined in C11.
1337 If an error would be returned, then the function will also ASSERT().
1339 If an error is returned, then the Destination is unmodified.
1341 @param Destination A pointer to a Null-terminated Ascii string.
1342 @param DestMax The maximum number of Destination Ascii
1343 char, including terminating null char.
1344 @param Source A pointer to a Null-terminated Ascii string.
1346 @retval RETURN_SUCCESS String is appended.
1347 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
1348 StrLen(Destination).
1349 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
1350 greater than StrLen(Source).
1351 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1353 If PcdMaximumAsciiStringLength is not zero,
1354 and DestMax is greater than
1355 PcdMaximumAsciiStringLength.
1357 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1362 IN OUT CHAR8
*Destination
,
1364 IN CONST CHAR8
*Source
1372 // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrCatS.
1374 DestLen
= AsciiStrnLenS (Destination
, DestMax
);
1375 CopyLen
= DestMax
- DestLen
;
1378 // 1. Neither Destination nor Source shall be a null pointer.
1380 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
1381 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
1384 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX.
1386 if (ASCII_RSIZE_MAX
!= 0) {
1387 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1391 // 3. DestMax shall not equal zero.
1393 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
1396 // 4. CopyLen shall not equal zero.
1398 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
!= 0), RETURN_BAD_BUFFER_SIZE
);
1401 // 5. CopyLen shall be greater than AsciiStrnLenS(Source, CopyLen).
1403 SourceLen
= AsciiStrnLenS (Source
, CopyLen
);
1404 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
1407 // 6. Copying shall not take place between objects that overlap.
1409 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination
, DestMax
, (CHAR8
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
1412 // The AsciiStrCatS function appends a copy of the string pointed to by Source (including the
1413 // terminating null character) to the end of the string pointed to by Destination. The initial character
1414 // from Source overwrites the null character at the end of Destination.
1416 Destination
= Destination
+ DestLen
;
1417 while (*Source
!= 0) {
1418 *(Destination
++) = *(Source
++);
1422 return RETURN_SUCCESS
;
1426 Appends not more than Length successive char from the string pointed to by
1427 Source to the end of the string pointed to by Destination. If no null char is
1428 copied from Source, then Destination[StrLen(Destination) + Length] is always
1431 This function is similar as strncat_s defined in C11.
1433 If an error would be returned, then the function will also ASSERT().
1435 If an error is returned, then the Destination is unmodified.
1437 @param Destination A pointer to a Null-terminated Ascii string.
1438 @param DestMax The maximum number of Destination Ascii
1439 char, including terminating null char.
1440 @param Source A pointer to a Null-terminated Ascii string.
1441 @param Length The maximum number of Ascii characters to copy.
1443 @retval RETURN_SUCCESS String is appended.
1444 @retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
1445 StrLen(Destination).
1446 @retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
1447 greater than MIN(StrLen(Source), Length).
1448 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
1450 If PcdMaximumAsciiStringLength is not zero,
1451 and DestMax is greater than
1452 PcdMaximumAsciiStringLength.
1454 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
1459 IN OUT CHAR8
*Destination
,
1461 IN CONST CHAR8
*Source
,
1470 // Let CopyLen denote the value DestMax - AsciiStrnLenS(Destination, DestMax) upon entry to AsciiStrnCatS.
1472 DestLen
= AsciiStrnLenS (Destination
, DestMax
);
1473 CopyLen
= DestMax
- DestLen
;
1476 // 1. Neither Destination nor Source shall be a null pointer.
1478 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
1479 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
1482 // 2. Neither DestMax nor Length shall be greater than ASCII_RSIZE_MAX.
1484 if (ASCII_RSIZE_MAX
!= 0) {
1485 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1486 SAFE_STRING_CONSTRAINT_CHECK ((Length
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1490 // 3. DestMax shall not equal zero.
1492 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
1495 // 4. CopyLen shall not equal zero.
1497 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
!= 0), RETURN_BAD_BUFFER_SIZE
);
1500 // 5. If Length is not less than CopyLen, then CopyLen shall be greater than AsciiStrnLenS(Source, CopyLen).
1502 SourceLen
= AsciiStrnLenS (Source
, CopyLen
);
1503 if (Length
>= CopyLen
) {
1504 SAFE_STRING_CONSTRAINT_CHECK ((CopyLen
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
1508 // 6. Copying shall not take place between objects that overlap.
1510 if (SourceLen
> Length
) {
1513 SAFE_STRING_CONSTRAINT_CHECK (InternalSafeStringNoAsciiStrOverlap (Destination
, DestMax
, (CHAR8
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
1516 // The AsciiStrnCatS function appends not more than Length successive characters (characters
1517 // that follow a null character are not copied) from the array pointed to by Source to the end of
1518 // the string pointed to by Destination. The initial character from Source overwrites the null character at
1519 // the end of Destination. If no null character was copied from Source, then Destination[DestMax-CopyLen+Length] is set to
1520 // a null character.
1522 Destination
= Destination
+ DestLen
;
1523 while ((*Source
!= 0) && (SourceLen
> 0)) {
1524 *(Destination
++) = *(Source
++);
1529 return RETURN_SUCCESS
;
1533 Convert a Null-terminated Ascii decimal string to a value of type UINTN.
1535 This function outputs a value of type UINTN by interpreting the contents of
1536 the Ascii string specified by String as a decimal number. The format of the
1537 input Ascii string String is:
1539 [spaces] [decimal digits].
1541 The valid decimal digit character is in the range [0-9]. The function will
1542 ignore the pad space, which includes spaces or tab characters, before
1543 [decimal digits]. The running zero in the beginning of [decimal digits] will
1544 be ignored. Then, the function stops at the first character that is a not a
1545 valid decimal character or a Null-terminator, whichever one comes first.
1547 If String is NULL, then ASSERT().
1548 If Data is NULL, then ASSERT().
1549 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1550 PcdMaximumAsciiStringLength Ascii characters, not including the
1551 Null-terminator, then ASSERT().
1553 If String has no valid decimal digits in the above format, then 0 is stored
1554 at the location pointed to by Data.
1555 If the number represented by String exceeds the range defined by UINTN, then
1556 MAX_UINTN is stored at the location pointed to by Data.
1558 If EndPointer is not NULL, a pointer to the character that stopped the scan
1559 is stored at the location pointed to by EndPointer. If String has no valid
1560 decimal digits right after the optional pad spaces, the value of String is
1561 stored at the location pointed to by EndPointer.
1563 @param String Pointer to a Null-terminated Ascii string.
1564 @param EndPointer Pointer to character that stops scan.
1565 @param Data Pointer to the converted value.
1567 @retval RETURN_SUCCESS Value is translated from String.
1568 @retval RETURN_INVALID_PARAMETER If String is NULL.
1570 If PcdMaximumAsciiStringLength is not zero,
1571 and String contains more than
1572 PcdMaximumAsciiStringLength Ascii
1573 characters, not including the
1575 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
1576 the range defined by UINTN.
1581 AsciiStrDecimalToUintnS (
1582 IN CONST CHAR8
*String
,
1583 OUT CHAR8
**EndPointer
, OPTIONAL
1588 // 1. Neither String nor Data shall be a null pointer.
1590 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
1591 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
1594 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
1596 if (ASCII_RSIZE_MAX
!= 0) {
1597 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1600 if (EndPointer
!= NULL
) {
1601 *EndPointer
= (CHAR8
*) String
;
1605 // Ignore the pad spaces (space or tab)
1607 while ((*String
== ' ') || (*String
== '\t')) {
1612 // Ignore leading Zeros after the spaces
1614 while (*String
== '0') {
1620 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1622 // If the number represented by String overflows according to the range
1623 // defined by UINTN, then MAX_UINTN is stored in *Data and
1624 // RETURN_UNSUPPORTED is returned.
1626 if (*Data
> ((MAX_UINTN
- (*String
- '0')) / 10)) {
1628 if (EndPointer
!= NULL
) {
1629 *EndPointer
= (CHAR8
*) String
;
1631 return RETURN_UNSUPPORTED
;
1634 *Data
= *Data
* 10 + (*String
- '0');
1638 if (EndPointer
!= NULL
) {
1639 *EndPointer
= (CHAR8
*) String
;
1641 return RETURN_SUCCESS
;
1645 Convert a Null-terminated Ascii decimal string to a value of type UINT64.
1647 This function outputs a value of type UINT64 by interpreting the contents of
1648 the Ascii string specified by String as a decimal number. The format of the
1649 input Ascii string String is:
1651 [spaces] [decimal digits].
1653 The valid decimal digit character is in the range [0-9]. The function will
1654 ignore the pad space, which includes spaces or tab characters, before
1655 [decimal digits]. The running zero in the beginning of [decimal digits] will
1656 be ignored. Then, the function stops at the first character that is a not a
1657 valid decimal character or a Null-terminator, whichever one comes first.
1659 If String is NULL, then ASSERT().
1660 If Data is NULL, then ASSERT().
1661 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1662 PcdMaximumAsciiStringLength Ascii characters, not including the
1663 Null-terminator, then ASSERT().
1665 If String has no valid decimal digits in the above format, then 0 is stored
1666 at the location pointed to by Data.
1667 If the number represented by String exceeds the range defined by UINT64, then
1668 MAX_UINT64 is stored at the location pointed to by Data.
1670 If EndPointer is not NULL, a pointer to the character that stopped the scan
1671 is stored at the location pointed to by EndPointer. If String has no valid
1672 decimal digits right after the optional pad spaces, the value of String is
1673 stored at the location pointed to by EndPointer.
1675 @param String Pointer to a Null-terminated Ascii string.
1676 @param EndPointer Pointer to character that stops scan.
1677 @param Data Pointer to the converted value.
1679 @retval RETURN_SUCCESS Value is translated from String.
1680 @retval RETURN_INVALID_PARAMETER If String is NULL.
1682 If PcdMaximumAsciiStringLength is not zero,
1683 and String contains more than
1684 PcdMaximumAsciiStringLength Ascii
1685 characters, not including the
1687 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
1688 the range defined by UINT64.
1693 AsciiStrDecimalToUint64S (
1694 IN CONST CHAR8
*String
,
1695 OUT CHAR8
**EndPointer
, OPTIONAL
1700 // 1. Neither String nor Data shall be a null pointer.
1702 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
1703 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
1706 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
1708 if (ASCII_RSIZE_MAX
!= 0) {
1709 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1712 if (EndPointer
!= NULL
) {
1713 *EndPointer
= (CHAR8
*) String
;
1717 // Ignore the pad spaces (space or tab)
1719 while ((*String
== ' ') || (*String
== '\t')) {
1724 // Ignore leading Zeros after the spaces
1726 while (*String
== '0') {
1732 while (InternalAsciiIsDecimalDigitCharacter (*String
)) {
1734 // If the number represented by String overflows according to the range
1735 // defined by UINT64, then MAX_UINT64 is stored in *Data and
1736 // RETURN_UNSUPPORTED is returned.
1738 if (*Data
> DivU64x32 (MAX_UINT64
- (*String
- '0'), 10)) {
1740 if (EndPointer
!= NULL
) {
1741 *EndPointer
= (CHAR8
*) String
;
1743 return RETURN_UNSUPPORTED
;
1746 *Data
= MultU64x32 (*Data
, 10) + (*String
- '0');
1750 if (EndPointer
!= NULL
) {
1751 *EndPointer
= (CHAR8
*) String
;
1753 return RETURN_SUCCESS
;
1757 Convert a Null-terminated Ascii hexadecimal string to a value of type UINTN.
1759 This function outputs a value of type UINTN by interpreting the contents of
1760 the Ascii string specified by String as a hexadecimal number. The format of
1761 the input Ascii string String is:
1763 [spaces][zeros][x][hexadecimal digits].
1765 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1766 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
1767 "x" appears in the input string, it must be prefixed with at least one 0. The
1768 function will ignore the pad space, which includes spaces or tab characters,
1769 before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
1770 [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
1771 the first valid hexadecimal digit. Then, the function stops at the first
1772 character that is a not a valid hexadecimal character or Null-terminator,
1773 whichever on comes first.
1775 If String is NULL, then ASSERT().
1776 If Data is NULL, then ASSERT().
1777 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1778 PcdMaximumAsciiStringLength Ascii characters, not including the
1779 Null-terminator, then ASSERT().
1781 If String has no valid hexadecimal digits in the above format, then 0 is
1782 stored at the location pointed to by Data.
1783 If the number represented by String exceeds the range defined by UINTN, then
1784 MAX_UINTN is stored at the location pointed to by Data.
1786 If EndPointer is not NULL, a pointer to the character that stopped the scan
1787 is stored at the location pointed to by EndPointer. If String has no valid
1788 hexadecimal digits right after the optional pad spaces, the value of String
1789 is stored at the location pointed to by EndPointer.
1791 @param String Pointer to a Null-terminated Ascii string.
1792 @param EndPointer Pointer to character that stops scan.
1793 @param Data Pointer to the converted value.
1795 @retval RETURN_SUCCESS Value is translated from String.
1796 @retval RETURN_INVALID_PARAMETER If String is NULL.
1798 If PcdMaximumAsciiStringLength is not zero,
1799 and String contains more than
1800 PcdMaximumAsciiStringLength Ascii
1801 characters, not including the
1803 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
1804 the range defined by UINTN.
1809 AsciiStrHexToUintnS (
1810 IN CONST CHAR8
*String
,
1811 OUT CHAR8
**EndPointer
, OPTIONAL
1816 // 1. Neither String nor Data shall be a null pointer.
1818 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
1819 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
1822 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
1824 if (ASCII_RSIZE_MAX
!= 0) {
1825 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1828 if (EndPointer
!= NULL
) {
1829 *EndPointer
= (CHAR8
*) String
;
1833 // Ignore the pad spaces (space or tab)
1835 while ((*String
== ' ') || (*String
== '\t')) {
1840 // Ignore leading Zeros after the spaces
1842 while (*String
== '0') {
1846 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1847 if (*(String
- 1) != '0') {
1849 return RETURN_SUCCESS
;
1859 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1861 // If the number represented by String overflows according to the range
1862 // defined by UINTN, then MAX_UINTN is stored in *Data and
1863 // RETURN_UNSUPPORTED is returned.
1865 if (*Data
> ((MAX_UINTN
- InternalAsciiHexCharToUintn (*String
)) >> 4)) {
1867 if (EndPointer
!= NULL
) {
1868 *EndPointer
= (CHAR8
*) String
;
1870 return RETURN_UNSUPPORTED
;
1873 *Data
= (*Data
<< 4) + InternalAsciiHexCharToUintn (*String
);
1877 if (EndPointer
!= NULL
) {
1878 *EndPointer
= (CHAR8
*) String
;
1880 return RETURN_SUCCESS
;
1884 Convert a Null-terminated Ascii hexadecimal string to a value of type UINT64.
1886 This function outputs a value of type UINT64 by interpreting the contents of
1887 the Ascii string specified by String as a hexadecimal number. The format of
1888 the input Ascii string String is:
1890 [spaces][zeros][x][hexadecimal digits].
1892 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1893 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If
1894 "x" appears in the input string, it must be prefixed with at least one 0. The
1895 function will ignore the pad space, which includes spaces or tab characters,
1896 before [zeros], [x] or [hexadecimal digits]. The running zero before [x] or
1897 [hexadecimal digits] will be ignored. Then, the decoding starts after [x] or
1898 the first valid hexadecimal digit. Then, the function stops at the first
1899 character that is a not a valid hexadecimal character or Null-terminator,
1900 whichever on comes first.
1902 If String is NULL, then ASSERT().
1903 If Data is NULL, then ASSERT().
1904 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1905 PcdMaximumAsciiStringLength Ascii characters, not including the
1906 Null-terminator, then ASSERT().
1908 If String has no valid hexadecimal digits in the above format, then 0 is
1909 stored at the location pointed to by Data.
1910 If the number represented by String exceeds the range defined by UINT64, then
1911 MAX_UINT64 is stored at the location pointed to by Data.
1913 If EndPointer is not NULL, a pointer to the character that stopped the scan
1914 is stored at the location pointed to by EndPointer. If String has no valid
1915 hexadecimal digits right after the optional pad spaces, the value of String
1916 is stored at the location pointed to by EndPointer.
1918 @param String Pointer to a Null-terminated Ascii string.
1919 @param EndPointer Pointer to character that stops scan.
1920 @param Data Pointer to the converted value.
1922 @retval RETURN_SUCCESS Value is translated from String.
1923 @retval RETURN_INVALID_PARAMETER If String is NULL.
1925 If PcdMaximumAsciiStringLength is not zero,
1926 and String contains more than
1927 PcdMaximumAsciiStringLength Ascii
1928 characters, not including the
1930 @retval RETURN_UNSUPPORTED If the number represented by String exceeds
1931 the range defined by UINT64.
1936 AsciiStrHexToUint64S (
1937 IN CONST CHAR8
*String
,
1938 OUT CHAR8
**EndPointer
, OPTIONAL
1943 // 1. Neither String nor Data shall be a null pointer.
1945 SAFE_STRING_CONSTRAINT_CHECK ((String
!= NULL
), RETURN_INVALID_PARAMETER
);
1946 SAFE_STRING_CONSTRAINT_CHECK ((Data
!= NULL
), RETURN_INVALID_PARAMETER
);
1949 // 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
1951 if (ASCII_RSIZE_MAX
!= 0) {
1952 SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String
, ASCII_RSIZE_MAX
+ 1) <= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
1955 if (EndPointer
!= NULL
) {
1956 *EndPointer
= (CHAR8
*) String
;
1960 // Ignore the pad spaces (space or tab)
1962 while ((*String
== ' ') || (*String
== '\t')) {
1967 // Ignore leading Zeros after the spaces
1969 while (*String
== '0') {
1973 if (InternalBaseLibAsciiToUpper (*String
) == 'X') {
1974 if (*(String
- 1) != '0') {
1976 return RETURN_SUCCESS
;
1986 while (InternalAsciiIsHexaDecimalDigitCharacter (*String
)) {
1988 // If the number represented by String overflows according to the range
1989 // defined by UINT64, then MAX_UINT64 is stored in *Data and
1990 // RETURN_UNSUPPORTED is returned.
1992 if (*Data
> RShiftU64 (MAX_UINT64
- InternalAsciiHexCharToUintn (*String
), 4)) {
1994 if (EndPointer
!= NULL
) {
1995 *EndPointer
= (CHAR8
*) String
;
1997 return RETURN_UNSUPPORTED
;
2000 *Data
= LShiftU64 (*Data
, 4) + InternalAsciiHexCharToUintn (*String
);
2004 if (EndPointer
!= NULL
) {
2005 *EndPointer
= (CHAR8
*) String
;
2007 return RETURN_SUCCESS
;
2011 Convert a Null-terminated Unicode string to a Null-terminated
2014 This function is similar to AsciiStrCpyS.
2016 This function converts the content of the Unicode string Source
2017 to the ASCII string Destination by copying the lower 8 bits of
2018 each Unicode character. The function terminates the ASCII string
2019 Destination by appending a Null-terminator character at the end.
2021 The caller is responsible to make sure Destination points to a buffer with size
2022 equal or greater than ((StrLen (Source) + 1) * sizeof (CHAR8)) in bytes.
2024 If any Unicode characters in Source contain non-zero value in
2025 the upper 8 bits, then ASSERT().
2027 If Source is not aligned on a 16-bit boundary, then ASSERT().
2028 If an error would be returned, then the function will also ASSERT().
2030 If an error is returned, then the Destination is unmodified.
2032 @param Source The pointer to a Null-terminated Unicode string.
2033 @param Destination The pointer to a Null-terminated ASCII string.
2034 @param DestMax The maximum number of Destination Ascii
2035 char, including terminating null char.
2037 @retval RETURN_SUCCESS String is converted.
2038 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
2039 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2041 If PcdMaximumAsciiStringLength is not zero,
2042 and DestMax is greater than
2043 PcdMaximumAsciiStringLength.
2044 If PcdMaximumUnicodeStringLength is not zero,
2045 and DestMax is greater than
2046 PcdMaximumUnicodeStringLength.
2048 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2053 UnicodeStrToAsciiStrS (
2054 IN CONST CHAR16
*Source
,
2055 OUT CHAR8
*Destination
,
2061 ASSERT (((UINTN
) Source
& BIT0
) == 0);
2064 // 1. Neither Destination nor Source shall be a null pointer.
2066 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
2067 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
2070 // 2. DestMax shall not be greater than ASCII_RSIZE_MAX or RSIZE_MAX.
2072 if (ASCII_RSIZE_MAX
!= 0) {
2073 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
2075 if (RSIZE_MAX
!= 0) {
2076 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
2080 // 3. DestMax shall not equal zero.
2082 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
2085 // 4. DestMax shall be greater than StrnLenS (Source, DestMax).
2087 SourceLen
= StrnLenS (Source
, DestMax
);
2088 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
2091 // 5. Copying shall not take place between objects that overlap.
2093 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination
, DestMax
, (VOID
*)Source
, (SourceLen
+ 1) * sizeof(CHAR16
)), RETURN_ACCESS_DENIED
);
2098 while (*Source
!= '\0') {
2100 // If any Unicode characters in Source contain
2101 // non-zero value in the upper 8 bits, then ASSERT().
2103 ASSERT (*Source
< 0x100);
2104 *(Destination
++) = (CHAR8
) *(Source
++);
2106 *Destination
= '\0';
2108 return RETURN_SUCCESS
;
2113 Convert one Null-terminated ASCII string to a Null-terminated
2116 This function is similar to StrCpyS.
2118 This function converts the contents of the ASCII string Source to the Unicode
2119 string Destination. The function terminates the Unicode string Destination by
2120 appending a Null-terminator character at the end.
2122 The caller is responsible to make sure Destination points to a buffer with size
2123 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
2125 If Destination is not aligned on a 16-bit boundary, then ASSERT().
2126 If an error would be returned, then the function will also ASSERT().
2128 If an error is returned, then the Destination is unmodified.
2130 @param Source The pointer to a Null-terminated ASCII string.
2131 @param Destination The pointer to a Null-terminated Unicode string.
2132 @param DestMax The maximum number of Destination Unicode
2133 char, including terminating null char.
2135 @retval RETURN_SUCCESS String is converted.
2136 @retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
2137 @retval RETURN_INVALID_PARAMETER If Destination is NULL.
2139 If PcdMaximumUnicodeStringLength is not zero,
2140 and DestMax is greater than
2141 PcdMaximumUnicodeStringLength.
2142 If PcdMaximumAsciiStringLength is not zero,
2143 and DestMax is greater than
2144 PcdMaximumAsciiStringLength.
2146 @retval RETURN_ACCESS_DENIED If Source and Destination overlap.
2151 AsciiStrToUnicodeStrS (
2152 IN CONST CHAR8
*Source
,
2153 OUT CHAR16
*Destination
,
2159 ASSERT (((UINTN
) Destination
& BIT0
) == 0);
2162 // 1. Neither Destination nor Source shall be a null pointer.
2164 SAFE_STRING_CONSTRAINT_CHECK ((Destination
!= NULL
), RETURN_INVALID_PARAMETER
);
2165 SAFE_STRING_CONSTRAINT_CHECK ((Source
!= NULL
), RETURN_INVALID_PARAMETER
);
2168 // 2. DestMax shall not be greater than RSIZE_MAX or ASCII_RSIZE_MAX.
2170 if (RSIZE_MAX
!= 0) {
2171 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
2173 if (ASCII_RSIZE_MAX
!= 0) {
2174 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
<= ASCII_RSIZE_MAX
), RETURN_INVALID_PARAMETER
);
2178 // 3. DestMax shall not equal zero.
2180 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
!= 0), RETURN_INVALID_PARAMETER
);
2183 // 4. DestMax shall be greater than AsciiStrnLenS(Source, DestMax).
2185 SourceLen
= AsciiStrnLenS (Source
, DestMax
);
2186 SAFE_STRING_CONSTRAINT_CHECK ((DestMax
> SourceLen
), RETURN_BUFFER_TOO_SMALL
);
2189 // 5. Copying shall not take place between objects that overlap.
2191 SAFE_STRING_CONSTRAINT_CHECK (!InternalSafeStringIsOverlap (Destination
, DestMax
* sizeof(CHAR16
), (VOID
*)Source
, SourceLen
+ 1), RETURN_ACCESS_DENIED
);
2196 while (*Source
!= '\0') {
2197 *(Destination
++) = (CHAR16
)*(Source
++);
2199 *Destination
= '\0';
2201 return RETURN_SUCCESS
;