1 /*******************************************************************************
3 * Module Name: utstring - Common functions for strings and characters
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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")
52 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
55 #ifdef ACPI_ASL_COMPILER
56 /*******************************************************************************
58 * FUNCTION: acpi_ut_strlwr (strlwr)
60 * PARAMETERS: src_string - The source string to convert
64 * DESCRIPTION: Convert string to lowercase
66 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
68 ******************************************************************************/
69 void acpi_ut_strlwr(char *src_string)
73 ACPI_FUNCTION_ENTRY();
79 /* Walk entire string, lowercasing the letters */
81 for (string = src_string; *string; string++) {
82 *string = (char)ACPI_TOLOWER(*string);
88 /******************************************************************************
90 * FUNCTION: acpi_ut_stricmp (stricmp)
92 * PARAMETERS: string1 - first string to compare
93 * string2 - second string to compare
95 * RETURN: int that signifies string relationship. Zero means strings
98 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
99 * strings with no case sensitivity)
101 ******************************************************************************/
103 int acpi_ut_stricmp(char *string1, char *string2)
109 c1 = tolower((int)*string1);
110 c2 = tolower((int)*string2);
115 while ((c1 == c2) && (c1));
121 /*******************************************************************************
123 * FUNCTION: acpi_ut_strupr (strupr)
125 * PARAMETERS: src_string - The source string to convert
129 * DESCRIPTION: Convert string to uppercase
131 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
133 ******************************************************************************/
135 void acpi_ut_strupr(char *src_string)
139 ACPI_FUNCTION_ENTRY();
145 /* Walk entire string, uppercasing the letters */
147 for (string = src_string; *string; string++) {
148 *string = (char)ACPI_TOUPPER(*string);
154 /*******************************************************************************
156 * FUNCTION: acpi_ut_strtoul64
158 * PARAMETERS: string - Null terminated string
159 * base - Radix of the string: 16 or ACPI_ANY_BASE;
160 * ACPI_ANY_BASE means 'in behalf of to_integer'
161 * ret_integer - Where the converted integer is returned
163 * RETURN: Status and Converted value
165 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
166 * 32-bit or 64-bit conversion, depending on the current mode
167 * of the interpreter.
168 * NOTE: Does not support Octal strings, not needed.
170 ******************************************************************************/
172 acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer)
175 u64 return_value = 0;
178 u32 to_integer_op = (base == ACPI_ANY_BASE);
179 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
184 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
196 return_ACPI_STATUS(AE_BAD_PARAMETER);
203 /* Skip over any white space in the buffer */
205 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
211 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
212 * We need to determine if it is decimal or hexadecimal.
214 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
218 /* Skip over the leading '0x' */
225 /* Any string left? Check that '0x' is not followed by white space. */
227 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
236 * Perform a 32-bit or 64-bit conversion, depending upon the current
237 * execution mode of the interpreter
239 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
241 /* Main loop: convert the string to a 32- or 64-bit integer */
244 if (ACPI_IS_DIGIT(*string)) {
246 /* Convert ASCII 0-9 to Decimal value */
248 this_digit = ((u8)*string) - '0';
249 } else if (base == 10) {
251 /* Digit is out of range; possible in to_integer case only */
255 this_digit = (u8)ACPI_TOUPPER(*string);
256 if (ACPI_IS_XDIGIT((char)this_digit)) {
258 /* Convert ASCII Hex char to value */
260 this_digit = this_digit - 'A' + 10;
272 } else if ((valid_digits == 0) && (this_digit == 0)
283 && ((valid_digits > 16)
284 || ((valid_digits > 8) && mode32))) {
286 * This is to_integer operation case.
287 * No any restrictions for string-to-integer conversion,
293 /* Divide the digit into the correct position */
295 (void)acpi_ut_short_divide((dividend - (u64)this_digit),
296 base, "ient, NULL);
298 if (return_value > quotient) {
306 return_value *= base;
307 return_value += this_digit;
311 /* All done, normal exit */
315 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
316 ACPI_FORMAT_UINT64(return_value)));
318 *ret_integer = return_value;
319 return_ACPI_STATUS(AE_OK);
322 /* Base was set/validated above */
325 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
327 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
331 /*******************************************************************************
333 * FUNCTION: acpi_ut_print_string
335 * PARAMETERS: string - Null terminated ASCII string
336 * max_length - Maximum output length
340 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
343 ******************************************************************************/
345 void acpi_ut_print_string(char *string, u8 max_length)
350 acpi_os_printf("<\"NULL STRING PTR\">");
354 acpi_os_printf("\"");
355 for (i = 0; string[i] && (i < max_length); i++) {
357 /* Escape sequences */
362 acpi_os_printf("\\a"); /* BELL */
367 acpi_os_printf("\\b"); /* BACKSPACE */
372 acpi_os_printf("\\f"); /* FORMFEED */
377 acpi_os_printf("\\n"); /* LINEFEED */
382 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
387 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
392 acpi_os_printf("\\v"); /* VERTICAL TAB */
395 case '\'': /* Single Quote */
396 case '\"': /* Double Quote */
397 case '\\': /* Backslash */
399 acpi_os_printf("\\%c", (int)string[i]);
404 /* Check for printable character or hex escape */
406 if (ACPI_IS_PRINT(string[i])) {
407 /* This is a normal character */
409 acpi_os_printf("%c", (int)string[i]);
411 /* All others will be Hex escapes */
413 acpi_os_printf("\\x%2.2X", (s32) string[i]);
418 acpi_os_printf("\"");
420 if (i == max_length && string[i]) {
421 acpi_os_printf("...");
425 /*******************************************************************************
427 * FUNCTION: acpi_ut_valid_acpi_char
429 * PARAMETERS: char - The character to be examined
430 * position - Byte position (0-3)
432 * RETURN: TRUE if the character is valid, FALSE otherwise
434 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
435 * 1) Upper case alpha
439 * We allow a '!' as the last character because of the ASF! table
441 ******************************************************************************/
443 u8 acpi_ut_valid_acpi_char(char character, u32 position)
446 if (!((character >= 'A' && character <= 'Z') ||
447 (character >= '0' && character <= '9') || (character == '_'))) {
449 /* Allow a '!' in the last position */
451 if (character == '!' && position == 3) {
461 /*******************************************************************************
463 * FUNCTION: acpi_ut_valid_acpi_name
465 * PARAMETERS: name - The name to be examined
467 * RETURN: TRUE if the name is valid, FALSE otherwise
469 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
470 * 1) Upper case alpha
474 ******************************************************************************/
476 u8 acpi_ut_valid_acpi_name(u32 name)
480 ACPI_FUNCTION_ENTRY();
482 for (i = 0; i < ACPI_NAME_SIZE; i++) {
483 if (!acpi_ut_valid_acpi_char
484 ((ACPI_CAST_PTR(char, &name))[i], i)) {
492 /*******************************************************************************
494 * FUNCTION: acpi_ut_repair_name
496 * PARAMETERS: name - The ACPI name to be repaired
498 * RETURN: Repaired version of the name
500 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
501 * return the new name. NOTE: the Name parameter must reside in
502 * read/write memory, cannot be a const.
504 * An ACPI Name must consist of valid ACPI characters. We will repair the name
505 * if necessary because we don't want to abort because of this, but we want
506 * all namespace names to be printable. A warning message is appropriate.
508 * This issue came up because there are in fact machines that exhibit
509 * this problem, and we want to be able to enable ACPI support for them,
510 * even though there are a few bad names.
512 ******************************************************************************/
514 void acpi_ut_repair_name(char *name)
517 u8 found_bad_char = FALSE;
520 ACPI_FUNCTION_NAME(ut_repair_name);
522 ACPI_MOVE_NAME(&original_name, name);
524 /* Check each character in the name */
526 for (i = 0; i < ACPI_NAME_SIZE; i++) {
527 if (acpi_ut_valid_acpi_char(name[i], i)) {
532 * Replace a bad character with something printable, yet technically
533 * still invalid. This prevents any collisions with existing "good"
534 * names in the namespace.
537 found_bad_char = TRUE;
540 if (found_bad_char) {
542 /* Report warning only if in strict mode or debug mode */
544 if (!acpi_gbl_enable_interpreter_slack) {
545 ACPI_WARNING((AE_INFO,
546 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
547 original_name, name));
549 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
550 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
551 original_name, name));
556 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
557 /*******************************************************************************
559 * FUNCTION: ut_convert_backslashes
561 * PARAMETERS: pathname - File pathname string to be converted
563 * RETURN: Modifies the input Pathname
565 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
566 * the entire input file pathname string.
568 ******************************************************************************/
570 void ut_convert_backslashes(char *pathname)
578 if (*pathname == '\\') {