1244971Sjkim/*******************************************************************************
2244971Sjkim *
3244971Sjkim * Module Name: utstring - Common functions for strings and characters
4244971Sjkim *
5244971Sjkim ******************************************************************************/
6244971Sjkim
7244971Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, Intel Corp.
9244971Sjkim * All rights reserved.
10244971Sjkim *
11244971Sjkim * Redistribution and use in source and binary forms, with or without
12244971Sjkim * modification, are permitted provided that the following conditions
13244971Sjkim * are met:
14244971Sjkim * 1. Redistributions of source code must retain the above copyright
15244971Sjkim *    notice, this list of conditions, and the following disclaimer,
16244971Sjkim *    without modification.
17244971Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18244971Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19244971Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20244971Sjkim *    including a substantially similar Disclaimer requirement for further
21244971Sjkim *    binary redistribution.
22244971Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23244971Sjkim *    of any contributors may be used to endorse or promote products derived
24244971Sjkim *    from this software without specific prior written permission.
25244971Sjkim *
26244971Sjkim * Alternatively, this software may be distributed under the terms of the
27244971Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28244971Sjkim * Software Foundation.
29244971Sjkim *
30244971Sjkim * NO WARRANTY
31244971Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32244971Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33244971Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34244971Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35244971Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36244971Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37244971Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38244971Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39244971Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40244971Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41244971Sjkim * POSSIBILITY OF SUCH DAMAGES.
42244971Sjkim */
43244971Sjkim
44245582Sjkim#include <contrib/dev/acpica/include/acpi.h>
45245582Sjkim#include <contrib/dev/acpica/include/accommon.h>
46245582Sjkim#include <contrib/dev/acpica/include/acnamesp.h>
47244971Sjkim
48244971Sjkim
49244971Sjkim#define _COMPONENT          ACPI_UTILITIES
50244971Sjkim        ACPI_MODULE_NAME    ("utstring")
51244971Sjkim
52244971Sjkim
53244971Sjkim/*******************************************************************************
54244971Sjkim *
55244971Sjkim * FUNCTION:    AcpiUtPrintString
56244971Sjkim *
57244971Sjkim * PARAMETERS:  String          - Null terminated ASCII string
58252279Sjkim *              MaxLength       - Maximum output length. Used to constrain the
59252279Sjkim *                                length of strings during debug output only.
60244971Sjkim *
61244971Sjkim * RETURN:      None
62244971Sjkim *
63244971Sjkim * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
64244971Sjkim *              sequences.
65244971Sjkim *
66244971Sjkim ******************************************************************************/
67244971Sjkim
68244971Sjkimvoid
69244971SjkimAcpiUtPrintString (
70244971Sjkim    char                    *String,
71252279Sjkim    UINT16                  MaxLength)
72244971Sjkim{
73244971Sjkim    UINT32                  i;
74244971Sjkim
75244971Sjkim
76244971Sjkim    if (!String)
77244971Sjkim    {
78244971Sjkim        AcpiOsPrintf ("<\"NULL STRING PTR\">");
79244971Sjkim        return;
80244971Sjkim    }
81244971Sjkim
82244971Sjkim    AcpiOsPrintf ("\"");
83281075Sdim    for (i = 0; (i < MaxLength) && String[i]; i++)
84244971Sjkim    {
85244971Sjkim        /* Escape sequences */
86244971Sjkim
87244971Sjkim        switch (String[i])
88244971Sjkim        {
89244971Sjkim        case 0x07:
90250838Sjkim
91244971Sjkim            AcpiOsPrintf ("\\a");       /* BELL */
92244971Sjkim            break;
93244971Sjkim
94244971Sjkim        case 0x08:
95250838Sjkim
96244971Sjkim            AcpiOsPrintf ("\\b");       /* BACKSPACE */
97244971Sjkim            break;
98244971Sjkim
99244971Sjkim        case 0x0C:
100250838Sjkim
101244971Sjkim            AcpiOsPrintf ("\\f");       /* FORMFEED */
102244971Sjkim            break;
103244971Sjkim
104244971Sjkim        case 0x0A:
105250838Sjkim
106244971Sjkim            AcpiOsPrintf ("\\n");       /* LINEFEED */
107244971Sjkim            break;
108244971Sjkim
109244971Sjkim        case 0x0D:
110250838Sjkim
111244971Sjkim            AcpiOsPrintf ("\\r");       /* CARRIAGE RETURN*/
112244971Sjkim            break;
113244971Sjkim
114244971Sjkim        case 0x09:
115250838Sjkim
116244971Sjkim            AcpiOsPrintf ("\\t");       /* HORIZONTAL TAB */
117244971Sjkim            break;
118244971Sjkim
119244971Sjkim        case 0x0B:
120250838Sjkim
121244971Sjkim            AcpiOsPrintf ("\\v");       /* VERTICAL TAB */
122244971Sjkim            break;
123244971Sjkim
124244971Sjkim        case '\'':                      /* Single Quote */
125244971Sjkim        case '\"':                      /* Double Quote */
126244971Sjkim        case '\\':                      /* Backslash */
127250838Sjkim
128244971Sjkim            AcpiOsPrintf ("\\%c", (int) String[i]);
129244971Sjkim            break;
130244971Sjkim
131244971Sjkim        default:
132244971Sjkim
133244971Sjkim            /* Check for printable character or hex escape */
134244971Sjkim
135306536Sjkim            if (isprint ((int) String[i]))
136244971Sjkim            {
137244971Sjkim                /* This is a normal character */
138244971Sjkim
139244971Sjkim                AcpiOsPrintf ("%c", (int) String[i]);
140244971Sjkim            }
141244971Sjkim            else
142244971Sjkim            {
143244971Sjkim                /* All others will be Hex escapes */
144244971Sjkim
145244971Sjkim                AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
146244971Sjkim            }
147244971Sjkim            break;
148244971Sjkim        }
149244971Sjkim    }
150306536Sjkim
151244971Sjkim    AcpiOsPrintf ("\"");
152244971Sjkim
153244971Sjkim    if (i == MaxLength && String[i])
154244971Sjkim    {
155244971Sjkim        AcpiOsPrintf ("...");
156244971Sjkim    }
157244971Sjkim}
158244971Sjkim
159244971Sjkim
160244971Sjkim/*******************************************************************************
161244971Sjkim *
162244971Sjkim * FUNCTION:    AcpiUtRepairName
163244971Sjkim *
164244971Sjkim * PARAMETERS:  Name            - The ACPI name to be repaired
165244971Sjkim *
166244971Sjkim * RETURN:      Repaired version of the name
167244971Sjkim *
168244971Sjkim * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
169244971Sjkim *              return the new name. NOTE: the Name parameter must reside in
170244971Sjkim *              read/write memory, cannot be a const.
171244971Sjkim *
172244971Sjkim * An ACPI Name must consist of valid ACPI characters. We will repair the name
173244971Sjkim * if necessary because we don't want to abort because of this, but we want
174244971Sjkim * all namespace names to be printable. A warning message is appropriate.
175244971Sjkim *
176244971Sjkim * This issue came up because there are in fact machines that exhibit
177244971Sjkim * this problem, and we want to be able to enable ACPI support for them,
178244971Sjkim * even though there are a few bad names.
179244971Sjkim *
180244971Sjkim ******************************************************************************/
181244971Sjkim
182244971Sjkimvoid
183244971SjkimAcpiUtRepairName (
184244971Sjkim    char                    *Name)
185244971Sjkim{
186244971Sjkim    UINT32                  i;
187244971Sjkim    BOOLEAN                 FoundBadChar = FALSE;
188244971Sjkim    UINT32                  OriginalName;
189244971Sjkim
190244971Sjkim
191244971Sjkim    ACPI_FUNCTION_NAME (UtRepairName);
192244971Sjkim
193244971Sjkim
194306536Sjkim    /*
195306536Sjkim     * Special case for the root node. This can happen if we get an
196306536Sjkim     * error during the execution of module-level code.
197306536Sjkim     */
198306536Sjkim    if (ACPI_COMPARE_NAME (Name, "\\___"))
199306536Sjkim    {
200306536Sjkim        return;
201306536Sjkim    }
202306536Sjkim
203244971Sjkim    ACPI_MOVE_NAME (&OriginalName, Name);
204244971Sjkim
205244971Sjkim    /* Check each character in the name */
206244971Sjkim
207244971Sjkim    for (i = 0; i < ACPI_NAME_SIZE; i++)
208244971Sjkim    {
209306536Sjkim        if (AcpiUtValidNameChar (Name[i], i))
210244971Sjkim        {
211244971Sjkim            continue;
212244971Sjkim        }
213244971Sjkim
214244971Sjkim        /*
215244971Sjkim         * Replace a bad character with something printable, yet technically
216244971Sjkim         * still invalid. This prevents any collisions with existing "good"
217244971Sjkim         * names in the namespace.
218244971Sjkim         */
219244971Sjkim        Name[i] = '*';
220244971Sjkim        FoundBadChar = TRUE;
221244971Sjkim    }
222244971Sjkim
223244971Sjkim    if (FoundBadChar)
224244971Sjkim    {
225244971Sjkim        /* Report warning only if in strict mode or debug mode */
226244971Sjkim
227244971Sjkim        if (!AcpiGbl_EnableInterpreterSlack)
228244971Sjkim        {
229244971Sjkim            ACPI_WARNING ((AE_INFO,
230244971Sjkim                "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
231244971Sjkim                OriginalName, Name));
232244971Sjkim        }
233244971Sjkim        else
234244971Sjkim        {
235244971Sjkim            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
236244971Sjkim                "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
237244971Sjkim                OriginalName, Name));
238244971Sjkim        }
239244971Sjkim    }
240244971Sjkim}
241244971Sjkim
242244971Sjkim
243244971Sjkim#if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
244244971Sjkim/*******************************************************************************
245244971Sjkim *
246244971Sjkim * FUNCTION:    UtConvertBackslashes
247244971Sjkim *
248244971Sjkim * PARAMETERS:  Pathname        - File pathname string to be converted
249244971Sjkim *
250244971Sjkim * RETURN:      Modifies the input Pathname
251244971Sjkim *
252244971Sjkim * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
253244971Sjkim *              the entire input file pathname string.
254244971Sjkim *
255244971Sjkim ******************************************************************************/
256244971Sjkim
257244971Sjkimvoid
258244971SjkimUtConvertBackslashes (
259244971Sjkim    char                    *Pathname)
260244971Sjkim{
261244971Sjkim
262244971Sjkim    if (!Pathname)
263244971Sjkim    {
264244971Sjkim        return;
265244971Sjkim    }
266244971Sjkim
267244971Sjkim    while (*Pathname)
268244971Sjkim    {
269244971Sjkim        if (*Pathname == '\\')
270244971Sjkim        {
271244971Sjkim            *Pathname = '/';
272244971Sjkim        }
273244971Sjkim
274244971Sjkim        Pathname++;
275244971Sjkim    }
276244971Sjkim}
277244971Sjkim#endif
278