]>
Commit | Line | Data |
---|---|---|
5ebd2eaa BM |
1 | /******************************************************************************* |
2 | * | |
72a29355 BM |
3 | * Module Name: utstrtoul64 - String-to-integer conversion support for both |
4 | * 64-bit and 32-bit integers | |
5ebd2eaa BM |
5 | * |
6 | ******************************************************************************/ | |
7 | ||
8 | /* | |
da6f8320 | 9 | * Copyright (C) 2000 - 2018, Intel Corp. |
5ebd2eaa BM |
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 | ||
5ebd2eaa BM |
48 | #define _COMPONENT ACPI_UTILITIES |
49 | ACPI_MODULE_NAME("utstrtoul64") | |
50 | ||
5ebd2eaa BM |
51 | /******************************************************************************* |
52 | * | |
72a29355 | 53 | * This module contains the top-level string to 64/32-bit unsigned integer |
fe97d287 | 54 | * conversion functions: |
5ebd2eaa | 55 | * |
72a29355 BM |
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. | |
fe97d287 BM |
60 | * 2) Runtime "Explicit conversion" as defined in the ACPI specification. |
61 | * 3) Runtime "Implicit conversion" as defined in the ACPI specification. | |
5ebd2eaa | 62 | * |
fe97d287 | 63 | * Current users of this module: |
5ebd2eaa | 64 | * |
72a29355 BM |
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) | |
fe97d287 | 68 | * interpreter - Implicit and explicit conversions, GPE method names |
c2e56e54 | 69 | * interpreter - Repair code for return values from predefined names |
fe97d287 | 70 | * debugger - Command line input string conversion |
c2e56e54 BM |
71 | * acpi_dump - ACPI table physical addresses |
72 | * acpi_exec - Support for namespace overrides | |
5ebd2eaa | 73 | * |
fe97d287 | 74 | * Notes concerning users of these interfaces: |
5ebd2eaa | 75 | * |
c2e56e54 BM |
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. | |
5ebd2eaa | 83 | * |
c2e56e54 BM |
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. | |
5ebd2eaa BM |
88 | * |
89 | ******************************************************************************/ | |
5ebd2eaa BM |
90 | /******************************************************************************* |
91 | * | |
92 | * FUNCTION: acpi_ut_strtoul64 | |
93 | * | |
72a29355 BM |
94 | * PARAMETERS: string - Null terminated input string, |
95 | * must be a valid pointer | |
5ebd2eaa | 96 | * return_value - Where the converted integer is |
fe97d287 | 97 | * returned. Must be a valid pointer |
5ebd2eaa | 98 | * |
c2e56e54 BM |
99 | * RETURN: Status and converted integer. Returns an exception on a |
100 | * 64-bit numeric overflow | |
5ebd2eaa | 101 | * |
c2e56e54 BM |
102 | * DESCRIPTION: Convert a string into an unsigned integer. Always performs a |
103 | * full 64-bit conversion, regardless of the current global | |
fe97d287 | 104 | * integer width. Supports Decimal, Hex, and Octal strings. |
5ebd2eaa | 105 | * |
fe97d287 | 106 | * Current users of this function: |
5ebd2eaa | 107 | * |
72a29355 BM |
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) | |
c2e56e54 BM |
111 | * interpreter - Repair code for return values from predefined names |
112 | * acpi_dump - ACPI table physical addresses | |
113 | * acpi_exec - Support for namespace overrides | |
5ebd2eaa BM |
114 | * |
115 | ******************************************************************************/ | |
fe97d287 | 116 | acpi_status acpi_ut_strtoul64(char *string, u64 *return_value) |
5ebd2eaa BM |
117 | { |
118 | acpi_status status = AE_OK; | |
c2e56e54 | 119 | u8 original_bit_width; |
fe97d287 | 120 | u32 base = 10; /* Default is decimal */ |
5ebd2eaa BM |
121 | |
122 | ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string); | |
123 | ||
5ebd2eaa BM |
124 | *return_value = 0; |
125 | ||
72a29355 | 126 | /* A NULL return string returns a value of zero */ |
5ebd2eaa BM |
127 | |
128 | if (*string == 0) { | |
129 | return_ACPI_STATUS(AE_OK); | |
130 | } | |
131 | ||
c2e56e54 BM |
132 | if (!acpi_ut_remove_whitespace(&string)) { |
133 | return_ACPI_STATUS(AE_OK); | |
134 | } | |
135 | ||
5ebd2eaa | 136 | /* |
72a29355 | 137 | * 1) Check for a hex constant. A "0x" prefix indicates base 16. |
5ebd2eaa | 138 | */ |
fe97d287 | 139 | if (acpi_ut_detect_hex_prefix(&string)) { |
5ebd2eaa BM |
140 | base = 16; |
141 | } | |
142 | ||
fe97d287 BM |
143 | /* |
144 | * 2) Check for an octal constant, defined to be a leading zero | |
72a29355 | 145 | * followed by sequence of octal digits (0-7) |
fe97d287 BM |
146 | */ |
147 | else if (acpi_ut_detect_octal_prefix(&string)) { | |
148 | base = 8; | |
5ebd2eaa BM |
149 | } |
150 | ||
fe97d287 BM |
151 | if (!acpi_ut_remove_leading_zeros(&string)) { |
152 | return_ACPI_STATUS(AE_OK); /* Return value 0 */ | |
5ebd2eaa BM |
153 | } |
154 | ||
fe97d287 | 155 | /* |
c2e56e54 BM |
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). | |
fe97d287 BM |
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: | |
fe97d287 | 177 | default: |
72a29355 | 178 | status = acpi_ut_convert_hex_string(string, return_value); |
fe97d287 | 179 | break; |
5ebd2eaa BM |
180 | } |
181 | ||
c2e56e54 BM |
182 | /* Only possible exception from above is a 64-bit overflow */ |
183 | ||
184 | acpi_gbl_integer_bit_width = original_bit_width; | |
5ebd2eaa BM |
185 | return_ACPI_STATUS(status); |
186 | } | |
187 | ||
188 | /******************************************************************************* | |
189 | * | |
fe97d287 BM |
190 | * FUNCTION: acpi_ut_implicit_strtoul64 |
191 | * | |
72a29355 BM |
192 | * PARAMETERS: string - Null terminated input string, |
193 | * must be a valid pointer | |
5ebd2eaa | 194 | * |
fe97d287 | 195 | * RETURN: Converted integer |
5ebd2eaa | 196 | * |
fe97d287 BM |
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 | |
72a29355 BM |
201 | * to the final integer operand. The major restriction is that |
202 | * only hex strings are supported. | |
5ebd2eaa | 203 | * |
fe97d287 BM |
204 | * ----------------------------------------------------------------------------- |
205 | * | |
72a29355 BM |
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. | |
fe97d287 BM |
208 | * |
209 | * Examples (both are hex values): | |
210 | * Add ("BA98", Arg0, Local0) | |
211 | * Subtract ("0x12345678", Arg1, Local1) | |
212 | * | |
72a29355 | 213 | * Conversion rules as extracted from the ACPI specification: |
fe97d287 BM |
214 | * |
215 | * The converted integer is initialized to the value zero. | |
72a29355 | 216 | * The ASCII string is always interpreted as a hexadecimal constant. |
fe97d287 | 217 | * |
72a29355 BM |
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) | |
fe97d287 | 221 | * |
72a29355 BM |
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) | |
fe97d287 BM |
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 | |
72a29355 BM |
229 | * technically not allowed. However, ACPICA allows this as an ACPI |
230 | * extension. The conversion returns the value 0. (NO ERROR) | |
fe97d287 | 231 | * |
72a29355 | 232 | * NOTE: There are no error conditions returned by this function. At |
fe97d287 BM |
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 | |
72a29355 | 238 | * iASL - Data Table Compiler parser (constants and math expressions) |
5ebd2eaa BM |
239 | * |
240 | ******************************************************************************/ | |
241 | ||
fe97d287 | 242 | u64 acpi_ut_implicit_strtoul64(char *string) |
5ebd2eaa | 243 | { |
fe97d287 | 244 | u64 converted_integer = 0; |
5ebd2eaa | 245 | |
fe97d287 | 246 | ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string); |
5ebd2eaa | 247 | |
c2e56e54 BM |
248 | if (!acpi_ut_remove_whitespace(&string)) { |
249 | return_VALUE(0); | |
250 | } | |
251 | ||
fe97d287 BM |
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); | |
5ebd2eaa | 258 | |
fe97d287 BM |
259 | if (!acpi_ut_remove_leading_zeros(&string)) { |
260 | return_VALUE(0); | |
5ebd2eaa BM |
261 | } |
262 | ||
fe97d287 BM |
263 | /* |
264 | * Ignore overflow as per the ACPI specification. This is implemented by | |
c2e56e54 BM |
265 | * ignoring the return status from the conversion function called below. |
266 | * On overflow, the input string is simply truncated. | |
fe97d287 BM |
267 | */ |
268 | acpi_ut_convert_hex_string(string, &converted_integer); | |
269 | return_VALUE(converted_integer); | |
5ebd2eaa BM |
270 | } |
271 | ||
272 | /******************************************************************************* | |
273 | * | |
fe97d287 BM |
274 | * FUNCTION: acpi_ut_explicit_strtoul64 |
275 | * | |
72a29355 BM |
276 | * PARAMETERS: string - Null terminated input string, |
277 | * must be a valid pointer | |
fe97d287 BM |
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 | * | |
72a29355 BM |
287 | * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings |
288 | * are not supported, as per the ACPI specification. | |
fe97d287 BM |
289 | * |
290 | * Examples: | |
291 | * to_integer ("1000") Decimal | |
292 | * to_integer ("0xABCD") Hex | |
293 | * | |
72a29355 | 294 | * Conversion rules as extracted from the ACPI specification: |
fe97d287 | 295 | * |
72a29355 | 296 | * 1) The input string is either a decimal or hexadecimal numeric string. |
fe97d287 BM |
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 | * | |
72a29355 | 304 | * 3) Behavior on the first non-hex character is not defined by the ACPI |
fe97d287 BM |
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) | |
5ebd2eaa | 309 | * |
fe97d287 | 310 | * 4) Conversion of a null (zero-length) string to an integer is |
72a29355 BM |
311 | * technically not allowed. However, ACPICA allows this as an ACPI |
312 | * extension. The conversion returns the value 0. (NO ERROR) | |
5ebd2eaa | 313 | * |
72a29355 BM |
314 | * NOTE: There are no error conditions returned by this function. At the |
315 | * minimum, a value of zero is returned. | |
5ebd2eaa | 316 | * |
fe97d287 BM |
317 | * Current users of this function: |
318 | * | |
319 | * interpreter - Runtime ASL to_integer operator, as per the ACPI specification | |
5ebd2eaa BM |
320 | * |
321 | ******************************************************************************/ | |
322 | ||
fe97d287 | 323 | u64 acpi_ut_explicit_strtoul64(char *string) |
5ebd2eaa | 324 | { |
fe97d287 BM |
325 | u64 converted_integer = 0; |
326 | u32 base = 10; /* Default is decimal */ | |
5ebd2eaa | 327 | |
fe97d287 | 328 | ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string); |
5ebd2eaa | 329 | |
c2e56e54 BM |
330 | if (!acpi_ut_remove_whitespace(&string)) { |
331 | return_VALUE(0); | |
332 | } | |
333 | ||
fe97d287 BM |
334 | /* |
335 | * Only Hex and Decimal are supported, as per the ACPI specification. | |
72a29355 | 336 | * A "0x" prefix indicates hex; otherwise decimal is assumed. |
fe97d287 BM |
337 | */ |
338 | if (acpi_ut_detect_hex_prefix(&string)) { | |
339 | base = 16; | |
340 | } | |
5ebd2eaa | 341 | |
fe97d287 BM |
342 | if (!acpi_ut_remove_leading_zeros(&string)) { |
343 | return_VALUE(0); | |
344 | } | |
5ebd2eaa | 345 | |
fe97d287 BM |
346 | /* |
347 | * Ignore overflow as per the ACPI specification. This is implemented by | |
c2e56e54 BM |
348 | * ignoring the return status from the conversion functions called below. |
349 | * On overflow, the input string is simply truncated. | |
fe97d287 BM |
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; | |
5ebd2eaa BM |
360 | } |
361 | ||
fe97d287 | 362 | return_VALUE(converted_integer); |
5ebd2eaa | 363 | } |