xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/utilities/utstrtoul64.c (revision 6dde014f768e0ee49bae871ed857cf433837b9ff)
1ff2e2f81SFredrik Holmqvist /*******************************************************************************
2ff2e2f81SFredrik Holmqvist  *
3ff2e2f81SFredrik Holmqvist  * Module Name: utstrtoul64 - String-to-integer conversion support for both
4ff2e2f81SFredrik Holmqvist  *                            64-bit and 32-bit integers
5ff2e2f81SFredrik Holmqvist  *
6ff2e2f81SFredrik Holmqvist  ******************************************************************************/
7ff2e2f81SFredrik Holmqvist 
8ff2e2f81SFredrik Holmqvist /******************************************************************************
9ff2e2f81SFredrik Holmqvist  *
10ff2e2f81SFredrik Holmqvist  * 1. Copyright Notice
11ff2e2f81SFredrik Holmqvist  *
12*6dde014fSPulkoMandy  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
13ff2e2f81SFredrik Holmqvist  * All rights reserved.
14ff2e2f81SFredrik Holmqvist  *
15ff2e2f81SFredrik Holmqvist  * 2. License
16ff2e2f81SFredrik Holmqvist  *
17ff2e2f81SFredrik Holmqvist  * 2.1. This is your license from Intel Corp. under its intellectual property
18ff2e2f81SFredrik Holmqvist  * rights. You may have additional license terms from the party that provided
19ff2e2f81SFredrik Holmqvist  * you this software, covering your right to use that party's intellectual
20ff2e2f81SFredrik Holmqvist  * property rights.
21ff2e2f81SFredrik Holmqvist  *
22ff2e2f81SFredrik Holmqvist  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23ff2e2f81SFredrik Holmqvist  * copy of the source code appearing in this file ("Covered Code") an
24ff2e2f81SFredrik Holmqvist  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25ff2e2f81SFredrik Holmqvist  * base code distributed originally by Intel ("Original Intel Code") to copy,
26ff2e2f81SFredrik Holmqvist  * make derivatives, distribute, use and display any portion of the Covered
27ff2e2f81SFredrik Holmqvist  * Code in any form, with the right to sublicense such rights; and
28ff2e2f81SFredrik Holmqvist  *
29ff2e2f81SFredrik Holmqvist  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30ff2e2f81SFredrik Holmqvist  * license (with the right to sublicense), under only those claims of Intel
31ff2e2f81SFredrik Holmqvist  * patents that are infringed by the Original Intel Code, to make, use, sell,
32ff2e2f81SFredrik Holmqvist  * offer to sell, and import the Covered Code and derivative works thereof
33ff2e2f81SFredrik Holmqvist  * solely to the minimum extent necessary to exercise the above copyright
34ff2e2f81SFredrik Holmqvist  * license, and in no event shall the patent license extend to any additions
35ff2e2f81SFredrik Holmqvist  * to or modifications of the Original Intel Code. No other license or right
36ff2e2f81SFredrik Holmqvist  * is granted directly or by implication, estoppel or otherwise;
37ff2e2f81SFredrik Holmqvist  *
38ff2e2f81SFredrik Holmqvist  * The above copyright and patent license is granted only if the following
39ff2e2f81SFredrik Holmqvist  * conditions are met:
40ff2e2f81SFredrik Holmqvist  *
41ff2e2f81SFredrik Holmqvist  * 3. Conditions
42ff2e2f81SFredrik Holmqvist  *
43ff2e2f81SFredrik Holmqvist  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44ff2e2f81SFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
45ff2e2f81SFredrik Holmqvist  * Code or modification with rights to further distribute source must include
46ff2e2f81SFredrik Holmqvist  * the above Copyright Notice, the above License, this list of Conditions,
47ff2e2f81SFredrik Holmqvist  * and the following Disclaimer and Export Compliance provision. In addition,
48ff2e2f81SFredrik Holmqvist  * Licensee must cause all Covered Code to which Licensee contributes to
49ff2e2f81SFredrik Holmqvist  * contain a file documenting the changes Licensee made to create that Covered
50ff2e2f81SFredrik Holmqvist  * Code and the date of any change. Licensee must include in that file the
51ff2e2f81SFredrik Holmqvist  * documentation of any changes made by any predecessor Licensee. Licensee
52ff2e2f81SFredrik Holmqvist  * must include a prominent statement that the modification is derived,
53ff2e2f81SFredrik Holmqvist  * directly or indirectly, from Original Intel Code.
54ff2e2f81SFredrik Holmqvist  *
55ff2e2f81SFredrik Holmqvist  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56ff2e2f81SFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
57ff2e2f81SFredrik Holmqvist  * Code or modification without rights to further distribute source must
58ff2e2f81SFredrik Holmqvist  * include the following Disclaimer and Export Compliance provision in the
59ff2e2f81SFredrik Holmqvist  * documentation and/or other materials provided with distribution. In
60ff2e2f81SFredrik Holmqvist  * addition, Licensee may not authorize further sublicense of source of any
61ff2e2f81SFredrik Holmqvist  * portion of the Covered Code, and must include terms to the effect that the
62ff2e2f81SFredrik Holmqvist  * license from Licensee to its licensee is limited to the intellectual
63ff2e2f81SFredrik Holmqvist  * property embodied in the software Licensee provides to its licensee, and
64ff2e2f81SFredrik Holmqvist  * not to intellectual property embodied in modifications its licensee may
65ff2e2f81SFredrik Holmqvist  * make.
66ff2e2f81SFredrik Holmqvist  *
67ff2e2f81SFredrik Holmqvist  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68ff2e2f81SFredrik Holmqvist  * substantial portion of the Covered Code or modification must reproduce the
69ff2e2f81SFredrik Holmqvist  * above Copyright Notice, and the following Disclaimer and Export Compliance
70ff2e2f81SFredrik Holmqvist  * provision in the documentation and/or other materials provided with the
71ff2e2f81SFredrik Holmqvist  * distribution.
72ff2e2f81SFredrik Holmqvist  *
73ff2e2f81SFredrik Holmqvist  * 3.4. Intel retains all right, title, and interest in and to the Original
74ff2e2f81SFredrik Holmqvist  * Intel Code.
75ff2e2f81SFredrik Holmqvist  *
76ff2e2f81SFredrik Holmqvist  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77ff2e2f81SFredrik Holmqvist  * Intel shall be used in advertising or otherwise to promote the sale, use or
78ff2e2f81SFredrik Holmqvist  * other dealings in products derived from or relating to the Covered Code
79ff2e2f81SFredrik Holmqvist  * without prior written authorization from Intel.
80ff2e2f81SFredrik Holmqvist  *
81ff2e2f81SFredrik Holmqvist  * 4. Disclaimer and Export Compliance
82ff2e2f81SFredrik Holmqvist  *
83ff2e2f81SFredrik Holmqvist  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84ff2e2f81SFredrik Holmqvist  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85ff2e2f81SFredrik Holmqvist  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86ff2e2f81SFredrik Holmqvist  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87ff2e2f81SFredrik Holmqvist  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88ff2e2f81SFredrik Holmqvist  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89ff2e2f81SFredrik Holmqvist  * PARTICULAR PURPOSE.
90ff2e2f81SFredrik Holmqvist  *
91ff2e2f81SFredrik Holmqvist  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92ff2e2f81SFredrik Holmqvist  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93ff2e2f81SFredrik Holmqvist  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94ff2e2f81SFredrik Holmqvist  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95ff2e2f81SFredrik Holmqvist  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96ff2e2f81SFredrik Holmqvist  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97ff2e2f81SFredrik Holmqvist  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98ff2e2f81SFredrik Holmqvist  * LIMITED REMEDY.
99ff2e2f81SFredrik Holmqvist  *
100ff2e2f81SFredrik Holmqvist  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101ff2e2f81SFredrik Holmqvist  * software or system incorporating such software without first obtaining any
102ff2e2f81SFredrik Holmqvist  * required license or other approval from the U. S. Department of Commerce or
103ff2e2f81SFredrik Holmqvist  * any other agency or department of the United States Government. In the
104ff2e2f81SFredrik Holmqvist  * event Licensee exports any such software from the United States or
105ff2e2f81SFredrik Holmqvist  * re-exports any such software from a foreign destination, Licensee shall
106ff2e2f81SFredrik Holmqvist  * ensure that the distribution and export/re-export of the software is in
107ff2e2f81SFredrik Holmqvist  * compliance with all laws, regulations, orders, or other restrictions of the
108ff2e2f81SFredrik Holmqvist  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109ff2e2f81SFredrik Holmqvist  * any of its subsidiaries will export/re-export any technical data, process,
110ff2e2f81SFredrik Holmqvist  * software, or service, directly or indirectly, to any country for which the
111ff2e2f81SFredrik Holmqvist  * United States government or any agency thereof requires an export license,
112ff2e2f81SFredrik Holmqvist  * other governmental approval, or letter of assurance, without first obtaining
113ff2e2f81SFredrik Holmqvist  * such license, approval or letter.
114ff2e2f81SFredrik Holmqvist  *
115ff2e2f81SFredrik Holmqvist  *****************************************************************************
116ff2e2f81SFredrik Holmqvist  *
117ff2e2f81SFredrik Holmqvist  * Alternatively, you may choose to be licensed under the terms of the
118ff2e2f81SFredrik Holmqvist  * following license:
119ff2e2f81SFredrik Holmqvist  *
120ff2e2f81SFredrik Holmqvist  * Redistribution and use in source and binary forms, with or without
121ff2e2f81SFredrik Holmqvist  * modification, are permitted provided that the following conditions
122ff2e2f81SFredrik Holmqvist  * are met:
123ff2e2f81SFredrik Holmqvist  * 1. Redistributions of source code must retain the above copyright
124ff2e2f81SFredrik Holmqvist  *    notice, this list of conditions, and the following disclaimer,
125ff2e2f81SFredrik Holmqvist  *    without modification.
126ff2e2f81SFredrik Holmqvist  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127ff2e2f81SFredrik Holmqvist  *    substantially similar to the "NO WARRANTY" disclaimer below
128ff2e2f81SFredrik Holmqvist  *    ("Disclaimer") and any redistribution must be conditioned upon
129ff2e2f81SFredrik Holmqvist  *    including a substantially similar Disclaimer requirement for further
130ff2e2f81SFredrik Holmqvist  *    binary redistribution.
131ff2e2f81SFredrik Holmqvist  * 3. Neither the names of the above-listed copyright holders nor the names
132ff2e2f81SFredrik Holmqvist  *    of any contributors may be used to endorse or promote products derived
133ff2e2f81SFredrik Holmqvist  *    from this software without specific prior written permission.
134ff2e2f81SFredrik Holmqvist  *
135ff2e2f81SFredrik Holmqvist  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136ff2e2f81SFredrik Holmqvist  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137ff2e2f81SFredrik Holmqvist  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138ff2e2f81SFredrik Holmqvist  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139ff2e2f81SFredrik Holmqvist  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140ff2e2f81SFredrik Holmqvist  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141ff2e2f81SFredrik Holmqvist  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142ff2e2f81SFredrik Holmqvist  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143ff2e2f81SFredrik Holmqvist  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144ff2e2f81SFredrik Holmqvist  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145ff2e2f81SFredrik Holmqvist  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146ff2e2f81SFredrik Holmqvist  *
147ff2e2f81SFredrik Holmqvist  * Alternatively, you may choose to be licensed under the terms of the
148ff2e2f81SFredrik Holmqvist  * GNU General Public License ("GPL") version 2 as published by the Free
149ff2e2f81SFredrik Holmqvist  * Software Foundation.
150ff2e2f81SFredrik Holmqvist  *
151ff2e2f81SFredrik Holmqvist  *****************************************************************************/
152ff2e2f81SFredrik Holmqvist 
153ff2e2f81SFredrik Holmqvist #include "acpi.h"
154ff2e2f81SFredrik Holmqvist #include "accommon.h"
155ff2e2f81SFredrik Holmqvist 
156ff2e2f81SFredrik Holmqvist #define _COMPONENT          ACPI_UTILITIES
157ff2e2f81SFredrik Holmqvist         ACPI_MODULE_NAME    ("utstrtoul64")
158ff2e2f81SFredrik Holmqvist 
159ff2e2f81SFredrik Holmqvist 
160ff2e2f81SFredrik Holmqvist /*******************************************************************************
161ff2e2f81SFredrik Holmqvist  *
162ff2e2f81SFredrik Holmqvist  * This module contains the top-level string to 64/32-bit unsigned integer
163ff2e2f81SFredrik Holmqvist  * conversion functions:
164ff2e2f81SFredrik Holmqvist  *
165ff2e2f81SFredrik Holmqvist  *  1) A standard strtoul() function that supports 64-bit integers, base
166ff2e2f81SFredrik Holmqvist  *     8/10/16, with integer overflow support. This is used mainly by the
167ff2e2f81SFredrik Holmqvist  *     iASL compiler, which implements tighter constraints on integer
168ff2e2f81SFredrik Holmqvist  *     constants than the runtime (interpreter) integer-to-string conversions.
169ff2e2f81SFredrik Holmqvist  *  2) Runtime "Explicit conversion" as defined in the ACPI specification.
170ff2e2f81SFredrik Holmqvist  *  3) Runtime "Implicit conversion" as defined in the ACPI specification.
171ff2e2f81SFredrik Holmqvist  *
172ff2e2f81SFredrik Holmqvist  * Current users of this module:
173ff2e2f81SFredrik Holmqvist  *
174ff2e2f81SFredrik Holmqvist  *  iASL        - Preprocessor (constants and math expressions)
175ff2e2f81SFredrik Holmqvist  *  iASL        - Main parser, conversion of constants to integers
176ff2e2f81SFredrik Holmqvist  *  iASL        - Data Table Compiler parser (constants and math expressions)
177ff2e2f81SFredrik Holmqvist  *  Interpreter - Implicit and explicit conversions, GPE method names
178ff2e2f81SFredrik Holmqvist  *  Interpreter - Repair code for return values from predefined names
179ff2e2f81SFredrik Holmqvist  *  Debugger    - Command line input string conversion
180ff2e2f81SFredrik Holmqvist  *  AcpiDump    - ACPI table physical addresses
181ff2e2f81SFredrik Holmqvist  *  AcpiExec    - Support for namespace overrides
182ff2e2f81SFredrik Holmqvist  *
183ff2e2f81SFredrik Holmqvist  * Notes concerning users of these interfaces:
184ff2e2f81SFredrik Holmqvist  *
185ff2e2f81SFredrik Holmqvist  * AcpiGbl_IntegerByteWidth is used to set the 32/64 bit limit for explicit
186ff2e2f81SFredrik Holmqvist  * and implicit conversions. This global must be set to the proper width.
187ff2e2f81SFredrik Holmqvist  * For the core ACPICA code, the width depends on the DSDT version. For the
188ff2e2f81SFredrik Holmqvist  * AcpiUtStrtoul64 interface, all conversions are 64 bits. This interface is
189ff2e2f81SFredrik Holmqvist  * used primarily for iASL, where the default width is 64 bits for all parsers,
190ff2e2f81SFredrik Holmqvist  * but error checking is performed later to flag cases where a 64-bit constant
191ff2e2f81SFredrik Holmqvist  * is wrongly defined in a 32-bit DSDT/SSDT.
192ff2e2f81SFredrik Holmqvist  *
193ff2e2f81SFredrik Holmqvist  * In ACPI, the only place where octal numbers are supported is within
194ff2e2f81SFredrik Holmqvist  * the ASL language itself. This is implemented via the main AcpiUtStrtoul64
195ff2e2f81SFredrik Holmqvist  * interface. According the ACPI specification, there is no ACPI runtime
196ff2e2f81SFredrik Holmqvist  * support (explicit/implicit) for octal string conversions.
197ff2e2f81SFredrik Holmqvist  *
198ff2e2f81SFredrik Holmqvist  ******************************************************************************/
199ff2e2f81SFredrik Holmqvist 
200ff2e2f81SFredrik Holmqvist 
201ff2e2f81SFredrik Holmqvist /*******************************************************************************
202ff2e2f81SFredrik Holmqvist  *
203ff2e2f81SFredrik Holmqvist  * FUNCTION:    AcpiUtStrtoul64
204ff2e2f81SFredrik Holmqvist  *
205ff2e2f81SFredrik Holmqvist  * PARAMETERS:  String                  - Null terminated input string,
206ff2e2f81SFredrik Holmqvist  *                                        must be a valid pointer
207ff2e2f81SFredrik Holmqvist  *              ReturnValue             - Where the converted integer is
208ff2e2f81SFredrik Holmqvist  *                                        returned. Must be a valid pointer
209ff2e2f81SFredrik Holmqvist  *
210ff2e2f81SFredrik Holmqvist  * RETURN:      Status and converted integer. Returns an exception on a
211ff2e2f81SFredrik Holmqvist  *              64-bit numeric overflow
212ff2e2f81SFredrik Holmqvist  *
213ff2e2f81SFredrik Holmqvist  * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
214ff2e2f81SFredrik Holmqvist  *              full 64-bit conversion, regardless of the current global
215ff2e2f81SFredrik Holmqvist  *              integer width. Supports Decimal, Hex, and Octal strings.
216ff2e2f81SFredrik Holmqvist  *
217ff2e2f81SFredrik Holmqvist  * Current users of this function:
218ff2e2f81SFredrik Holmqvist  *
219ff2e2f81SFredrik Holmqvist  *  iASL        - Preprocessor (constants and math expressions)
220ff2e2f81SFredrik Holmqvist  *  iASL        - Main ASL parser, conversion of ASL constants to integers
221ff2e2f81SFredrik Holmqvist  *  iASL        - Data Table Compiler parser (constants and math expressions)
222ff2e2f81SFredrik Holmqvist  *  Interpreter - Repair code for return values from predefined names
223ff2e2f81SFredrik Holmqvist  *  AcpiDump    - ACPI table physical addresses
224ff2e2f81SFredrik Holmqvist  *  AcpiExec    - Support for namespace overrides
225ff2e2f81SFredrik Holmqvist  *
226ff2e2f81SFredrik Holmqvist  ******************************************************************************/
227ff2e2f81SFredrik Holmqvist 
228ff2e2f81SFredrik Holmqvist ACPI_STATUS
AcpiUtStrtoul64(char * String,UINT64 * ReturnValue)229ff2e2f81SFredrik Holmqvist AcpiUtStrtoul64 (
230ff2e2f81SFredrik Holmqvist     char                    *String,
231ff2e2f81SFredrik Holmqvist     UINT64                  *ReturnValue)
232ff2e2f81SFredrik Holmqvist {
233ff2e2f81SFredrik Holmqvist     ACPI_STATUS             Status = AE_OK;
234ff2e2f81SFredrik Holmqvist     UINT8                   OriginalBitWidth;
235ff2e2f81SFredrik Holmqvist     UINT32                  Base = 10;          /* Default is decimal */
236ff2e2f81SFredrik Holmqvist 
237ff2e2f81SFredrik Holmqvist 
238ff2e2f81SFredrik Holmqvist     ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
239ff2e2f81SFredrik Holmqvist 
240ff2e2f81SFredrik Holmqvist 
241ff2e2f81SFredrik Holmqvist     *ReturnValue = 0;
242ff2e2f81SFredrik Holmqvist 
243ff2e2f81SFredrik Holmqvist     /* A NULL return string returns a value of zero */
244ff2e2f81SFredrik Holmqvist 
245ff2e2f81SFredrik Holmqvist     if (*String == 0)
246ff2e2f81SFredrik Holmqvist     {
247ff2e2f81SFredrik Holmqvist         return_ACPI_STATUS (AE_OK);
248ff2e2f81SFredrik Holmqvist     }
249ff2e2f81SFredrik Holmqvist 
250ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveWhitespace (&String))
251ff2e2f81SFredrik Holmqvist     {
252ff2e2f81SFredrik Holmqvist         return_ACPI_STATUS (AE_OK);
253ff2e2f81SFredrik Holmqvist     }
254ff2e2f81SFredrik Holmqvist 
255ff2e2f81SFredrik Holmqvist     /*
256ff2e2f81SFredrik Holmqvist      * 1) Check for a hex constant. A "0x" prefix indicates base 16.
257ff2e2f81SFredrik Holmqvist      */
258ff2e2f81SFredrik Holmqvist     if (AcpiUtDetectHexPrefix (&String))
259ff2e2f81SFredrik Holmqvist     {
260ff2e2f81SFredrik Holmqvist         Base = 16;
261ff2e2f81SFredrik Holmqvist     }
262ff2e2f81SFredrik Holmqvist 
263ff2e2f81SFredrik Holmqvist     /*
264ff2e2f81SFredrik Holmqvist      * 2) Check for an octal constant, defined to be a leading zero
265ff2e2f81SFredrik Holmqvist      * followed by sequence of octal digits (0-7)
266ff2e2f81SFredrik Holmqvist      */
267ff2e2f81SFredrik Holmqvist     else if (AcpiUtDetectOctalPrefix (&String))
268ff2e2f81SFredrik Holmqvist     {
269ff2e2f81SFredrik Holmqvist         Base = 8;
270ff2e2f81SFredrik Holmqvist     }
271ff2e2f81SFredrik Holmqvist 
272ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveLeadingZeros (&String))
273ff2e2f81SFredrik Holmqvist     {
274ff2e2f81SFredrik Holmqvist         return_ACPI_STATUS (AE_OK);     /* Return value 0 */
275ff2e2f81SFredrik Holmqvist     }
276ff2e2f81SFredrik Holmqvist 
277ff2e2f81SFredrik Holmqvist     /*
278ff2e2f81SFredrik Holmqvist      * Force a full 64-bit conversion. The caller (usually iASL) must
279ff2e2f81SFredrik Holmqvist      * check for a 32-bit overflow later as necessary (If current mode
280ff2e2f81SFredrik Holmqvist      * is 32-bit, meaning a 32-bit DSDT).
281ff2e2f81SFredrik Holmqvist      */
282ff2e2f81SFredrik Holmqvist     OriginalBitWidth = AcpiGbl_IntegerBitWidth;
283ff2e2f81SFredrik Holmqvist     AcpiGbl_IntegerBitWidth = 64;
284ff2e2f81SFredrik Holmqvist 
285ff2e2f81SFredrik Holmqvist     /*
286ff2e2f81SFredrik Holmqvist      * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
287ff2e2f81SFredrik Holmqvist      * will return an exception (to allow iASL to flag the statement).
288ff2e2f81SFredrik Holmqvist      */
289ff2e2f81SFredrik Holmqvist     switch (Base)
290ff2e2f81SFredrik Holmqvist     {
291ff2e2f81SFredrik Holmqvist     case 8:
292ff2e2f81SFredrik Holmqvist         Status = AcpiUtConvertOctalString (String, ReturnValue);
293ff2e2f81SFredrik Holmqvist         break;
294ff2e2f81SFredrik Holmqvist 
295ff2e2f81SFredrik Holmqvist     case 10:
296ff2e2f81SFredrik Holmqvist         Status = AcpiUtConvertDecimalString (String, ReturnValue);
297ff2e2f81SFredrik Holmqvist         break;
298ff2e2f81SFredrik Holmqvist 
299ff2e2f81SFredrik Holmqvist     case 16:
300ff2e2f81SFredrik Holmqvist     default:
301ff2e2f81SFredrik Holmqvist         Status = AcpiUtConvertHexString (String, ReturnValue);
302ff2e2f81SFredrik Holmqvist         break;
303ff2e2f81SFredrik Holmqvist     }
304ff2e2f81SFredrik Holmqvist 
305ff2e2f81SFredrik Holmqvist     /* Only possible exception from above is a 64-bit overflow */
306ff2e2f81SFredrik Holmqvist 
307ff2e2f81SFredrik Holmqvist     AcpiGbl_IntegerBitWidth = OriginalBitWidth;
308ff2e2f81SFredrik Holmqvist     return_ACPI_STATUS (Status);
309ff2e2f81SFredrik Holmqvist }
310ff2e2f81SFredrik Holmqvist 
311ff2e2f81SFredrik Holmqvist 
312ff2e2f81SFredrik Holmqvist /*******************************************************************************
313ff2e2f81SFredrik Holmqvist  *
314ff2e2f81SFredrik Holmqvist  * FUNCTION:    AcpiUtImplicitStrtoul64
315ff2e2f81SFredrik Holmqvist  *
316ff2e2f81SFredrik Holmqvist  * PARAMETERS:  String                  - Null terminated input string,
317ff2e2f81SFredrik Holmqvist  *                                        must be a valid pointer
318ff2e2f81SFredrik Holmqvist  *
319ff2e2f81SFredrik Holmqvist  * RETURN:      Converted integer
320ff2e2f81SFredrik Holmqvist  *
321ff2e2f81SFredrik Holmqvist  * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
322ff2e2f81SFredrik Holmqvist  *              an "implicit conversion" by the ACPI specification. Used by
323ff2e2f81SFredrik Holmqvist  *              many ASL operators that require an integer operand, and support
324ff2e2f81SFredrik Holmqvist  *              an automatic (implicit) conversion from a string operand
325ff2e2f81SFredrik Holmqvist  *              to the final integer operand. The major restriction is that
326ff2e2f81SFredrik Holmqvist  *              only hex strings are supported.
327ff2e2f81SFredrik Holmqvist  *
328ff2e2f81SFredrik Holmqvist  * -----------------------------------------------------------------------------
329ff2e2f81SFredrik Holmqvist  *
330ff2e2f81SFredrik Holmqvist  * Base is always 16, either with or without the 0x prefix. Decimal and
331ff2e2f81SFredrik Holmqvist  * Octal strings are not supported, as per the ACPI specification.
332ff2e2f81SFredrik Holmqvist  *
333ff2e2f81SFredrik Holmqvist  * Examples (both are hex values):
334ff2e2f81SFredrik Holmqvist  *      Add ("BA98", Arg0, Local0)
335ff2e2f81SFredrik Holmqvist  *      Subtract ("0x12345678", Arg1, Local1)
336ff2e2f81SFredrik Holmqvist  *
337ff2e2f81SFredrik Holmqvist  * Conversion rules as extracted from the ACPI specification:
338ff2e2f81SFredrik Holmqvist  *
339ff2e2f81SFredrik Holmqvist  *  The converted integer is initialized to the value zero.
340ff2e2f81SFredrik Holmqvist  *  The ASCII string is always interpreted as a hexadecimal constant.
341ff2e2f81SFredrik Holmqvist  *
342ff2e2f81SFredrik Holmqvist  *  1)  According to the ACPI specification, a "0x" prefix is not allowed.
343ff2e2f81SFredrik Holmqvist  *      However, ACPICA allows this as an ACPI extension on general
344ff2e2f81SFredrik Holmqvist  *      principle. (NO ERROR)
345ff2e2f81SFredrik Holmqvist  *
346ff2e2f81SFredrik Holmqvist  *  2)  The conversion terminates when the size of an integer is reached
347ff2e2f81SFredrik Holmqvist  *      (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
348ff2e2f81SFredrik Holmqvist  *
349ff2e2f81SFredrik Holmqvist  *  3)  The first non-hex character terminates the conversion and returns
350ff2e2f81SFredrik Holmqvist  *      the current accumulated value of the converted integer (NO ERROR).
351ff2e2f81SFredrik Holmqvist  *
352ff2e2f81SFredrik Holmqvist  *  4)  Conversion of a null (zero-length) string to an integer is
353ff2e2f81SFredrik Holmqvist  *      technically not allowed. However, ACPICA allows this as an ACPI
354ff2e2f81SFredrik Holmqvist  *      extension. The conversion returns the value 0. (NO ERROR)
355ff2e2f81SFredrik Holmqvist  *
356ff2e2f81SFredrik Holmqvist  * NOTE: There are no error conditions returned by this function. At
357ff2e2f81SFredrik Holmqvist  * the minimum, a value of zero is returned.
358ff2e2f81SFredrik Holmqvist  *
359ff2e2f81SFredrik Holmqvist  * Current users of this function:
360ff2e2f81SFredrik Holmqvist  *
361ff2e2f81SFredrik Holmqvist  *  Interpreter - All runtime implicit conversions, as per ACPI specification
362ff2e2f81SFredrik Holmqvist  *  iASL        - Data Table Compiler parser (constants and math expressions)
363ff2e2f81SFredrik Holmqvist  *
364ff2e2f81SFredrik Holmqvist  ******************************************************************************/
365ff2e2f81SFredrik Holmqvist 
366ff2e2f81SFredrik Holmqvist UINT64
AcpiUtImplicitStrtoul64(char * String)367ff2e2f81SFredrik Holmqvist AcpiUtImplicitStrtoul64 (
368ff2e2f81SFredrik Holmqvist     char                    *String)
369ff2e2f81SFredrik Holmqvist {
370ff2e2f81SFredrik Holmqvist     UINT64                  ConvertedInteger = 0;
371ff2e2f81SFredrik Holmqvist 
372ff2e2f81SFredrik Holmqvist 
373ff2e2f81SFredrik Holmqvist     ACPI_FUNCTION_TRACE_STR (UtImplicitStrtoul64, String);
374ff2e2f81SFredrik Holmqvist 
375ff2e2f81SFredrik Holmqvist 
376ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveWhitespace (&String))
377ff2e2f81SFredrik Holmqvist     {
378ff2e2f81SFredrik Holmqvist         return_VALUE (0);
379ff2e2f81SFredrik Holmqvist     }
380ff2e2f81SFredrik Holmqvist 
381ff2e2f81SFredrik Holmqvist     /*
382ff2e2f81SFredrik Holmqvist      * Per the ACPI specification, only hexadecimal is supported for
383ff2e2f81SFredrik Holmqvist      * implicit conversions, and the "0x" prefix is "not allowed".
384ff2e2f81SFredrik Holmqvist      * However, allow a "0x" prefix as an ACPI extension.
385ff2e2f81SFredrik Holmqvist      */
38608c9948cSFredrik Holmqvist     AcpiUtRemoveHexPrefix (&String);
387ff2e2f81SFredrik Holmqvist 
388ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveLeadingZeros (&String))
389ff2e2f81SFredrik Holmqvist     {
390ff2e2f81SFredrik Holmqvist         return_VALUE (0);
391ff2e2f81SFredrik Holmqvist     }
392ff2e2f81SFredrik Holmqvist 
393ff2e2f81SFredrik Holmqvist     /*
394ff2e2f81SFredrik Holmqvist      * Ignore overflow as per the ACPI specification. This is implemented by
395ff2e2f81SFredrik Holmqvist      * ignoring the return status from the conversion function called below.
396ff2e2f81SFredrik Holmqvist      * On overflow, the input string is simply truncated.
397ff2e2f81SFredrik Holmqvist      */
398ff2e2f81SFredrik Holmqvist     AcpiUtConvertHexString (String, &ConvertedInteger);
399ff2e2f81SFredrik Holmqvist     return_VALUE (ConvertedInteger);
400ff2e2f81SFredrik Holmqvist }
401ff2e2f81SFredrik Holmqvist 
402ff2e2f81SFredrik Holmqvist 
403ff2e2f81SFredrik Holmqvist /*******************************************************************************
404ff2e2f81SFredrik Holmqvist  *
405ff2e2f81SFredrik Holmqvist  * FUNCTION:    AcpiUtExplicitStrtoul64
406ff2e2f81SFredrik Holmqvist  *
407ff2e2f81SFredrik Holmqvist  * PARAMETERS:  String                  - Null terminated input string,
408ff2e2f81SFredrik Holmqvist  *                                        must be a valid pointer
409ff2e2f81SFredrik Holmqvist  *
410ff2e2f81SFredrik Holmqvist  * RETURN:      Converted integer
411ff2e2f81SFredrik Holmqvist  *
412ff2e2f81SFredrik Holmqvist  * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
413ff2e2f81SFredrik Holmqvist  *              an "explicit conversion" by the ACPI specification. The
414ff2e2f81SFredrik Holmqvist  *              main restriction is that only hex and decimal are supported.
415ff2e2f81SFredrik Holmqvist  *
416ff2e2f81SFredrik Holmqvist  * -----------------------------------------------------------------------------
417ff2e2f81SFredrik Holmqvist  *
418ff2e2f81SFredrik Holmqvist  * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
419ff2e2f81SFredrik Holmqvist  * are not supported, as per the ACPI specification.
420ff2e2f81SFredrik Holmqvist  *
421ff2e2f81SFredrik Holmqvist  * Examples:
422ff2e2f81SFredrik Holmqvist  *      ToInteger ("1000")      Decimal
423ff2e2f81SFredrik Holmqvist  *      ToInteger ("0xABCD")    Hex
424ff2e2f81SFredrik Holmqvist  *
425ff2e2f81SFredrik Holmqvist  * Conversion rules as extracted from the ACPI specification:
426ff2e2f81SFredrik Holmqvist  *
427ff2e2f81SFredrik Holmqvist  *  1)  The input string is either a decimal or hexadecimal numeric string.
428ff2e2f81SFredrik Holmqvist  *      A hex value must be prefixed by "0x" or it is interpreted as decimal.
429ff2e2f81SFredrik Holmqvist  *
430ff2e2f81SFredrik Holmqvist  *  2)  The value must not exceed the maximum of an integer value
431ff2e2f81SFredrik Holmqvist  *      (32 or 64 bits). The ACPI specification states the behavior is
432ff2e2f81SFredrik Holmqvist  *      "unpredictable", so ACPICA matches the behavior of the implicit
433ff2e2f81SFredrik Holmqvist  *      conversion case. There are no numeric overflow conditions. (NO ERROR)
434ff2e2f81SFredrik Holmqvist  *
435ff2e2f81SFredrik Holmqvist  *  3)  Behavior on the first non-hex character is not defined by the ACPI
436ff2e2f81SFredrik Holmqvist  *      specification (for the ToInteger operator), so ACPICA matches the
437ff2e2f81SFredrik Holmqvist  *      behavior of the implicit conversion case. It terminates the
438ff2e2f81SFredrik Holmqvist  *      conversion and returns the current accumulated value of the converted
439ff2e2f81SFredrik Holmqvist  *      integer. (NO ERROR)
440ff2e2f81SFredrik Holmqvist  *
441ff2e2f81SFredrik Holmqvist  *  4)  Conversion of a null (zero-length) string to an integer is
442ff2e2f81SFredrik Holmqvist  *      technically not allowed. However, ACPICA allows this as an ACPI
443ff2e2f81SFredrik Holmqvist  *      extension. The conversion returns the value 0. (NO ERROR)
444ff2e2f81SFredrik Holmqvist  *
445ff2e2f81SFredrik Holmqvist  * NOTE: There are no error conditions returned by this function. At the
446ff2e2f81SFredrik Holmqvist  * minimum, a value of zero is returned.
447ff2e2f81SFredrik Holmqvist  *
448ff2e2f81SFredrik Holmqvist  * Current users of this function:
449ff2e2f81SFredrik Holmqvist  *
450ff2e2f81SFredrik Holmqvist  *  Interpreter - Runtime ASL ToInteger operator, as per the ACPI specification
451ff2e2f81SFredrik Holmqvist  *
452ff2e2f81SFredrik Holmqvist  ******************************************************************************/
453ff2e2f81SFredrik Holmqvist 
454ff2e2f81SFredrik Holmqvist UINT64
AcpiUtExplicitStrtoul64(char * String)455ff2e2f81SFredrik Holmqvist AcpiUtExplicitStrtoul64 (
456ff2e2f81SFredrik Holmqvist     char                    *String)
457ff2e2f81SFredrik Holmqvist {
458ff2e2f81SFredrik Holmqvist     UINT64                  ConvertedInteger = 0;
459ff2e2f81SFredrik Holmqvist     UINT32                  Base = 10;          /* Default is decimal */
460ff2e2f81SFredrik Holmqvist 
461ff2e2f81SFredrik Holmqvist 
462ff2e2f81SFredrik Holmqvist     ACPI_FUNCTION_TRACE_STR (UtExplicitStrtoul64, String);
463ff2e2f81SFredrik Holmqvist 
464ff2e2f81SFredrik Holmqvist 
465ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveWhitespace (&String))
466ff2e2f81SFredrik Holmqvist     {
467ff2e2f81SFredrik Holmqvist         return_VALUE (0);
468ff2e2f81SFredrik Holmqvist     }
469ff2e2f81SFredrik Holmqvist 
470ff2e2f81SFredrik Holmqvist     /*
471ff2e2f81SFredrik Holmqvist      * Only Hex and Decimal are supported, as per the ACPI specification.
472ff2e2f81SFredrik Holmqvist      * A "0x" prefix indicates hex; otherwise decimal is assumed.
473ff2e2f81SFredrik Holmqvist      */
474ff2e2f81SFredrik Holmqvist     if (AcpiUtDetectHexPrefix (&String))
475ff2e2f81SFredrik Holmqvist     {
476ff2e2f81SFredrik Holmqvist         Base = 16;
477ff2e2f81SFredrik Holmqvist     }
478ff2e2f81SFredrik Holmqvist 
479ff2e2f81SFredrik Holmqvist     if (!AcpiUtRemoveLeadingZeros (&String))
480ff2e2f81SFredrik Holmqvist     {
481ff2e2f81SFredrik Holmqvist         return_VALUE (0);
482ff2e2f81SFredrik Holmqvist     }
483ff2e2f81SFredrik Holmqvist 
484ff2e2f81SFredrik Holmqvist     /*
485ff2e2f81SFredrik Holmqvist      * Ignore overflow as per the ACPI specification. This is implemented by
486ff2e2f81SFredrik Holmqvist      * ignoring the return status from the conversion functions called below.
487ff2e2f81SFredrik Holmqvist      * On overflow, the input string is simply truncated.
488ff2e2f81SFredrik Holmqvist      */
489ff2e2f81SFredrik Holmqvist     switch (Base)
490ff2e2f81SFredrik Holmqvist     {
491ff2e2f81SFredrik Holmqvist     case 10:
492ff2e2f81SFredrik Holmqvist     default:
493ff2e2f81SFredrik Holmqvist         AcpiUtConvertDecimalString (String, &ConvertedInteger);
494ff2e2f81SFredrik Holmqvist         break;
495ff2e2f81SFredrik Holmqvist 
496ff2e2f81SFredrik Holmqvist     case 16:
497ff2e2f81SFredrik Holmqvist         AcpiUtConvertHexString (String, &ConvertedInteger);
498ff2e2f81SFredrik Holmqvist         break;
499ff2e2f81SFredrik Holmqvist     }
500ff2e2f81SFredrik Holmqvist 
501ff2e2f81SFredrik Holmqvist     return_VALUE (ConvertedInteger);
502ff2e2f81SFredrik Holmqvist }
503