1 /*******************************************************************************
2  *
3  * Module Name: utstrtoul64 - String-to-integer conversion support for both
4  *                            64-bit and 32-bit integers
5  *
6  ******************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2018, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 
48 #define _COMPONENT          ACPI_UTILITIES
49 ACPI_MODULE_NAME("utstrtoul64")
50 
51 /*******************************************************************************
52  *
53  * This module contains the top-level string to 64/32-bit unsigned integer
54  * conversion functions:
55  *
56  *  1) A standard strtoul() function that supports 64-bit integers, base
57  *     8/10/16, with integer overflow support. This is used mainly by the
58  *     iASL compiler, which implements tighter constraints on integer
59  *     constants than the runtime (interpreter) integer-to-string conversions.
60  *  2) Runtime "Explicit conversion" as defined in the ACPI specification.
61  *  3) Runtime "Implicit conversion" as defined in the ACPI specification.
62  *
63  * Current users of this module:
64  *
65  *  iASL        - Preprocessor (constants and math expressions)
66  *  iASL        - Main parser, conversion of constants to integers
67  *  iASL        - Data Table Compiler parser (constants and math expressions)
68  *  interpreter - Implicit and explicit conversions, GPE method names
69  *  interpreter - Repair code for return values from predefined names
70  *  debugger    - Command line input string conversion
71  *  acpi_dump   - ACPI table physical addresses
72  *  acpi_exec   - Support for namespace overrides
73  *
74  * Notes concerning users of these interfaces:
75  *
76  * acpi_gbl_integer_byte_width is used to set the 32/64 bit limit for explicit
77  * and implicit conversions. This global must be set to the proper width.
78  * For the core ACPICA code, the width depends on the DSDT version. For the
79  * acpi_ut_strtoul64 interface, all conversions are 64 bits. This interface is
80  * used primarily for iASL, where the default width is 64 bits for all parsers,
81  * but error checking is performed later to flag cases where a 64-bit constant
82  * is wrongly defined in a 32-bit DSDT/SSDT.
83  *
84  * In ACPI, the only place where octal numbers are supported is within
85  * the ASL language itself. This is implemented via the main acpi_ut_strtoul64
86  * interface. According the ACPI specification, there is no ACPI runtime
87  * support (explicit/implicit) for octal string conversions.
88  *
89  ******************************************************************************/
90 /*******************************************************************************
91  *
92  * FUNCTION:    acpi_ut_strtoul64
93  *
94  * PARAMETERS:  string                  - Null terminated input string,
95  *                                        must be a valid pointer
96  *              return_value            - Where the converted integer is
97  *                                        returned. Must be a valid pointer
98  *
99  * RETURN:      Status and converted integer. Returns an exception on a
100  *              64-bit numeric overflow
101  *
102  * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
103  *              full 64-bit conversion, regardless of the current global
104  *              integer width. Supports Decimal, Hex, and Octal strings.
105  *
106  * Current users of this function:
107  *
108  *  iASL        - Preprocessor (constants and math expressions)
109  *  iASL        - Main ASL parser, conversion of ASL constants to integers
110  *  iASL        - Data Table Compiler parser (constants and math expressions)
111  *  interpreter - Repair code for return values from predefined names
112  *  acpi_dump   - ACPI table physical addresses
113  *  acpi_exec   - Support for namespace overrides
114  *
115  ******************************************************************************/
116 acpi_status acpi_ut_strtoul64(char *string, u64 *return_value)
117 {
118 	acpi_status status = AE_OK;
119 	u8 original_bit_width;
120 	u32 base = 10;		/* Default is decimal */
121 
122 	ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string);
123 
124 	*return_value = 0;
125 
126 	/* A NULL return string returns a value of zero */
127 
128 	if (*string == 0) {
129 		return_ACPI_STATUS(AE_OK);
130 	}
131 
132 	if (!acpi_ut_remove_whitespace(&string)) {
133 		return_ACPI_STATUS(AE_OK);
134 	}
135 
136 	/*
137 	 * 1) Check for a hex constant. A "0x" prefix indicates base 16.
138 	 */
139 	if (acpi_ut_detect_hex_prefix(&string)) {
140 		base = 16;
141 	}
142 
143 	/*
144 	 * 2) Check for an octal constant, defined to be a leading zero
145 	 * followed by sequence of octal digits (0-7)
146 	 */
147 	else if (acpi_ut_detect_octal_prefix(&string)) {
148 		base = 8;
149 	}
150 
151 	if (!acpi_ut_remove_leading_zeros(&string)) {
152 		return_ACPI_STATUS(AE_OK);	/* Return value 0 */
153 	}
154 
155 	/*
156 	 * Force a full 64-bit conversion. The caller (usually iASL) must
157 	 * check for a 32-bit overflow later as necessary (If current mode
158 	 * is 32-bit, meaning a 32-bit DSDT).
159 	 */
160 	original_bit_width = acpi_gbl_integer_bit_width;
161 	acpi_gbl_integer_bit_width = 64;
162 
163 	/*
164 	 * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
165 	 * will return an exception (to allow iASL to flag the statement).
166 	 */
167 	switch (base) {
168 	case 8:
169 		status = acpi_ut_convert_octal_string(string, return_value);
170 		break;
171 
172 	case 10:
173 		status = acpi_ut_convert_decimal_string(string, return_value);
174 		break;
175 
176 	case 16:
177 	default:
178 		status = acpi_ut_convert_hex_string(string, return_value);
179 		break;
180 	}
181 
182 	/* Only possible exception from above is a 64-bit overflow */
183 
184 	acpi_gbl_integer_bit_width = original_bit_width;
185 	return_ACPI_STATUS(status);
186 }
187 
188 /*******************************************************************************
189  *
190  * FUNCTION:    acpi_ut_implicit_strtoul64
191  *
192  * PARAMETERS:  string                  - Null terminated input string,
193  *                                        must be a valid pointer
194  *
195  * RETURN:      Converted integer
196  *
197  * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
198  *              an "implicit conversion" by the ACPI specification. Used by
199  *              many ASL operators that require an integer operand, and support
200  *              an automatic (implicit) conversion from a string operand
201  *              to the final integer operand. The major restriction is that
202  *              only hex strings are supported.
203  *
204  * -----------------------------------------------------------------------------
205  *
206  * Base is always 16, either with or without the 0x prefix. Decimal and
207  * Octal strings are not supported, as per the ACPI specification.
208  *
209  * Examples (both are hex values):
210  *      Add ("BA98", Arg0, Local0)
211  *      Subtract ("0x12345678", Arg1, Local1)
212  *
213  * Conversion rules as extracted from the ACPI specification:
214  *
215  *  The converted integer is initialized to the value zero.
216  *  The ASCII string is always interpreted as a hexadecimal constant.
217  *
218  *  1)  According to the ACPI specification, a "0x" prefix is not allowed.
219  *      However, ACPICA allows this as an ACPI extension on general
220  *      principle. (NO ERROR)
221  *
222  *  2)  The conversion terminates when the size of an integer is reached
223  *      (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
224  *
225  *  3)  The first non-hex character terminates the conversion and returns
226  *      the current accumulated value of the converted integer (NO ERROR).
227  *
228  *  4)  Conversion of a null (zero-length) string to an integer is
229  *      technically not allowed. However, ACPICA allows this as an ACPI
230  *      extension. The conversion returns the value 0. (NO ERROR)
231  *
232  * NOTE: There are no error conditions returned by this function. At
233  * the minimum, a value of zero is returned.
234  *
235  * Current users of this function:
236  *
237  *  interpreter - All runtime implicit conversions, as per ACPI specification
238  *  iASL        - Data Table Compiler parser (constants and math expressions)
239  *
240  ******************************************************************************/
241 
242 u64 acpi_ut_implicit_strtoul64(char *string)
243 {
244 	u64 converted_integer = 0;
245 
246 	ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string);
247 
248 	if (!acpi_ut_remove_whitespace(&string)) {
249 		return_VALUE(0);
250 	}
251 
252 	/*
253 	 * Per the ACPI specification, only hexadecimal is supported for
254 	 * implicit conversions, and the "0x" prefix is "not allowed".
255 	 * However, allow a "0x" prefix as an ACPI extension.
256 	 */
257 	acpi_ut_detect_hex_prefix(&string);
258 
259 	if (!acpi_ut_remove_leading_zeros(&string)) {
260 		return_VALUE(0);
261 	}
262 
263 	/*
264 	 * Ignore overflow as per the ACPI specification. This is implemented by
265 	 * ignoring the return status from the conversion function called below.
266 	 * On overflow, the input string is simply truncated.
267 	 */
268 	acpi_ut_convert_hex_string(string, &converted_integer);
269 	return_VALUE(converted_integer);
270 }
271 
272 /*******************************************************************************
273  *
274  * FUNCTION:    acpi_ut_explicit_strtoul64
275  *
276  * PARAMETERS:  string                  - Null terminated input string,
277  *                                        must be a valid pointer
278  *
279  * RETURN:      Converted integer
280  *
281  * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
282  *              an "explicit conversion" by the ACPI specification. The
283  *              main restriction is that only hex and decimal are supported.
284  *
285  * -----------------------------------------------------------------------------
286  *
287  * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
288  * are not supported, as per the ACPI specification.
289  *
290  * Examples:
291  *      to_integer ("1000")     Decimal
292  *      to_integer ("0xABCD")   Hex
293  *
294  * Conversion rules as extracted from the ACPI specification:
295  *
296  *  1)  The input string is either a decimal or hexadecimal numeric string.
297  *      A hex value must be prefixed by "0x" or it is interpreted as decimal.
298  *
299  *  2)  The value must not exceed the maximum of an integer value
300  *      (32 or 64 bits). The ACPI specification states the behavior is
301  *      "unpredictable", so ACPICA matches the behavior of the implicit
302  *      conversion case. There are no numeric overflow conditions. (NO ERROR)
303  *
304  *  3)  Behavior on the first non-hex character is not defined by the ACPI
305  *      specification (for the to_integer operator), so ACPICA matches the
306  *      behavior of the implicit conversion case. It terminates the
307  *      conversion and returns the current accumulated value of the converted
308  *      integer. (NO ERROR)
309  *
310  *  4)  Conversion of a null (zero-length) string to an integer is
311  *      technically not allowed. However, ACPICA allows this as an ACPI
312  *      extension. The conversion returns the value 0. (NO ERROR)
313  *
314  * NOTE: There are no error conditions returned by this function. At the
315  * minimum, a value of zero is returned.
316  *
317  * Current users of this function:
318  *
319  *  interpreter - Runtime ASL to_integer operator, as per the ACPI specification
320  *
321  ******************************************************************************/
322 
323 u64 acpi_ut_explicit_strtoul64(char *string)
324 {
325 	u64 converted_integer = 0;
326 	u32 base = 10;		/* Default is decimal */
327 
328 	ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string);
329 
330 	if (!acpi_ut_remove_whitespace(&string)) {
331 		return_VALUE(0);
332 	}
333 
334 	/*
335 	 * Only Hex and Decimal are supported, as per the ACPI specification.
336 	 * A "0x" prefix indicates hex; otherwise decimal is assumed.
337 	 */
338 	if (acpi_ut_detect_hex_prefix(&string)) {
339 		base = 16;
340 	}
341 
342 	if (!acpi_ut_remove_leading_zeros(&string)) {
343 		return_VALUE(0);
344 	}
345 
346 	/*
347 	 * Ignore overflow as per the ACPI specification. This is implemented by
348 	 * ignoring the return status from the conversion functions called below.
349 	 * On overflow, the input string is simply truncated.
350 	 */
351 	switch (base) {
352 	case 10:
353 	default:
354 		acpi_ut_convert_decimal_string(string, &converted_integer);
355 		break;
356 
357 	case 16:
358 		acpi_ut_convert_hex_string(string, &converted_integer);
359 		break;
360 	}
361 
362 	return_VALUE(converted_integer);
363 }
364