^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Module Name: utstrtoul64 - String-to-integer conversion support for both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 64-bit and 32-bit integers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <acpi/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "accommon.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define _COMPONENT ACPI_UTILITIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) ACPI_MODULE_NAME("utstrtoul64")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * This module contains the top-level string to 64/32-bit unsigned integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * conversion functions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * 1) A standard strtoul() function that supports 64-bit integers, base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * 8/10/16, with integer overflow support. This is used mainly by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * iASL compiler, which implements tighter constraints on integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * constants than the runtime (interpreter) integer-to-string conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * 2) Runtime "Explicit conversion" as defined in the ACPI specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * 3) Runtime "Implicit conversion" as defined in the ACPI specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Current users of this module:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * iASL - Preprocessor (constants and math expressions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * iASL - Main parser, conversion of constants to integers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * iASL - Data Table Compiler parser (constants and math expressions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * interpreter - Implicit and explicit conversions, GPE method names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * interpreter - Repair code for return values from predefined names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * debugger - Command line input string conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * acpi_dump - ACPI table physical addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * acpi_exec - Support for namespace overrides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Notes concerning users of these interfaces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * acpi_gbl_integer_byte_width is used to set the 32/64 bit limit for explicit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * and implicit conversions. This global must be set to the proper width.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * For the core ACPICA code, the width depends on the DSDT version. For the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * acpi_ut_strtoul64 interface, all conversions are 64 bits. This interface is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * used primarily for iASL, where the default width is 64 bits for all parsers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * but error checking is performed later to flag cases where a 64-bit constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * is wrongly defined in a 32-bit DSDT/SSDT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * In ACPI, the only place where octal numbers are supported is within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * the ASL language itself. This is implemented via the main acpi_ut_strtoul64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * interface. According the ACPI specification, there is no ACPI runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * support (explicit/implicit) for octal string conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * FUNCTION: acpi_ut_strtoul64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * PARAMETERS: string - Null terminated input string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * must be a valid pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * return_value - Where the converted integer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * returned. Must be a valid pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * RETURN: Status and converted integer. Returns an exception on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * 64-bit numeric overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * full 64-bit conversion, regardless of the current global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * integer width. Supports Decimal, Hex, and Octal strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Current users of this function:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * iASL - Preprocessor (constants and math expressions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * iASL - Main ASL parser, conversion of ASL constants to integers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * iASL - Data Table Compiler parser (constants and math expressions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * interpreter - Repair code for return values from predefined names
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * acpi_dump - ACPI table physical addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * acpi_exec - Support for namespace overrides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) acpi_status acpi_ut_strtoul64(char *string, u64 *return_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) acpi_status status = AE_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u8 original_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u32 base = 10; /* Default is decimal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ACPI_FUNCTION_TRACE_STR(ut_strtoul64, string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *return_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* A NULL return string returns a value of zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (*string == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return_ACPI_STATUS(AE_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!acpi_ut_remove_whitespace(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return_ACPI_STATUS(AE_OK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * 1) Check for a hex constant. A "0x" prefix indicates base 16.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (acpi_ut_detect_hex_prefix(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) base = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * 2) Check for an octal constant, defined to be a leading zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * followed by sequence of octal digits (0-7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) else if (acpi_ut_detect_octal_prefix(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) base = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!acpi_ut_remove_leading_zeros(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return_ACPI_STATUS(AE_OK); /* Return value 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Force a full 64-bit conversion. The caller (usually iASL) must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * check for a 32-bit overflow later as necessary (If current mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * is 32-bit, meaning a 32-bit DSDT).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) original_bit_width = acpi_gbl_integer_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) acpi_gbl_integer_bit_width = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * will return an exception (to allow iASL to flag the statement).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) switch (base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) status = acpi_ut_convert_octal_string(string, return_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) status = acpi_ut_convert_decimal_string(string, return_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) status = acpi_ut_convert_hex_string(string, return_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Only possible exception from above is a 64-bit overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) acpi_gbl_integer_bit_width = original_bit_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return_ACPI_STATUS(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * FUNCTION: acpi_ut_implicit_strtoul64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * PARAMETERS: string - Null terminated input string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * must be a valid pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * RETURN: Converted integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * an "implicit conversion" by the ACPI specification. Used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * many ASL operators that require an integer operand, and support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * an automatic (implicit) conversion from a string operand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * to the final integer operand. The major restriction is that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * only hex strings are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * -----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * Base is always 16, either with or without the 0x prefix. Decimal and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Octal strings are not supported, as per the ACPI specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Examples (both are hex values):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Add ("BA98", Arg0, Local0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * Subtract ("0x12345678", Arg1, Local1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * Conversion rules as extracted from the ACPI specification:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * The converted integer is initialized to the value zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * The ASCII string is always interpreted as a hexadecimal constant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * 1) According to the ACPI specification, a "0x" prefix is not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * However, ACPICA allows this as an ACPI extension on general
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * principle. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * 2) The conversion terminates when the size of an integer is reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * 3) The first non-hex character terminates the conversion and returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * the current accumulated value of the converted integer (NO ERROR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * 4) Conversion of a null (zero-length) string to an integer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * technically not allowed. However, ACPICA allows this as an ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * extension. The conversion returns the value 0. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * NOTE: There are no error conditions returned by this function. At
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * the minimum, a value of zero is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Current users of this function:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * interpreter - All runtime implicit conversions, as per ACPI specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * iASL - Data Table Compiler parser (constants and math expressions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) u64 acpi_ut_implicit_strtoul64(char *string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u64 converted_integer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ACPI_FUNCTION_TRACE_STR(ut_implicit_strtoul64, string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!acpi_ut_remove_whitespace(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return_VALUE(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * Per the ACPI specification, only hexadecimal is supported for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * implicit conversions, and the "0x" prefix is "not allowed".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * However, allow a "0x" prefix as an ACPI extension.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) acpi_ut_remove_hex_prefix(&string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!acpi_ut_remove_leading_zeros(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return_VALUE(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * Ignore overflow as per the ACPI specification. This is implemented by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * ignoring the return status from the conversion function called below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * On overflow, the input string is simply truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) acpi_ut_convert_hex_string(string, &converted_integer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return_VALUE(converted_integer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /*******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * FUNCTION: acpi_ut_explicit_strtoul64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * PARAMETERS: string - Null terminated input string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * must be a valid pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * RETURN: Converted integer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * an "explicit conversion" by the ACPI specification. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * main restriction is that only hex and decimal are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * -----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * are not supported, as per the ACPI specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * to_integer ("1000") Decimal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * to_integer ("0xABCD") Hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * Conversion rules as extracted from the ACPI specification:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * 1) The input string is either a decimal or hexadecimal numeric string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * A hex value must be prefixed by "0x" or it is interpreted as decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * 2) The value must not exceed the maximum of an integer value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * (32 or 64 bits). The ACPI specification states the behavior is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * "unpredictable", so ACPICA matches the behavior of the implicit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * conversion case. There are no numeric overflow conditions. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * 3) Behavior on the first non-hex character is not defined by the ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * specification (for the to_integer operator), so ACPICA matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * behavior of the implicit conversion case. It terminates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * conversion and returns the current accumulated value of the converted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * integer. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * 4) Conversion of a null (zero-length) string to an integer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * technically not allowed. However, ACPICA allows this as an ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * extension. The conversion returns the value 0. (NO ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * NOTE: There are no error conditions returned by this function. At the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * minimum, a value of zero is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Current users of this function:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * interpreter - Runtime ASL to_integer operator, as per the ACPI specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) u64 acpi_ut_explicit_strtoul64(char *string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) u64 converted_integer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) u32 base = 10; /* Default is decimal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ACPI_FUNCTION_TRACE_STR(ut_explicit_strtoul64, string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!acpi_ut_remove_whitespace(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return_VALUE(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Only Hex and Decimal are supported, as per the ACPI specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * A "0x" prefix indicates hex; otherwise decimal is assumed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (acpi_ut_detect_hex_prefix(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) base = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (!acpi_ut_remove_leading_zeros(&string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return_VALUE(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Ignore overflow as per the ACPI specification. This is implemented by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * ignoring the return status from the conversion functions called below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * On overflow, the input string is simply truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) switch (base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) acpi_ut_convert_decimal_string(string, &converted_integer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) acpi_ut_convert_hex_string(string, &converted_integer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return_VALUE(converted_integer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }