Path: blob/main/sys/contrib/dev/acpica/components/utilities/utstrtoul64.c
48406 views
/*******************************************************************************1*2* Module Name: utstrtoul64 - String-to-integer conversion support for both3* 64-bit and 32-bit integers4*5******************************************************************************/67/******************************************************************************8*9* 1. Copyright Notice10*11* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.12* All rights reserved.13*14* 2. License15*16* 2.1. This is your license from Intel Corp. under its intellectual property17* rights. You may have additional license terms from the party that provided18* you this software, covering your right to use that party's intellectual19* property rights.20*21* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a22* copy of the source code appearing in this file ("Covered Code") an23* irrevocable, perpetual, worldwide license under Intel's copyrights in the24* base code distributed originally by Intel ("Original Intel Code") to copy,25* make derivatives, distribute, use and display any portion of the Covered26* Code in any form, with the right to sublicense such rights; and27*28* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent29* license (with the right to sublicense), under only those claims of Intel30* patents that are infringed by the Original Intel Code, to make, use, sell,31* offer to sell, and import the Covered Code and derivative works thereof32* solely to the minimum extent necessary to exercise the above copyright33* license, and in no event shall the patent license extend to any additions34* to or modifications of the Original Intel Code. No other license or right35* is granted directly or by implication, estoppel or otherwise;36*37* The above copyright and patent license is granted only if the following38* conditions are met:39*40* 3. Conditions41*42* 3.1. Redistribution of Source with Rights to Further Distribute Source.43* Redistribution of source code of any substantial portion of the Covered44* Code or modification with rights to further distribute source must include45* the above Copyright Notice, the above License, this list of Conditions,46* and the following Disclaimer and Export Compliance provision. In addition,47* Licensee must cause all Covered Code to which Licensee contributes to48* contain a file documenting the changes Licensee made to create that Covered49* Code and the date of any change. Licensee must include in that file the50* documentation of any changes made by any predecessor Licensee. Licensee51* must include a prominent statement that the modification is derived,52* directly or indirectly, from Original Intel Code.53*54* 3.2. Redistribution of Source with no Rights to Further Distribute Source.55* Redistribution of source code of any substantial portion of the Covered56* Code or modification without rights to further distribute source must57* include the following Disclaimer and Export Compliance provision in the58* documentation and/or other materials provided with distribution. In59* addition, Licensee may not authorize further sublicense of source of any60* portion of the Covered Code, and must include terms to the effect that the61* license from Licensee to its licensee is limited to the intellectual62* property embodied in the software Licensee provides to its licensee, and63* not to intellectual property embodied in modifications its licensee may64* make.65*66* 3.3. Redistribution of Executable. Redistribution in executable form of any67* substantial portion of the Covered Code or modification must reproduce the68* above Copyright Notice, and the following Disclaimer and Export Compliance69* provision in the documentation and/or other materials provided with the70* distribution.71*72* 3.4. Intel retains all right, title, and interest in and to the Original73* Intel Code.74*75* 3.5. Neither the name Intel nor any other trademark owned or controlled by76* Intel shall be used in advertising or otherwise to promote the sale, use or77* other dealings in products derived from or relating to the Covered Code78* without prior written authorization from Intel.79*80* 4. Disclaimer and Export Compliance81*82* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED83* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE84* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,85* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY86* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY87* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A88* PARTICULAR PURPOSE.89*90* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES91* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR92* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,93* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY94* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL95* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS96* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY97* LIMITED REMEDY.98*99* 4.3. Licensee shall not export, either directly or indirectly, any of this100* software or system incorporating such software without first obtaining any101* required license or other approval from the U. S. Department of Commerce or102* any other agency or department of the United States Government. In the103* event Licensee exports any such software from the United States or104* re-exports any such software from a foreign destination, Licensee shall105* ensure that the distribution and export/re-export of the software is in106* compliance with all laws, regulations, orders, or other restrictions of the107* U.S. Export Administration Regulations. Licensee agrees that neither it nor108* any of its subsidiaries will export/re-export any technical data, process,109* software, or service, directly or indirectly, to any country for which the110* United States government or any agency thereof requires an export license,111* other governmental approval, or letter of assurance, without first obtaining112* such license, approval or letter.113*114*****************************************************************************115*116* Alternatively, you may choose to be licensed under the terms of the117* following license:118*119* Redistribution and use in source and binary forms, with or without120* modification, are permitted provided that the following conditions121* are met:122* 1. Redistributions of source code must retain the above copyright123* notice, this list of conditions, and the following disclaimer,124* without modification.125* 2. Redistributions in binary form must reproduce at minimum a disclaimer126* substantially similar to the "NO WARRANTY" disclaimer below127* ("Disclaimer") and any redistribution must be conditioned upon128* including a substantially similar Disclaimer requirement for further129* binary redistribution.130* 3. Neither the names of the above-listed copyright holders nor the names131* of any contributors may be used to endorse or promote products derived132* from this software without specific prior written permission.133*134* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS135* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT136* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR137* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT138* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,139* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT140* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,141* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY142* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT143* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE144* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.145*146* Alternatively, you may choose to be licensed under the terms of the147* GNU General Public License ("GPL") version 2 as published by the Free148* Software Foundation.149*150*****************************************************************************/151152#include <contrib/dev/acpica/include/acpi.h>153#include <contrib/dev/acpica/include/accommon.h>154155#define _COMPONENT ACPI_UTILITIES156ACPI_MODULE_NAME ("utstrtoul64")157158159/*******************************************************************************160*161* This module contains the top-level string to 64/32-bit unsigned integer162* conversion functions:163*164* 1) A standard strtoul() function that supports 64-bit integers, base165* 8/10/16, with integer overflow support. This is used mainly by the166* iASL compiler, which implements tighter constraints on integer167* constants than the runtime (interpreter) integer-to-string conversions.168* 2) Runtime "Explicit conversion" as defined in the ACPI specification.169* 3) Runtime "Implicit conversion" as defined in the ACPI specification.170*171* Current users of this module:172*173* iASL - Preprocessor (constants and math expressions)174* iASL - Main parser, conversion of constants to integers175* iASL - Data Table Compiler parser (constants and math expressions)176* Interpreter - Implicit and explicit conversions, GPE method names177* Interpreter - Repair code for return values from predefined names178* Debugger - Command line input string conversion179* AcpiDump - ACPI table physical addresses180* AcpiExec - Support for namespace overrides181*182* Notes concerning users of these interfaces:183*184* AcpiGbl_IntegerByteWidth is used to set the 32/64 bit limit for explicit185* and implicit conversions. This global must be set to the proper width.186* For the core ACPICA code, the width depends on the DSDT version. For the187* AcpiUtStrtoul64 interface, all conversions are 64 bits. This interface is188* used primarily for iASL, where the default width is 64 bits for all parsers,189* but error checking is performed later to flag cases where a 64-bit constant190* is wrongly defined in a 32-bit DSDT/SSDT.191*192* In ACPI, the only place where octal numbers are supported is within193* the ASL language itself. This is implemented via the main AcpiUtStrtoul64194* interface. According the ACPI specification, there is no ACPI runtime195* support (explicit/implicit) for octal string conversions.196*197******************************************************************************/198199200/*******************************************************************************201*202* FUNCTION: AcpiUtStrtoul64203*204* PARAMETERS: String - Null terminated input string,205* must be a valid pointer206* ReturnValue - Where the converted integer is207* returned. Must be a valid pointer208*209* RETURN: Status and converted integer. Returns an exception on a210* 64-bit numeric overflow211*212* DESCRIPTION: Convert a string into an unsigned integer. Always performs a213* full 64-bit conversion, regardless of the current global214* integer width. Supports Decimal, Hex, and Octal strings.215*216* Current users of this function:217*218* iASL - Preprocessor (constants and math expressions)219* iASL - Main ASL parser, conversion of ASL constants to integers220* iASL - Data Table Compiler parser (constants and math expressions)221* Interpreter - Repair code for return values from predefined names222* AcpiDump - ACPI table physical addresses223* AcpiExec - Support for namespace overrides224*225******************************************************************************/226227ACPI_STATUS228AcpiUtStrtoul64 (229char *String,230UINT64 *ReturnValue)231{232ACPI_STATUS Status = AE_OK;233UINT8 OriginalBitWidth;234UINT32 Base = 10; /* Default is decimal */235236237ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);238239240*ReturnValue = 0;241242/* A NULL return string returns a value of zero */243244if (*String == 0)245{246return_ACPI_STATUS (AE_OK);247}248249if (!AcpiUtRemoveWhitespace (&String))250{251return_ACPI_STATUS (AE_OK);252}253254/*255* 1) Check for a hex constant. A "0x" prefix indicates base 16.256*/257if (AcpiUtDetectHexPrefix (&String))258{259Base = 16;260}261262/*263* 2) Check for an octal constant, defined to be a leading zero264* followed by sequence of octal digits (0-7)265*/266else if (AcpiUtDetectOctalPrefix (&String))267{268Base = 8;269}270271if (!AcpiUtRemoveLeadingZeros (&String))272{273return_ACPI_STATUS (AE_OK); /* Return value 0 */274}275276/*277* Force a full 64-bit conversion. The caller (usually iASL) must278* check for a 32-bit overflow later as necessary (If current mode279* is 32-bit, meaning a 32-bit DSDT).280*/281OriginalBitWidth = AcpiGbl_IntegerBitWidth;282AcpiGbl_IntegerBitWidth = 64;283284/*285* Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow286* will return an exception (to allow iASL to flag the statement).287*/288switch (Base)289{290case 8:291Status = AcpiUtConvertOctalString (String, ReturnValue);292break;293294case 10:295Status = AcpiUtConvertDecimalString (String, ReturnValue);296break;297298case 16:299default:300Status = AcpiUtConvertHexString (String, ReturnValue);301break;302}303304/* Only possible exception from above is a 64-bit overflow */305306AcpiGbl_IntegerBitWidth = OriginalBitWidth;307return_ACPI_STATUS (Status);308}309310311/*******************************************************************************312*313* FUNCTION: AcpiUtImplicitStrtoul64314*315* PARAMETERS: String - Null terminated input string,316* must be a valid pointer317*318* RETURN: Converted integer319*320* DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon321* an "implicit conversion" by the ACPI specification. Used by322* many ASL operators that require an integer operand, and support323* an automatic (implicit) conversion from a string operand324* to the final integer operand. The major restriction is that325* only hex strings are supported.326*327* -----------------------------------------------------------------------------328*329* Base is always 16, either with or without the 0x prefix. Decimal and330* Octal strings are not supported, as per the ACPI specification.331*332* Examples (both are hex values):333* Add ("BA98", Arg0, Local0)334* Subtract ("0x12345678", Arg1, Local1)335*336* Conversion rules as extracted from the ACPI specification:337*338* The converted integer is initialized to the value zero.339* The ASCII string is always interpreted as a hexadecimal constant.340*341* 1) According to the ACPI specification, a "0x" prefix is not allowed.342* However, ACPICA allows this as an ACPI extension on general343* principle. (NO ERROR)344*345* 2) The conversion terminates when the size of an integer is reached346* (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)347*348* 3) The first non-hex character terminates the conversion and returns349* the current accumulated value of the converted integer (NO ERROR).350*351* 4) Conversion of a null (zero-length) string to an integer is352* technically not allowed. However, ACPICA allows this as an ACPI353* extension. The conversion returns the value 0. (NO ERROR)354*355* NOTE: There are no error conditions returned by this function. At356* the minimum, a value of zero is returned.357*358* Current users of this function:359*360* Interpreter - All runtime implicit conversions, as per ACPI specification361* iASL - Data Table Compiler parser (constants and math expressions)362*363******************************************************************************/364365UINT64366AcpiUtImplicitStrtoul64 (367char *String)368{369UINT64 ConvertedInteger = 0;370371372ACPI_FUNCTION_TRACE_STR (UtImplicitStrtoul64, String);373374375if (!AcpiUtRemoveWhitespace (&String))376{377return_VALUE (0);378}379380/*381* Per the ACPI specification, only hexadecimal is supported for382* implicit conversions, and the "0x" prefix is "not allowed".383* However, allow a "0x" prefix as an ACPI extension.384*/385AcpiUtRemoveHexPrefix (&String);386387if (!AcpiUtRemoveLeadingZeros (&String))388{389return_VALUE (0);390}391392/*393* Ignore overflow as per the ACPI specification. This is implemented by394* ignoring the return status from the conversion function called below.395* On overflow, the input string is simply truncated.396*/397AcpiUtConvertHexString (String, &ConvertedInteger);398return_VALUE (ConvertedInteger);399}400401402/*******************************************************************************403*404* FUNCTION: AcpiUtExplicitStrtoul64405*406* PARAMETERS: String - Null terminated input string,407* must be a valid pointer408*409* RETURN: Converted integer410*411* DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon412* an "explicit conversion" by the ACPI specification. The413* main restriction is that only hex and decimal are supported.414*415* -----------------------------------------------------------------------------416*417* Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings418* are not supported, as per the ACPI specification.419*420* Examples:421* ToInteger ("1000") Decimal422* ToInteger ("0xABCD") Hex423*424* Conversion rules as extracted from the ACPI specification:425*426* 1) The input string is either a decimal or hexadecimal numeric string.427* A hex value must be prefixed by "0x" or it is interpreted as decimal.428*429* 2) The value must not exceed the maximum of an integer value430* (32 or 64 bits). The ACPI specification states the behavior is431* "unpredictable", so ACPICA matches the behavior of the implicit432* conversion case. There are no numeric overflow conditions. (NO ERROR)433*434* 3) Behavior on the first non-hex character is not defined by the ACPI435* specification (for the ToInteger operator), so ACPICA matches the436* behavior of the implicit conversion case. It terminates the437* conversion and returns the current accumulated value of the converted438* integer. (NO ERROR)439*440* 4) Conversion of a null (zero-length) string to an integer is441* technically not allowed. However, ACPICA allows this as an ACPI442* extension. The conversion returns the value 0. (NO ERROR)443*444* NOTE: There are no error conditions returned by this function. At the445* minimum, a value of zero is returned.446*447* Current users of this function:448*449* Interpreter - Runtime ASL ToInteger operator, as per the ACPI specification450*451******************************************************************************/452453UINT64454AcpiUtExplicitStrtoul64 (455char *String)456{457UINT64 ConvertedInteger = 0;458UINT32 Base = 10; /* Default is decimal */459460461ACPI_FUNCTION_TRACE_STR (UtExplicitStrtoul64, String);462463464if (!AcpiUtRemoveWhitespace (&String))465{466return_VALUE (0);467}468469/*470* Only Hex and Decimal are supported, as per the ACPI specification.471* A "0x" prefix indicates hex; otherwise decimal is assumed.472*/473if (AcpiUtDetectHexPrefix (&String))474{475Base = 16;476}477478if (!AcpiUtRemoveLeadingZeros (&String))479{480return_VALUE (0);481}482483/*484* Ignore overflow as per the ACPI specification. This is implemented by485* ignoring the return status from the conversion functions called below.486* On overflow, the input string is simply truncated.487*/488switch (Base)489{490case 10:491default:492AcpiUtConvertDecimalString (String, &ConvertedInteger);493break;494495case 16:496AcpiUtConvertHexString (String, &ConvertedInteger);497break;498}499500return_VALUE (ConvertedInteger);501}502503504