Import acpica-unix-20061109 from Intel.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / compiler / aslutils.c
blob4a0fc08f7d0f04d1ee50e50d5f73a1d01137e0cd
2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
5 * $Revision: 1.71 $
7 *****************************************************************************/
9 /******************************************************************************
11 * 1. Copyright Notice
13 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
14 * All rights reserved.
16 * 2. License
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
42 * 3. Conditions
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
119 #include "aslcompiler.h"
120 #include "aslcompiler.y.h"
121 #include "acnamesp.h"
122 #include "amlcode.h"
124 #define _COMPONENT ACPI_COMPILER
125 ACPI_MODULE_NAME ("aslutils")
127 #ifdef _USE_BERKELEY_YACC
128 extern const char * const AslCompilername[];
129 static const char * const *yytname = &AslCompilername[254];
130 #else
131 extern const char * const yytname[];
132 #endif
134 /* Local prototypes */
136 static ACPI_STATUS
137 UtStrtoul64 (
138 char *String,
139 UINT32 Base,
140 ACPI_INTEGER *RetInteger);
142 static void
143 UtPadNameWithUnderscores (
144 char *NameSeg,
145 char *PaddedNameSeg);
147 static void
148 UtAttachNameseg (
149 ACPI_PARSE_OBJECT *Op,
150 char *Name);
153 /*******************************************************************************
155 * FUNCTION: AcpiPsDisplayConstantOpcodes
157 * PARAMETERS: None
159 * RETURN: None
161 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
163 ******************************************************************************/
165 void
166 UtDisplayConstantOpcodes (
167 void)
169 UINT32 i;
172 printf ("Constant expression opcode information\n\n");
174 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
176 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
178 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
184 /*******************************************************************************
186 * FUNCTION: UtLocalCalloc
188 * PARAMETERS: Size - Bytes to be allocated
190 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
192 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
193 * allocation failure, on the assumption that nothing more can be
194 * accomplished.
196 ******************************************************************************/
198 void *
199 UtLocalCalloc (
200 UINT32 Size)
202 void *Allocated;
205 Allocated = ACPI_ALLOCATE_ZEROED (Size);
206 if (!Allocated)
208 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
209 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
210 Gbl_InputByteCount, Gbl_CurrentColumn,
211 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
212 exit (1);
215 TotalAllocations++;
216 TotalAllocated += Size;
217 return (Allocated);
221 /*******************************************************************************
223 * FUNCTION: UtBeginEvent
225 * PARAMETERS: Name - Ascii name of this event
227 * RETURN: Event - Event number (integer index)
229 * DESCRIPTION: Saves the current time with this event
231 ******************************************************************************/
233 UINT8
234 UtBeginEvent (
235 char *Name)
238 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
240 AcpiOsPrintf ("Ran out of compiler event structs!\n");
241 return (AslGbl_NextEvent);
244 /* Init event with current (start) time */
246 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
247 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
248 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
250 return (AslGbl_NextEvent++);
254 /*******************************************************************************
256 * FUNCTION: UtEndEvent
258 * PARAMETERS: Event - Event number (integer index)
260 * RETURN: None
262 * DESCRIPTION: Saves the current time (end time) with this event
264 ******************************************************************************/
266 void
267 UtEndEvent (
268 UINT8 Event)
271 if (Event >= ASL_NUM_EVENTS)
273 return;
276 /* Insert end time for event */
278 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
282 /*******************************************************************************
284 * FUNCTION: UtHexCharToValue
286 * PARAMETERS: HexChar - Hex character in Ascii
288 * RETURN: The binary value of the hex character
290 * DESCRIPTION: Perform ascii-to-hex translation
292 ******************************************************************************/
294 UINT8
295 UtHexCharToValue (
296 int HexChar)
299 if (HexChar <= 0x39)
301 return ((UINT8) (HexChar - 0x30));
304 if (HexChar <= 0x46)
306 return ((UINT8) (HexChar - 0x37));
309 return ((UINT8) (HexChar - 0x57));
313 /*******************************************************************************
315 * FUNCTION: UtConvertByteToHex
317 * PARAMETERS: RawByte - Binary data
318 * Buffer - Pointer to where the hex bytes will be stored
320 * RETURN: Ascii hex byte is stored in Buffer.
322 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
323 * with "0x"
325 ******************************************************************************/
327 void
328 UtConvertByteToHex (
329 UINT8 RawByte,
330 UINT8 *Buffer)
333 Buffer[0] = '0';
334 Buffer[1] = 'x';
336 Buffer[2] = (UINT8) hex[(RawByte >> 4) & 0xF];
337 Buffer[3] = (UINT8) hex[RawByte & 0xF];
341 /*******************************************************************************
343 * FUNCTION: UtConvertByteToAsmHex
345 * PARAMETERS: RawByte - Binary data
346 * Buffer - Pointer to where the hex bytes will be stored
348 * RETURN: Ascii hex byte is stored in Buffer.
350 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
351 * with "0x"
353 ******************************************************************************/
355 void
356 UtConvertByteToAsmHex (
357 UINT8 RawByte,
358 UINT8 *Buffer)
361 Buffer[0] = '0';
362 Buffer[1] = (UINT8) hex[(RawByte >> 4) & 0xF];
363 Buffer[2] = (UINT8) hex[RawByte & 0xF];
364 Buffer[3] = 'h';
368 /*******************************************************************************
370 * FUNCTION: DbgPrint
372 * PARAMETERS: Type - Type of output
373 * Fmt - Printf format string
374 * ... - variable printf list
376 * RETURN: None
378 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
379 * debug flag is set.
381 ******************************************************************************/
383 void
384 DbgPrint (
385 UINT32 Type,
386 char *Fmt,
387 ...)
389 va_list Args;
392 va_start (Args, Fmt);
394 if (!Gbl_DebugFlag)
396 return;
399 if ((Type == ASL_PARSE_OUTPUT) &&
400 (!(AslCompilerdebug)))
402 return;
405 (void) vfprintf (stderr, Fmt, Args);
406 va_end (Args);
407 return;
411 /*******************************************************************************
413 * FUNCTION: UtPrintFormattedName
415 * PARAMETERS: ParseOpcode - Parser keyword ID
416 * Level - Indentation level
418 * RETURN: None
420 * DESCRIPTION: Print the ascii name of the parse opcode.
422 ******************************************************************************/
424 #define TEXT_OFFSET 10
426 void
427 UtPrintFormattedName (
428 UINT16 ParseOpcode,
429 UINT32 Level)
432 if (Level)
434 DbgPrint (ASL_TREE_OUTPUT,
435 "%*s", (3 * Level), " ");
437 DbgPrint (ASL_TREE_OUTPUT,
438 " %-20.20s", UtGetOpName (ParseOpcode));
440 if (Level < TEXT_OFFSET)
442 DbgPrint (ASL_TREE_OUTPUT,
443 "%*s", (TEXT_OFFSET - Level) * 3, " ");
448 /*******************************************************************************
450 * FUNCTION: UtSetParseOpName
452 * PARAMETERS: Op
454 * RETURN: None
456 * DESCRIPTION: Insert the ascii name of the parse opcode
458 ******************************************************************************/
460 void
461 UtSetParseOpName (
462 ACPI_PARSE_OBJECT *Op)
465 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
466 ACPI_MAX_PARSEOP_NAME);
470 /*******************************************************************************
472 * FUNCTION: UtGetOpName
474 * PARAMETERS: ParseOpcode - Parser keyword ID
476 * RETURN: Pointer to the opcode name
478 * DESCRIPTION: Get the ascii name of the parse opcode
480 ******************************************************************************/
482 char *
483 UtGetOpName (
484 UINT32 ParseOpcode)
488 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
489 * Ignore first 8 characters of the name
491 return ((char *) yytname
492 [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8);
496 /*******************************************************************************
498 * FUNCTION: UtDisplaySummary
500 * PARAMETERS: FileID - ID of outpout file
502 * RETURN: None
504 * DESCRIPTION: Display compilation statistics
506 ******************************************************************************/
508 void
509 UtDisplaySummary (
510 UINT32 FileId)
513 if (FileId != ASL_FILE_STDOUT)
515 /* Compiler name and version number */
517 FlPrintFile (FileId, "%s version %X [%s]\n",
518 CompilerId, (UINT32) ACPI_CA_VERSION, __DATE__);
521 /* Input/Output summary */
523 FlPrintFile (FileId,
524 "ASL Input: %s - %d lines, %d bytes, %d keywords\n",
525 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
526 Gbl_InputByteCount, TotalKeywords);
528 /* AML summary */
530 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
532 FlPrintFile (FileId,
533 "AML Output: %s - %d bytes %d named objects %d executable opcodes\n\n",
534 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
535 TotalNamedObjects, TotalExecutableOpcodes);
538 /* Error summary */
540 FlPrintFile (FileId,
541 "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n",
542 Gbl_ExceptionCount[ASL_ERROR],
543 Gbl_ExceptionCount[ASL_WARNING] +
544 Gbl_ExceptionCount[ASL_WARNING2] +
545 Gbl_ExceptionCount[ASL_WARNING3],
546 Gbl_ExceptionCount[ASL_REMARK],
547 Gbl_ExceptionCount[ASL_OPTIMIZATION]);
551 /*******************************************************************************
553 * FUNCTION: UtDisplaySummary
555 * PARAMETERS: Op - Integer parse node
556 * LowValue - Smallest allowed value
557 * HighValue - Largest allowed value
559 * RETURN: Op if OK, otherwise NULL
561 * DESCRIPTION: Check integer for an allowable range
563 ******************************************************************************/
565 ACPI_PARSE_OBJECT *
566 UtCheckIntegerRange (
567 ACPI_PARSE_OBJECT *Op,
568 UINT32 LowValue,
569 UINT32 HighValue)
571 char *ParseError = NULL;
572 char Buffer[64];
575 if (!Op)
577 return NULL;
580 if (Op->Asl.Value.Integer < LowValue)
582 ParseError = "Value below valid range";
583 Op->Asl.Value.Integer = LowValue;
586 if (Op->Asl.Value.Integer > HighValue)
588 ParseError = "Value above valid range";
589 Op->Asl.Value.Integer = HighValue;
592 if (ParseError)
594 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
595 AslCompilererror (Buffer);
597 return NULL;
600 return Op;
604 /*******************************************************************************
606 * FUNCTION: UtGetStringBuffer
608 * PARAMETERS: Length - Size of buffer requested
610 * RETURN: Pointer to the buffer. Aborts on allocation failure
612 * DESCRIPTION: Allocate a string buffer. Bypass the local
613 * dynamic memory manager for performance reasons (This has a
614 * major impact on the speed of the compiler.)
616 ******************************************************************************/
618 char *
619 UtGetStringBuffer (
620 UINT32 Length)
622 char *Buffer;
625 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
627 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
628 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
629 Length;
632 Buffer = Gbl_StringCacheNext;
633 Gbl_StringCacheNext += Length;
635 return (Buffer);
639 /*******************************************************************************
641 * FUNCTION: UtInternalizeName
643 * PARAMETERS: ExternalName - Name to convert
644 * ConvertedName - Where the converted name is returned
646 * RETURN: Status
648 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
650 ******************************************************************************/
652 ACPI_STATUS
653 UtInternalizeName (
654 char *ExternalName,
655 char **ConvertedName)
657 ACPI_NAMESTRING_INFO Info;
658 ACPI_STATUS Status;
661 if (!ExternalName)
663 return (AE_OK);
666 /* Get the length of the new internal name */
668 Info.ExternalName = ExternalName;
669 AcpiNsGetInternalNameLength (&Info);
671 /* We need a segment to store the internal name */
673 Info.InternalName = UtGetStringBuffer (Info.Length);
674 if (!Info.InternalName)
676 return (AE_NO_MEMORY);
679 /* Build the name */
681 Status = AcpiNsBuildInternalName (&Info);
682 if (ACPI_FAILURE (Status))
684 return (Status);
687 *ConvertedName = Info.InternalName;
688 return (AE_OK);
692 /*******************************************************************************
694 * FUNCTION: UtPadNameWithUnderscores
696 * PARAMETERS: NameSeg - Input nameseg
697 * PaddedNameSeg - Output padded nameseg
699 * RETURN: Padded nameseg.
701 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
702 * ACPI_NAME.
704 ******************************************************************************/
706 static void
707 UtPadNameWithUnderscores (
708 char *NameSeg,
709 char *PaddedNameSeg)
711 UINT32 i;
714 for (i = 0; (i < ACPI_NAME_SIZE); i++)
716 if (*NameSeg)
718 *PaddedNameSeg = *NameSeg;
719 NameSeg++;
721 else
723 *PaddedNameSeg = '_';
725 PaddedNameSeg++;
730 /*******************************************************************************
732 * FUNCTION: UtAttachNameseg
734 * PARAMETERS: Op - Parent parse node
735 * Name - Full ExternalName
737 * RETURN: None; Sets the NameSeg field in parent node
739 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
740 * in the NameSeg field of the Op.
742 ******************************************************************************/
744 static void
745 UtAttachNameseg (
746 ACPI_PARSE_OBJECT *Op,
747 char *Name)
749 char *NameSeg;
750 char PaddedNameSeg[4];
753 if (!Name)
755 return;
758 /* Look for the last dot in the namepath */
760 NameSeg = strrchr (Name, '.');
761 if (NameSeg)
763 /* Found last dot, we have also found the final nameseg */
765 NameSeg++;
766 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
768 else
770 /* No dots in the namepath, there is only a single nameseg. */
771 /* Handle prefixes */
773 while ((*Name == '\\') || (*Name == '^'))
775 Name++;
778 /* Remaing string should be one single nameseg */
780 UtPadNameWithUnderscores (Name, PaddedNameSeg);
783 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
787 /*******************************************************************************
789 * FUNCTION: UtAttachNamepathToOwner
791 * PARAMETERS: Op - Parent parse node
792 * NameOp - Node that contains the name
794 * RETURN: Sets the ExternalName and Namepath in the parent node
796 * DESCRIPTION: Store the name in two forms in the parent node: The original
797 * (external) name, and the internalized name that is used within
798 * the ACPI namespace manager.
800 ******************************************************************************/
802 void
803 UtAttachNamepathToOwner (
804 ACPI_PARSE_OBJECT *Op,
805 ACPI_PARSE_OBJECT *NameOp)
807 ACPI_STATUS Status;
810 /* Full external path */
812 Op->Asl.ExternalName = NameOp->Asl.Value.String;
814 /* Save the NameOp for possible error reporting later */
816 Op->Asl.ParentMethod = (void *) NameOp;
818 /* Last nameseg of the path */
820 UtAttachNameseg (Op, Op->Asl.ExternalName);
822 /* Create internalized path */
824 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
825 if (ACPI_FAILURE (Status))
827 /* TBD: abort on no memory */
832 /*******************************************************************************
834 * FUNCTION: UtDoConstant
836 * PARAMETERS: String - Hex, Octal, or Decimal string
838 * RETURN: Converted Integer
840 * DESCRIPTION: Convert a string to an integer. With error checking.
842 ******************************************************************************/
844 ACPI_INTEGER
845 UtDoConstant (
846 char *String)
848 ACPI_STATUS Status;
849 ACPI_INTEGER Converted;
850 char ErrBuf[64];
853 Status = UtStrtoul64 (String, 0, &Converted);
854 if (ACPI_FAILURE (Status))
856 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
857 AcpiFormatException (Status));
858 AslCompilererror (ErrBuf);
861 return (Converted);
865 /* TBD: use version in ACPI CA main code base? */
867 /*******************************************************************************
869 * FUNCTION: UtStrtoul64
871 * PARAMETERS: String - Null terminated string
872 * Terminater - Where a pointer to the terminating byte is
873 * returned
874 * Base - Radix of the string
876 * RETURN: Converted value
878 * DESCRIPTION: Convert a string into an unsigned value.
880 ******************************************************************************/
882 static ACPI_STATUS
883 UtStrtoul64 (
884 char *String,
885 UINT32 Base,
886 ACPI_INTEGER *RetInteger)
888 UINT32 Index;
889 UINT32 Sign;
890 ACPI_INTEGER ReturnValue = 0;
891 ACPI_STATUS Status = AE_OK;
894 *RetInteger = 0;
896 switch (Base)
898 case 0:
899 case 8:
900 case 10:
901 case 16:
902 break;
904 default:
906 * The specified Base parameter is not in the domain of
907 * this function:
909 return (AE_BAD_PARAMETER);
912 /* Skip over any white space in the buffer: */
914 while (isspace (*String) || *String == '\t')
916 ++String;
920 * The buffer may contain an optional plus or minus sign.
921 * If it does, then skip over it but remember what is was:
923 if (*String == '-')
925 Sign = NEGATIVE;
926 ++String;
928 else if (*String == '+')
930 ++String;
931 Sign = POSITIVE;
933 else
935 Sign = POSITIVE;
939 * If the input parameter Base is zero, then we need to
940 * determine if it is octal, decimal, or hexadecimal:
942 if (Base == 0)
944 if (*String == '0')
946 if (tolower (*(++String)) == 'x')
948 Base = 16;
949 ++String;
951 else
953 Base = 8;
956 else
958 Base = 10;
963 * For octal and hexadecimal bases, skip over the leading
964 * 0 or 0x, if they are present.
966 if (Base == 8 && *String == '0')
968 String++;
971 if (Base == 16 &&
972 *String == '0' &&
973 tolower (*(++String)) == 'x')
975 String++;
978 /* Main loop: convert the string to an unsigned long */
980 while (*String)
982 if (isdigit (*String))
984 Index = ((UINT8) *String) - '0';
986 else
988 Index = (UINT8) toupper (*String);
989 if (isupper ((char) Index))
991 Index = Index - 'A' + 10;
993 else
995 goto ErrorExit;
999 if (Index >= Base)
1001 goto ErrorExit;
1004 /* Check to see if value is out of range: */
1006 if (ReturnValue > ((ACPI_INTEGER_MAX - (ACPI_INTEGER) Index) /
1007 (ACPI_INTEGER) Base))
1009 goto ErrorExit;
1011 else
1013 ReturnValue *= Base;
1014 ReturnValue += Index;
1017 ++String;
1021 /* If a minus sign was present, then "the conversion is negated": */
1023 if (Sign == NEGATIVE)
1025 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1028 *RetInteger = ReturnValue;
1029 return (Status);
1032 ErrorExit:
1033 switch (Base)
1035 case 8:
1036 Status = AE_BAD_OCTAL_CONSTANT;
1037 break;
1039 case 10:
1040 Status = AE_BAD_DECIMAL_CONSTANT;
1041 break;
1043 case 16:
1044 Status = AE_BAD_HEX_CONSTANT;
1045 break;
1047 default:
1048 /* Base validated above */
1049 break;
1052 return (Status);