Home | History | Annotate | Line # | Download | only in utilities
utnonansi.c revision 1.1.1.2
      1      1.1  christos /*******************************************************************************
      2      1.1  christos  *
      3      1.1  christos  * Module Name: utnonansi - Non-ansi C library functions
      4      1.1  christos  *
      5      1.1  christos  ******************************************************************************/
      6      1.1  christos 
      7      1.1  christos /*
      8  1.1.1.2  christos  * Copyright (C) 2000 - 2016, 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 
     48      1.1  christos #define _COMPONENT          ACPI_UTILITIES
     49      1.1  christos         ACPI_MODULE_NAME    ("utnonansi")
     50      1.1  christos 
     51      1.1  christos 
     52      1.1  christos /*
     53      1.1  christos  * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
     54      1.1  christos  * version of strtoul.
     55      1.1  christos  */
     56      1.1  christos 
     57      1.1  christos /*******************************************************************************
     58      1.1  christos  *
     59      1.1  christos  * FUNCTION:    AcpiUtStrlwr (strlwr)
     60      1.1  christos  *
     61      1.1  christos  * PARAMETERS:  SrcString       - The source string to convert
     62      1.1  christos  *
     63      1.1  christos  * RETURN:      None
     64      1.1  christos  *
     65      1.1  christos  * DESCRIPTION: Convert a string to lowercase
     66      1.1  christos  *
     67      1.1  christos  ******************************************************************************/
     68      1.1  christos 
     69      1.1  christos void
     70      1.1  christos AcpiUtStrlwr (
     71      1.1  christos     char                    *SrcString)
     72      1.1  christos {
     73      1.1  christos     char                    *String;
     74      1.1  christos 
     75      1.1  christos 
     76      1.1  christos     ACPI_FUNCTION_ENTRY ();
     77      1.1  christos 
     78      1.1  christos 
     79      1.1  christos     if (!SrcString)
     80      1.1  christos     {
     81      1.1  christos         return;
     82      1.1  christos     }
     83      1.1  christos 
     84      1.1  christos     /* Walk entire string, lowercasing the letters */
     85      1.1  christos 
     86      1.1  christos     for (String = SrcString; *String; String++)
     87      1.1  christos     {
     88      1.1  christos         *String = (char) tolower ((int) *String);
     89      1.1  christos     }
     90      1.1  christos }
     91      1.1  christos 
     92      1.1  christos 
     93      1.1  christos /*******************************************************************************
     94      1.1  christos  *
     95      1.1  christos  * FUNCTION:    AcpiUtStrupr (strupr)
     96      1.1  christos  *
     97      1.1  christos  * PARAMETERS:  SrcString       - The source string to convert
     98      1.1  christos  *
     99      1.1  christos  * RETURN:      None
    100      1.1  christos  *
    101      1.1  christos  * DESCRIPTION: Convert a string to uppercase
    102      1.1  christos  *
    103      1.1  christos  ******************************************************************************/
    104      1.1  christos 
    105      1.1  christos void
    106      1.1  christos AcpiUtStrupr (
    107      1.1  christos     char                    *SrcString)
    108      1.1  christos {
    109      1.1  christos     char                    *String;
    110      1.1  christos 
    111      1.1  christos 
    112      1.1  christos     ACPI_FUNCTION_ENTRY ();
    113      1.1  christos 
    114      1.1  christos 
    115      1.1  christos     if (!SrcString)
    116      1.1  christos     {
    117      1.1  christos         return;
    118      1.1  christos     }
    119      1.1  christos 
    120      1.1  christos     /* Walk entire string, uppercasing the letters */
    121      1.1  christos 
    122      1.1  christos     for (String = SrcString; *String; String++)
    123      1.1  christos     {
    124      1.1  christos         *String = (char) toupper ((int) *String);
    125      1.1  christos     }
    126      1.1  christos }
    127      1.1  christos 
    128      1.1  christos 
    129      1.1  christos /******************************************************************************
    130      1.1  christos  *
    131      1.1  christos  * FUNCTION:    AcpiUtStricmp (stricmp)
    132      1.1  christos  *
    133      1.1  christos  * PARAMETERS:  String1             - first string to compare
    134      1.1  christos  *              String2             - second string to compare
    135      1.1  christos  *
    136      1.1  christos  * RETURN:      int that signifies string relationship. Zero means strings
    137      1.1  christos  *              are equal.
    138      1.1  christos  *
    139      1.1  christos  * DESCRIPTION: Case-insensitive string compare. Implementation of the
    140      1.1  christos  *              non-ANSI stricmp function.
    141      1.1  christos  *
    142      1.1  christos  ******************************************************************************/
    143      1.1  christos 
    144      1.1  christos int
    145      1.1  christos AcpiUtStricmp (
    146      1.1  christos     char                    *String1,
    147      1.1  christos     char                    *String2)
    148      1.1  christos {
    149      1.1  christos     int                     c1;
    150      1.1  christos     int                     c2;
    151      1.1  christos 
    152      1.1  christos 
    153      1.1  christos     do
    154      1.1  christos     {
    155      1.1  christos         c1 = tolower ((int) *String1);
    156      1.1  christos         c2 = tolower ((int) *String2);
    157      1.1  christos 
    158      1.1  christos         String1++;
    159      1.1  christos         String2++;
    160      1.1  christos     }
    161      1.1  christos     while ((c1 == c2) && (c1));
    162      1.1  christos 
    163      1.1  christos     return (c1 - c2);
    164      1.1  christos }
    165      1.1  christos 
    166      1.1  christos 
    167      1.1  christos /*******************************************************************************
    168      1.1  christos  *
    169      1.1  christos  * FUNCTION:    AcpiUtStrtoul64
    170      1.1  christos  *
    171      1.1  christos  * PARAMETERS:  String          - Null terminated string
    172      1.1  christos  *              Base            - Radix of the string: 16 or ACPI_ANY_BASE;
    173      1.1  christos  *                                ACPI_ANY_BASE means 'in behalf of ToInteger'
    174      1.1  christos  *              RetInteger      - Where the converted integer is returned
    175      1.1  christos  *
    176      1.1  christos  * RETURN:      Status and Converted value
    177      1.1  christos  *
    178      1.1  christos  * DESCRIPTION: Convert a string into an unsigned value. Performs either a
    179      1.1  christos  *              32-bit or 64-bit conversion, depending on the current mode
    180      1.1  christos  *              of the interpreter.
    181      1.1  christos  *
    182      1.1  christos  * NOTE:        Does not support Octal strings, not needed.
    183      1.1  christos  *
    184      1.1  christos  ******************************************************************************/
    185      1.1  christos 
    186      1.1  christos ACPI_STATUS
    187      1.1  christos AcpiUtStrtoul64 (
    188      1.1  christos     char                    *String,
    189      1.1  christos     UINT32                  Base,
    190      1.1  christos     UINT64                  *RetInteger)
    191      1.1  christos {
    192      1.1  christos     UINT32                  ThisDigit = 0;
    193      1.1  christos     UINT64                  ReturnValue = 0;
    194      1.1  christos     UINT64                  Quotient;
    195      1.1  christos     UINT64                  Dividend;
    196      1.1  christos     UINT32                  ToIntegerOp = (Base == ACPI_ANY_BASE);
    197      1.1  christos     UINT32                  Mode32 = (AcpiGbl_IntegerByteWidth == 4);
    198      1.1  christos     UINT8                   ValidDigits = 0;
    199      1.1  christos     UINT8                   SignOf0x = 0;
    200      1.1  christos     UINT8                   Term = 0;
    201      1.1  christos 
    202      1.1  christos 
    203      1.1  christos     ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
    204      1.1  christos 
    205      1.1  christos 
    206      1.1  christos     switch (Base)
    207      1.1  christos     {
    208      1.1  christos     case ACPI_ANY_BASE:
    209      1.1  christos     case 16:
    210      1.1  christos 
    211      1.1  christos         break;
    212      1.1  christos 
    213      1.1  christos     default:
    214      1.1  christos 
    215      1.1  christos         /* Invalid Base */
    216      1.1  christos 
    217      1.1  christos         return_ACPI_STATUS (AE_BAD_PARAMETER);
    218      1.1  christos     }
    219      1.1  christos 
    220      1.1  christos     if (!String)
    221      1.1  christos     {
    222      1.1  christos         goto ErrorExit;
    223      1.1  christos     }
    224      1.1  christos 
    225      1.1  christos     /* Skip over any white space in the buffer */
    226      1.1  christos 
    227      1.1  christos     while ((*String) && (isspace ((int) *String) || *String == '\t'))
    228      1.1  christos     {
    229      1.1  christos         String++;
    230      1.1  christos     }
    231      1.1  christos 
    232      1.1  christos     if (ToIntegerOp)
    233      1.1  christos     {
    234      1.1  christos         /*
    235      1.1  christos          * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
    236      1.1  christos          * We need to determine if it is decimal or hexadecimal.
    237      1.1  christos          */
    238      1.1  christos         if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x'))
    239      1.1  christos         {
    240      1.1  christos             SignOf0x = 1;
    241      1.1  christos             Base = 16;
    242      1.1  christos 
    243      1.1  christos             /* Skip over the leading '0x' */
    244      1.1  christos             String += 2;
    245      1.1  christos         }
    246      1.1  christos         else
    247      1.1  christos         {
    248      1.1  christos             Base = 10;
    249      1.1  christos         }
    250      1.1  christos     }
    251      1.1  christos 
    252      1.1  christos     /* Any string left? Check that '0x' is not followed by white space. */
    253      1.1  christos 
    254      1.1  christos     if (!(*String) || isspace ((int) *String) || *String == '\t')
    255      1.1  christos     {
    256      1.1  christos         if (ToIntegerOp)
    257      1.1  christos         {
    258      1.1  christos             goto ErrorExit;
    259      1.1  christos         }
    260      1.1  christos         else
    261      1.1  christos         {
    262      1.1  christos             goto AllDone;
    263      1.1  christos         }
    264      1.1  christos     }
    265      1.1  christos 
    266      1.1  christos     /*
    267      1.1  christos      * Perform a 32-bit or 64-bit conversion, depending upon the current
    268      1.1  christos      * execution mode of the interpreter
    269      1.1  christos      */
    270      1.1  christos     Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
    271      1.1  christos 
    272      1.1  christos     /* Main loop: convert the string to a 32- or 64-bit integer */
    273      1.1  christos 
    274      1.1  christos     while (*String)
    275      1.1  christos     {
    276      1.1  christos         if (isdigit ((int) *String))
    277      1.1  christos         {
    278      1.1  christos             /* Convert ASCII 0-9 to Decimal value */
    279      1.1  christos 
    280      1.1  christos             ThisDigit = ((UINT8) *String) - '0';
    281      1.1  christos         }
    282      1.1  christos         else if (Base == 10)
    283      1.1  christos         {
    284      1.1  christos             /* Digit is out of range; possible in ToInteger case only */
    285      1.1  christos 
    286      1.1  christos             Term = 1;
    287      1.1  christos         }
    288      1.1  christos         else
    289      1.1  christos         {
    290      1.1  christos             ThisDigit = (UINT8) toupper ((int) *String);
    291      1.1  christos             if (isxdigit ((int) ThisDigit))
    292      1.1  christos             {
    293      1.1  christos                 /* Convert ASCII Hex char to value */
    294      1.1  christos 
    295      1.1  christos                 ThisDigit = ThisDigit - 'A' + 10;
    296      1.1  christos             }
    297      1.1  christos             else
    298      1.1  christos             {
    299      1.1  christos                 Term = 1;
    300      1.1  christos             }
    301      1.1  christos         }
    302      1.1  christos 
    303      1.1  christos         if (Term)
    304      1.1  christos         {
    305      1.1  christos             if (ToIntegerOp)
    306      1.1  christos             {
    307      1.1  christos                 goto ErrorExit;
    308      1.1  christos             }
    309      1.1  christos             else
    310      1.1  christos             {
    311      1.1  christos                 break;
    312      1.1  christos             }
    313      1.1  christos         }
    314      1.1  christos         else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
    315      1.1  christos         {
    316      1.1  christos             /* Skip zeros */
    317      1.1  christos             String++;
    318      1.1  christos             continue;
    319      1.1  christos         }
    320      1.1  christos 
    321      1.1  christos         ValidDigits++;
    322      1.1  christos 
    323      1.1  christos         if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
    324      1.1  christos         {
    325      1.1  christos             /*
    326      1.1  christos              * This is ToInteger operation case.
    327      1.1  christos              * No any restrictions for string-to-integer conversion,
    328      1.1  christos              * see ACPI spec.
    329      1.1  christos              */
    330      1.1  christos             goto ErrorExit;
    331      1.1  christos         }
    332      1.1  christos 
    333      1.1  christos         /* Divide the digit into the correct position */
    334      1.1  christos 
    335  1.1.1.2  christos         (void) AcpiUtShortDivide (
    336  1.1.1.2  christos             (Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL);
    337      1.1  christos 
    338      1.1  christos         if (ReturnValue > Quotient)
    339      1.1  christos         {
    340      1.1  christos             if (ToIntegerOp)
    341      1.1  christos             {
    342      1.1  christos                 goto ErrorExit;
    343      1.1  christos             }
    344      1.1  christos             else
    345      1.1  christos             {
    346      1.1  christos                 break;
    347      1.1  christos             }
    348      1.1  christos         }
    349      1.1  christos 
    350      1.1  christos         ReturnValue *= Base;
    351      1.1  christos         ReturnValue += ThisDigit;
    352      1.1  christos         String++;
    353      1.1  christos     }
    354      1.1  christos 
    355      1.1  christos     /* All done, normal exit */
    356      1.1  christos 
    357      1.1  christos AllDone:
    358      1.1  christos 
    359      1.1  christos     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
    360      1.1  christos         ACPI_FORMAT_UINT64 (ReturnValue)));
    361      1.1  christos 
    362      1.1  christos     *RetInteger = ReturnValue;
    363      1.1  christos     return_ACPI_STATUS (AE_OK);
    364      1.1  christos 
    365      1.1  christos 
    366      1.1  christos ErrorExit:
    367      1.1  christos     /* Base was set/validated above */
    368      1.1  christos 
    369      1.1  christos     if (Base == 10)
    370      1.1  christos     {
    371      1.1  christos         return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
    372      1.1  christos     }
    373      1.1  christos     else
    374      1.1  christos     {
    375      1.1  christos         return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
    376      1.1  christos     }
    377      1.1  christos }
    378      1.1  christos 
    379      1.1  christos 
    380      1.1  christos #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
    381      1.1  christos /*******************************************************************************
    382      1.1  christos  *
    383      1.1  christos  * FUNCTION:    AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
    384      1.1  christos  *
    385      1.1  christos  * PARAMETERS:  Adds a "DestSize" parameter to each of the standard string
    386      1.1  christos  *              functions. This is the size of the Destination buffer.
    387      1.1  christos  *
    388      1.1  christos  * RETURN:      TRUE if the operation would overflow the destination buffer.
    389      1.1  christos  *
    390      1.1  christos  * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
    391      1.1  christos  *              the result of the operation will not overflow the output string
    392      1.1  christos  *              buffer.
    393      1.1  christos  *
    394      1.1  christos  * NOTE:        These functions are typically only helpful for processing
    395      1.1  christos  *              user input and command lines. For most ACPICA code, the
    396      1.1  christos  *              required buffer length is precisely calculated before buffer
    397      1.1  christos  *              allocation, so the use of these functions is unnecessary.
    398      1.1  christos  *
    399      1.1  christos  ******************************************************************************/
    400      1.1  christos 
    401      1.1  christos BOOLEAN
    402      1.1  christos AcpiUtSafeStrcpy (
    403      1.1  christos     char                    *Dest,
    404      1.1  christos     ACPI_SIZE               DestSize,
    405      1.1  christos     char                    *Source)
    406      1.1  christos {
    407      1.1  christos 
    408      1.1  christos     if (strlen (Source) >= DestSize)
    409      1.1  christos     {
    410      1.1  christos         return (TRUE);
    411      1.1  christos     }
    412      1.1  christos 
    413      1.1  christos     strcpy (Dest, Source);
    414      1.1  christos     return (FALSE);
    415      1.1  christos }
    416      1.1  christos 
    417      1.1  christos BOOLEAN
    418      1.1  christos AcpiUtSafeStrcat (
    419      1.1  christos     char                    *Dest,
    420      1.1  christos     ACPI_SIZE               DestSize,
    421      1.1  christos     char                    *Source)
    422      1.1  christos {
    423      1.1  christos 
    424      1.1  christos     if ((strlen (Dest) + strlen (Source)) >= DestSize)
    425      1.1  christos     {
    426      1.1  christos         return (TRUE);
    427      1.1  christos     }
    428      1.1  christos 
    429      1.1  christos     strcat (Dest, Source);
    430      1.1  christos     return (FALSE);
    431      1.1  christos }
    432      1.1  christos 
    433      1.1  christos BOOLEAN
    434      1.1  christos AcpiUtSafeStrncat (
    435      1.1  christos     char                    *Dest,
    436      1.1  christos     ACPI_SIZE               DestSize,
    437      1.1  christos     char                    *Source,
    438      1.1  christos     ACPI_SIZE               MaxTransferLength)
    439      1.1  christos {
    440      1.1  christos     ACPI_SIZE               ActualTransferLength;
    441      1.1  christos 
    442      1.1  christos 
    443      1.1  christos     ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
    444      1.1  christos 
    445      1.1  christos     if ((strlen (Dest) + ActualTransferLength) >= DestSize)
    446      1.1  christos     {
    447      1.1  christos         return (TRUE);
    448      1.1  christos     }
    449      1.1  christos 
    450      1.1  christos     strncat (Dest, Source, MaxTransferLength);
    451      1.1  christos     return (FALSE);
    452      1.1  christos }
    453      1.1  christos #endif
    454