Home | History | Annotate | Line # | Download | only in utilities
utstrsuppt.c revision 1.1.1.7
      1      1.1  christos /*******************************************************************************
      2      1.1  christos  *
      3      1.1  christos  * Module Name: utstrsuppt - Support functions for string-to-integer conversion
      4      1.1  christos  *
      5      1.1  christos  ******************************************************************************/
      6      1.1  christos 
      7      1.1  christos /*
      8  1.1.1.6  christos  * Copyright (C) 2000 - 2020, Intel Corp.
      9      1.1  christos  * All rights reserved.
     10      1.1  christos  *
     11      1.1  christos  * Redistribution and use in source and binary forms, with or without
     12      1.1  christos  * modification, are permitted provided that the following conditions
     13      1.1  christos  * are met:
     14      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16      1.1  christos  *    without modification.
     17      1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21      1.1  christos  *    binary redistribution.
     22      1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24      1.1  christos  *    from this software without specific prior written permission.
     25      1.1  christos  *
     26      1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27      1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1  christos  * Software Foundation.
     29      1.1  christos  *
     30      1.1  christos  * NO WARRANTY
     31      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1  christos  */
     43      1.1  christos 
     44      1.1  christos #include "acpi.h"
     45      1.1  christos #include "accommon.h"
     46      1.1  christos 
     47      1.1  christos #define _COMPONENT          ACPI_UTILITIES
     48      1.1  christos         ACPI_MODULE_NAME    ("utstrsuppt")
     49      1.1  christos 
     50      1.1  christos 
     51      1.1  christos /* Local prototypes */
     52      1.1  christos 
     53      1.1  christos static ACPI_STATUS
     54      1.1  christos AcpiUtInsertDigit (
     55      1.1  christos     UINT64                  *AccumulatedValue,
     56      1.1  christos     UINT32                  Base,
     57      1.1  christos     int                     AsciiDigit);
     58      1.1  christos 
     59      1.1  christos static ACPI_STATUS
     60      1.1  christos AcpiUtStrtoulMultiply64 (
     61      1.1  christos     UINT64                  Multiplicand,
     62  1.1.1.2  christos     UINT32                  Base,
     63      1.1  christos     UINT64                  *OutProduct);
     64      1.1  christos 
     65      1.1  christos static ACPI_STATUS
     66      1.1  christos AcpiUtStrtoulAdd64 (
     67      1.1  christos     UINT64                  Addend1,
     68  1.1.1.2  christos     UINT32                  Digit,
     69      1.1  christos     UINT64                  *OutSum);
     70      1.1  christos 
     71      1.1  christos 
     72      1.1  christos /*******************************************************************************
     73      1.1  christos  *
     74      1.1  christos  * FUNCTION:    AcpiUtConvertOctalString
     75      1.1  christos  *
     76      1.1  christos  * PARAMETERS:  String                  - Null terminated input string
     77      1.1  christos  *              ReturnValuePtr          - Where the converted value is returned
     78      1.1  christos  *
     79      1.1  christos  * RETURN:      Status and 64-bit converted integer
     80      1.1  christos  *
     81      1.1  christos  * DESCRIPTION: Performs a base 8 conversion of the input string to an
     82      1.1  christos  *              integer value, either 32 or 64 bits.
     83      1.1  christos  *
     84      1.1  christos  * NOTE:        Maximum 64-bit unsigned octal value is 01777777777777777777777
     85      1.1  christos  *              Maximum 32-bit unsigned octal value is 037777777777
     86      1.1  christos  *
     87      1.1  christos  ******************************************************************************/
     88      1.1  christos 
     89      1.1  christos ACPI_STATUS
     90      1.1  christos AcpiUtConvertOctalString (
     91      1.1  christos     char                    *String,
     92      1.1  christos     UINT64                  *ReturnValuePtr)
     93      1.1  christos {
     94      1.1  christos     UINT64                  AccumulatedValue = 0;
     95      1.1  christos     ACPI_STATUS             Status = AE_OK;
     96      1.1  christos 
     97      1.1  christos 
     98      1.1  christos     /* Convert each ASCII byte in the input string */
     99      1.1  christos 
    100      1.1  christos     while (*String)
    101      1.1  christos     {
    102  1.1.1.7  christos         /*
    103  1.1.1.7  christos          * Character must be ASCII 0-7, otherwise:
    104  1.1.1.7  christos          * 1) Runtime: terminate with no error, per the ACPI spec
    105  1.1.1.7  christos          * 2) Compiler: return an error
    106  1.1.1.7  christos          */
    107      1.1  christos         if (!(ACPI_IS_OCTAL_DIGIT (*String)))
    108      1.1  christos         {
    109  1.1.1.7  christos #ifdef ACPI_ASL_COMPILER
    110  1.1.1.7  christos             Status = AE_BAD_OCTAL_CONSTANT;
    111  1.1.1.7  christos #endif
    112      1.1  christos             break;
    113      1.1  christos         }
    114      1.1  christos 
    115      1.1  christos         /* Convert and insert this octal digit into the accumulator */
    116      1.1  christos 
    117      1.1  christos         Status = AcpiUtInsertDigit (&AccumulatedValue, 8, *String);
    118      1.1  christos         if (ACPI_FAILURE (Status))
    119      1.1  christos         {
    120      1.1  christos             Status = AE_OCTAL_OVERFLOW;
    121      1.1  christos             break;
    122      1.1  christos         }
    123      1.1  christos 
    124      1.1  christos         String++;
    125      1.1  christos     }
    126      1.1  christos 
    127      1.1  christos     /* Always return the value that has been accumulated */
    128      1.1  christos 
    129      1.1  christos     *ReturnValuePtr = AccumulatedValue;
    130      1.1  christos     return (Status);
    131      1.1  christos }
    132      1.1  christos 
    133      1.1  christos 
    134      1.1  christos /*******************************************************************************
    135      1.1  christos  *
    136      1.1  christos  * FUNCTION:    AcpiUtConvertDecimalString
    137      1.1  christos  *
    138      1.1  christos  * PARAMETERS:  String                  - Null terminated input string
    139      1.1  christos  *              ReturnValuePtr          - Where the converted value is returned
    140      1.1  christos  *
    141      1.1  christos  * RETURN:      Status and 64-bit converted integer
    142      1.1  christos  *
    143      1.1  christos  * DESCRIPTION: Performs a base 10 conversion of the input string to an
    144      1.1  christos  *              integer value, either 32 or 64 bits.
    145      1.1  christos  *
    146      1.1  christos  * NOTE:        Maximum 64-bit unsigned decimal value is 18446744073709551615
    147      1.1  christos  *              Maximum 32-bit unsigned decimal value is 4294967295
    148      1.1  christos  *
    149      1.1  christos  ******************************************************************************/
    150      1.1  christos 
    151      1.1  christos ACPI_STATUS
    152      1.1  christos AcpiUtConvertDecimalString (
    153      1.1  christos     char                    *String,
    154      1.1  christos     UINT64                  *ReturnValuePtr)
    155      1.1  christos {
    156      1.1  christos     UINT64                  AccumulatedValue = 0;
    157      1.1  christos     ACPI_STATUS             Status = AE_OK;
    158      1.1  christos 
    159      1.1  christos 
    160      1.1  christos     /* Convert each ASCII byte in the input string */
    161      1.1  christos 
    162      1.1  christos     while (*String)
    163      1.1  christos     {
    164  1.1.1.7  christos         /*
    165  1.1.1.7  christos          * Character must be ASCII 0-9, otherwise:
    166  1.1.1.7  christos          * 1) Runtime: terminate with no error, per the ACPI spec
    167  1.1.1.7  christos          * 2) Compiler: return an error
    168  1.1.1.7  christos          */
    169      1.1  christos         if (!isdigit (*String))
    170      1.1  christos         {
    171  1.1.1.7  christos #ifdef ACPI_ASL_COMPILER
    172  1.1.1.7  christos             Status = AE_BAD_DECIMAL_CONSTANT;
    173  1.1.1.7  christos #endif
    174      1.1  christos            break;
    175      1.1  christos         }
    176      1.1  christos 
    177      1.1  christos         /* Convert and insert this decimal digit into the accumulator */
    178      1.1  christos 
    179      1.1  christos         Status = AcpiUtInsertDigit (&AccumulatedValue, 10, *String);
    180      1.1  christos         if (ACPI_FAILURE (Status))
    181      1.1  christos         {
    182      1.1  christos             Status = AE_DECIMAL_OVERFLOW;
    183      1.1  christos             break;
    184      1.1  christos         }
    185      1.1  christos 
    186      1.1  christos         String++;
    187      1.1  christos     }
    188      1.1  christos 
    189      1.1  christos     /* Always return the value that has been accumulated */
    190      1.1  christos 
    191      1.1  christos     *ReturnValuePtr = AccumulatedValue;
    192      1.1  christos     return (Status);
    193      1.1  christos }
    194      1.1  christos 
    195      1.1  christos 
    196      1.1  christos /*******************************************************************************
    197      1.1  christos  *
    198      1.1  christos  * FUNCTION:    AcpiUtConvertHexString
    199      1.1  christos  *
    200      1.1  christos  * PARAMETERS:  String                  - Null terminated input string
    201      1.1  christos  *              ReturnValuePtr          - Where the converted value is returned
    202      1.1  christos  *
    203      1.1  christos  * RETURN:      Status and 64-bit converted integer
    204      1.1  christos  *
    205      1.1  christos  * DESCRIPTION: Performs a base 16 conversion of the input string to an
    206      1.1  christos  *              integer value, either 32 or 64 bits.
    207      1.1  christos  *
    208      1.1  christos  * NOTE:        Maximum 64-bit unsigned hex value is 0xFFFFFFFFFFFFFFFF
    209      1.1  christos  *              Maximum 32-bit unsigned hex value is 0xFFFFFFFF
    210      1.1  christos  *
    211      1.1  christos  ******************************************************************************/
    212      1.1  christos 
    213      1.1  christos ACPI_STATUS
    214      1.1  christos AcpiUtConvertHexString (
    215      1.1  christos     char                    *String,
    216      1.1  christos     UINT64                  *ReturnValuePtr)
    217      1.1  christos {
    218      1.1  christos     UINT64                  AccumulatedValue = 0;
    219      1.1  christos     ACPI_STATUS             Status = AE_OK;
    220      1.1  christos 
    221      1.1  christos 
    222      1.1  christos     /* Convert each ASCII byte in the input string */
    223      1.1  christos 
    224      1.1  christos     while (*String)
    225      1.1  christos     {
    226  1.1.1.7  christos         /*
    227  1.1.1.7  christos          * Character must be ASCII A-F, a-f, or 0-9, otherwise:
    228  1.1.1.7  christos          * 1) Runtime: terminate with no error, per the ACPI spec
    229  1.1.1.7  christos          * 2) Compiler: return an error
    230  1.1.1.7  christos          */
    231      1.1  christos         if (!isxdigit (*String))
    232      1.1  christos         {
    233  1.1.1.7  christos #ifdef ACPI_ASL_COMPILER
    234  1.1.1.7  christos             Status = AE_BAD_HEX_CONSTANT;
    235  1.1.1.7  christos #endif
    236      1.1  christos             break;
    237      1.1  christos         }
    238      1.1  christos 
    239      1.1  christos         /* Convert and insert this hex digit into the accumulator */
    240      1.1  christos 
    241      1.1  christos         Status = AcpiUtInsertDigit (&AccumulatedValue, 16, *String);
    242      1.1  christos         if (ACPI_FAILURE (Status))
    243      1.1  christos         {
    244      1.1  christos             Status = AE_HEX_OVERFLOW;
    245      1.1  christos             break;
    246      1.1  christos         }
    247      1.1  christos 
    248      1.1  christos         String++;
    249      1.1  christos     }
    250      1.1  christos 
    251      1.1  christos     /* Always return the value that has been accumulated */
    252      1.1  christos 
    253      1.1  christos     *ReturnValuePtr = AccumulatedValue;
    254      1.1  christos     return (Status);
    255      1.1  christos }
    256      1.1  christos 
    257      1.1  christos 
    258      1.1  christos /*******************************************************************************
    259      1.1  christos  *
    260      1.1  christos  * FUNCTION:    AcpiUtRemoveLeadingZeros
    261      1.1  christos  *
    262      1.1  christos  * PARAMETERS:  String                  - Pointer to input ASCII string
    263      1.1  christos  *
    264      1.1  christos  * RETURN:      Next character after any leading zeros. This character may be
    265      1.1  christos  *              used by the caller to detect end-of-string.
    266      1.1  christos  *
    267      1.1  christos  * DESCRIPTION: Remove any leading zeros in the input string. Return the
    268      1.1  christos  *              next character after the final ASCII zero to enable the caller
    269      1.1  christos  *              to check for the end of the string (NULL terminator).
    270      1.1  christos  *
    271      1.1  christos  ******************************************************************************/
    272      1.1  christos 
    273      1.1  christos char
    274      1.1  christos AcpiUtRemoveLeadingZeros (
    275      1.1  christos     char                    **String)
    276      1.1  christos {
    277      1.1  christos 
    278      1.1  christos     while (**String == ACPI_ASCII_ZERO)
    279      1.1  christos     {
    280      1.1  christos         *String += 1;
    281      1.1  christos     }
    282      1.1  christos 
    283      1.1  christos     return (**String);
    284      1.1  christos }
    285      1.1  christos 
    286      1.1  christos 
    287      1.1  christos /*******************************************************************************
    288      1.1  christos  *
    289      1.1  christos  * FUNCTION:    AcpiUtRemoveWhitespace
    290      1.1  christos  *
    291      1.1  christos  * PARAMETERS:  String                  - Pointer to input ASCII string
    292      1.1  christos  *
    293      1.1  christos  * RETURN:      Next character after any whitespace. This character may be
    294      1.1  christos  *              used by the caller to detect end-of-string.
    295      1.1  christos  *
    296      1.1  christos  * DESCRIPTION: Remove any leading whitespace in the input string. Return the
    297      1.1  christos  *              next character after the final ASCII zero to enable the caller
    298      1.1  christos  *              to check for the end of the string (NULL terminator).
    299      1.1  christos  *
    300      1.1  christos  ******************************************************************************/
    301      1.1  christos 
    302      1.1  christos char
    303      1.1  christos AcpiUtRemoveWhitespace (
    304      1.1  christos     char                    **String)
    305      1.1  christos {
    306      1.1  christos 
    307      1.1  christos     while (isspace ((UINT8) **String))
    308      1.1  christos     {
    309      1.1  christos         *String += 1;
    310      1.1  christos     }
    311      1.1  christos 
    312      1.1  christos     return (**String);
    313      1.1  christos }
    314      1.1  christos 
    315      1.1  christos 
    316      1.1  christos /*******************************************************************************
    317      1.1  christos  *
    318      1.1  christos  * FUNCTION:    AcpiUtDetectHexPrefix
    319      1.1  christos  *
    320      1.1  christos  * PARAMETERS:  String                  - Pointer to input ASCII string
    321      1.1  christos  *
    322      1.1  christos  * RETURN:      TRUE if a "0x" prefix was found at the start of the string
    323      1.1  christos  *
    324      1.1  christos  * DESCRIPTION: Detect and remove a hex "0x" prefix
    325      1.1  christos  *
    326      1.1  christos  ******************************************************************************/
    327      1.1  christos 
    328      1.1  christos BOOLEAN
    329      1.1  christos AcpiUtDetectHexPrefix (
    330      1.1  christos     char                    **String)
    331      1.1  christos {
    332  1.1.1.4  christos     char                    *InitialPosition = *String;
    333      1.1  christos 
    334  1.1.1.4  christos     AcpiUtRemoveHexPrefix (String);
    335  1.1.1.4  christos     if (*String != InitialPosition)
    336  1.1.1.4  christos     {
    337  1.1.1.4  christos         return (TRUE); /* String is past leading 0x */
    338  1.1.1.4  christos     }
    339  1.1.1.4  christos 
    340  1.1.1.4  christos     return (FALSE);     /* Not a hex string */
    341  1.1.1.4  christos }
    342  1.1.1.4  christos 
    343  1.1.1.4  christos 
    344  1.1.1.4  christos /*******************************************************************************
    345  1.1.1.4  christos  *
    346  1.1.1.4  christos  * FUNCTION:    AcpiUtRemoveHexPrefix
    347  1.1.1.4  christos  *
    348  1.1.1.4  christos  * PARAMETERS:  String                  - Pointer to input ASCII string
    349  1.1.1.4  christos  *
    350  1.1.1.4  christos  * RETURN:      none
    351  1.1.1.4  christos  *
    352  1.1.1.4  christos  * DESCRIPTION: Remove a hex "0x" prefix
    353  1.1.1.4  christos  *
    354  1.1.1.4  christos  ******************************************************************************/
    355  1.1.1.4  christos 
    356  1.1.1.4  christos void
    357  1.1.1.4  christos AcpiUtRemoveHexPrefix (
    358  1.1.1.4  christos     char                    **String)
    359  1.1.1.4  christos {
    360      1.1  christos     if ((**String == ACPI_ASCII_ZERO) &&
    361      1.1  christos         (tolower ((int) *(*String + 1)) == 'x'))
    362      1.1  christos     {
    363      1.1  christos         *String += 2;        /* Go past the leading 0x */
    364      1.1  christos     }
    365      1.1  christos }
    366      1.1  christos 
    367      1.1  christos 
    368      1.1  christos /*******************************************************************************
    369      1.1  christos  *
    370      1.1  christos  * FUNCTION:    AcpiUtDetectOctalPrefix
    371      1.1  christos  *
    372      1.1  christos  * PARAMETERS:  String                  - Pointer to input ASCII string
    373      1.1  christos  *
    374      1.1  christos  * RETURN:      True if an octal "0" prefix was found at the start of the
    375      1.1  christos  *              string
    376      1.1  christos  *
    377      1.1  christos  * DESCRIPTION: Detect and remove an octal prefix (zero)
    378      1.1  christos  *
    379      1.1  christos  ******************************************************************************/
    380      1.1  christos 
    381      1.1  christos BOOLEAN
    382      1.1  christos AcpiUtDetectOctalPrefix (
    383      1.1  christos     char                    **String)
    384      1.1  christos {
    385      1.1  christos 
    386      1.1  christos     if (**String == ACPI_ASCII_ZERO)
    387      1.1  christos     {
    388      1.1  christos         *String += 1;       /* Go past the leading 0 */
    389      1.1  christos         return (TRUE);
    390      1.1  christos     }
    391      1.1  christos 
    392      1.1  christos     return (FALSE);     /* Not an octal string */
    393      1.1  christos }
    394      1.1  christos 
    395      1.1  christos 
    396      1.1  christos /*******************************************************************************
    397      1.1  christos  *
    398      1.1  christos  * FUNCTION:    AcpiUtInsertDigit
    399      1.1  christos  *
    400      1.1  christos  * PARAMETERS:  AccumulatedValue        - Current value of the integer value
    401      1.1  christos  *                                        accumulator. The new value is
    402      1.1  christos  *                                        returned here.
    403      1.1  christos  *              Base                    - Radix, either 8/10/16
    404      1.1  christos  *              AsciiDigit              - ASCII single digit to be inserted
    405      1.1  christos  *
    406      1.1  christos  * RETURN:      Status and result of the convert/insert operation. The only
    407      1.1  christos  *              possible returned exception code is numeric overflow of
    408      1.1  christos  *              either the multiply or add conversion operations.
    409      1.1  christos  *
    410      1.1  christos  * DESCRIPTION: Generic conversion and insertion function for all bases:
    411      1.1  christos  *
    412      1.1  christos  *              1) Multiply the current accumulated/converted value by the
    413      1.1  christos  *              base in order to make room for the new character.
    414      1.1  christos  *
    415      1.1  christos  *              2) Convert the new character to binary and add it to the
    416      1.1  christos  *              current accumulated value.
    417      1.1  christos  *
    418      1.1  christos  *              Note: The only possible exception indicates an integer
    419      1.1  christos  *              overflow (AE_NUMERIC_OVERFLOW)
    420      1.1  christos  *
    421      1.1  christos  ******************************************************************************/
    422      1.1  christos 
    423      1.1  christos static ACPI_STATUS
    424      1.1  christos AcpiUtInsertDigit (
    425      1.1  christos     UINT64                  *AccumulatedValue,
    426      1.1  christos     UINT32                  Base,
    427      1.1  christos     int                     AsciiDigit)
    428      1.1  christos {
    429      1.1  christos     ACPI_STATUS             Status;
    430      1.1  christos     UINT64                  Product;
    431      1.1  christos 
    432      1.1  christos 
    433      1.1  christos      /* Make room in the accumulated value for the incoming digit */
    434      1.1  christos 
    435      1.1  christos     Status = AcpiUtStrtoulMultiply64 (*AccumulatedValue, Base, &Product);
    436      1.1  christos     if (ACPI_FAILURE (Status))
    437      1.1  christos     {
    438      1.1  christos         return (Status);
    439      1.1  christos     }
    440      1.1  christos 
    441      1.1  christos     /* Add in the new digit, and store the sum to the accumulated value */
    442      1.1  christos 
    443      1.1  christos     Status = AcpiUtStrtoulAdd64 (Product, AcpiUtAsciiCharToHex (AsciiDigit),
    444      1.1  christos         AccumulatedValue);
    445      1.1  christos 
    446      1.1  christos     return (Status);
    447      1.1  christos }
    448      1.1  christos 
    449      1.1  christos 
    450      1.1  christos /*******************************************************************************
    451      1.1  christos  *
    452      1.1  christos  * FUNCTION:    AcpiUtStrtoulMultiply64
    453      1.1  christos  *
    454      1.1  christos  * PARAMETERS:  Multiplicand            - Current accumulated converted integer
    455  1.1.1.2  christos  *              Base                    - Base/Radix
    456      1.1  christos  *              OutProduct              - Where the product is returned
    457      1.1  christos  *
    458      1.1  christos  * RETURN:      Status and 64-bit product
    459      1.1  christos  *
    460      1.1  christos  * DESCRIPTION: Multiply two 64-bit values, with checking for 64-bit overflow as
    461      1.1  christos  *              well as 32-bit overflow if necessary (if the current global
    462      1.1  christos  *              integer width is 32).
    463      1.1  christos  *
    464      1.1  christos  ******************************************************************************/
    465      1.1  christos 
    466      1.1  christos static ACPI_STATUS
    467      1.1  christos AcpiUtStrtoulMultiply64 (
    468      1.1  christos     UINT64                  Multiplicand,
    469  1.1.1.2  christos     UINT32                  Base,
    470      1.1  christos     UINT64                  *OutProduct)
    471      1.1  christos {
    472      1.1  christos     UINT64                  Product;
    473  1.1.1.2  christos     UINT64                  Quotient;
    474      1.1  christos 
    475      1.1  christos 
    476      1.1  christos     /* Exit if either operand is zero */
    477      1.1  christos 
    478      1.1  christos     *OutProduct = 0;
    479  1.1.1.2  christos     if (!Multiplicand || !Base)
    480      1.1  christos     {
    481      1.1  christos         return (AE_OK);
    482      1.1  christos     }
    483      1.1  christos 
    484  1.1.1.2  christos     /*
    485  1.1.1.2  christos      * Check for 64-bit overflow before the actual multiplication.
    486  1.1.1.2  christos      *
    487  1.1.1.2  christos      * Notes: 64-bit division is often not supported on 32-bit platforms
    488  1.1.1.2  christos      * (it requires a library function), Therefore ACPICA has a local
    489  1.1.1.2  christos      * 64-bit divide function. Also, Multiplier is currently only used
    490  1.1.1.2  christos      * as the radix (8/10/16), to the 64/32 divide will always work.
    491  1.1.1.2  christos      */
    492  1.1.1.2  christos     AcpiUtShortDivide (ACPI_UINT64_MAX, Base, &Quotient, NULL);
    493  1.1.1.2  christos     if (Multiplicand > Quotient)
    494      1.1  christos     {
    495      1.1  christos         return (AE_NUMERIC_OVERFLOW);
    496      1.1  christos     }
    497      1.1  christos 
    498  1.1.1.2  christos     Product = Multiplicand * Base;
    499      1.1  christos 
    500      1.1  christos     /* Check for 32-bit overflow if necessary */
    501      1.1  christos 
    502      1.1  christos     if ((AcpiGbl_IntegerBitWidth == 32) && (Product > ACPI_UINT32_MAX))
    503      1.1  christos     {
    504      1.1  christos         return (AE_NUMERIC_OVERFLOW);
    505      1.1  christos     }
    506      1.1  christos 
    507      1.1  christos     *OutProduct = Product;
    508      1.1  christos     return (AE_OK);
    509      1.1  christos }
    510      1.1  christos 
    511      1.1  christos 
    512      1.1  christos /*******************************************************************************
    513      1.1  christos  *
    514      1.1  christos  * FUNCTION:    AcpiUtStrtoulAdd64
    515      1.1  christos  *
    516      1.1  christos  * PARAMETERS:  Addend1                 - Current accumulated converted integer
    517  1.1.1.2  christos  *              Digit                   - New hex value/char
    518      1.1  christos  *              OutSum                  - Where sum is returned (Accumulator)
    519      1.1  christos  *
    520      1.1  christos  * RETURN:      Status and 64-bit sum
    521      1.1  christos  *
    522      1.1  christos  * DESCRIPTION: Add two 64-bit values, with checking for 64-bit overflow as
    523      1.1  christos  *              well as 32-bit overflow if necessary (if the current global
    524      1.1  christos  *              integer width is 32).
    525      1.1  christos  *
    526      1.1  christos  ******************************************************************************/
    527      1.1  christos 
    528      1.1  christos static ACPI_STATUS
    529      1.1  christos AcpiUtStrtoulAdd64 (
    530      1.1  christos     UINT64                  Addend1,
    531  1.1.1.2  christos     UINT32                  Digit,
    532      1.1  christos     UINT64                  *OutSum)
    533      1.1  christos {
    534      1.1  christos     UINT64                  Sum;
    535      1.1  christos 
    536      1.1  christos 
    537      1.1  christos     /* Check for 64-bit overflow before the actual addition */
    538      1.1  christos 
    539  1.1.1.2  christos     if ((Addend1 > 0) && (Digit > (ACPI_UINT64_MAX - Addend1)))
    540      1.1  christos     {
    541      1.1  christos         return (AE_NUMERIC_OVERFLOW);
    542      1.1  christos     }
    543      1.1  christos 
    544  1.1.1.2  christos     Sum = Addend1 + Digit;
    545      1.1  christos 
    546      1.1  christos     /* Check for 32-bit overflow if necessary */
    547      1.1  christos 
    548      1.1  christos     if ((AcpiGbl_IntegerBitWidth == 32) && (Sum > ACPI_UINT32_MAX))
    549      1.1  christos     {
    550      1.1  christos         return (AE_NUMERIC_OVERFLOW);
    551      1.1  christos     }
    552      1.1  christos 
    553      1.1  christos     *OutSum = Sum;
    554      1.1  christos     return (AE_OK);
    555      1.1  christos }
    556