]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EfiCommonLib/String.c
3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Unicode string primatives
23 #include "EfiDriverLib.h"
24 #include "EfiCommonLib.h"
28 IN CHAR16
*Destination
,
34 Copy the Unicode string Source to Destination.
37 Destination - Location to copy string
38 Source - String to copy
46 *(Destination
++) = *(Source
++);
60 Copy a string from source to destination
63 Dst Destination string
65 Length Length of destination string
74 SrcLen
= EfiStrLen (Src
);
77 while (Index
< Length
&& Index
< SrcLen
) {
78 Dst
[Index
] = Src
[Index
];
81 for (Index
= SrcLen
; Index
< Length
; Index
++) {
93 Return the number of Unicode characters in String. This is not the same as
94 the length of the string in bytes.
97 String - String to process
100 Number of Unicode characters in String
106 for (Length
=0; *String
; String
++, Length
++);
118 Return the number bytes in the Unicode String. This is not the same as
119 the length of the string in characters. The string size includes the NULL
122 String - String to process
125 Number of bytes in String
129 return ((EfiStrLen (String
) + 1) * sizeof (CHAR16
));
141 Compare the Unicode string pointed by String to the string pointed by String2.
144 String - String to process
146 String2 - The other string to process
149 Return a positive integer if String is lexicall greater than String2; Zero if
150 the two strings are identical; and a negative interger if String is lexically
156 if (*String
!= *String2
) {
164 return *String
- *String2
;
176 This function compares the Unicode string String to the Unicode
177 string String2 for len characters. If the first len characters
178 of String is identical to the first len characters of String2,
179 then 0 is returned. If substring of String sorts lexicographically
180 after String2, the function returns a number greater than 0. If
181 substring of String sorts lexicographically before String2, the
182 function returns a number less than 0.
185 String - Compare to String2
186 String2 - Compare to String
187 Length - Number of Unicode characters to compare
190 0 - The substring of String and String2 is identical.
191 > 0 - The substring of String sorts lexicographically after String2
192 < 0 - The substring of String sorts lexicographically before String2
196 while (*String
&& Length
!= 0) {
197 if (*String
!= *String2
) {
204 return Length
> 0 ? *String
- *String2
: 0;
209 IN CHAR16
*Destination
,
215 Concatinate Source on the end of Destination
218 Destination - String to added to the end of.
219 Source - String to concatinate.
226 EfiStrCpy (Destination
+ EfiStrLen (Destination
), Source
);
238 Concatinate Source on the end of Destination
241 Dst Destination string
243 Length Length of destination string
249 EfiStrnCpy (Dest
+ EfiStrLen (Dest
), Src
, Length
);
259 Return the number of Ascii characters in String. This is not the same as
260 the length of the string in bytes.
263 String - String to process
266 Number of Ascii characters in String
272 for (Length
=0; *String
; String
++, Length
++);
279 IN CHAR8
*Destination
,
285 Copy the Ascii string Source to Destination.
288 Destination - Location to copy string
289 Source - String to copy
292 Pointer just pass the end of Destination
297 *(Destination
++) = *(Source
++);
300 return Destination
+ 1;
312 Copy the Ascii string from source to destination
315 Dst Destination string
317 Length Length of destination string
326 SrcLen
= EfiAsciiStrLen (Src
);
329 while (Index
< Length
&& Index
< SrcLen
) {
330 Dst
[Index
] = Src
[Index
];
333 for (Index
= SrcLen
; Index
< Length
; Index
++) {
345 Return the number bytes in the Ascii String. This is not the same as
346 the length of the string in characters. The string size includes the NULL
349 String - String to process
352 Number of bytes in String
356 return (EfiAsciiStrLen (String
) + 1);
368 Compare the Ascii string pointed by String to the string pointed by String2.
371 String - String to process
373 String2 - The other string to process
376 Return a positive integer if String is lexicall greater than String2; Zero if
377 the two strings are identical; and a negative interger if String is lexically
382 if (*String
!= *String2
) {
390 return *String
- *String2
;
404 while ((*String
!= '\0') &&
405 (*String
== *String2
) &&
411 return *String
- *String2
;
416 IN CHAR8
*Destination
,
422 Concatinate Source on the end of Destination
425 Destination - String to added to the end of.
426 Source - String to concatinate.
433 EfiAsciiStrCpy (Destination
+ EfiAsciiStrLen (Destination
), Source
);
438 IN CHAR8
*Destination
,
445 Concatinate Source on the end of Destination
448 Destination - String to added to the end of.
449 Source - String to concatinate.
456 EfiAsciiStrnCpy (Destination
+ EfiAsciiStrLen (Destination
), Source
, Length
);
467 Determines if a Unicode character is a hexadecimal digit.
468 The test is case insensitive.
471 Digit - Pointer to byte that receives the value of the hex character.
472 Char - Unicode character to test.
475 TRUE - If the character is a hexadecimal digit.
480 if ((Char
>= L
'0') && (Char
<= L
'9')) {
481 *Digit
= (UINT8
) (Char
- L
'0');
485 if ((Char
>= L
'A') && (Char
<= L
'F')) {
486 *Digit
= (UINT8
) (Char
- L
'A' + 0x0A);
490 if ((Char
>= L
'a') && (Char
<= L
'f')) {
491 *Digit
= (UINT8
) (Char
- L
'a' + 0x0A);
505 Converts the low nibble of a byte to hex unicode character.
508 Nibble - lower nibble of a byte.
511 Hex unicode character.
517 return (CHAR16
)(Nibble
+ L
'0');
520 return (CHAR16
)(Nibble
- 0xA + L
'A');
528 OUT UINTN
*ConvertedStrLen OPTIONAL
533 Converts Unicode string to binary buffer.
534 The conversion may be partial.
535 The first character in the string that is not hex digit stops the conversion.
536 At a minimum, any blob of data could be represented as a hex string.
539 Buf - Pointer to buffer that receives the data.
540 Len - Length in bytes of the buffer to hold converted data.
541 If routine return with EFI_SUCCESS, containing length of converted data.
542 If routine return with EFI_BUFFER_TOO_SMALL, containg length of buffer desired.
543 Str - String to be converted from.
544 ConvertedStrLen - Length of the Hex String consumed.
547 EFI_SUCCESS: Routine Success.
548 EFI_BUFFER_TOO_SMALL: The buffer is too small to hold converted data.
560 // Find out how many hex characters the string has.
562 for (Idx
= 0, HexCnt
= 0; IsHexDigit (&Digit
, Str
[Idx
]); Idx
++, HexCnt
++);
569 // Two Unicode characters make up 1 buffer byte. Round up.
571 BufferLength
= (HexCnt
+ 1) / 2;
574 // Test if buffer is passed enough.
576 if (BufferLength
> (*Len
)) {
578 return EFI_BUFFER_TOO_SMALL
;
583 for (Idx
= 0; Idx
< HexCnt
; Idx
++) {
585 IsHexDigit (&Digit
, Str
[HexCnt
- 1 - Idx
]);
588 // For odd charaters, write the lower nibble for each buffer byte,
589 // and for even characters, the upper nibble.
591 if ((Idx
& 1) == 0) {
602 if (ConvertedStrLen
!= NULL
) {
603 *ConvertedStrLen
= HexCnt
;
612 IN OUT UINTN
*HexStringBufferLength
,
619 Converts binary buffer to Unicode string.
620 At a minimum, any blob of data could be represented as a hex string.
623 Str - Pointer to the string.
624 HexStringBufferLength - Length in bytes of buffer to hold the hex string. Includes tailing '\0' character.
625 If routine return with EFI_SUCCESS, containing length of hex string buffer.
626 If routine return with EFI_BUFFER_TOO_SMALL, containg length of hex string buffer desired.
627 Buf - Buffer to be converted from.
628 Len - Length in bytes of the buffer to be converted.
631 EFI_SUCCESS: Routine success.
632 EFI_BUFFER_TOO_SMALL: The hex string buffer is too small.
641 // Make sure string is either passed or allocate enough.
642 // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.
643 // Plus the Unicode termination character.
646 if (StrLen
> ((*HexStringBufferLength
) - 1)) {
647 *HexStringBufferLength
= StrLen
+ 1;
648 return EFI_BUFFER_TOO_SMALL
;
651 *HexStringBufferLength
= StrLen
+ 1;
657 for (Idx
= 0; Idx
< Len
; Idx
++) {
660 Str
[StrLen
- 1 - Idx
* 2] = NibbleToHexChar (Byte
);
661 Str
[StrLen
- 2 - Idx
* 2] = NibbleToHexChar ((UINT8
)(Byte
>> 4));
676 Removes (trims) specified leading and trailing characters from a string.
680 str - Pointer to the null-terminated string to be trimmed. On return,
681 str will hold the trimmed string.
682 CharC - Character will be trimmed from str.
696 // Trim off the leading and trailing characters c
698 for (p1
= str
; *p1
&& *p1
== CharC
; p1
++) {
718 for (p1
= str
+ EfiStrLen(str
) - 1; p1
>= str
&& *p1
== CharC
; p1
--) {
721 if (p1
!= str
+ EfiStrLen(str
) - 1) {
728 IN CHAR16
*StrCharSet
738 String - Null-terminated string to search.
739 StrCharSet - Null-terminated string to search for.
742 The address of the first occurrence of the matching substring if successful, or NULL otherwise.
751 while ((*String
!= L
'\0') && (*StrCharSet
!= L
'\0')) {
752 if (*String
++ != *StrCharSet
++) {
757 if (*StrCharSet
== L
'\0') {
773 Find a Ascii substring.
777 String - Null-terminated Ascii string to search.
778 StrCharSet - Null-terminated Ascii string to search for.
781 The address of the first occurrence of the matching Ascii substring if successful, or NULL otherwise.
790 while ((*String
!= '\0') && (*StrCharSet
!= '\0')) {
791 if (*String
++ != *StrCharSet
++) {
796 if (*StrCharSet
== '\0') {