1 /*******************************************************************************
3 * Module Name: utstrtoul64 - string to 64-bit integer support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
47 /*******************************************************************************
49 * The functions in this module satisfy the need for 64-bit string-to-integer
50 * conversions on both 32-bit and 64-bit platforms.
52 ******************************************************************************/
54 #define _COMPONENT ACPI_UTILITIES
55 ACPI_MODULE_NAME("utstrtoul64")
57 /* Local prototypes */
58 static u64
acpi_ut_strtoul_base10(char *string
, u32 flags
);
60 static u64
acpi_ut_strtoul_base16(char *string
, u32 flags
);
62 /*******************************************************************************
64 * String conversion rules as written in the ACPI specification. The error
65 * conditions and behavior are different depending on the type of conversion.
68 * Implicit data type conversion: string-to-integer
69 * --------------------------------------------------
71 * Base is always 16. This is the ACPI_STRTOUL_BASE16 case.
74 * Add ("BA98", Arg0, Local0)
76 * The integer is initialized to the value zero.
77 * The ASCII string is interpreted as a hexadecimal constant.
79 * 1) A "0x" prefix is not allowed. However, ACPICA allows this for
80 * compatibility with previous ACPICA. (NO ERROR)
82 * 2) Terminates when the size of an integer is reached (32 or 64 bits).
85 * 3) The first non-hex character terminates the conversion without error.
88 * 4) Conversion of a null (zero-length) string to an integer is not
89 * allowed. However, ACPICA allows this for compatibility with previous
90 * ACPICA. This conversion returns the value 0. (NO ERROR)
93 * Explicit data type conversion: to_integer() with string operand
94 * ---------------------------------------------------------------
96 * Base is either 10 (default) or 16 (with 0x prefix)
100 * to_integer ("0xABCD")
102 * 1) Can be (must be) either a decimal or hexadecimal numeric string.
103 * A hex value must be prefixed by "0x" or it is interpreted as a decimal.
105 * 2) The value must not exceed the maximum of an integer value. ACPI spec
106 * states the behavior is "unpredictable", so ACPICA matches the behavior
107 * of the implicit conversion case.(NO ERROR)
109 * 3) Behavior on the first non-hex character is not specified by the ACPI
110 * spec, so ACPICA matches the behavior of the implicit conversion case
111 * and terminates. (NO ERROR)
113 * 4) A null (zero-length) string is illegal.
114 * However, ACPICA allows this for compatibility with previous ACPICA.
115 * This conversion returns the value 0. (NO ERROR)
117 ******************************************************************************/
119 /*******************************************************************************
121 * FUNCTION: acpi_ut_strtoul64
123 * PARAMETERS: string - Null terminated input string
124 * flags - Conversion info, see below
125 * return_value - Where the converted integer is
128 * RETURN: Status and Converted value
130 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
131 * 32-bit or 64-bit conversion, depending on the input integer
132 * size in Flags (often the current mode of the interpreter).
135 * ACPI_STRTOUL_32BIT - Max integer value is 32 bits
136 * ACPI_STRTOUL_64BIT - Max integer value is 64 bits
137 * ACPI_STRTOUL_BASE16 - Input string is hexadecimal. Default
138 * is 10/16 based on string prefix (0x).
141 * Negative numbers are not supported, as they are not supported by ACPI.
143 * Supports only base 16 or base 10 strings/values. Does not
144 * support Octal strings, as these are not supported by ACPI.
146 * Current users of this support:
148 * interpreter - Implicit and explicit conversions, GPE method names
149 * debugger - Command line input string conversion
150 * iASL - Main parser, conversion of constants to integers
151 * iASL - Data Table Compiler parser (constant math expressions)
152 * iASL - Preprocessor (constant math expressions)
153 * acpi_dump - Input table addresses
154 * acpi_exec - Testing of the acpi_ut_strtoul64 function
156 * Note concerning callers:
157 * acpi_gbl_integer_byte_width can be used to set the 32/64 limit. If used,
158 * this global should be set to the proper width. For the core ACPICA code,
159 * this width depends on the DSDT version. For iASL, the default byte
160 * width is always 8 for the parser, but error checking is performed later
161 * to flag cases where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
163 ******************************************************************************/
165 acpi_status
acpi_ut_strtoul64(char *string
, u32 flags
, u64
*return_value
)
167 acpi_status status
= AE_OK
;
170 ACPI_FUNCTION_TRACE_STR(ut_strtoul64
, string
);
172 /* Parameter validation */
174 if (!string
|| !return_value
) {
175 return_ACPI_STATUS(AE_BAD_PARAMETER
);
180 /* Check for zero-length string, returns 0 */
183 return_ACPI_STATUS(AE_OK
);
186 /* Skip over any white space at start of string */
188 while (isspace((int)*string
)) {
192 /* End of string? return 0 */
195 return_ACPI_STATUS(AE_OK
);
199 * 1) The "0x" prefix indicates base 16. Per the ACPI specification,
200 * the "0x" prefix is only allowed for implicit (non-strict) conversions.
201 * However, we always allow it for compatibility with older ACPICA.
203 if ((*string
== ACPI_ASCII_ZERO
) &&
204 (tolower((int)*(string
+ 1)) == 'x')) {
205 string
+= 2; /* Go past the 0x */
207 return_ACPI_STATUS(AE_OK
); /* Return value 0 */
213 /* 2) Force to base 16 (implicit conversion case) */
215 else if (flags
& ACPI_STRTOUL_BASE16
) {
219 /* 3) Default fallback is to Base 10 */
225 /* Skip all leading zeros */
227 while (*string
== ACPI_ASCII_ZERO
) {
230 return_ACPI_STATUS(AE_OK
); /* Return value 0 */
234 /* Perform the base 16 or 10 conversion */
237 *return_value
= acpi_ut_strtoul_base16(string
, flags
);
239 *return_value
= acpi_ut_strtoul_base10(string
, flags
);
242 return_ACPI_STATUS(status
);
245 /*******************************************************************************
247 * FUNCTION: acpi_ut_strtoul_base10
249 * PARAMETERS: string - Null terminated input string
250 * flags - Conversion info
252 * RETURN: 64-bit converted integer
254 * DESCRIPTION: Performs a base 10 conversion of the input string to an
255 * integer value, either 32 or 64 bits.
256 * Note: String must be valid and non-null.
258 ******************************************************************************/
260 static u64
acpi_ut_strtoul_base10(char *string
, u32 flags
)
264 u64 return_value
= 0;
266 /* Main loop: convert each ASCII byte in the input string */
269 ascii_digit
= *string
;
270 if (!isdigit(ascii_digit
)) {
272 /* Not ASCII 0-9, terminate */
277 /* Convert and insert (add) the decimal digit */
280 (return_value
* 10) + (ascii_digit
- ACPI_ASCII_ZERO
);
282 /* Check for overflow (32 or 64 bit) - return current converted value */
284 if (((flags
& ACPI_STRTOUL_32BIT
) && (next_value
> ACPI_UINT32_MAX
)) || (next_value
< return_value
)) { /* 64-bit overflow case */
288 return_value
= next_value
;
293 return (return_value
);
296 /*******************************************************************************
298 * FUNCTION: acpi_ut_strtoul_base16
300 * PARAMETERS: string - Null terminated input string
301 * flags - conversion info
303 * RETURN: 64-bit converted integer
305 * DESCRIPTION: Performs a base 16 conversion of the input string to an
306 * integer value, either 32 or 64 bits.
307 * Note: String must be valid and non-null.
309 ******************************************************************************/
311 static u64
acpi_ut_strtoul_base16(char *string
, u32 flags
)
314 u32 valid_digits
= 1;
315 u64 return_value
= 0;
317 /* Main loop: convert each ASCII byte in the input string */
321 /* Check for overflow (32 or 64 bit) - return current converted value */
323 if ((valid_digits
> 16) ||
324 ((valid_digits
> 8) && (flags
& ACPI_STRTOUL_32BIT
))) {
328 ascii_digit
= *string
;
329 if (!isxdigit(ascii_digit
)) {
331 /* Not Hex ASCII A-F, a-f, or 0-9, terminate */
336 /* Convert and insert the hex digit */
339 (return_value
<< 4) |
340 acpi_ut_ascii_char_to_hex(ascii_digit
);
347 return (return_value
);