Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / utilities / utstring.c
blob282869c5416dc56683470fddc9a8e67c6516dd5d
1 /*******************************************************************************
3 * Module Name: utstring - Common functions for strings and characters
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
45 #define __UTSTRING_C__
47 #include "acpi.h"
48 #include "accommon.h"
49 #include "acnamesp.h"
52 #define _COMPONENT ACPI_UTILITIES
53 ACPI_MODULE_NAME ("utstring")
57 * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
58 * version of strtoul.
61 #ifdef ACPI_ASL_COMPILER
62 /*******************************************************************************
64 * FUNCTION: AcpiUtStrlwr (strlwr)
66 * PARAMETERS: SrcString - The source string to convert
68 * RETURN: None
70 * DESCRIPTION: Convert string to lowercase
72 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
74 ******************************************************************************/
76 void
77 AcpiUtStrlwr (
78 char *SrcString)
80 char *String;
83 ACPI_FUNCTION_ENTRY ();
86 if (!SrcString)
88 return;
91 /* Walk entire string, lowercasing the letters */
93 for (String = SrcString; *String; String++)
95 *String = (char) ACPI_TOLOWER (*String);
98 return;
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
110 * are equal.
112 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
113 * strings with no case sensitivity)
115 ******************************************************************************/
118 AcpiUtStricmp (
119 char *String1,
120 char *String2)
122 int c1;
123 int c2;
128 c1 = tolower ((int) *String1);
129 c2 = tolower ((int) *String2);
131 String1++;
132 String2++;
134 while ((c1 == c2) && (c1));
136 return (c1 - c2);
138 #endif
141 /*******************************************************************************
143 * FUNCTION: AcpiUtStrupr (strupr)
145 * PARAMETERS: SrcString - The source string to convert
147 * RETURN: None
149 * DESCRIPTION: Convert string to uppercase
151 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
153 ******************************************************************************/
155 void
156 AcpiUtStrupr (
157 char *SrcString)
159 char *String;
162 ACPI_FUNCTION_ENTRY ();
165 if (!SrcString)
167 return;
170 /* Walk entire string, uppercasing the letters */
172 for (String = SrcString; *String; String++)
174 *String = (char) ACPI_TOUPPER (*String);
177 return;
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 ******************************************************************************/
199 ACPI_STATUS
200 AcpiUtStrtoul64 (
201 char *String,
202 UINT32 Base,
203 UINT64 *RetInteger)
205 UINT32 ThisDigit = 0;
206 UINT64 ReturnValue = 0;
207 UINT64 Quotient;
208 UINT64 Dividend;
209 UINT32 ToIntegerOp = (Base == ACPI_ANY_BASE);
210 UINT32 Mode32 = (AcpiGbl_IntegerByteWidth == 4);
211 UINT8 ValidDigits = 0;
212 UINT8 SignOf0x = 0;
213 UINT8 Term = 0;
216 ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
219 switch (Base)
221 case ACPI_ANY_BASE:
222 case 16:
224 break;
226 default:
228 /* Invalid Base */
230 return_ACPI_STATUS (AE_BAD_PARAMETER);
233 if (!String)
235 goto ErrorExit;
238 /* Skip over any white space in the buffer */
240 while ((*String) && (ACPI_IS_SPACE (*String) || *String == '\t'))
242 String++;
245 if (ToIntegerOp)
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'))
253 SignOf0x = 1;
254 Base = 16;
256 /* Skip over the leading '0x' */
257 String += 2;
259 else
261 Base = 10;
265 /* Any string left? Check that '0x' is not followed by white space. */
267 if (!(*String) || ACPI_IS_SPACE (*String) || *String == '\t')
269 if (ToIntegerOp)
271 goto ErrorExit;
273 else
275 goto AllDone;
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 */
287 while (*String)
289 if (ACPI_IS_DIGIT (*String))
291 /* Convert ASCII 0-9 to Decimal value */
293 ThisDigit = ((UINT8) *String) - '0';
295 else if (Base == 10)
297 /* Digit is out of range; possible in ToInteger case only */
299 Term = 1;
301 else
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;
310 else
312 Term = 1;
316 if (Term)
318 if (ToIntegerOp)
320 goto ErrorExit;
322 else
324 break;
327 else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
329 /* Skip zeros */
330 String++;
331 continue;
334 ValidDigits++;
336 if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
339 * This is ToInteger operation case.
340 * No any restrictions for string-to-integer conversion,
341 * see ACPI spec.
343 goto ErrorExit;
346 /* Divide the digit into the correct position */
348 (void) AcpiUtShortDivide ((Dividend - (UINT64) ThisDigit),
349 Base, &Quotient, NULL);
351 if (ReturnValue > Quotient)
353 if (ToIntegerOp)
355 goto ErrorExit;
357 else
359 break;
363 ReturnValue *= Base;
364 ReturnValue += ThisDigit;
365 String++;
368 /* All done, normal exit */
370 AllDone:
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);
379 ErrorExit:
380 /* Base was set/validated above */
382 if (Base == 10)
384 return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
386 else
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.
401 * RETURN: None
403 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
404 * sequences.
406 ******************************************************************************/
408 void
409 AcpiUtPrintString (
410 char *String,
411 UINT16 MaxLength)
413 UINT32 i;
416 if (!String)
418 AcpiOsPrintf ("<\"NULL STRING PTR\">");
419 return;
422 AcpiOsPrintf ("\"");
423 for (i = 0; String[i] && (i < MaxLength); i++)
425 /* Escape sequences */
427 switch (String[i])
429 case 0x07:
431 AcpiOsPrintf ("\\a"); /* BELL */
432 break;
434 case 0x08:
436 AcpiOsPrintf ("\\b"); /* BACKSPACE */
437 break;
439 case 0x0C:
441 AcpiOsPrintf ("\\f"); /* FORMFEED */
442 break;
444 case 0x0A:
446 AcpiOsPrintf ("\\n"); /* LINEFEED */
447 break;
449 case 0x0D:
451 AcpiOsPrintf ("\\r"); /* CARRIAGE RETURN*/
452 break;
454 case 0x09:
456 AcpiOsPrintf ("\\t"); /* HORIZONTAL TAB */
457 break;
459 case 0x0B:
461 AcpiOsPrintf ("\\v"); /* VERTICAL TAB */
462 break;
464 case '\'': /* Single Quote */
465 case '\"': /* Double Quote */
466 case '\\': /* Backslash */
468 AcpiOsPrintf ("\\%c", (int) String[i]);
469 break;
471 default:
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]);
481 else
483 /* All others will be Hex escapes */
485 AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
487 break;
490 AcpiOsPrintf ("\"");
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
510 * 2) numeric
511 * 3) underscore
513 * We allow a '!' as the last character because of the ASF! table
515 ******************************************************************************/
517 BOOLEAN
518 AcpiUtValidAcpiChar (
519 char Character,
520 UINT32 Position)
523 if (!((Character >= 'A' && Character <= 'Z') ||
524 (Character >= '0' && Character <= '9') ||
525 (Character == '_')))
527 /* Allow a '!' in the last position */
529 if (Character == '!' && Position == 3)
531 return (TRUE);
534 return (FALSE);
537 return (TRUE);
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
552 * 2) numeric
553 * 3) underscore
555 ******************************************************************************/
557 BOOLEAN
558 AcpiUtValidAcpiName (
559 char *Name)
561 UINT32 i;
564 ACPI_FUNCTION_ENTRY ();
567 for (i = 0; i < ACPI_NAME_SIZE; i++)
569 if (!AcpiUtValidAcpiChar (Name[i], i))
571 return (FALSE);
575 return (TRUE);
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 ******************************************************************************/
601 void
602 AcpiUtRepairName (
603 char *Name)
605 UINT32 i;
606 BOOLEAN FoundBadChar = FALSE;
607 UINT32 OriginalName;
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))
621 continue;
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.
629 Name[i] = '*';
630 FoundBadChar = TRUE;
633 if (FoundBadChar)
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));
643 else
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 ******************************************************************************/
667 void
668 UtConvertBackslashes (
669 char *Pathname)
672 if (!Pathname)
674 return;
677 while (*Pathname)
679 if (*Pathname == '\\')
681 *Pathname = '/';
684 Pathname++;
687 #endif
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
701 * buffer.
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 ******************************************************************************/
710 BOOLEAN
711 AcpiUtSafeStrcpy (
712 char *Dest,
713 ACPI_SIZE DestSize,
714 char *Source)
717 if (ACPI_STRLEN (Source) >= DestSize)
719 return (TRUE);
722 ACPI_STRCPY (Dest, Source);
723 return (FALSE);
726 BOOLEAN
727 AcpiUtSafeStrcat (
728 char *Dest,
729 ACPI_SIZE DestSize,
730 char *Source)
733 if ((ACPI_STRLEN (Dest) + ACPI_STRLEN (Source)) >= DestSize)
735 return (TRUE);
738 ACPI_STRCAT (Dest, Source);
739 return (FALSE);
742 BOOLEAN
743 AcpiUtSafeStrncat (
744 char *Dest,
745 ACPI_SIZE DestSize,
746 char *Source,
747 ACPI_SIZE MaxTransferLength)
749 ACPI_SIZE ActualTransferLength;
752 ActualTransferLength = ACPI_MIN (MaxTransferLength, ACPI_STRLEN (Source));
754 if ((ACPI_STRLEN (Dest) + ActualTransferLength) >= DestSize)
756 return (TRUE);
759 ACPI_STRNCAT (Dest, Source, MaxTransferLength);
760 return (FALSE);
762 #endif