xref: /titanic_50/usr/src/uts/intel/io/acpica/utilities/utnonansi.c (revision 385cc6b4ad1792caef3f84eb61eed3f27085801f)
1*385cc6b4SJerry Jelinek /*******************************************************************************
2*385cc6b4SJerry Jelinek  *
3*385cc6b4SJerry Jelinek  * Module Name: utnonansi - Non-ansi C library functions
4*385cc6b4SJerry Jelinek  *
5*385cc6b4SJerry Jelinek  ******************************************************************************/
6*385cc6b4SJerry Jelinek 
7*385cc6b4SJerry Jelinek /*
8*385cc6b4SJerry Jelinek  * Copyright (C) 2000 - 2016, Intel Corp.
9*385cc6b4SJerry Jelinek  * All rights reserved.
10*385cc6b4SJerry Jelinek  *
11*385cc6b4SJerry Jelinek  * Redistribution and use in source and binary forms, with or without
12*385cc6b4SJerry Jelinek  * modification, are permitted provided that the following conditions
13*385cc6b4SJerry Jelinek  * are met:
14*385cc6b4SJerry Jelinek  * 1. Redistributions of source code must retain the above copyright
15*385cc6b4SJerry Jelinek  *    notice, this list of conditions, and the following disclaimer,
16*385cc6b4SJerry Jelinek  *    without modification.
17*385cc6b4SJerry Jelinek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*385cc6b4SJerry Jelinek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*385cc6b4SJerry Jelinek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*385cc6b4SJerry Jelinek  *    including a substantially similar Disclaimer requirement for further
21*385cc6b4SJerry Jelinek  *    binary redistribution.
22*385cc6b4SJerry Jelinek  * 3. Neither the names of the above-listed copyright holders nor the names
23*385cc6b4SJerry Jelinek  *    of any contributors may be used to endorse or promote products derived
24*385cc6b4SJerry Jelinek  *    from this software without specific prior written permission.
25*385cc6b4SJerry Jelinek  *
26*385cc6b4SJerry Jelinek  * Alternatively, this software may be distributed under the terms of the
27*385cc6b4SJerry Jelinek  * GNU General Public License ("GPL") version 2 as published by the Free
28*385cc6b4SJerry Jelinek  * Software Foundation.
29*385cc6b4SJerry Jelinek  *
30*385cc6b4SJerry Jelinek  * NO WARRANTY
31*385cc6b4SJerry Jelinek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*385cc6b4SJerry Jelinek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*385cc6b4SJerry Jelinek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*385cc6b4SJerry Jelinek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*385cc6b4SJerry Jelinek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*385cc6b4SJerry Jelinek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*385cc6b4SJerry Jelinek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*385cc6b4SJerry Jelinek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*385cc6b4SJerry Jelinek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*385cc6b4SJerry Jelinek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*385cc6b4SJerry Jelinek  * POSSIBILITY OF SUCH DAMAGES.
42*385cc6b4SJerry Jelinek  */
43*385cc6b4SJerry Jelinek 
44*385cc6b4SJerry Jelinek #include "acpi.h"
45*385cc6b4SJerry Jelinek #include "accommon.h"
46*385cc6b4SJerry Jelinek 
47*385cc6b4SJerry Jelinek 
48*385cc6b4SJerry Jelinek #define _COMPONENT          ACPI_UTILITIES
49*385cc6b4SJerry Jelinek         ACPI_MODULE_NAME    ("utnonansi")
50*385cc6b4SJerry Jelinek 
51*385cc6b4SJerry Jelinek 
52*385cc6b4SJerry Jelinek /*
53*385cc6b4SJerry Jelinek  * Non-ANSI C library functions - strlwr, strupr, stricmp, and a 64-bit
54*385cc6b4SJerry Jelinek  * version of strtoul.
55*385cc6b4SJerry Jelinek  */
56*385cc6b4SJerry Jelinek 
57*385cc6b4SJerry Jelinek /*******************************************************************************
58*385cc6b4SJerry Jelinek  *
59*385cc6b4SJerry Jelinek  * FUNCTION:    AcpiUtStrlwr (strlwr)
60*385cc6b4SJerry Jelinek  *
61*385cc6b4SJerry Jelinek  * PARAMETERS:  SrcString       - The source string to convert
62*385cc6b4SJerry Jelinek  *
63*385cc6b4SJerry Jelinek  * RETURN:      None
64*385cc6b4SJerry Jelinek  *
65*385cc6b4SJerry Jelinek  * DESCRIPTION: Convert a string to lowercase
66*385cc6b4SJerry Jelinek  *
67*385cc6b4SJerry Jelinek  ******************************************************************************/
68*385cc6b4SJerry Jelinek 
69*385cc6b4SJerry Jelinek void
AcpiUtStrlwr(char * SrcString)70*385cc6b4SJerry Jelinek AcpiUtStrlwr (
71*385cc6b4SJerry Jelinek     char                    *SrcString)
72*385cc6b4SJerry Jelinek {
73*385cc6b4SJerry Jelinek     char                    *String;
74*385cc6b4SJerry Jelinek 
75*385cc6b4SJerry Jelinek 
76*385cc6b4SJerry Jelinek     ACPI_FUNCTION_ENTRY ();
77*385cc6b4SJerry Jelinek 
78*385cc6b4SJerry Jelinek 
79*385cc6b4SJerry Jelinek     if (!SrcString)
80*385cc6b4SJerry Jelinek     {
81*385cc6b4SJerry Jelinek         return;
82*385cc6b4SJerry Jelinek     }
83*385cc6b4SJerry Jelinek 
84*385cc6b4SJerry Jelinek     /* Walk entire string, lowercasing the letters */
85*385cc6b4SJerry Jelinek 
86*385cc6b4SJerry Jelinek     for (String = SrcString; *String; String++)
87*385cc6b4SJerry Jelinek     {
88*385cc6b4SJerry Jelinek         *String = (char) tolower ((int) *String);
89*385cc6b4SJerry Jelinek     }
90*385cc6b4SJerry Jelinek }
91*385cc6b4SJerry Jelinek 
92*385cc6b4SJerry Jelinek 
93*385cc6b4SJerry Jelinek /*******************************************************************************
94*385cc6b4SJerry Jelinek  *
95*385cc6b4SJerry Jelinek  * FUNCTION:    AcpiUtStrupr (strupr)
96*385cc6b4SJerry Jelinek  *
97*385cc6b4SJerry Jelinek  * PARAMETERS:  SrcString       - The source string to convert
98*385cc6b4SJerry Jelinek  *
99*385cc6b4SJerry Jelinek  * RETURN:      None
100*385cc6b4SJerry Jelinek  *
101*385cc6b4SJerry Jelinek  * DESCRIPTION: Convert a string to uppercase
102*385cc6b4SJerry Jelinek  *
103*385cc6b4SJerry Jelinek  ******************************************************************************/
104*385cc6b4SJerry Jelinek 
105*385cc6b4SJerry Jelinek void
AcpiUtStrupr(char * SrcString)106*385cc6b4SJerry Jelinek AcpiUtStrupr (
107*385cc6b4SJerry Jelinek     char                    *SrcString)
108*385cc6b4SJerry Jelinek {
109*385cc6b4SJerry Jelinek     char                    *String;
110*385cc6b4SJerry Jelinek 
111*385cc6b4SJerry Jelinek 
112*385cc6b4SJerry Jelinek     ACPI_FUNCTION_ENTRY ();
113*385cc6b4SJerry Jelinek 
114*385cc6b4SJerry Jelinek 
115*385cc6b4SJerry Jelinek     if (!SrcString)
116*385cc6b4SJerry Jelinek     {
117*385cc6b4SJerry Jelinek         return;
118*385cc6b4SJerry Jelinek     }
119*385cc6b4SJerry Jelinek 
120*385cc6b4SJerry Jelinek     /* Walk entire string, uppercasing the letters */
121*385cc6b4SJerry Jelinek 
122*385cc6b4SJerry Jelinek     for (String = SrcString; *String; String++)
123*385cc6b4SJerry Jelinek     {
124*385cc6b4SJerry Jelinek         *String = (char) toupper ((int) *String);
125*385cc6b4SJerry Jelinek     }
126*385cc6b4SJerry Jelinek }
127*385cc6b4SJerry Jelinek 
128*385cc6b4SJerry Jelinek 
129*385cc6b4SJerry Jelinek /******************************************************************************
130*385cc6b4SJerry Jelinek  *
131*385cc6b4SJerry Jelinek  * FUNCTION:    AcpiUtStricmp (stricmp)
132*385cc6b4SJerry Jelinek  *
133*385cc6b4SJerry Jelinek  * PARAMETERS:  String1             - first string to compare
134*385cc6b4SJerry Jelinek  *              String2             - second string to compare
135*385cc6b4SJerry Jelinek  *
136*385cc6b4SJerry Jelinek  * RETURN:      int that signifies string relationship. Zero means strings
137*385cc6b4SJerry Jelinek  *              are equal.
138*385cc6b4SJerry Jelinek  *
139*385cc6b4SJerry Jelinek  * DESCRIPTION: Case-insensitive string compare. Implementation of the
140*385cc6b4SJerry Jelinek  *              non-ANSI stricmp function.
141*385cc6b4SJerry Jelinek  *
142*385cc6b4SJerry Jelinek  ******************************************************************************/
143*385cc6b4SJerry Jelinek 
144*385cc6b4SJerry Jelinek int
AcpiUtStricmp(char * String1,char * String2)145*385cc6b4SJerry Jelinek AcpiUtStricmp (
146*385cc6b4SJerry Jelinek     char                    *String1,
147*385cc6b4SJerry Jelinek     char                    *String2)
148*385cc6b4SJerry Jelinek {
149*385cc6b4SJerry Jelinek     int                     c1;
150*385cc6b4SJerry Jelinek     int                     c2;
151*385cc6b4SJerry Jelinek 
152*385cc6b4SJerry Jelinek 
153*385cc6b4SJerry Jelinek     do
154*385cc6b4SJerry Jelinek     {
155*385cc6b4SJerry Jelinek         c1 = tolower ((int) *String1);
156*385cc6b4SJerry Jelinek         c2 = tolower ((int) *String2);
157*385cc6b4SJerry Jelinek 
158*385cc6b4SJerry Jelinek         String1++;
159*385cc6b4SJerry Jelinek         String2++;
160*385cc6b4SJerry Jelinek     }
161*385cc6b4SJerry Jelinek     while ((c1 == c2) && (c1));
162*385cc6b4SJerry Jelinek 
163*385cc6b4SJerry Jelinek     return (c1 - c2);
164*385cc6b4SJerry Jelinek }
165*385cc6b4SJerry Jelinek 
166*385cc6b4SJerry Jelinek 
167*385cc6b4SJerry Jelinek #if defined (ACPI_DEBUGGER) || defined (ACPI_APPLICATION)
168*385cc6b4SJerry Jelinek /*******************************************************************************
169*385cc6b4SJerry Jelinek  *
170*385cc6b4SJerry Jelinek  * FUNCTION:    AcpiUtSafeStrcpy, AcpiUtSafeStrcat, AcpiUtSafeStrncat
171*385cc6b4SJerry Jelinek  *
172*385cc6b4SJerry Jelinek  * PARAMETERS:  Adds a "DestSize" parameter to each of the standard string
173*385cc6b4SJerry Jelinek  *              functions. This is the size of the Destination buffer.
174*385cc6b4SJerry Jelinek  *
175*385cc6b4SJerry Jelinek  * RETURN:      TRUE if the operation would overflow the destination buffer.
176*385cc6b4SJerry Jelinek  *
177*385cc6b4SJerry Jelinek  * DESCRIPTION: Safe versions of standard Clib string functions. Ensure that
178*385cc6b4SJerry Jelinek  *              the result of the operation will not overflow the output string
179*385cc6b4SJerry Jelinek  *              buffer.
180*385cc6b4SJerry Jelinek  *
181*385cc6b4SJerry Jelinek  * NOTE:        These functions are typically only helpful for processing
182*385cc6b4SJerry Jelinek  *              user input and command lines. For most ACPICA code, the
183*385cc6b4SJerry Jelinek  *              required buffer length is precisely calculated before buffer
184*385cc6b4SJerry Jelinek  *              allocation, so the use of these functions is unnecessary.
185*385cc6b4SJerry Jelinek  *
186*385cc6b4SJerry Jelinek  ******************************************************************************/
187*385cc6b4SJerry Jelinek 
188*385cc6b4SJerry Jelinek BOOLEAN
AcpiUtSafeStrcpy(char * Dest,ACPI_SIZE DestSize,char * Source)189*385cc6b4SJerry Jelinek AcpiUtSafeStrcpy (
190*385cc6b4SJerry Jelinek     char                    *Dest,
191*385cc6b4SJerry Jelinek     ACPI_SIZE               DestSize,
192*385cc6b4SJerry Jelinek     char                    *Source)
193*385cc6b4SJerry Jelinek {
194*385cc6b4SJerry Jelinek 
195*385cc6b4SJerry Jelinek     if (strlen (Source) >= DestSize)
196*385cc6b4SJerry Jelinek     {
197*385cc6b4SJerry Jelinek         return (TRUE);
198*385cc6b4SJerry Jelinek     }
199*385cc6b4SJerry Jelinek 
200*385cc6b4SJerry Jelinek     strcpy (Dest, Source);
201*385cc6b4SJerry Jelinek     return (FALSE);
202*385cc6b4SJerry Jelinek }
203*385cc6b4SJerry Jelinek 
204*385cc6b4SJerry Jelinek BOOLEAN
AcpiUtSafeStrcat(char * Dest,ACPI_SIZE DestSize,char * Source)205*385cc6b4SJerry Jelinek AcpiUtSafeStrcat (
206*385cc6b4SJerry Jelinek     char                    *Dest,
207*385cc6b4SJerry Jelinek     ACPI_SIZE               DestSize,
208*385cc6b4SJerry Jelinek     char                    *Source)
209*385cc6b4SJerry Jelinek {
210*385cc6b4SJerry Jelinek 
211*385cc6b4SJerry Jelinek     if ((strlen (Dest) + strlen (Source)) >= DestSize)
212*385cc6b4SJerry Jelinek     {
213*385cc6b4SJerry Jelinek         return (TRUE);
214*385cc6b4SJerry Jelinek     }
215*385cc6b4SJerry Jelinek 
216*385cc6b4SJerry Jelinek     strcat (Dest, Source);
217*385cc6b4SJerry Jelinek     return (FALSE);
218*385cc6b4SJerry Jelinek }
219*385cc6b4SJerry Jelinek 
220*385cc6b4SJerry Jelinek BOOLEAN
AcpiUtSafeStrncat(char * Dest,ACPI_SIZE DestSize,char * Source,ACPI_SIZE MaxTransferLength)221*385cc6b4SJerry Jelinek AcpiUtSafeStrncat (
222*385cc6b4SJerry Jelinek     char                    *Dest,
223*385cc6b4SJerry Jelinek     ACPI_SIZE               DestSize,
224*385cc6b4SJerry Jelinek     char                    *Source,
225*385cc6b4SJerry Jelinek     ACPI_SIZE               MaxTransferLength)
226*385cc6b4SJerry Jelinek {
227*385cc6b4SJerry Jelinek     ACPI_SIZE               ActualTransferLength;
228*385cc6b4SJerry Jelinek 
229*385cc6b4SJerry Jelinek 
230*385cc6b4SJerry Jelinek     ActualTransferLength = ACPI_MIN (MaxTransferLength, strlen (Source));
231*385cc6b4SJerry Jelinek 
232*385cc6b4SJerry Jelinek     if ((strlen (Dest) + ActualTransferLength) >= DestSize)
233*385cc6b4SJerry Jelinek     {
234*385cc6b4SJerry Jelinek         return (TRUE);
235*385cc6b4SJerry Jelinek     }
236*385cc6b4SJerry Jelinek 
237*385cc6b4SJerry Jelinek     strncat (Dest, Source, MaxTransferLength);
238*385cc6b4SJerry Jelinek     return (FALSE);
239*385cc6b4SJerry Jelinek }
240*385cc6b4SJerry Jelinek #endif
241*385cc6b4SJerry Jelinek 
242*385cc6b4SJerry Jelinek 
243*385cc6b4SJerry Jelinek /*******************************************************************************
244*385cc6b4SJerry Jelinek  *
245*385cc6b4SJerry Jelinek  * FUNCTION:    AcpiUtStrtoul64
246*385cc6b4SJerry Jelinek  *
247*385cc6b4SJerry Jelinek  * PARAMETERS:  String                  - Null terminated string
248*385cc6b4SJerry Jelinek  *              Base                    - Radix of the string: 16 or 10 or
249*385cc6b4SJerry Jelinek  *                                        ACPI_ANY_BASE
250*385cc6b4SJerry Jelinek  *              MaxIntegerByteWidth     - Maximum allowable integer,in bytes:
251*385cc6b4SJerry Jelinek  *                                        4 or 8 (32 or 64 bits)
252*385cc6b4SJerry Jelinek  *              RetInteger              - Where the converted integer is
253*385cc6b4SJerry Jelinek  *                                        returned
254*385cc6b4SJerry Jelinek  *
255*385cc6b4SJerry Jelinek  * RETURN:      Status and Converted value
256*385cc6b4SJerry Jelinek  *
257*385cc6b4SJerry Jelinek  * DESCRIPTION: Convert a string into an unsigned value. Performs either a
258*385cc6b4SJerry Jelinek  *              32-bit or 64-bit conversion, depending on the input integer
259*385cc6b4SJerry Jelinek  *              size (often the current mode of the interpreter).
260*385cc6b4SJerry Jelinek  *
261*385cc6b4SJerry Jelinek  * NOTES:       Negative numbers are not supported, as they are not supported
262*385cc6b4SJerry Jelinek  *              by ACPI.
263*385cc6b4SJerry Jelinek  *
264*385cc6b4SJerry Jelinek  *              AcpiGbl_IntegerByteWidth should be set to the proper width.
265*385cc6b4SJerry Jelinek  *              For the core ACPICA code, this width depends on the DSDT
266*385cc6b4SJerry Jelinek  *              version. For iASL, the default byte width is always 8 for the
267*385cc6b4SJerry Jelinek  *              parser, but error checking is performed later to flag cases
268*385cc6b4SJerry Jelinek  *              where a 64-bit constant is defined in a 32-bit DSDT/SSDT.
269*385cc6b4SJerry Jelinek  *
270*385cc6b4SJerry Jelinek  *              Does not support Octal strings, not needed at this time.
271*385cc6b4SJerry Jelinek  *
272*385cc6b4SJerry Jelinek  ******************************************************************************/
273*385cc6b4SJerry Jelinek 
274*385cc6b4SJerry Jelinek ACPI_STATUS
AcpiUtStrtoul64(char * String,UINT32 Base,UINT32 MaxIntegerByteWidth,UINT64 * RetInteger)275*385cc6b4SJerry Jelinek AcpiUtStrtoul64 (
276*385cc6b4SJerry Jelinek     char                    *String,
277*385cc6b4SJerry Jelinek     UINT32                  Base,
278*385cc6b4SJerry Jelinek     UINT32                  MaxIntegerByteWidth,
279*385cc6b4SJerry Jelinek     UINT64                  *RetInteger)
280*385cc6b4SJerry Jelinek {
281*385cc6b4SJerry Jelinek     UINT32                  ThisDigit = 0;
282*385cc6b4SJerry Jelinek     UINT64                  ReturnValue = 0;
283*385cc6b4SJerry Jelinek     UINT64                  Quotient;
284*385cc6b4SJerry Jelinek     UINT64                  Dividend;
285*385cc6b4SJerry Jelinek     UINT8                   ValidDigits = 0;
286*385cc6b4SJerry Jelinek     UINT8                   SignOf0x = 0;
287*385cc6b4SJerry Jelinek     UINT8                   Term = 0;
288*385cc6b4SJerry Jelinek 
289*385cc6b4SJerry Jelinek 
290*385cc6b4SJerry Jelinek     ACPI_FUNCTION_TRACE_STR (UtStrtoul64, String);
291*385cc6b4SJerry Jelinek 
292*385cc6b4SJerry Jelinek 
293*385cc6b4SJerry Jelinek     switch (Base)
294*385cc6b4SJerry Jelinek     {
295*385cc6b4SJerry Jelinek     case ACPI_ANY_BASE:
296*385cc6b4SJerry Jelinek     case 10:
297*385cc6b4SJerry Jelinek     case 16:
298*385cc6b4SJerry Jelinek 
299*385cc6b4SJerry Jelinek         break;
300*385cc6b4SJerry Jelinek 
301*385cc6b4SJerry Jelinek     default:
302*385cc6b4SJerry Jelinek 
303*385cc6b4SJerry Jelinek         /* Invalid Base */
304*385cc6b4SJerry Jelinek 
305*385cc6b4SJerry Jelinek         return_ACPI_STATUS (AE_BAD_PARAMETER);
306*385cc6b4SJerry Jelinek     }
307*385cc6b4SJerry Jelinek 
308*385cc6b4SJerry Jelinek     if (!String)
309*385cc6b4SJerry Jelinek     {
310*385cc6b4SJerry Jelinek         goto ErrorExit;
311*385cc6b4SJerry Jelinek     }
312*385cc6b4SJerry Jelinek 
313*385cc6b4SJerry Jelinek     /* Skip over any white space in the buffer */
314*385cc6b4SJerry Jelinek 
315*385cc6b4SJerry Jelinek     while ((*String) && (isspace ((int) *String) || *String == '\t'))
316*385cc6b4SJerry Jelinek     {
317*385cc6b4SJerry Jelinek         String++;
318*385cc6b4SJerry Jelinek     }
319*385cc6b4SJerry Jelinek 
320*385cc6b4SJerry Jelinek     if (Base == ACPI_ANY_BASE)
321*385cc6b4SJerry Jelinek     {
322*385cc6b4SJerry Jelinek         /*
323*385cc6b4SJerry Jelinek          * Base equal to ACPI_ANY_BASE means 'Either decimal or hex'.
324*385cc6b4SJerry Jelinek          * We need to determine if it is decimal or hexadecimal.
325*385cc6b4SJerry Jelinek          */
326*385cc6b4SJerry Jelinek         if ((*String == '0') && (tolower ((int) *(String + 1)) == 'x'))
327*385cc6b4SJerry Jelinek         {
328*385cc6b4SJerry Jelinek             SignOf0x = 1;
329*385cc6b4SJerry Jelinek             Base = 16;
330*385cc6b4SJerry Jelinek 
331*385cc6b4SJerry Jelinek             /* Skip over the leading '0x' */
332*385cc6b4SJerry Jelinek             String += 2;
333*385cc6b4SJerry Jelinek         }
334*385cc6b4SJerry Jelinek         else
335*385cc6b4SJerry Jelinek         {
336*385cc6b4SJerry Jelinek             Base = 10;
337*385cc6b4SJerry Jelinek         }
338*385cc6b4SJerry Jelinek     }
339*385cc6b4SJerry Jelinek 
340*385cc6b4SJerry Jelinek     /* Any string left? Check that '0x' is not followed by white space. */
341*385cc6b4SJerry Jelinek 
342*385cc6b4SJerry Jelinek     if (!(*String) || isspace ((int) *String) || *String == '\t')
343*385cc6b4SJerry Jelinek     {
344*385cc6b4SJerry Jelinek         if (Base == ACPI_ANY_BASE)
345*385cc6b4SJerry Jelinek         {
346*385cc6b4SJerry Jelinek             goto ErrorExit;
347*385cc6b4SJerry Jelinek         }
348*385cc6b4SJerry Jelinek         else
349*385cc6b4SJerry Jelinek         {
350*385cc6b4SJerry Jelinek             goto AllDone;
351*385cc6b4SJerry Jelinek         }
352*385cc6b4SJerry Jelinek     }
353*385cc6b4SJerry Jelinek 
354*385cc6b4SJerry Jelinek     /*
355*385cc6b4SJerry Jelinek      * Perform a 32-bit or 64-bit conversion, depending upon the input
356*385cc6b4SJerry Jelinek      * byte width
357*385cc6b4SJerry Jelinek      */
358*385cc6b4SJerry Jelinek     Dividend = (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH) ?
359*385cc6b4SJerry Jelinek         ACPI_UINT32_MAX : ACPI_UINT64_MAX;
360*385cc6b4SJerry Jelinek 
361*385cc6b4SJerry Jelinek     /* Main loop: convert the string to a 32- or 64-bit integer */
362*385cc6b4SJerry Jelinek 
363*385cc6b4SJerry Jelinek     while (*String)
364*385cc6b4SJerry Jelinek     {
365*385cc6b4SJerry Jelinek         if (isdigit ((int) *String))
366*385cc6b4SJerry Jelinek         {
367*385cc6b4SJerry Jelinek             /* Convert ASCII 0-9 to Decimal value */
368*385cc6b4SJerry Jelinek 
369*385cc6b4SJerry Jelinek             ThisDigit = ((UINT8) *String) - '0';
370*385cc6b4SJerry Jelinek         }
371*385cc6b4SJerry Jelinek         else if (Base == 10)
372*385cc6b4SJerry Jelinek         {
373*385cc6b4SJerry Jelinek             /* Digit is out of range; possible in ToInteger case only */
374*385cc6b4SJerry Jelinek 
375*385cc6b4SJerry Jelinek             Term = 1;
376*385cc6b4SJerry Jelinek         }
377*385cc6b4SJerry Jelinek         else
378*385cc6b4SJerry Jelinek         {
379*385cc6b4SJerry Jelinek             ThisDigit = (UINT8) toupper ((int) *String);
380*385cc6b4SJerry Jelinek             if (isxdigit ((int) ThisDigit))
381*385cc6b4SJerry Jelinek             {
382*385cc6b4SJerry Jelinek                 /* Convert ASCII Hex char to value */
383*385cc6b4SJerry Jelinek 
384*385cc6b4SJerry Jelinek                 ThisDigit = ThisDigit - 'A' + 10;
385*385cc6b4SJerry Jelinek             }
386*385cc6b4SJerry Jelinek             else
387*385cc6b4SJerry Jelinek             {
388*385cc6b4SJerry Jelinek                 Term = 1;
389*385cc6b4SJerry Jelinek             }
390*385cc6b4SJerry Jelinek         }
391*385cc6b4SJerry Jelinek 
392*385cc6b4SJerry Jelinek         if (Term)
393*385cc6b4SJerry Jelinek         {
394*385cc6b4SJerry Jelinek             if (Base == ACPI_ANY_BASE)
395*385cc6b4SJerry Jelinek             {
396*385cc6b4SJerry Jelinek                 goto ErrorExit;
397*385cc6b4SJerry Jelinek             }
398*385cc6b4SJerry Jelinek             else
399*385cc6b4SJerry Jelinek             {
400*385cc6b4SJerry Jelinek                 break;
401*385cc6b4SJerry Jelinek             }
402*385cc6b4SJerry Jelinek         }
403*385cc6b4SJerry Jelinek         else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
404*385cc6b4SJerry Jelinek         {
405*385cc6b4SJerry Jelinek             /* Skip zeros */
406*385cc6b4SJerry Jelinek             String++;
407*385cc6b4SJerry Jelinek             continue;
408*385cc6b4SJerry Jelinek         }
409*385cc6b4SJerry Jelinek 
410*385cc6b4SJerry Jelinek         ValidDigits++;
411*385cc6b4SJerry Jelinek 
412*385cc6b4SJerry Jelinek         if (SignOf0x && ((ValidDigits > 16) ||
413*385cc6b4SJerry Jelinek             ((ValidDigits > 8) && (MaxIntegerByteWidth <= ACPI_MAX32_BYTE_WIDTH))))
414*385cc6b4SJerry Jelinek         {
415*385cc6b4SJerry Jelinek             /*
416*385cc6b4SJerry Jelinek              * This is ToInteger operation case.
417*385cc6b4SJerry Jelinek              * No restrictions for string-to-integer conversion,
418*385cc6b4SJerry Jelinek              * see ACPI spec.
419*385cc6b4SJerry Jelinek              */
420*385cc6b4SJerry Jelinek             goto ErrorExit;
421*385cc6b4SJerry Jelinek         }
422*385cc6b4SJerry Jelinek 
423*385cc6b4SJerry Jelinek         /* Divide the digit into the correct position */
424*385cc6b4SJerry Jelinek 
425*385cc6b4SJerry Jelinek         (void) AcpiUtShortDivide (
426*385cc6b4SJerry Jelinek             (Dividend - (UINT64) ThisDigit), Base, &Quotient, NULL);
427*385cc6b4SJerry Jelinek 
428*385cc6b4SJerry Jelinek         if (ReturnValue > Quotient)
429*385cc6b4SJerry Jelinek         {
430*385cc6b4SJerry Jelinek             if (Base == ACPI_ANY_BASE)
431*385cc6b4SJerry Jelinek             {
432*385cc6b4SJerry Jelinek                 goto ErrorExit;
433*385cc6b4SJerry Jelinek             }
434*385cc6b4SJerry Jelinek             else
435*385cc6b4SJerry Jelinek             {
436*385cc6b4SJerry Jelinek                 break;
437*385cc6b4SJerry Jelinek             }
438*385cc6b4SJerry Jelinek         }
439*385cc6b4SJerry Jelinek 
440*385cc6b4SJerry Jelinek         ReturnValue *= Base;
441*385cc6b4SJerry Jelinek         ReturnValue += ThisDigit;
442*385cc6b4SJerry Jelinek         String++;
443*385cc6b4SJerry Jelinek     }
444*385cc6b4SJerry Jelinek 
445*385cc6b4SJerry Jelinek     /* All done, normal exit */
446*385cc6b4SJerry Jelinek 
447*385cc6b4SJerry Jelinek AllDone:
448*385cc6b4SJerry Jelinek 
449*385cc6b4SJerry Jelinek     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
450*385cc6b4SJerry Jelinek         ACPI_FORMAT_UINT64 (ReturnValue)));
451*385cc6b4SJerry Jelinek 
452*385cc6b4SJerry Jelinek     *RetInteger = ReturnValue;
453*385cc6b4SJerry Jelinek     return_ACPI_STATUS (AE_OK);
454*385cc6b4SJerry Jelinek 
455*385cc6b4SJerry Jelinek 
456*385cc6b4SJerry Jelinek ErrorExit:
457*385cc6b4SJerry Jelinek 
458*385cc6b4SJerry Jelinek     /* Base was set/validated above (10 or 16) */
459*385cc6b4SJerry Jelinek 
460*385cc6b4SJerry Jelinek     if (Base == 10)
461*385cc6b4SJerry Jelinek     {
462*385cc6b4SJerry Jelinek         return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
463*385cc6b4SJerry Jelinek     }
464*385cc6b4SJerry Jelinek     else
465*385cc6b4SJerry Jelinek     {
466*385cc6b4SJerry Jelinek         return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
467*385cc6b4SJerry Jelinek     }
468*385cc6b4SJerry Jelinek }
469*385cc6b4SJerry Jelinek 
470*385cc6b4SJerry Jelinek 
471*385cc6b4SJerry Jelinek #ifdef _OBSOLETE_FUNCTIONS
472*385cc6b4SJerry Jelinek /* Removed: 01/2016 */
473*385cc6b4SJerry Jelinek 
474*385cc6b4SJerry Jelinek /*******************************************************************************
475*385cc6b4SJerry Jelinek  *
476*385cc6b4SJerry Jelinek  * FUNCTION:    strtoul64
477*385cc6b4SJerry Jelinek  *
478*385cc6b4SJerry Jelinek  * PARAMETERS:  String              - Null terminated string
479*385cc6b4SJerry Jelinek  *              Terminater          - Where a pointer to the terminating byte
480*385cc6b4SJerry Jelinek  *                                    is returned
481*385cc6b4SJerry Jelinek  *              Base                - Radix of the string
482*385cc6b4SJerry Jelinek  *
483*385cc6b4SJerry Jelinek  * RETURN:      Converted value
484*385cc6b4SJerry Jelinek  *
485*385cc6b4SJerry Jelinek  * DESCRIPTION: Convert a string into an unsigned value.
486*385cc6b4SJerry Jelinek  *
487*385cc6b4SJerry Jelinek  ******************************************************************************/
488*385cc6b4SJerry Jelinek 
489*385cc6b4SJerry Jelinek ACPI_STATUS
strtoul64(char * String,UINT32 Base,UINT64 * RetInteger)490*385cc6b4SJerry Jelinek strtoul64 (
491*385cc6b4SJerry Jelinek     char                    *String,
492*385cc6b4SJerry Jelinek     UINT32                  Base,
493*385cc6b4SJerry Jelinek     UINT64                  *RetInteger)
494*385cc6b4SJerry Jelinek {
495*385cc6b4SJerry Jelinek     UINT32                  Index;
496*385cc6b4SJerry Jelinek     UINT32                  Sign;
497*385cc6b4SJerry Jelinek     UINT64                  ReturnValue = 0;
498*385cc6b4SJerry Jelinek     ACPI_STATUS             Status = AE_OK;
499*385cc6b4SJerry Jelinek 
500*385cc6b4SJerry Jelinek 
501*385cc6b4SJerry Jelinek     *RetInteger = 0;
502*385cc6b4SJerry Jelinek 
503*385cc6b4SJerry Jelinek     switch (Base)
504*385cc6b4SJerry Jelinek     {
505*385cc6b4SJerry Jelinek     case 0:
506*385cc6b4SJerry Jelinek     case 8:
507*385cc6b4SJerry Jelinek     case 10:
508*385cc6b4SJerry Jelinek     case 16:
509*385cc6b4SJerry Jelinek 
510*385cc6b4SJerry Jelinek         break;
511*385cc6b4SJerry Jelinek 
512*385cc6b4SJerry Jelinek     default:
513*385cc6b4SJerry Jelinek         /*
514*385cc6b4SJerry Jelinek          * The specified Base parameter is not in the domain of
515*385cc6b4SJerry Jelinek          * this function:
516*385cc6b4SJerry Jelinek          */
517*385cc6b4SJerry Jelinek         return (AE_BAD_PARAMETER);
518*385cc6b4SJerry Jelinek     }
519*385cc6b4SJerry Jelinek 
520*385cc6b4SJerry Jelinek     /* Skip over any white space in the buffer: */
521*385cc6b4SJerry Jelinek 
522*385cc6b4SJerry Jelinek     while (isspace ((int) *String) || *String == '\t')
523*385cc6b4SJerry Jelinek     {
524*385cc6b4SJerry Jelinek         ++String;
525*385cc6b4SJerry Jelinek     }
526*385cc6b4SJerry Jelinek 
527*385cc6b4SJerry Jelinek     /*
528*385cc6b4SJerry Jelinek      * The buffer may contain an optional plus or minus sign.
529*385cc6b4SJerry Jelinek      * If it does, then skip over it but remember what is was:
530*385cc6b4SJerry Jelinek      */
531*385cc6b4SJerry Jelinek     if (*String == '-')
532*385cc6b4SJerry Jelinek     {
533*385cc6b4SJerry Jelinek         Sign = ACPI_SIGN_NEGATIVE;
534*385cc6b4SJerry Jelinek         ++String;
535*385cc6b4SJerry Jelinek     }
536*385cc6b4SJerry Jelinek     else if (*String == '+')
537*385cc6b4SJerry Jelinek     {
538*385cc6b4SJerry Jelinek         ++String;
539*385cc6b4SJerry Jelinek         Sign = ACPI_SIGN_POSITIVE;
540*385cc6b4SJerry Jelinek     }
541*385cc6b4SJerry Jelinek     else
542*385cc6b4SJerry Jelinek     {
543*385cc6b4SJerry Jelinek         Sign = ACPI_SIGN_POSITIVE;
544*385cc6b4SJerry Jelinek     }
545*385cc6b4SJerry Jelinek 
546*385cc6b4SJerry Jelinek     /*
547*385cc6b4SJerry Jelinek      * If the input parameter Base is zero, then we need to
548*385cc6b4SJerry Jelinek      * determine if it is octal, decimal, or hexadecimal:
549*385cc6b4SJerry Jelinek      */
550*385cc6b4SJerry Jelinek     if (Base == 0)
551*385cc6b4SJerry Jelinek     {
552*385cc6b4SJerry Jelinek         if (*String == '0')
553*385cc6b4SJerry Jelinek         {
554*385cc6b4SJerry Jelinek             if (tolower ((int) *(++String)) == 'x')
555*385cc6b4SJerry Jelinek             {
556*385cc6b4SJerry Jelinek                 Base = 16;
557*385cc6b4SJerry Jelinek                 ++String;
558*385cc6b4SJerry Jelinek             }
559*385cc6b4SJerry Jelinek             else
560*385cc6b4SJerry Jelinek             {
561*385cc6b4SJerry Jelinek                 Base = 8;
562*385cc6b4SJerry Jelinek             }
563*385cc6b4SJerry Jelinek         }
564*385cc6b4SJerry Jelinek         else
565*385cc6b4SJerry Jelinek         {
566*385cc6b4SJerry Jelinek             Base = 10;
567*385cc6b4SJerry Jelinek         }
568*385cc6b4SJerry Jelinek     }
569*385cc6b4SJerry Jelinek 
570*385cc6b4SJerry Jelinek     /*
571*385cc6b4SJerry Jelinek      * For octal and hexadecimal bases, skip over the leading
572*385cc6b4SJerry Jelinek      * 0 or 0x, if they are present.
573*385cc6b4SJerry Jelinek      */
574*385cc6b4SJerry Jelinek     if (Base == 8 && *String == '0')
575*385cc6b4SJerry Jelinek     {
576*385cc6b4SJerry Jelinek         String++;
577*385cc6b4SJerry Jelinek     }
578*385cc6b4SJerry Jelinek 
579*385cc6b4SJerry Jelinek     if (Base == 16 &&
580*385cc6b4SJerry Jelinek         *String == '0' &&
581*385cc6b4SJerry Jelinek         tolower ((int) *(++String)) == 'x')
582*385cc6b4SJerry Jelinek     {
583*385cc6b4SJerry Jelinek         String++;
584*385cc6b4SJerry Jelinek     }
585*385cc6b4SJerry Jelinek 
586*385cc6b4SJerry Jelinek     /* Main loop: convert the string to an unsigned long */
587*385cc6b4SJerry Jelinek 
588*385cc6b4SJerry Jelinek     while (*String)
589*385cc6b4SJerry Jelinek     {
590*385cc6b4SJerry Jelinek         if (isdigit ((int) *String))
591*385cc6b4SJerry Jelinek         {
592*385cc6b4SJerry Jelinek             Index = ((UINT8) *String) - '0';
593*385cc6b4SJerry Jelinek         }
594*385cc6b4SJerry Jelinek         else
595*385cc6b4SJerry Jelinek         {
596*385cc6b4SJerry Jelinek             Index = (UINT8) toupper ((int) *String);
597*385cc6b4SJerry Jelinek             if (isupper ((int) Index))
598*385cc6b4SJerry Jelinek             {
599*385cc6b4SJerry Jelinek                 Index = Index - 'A' + 10;
600*385cc6b4SJerry Jelinek             }
601*385cc6b4SJerry Jelinek             else
602*385cc6b4SJerry Jelinek             {
603*385cc6b4SJerry Jelinek                 goto ErrorExit;
604*385cc6b4SJerry Jelinek             }
605*385cc6b4SJerry Jelinek         }
606*385cc6b4SJerry Jelinek 
607*385cc6b4SJerry Jelinek         if (Index >= Base)
608*385cc6b4SJerry Jelinek         {
609*385cc6b4SJerry Jelinek             goto ErrorExit;
610*385cc6b4SJerry Jelinek         }
611*385cc6b4SJerry Jelinek 
612*385cc6b4SJerry Jelinek         /* Check to see if value is out of range: */
613*385cc6b4SJerry Jelinek 
614*385cc6b4SJerry Jelinek         if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
615*385cc6b4SJerry Jelinek             (UINT64) Base))
616*385cc6b4SJerry Jelinek         {
617*385cc6b4SJerry Jelinek             goto ErrorExit;
618*385cc6b4SJerry Jelinek         }
619*385cc6b4SJerry Jelinek         else
620*385cc6b4SJerry Jelinek         {
621*385cc6b4SJerry Jelinek             ReturnValue *= Base;
622*385cc6b4SJerry Jelinek             ReturnValue += Index;
623*385cc6b4SJerry Jelinek         }
624*385cc6b4SJerry Jelinek 
625*385cc6b4SJerry Jelinek         ++String;
626*385cc6b4SJerry Jelinek     }
627*385cc6b4SJerry Jelinek 
628*385cc6b4SJerry Jelinek 
629*385cc6b4SJerry Jelinek     /* If a minus sign was present, then "the conversion is negated": */
630*385cc6b4SJerry Jelinek 
631*385cc6b4SJerry Jelinek     if (Sign == ACPI_SIGN_NEGATIVE)
632*385cc6b4SJerry Jelinek     {
633*385cc6b4SJerry Jelinek         ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
634*385cc6b4SJerry Jelinek     }
635*385cc6b4SJerry Jelinek 
636*385cc6b4SJerry Jelinek     *RetInteger = ReturnValue;
637*385cc6b4SJerry Jelinek     return (Status);
638*385cc6b4SJerry Jelinek 
639*385cc6b4SJerry Jelinek 
640*385cc6b4SJerry Jelinek ErrorExit:
641*385cc6b4SJerry Jelinek     switch (Base)
642*385cc6b4SJerry Jelinek     {
643*385cc6b4SJerry Jelinek     case 8:
644*385cc6b4SJerry Jelinek 
645*385cc6b4SJerry Jelinek         Status = AE_BAD_OCTAL_CONSTANT;
646*385cc6b4SJerry Jelinek         break;
647*385cc6b4SJerry Jelinek 
648*385cc6b4SJerry Jelinek     case 10:
649*385cc6b4SJerry Jelinek 
650*385cc6b4SJerry Jelinek         Status = AE_BAD_DECIMAL_CONSTANT;
651*385cc6b4SJerry Jelinek         break;
652*385cc6b4SJerry Jelinek 
653*385cc6b4SJerry Jelinek     case 16:
654*385cc6b4SJerry Jelinek 
655*385cc6b4SJerry Jelinek         Status = AE_BAD_HEX_CONSTANT;
656*385cc6b4SJerry Jelinek         break;
657*385cc6b4SJerry Jelinek 
658*385cc6b4SJerry Jelinek     default:
659*385cc6b4SJerry Jelinek 
660*385cc6b4SJerry Jelinek         /* Base validated above */
661*385cc6b4SJerry Jelinek 
662*385cc6b4SJerry Jelinek         break;
663*385cc6b4SJerry Jelinek     }
664*385cc6b4SJerry Jelinek 
665*385cc6b4SJerry Jelinek     return (Status);
666*385cc6b4SJerry Jelinek }
667*385cc6b4SJerry Jelinek #endif
668