1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /*******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: utstrtoul64 - String-to-integer conversion support for both
5*4882a593Smuzhiyun * 64-bit and 32-bit integers
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun ******************************************************************************/
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <acpi/acpi.h>
10*4882a593Smuzhiyun #include "accommon.h"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define _COMPONENT ACPI_UTILITIES
13*4882a593Smuzhiyun ACPI_MODULE_NAME("utstrtoul64")
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*******************************************************************************
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * This module contains the top-level string to 64/32-bit unsigned integer
18*4882a593Smuzhiyun * conversion functions:
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * 1) A standard strtoul() function that supports 64-bit integers, base
21*4882a593Smuzhiyun * 8/10/16, with integer overflow support. This is used mainly by the
22*4882a593Smuzhiyun * iASL compiler, which implements tighter constraints on integer
23*4882a593Smuzhiyun * constants than the runtime (interpreter) integer-to-string conversions.
24*4882a593Smuzhiyun * 2) Runtime "Explicit conversion" as defined in the ACPI specification.
25*4882a593Smuzhiyun * 3) Runtime "Implicit conversion" as defined in the ACPI specification.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Current users of this module:
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * iASL - Preprocessor (constants and math expressions)
30*4882a593Smuzhiyun * iASL - Main parser, conversion of constants to integers
31*4882a593Smuzhiyun * iASL - Data Table Compiler parser (constants and math expressions)
32*4882a593Smuzhiyun * interpreter - Implicit and explicit conversions, GPE method names
33*4882a593Smuzhiyun * interpreter - Repair code for return values from predefined names
34*4882a593Smuzhiyun * debugger - Command line input string conversion
35*4882a593Smuzhiyun * acpi_dump - ACPI table physical addresses
36*4882a593Smuzhiyun * acpi_exec - Support for namespace overrides
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Notes concerning users of these interfaces:
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * acpi_gbl_integer_byte_width is used to set the 32/64 bit limit for explicit
41*4882a593Smuzhiyun * and implicit conversions. This global must be set to the proper width.
42*4882a593Smuzhiyun * For the core ACPICA code, the width depends on the DSDT version. For the
43*4882a593Smuzhiyun * acpi_ut_strtoul64 interface, all conversions are 64 bits. This interface is
44*4882a593Smuzhiyun * used primarily for iASL, where the default width is 64 bits for all parsers,
45*4882a593Smuzhiyun * but error checking is performed later to flag cases where a 64-bit constant
46*4882a593Smuzhiyun * is wrongly defined in a 32-bit DSDT/SSDT.
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * In ACPI, the only place where octal numbers are supported is within
49*4882a593Smuzhiyun * the ASL language itself. This is implemented via the main acpi_ut_strtoul64
50*4882a593Smuzhiyun * interface. According the ACPI specification, there is no ACPI runtime
51*4882a593Smuzhiyun * support (explicit/implicit) for octal string conversions.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun ******************************************************************************/
54*4882a593Smuzhiyun /*******************************************************************************
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * FUNCTION: acpi_ut_strtoul64
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * PARAMETERS: string - Null terminated input string,
59*4882a593Smuzhiyun * must be a valid pointer
60*4882a593Smuzhiyun * return_value - Where the converted integer is
61*4882a593Smuzhiyun * returned. Must be a valid pointer
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * RETURN: Status and converted integer. Returns an exception on a
64*4882a593Smuzhiyun * 64-bit numeric overflow
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
67*4882a593Smuzhiyun * full 64-bit conversion, regardless of the current global
68*4882a593Smuzhiyun * integer width. Supports Decimal, Hex, and Octal strings.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * Current users of this function:
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * iASL - Preprocessor (constants and math expressions)
73*4882a593Smuzhiyun * iASL - Main ASL parser, conversion of ASL constants to integers
74*4882a593Smuzhiyun * iASL - Data Table Compiler parser (constants and math expressions)
75*4882a593Smuzhiyun * interpreter - Repair code for return values from predefined names
76*4882a593Smuzhiyun * acpi_dump - ACPI table physical addresses
77*4882a593Smuzhiyun * acpi_exec - Support for namespace overrides
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun ******************************************************************************/
acpi_ut_strtoul64(char * string,u64 * return_value)80*4882a593Smuzhiyun acpi_status acpi_ut_strtoul64(char *string, u64 *return_value)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun acpi_status status = AE_OK;
83*4882a593Smuzhiyun u8 original_bit_width;
84*4882a593Smuzhiyun u32 base = 10; /* Default is decimal */
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun *return_value = 0;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* A NULL return string returns a value of zero */
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (*string == 0) {
93*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (!acpi_ut_remove_whitespace(&string)) {
97*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * 1) Check for a hex constant. A "0x" prefix indicates base 16.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun if (acpi_ut_detect_hex_prefix(&string)) {
104*4882a593Smuzhiyun base = 16;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * 2) Check for an octal constant, defined to be a leading zero
109*4882a593Smuzhiyun * followed by sequence of octal digits (0-7)
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun else if (acpi_ut_detect_octal_prefix(&string)) {
112*4882a593Smuzhiyun base = 8;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun if (!acpi_ut_remove_leading_zeros(&string)) {
116*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK); /* Return value 0 */
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Force a full 64-bit conversion. The caller (usually iASL) must
121*4882a593Smuzhiyun * check for a 32-bit overflow later as necessary (If current mode
122*4882a593Smuzhiyun * is 32-bit, meaning a 32-bit DSDT).
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun original_bit_width = acpi_gbl_integer_bit_width;
125*4882a593Smuzhiyun acpi_gbl_integer_bit_width = 64;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
129*4882a593Smuzhiyun * will return an exception (to allow iASL to flag the statement).
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun switch (base) {
132*4882a593Smuzhiyun case 8:
133*4882a593Smuzhiyun status = acpi_ut_convert_octal_string(string, return_value);
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun case 10:
137*4882a593Smuzhiyun status = acpi_ut_convert_decimal_string(string, return_value);
138*4882a593Smuzhiyun break;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun case 16:
141*4882a593Smuzhiyun default:
142*4882a593Smuzhiyun status = acpi_ut_convert_hex_string(string, return_value);
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Only possible exception from above is a 64-bit overflow */
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun acpi_gbl_integer_bit_width = original_bit_width;
149*4882a593Smuzhiyun return_ACPI_STATUS(status);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*******************************************************************************
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * FUNCTION: acpi_ut_implicit_strtoul64
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * PARAMETERS: string - Null terminated input string,
157*4882a593Smuzhiyun * must be a valid pointer
158*4882a593Smuzhiyun *
159*4882a593Smuzhiyun * RETURN: Converted integer
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
162*4882a593Smuzhiyun * an "implicit conversion" by the ACPI specification. Used by
163*4882a593Smuzhiyun * many ASL operators that require an integer operand, and support
164*4882a593Smuzhiyun * an automatic (implicit) conversion from a string operand
165*4882a593Smuzhiyun * to the final integer operand. The major restriction is that
166*4882a593Smuzhiyun * only hex strings are supported.
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * -----------------------------------------------------------------------------
169*4882a593Smuzhiyun *
170*4882a593Smuzhiyun * Base is always 16, either with or without the 0x prefix. Decimal and
171*4882a593Smuzhiyun * Octal strings are not supported, as per the ACPI specification.
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Examples (both are hex values):
174*4882a593Smuzhiyun * Add ("BA98", Arg0, Local0)
175*4882a593Smuzhiyun * Subtract ("0x12345678", Arg1, Local1)
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * Conversion rules as extracted from the ACPI specification:
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * The converted integer is initialized to the value zero.
180*4882a593Smuzhiyun * The ASCII string is always interpreted as a hexadecimal constant.
181*4882a593Smuzhiyun *
182*4882a593Smuzhiyun * 1) According to the ACPI specification, a "0x" prefix is not allowed.
183*4882a593Smuzhiyun * However, ACPICA allows this as an ACPI extension on general
184*4882a593Smuzhiyun * principle. (NO ERROR)
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * 2) The conversion terminates when the size of an integer is reached
187*4882a593Smuzhiyun * (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * 3) The first non-hex character terminates the conversion and returns
190*4882a593Smuzhiyun * the current accumulated value of the converted integer (NO ERROR).
191*4882a593Smuzhiyun *
192*4882a593Smuzhiyun * 4) Conversion of a null (zero-length) string to an integer is
193*4882a593Smuzhiyun * technically not allowed. However, ACPICA allows this as an ACPI
194*4882a593Smuzhiyun * extension. The conversion returns the value 0. (NO ERROR)
195*4882a593Smuzhiyun *
196*4882a593Smuzhiyun * NOTE: There are no error conditions returned by this function. At
197*4882a593Smuzhiyun * the minimum, a value of zero is returned.
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * Current users of this function:
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * interpreter - All runtime implicit conversions, as per ACPI specification
202*4882a593Smuzhiyun * iASL - Data Table Compiler parser (constants and math expressions)
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun ******************************************************************************/
205*4882a593Smuzhiyun
acpi_ut_implicit_strtoul64(char * string)206*4882a593Smuzhiyun u64 acpi_ut_implicit_strtoul64(char *string)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun u64 converted_integer = 0;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (!acpi_ut_remove_whitespace(&string)) {
213*4882a593Smuzhiyun return_VALUE(0);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * Per the ACPI specification, only hexadecimal is supported for
218*4882a593Smuzhiyun * implicit conversions, and the "0x" prefix is "not allowed".
219*4882a593Smuzhiyun * However, allow a "0x" prefix as an ACPI extension.
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun acpi_ut_remove_hex_prefix(&string);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun if (!acpi_ut_remove_leading_zeros(&string)) {
224*4882a593Smuzhiyun return_VALUE(0);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * Ignore overflow as per the ACPI specification. This is implemented by
229*4882a593Smuzhiyun * ignoring the return status from the conversion function called below.
230*4882a593Smuzhiyun * On overflow, the input string is simply truncated.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun acpi_ut_convert_hex_string(string, &converted_integer);
233*4882a593Smuzhiyun return_VALUE(converted_integer);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /*******************************************************************************
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * FUNCTION: acpi_ut_explicit_strtoul64
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * PARAMETERS: string - Null terminated input string,
241*4882a593Smuzhiyun * must be a valid pointer
242*4882a593Smuzhiyun *
243*4882a593Smuzhiyun * RETURN: Converted integer
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
246*4882a593Smuzhiyun * an "explicit conversion" by the ACPI specification. The
247*4882a593Smuzhiyun * main restriction is that only hex and decimal are supported.
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * -----------------------------------------------------------------------------
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
252*4882a593Smuzhiyun * are not supported, as per the ACPI specification.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Examples:
255*4882a593Smuzhiyun * to_integer ("1000") Decimal
256*4882a593Smuzhiyun * to_integer ("0xABCD") Hex
257*4882a593Smuzhiyun *
258*4882a593Smuzhiyun * Conversion rules as extracted from the ACPI specification:
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * 1) The input string is either a decimal or hexadecimal numeric string.
261*4882a593Smuzhiyun * A hex value must be prefixed by "0x" or it is interpreted as decimal.
262*4882a593Smuzhiyun *
263*4882a593Smuzhiyun * 2) The value must not exceed the maximum of an integer value
264*4882a593Smuzhiyun * (32 or 64 bits). The ACPI specification states the behavior is
265*4882a593Smuzhiyun * "unpredictable", so ACPICA matches the behavior of the implicit
266*4882a593Smuzhiyun * conversion case. There are no numeric overflow conditions. (NO ERROR)
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * 3) Behavior on the first non-hex character is not defined by the ACPI
269*4882a593Smuzhiyun * specification (for the to_integer operator), so ACPICA matches the
270*4882a593Smuzhiyun * behavior of the implicit conversion case. It terminates the
271*4882a593Smuzhiyun * conversion and returns the current accumulated value of the converted
272*4882a593Smuzhiyun * integer. (NO ERROR)
273*4882a593Smuzhiyun *
274*4882a593Smuzhiyun * 4) Conversion of a null (zero-length) string to an integer is
275*4882a593Smuzhiyun * technically not allowed. However, ACPICA allows this as an ACPI
276*4882a593Smuzhiyun * extension. The conversion returns the value 0. (NO ERROR)
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * NOTE: There are no error conditions returned by this function. At the
279*4882a593Smuzhiyun * minimum, a value of zero is returned.
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun * Current users of this function:
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * interpreter - Runtime ASL to_integer operator, as per the ACPI specification
284*4882a593Smuzhiyun *
285*4882a593Smuzhiyun ******************************************************************************/
286*4882a593Smuzhiyun
acpi_ut_explicit_strtoul64(char * string)287*4882a593Smuzhiyun u64 acpi_ut_explicit_strtoul64(char *string)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun u64 converted_integer = 0;
290*4882a593Smuzhiyun u32 base = 10; /* Default is decimal */
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (!acpi_ut_remove_whitespace(&string)) {
295*4882a593Smuzhiyun return_VALUE(0);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /*
299*4882a593Smuzhiyun * Only Hex and Decimal are supported, as per the ACPI specification.
300*4882a593Smuzhiyun * A "0x" prefix indicates hex; otherwise decimal is assumed.
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun if (acpi_ut_detect_hex_prefix(&string)) {
303*4882a593Smuzhiyun base = 16;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (!acpi_ut_remove_leading_zeros(&string)) {
307*4882a593Smuzhiyun return_VALUE(0);
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /*
311*4882a593Smuzhiyun * Ignore overflow as per the ACPI specification. This is implemented by
312*4882a593Smuzhiyun * ignoring the return status from the conversion functions called below.
313*4882a593Smuzhiyun * On overflow, the input string is simply truncated.
314*4882a593Smuzhiyun */
315*4882a593Smuzhiyun switch (base) {
316*4882a593Smuzhiyun case 10:
317*4882a593Smuzhiyun default:
318*4882a593Smuzhiyun acpi_ut_convert_decimal_string(string, &converted_integer);
319*4882a593Smuzhiyun break;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun case 16:
322*4882a593Smuzhiyun acpi_ut_convert_hex_string(string, &converted_integer);
323*4882a593Smuzhiyun break;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun return_VALUE(converted_integer);
327*4882a593Smuzhiyun }
328