1 /******************************************************************************* 2 * 3 * Module Name: utnonansi - Non-ansi C library functions 4 * 5 ******************************************************************************/ 6 7 /****************************************************************************** 8 * 9 * 1. Copyright Notice 10 * 11 * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp. 12 * All rights reserved. 13 * 14 * 2. License 15 * 16 * 2.1. This is your license from Intel Corp. under its intellectual property 17 * rights. You may have additional license terms from the party that provided 18 * you this software, covering your right to use that party's intellectual 19 * property rights. 20 * 21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 22 * copy of the source code appearing in this file ("Covered Code") an 23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 24 * base code distributed originally by Intel ("Original Intel Code") to copy, 25 * make derivatives, distribute, use and display any portion of the Covered 26 * Code in any form, with the right to sublicense such rights; and 27 * 28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 29 * license (with the right to sublicense), under only those claims of Intel 30 * patents that are infringed by the Original Intel Code, to make, use, sell, 31 * offer to sell, and import the Covered Code and derivative works thereof 32 * solely to the minimum extent necessary to exercise the above copyright 33 * license, and in no event shall the patent license extend to any additions 34 * to or modifications of the Original Intel Code. No other license or right 35 * is granted directly or by implication, estoppel or otherwise; 36 * 37 * The above copyright and patent license is granted only if the following 38 * conditions are met: 39 * 40 * 3. Conditions 41 * 42 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 43 * Redistribution of source code of any substantial portion of the Covered 44 * Code or modification with rights to further distribute source must include 45 * the above Copyright Notice, the above License, this list of Conditions, 46 * and the following Disclaimer and Export Compliance provision. In addition, 47 * Licensee must cause all Covered Code to which Licensee contributes to 48 * contain a file documenting the changes Licensee made to create that Covered 49 * Code and the date of any change. Licensee must include in that file the 50 * documentation of any changes made by any predecessor Licensee. Licensee 51 * must include a prominent statement that the modification is derived, 52 * directly or indirectly, from Original Intel Code. 53 * 54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 55 * Redistribution of source code of any substantial portion of the Covered 56 * Code or modification without rights to further distribute source must 57 * include the following Disclaimer and Export Compliance provision in the 58 * documentation and/or other materials provided with distribution. In 59 * addition, Licensee may not authorize further sublicense of source of any 60 * portion of the Covered Code, and must include terms to the effect that the 61 * license from Licensee to its licensee is limited to the intellectual 62 * property embodied in the software Licensee provides to its licensee, and 63 * not to intellectual property embodied in modifications its licensee may 64 * make. 65 * 66 * 3.3. Redistribution of Executable. Redistribution in executable form of any 67 * substantial portion of the Covered Code or modification must reproduce the 68 * above Copyright Notice, and the following Disclaimer and Export Compliance 69 * provision in the documentation and/or other materials provided with the 70 * distribution. 71 * 72 * 3.4. Intel retains all right, title, and interest in and to the Original 73 * Intel Code. 74 * 75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 76 * Intel shall be used in advertising or otherwise to promote the sale, use or 77 * other dealings in products derived from or relating to the Covered Code 78 * without prior written authorization from Intel. 79 * 80 * 4. Disclaimer and Export Compliance 81 * 82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 88 * PARTICULAR PURPOSE. 89 * 90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 97 * LIMITED REMEDY. 98 * 99 * 4.3. Licensee shall not export, either directly or indirectly, any of this 100 * software or system incorporating such software without first obtaining any 101 * required license or other approval from the U. S. Department of Commerce or 102 * any other agency or department of the United States Government. In the 103 * event Licensee exports any such software from the United States or 104 * re-exports any such software from a foreign destination, Licensee shall 105 * ensure that the distribution and export/re-export of the software is in 106 * compliance with all laws, regulations, orders, or other restrictions of the 107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 108 * any of its subsidiaries will export/re-export any technical data, process, 109 * software, or service, directly or indirectly, to any country for which the 110 * United States government or any agency thereof requires an export license, 111 * other governmental approval, or letter of assurance, without first obtaining 112 * such license, approval or letter. 113 * 114 *****************************************************************************/ 115 116 #include "acpi.h" 117 #include "accommon.h" 118 119 120 #define _COMPONENT ACPI_UTILITIES 121 ACPI_MODULE_NAME ("utnonansi") 122 123 124 /* 125 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit 126 * version of strtoul. 127 */ 128 129 /******************************************************************************* 130 * 131 * FUNCTION: AcpiUtStrlwr (strlwr) 132 * 133 * PARAMETERS: SrcString - The source string to convert 134 * 135 * RETURN: None 136 * 137 * DESCRIPTION: Convert a string to lowercase 138 * 139 ******************************************************************************/ 140 141 void 142 AcpiUtStrlwr ( 143 char *SrcString) 144 { 145 char *String; 146 147 148 ACPI_FUNCTION_ENTRY (); 149 150 151 if (!SrcString) 152 { 153 return; 154 } 155 156 /* Walk entire string, lowercasing the letters */ 157 158 for (String = SrcString; *String; String++) 159 { 160 *String = (char) tolower ((int) *String); 161 } 162 } 163 164 165 /******************************************************************************* 166 * 167 * FUNCTION: AcpiUtStrupr (strupr) 168 * 169 * PARAMETERS: SrcString - The source string to convert 170 * 171 * RETURN: None 172 * 173 * DESCRIPTION: Convert a string to uppercase 174 * 175 ******************************************************************************/ 176 177 void 178 AcpiUtStrupr ( 179 char *SrcString) 180 { 181 char *String; 182 183 184 ACPI_FUNCTION_ENTRY (); 185 186 187 if (!SrcString) 188 { 189 return; 190 } 191 192 /* Walk entire string, uppercasing the letters */ 193 194 for (String = SrcString; *String; String++) 195 { 196 *String = (char) toupper ((int) *String); 197 } 198 } 199 200 201 /****************************************************************************** 202 * 203 * FUNCTION: AcpiUtStricmp (stricmp) 204 * 205 * PARAMETERS: String1 - first string to compare 206 * String2 - second string to compare 207 * 208 * RETURN: int that signifies string relationship. Zero means strings 209 * are equal. 210 * 211 * DESCRIPTION: Case-insensitive string compare. Implementation of the 212 * non-ANSI stricmp function. 213 * 214 ******************************************************************************/ 215 216 int 217 AcpiUtStricmp ( 218 char *String1, 219 char *String2) 220 { 221 int c1; 222 int c2; 223 224 225 do 226 { 227 c1 = tolower ((int) *String1); 228 c2 = tolower ((int) *String2); 229 230 String1++; 231 String2++; 232 } 233 while ((c1 == c2) && (c1)); 234 235 return (c1 - c2); 236 } 237 238 239 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION) 240 /******************************************************************************* 241 * 242 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat 243 * 244 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string 245 * functions. This is the size of the Destination buffer. 246 * 247 * RETURN: TRUE if the operation would overflow the destination buffer. 248 * 249 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that 250 * the result of the operation will not overflow the output string 251 * buffer. 252 * 253 * NOTE: These functions are typically only helpful for processing 254 * user input and command lines. For most ACPICA code, the 255 * required buffer length is precisely calculated before buffer 256 * allocation, so the use of these functions is unnecessary. 257 * 258 ******************************************************************************/ 259 260 BOOLEAN 261 AcpiUtSafeStrcpy ( 262 char *Dest, 263 ACPI_SIZE DestSize, 264 char *Source) 265 { 266 267 if (strlen (Source) >= DestSize) 268 { 269 return (TRUE); 270 } 271 272 strcpy (Dest, Source); 273 return (FALSE); 274 } 275 276 BOOLEAN 277 AcpiUtSafeStrcat ( 278 char *Dest, 279 ACPI_SIZE DestSize, 280 char *Source) 281 { 282 283 if ((strlen (Dest) + strlen (Source)) >= DestSize) 284 { 285 return (TRUE); 286 } 287 288 strcat (Dest, Source); 289 return (FALSE); 290 } 291 292 BOOLEAN 293 AcpiUtSafeStrncat ( 294 char *Dest, 295 ACPI_SIZE DestSize, 296 char *Source, 297 ACPI_SIZE MaxTransferLength) 298 { 299 ACPI_SIZE ActualTransferLength; 300 301 302 ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source)); 303 304 if ((strlen (Dest) + ActualTransferLength) >= DestSize) 305 { 306 return (TRUE); 307 } 308 309 strncat (Dest, Source, MaxTransferLength); 310 return (FALSE); 311 } 312 #endif 313 314 315 /******************************************************************************* 316 * 317 * FUNCTION: AcpiUtStrtoul64 318 * 319 * PARAMETERS: String - Null terminated string 320 * Base - Radix of the string: 16 or 10 or 321 * ACPI_ANY_BASE 322 * MaxIntegerByteWidth - Maximum allowable integer,in bytes: 323 * 4 or 8 (32 or 64 bits) 324 * RetInteger - Where the converted integer is 325 * returned 326 * 327 * RETURN: Status and Converted value 328 * 329 * DESCRIPTION: Convert a string into an unsigned value. Performs either a 330 * 32-bit or 64-bit conversion, depending on the input integer 331 * size (often the current mode of the interpreter). 332 * 333 * NOTES: Negative numbers are not supported, as they are not supported 334 * by ACPI. 335 * 336 * AcpiGbl_IntegerByteWidth should be set to the proper width. 337 * For the core ACPICA code, this width depends on the DSDT 338 * version. For iASL, the default byte width is always 8 for the 339 * parser, but error checking is performed later to flag cases 340 * where a 64-bit constant is defined in a 32-bit DSDT/SSDT. 341 * 342 * Does not support Octal strings, not needed at this time. 343 * 344 ******************************************************************************/ 345 346 ACPI_STATUS 347 AcpiUtStrtoul64 ( 348 char *String, 349 UINT32 Base, 350 UINT32 MaxIntegerByteWidth, 351 UINT64 *RetInteger) 352 { 353 UINT32 ThisDigit = 0; 354 UINT64 ReturnValue = 0; 355 UINT64 Quotient; 356 UINT64 Dividend; 357 UINT8 ValidDigits = 0; 358 UINT8 SignOf0x = 0; 359 UINT8 Term = 0; 360 361 362 ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String); 363 364 365 switch (Base) 366 { 367 case ACPI_ANY_BASE: 368 case 10: 369 case 16: 370 371 break; 372 373 default: 374 375 /* Invalid Base */ 376 377 return_ACPI_STATUS (AE_BAD_PARAMETER); 378 } 379 380 if (!String) 381 { 382 goto ErrorExit; 383 } 384 385 /* Skip over any white space in the buffer */ 386 387 while ((*String) && (isspace ((int) *String) || *String == '\t')) 388 { 389 String++; 390 } 391 392 if (Base == ACPI_ANY_BASE) 393 { 394 /* 395 * Base equal to ACPI_ANY_BASE means 'Either decimal or hex'. 396 * We need to determine if it is decimal or hexadecimal. 397 */ 398 if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x')) 399 { 400 SignOf0x = 1; 401 Base = 16; 402 403 /* Skip over the leading '0x' */ 404 String += 2; 405 } 406 else 407 { 408 Base = 10; 409 } 410 } 411 412 /* Any string left? Check that '0x' is not followed by white space. */ 413 414 if (!(*String) || isspace ((int) *String) || *String == '\t') 415 { 416 if (Base == ACPI_ANY_BASE) 417 { 418 goto ErrorExit; 419 } 420 else 421 { 422 goto AllDone; 423 } 424 } 425 426 /* 427 * Perform a 32-bit or 64-bit conversion, depending upon the input 428 * byte width 429 */ 430 Dividend = (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH) ? 431 ACPI_UINT32_MAX : ACPI_UINT64_MAX; 432 433 /* Main loop: convert the string to a 32- or 64-bit integer */ 434 435 while (*String) 436 { 437 if (isdigit ((int) *String)) 438 { 439 /* Convert ASCII 0-9 to Decimal value */ 440 441 ThisDigit = ((UINT8) *String) - '0'; 442 } 443 else if (Base == 10) 444 { 445 /* Digit is out of range; possible in ToInteger case only */ 446 447 Term = 1; 448 } 449 else 450 { 451 ThisDigit = (UINT8) toupper ((int) *String); 452 if (isxdigit ((int) ThisDigit)) 453 { 454 /* Convert ASCII Hex char to value */ 455 456 ThisDigit = ThisDigit - 'A' + 10; 457 } 458 else 459 { 460 Term = 1; 461 } 462 } 463 464 if (Term) 465 { 466 if (Base == ACPI_ANY_BASE) 467 { 468 goto ErrorExit; 469 } 470 else 471 { 472 break; 473 } 474 } 475 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x) 476 { 477 /* Skip zeros */ 478 String++; 479 continue; 480 } 481 482 ValidDigits++; 483 484 if (SignOf0x && ((ValidDigits > 16) || 485 ((ValidDigits > 8) && (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH)))) 486 { 487 /* 488 * This is ToInteger operation case. 489 * No restrictions for string-to-integer conversion, 490 * see ACPI spec. 491 */ 492 goto ErrorExit; 493 } 494 495 /* Divide the digit into the correct position */ 496 497 (void) AcpiUtShortDivide ( 498 (Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL); 499 500 if (ReturnValue > Quotient) 501 { 502 if (Base == ACPI_ANY_BASE) 503 { 504 goto ErrorExit; 505 } 506 else 507 { 508 break; 509 } 510 } 511 512 ReturnValue *= Base; 513 ReturnValue += ThisDigit; 514 String++; 515 } 516 517 /* All done, normal exit */ 518 519 AllDone: 520 521 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n", 522 ACPI_FORMAT_UINT64 (ReturnValue))); 523 524 *RetInteger = ReturnValue; 525 return_ACPI_STATUS (AE_OK); 526 527 528 ErrorExit: 529 530 /* Base was set/validated above (10 or 16) */ 531 532 if (Base == 10) 533 { 534 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT); 535 } 536 else 537 { 538 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT); 539 } 540 } 541 542 543 #ifdef _OBSOLETE_FUNCTIONS 544 /* Removed: 01/2016 */ 545 546 /******************************************************************************* 547 * 548 * FUNCTION: strtoul64 549 * 550 * PARAMETERS: String - Null terminated string 551 * Terminater - Where a pointer to the terminating byte 552 * is returned 553 * Base - Radix of the string 554 * 555 * RETURN: Converted value 556 * 557 * DESCRIPTION: Convert a string into an unsigned value. 558 * 559 ******************************************************************************/ 560 561 ACPI_STATUS 562 strtoul64 ( 563 char *String, 564 UINT32 Base, 565 UINT64 *RetInteger) 566 { 567 UINT32 Index; 568 UINT32 Sign; 569 UINT64 ReturnValue = 0; 570 ACPI_STATUS Status = AE_OK; 571 572 573 *RetInteger = 0; 574 575 switch (Base) 576 { 577 case 0: 578 case 8: 579 case 10: 580 case 16: 581 582 break; 583 584 default: 585 /* 586 * The specified Base parameter is not in the domain of 587 * this function: 588 */ 589 return (AE_BAD_PARAMETER); 590 } 591 592 /* Skip over any white space in the buffer: */ 593 594 while (isspace ((int) *String) || *String == '\t') 595 { 596 ++String; 597 } 598 599 /* 600 * The buffer may contain an optional plus or minus sign. 601 * If it does, then skip over it but remember what is was: 602 */ 603 if (*String == '-') 604 { 605 Sign = ACPI_SIGN_NEGATIVE; 606 ++String; 607 } 608 else if (*String == '+') 609 { 610 ++String; 611 Sign = ACPI_SIGN_POSITIVE; 612 } 613 else 614 { 615 Sign = ACPI_SIGN_POSITIVE; 616 } 617 618 /* 619 * If the input parameter Base is zero, then we need to 620 * determine if it is octal, decimal, or hexadecimal: 621 */ 622 if (Base == 0) 623 { 624 if (*String == '0') 625 { 626 if (tolower ((int) *(++String)) == 'x') 627 { 628 Base = 16; 629 ++String; 630 } 631 else 632 { 633 Base = 8; 634 } 635 } 636 else 637 { 638 Base = 10; 639 } 640 } 641 642 /* 643 * For octal and hexadecimal bases, skip over the leading 644 * 0 or 0x, if they are present. 645 */ 646 if (Base == 8 && *String == '0') 647 { 648 String++; 649 } 650 651 if (Base == 16 && 652 *String == '0' && 653 tolower ((int) *(++String)) == 'x') 654 { 655 String++; 656 } 657 658 /* Main loop: convert the string to an unsigned long */ 659 660 while (*String) 661 { 662 if (isdigit ((int) *String)) 663 { 664 Index = ((UINT8) *String) - '0'; 665 } 666 else 667 { 668 Index = (UINT8) toupper ((int) *String); 669 if (isupper ((int) Index)) 670 { 671 Index = Index - 'A' + 10; 672 } 673 else 674 { 675 goto ErrorExit; 676 } 677 } 678 679 if (Index >= Base) 680 { 681 goto ErrorExit; 682 } 683 684 /* Check to see if value is out of range: */ 685 686 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) / 687 (UINT64) Base)) 688 { 689 goto ErrorExit; 690 } 691 else 692 { 693 ReturnValue *= Base; 694 ReturnValue += Index; 695 } 696 697 ++String; 698 } 699 700 701 /* If a minus sign was present, then "the conversion is negated": */ 702 703 if (Sign == ACPI_SIGN_NEGATIVE) 704 { 705 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1; 706 } 707 708 *RetInteger = ReturnValue; 709 return (Status); 710 711 712 ErrorExit: 713 switch (Base) 714 { 715 case 8: 716 717 Status = AE_BAD_OCTAL_CONSTANT; 718 break; 719 720 case 10: 721 722 Status = AE_BAD_DECIMAL_CONSTANT; 723 break; 724 725 case 16: 726 727 Status = AE_BAD_HEX_CONSTANT; 728 break; 729 730 default: 731 732 /* Base validated above */ 733 734 break; 735 } 736 737 return (Status); 738 } 739 #endif 740