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