Home | History | Annotate | Line # | Download | only in utilities
utstring.c revision 1.1.1.2.2.3
      1          1.1  christos /*******************************************************************************
      2          1.1  christos  *
      3          1.1  christos  * Module Name: utstring - Common functions for strings and characters
      4          1.1  christos  *
      5          1.1  christos  ******************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8  1.1.1.2.2.3     skrll  * 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 #include "acnamesp.h"
     47          1.1  christos 
     48          1.1  christos 
     49          1.1  christos #define _COMPONENT          ACPI_UTILITIES
     50          1.1  christos         ACPI_MODULE_NAME    ("utstring")
     51          1.1  christos 
     52          1.1  christos 
     53          1.1  christos /*******************************************************************************
     54          1.1  christos  *
     55          1.1  christos  * FUNCTION:    AcpiUtPrintString
     56          1.1  christos  *
     57          1.1  christos  * PARAMETERS:  String          - Null terminated ASCII string
     58          1.1  christos  *              MaxLength       - Maximum output length. Used to constrain the
     59          1.1  christos  *                                length of strings during debug output only.
     60          1.1  christos  *
     61          1.1  christos  * RETURN:      None
     62          1.1  christos  *
     63          1.1  christos  * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
     64          1.1  christos  *              sequences.
     65          1.1  christos  *
     66          1.1  christos  ******************************************************************************/
     67          1.1  christos 
     68          1.1  christos void
     69          1.1  christos AcpiUtPrintString (
     70          1.1  christos     char                    *String,
     71          1.1  christos     UINT16                  MaxLength)
     72          1.1  christos {
     73          1.1  christos     UINT32                  i;
     74          1.1  christos 
     75          1.1  christos 
     76          1.1  christos     if (!String)
     77          1.1  christos     {
     78          1.1  christos         AcpiOsPrintf ("<\"NULL STRING PTR\">");
     79          1.1  christos         return;
     80          1.1  christos     }
     81          1.1  christos 
     82          1.1  christos     AcpiOsPrintf ("\"");
     83      1.1.1.2  christos     for (i = 0; (i < MaxLength) && String[i]; i++)
     84          1.1  christos     {
     85          1.1  christos         /* Escape sequences */
     86          1.1  christos 
     87          1.1  christos         switch (String[i])
     88          1.1  christos         {
     89          1.1  christos         case 0x07:
     90          1.1  christos 
     91          1.1  christos             AcpiOsPrintf ("\\a");       /* BELL */
     92          1.1  christos             break;
     93          1.1  christos 
     94          1.1  christos         case 0x08:
     95          1.1  christos 
     96          1.1  christos             AcpiOsPrintf ("\\b");       /* BACKSPACE */
     97          1.1  christos             break;
     98          1.1  christos 
     99          1.1  christos         case 0x0C:
    100          1.1  christos 
    101          1.1  christos             AcpiOsPrintf ("\\f");       /* FORMFEED */
    102          1.1  christos             break;
    103          1.1  christos 
    104          1.1  christos         case 0x0A:
    105          1.1  christos 
    106          1.1  christos             AcpiOsPrintf ("\\n");       /* LINEFEED */
    107          1.1  christos             break;
    108          1.1  christos 
    109          1.1  christos         case 0x0D:
    110          1.1  christos 
    111          1.1  christos             AcpiOsPrintf ("\\r");       /* CARRIAGE RETURN*/
    112          1.1  christos             break;
    113          1.1  christos 
    114          1.1  christos         case 0x09:
    115          1.1  christos 
    116          1.1  christos             AcpiOsPrintf ("\\t");       /* HORIZONTAL TAB */
    117          1.1  christos             break;
    118          1.1  christos 
    119          1.1  christos         case 0x0B:
    120          1.1  christos 
    121          1.1  christos             AcpiOsPrintf ("\\v");       /* VERTICAL TAB */
    122          1.1  christos             break;
    123          1.1  christos 
    124          1.1  christos         case '\'':                      /* Single Quote */
    125          1.1  christos         case '\"':                      /* Double Quote */
    126          1.1  christos         case '\\':                      /* Backslash */
    127          1.1  christos 
    128          1.1  christos             AcpiOsPrintf ("\\%c", (int) String[i]);
    129          1.1  christos             break;
    130          1.1  christos 
    131          1.1  christos         default:
    132          1.1  christos 
    133          1.1  christos             /* Check for printable character or hex escape */
    134          1.1  christos 
    135  1.1.1.2.2.2     skrll             if (isprint ((int) String[i]))
    136          1.1  christos             {
    137          1.1  christos                 /* This is a normal character */
    138          1.1  christos 
    139          1.1  christos                 AcpiOsPrintf ("%c", (int) String[i]);
    140          1.1  christos             }
    141          1.1  christos             else
    142          1.1  christos             {
    143          1.1  christos                 /* All others will be Hex escapes */
    144          1.1  christos 
    145          1.1  christos                 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
    146          1.1  christos             }
    147          1.1  christos             break;
    148          1.1  christos         }
    149          1.1  christos     }
    150  1.1.1.2.2.3     skrll 
    151          1.1  christos     AcpiOsPrintf ("\"");
    152          1.1  christos 
    153          1.1  christos     if (i == MaxLength && String[i])
    154          1.1  christos     {
    155          1.1  christos         AcpiOsPrintf ("...");
    156          1.1  christos     }
    157          1.1  christos }
    158          1.1  christos 
    159          1.1  christos 
    160          1.1  christos /*******************************************************************************
    161          1.1  christos  *
    162          1.1  christos  * FUNCTION:    AcpiUtValidAcpiChar
    163          1.1  christos  *
    164          1.1  christos  * PARAMETERS:  Char            - The character to be examined
    165          1.1  christos  *              Position        - Byte position (0-3)
    166          1.1  christos  *
    167          1.1  christos  * RETURN:      TRUE if the character is valid, FALSE otherwise
    168          1.1  christos  *
    169          1.1  christos  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
    170          1.1  christos  *              1) Upper case alpha
    171          1.1  christos  *              2) numeric
    172          1.1  christos  *              3) underscore
    173          1.1  christos  *
    174          1.1  christos  *              We allow a '!' as the last character because of the ASF! table
    175          1.1  christos  *
    176          1.1  christos  ******************************************************************************/
    177          1.1  christos 
    178          1.1  christos BOOLEAN
    179          1.1  christos AcpiUtValidAcpiChar (
    180          1.1  christos     char                    Character,
    181          1.1  christos     UINT32                  Position)
    182          1.1  christos {
    183          1.1  christos 
    184          1.1  christos     if (!((Character >= 'A' && Character <= 'Z') ||
    185          1.1  christos           (Character >= '0' && Character <= '9') ||
    186          1.1  christos           (Character == '_')))
    187          1.1  christos     {
    188          1.1  christos         /* Allow a '!' in the last position */
    189          1.1  christos 
    190          1.1  christos         if (Character == '!' && Position == 3)
    191          1.1  christos         {
    192          1.1  christos             return (TRUE);
    193          1.1  christos         }
    194          1.1  christos 
    195          1.1  christos         return (FALSE);
    196          1.1  christos     }
    197          1.1  christos 
    198          1.1  christos     return (TRUE);
    199          1.1  christos }
    200          1.1  christos 
    201          1.1  christos 
    202          1.1  christos /*******************************************************************************
    203          1.1  christos  *
    204          1.1  christos  * FUNCTION:    AcpiUtValidAcpiName
    205          1.1  christos  *
    206          1.1  christos  * PARAMETERS:  Name            - The name to be examined. Does not have to
    207          1.1  christos  *                                be NULL terminated string.
    208          1.1  christos  *
    209          1.1  christos  * RETURN:      TRUE if the name is valid, FALSE otherwise
    210          1.1  christos  *
    211          1.1  christos  * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
    212          1.1  christos  *              1) Upper case alpha
    213          1.1  christos  *              2) numeric
    214          1.1  christos  *              3) underscore
    215          1.1  christos  *
    216          1.1  christos  ******************************************************************************/
    217          1.1  christos 
    218          1.1  christos BOOLEAN
    219          1.1  christos AcpiUtValidAcpiName (
    220          1.1  christos     char                    *Name)
    221          1.1  christos {
    222          1.1  christos     UINT32                  i;
    223          1.1  christos 
    224          1.1  christos 
    225          1.1  christos     ACPI_FUNCTION_ENTRY ();
    226          1.1  christos 
    227          1.1  christos 
    228          1.1  christos     for (i = 0; i < ACPI_NAME_SIZE; i++)
    229          1.1  christos     {
    230          1.1  christos         if (!AcpiUtValidAcpiChar (Name[i], i))
    231          1.1  christos         {
    232          1.1  christos             return (FALSE);
    233          1.1  christos         }
    234          1.1  christos     }
    235          1.1  christos 
    236          1.1  christos     return (TRUE);
    237          1.1  christos }
    238          1.1  christos 
    239          1.1  christos 
    240          1.1  christos /*******************************************************************************
    241          1.1  christos  *
    242          1.1  christos  * FUNCTION:    AcpiUtRepairName
    243          1.1  christos  *
    244          1.1  christos  * PARAMETERS:  Name            - The ACPI name to be repaired
    245          1.1  christos  *
    246          1.1  christos  * RETURN:      Repaired version of the name
    247          1.1  christos  *
    248          1.1  christos  * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
    249          1.1  christos  *              return the new name. NOTE: the Name parameter must reside in
    250          1.1  christos  *              read/write memory, cannot be a const.
    251          1.1  christos  *
    252          1.1  christos  * An ACPI Name must consist of valid ACPI characters. We will repair the name
    253          1.1  christos  * if necessary because we don't want to abort because of this, but we want
    254          1.1  christos  * all namespace names to be printable. A warning message is appropriate.
    255          1.1  christos  *
    256          1.1  christos  * This issue came up because there are in fact machines that exhibit
    257          1.1  christos  * this problem, and we want to be able to enable ACPI support for them,
    258          1.1  christos  * even though there are a few bad names.
    259          1.1  christos  *
    260          1.1  christos  ******************************************************************************/
    261          1.1  christos 
    262          1.1  christos void
    263          1.1  christos AcpiUtRepairName (
    264          1.1  christos     char                    *Name)
    265          1.1  christos {
    266          1.1  christos     UINT32                  i;
    267          1.1  christos     BOOLEAN                 FoundBadChar = FALSE;
    268          1.1  christos     UINT32                  OriginalName;
    269          1.1  christos 
    270          1.1  christos 
    271          1.1  christos     ACPI_FUNCTION_NAME (UtRepairName);
    272          1.1  christos 
    273          1.1  christos 
    274  1.1.1.2.2.3     skrll     /*
    275  1.1.1.2.2.3     skrll      * Special case for the root node. This can happen if we get an
    276  1.1.1.2.2.3     skrll      * error during the execution of module-level code.
    277  1.1.1.2.2.3     skrll      */
    278  1.1.1.2.2.3     skrll     if (ACPI_COMPARE_NAME (Name, "\\___"))
    279  1.1.1.2.2.3     skrll     {
    280  1.1.1.2.2.3     skrll         return;
    281  1.1.1.2.2.3     skrll     }
    282  1.1.1.2.2.3     skrll 
    283          1.1  christos     ACPI_MOVE_NAME (&OriginalName, Name);
    284          1.1  christos 
    285          1.1  christos     /* Check each character in the name */
    286          1.1  christos 
    287          1.1  christos     for (i = 0; i < ACPI_NAME_SIZE; i++)
    288          1.1  christos     {
    289          1.1  christos         if (AcpiUtValidAcpiChar (Name[i], i))
    290          1.1  christos         {
    291          1.1  christos             continue;
    292          1.1  christos         }
    293          1.1  christos 
    294          1.1  christos         /*
    295          1.1  christos          * Replace a bad character with something printable, yet technically
    296          1.1  christos          * still invalid. This prevents any collisions with existing "good"
    297          1.1  christos          * names in the namespace.
    298          1.1  christos          */
    299          1.1  christos         Name[i] = '*';
    300          1.1  christos         FoundBadChar = TRUE;
    301          1.1  christos     }
    302          1.1  christos 
    303          1.1  christos     if (FoundBadChar)
    304          1.1  christos     {
    305          1.1  christos         /* Report warning only if in strict mode or debug mode */
    306          1.1  christos 
    307          1.1  christos         if (!AcpiGbl_EnableInterpreterSlack)
    308          1.1  christos         {
    309          1.1  christos             ACPI_WARNING ((AE_INFO,
    310          1.1  christos                 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
    311          1.1  christos                 OriginalName, Name));
    312          1.1  christos         }
    313          1.1  christos         else
    314          1.1  christos         {
    315          1.1  christos             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    316          1.1  christos                 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
    317          1.1  christos                 OriginalName, Name));
    318          1.1  christos         }
    319          1.1  christos     }
    320          1.1  christos }
    321          1.1  christos 
    322          1.1  christos 
    323          1.1  christos #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
    324          1.1  christos /*******************************************************************************
    325          1.1  christos  *
    326          1.1  christos  * FUNCTION:    UtConvertBackslashes
    327          1.1  christos  *
    328          1.1  christos  * PARAMETERS:  Pathname        - File pathname string to be converted
    329          1.1  christos  *
    330          1.1  christos  * RETURN:      Modifies the input Pathname
    331          1.1  christos  *
    332          1.1  christos  * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
    333          1.1  christos  *              the entire input file pathname string.
    334          1.1  christos  *
    335          1.1  christos  ******************************************************************************/
    336          1.1  christos 
    337          1.1  christos void
    338          1.1  christos UtConvertBackslashes (
    339          1.1  christos     char                    *Pathname)
    340          1.1  christos {
    341          1.1  christos 
    342          1.1  christos     if (!Pathname)
    343          1.1  christos     {
    344          1.1  christos         return;
    345          1.1  christos     }
    346          1.1  christos 
    347          1.1  christos     while (*Pathname)
    348          1.1  christos     {
    349          1.1  christos         if (*Pathname == '\\')
    350          1.1  christos         {
    351          1.1  christos             *Pathname = '/';
    352          1.1  christos         }
    353          1.1  christos 
    354          1.1  christos         Pathname++;
    355          1.1  christos     }
    356          1.1  christos }
    357          1.1  christos #endif
    358