1 /******************************************************************************* 2 * 3 * Module Name: utstring - Common functions for strings and characters 4 * 5 ******************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2014, 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 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit 53 * version of strtoul. 54 */ 55 #ifdef ACPI_ASL_COMPILER 56 /******************************************************************************* 57 * 58 * FUNCTION: acpi_ut_strlwr (strlwr) 59 * 60 * PARAMETERS: src_string - The source string to convert 61 * 62 * RETURN: None 63 * 64 * DESCRIPTION: Convert string to lowercase 65 * 66 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c 67 * 68 ******************************************************************************/ 69 void acpi_ut_strlwr(char *src_string) 70 { 71 char *string; 72 73 ACPI_FUNCTION_ENTRY(); 74 75 if (!src_string) { 76 return; 77 } 78 79 /* Walk entire string, lowercasing the letters */ 80 81 for (string = src_string; *string; string++) { 82 *string = (char)ACPI_TOLOWER(*string); 83 } 84 85 return; 86 } 87 88 /****************************************************************************** 89 * 90 * FUNCTION: acpi_ut_stricmp (stricmp) 91 * 92 * PARAMETERS: string1 - first string to compare 93 * string2 - second string to compare 94 * 95 * RETURN: int that signifies string relationship. Zero means strings 96 * are equal. 97 * 98 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare 99 * strings with no case sensitivity) 100 * 101 ******************************************************************************/ 102 103 int acpi_ut_stricmp(char *string1, char *string2) 104 { 105 int c1; 106 int c2; 107 108 do { 109 c1 = tolower((int)*string1); 110 c2 = tolower((int)*string2); 111 112 string1++; 113 string2++; 114 } 115 while ((c1 == c2) && (c1)); 116 117 return (c1 - c2); 118 } 119 #endif 120 121 /******************************************************************************* 122 * 123 * FUNCTION: acpi_ut_strupr (strupr) 124 * 125 * PARAMETERS: src_string - The source string to convert 126 * 127 * RETURN: None 128 * 129 * DESCRIPTION: Convert string to uppercase 130 * 131 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c 132 * 133 ******************************************************************************/ 134 135 void acpi_ut_strupr(char *src_string) 136 { 137 char *string; 138 139 ACPI_FUNCTION_ENTRY(); 140 141 if (!src_string) { 142 return; 143 } 144 145 /* Walk entire string, uppercasing the letters */ 146 147 for (string = src_string; *string; string++) { 148 *string = (char)ACPI_TOUPPER(*string); 149 } 150 151 return; 152 } 153 154 /******************************************************************************* 155 * 156 * FUNCTION: acpi_ut_strtoul64 157 * 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 162 * 163 * RETURN: Status and Converted value 164 * 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. 169 * 170 ******************************************************************************/ 171 172 acpi_status acpi_ut_strtoul64(char *string, u32 base, u64 *ret_integer) 173 { 174 u32 this_digit = 0; 175 u64 return_value = 0; 176 u64 quotient; 177 u64 dividend; 178 u32 to_integer_op = (base == ACPI_ANY_BASE); 179 u32 mode32 = (acpi_gbl_integer_byte_width == 4); 180 u8 valid_digits = 0; 181 u8 sign_of0x = 0; 182 u8 term = 0; 183 184 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string); 185 186 switch (base) { 187 case ACPI_ANY_BASE: 188 case 16: 189 190 break; 191 192 default: 193 194 /* Invalid Base */ 195 196 return_ACPI_STATUS(AE_BAD_PARAMETER); 197 } 198 199 if (!string) { 200 goto error_exit; 201 } 202 203 /* Skip over any white space in the buffer */ 204 205 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) { 206 string++; 207 } 208 209 if (to_integer_op) { 210 /* 211 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'. 212 * We need to determine if it is decimal or hexadecimal. 213 */ 214 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) { 215 sign_of0x = 1; 216 base = 16; 217 218 /* Skip over the leading '0x' */ 219 string += 2; 220 } else { 221 base = 10; 222 } 223 } 224 225 /* Any string left? Check that '0x' is not followed by white space. */ 226 227 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') { 228 if (to_integer_op) { 229 goto error_exit; 230 } else { 231 goto all_done; 232 } 233 } 234 235 /* 236 * Perform a 32-bit or 64-bit conversion, depending upon the current 237 * execution mode of the interpreter 238 */ 239 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX; 240 241 /* Main loop: convert the string to a 32- or 64-bit integer */ 242 243 while (*string) { 244 if (ACPI_IS_DIGIT(*string)) { 245 246 /* Convert ASCII 0-9 to Decimal value */ 247 248 this_digit = ((u8)*string) - '0'; 249 } else if (base == 10) { 250 251 /* Digit is out of range; possible in to_integer case only */ 252 253 term = 1; 254 } else { 255 this_digit = (u8)ACPI_TOUPPER(*string); 256 if (ACPI_IS_XDIGIT((char)this_digit)) { 257 258 /* Convert ASCII Hex char to value */ 259 260 this_digit = this_digit - 'A' + 10; 261 } else { 262 term = 1; 263 } 264 } 265 266 if (term) { 267 if (to_integer_op) { 268 goto error_exit; 269 } else { 270 break; 271 } 272 } else if ((valid_digits == 0) && (this_digit == 0) 273 && !sign_of0x) { 274 275 /* Skip zeros */ 276 string++; 277 continue; 278 } 279 280 valid_digits++; 281 282 if (sign_of0x 283 && ((valid_digits > 16) 284 || ((valid_digits > 8) && mode32))) { 285 /* 286 * This is to_integer operation case. 287 * No any restrictions for string-to-integer conversion, 288 * see ACPI spec. 289 */ 290 goto error_exit; 291 } 292 293 /* Divide the digit into the correct position */ 294 295 (void)acpi_ut_short_divide((dividend - (u64)this_digit), 296 base, "ient, NULL); 297 298 if (return_value > quotient) { 299 if (to_integer_op) { 300 goto error_exit; 301 } else { 302 break; 303 } 304 } 305 306 return_value *= base; 307 return_value += this_digit; 308 string++; 309 } 310 311 /* All done, normal exit */ 312 313 all_done: 314 315 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n", 316 ACPI_FORMAT_UINT64(return_value))); 317 318 *ret_integer = return_value; 319 return_ACPI_STATUS(AE_OK); 320 321 error_exit: 322 /* Base was set/validated above */ 323 324 if (base == 10) { 325 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT); 326 } else { 327 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT); 328 } 329 } 330 331 /******************************************************************************* 332 * 333 * FUNCTION: acpi_ut_print_string 334 * 335 * PARAMETERS: string - Null terminated ASCII string 336 * max_length - Maximum output length. Used to constrain the 337 * length of strings during debug output only. 338 * 339 * RETURN: None 340 * 341 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape 342 * sequences. 343 * 344 ******************************************************************************/ 345 346 void acpi_ut_print_string(char *string, u16 max_length) 347 { 348 u32 i; 349 350 if (!string) { 351 acpi_os_printf("<\"NULL STRING PTR\">"); 352 return; 353 } 354 355 acpi_os_printf("\""); 356 for (i = 0; string[i] && (i < max_length); i++) { 357 358 /* Escape sequences */ 359 360 switch (string[i]) { 361 case 0x07: 362 363 acpi_os_printf("\\a"); /* BELL */ 364 break; 365 366 case 0x08: 367 368 acpi_os_printf("\\b"); /* BACKSPACE */ 369 break; 370 371 case 0x0C: 372 373 acpi_os_printf("\\f"); /* FORMFEED */ 374 break; 375 376 case 0x0A: 377 378 acpi_os_printf("\\n"); /* LINEFEED */ 379 break; 380 381 case 0x0D: 382 383 acpi_os_printf("\\r"); /* CARRIAGE RETURN */ 384 break; 385 386 case 0x09: 387 388 acpi_os_printf("\\t"); /* HORIZONTAL TAB */ 389 break; 390 391 case 0x0B: 392 393 acpi_os_printf("\\v"); /* VERTICAL TAB */ 394 break; 395 396 case '\'': /* Single Quote */ 397 case '\"': /* Double Quote */ 398 case '\\': /* Backslash */ 399 400 acpi_os_printf("\\%c", (int)string[i]); 401 break; 402 403 default: 404 405 /* Check for printable character or hex escape */ 406 407 if (ACPI_IS_PRINT(string[i])) { 408 /* This is a normal character */ 409 410 acpi_os_printf("%c", (int)string[i]); 411 } else { 412 /* All others will be Hex escapes */ 413 414 acpi_os_printf("\\x%2.2X", (s32) string[i]); 415 } 416 break; 417 } 418 } 419 acpi_os_printf("\""); 420 421 if (i == max_length && string[i]) { 422 acpi_os_printf("..."); 423 } 424 } 425 426 /******************************************************************************* 427 * 428 * FUNCTION: acpi_ut_valid_acpi_char 429 * 430 * PARAMETERS: char - The character to be examined 431 * position - Byte position (0-3) 432 * 433 * RETURN: TRUE if the character is valid, FALSE otherwise 434 * 435 * DESCRIPTION: Check for a valid ACPI character. Must be one of: 436 * 1) Upper case alpha 437 * 2) numeric 438 * 3) underscore 439 * 440 * We allow a '!' as the last character because of the ASF! table 441 * 442 ******************************************************************************/ 443 444 u8 acpi_ut_valid_acpi_char(char character, u32 position) 445 { 446 447 if (!((character >= 'A' && character <= 'Z') || 448 (character >= '0' && character <= '9') || (character == '_'))) { 449 450 /* Allow a '!' in the last position */ 451 452 if (character == '!' && position == 3) { 453 return (TRUE); 454 } 455 456 return (FALSE); 457 } 458 459 return (TRUE); 460 } 461 462 /******************************************************************************* 463 * 464 * FUNCTION: acpi_ut_valid_acpi_name 465 * 466 * PARAMETERS: name - The name to be examined. Does not have to 467 * be NULL terminated string. 468 * 469 * RETURN: TRUE if the name is valid, FALSE otherwise 470 * 471 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of: 472 * 1) Upper case alpha 473 * 2) numeric 474 * 3) underscore 475 * 476 ******************************************************************************/ 477 478 u8 acpi_ut_valid_acpi_name(char *name) 479 { 480 u32 i; 481 482 ACPI_FUNCTION_ENTRY(); 483 484 for (i = 0; i < ACPI_NAME_SIZE; i++) { 485 if (!acpi_ut_valid_acpi_char(name[i], i)) { 486 return (FALSE); 487 } 488 } 489 490 return (TRUE); 491 } 492 493 /******************************************************************************* 494 * 495 * FUNCTION: acpi_ut_repair_name 496 * 497 * PARAMETERS: name - The ACPI name to be repaired 498 * 499 * RETURN: Repaired version of the name 500 * 501 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and 502 * return the new name. NOTE: the Name parameter must reside in 503 * read/write memory, cannot be a const. 504 * 505 * An ACPI Name must consist of valid ACPI characters. We will repair the name 506 * if necessary because we don't want to abort because of this, but we want 507 * all namespace names to be printable. A warning message is appropriate. 508 * 509 * This issue came up because there are in fact machines that exhibit 510 * this problem, and we want to be able to enable ACPI support for them, 511 * even though there are a few bad names. 512 * 513 ******************************************************************************/ 514 515 void acpi_ut_repair_name(char *name) 516 { 517 u32 i; 518 u8 found_bad_char = FALSE; 519 u32 original_name; 520 521 ACPI_FUNCTION_NAME(ut_repair_name); 522 523 ACPI_MOVE_NAME(&original_name, name); 524 525 /* Check each character in the name */ 526 527 for (i = 0; i < ACPI_NAME_SIZE; i++) { 528 if (acpi_ut_valid_acpi_char(name[i], i)) { 529 continue; 530 } 531 532 /* 533 * Replace a bad character with something printable, yet technically 534 * still invalid. This prevents any collisions with existing "good" 535 * names in the namespace. 536 */ 537 name[i] = '*'; 538 found_bad_char = TRUE; 539 } 540 541 if (found_bad_char) { 542 543 /* Report warning only if in strict mode or debug mode */ 544 545 if (!acpi_gbl_enable_interpreter_slack) { 546 ACPI_WARNING((AE_INFO, 547 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]", 548 original_name, name)); 549 } else { 550 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 551 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]", 552 original_name, name)); 553 } 554 } 555 } 556 557 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP 558 /******************************************************************************* 559 * 560 * FUNCTION: ut_convert_backslashes 561 * 562 * PARAMETERS: pathname - File pathname string to be converted 563 * 564 * RETURN: Modifies the input Pathname 565 * 566 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within 567 * the entire input file pathname string. 568 * 569 ******************************************************************************/ 570 571 void ut_convert_backslashes(char *pathname) 572 { 573 574 if (!pathname) { 575 return; 576 } 577 578 while (*pathname) { 579 if (*pathname == '\\') { 580 *pathname = '/'; 581 } 582 583 pathname++; 584 } 585 } 586 #endif 587 588 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION) 589 /******************************************************************************* 590 * 591 * FUNCTION: acpi_ut_safe_strcpy, acpi_ut_safe_strcat, acpi_ut_safe_strncat 592 * 593 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string 594 * functions. This is the size of the Destination buffer. 595 * 596 * RETURN: TRUE if the operation would overflow the destination buffer. 597 * 598 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that 599 * the result of the operation will not overflow the output string 600 * buffer. 601 * 602 * NOTE: These functions are typically only helpful for processing 603 * user input and command lines. For most ACPICA code, the 604 * required buffer length is precisely calculated before buffer 605 * allocation, so the use of these functions is unnecessary. 606 * 607 ******************************************************************************/ 608 609 u8 acpi_ut_safe_strcpy(char *dest, acpi_size dest_size, char *source) 610 { 611 612 if (ACPI_STRLEN(source) >= dest_size) { 613 return (TRUE); 614 } 615 616 ACPI_STRCPY(dest, source); 617 return (FALSE); 618 } 619 620 u8 acpi_ut_safe_strcat(char *dest, acpi_size dest_size, char *source) 621 { 622 623 if ((ACPI_STRLEN(dest) + ACPI_STRLEN(source)) >= dest_size) { 624 return (TRUE); 625 } 626 627 ACPI_STRCAT(dest, source); 628 return (FALSE); 629 } 630 631 u8 632 acpi_ut_safe_strncat(char *dest, 633 acpi_size dest_size, 634 char *source, acpi_size max_transfer_length) 635 { 636 acpi_size actual_transfer_length; 637 638 actual_transfer_length = 639 ACPI_MIN(max_transfer_length, ACPI_STRLEN(source)); 640 641 if ((ACPI_STRLEN(dest) + actual_transfer_length) >= dest_size) { 642 return (TRUE); 643 } 644 645 ACPI_STRNCAT(dest, source, max_transfer_length); 646 return (FALSE); 647 } 648 #endif 649