1 /*******************************************************************************
3 * Module Name: utstring - Common functions for strings and characters
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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 #define __UTSTRING_C__
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utstring")
56 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
60 #ifdef ACPI_ASL_COMPILER
61 /*******************************************************************************
63 * FUNCTION: AcpiUtStrlwr (strlwr)
65 * PARAMETERS: SrcString - The source string to convert
69 * DESCRIPTION: Convert string to lowercase
71 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
73 ******************************************************************************/
82 ACPI_FUNCTION_ENTRY ();
90 /* Walk entire string, lowercasing the letters */
92 for (String = SrcString; *String; String++)
94 *String = (char) ACPI_TOLOWER (*String);
101 /******************************************************************************
103 * FUNCTION: AcpiUtStricmp (stricmp)
105 * PARAMETERS: String1 - first string to compare
106 * String2 - second string to compare
108 * RETURN: int that signifies string relationship. Zero means strings
111 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
112 * strings with no case sensitivity)
114 ******************************************************************************/
127 c1 = tolower ((int) *String1);
128 c2 = tolower ((int) *String2);
133 while ((c1 == c2) && (c1));
140 /*******************************************************************************
142 * FUNCTION: AcpiUtStrupr (strupr)
144 * PARAMETERS: SrcString - The source string to convert
148 * DESCRIPTION: Convert string to uppercase
150 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
152 ******************************************************************************/
161 ACPI_FUNCTION_ENTRY ();
169 /* Walk entire string, uppercasing the letters */
171 for (String = SrcString; *String; String++)
173 *String = (char) ACPI_TOUPPER (*String);
180 /*******************************************************************************
182 * FUNCTION: AcpiUtStrtoul64
184 * PARAMETERS: String - Null terminated string
185 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
186 * ACPI_ANY_BASE means 'in behalf of ToInteger'
187 * RetInteger - Where the converted integer is returned
189 * RETURN: Status and Converted value
191 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
192 * 32-bit or 64-bit conversion, depending on the current mode
193 * of the interpreter.
194 * NOTE: Does not support Octal strings, not needed.
196 ******************************************************************************/
204 UINT32 ThisDigit = 0;
205 UINT64 ReturnValue = 0;
208 UINT32 ToIntegerOp = (Base == ACPI_ANY_BASE);
209 UINT32 Mode32 = (AcpiGbl_IntegerByteWidth == 4);
210 UINT8 ValidDigits = 0;
215 ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
229 return_ACPI_STATUS (AE_BAD_PARAMETER);
237 /* Skip over any white space in the buffer */
239 while ((*String) && (ACPI_IS_SPACE (*String) || *String == '\t'))
247 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
248 * We need to determine if it is decimal or hexadecimal.
250 if ((*String == '0') && (ACPI_TOLOWER (*(String + 1)) == 'x'))
255 /* Skip over the leading '0x' */
264 /* Any string left? Check that '0x' is not followed by white space. */
266 if (!(*String) || ACPI_IS_SPACE (*String) || *String == '\t')
279 * Perform a 32-bit or 64-bit conversion, depending upon the current
280 * execution mode of the interpreter
282 Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
284 /* Main loop: convert the string to a 32- or 64-bit integer */
288 if (ACPI_IS_DIGIT (*String))
290 /* Convert ASCII 0-9 to Decimal value */
292 ThisDigit = ((UINT8) *String) - '0';
296 /* Digit is out of range; possible in ToInteger case only */
302 ThisDigit = (UINT8) ACPI_TOUPPER (*String);
303 if (ACPI_IS_XDIGIT ((char) ThisDigit))
305 /* Convert ASCII Hex char to value */
307 ThisDigit = ThisDigit - 'A' + 10;
326 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
335 if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
338 * This is ToInteger operation case.
339 * No any restrictions for string-to-integer conversion,
345 /* Divide the digit into the correct position */
347 (void) AcpiUtShortDivide ((Dividend - (UINT64) ThisDigit),
348 Base, &Quotient, NULL);
350 if (ReturnValue > Quotient)
363 ReturnValue += ThisDigit;
367 /* All done, normal exit */
371 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
372 ACPI_FORMAT_UINT64 (ReturnValue)));
374 *RetInteger = ReturnValue;
375 return_ACPI_STATUS (AE_OK);
379 /* Base was set/validated above */
383 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
387 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
392 /*******************************************************************************
394 * FUNCTION: AcpiUtPrintString
396 * PARAMETERS: String - Null terminated ASCII string
397 * MaxLength - Maximum output length. Used to constrain the
398 * length of strings during debug output only.
402 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
405 ******************************************************************************/
417 AcpiOsPrintf ("<\"NULL STRING PTR\">");
422 for (i = 0; (i < MaxLength) && String[i]; i++)
424 /* Escape sequences */
430 AcpiOsPrintf ("\\a"); /* BELL */
435 AcpiOsPrintf ("\\b"); /* BACKSPACE */
440 AcpiOsPrintf ("\\f"); /* FORMFEED */
445 AcpiOsPrintf ("\\n"); /* LINEFEED */
450 AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
455 AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
460 AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
463 case '\'': /* Single Quote */
464 case '\"': /* Double Quote */
465 case '\\': /* Backslash */
467 AcpiOsPrintf ("\\%c", (int) String[i]);
472 /* Check for printable character or hex escape */
474 if (ACPI_IS_PRINT (String[i]))
476 /* This is a normal character */
478 AcpiOsPrintf ("%c", (int) String[i]);
482 /* All others will be Hex escapes */
484 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
491 if (i == MaxLength && String[i])
493 AcpiOsPrintf ("...");
498 /*******************************************************************************
500 * FUNCTION: AcpiUtValidAcpiChar
502 * PARAMETERS: Char - The character to be examined
503 * Position - Byte position (0-3)
505 * RETURN: TRUE if the character is valid, FALSE otherwise
507 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
508 * 1) Upper case alpha
512 * We allow a '!' as the last character because of the ASF! table
514 ******************************************************************************/
517 AcpiUtValidAcpiChar (
522 if (!((Character >= 'A' && Character <= 'Z') ||
523 (Character >= '0' && Character <= '9') ||
526 /* Allow a '!' in the last position */
528 if (Character == '!' && Position == 3)
540 /*******************************************************************************
542 * FUNCTION: AcpiUtValidAcpiName
544 * PARAMETERS: Name - The name to be examined. Does not have to
545 * be NULL terminated string.
547 * RETURN: TRUE if the name is valid, FALSE otherwise
549 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
550 * 1) Upper case alpha
554 ******************************************************************************/
557 AcpiUtValidAcpiName (
563 ACPI_FUNCTION_ENTRY ();
566 for (i = 0; i < ACPI_NAME_SIZE; i++)
568 if (!AcpiUtValidAcpiChar (Name[i], i))
578 /*******************************************************************************
580 * FUNCTION: AcpiUtRepairName
582 * PARAMETERS: Name - The ACPI name to be repaired
584 * RETURN: Repaired version of the name
586 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
587 * return the new name. NOTE: the Name parameter must reside in
588 * read/write memory, cannot be a const.
590 * An ACPI Name must consist of valid ACPI characters. We will repair the name
591 * if necessary because we don't want to abort because of this, but we want
592 * all namespace names to be printable. A warning message is appropriate.
594 * This issue came up because there are in fact machines that exhibit
595 * this problem, and we want to be able to enable ACPI support for them,
596 * even though there are a few bad names.
598 ******************************************************************************/
605 BOOLEAN FoundBadChar = FALSE;
609 ACPI_FUNCTION_NAME (UtRepairName);
612 ACPI_MOVE_NAME (&OriginalName, Name);
614 /* Check each character in the name */
616 for (i = 0; i < ACPI_NAME_SIZE; i++)
618 if (AcpiUtValidAcpiChar (Name[i], i))
624 * Replace a bad character with something printable, yet technically
625 * still invalid. This prevents any collisions with existing "good"
626 * names in the namespace.
634 /* Report warning only if in strict mode or debug mode */
636 if (!AcpiGbl_EnableInterpreterSlack)
638 ACPI_WARNING ((AE_INFO,
639 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
640 OriginalName, Name));
644 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
645 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
646 OriginalName, Name));
652 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
653 /*******************************************************************************
655 * FUNCTION: UtConvertBackslashes
657 * PARAMETERS: Pathname - File pathname string to be converted
659 * RETURN: Modifies the input Pathname
661 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
662 * the entire input file pathname string.
664 ******************************************************************************/
667 UtConvertBackslashes (
678 if (*Pathname == '\\')
688 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
689 /*******************************************************************************
691 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
693 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
694 * functions. This is the size of the Destination buffer.
696 * RETURN: TRUE if the operation would overflow the destination buffer.
698 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
699 * the result of the operation will not overflow the output string
702 * NOTE: These functions are typically only helpful for processing
703 * user input and command lines. For most ACPICA code, the
704 * required buffer length is precisely calculated before buffer
705 * allocation, so the use of these functions is unnecessary.
707 ******************************************************************************/
716 if (ACPI_STRLEN (Source) >= DestSize)
721 ACPI_STRCPY (Dest, Source);
732 if ((ACPI_STRLEN (Dest) + ACPI_STRLEN (Source)) >= DestSize)
737 ACPI_STRCAT (Dest, Source);
746 ACPI_SIZE MaxTransferLength)
748 ACPI_SIZE ActualTransferLength;
751 ActualTransferLength = ACPI_MIN (MaxTransferLength, ACPI_STRLEN (Source));
753 if ((ACPI_STRLEN (Dest) + ActualTransferLength) >= DestSize)
758 ACPI_STRNCAT (Dest, Source, MaxTransferLength);