1 /*******************************************************************************
3 * Module Name: utstring - Common functions for strings and characters
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <acpi/acpi.h>
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME("utstring")
51 /*******************************************************************************
53 * FUNCTION: acpi_ut_print_string
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.
61 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
64 ******************************************************************************/
65 void acpi_ut_print_string(char *string, u16 max_length)
70 acpi_os_printf("<\"NULL STRING PTR\">");
75 for (i = 0; (i < max_length) && string[i]; i++) {
77 /* Escape sequences */
82 acpi_os_printf("\\a"); /* BELL */
87 acpi_os_printf("\\b"); /* BACKSPACE */
92 acpi_os_printf("\\f"); /* FORMFEED */
97 acpi_os_printf("\\n"); /* LINEFEED */
102 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
107 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
112 acpi_os_printf("\\v"); /* VERTICAL TAB */
115 case '\'': /* Single Quote */
116 case '\"': /* Double Quote */
117 case '\\': /* Backslash */
119 acpi_os_printf("\\%c", (int)string[i]);
124 /* Check for printable character or hex escape */
126 if (isprint((int)string[i])) {
127 /* This is a normal character */
129 acpi_os_printf("%c", (int)string[i]);
131 /* All others will be Hex escapes */
133 acpi_os_printf("\\x%2.2X", (s32) string[i]);
139 acpi_os_printf("\"");
141 if (i == max_length && string[i]) {
142 acpi_os_printf("...");
146 /*******************************************************************************
148 * FUNCTION: acpi_ut_valid_acpi_char
150 * PARAMETERS: char - The character to be examined
151 * position - Byte position (0-3)
153 * RETURN: TRUE if the character is valid, FALSE otherwise
155 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
156 * 1) Upper case alpha
160 * We allow a '!' as the last character because of the ASF! table
162 ******************************************************************************/
164 u8 acpi_ut_valid_acpi_char(char character, u32 position)
167 if (!((character >= 'A' && character <= 'Z') ||
168 (character >= '0' && character <= '9') || (character == '_'))) {
170 /* Allow a '!' in the last position */
172 if (character == '!' && position == 3) {
182 /*******************************************************************************
184 * FUNCTION: acpi_ut_valid_acpi_name
186 * PARAMETERS: name - The name to be examined. Does not have to
187 * be NULL terminated string.
189 * RETURN: TRUE if the name is valid, FALSE otherwise
191 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
192 * 1) Upper case alpha
196 ******************************************************************************/
198 u8 acpi_ut_valid_acpi_name(char *name)
202 ACPI_FUNCTION_ENTRY();
204 for (i = 0; i < ACPI_NAME_SIZE; i++) {
205 if (!acpi_ut_valid_acpi_char(name[i], i)) {
213 /*******************************************************************************
215 * FUNCTION: acpi_ut_repair_name
217 * PARAMETERS: name - The ACPI name to be repaired
219 * RETURN: Repaired version of the name
221 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
222 * return the new name. NOTE: the Name parameter must reside in
223 * read/write memory, cannot be a const.
225 * An ACPI Name must consist of valid ACPI characters. We will repair the name
226 * if necessary because we don't want to abort because of this, but we want
227 * all namespace names to be printable. A warning message is appropriate.
229 * This issue came up because there are in fact machines that exhibit
230 * this problem, and we want to be able to enable ACPI support for them,
231 * even though there are a few bad names.
233 ******************************************************************************/
235 void acpi_ut_repair_name(char *name)
238 u8 found_bad_char = FALSE;
241 ACPI_FUNCTION_NAME(ut_repair_name);
244 * Special case for the root node. This can happen if we get an
245 * error during the execution of module-level code.
247 if (ACPI_COMPARE_NAME(name, "\\___")) {
251 ACPI_MOVE_NAME(&original_name, name);
253 /* Check each character in the name */
255 for (i = 0; i < ACPI_NAME_SIZE; i++) {
256 if (acpi_ut_valid_acpi_char(name[i], i)) {
261 * Replace a bad character with something printable, yet technically
262 * still invalid. This prevents any collisions with existing "good"
263 * names in the namespace.
266 found_bad_char = TRUE;
269 if (found_bad_char) {
271 /* Report warning only if in strict mode or debug mode */
273 if (!acpi_gbl_enable_interpreter_slack) {
274 ACPI_WARNING((AE_INFO,
275 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
276 original_name, name));
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
279 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
280 original_name, name));
285 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
286 /*******************************************************************************
288 * FUNCTION: ut_convert_backslashes
290 * PARAMETERS: pathname - File pathname string to be converted
292 * RETURN: Modifies the input Pathname
294 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
295 * the entire input file pathname string.
297 ******************************************************************************/
299 void ut_convert_backslashes(char *pathname)
307 if (*pathname == '\\') {