Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:04

0001 /*******************************************************************************
0002  *
0003  * Module Name: utstrtoul64 - String-to-integer conversion support for both
0004  *                            64-bit and 32-bit integers
0005  *
0006  ******************************************************************************/
0007 
0008 /******************************************************************************
0009  *
0010  * 1. Copyright Notice
0011  *
0012  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
0013  * All rights reserved.
0014  *
0015  * 2. License
0016  *
0017  * 2.1. This is your license from Intel Corp. under its intellectual property
0018  * rights. You may have additional license terms from the party that provided
0019  * you this software, covering your right to use that party's intellectual
0020  * property rights.
0021  *
0022  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
0023  * copy of the source code appearing in this file ("Covered Code") an
0024  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
0025  * base code distributed originally by Intel ("Original Intel Code") to copy,
0026  * make derivatives, distribute, use and display any portion of the Covered
0027  * Code in any form, with the right to sublicense such rights; and
0028  *
0029  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
0030  * license (with the right to sublicense), under only those claims of Intel
0031  * patents that are infringed by the Original Intel Code, to make, use, sell,
0032  * offer to sell, and import the Covered Code and derivative works thereof
0033  * solely to the minimum extent necessary to exercise the above copyright
0034  * license, and in no event shall the patent license extend to any additions
0035  * to or modifications of the Original Intel Code. No other license or right
0036  * is granted directly or by implication, estoppel or otherwise;
0037  *
0038  * The above copyright and patent license is granted only if the following
0039  * conditions are met:
0040  *
0041  * 3. Conditions
0042  *
0043  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
0044  * Redistribution of source code of any substantial portion of the Covered
0045  * Code or modification with rights to further distribute source must include
0046  * the above Copyright Notice, the above License, this list of Conditions,
0047  * and the following Disclaimer and Export Compliance provision. In addition,
0048  * Licensee must cause all Covered Code to which Licensee contributes to
0049  * contain a file documenting the changes Licensee made to create that Covered
0050  * Code and the date of any change. Licensee must include in that file the
0051  * documentation of any changes made by any predecessor Licensee. Licensee
0052  * must include a prominent statement that the modification is derived,
0053  * directly or indirectly, from Original Intel Code.
0054  *
0055  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
0056  * Redistribution of source code of any substantial portion of the Covered
0057  * Code or modification without rights to further distribute source must
0058  * include the following Disclaimer and Export Compliance provision in the
0059  * documentation and/or other materials provided with distribution. In
0060  * addition, Licensee may not authorize further sublicense of source of any
0061  * portion of the Covered Code, and must include terms to the effect that the
0062  * license from Licensee to its licensee is limited to the intellectual
0063  * property embodied in the software Licensee provides to its licensee, and
0064  * not to intellectual property embodied in modifications its licensee may
0065  * make.
0066  *
0067  * 3.3. Redistribution of Executable. Redistribution in executable form of any
0068  * substantial portion of the Covered Code or modification must reproduce the
0069  * above Copyright Notice, and the following Disclaimer and Export Compliance
0070  * provision in the documentation and/or other materials provided with the
0071  * distribution.
0072  *
0073  * 3.4. Intel retains all right, title, and interest in and to the Original
0074  * Intel Code.
0075  *
0076  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
0077  * Intel shall be used in advertising or otherwise to promote the sale, use or
0078  * other dealings in products derived from or relating to the Covered Code
0079  * without prior written authorization from Intel.
0080  *
0081  * 4. Disclaimer and Export Compliance
0082  *
0083  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
0084  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
0085  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
0086  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
0087  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
0088  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
0089  * PARTICULAR PURPOSE.
0090  *
0091  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
0092  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
0093  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
0094  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
0095  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
0096  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
0097  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
0098  * LIMITED REMEDY.
0099  *
0100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
0101  * software or system incorporating such software without first obtaining any
0102  * required license or other approval from the U. S. Department of Commerce or
0103  * any other agency or department of the United States Government. In the
0104  * event Licensee exports any such software from the United States or
0105  * re-exports any such software from a foreign destination, Licensee shall
0106  * ensure that the distribution and export/re-export of the software is in
0107  * compliance with all laws, regulations, orders, or other restrictions of the
0108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
0109  * any of its subsidiaries will export/re-export any technical data, process,
0110  * software, or service, directly or indirectly, to any country for which the
0111  * United States government or any agency thereof requires an export license,
0112  * other governmental approval, or letter of assurance, without first obtaining
0113  * such license, approval or letter.
0114  *
0115  *****************************************************************************
0116  *
0117  * Alternatively, you may choose to be licensed under the terms of the
0118  * following license:
0119  *
0120  * Redistribution and use in source and binary forms, with or without
0121  * modification, are permitted provided that the following conditions
0122  * are met:
0123  * 1. Redistributions of source code must retain the above copyright
0124  *    notice, this list of conditions, and the following disclaimer,
0125  *    without modification.
0126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
0127  *    substantially similar to the "NO WARRANTY" disclaimer below
0128  *    ("Disclaimer") and any redistribution must be conditioned upon
0129  *    including a substantially similar Disclaimer requirement for further
0130  *    binary redistribution.
0131  * 3. Neither the names of the above-listed copyright holders nor the names
0132  *    of any contributors may be used to endorse or promote products derived
0133  *    from this software without specific prior written permission.
0134  *
0135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0146  *
0147  * Alternatively, you may choose to be licensed under the terms of the
0148  * GNU General Public License ("GPL") version 2 as published by the Free
0149  * Software Foundation.
0150  *
0151  *****************************************************************************/
0152 
0153 #include "acpi.h"
0154 #include "accommon.h"
0155 
0156 #define _COMPONENT          ACPI_UTILITIES
0157         ACPI_MODULE_NAME    ("utstrtoul64")
0158 
0159 
0160 /*******************************************************************************
0161  *
0162  * This module contains the top-level string to 64/32-bit unsigned integer
0163  * conversion functions:
0164  *
0165  *  1) A standard strtoul() function that supports 64-bit integers, base
0166  *     8/10/16, with integer overflow support. This is used mainly by the
0167  *     iASL compiler, which implements tighter constraints on integer
0168  *     constants than the runtime (interpreter) integer-to-string conversions.
0169  *  2) Runtime "Explicit conversion" as defined in the ACPI specification.
0170  *  3) Runtime "Implicit conversion" as defined in the ACPI specification.
0171  *
0172  * Current users of this module:
0173  *
0174  *  iASL        - Preprocessor (constants and math expressions)
0175  *  iASL        - Main parser, conversion of constants to integers
0176  *  iASL        - Data Table Compiler parser (constants and math expressions)
0177  *  Interpreter - Implicit and explicit conversions, GPE method names
0178  *  Interpreter - Repair code for return values from predefined names
0179  *  Debugger    - Command line input string conversion
0180  *  AcpiDump    - ACPI table physical addresses
0181  *  AcpiExec    - Support for namespace overrides
0182  *
0183  * Notes concerning users of these interfaces:
0184  *
0185  * AcpiGbl_IntegerByteWidth is used to set the 32/64 bit limit for explicit
0186  * and implicit conversions. This global must be set to the proper width.
0187  * For the core ACPICA code, the width depends on the DSDT version. For the
0188  * AcpiUtStrtoul64 interface, all conversions are 64 bits. This interface is
0189  * used primarily for iASL, where the default width is 64 bits for all parsers,
0190  * but error checking is performed later to flag cases where a 64-bit constant
0191  * is wrongly defined in a 32-bit DSDT/SSDT.
0192  *
0193  * In ACPI, the only place where octal numbers are supported is within
0194  * the ASL language itself. This is implemented via the main AcpiUtStrtoul64
0195  * interface. According the ACPI specification, there is no ACPI runtime
0196  * support (explicit/implicit) for octal string conversions.
0197  *
0198  ******************************************************************************/
0199 
0200 
0201 /*******************************************************************************
0202  *
0203  * FUNCTION:    AcpiUtStrtoul64
0204  *
0205  * PARAMETERS:  String                  - Null terminated input string,
0206  *                                        must be a valid pointer
0207  *              ReturnValue             - Where the converted integer is
0208  *                                        returned. Must be a valid pointer
0209  *
0210  * RETURN:      Status and converted integer. Returns an exception on a
0211  *              64-bit numeric overflow
0212  *
0213  * DESCRIPTION: Convert a string into an unsigned integer. Always performs a
0214  *              full 64-bit conversion, regardless of the current global
0215  *              integer width. Supports Decimal, Hex, and Octal strings.
0216  *
0217  * Current users of this function:
0218  *
0219  *  iASL        - Preprocessor (constants and math expressions)
0220  *  iASL        - Main ASL parser, conversion of ASL constants to integers
0221  *  iASL        - Data Table Compiler parser (constants and math expressions)
0222  *  Interpreter - Repair code for return values from predefined names
0223  *  AcpiDump    - ACPI table physical addresses
0224  *  AcpiExec    - Support for namespace overrides
0225  *
0226  ******************************************************************************/
0227 
0228 ACPI_STATUS
0229 AcpiUtStrtoul64 (
0230     char                    *String,
0231     UINT64                  *ReturnValue)
0232 {
0233     ACPI_STATUS             Status = AE_OK;
0234     UINT8                   OriginalBitWidth;
0235     UINT32                  Base = 10;          /* Default is decimal */
0236 
0237 
0238     ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
0239 
0240 
0241     *ReturnValue = 0;
0242 
0243     /* A NULL return string returns a value of zero */
0244 
0245     if (*String == 0)
0246     {
0247         return_ACPI_STATUS (AE_OK);
0248     }
0249 
0250     if (!AcpiUtRemoveWhitespace (&String))
0251     {
0252         return_ACPI_STATUS (AE_OK);
0253     }
0254 
0255     /*
0256      * 1) Check for a hex constant. A "0x" prefix indicates base 16.
0257      */
0258     if (AcpiUtDetectHexPrefix (&String))
0259     {
0260         Base = 16;
0261     }
0262 
0263     /*
0264      * 2) Check for an octal constant, defined to be a leading zero
0265      * followed by sequence of octal digits (0-7)
0266      */
0267     else if (AcpiUtDetectOctalPrefix (&String))
0268     {
0269         Base = 8;
0270     }
0271 
0272     if (!AcpiUtRemoveLeadingZeros (&String))
0273     {
0274         return_ACPI_STATUS (AE_OK);     /* Return value 0 */
0275     }
0276 
0277     /*
0278      * Force a full 64-bit conversion. The caller (usually iASL) must
0279      * check for a 32-bit overflow later as necessary (If current mode
0280      * is 32-bit, meaning a 32-bit DSDT).
0281      */
0282     OriginalBitWidth = AcpiGbl_IntegerBitWidth;
0283     AcpiGbl_IntegerBitWidth = 64;
0284 
0285     /*
0286      * Perform the base 8, 10, or 16 conversion. A 64-bit numeric overflow
0287      * will return an exception (to allow iASL to flag the statement).
0288      */
0289     switch (Base)
0290     {
0291     case 8:
0292         Status = AcpiUtConvertOctalString (String, ReturnValue);
0293         break;
0294 
0295     case 10:
0296         Status = AcpiUtConvertDecimalString (String, ReturnValue);
0297         break;
0298 
0299     case 16:
0300     default:
0301         Status = AcpiUtConvertHexString (String, ReturnValue);
0302         break;
0303     }
0304 
0305     /* Only possible exception from above is a 64-bit overflow */
0306 
0307     AcpiGbl_IntegerBitWidth = OriginalBitWidth;
0308     return_ACPI_STATUS (Status);
0309 }
0310 
0311 
0312 /*******************************************************************************
0313  *
0314  * FUNCTION:    AcpiUtImplicitStrtoul64
0315  *
0316  * PARAMETERS:  String                  - Null terminated input string,
0317  *                                        must be a valid pointer
0318  *
0319  * RETURN:      Converted integer
0320  *
0321  * DESCRIPTION: Perform a 64-bit conversion with restrictions placed upon
0322  *              an "implicit conversion" by the ACPI specification. Used by
0323  *              many ASL operators that require an integer operand, and support
0324  *              an automatic (implicit) conversion from a string operand
0325  *              to the final integer operand. The major restriction is that
0326  *              only hex strings are supported.
0327  *
0328  * -----------------------------------------------------------------------------
0329  *
0330  * Base is always 16, either with or without the 0x prefix. Decimal and
0331  * Octal strings are not supported, as per the ACPI specification.
0332  *
0333  * Examples (both are hex values):
0334  *      Add ("BA98", Arg0, Local0)
0335  *      Subtract ("0x12345678", Arg1, Local1)
0336  *
0337  * Conversion rules as extracted from the ACPI specification:
0338  *
0339  *  The converted integer is initialized to the value zero.
0340  *  The ASCII string is always interpreted as a hexadecimal constant.
0341  *
0342  *  1)  According to the ACPI specification, a "0x" prefix is not allowed.
0343  *      However, ACPICA allows this as an ACPI extension on general
0344  *      principle. (NO ERROR)
0345  *
0346  *  2)  The conversion terminates when the size of an integer is reached
0347  *      (32 or 64 bits). There are no numeric overflow conditions. (NO ERROR)
0348  *
0349  *  3)  The first non-hex character terminates the conversion and returns
0350  *      the current accumulated value of the converted integer (NO ERROR).
0351  *
0352  *  4)  Conversion of a null (zero-length) string to an integer is
0353  *      technically not allowed. However, ACPICA allows this as an ACPI
0354  *      extension. The conversion returns the value 0. (NO ERROR)
0355  *
0356  * NOTE: There are no error conditions returned by this function. At
0357  * the minimum, a value of zero is returned.
0358  *
0359  * Current users of this function:
0360  *
0361  *  Interpreter - All runtime implicit conversions, as per ACPI specification
0362  *  iASL        - Data Table Compiler parser (constants and math expressions)
0363  *
0364  ******************************************************************************/
0365 
0366 UINT64
0367 AcpiUtImplicitStrtoul64 (
0368     char                    *String)
0369 {
0370     UINT64                  ConvertedInteger = 0;
0371 
0372 
0373     ACPI_FUNCTION_TRACE_STR (UtImplicitStrtoul64, String);
0374 
0375 
0376     if (!AcpiUtRemoveWhitespace (&String))
0377     {
0378         return_VALUE (0);
0379     }
0380 
0381     /*
0382      * Per the ACPI specification, only hexadecimal is supported for
0383      * implicit conversions, and the "0x" prefix is "not allowed".
0384      * However, allow a "0x" prefix as an ACPI extension.
0385      */
0386     AcpiUtRemoveHexPrefix (&String);
0387 
0388     if (!AcpiUtRemoveLeadingZeros (&String))
0389     {
0390         return_VALUE (0);
0391     }
0392 
0393     /*
0394      * Ignore overflow as per the ACPI specification. This is implemented by
0395      * ignoring the return status from the conversion function called below.
0396      * On overflow, the input string is simply truncated.
0397      */
0398     AcpiUtConvertHexString (String, &ConvertedInteger);
0399     return_VALUE (ConvertedInteger);
0400 }
0401 
0402 
0403 /*******************************************************************************
0404  *
0405  * FUNCTION:    AcpiUtExplicitStrtoul64
0406  *
0407  * PARAMETERS:  String                  - Null terminated input string,
0408  *                                        must be a valid pointer
0409  *
0410  * RETURN:      Converted integer
0411  *
0412  * DESCRIPTION: Perform a 64-bit conversion with the restrictions placed upon
0413  *              an "explicit conversion" by the ACPI specification. The
0414  *              main restriction is that only hex and decimal are supported.
0415  *
0416  * -----------------------------------------------------------------------------
0417  *
0418  * Base is either 10 (default) or 16 (with 0x prefix). Octal (base 8) strings
0419  * are not supported, as per the ACPI specification.
0420  *
0421  * Examples:
0422  *      ToInteger ("1000")      Decimal
0423  *      ToInteger ("0xABCD")    Hex
0424  *
0425  * Conversion rules as extracted from the ACPI specification:
0426  *
0427  *  1)  The input string is either a decimal or hexadecimal numeric string.
0428  *      A hex value must be prefixed by "0x" or it is interpreted as decimal.
0429  *
0430  *  2)  The value must not exceed the maximum of an integer value
0431  *      (32 or 64 bits). The ACPI specification states the behavior is
0432  *      "unpredictable", so ACPICA matches the behavior of the implicit
0433  *      conversion case. There are no numeric overflow conditions. (NO ERROR)
0434  *
0435  *  3)  Behavior on the first non-hex character is not defined by the ACPI
0436  *      specification (for the ToInteger operator), so ACPICA matches the
0437  *      behavior of the implicit conversion case. It terminates the
0438  *      conversion and returns the current accumulated value of the converted
0439  *      integer. (NO ERROR)
0440  *
0441  *  4)  Conversion of a null (zero-length) string to an integer is
0442  *      technically not allowed. However, ACPICA allows this as an ACPI
0443  *      extension. The conversion returns the value 0. (NO ERROR)
0444  *
0445  * NOTE: There are no error conditions returned by this function. At the
0446  * minimum, a value of zero is returned.
0447  *
0448  * Current users of this function:
0449  *
0450  *  Interpreter - Runtime ASL ToInteger operator, as per the ACPI specification
0451  *
0452  ******************************************************************************/
0453 
0454 UINT64
0455 AcpiUtExplicitStrtoul64 (
0456     char                    *String)
0457 {
0458     UINT64                  ConvertedInteger = 0;
0459     UINT32                  Base = 10;          /* Default is decimal */
0460 
0461 
0462     ACPI_FUNCTION_TRACE_STR (UtExplicitStrtoul64, String);
0463 
0464 
0465     if (!AcpiUtRemoveWhitespace (&String))
0466     {
0467         return_VALUE (0);
0468     }
0469 
0470     /*
0471      * Only Hex and Decimal are supported, as per the ACPI specification.
0472      * A "0x" prefix indicates hex; otherwise decimal is assumed.
0473      */
0474     if (AcpiUtDetectHexPrefix (&String))
0475     {
0476         Base = 16;
0477     }
0478 
0479     if (!AcpiUtRemoveLeadingZeros (&String))
0480     {
0481         return_VALUE (0);
0482     }
0483 
0484     /*
0485      * Ignore overflow as per the ACPI specification. This is implemented by
0486      * ignoring the return status from the conversion functions called below.
0487      * On overflow, the input string is simply truncated.
0488      */
0489     switch (Base)
0490     {
0491     case 10:
0492     default:
0493         AcpiUtConvertDecimalString (String, &ConvertedInteger);
0494         break;
0495 
0496     case 16:
0497         AcpiUtConvertHexString (String, &ConvertedInteger);
0498         break;
0499     }
0500 
0501     return_VALUE (ConvertedInteger);
0502 }