1*385cc6b4SJerry Jelinek /*******************************************************************************
2*385cc6b4SJerry Jelinek *
3*385cc6b4SJerry Jelinek * Module Name: utstring - Common functions for strings and characters
4*385cc6b4SJerry Jelinek *
5*385cc6b4SJerry Jelinek ******************************************************************************/
6*385cc6b4SJerry Jelinek
7*385cc6b4SJerry Jelinek /*
8*385cc6b4SJerry Jelinek * Copyright (C) 2000 - 2016, Intel Corp.
9*385cc6b4SJerry Jelinek * All rights reserved.
10*385cc6b4SJerry Jelinek *
11*385cc6b4SJerry Jelinek * Redistribution and use in source and binary forms, with or without
12*385cc6b4SJerry Jelinek * modification, are permitted provided that the following conditions
13*385cc6b4SJerry Jelinek * are met:
14*385cc6b4SJerry Jelinek * 1. Redistributions of source code must retain the above copyright
15*385cc6b4SJerry Jelinek * notice, this list of conditions, and the following disclaimer,
16*385cc6b4SJerry Jelinek * without modification.
17*385cc6b4SJerry Jelinek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*385cc6b4SJerry Jelinek * substantially similar to the "NO WARRANTY" disclaimer below
19*385cc6b4SJerry Jelinek * ("Disclaimer") and any redistribution must be conditioned upon
20*385cc6b4SJerry Jelinek * including a substantially similar Disclaimer requirement for further
21*385cc6b4SJerry Jelinek * binary redistribution.
22*385cc6b4SJerry Jelinek * 3. Neither the names of the above-listed copyright holders nor the names
23*385cc6b4SJerry Jelinek * of any contributors may be used to endorse or promote products derived
24*385cc6b4SJerry Jelinek * from this software without specific prior written permission.
25*385cc6b4SJerry Jelinek *
26*385cc6b4SJerry Jelinek * Alternatively, this software may be distributed under the terms of the
27*385cc6b4SJerry Jelinek * GNU General Public License ("GPL") version 2 as published by the Free
28*385cc6b4SJerry Jelinek * Software Foundation.
29*385cc6b4SJerry Jelinek *
30*385cc6b4SJerry Jelinek * NO WARRANTY
31*385cc6b4SJerry Jelinek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*385cc6b4SJerry Jelinek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*385cc6b4SJerry Jelinek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*385cc6b4SJerry Jelinek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*385cc6b4SJerry Jelinek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*385cc6b4SJerry Jelinek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*385cc6b4SJerry Jelinek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*385cc6b4SJerry Jelinek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*385cc6b4SJerry Jelinek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*385cc6b4SJerry Jelinek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*385cc6b4SJerry Jelinek * POSSIBILITY OF SUCH DAMAGES.
42*385cc6b4SJerry Jelinek */
43*385cc6b4SJerry Jelinek
44*385cc6b4SJerry Jelinek #include "acpi.h"
45*385cc6b4SJerry Jelinek #include "accommon.h"
46*385cc6b4SJerry Jelinek #include "acnamesp.h"
47*385cc6b4SJerry Jelinek
48*385cc6b4SJerry Jelinek
49*385cc6b4SJerry Jelinek #define _COMPONENT ACPI_UTILITIES
50*385cc6b4SJerry Jelinek ACPI_MODULE_NAME ("utstring")
51*385cc6b4SJerry Jelinek
52*385cc6b4SJerry Jelinek
53*385cc6b4SJerry Jelinek /*******************************************************************************
54*385cc6b4SJerry Jelinek *
55*385cc6b4SJerry Jelinek * FUNCTION: AcpiUtPrintString
56*385cc6b4SJerry Jelinek *
57*385cc6b4SJerry Jelinek * PARAMETERS: String - Null terminated ASCII string
58*385cc6b4SJerry Jelinek * MaxLength - Maximum output length. Used to constrain the
59*385cc6b4SJerry Jelinek * length of strings during debug output only.
60*385cc6b4SJerry Jelinek *
61*385cc6b4SJerry Jelinek * RETURN: None
62*385cc6b4SJerry Jelinek *
63*385cc6b4SJerry Jelinek * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
64*385cc6b4SJerry Jelinek * sequences.
65*385cc6b4SJerry Jelinek *
66*385cc6b4SJerry Jelinek ******************************************************************************/
67*385cc6b4SJerry Jelinek
68*385cc6b4SJerry Jelinek void
AcpiUtPrintString(char * String,UINT16 MaxLength)69*385cc6b4SJerry Jelinek AcpiUtPrintString (
70*385cc6b4SJerry Jelinek char *String,
71*385cc6b4SJerry Jelinek UINT16 MaxLength)
72*385cc6b4SJerry Jelinek {
73*385cc6b4SJerry Jelinek UINT32 i;
74*385cc6b4SJerry Jelinek
75*385cc6b4SJerry Jelinek
76*385cc6b4SJerry Jelinek if (!String)
77*385cc6b4SJerry Jelinek {
78*385cc6b4SJerry Jelinek AcpiOsPrintf ("<\"NULL STRING PTR\">");
79*385cc6b4SJerry Jelinek return;
80*385cc6b4SJerry Jelinek }
81*385cc6b4SJerry Jelinek
82*385cc6b4SJerry Jelinek AcpiOsPrintf ("\"");
83*385cc6b4SJerry Jelinek for (i = 0; (i < MaxLength) && String[i]; i++)
84*385cc6b4SJerry Jelinek {
85*385cc6b4SJerry Jelinek /* Escape sequences */
86*385cc6b4SJerry Jelinek
87*385cc6b4SJerry Jelinek switch (String[i])
88*385cc6b4SJerry Jelinek {
89*385cc6b4SJerry Jelinek case 0x07:
90*385cc6b4SJerry Jelinek
91*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\a"); /* BELL */
92*385cc6b4SJerry Jelinek break;
93*385cc6b4SJerry Jelinek
94*385cc6b4SJerry Jelinek case 0x08:
95*385cc6b4SJerry Jelinek
96*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\b"); /* BACKSPACE */
97*385cc6b4SJerry Jelinek break;
98*385cc6b4SJerry Jelinek
99*385cc6b4SJerry Jelinek case 0x0C:
100*385cc6b4SJerry Jelinek
101*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\f"); /* FORMFEED */
102*385cc6b4SJerry Jelinek break;
103*385cc6b4SJerry Jelinek
104*385cc6b4SJerry Jelinek case 0x0A:
105*385cc6b4SJerry Jelinek
106*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\n"); /* LINEFEED */
107*385cc6b4SJerry Jelinek break;
108*385cc6b4SJerry Jelinek
109*385cc6b4SJerry Jelinek case 0x0D:
110*385cc6b4SJerry Jelinek
111*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
112*385cc6b4SJerry Jelinek break;
113*385cc6b4SJerry Jelinek
114*385cc6b4SJerry Jelinek case 0x09:
115*385cc6b4SJerry Jelinek
116*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
117*385cc6b4SJerry Jelinek break;
118*385cc6b4SJerry Jelinek
119*385cc6b4SJerry Jelinek case 0x0B:
120*385cc6b4SJerry Jelinek
121*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
122*385cc6b4SJerry Jelinek break;
123*385cc6b4SJerry Jelinek
124*385cc6b4SJerry Jelinek case '\'': /* Single Quote */
125*385cc6b4SJerry Jelinek case '\"': /* Double Quote */
126*385cc6b4SJerry Jelinek case '\\': /* Backslash */
127*385cc6b4SJerry Jelinek
128*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\%c", (int) String[i]);
129*385cc6b4SJerry Jelinek break;
130*385cc6b4SJerry Jelinek
131*385cc6b4SJerry Jelinek default:
132*385cc6b4SJerry Jelinek
133*385cc6b4SJerry Jelinek /* Check for printable character or hex escape */
134*385cc6b4SJerry Jelinek
135*385cc6b4SJerry Jelinek if (isprint ((int) String[i]))
136*385cc6b4SJerry Jelinek {
137*385cc6b4SJerry Jelinek /* This is a normal character */
138*385cc6b4SJerry Jelinek
139*385cc6b4SJerry Jelinek AcpiOsPrintf ("%c", (int) String[i]);
140*385cc6b4SJerry Jelinek }
141*385cc6b4SJerry Jelinek else
142*385cc6b4SJerry Jelinek {
143*385cc6b4SJerry Jelinek /* All others will be Hex escapes */
144*385cc6b4SJerry Jelinek
145*385cc6b4SJerry Jelinek AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
146*385cc6b4SJerry Jelinek }
147*385cc6b4SJerry Jelinek break;
148*385cc6b4SJerry Jelinek }
149*385cc6b4SJerry Jelinek }
150*385cc6b4SJerry Jelinek
151*385cc6b4SJerry Jelinek AcpiOsPrintf ("\"");
152*385cc6b4SJerry Jelinek
153*385cc6b4SJerry Jelinek if (i == MaxLength && String[i])
154*385cc6b4SJerry Jelinek {
155*385cc6b4SJerry Jelinek AcpiOsPrintf ("...");
156*385cc6b4SJerry Jelinek }
157*385cc6b4SJerry Jelinek }
158*385cc6b4SJerry Jelinek
159*385cc6b4SJerry Jelinek
160*385cc6b4SJerry Jelinek /*******************************************************************************
161*385cc6b4SJerry Jelinek *
162*385cc6b4SJerry Jelinek * FUNCTION: AcpiUtRepairName
163*385cc6b4SJerry Jelinek *
164*385cc6b4SJerry Jelinek * PARAMETERS: Name - The ACPI name to be repaired
165*385cc6b4SJerry Jelinek *
166*385cc6b4SJerry Jelinek * RETURN: Repaired version of the name
167*385cc6b4SJerry Jelinek *
168*385cc6b4SJerry Jelinek * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
169*385cc6b4SJerry Jelinek * return the new name. NOTE: the Name parameter must reside in
170*385cc6b4SJerry Jelinek * read/write memory, cannot be a const.
171*385cc6b4SJerry Jelinek *
172*385cc6b4SJerry Jelinek * An ACPI Name must consist of valid ACPI characters. We will repair the name
173*385cc6b4SJerry Jelinek * if necessary because we don't want to abort because of this, but we want
174*385cc6b4SJerry Jelinek * all namespace names to be printable. A warning message is appropriate.
175*385cc6b4SJerry Jelinek *
176*385cc6b4SJerry Jelinek * This issue came up because there are in fact machines that exhibit
177*385cc6b4SJerry Jelinek * this problem, and we want to be able to enable ACPI support for them,
178*385cc6b4SJerry Jelinek * even though there are a few bad names.
179*385cc6b4SJerry Jelinek *
180*385cc6b4SJerry Jelinek ******************************************************************************/
181*385cc6b4SJerry Jelinek
182*385cc6b4SJerry Jelinek void
AcpiUtRepairName(char * Name)183*385cc6b4SJerry Jelinek AcpiUtRepairName (
184*385cc6b4SJerry Jelinek char *Name)
185*385cc6b4SJerry Jelinek {
186*385cc6b4SJerry Jelinek UINT32 i;
187*385cc6b4SJerry Jelinek BOOLEAN FoundBadChar = FALSE;
188*385cc6b4SJerry Jelinek UINT32 OriginalName;
189*385cc6b4SJerry Jelinek
190*385cc6b4SJerry Jelinek
191*385cc6b4SJerry Jelinek ACPI_FUNCTION_NAME (UtRepairName);
192*385cc6b4SJerry Jelinek
193*385cc6b4SJerry Jelinek
194*385cc6b4SJerry Jelinek /*
195*385cc6b4SJerry Jelinek * Special case for the root node. This can happen if we get an
196*385cc6b4SJerry Jelinek * error during the execution of module-level code.
197*385cc6b4SJerry Jelinek */
198*385cc6b4SJerry Jelinek if (ACPI_COMPARE_NAME (Name, "\\___"))
199*385cc6b4SJerry Jelinek {
200*385cc6b4SJerry Jelinek return;
201*385cc6b4SJerry Jelinek }
202*385cc6b4SJerry Jelinek
203*385cc6b4SJerry Jelinek ACPI_MOVE_NAME (&OriginalName, Name);
204*385cc6b4SJerry Jelinek
205*385cc6b4SJerry Jelinek /* Check each character in the name */
206*385cc6b4SJerry Jelinek
207*385cc6b4SJerry Jelinek for (i = 0; i < ACPI_NAME_SIZE; i++)
208*385cc6b4SJerry Jelinek {
209*385cc6b4SJerry Jelinek if (AcpiUtValidNameChar (Name[i], i))
210*385cc6b4SJerry Jelinek {
211*385cc6b4SJerry Jelinek continue;
212*385cc6b4SJerry Jelinek }
213*385cc6b4SJerry Jelinek
214*385cc6b4SJerry Jelinek /*
215*385cc6b4SJerry Jelinek * Replace a bad character with something printable, yet technically
216*385cc6b4SJerry Jelinek * still invalid. This prevents any collisions with existing "good"
217*385cc6b4SJerry Jelinek * names in the namespace.
218*385cc6b4SJerry Jelinek */
219*385cc6b4SJerry Jelinek Name[i] = '*';
220*385cc6b4SJerry Jelinek FoundBadChar = TRUE;
221*385cc6b4SJerry Jelinek }
222*385cc6b4SJerry Jelinek
223*385cc6b4SJerry Jelinek if (FoundBadChar)
224*385cc6b4SJerry Jelinek {
225*385cc6b4SJerry Jelinek /* Report warning only if in strict mode or debug mode */
226*385cc6b4SJerry Jelinek
227*385cc6b4SJerry Jelinek if (!AcpiGbl_EnableInterpreterSlack)
228*385cc6b4SJerry Jelinek {
229*385cc6b4SJerry Jelinek ACPI_WARNING ((AE_INFO,
230*385cc6b4SJerry Jelinek "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
231*385cc6b4SJerry Jelinek OriginalName, Name));
232*385cc6b4SJerry Jelinek }
233*385cc6b4SJerry Jelinek else
234*385cc6b4SJerry Jelinek {
235*385cc6b4SJerry Jelinek ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
236*385cc6b4SJerry Jelinek "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
237*385cc6b4SJerry Jelinek OriginalName, Name));
238*385cc6b4SJerry Jelinek }
239*385cc6b4SJerry Jelinek }
240*385cc6b4SJerry Jelinek }
241*385cc6b4SJerry Jelinek
242*385cc6b4SJerry Jelinek
243*385cc6b4SJerry Jelinek #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
244*385cc6b4SJerry Jelinek /*******************************************************************************
245*385cc6b4SJerry Jelinek *
246*385cc6b4SJerry Jelinek * FUNCTION: UtConvertBackslashes
247*385cc6b4SJerry Jelinek *
248*385cc6b4SJerry Jelinek * PARAMETERS: Pathname - File pathname string to be converted
249*385cc6b4SJerry Jelinek *
250*385cc6b4SJerry Jelinek * RETURN: Modifies the input Pathname
251*385cc6b4SJerry Jelinek *
252*385cc6b4SJerry Jelinek * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
253*385cc6b4SJerry Jelinek * the entire input file pathname string.
254*385cc6b4SJerry Jelinek *
255*385cc6b4SJerry Jelinek ******************************************************************************/
256*385cc6b4SJerry Jelinek
257*385cc6b4SJerry Jelinek void
UtConvertBackslashes(char * Pathname)258*385cc6b4SJerry Jelinek UtConvertBackslashes (
259*385cc6b4SJerry Jelinek char *Pathname)
260*385cc6b4SJerry Jelinek {
261*385cc6b4SJerry Jelinek
262*385cc6b4SJerry Jelinek if (!Pathname)
263*385cc6b4SJerry Jelinek {
264*385cc6b4SJerry Jelinek return;
265*385cc6b4SJerry Jelinek }
266*385cc6b4SJerry Jelinek
267*385cc6b4SJerry Jelinek while (*Pathname)
268*385cc6b4SJerry Jelinek {
269*385cc6b4SJerry Jelinek if (*Pathname == '\\')
270*385cc6b4SJerry Jelinek {
271*385cc6b4SJerry Jelinek *Pathname = '/';
272*385cc6b4SJerry Jelinek }
273*385cc6b4SJerry Jelinek
274*385cc6b4SJerry Jelinek Pathname++;
275*385cc6b4SJerry Jelinek }
276*385cc6b4SJerry Jelinek }
277*385cc6b4SJerry Jelinek #endif
278