Home | History | Annotate | Line # | Download | only in utilities
utstring.c revision 1.1.1.4
      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.3  christos  * Copyright (C) 2000 - 2015, 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.4  christos             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  christos     AcpiOsPrintf ("\"");
    151      1.1  christos 
    152      1.1  christos     if (i == MaxLength && String[i])
    153      1.1  christos     {
    154      1.1  christos         AcpiOsPrintf ("...");
    155      1.1  christos     }
    156      1.1  christos }
    157      1.1  christos 
    158      1.1  christos 
    159      1.1  christos /*******************************************************************************
    160      1.1  christos  *
    161      1.1  christos  * FUNCTION:    AcpiUtValidAcpiChar
    162      1.1  christos  *
    163      1.1  christos  * PARAMETERS:  Char            - The character to be examined
    164      1.1  christos  *              Position        - Byte position (0-3)
    165      1.1  christos  *
    166      1.1  christos  * RETURN:      TRUE if the character is valid, FALSE otherwise
    167      1.1  christos  *
    168      1.1  christos  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
    169      1.1  christos  *              1) Upper case alpha
    170      1.1  christos  *              2) numeric
    171      1.1  christos  *              3) underscore
    172      1.1  christos  *
    173      1.1  christos  *              We allow a '!' as the last character because of the ASF! table
    174      1.1  christos  *
    175      1.1  christos  ******************************************************************************/
    176      1.1  christos 
    177      1.1  christos BOOLEAN
    178      1.1  christos AcpiUtValidAcpiChar (
    179      1.1  christos     char                    Character,
    180      1.1  christos     UINT32                  Position)
    181      1.1  christos {
    182      1.1  christos 
    183      1.1  christos     if (!((Character >= 'A' && Character <= 'Z') ||
    184      1.1  christos           (Character >= '0' && Character <= '9') ||
    185      1.1  christos           (Character == '_')))
    186      1.1  christos     {
    187      1.1  christos         /* Allow a '!' in the last position */
    188      1.1  christos 
    189      1.1  christos         if (Character == '!' && Position == 3)
    190      1.1  christos         {
    191      1.1  christos             return (TRUE);
    192      1.1  christos         }
    193      1.1  christos 
    194      1.1  christos         return (FALSE);
    195      1.1  christos     }
    196      1.1  christos 
    197      1.1  christos     return (TRUE);
    198      1.1  christos }
    199      1.1  christos 
    200      1.1  christos 
    201      1.1  christos /*******************************************************************************
    202      1.1  christos  *
    203      1.1  christos  * FUNCTION:    AcpiUtValidAcpiName
    204      1.1  christos  *
    205      1.1  christos  * PARAMETERS:  Name            - The name to be examined. Does not have to
    206      1.1  christos  *                                be NULL terminated string.
    207      1.1  christos  *
    208      1.1  christos  * RETURN:      TRUE if the name is valid, FALSE otherwise
    209      1.1  christos  *
    210      1.1  christos  * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
    211      1.1  christos  *              1) Upper case alpha
    212      1.1  christos  *              2) numeric
    213      1.1  christos  *              3) underscore
    214      1.1  christos  *
    215      1.1  christos  ******************************************************************************/
    216      1.1  christos 
    217      1.1  christos BOOLEAN
    218      1.1  christos AcpiUtValidAcpiName (
    219      1.1  christos     char                    *Name)
    220      1.1  christos {
    221      1.1  christos     UINT32                  i;
    222      1.1  christos 
    223      1.1  christos 
    224      1.1  christos     ACPI_FUNCTION_ENTRY ();
    225      1.1  christos 
    226      1.1  christos 
    227      1.1  christos     for (i = 0; i < ACPI_NAME_SIZE; i++)
    228      1.1  christos     {
    229      1.1  christos         if (!AcpiUtValidAcpiChar (Name[i], i))
    230      1.1  christos         {
    231      1.1  christos             return (FALSE);
    232      1.1  christos         }
    233      1.1  christos     }
    234      1.1  christos 
    235      1.1  christos     return (TRUE);
    236      1.1  christos }
    237      1.1  christos 
    238      1.1  christos 
    239      1.1  christos /*******************************************************************************
    240      1.1  christos  *
    241      1.1  christos  * FUNCTION:    AcpiUtRepairName
    242      1.1  christos  *
    243      1.1  christos  * PARAMETERS:  Name            - The ACPI name to be repaired
    244      1.1  christos  *
    245      1.1  christos  * RETURN:      Repaired version of the name
    246      1.1  christos  *
    247      1.1  christos  * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
    248      1.1  christos  *              return the new name. NOTE: the Name parameter must reside in
    249      1.1  christos  *              read/write memory, cannot be a const.
    250      1.1  christos  *
    251      1.1  christos  * An ACPI Name must consist of valid ACPI characters. We will repair the name
    252      1.1  christos  * if necessary because we don't want to abort because of this, but we want
    253      1.1  christos  * all namespace names to be printable. A warning message is appropriate.
    254      1.1  christos  *
    255      1.1  christos  * This issue came up because there are in fact machines that exhibit
    256      1.1  christos  * this problem, and we want to be able to enable ACPI support for them,
    257      1.1  christos  * even though there are a few bad names.
    258      1.1  christos  *
    259      1.1  christos  ******************************************************************************/
    260      1.1  christos 
    261      1.1  christos void
    262      1.1  christos AcpiUtRepairName (
    263      1.1  christos     char                    *Name)
    264      1.1  christos {
    265      1.1  christos     UINT32                  i;
    266      1.1  christos     BOOLEAN                 FoundBadChar = FALSE;
    267      1.1  christos     UINT32                  OriginalName;
    268      1.1  christos 
    269      1.1  christos 
    270      1.1  christos     ACPI_FUNCTION_NAME (UtRepairName);
    271      1.1  christos 
    272      1.1  christos 
    273      1.1  christos     ACPI_MOVE_NAME (&OriginalName, Name);
    274      1.1  christos 
    275      1.1  christos     /* Check each character in the name */
    276      1.1  christos 
    277      1.1  christos     for (i = 0; i < ACPI_NAME_SIZE; i++)
    278      1.1  christos     {
    279      1.1  christos         if (AcpiUtValidAcpiChar (Name[i], i))
    280      1.1  christos         {
    281      1.1  christos             continue;
    282      1.1  christos         }
    283      1.1  christos 
    284      1.1  christos         /*
    285      1.1  christos          * Replace a bad character with something printable, yet technically
    286      1.1  christos          * still invalid. This prevents any collisions with existing "good"
    287      1.1  christos          * names in the namespace.
    288      1.1  christos          */
    289      1.1  christos         Name[i] = '*';
    290      1.1  christos         FoundBadChar = TRUE;
    291      1.1  christos     }
    292      1.1  christos 
    293      1.1  christos     if (FoundBadChar)
    294      1.1  christos     {
    295      1.1  christos         /* Report warning only if in strict mode or debug mode */
    296      1.1  christos 
    297      1.1  christos         if (!AcpiGbl_EnableInterpreterSlack)
    298      1.1  christos         {
    299      1.1  christos             ACPI_WARNING ((AE_INFO,
    300      1.1  christos                 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
    301      1.1  christos                 OriginalName, Name));
    302      1.1  christos         }
    303      1.1  christos         else
    304      1.1  christos         {
    305      1.1  christos             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    306      1.1  christos                 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
    307      1.1  christos                 OriginalName, Name));
    308      1.1  christos         }
    309      1.1  christos     }
    310      1.1  christos }
    311      1.1  christos 
    312      1.1  christos 
    313      1.1  christos #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
    314      1.1  christos /*******************************************************************************
    315      1.1  christos  *
    316      1.1  christos  * FUNCTION:    UtConvertBackslashes
    317      1.1  christos  *
    318      1.1  christos  * PARAMETERS:  Pathname        - File pathname string to be converted
    319      1.1  christos  *
    320      1.1  christos  * RETURN:      Modifies the input Pathname
    321      1.1  christos  *
    322      1.1  christos  * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
    323      1.1  christos  *              the entire input file pathname string.
    324      1.1  christos  *
    325      1.1  christos  ******************************************************************************/
    326      1.1  christos 
    327      1.1  christos void
    328      1.1  christos UtConvertBackslashes (
    329      1.1  christos     char                    *Pathname)
    330      1.1  christos {
    331      1.1  christos 
    332      1.1  christos     if (!Pathname)
    333      1.1  christos     {
    334      1.1  christos         return;
    335      1.1  christos     }
    336      1.1  christos 
    337      1.1  christos     while (*Pathname)
    338      1.1  christos     {
    339      1.1  christos         if (*Pathname == '\\')
    340      1.1  christos         {
    341      1.1  christos             *Pathname = '/';
    342      1.1  christos         }
    343      1.1  christos 
    344      1.1  christos         Pathname++;
    345      1.1  christos     }
    346      1.1  christos }
    347      1.1  christos #endif
    348