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