13446Smrj /******************************************************************************
23446Smrj *
33446Smrj * Module Name: cmclib - Local implementation of C library functions
43446Smrj *
53446Smrj *****************************************************************************/
63446Smrj
73446Smrj /******************************************************************************
83446Smrj *
93446Smrj * 1. Copyright Notice
103446Smrj *
11*9980SDana.Myers@Sun.COM * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
123446Smrj * All rights reserved.
133446Smrj *
143446Smrj * 2. License
153446Smrj *
163446Smrj * 2.1. This is your license from Intel Corp. under its intellectual property
173446Smrj * rights. You may have additional license terms from the party that provided
183446Smrj * you this software, covering your right to use that party's intellectual
193446Smrj * property rights.
203446Smrj *
213446Smrj * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
223446Smrj * copy of the source code appearing in this file ("Covered Code") an
233446Smrj * irrevocable, perpetual, worldwide license under Intel's copyrights in the
243446Smrj * base code distributed originally by Intel ("Original Intel Code") to copy,
253446Smrj * make derivatives, distribute, use and display any portion of the Covered
263446Smrj * Code in any form, with the right to sublicense such rights; and
273446Smrj *
283446Smrj * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
293446Smrj * license (with the right to sublicense), under only those claims of Intel
303446Smrj * patents that are infringed by the Original Intel Code, to make, use, sell,
313446Smrj * offer to sell, and import the Covered Code and derivative works thereof
323446Smrj * solely to the minimum extent necessary to exercise the above copyright
333446Smrj * license, and in no event shall the patent license extend to any additions
343446Smrj * to or modifications of the Original Intel Code. No other license or right
353446Smrj * is granted directly or by implication, estoppel or otherwise;
363446Smrj *
373446Smrj * The above copyright and patent license is granted only if the following
383446Smrj * conditions are met:
393446Smrj *
403446Smrj * 3. Conditions
413446Smrj *
423446Smrj * 3.1. Redistribution of Source with Rights to Further Distribute Source.
433446Smrj * Redistribution of source code of any substantial portion of the Covered
443446Smrj * Code or modification with rights to further distribute source must include
453446Smrj * the above Copyright Notice, the above License, this list of Conditions,
463446Smrj * and the following Disclaimer and Export Compliance provision. In addition,
473446Smrj * Licensee must cause all Covered Code to which Licensee contributes to
483446Smrj * contain a file documenting the changes Licensee made to create that Covered
493446Smrj * Code and the date of any change. Licensee must include in that file the
503446Smrj * documentation of any changes made by any predecessor Licensee. Licensee
513446Smrj * must include a prominent statement that the modification is derived,
523446Smrj * directly or indirectly, from Original Intel Code.
533446Smrj *
543446Smrj * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
553446Smrj * Redistribution of source code of any substantial portion of the Covered
563446Smrj * Code or modification without rights to further distribute source must
573446Smrj * include the following Disclaimer and Export Compliance provision in the
583446Smrj * documentation and/or other materials provided with distribution. In
593446Smrj * addition, Licensee may not authorize further sublicense of source of any
603446Smrj * portion of the Covered Code, and must include terms to the effect that the
613446Smrj * license from Licensee to its licensee is limited to the intellectual
623446Smrj * property embodied in the software Licensee provides to its licensee, and
633446Smrj * not to intellectual property embodied in modifications its licensee may
643446Smrj * make.
653446Smrj *
663446Smrj * 3.3. Redistribution of Executable. Redistribution in executable form of any
673446Smrj * substantial portion of the Covered Code or modification must reproduce the
683446Smrj * above Copyright Notice, and the following Disclaimer and Export Compliance
693446Smrj * provision in the documentation and/or other materials provided with the
703446Smrj * distribution.
713446Smrj *
723446Smrj * 3.4. Intel retains all right, title, and interest in and to the Original
733446Smrj * Intel Code.
743446Smrj *
753446Smrj * 3.5. Neither the name Intel nor any other trademark owned or controlled by
763446Smrj * Intel shall be used in advertising or otherwise to promote the sale, use or
773446Smrj * other dealings in products derived from or relating to the Covered Code
783446Smrj * without prior written authorization from Intel.
793446Smrj *
803446Smrj * 4. Disclaimer and Export Compliance
813446Smrj *
823446Smrj * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
833446Smrj * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
843446Smrj * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
853446Smrj * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
863446Smrj * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
873446Smrj * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
883446Smrj * PARTICULAR PURPOSE.
893446Smrj *
903446Smrj * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
913446Smrj * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
923446Smrj * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
933446Smrj * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
943446Smrj * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
953446Smrj * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
963446Smrj * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
973446Smrj * LIMITED REMEDY.
983446Smrj *
993446Smrj * 4.3. Licensee shall not export, either directly or indirectly, any of this
1003446Smrj * software or system incorporating such software without first obtaining any
1013446Smrj * required license or other approval from the U. S. Department of Commerce or
1023446Smrj * any other agency or department of the United States Government. In the
1033446Smrj * event Licensee exports any such software from the United States or
1043446Smrj * re-exports any such software from a foreign destination, Licensee shall
1053446Smrj * ensure that the distribution and export/re-export of the software is in
1063446Smrj * compliance with all laws, regulations, orders, or other restrictions of the
1073446Smrj * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1083446Smrj * any of its subsidiaries will export/re-export any technical data, process,
1093446Smrj * software, or service, directly or indirectly, to any country for which the
1103446Smrj * United States government or any agency thereof requires an export license,
1113446Smrj * other governmental approval, or letter of assurance, without first obtaining
1123446Smrj * such license, approval or letter.
1133446Smrj *
1143446Smrj *****************************************************************************/
1153446Smrj
1163446Smrj
1173446Smrj #define __CMCLIB_C__
1183446Smrj
1193446Smrj #include "acpi.h"
120*9980SDana.Myers@Sun.COM #include "accommon.h"
1213446Smrj
1223446Smrj /*
1233446Smrj * These implementations of standard C Library routines can optionally be
1243446Smrj * used if a C library is not available. In general, they are less efficient
1253446Smrj * than an inline or assembly implementation
1263446Smrj */
1273446Smrj
1283446Smrj #define _COMPONENT ACPI_UTILITIES
1293446Smrj ACPI_MODULE_NAME ("cmclib")
1303446Smrj
1313446Smrj
1323446Smrj #ifndef ACPI_USE_SYSTEM_CLIBRARY
1333446Smrj
1343446Smrj #define NEGATIVE 1
1353446Smrj #define POSITIVE 0
1363446Smrj
1373446Smrj
1383446Smrj /*******************************************************************************
1393446Smrj *
1403446Smrj * FUNCTION: AcpiUtMemcmp (memcmp)
1413446Smrj *
1423446Smrj * PARAMETERS: Buffer1 - First Buffer
1433446Smrj * Buffer2 - Second Buffer
1443446Smrj * Count - Maximum # of bytes to compare
1453446Smrj *
1463446Smrj * RETURN: Index where Buffers mismatched, or 0 if Buffers matched
1473446Smrj *
1483446Smrj * DESCRIPTION: Compare two Buffers, with a maximum length
1493446Smrj *
1503446Smrj ******************************************************************************/
1513446Smrj
1523446Smrj int
AcpiUtMemcmp(const char * Buffer1,const char * Buffer2,ACPI_SIZE Count)1533446Smrj AcpiUtMemcmp (
1543446Smrj const char *Buffer1,
1553446Smrj const char *Buffer2,
1563446Smrj ACPI_SIZE Count)
1573446Smrj {
1583446Smrj
1593446Smrj for ( ; Count-- && (*Buffer1 == *Buffer2); Buffer1++, Buffer2++)
1603446Smrj {
1613446Smrj }
1623446Smrj
1633446Smrj return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *Buffer1 -
1643446Smrj (unsigned char) *Buffer2));
1653446Smrj }
1663446Smrj
1673446Smrj
1683446Smrj /*******************************************************************************
1693446Smrj *
1703446Smrj * FUNCTION: AcpiUtMemcpy (memcpy)
1713446Smrj *
1723446Smrj * PARAMETERS: Dest - Target of the copy
1733446Smrj * Src - Source buffer to copy
1743446Smrj * Count - Number of bytes to copy
1753446Smrj *
1763446Smrj * RETURN: Dest
1773446Smrj *
1783446Smrj * DESCRIPTION: Copy arbitrary bytes of memory
1793446Smrj *
1803446Smrj ******************************************************************************/
1813446Smrj
1823446Smrj void *
AcpiUtMemcpy(void * Dest,const void * Src,ACPI_SIZE Count)1833446Smrj AcpiUtMemcpy (
1843446Smrj void *Dest,
1853446Smrj const void *Src,
1863446Smrj ACPI_SIZE Count)
1873446Smrj {
1883446Smrj char *New = (char *) Dest;
1893446Smrj char *Old = (char *) Src;
1903446Smrj
1913446Smrj
1923446Smrj while (Count)
1933446Smrj {
1943446Smrj *New = *Old;
1953446Smrj New++;
1963446Smrj Old++;
1973446Smrj Count--;
1983446Smrj }
1993446Smrj
2003446Smrj return (Dest);
2013446Smrj }
2023446Smrj
2033446Smrj
2043446Smrj /*******************************************************************************
2053446Smrj *
2063446Smrj * FUNCTION: AcpiUtMemset (memset)
2073446Smrj *
2083446Smrj * PARAMETERS: Dest - Buffer to set
2093446Smrj * Value - Value to set each byte of memory
2103446Smrj * Count - Number of bytes to set
2113446Smrj *
2123446Smrj * RETURN: Dest
2133446Smrj *
2143446Smrj * DESCRIPTION: Initialize a buffer to a known value.
2153446Smrj *
2163446Smrj ******************************************************************************/
2173446Smrj
2183446Smrj void *
AcpiUtMemset(void * Dest,UINT8 Value,ACPI_SIZE Count)2193446Smrj AcpiUtMemset (
2203446Smrj void *Dest,
2217851SDana.Myers@Sun.COM UINT8 Value,
2223446Smrj ACPI_SIZE Count)
2233446Smrj {
2243446Smrj char *New = (char *) Dest;
2253446Smrj
2263446Smrj
2273446Smrj while (Count)
2283446Smrj {
2293446Smrj *New = (char) Value;
2303446Smrj New++;
2313446Smrj Count--;
2323446Smrj }
2333446Smrj
2343446Smrj return (Dest);
2353446Smrj }
2363446Smrj
2373446Smrj
2383446Smrj /*******************************************************************************
2393446Smrj *
2403446Smrj * FUNCTION: AcpiUtStrlen (strlen)
2413446Smrj *
2423446Smrj * PARAMETERS: String - Null terminated string
2433446Smrj *
2443446Smrj * RETURN: Length
2453446Smrj *
2463446Smrj * DESCRIPTION: Returns the length of the input string
2473446Smrj *
2483446Smrj ******************************************************************************/
2493446Smrj
2503446Smrj
2513446Smrj ACPI_SIZE
AcpiUtStrlen(const char * String)2523446Smrj AcpiUtStrlen (
2533446Smrj const char *String)
2543446Smrj {
2553446Smrj UINT32 Length = 0;
2563446Smrj
2573446Smrj
2583446Smrj /* Count the string until a null is encountered */
2593446Smrj
2603446Smrj while (*String)
2613446Smrj {
2623446Smrj Length++;
2633446Smrj String++;
2643446Smrj }
2653446Smrj
2663446Smrj return (Length);
2673446Smrj }
2683446Smrj
2693446Smrj
2703446Smrj /*******************************************************************************
2713446Smrj *
2723446Smrj * FUNCTION: AcpiUtStrcpy (strcpy)
2733446Smrj *
2743446Smrj * PARAMETERS: DstString - Target of the copy
2753446Smrj * SrcString - The source string to copy
2763446Smrj *
2773446Smrj * RETURN: DstString
2783446Smrj *
2793446Smrj * DESCRIPTION: Copy a null terminated string
2803446Smrj *
2813446Smrj ******************************************************************************/
2823446Smrj
2833446Smrj char *
AcpiUtStrcpy(char * DstString,const char * SrcString)2843446Smrj AcpiUtStrcpy (
2853446Smrj char *DstString,
2863446Smrj const char *SrcString)
2873446Smrj {
2883446Smrj char *String = DstString;
2893446Smrj
2903446Smrj
2913446Smrj /* Move bytes brute force */
2923446Smrj
2933446Smrj while (*SrcString)
2943446Smrj {
2953446Smrj *String = *SrcString;
2963446Smrj
2973446Smrj String++;
2983446Smrj SrcString++;
2993446Smrj }
3003446Smrj
3013446Smrj /* Null terminate */
3023446Smrj
3033446Smrj *String = 0;
3043446Smrj return (DstString);
3053446Smrj }
3063446Smrj
3073446Smrj
3083446Smrj /*******************************************************************************
3093446Smrj *
3103446Smrj * FUNCTION: AcpiUtStrncpy (strncpy)
3113446Smrj *
3123446Smrj * PARAMETERS: DstString - Target of the copy
3133446Smrj * SrcString - The source string to copy
3143446Smrj * Count - Maximum # of bytes to copy
3153446Smrj *
3163446Smrj * RETURN: DstString
3173446Smrj *
3183446Smrj * DESCRIPTION: Copy a null terminated string, with a maximum length
3193446Smrj *
3203446Smrj ******************************************************************************/
3213446Smrj
3223446Smrj char *
AcpiUtStrncpy(char * DstString,const char * SrcString,ACPI_SIZE Count)3233446Smrj AcpiUtStrncpy (
3243446Smrj char *DstString,
3253446Smrj const char *SrcString,
3263446Smrj ACPI_SIZE Count)
3273446Smrj {
3283446Smrj char *String = DstString;
3293446Smrj
3303446Smrj
3313446Smrj /* Copy the string */
3323446Smrj
3333446Smrj for (String = DstString;
3343446Smrj Count && (Count--, (*String++ = *SrcString++)); )
3353446Smrj {;}
3363446Smrj
3373446Smrj /* Pad with nulls if necessary */
3383446Smrj
3393446Smrj while (Count--)
3403446Smrj {
3413446Smrj *String = 0;
3423446Smrj String++;
3433446Smrj }
3443446Smrj
3453446Smrj /* Return original pointer */
3463446Smrj
3473446Smrj return (DstString);
3483446Smrj }
3493446Smrj
3503446Smrj
3513446Smrj /*******************************************************************************
3523446Smrj *
3533446Smrj * FUNCTION: AcpiUtStrcmp (strcmp)
3543446Smrj *
3553446Smrj * PARAMETERS: String1 - First string
3563446Smrj * String2 - Second string
3573446Smrj *
3583446Smrj * RETURN: Index where strings mismatched, or 0 if strings matched
3593446Smrj *
3603446Smrj * DESCRIPTION: Compare two null terminated strings
3613446Smrj *
3623446Smrj ******************************************************************************/
3633446Smrj
3643446Smrj int
AcpiUtStrcmp(const char * String1,const char * String2)3653446Smrj AcpiUtStrcmp (
3663446Smrj const char *String1,
3673446Smrj const char *String2)
3683446Smrj {
3693446Smrj
3703446Smrj
3713446Smrj for ( ; (*String1 == *String2); String2++)
3723446Smrj {
3733446Smrj if (!*String1++)
3743446Smrj {
3753446Smrj return (0);
3763446Smrj }
3773446Smrj }
3783446Smrj
3793446Smrj return ((unsigned char) *String1 - (unsigned char) *String2);
3803446Smrj }
3813446Smrj
3823446Smrj
3833446Smrj #ifdef ACPI_FUTURE_IMPLEMENTATION
3843446Smrj /* Not used at this time */
3853446Smrj /*******************************************************************************
3863446Smrj *
3873446Smrj * FUNCTION: AcpiUtStrchr (strchr)
3883446Smrj *
3893446Smrj * PARAMETERS: String - Search string
3903446Smrj * ch - character to search for
3913446Smrj *
3923446Smrj * RETURN: Ptr to char or NULL if not found
3933446Smrj *
3943446Smrj * DESCRIPTION: Search a string for a character
3953446Smrj *
3963446Smrj ******************************************************************************/
3973446Smrj
3983446Smrj char *
AcpiUtStrchr(const char * String,int ch)3993446Smrj AcpiUtStrchr (
4003446Smrj const char *String,
4013446Smrj int ch)
4023446Smrj {
4033446Smrj
4043446Smrj
4053446Smrj for ( ; (*String); String++)
4063446Smrj {
4073446Smrj if ((*String) == (char) ch)
4083446Smrj {
4093446Smrj return ((char *) String);
4103446Smrj }
4113446Smrj }
4123446Smrj
4133446Smrj return (NULL);
4143446Smrj }
4153446Smrj #endif
4163446Smrj
4173446Smrj /*******************************************************************************
4183446Smrj *
4193446Smrj * FUNCTION: AcpiUtStrncmp (strncmp)
4203446Smrj *
4213446Smrj * PARAMETERS: String1 - First string
4223446Smrj * String2 - Second string
4233446Smrj * Count - Maximum # of bytes to compare
4243446Smrj *
4253446Smrj * RETURN: Index where strings mismatched, or 0 if strings matched
4263446Smrj *
4273446Smrj * DESCRIPTION: Compare two null terminated strings, with a maximum length
4283446Smrj *
4293446Smrj ******************************************************************************/
4303446Smrj
4313446Smrj int
AcpiUtStrncmp(const char * String1,const char * String2,ACPI_SIZE Count)4323446Smrj AcpiUtStrncmp (
4333446Smrj const char *String1,
4343446Smrj const char *String2,
4353446Smrj ACPI_SIZE Count)
4363446Smrj {
4373446Smrj
4383446Smrj
4393446Smrj for ( ; Count-- && (*String1 == *String2); String2++)
4403446Smrj {
4413446Smrj if (!*String1++)
4423446Smrj {
4433446Smrj return (0);
4443446Smrj }
4453446Smrj }
4463446Smrj
4473446Smrj return ((Count == ACPI_SIZE_MAX) ? 0 : ((unsigned char) *String1 -
4483446Smrj (unsigned char) *String2));
4493446Smrj }
4503446Smrj
4513446Smrj
4523446Smrj /*******************************************************************************
4533446Smrj *
4543446Smrj * FUNCTION: AcpiUtStrcat (Strcat)
4553446Smrj *
4563446Smrj * PARAMETERS: DstString - Target of the copy
4573446Smrj * SrcString - The source string to copy
4583446Smrj *
4593446Smrj * RETURN: DstString
4603446Smrj *
4613446Smrj * DESCRIPTION: Append a null terminated string to a null terminated string
4623446Smrj *
4633446Smrj ******************************************************************************/
4643446Smrj
4653446Smrj char *
AcpiUtStrcat(char * DstString,const char * SrcString)4663446Smrj AcpiUtStrcat (
4673446Smrj char *DstString,
4683446Smrj const char *SrcString)
4693446Smrj {
4703446Smrj char *String;
4713446Smrj
4723446Smrj
4733446Smrj /* Find end of the destination string */
4743446Smrj
4753446Smrj for (String = DstString; *String++; )
4763446Smrj { ; }
4773446Smrj
4783446Smrj /* Concatenate the string */
4793446Smrj
4803446Smrj for (--String; (*String++ = *SrcString++); )
4813446Smrj { ; }
4823446Smrj
4833446Smrj return (DstString);
4843446Smrj }
4853446Smrj
4863446Smrj
4873446Smrj /*******************************************************************************
4883446Smrj *
4893446Smrj * FUNCTION: AcpiUtStrncat (strncat)
4903446Smrj *
4913446Smrj * PARAMETERS: DstString - Target of the copy
4923446Smrj * SrcString - The source string to copy
4933446Smrj * Count - Maximum # of bytes to copy
4943446Smrj *
4953446Smrj * RETURN: DstString
4963446Smrj *
4973446Smrj * DESCRIPTION: Append a null terminated string to a null terminated string,
4983446Smrj * with a maximum count.
4993446Smrj *
5003446Smrj ******************************************************************************/
5013446Smrj
5023446Smrj char *
AcpiUtStrncat(char * DstString,const char * SrcString,ACPI_SIZE Count)5033446Smrj AcpiUtStrncat (
5043446Smrj char *DstString,
5053446Smrj const char *SrcString,
5063446Smrj ACPI_SIZE Count)
5073446Smrj {
5083446Smrj char *String;
5093446Smrj
5103446Smrj
5113446Smrj if (Count)
5123446Smrj {
5133446Smrj /* Find end of the destination string */
5143446Smrj
5153446Smrj for (String = DstString; *String++; )
5163446Smrj { ; }
5173446Smrj
5183446Smrj /* Concatenate the string */
5193446Smrj
5203446Smrj for (--String; (*String++ = *SrcString++) && --Count; )
5213446Smrj { ; }
5223446Smrj
5233446Smrj /* Null terminate if necessary */
5243446Smrj
5253446Smrj if (!Count)
5263446Smrj {
5273446Smrj *String = 0;
5283446Smrj }
5293446Smrj }
5303446Smrj
5313446Smrj return (DstString);
5323446Smrj }
5333446Smrj
5343446Smrj
5353446Smrj /*******************************************************************************
5363446Smrj *
5373446Smrj * FUNCTION: AcpiUtStrstr (strstr)
5383446Smrj *
5393446Smrj * PARAMETERS: String1 - Target string
5403446Smrj * String2 - Substring to search for
5413446Smrj *
5423446Smrj * RETURN: Where substring match starts, Null if no match found
5433446Smrj *
5443446Smrj * DESCRIPTION: Checks if String2 occurs in String1. This is not really a
5453446Smrj * full implementation of strstr, only sufficient for command
5463446Smrj * matching
5473446Smrj *
5483446Smrj ******************************************************************************/
5493446Smrj
5503446Smrj char *
AcpiUtStrstr(char * String1,char * String2)5513446Smrj AcpiUtStrstr (
5523446Smrj char *String1,
5533446Smrj char *String2)
5543446Smrj {
5553446Smrj char *String;
5563446Smrj
5573446Smrj
5583446Smrj if (AcpiUtStrlen (String2) > AcpiUtStrlen (String1))
5593446Smrj {
5603446Smrj return (NULL);
5613446Smrj }
5623446Smrj
5633446Smrj /* Walk entire string, comparing the letters */
5643446Smrj
5653446Smrj for (String = String1; *String2; )
5663446Smrj {
5673446Smrj if (*String2 != *String)
5683446Smrj {
5693446Smrj return (NULL);
5703446Smrj }
5713446Smrj
5723446Smrj String2++;
5733446Smrj String++;
5743446Smrj }
5753446Smrj
5763446Smrj return (String1);
5773446Smrj }
5783446Smrj
5793446Smrj
5803446Smrj /*******************************************************************************
5813446Smrj *
5823446Smrj * FUNCTION: AcpiUtStrtoul (strtoul)
5833446Smrj *
5843446Smrj * PARAMETERS: String - Null terminated string
5853446Smrj * Terminater - Where a pointer to the terminating byte is
5863446Smrj * returned
5873446Smrj * Base - Radix of the string
5883446Smrj *
5893446Smrj * RETURN: Converted value
5903446Smrj *
5913446Smrj * DESCRIPTION: Convert a string into a 32-bit unsigned value.
5923446Smrj * Note: use AcpiUtStrtoul64 for 64-bit integers.
5933446Smrj *
5943446Smrj ******************************************************************************/
5953446Smrj
5963446Smrj UINT32
AcpiUtStrtoul(const char * String,char ** Terminator,UINT32 Base)5973446Smrj AcpiUtStrtoul (
5983446Smrj const char *String,
5993446Smrj char **Terminator,
6003446Smrj UINT32 Base)
6013446Smrj {
6023446Smrj UINT32 converted = 0;
6033446Smrj UINT32 index;
6043446Smrj UINT32 sign;
6053446Smrj const char *StringStart;
6063446Smrj UINT32 ReturnValue = 0;
6073446Smrj ACPI_STATUS Status = AE_OK;
6083446Smrj
6093446Smrj
6103446Smrj /*
6113446Smrj * Save the value of the pointer to the buffer's first
6123446Smrj * character, save the current errno value, and then
6133446Smrj * skip over any white space in the buffer:
6143446Smrj */
6153446Smrj StringStart = String;
6163446Smrj while (ACPI_IS_SPACE (*String) || *String == '\t')
6173446Smrj {
6183446Smrj ++String;
6193446Smrj }
6203446Smrj
6213446Smrj /*
6223446Smrj * The buffer may contain an optional plus or minus sign.
6233446Smrj * If it does, then skip over it but remember what is was:
6243446Smrj */
6253446Smrj if (*String == '-')
6263446Smrj {
6273446Smrj sign = NEGATIVE;
6283446Smrj ++String;
6293446Smrj }
6303446Smrj else if (*String == '+')
6313446Smrj {
6323446Smrj ++String;
6333446Smrj sign = POSITIVE;
6343446Smrj }
6353446Smrj else
6363446Smrj {
6373446Smrj sign = POSITIVE;
6383446Smrj }
6393446Smrj
6403446Smrj /*
6413446Smrj * If the input parameter Base is zero, then we need to
6423446Smrj * determine if it is octal, decimal, or hexadecimal:
6433446Smrj */
6443446Smrj if (Base == 0)
6453446Smrj {
6463446Smrj if (*String == '0')
6473446Smrj {
6483446Smrj if (AcpiUtToLower (*(++String)) == 'x')
6493446Smrj {
6503446Smrj Base = 16;
6513446Smrj ++String;
6523446Smrj }
6533446Smrj else
6543446Smrj {
6553446Smrj Base = 8;
6563446Smrj }
6573446Smrj }
6583446Smrj else
6593446Smrj {
6603446Smrj Base = 10;
6613446Smrj }
6623446Smrj }
6633446Smrj else if (Base < 2 || Base > 36)
6643446Smrj {
6653446Smrj /*
6663446Smrj * The specified Base parameter is not in the domain of
6673446Smrj * this function:
6683446Smrj */
6693446Smrj goto done;
6703446Smrj }
6713446Smrj
6723446Smrj /*
6733446Smrj * For octal and hexadecimal bases, skip over the leading
6743446Smrj * 0 or 0x, if they are present.
6753446Smrj */
6763446Smrj if (Base == 8 && *String == '0')
6773446Smrj {
6783446Smrj String++;
6793446Smrj }
6803446Smrj
6813446Smrj if (Base == 16 &&
6823446Smrj *String == '0' &&
6833446Smrj AcpiUtToLower (*(++String)) == 'x')
6843446Smrj {
6853446Smrj String++;
6863446Smrj }
6873446Smrj
6883446Smrj /*
6893446Smrj * Main loop: convert the string to an unsigned long:
6903446Smrj */
6913446Smrj while (*String)
6923446Smrj {
6933446Smrj if (ACPI_IS_DIGIT (*String))
6943446Smrj {
6953446Smrj index = (UINT32) ((UINT8) *String - '0');
6963446Smrj }
6973446Smrj else
6983446Smrj {
6993446Smrj index = (UINT32) AcpiUtToUpper (*String);
7003446Smrj if (ACPI_IS_UPPER (index))
7013446Smrj {
7023446Smrj index = index - 'A' + 10;
7033446Smrj }
7043446Smrj else
7053446Smrj {
7063446Smrj goto done;
7073446Smrj }
7083446Smrj }
7093446Smrj
7103446Smrj if (index >= Base)
7113446Smrj {
7123446Smrj goto done;
7133446Smrj }
7143446Smrj
7153446Smrj /*
7163446Smrj * Check to see if value is out of range:
7173446Smrj */
7183446Smrj
7193446Smrj if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) /
7203446Smrj (UINT32) Base))
7213446Smrj {
7223446Smrj Status = AE_ERROR;
7233446Smrj ReturnValue = 0; /* reset */
7243446Smrj }
7253446Smrj else
7263446Smrj {
7273446Smrj ReturnValue *= Base;
7283446Smrj ReturnValue += index;
7293446Smrj converted = 1;
7303446Smrj }
7313446Smrj
7323446Smrj ++String;
7333446Smrj }
7343446Smrj
7353446Smrj done:
7363446Smrj /*
7373446Smrj * If appropriate, update the caller's pointer to the next
7383446Smrj * unconverted character in the buffer.
7393446Smrj */
7403446Smrj if (Terminator)
7413446Smrj {
7423446Smrj if (converted == 0 && ReturnValue == 0 && String != NULL)
7433446Smrj {
7443446Smrj *Terminator = (char *) StringStart;
7453446Smrj }
7463446Smrj else
7473446Smrj {
7483446Smrj *Terminator = (char *) String;
7493446Smrj }
7503446Smrj }
7513446Smrj
7523446Smrj if (Status == AE_ERROR)
7533446Smrj {
7543446Smrj ReturnValue = ACPI_UINT32_MAX;
7553446Smrj }
7563446Smrj
7573446Smrj /*
7583446Smrj * If a minus sign was present, then "the conversion is negated":
7593446Smrj */
7603446Smrj if (sign == NEGATIVE)
7613446Smrj {
7623446Smrj ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
7633446Smrj }
7643446Smrj
7653446Smrj return (ReturnValue);
7663446Smrj }
7673446Smrj
7683446Smrj
7693446Smrj /*******************************************************************************
7703446Smrj *
7713446Smrj * FUNCTION: AcpiUtToUpper (TOUPPER)
7723446Smrj *
7733446Smrj * PARAMETERS: c - Character to convert
7743446Smrj *
7753446Smrj * RETURN: Converted character as an int
7763446Smrj *
7773446Smrj * DESCRIPTION: Convert character to uppercase
7783446Smrj *
7793446Smrj ******************************************************************************/
7803446Smrj
7813446Smrj int
AcpiUtToUpper(int c)7823446Smrj AcpiUtToUpper (
7833446Smrj int c)
7843446Smrj {
7853446Smrj
7863446Smrj return (ACPI_IS_LOWER(c) ? ((c)-0x20) : (c));
7873446Smrj }
7883446Smrj
7893446Smrj
7903446Smrj /*******************************************************************************
7913446Smrj *
7923446Smrj * FUNCTION: AcpiUtToLower (TOLOWER)
7933446Smrj *
7943446Smrj * PARAMETERS: c - Character to convert
7953446Smrj *
7963446Smrj * RETURN: Converted character as an int
7973446Smrj *
7983446Smrj * DESCRIPTION: Convert character to lowercase
7993446Smrj *
8003446Smrj ******************************************************************************/
8013446Smrj
8023446Smrj int
AcpiUtToLower(int c)8033446Smrj AcpiUtToLower (
8043446Smrj int c)
8053446Smrj {
8063446Smrj
8073446Smrj return (ACPI_IS_UPPER(c) ? ((c)+0x20) : (c));
8083446Smrj }
8093446Smrj
8103446Smrj
8113446Smrj /*******************************************************************************
8123446Smrj *
8133446Smrj * FUNCTION: is* functions
8143446Smrj *
8153446Smrj * DESCRIPTION: is* functions use the ctype table below
8163446Smrj *
8173446Smrj ******************************************************************************/
8183446Smrj
8193446Smrj const UINT8 _acpi_ctype[257] = {
8203446Smrj _ACPI_CN, /* 0x0 0. */
8213446Smrj _ACPI_CN, /* 0x1 1. */
8223446Smrj _ACPI_CN, /* 0x2 2. */
8233446Smrj _ACPI_CN, /* 0x3 3. */
8243446Smrj _ACPI_CN, /* 0x4 4. */
8253446Smrj _ACPI_CN, /* 0x5 5. */
8263446Smrj _ACPI_CN, /* 0x6 6. */
8273446Smrj _ACPI_CN, /* 0x7 7. */
8283446Smrj _ACPI_CN, /* 0x8 8. */
8293446Smrj _ACPI_CN|_ACPI_SP, /* 0x9 9. */
8303446Smrj _ACPI_CN|_ACPI_SP, /* 0xA 10. */
8313446Smrj _ACPI_CN|_ACPI_SP, /* 0xB 11. */
8323446Smrj _ACPI_CN|_ACPI_SP, /* 0xC 12. */
8333446Smrj _ACPI_CN|_ACPI_SP, /* 0xD 13. */
8343446Smrj _ACPI_CN, /* 0xE 14. */
8353446Smrj _ACPI_CN, /* 0xF 15. */
8363446Smrj _ACPI_CN, /* 0x10 16. */
8373446Smrj _ACPI_CN, /* 0x11 17. */
8383446Smrj _ACPI_CN, /* 0x12 18. */
8393446Smrj _ACPI_CN, /* 0x13 19. */
8403446Smrj _ACPI_CN, /* 0x14 20. */
8413446Smrj _ACPI_CN, /* 0x15 21. */
8423446Smrj _ACPI_CN, /* 0x16 22. */
8433446Smrj _ACPI_CN, /* 0x17 23. */
8443446Smrj _ACPI_CN, /* 0x18 24. */
8453446Smrj _ACPI_CN, /* 0x19 25. */
8463446Smrj _ACPI_CN, /* 0x1A 26. */
8473446Smrj _ACPI_CN, /* 0x1B 27. */
8483446Smrj _ACPI_CN, /* 0x1C 28. */
8493446Smrj _ACPI_CN, /* 0x1D 29. */
8503446Smrj _ACPI_CN, /* 0x1E 30. */
8513446Smrj _ACPI_CN, /* 0x1F 31. */
8523446Smrj _ACPI_XS|_ACPI_SP, /* 0x20 32. ' ' */
8533446Smrj _ACPI_PU, /* 0x21 33. '!' */
8543446Smrj _ACPI_PU, /* 0x22 34. '"' */
8553446Smrj _ACPI_PU, /* 0x23 35. '#' */
8563446Smrj _ACPI_PU, /* 0x24 36. '$' */
8573446Smrj _ACPI_PU, /* 0x25 37. '%' */
8583446Smrj _ACPI_PU, /* 0x26 38. '&' */
8593446Smrj _ACPI_PU, /* 0x27 39. ''' */
8603446Smrj _ACPI_PU, /* 0x28 40. '(' */
8613446Smrj _ACPI_PU, /* 0x29 41. ')' */
8623446Smrj _ACPI_PU, /* 0x2A 42. '*' */
8633446Smrj _ACPI_PU, /* 0x2B 43. '+' */
8643446Smrj _ACPI_PU, /* 0x2C 44. ',' */
8653446Smrj _ACPI_PU, /* 0x2D 45. '-' */
8663446Smrj _ACPI_PU, /* 0x2E 46. '.' */
8673446Smrj _ACPI_PU, /* 0x2F 47. '/' */
8683446Smrj _ACPI_XD|_ACPI_DI, /* 0x30 48. '0' */
8693446Smrj _ACPI_XD|_ACPI_DI, /* 0x31 49. '1' */
8703446Smrj _ACPI_XD|_ACPI_DI, /* 0x32 50. '2' */
8713446Smrj _ACPI_XD|_ACPI_DI, /* 0x33 51. '3' */
8723446Smrj _ACPI_XD|_ACPI_DI, /* 0x34 52. '4' */
8733446Smrj _ACPI_XD|_ACPI_DI, /* 0x35 53. '5' */
8743446Smrj _ACPI_XD|_ACPI_DI, /* 0x36 54. '6' */
8753446Smrj _ACPI_XD|_ACPI_DI, /* 0x37 55. '7' */
8763446Smrj _ACPI_XD|_ACPI_DI, /* 0x38 56. '8' */
8773446Smrj _ACPI_XD|_ACPI_DI, /* 0x39 57. '9' */
8783446Smrj _ACPI_PU, /* 0x3A 58. ':' */
8793446Smrj _ACPI_PU, /* 0x3B 59. ';' */
8803446Smrj _ACPI_PU, /* 0x3C 60. '<' */
8813446Smrj _ACPI_PU, /* 0x3D 61. '=' */
8823446Smrj _ACPI_PU, /* 0x3E 62. '>' */
8833446Smrj _ACPI_PU, /* 0x3F 63. '?' */
8843446Smrj _ACPI_PU, /* 0x40 64. '@' */
8853446Smrj _ACPI_XD|_ACPI_UP, /* 0x41 65. 'A' */
8863446Smrj _ACPI_XD|_ACPI_UP, /* 0x42 66. 'B' */
8873446Smrj _ACPI_XD|_ACPI_UP, /* 0x43 67. 'C' */
8883446Smrj _ACPI_XD|_ACPI_UP, /* 0x44 68. 'D' */
8893446Smrj _ACPI_XD|_ACPI_UP, /* 0x45 69. 'E' */
8903446Smrj _ACPI_XD|_ACPI_UP, /* 0x46 70. 'F' */
8913446Smrj _ACPI_UP, /* 0x47 71. 'G' */
8923446Smrj _ACPI_UP, /* 0x48 72. 'H' */
8933446Smrj _ACPI_UP, /* 0x49 73. 'I' */
8943446Smrj _ACPI_UP, /* 0x4A 74. 'J' */
8953446Smrj _ACPI_UP, /* 0x4B 75. 'K' */
8963446Smrj _ACPI_UP, /* 0x4C 76. 'L' */
8973446Smrj _ACPI_UP, /* 0x4D 77. 'M' */
8983446Smrj _ACPI_UP, /* 0x4E 78. 'N' */
8993446Smrj _ACPI_UP, /* 0x4F 79. 'O' */
9003446Smrj _ACPI_UP, /* 0x50 80. 'P' */
9013446Smrj _ACPI_UP, /* 0x51 81. 'Q' */
9023446Smrj _ACPI_UP, /* 0x52 82. 'R' */
9033446Smrj _ACPI_UP, /* 0x53 83. 'S' */
9043446Smrj _ACPI_UP, /* 0x54 84. 'T' */
9053446Smrj _ACPI_UP, /* 0x55 85. 'U' */
9063446Smrj _ACPI_UP, /* 0x56 86. 'V' */
9073446Smrj _ACPI_UP, /* 0x57 87. 'W' */
9083446Smrj _ACPI_UP, /* 0x58 88. 'X' */
9093446Smrj _ACPI_UP, /* 0x59 89. 'Y' */
9103446Smrj _ACPI_UP, /* 0x5A 90. 'Z' */
9113446Smrj _ACPI_PU, /* 0x5B 91. '[' */
9123446Smrj _ACPI_PU, /* 0x5C 92. '\' */
9133446Smrj _ACPI_PU, /* 0x5D 93. ']' */
9143446Smrj _ACPI_PU, /* 0x5E 94. '^' */
9153446Smrj _ACPI_PU, /* 0x5F 95. '_' */
9163446Smrj _ACPI_PU, /* 0x60 96. '`' */
9173446Smrj _ACPI_XD|_ACPI_LO, /* 0x61 97. 'a' */
9183446Smrj _ACPI_XD|_ACPI_LO, /* 0x62 98. 'b' */
9193446Smrj _ACPI_XD|_ACPI_LO, /* 0x63 99. 'c' */
9203446Smrj _ACPI_XD|_ACPI_LO, /* 0x64 100. 'd' */
9213446Smrj _ACPI_XD|_ACPI_LO, /* 0x65 101. 'e' */
9223446Smrj _ACPI_XD|_ACPI_LO, /* 0x66 102. 'f' */
9233446Smrj _ACPI_LO, /* 0x67 103. 'g' */
9243446Smrj _ACPI_LO, /* 0x68 104. 'h' */
9253446Smrj _ACPI_LO, /* 0x69 105. 'i' */
9263446Smrj _ACPI_LO, /* 0x6A 106. 'j' */
9273446Smrj _ACPI_LO, /* 0x6B 107. 'k' */
9283446Smrj _ACPI_LO, /* 0x6C 108. 'l' */
9293446Smrj _ACPI_LO, /* 0x6D 109. 'm' */
9303446Smrj _ACPI_LO, /* 0x6E 110. 'n' */
9313446Smrj _ACPI_LO, /* 0x6F 111. 'o' */
9323446Smrj _ACPI_LO, /* 0x70 112. 'p' */
9333446Smrj _ACPI_LO, /* 0x71 113. 'q' */
9343446Smrj _ACPI_LO, /* 0x72 114. 'r' */
9353446Smrj _ACPI_LO, /* 0x73 115. 's' */
9363446Smrj _ACPI_LO, /* 0x74 116. 't' */
9373446Smrj _ACPI_LO, /* 0x75 117. 'u' */
9383446Smrj _ACPI_LO, /* 0x76 118. 'v' */
9393446Smrj _ACPI_LO, /* 0x77 119. 'w' */
9403446Smrj _ACPI_LO, /* 0x78 120. 'x' */
9413446Smrj _ACPI_LO, /* 0x79 121. 'y' */
9423446Smrj _ACPI_LO, /* 0x7A 122. 'z' */
9433446Smrj _ACPI_PU, /* 0x7B 123. '{' */
9443446Smrj _ACPI_PU, /* 0x7C 124. '|' */
9453446Smrj _ACPI_PU, /* 0x7D 125. '}' */
9463446Smrj _ACPI_PU, /* 0x7E 126. '~' */
9473446Smrj _ACPI_CN, /* 0x7F 127. */
9483446Smrj
9493446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 to 0x8F */
9503446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90 to 0x9F */
9513446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xA0 to 0xAF */
9523446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xB0 to 0xBF */
9533446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xC0 to 0xCF */
9543446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xD0 to 0xDF */
9553446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xE0 to 0xEF */
9563446Smrj 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xF0 to 0x100 */
9573446Smrj };
9583446Smrj
9593446Smrj
9603446Smrj #endif /* ACPI_USE_SYSTEM_CLIBRARY */
9613446Smrj
962