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.
45 #define __UTSTRING_C__
52 #define _COMPONENT ACPI_UTILITIES
53 ACPI_MODULE_NAME ("utstring")
57 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
61 #ifdef ACPI_ASL_COMPILER
62 /*******************************************************************************
64 * FUNCTION: AcpiUtStrlwr (strlwr)
66 * PARAMETERS: SrcString - The source string to convert
70 * DESCRIPTION: Convert string to lowercase
72 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
74 ******************************************************************************/
83 ACPI_FUNCTION_ENTRY ();
91 /* Walk entire string, lowercasing the letters */
93 for (String
= SrcString
; *String
; String
++)
95 *String
= (char) ACPI_TOLOWER (*String
);
102 /******************************************************************************
104 * FUNCTION: AcpiUtStricmp (stricmp)
106 * PARAMETERS: String1 - first string to compare
107 * String2 - second string to compare
109 * RETURN: int that signifies string relationship. Zero means strings
112 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
113 * strings with no case sensitivity)
115 ******************************************************************************/
128 c1
= tolower ((int) *String1
);
129 c2
= tolower ((int) *String2
);
134 while ((c1
== c2
) && (c1
));
141 /*******************************************************************************
143 * FUNCTION: AcpiUtStrupr (strupr)
145 * PARAMETERS: SrcString - The source string to convert
149 * DESCRIPTION: Convert string to uppercase
151 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
153 ******************************************************************************/
162 ACPI_FUNCTION_ENTRY ();
170 /* Walk entire string, uppercasing the letters */
172 for (String
= SrcString
; *String
; String
++)
174 *String
= (char) ACPI_TOUPPER (*String
);
181 /*******************************************************************************
183 * FUNCTION: AcpiUtStrtoul64
185 * PARAMETERS: String - Null terminated string
186 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
187 * ACPI_ANY_BASE means 'in behalf of ToInteger'
188 * RetInteger - Where the converted integer is returned
190 * RETURN: Status and Converted value
192 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
193 * 32-bit or 64-bit conversion, depending on the current mode
194 * of the interpreter.
195 * NOTE: Does not support Octal strings, not needed.
197 ******************************************************************************/
205 UINT32 ThisDigit
= 0;
206 UINT64 ReturnValue
= 0;
209 UINT32 ToIntegerOp
= (Base
== ACPI_ANY_BASE
);
210 UINT32 Mode32
= (AcpiGbl_IntegerByteWidth
== 4);
211 UINT8 ValidDigits
= 0;
216 ACPI_FUNCTION_TRACE_STR (UtStroul64
, String
);
230 return_ACPI_STATUS (AE_BAD_PARAMETER
);
238 /* Skip over any white space in the buffer */
240 while ((*String
) && (ACPI_IS_SPACE (*String
) || *String
== '\t'))
248 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
249 * We need to determine if it is decimal or hexadecimal.
251 if ((*String
== '0') && (ACPI_TOLOWER (*(String
+ 1)) == 'x'))
256 /* Skip over the leading '0x' */
265 /* Any string left? Check that '0x' is not followed by white space. */
267 if (!(*String
) || ACPI_IS_SPACE (*String
) || *String
== '\t')
280 * Perform a 32-bit or 64-bit conversion, depending upon the current
281 * execution mode of the interpreter
283 Dividend
= (Mode32
) ? ACPI_UINT32_MAX
: ACPI_UINT64_MAX
;
285 /* Main loop: convert the string to a 32- or 64-bit integer */
289 if (ACPI_IS_DIGIT (*String
))
291 /* Convert ASCII 0-9 to Decimal value */
293 ThisDigit
= ((UINT8
) *String
) - '0';
297 /* Digit is out of range; possible in ToInteger case only */
303 ThisDigit
= (UINT8
) ACPI_TOUPPER (*String
);
304 if (ACPI_IS_XDIGIT ((char) ThisDigit
))
306 /* Convert ASCII Hex char to value */
308 ThisDigit
= ThisDigit
- 'A' + 10;
327 else if ((ValidDigits
== 0) && (ThisDigit
== 0) && !SignOf0x
)
336 if (SignOf0x
&& ((ValidDigits
> 16) || ((ValidDigits
> 8) && Mode32
)))
339 * This is ToInteger operation case.
340 * No any restrictions for string-to-integer conversion,
346 /* Divide the digit into the correct position */
348 (void) AcpiUtShortDivide ((Dividend
- (UINT64
) ThisDigit
),
349 Base
, &Quotient
, NULL
);
351 if (ReturnValue
> Quotient
)
364 ReturnValue
+= ThisDigit
;
368 /* All done, normal exit */
372 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
373 ACPI_FORMAT_UINT64 (ReturnValue
)));
375 *RetInteger
= ReturnValue
;
376 return_ACPI_STATUS (AE_OK
);
380 /* Base was set/validated above */
384 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT
);
388 return_ACPI_STATUS (AE_BAD_HEX_CONSTANT
);
393 /*******************************************************************************
395 * FUNCTION: AcpiUtPrintString
397 * PARAMETERS: String - Null terminated ASCII string
398 * MaxLength - Maximum output length. Used to constrain the
399 * length of strings during debug output only.
403 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
406 ******************************************************************************/
418 AcpiOsPrintf ("<\"NULL STRING PTR\">");
423 for (i
= 0; String
[i
] && (i
< MaxLength
); i
++)
425 /* Escape sequences */
431 AcpiOsPrintf ("\\a"); /* BELL */
436 AcpiOsPrintf ("\\b"); /* BACKSPACE */
441 AcpiOsPrintf ("\\f"); /* FORMFEED */
446 AcpiOsPrintf ("\\n"); /* LINEFEED */
451 AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
456 AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
461 AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
464 case '\'': /* Single Quote */
465 case '\"': /* Double Quote */
466 case '\\': /* Backslash */
468 AcpiOsPrintf ("\\%c", (int) String
[i
]);
473 /* Check for printable character or hex escape */
475 if (ACPI_IS_PRINT (String
[i
]))
477 /* This is a normal character */
479 AcpiOsPrintf ("%c", (int) String
[i
]);
483 /* All others will be Hex escapes */
485 AcpiOsPrintf ("\\x%2.2X", (INT32
) String
[i
]);
492 if (i
== MaxLength
&& String
[i
])
494 AcpiOsPrintf ("...");
499 /*******************************************************************************
501 * FUNCTION: AcpiUtValidAcpiChar
503 * PARAMETERS: Char - The character to be examined
504 * Position - Byte position (0-3)
506 * RETURN: TRUE if the character is valid, FALSE otherwise
508 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
509 * 1) Upper case alpha
513 * We allow a '!' as the last character because of the ASF! table
515 ******************************************************************************/
518 AcpiUtValidAcpiChar (
523 if (!((Character
>= 'A' && Character
<= 'Z') ||
524 (Character
>= '0' && Character
<= '9') ||
527 /* Allow a '!' in the last position */
529 if (Character
== '!' && Position
== 3)
541 /*******************************************************************************
543 * FUNCTION: AcpiUtValidAcpiName
545 * PARAMETERS: Name - The name to be examined. Does not have to
546 * be NULL terminated string.
548 * RETURN: TRUE if the name is valid, FALSE otherwise
550 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
551 * 1) Upper case alpha
555 ******************************************************************************/
558 AcpiUtValidAcpiName (
564 ACPI_FUNCTION_ENTRY ();
567 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++)
569 if (!AcpiUtValidAcpiChar (Name
[i
], i
))
579 /*******************************************************************************
581 * FUNCTION: AcpiUtRepairName
583 * PARAMETERS: Name - The ACPI name to be repaired
585 * RETURN: Repaired version of the name
587 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
588 * return the new name. NOTE: the Name parameter must reside in
589 * read/write memory, cannot be a const.
591 * An ACPI Name must consist of valid ACPI characters. We will repair the name
592 * if necessary because we don't want to abort because of this, but we want
593 * all namespace names to be printable. A warning message is appropriate.
595 * This issue came up because there are in fact machines that exhibit
596 * this problem, and we want to be able to enable ACPI support for them,
597 * even though there are a few bad names.
599 ******************************************************************************/
606 BOOLEAN FoundBadChar
= FALSE
;
610 ACPI_FUNCTION_NAME (UtRepairName
);
613 ACPI_MOVE_NAME (&OriginalName
, Name
);
615 /* Check each character in the name */
617 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++)
619 if (AcpiUtValidAcpiChar (Name
[i
], i
))
625 * Replace a bad character with something printable, yet technically
626 * still invalid. This prevents any collisions with existing "good"
627 * names in the namespace.
635 /* Report warning only if in strict mode or debug mode */
637 if (!AcpiGbl_EnableInterpreterSlack
)
639 ACPI_WARNING ((AE_INFO
,
640 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
641 OriginalName
, Name
));
645 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
,
646 "Invalid character(s) in name (0x%.8X), repaired: [%4.4s]",
647 OriginalName
, Name
));
653 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
654 /*******************************************************************************
656 * FUNCTION: UtConvertBackslashes
658 * PARAMETERS: Pathname - File pathname string to be converted
660 * RETURN: Modifies the input Pathname
662 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
663 * the entire input file pathname string.
665 ******************************************************************************/
668 UtConvertBackslashes (
679 if (*Pathname
== '\\')
689 #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
690 /*******************************************************************************
692 * FUNCTION: AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
694 * PARAMETERS: Adds a "DestSize" parameter to each of the standard string
695 * functions. This is the size of the Destination buffer.
697 * RETURN: TRUE if the operation would overflow the destination buffer.
699 * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
700 * the result of the operation will not overflow the output string
703 * NOTE: These functions are typically only helpful for processing
704 * user input and command lines. For most ACPICA code, the
705 * required buffer length is precisely calculated before buffer
706 * allocation, so the use of these functions is unnecessary.
708 ******************************************************************************/
717 if (ACPI_STRLEN (Source
) >= DestSize
)
722 ACPI_STRCPY (Dest
, Source
);
733 if ((ACPI_STRLEN (Dest
) + ACPI_STRLEN (Source
)) >= DestSize
)
738 ACPI_STRCAT (Dest
, Source
);
747 ACPI_SIZE MaxTransferLength
)
749 ACPI_SIZE ActualTransferLength
;
752 ActualTransferLength
= ACPI_MIN (MaxTransferLength
, ACPI_STRLEN (Source
));
754 if ((ACPI_STRLEN (Dest
) + ActualTransferLength
) >= DestSize
)
759 ACPI_STRNCAT (Dest
, Source
, MaxTransferLength
);