1 /******************************************************************************
3 * Module Name: cmclib - Local implementation of C library functions
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2007, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
120 #include <contrib/dev/acpica/acpi.h>
123 * These implementations of standard C Library routines can optionally be
124 * used if a C library is not available. In general, they are less efficient
125 * than an inline or assembly implementation
128 #define _COMPONENT ACPI_UTILITIES
129 ACPI_MODULE_NAME ("cmclib")
132 #ifndef ACPI_USE_SYSTEM_CLIBRARY
138 /*******************************************************************************
140 * FUNCTION: AcpiUtMemcmp (memcmp)
142 * PARAMETERS: Buffer1 - First Buffer
143 * Buffer2 - Second Buffer
144 * Count - Maximum # of bytes to compare
146 * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
148 * DESCRIPTION: Compare two Buffers, with a maximum length
150 ******************************************************************************/
159 for ( ; Count-- && (*Buffer1 == *Buffer2); Buffer1++, Buffer2++)
163 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *Buffer1 -
164 (unsigned char) *Buffer2));
168 /*******************************************************************************
170 * FUNCTION: AcpiUtMemcpy (memcpy)
172 * PARAMETERS: Dest - Target of the copy
173 * Src - Source buffer to copy
174 * Count - Number of bytes to copy
178 * DESCRIPTION: Copy arbitrary bytes of memory
180 ******************************************************************************/
188 char *New = (char *) Dest;
189 char *Old = (char *) Src;
204 /*******************************************************************************
206 * FUNCTION: AcpiUtMemset (memset)
208 * PARAMETERS: Dest - Buffer to set
209 * Value - Value to set each byte of memory
210 * Count - Number of bytes to set
214 * DESCRIPTION: Initialize a buffer to a known value.
216 ******************************************************************************/
221 ACPI_NATIVE_UINT Value,
224 char *New = (char *) Dest;
238 /*******************************************************************************
240 * FUNCTION: AcpiUtStrlen (strlen)
242 * PARAMETERS: String - Null terminated string
246 * DESCRIPTION: Returns the length of the input string
248 ******************************************************************************/
258 /* Count the string until a null is encountered */
270 /*******************************************************************************
272 * FUNCTION: AcpiUtStrcpy (strcpy)
274 * PARAMETERS: DstString - Target of the copy
275 * SrcString - The source string to copy
279 * DESCRIPTION: Copy a null terminated string
281 ******************************************************************************/
286 const char *SrcString)
288 char *String = DstString;
291 /* Move bytes brute force */
295 *String = *SrcString;
308 /*******************************************************************************
310 * FUNCTION: AcpiUtStrncpy (strncpy)
312 * PARAMETERS: DstString - Target of the copy
313 * SrcString - The source string to copy
314 * Count - Maximum # of bytes to copy
318 * DESCRIPTION: Copy a null terminated string, with a maximum length
320 ******************************************************************************/
325 const char *SrcString,
328 char *String = DstString;
331 /* Copy the string */
333 for (String = DstString;
334 Count && (Count--, (*String++ = *SrcString++)); )
337 /* Pad with nulls if necessary */
345 /* Return original pointer */
351 /*******************************************************************************
353 * FUNCTION: AcpiUtStrcmp (strcmp)
355 * PARAMETERS: String1 - First string
356 * String2 - Second string
358 * RETURN: Index where strings mismatched, or 0 if strings matched
360 * DESCRIPTION: Compare two null terminated strings
362 ******************************************************************************/
371 for ( ; (*String1 == *String2); String2++)
379 return ((unsigned char) *String1 - (unsigned char) *String2);
383 #ifdef ACPI_FUTURE_IMPLEMENTATION
384 /* Not used at this time */
385 /*******************************************************************************
387 * FUNCTION: AcpiUtStrchr (strchr)
389 * PARAMETERS: String - Search string
390 * ch - character to search for
392 * RETURN: Ptr to char or NULL if not found
394 * DESCRIPTION: Search a string for a character
396 ******************************************************************************/
405 for ( ; (*String); String++)
407 if ((*String) == (char) ch)
409 return ((char *) String);
417 /*******************************************************************************
419 * FUNCTION: AcpiUtStrncmp (strncmp)
421 * PARAMETERS: String1 - First string
422 * String2 - Second string
423 * Count - Maximum # of bytes to compare
425 * RETURN: Index where strings mismatched, or 0 if strings matched
427 * DESCRIPTION: Compare two null terminated strings, with a maximum length
429 ******************************************************************************/
439 for ( ; Count-- && (*String1 == *String2); String2++)
447 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *String1 -
448 (unsigned char) *String2));
452 /*******************************************************************************
454 * FUNCTION: AcpiUtStrcat (Strcat)
456 * PARAMETERS: DstString - Target of the copy
457 * SrcString - The source string to copy
461 * DESCRIPTION: Append a null terminated string to a null terminated string
463 ******************************************************************************/
468 const char *SrcString)
473 /* Find end of the destination string */
475 for (String = DstString; *String++; )
478 /* Concatenate the string */
480 for (--String; (*String++ = *SrcString++); )
487 /*******************************************************************************
489 * FUNCTION: AcpiUtStrncat (strncat)
491 * PARAMETERS: DstString - Target of the copy
492 * SrcString - The source string to copy
493 * Count - Maximum # of bytes to copy
497 * DESCRIPTION: Append a null terminated string to a null terminated string,
498 * with a maximum count.
500 ******************************************************************************/
505 const char *SrcString,
513 /* Find end of the destination string */
515 for (String = DstString; *String++; )
518 /* Concatenate the string */
520 for (--String; (*String++ = *SrcString++) && --Count; )
523 /* Null terminate if necessary */
535 /*******************************************************************************
537 * FUNCTION: AcpiUtStrstr (strstr)
539 * PARAMETERS: String1 - Target string
540 * String2 - Substring to search for
542 * RETURN: Where substring match starts, Null if no match found
544 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
545 * full implementation of strstr, only sufficient for command
548 ******************************************************************************/
558 if (AcpiUtStrlen (String2) > AcpiUtStrlen (String1))
563 /* Walk entire string, comparing the letters */
565 for (String = String1; *String2; )
567 if (*String2 != *String)
580 /*******************************************************************************
582 * FUNCTION: AcpiUtStrtoul (strtoul)
584 * PARAMETERS: String - Null terminated string
585 * Terminater - Where a pointer to the terminating byte is
587 * Base - Radix of the string
589 * RETURN: Converted value
591 * DESCRIPTION: Convert a string into a 32-bit unsigned value.
592 * Note: use AcpiUtStrtoul64 for 64-bit integers.
594 ******************************************************************************/
602 UINT32 converted = 0;
605 const char *StringStart;
606 UINT32 ReturnValue = 0;
607 ACPI_STATUS Status = AE_OK;
611 * Save the value of the pointer to the buffer's first
612 * character, save the current errno value, and then
613 * skip over any white space in the buffer:
615 StringStart = String;
616 while (ACPI_IS_SPACE (*String) || *String == '\t')
622 * The buffer may contain an optional plus or minus sign.
623 * If it does, then skip over it but remember what is was:
630 else if (*String == '+')
641 * If the input parameter Base is zero, then we need to
642 * determine if it is octal, decimal, or hexadecimal:
648 if (AcpiUtToLower (*(++String)) == 'x')
663 else if (Base < 2 || Base > 36)
666 * The specified Base parameter is not in the domain of
673 * For octal and hexadecimal bases, skip over the leading
674 * 0 or 0x, if they are present.
676 if (Base == 8 && *String == '0')
683 AcpiUtToLower (*(++String)) == 'x')
689 * Main loop: convert the string to an unsigned long:
693 if (ACPI_IS_DIGIT (*String))
695 index = (UINT32) ((UINT8) *String - '0');
699 index = (UINT32) AcpiUtToUpper (*String);
700 if (ACPI_IS_UPPER (index))
702 index = index - 'A' + 10;
716 * Check to see if value is out of range:
719 if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) /
723 ReturnValue = 0; /* reset */
728 ReturnValue += index;
737 * If appropriate, update the caller's pointer to the next
738 * unconverted character in the buffer.
742 if (converted == 0 && ReturnValue == 0 && String != NULL)
744 *Terminator = (char *) StringStart;
748 *Terminator = (char *) String;
752 if (Status == AE_ERROR)
754 ReturnValue = ACPI_UINT32_MAX;
758 * If a minus sign was present, then "the conversion is negated":
760 if (sign == NEGATIVE)
762 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
765 return (ReturnValue);
769 /*******************************************************************************
771 * FUNCTION: AcpiUtToUpper (TOUPPER)
773 * PARAMETERS: c - Character to convert
775 * RETURN: Converted character as an int
777 * DESCRIPTION: Convert character to uppercase
779 ******************************************************************************/
786 return (ACPI_IS_LOWER(c) ? ((c)-0x20) : (c));
790 /*******************************************************************************
792 * FUNCTION: AcpiUtToLower (TOLOWER)
794 * PARAMETERS: c - Character to convert
796 * RETURN: Converted character as an int
798 * DESCRIPTION: Convert character to lowercase
800 ******************************************************************************/
807 return (ACPI_IS_UPPER(c) ? ((c)+0x20) : (c));
811 /*******************************************************************************
813 * FUNCTION: is* functions
815 * DESCRIPTION: is* functions use the ctype table below
817 ******************************************************************************/
819 const UINT8 _acpi_ctype[257] = {
820 _ACPI_CN, /* 0x0 0. */
821 _ACPI_CN, /* 0x1 1. */
822 _ACPI_CN, /* 0x2 2. */
823 _ACPI_CN, /* 0x3 3. */
824 _ACPI_CN, /* 0x4 4. */
825 _ACPI_CN, /* 0x5 5. */
826 _ACPI_CN, /* 0x6 6. */
827 _ACPI_CN, /* 0x7 7. */
828 _ACPI_CN, /* 0x8 8. */
829 _ACPI_CN|_ACPI_SP, /* 0x9 9. */
830 _ACPI_CN|_ACPI_SP, /* 0xA 10. */
831 _ACPI_CN|_ACPI_SP, /* 0xB 11. */
832 _ACPI_CN|_ACPI_SP, /* 0xC 12. */
833 _ACPI_CN|_ACPI_SP, /* 0xD 13. */
834 _ACPI_CN, /* 0xE 14. */
835 _ACPI_CN, /* 0xF 15. */
836 _ACPI_CN, /* 0x10 16. */
837 _ACPI_CN, /* 0x11 17. */
838 _ACPI_CN, /* 0x12 18. */
839 _ACPI_CN, /* 0x13 19. */
840 _ACPI_CN, /* 0x14 20. */
841 _ACPI_CN, /* 0x15 21. */
842 _ACPI_CN, /* 0x16 22. */
843 _ACPI_CN, /* 0x17 23. */
844 _ACPI_CN, /* 0x18 24. */
845 _ACPI_CN, /* 0x19 25. */
846 _ACPI_CN, /* 0x1A 26. */
847 _ACPI_CN, /* 0x1B 27. */
848 _ACPI_CN, /* 0x1C 28. */
849 _ACPI_CN, /* 0x1D 29. */
850 _ACPI_CN, /* 0x1E 30. */
851 _ACPI_CN, /* 0x1F 31. */
852 _ACPI_XS|_ACPI_SP, /* 0x20 32. ' ' */
853 _ACPI_PU, /* 0x21 33. '!' */
854 _ACPI_PU, /* 0x22 34. '"' */
855 _ACPI_PU, /* 0x23 35. '#' */
856 _ACPI_PU, /* 0x24 36. '$' */
857 _ACPI_PU, /* 0x25 37. '%' */
858 _ACPI_PU, /* 0x26 38. '&' */
859 _ACPI_PU, /* 0x27 39. ''' */
860 _ACPI_PU, /* 0x28 40. '(' */
861 _ACPI_PU, /* 0x29 41. ')' */
862 _ACPI_PU, /* 0x2A 42. '*' */
863 _ACPI_PU, /* 0x2B 43. '+' */
864 _ACPI_PU, /* 0x2C 44. ',' */
865 _ACPI_PU, /* 0x2D 45. '-' */
866 _ACPI_PU, /* 0x2E 46. '.' */
867 _ACPI_PU, /* 0x2F 47. '/' */
868 _ACPI_XD|_ACPI_DI, /* 0x30 48. '0' */
869 _ACPI_XD|_ACPI_DI, /* 0x31 49. '1' */
870 _ACPI_XD|_ACPI_DI, /* 0x32 50. '2' */
871 _ACPI_XD|_ACPI_DI, /* 0x33 51. '3' */
872 _ACPI_XD|_ACPI_DI, /* 0x34 52. '4' */
873 _ACPI_XD|_ACPI_DI, /* 0x35 53. '5' */
874 _ACPI_XD|_ACPI_DI, /* 0x36 54. '6' */
875 _ACPI_XD|_ACPI_DI, /* 0x37 55. '7' */
876 _ACPI_XD|_ACPI_DI, /* 0x38 56. '8' */
877 _ACPI_XD|_ACPI_DI, /* 0x39 57. '9' */
878 _ACPI_PU, /* 0x3A 58. ':' */
879 _ACPI_PU, /* 0x3B 59. ';' */
880 _ACPI_PU, /* 0x3C 60. '<' */
881 _ACPI_PU, /* 0x3D 61. '=' */
882 _ACPI_PU, /* 0x3E 62. '>' */
883 _ACPI_PU, /* 0x3F 63. '?' */
884 _ACPI_PU, /* 0x40 64. '@' */
885 _ACPI_XD|_ACPI_UP, /* 0x41 65. 'A' */
886 _ACPI_XD|_ACPI_UP, /* 0x42 66. 'B' */
887 _ACPI_XD|_ACPI_UP, /* 0x43 67. 'C' */
888 _ACPI_XD|_ACPI_UP, /* 0x44 68. 'D' */
889 _ACPI_XD|_ACPI_UP, /* 0x45 69. 'E' */
890 _ACPI_XD|_ACPI_UP, /* 0x46 70. 'F' */
891 _ACPI_UP, /* 0x47 71. 'G' */
892 _ACPI_UP, /* 0x48 72. 'H' */
893 _ACPI_UP, /* 0x49 73. 'I' */
894 _ACPI_UP, /* 0x4A 74. 'J' */
895 _ACPI_UP, /* 0x4B 75. 'K' */
896 _ACPI_UP, /* 0x4C 76. 'L' */
897 _ACPI_UP, /* 0x4D 77. 'M' */
898 _ACPI_UP, /* 0x4E 78. 'N' */
899 _ACPI_UP, /* 0x4F 79. 'O' */
900 _ACPI_UP, /* 0x50 80. 'P' */
901 _ACPI_UP, /* 0x51 81. 'Q' */
902 _ACPI_UP, /* 0x52 82. 'R' */
903 _ACPI_UP, /* 0x53 83. 'S' */
904 _ACPI_UP, /* 0x54 84. 'T' */
905 _ACPI_UP, /* 0x55 85. 'U' */
906 _ACPI_UP, /* 0x56 86. 'V' */
907 _ACPI_UP, /* 0x57 87. 'W' */
908 _ACPI_UP, /* 0x58 88. 'X' */
909 _ACPI_UP, /* 0x59 89. 'Y' */
910 _ACPI_UP, /* 0x5A 90. 'Z' */
911 _ACPI_PU, /* 0x5B 91. '[' */
912 _ACPI_PU, /* 0x5C 92. '\' */
913 _ACPI_PU, /* 0x5D 93. ']' */
914 _ACPI_PU, /* 0x5E 94. '^' */
915 _ACPI_PU, /* 0x5F 95. '_' */
916 _ACPI_PU, /* 0x60 96. '`' */
917 _ACPI_XD|_ACPI_LO, /* 0x61 97. 'a' */
918 _ACPI_XD|_ACPI_LO, /* 0x62 98. 'b' */
919 _ACPI_XD|_ACPI_LO, /* 0x63 99. 'c' */
920 _ACPI_XD|_ACPI_LO, /* 0x64 100. 'd' */
921 _ACPI_XD|_ACPI_LO, /* 0x65 101. 'e' */
922 _ACPI_XD|_ACPI_LO, /* 0x66 102. 'f' */
923 _ACPI_LO, /* 0x67 103. 'g' */
924 _ACPI_LO, /* 0x68 104. 'h' */
925 _ACPI_LO, /* 0x69 105. 'i' */
926 _ACPI_LO, /* 0x6A 106. 'j' */
927 _ACPI_LO, /* 0x6B 107. 'k' */
928 _ACPI_LO, /* 0x6C 108. 'l' */
929 _ACPI_LO, /* 0x6D 109. 'm' */
930 _ACPI_LO, /* 0x6E 110. 'n' */
931 _ACPI_LO, /* 0x6F 111. 'o' */
932 _ACPI_LO, /* 0x70 112. 'p' */
933 _ACPI_LO, /* 0x71 113. 'q' */
934 _ACPI_LO, /* 0x72 114. 'r' */
935 _ACPI_LO, /* 0x73 115. 's' */
936 _ACPI_LO, /* 0x74 116. 't' */
937 _ACPI_LO, /* 0x75 117. 'u' */
938 _ACPI_LO, /* 0x76 118. 'v' */
939 _ACPI_LO, /* 0x77 119. 'w' */
940 _ACPI_LO, /* 0x78 120. 'x' */
941 _ACPI_LO, /* 0x79 121. 'y' */
942 _ACPI_LO, /* 0x7A 122. 'z' */
943 _ACPI_PU, /* 0x7B 123. '{' */
944 _ACPI_PU, /* 0x7C 124. '|' */
945 _ACPI_PU, /* 0x7D 125. '}' */
946 _ACPI_PU, /* 0x7E 126. '~' */
947 _ACPI_CN, /* 0x7F 127. */
949 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
950 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
951 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
952 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
953 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
954 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
955 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
956 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xF0 to 0x100 */
960 #endif /* ACPI_USE_SYSTEM_CLIBRARY */