Path: blob/main/sys/contrib/dev/acpica/components/utilities/utstrsuppt.c
48406 views
/*******************************************************************************1*2* Module Name: utstrsuppt - Support functions for string-to-integer conversion3*4******************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153154#define _COMPONENT ACPI_UTILITIES155ACPI_MODULE_NAME ("utstrsuppt")156157158/* Local prototypes */159160static ACPI_STATUS161AcpiUtInsertDigit (162UINT64 *AccumulatedValue,163UINT32 Base,164int AsciiDigit);165166static ACPI_STATUS167AcpiUtStrtoulMultiply64 (168UINT64 Multiplicand,169UINT32 Base,170UINT64 *OutProduct);171172static ACPI_STATUS173AcpiUtStrtoulAdd64 (174UINT64 Addend1,175UINT32 Digit,176UINT64 *OutSum);177178179/*******************************************************************************180*181* FUNCTION: AcpiUtConvertOctalString182*183* PARAMETERS: String - Null terminated input string184* ReturnValuePtr - Where the converted value is returned185*186* RETURN: Status and 64-bit converted integer187*188* DESCRIPTION: Performs a base 8 conversion of the input string to an189* integer value, either 32 or 64 bits.190*191* NOTE: Maximum 64-bit unsigned octal value is 01777777777777777777777192* Maximum 32-bit unsigned octal value is 037777777777193*194******************************************************************************/195196ACPI_STATUS197AcpiUtConvertOctalString (198char *String,199UINT64 *ReturnValuePtr)200{201UINT64 AccumulatedValue = 0;202ACPI_STATUS Status = AE_OK;203204205/* Convert each ASCII byte in the input string */206207while (*String)208{209/*210* Character must be ASCII 0-7, otherwise:211* 1) Runtime: terminate with no error, per the ACPI spec212* 2) Compiler: return an error213*/214if (!(ACPI_IS_OCTAL_DIGIT (*String)))215{216#ifdef ACPI_ASL_COMPILER217Status = AE_BAD_OCTAL_CONSTANT;218#endif219break;220}221222/* Convert and insert this octal digit into the accumulator */223224Status = AcpiUtInsertDigit (&AccumulatedValue, 8, *String);225if (ACPI_FAILURE (Status))226{227Status = AE_OCTAL_OVERFLOW;228break;229}230231String++;232}233234/* Always return the value that has been accumulated */235236*ReturnValuePtr = AccumulatedValue;237return (Status);238}239240241/*******************************************************************************242*243* FUNCTION: AcpiUtConvertDecimalString244*245* PARAMETERS: String - Null terminated input string246* ReturnValuePtr - Where the converted value is returned247*248* RETURN: Status and 64-bit converted integer249*250* DESCRIPTION: Performs a base 10 conversion of the input string to an251* integer value, either 32 or 64 bits.252*253* NOTE: Maximum 64-bit unsigned decimal value is 18446744073709551615254* Maximum 32-bit unsigned decimal value is 4294967295255*256******************************************************************************/257258ACPI_STATUS259AcpiUtConvertDecimalString (260char *String,261UINT64 *ReturnValuePtr)262{263UINT64 AccumulatedValue = 0;264ACPI_STATUS Status = AE_OK;265266267/* Convert each ASCII byte in the input string */268269while (*String)270{271/*272* Character must be ASCII 0-9, otherwise:273* 1) Runtime: terminate with no error, per the ACPI spec274* 2) Compiler: return an error275*/276if (!isdigit ((int) *String))277{278#ifdef ACPI_ASL_COMPILER279Status = AE_BAD_DECIMAL_CONSTANT;280#endif281break;282}283284/* Convert and insert this decimal digit into the accumulator */285286Status = AcpiUtInsertDigit (&AccumulatedValue, 10, *String);287if (ACPI_FAILURE (Status))288{289Status = AE_DECIMAL_OVERFLOW;290break;291}292293String++;294}295296/* Always return the value that has been accumulated */297298*ReturnValuePtr = AccumulatedValue;299return (Status);300}301302303/*******************************************************************************304*305* FUNCTION: AcpiUtConvertHexString306*307* PARAMETERS: String - Null terminated input string308* ReturnValuePtr - Where the converted value is returned309*310* RETURN: Status and 64-bit converted integer311*312* DESCRIPTION: Performs a base 16 conversion of the input string to an313* integer value, either 32 or 64 bits.314*315* NOTE: Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF316* Maximum 32-bit unsigned hex value is 0xFFFFFFFF317*318******************************************************************************/319320ACPI_STATUS321AcpiUtConvertHexString (322char *String,323UINT64 *ReturnValuePtr)324{325UINT64 AccumulatedValue = 0;326ACPI_STATUS Status = AE_OK;327328329/* Convert each ASCII byte in the input string */330331while (*String)332{333/*334* Character must be ASCII A-F, a-f, or 0-9, otherwise:335* 1) Runtime: terminate with no error, per the ACPI spec336* 2) Compiler: return an error337*/338if (!isxdigit ((int) *String))339{340#ifdef ACPI_ASL_COMPILER341Status = AE_BAD_HEX_CONSTANT;342#endif343break;344}345346/* Convert and insert this hex digit into the accumulator */347348Status = AcpiUtInsertDigit (&AccumulatedValue, 16, *String);349if (ACPI_FAILURE (Status))350{351Status = AE_HEX_OVERFLOW;352break;353}354355String++;356}357358/* Always return the value that has been accumulated */359360*ReturnValuePtr = AccumulatedValue;361return (Status);362}363364365/*******************************************************************************366*367* FUNCTION: AcpiUtRemoveLeadingZeros368*369* PARAMETERS: String - Pointer to input ASCII string370*371* RETURN: Next character after any leading zeros. This character may be372* used by the caller to detect end-of-string.373*374* DESCRIPTION: Remove any leading zeros in the input string. Return the375* next character after the final ASCII zero to enable the caller376* to check for the end of the string (NULL terminator).377*378******************************************************************************/379380char381AcpiUtRemoveLeadingZeros (382char **String)383{384385while (**String == ACPI_ASCII_ZERO)386{387*String += 1;388}389390return (**String);391}392393394/*******************************************************************************395*396* FUNCTION: AcpiUtRemoveWhitespace397*398* PARAMETERS: String - Pointer to input ASCII string399*400* RETURN: Next character after any whitespace. This character may be401* used by the caller to detect end-of-string.402*403* DESCRIPTION: Remove any leading whitespace in the input string. Return the404* next character after the final ASCII zero to enable the caller405* to check for the end of the string (NULL terminator).406*407******************************************************************************/408409char410AcpiUtRemoveWhitespace (411char **String)412{413414while (isspace ((UINT8) **String))415{416*String += 1;417}418419return (**String);420}421422423/*******************************************************************************424*425* FUNCTION: AcpiUtDetectHexPrefix426*427* PARAMETERS: String - Pointer to input ASCII string428*429* RETURN: TRUE if a "0x" prefix was found at the start of the string430*431* DESCRIPTION: Detect and remove a hex "0x" prefix432*433******************************************************************************/434435BOOLEAN436AcpiUtDetectHexPrefix (437char **String)438{439char *InitialPosition = *String;440441AcpiUtRemoveHexPrefix (String);442if (*String != InitialPosition)443{444return (TRUE); /* String is past leading 0x */445}446447return (FALSE); /* Not a hex string */448}449450451/*******************************************************************************452*453* FUNCTION: AcpiUtRemoveHexPrefix454*455* PARAMETERS: String - Pointer to input ASCII string456*457* RETURN: none458*459* DESCRIPTION: Remove a hex "0x" prefix460*461******************************************************************************/462463void464AcpiUtRemoveHexPrefix (465char **String)466{467if ((**String == ACPI_ASCII_ZERO) &&468(tolower ((int) *(*String + 1)) == 'x'))469{470*String += 2; /* Go past the leading 0x */471}472}473474475/*******************************************************************************476*477* FUNCTION: AcpiUtDetectOctalPrefix478*479* PARAMETERS: String - Pointer to input ASCII string480*481* RETURN: True if an octal "0" prefix was found at the start of the482* string483*484* DESCRIPTION: Detect and remove an octal prefix (zero)485*486******************************************************************************/487488BOOLEAN489AcpiUtDetectOctalPrefix (490char **String)491{492493if (**String == ACPI_ASCII_ZERO)494{495*String += 1; /* Go past the leading 0 */496return (TRUE);497}498499return (FALSE); /* Not an octal string */500}501502503/*******************************************************************************504*505* FUNCTION: AcpiUtInsertDigit506*507* PARAMETERS: AccumulatedValue - Current value of the integer value508* accumulator. The new value is509* returned here.510* Base - Radix, either 8/10/16511* AsciiDigit - ASCII single digit to be inserted512*513* RETURN: Status and result of the convert/insert operation. The only514* possible returned exception code is numeric overflow of515* either the multiply or add conversion operations.516*517* DESCRIPTION: Generic conversion and insertion function for all bases:518*519* 1) Multiply the current accumulated/converted value by the520* base in order to make room for the new character.521*522* 2) Convert the new character to binary and add it to the523* current accumulated value.524*525* Note: The only possible exception indicates an integer526* overflow (AE_NUMERIC_OVERFLOW)527*528******************************************************************************/529530static ACPI_STATUS531AcpiUtInsertDigit (532UINT64 *AccumulatedValue,533UINT32 Base,534int AsciiDigit)535{536ACPI_STATUS Status;537UINT64 Product;538539540/* Make room in the accumulated value for the incoming digit */541542Status = AcpiUtStrtoulMultiply64 (*AccumulatedValue, Base, &Product);543if (ACPI_FAILURE (Status))544{545return (Status);546}547548/* Add in the new digit, and store the sum to the accumulated value */549550Status = AcpiUtStrtoulAdd64 (Product, AcpiUtAsciiCharToHex (AsciiDigit),551AccumulatedValue);552553return (Status);554}555556557/*******************************************************************************558*559* FUNCTION: AcpiUtStrtoulMultiply64560*561* PARAMETERS: Multiplicand - Current accumulated converted integer562* Base - Base/Radix563* OutProduct - Where the product is returned564*565* RETURN: Status and 64-bit product566*567* DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as568* well as 32-bit overflow if necessary (if the current global569* integer width is 32).570*571******************************************************************************/572573static ACPI_STATUS574AcpiUtStrtoulMultiply64 (575UINT64 Multiplicand,576UINT32 Base,577UINT64 *OutProduct)578{579UINT64 Product;580UINT64 Quotient;581582583/* Exit if either operand is zero */584585*OutProduct = 0;586if (!Multiplicand || !Base)587{588return (AE_OK);589}590591/*592* Check for 64-bit overflow before the actual multiplication.593*594* Notes: 64-bit division is often not supported on 32-bit platforms595* (it requires a library function), Therefore ACPICA has a local596* 64-bit divide function. Also, Multiplier is currently only used597* as the radix (8/10/16), to the 64/32 divide will always work.598*/599AcpiUtShortDivide (ACPI_UINT64_MAX, Base, &Quotient, NULL);600if (Multiplicand > Quotient)601{602return (AE_NUMERIC_OVERFLOW);603}604605Product = Multiplicand * Base;606607/* Check for 32-bit overflow if necessary */608609if ((AcpiGbl_IntegerBitWidth == 32) && (Product > ACPI_UINT32_MAX))610{611return (AE_NUMERIC_OVERFLOW);612}613614*OutProduct = Product;615return (AE_OK);616}617618619/*******************************************************************************620*621* FUNCTION: AcpiUtStrtoulAdd64622*623* PARAMETERS: Addend1 - Current accumulated converted integer624* Digit - New hex value/char625* OutSum - Where sum is returned (Accumulator)626*627* RETURN: Status and 64-bit sum628*629* DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as630* well as 32-bit overflow if necessary (if the current global631* integer width is 32).632*633******************************************************************************/634635static ACPI_STATUS636AcpiUtStrtoulAdd64 (637UINT64 Addend1,638UINT32 Digit,639UINT64 *OutSum)640{641UINT64 Sum;642643644/* Check for 64-bit overflow before the actual addition */645646if ((Addend1 > 0) && (Digit > (ACPI_UINT64_MAX - Addend1)))647{648return (AE_NUMERIC_OVERFLOW);649}650651Sum = Addend1 + Digit;652653/* Check for 32-bit overflow if necessary */654655if ((AcpiGbl_IntegerBitWidth == 32) && (Sum > ACPI_UINT32_MAX))656{657return (AE_NUMERIC_OVERFLOW);658}659660*OutSum = Sum;661return (AE_OK);662}663664665