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