1 /******************************************************************************
3 * Module Name: utclib - ACPICA implementations of C library functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2018, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
49 * This module contains implementations of the standard C library functions
50 * that are required by the ACPICA code at both application level and kernel
53 * The module is an optional feature that can be used if a local/system
54 * C library is not available. Some operating system kernels may not have
55 * an internal C library.
57 * In general, these functions are less efficient than an inline or assembly
58 * code implementation.
60 * These C functions and the associated prototypes are enabled by default
61 * unless the ACPI_USE_SYSTEM_CLIBRARY symbol is defined. This is usually
62 * automatically defined for the ACPICA applications such as iASL and
63 * AcpiExec, so that these user-level applications use the local C library
64 * instead of the functions in this module.
67 /*******************************************************************************
69 * Functions implemented in this module:
86 * FUNCTION: is* functions
88 ******************************************************************************/
90 #define _COMPONENT ACPI_UTILITIES
91 ACPI_MODULE_NAME ("utclib")
94 #ifndef ACPI_USE_SYSTEM_CLIBRARY /* Entire module */
97 /*******************************************************************************
101 * PARAMETERS: Buffer1 - First Buffer
102 * Buffer2 - Second Buffer
103 * Count - Maximum # of bytes to compare
105 * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
107 * DESCRIPTION: Compare two Buffers, with a maximum length
109 ******************************************************************************/
117 char *Buffer1
= (char *) VBuffer1
;
118 char *Buffer2
= (char *) VBuffer2
;
121 for ( ; Count
-- && (*Buffer1
== *Buffer2
); Buffer1
++, Buffer2
++)
125 return ((Count
== ACPI_SIZE_MAX
) ? 0 : ((unsigned char) *Buffer1
-
126 (unsigned char) *Buffer2
));
130 /*******************************************************************************
134 * PARAMETERS: Dest - Target of the copy
135 * Src - Source buffer to copy
136 * Count - Number of bytes to copy
140 * DESCRIPTION: Copy arbitrary bytes of memory with respect to the overlapping
142 ******************************************************************************/
150 char *New
= (char *) Dest
;
151 char *Old
= (char *) Src
;
156 /* Copy from the beginning */
168 /* Copy from the end */
170 New
= New
+ Count
- 1;
171 Old
= Old
+ Count
- 1;
185 /*******************************************************************************
189 * PARAMETERS: Dest - Target of the copy
190 * Src - Source buffer to copy
191 * Count - Number of bytes to copy
195 * DESCRIPTION: Copy arbitrary bytes of memory
197 ******************************************************************************/
205 char *New
= (char *) Dest
;
206 char *Old
= (char *) Src
;
221 /*******************************************************************************
225 * PARAMETERS: Dest - Buffer to set
226 * Value - Value to set each byte of memory
227 * Count - Number of bytes to set
231 * DESCRIPTION: Initialize a buffer to a known value.
233 ******************************************************************************/
241 char *New
= (char *) Dest
;
255 /*******************************************************************************
259 * PARAMETERS: String - Null terminated string
263 * DESCRIPTION: Returns the length of the input string
265 ******************************************************************************/
275 /* Count the string until a null is encountered */
287 /*******************************************************************************
291 * PARAMETERS: String - Null terminated string
292 * Delimiters - Delimiters to match
294 * RETURN: The first occurance in the string of any of the bytes in the
297 * DESCRIPTION: Search a string for any of a set of the delimiters
299 ******************************************************************************/
304 const char *Delimiters
)
306 const char *Delimiter
;
309 for ( ; *String
!= '\0'; ++String
)
311 for (Delimiter
= Delimiters
; *Delimiter
!= '\0'; Delimiter
++)
313 if (*String
== *Delimiter
)
315 return (ACPI_CAST_PTR (char, String
));
324 /*******************************************************************************
328 * PARAMETERS: String - Null terminated string
329 * Delimiters - Delimiters to match
331 * RETURN: Pointer to the next token
333 * DESCRIPTION: Split string into tokens
335 ******************************************************************************/
340 const char *Delimiters
)
342 char *Begin
= String
;
343 static char *SavedPtr
;
348 if (SavedPtr
== NULL
)
355 SavedPtr
= strpbrk (Begin
, Delimiters
);
356 while (SavedPtr
== Begin
)
359 SavedPtr
= strpbrk (Begin
, Delimiters
);
374 /*******************************************************************************
378 * PARAMETERS: DstString - Target of the copy
379 * SrcString - The source string to copy
383 * DESCRIPTION: Copy a null terminated string
385 ******************************************************************************/
390 const char *SrcString
)
392 char *String
= DstString
;
395 /* Move bytes brute force */
399 *String
= *SrcString
;
412 /*******************************************************************************
416 * PARAMETERS: DstString - Target of the copy
417 * SrcString - The source string to copy
418 * Count - Maximum # of bytes to copy
422 * DESCRIPTION: Copy a null terminated string, with a maximum length
424 ******************************************************************************/
429 const char *SrcString
,
432 char *String
= DstString
;
435 /* Copy the string */
437 for (String
= DstString
;
438 Count
&& (Count
--, (*String
++ = *SrcString
++)); )
441 /* Pad with nulls if necessary */
449 /* Return original pointer */
455 /*******************************************************************************
459 * PARAMETERS: String1 - First string
460 * String2 - Second string
462 * RETURN: Index where strings mismatched, or 0 if strings matched
464 * DESCRIPTION: Compare two null terminated strings
466 ******************************************************************************/
475 for ( ; (*String1
== *String2
); String2
++)
483 return ((unsigned char) *String1
- (unsigned char) *String2
);
487 /*******************************************************************************
491 * PARAMETERS: String - Search string
492 * ch - character to search for
494 * RETURN: Ptr to char or NULL if not found
496 * DESCRIPTION: Search a string for a character
498 ******************************************************************************/
507 for ( ; (*String
); String
++)
509 if ((*String
) == (char) ch
)
511 return ((char *) String
);
519 /*******************************************************************************
523 * PARAMETERS: String1 - First string
524 * String2 - Second string
525 * Count - Maximum # of bytes to compare
527 * RETURN: Index where strings mismatched, or 0 if strings matched
529 * DESCRIPTION: Compare two null terminated strings, with a maximum length
531 ******************************************************************************/
541 for ( ; Count
-- && (*String1
== *String2
); String2
++)
549 return ((Count
== ACPI_SIZE_MAX
) ? 0 : ((unsigned char) *String1
-
550 (unsigned char) *String2
));
554 /*******************************************************************************
558 * PARAMETERS: DstString - Target of the copy
559 * SrcString - The source string to copy
563 * DESCRIPTION: Append a null terminated string to a null terminated string
565 ******************************************************************************/
570 const char *SrcString
)
575 /* Find end of the destination string */
577 for (String
= DstString
; *String
++; )
580 /* Concatenate the string */
582 for (--String
; (*String
++ = *SrcString
++); )
589 /*******************************************************************************
593 * PARAMETERS: DstString - Target of the copy
594 * SrcString - The source string to copy
595 * Count - Maximum # of bytes to copy
599 * DESCRIPTION: Append a null terminated string to a null terminated string,
600 * with a maximum count.
602 ******************************************************************************/
607 const char *SrcString
,
615 /* Find end of the destination string */
617 for (String
= DstString
; *String
++; )
620 /* Concatenate the string */
622 for (--String
; (*String
++ = *SrcString
++) && --Count
; )
625 /* Null terminate if necessary */
637 /*******************************************************************************
641 * PARAMETERS: String1 - Target string
642 * String2 - Substring to search for
644 * RETURN: Where substring match starts, Null if no match found
646 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
647 * full implementation of strstr, only sufficient for command
650 ******************************************************************************/
660 Length
= strlen (String2
);
666 while (strlen (String1
) >= Length
)
668 if (memcmp (String1
, String2
, Length
) == 0)
679 /*******************************************************************************
683 * PARAMETERS: String - Null terminated string
684 * Terminater - Where a pointer to the terminating byte is
686 * Base - Radix of the string
688 * RETURN: Converted value
690 * DESCRIPTION: Convert a string into a 32-bit unsigned value.
691 * Note: use strtoul64 for 64-bit integers.
693 ******************************************************************************/
701 UINT32 converted
= 0;
704 const char *StringStart
;
705 UINT32 ReturnValue
= 0;
706 ACPI_STATUS Status
= AE_OK
;
710 * Save the value of the pointer to the buffer's first
711 * character, save the current errno value, and then
712 * skip over any white space in the buffer:
714 StringStart
= String
;
715 while (isspace (*String
) || *String
== '\t')
721 * The buffer may contain an optional plus or minus sign.
722 * If it does, then skip over it but remember what is was:
726 sign
= ACPI_SIGN_NEGATIVE
;
729 else if (*String
== '+')
732 sign
= ACPI_SIGN_POSITIVE
;
736 sign
= ACPI_SIGN_POSITIVE
;
740 * If the input parameter Base is zero, then we need to
741 * determine if it is octal, decimal, or hexadecimal:
747 if (tolower (*(++String
)) == 'x')
762 else if (Base
< 2 || Base
> 36)
765 * The specified Base parameter is not in the domain of
772 * For octal and hexadecimal bases, skip over the leading
773 * 0 or 0x, if they are present.
775 if (Base
== 8 && *String
== '0')
782 tolower (*(++String
)) == 'x')
788 * Main loop: convert the string to an unsigned long:
792 if (isdigit (*String
))
794 index
= (UINT32
) ((UINT8
) *String
- '0');
798 index
= (UINT32
) toupper (*String
);
801 index
= index
- 'A' + 10;
815 * Check to see if value is out of range:
818 if (ReturnValue
> ((ACPI_UINT32_MAX
- (UINT32
) index
) /
822 ReturnValue
= 0; /* reset */
827 ReturnValue
+= index
;
836 * If appropriate, update the caller's pointer to the next
837 * unconverted character in the buffer.
841 if (converted
== 0 && ReturnValue
== 0 && String
!= NULL
)
843 *Terminator
= (char *) StringStart
;
847 *Terminator
= (char *) String
;
851 if (Status
== AE_ERROR
)
853 ReturnValue
= ACPI_UINT32_MAX
;
857 * If a minus sign was present, then "the conversion is negated":
859 if (sign
== ACPI_SIGN_NEGATIVE
)
861 ReturnValue
= (ACPI_UINT32_MAX
- ReturnValue
) + 1;
864 return (ReturnValue
);
868 /*******************************************************************************
872 * PARAMETERS: c - Character to convert
874 * RETURN: Converted character as an int
876 * DESCRIPTION: Convert character to uppercase
878 ******************************************************************************/
885 return (islower(c
) ? ((c
)-0x20) : (c
));
889 /*******************************************************************************
893 * PARAMETERS: c - Character to convert
895 * RETURN: Converted character as an int
897 * DESCRIPTION: Convert character to lowercase
899 ******************************************************************************/
906 return (isupper(c
) ? ((c
)+0x20) : (c
));
910 /*******************************************************************************
912 * FUNCTION: is* function array
914 * DESCRIPTION: is* functions use the ctype table below
916 ******************************************************************************/
918 const UINT8 AcpiGbl_Ctypes
[257] = {
919 _ACPI_CN
, /* 0x00 0 NUL */
920 _ACPI_CN
, /* 0x01 1 SOH */
921 _ACPI_CN
, /* 0x02 2 STX */
922 _ACPI_CN
, /* 0x03 3 ETX */
923 _ACPI_CN
, /* 0x04 4 EOT */
924 _ACPI_CN
, /* 0x05 5 ENQ */
925 _ACPI_CN
, /* 0x06 6 ACK */
926 _ACPI_CN
, /* 0x07 7 BEL */
927 _ACPI_CN
, /* 0x08 8 BS */
928 _ACPI_CN
|_ACPI_SP
, /* 0x09 9 TAB */
929 _ACPI_CN
|_ACPI_SP
, /* 0x0A 10 LF */
930 _ACPI_CN
|_ACPI_SP
, /* 0x0B 11 VT */
931 _ACPI_CN
|_ACPI_SP
, /* 0x0C 12 FF */
932 _ACPI_CN
|_ACPI_SP
, /* 0x0D 13 CR */
933 _ACPI_CN
, /* 0x0E 14 SO */
934 _ACPI_CN
, /* 0x0F 15 SI */
935 _ACPI_CN
, /* 0x10 16 DLE */
936 _ACPI_CN
, /* 0x11 17 DC1 */
937 _ACPI_CN
, /* 0x12 18 DC2 */
938 _ACPI_CN
, /* 0x13 19 DC3 */
939 _ACPI_CN
, /* 0x14 20 DC4 */
940 _ACPI_CN
, /* 0x15 21 NAK */
941 _ACPI_CN
, /* 0x16 22 SYN */
942 _ACPI_CN
, /* 0x17 23 ETB */
943 _ACPI_CN
, /* 0x18 24 CAN */
944 _ACPI_CN
, /* 0x19 25 EM */
945 _ACPI_CN
, /* 0x1A 26 SUB */
946 _ACPI_CN
, /* 0x1B 27 ESC */
947 _ACPI_CN
, /* 0x1C 28 FS */
948 _ACPI_CN
, /* 0x1D 29 GS */
949 _ACPI_CN
, /* 0x1E 30 RS */
950 _ACPI_CN
, /* 0x1F 31 US */
951 _ACPI_XS
|_ACPI_SP
, /* 0x20 32 ' ' */
952 _ACPI_PU
, /* 0x21 33 '!' */
953 _ACPI_PU
, /* 0x22 34 '"' */
954 _ACPI_PU
, /* 0x23 35 '#' */
955 _ACPI_PU
, /* 0x24 36 '$' */
956 _ACPI_PU
, /* 0x25 37 '%' */
957 _ACPI_PU
, /* 0x26 38 '&' */
958 _ACPI_PU
, /* 0x27 39 ''' */
959 _ACPI_PU
, /* 0x28 40 '(' */
960 _ACPI_PU
, /* 0x29 41 ')' */
961 _ACPI_PU
, /* 0x2A 42 '*' */
962 _ACPI_PU
, /* 0x2B 43 '+' */
963 _ACPI_PU
, /* 0x2C 44 ',' */
964 _ACPI_PU
, /* 0x2D 45 '-' */
965 _ACPI_PU
, /* 0x2E 46 '.' */
966 _ACPI_PU
, /* 0x2F 47 '/' */
967 _ACPI_XD
|_ACPI_DI
, /* 0x30 48 '0' */
968 _ACPI_XD
|_ACPI_DI
, /* 0x31 49 '1' */
969 _ACPI_XD
|_ACPI_DI
, /* 0x32 50 '2' */
970 _ACPI_XD
|_ACPI_DI
, /* 0x33 51 '3' */
971 _ACPI_XD
|_ACPI_DI
, /* 0x34 52 '4' */
972 _ACPI_XD
|_ACPI_DI
, /* 0x35 53 '5' */
973 _ACPI_XD
|_ACPI_DI
, /* 0x36 54 '6' */
974 _ACPI_XD
|_ACPI_DI
, /* 0x37 55 '7' */
975 _ACPI_XD
|_ACPI_DI
, /* 0x38 56 '8' */
976 _ACPI_XD
|_ACPI_DI
, /* 0x39 57 '9' */
977 _ACPI_PU
, /* 0x3A 58 ':' */
978 _ACPI_PU
, /* 0x3B 59 ';' */
979 _ACPI_PU
, /* 0x3C 60 '<' */
980 _ACPI_PU
, /* 0x3D 61 '=' */
981 _ACPI_PU
, /* 0x3E 62 '>' */
982 _ACPI_PU
, /* 0x3F 63 '?' */
983 _ACPI_PU
, /* 0x40 64 '@' */
984 _ACPI_XD
|_ACPI_UP
, /* 0x41 65 'A' */
985 _ACPI_XD
|_ACPI_UP
, /* 0x42 66 'B' */
986 _ACPI_XD
|_ACPI_UP
, /* 0x43 67 'C' */
987 _ACPI_XD
|_ACPI_UP
, /* 0x44 68 'D' */
988 _ACPI_XD
|_ACPI_UP
, /* 0x45 69 'E' */
989 _ACPI_XD
|_ACPI_UP
, /* 0x46 70 'F' */
990 _ACPI_UP
, /* 0x47 71 'G' */
991 _ACPI_UP
, /* 0x48 72 'H' */
992 _ACPI_UP
, /* 0x49 73 'I' */
993 _ACPI_UP
, /* 0x4A 74 'J' */
994 _ACPI_UP
, /* 0x4B 75 'K' */
995 _ACPI_UP
, /* 0x4C 76 'L' */
996 _ACPI_UP
, /* 0x4D 77 'M' */
997 _ACPI_UP
, /* 0x4E 78 'N' */
998 _ACPI_UP
, /* 0x4F 79 'O' */
999 _ACPI_UP
, /* 0x50 80 'P' */
1000 _ACPI_UP
, /* 0x51 81 'Q' */
1001 _ACPI_UP
, /* 0x52 82 'R' */
1002 _ACPI_UP
, /* 0x53 83 'S' */
1003 _ACPI_UP
, /* 0x54 84 'T' */
1004 _ACPI_UP
, /* 0x55 85 'U' */
1005 _ACPI_UP
, /* 0x56 86 'V' */
1006 _ACPI_UP
, /* 0x57 87 'W' */
1007 _ACPI_UP
, /* 0x58 88 'X' */
1008 _ACPI_UP
, /* 0x59 89 'Y' */
1009 _ACPI_UP
, /* 0x5A 90 'Z' */
1010 _ACPI_PU
, /* 0x5B 91 '[' */
1011 _ACPI_PU
, /* 0x5C 92 '\' */
1012 _ACPI_PU
, /* 0x5D 93 ']' */
1013 _ACPI_PU
, /* 0x5E 94 '^' */
1014 _ACPI_PU
, /* 0x5F 95 '_' */
1015 _ACPI_PU
, /* 0x60 96 '`' */
1016 _ACPI_XD
|_ACPI_LO
, /* 0x61 97 'a' */
1017 _ACPI_XD
|_ACPI_LO
, /* 0x62 98 'b' */
1018 _ACPI_XD
|_ACPI_LO
, /* 0x63 99 'c' */
1019 _ACPI_XD
|_ACPI_LO
, /* 0x64 100 'd' */
1020 _ACPI_XD
|_ACPI_LO
, /* 0x65 101 'e' */
1021 _ACPI_XD
|_ACPI_LO
, /* 0x66 102 'f' */
1022 _ACPI_LO
, /* 0x67 103 'g' */
1023 _ACPI_LO
, /* 0x68 104 'h' */
1024 _ACPI_LO
, /* 0x69 105 'i' */
1025 _ACPI_LO
, /* 0x6A 106 'j' */
1026 _ACPI_LO
, /* 0x6B 107 'k' */
1027 _ACPI_LO
, /* 0x6C 108 'l' */
1028 _ACPI_LO
, /* 0x6D 109 'm' */
1029 _ACPI_LO
, /* 0x6E 110 'n' */
1030 _ACPI_LO
, /* 0x6F 111 'o' */
1031 _ACPI_LO
, /* 0x70 112 'p' */
1032 _ACPI_LO
, /* 0x71 113 'q' */
1033 _ACPI_LO
, /* 0x72 114 'r' */
1034 _ACPI_LO
, /* 0x73 115 's' */
1035 _ACPI_LO
, /* 0x74 116 't' */
1036 _ACPI_LO
, /* 0x75 117 'u' */
1037 _ACPI_LO
, /* 0x76 118 'v' */
1038 _ACPI_LO
, /* 0x77 119 'w' */
1039 _ACPI_LO
, /* 0x78 120 'x' */
1040 _ACPI_LO
, /* 0x79 121 'y' */
1041 _ACPI_LO
, /* 0x7A 122 'z' */
1042 _ACPI_PU
, /* 0x7B 123 '{' */
1043 _ACPI_PU
, /* 0x7C 124 '|' */
1044 _ACPI_PU
, /* 0x7D 125 '}' */
1045 _ACPI_PU
, /* 0x7E 126 '~' */
1046 _ACPI_CN
, /* 0x7F 127 DEL */
1048 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
1049 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
1050 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
1051 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
1052 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
1053 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
1054 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
1055 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xF0 to 0xFF */
1060 #endif /* ACPI_USE_SYSTEM_CLIBRARY */