Sync ACPICA with Intel's version 20160831.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / utilities / utstrtoul64.c
blob96cf112a4796af0ab4c11a816a196b79c4ab44ef
1 /*******************************************************************************
3 * Module Name: utstrtoul64 - string to 64-bit integer support
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.h"
45 #include "accommon.h"
48 /*******************************************************************************
50 * The functions in this module satisfy the need for 64-bit string-to-integer
51 * conversions on both 32-bit and 64-bit platforms.
53 ******************************************************************************/
55 #define _COMPONENT ACPI_UTILITIES
56 ACPI_MODULE_NAME ("utstrtoul64")
58 /* Local prototypes */
60 static UINT64
61 AcpiUtStrtoulBase10 (
62 char *String,
63 UINT32 Flags);
65 static UINT64
66 AcpiUtStrtoulBase16 (
67 char *String,
68 UINT32 Flags);
71 /*******************************************************************************
73 * String conversion rules as written in the ACPI specification. The error
74 * conditions and behavior are different depending on the type of conversion.
77 * Implicit data type conversion: string-to-integer
78 * --------------------------------------------------
80 * Base is always 16. This is the ACPI_STRTOUL_BASE16 case.
82 * Example:
83 * Add ("BA98", Arg0, Local0)
85 * The integer is initialized to the value zero.
86 * The ASCII string is interpreted as a hexadecimal constant.
88 * 1) A “0x” prefix is not allowed. However, ACPICA allows this for
89 * compatibility with previous ACPICA. (NO ERROR)
91 * 2) Terminates when the size of an integer is reached (32 or 64 bits).
92 * (NO ERROR)
94 * 3) The first non-hex character terminates the conversion without error.
95 * (NO ERROR)
97 * 4) Conversion of a null (zero-length) string to an integer is not
98 * allowed. However, ACPICA allows this for compatibility with previous
99 * ACPICA. This conversion returns the value 0. (NO ERROR)
102 * Explicit data type conversion: ToInteger() with string operand
103 * ---------------------------------------------------------------
105 * Base is either 10 (default) or 16 (with 0x prefix)
107 * Examples:
108 * ToInteger ("1000")
109 * ToInteger ("0xABCD")
111 * 1) Can be (must be) either a decimal or hexadecimal numeric string.
112 * A hex value must be prefixed by "0x" or it is interpreted as a decimal.
114 * 2) The value must not exceed the maximum of an integer value. ACPI spec
115 * states the behavior is "unpredictable", so ACPICA matches the behavior
116 * of the implicit conversion case.(NO ERROR)
118 * 3) Behavior on the first non-hex character is not specified by the ACPI
119 * spec, so ACPICA matches the behavior of the implicit conversion case
120 * and terminates. (NO ERROR)
122 * 4) A null (zero-length) string is illegal.
123 * However, ACPICA allows this for compatibility with previous ACPICA.
124 * This conversion returns the value 0. (NO ERROR)
126 ******************************************************************************/
129 /*******************************************************************************
131 * FUNCTION: AcpiUtStrtoul64
133 * PARAMETERS: String - Null terminated input string
134 * Flags - Conversion info, see below
135 * ReturnValue - Where the converted integer is
136 * returned
138 * RETURN: Status and Converted value
140 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
141 * 32-bit or 64-bit conversion, depending on the input integer
142 * size in Flags (often the current mode of the interpreter).
144 * Values for Flags:
145 * ACPI_STRTOUL_32BIT - Max integer value is 32 bits
146 * ACPI_STRTOUL_64BIT - Max integer value is 64 bits
147 * ACPI_STRTOUL_BASE16 - Input string is hexadecimal. Default
148 * is 10/16 based on string prefix (0x).
150 * NOTES:
151 * Negative numbers are not supported, as they are not supported by ACPI.
153 * Supports only base 16 or base 10 strings/values. Does not
154 * support Octal strings, as these are not supported by ACPI.
156 * Current users of this support:
158 * Interpreter - Implicit and explicit conversions, GPE method names
159 * Debugger - Command line input string conversion
160 * iASL - Main parser, conversion of constants to integers
161 * iASL - Data Table Compiler parser (constant math expressions)
162 * iASL - Preprocessor (constant math expressions)
163 * AcpiDump - Input table addresses
164 * AcpiExec - Testing of the AcpiUtStrtoul64 function
166 * Note concerning callers:
167 * AcpiGbl_IntegerByteWidth can be used to set the 32/64 limit. If used,
168 * this global should be set to the proper width. For the core ACPICA code,
169 * this width depends on the DSDT version. For iASL, the default byte
170 * width is always 8 for the parser, but error checking is performed later
171 * to flag cases where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
173 ******************************************************************************/
175 ACPI_STATUS
176 AcpiUtStrtoul64 (
177 char *String,
178 UINT32 Flags,
179 UINT64 *ReturnValue)
181 ACPI_STATUS Status = AE_OK;
182 UINT32 Base;
185 ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
188 /* Parameter validation */
190 if (!String || !ReturnValue)
192 return_ACPI_STATUS (AE_BAD_PARAMETER);
195 *ReturnValue = 0;
197 /* Check for zero-length string, returns 0 */
199 if (*String == 0)
201 return_ACPI_STATUS (AE_OK);
204 /* Skip over any white space at start of string */
206 while (isspace ((int) *String))
208 String++;
211 /* End of string? return 0 */
213 if (*String == 0)
215 return_ACPI_STATUS (AE_OK);
219 * 1) The "0x" prefix indicates base 16. Per the ACPI specification,
220 * the "0x" prefix is only allowed for implicit (non-strict) conversions.
221 * However, we always allow it for compatibility with older ACPICA.
223 if ((*String == ACPI_ASCII_ZERO) &&
224 (tolower ((int) *(String + 1)) == 'x'))
226 String += 2; /* Go past the 0x */
227 if (*String == 0)
229 return_ACPI_STATUS (AE_OK); /* Return value 0 */
232 Base = 16;
235 /* 2) Force to base 16 (implicit conversion case) */
237 else if (Flags & ACPI_STRTOUL_BASE16)
239 Base = 16;
242 /* 3) Default fallback is to Base 10 */
244 else
246 Base = 10;
249 /* Skip all leading zeros */
251 while (*String == ACPI_ASCII_ZERO)
253 String++;
254 if (*String == 0)
256 return_ACPI_STATUS (AE_OK); /* Return value 0 */
260 /* Perform the base 16 or 10 conversion */
262 if (Base == 16)
264 *ReturnValue = AcpiUtStrtoulBase16 (String, Flags);
266 else
268 *ReturnValue = AcpiUtStrtoulBase10 (String, Flags);
271 return_ACPI_STATUS (Status);
275 /*******************************************************************************
277 * FUNCTION: AcpiUtStrtoulBase10
279 * PARAMETERS: String - Null terminated input string
280 * Flags - Conversion info
282 * RETURN: 64-bit converted integer
284 * DESCRIPTION: Performs a base 10 conversion of the input string to an
285 * integer value, either 32 or 64 bits.
286 * Note: String must be valid and non-null.
288 ******************************************************************************/
290 static UINT64
291 AcpiUtStrtoulBase10 (
292 char *String,
293 UINT32 Flags)
295 int AsciiDigit;
296 UINT64 NextValue;
297 UINT64 ReturnValue = 0;
300 /* Main loop: convert each ASCII byte in the input string */
302 while (*String)
304 AsciiDigit = *String;
305 if (!isdigit (AsciiDigit))
307 /* Not ASCII 0-9, terminate */
309 goto Exit;
312 /* Convert and insert (add) the decimal digit */
314 NextValue =
315 (ReturnValue * 10) + (AsciiDigit - ACPI_ASCII_ZERO);
317 /* Check for overflow (32 or 64 bit) - return current converted value */
319 if (((Flags & ACPI_STRTOUL_32BIT) && (NextValue > ACPI_UINT32_MAX)) ||
320 (NextValue < ReturnValue)) /* 64-bit overflow case */
322 goto Exit;
325 ReturnValue = NextValue;
326 String++;
329 Exit:
330 return (ReturnValue);
334 /*******************************************************************************
336 * FUNCTION: AcpiUtStrtoulBase16
338 * PARAMETERS: String - Null terminated input string
339 * Flags - conversion info
341 * RETURN: 64-bit converted integer
343 * DESCRIPTION: Performs a base 16 conversion of the input string to an
344 * integer value, either 32 or 64 bits.
345 * Note: String must be valid and non-null.
347 ******************************************************************************/
349 static UINT64
350 AcpiUtStrtoulBase16 (
351 char *String,
352 UINT32 Flags)
354 int AsciiDigit;
355 UINT32 ValidDigits = 1;
356 UINT64 ReturnValue = 0;
359 /* Main loop: convert each ASCII byte in the input string */
361 while (*String)
363 /* Check for overflow (32 or 64 bit) - return current converted value */
365 if ((ValidDigits > 16) ||
366 ((ValidDigits > 8) && (Flags & ACPI_STRTOUL_32BIT)))
368 goto Exit;
371 AsciiDigit = *String;
372 if (!isxdigit (AsciiDigit))
374 /* Not Hex ASCII A-F, a-f, or 0-9, terminate */
376 goto Exit;
379 /* Convert and insert the hex digit */
381 ReturnValue =
382 (ReturnValue << 4) | AcpiUtAsciiCharToHex (AsciiDigit);
384 String++;
385 ValidDigits++;
388 Exit:
389 return (ReturnValue);