1 /******************************************************************************
3 * Module Name: cmclib - Local implementation of C library functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
51 * These implementations of standard C Library routines can optionally be
52 * used if a C library is not available. In general, they are less efficient
53 * than an inline or assembly implementation
56 #define _COMPONENT ACPI_UTILITIES
57 ACPI_MODULE_NAME ("cmclib")
60 #ifndef ACPI_USE_SYSTEM_CLIBRARY
66 /*******************************************************************************
68 * FUNCTION: AcpiUtMemcmp (memcmp)
70 * PARAMETERS: Buffer1 - First Buffer
71 * Buffer2 - Second Buffer
72 * Count - Maximum # of bytes to compare
74 * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
76 * DESCRIPTION: Compare two Buffers, with a maximum length
78 ******************************************************************************/
87 for ( ; Count-- && (*Buffer1 == *Buffer2); Buffer1++, Buffer2++)
91 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *Buffer1 -
92 (unsigned char) *Buffer2));
96 /*******************************************************************************
98 * FUNCTION: AcpiUtMemcpy (memcpy)
100 * PARAMETERS: Dest - Target of the copy
101 * Src - Source buffer to copy
102 * Count - Number of bytes to copy
106 * DESCRIPTION: Copy arbitrary bytes of memory
108 ******************************************************************************/
116 char *New = (char *) Dest;
117 char *Old = __DECONST(char *, Src);
132 /*******************************************************************************
134 * FUNCTION: AcpiUtMemset (memset)
136 * PARAMETERS: Dest - Buffer to set
137 * Value - Value to set each byte of memory
138 * Count - Number of bytes to set
142 * DESCRIPTION: Initialize a buffer to a known value.
144 ******************************************************************************/
152 char *New = (char *) Dest;
166 /*******************************************************************************
168 * FUNCTION: AcpiUtStrlen (strlen)
170 * PARAMETERS: String - Null terminated string
174 * DESCRIPTION: Returns the length of the input string
176 ******************************************************************************/
186 /* Count the string until a null is encountered */
198 /*******************************************************************************
200 * FUNCTION: AcpiUtStrcpy (strcpy)
202 * PARAMETERS: DstString - Target of the copy
203 * SrcString - The source string to copy
207 * DESCRIPTION: Copy a null terminated string
209 ******************************************************************************/
214 const char *SrcString)
216 char *String = DstString;
219 /* Move bytes brute force */
223 *String = *SrcString;
236 /*******************************************************************************
238 * FUNCTION: AcpiUtStrncpy (strncpy)
240 * PARAMETERS: DstString - Target of the copy
241 * SrcString - The source string to copy
242 * Count - Maximum # of bytes to copy
246 * DESCRIPTION: Copy a null terminated string, with a maximum length
248 ******************************************************************************/
253 const char *SrcString,
256 char *String = DstString;
259 /* Copy the string */
261 for (String = DstString;
262 Count && (Count--, (*String++ = *SrcString++)); )
265 /* Pad with nulls if necessary */
273 /* Return original pointer */
279 /*******************************************************************************
281 * FUNCTION: AcpiUtStrcmp (strcmp)
283 * PARAMETERS: String1 - First string
284 * String2 - Second string
286 * RETURN: Index where strings mismatched, or 0 if strings matched
288 * DESCRIPTION: Compare two null terminated strings
290 ******************************************************************************/
299 for ( ; (*String1 == *String2); String2++)
307 return ((unsigned char) *String1 - (unsigned char) *String2);
311 /*******************************************************************************
313 * FUNCTION: AcpiUtStrchr (strchr)
315 * PARAMETERS: String - Search string
316 * ch - character to search for
318 * RETURN: Ptr to char or NULL if not found
320 * DESCRIPTION: Search a string for a character
322 ******************************************************************************/
331 for ( ; (*String); String++)
333 if ((*String) == (char) ch)
335 return (__DECONST(char *, String));
343 /*******************************************************************************
345 * FUNCTION: AcpiUtStrncmp (strncmp)
347 * PARAMETERS: String1 - First string
348 * String2 - Second string
349 * Count - Maximum # of bytes to compare
351 * RETURN: Index where strings mismatched, or 0 if strings matched
353 * DESCRIPTION: Compare two null terminated strings, with a maximum length
355 ******************************************************************************/
365 for ( ; Count-- && (*String1 == *String2); String2++)
373 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *String1 -
374 (unsigned char) *String2));
378 /*******************************************************************************
380 * FUNCTION: AcpiUtStrcat (Strcat)
382 * PARAMETERS: DstString - Target of the copy
383 * SrcString - The source string to copy
387 * DESCRIPTION: Append a null terminated string to a null terminated string
389 ******************************************************************************/
394 const char *SrcString)
399 /* Find end of the destination string */
401 for (String = DstString; *String++; )
404 /* Concatenate the string */
406 for (--String; (*String++ = *SrcString++); )
413 /*******************************************************************************
415 * FUNCTION: AcpiUtStrncat (strncat)
417 * PARAMETERS: DstString - Target of the copy
418 * SrcString - The source string to copy
419 * Count - Maximum # of bytes to copy
423 * DESCRIPTION: Append a null terminated string to a null terminated string,
424 * with a maximum count.
426 ******************************************************************************/
431 const char *SrcString,
439 /* Find end of the destination string */
441 for (String = DstString; *String++; )
444 /* Concatenate the string */
446 for (--String; (*String++ = *SrcString++) && --Count; )
449 /* Null terminate if necessary */
461 /*******************************************************************************
463 * FUNCTION: AcpiUtStrstr (strstr)
465 * PARAMETERS: String1 - Target string
466 * String2 - Substring to search for
468 * RETURN: Where substring match starts, Null if no match found
470 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
471 * full implementation of strstr, only sufficient for command
474 ******************************************************************************/
484 if (AcpiUtStrlen (String2) > AcpiUtStrlen (String1))
489 /* Walk entire string, comparing the letters */
491 for (String = String1; *String2; )
493 if (*String2 != *String)
506 /*******************************************************************************
508 * FUNCTION: AcpiUtStrtoul (strtoul)
510 * PARAMETERS: String - Null terminated string
511 * Terminater - Where a pointer to the terminating byte is
513 * Base - Radix of the string
515 * RETURN: Converted value
517 * DESCRIPTION: Convert a string into a 32-bit unsigned value.
518 * Note: use AcpiUtStrtoul64 for 64-bit integers.
520 ******************************************************************************/
528 UINT32 converted = 0;
531 const char *StringStart;
532 UINT32 ReturnValue = 0;
533 ACPI_STATUS Status = AE_OK;
537 * Save the value of the pointer to the buffer's first
538 * character, save the current errno value, and then
539 * skip over any white space in the buffer:
541 StringStart = String;
542 while (ACPI_IS_SPACE (*String) || *String == '\t')
548 * The buffer may contain an optional plus or minus sign.
549 * If it does, then skip over it but remember what is was:
556 else if (*String == '+')
567 * If the input parameter Base is zero, then we need to
568 * determine if it is octal, decimal, or hexadecimal:
574 if (AcpiUtToLower (*(++String)) == 'x')
589 else if (Base < 2 || Base > 36)
592 * The specified Base parameter is not in the domain of
599 * For octal and hexadecimal bases, skip over the leading
600 * 0 or 0x, if they are present.
602 if (Base == 8 && *String == '0')
609 AcpiUtToLower (*(++String)) == 'x')
615 * Main loop: convert the string to an unsigned long:
619 if (ACPI_IS_DIGIT (*String))
621 index = (UINT32) ((UINT8) *String - '0');
625 index = (UINT32) AcpiUtToUpper (*String);
626 if (ACPI_IS_UPPER (index))
628 index = index - 'A' + 10;
642 * Check to see if value is out of range:
645 if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) /
649 ReturnValue = 0; /* reset */
654 ReturnValue += index;
663 * If appropriate, update the caller's pointer to the next
664 * unconverted character in the buffer.
668 if (converted == 0 && ReturnValue == 0 && String != NULL)
670 *Terminator = __DECONST(char *, StringStart);
674 *Terminator = __DECONST(char *, String);
678 if (Status == AE_ERROR)
680 ReturnValue = ACPI_UINT32_MAX;
684 * If a minus sign was present, then "the conversion is negated":
686 if (sign == NEGATIVE)
688 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
691 return (ReturnValue);
695 /*******************************************************************************
697 * FUNCTION: AcpiUtToUpper (TOUPPER)
699 * PARAMETERS: c - Character to convert
701 * RETURN: Converted character as an int
703 * DESCRIPTION: Convert character to uppercase
705 ******************************************************************************/
712 return (ACPI_IS_LOWER(c) ? ((c)-0x20) : (c));
716 /*******************************************************************************
718 * FUNCTION: AcpiUtToLower (TOLOWER)
720 * PARAMETERS: c - Character to convert
722 * RETURN: Converted character as an int
724 * DESCRIPTION: Convert character to lowercase
726 ******************************************************************************/
733 return (ACPI_IS_UPPER(c) ? ((c)+0x20) : (c));
737 /*******************************************************************************
739 * FUNCTION: is* functions
741 * DESCRIPTION: is* functions use the ctype table below
743 ******************************************************************************/
745 const UINT8 _acpi_ctype[257] = {
746 _ACPI_CN, /* 0x00 0 NUL */
747 _ACPI_CN, /* 0x01 1 SOH */
748 _ACPI_CN, /* 0x02 2 STX */
749 _ACPI_CN, /* 0x03 3 ETX */
750 _ACPI_CN, /* 0x04 4 EOT */
751 _ACPI_CN, /* 0x05 5 ENQ */
752 _ACPI_CN, /* 0x06 6 ACK */
753 _ACPI_CN, /* 0x07 7 BEL */
754 _ACPI_CN, /* 0x08 8 BS */
755 _ACPI_CN|_ACPI_SP, /* 0x09 9 TAB */
756 _ACPI_CN|_ACPI_SP, /* 0x0A 10 LF */
757 _ACPI_CN|_ACPI_SP, /* 0x0B 11 VT */
758 _ACPI_CN|_ACPI_SP, /* 0x0C 12 FF */
759 _ACPI_CN|_ACPI_SP, /* 0x0D 13 CR */
760 _ACPI_CN, /* 0x0E 14 SO */
761 _ACPI_CN, /* 0x0F 15 SI */
762 _ACPI_CN, /* 0x10 16 DLE */
763 _ACPI_CN, /* 0x11 17 DC1 */
764 _ACPI_CN, /* 0x12 18 DC2 */
765 _ACPI_CN, /* 0x13 19 DC3 */
766 _ACPI_CN, /* 0x14 20 DC4 */
767 _ACPI_CN, /* 0x15 21 NAK */
768 _ACPI_CN, /* 0x16 22 SYN */
769 _ACPI_CN, /* 0x17 23 ETB */
770 _ACPI_CN, /* 0x18 24 CAN */
771 _ACPI_CN, /* 0x19 25 EM */
772 _ACPI_CN, /* 0x1A 26 SUB */
773 _ACPI_CN, /* 0x1B 27 ESC */
774 _ACPI_CN, /* 0x1C 28 FS */
775 _ACPI_CN, /* 0x1D 29 GS */
776 _ACPI_CN, /* 0x1E 30 RS */
777 _ACPI_CN, /* 0x1F 31 US */
778 _ACPI_XS|_ACPI_SP, /* 0x20 32 ' ' */
779 _ACPI_PU, /* 0x21 33 '!' */
780 _ACPI_PU, /* 0x22 34 '"' */
781 _ACPI_PU, /* 0x23 35 '#' */
782 _ACPI_PU, /* 0x24 36 '$' */
783 _ACPI_PU, /* 0x25 37 '%' */
784 _ACPI_PU, /* 0x26 38 '&' */
785 _ACPI_PU, /* 0x27 39 ''' */
786 _ACPI_PU, /* 0x28 40 '(' */
787 _ACPI_PU, /* 0x29 41 ')' */
788 _ACPI_PU, /* 0x2A 42 '*' */
789 _ACPI_PU, /* 0x2B 43 '+' */
790 _ACPI_PU, /* 0x2C 44 ',' */
791 _ACPI_PU, /* 0x2D 45 '-' */
792 _ACPI_PU, /* 0x2E 46 '.' */
793 _ACPI_PU, /* 0x2F 47 '/' */
794 _ACPI_XD|_ACPI_DI, /* 0x30 48 '0' */
795 _ACPI_XD|_ACPI_DI, /* 0x31 49 '1' */
796 _ACPI_XD|_ACPI_DI, /* 0x32 50 '2' */
797 _ACPI_XD|_ACPI_DI, /* 0x33 51 '3' */
798 _ACPI_XD|_ACPI_DI, /* 0x34 52 '4' */
799 _ACPI_XD|_ACPI_DI, /* 0x35 53 '5' */
800 _ACPI_XD|_ACPI_DI, /* 0x36 54 '6' */
801 _ACPI_XD|_ACPI_DI, /* 0x37 55 '7' */
802 _ACPI_XD|_ACPI_DI, /* 0x38 56 '8' */
803 _ACPI_XD|_ACPI_DI, /* 0x39 57 '9' */
804 _ACPI_PU, /* 0x3A 58 ':' */
805 _ACPI_PU, /* 0x3B 59 ';' */
806 _ACPI_PU, /* 0x3C 60 '<' */
807 _ACPI_PU, /* 0x3D 61 '=' */
808 _ACPI_PU, /* 0x3E 62 '>' */
809 _ACPI_PU, /* 0x3F 63 '?' */
810 _ACPI_PU, /* 0x40 64 '@' */
811 _ACPI_XD|_ACPI_UP, /* 0x41 65 'A' */
812 _ACPI_XD|_ACPI_UP, /* 0x42 66 'B' */
813 _ACPI_XD|_ACPI_UP, /* 0x43 67 'C' */
814 _ACPI_XD|_ACPI_UP, /* 0x44 68 'D' */
815 _ACPI_XD|_ACPI_UP, /* 0x45 69 'E' */
816 _ACPI_XD|_ACPI_UP, /* 0x46 70 'F' */
817 _ACPI_UP, /* 0x47 71 'G' */
818 _ACPI_UP, /* 0x48 72 'H' */
819 _ACPI_UP, /* 0x49 73 'I' */
820 _ACPI_UP, /* 0x4A 74 'J' */
821 _ACPI_UP, /* 0x4B 75 'K' */
822 _ACPI_UP, /* 0x4C 76 'L' */
823 _ACPI_UP, /* 0x4D 77 'M' */
824 _ACPI_UP, /* 0x4E 78 'N' */
825 _ACPI_UP, /* 0x4F 79 'O' */
826 _ACPI_UP, /* 0x50 80 'P' */
827 _ACPI_UP, /* 0x51 81 'Q' */
828 _ACPI_UP, /* 0x52 82 'R' */
829 _ACPI_UP, /* 0x53 83 'S' */
830 _ACPI_UP, /* 0x54 84 'T' */
831 _ACPI_UP, /* 0x55 85 'U' */
832 _ACPI_UP, /* 0x56 86 'V' */
833 _ACPI_UP, /* 0x57 87 'W' */
834 _ACPI_UP, /* 0x58 88 'X' */
835 _ACPI_UP, /* 0x59 89 'Y' */
836 _ACPI_UP, /* 0x5A 90 'Z' */
837 _ACPI_PU, /* 0x5B 91 '[' */
838 _ACPI_PU, /* 0x5C 92 '\' */
839 _ACPI_PU, /* 0x5D 93 ']' */
840 _ACPI_PU, /* 0x5E 94 '^' */
841 _ACPI_PU, /* 0x5F 95 '_' */
842 _ACPI_PU, /* 0x60 96 '`' */
843 _ACPI_XD|_ACPI_LO, /* 0x61 97 'a' */
844 _ACPI_XD|_ACPI_LO, /* 0x62 98 'b' */
845 _ACPI_XD|_ACPI_LO, /* 0x63 99 'c' */
846 _ACPI_XD|_ACPI_LO, /* 0x64 100 'd' */
847 _ACPI_XD|_ACPI_LO, /* 0x65 101 'e' */
848 _ACPI_XD|_ACPI_LO, /* 0x66 102 'f' */
849 _ACPI_LO, /* 0x67 103 'g' */
850 _ACPI_LO, /* 0x68 104 'h' */
851 _ACPI_LO, /* 0x69 105 'i' */
852 _ACPI_LO, /* 0x6A 106 'j' */
853 _ACPI_LO, /* 0x6B 107 'k' */
854 _ACPI_LO, /* 0x6C 108 'l' */
855 _ACPI_LO, /* 0x6D 109 'm' */
856 _ACPI_LO, /* 0x6E 110 'n' */
857 _ACPI_LO, /* 0x6F 111 'o' */
858 _ACPI_LO, /* 0x70 112 'p' */
859 _ACPI_LO, /* 0x71 113 'q' */
860 _ACPI_LO, /* 0x72 114 'r' */
861 _ACPI_LO, /* 0x73 115 's' */
862 _ACPI_LO, /* 0x74 116 't' */
863 _ACPI_LO, /* 0x75 117 'u' */
864 _ACPI_LO, /* 0x76 118 'v' */
865 _ACPI_LO, /* 0x77 119 'w' */
866 _ACPI_LO, /* 0x78 120 'x' */
867 _ACPI_LO, /* 0x79 121 'y' */
868 _ACPI_LO, /* 0x7A 122 'z' */
869 _ACPI_PU, /* 0x7B 123 '{' */
870 _ACPI_PU, /* 0x7C 124 '|' */
871 _ACPI_PU, /* 0x7D 125 '}' */
872 _ACPI_PU, /* 0x7E 126 '~' */
873 _ACPI_CN, /* 0x7F 127 DEL */
875 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
876 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
877 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
878 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
879 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
880 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
881 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
882 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xF0 to 0xFF */
887 #endif /* ACPI_USE_SYSTEM_CLIBRARY */