1 /*******************************************************************************
3 * Module Name: utstring - Common functions for strings and characters
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utstring")
54 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
58 #ifdef ACPI_ASL_COMPILER
59 /*******************************************************************************
61 * FUNCTION: AcpiUtStrlwr (strlwr)
63 * PARAMETERS: SrcString - The source string to convert
67 * DESCRIPTION: Convert string to lowercase
69 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
71 ******************************************************************************/
80 ACPI_FUNCTION_ENTRY ();
88 /* Walk entire string, lowercasing the letters */
90 for (String = SrcString; *String; String++)
92 *String = (char) tolower ((int) *String);
99 /******************************************************************************
101 * FUNCTION: AcpiUtStricmp (stricmp)
103 * PARAMETERS: String1 - first string to compare
104 * String2 - second string to compare
106 * RETURN: int that signifies string relationship. Zero means strings
109 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
110 * strings with no case sensitivity)
112 ******************************************************************************/
125 c1 = tolower ((int) *String1);
126 c2 = tolower ((int) *String2);
131 while ((c1 == c2) && (c1));
138 /*******************************************************************************
140 * FUNCTION: AcpiUtStrupr (strupr)
142 * PARAMETERS: SrcString - The source string to convert
146 * DESCRIPTION: Convert string to uppercase
148 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
150 ******************************************************************************/
159 ACPI_FUNCTION_ENTRY ();
167 /* Walk entire string, uppercasing the letters */
169 for (String = SrcString; *String; String++)
171 *String = (char) toupper ((int) *String);
178 /*******************************************************************************
180 * FUNCTION: AcpiUtStrtoul64
182 * PARAMETERS: String - Null terminated string
183 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
184 * ACPI_ANY_BASE means 'in behalf of ToInteger'
185 * RetInteger - Where the converted integer is returned
187 * RETURN: Status and Converted value
189 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
190 * 32-bit or 64-bit conversion, depending on the current mode
191 * of the interpreter.
192 * NOTE: Does not support Octal strings, not needed.
194 ******************************************************************************/
202 UINT32 ThisDigit = 0;
203 UINT64 ReturnValue = 0;
206 UINT32 ToIntegerOp = (Base == ACPI_ANY_BASE);
207 UINT32 Mode32 = (AcpiGbl_IntegerByteWidth == 4);
208 UINT8 ValidDigits = 0;
213 ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
227 return_ACPI_STATUS (AE_BAD_PARAMETER);
235 /* Skip over any white space in the buffer */
237 while ((*String) && (isspace ((int) *String) || *String == '\t'))
245 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
246 * We need to determine if it is decimal or hexadecimal.
248 if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x'))
253 /* Skip over the leading '0x' */
262 /* Any string left? Check that '0x' is not followed by white space. */
264 if (!(*String) || isspace ((int) *String) || *String == '\t')
277 * Perform a 32-bit or 64-bit conversion, depending upon the current
278 * execution mode of the interpreter
280 Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
282 /* Main loop: convert the string to a 32- or 64-bit integer */
286 if (isdigit ((int) *String))
288 /* Convert ASCII 0-9 to Decimal value */
290 ThisDigit = ((UINT8) *String) - '0';
294 /* Digit is out of range; possible in ToInteger case only */
300 ThisDigit = (UINT8) toupper ((int) *String);
301 if (isxdigit ((int) ThisDigit))
303 /* Convert ASCII Hex char to value */
305 ThisDigit = ThisDigit - 'A' + 10;
324 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
333 if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
336 * This is ToInteger operation case.
337 * No any restrictions for string-to-integer conversion,
343 /* Divide the digit into the correct position */
345 (void) AcpiUtShortDivide ((Dividend - (UINT64) ThisDigit),
346 Base, &Quotient, NULL);
348 if (ReturnValue > Quotient)
361 ReturnValue += ThisDigit;
365 /* All done, normal exit */
369 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
370 ACPI_FORMAT_UINT64 (ReturnValue)));
372 *RetInteger = ReturnValue;
373 return_ACPI_STATUS (AE_OK);
377 /* Base was set/validated above */
381 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
385 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
390 /*******************************************************************************
392 * FUNCTION: AcpiUtPrintString
394 * PARAMETERS: String - Null terminated ASCII string
395 * MaxLength - Maximum output length. Used to constrain the
396 * length of strings during debug output only.
400 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
403 ******************************************************************************/
415 AcpiOsPrintf ("<\"NULL STRING PTR\">");
420 for (i = 0; (i < MaxLength) && String[i]; i++)
422 /* Escape sequences */
428 AcpiOsPrintf ("\\a"); /* BELL */
433 AcpiOsPrintf ("\\b"); /* BACKSPACE */
438 AcpiOsPrintf ("\\f"); /* FORMFEED */
443 AcpiOsPrintf ("\\n"); /* LINEFEED */
448 AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
453 AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
458 AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
461 case '\'': /* Single Quote */
462 case '\"': /* Double Quote */
463 case '\\': /* Backslash */
465 AcpiOsPrintf ("\\%c", (int) String[i]);
470 /* Check for printable character or hex escape */
472 if (isprint ((int) String[i]))
474 /* This is a normal character */
476 AcpiOsPrintf ("%c", (int) String[i]);
480 /* All others will be Hex escapes */
482 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
489 if (i == MaxLength && String[i])
491 AcpiOsPrintf ("...");
496 /*******************************************************************************
498 * FUNCTION: AcpiUtValidAcpiChar
500 * PARAMETERS: Char - The character to be examined
501 * Position - Byte position (0-3)
503 * RETURN: TRUE if the character is valid, FALSE otherwise
505 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
506 * 1) Upper case alpha
510 * We allow a '!' as the last character because of the ASF! table
512 ******************************************************************************/
515 AcpiUtValidAcpiChar (
520 if (!((Character >= 'A' && Character <= 'Z') ||
521 (Character >= '0' && Character <= '9') ||
524 /* Allow a '!' in the last position */
526 if (Character == '!' && Position == 3)
538 /*******************************************************************************
540 * FUNCTION: AcpiUtValidAcpiName
542 * PARAMETERS: Name - The name to be examined. Does not have to
543 * be NULL terminated string.
545 * RETURN: TRUE if the name is valid, FALSE otherwise
547 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
548 * 1) Upper case alpha
552 ******************************************************************************/
555 AcpiUtValidAcpiName (
561 ACPI_FUNCTION_ENTRY ();
564 for (i = 0; i < ACPI_NAME_SIZE; i++)
566 if (!AcpiUtValidAcpiChar (Name[i], i))
576 /*******************************************************************************
578 * FUNCTION: AcpiUtRepairName
580 * PARAMETERS: Name - The ACPI name to be repaired
582 * RETURN: Repaired version of the name
584 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
585 * return the new name. NOTE: the Name parameter must reside in
586 * read/write memory, cannot be a const.
588 * An ACPI Name must consist of valid ACPI characters. We will repair the name
589 * if necessary because we don't want to abort because of this, but we want
590 * all namespace names to be printable. A warning message is appropriate.
592 * This issue came up because there are in fact machines that exhibit
593 * this problem, and we want to be able to enable ACPI support for them,
594 * even though there are a few bad names.
596 ******************************************************************************/
603 BOOLEAN FoundBadChar = FALSE;
607 ACPI_FUNCTION_NAME (UtRepairName);
610 ACPI_MOVE_NAME (&OriginalName, Name);
612 /* Check each character in the name */
614 for (i = 0; i < ACPI_NAME_SIZE; i++)
616 if (AcpiUtValidAcpiChar (Name[i], i))
622 * Replace a bad character with something printable, yet technically
623 * still invalid. This prevents any collisions with existing "good"
624 * names in the namespace.
632 /* Report warning only if in strict mode or debug mode */
634 if (!AcpiGbl_EnableInterpreterSlack)
636 ACPI_WARNING ((AE_INFO,
637 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
638 OriginalName, Name));
642 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
643 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
644 OriginalName, Name));
650 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
651 /*******************************************************************************
653 * FUNCTION: UtConvertBackslashes
655 * PARAMETERS: Pathname - File pathname string to be converted
657 * RETURN: Modifies the input Pathname
659 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
660 * the entire input file pathname string.
662 ******************************************************************************/
665 UtConvertBackslashes (
676 if (*Pathname == '\\')
686 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
687 /*******************************************************************************
689 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
691 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
692 * functions. This is the size of the Destination buffer.
694 * RETURN: TRUE if the operation would overflow the destination buffer.
696 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
697 * the result of the operation will not overflow the output string
700 * NOTE: These functions are typically only helpful for processing
701 * user input and command lines. For most ACPICA code, the
702 * required buffer length is precisely calculated before buffer
703 * allocation, so the use of these functions is unnecessary.
705 ******************************************************************************/
714 if (strlen (Source) >= DestSize)
719 strcpy (Dest, Source);
730 if ((strlen (Dest) + strlen (Source)) >= DestSize)
735 strcat (Dest, Source);
744 ACPI_SIZE MaxTransferLength)
746 ACPI_SIZE ActualTransferLength;
749 ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
751 if ((strlen (Dest) + ActualTransferLength) >= DestSize)
756 strncat (Dest, Source, MaxTransferLength);