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.
50 * These implementations of standard C Library routines can optionally be
51 * used if a C library is not available. In general, they are less efficient
52 * than an inline or assembly implementation
55 #define _COMPONENT ACPI_UTILITIES
56 ACPI_MODULE_NAME ("cmclib")
59 #ifndef ACPI_USE_SYSTEM_CLIBRARY
65 /*******************************************************************************
67 * FUNCTION: AcpiUtMemcmp (memcmp)
69 * PARAMETERS: Buffer1 - First Buffer
70 * Buffer2 - Second Buffer
71 * Count - Maximum # of bytes to compare
73 * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
75 * DESCRIPTION: Compare two Buffers, with a maximum length
77 ******************************************************************************/
86 for ( ; Count-- && (*Buffer1 == *Buffer2); Buffer1++, Buffer2++)
90 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *Buffer1 -
91 (unsigned char) *Buffer2));
95 /*******************************************************************************
97 * FUNCTION: AcpiUtMemcpy (memcpy)
99 * PARAMETERS: Dest - Target of the copy
100 * Src - Source buffer to copy
101 * Count - Number of bytes to copy
105 * DESCRIPTION: Copy arbitrary bytes of memory
107 ******************************************************************************/
115 char *New = (char *) Dest;
116 char *Old = __DECONST(char *, Src);
131 /*******************************************************************************
133 * FUNCTION: AcpiUtMemset (memset)
135 * PARAMETERS: Dest - Buffer to set
136 * Value - Value to set each byte of memory
137 * Count - Number of bytes to set
141 * DESCRIPTION: Initialize a buffer to a known value.
143 ******************************************************************************/
151 char *New = (char *) Dest;
165 /*******************************************************************************
167 * FUNCTION: AcpiUtStrlen (strlen)
169 * PARAMETERS: String - Null terminated string
173 * DESCRIPTION: Returns the length of the input string
175 ******************************************************************************/
185 /* Count the string until a null is encountered */
197 /*******************************************************************************
199 * FUNCTION: AcpiUtStrcpy (strcpy)
201 * PARAMETERS: DstString - Target of the copy
202 * SrcString - The source string to copy
206 * DESCRIPTION: Copy a null terminated string
208 ******************************************************************************/
213 const char *SrcString)
215 char *String = DstString;
218 /* Move bytes brute force */
222 *String = *SrcString;
235 /*******************************************************************************
237 * FUNCTION: AcpiUtStrncpy (strncpy)
239 * PARAMETERS: DstString - Target of the copy
240 * SrcString - The source string to copy
241 * Count - Maximum # of bytes to copy
245 * DESCRIPTION: Copy a null terminated string, with a maximum length
247 ******************************************************************************/
252 const char *SrcString,
255 char *String = DstString;
258 /* Copy the string */
260 for (String = DstString;
261 Count && (Count--, (*String++ = *SrcString++)); )
264 /* Pad with nulls if necessary */
272 /* Return original pointer */
278 /*******************************************************************************
280 * FUNCTION: AcpiUtStrcmp (strcmp)
282 * PARAMETERS: String1 - First string
283 * String2 - Second string
285 * RETURN: Index where strings mismatched, or 0 if strings matched
287 * DESCRIPTION: Compare two null terminated strings
289 ******************************************************************************/
298 for ( ; (*String1 == *String2); String2++)
306 return ((unsigned char) *String1 - (unsigned char) *String2);
310 /*******************************************************************************
312 * FUNCTION: AcpiUtStrchr (strchr)
314 * PARAMETERS: String - Search string
315 * ch - character to search for
317 * RETURN: Ptr to char or NULL if not found
319 * DESCRIPTION: Search a string for a character
321 ******************************************************************************/
330 for ( ; (*String); String++)
332 if ((*String) == (char) ch)
334 return (__DECONST(char *, String));
342 /*******************************************************************************
344 * FUNCTION: AcpiUtStrncmp (strncmp)
346 * PARAMETERS: String1 - First string
347 * String2 - Second string
348 * Count - Maximum # of bytes to compare
350 * RETURN: Index where strings mismatched, or 0 if strings matched
352 * DESCRIPTION: Compare two null terminated strings, with a maximum length
354 ******************************************************************************/
364 for ( ; Count-- && (*String1 == *String2); String2++)
372 return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *String1 -
373 (unsigned char) *String2));
377 /*******************************************************************************
379 * FUNCTION: AcpiUtStrcat (Strcat)
381 * PARAMETERS: DstString - Target of the copy
382 * SrcString - The source string to copy
386 * DESCRIPTION: Append a null terminated string to a null terminated string
388 ******************************************************************************/
393 const char *SrcString)
398 /* Find end of the destination string */
400 for (String = DstString; *String++; )
403 /* Concatenate the string */
405 for (--String; (*String++ = *SrcString++); )
412 /*******************************************************************************
414 * FUNCTION: AcpiUtStrncat (strncat)
416 * PARAMETERS: DstString - Target of the copy
417 * SrcString - The source string to copy
418 * Count - Maximum # of bytes to copy
422 * DESCRIPTION: Append a null terminated string to a null terminated string,
423 * with a maximum count.
425 ******************************************************************************/
430 const char *SrcString,
438 /* Find end of the destination string */
440 for (String = DstString; *String++; )
443 /* Concatenate the string */
445 for (--String; (*String++ = *SrcString++) && --Count; )
448 /* Null terminate if necessary */
460 /*******************************************************************************
462 * FUNCTION: AcpiUtStrstr (strstr)
464 * PARAMETERS: String1 - Target string
465 * String2 - Substring to search for
467 * RETURN: Where substring match starts, Null if no match found
469 * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
470 * full implementation of strstr, only sufficient for command
473 ******************************************************************************/
483 if (AcpiUtStrlen (String2) > AcpiUtStrlen (String1))
488 /* Walk entire string, comparing the letters */
490 for (String = String1; *String2; )
492 if (*String2 != *String)
505 /*******************************************************************************
507 * FUNCTION: AcpiUtStrtoul (strtoul)
509 * PARAMETERS: String - Null terminated string
510 * Terminater - Where a pointer to the terminating byte is
512 * Base - Radix of the string
514 * RETURN: Converted value
516 * DESCRIPTION: Convert a string into a 32-bit unsigned value.
517 * Note: use AcpiUtStrtoul64 for 64-bit integers.
519 ******************************************************************************/
527 UINT32 converted = 0;
530 const char *StringStart;
531 UINT32 ReturnValue = 0;
532 ACPI_STATUS Status = AE_OK;
536 * Save the value of the pointer to the buffer's first
537 * character, save the current errno value, and then
538 * skip over any white space in the buffer:
540 StringStart = String;
541 while (ACPI_IS_SPACE (*String) || *String == '\t')
547 * The buffer may contain an optional plus or minus sign.
548 * If it does, then skip over it but remember what is was:
555 else if (*String == '+')
566 * If the input parameter Base is zero, then we need to
567 * determine if it is octal, decimal, or hexadecimal:
573 if (AcpiUtToLower (*(++String)) == 'x')
588 else if (Base < 2 || Base > 36)
591 * The specified Base parameter is not in the domain of
598 * For octal and hexadecimal bases, skip over the leading
599 * 0 or 0x, if they are present.
601 if (Base == 8 && *String == '0')
608 AcpiUtToLower (*(++String)) == 'x')
614 * Main loop: convert the string to an unsigned long:
618 if (ACPI_IS_DIGIT (*String))
620 index = (UINT32) ((UINT8) *String - '0');
624 index = (UINT32) AcpiUtToUpper (*String);
625 if (ACPI_IS_UPPER (index))
627 index = index - 'A' + 10;
641 * Check to see if value is out of range:
644 if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) /
648 ReturnValue = 0; /* reset */
653 ReturnValue += index;
662 * If appropriate, update the caller's pointer to the next
663 * unconverted character in the buffer.
667 if (converted == 0 && ReturnValue == 0 && String != NULL)
669 *Terminator = __DECONST(char *, StringStart);
673 *Terminator = __DECONST(char *, String);
677 if (Status == AE_ERROR)
679 ReturnValue = ACPI_UINT32_MAX;
683 * If a minus sign was present, then "the conversion is negated":
685 if (sign == NEGATIVE)
687 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
690 return (ReturnValue);
694 /*******************************************************************************
696 * FUNCTION: AcpiUtToUpper (TOUPPER)
698 * PARAMETERS: c - Character to convert
700 * RETURN: Converted character as an int
702 * DESCRIPTION: Convert character to uppercase
704 ******************************************************************************/
711 return (ACPI_IS_LOWER(c) ? ((c)-0x20) : (c));
715 /*******************************************************************************
717 * FUNCTION: AcpiUtToLower (TOLOWER)
719 * PARAMETERS: c - Character to convert
721 * RETURN: Converted character as an int
723 * DESCRIPTION: Convert character to lowercase
725 ******************************************************************************/
732 return (ACPI_IS_UPPER(c) ? ((c)+0x20) : (c));
736 /*******************************************************************************
738 * FUNCTION: is* functions
740 * DESCRIPTION: is* functions use the ctype table below
742 ******************************************************************************/
744 const UINT8 _acpi_ctype[257] = {
745 _ACPI_CN, /* 0x00 0 NUL */
746 _ACPI_CN, /* 0x01 1 SOH */
747 _ACPI_CN, /* 0x02 2 STX */
748 _ACPI_CN, /* 0x03 3 ETX */
749 _ACPI_CN, /* 0x04 4 EOT */
750 _ACPI_CN, /* 0x05 5 ENQ */
751 _ACPI_CN, /* 0x06 6 ACK */
752 _ACPI_CN, /* 0x07 7 BEL */
753 _ACPI_CN, /* 0x08 8 BS */
754 _ACPI_CN|_ACPI_SP, /* 0x09 9 TAB */
755 _ACPI_CN|_ACPI_SP, /* 0x0A 10 LF */
756 _ACPI_CN|_ACPI_SP, /* 0x0B 11 VT */
757 _ACPI_CN|_ACPI_SP, /* 0x0C 12 FF */
758 _ACPI_CN|_ACPI_SP, /* 0x0D 13 CR */
759 _ACPI_CN, /* 0x0E 14 SO */
760 _ACPI_CN, /* 0x0F 15 SI */
761 _ACPI_CN, /* 0x10 16 DLE */
762 _ACPI_CN, /* 0x11 17 DC1 */
763 _ACPI_CN, /* 0x12 18 DC2 */
764 _ACPI_CN, /* 0x13 19 DC3 */
765 _ACPI_CN, /* 0x14 20 DC4 */
766 _ACPI_CN, /* 0x15 21 NAK */
767 _ACPI_CN, /* 0x16 22 SYN */
768 _ACPI_CN, /* 0x17 23 ETB */
769 _ACPI_CN, /* 0x18 24 CAN */
770 _ACPI_CN, /* 0x19 25 EM */
771 _ACPI_CN, /* 0x1A 26 SUB */
772 _ACPI_CN, /* 0x1B 27 ESC */
773 _ACPI_CN, /* 0x1C 28 FS */
774 _ACPI_CN, /* 0x1D 29 GS */
775 _ACPI_CN, /* 0x1E 30 RS */
776 _ACPI_CN, /* 0x1F 31 US */
777 _ACPI_XS|_ACPI_SP, /* 0x20 32 ' ' */
778 _ACPI_PU, /* 0x21 33 '!' */
779 _ACPI_PU, /* 0x22 34 '"' */
780 _ACPI_PU, /* 0x23 35 '#' */
781 _ACPI_PU, /* 0x24 36 '$' */
782 _ACPI_PU, /* 0x25 37 '%' */
783 _ACPI_PU, /* 0x26 38 '&' */
784 _ACPI_PU, /* 0x27 39 ''' */
785 _ACPI_PU, /* 0x28 40 '(' */
786 _ACPI_PU, /* 0x29 41 ')' */
787 _ACPI_PU, /* 0x2A 42 '*' */
788 _ACPI_PU, /* 0x2B 43 '+' */
789 _ACPI_PU, /* 0x2C 44 ',' */
790 _ACPI_PU, /* 0x2D 45 '-' */
791 _ACPI_PU, /* 0x2E 46 '.' */
792 _ACPI_PU, /* 0x2F 47 '/' */
793 _ACPI_XD|_ACPI_DI, /* 0x30 48 '0' */
794 _ACPI_XD|_ACPI_DI, /* 0x31 49 '1' */
795 _ACPI_XD|_ACPI_DI, /* 0x32 50 '2' */
796 _ACPI_XD|_ACPI_DI, /* 0x33 51 '3' */
797 _ACPI_XD|_ACPI_DI, /* 0x34 52 '4' */
798 _ACPI_XD|_ACPI_DI, /* 0x35 53 '5' */
799 _ACPI_XD|_ACPI_DI, /* 0x36 54 '6' */
800 _ACPI_XD|_ACPI_DI, /* 0x37 55 '7' */
801 _ACPI_XD|_ACPI_DI, /* 0x38 56 '8' */
802 _ACPI_XD|_ACPI_DI, /* 0x39 57 '9' */
803 _ACPI_PU, /* 0x3A 58 ':' */
804 _ACPI_PU, /* 0x3B 59 ';' */
805 _ACPI_PU, /* 0x3C 60 '<' */
806 _ACPI_PU, /* 0x3D 61 '=' */
807 _ACPI_PU, /* 0x3E 62 '>' */
808 _ACPI_PU, /* 0x3F 63 '?' */
809 _ACPI_PU, /* 0x40 64 '@' */
810 _ACPI_XD|_ACPI_UP, /* 0x41 65 'A' */
811 _ACPI_XD|_ACPI_UP, /* 0x42 66 'B' */
812 _ACPI_XD|_ACPI_UP, /* 0x43 67 'C' */
813 _ACPI_XD|_ACPI_UP, /* 0x44 68 'D' */
814 _ACPI_XD|_ACPI_UP, /* 0x45 69 'E' */
815 _ACPI_XD|_ACPI_UP, /* 0x46 70 'F' */
816 _ACPI_UP, /* 0x47 71 'G' */
817 _ACPI_UP, /* 0x48 72 'H' */
818 _ACPI_UP, /* 0x49 73 'I' */
819 _ACPI_UP, /* 0x4A 74 'J' */
820 _ACPI_UP, /* 0x4B 75 'K' */
821 _ACPI_UP, /* 0x4C 76 'L' */
822 _ACPI_UP, /* 0x4D 77 'M' */
823 _ACPI_UP, /* 0x4E 78 'N' */
824 _ACPI_UP, /* 0x4F 79 'O' */
825 _ACPI_UP, /* 0x50 80 'P' */
826 _ACPI_UP, /* 0x51 81 'Q' */
827 _ACPI_UP, /* 0x52 82 'R' */
828 _ACPI_UP, /* 0x53 83 'S' */
829 _ACPI_UP, /* 0x54 84 'T' */
830 _ACPI_UP, /* 0x55 85 'U' */
831 _ACPI_UP, /* 0x56 86 'V' */
832 _ACPI_UP, /* 0x57 87 'W' */
833 _ACPI_UP, /* 0x58 88 'X' */
834 _ACPI_UP, /* 0x59 89 'Y' */
835 _ACPI_UP, /* 0x5A 90 'Z' */
836 _ACPI_PU, /* 0x5B 91 '[' */
837 _ACPI_PU, /* 0x5C 92 '\' */
838 _ACPI_PU, /* 0x5D 93 ']' */
839 _ACPI_PU, /* 0x5E 94 '^' */
840 _ACPI_PU, /* 0x5F 95 '_' */
841 _ACPI_PU, /* 0x60 96 '`' */
842 _ACPI_XD|_ACPI_LO, /* 0x61 97 'a' */
843 _ACPI_XD|_ACPI_LO, /* 0x62 98 'b' */
844 _ACPI_XD|_ACPI_LO, /* 0x63 99 'c' */
845 _ACPI_XD|_ACPI_LO, /* 0x64 100 'd' */
846 _ACPI_XD|_ACPI_LO, /* 0x65 101 'e' */
847 _ACPI_XD|_ACPI_LO, /* 0x66 102 'f' */
848 _ACPI_LO, /* 0x67 103 'g' */
849 _ACPI_LO, /* 0x68 104 'h' */
850 _ACPI_LO, /* 0x69 105 'i' */
851 _ACPI_LO, /* 0x6A 106 'j' */
852 _ACPI_LO, /* 0x6B 107 'k' */
853 _ACPI_LO, /* 0x6C 108 'l' */
854 _ACPI_LO, /* 0x6D 109 'm' */
855 _ACPI_LO, /* 0x6E 110 'n' */
856 _ACPI_LO, /* 0x6F 111 'o' */
857 _ACPI_LO, /* 0x70 112 'p' */
858 _ACPI_LO, /* 0x71 113 'q' */
859 _ACPI_LO, /* 0x72 114 'r' */
860 _ACPI_LO, /* 0x73 115 's' */
861 _ACPI_LO, /* 0x74 116 't' */
862 _ACPI_LO, /* 0x75 117 'u' */
863 _ACPI_LO, /* 0x76 118 'v' */
864 _ACPI_LO, /* 0x77 119 'w' */
865 _ACPI_LO, /* 0x78 120 'x' */
866 _ACPI_LO, /* 0x79 121 'y' */
867 _ACPI_LO, /* 0x7A 122 'z' */
868 _ACPI_PU, /* 0x7B 123 '{' */
869 _ACPI_PU, /* 0x7C 124 '|' */
870 _ACPI_PU, /* 0x7D 125 '}' */
871 _ACPI_PU, /* 0x7E 126 '~' */
872 _ACPI_CN, /* 0x7F 127 DEL */
874 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
875 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
876 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
877 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
878 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
879 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
880 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
881 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xF0 to 0xFF */
886 #endif /* ACPI_USE_SYSTEM_CLIBRARY */