xref: /freebsd-src/sys/contrib/dev/acpica/components/utilities/utstrtoul64.c (revision 804fe2660352e090f4481f2c1d646b508859e79a)
1493deb39SJung-uk Kim /*******************************************************************************
2493deb39SJung-uk Kim  *
32f6a1a81SJung-uk Kim  * Module Name: utstrtoul64 - String-to-integer conversion support for both
42f6a1a81SJung-uk Kim  *                            64-bit and 32-bit integers
5493deb39SJung-uk Kim  *
6493deb39SJung-uk Kim  ******************************************************************************/
7493deb39SJung-uk Kim 
80d84335fSJung-uk Kim /******************************************************************************
90d84335fSJung-uk Kim  *
100d84335fSJung-uk Kim  * 1. Copyright Notice
110d84335fSJung-uk Kim  *
12*804fe266SJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
13493deb39SJung-uk Kim  * All rights reserved.
14493deb39SJung-uk Kim  *
150d84335fSJung-uk Kim  * 2. License
160d84335fSJung-uk Kim  *
170d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
180d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
190d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
200d84335fSJung-uk Kim  * property rights.
210d84335fSJung-uk Kim  *
220d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
230d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
240d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
250d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
260d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
270d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
280d84335fSJung-uk Kim  *
290d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
300d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
310d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
320d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
330d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
340d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
350d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
360d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
370d84335fSJung-uk Kim  *
380d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
390d84335fSJung-uk Kim  * conditions are met:
400d84335fSJung-uk Kim  *
410d84335fSJung-uk Kim  * 3. Conditions
420d84335fSJung-uk Kim  *
430d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
440d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
450d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
460d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
470d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
480d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
490d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
500d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
510d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
520d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
530d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
540d84335fSJung-uk Kim  *
550d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
560d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
570d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
580d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
590d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
600d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
610d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
620d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
630d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
640d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
650d84335fSJung-uk Kim  * make.
660d84335fSJung-uk Kim  *
670d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
680d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
690d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
700d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
710d84335fSJung-uk Kim  * distribution.
720d84335fSJung-uk Kim  *
730d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
740d84335fSJung-uk Kim  * Intel Code.
750d84335fSJung-uk Kim  *
760d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
770d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
780d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
790d84335fSJung-uk Kim  * without prior written authorization from Intel.
800d84335fSJung-uk Kim  *
810d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
820d84335fSJung-uk Kim  *
830d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
840d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
850d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
860d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
870d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
880d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
890d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
900d84335fSJung-uk Kim  *
910d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
920d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
930d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
940d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
950d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
960d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
970d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
980d84335fSJung-uk Kim  * LIMITED REMEDY.
990d84335fSJung-uk Kim  *
1000d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1010d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1020d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1030d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1040d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1050d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1060d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1070d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1080d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1090d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1100d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1110d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1120d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1130d84335fSJung-uk Kim  * such license, approval or letter.
1140d84335fSJung-uk Kim  *
1150d84335fSJung-uk Kim  *****************************************************************************
1160d84335fSJung-uk Kim  *
1170d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1180d84335fSJung-uk Kim  * following license:
1190d84335fSJung-uk Kim  *
120493deb39SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
121493deb39SJung-uk Kim  * modification, are permitted provided that the following conditions
122493deb39SJung-uk Kim  * are met:
123493deb39SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
124493deb39SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
125493deb39SJung-uk Kim  *    without modification.
126493deb39SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127493deb39SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
128493deb39SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
129493deb39SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
130493deb39SJung-uk Kim  *    binary redistribution.
131493deb39SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
132493deb39SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
133493deb39SJung-uk Kim  *    from this software without specific prior written permission.
134493deb39SJung-uk Kim  *
1350d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1360d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1370d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1380d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1390d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1400d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1410d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1420d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1430d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1440d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1450d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1460d84335fSJung-uk Kim  *
1470d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
148493deb39SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
149493deb39SJung-uk Kim  * Software Foundation.
150493deb39SJung-uk Kim  *
1510d84335fSJung-uk Kim  *****************************************************************************/
152493deb39SJung-uk Kim 
153493deb39SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
154493deb39SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
155493deb39SJung-uk Kim 
156493deb39SJung-uk Kim #define _COMPONENT          ACPI_UTILITIES
157493deb39SJung-uk Kim         ACPI_MODULE_NAME    ("utstrtoul64")
158493deb39SJung-uk Kim 
159493deb39SJung-uk Kim 
160493deb39SJung-uk Kim /*******************************************************************************
161493deb39SJung-uk Kim  *
1622f6a1a81SJung-uk Kim  * This module contains the top-level string to 64/32-bit unsigned integer
1632f6a1a81SJung-uk Kim  * conversion functions:
164493deb39SJung-uk Kim  *
1652f6a1a81SJung-uk Kim  *  1) A standard strtoul() function that supports 64-bit integers, base
1662f6a1a81SJung-uk Kim  *     8/10/16, with integer overflow support. This is used mainly by the
1672f6a1a81SJung-uk Kim  *     iASL compiler, which implements tighter constraints on integer
1682f6a1a81SJung-uk Kim  *     constants than the runtime (interpreter) integer-to-string conversions.
1692f6a1a81SJung-uk Kim  *  2) Runtime "Explicit conversion" as defined in the ACPI specification.
1702f6a1a81SJung-uk Kim  *  3) Runtime "Implicit conversion" as defined in the ACPI specification.
171493deb39SJung-uk Kim  *
1722f6a1a81SJung-uk Kim  * Current users of this module:
173493deb39SJung-uk Kim  *
1742f6a1a81SJung-uk Kim  *  iASL        - Preprocessor (constants and math expressions)
1752f6a1a81SJung-uk Kim  *  iASL        - Main parser, conversion of constants to integers
1762f6a1a81SJung-uk Kim  *  iASL        - Data Table Compiler parser (constants and math expressions)
1772f6a1a81SJung-uk Kim  *  Interpreter - Implicit and explicit conversions, GPE method names
1782f6a1a81SJung-uk Kim  *  Interpreter - Repair code for return values from predefined names
1792f6a1a81SJung-uk Kim  *  Debugger    - Command line input string conversion
1802f6a1a81SJung-uk Kim  *  AcpiDump    - ACPI table physical addresses
1812f6a1a81SJung-uk Kim  *  AcpiExec    - Support for namespace overrides
182493deb39SJung-uk Kim  *
1832f6a1a81SJung-uk Kim  * Notes concerning users of these interfaces:
184493deb39SJung-uk Kim  *
1852f6a1a81SJung-uk Kim  * AcpiGbl_IntegerByteWidth is used to set the 32/64 bit limit for explicit
1862f6a1a81SJung-uk Kim  * and implicit conversions. This global must be set to the proper width.
1872f6a1a81SJung-uk Kim  * For the core ACPICA code, the width depends on the DSDT version. For the
1882f6a1a81SJung-uk Kim  * AcpiUtStrtoul64 interface, all conversions are 64 bits. This interface is
1892f6a1a81SJung-uk Kim  * used primarily for iASL, where the default width is 64 bits for all parsers,
1902f6a1a81SJung-uk Kim  * but error checking is performed later to flag cases where a 64-bit constant
1912f6a1a81SJung-uk Kim  * is wrongly defined in a 32-bit DSDT/SSDT.
192493deb39SJung-uk Kim  *
1932f6a1a81SJung-uk Kim  * In ACPI, the only place where octal numbers are supported is within
1942f6a1a81SJung-uk Kim  * the ASL language itself. This is implemented via the main AcpiUtStrtoul64
1952f6a1a81SJung-uk Kim  * interface. According the ACPI specification, there is no ACPI runtime
1962f6a1a81SJung-uk Kim  * support (explicit/implicit) for octal string conversions.
197493deb39SJung-uk Kim  *
198493deb39SJung-uk Kim  ******************************************************************************/
199493deb39SJung-uk Kim 
200493deb39SJung-uk Kim 
201493deb39SJung-uk Kim /*******************************************************************************
202493deb39SJung-uk Kim  *
203493deb39SJung-uk Kim  * FUNCTION:    AcpiUtStrtoul64
204493deb39SJung-uk Kim  *
2052f6a1a81SJung-uk Kim  * PARAMETERS:  String                  - Null terminated input string,
2062f6a1a81SJung-uk Kim  *                                        must be a valid pointer
207493deb39SJung-uk Kim  *              ReturnValue             - Where the converted integer is
2082f6a1a81SJung-uk Kim  *                                        returned. Must be a valid pointer
209493deb39SJung-uk Kim  *
2102f6a1a81SJung-uk Kim  * RETURN:      Status and converted integer. Returns an exception on a
2112f6a1a81SJung-uk Kim  *              64-bit numeric overflow
212493deb39SJung-uk Kim  *
2132f6a1a81SJung-uk Kim  * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
2142f6a1a81SJung-uk Kim  *              full 64-bit conversion, regardless of the current global
2152f6a1a81SJung-uk Kim  *              integer width. Supports Decimal, Hex, and Octal strings.
216493deb39SJung-uk Kim  *
2172f6a1a81SJung-uk Kim  * Current users of this function:
218493deb39SJung-uk Kim  *
2192f6a1a81SJung-uk Kim  *  iASL        - Preprocessor (constants and math expressions)
2202f6a1a81SJung-uk Kim  *  iASL        - Main ASL parser, conversion of ASL constants to integers
2212f6a1a81SJung-uk Kim  *  iASL        - Data Table Compiler parser (constants and math expressions)
2222f6a1a81SJung-uk Kim  *  Interpreter - Repair code for return values from predefined names
2232f6a1a81SJung-uk Kim  *  AcpiDump    - ACPI table physical addresses
2242f6a1a81SJung-uk Kim  *  AcpiExec    - Support for namespace overrides
225493deb39SJung-uk Kim  *
226493deb39SJung-uk Kim  ******************************************************************************/
227493deb39SJung-uk Kim 
228493deb39SJung-uk Kim ACPI_STATUS
229493deb39SJung-uk Kim AcpiUtStrtoul64 (
230493deb39SJung-uk Kim     char                    *String,
231493deb39SJung-uk Kim     UINT64                  *ReturnValue)
232493deb39SJung-uk Kim {
233493deb39SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
2342f6a1a81SJung-uk Kim     UINT8                   OriginalBitWidth;
2352f6a1a81SJung-uk Kim     UINT32                  Base = 10;          /* Default is decimal */
236493deb39SJung-uk Kim 
237493deb39SJung-uk Kim 
238493deb39SJung-uk Kim     ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
239493deb39SJung-uk Kim 
240493deb39SJung-uk Kim 
241493deb39SJung-uk Kim     *ReturnValue = 0;
242493deb39SJung-uk Kim 
2432f6a1a81SJung-uk Kim     /* A NULL return string returns a value of zero */
244493deb39SJung-uk Kim 
245493deb39SJung-uk Kim     if (*String == 0)
246493deb39SJung-uk Kim     {
247493deb39SJung-uk Kim         return_ACPI_STATUS (AE_OK);
248493deb39SJung-uk Kim     }
249493deb39SJung-uk Kim 
2502f6a1a81SJung-uk Kim     if (!AcpiUtRemoveWhitespace (&String))
251493deb39SJung-uk Kim     {
252493deb39SJung-uk Kim         return_ACPI_STATUS (AE_OK);
253493deb39SJung-uk Kim     }
254493deb39SJung-uk Kim 
255493deb39SJung-uk Kim     /*
2562f6a1a81SJung-uk Kim      * 1) Check for a hex constant. A "0x" prefix indicates base 16.
257493deb39SJung-uk Kim      */
2582f6a1a81SJung-uk Kim     if (AcpiUtDetectHexPrefix (&String))
259493deb39SJung-uk Kim     {
260493deb39SJung-uk Kim         Base = 16;
261493deb39SJung-uk Kim     }
262493deb39SJung-uk Kim 
2632f6a1a81SJung-uk Kim     /*
2642f6a1a81SJung-uk Kim      * 2) Check for an octal constant, defined to be a leading zero
2652f6a1a81SJung-uk Kim      * followed by sequence of octal digits (0-7)
2662f6a1a81SJung-uk Kim      */
2672f6a1a81SJung-uk Kim     else if (AcpiUtDetectOctalPrefix (&String))
268493deb39SJung-uk Kim     {
2692f6a1a81SJung-uk Kim         Base = 8;
270493deb39SJung-uk Kim     }
271493deb39SJung-uk Kim 
2722f6a1a81SJung-uk Kim     if (!AcpiUtRemoveLeadingZeros (&String))
273493deb39SJung-uk Kim     {
274493deb39SJung-uk Kim         return_ACPI_STATUS (AE_OK);     /* Return value 0 */
275493deb39SJung-uk Kim     }
276493deb39SJung-uk Kim 
2772f6a1a81SJung-uk Kim     /*
2782f6a1a81SJung-uk Kim      * Force a full 64-bit conversion. The caller (usually iASL) must
2792f6a1a81SJung-uk Kim      * check for a 32-bit overflow later as necessary (If current mode
2802f6a1a81SJung-uk Kim      * is 32-bit, meaning a 32-bit DSDT).
2812f6a1a81SJung-uk Kim      */
2822f6a1a81SJung-uk Kim     OriginalBitWidth = AcpiGbl_IntegerBitWidth;
2832f6a1a81SJung-uk Kim     AcpiGbl_IntegerBitWidth = 64;
284493deb39SJung-uk Kim 
2852f6a1a81SJung-uk Kim     /*
2862f6a1a81SJung-uk Kim      * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
2872f6a1a81SJung-uk Kim      * will return an exception (to allow iASL to flag the statement).
2882f6a1a81SJung-uk Kim      */
2892f6a1a81SJung-uk Kim     switch (Base)
290493deb39SJung-uk Kim     {
2912f6a1a81SJung-uk Kim     case 8:
2922f6a1a81SJung-uk Kim         Status = AcpiUtConvertOctalString (String, ReturnValue);
2932f6a1a81SJung-uk Kim         break;
2942f6a1a81SJung-uk Kim 
2952f6a1a81SJung-uk Kim     case 10:
2962f6a1a81SJung-uk Kim         Status = AcpiUtConvertDecimalString (String, ReturnValue);
2972f6a1a81SJung-uk Kim         break;
2982f6a1a81SJung-uk Kim 
2992f6a1a81SJung-uk Kim     case 16:
3002f6a1a81SJung-uk Kim     default:
3012f6a1a81SJung-uk Kim         Status = AcpiUtConvertHexString (String, ReturnValue);
3022f6a1a81SJung-uk Kim         break;
303493deb39SJung-uk Kim     }
304493deb39SJung-uk Kim 
3052f6a1a81SJung-uk Kim     /* Only possible exception from above is a 64-bit overflow */
3062f6a1a81SJung-uk Kim 
3072f6a1a81SJung-uk Kim     AcpiGbl_IntegerBitWidth = OriginalBitWidth;
308493deb39SJung-uk Kim     return_ACPI_STATUS (Status);
309493deb39SJung-uk Kim }
310493deb39SJung-uk Kim 
311493deb39SJung-uk Kim 
312493deb39SJung-uk Kim /*******************************************************************************
313493deb39SJung-uk Kim  *
3142f6a1a81SJung-uk Kim  * FUNCTION:    AcpiUtImplicitStrtoul64
315493deb39SJung-uk Kim  *
3162f6a1a81SJung-uk Kim  * PARAMETERS:  String                  - Null terminated input string,
3172f6a1a81SJung-uk Kim  *                                        must be a valid pointer
318493deb39SJung-uk Kim  *
3192f6a1a81SJung-uk Kim  * RETURN:      Converted integer
320493deb39SJung-uk Kim  *
3212f6a1a81SJung-uk Kim  * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
3222f6a1a81SJung-uk Kim  *              an "implicit conversion" by the ACPI specification. Used by
3232f6a1a81SJung-uk Kim  *              many ASL operators that require an integer operand, and support
3242f6a1a81SJung-uk Kim  *              an automatic (implicit) conversion from a string operand
3252f6a1a81SJung-uk Kim  *              to the final integer operand. The major restriction is that
3262f6a1a81SJung-uk Kim  *              only hex strings are supported.
3272f6a1a81SJung-uk Kim  *
3282f6a1a81SJung-uk Kim  * -----------------------------------------------------------------------------
3292f6a1a81SJung-uk Kim  *
3302f6a1a81SJung-uk Kim  * Base is always 16, either with or without the 0x prefix. Decimal and
3312f6a1a81SJung-uk Kim  * Octal strings are not supported, as per the ACPI specification.
3322f6a1a81SJung-uk Kim  *
3332f6a1a81SJung-uk Kim  * Examples (both are hex values):
3342f6a1a81SJung-uk Kim  *      Add ("BA98", Arg0, Local0)
3352f6a1a81SJung-uk Kim  *      Subtract ("0x12345678", Arg1, Local1)
3362f6a1a81SJung-uk Kim  *
3372f6a1a81SJung-uk Kim  * Conversion rules as extracted from the ACPI specification:
3382f6a1a81SJung-uk Kim  *
3392f6a1a81SJung-uk Kim  *  The converted integer is initialized to the value zero.
3402f6a1a81SJung-uk Kim  *  The ASCII string is always interpreted as a hexadecimal constant.
3412f6a1a81SJung-uk Kim  *
3422f6a1a81SJung-uk Kim  *  1)  According to the ACPI specification, a "0x" prefix is not allowed.
3432f6a1a81SJung-uk Kim  *      However, ACPICA allows this as an ACPI extension on general
3442f6a1a81SJung-uk Kim  *      principle. (NO ERROR)
3452f6a1a81SJung-uk Kim  *
3462f6a1a81SJung-uk Kim  *  2)  The conversion terminates when the size of an integer is reached
3472f6a1a81SJung-uk Kim  *      (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
3482f6a1a81SJung-uk Kim  *
3492f6a1a81SJung-uk Kim  *  3)  The first non-hex character terminates the conversion and returns
3502f6a1a81SJung-uk Kim  *      the current accumulated value of the converted integer (NO ERROR).
3512f6a1a81SJung-uk Kim  *
3522f6a1a81SJung-uk Kim  *  4)  Conversion of a null (zero-length) string to an integer is
3532f6a1a81SJung-uk Kim  *      technically not allowed. However, ACPICA allows this as an ACPI
3542f6a1a81SJung-uk Kim  *      extension. The conversion returns the value 0. (NO ERROR)
3552f6a1a81SJung-uk Kim  *
3562f6a1a81SJung-uk Kim  * NOTE: There are no error conditions returned by this function. At
3572f6a1a81SJung-uk Kim  * the minimum, a value of zero is returned.
3582f6a1a81SJung-uk Kim  *
3592f6a1a81SJung-uk Kim  * Current users of this function:
3602f6a1a81SJung-uk Kim  *
3612f6a1a81SJung-uk Kim  *  Interpreter - All runtime implicit conversions, as per ACPI specification
3622f6a1a81SJung-uk Kim  *  iASL        - Data Table Compiler parser (constants and math expressions)
363493deb39SJung-uk Kim  *
364493deb39SJung-uk Kim  ******************************************************************************/
365493deb39SJung-uk Kim 
3662f6a1a81SJung-uk Kim UINT64
3672f6a1a81SJung-uk Kim AcpiUtImplicitStrtoul64 (
3682f6a1a81SJung-uk Kim     char                    *String)
369493deb39SJung-uk Kim {
3702f6a1a81SJung-uk Kim     UINT64                  ConvertedInteger = 0;
371493deb39SJung-uk Kim 
372493deb39SJung-uk Kim 
3732f6a1a81SJung-uk Kim     ACPI_FUNCTION_TRACE_STR (UtImplicitStrtoul64, String);
374493deb39SJung-uk Kim 
3752f6a1a81SJung-uk Kim 
3762f6a1a81SJung-uk Kim     if (!AcpiUtRemoveWhitespace (&String))
377493deb39SJung-uk Kim     {
3782f6a1a81SJung-uk Kim         return_VALUE (0);
379493deb39SJung-uk Kim     }
380493deb39SJung-uk Kim 
3812f6a1a81SJung-uk Kim     /*
3822f6a1a81SJung-uk Kim      * Per the ACPI specification, only hexadecimal is supported for
3832f6a1a81SJung-uk Kim      * implicit conversions, and the "0x" prefix is "not allowed".
3842f6a1a81SJung-uk Kim      * However, allow a "0x" prefix as an ACPI extension.
3852f6a1a81SJung-uk Kim      */
38651f42badSJung-uk Kim     AcpiUtRemoveHexPrefix (&String);
387493deb39SJung-uk Kim 
3882f6a1a81SJung-uk Kim     if (!AcpiUtRemoveLeadingZeros (&String))
389493deb39SJung-uk Kim     {
3902f6a1a81SJung-uk Kim         return_VALUE (0);
391493deb39SJung-uk Kim     }
392493deb39SJung-uk Kim 
3932f6a1a81SJung-uk Kim     /*
3942f6a1a81SJung-uk Kim      * Ignore overflow as per the ACPI specification. This is implemented by
3952f6a1a81SJung-uk Kim      * ignoring the return status from the conversion function called below.
3962f6a1a81SJung-uk Kim      * On overflow, the input string is simply truncated.
3972f6a1a81SJung-uk Kim      */
3982f6a1a81SJung-uk Kim     AcpiUtConvertHexString (String, &ConvertedInteger);
3992f6a1a81SJung-uk Kim     return_VALUE (ConvertedInteger);
400493deb39SJung-uk Kim }
401493deb39SJung-uk Kim 
402493deb39SJung-uk Kim 
403493deb39SJung-uk Kim /*******************************************************************************
404493deb39SJung-uk Kim  *
4052f6a1a81SJung-uk Kim  * FUNCTION:    AcpiUtExplicitStrtoul64
406493deb39SJung-uk Kim  *
4072f6a1a81SJung-uk Kim  * PARAMETERS:  String                  - Null terminated input string,
4082f6a1a81SJung-uk Kim  *                                        must be a valid pointer
409493deb39SJung-uk Kim  *
4102f6a1a81SJung-uk Kim  * RETURN:      Converted integer
411493deb39SJung-uk Kim  *
4122f6a1a81SJung-uk Kim  * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
4132f6a1a81SJung-uk Kim  *              an "explicit conversion" by the ACPI specification. The
4142f6a1a81SJung-uk Kim  *              main restriction is that only hex and decimal are supported.
4152f6a1a81SJung-uk Kim  *
4162f6a1a81SJung-uk Kim  * -----------------------------------------------------------------------------
4172f6a1a81SJung-uk Kim  *
4182f6a1a81SJung-uk Kim  * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
4192f6a1a81SJung-uk Kim  * are not supported, as per the ACPI specification.
4202f6a1a81SJung-uk Kim  *
4212f6a1a81SJung-uk Kim  * Examples:
4222f6a1a81SJung-uk Kim  *      ToInteger ("1000")      Decimal
4232f6a1a81SJung-uk Kim  *      ToInteger ("0xABCD")    Hex
4242f6a1a81SJung-uk Kim  *
4252f6a1a81SJung-uk Kim  * Conversion rules as extracted from the ACPI specification:
4262f6a1a81SJung-uk Kim  *
4272f6a1a81SJung-uk Kim  *  1)  The input string is either a decimal or hexadecimal numeric string.
4282f6a1a81SJung-uk Kim  *      A hex value must be prefixed by "0x" or it is interpreted as decimal.
4292f6a1a81SJung-uk Kim  *
4302f6a1a81SJung-uk Kim  *  2)  The value must not exceed the maximum of an integer value
4312f6a1a81SJung-uk Kim  *      (32 or 64 bits). The ACPI specification states the behavior is
4322f6a1a81SJung-uk Kim  *      "unpredictable", so ACPICA matches the behavior of the implicit
4332f6a1a81SJung-uk Kim  *      conversion case. There are no numeric overflow conditions. (NO ERROR)
4342f6a1a81SJung-uk Kim  *
4352f6a1a81SJung-uk Kim  *  3)  Behavior on the first non-hex character is not defined by the ACPI
4362f6a1a81SJung-uk Kim  *      specification (for the ToInteger operator), so ACPICA matches the
4372f6a1a81SJung-uk Kim  *      behavior of the implicit conversion case. It terminates the
4382f6a1a81SJung-uk Kim  *      conversion and returns the current accumulated value of the converted
4392f6a1a81SJung-uk Kim  *      integer. (NO ERROR)
4402f6a1a81SJung-uk Kim  *
4412f6a1a81SJung-uk Kim  *  4)  Conversion of a null (zero-length) string to an integer is
4422f6a1a81SJung-uk Kim  *      technically not allowed. However, ACPICA allows this as an ACPI
4432f6a1a81SJung-uk Kim  *      extension. The conversion returns the value 0. (NO ERROR)
4442f6a1a81SJung-uk Kim  *
4452f6a1a81SJung-uk Kim  * NOTE: There are no error conditions returned by this function. At the
4462f6a1a81SJung-uk Kim  * minimum, a value of zero is returned.
4472f6a1a81SJung-uk Kim  *
4482f6a1a81SJung-uk Kim  * Current users of this function:
4492f6a1a81SJung-uk Kim  *
4502f6a1a81SJung-uk Kim  *  Interpreter - Runtime ASL ToInteger operator, as per the ACPI specification
451493deb39SJung-uk Kim  *
452493deb39SJung-uk Kim  ******************************************************************************/
453493deb39SJung-uk Kim 
4542f6a1a81SJung-uk Kim UINT64
4552f6a1a81SJung-uk Kim AcpiUtExplicitStrtoul64 (
4562f6a1a81SJung-uk Kim     char                    *String)
457493deb39SJung-uk Kim {
4582f6a1a81SJung-uk Kim     UINT64                  ConvertedInteger = 0;
4592f6a1a81SJung-uk Kim     UINT32                  Base = 10;          /* Default is decimal */
460493deb39SJung-uk Kim 
461493deb39SJung-uk Kim 
4622f6a1a81SJung-uk Kim     ACPI_FUNCTION_TRACE_STR (UtExplicitStrtoul64, String);
463493deb39SJung-uk Kim 
4642f6a1a81SJung-uk Kim 
4652f6a1a81SJung-uk Kim     if (!AcpiUtRemoveWhitespace (&String))
466493deb39SJung-uk Kim     {
4672f6a1a81SJung-uk Kim         return_VALUE (0);
468493deb39SJung-uk Kim     }
469493deb39SJung-uk Kim 
4702f6a1a81SJung-uk Kim     /*
4712f6a1a81SJung-uk Kim      * Only Hex and Decimal are supported, as per the ACPI specification.
4722f6a1a81SJung-uk Kim      * A "0x" prefix indicates hex; otherwise decimal is assumed.
4732f6a1a81SJung-uk Kim      */
4742f6a1a81SJung-uk Kim     if (AcpiUtDetectHexPrefix (&String))
475493deb39SJung-uk Kim     {
4762f6a1a81SJung-uk Kim         Base = 16;
477493deb39SJung-uk Kim     }
478493deb39SJung-uk Kim 
4792f6a1a81SJung-uk Kim     if (!AcpiUtRemoveLeadingZeros (&String))
4802f6a1a81SJung-uk Kim     {
4812f6a1a81SJung-uk Kim         return_VALUE (0);
482493deb39SJung-uk Kim     }
483493deb39SJung-uk Kim 
4842f6a1a81SJung-uk Kim     /*
4852f6a1a81SJung-uk Kim      * Ignore overflow as per the ACPI specification. This is implemented by
4862f6a1a81SJung-uk Kim      * ignoring the return status from the conversion functions called below.
4872f6a1a81SJung-uk Kim      * On overflow, the input string is simply truncated.
4882f6a1a81SJung-uk Kim      */
4892f6a1a81SJung-uk Kim     switch (Base)
4902f6a1a81SJung-uk Kim     {
4912f6a1a81SJung-uk Kim     case 10:
4922f6a1a81SJung-uk Kim     default:
4932f6a1a81SJung-uk Kim         AcpiUtConvertDecimalString (String, &ConvertedInteger);
4942f6a1a81SJung-uk Kim         break;
4952f6a1a81SJung-uk Kim 
4962f6a1a81SJung-uk Kim     case 16:
4972f6a1a81SJung-uk Kim         AcpiUtConvertHexString (String, &ConvertedInteger);
4982f6a1a81SJung-uk Kim         break;
4992f6a1a81SJung-uk Kim     }
5002f6a1a81SJung-uk Kim 
5012f6a1a81SJung-uk Kim     return_VALUE (ConvertedInteger);
502493deb39SJung-uk Kim }
503